pykep-core 0.1.1

Native Rust astrodynamics core for the pykep-rust port.
Documentation
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
// Copyright (c) 2023-2026 Dario Izzo (dario.izzo@gmail.com)
//                          Advanced Concepts Team, European Space Agency (ESA)
// Copyright (c) 2026 pykep-rust contributors
// SPDX-License-Identifier: MPL-2.0

//! Evaluated Kepler, circular restricted three-body, and bicircular dynamics.
//!
//! The rotating-frame models use nondimensional CR3BP units and the state
//! order `[x, y, z, vx, vy, vz]`. Their primaries lie at `(-mu, 0, 0)` and
//! `(1 - mu, 0, 0)`. BCP adds a Sun whose rotating-frame position is
//! `rho_sun * [cos(omega_sun * t), sin(omega_sun * t), 0]`.
//!
//! This file is an evaluated Rust adaptation of the symbolic systems in
//! `src/ta/kep.cpp`, `src/ta/cr3bp.cpp`, and `src/ta/bcp.cpp` from the pinned
//! pykep/kep3 upstream source.
//!
//! ```
//! use pykep_core::dynamics::KeplerDynamics;
//!
//! let derivative = KeplerDynamics.evaluate(
//!     &[1.0, 0.0, 0.0, 0.0, 1.0, 0.0],
//!     1.0,
//! )?;
//! assert_eq!(derivative, [0.0, 1.0, 0.0, -1.0, -0.0, -0.0]);
//! # Ok::<(), pykep_core::PykepError>(())
//! ```

/// Cartesian and modified-equinoctial Pontryagin dynamics.
pub mod pontryagin;
/// Zero-order-hold low-thrust and solar-sail dynamics.
pub mod zoh;

use crate::error::ensure_finite;
use crate::integration::{
    DifferentiableDynamicsModel, Dop853, DynamicsModel, InitialValueProblem, IntegratorOptions,
    Propagation, SensitivityProblem, SensitivityPropagation,
};
use crate::{CartesianState, Matrix6, PykepError, Result};

const POSITION_DIMENSION: usize = 3;

/// Evaluated two-body Cartesian dynamics.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct KeplerDynamics;

impl KeplerDynamics {
    /// Evaluates the two-body right-hand side for gravitational parameter
    /// `mu`.
    ///
    /// # Errors
    ///
    /// Returns an error for non-finite inputs, non-positive `mu`, collision
    /// with the central body, or a non-finite result.
    pub fn evaluate(&self, state: &CartesianState, mu: f64) -> Result<CartesianState> {
        let mut derivative = [0.0; 6];
        self.rhs(0.0, state, &[mu], &mut derivative)?;
        Ok(derivative)
    }

    /// Propagates a Cartesian state with adaptive DOP853 integration.
    ///
    /// # Errors
    ///
    /// Returns a model-domain or integration error.
    pub fn propagate(
        &self,
        initial_time: f64,
        initial_state: CartesianState,
        final_time: f64,
        mu: f64,
        options: IntegratorOptions,
    ) -> Result<Propagation<6>> {
        Dop853.propagate(
            self,
            InitialValueProblem::new(initial_time, initial_state, final_time, [mu]),
            options,
        )
    }

    /// Propagates a state and its row-major 6 × 6 state-transition matrix.
    ///
    /// # Errors
    ///
    /// Returns a model-domain, Jacobian, or integration error.
    pub fn propagate_with_stm(
        &self,
        initial_time: f64,
        initial_state: CartesianState,
        final_time: f64,
        mu: f64,
        options: IntegratorOptions,
    ) -> Result<SensitivityPropagation<6, 6>> {
        propagate_stm(self, initial_time, initial_state, final_time, [mu], options)
    }
}

impl DynamicsModel<6, 1> for KeplerDynamics {
    const NAME: &'static str = "Kepler dynamics";

    fn validate(&self, time: f64, state: &CartesianState, parameters: &[f64; 1]) -> Result<()> {
        validate_time_state(time, state)?;
        validate_positive("mu", parameters[0])?;
        radius_squared([state[0], state[1], state[2]], "Kepler dynamics radius")?;
        Ok(())
    }

