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
use crate::orbit::{self, MeanMotion, Perihelion, SemiAxis};
#[derive(Debug, Clone, Copy)]
/// This represents ways of describing an object in its orbit
pub struct Anomaly;
impl Anomaly {
/// (Mean Anomaly) Calculates the period since the last periapsis.
pub fn mean(self, day: f64, peri: Perihelion, orbital_period: f64) -> f64 {
MeanMotion.by(day, peri, orbital_period).abs()
}
/// (Eccentric Anomaly) Calculates the body's position along its orbital path.
///
/// * (HKE) Hyperbolic Kepler Equation
/// > - H)
/// > - +H_k}{e\cosh(H_k)-1}})
///
/// * (EKE) Elliptical Kepler Equation
/// > - 
/// > - =E-e\sin(E)-M(t))
///
/// * (PKE) Parabolic Kepler Equation
/// > - 
/// > - 
/// > - )
///
pub fn eccentric(
self,
shape: crate::orbit::Type,
day: f64,
orbital_eccentricity: f64,
peri: Perihelion,
orbital_period: f64,
major_axis: f64,
) -> f64 {
match shape {
orbit::Type::Circular => {
// Mean Anomaly
let xref = self.mean(day, peri, orbital_period);
// v = M = E
xref
}
orbit::Type::Parabolic => {
// Initial Pn which allows for precesion
let mut pdx = 10.0;
// Mean Anomaly
let xref = self.mean(day, peri, orbital_period);
// Initial Parabolic Anomaly
let mut px0 = xref;
// Newtons Iterative Step
while pdx > 1.0e-7 {
let x0 = px0.powf(3.0);
let x1 = 6.0;
pdx = x0 / x1;
// Semi-Latus Rectum ( semji-major-axis * (1.0 - eccentricity^2))
let p =
SemiAxis(major_axis).major() * (1.0_f64 - orbital_eccentricity.powf(2.0));
// (Perifocal Distance) q = p/2
let q = p / 2.0;
// M = qD + (D^3 / 6)
px0 = (q * px0) + pdx;
}
let mean_motion = MeanMotion.by(day, peri, orbital_period);
// makes sure that the mean motion isn't negative
if mean_motion < 0.0 {
px0 = -px0;
}
px0
}
orbit::Type::Hyperbolic => {
// Initial Hn which allows for precesion
let mut hdx = 10.0;
// Mean Anomaly
let xref = self.mean(day, peri, orbital_period);
// Initial Hyperbolic Anomaly
let mut hx0 = xref;
// Newtons Iterative Step
while hdx > 1.0e-7 {
// M-esinh(Hk)+Hk
let x0 = (xref - orbital_eccentricity) * hx0.sinh() + hx0;
// ecosh(Hk)-1
let x1 = orbital_eccentricity * hx0.cosh() - 1.0;
// (M-esinh(Hk)+Hk)/(ecosh(Hk)-1)
hdx = x0 / x1;
// Hk+1 = Hk + (M-esinh(Hk)+Hk)/(ecosh(Hk)-1)
hx0 = hx0 + hdx;
}
let mean_motion = MeanMotion.by(day, peri, orbital_period);
// makes sure that the mean motion isn't negative
if mean_motion < 0.0 {
hx0 = -hx0;
}
hx0
}
orbit::Type::Elliptical => {
// Initial En which allows for precesion
let mut zdx: f64 = 10.0;
// Mean Anomaly
let xref = self.mean(day, peri, orbital_period);
// Initial Eccentric Anomaly
let mut zx0 = xref + orbital_eccentricity * xref.sin();
// Newtons Iterative step
while zdx > 1.0e-7 {
let x0 = -(zx0 - orbital_eccentricity * zx0.sin() - xref);
let x1 = 1.0 - orbital_eccentricity * zx0.cos();
// En = - ((En - e * En.sin() - M(t)) / 1 - e * En.cos() )
// the En at its first increment En = E0
zdx = x0 / x1;
// En = En + En+1
zx0 = zx0 + zdx;
}
let mean_motion = MeanMotion.by(day, peri, orbital_period);
// makes sure that the mean motion isn't negative
if mean_motion < 0.0 {
zx0 = -zx0;
}
// println!("zx0: {:?}", zx0);
zx0
}
_ => 0.0,
}
}
/// (True Anomaly) Calculates the angle between the periapsis and the body's current position.
///
/// * Elliptical Eccentric Anomaly
/// > - )
///
/// * Hyperbolic (Eccentric) Anomaly
/// > - ^{1/2}\tanh(\frac{H}{2}))
///
/// * Parabolic (Eccentric) Anomaly
/// > - 
///
/// * Circular (Eccentric) Anomaly
/// > - )
/// > - 
///
pub fn truly(
self,
shape: crate::orbit::Type,
day: f64,
orbital_eccentricity: f64,
peri: Perihelion,
orbital_period: f64,
major_axis: f64,
) -> f64 {
match shape {
orbit::Type::Circular => {
let mut theta: f64 = self.eccentric(
shape,
day,
orbital_eccentricity,
peri,
orbital_period,
major_axis,
);
let mean_motion = MeanMotion.by(day, peri, orbital_period);
theta = theta + mean_motion;
theta
}
orbit::Type::Parabolic => {
let theta: f64 = self.eccentric(
shape,
day,
orbital_eccentricity,
peri,
orbital_period,
major_axis,
);
let p = 0.0;
let q = p / 2.0_f64;
theta / (2.0_f64 * q).sqrt()
}
orbit::Type::Hyperbolic => {
let theta: f64 = self.eccentric(
shape,
day,
orbital_eccentricity,
peri,
orbital_period,
major_axis,
);
// tan v/2 = (e+1/e-1)^1/2 * tanh(F/2)
// `where F = H`
((orbital_eccentricity + 1.0) / (orbital_eccentricity - 1.0)).powf(0.5)
* (theta / 2.0).tanh()
}
orbit::Type::Elliptical => {
let theta: f64 = self.eccentric(
shape,
day,
orbital_eccentricity,
peri,
orbital_period,
major_axis,
);
// println!("zx0: {:?}", theta);
let mean_motion =
((1.0 + orbital_eccentricity) / (1.0 - orbital_eccentricity)).sqrt();
2.0 * (mean_motion * (theta / 2.0).tan()).atan()
}
_ => 0.0,
}
}
}