1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use super::hyperdual::linalg::norm;
use super::hyperdual::{Float, Hyperdual};
use crate::celestia::{Cosm, Frame, Orbit};
use crate::dimensions::{DMatrix, Matrix3, Vector3, U7};
use crate::dynamics::AccelModel;
use crate::errors::NyxError;
use crate::io::gravity::GravityPotentialStor;
use crate::TimeTagged;
use std::cmp::min;
use std::sync::Arc;
#[derive(Clone)]
pub struct Harmonics<S>
where
S: GravityPotentialStor,
{
cosm: Arc<Cosm>,
compute_frame: Frame,
stor: S,
a_nm: DMatrix<f64>,
b_nm: DMatrix<f64>,
c_nm: DMatrix<f64>,
vr01: DMatrix<f64>,
vr11: DMatrix<f64>,
a_nm_h: DMatrix<Hyperdual<f64, U7>>,
b_nm_h: DMatrix<Hyperdual<f64, U7>>,
c_nm_h: DMatrix<Hyperdual<f64, U7>>,
vr01_h: DMatrix<Hyperdual<f64, U7>>,
vr11_h: DMatrix<Hyperdual<f64, U7>>,
}
impl<S> Harmonics<S>
where
S: GravityPotentialStor,
{
pub fn from_stor(compute_frame: Frame, stor: S, cosm: Arc<Cosm>) -> Arc<Self> {
assert!(
compute_frame.is_geoid(),
"harmonics only work around geoids"
);
let degree_np2 = stor.max_degree_n() + 2;
let mut a_nm = DMatrix::from_element(degree_np2 + 1, degree_np2 + 1, 0.0);
let mut b_nm = DMatrix::from_element(degree_np2, degree_np2, 0.0);
let mut c_nm = DMatrix::from_element(degree_np2, degree_np2, 0.0);
let mut vr01 = DMatrix::from_element(degree_np2, degree_np2, 0.0);
let mut vr11 = DMatrix::from_element(degree_np2, degree_np2, 0.0);
a_nm[(0, 0)] = 1.0;
for n in 1..=degree_np2 {
let nf64 = n as f64;
a_nm[(n, n)] = (1.0 + 1.0 / (2.0 * nf64)).sqrt() * a_nm[(n - 1, n - 1)];
}
for n in 0..degree_np2 {
for m in 0..degree_np2 {
let nf64 = n as f64;
let mf64 = m as f64;
c_nm[(n, m)] = (((2.0 * nf64 + 1.0) * (nf64 + mf64 - 1.0) * (nf64 - mf64 - 1.0))
/ ((nf64 - mf64) * (nf64 + mf64) * (2.0 * nf64 - 3.0)))
.sqrt();
b_nm[(n, m)] = (((2.0 * nf64 + 1.0) * (2.0 * nf64 - 1.0))
/ ((nf64 + mf64) * (nf64 - mf64)))
.sqrt();
vr01[(n, m)] = ((nf64 - mf64) * (nf64 + mf64 + 1.0)).sqrt();
vr11[(n, m)] = (((2.0 * nf64 + 1.0) * (nf64 + mf64 + 2.0) * (nf64 + mf64 + 1.0))
/ (2.0 * nf64 + 3.0))
.sqrt();
if m == 0 {
vr01[(n, m)] /= 2.0_f64.sqrt();
vr11[(n, m)] /= 2.0_f64.sqrt();
}
}
}
let mut a_nm_h =
DMatrix::from_element(degree_np2 + 1, degree_np2 + 1, Hyperdual::from(0.0));
let mut b_nm_h = DMatrix::from_element(degree_np2, degree_np2, Hyperdual::from(0.0));
let mut c_nm_h = DMatrix::from_element(degree_np2, degree_np2, Hyperdual::from(0.0));
let mut vr01_h = DMatrix::from_element(degree_np2, degree_np2, Hyperdual::from(0.0));
let mut vr11_h = DMatrix::from_element(degree_np2, degree_np2, Hyperdual::from(0.0));
a_nm_h[(0, 0)] = Hyperdual::from(1.0);
for n in 1..=degree_np2 {
a_nm_h[(n, n)] = Hyperdual::from(a_nm[(n, n)]);
}
for n in 0..degree_np2 {
for m in 0..degree_np2 {
vr01_h[(n, m)] = Hyperdual::from(vr01[(n, m)]);
vr11_h[(n, m)] = Hyperdual::from(vr11[(n, m)]);
b_nm_h[(n, m)] = Hyperdual::from(b_nm[(n, m)]);
c_nm_h[(n, m)] = Hyperdual::from(c_nm[(n, m)]);
}
}
Arc::new(Self {
cosm,
compute_frame,
stor,
a_nm,
b_nm,
c_nm,
vr01,
vr11,
a_nm_h,
b_nm_h,
c_nm_h,
vr01_h,
vr11_h,
})
}
}
impl<S: GravityPotentialStor + Send> AccelModel for Harmonics<S> {
fn eom(&self, osc: &Orbit) -> Result<Vector3<f64>, NyxError> {
let state = self.cosm.frame_chg(osc, self.compute_frame);
let r_ = state.rmag();
let s_ = state.x / r_;
let t_ = state.y / r_;
let u_ = state.z / r_;
let max_degree = self.stor.max_degree_n() as usize;
let max_order = self.stor.max_order_m() as usize;
let mut a_nm = self.a_nm.clone();
a_nm[(1, 0)] = u_ * 3.0f64.sqrt();
for n in 1..=max_degree + 1 {
let nf64 = n as f64;
a_nm[(n + 1, n)] = (2.0 * nf64 + 3.0).sqrt() * u_ * a_nm[(n, n)];
}
for m in 0..=max_order + 1 {
for n in (m + 2)..=max_degree + 1 {
let hm_idx = (n, m);
a_nm[hm_idx] = u_ * self.b_nm[hm_idx] * a_nm[(n - 1, m)]
- self.c_nm[hm_idx] * a_nm[(n - 2, m)];
}
}
let mut r_m = Vec::with_capacity(min(max_degree, max_order) + 1);
let mut i_m = Vec::with_capacity(min(max_degree, max_order) + 1);
r_m.push(1.0);
i_m.push(0.0);
for m in 1..=min(max_degree, max_order) {
r_m.push(s_ * r_m[m - 1] - t_ * i_m[m - 1]);
i_m.push(s_ * i_m[m - 1] + t_ * r_m[m - 1]);
}
let rho = self.compute_frame.equatorial_radius() / r_;
let mut rho_np1 = self.compute_frame.gm() / r_ * rho;
let mut a0 = 0.0;
let mut a1 = 0.0;
let mut a2 = 0.0;
let mut a3 = 0.0;
for n in 1..max_degree {
let mut sum0 = 0.0;
let mut sum1 = 0.0;
let mut sum2 = 0.0;
let mut sum3 = 0.0;
rho_np1 *= rho;
for m in 0..=min(n, max_order) {
let (c_val, s_val) = self.stor.cs_nm(n, m);
let d_ = (c_val * r_m[m] + s_val * i_m[m]) * 2.0.sqrt();
let e_ = if m == 0 {
0.0
} else {
(c_val * r_m[m - 1] + s_val * i_m[m - 1]) * 2.0.sqrt()
};
let f_ = if m == 0 {
0.0
} else {
(s_val * r_m[m - 1] - c_val * i_m[m - 1]) * 2.0.sqrt()
};
sum0 += (m as f64) * a_nm[(n, m)] * e_;
sum1 += (m as f64) * a_nm[(n, m)] * f_;
sum2 += self.vr01[(n, m)] * a_nm[(n, m + 1)] * d_;
sum3 += self.vr11[(n, m)] * a_nm[(n + 1, m + 1)] * d_;
}
let rr = rho_np1 / self.compute_frame.equatorial_radius();
a0 += rr * sum0;
a1 += rr * sum1;
a2 += rr * sum2;
a3 -= rr * sum3;
}
let accel = Vector3::new(a0 + a3 * s_, a1 + a3 * t_, a2 + a3 * u_);
let dcm = self
.cosm
.try_position_dcm_from_to(&self.compute_frame, &osc.frame, osc.dt)?;
Ok(dcm * accel)
}
fn dual_eom(
&self,
radius: &Vector3<Hyperdual<f64, U7>>,
ctx: &Orbit,
) -> Result<(Vector3<f64>, Matrix3<f64>), NyxError> {
let mut ctx = *ctx;
ctx.x = radius[0].real();
ctx.y = radius[1].real();
ctx.z = radius[2].real();
let state = self.cosm.try_frame_translation(&ctx, self.compute_frame)?;
let mut radius = *radius;
radius[0][0] = state.x;
radius[1][0] = state.y;
radius[2][0] = state.z;
let dcm =
self.cosm
.try_position_dcm_from_to(&ctx.frame, &self.compute_frame, ctx.epoch())?;
let mut dcm_d = Matrix3::<Hyperdual<f64, U7>>::zeros();
for i in 0..3 {
for j in 0..3 {
dcm_d[(i, j)] = Hyperdual::from_fn(|k| {
if k == 0 {
dcm[(i, j)]
} else if i + 1 == k {
1.0
} else {
0.0
}
})
}
}
let radius = dcm_d * radius;
let r_ = norm(&radius);
let s_ = radius[0] / r_;
let t_ = radius[1] / r_;
let u_ = radius[2] / r_;
let max_degree = self.stor.max_degree_n() as usize;
let max_order = self.stor.max_order_m() as usize;
let mut a_nm = self.a_nm_h.clone();
a_nm[(1, 0)] = u_ * 3.0f64.sqrt();
for n in 1..=max_degree + 1 {
let nf64 = n as f64;
a_nm[(n + 1, n)] = Hyperdual::from((2.0 * nf64 + 3.0).sqrt()) * u_ * a_nm[(n, n)];
}
for m in 0..=max_order + 1 {
for n in (m + 2)..=max_degree + 1 {
let hm_idx = (n, m);
a_nm[hm_idx] = u_ * self.b_nm_h[hm_idx] * a_nm[(n - 1, m)]
- self.c_nm_h[hm_idx] * a_nm[(n - 2, m)];
}
}
let mut r_m = Vec::with_capacity(min(max_degree, max_order) + 1);
let mut i_m = Vec::with_capacity(min(max_degree, max_order) + 1);
r_m.push(Hyperdual::<f64, U7>::from(1.0));
i_m.push(Hyperdual::<f64, U7>::from(0.0));
for m in 1..=min(max_degree, max_order) {
r_m.push(s_ * r_m[m - 1] - t_ * i_m[m - 1]);
i_m.push(s_ * i_m[m - 1] + t_ * r_m[m - 1]);
}
let eq_radius = Hyperdual::<f64, U7>::from(self.compute_frame.equatorial_radius());
let rho = eq_radius / r_;
let mut rho_np1 = Hyperdual::<f64, U7>::from(self.compute_frame.gm()) / r_ * rho;
let mut a0 = Hyperdual::<f64, U7>::from(0.0);
let mut a1 = Hyperdual::<f64, U7>::from(0.0);
let mut a2 = Hyperdual::<f64, U7>::from(0.0);
let mut a3 = Hyperdual::<f64, U7>::from(0.0);
let sqrt2 = Hyperdual::<f64, U7>::from(2.0.sqrt());
for n in 1..max_degree {
let mut sum0 = Hyperdual::from(0.0);
let mut sum1 = Hyperdual::from(0.0);
let mut sum2 = Hyperdual::from(0.0);
let mut sum3 = Hyperdual::from(0.0);
rho_np1 *= rho;
for m in 0..=min(n, max_order) {
let (c_valf64, s_valf64) = self.stor.cs_nm(n, m);
let c_val = Hyperdual::<f64, U7>::from(c_valf64);
let s_val = Hyperdual::<f64, U7>::from(s_valf64);
let d_ = (c_val * r_m[m] + s_val * i_m[m]) * sqrt2;
let e_ = if m == 0 {
Hyperdual::from(0.0)
} else {
(c_val * r_m[m - 1] + s_val * i_m[m - 1]) * sqrt2
};
let f_ = if m == 0 {
Hyperdual::from(0.0)
} else {
(s_val * r_m[m - 1] - c_val * i_m[m - 1]) * sqrt2
};
sum0 += Hyperdual::from(m as f64) * a_nm[(n, m)] * e_;
sum1 += Hyperdual::from(m as f64) * a_nm[(n, m)] * f_;
sum2 += self.vr01_h[(n, m)] * a_nm[(n, m + 1)] * d_;
sum3 += self.vr11_h[(n, m)] * a_nm[(n + 1, m + 1)] * d_;
}
let rr = rho_np1 / eq_radius;
a0 += rr * sum0;
a1 += rr * sum1;
a2 += rr * sum2;
a3 -= rr * sum3;
}
let accel = dcm_d.transpose() * Vector3::new(a0 + a3 * s_, a1 + a3 * t_, a2 + a3 * u_);
let mut fx = Vector3::zeros();
let mut grad = Matrix3::zeros();
for i in 0..3 {
fx[i] += accel[i][0];
for j in 1..4 {
grad[(i, j - 1)] += accel[i][j];
}
}
Ok((fx, grad))
}
}