    fn rhs(
        &self,
        time: f64,
        state: &CartesianState,
        parameters: &[f64; 1],
        derivative: &mut CartesianState,
    ) -> Result<()> {
        self.validate(time, state, parameters)?;
        let position = [state[0], state[1], state[2]];
        let (acceleration, _) = point_mass_acceleration_and_gradient(
            position,
            parameters[0],
            "Kepler dynamics radius",
        )?;
        *derivative = [
            state[3],
            state[4],
            state[5],
            acceleration[0],
            acceleration[1],
            acceleration[2],
        ];
        validate_output(Self::NAME, derivative)
    }
}

impl DifferentiableDynamicsModel<6, 1> for KeplerDynamics {
    fn jacobians(
        &self,
        time: f64,
        state: &CartesianState,
        parameters: &[f64; 1],
        state_jacobian: &mut Matrix6,
        parameter_jacobian: &mut [[f64; 1]; 6],
    ) -> Result<()> {
        self.validate(time, state, parameters)?;
        let position = [state[0], state[1], state[2]];
        let (_, gradient) = point_mass_acceleration_and_gradient(
            position,
            parameters[0],
            "Kepler dynamics radius",
        )?;
        *state_jacobian = kinematic_jacobian();
        set_spatial_gradient(state_jacobian, &gradient);
        let radius = radius_squared(position, "Kepler dynamics radius")?.sqrt();
        let inverse_radius_cubed = 1.0 / (radius * radius * radius);
        *parameter_jacobian = [[0.0]; 6];
        for row in 0..POSITION_DIMENSION {
            parameter_jacobian[row + 3][0] = -position[row] * inverse_radius_cubed;
        }
        validate_jacobians(Self::NAME, state_jacobian, parameter_jacobian)
    }
}

/// Evaluated circular restricted three-body dynamics in the synodic frame.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Cr3bpDynamics;

impl Cr3bpDynamics {
    /// Evaluates the CR3BP right-hand side.
    ///
    /// `mu` is the secondary mass divided by the total primary mass and must
    /// lie in `[0, 1]`; the conventional primary ordering uses `mu <= 0.5`.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid inputs, collision with either primary, or
    /// a non-finite result.
    pub fn evaluate(&self, state: &CartesianState, mu: f64) -> Result<CartesianState> {
        let mut derivative = [0.0; 6];
        self.rhs(0.0, state, &[mu], &mut derivative)?;
        Ok(derivative)
    }

    /// Returns the positive effective potential
    /// `U = (x² + y²)/2 + (1-mu)/r1 + mu/r2`.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid inputs or collision with a primary.
    pub fn effective_potential(&self, state: &CartesianState, mu: f64) -> Result<f64> {
        self.validate(0.0, state, &[mu])?;
        let (d1, d2) = primary_displacements(state, mu);
        let r1 = radius_squared(d1, "CR3BP primary-one distance")?.sqrt();
        let r2 = radius_squared(d2, "CR3BP primary-two distance")?.sqrt();
        let potential =
            0.5 * (state[0] * state[0] + state[1] * state[1]) + (1.0 - mu) / r1 + mu / r2;
        finite_output(Self::NAME, potential)
    }

    /// Returns the Jacobi constant `C = 2 U - (vx² + vy² + vz²)`.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid inputs or collision with a primary.
    pub fn jacobi_constant(&self, state: &CartesianState, mu: f64) -> Result<f64> {
        let potential = self.effective_potential(state, mu)?;
        let velocity_squared = state[3] * state[3] + state[4] * state[4] + state[5] * state[5];
        finite_output(Self::NAME, 2.0 * potential - velocity_squared)
    }

    /// Propagates a CR3BP state with adaptive DOP853 integration.
    ///
    /// # Errors
    ///
    /// Returns a model-domain or integration error.
    pub fn propagate(
        &self,
        initial_time: f64,
        initial_state: CartesianState,
        final_time: f64,
        mu: f64,
        options: IntegratorOptions,
    ) -> Result<Propagation<6>> {
        Dop853.propagate(
            self,
            InitialValueProblem::new(initial_time, initial_state, final_time, [mu]),
            options,
        )
    }

    /// Propagates a CR3BP state and its row-major 6 × 6 STM.
    ///
    /// # Errors
    ///
    /// Returns a model-domain, Jacobian, or integration error.
    pub fn propagate_with_stm(
        &self,
        initial_time: f64,
        initial_state: CartesianState,
        final_time: f64,
        mu: f64,
        options: IntegratorOptions,
    ) -> Result<SensitivityPropagation<6, 6>> {
        propagate_stm(self, initial_time, initial_state, final_time, [mu], options)
    }
}

