16#ifndef UTILS_MAGNETIC_FIELD_UTILS_H
17#define UTILS_MAGNETIC_FIELD_UTILS_H
66 std::ifstream infile(filename.c_str());
72 std::istringstream header_stream(line);
73 header_stream >> epoch;
79 if (line.find(
"9999999999") == 0) {
break;}
83 double g_val, h_val, gdot_val, hdot_val;
84 std::istringstream iss(line);
85 if (!(iss >> n >> m >> g_val >> h_val >> gdot_val >> hdot_val)) {
93 gdot[index] = gdot_val;
94 hdot[index] = hdot_val;
120 return std::sin(input);
137 return std::cos(input);
158 static std::vector<double> norm;
159 static int cached_N = -1;
163 const int NumTerms = (N + 1) * (N + 2) / 2;
164 norm.assign(NumTerms + 1, 1.0);
166 auto idx = [](
int n,
int m) {
return n * (n + 1) / 2 + m; };
168 for (
int n = 1; n <= N; ++n) {
169 int base = idx(n, 0);
170 int prev = idx(n - 1, 0);
171 norm[base] = norm[prev] * (2.0 * n - 1) / n;
172 for (
int m = 1; m <= n; ++m) {
174 int ip = idx(n, m - 1);
175 double numer = (n - m + 1) * (m == 1 ? 2.0 : 1.0);
176 double denom = n + m;
177 norm[i] = norm[ip] * std::sqrt(numer / denom);
184 const double z = std::sqrt(1.0 - x * x);
189 auto idx = [](
int n,
int m) {
return n * (n + 1) / 2 + m; };
192 for (
int n = 1; n <= N; ++n) {
193 for (
int m = 0; m <= n; ++m) {
194 const int i = idx(n, m);
196 const int i1 = idx(n - 1, m - 1);
197 Pcup[i] = z * Pcup[i1];
198 dPcup[i] = z * dPcup[i1] + x * Pcup[i1];
199 }
else if (n == 1 && m == 0) {
200 const int i1 = idx(0, 0);
201 Pcup[i] = x * Pcup[i1];
202 dPcup[i] = x * dPcup[i1] - z * Pcup[i1];
204 const int i1 = idx(n - 2, m);
205 const int i2 = idx(n - 1, m);
207 Pcup[i] = x * Pcup[i2];
208 dPcup[i] = x * dPcup[i2] - z * Pcup[i2];
210 const double k = ((n - 1.0) * (n - 1.0) - m * m) / ((2.0 * n - 1) * (2.0 * n - 3));
211 Pcup[i] = x * Pcup[i2] - k * Pcup[i1];
212 dPcup[i] = x * dPcup[i2] - z * Pcup[i2] - k * dPcup[i1];
219 for (
int n = 1; n <= N; ++n) {
220 for (
int m = 0; m <= n; ++m) {
222 double scale = norm[i];
224 dPcup[i] = -dPcup[i] * scale;
249 static std::vector<double> normS;
250 static int cached_N = -1;
254 normS.assign(N + 1, 1.0);
255 double schmidtQuasiNorm1 = 1.0;
256 for (
int n = 1; n <= N; ++n) {
257 double schmidtQuasiNorm2 = schmidtQuasiNorm1 * (2.0 * n - 1) / n;
258 double schmidtQuasiNorm3 = schmidtQuasiNorm2 * std::sqrt((
double)(n * 2) / (n + 1));
259 normS[n] = schmidtQuasiNorm3;
260 schmidtQuasiNorm1 = schmidtQuasiNorm2;
268 for (
int n = 1; n <= N; ++n) {
274 PcupS[n] = PcupS[n - 1];
276 double k = ((n - 1) * (n - 1) - 1.0) / ((2.0 * n - 1) * (2.0 * n - 3));
277 PcupS[n] = (x * PcupS[n - 1] - k * PcupS[n - 2]) * normS[n];
#define ERROR_INVALID_RANGE
Definition clockwerkerrors.h:53
#define ERROR_NOT_FOUND
Error in the case where dynamics model overruns steps or gets out of whack.
Definition clockwerkerrors.h:163
Class to propagate CR3BP dynamics in characteristic units.
Definition statistics.hpp:22
int schmidtSemiNormLegendreWMM(int N, double x, double *Pcup, double *dPcup)
Computes the Schmidt Semi-Normalized Legendre function outputs provided the maximum degree (N) as a p...
Definition magneticFieldUtils.hpp:152
int schmidtSemiNormLegendreSingularityWMM(int N, double x, double *PcupS)
Computes the Schmidt Semi-Normalized Legendre function outputs provided the maximum degree (N) as a p...
Definition magneticFieldUtils.hpp:243
const int MAX_HARMONIC_COEFFICIENTS
Definition magneticFieldUtils.hpp:29
const int NUMBER_OF_READ_COEFFICIENTS
Definition magneticFieldUtils.hpp:30
double dmuWMM(double input)
Unary functional input to the associated Legendre function specific to the WMM modelm,...
Definition magneticFieldUtils.hpp:136
bool fileExist(const std::string &name)
Function to quickly check and verify whether a file exists.
Definition fileutils.cpp:26
double muWMM(double input)
Unary functional input to the associated Legendre function specific to the WMM model....
Definition magneticFieldUtils.hpp:119
const int NO_ERROR
Definition simlicense.cpp:30
int readWMMCoefficientsFile(const std::string &filename, double g[NUMBER_OF_READ_COEFFICIENTS], double h[NUMBER_OF_READ_COEFFICIENTS], double gdot[NUMBER_OF_READ_COEFFICIENTS], double hdot[NUMBER_OF_READ_COEFFICIENTS], double &epoch)
Reads magnetic field coefficients for WMM from a file and stores them in provided matrices.
Definition magneticFieldUtils.hpp:57