Vulcan
Aerospace Engineering Utilities Built on Janus
Loading...
Searching...
No Matches
J2.hpp
Go to the documentation of this file.
1// Vulcan J2 Gravity Model
2// Accounts for Earth's oblateness (equatorial bulge)
3#pragma once
4
5#include <janus/janus.hpp>
8
10
29template <typename Scalar>
30Vec3<Scalar> acceleration(const Vec3<Scalar> &r_ecef,
31 double mu = constants::earth::mu,
32 double J2_coeff = constants::earth::J2,
33 double R_eq = constants::earth::R_eq) {
34 const Scalar x = r_ecef(0);
35 const Scalar y = r_ecef(1);
36 const Scalar z = r_ecef(2);
37
38 const Scalar r2 = x * x + y * y + z * z;
39 const Scalar r = janus::sqrt(r2);
40 const Scalar r3 = r2 * r;
41
42 // Precompute common terms
43 const Scalar z2_over_r2 = z * z / r2;
44 const Scalar R_eq_over_r_sq = (R_eq * R_eq) / r2;
45 const Scalar J2_factor = 1.5 * J2_coeff * R_eq_over_r_sq;
46
47 // Point mass term coefficient: -μ/r³
48 const Scalar pm_coeff = -mu / r3;
49
50 // J2 perturbation factors
51 const Scalar xy_factor = 1.0 - J2_factor * (5.0 * z2_over_r2 - 1.0);
52 const Scalar z_factor = 1.0 - J2_factor * (5.0 * z2_over_r2 - 3.0);
53
54 Vec3<Scalar> accel;
55 accel(0) = pm_coeff * x * xy_factor;
56 accel(1) = pm_coeff * y * xy_factor;
57 accel(2) = pm_coeff * z * z_factor;
58
59 return accel;
60}
61
76template <typename Scalar>
77Scalar potential(const Vec3<Scalar> &r_ecef, double mu = constants::earth::mu,
78 double J2_coeff = constants::earth::J2,
79 double R_eq = constants::earth::R_eq) {
80 const Scalar x = r_ecef(0);
81 const Scalar y = r_ecef(1);
82 const Scalar z = r_ecef(2);
83
84 const Scalar r2 = x * x + y * y + z * z;
85 const Scalar r = janus::sqrt(r2);
86
87 // sin(φ) = z/r (geocentric latitude)
88 const Scalar sin_phi = z / r;
89 const Scalar sin_phi_sq = sin_phi * sin_phi;
90
91 // P₂(sin φ) = (3 sin²φ - 1) / 2
92 const Scalar P2 = (3.0 * sin_phi_sq - 1.0) / 2.0;
93
94 // U = -μ/r · [1 - J2·(R_eq/r)²·P₂]
95 const Scalar R_eq_over_r_sq = (R_eq * R_eq) / r2;
96
97 return -mu / r * (1.0 - J2_coeff * R_eq_over_r_sq * P2);
98}
99
100} // namespace vulcan::gravity::j2
constexpr double R_eq
Equatorial radius [m] (WGS84).
Definition Constants.hpp:16
constexpr double J2
J2 zonal harmonic coefficient.
Definition Constants.hpp:28
constexpr double mu
Gravitational parameter (GM) [m^3/s^2].
Definition Constants.hpp:13
Definition J2.hpp:9
Vec3< Scalar > acceleration(const Vec3< Scalar > &r_ecef, double mu=constants::earth::mu, double J2_coeff=constants::earth::J2, double R_eq=constants::earth::R_eq)
J2 gravitational acceleration (oblate Earth).
Definition J2.hpp:30
Scalar potential(const Vec3< Scalar > &r_ecef, double mu=constants::earth::mu, double J2_coeff=constants::earth::J2, double R_eq=constants::earth::R_eq)
J2 gravitational potential.
Definition J2.hpp:77