impl DynamicsModel<6, 1> for Cr3bpDynamics {
    const NAME: &'static str = "CR3BP dynamics";

    fn validate(&self, time: f64, state: &CartesianState, parameters: &[f64; 1]) -> Result<()> {
        validate_time_state(time, state)?;
        validate_mass_fraction(parameters[0])?;
        let (d1, d2) = primary_displacements(state, parameters[0]);
        radius_squared(d1, "CR3BP primary-one distance")?;
        radius_squared(d2, "CR3BP primary-two distance")?;
        Ok(())
    }

    fn rhs(
        &self,
        time: f64,
        state: &CartesianState,
        parameters: &[f64; 1],
        derivative: &mut CartesianState,
    ) -> Result<()> {
        self.validate(time, state, parameters)?;
        let mu = parameters[0];
        let (d1, d2) = primary_displacements(state, mu);
        let (a1, _) =
            point_mass_acceleration_and_gradient(d1, 1.0 - mu, "CR3BP primary-one distance")?;
        let (a2, _) = point_mass_acceleration_and_gradient(d2, mu, "CR3BP primary-two distance")?;
        *derivative = [
            state[3],
            state[4],
            state[5],
            2.0 * state[4] + state[0] + a1[0] + a2[0],
            -2.0 * state[3] + state[1] + a1[1] + a2[1],
            a1[2] + a2[2],
        ];
        validate_output(Self::NAME, derivative)
    }
}

impl DifferentiableDynamicsModel<6, 1> for Cr3bpDynamics {
    fn jacobians(
        &self,
        time: f64,
        state: &CartesianState,
        parameters: &[f64; 1],
        state_jacobian: &mut Matrix6,
        parameter_jacobian: &mut [[f64; 1]; 6],
    ) -> Result<()> {
        self.validate(time, state, parameters)?;
        let mu = parameters[0];
        let (d1, d2) = primary_displacements(state, mu);
        let (_, gradient1) =
            point_mass_acceleration_and_gradient(d1, 1.0 - mu, "CR3BP primary-one distance")?;
        let (_, gradient2) =
            point_mass_acceleration_and_gradient(d2, mu, "CR3BP primary-two distance")?;
        *state_jacobian = rotating_kinematic_jacobian();
        add_spatial_gradient(state_jacobian, &gradient1);
        add_spatial_gradient(state_jacobian, &gradient2);

        *parameter_jacobian = [[0.0]; 6];
        let derivative1 =
            moving_mass_parameter_derivative(d1, 1.0 - mu, -1.0, "CR3BP primary-one distance")?;
        let derivative2 =
            moving_mass_parameter_derivative(d2, mu, 1.0, "CR3BP primary-two distance")?;
        for row in 0..POSITION_DIMENSION {
            parameter_jacobian[row + 3][0] = derivative1[row] + derivative2[row];
        }
        validate_jacobians(Self::NAME, state_jacobian, parameter_jacobian)
    }
}

/// Evaluated bicircular-problem dynamics in the Earth–Moon synodic frame.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct BcpDynamics;

impl BcpDynamics {
    /// Evaluates the time-dependent BCP right-hand side.
    ///
    /// Parameters are `[mu, mu_sun, rho_sun, omega_sun]`.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid parameters, collision with a massive body,
    /// or a non-finite result.
    pub fn evaluate(
        &self,
        time: f64,
        state: &CartesianState,
        parameters: [f64; 4],
    ) -> Result<CartesianState> {
        let mut derivative = [0.0; 6];
        self.rhs(time, state, &parameters, &mut derivative)?;
        Ok(derivative)
    }

    /// Propagates a BCP state with adaptive DOP853 integration.
    ///
    /// # Errors
    ///
    /// Returns a model-domain or integration error.
    pub fn propagate(
        &self,
        initial_time: f64,
        initial_state: CartesianState,
        final_time: f64,
        parameters: [f64; 4],
        options: IntegratorOptions,
    ) -> Result<Propagation<6>> {
        Dop853.propagate(
            self,
            InitialValueProblem::new(initial_time, initial_state, final_time, parameters),
            options,
        )
    }

