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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
//! Input parameters bundle for the universal-variable Kepler solver.
#[cfg(feature = "serde")]
use serde::Deserialize;
#[cfg(feature = "serde")]
use serde::Serialize;
use crate::{
kepler::{
brent_dekker_solver::solve_kepuni_brent_dekker,
prelim_elliptic, prelim_hyperbolic,
prelim_kepler::prelim_parabolic::{prelim_parabolic, ParabolicPrelimMethod},
solve_kepuni_with_guess, UniversalKeplerSolution,
},
OutfitError,
};
use super::orbit_type::OrbitType;
/// Common tuning parameters shared by all Kepler equation solvers.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct SolverParams {
/// Convergence threshold on the solved variable.
pub convergency: f64,
/// Initial guess for the universal anomaly $\psi$.
pub psi_guess: Option<f64>,
/// Maximum number of iterations for the preliminary `kepuni` step.
pub max_iter_prelim_kepuni: usize,
/// Choose between the exact analytical method or Newton-Raphson minimization
pub parabolic_solving_method: ParabolicPrelimMethod,
}
impl Default for SolverParams {
fn default() -> Self {
Self {
convergency: 100.0 * f64::EPSILON,
psi_guess: None,
max_iter_prelim_kepuni: 20,
parabolic_solving_method: ParabolicPrelimMethod::Cardano,
}
}
}
/// Selects which root-finding algorithm is used to solve Kepler's equation.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub enum SolverKind {
/// Newton-Raphson iteration.
NewtonRaphson,
/// Brent-Decker's bracketing method.
BrentDecker,
/// Automatically selects between available solvers.
Auto,
}
/// Full solver configuration: algorithm choice plus its tuning parameters.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct SolverType {
pub kind: SolverKind,
pub params: SolverParams,
}
impl Default for SolverType {
fn default() -> Self {
Self {
kind: SolverKind::NewtonRaphson,
params: Default::default(),
}
}
}
/// Parameters required to solve the universal Kepler equation.
///
/// This struct bundles together the state and constants needed to propagate an
/// orbit using the universal variable formulation:
///
/// * `dt`: Propagation time interval Δt = t - t₀ (in days).
/// * `r0`: Heliocentric distance at the reference epoch tâ‚€ (in AU).
/// * `sig0`: Radial velocity component at tâ‚€ (in AU/day).
/// * `mu`: Standard gravitational parameter μ = GM (in AU³/day²).
/// * `alpha`: Twice the specific orbital energy (2E).
/// * `e0`: Orbital eccentricity (unitless).
///
/// The associated method [`orbit_type`](UniversalKeplerParams::orbit_type)
/// classifies the orbit into elliptical, parabolic, or hyperbolic regimes
/// based on `alpha`.
#[derive(Debug, Clone, Copy)]
pub struct UniversalKeplerParams {
/// Propagation time interval Δt = t - t₀, in days.
pub dt: f64,
/// Heliocentric distance at the reference epoch tâ‚€, in AU.
pub r0: f64,
/// Radial velocity component at t₀ (= r₀·v₀ / r₀), in AU/day.
pub sig0: f64,
/// Standard gravitational parameter μ = GM, in AU³/day².
pub mu: f64,
/// Twice the specific orbital energy (alpha = 2E).
pub alpha: f64,
/// Orbital eccentricity (unitless).
pub e0: f64,
/// solver type
pub solver_type: SolverType,
}
impl UniversalKeplerParams {
/// Returns the [`OrbitType`](crate::kepler::OrbitType) (elliptic, parabolic, or hyperbolic)
/// corresponding to the value of `alpha`.
pub fn orbit_type(&self) -> OrbitType {
OrbitType::from_alpha(self.alpha)
}
/// Return `Some(())` for elliptic or hyperbolic orbits, `None` for parabolic.
///
/// Parabolic motion ( $ \alpha = 0 $ ) is not supported by the universal-variable
/// formulation used here and must be rejected before entering the solver.
#[inline]
pub fn reject_parabolic_orbit(&self) -> Option<()> {
match self.orbit_type() {
OrbitType::Parabolic => None,
OrbitType::Elliptic | OrbitType::Hyperbolic => Some(()),
}
}
pub fn solve(&self) -> Result<UniversalKeplerSolution, OutfitError> {
match self.solver_type.kind {
SolverKind::NewtonRaphson => {
solve_kepuni_with_guess(self).ok_or(OutfitError::NewtonRaphsonKeplerConvergence)
}
SolverKind::BrentDecker => {
solve_kepuni_brent_dekker(self).ok_or(OutfitError::BrentDekkerKeplerConvergence)
}
SolverKind::Auto => solve_kepuni_with_guess(self)
.or_else(|| solve_kepuni_brent_dekker(self))
.ok_or(OutfitError::BrentDekkerKeplerConvergence),
}
}
/// Compute an initial estimate of the universal anomaly $\psi_0$.
///
/// # Goal
///
/// Provide a starting value for $\psi$ before the main solver refines it to
/// full convergence. The appropriate branch is selected based on the orbit
/// type encoded in `self.alpha`:
///
/// | Regime | Condition | Branch |
/// |---|---|---|
/// | Elliptic | $\alpha < 0$ | [`prelim_elliptic`] |
/// | Hyperbolic | $\alpha > 0$ | [`prelim_hyperbolic`] |
/// | Parabolic | $\alpha = 0$ | Not supported — returns `None` |
///
/// # Convergence tolerance
///
/// The tolerance passed to the preliminary Newton–Raphson sub-iterations is
/// extracted from [`SolverType`]:
///
/// - [`SolverKind::NewtonRaphson`] and [`SolverKind::Auto`] — uses the
/// `convergency` field directly, defaulting to $100\varepsilon$ if `None`.
/// - [`SolverKind::BrentDecker`] — same extraction, since the preliminary
/// guess is solver-agnostic.
///
/// # Return
///
/// * `Some(psi_0)` — initial estimate of the universal anomaly.
/// * `None` — if the orbit is parabolic ($\alpha = 0$).
///
/// # Notes
///
/// - This method does **not** iterate to full convergence; it only produces
/// a starting point for the main solver.
/// - Each branch runs at most [`max_iter_prelim_kepuni`](crate::kepler::SolverType) Newton steps
/// internally.
///
/// # See also
///
/// * [`prelim_elliptic`] — elliptic branch.
/// * [`prelim_hyperbolic`] — hyperbolic branch.
/// * [`UniversalKeplerParams::solve`] — main solver entry point.
pub fn prelim_kepuni(&self) -> Option<f64> {
match self.orbit_type() {
OrbitType::Elliptic => Some(prelim_elliptic(self)),
OrbitType::Hyperbolic => Some(prelim_hyperbolic(self)),
OrbitType::Parabolic => Some(prelim_parabolic(self)),
}
}
}
#[cfg(test)]
mod kepler_params_tests {
use crate::kepler::s_funct;
use super::*;
use approx::assert_abs_diff_eq;
use proptest::prelude::*;
// -----------------------------------------------------------------------
// Test helpers
// -----------------------------------------------------------------------
/// Standard gravitational parameter for the Sun, AU³/day².
const MU_SUN: f64 = 2.959_122_082_855_911e-4;
/// Evaluate the universal Kepler residual at the solution `psi` and return
/// its absolute value. Used to verify that a solution actually satisfies
/// the equation regardless of which solver produced it.
fn kepler_residual(sol: &UniversalKeplerSolution, params: &UniversalKeplerParams) -> f64 {
// Recompute Stumpff functions from psi to catch any inconsistency
// between the stored universal anomaly and the cached s.. values.
let (_, s1, s2, s3) = s_funct(sol.universal_anomaly, params.alpha);
(params.r0 * s1 + params.sig0 * s2 + params.mu * s3 - params.dt).abs()
}
/// Build a [`UniversalKeplerParams`] for a near-circular elliptic orbit.
///
/// Uses a semi-major axis `a` (AU) to derive `alpha` and a nearly-zero
/// radial velocity proxy.
fn elliptic_params(dt: f64, a: f64, kind: SolverKind) -> UniversalKeplerParams {
// alpha = -mu / a (twice specific orbital energy for elliptic orbit)
let alpha = -MU_SUN / a;
// r0 ~ a for a near-circular orbit.
let r0 = a;
// sig0 ~ 0 for a circular orbit (radial velocity is zero at periapsis/apoapsis).
let sig0 = 0.0;
UniversalKeplerParams {
dt,
r0,
sig0,
mu: MU_SUN,
alpha,
e0: 0.01,
solver_type: SolverType {
kind,
params: SolverParams::default(),
},
}
}
/// Build a [`UniversalKeplerParams`] for a hyperbolic orbit.
///
/// Uses a characteristic energy $C_3 > 0$ (AU²/day²) so that
/// $\alpha = C_3 > 0$.
fn hyperbolic_params(dt: f64, c3: f64, kind: SolverKind) -> UniversalKeplerParams {
let alpha = c3; // positive energy -> hyperbolic
let r0 = 1.5; // AU
let sig0 = 0.001;
UniversalKeplerParams {
dt,
r0,
sig0,
mu: MU_SUN,
alpha,
e0: 1.5,
solver_type: SolverType {
kind,
params: SolverParams::default(),
},
}
}
// -----------------------------------------------------------------------
// Unit tests — Newton–Raphson
// -----------------------------------------------------------------------
#[test]
fn newton_solves_elliptic_short_arc() {
let params = elliptic_params(
10.0, // 10 days
1.0, // 1 AU semi-major axis (Earth-like)
SolverKind::NewtonRaphson,
);
let sol = params
.solve()
.expect("Newton should converge on elliptic short arc");
assert!(
kepler_residual(&sol, ¶ms) < 1e-10,
"residual too large: {}",
kepler_residual(&sol, ¶ms)
);
}
#[test]
fn newton_solves_elliptic_full_orbit() {
// Propagate for approximately one full period of an Earth-like orbit (~365 days).
let params = elliptic_params(365.0, 1.0, SolverKind::NewtonRaphson);
let sol = params
.solve()
.expect("Newton should converge on full elliptic orbit");
assert!(
kepler_residual(&sol, ¶ms) < 1e-9,
"residual too large: {}",
kepler_residual(&sol, ¶ms)
);
}
#[test]
fn newton_solves_elliptic_negative_dt() {
// Backward propagation: dt < 0 should be handled symmetrically.
let params = elliptic_params(-30.0, 1.0, SolverKind::NewtonRaphson);
let sol = params
.solve()
.expect("Newton should converge for negative dt");
assert!(kepler_residual(&sol, ¶ms) < 1e-10);
}
#[test]
fn newton_solves_hyperbolic() {
let params = hyperbolic_params(
5.0,
1e-5, // small positive C3 -> mildly hyperbolic
SolverKind::NewtonRaphson,
);
let sol = params
.solve()
.expect("Newton should converge on hyperbolic orbit");
assert!(
kepler_residual(&sol, ¶ms) < 1e-10,
"residual: {}",
kepler_residual(&sol, ¶ms)
);
}
#[test]
fn newton_warm_start_consistent_with_cold_start() {
let mut cold = elliptic_params(50.0, 2.0, SolverKind::NewtonRaphson);
let sol_cold = cold.solve().expect("cold start should converge");
// Feed the cold-start solution as a warm-start guess.
cold.solver_type = SolverType {
kind: SolverKind::NewtonRaphson,
params: SolverParams {
psi_guess: Some(sol_cold.universal_anomaly),
..Default::default()
},
};
let sol_warm = cold.solve().expect("warm start should converge");
assert_abs_diff_eq!(
sol_cold.universal_anomaly,
sol_warm.universal_anomaly,
epsilon = 1e-10
);
}
// -----------------------------------------------------------------------
// Unit tests — Brent–Dekker
// -----------------------------------------------------------------------
#[test]
fn brent_solves_elliptic_short_arc() {
let params = elliptic_params(10.0, 1.0, SolverKind::BrentDecker);
let sol = params
.solve()
.expect("Brent should converge on elliptic short arc");
assert!(
kepler_residual(&sol, ¶ms) < 1e-10,
"residual: {}",
kepler_residual(&sol, ¶ms)
);
}
#[test]
fn brent_solves_elliptic_full_orbit() {
let params = elliptic_params(365.0, 1.0, SolverKind::BrentDecker);
let sol = params
.solve()
.expect("Brent should converge on full elliptic orbit");
assert!(kepler_residual(&sol, ¶ms) < 1e-9);
}
#[test]
fn brent_solves_elliptic_negative_dt() {
let params = elliptic_params(-30.0, 1.0, SolverKind::BrentDecker);
let sol = params
.solve()
.expect("Brent should converge for negative dt");
assert!(kepler_residual(&sol, ¶ms) < 1e-10);
}
#[test]
fn brent_solves_hyperbolic() {
let params = hyperbolic_params(5.0, 1e-5, SolverKind::BrentDecker);
let sol = params
.solve()
.expect("Brent should converge on hyperbolic orbit");
assert!(
kepler_residual(&sol, ¶ms) < 1e-10,
"residual: {}",
kepler_residual(&sol, ¶ms)
);
}
// -----------------------------------------------------------------------
// Consistency tests — Newton is the reference
// -----------------------------------------------------------------------
/// Tolerance on `psi` agreement between the two solvers.
/// Newton is the reference: Brent must land within this bound.
const PSI_CONSISTENCY_TOL: f64 = 1e-12;
fn assert_solvers_consistent(params_template: UniversalKeplerParams) {
let mut newton_params = params_template;
newton_params.solver_type.kind = SolverKind::NewtonRaphson;
let mut brent_params = params_template;
brent_params.solver_type.kind = SolverKind::BrentDecker;
let sol_newton = newton_params.solve().expect("Newton must converge");
let sol_brent = brent_params.solve().expect("Brent must converge");
// Both solutions must satisfy the Kepler equation.
assert!(
kepler_residual(&sol_newton, &newton_params) < 1e-10,
"Newton residual too large"
);
assert!(
kepler_residual(&sol_brent, &brent_params) < 1e-10,
"Brent residual too large"
);
// The two solvers must agree on psi (Newton is the reference).
assert_abs_diff_eq!(
sol_newton.universal_anomaly,
sol_brent.universal_anomaly,
epsilon = PSI_CONSISTENCY_TOL
);
}
#[test]
fn solvers_consistent_elliptic_short_arc() {
assert_solvers_consistent(elliptic_params(10.0, 1.0, SolverKind::BrentDecker));
}
#[test]
fn solvers_consistent_elliptic_long_arc() {
assert_solvers_consistent(elliptic_params(200.0, 2.5, SolverKind::BrentDecker));
}
#[test]
fn solvers_consistent_elliptic_negative_dt() {
assert_solvers_consistent(elliptic_params(-45.0, 1.5, SolverKind::BrentDecker));
}
#[test]
fn solvers_consistent_hyperbolic() {
assert_solvers_consistent(hyperbolic_params(5.0, 1e-5, SolverKind::BrentDecker));
}
#[test]
fn solvers_consistent_outer_solar_system_elliptic() {
// Jupiter-like orbit: a ~ 5.2 AU, dt ~ 1000 days.
assert_solvers_consistent(elliptic_params(1000.0, 5.2, SolverKind::BrentDecker));
}
// -----------------------------------------------------------------------
// Property-based tests
// -----------------------------------------------------------------------
/// Strategy for physically plausible elliptic propagation parameters.
///
/// * `a` in [0.3, 10.0] AU — Mercury-like to outer solar system.
/// * `dt` in [1.0, 500.0] days — short to moderately long arcs.
fn elliptic_strategy() -> impl Strategy<Value = (f64, f64)> {
(
0.3_f64..10.0_f64, // semi-major axis (AU)
1.0_f64..500.0_f64, // time of flight (days)
)
}
/// Strategy for hyperbolic parameters.
///
/// * `c3` in [1e-6, 1e-3] AU²/day² — mildly to moderately hyperbolic.
/// * `dt` in [1.0, 100.0] days.
fn hyperbolic_strategy() -> impl Strategy<Value = (f64, f64)> {
(1e-6_f64..1e-3_f64, 1.0_f64..100.0_f64)
}
proptest! {
/// For a wide range of elliptic orbits, Newton must converge and the
/// residual must vanish to within numerical tolerance.
#[test]
fn proptest_newton_elliptic_residual(
(a, dt) in elliptic_strategy()
) {
let params = elliptic_params(dt, a, SolverKind::NewtonRaphson);
if let Ok(sol) = params.solve() {
prop_assert!(
kepler_residual(&sol, ¶ms) < 1e-9,
"Newton residual too large: {}",
kepler_residual(&sol, ¶ms)
);
}
// If Newton fails to converge on a valid input we do not fail the
// proptest — coverage of the failure path is handled by dedicated
// unit tests.
}
/// For a wide range of elliptic orbits, Brent–Dekker must converge and
/// the residual must vanish to within numerical tolerance.
#[test]
fn proptest_brent_elliptic_residual(
(a, dt) in elliptic_strategy()
) {
let params = elliptic_params(dt, a, SolverKind::BrentDecker);
if let Ok(sol) = params.solve() {
prop_assert!(
kepler_residual(&sol, ¶ms) < 1e-9,
"Brent residual too large: {}",
kepler_residual(&sol, ¶ms)
);
}
}
/// When both solvers converge on the same elliptic input, their `psi`
/// values must agree. Newton is the reference.
#[test]
fn proptest_solvers_consistent_elliptic(
(a, dt) in elliptic_strategy()
) {
let newton_params = elliptic_params(dt, a, SolverKind::NewtonRaphson);
let mut brent_params = newton_params;
brent_params.solver_type.kind = SolverKind::BrentDecker;
if let (Ok(sol_n), Ok(sol_b)) = (newton_params.solve(), brent_params.solve()) {
prop_assert!(
(sol_n.universal_anomaly - sol_b.universal_anomaly).abs()
< PSI_CONSISTENCY_TOL,
"psi mismatch: Newton={}, Brent={}",
sol_n.universal_anomaly,
sol_b.universal_anomaly
);
// If either solver diverges, skip this sample — the residual
// tests above already cover individual solver robustness.
}
}
/// For hyperbolic orbits, whenever Newton converges, Brent must also
/// converge and agree on `psi`.
#[test]
fn proptest_solvers_consistent_hyperbolic(
(c3, dt) in hyperbolic_strategy()
) {
let newton_params = hyperbolic_params(dt, c3, SolverKind::NewtonRaphson);
let mut brent_params = newton_params;
brent_params.solver_type.kind = SolverKind::BrentDecker;
if let (Ok(sol_n), Ok(sol_b)) = (newton_params.solve(), brent_params.solve()) {
prop_assert!(
(sol_n.universal_anomaly - sol_b.universal_anomaly).abs()
< PSI_CONSISTENCY_TOL,
"psi mismatch on hyperbolic: Newton={}, Brent={}",
sol_n.universal_anomaly,
sol_b.universal_anomaly
);
}
}
/// The `orbit_type` method must return a regime consistent with the
/// sign of `alpha`, independently of the solver chosen.
#[test]
fn proptest_orbit_type_consistent_with_alpha(
alpha in prop_oneof![
(-1e-2_f64..-1e-6_f64), // elliptic
(1e-6_f64..1e-2_f64), // hyperbolic
]
) {
let params = UniversalKeplerParams {
dt: 10.0,
r0: 1.0,
sig0: 0.0,
mu: MU_SUN,
alpha,
e0: 0.5,
solver_type: SolverType::default(),
};
match params.orbit_type() {
OrbitType::Elliptic => prop_assert!(alpha < 0.0),
OrbitType::Hyperbolic => prop_assert!(alpha > 0.0),
OrbitType::Parabolic => prop_assert!(alpha == 0.0),
}
}
}
}
#[cfg(test)]
mod tests_prelim_kepuni {
use super::*;
const MU: f64 = 1.0;
const CONTR: f64 = 1e-12;
fn make_params(
dt: f64,
r0: f64,
sig0: f64,
mu: f64,
alpha: f64,
e0: f64,
) -> UniversalKeplerParams {
UniversalKeplerParams {
dt,
r0,
sig0,
mu,
alpha,
e0,
solver_type: SolverType {
params: SolverParams {
convergency: CONTR,
..Default::default()
},
..Default::default()
},
}
}
#[test]
fn test_returns_none_for_alpha_zero() {
let params = make_params(1.0, 1.0, 0.0, MU, 0.0, 0.1);
let res = params.prelim_kepuni().unwrap();
assert_eq!(res, 0.8846222003969053);
}
#[test]
fn test_elliptic_small_eccentricity() {
let params = make_params(0.5, 1.0, 0.1, MU, -1.0, 1e-8);
let result = params.prelim_kepuni();
assert!(result.is_some());
assert!(result.unwrap().is_finite());
}
#[test]
fn test_elliptic_high_eccentricity() {
let params = make_params(0.1, 0.5, 0.2, MU, -1.0, 0.8);
let result = params.prelim_kepuni();
assert!(result.is_some());
assert!(result.unwrap().is_finite());
}
#[test]
fn test_hyperbolic_case() {
let params = make_params(0.3, 2.0, -0.1, MU, 1.0, 1.5);
let result = params.prelim_kepuni();
assert!(result.is_some());
assert!(result.unwrap().is_finite());
}
#[test]
fn test_negative_sig0_changes_direction() {
let alpha = -1.0;
let r0 = 1.0;
let e0 = 0.5;
let dt = 0.25;
let params_pos = make_params(dt, r0, 0.1, MU, alpha, e0);
let params_neg = make_params(dt, r0, -0.1, MU, alpha, e0);
let psi_pos = params_pos.prelim_kepuni().unwrap();
let psi_neg = params_neg.prelim_kepuni().unwrap();
assert!(
(psi_pos - psi_neg).abs() > 1e-8,
"psi did not change significantly when changing sig0 sign: {psi_pos} vs {psi_neg}"
);
}
#[test]
fn test_stability_long_dt() {
let params = make_params(50.0, 1.0, 0.1, MU, -1.0, 0.5);
let result = params.prelim_kepuni();
assert!(result.is_some());
assert!(result.unwrap().is_finite());
}
#[test]
fn test_edge_cosine_limits() {
let params = make_params(0.25, 2.0, 0.1, MU, -1.0, 0.1);
let result = params.prelim_kepuni();
assert!(result.is_some());
}
#[test]
fn test_prelim_kepuni_real_data() {
let dt = -20.765849999996135;
let r0 = 1.3803870211345761;
let sig0 = 3.701_354_484_003_874_8E-3;
let mu = 2.959_122_082_855_911_5E-4;
let alpha = -1.642_158_377_771_140_7E-4;
let e0 = 0.283_599_599_137_344_5;
let params = UniversalKeplerParams {
dt,
r0,
sig0,
mu,
alpha,
e0,
solver_type: SolverType::default(),
};
let psi = params.prelim_kepuni().unwrap();
assert_eq!(psi, -15.327414893041848);
let params2 = UniversalKeplerParams {
alpha: 1.642_158_377_771_140_7E-4,
..params
};
let psi = params2.prelim_kepuni().unwrap();
assert_eq!(psi, -73.1875935362658);
let params3 = UniversalKeplerParams {
alpha: 0.0,
..params
};
let psi = params3.prelim_kepuni().unwrap();
assert_eq!(psi, -15.228233329763219);
}
mod kepuni_prop_tests {
use super::*;
use proptest::prelude::*;
fn arb_params() -> impl Strategy<Value = UniversalKeplerParams> {
(
-10.0..10.0f64,
0.1..5.0f64,
-2.0..2.0f64,
0.5..2.0f64,
prop_oneof![(-5.0..-0.01f64), (0.01..5.0f64)],
0.0..3.0f64,
1e-14..1e-8f64,
)
.prop_map(|(dt, r0, sig0, mu, alpha, e0, contr)| {
UniversalKeplerParams {
dt,
r0,
sig0,
mu,
alpha,
e0,
solver_type: SolverType {
params: SolverParams {
convergency: contr,
..Default::default()
},
..Default::default()
},
}
})
}
proptest! {
#[test]
fn prop_prelim_kepuni_behaves_well(params in arb_params()) {
let result = params.prelim_kepuni();
prop_assert!(result.is_some());
prop_assert!(result.unwrap().is_finite());
}
}
proptest! {
#[test]
fn prop_prelim_kepuni_alpha_zero(
dt in -10.0..10.0f64,
r0 in 0.1..5.0f64,
sig0 in -2.0..2.0f64,
mu in 0.5..2.0f64,
e0 in 0.0..3.0f64,
contr in 1e-14..1e-8f64
) {
let params = UniversalKeplerParams { dt, r0, sig0, mu, alpha: 0.0, e0, solver_type: SolverType {
params: SolverParams {
convergency: contr,
..Default::default()
},
..Default::default()
}
};
let result = params.prelim_kepuni();
if let Some(psi0) = result {
prop_assert!(psi0.is_finite());
}
}
}
proptest! {
#[test]
fn prop_sig0_influences_psi(params in arb_params()) {
prop_assume!(params.dt.abs() > 1e-6);
prop_assume!(params.e0 > 1e-6);
prop_assume!(params.r0 > 1e-6);
let mut params_pos = params;
params_pos.sig0 = 0.1;
let mut params_neg = params;
params_neg.sig0 = -0.1;
let res_pos = params_pos.prelim_kepuni();
let res_neg = params_neg.prelim_kepuni();
prop_assume!(res_pos.is_some() && res_neg.is_some());
let diff = (res_pos.unwrap() - res_neg.unwrap()).abs();
prop_assert!(diff >= 0.0);
}
}
}
}