differential_equations/ode/numerical_method.rs
1//! Numerical Methods for solving ordinary differential equations (ODEs).
2
3use crate::{
4 Error, Status,
5 alias::Evals,
6 ode::ODE,
7 traits::{CallBackData, Real, State},
8};
9
10/// OrdinaryNumericalMethod Trait for ODE NumericalMethods
11///
12/// ODE NumericalMethods implement this trait to solve ordinary differential equations.
13/// This step function is called iteratively to solve the ODE.
14/// By implementing this trait, different functions can use a user provided
15/// ODE solver to solve the ODE that fits their requirements.
16///
17pub trait OrdinaryNumericalMethod<T, V, D = String>
18where
19 T: Real,
20 V: State<T>,
21 D: CallBackData,
22{
23 /// Initialize OrdinaryNumericalMethod before solving ODE
24 ///
25 /// # Arguments
26 /// * `system` - System of ODEs to solve.
27 /// * `t0` - Initial time.
28 /// * `tf` - Final time.
29 /// * `y` - Initial state.
30 ///
31 /// # Returns
32 /// * Result<Evals, Error<T, V>> - Ok if initialization is successful,
33 ///
34 fn init<F>(&mut self, ode: &F, t0: T, tf: T, y: &V) -> Result<Evals, Error<T, V>>
35 where
36 F: ODE<T, V, D>;
37
38 /// Step through solving the ODE by one step
39 ///
40 /// # Arguments
41 /// * `system` - System of ODEs to solve.
42 ///
43 /// # Returns
44 /// * Result<Evals, Errors<T, V>> - Ok if step is successful with the number of function evaluations,
45 ///
46 fn step<F>(&mut self, ode: &F) -> Result<Evals, Error<T, V>>
47 where
48 F: ODE<T, V, D>;
49
50 // Access fields of the solver
51
52 /// Access time of last accepted step
53 fn t(&self) -> T;
54
55 /// Access solution of last accepted step
56 fn y(&self) -> &V;
57
58 /// Access time of previous accepted step
59 fn t_prev(&self) -> T;
60
61 /// Access solution of previous accepted step
62 fn y_prev(&self) -> &V;
63
64 /// Access step size of next step
65 fn h(&self) -> T;
66
67 /// Set step size of next step
68 fn set_h(&mut self, h: T);
69
70 /// Status of solver
71 fn status(&self) -> &Status<T, V, D>;
72
73 /// Set status of solver
74 fn set_status(&mut self, status: Status<T, V, D>);
75}