differential_equations/ode/numerical_method.rs
1//! Numerical methods for ODEs.
2
3use crate::{
4 error::Error,
5 ode::ODE,
6 stats::Evals,
7 status::Status,
8 traits::{CallBackData, Real, State},
9};
10
11/// Trait for ODE solvers.
12///
13/// Implemented by types that can solve ODEs via repeated calls to `step`.
14pub trait OrdinaryNumericalMethod<T, Y, D = String>
15where
16 T: Real,
17 Y: State<T>,
18 D: CallBackData,
19{
20 /// Initialize the solver before integration
21 ///
22 /// # Arguments
23 /// * `system` - System of ODEs to solve.
24 /// * `t0` - Initial time.
25 /// * `tf` - Final time.
26 /// * `y` - Initial state.
27 ///
28 /// # Returns
29 /// * Result<Evals, Error<T, Y>> - Ok if initialization is successful,
30 ///
31 fn init<F>(&mut self, ode: &F, t0: T, tf: T, y: &Y) -> Result<Evals, Error<T, Y>>
32 where
33 F: ODE<T, Y, D>;
34
35 /// Advance the solution by one step
36 ///
37 /// # Arguments
38 /// * `system` - System of ODEs to solve.
39 ///
40 /// # Returns
41 /// * Result<Evals, Errors<T, Y>> - Ok if step is successful with the number of function evaluations,
42 ///
43 fn step<F>(&mut self, ode: &F) -> Result<Evals, Error<T, Y>>
44 where
45 F: ODE<T, Y, D>;
46
47 // Accessors
48
49 /// Time of last accepted step
50 fn t(&self) -> T;
51
52 /// State at last accepted step
53 fn y(&self) -> &Y;
54
55 /// Time of previous accepted step
56 fn t_prev(&self) -> T;
57
58 /// State at previous accepted step
59 fn y_prev(&self) -> &Y;
60
61 /// Step size for next step
62 fn h(&self) -> T;
63
64 /// Set step size for next step
65 fn set_h(&mut self, h: T);
66
67 /// Current solver status
68 fn status(&self) -> &Status<T, Y, D>;
69
70 /// Set solver status
71 fn set_status(&mut self, status: Status<T, Y, D>);
72}