GeographicLib  1.50.1
NormalGravity.cpp
Go to the documentation of this file.
1 /**
2  * \file NormalGravity.cpp
3  * \brief Implementation for GeographicLib::NormalGravity class
4  *
5  * Copyright (c) Charles Karney (2011-2018) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
8  **********************************************************************/
9 
11 
12 #if defined(_MSC_VER)
13 // Squelch warnings about constant conditional expressions
14 # pragma warning (disable: 4127)
15 #endif
16 
17 namespace GeographicLib {
18 
19  using namespace std;
20 
21  void NormalGravity::Initialize(real a, real GM, real omega, real f_J2,
22  bool geometricp) {
23  _a = a;
24  if (!(Math::isfinite(_a) && _a > 0))
25  throw GeographicErr("Equatorial radius is not positive");
26  _GM = GM;
27  if (!Math::isfinite(_GM))
28  throw GeographicErr("Gravitational constant is not finite");
29  _omega = omega;
30  _omega2 = Math::sq(_omega);
31  _aomega2 = Math::sq(_omega * _a);
32  if (!(Math::isfinite(_omega2) && Math::isfinite(_aomega2)))
33  throw GeographicErr("Rotation velocity is not finite");
34  _f = geometricp ? f_J2 : J2ToFlattening(_a, _GM, _omega, f_J2);
35  _b = _a * (1 - _f);
36  if (!(Math::isfinite(_b) && _b > 0))
37  throw GeographicErr("Polar semi-axis is not positive");
38  _J2 = geometricp ? FlatteningToJ2(_a, _GM, _omega, f_J2) : f_J2;
39  _e2 = _f * (2 - _f);
40  _ep2 = _e2 / (1 - _e2);
41  real ex2 = _f < 0 ? -_e2 : _ep2;
42  _Q0 = Qf(ex2, _f < 0);
43  _earth = Geocentric(_a, _f);
44  _E = _a * sqrt(abs(_e2)); // H+M, Eq 2-54
45  // H+M, Eq 2-61
46  _U0 = _GM * atanzz(ex2, _f < 0) / _b + _aomega2 / 3;
47  real P = Hf(ex2, _f < 0) / (6 * _Q0);
48  // H+M, Eq 2-73
49  _gammae = _GM / (_a * _b) - (1 + P) * _a * _omega2;
50  // H+M, Eq 2-74
51  _gammap = _GM / (_a * _a) + 2 * P * _b * _omega2;
52  // k = gammae * (b * gammap / (a * gammae) - 1)
53  // = (b * gammap - a * gammae) / a
54  _k = -_e2 * _GM / (_a * _b) +
55  _omega2 * (P * (_a + 2 * _b * (1 - _f)) + _a);
56  // f* = (gammap - gammae) / gammae
57  _fstar = (-_f * _GM / (_a * _b) + _omega2 * (P * (_a + 2 * _b) + _a)) /
58  _gammae;
59  }
60 
61  NormalGravity::NormalGravity(real a, real GM, real omega, real f_J2,
62  bool geometricp) {
63  Initialize(a, GM, omega, f_J2, geometricp);
64  }
65 
67  static const NormalGravity wgs84(Constants::WGS84_a(),
70  Constants::WGS84_f(), true);
71  return wgs84;
72  }
73 
75  static const NormalGravity grs80(Constants::GRS80_a(),
78  Constants::GRS80_J2(), false);
79  return grs80;
80  }
81 
82  Math::real NormalGravity::atan7series(real x) {
83  // compute -sum( (-x)^n/(2*n+7), n, 0, inf)
84  // = -1/7 + x/9 - x^2/11 + x^3/13 ...
85  // = (atan(sqrt(x))/sqrt(x)-(1-x/3+x^2/5)) / x^3 (x > 0)
86  // = (atanh(sqrt(-x))/sqrt(-x)-(1-x/3+x^2/5)) / x^3 (x < 0)
87  // require abs(x) < 1/2, but better to restrict calls to abs(x) < 1/4
88  static const real lg2eps_ =
89  -log(numeric_limits<real>::epsilon() / 2) / log(real(2));
90  int e;
91  frexp(x, &e);
92  e = max(-e, 1); // Here's where abs(x) < 1/2 is assumed
93  // x = [0.5,1) * 2^(-e)
94  // estimate n s.t. x^n/n < 1/7 * epsilon/2
95  // a stronger condition is x^n < epsilon/2
96  // taking log2 of both sides, a stronger condition is n*(-e) < -lg2eps;
97  // or n*e > lg2eps or n > ceiling(lg2eps/e)
98  int n = int(ceil(lg2eps_ / e));
99  Math::real v = 0;
100  while (n--) // iterating from n-1 down to 0
101  v = - x * v - 1/Math::real(2*n + 7);
102  return v;
103  }
104 
105  Math::real NormalGravity::atan5series(real x) {
106  // Compute Taylor series approximations to
107  // (atan(z)-(z-z^3/3))/z^5,
108  // z = sqrt(x)
109  // require abs(x) < 1/2, but better to restrict calls to abs(x) < 1/4
110  return 1/real(5) + x * atan7series(x);
111  }
112 
113  Math::real NormalGravity::Qf(real x, bool alt) {
114  // Compute
115  // Q(z) = (((1 + 3/z^2) * atan(z) - 3/z)/2) / z^3
116  // = q(z)/z^3 with q(z) defined by H+M, Eq 2-57 with z = E/u
117  // z = sqrt(x)
118  real y = alt ? -x / (1 + x) : x;
119  return !(4 * abs(y) < 1) ? // Backwards test to allow NaNs through
120  ((1 + 3/y) * atanzz(x, alt) - 3/y) / (2 * y) :
121  (3 * (3 + y) * atan5series(y) - 1) / 6;
122  }
123 
124  Math::real NormalGravity::Hf(real x, bool alt) {
125  // z = sqrt(x)
126  // Compute
127  // H(z) = (3*Q(z)+z*diff(Q(z),z))*(1+z^2)
128  // = (3 * (1 + 1/z^2) * (1 - atan(z)/z) - 1) / z^2
129  // = q'(z)/z^2, with q'(z) defined by H+M, Eq 2-67, with z = E/u
130  real y = alt ? -x / (1 + x) : x;
131  return !(4 * abs(y) < 1) ? // Backwards test to allow NaNs through
132  (3 * (1 + 1/y) * (1 - atanzz(x, alt)) - 1) / y :
133  1 - 3 * (1 + y) * atan5series(y);
134  }
135 
136  Math::real NormalGravity::QH3f(real x, bool alt) {
137  // z = sqrt(x)
138  // (Q(z) - H(z)/3) / z^2
139  // = - (1+z^2)/(3*z) * d(Q(z))/dz - Q(z)
140  // = ((15+9*z^2)*atan(z)-4*z^3-15*z)/(6*z^7)
141  // = ((25+15*z^2)*atan7+3)/10
142  real y = alt ? -x / (1 + x) : x;
143  return !(4 * abs(y) < 1) ? // Backwards test to allow NaNs through
144  ((9 + 15/y) * atanzz(x, alt) - 4 - 15/y) / (6 * Math::sq(y)) :
145  ((25 + 15*y) * atan7series(y) + 3)/10;
146  }
147 
148  Math::real NormalGravity::Jn(int n) const {
149  // Note Jn(0) = -1; Jn(2) = _J2; Jn(odd) = 0
150  if (n & 1 || n < 0)
151  return 0;
152  n /= 2;
153  real e2n = 1; // Perhaps this should just be e2n = pow(-_e2, n);
154  for (int j = n; j--;)
155  e2n *= -_e2;
156  return // H+M, Eq 2-92
157  -3 * e2n * ((1 - n) + 5 * n * _J2 / _e2) / ((2 * n + 1) * (2 * n + 3));
158  }
159 
161  real sphi = Math::sind(Math::LatFix(lat));
162  // H+M, Eq 2-78
163  return (_gammae + _k * Math::sq(sphi)) / sqrt(1 - _e2 * Math::sq(sphi));
164  }
165 
166  Math::real NormalGravity::V0(real X, real Y, real Z,
167  real& GammaX, real& GammaY, real& GammaZ) const
168  {
169  // See H+M, Sec 6-2
170  real
171  p = Math::hypot(X, Y),
172  clam = p != 0 ? X/p : 1,
173  slam = p != 0 ? Y/p : 0,
174  r = Math::hypot(p, Z);
175  if (_f < 0) swap(p, Z);
176  real
177  Q = Math::sq(r) - Math::sq(_E),
178  t2 = Math::sq(2 * _E * Z),
179  disc = sqrt(Math::sq(Q) + t2),
180  // This is H+M, Eq 6-8a, but generalized to deal with Q negative
181  // accurately.
182  u = sqrt((Q >= 0 ? (Q + disc) : t2 / (disc - Q)) / 2),
183  uE = Math::hypot(u, _E),
184  // H+M, Eq 6-8b
185  sbet = u != 0 ? Z * uE : Math::copysign(sqrt(-Q), Z),
186  cbet = u != 0 ? p * u : p,
187  s = Math::hypot(cbet, sbet);
188  sbet = s != 0 ? sbet/s : 1;
189  cbet = s != 0 ? cbet/s : 0;
190  real
191  z = _E/u,
192  z2 = Math::sq(z),
193  den = Math::hypot(u, _E * sbet);
194  if (_f < 0) {
195  swap(sbet, cbet);
196  swap(u, uE);
197  }
198  real
199  invw = uE / den, // H+M, Eq 2-63
200  bu = _b / (u != 0 || _f < 0 ? u : _E),
201  // Qf(z2->inf, false) = pi/(4*z^3)
202  q = ((u != 0 || _f < 0 ? Qf(z2, _f < 0) : Math::pi() / 4) / _Q0) *
203  bu * Math::sq(bu),
204  qp = _b * Math::sq(bu) * (u != 0 || _f < 0 ? Hf(z2, _f < 0) : 2) / _Q0,
205  ang = (Math::sq(sbet) - 1/real(3)) / 2,
206  // H+M, Eqs 2-62 + 6-9, but omitting last (rotational) term.
207  Vres = _GM * (u != 0 || _f < 0 ?
208  atanzz(z2, _f < 0) / u :
209  Math::pi() / (2 * _E)) + _aomega2 * q * ang,
210  // H+M, Eq 6-10
211  gamu = - (_GM + (_aomega2 * qp * ang)) * invw / Math::sq(uE),
212  gamb = _aomega2 * q * sbet * cbet * invw / uE,
213  t = u * invw / uE,
214  gamp = t * cbet * gamu - invw * sbet * gamb;
215  // H+M, Eq 6-12
216  GammaX = gamp * clam;
217  GammaY = gamp * slam;
218  GammaZ = invw * sbet * gamu + t * cbet * gamb;
219  return Vres;
220  }
221 
222  Math::real NormalGravity::Phi(real X, real Y, real& fX, real& fY) const {
223  fX = _omega2 * X;
224  fY = _omega2 * Y;
225  // N.B. fZ = 0;
226  return _omega2 * (Math::sq(X) + Math::sq(Y)) / 2;
227  }
228 
229  Math::real NormalGravity::U(real X, real Y, real Z,
230  real& gammaX, real& gammaY, real& gammaZ) const {
231  real fX, fY;
232  real Ures = V0(X, Y, Z, gammaX, gammaY, gammaZ) + Phi(X, Y, fX, fY);
233  gammaX += fX;
234  gammaY += fY;
235  return Ures;
236  }
237 
239  real& gammay, real& gammaz) const {
240  real X, Y, Z;
241  real M[Geocentric::dim2_];
242  _earth.IntForward(lat, 0, h, X, Y, Z, M);
243  real gammaX, gammaY, gammaZ,
244  Ures = U(X, Y, Z, gammaX, gammaY, gammaZ);
245  // gammax = M[0] * gammaX + M[3] * gammaY + M[6] * gammaZ;
246  gammay = M[1] * gammaX + M[4] * gammaY + M[7] * gammaZ;
247  gammaz = M[2] * gammaX + M[5] * gammaY + M[8] * gammaZ;
248  return Ures;
249  }
250 
252  real omega, real J2) {
253  // Solve
254  // f = e^2 * (1 - K * e/q0) - 3 * J2 = 0
255  // for e^2 using Newton's method
256  static const real maxe_ = 1 - numeric_limits<real>::epsilon();
257  static const real eps2_ = sqrt(numeric_limits<real>::epsilon()) / 100;
258  real
259  K = 2 * Math::sq(a * omega) * a / (15 * GM),
260  J0 = (1 - 4 * K / Math::pi()) / 3;
261  if (!(GM > 0 && Math::isfinite(K) && K >= 0))
262  return Math::NaN();
263  if (!(Math::isfinite(J2) && J2 <= J0)) return Math::NaN();
264  if (J2 == J0) return 1;
265  // Solve e2 - f1 * f2 * K / Q0 - 3 * J2 = 0 for J2 close to J0;
266  // subst e2 = ep2/(1+ep2), f2 = 1/(1+ep2), f1 = 1/sqrt(1+ep2), J2 = J0-dJ2,
267  // Q0 = pi/(4*z^3) - 2/z^4 + (3*pi)/(4*z^5), z = sqrt(ep2), and balance two
268  // leading terms to give
269  real
270  ep2 = max(Math::sq(32 * K / (3 * Math::sq(Math::pi()) * (J0 - J2))),
271  -maxe_),
272  e2 = min(ep2 / (1 + ep2), maxe_);
273  for (int j = 0; j < maxit_ || GEOGRAPHICLIB_PANIC; ++j) {
274  real
275  e2a = e2, ep2a = ep2,
276  f2 = 1 - e2, // (1 - f)^2
277  f1 = sqrt(f2), // (1 - f)
278  Q0 = Qf(e2 < 0 ? -e2 : ep2, e2 < 0),
279  h = e2 - f1 * f2 * K / Q0 - 3 * J2,
280  dh = 1 - 3 * f1 * K * QH3f(e2 < 0 ? -e2 : ep2, e2 < 0) /
281  (2 * Math::sq(Q0));
282  e2 = min(e2a - h / dh, maxe_);
283  ep2 = max(e2 / (1 - e2), -maxe_);
284  if (abs(h) < eps2_ || e2 == e2a || ep2 == ep2a)
285  break;
286  }
287  return e2 / (1 + sqrt(1 - e2));
288  }
289 
291  real omega, real f) {
292  real
293  K = 2 * Math::sq(a * omega) * a / (15 * GM),
294  f1 = 1 - f,
295  f2 = Math::sq(f1),
296  e2 = f * (2 - f);
297  // H+M, Eq 2-90 + 2-92'
298  return (e2 - K * f1 * f2 / Qf(f < 0 ? -e2 : e2 / f2, f < 0)) / 3;
299  }
300 
301 } // namespace GeographicLib
real
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
GeographicLib::Constants::GRS80_a
static T GRS80_a()
Definition: Constants.hpp:206
GeographicLib
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
GeographicLib::Math::isfinite
static bool isfinite(T x)
Definition: Math.cpp:372
GeographicLib::Math::real
double real
Definition: Math.hpp:121
GeographicLib::Constants::WGS84_omega
static T WGS84_omega()
Definition: Constants.hpp:196
GeographicLib::NormalGravity
The normal gravity of the earth.
Definition: NormalGravity.hpp:79
GEOGRAPHICLIB_PANIC
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:83
GeographicLib::Math::hypot
static T hypot(T x, T y)
Definition: Math.cpp:58
GeographicLib::Constants::WGS84_GM
static T WGS84_GM()
Definition: Constants.hpp:185
GeographicLib::NormalGravity::J2ToFlattening
static Math::real J2ToFlattening(real a, real GM, real omega, real J2)
Definition: NormalGravity.cpp:251
std::swap
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
Definition: NearestNeighbor.hpp:827
GeographicLib::Math::LatFix
static T LatFix(T x)
Definition: Math.hpp:395
GeographicLib::NormalGravity::WGS84
static const NormalGravity & WGS84()
Definition: NormalGravity.cpp:66
GeographicLib::Math::sind
static T sind(T x)
Definition: Math.cpp:280
GeographicLib::NormalGravity::Phi
Math::real Phi(real X, real Y, real &fX, real &fY) const
Definition: NormalGravity.cpp:222
GeographicLib::Math::pi
static T pi()
Definition: Math.hpp:171
GeographicLib::Constants::WGS84_a
static T WGS84_a()
Definition: Constants.hpp:160
GeographicLib::NormalGravity::GRS80
static const NormalGravity & GRS80()
Definition: NormalGravity.cpp:74
GeographicLib::Constants::GRS80_J2
static T GRS80_J2()
Definition: Constants.hpp:246
std
Definition: NearestNeighbor.hpp:814
GeographicLib::Constants::WGS84_f
static T WGS84_f()
Definition: Constants.hpp:170
GeographicLib::Math::copysign
static T copysign(T x, T y)
Definition: Math.cpp:122
NormalGravity.hpp
Header for GeographicLib::NormalGravity class.
GeographicLib::Constants::GRS80_GM
static T GRS80_GM()
Definition: Constants.hpp:217
GeographicLib::NormalGravity::SurfaceGravity
Math::real SurfaceGravity(real lat) const
Definition: NormalGravity.cpp:160
GeographicLib::Constants::GRS80_omega
static T GRS80_omega()
Definition: Constants.hpp:235
GeographicLib::Math::sq
static T sq(T x)
Definition: Math.hpp:201
GeographicLib::NormalGravity::V0
Math::real V0(real X, real Y, real Z, real &GammaX, real &GammaY, real &GammaZ) const
Definition: NormalGravity.cpp:166
GeographicLib::NormalGravity::FlatteningToJ2
static Math::real FlatteningToJ2(real a, real GM, real omega, real f)
Definition: NormalGravity.cpp:290
GeographicLib::NormalGravity::NormalGravity
NormalGravity()
Definition: NormalGravity.hpp:150
GeographicLib::Math::NaN
static T NaN()
Definition: Math.cpp:389
GeographicLib::NormalGravity::Gravity
Math::real Gravity(real lat, real h, real &gammay, real &gammaz) const
Definition: NormalGravity.cpp:238
GeographicLib::NormalGravity::U
Math::real U(real X, real Y, real Z, real &gammaX, real &gammaY, real &gammaZ) const
Definition: NormalGravity.cpp:229