    /// Propagates a BCP state and its row-major 6 × 6 STM.
    ///
    /// # Errors
    ///
    /// Returns a model-domain, Jacobian, or integration error.
    pub fn propagate_with_stm(
        &self,
        initial_time: f64,
        initial_state: CartesianState,
        final_time: f64,
        parameters: [f64; 4],
        options: IntegratorOptions,
    ) -> Result<SensitivityPropagation<6, 6>> {
        propagate_stm(
            self,
            initial_time,
            initial_state,
            final_time,
            parameters,
            options,
        )
    }
}

impl DynamicsModel<6, 4> for BcpDynamics {
    const NAME: &'static str = "BCP dynamics";

    fn validate(&self, time: f64, state: &CartesianState, parameters: &[f64; 4]) -> Result<()> {
        validate_time_state(time, state)?;
        validate_mass_fraction(parameters[0])?;
        validate_non_negative("mu_sun", parameters[1])?;
        validate_positive("rho_sun", parameters[2])?;
        ensure_finite("omega_sun", parameters[3])?;
        let (d1, d2) = primary_displacements(state, parameters[0]);
        radius_squared(d1, "BCP primary-one distance")?;
        radius_squared(d2, "BCP primary-two distance")?;
        let (_, _, sun_displacement) = sun_geometry(time, state, parameters[2], parameters[3]);
        radius_squared(sun_displacement, "BCP Sun distance")?;
        Ok(())
    }

    fn rhs(
        &self,
        time: f64,
        state: &CartesianState,
        parameters: &[f64; 4],
        derivative: &mut CartesianState,
    ) -> Result<()> {
        self.validate(time, state, parameters)?;
        let [mu, mu_sun, rho_sun, omega_sun] = *parameters;
        let (d1, d2) = primary_displacements(state, mu);
        let (a1, _) =
            point_mass_acceleration_and_gradient(d1, 1.0 - mu, "BCP primary-one distance")?;
        let (a2, _) = point_mass_acceleration_and_gradient(d2, mu, "BCP primary-two distance")?;
        let (sun_direction, _, sun_displacement) = sun_geometry(time, state, rho_sun, omega_sun);
        let (sun_acceleration, _) =
            point_mass_acceleration_and_gradient(sun_displacement, mu_sun, "BCP Sun distance")?;
        let indirect_scale = -mu_sun / (rho_sun * rho_sun);
        *derivative = [
            state[3],
            state[4],
            state[5],
            2.0 * state[4]
                + state[0]
                + a1[0]
                + a2[0]
                + sun_acceleration[0]
                + indirect_scale * sun_direction[0],
            -2.0 * state[3]
                + state[1]
                + a1[1]
                + a2[1]
                + sun_acceleration[1]
                + indirect_scale * sun_direction[1],
            a1[2] + a2[2] + sun_acceleration[2],
        ];
        validate_output(Self::NAME, derivative)
    }
}

impl DifferentiableDynamicsModel<6, 4> for BcpDynamics {
    fn jacobians(
        &self,
        time: f64,
        state: &CartesianState,
        parameters: &[f64; 4],
        state_jacobian: &mut Matrix6,
        parameter_jacobian: &mut [[f64; 4]; 6],
    ) -> Result<()> {
        self.validate(time, state, parameters)?;
        let [mu, mu_sun, rho_sun, omega_sun] = *parameters;
        let (d1, d2) = primary_displacements(state, mu);
        let (_, gradient1) =
            point_mass_acceleration_and_gradient(d1, 1.0 - mu, "BCP primary-one distance")?;
        let (_, gradient2) =
            point_mass_acceleration_and_gradient(d2, mu, "BCP primary-two distance")?;
        let (sun_direction, sun_direction_rate, sun_displacement) =
            sun_geometry(time, state, rho_sun, omega_sun);
        let (_, sun_gradient) =
            point_mass_acceleration_and_gradient(sun_displacement, mu_sun, "BCP Sun distance")?;
        *state_jacobian = rotating_kinematic_jacobian();
        add_spatial_gradient(state_jacobian, &gradient1);
        add_spatial_gradient(state_jacobian, &gradient2);
        add_spatial_gradient(state_jacobian, &sun_gradient);

        *parameter_jacobian = [[0.0; 4]; 6];
        let mu_derivative1 =
            moving_mass_parameter_derivative(d1, 1.0 - mu, -1.0, "BCP primary-one distance")?;
        let mu_derivative2 =
            moving_mass_parameter_derivative(d2, mu, 1.0, "BCP primary-two distance")?;
        let sun_radius = radius_squared(sun_displacement, "BCP Sun distance")?.sqrt();
        let inverse_sun_radius_cubed = 1.0 / sun_radius.powi(3);
        let inverse_rho_squared = 1.0 / (rho_sun * rho_sun);
        let inverse_rho_cubed = inverse_rho_squared / rho_sun;
        for row in 0..POSITION_DIMENSION {
            parameter_jacobian[row + 3][0] = mu_derivative1[row] + mu_derivative2[row];
            parameter_jacobian[row + 3][1] = -sun_displacement[row] * inverse_sun_radius_cubed
                - sun_direction[row] * inverse_rho_squared;
            let gradient_times_direction = dot_row(&sun_gradient, row, &sun_direction);
            parameter_jacobian[row + 3][2] =
                -gradient_times_direction + 2.0 * mu_sun * sun_direction[row] * inverse_rho_cubed;
            let gradient_times_rate = dot_row(&sun_gradient, row, &sun_direction_rate);
            parameter_jacobian[row + 3][3] = -rho_sun * time * gradient_times_rate
                - mu_sun * time * sun_direction_rate[row] * inverse_rho_squared;
        }
        validate_jacobians(Self::NAME, state_jacobian, parameter_jacobian)
    }
}

