GRALE
monopolebasisfunction.h
1 /*
2 
3  This file is a part of GRALE, a library to facilitate the simulation
4  and inversion of gravitational lenses.
5 
6  Copyright (C) 2008-2012 Jori Liesenborgs
7 
8  Contact: jori.liesenborgs@gmail.com
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 
24 */
25 
26 #ifndef GRALE_MONOPOLEBASISFUNCTION_H
27 
28 #define GRALE_MONOPOLEBASISFUNCTION_H
29 
30 #include "graleconfig.h"
32 
33 namespace grale
34 {
35 
36 class MonopoleBasisFunction : public Real2DDerivableFunction
37 {
38 public:
39  MonopoleBasisFunction(double zeroPoint, double lengthScale)
40  {
41  m_zeroPoint = zeroPoint;
42  m_lengthScale = lengthScale;
43 
44  m_a2 = -1.0/(m_zeroPoint*m_zeroPoint);
45  m_a0 = 1.0;
46 
47  double m1 = m_zeroPoint-1;
48  double factor = (m_zeroPoint*m_zeroPoint)/4.0*(1.0/(m1*m1*m1));
49 
50  m_b1 = factor*(-6.0);
51  m_b0 = factor*6.0*(1.0+m_zeroPoint);
52  m_b_1 = factor*(-6.0)*m_zeroPoint;
53 
54  }
55 
56  ~MonopoleBasisFunction()
57  {
58  }
59 
60  double operator()(Vector2D<double> point) const
61  {
62  Vector2D<double> p = point/m_lengthScale;
63  double x = p.getLength();
64  double val = 0;
65 
66  if (x < m_zeroPoint)
67  {
68  double x2 = x*x;
69 
70  val = m_a2*x2 + m_a0;
71  }
72  else if (x < 1.0)
73  {
74  val = m_b1*x + m_b0 + m_b_1/x;
75  }
76 
77  return val;
78  }
79 
80  Vector2D<double> getGradient(Vector2D<double> point) const
81  {
82  Vector2D<double> p = point/m_lengthScale;
83  double x = p.getLength();
84  double val = 0;
85 
86  if (x < m_zeroPoint)
87  {
88  val = 2.0*m_a2*x;
89  }
90  else if (x < 1.0)
91  {
92  double x2 = x*x;
93 
94  val = m_b1-m_b_1/x2;
95  }
96 
97  if (val != 0)
98  val /= (x*m_lengthScale);
99  return val*p;
100  }
101 
102  double getLengthScale() const
103  {
104  return m_lengthScale;
105  }
106 
107  double getZeroPoint() const
108  {
109  return m_zeroPoint;
110  }
111 private:
112  double m_lengthScale;
113  double m_zeroPoint;
114  double m_a2, m_a0;
115  double m_b1, m_b0, m_b_1;
116 };
117 
118 } // end namespace
119 
120 #endif // GRALE_MONOPOLEBASISFUNCTION_H