Skip to main content

conspire/math/integrate/ode/dynamics/explicit/
mod.rs

1pub(crate) mod stacked;
2pub(crate) mod velocity_verlet;
3
4use crate::{
5    math::{
6        Derivative, Differentiable, Quantity, Scalar, Tensor, TensorVec,
7        integrate::{IntegrationError, Spectrum, Times},
8    },
9    units::Time,
10};
11use std::fmt::Debug;
12
13/// Explicit integrators for dynamics.
14/// ```math
15/// \ddot{x} = a(t, x, \dot{x})
16/// ```
17pub trait ExplicitDynamics<X, UX, UV, UA, T = Time>
18where
19    Self: Debug,
20    X: Differentiable<T> + Tensor,
21    Derivative<X, T>: Differentiable<T> + Tensor,
22    UX: TensorVec<Item = X>,
23    UV: TensorVec<Item = Derivative<X, T>>,
24    UA: TensorVec<Item = Derivative<Derivative<X, T>, T>>,
25{
26    #[doc = include_str!("doc.md")]
27    fn integrate(
28        &self,
29        function: impl FnMut(
30            Quantity<T>,
31            &X,
32            &Derivative<X, T>,
33        ) -> Result<Derivative<Derivative<X, T>, T>, String>,
34        time: &[Quantity<T>],
35        initial_position: X,
36        initial_velocity: Derivative<X, T>,
37    ) -> Result<(Times<T>, UX, UV, UA), IntegrationError>;
38    #[doc = include_str!("bounded.md")]
39    fn integrate_bounded(
40        &self,
41        function: impl FnMut(
42            Quantity<T>,
43            &X,
44            &Derivative<X, T>,
45        ) -> Result<Derivative<Derivative<X, T>, T>, String>,
46        bound: impl FnMut(Quantity<T>, &X, &Derivative<X, T>) -> Result<Quantity<T>, String>,
47        safety: Scalar,
48        time: &[Quantity<T>],
49        initial_position: X,
50        initial_velocity: Derivative<X, T>,
51    ) -> Result<(Times<T>, UX, UV, UA), IntegrationError>;
52}
53
54/// Explicit integrators for dynamics coupled to a first-order state.
55/// ```math
56/// \ddot{x} = a(t, x, \dot{x}, y),\quad \dot{y} = f(t, x, \dot{x}, y)
57/// ```
58pub trait ExplicitDynamicsCoupled<X, Y, UX, UV, UA, UY, UR, T = Time>
59where
60    Self: Debug,
61    X: Differentiable<T> + Tensor,
62    Derivative<X, T>: Differentiable<T> + Tensor,
63    Y: Differentiable<T> + Tensor,
64    UX: TensorVec<Item = X>,
65    UV: TensorVec<Item = Derivative<X, T>>,
66    UA: TensorVec<Item = Derivative<Derivative<X, T>, T>>,
67    UY: TensorVec<Item = Y>,
68    UR: TensorVec<Item = Derivative<Y, T>>,
69{
70    /// Solves an initial value problem for dynamics coupled to a first-order state, given the
71    /// acceleration and the rate of the state as a function of time, position, velocity, and
72    /// state.
73    ///
74    /// The solution contains the positions, velocities, accelerations, states, and rates of the
75    /// states at each time. The time step is that of the integrator when `time` holds only the
76    /// initial and final times, and otherwise `time` is the sequence of steps.
77    #[expect(clippy::type_complexity)]
78    fn integrate_coupled(
79        &self,
80        function: impl FnMut(
81            Quantity<T>,
82            &X,
83            &Derivative<X, T>,
84            &Y,
85        )
86            -> Result<(Derivative<Derivative<X, T>, T>, Derivative<Y, T>), String>,
87        time: &[Quantity<T>],
88        initial_position: X,
89        initial_velocity: Derivative<X, T>,
90        initial_state: Y,
91    ) -> Result<(Times<T>, UX, UV, UA, UY, UR), IntegrationError>;
92    /// Solves an initial value problem like [`integrate_coupled`](Self::integrate_coupled), subject to a
93    /// stability bound.
94    ///
95    /// At each step, `bound` reports the spectrum of the coupled system at the current state,
96    /// which is real for diffusion, imaginary for waves, and otherwise complex. The time step
97    /// may use at most the fraction `safety` of the stability limit, where `safety` is in
98    /// $`(0, 1]`$. A time step above it is an error.
99    #[expect(clippy::type_complexity)]
100    #[expect(clippy::too_many_arguments)]
101    fn integrate_coupled_bounded(
102        &self,
103        function: impl FnMut(
104            Quantity<T>,
105            &X,
106            &Derivative<X, T>,
107            &Y,
108        )
109            -> Result<(Derivative<Derivative<X, T>, T>, Derivative<Y, T>), String>,
110        bound: impl FnMut(Quantity<T>, &X, &Derivative<X, T>, &Y) -> Result<Spectrum<T>, String>,
111        safety: Scalar,
112        time: &[Quantity<T>],
113        initial_position: X,
114        initial_velocity: Derivative<X, T>,
115        initial_state: Y,
116    ) -> Result<(Times<T>, UX, UV, UA, UY, UR), IntegrationError>;
117}