fn propagate_stm<M, const P: usize>(
    model: &M,
    initial_time: f64,
    initial_state: CartesianState,
    final_time: f64,
    parameters: [f64; P],
    options: IntegratorOptions,
) -> Result<SensitivityPropagation<6, 6>>
where
    M: DifferentiableDynamicsModel<6, P>,
{
    Dop853.propagate_with_sensitivities(
        model,
        SensitivityProblem {
            nominal: InitialValueProblem::new(initial_time, initial_state, final_time, parameters),
            initial_sensitivities: identity6(),
            parameter_seeds: [[0.0; 6]; P],
        },
        options,
    )
}

const fn identity6() -> Matrix6 {
    let mut matrix = [[0.0; 6]; 6];
    let mut index = 0;
    while index < 6 {
        matrix[index][index] = 1.0;
        index += 1;
    }
    matrix
}

fn validate_time_state(time: f64, state: &CartesianState) -> Result<()> {
    ensure_finite("time", time)?;
    for &value in state {
        ensure_finite("state", value)?;
    }
    Ok(())
}

fn validate_positive(parameter: &'static str, value: f64) -> Result<()> {
    ensure_finite(parameter, value)?;
    if value > 0.0 {
        Ok(())
    } else {
        Err(PykepError::InvalidInput {
            parameter,
            reason: "must be greater than zero".into(),
        })
    }
}

fn validate_non_negative(parameter: &'static str, value: f64) -> Result<()> {
    ensure_finite(parameter, value)?;
    if value >= 0.0 {
        Ok(())
    } else {
        Err(PykepError::InvalidInput {
            parameter,
            reason: "must be non-negative".into(),
        })
    }
}

fn validate_mass_fraction(mu: f64) -> Result<()> {
    ensure_finite("mu", mu)?;
    if (0.0..=1.0).contains(&mu) {
        Ok(())
    } else {
        Err(PykepError::InvalidInput {
            parameter: "mu",
            reason: "must lie in the closed interval [0, 1]".into(),
        })
    }
}

fn primary_displacements(state: &CartesianState, mu: f64) -> ([f64; 3], [f64; 3]) {
    (
        [state[0] + mu, state[1], state[2]],
        [state[0] + mu - 1.0, state[1], state[2]],
    )
}

fn sun_geometry(
    time: f64,
    state: &CartesianState,
    rho_sun: f64,
    omega_sun: f64,
) -> ([f64; 3], [f64; 3], [f64; 3]) {
    let angle = omega_sun * time;
    let direction = [angle.cos(), angle.sin(), 0.0];
    let direction_rate = [-angle.sin(), angle.cos(), 0.0];
    let displacement = [
        state[0] - rho_sun * direction[0],
        state[1] - rho_sun * direction[1],
        state[2],
    ];
    (direction, direction_rate, displacement)
}

fn radius_squared(vector: [f64; 3], operation: &'static str) -> Result<f64> {
    let squared = vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2];
    if squared == 0.0 {
        return Err(PykepError::SingularGeometry { operation });
    }
    if squared.is_finite() {
        Ok(squared)
    } else {
        Err(PykepError::NumericalOverflow { operation })
    }
}

