GeographicLib  1.48
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-2017) <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) + _omega2 * (P * (_a + 2 * _b * (1 - _f)) + _a);
55  // f* = (gammap - gammae) / gammae
56  _fstar = (-_f * _GM / (_a * _b) + _omega2 * (P * (_a + 2 * _b) + _a)) /
57  _gammae;
58  }
59 
60  NormalGravity::NormalGravity(real a, real GM, real omega, real f_J2,
61  bool geometricp) {
62  Initialize(a, GM, omega, f_J2, geometricp);
63  }
64 
65  NormalGravity::NormalGravity(real a, real GM, real omega, real f, real J2) {
66  if (!(Math::isfinite(GM) && GM > 0))
67  throw GeographicErr("Gravitational constant is not positive");
68  bool geometricp;
69  if (!(omega == 0 && f == 0 && J2 == 0)) {
70  geometricp = f > 0 && Math::isfinite(f);
71  if (J2 > 0 && Math::isfinite(J2) && geometricp)
72  throw GeographicErr("Cannot specify both f and J2");
73  if (!(J2 > 0 && Math::isfinite(J2)) && !geometricp)
74  throw GeographicErr("Must specify one of f and J2");
75  if (!(Math::isfinite(omega) && omega != 0))
76  throw GeographicErr("Angular velocity is not non-zero");
77  } else
78  geometricp = true;
79  Initialize(a, GM, omega, geometricp ? f : J2, geometricp);
80  }
81 
83  static const NormalGravity wgs84(Constants::WGS84_a(),
86  Constants::WGS84_f(), true);
87  return wgs84;
88  }
89 
91  static const NormalGravity grs80(Constants::GRS80_a(),
94  Constants::GRS80_J2(), false);
95  return grs80;
96  }
97 
98  Math::real NormalGravity::atan7series(real x) {
99  // compute -sum( (-x)^n/(2*n+7), n, 0, inf)
100  // = -1/7 + x/9 - x^2/11 + x^3/13 ...
101  // = (atan(sqrt(x))/sqrt(x)-(1-x/3+x^2/5)) / x^3 (x > 0)
102  // = (atanh(sqrt(-x))/sqrt(-x)-(1-x/3+x^2/5)) / x^3 (x < 0)
103  // require abs(x) < 1/2, but better to restrict calls to abs(x) < 1/4
104  static const real lg2eps_ =
105  -log(numeric_limits<real>::epsilon() / 2) / log(real(2));
106  int e;
107  frexp(x, &e);
108  e = max(-e, 1); // Here's where abs(x) < 1/2 is assumed
109  // x = [0.5,1) * 2^(-e)
110  // estimate n s.t. x^n/n < 1/7 * epsilon/2
111  // a stronger condition is x^n < epsilon/2
112  // taking log2 of both sides, a stronger condition is n*(-e) < -lg2eps;
113  // or n*e > lg2eps or n > ceiling(lg2eps/e)
114  int n = int(ceil(lg2eps_ / e));
115  Math::real v = 0;
116  while (n--) // iterating from n-1 down to 0
117  v = - x * v - 1/Math::real(2*n + 7);
118  return v;
119  }
120 
121  Math::real NormalGravity::atan5series(real x) {
122  // Compute Taylor series approximations to
123  // (atan(z)-(z-z^3/3))/z^5,
124  // z = sqrt(x)
125  // require abs(x) < 1/2, but better to restrict calls to abs(x) < 1/4
126  return 1/real(5) + x * atan7series(x);
127  }
128 
129  Math::real NormalGravity::Qf(real x, bool alt) {
130  // Compute
131  // Q(z) = (((1 + 3/z^2) * atan(z) - 3/z)/2) / z^3
132  // = q(z)/z^3 with q(z) defined by H+M, Eq 2-57 with z = E/u
133  // z = sqrt(x)
134  real y = alt ? -x / (1 + x) : x;
135  return !(4 * abs(y) < 1) ? // Backwards test to allow NaNs through
136  ((1 + 3/y) * atanzz(x, alt) - 3/y) / (2 * y) :
137  (3 * (3 + y) * atan5series(y) - 1) / 6;
138  }
139 
140  Math::real NormalGravity::Hf(real x, bool alt) {
141  // z = sqrt(x)
142  // Compute
143  // H(z) = (3*Q(z)+z*diff(Q(z),z))*(1+z^2)
144  // = (3 * (1 + 1/z^2) * (1 - atan(z)/z) - 1) / z^2
145  // = q'(z)/z^2, with q'(z) defined by H+M, Eq 2-67, with z = E/u
146  real y = alt ? -x / (1 + x) : x;
147  return !(4 * abs(y) < 1) ? // Backwards test to allow NaNs through
148  (3 * (1 + 1/y) * (1 - atanzz(x, alt)) - 1) / y :
149  1 - 3 * (1 + y) * atan5series(y);
150  }
151 
152  Math::real NormalGravity::QH3f(real x, bool alt) {
153  // z = sqrt(x)
154  // (Q(z) - H(z)/3) / z^2
155  // = - (1+z^2)/(3*z) * d(Q(z))/dz - Q(z)
156  // = ((15+9*z^2)*atan(z)-4*z^3-15*z)/(6*z^7)
157  // = ((25+15*z^2)*atan7+3)/10
158  real y = alt ? -x / (1 + x) : x;
159  return !(4 * abs(y) < 1) ? // Backwards test to allow NaNs through
160  ((9 + 15/y) * atanzz(x, alt) - 4 - 15/y) / (6 * Math::sq(y)) :
161  ((25 + 15*y) * atan7series(y) + 3)/10;
162  }
163 
164  Math::real NormalGravity::Jn(int n) const {
165  // Note Jn(0) = -1; Jn(2) = _J2; Jn(odd) = 0
166  if (n & 1 || n < 0)
167  return 0;
168  n /= 2;
169  real e2n = 1; // Perhaps this should just be e2n = pow(-_e2, n);
170  for (int j = n; j--;)
171  e2n *= -_e2;
172  return // H+M, Eq 2-92
173  -3 * e2n * ((1 - n) + 5 * n * _J2 / _e2) / ((2 * n + 1) * (2 * n + 3));
174  }
175 
177  real sphi = Math::sind(Math::LatFix(lat));
178  // H+M, Eq 2-78
179  return (_gammae + _k * Math::sq(sphi)) / sqrt(1 - _e2 * Math::sq(sphi));
180  }
181 
182  Math::real NormalGravity::V0(real X, real Y, real Z,
183  real& GammaX, real& GammaY, real& GammaZ)
184  const {
185  // See H+M, Sec 6-2
186  real
187  p = Math::hypot(X, Y),
188  clam = p ? X/p : 1,
189  slam = p ? Y/p : 0,
190  r = Math::hypot(p, Z);
191  if (_f < 0) swap(p, Z);
192  real
193  Q = Math::sq(r) - Math::sq(_E),
194  t2 = Math::sq(2 * _E * Z),
195  disc = sqrt(Math::sq(Q) + t2),
196  // This is H+M, Eq 6-8a, but generalized to deal with Q negative
197  // accurately.
198  u = sqrt((Q >= 0 ? (Q + disc) : t2 / (disc - Q)) / 2),
199  uE = Math::hypot(u, _E),
200  // H+M, Eq 6-8b
201  sbet = u ? Z * uE : Math::copysign(sqrt(-Q), Z),
202  cbet = u ? p * u : p,
203  s = Math::hypot(cbet, sbet);
204  sbet = s ? sbet/s : 1;
205  cbet = s ? cbet/s : 0;
206  real
207  z = _E/u,
208  z2 = Math::sq(z),
209  den = Math::hypot(u, _E * sbet);
210  if (_f < 0) {
211  swap(sbet, cbet);
212  swap(u, uE);
213  }
214  real
215  invw = uE / den, // H+M, Eq 2-63
216  bu = _b / (u != 0 || _f < 0 ? u : _E),
217  // Qf(z2->inf, false) = pi/(4*z^3)
218  q = ((u != 0 || _f < 0 ? Qf(z2, _f < 0) : Math::pi() / 4) / _Q0) *
219  bu * Math::sq(bu),
220  qp = _b * Math::sq(bu) * (u != 0 || _f < 0 ? Hf(z2, _f < 0) : 2) / _Q0,
221  ang = (Math::sq(sbet) - 1/real(3)) / 2,
222  // H+M, Eqs 2-62 + 6-9, but omitting last (rotational) term.
223  Vres = _GM * (u != 0 || _f < 0 ?
224  atanzz(z2, _f < 0) / u :
225  Math::pi() / (2 * _E)) + _aomega2 * q * ang,
226  // H+M, Eq 6-10
227  gamu = - (_GM + (_aomega2 * qp * ang)) * invw / Math::sq(uE),
228  gamb = _aomega2 * q * sbet * cbet * invw / uE,
229  t = u * invw / uE;
230  // H+M, Eq 6-12
231  GammaX = t * cbet * gamu - invw * sbet * gamb;
232  GammaY = GammaX * slam;
233  GammaX *= clam;
234  GammaZ = invw * sbet * gamu + t * cbet * gamb;
235  return Vres;
236  }
237 
238  Math::real NormalGravity::Phi(real X, real Y, real& fX, real& fY)
239  const {
240  fX = _omega2 * X;
241  fY = _omega2 * Y;
242  // N.B. fZ = 0;
243  return _omega2 * (Math::sq(X) + Math::sq(Y)) / 2;
244  }
245 
246  Math::real NormalGravity::U(real X, real Y, real Z,
247  real& gammaX, real& gammaY, real& gammaZ)
248  const {
249  real fX, fY;
250  real Ures = V0(X, Y, Z, gammaX, gammaY, gammaZ) + Phi(X, Y, fX, fY);
251  gammaX += fX;
252  gammaY += fY;
253  return Ures;
254  }
255 
257  real& gammay, real& gammaz)
258  const {
259  real X, Y, Z;
260  real M[Geocentric::dim2_];
261  _earth.IntForward(lat, 0, h, X, Y, Z, M);
262  real gammaX, gammaY, gammaZ,
263  Ures = U(X, Y, Z, gammaX, gammaY, gammaZ);
264  // gammax = M[0] * gammaX + M[3] * gammaY + M[6] * gammaZ;
265  gammay = M[1] * gammaX + M[4] * gammaY + M[7] * gammaZ;
266  gammaz = M[2] * gammaX + M[5] * gammaY + M[8] * gammaZ;
267  return Ures;
268  }
269 
271  real omega, real J2) {
272  // Solve
273  // f = e^2 * (1 - K * e/q0) - 3 * J2 = 0
274  // for e^2 using Newton's method
275  static const real maxe_ = 1 - numeric_limits<real>::epsilon();
276  static const real eps2_ = sqrt(numeric_limits<real>::epsilon()) / 100;
277  real
278  K = 2 * Math::sq(a * omega) * a / (15 * GM),
279  J0 = (1 - 4 * K / Math::pi()) / 3;
280  if (!(GM > 0 && Math::isfinite(K) && K >= 0))
281  return Math::NaN();
282  if (!(Math::isfinite(J2) && J2 <= J0)) return Math::NaN();
283  if (J2 == J0) return 1;
284  // Solve e2 - f1 * f2 * K / Q0 - 3 * J2 = 0 for J2 close to J0;
285  // subst e2 = ep2/(1+ep2), f2 = 1/(1+ep2), f1 = 1/sqrt(1+ep2), J2 = J0-dJ2,
286  // Q0 = pi/(4*z^3) - 2/z^4 + (3*pi)/(4*z^5), z = sqrt(ep2), and balance two
287  // leading terms to give
288  real
289  ep2 = max(Math::sq(32 * K / (3 * Math::sq(Math::pi()) * (J0 - J2))),
290  -maxe_),
291  e2 = min(ep2 / (1 + ep2), maxe_);
292  for (int j = 0; j < maxit_ || GEOGRAPHICLIB_PANIC; ++j) {
293  real
294  e2a = e2, ep2a = ep2,
295  f2 = 1 - e2, // (1 - f)^2
296  f1 = sqrt(f2), // (1 - f)
297  Q0 = Qf(e2 < 0 ? -e2 : ep2, e2 < 0),
298  h = e2 - f1 * f2 * K / Q0 - 3 * J2,
299  dh = 1 - 3 * f1 * K * QH3f(e2 < 0 ? -e2 : ep2, e2 < 0) /
300  (2 * Math::sq(Q0));
301  e2 = min(e2a - h / dh, maxe_);
302  ep2 = max(e2 / (1 - e2), -maxe_);
303  if (abs(h) < eps2_ || e2 == e2a || ep2 == ep2a)
304  break;
305  }
306  return e2 / (1 + sqrt(1 - e2));
307  }
308 
310  real omega, real f) {
311  real
312  K = 2 * Math::sq(a * omega) * a / (15 * GM),
313  f1 = 1 - f,
314  f2 = Math::sq(f1),
315  e2 = f * (2 - f);
316  // H+M, Eq 2-90 + 2-92'
317  return (e2 - K * f1 * f2 / Qf(f < 0 ? -e2 : e2 / f2, f < 0)) / 3;
318  }
319 
320 } // namespace GeographicLib
static T NaN()
Definition: Math.hpp:825
static T pi()
Definition: Math.hpp:202
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
The normal gravity of the earth.
static bool isfinite(T x)
Definition: Math.hpp:801
static T LatFix(T x)
Definition: Math.hpp:464
static T sind(T x)
Definition: Math.hpp:606
static T hypot(T x, T y)
Definition: Math.hpp:243
static T sq(T x)
Definition: Math.hpp:232
static Math::real FlatteningToJ2(real a, real GM, real omega, real f)
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static Math::real J2ToFlattening(real a, real GM, real omega, real J2)
static const NormalGravity & GRS80()
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
Math::real SurfaceGravity(real lat) const
static T copysign(T x, T y)
Definition: Math.hpp:748
Header for GeographicLib::NormalGravity class.
Exception handling for GeographicLib.
Definition: Constants.hpp:389
Math::real Gravity(real lat, real h, real &gammay, real &gammaz) const
static const NormalGravity & WGS84()
Math::real V0(real X, real Y, real Z, real &GammaX, real &GammaY, real &GammaZ) const
Math::real Phi(real X, real Y, real &fX, real &fY) const
Math::real U(real X, real Y, real Z, real &gammaX, real &gammaY, real &gammaZ) const
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:87