fn point_mass_acceleration_and_gradient(
    displacement: [f64; 3],
    mass: f64,
    operation: &'static str,
) -> Result<([f64; 3], [[f64; 3]; 3])> {
    let radius_squared = radius_squared(displacement, operation)?;
    let radius = radius_squared.sqrt();
    let inverse_radius_cubed = 1.0 / (radius_squared * radius);
    let inverse_radius_fifth = inverse_radius_cubed / radius_squared;
    let mut acceleration = [0.0; 3];
    let mut gradient = [[0.0; 3]; 3];
    for row in 0..POSITION_DIMENSION {
        acceleration[row] = -mass * displacement[row] * inverse_radius_cubed;
        for column in 0..POSITION_DIMENSION {
            let identity = f64::from(row == column);
            gradient[row][column] = mass
                * (3.0 * displacement[row] * displacement[column] * inverse_radius_fifth
                    - identity * inverse_radius_cubed);
        }
    }
    if acceleration
        .iter()
        .chain(gradient.iter().flatten())
        .all(|value| value.is_finite())
    {
        Ok((acceleration, gradient))
    } else {
        Err(PykepError::NumericalOverflow { operation })
    }
}

fn moving_mass_parameter_derivative(
    displacement: [f64; 3],
    mass: f64,
    mass_derivative: f64,
    operation: &'static str,
) -> Result<[f64; 3]> {
    let radius_squared = radius_squared(displacement, operation)?;
    let radius = radius_squared.sqrt();
    let inverse_radius_cubed = 1.0 / (radius_squared * radius);
    let inverse_radius_fifth = inverse_radius_cubed / radius_squared;
    let mut derivative = [0.0; 3];
    for row in 0..POSITION_DIMENSION {
        let displacement_derivative = f64::from(row == 0);
        derivative[row] = -mass_derivative * displacement[row] * inverse_radius_cubed
            - mass
                * (displacement_derivative * inverse_radius_cubed
                    - 3.0 * displacement[row] * displacement[0] * inverse_radius_fifth);
    }
    if derivative.iter().all(|value| value.is_finite()) {
        Ok(derivative)
    } else {
        Err(PykepError::NumericalOverflow { operation })
    }
}

const fn kinematic_jacobian() -> Matrix6 {
    let mut matrix = [[0.0; 6]; 6];
    matrix[0][3] = 1.0;
    matrix[1][4] = 1.0;
    matrix[2][5] = 1.0;
    matrix
}

const fn rotating_kinematic_jacobian() -> Matrix6 {
    let mut matrix = kinematic_jacobian();
    matrix[3][0] = 1.0;
    matrix[4][1] = 1.0;
    matrix[3][4] = 2.0;
    matrix[4][3] = -2.0;
    matrix
}

fn set_spatial_gradient(jacobian: &mut Matrix6, gradient: &[[f64; 3]; 3]) {
    for row in 0..POSITION_DIMENSION {
        for column in 0..POSITION_DIMENSION {
            jacobian[row + 3][column] = gradient[row][column];
        }
    }
}

fn add_spatial_gradient(jacobian: &mut Matrix6, gradient: &[[f64; 3]; 3]) {
    for row in 0..POSITION_DIMENSION {
        for column in 0..POSITION_DIMENSION {
            jacobian[row + 3][column] += gradient[row][column];
        }
    }
}

fn dot_row(matrix: &[[f64; 3]; 3], row: usize, vector: &[f64; 3]) -> f64 {
    matrix[row][0] * vector[0] + matrix[row][1] * vector[1] + matrix[row][2] * vector[2]
}

fn validate_output(model: &'static str, values: &CartesianState) -> Result<()> {
    if values.iter().all(|value| value.is_finite()) {
        Ok(())
    } else {
        Err(PykepError::NumericalOverflow { operation: model })
    }
}

fn finite_output(model: &'static str, value: f64) -> Result<f64> {
    if value.is_finite() {
        Ok(value)
    } else {
        Err(PykepError::NumericalOverflow { operation: model })
    }
}

fn validate_jacobians<const P: usize>(
    model: &'static str,
    state_jacobian: &Matrix6,
    parameter_jacobian: &[[f64; P]; 6],
) -> Result<()> {
    if state_jacobian
        .iter()
        .flatten()
        .chain(parameter_jacobian.iter().flatten())
        .all(|value| value.is_finite())
    {
        Ok(())
    } else {
        Err(PykepError::NumericalOverflow { operation: model })
    }
}