9 Ordinary Differential Equation
9.1 Introduce ODE Trait & Structure
9.1.1 ODE Trait
ODEstructures are divided by two kindsExplicitODEImplicitODE
ODEtrait is given aspub trait ODE { type Records; type Vector; type Param; type ODEMethod; fn mut_update(&mut self); fn integrate(&mut self) -> Self::Records; fn set_initial_condition<T: Real>(&mut self, init: State<T>) -> &mut Self; fn set_boundary_condition<T: Real>( &mut self, bound1: (State<T>, BoundaryCondition), bound2: (State<T>, BoundaryCondition), ) -> &mut Self; fn set_step_size(&mut self, dt: f64) -> &mut Self; fn set_method(&mut self, method: Self::ODEMethod) -> &mut Self; fn set_stop_condition(&mut self, f: fn(&Self) -> bool) -> &mut Self; fn set_times(&mut self, n: usize) -> &mut Self; fn check_enough(&self) -> bool; }
Records: The type to save results of ODE. UsuallyMatrixis used.Vector: Vector can be below things.Vec<f64>: Used forExplicitODEVec<Dual>: Used forImplicitODE
Param: Also it can bef64orDualODEMethod: Method for solving ODEExMethod: Explicit methodEuler: Euler first orderRK4: Runge Kutta 4th order
ImMethod: Implicit method (to be implemented)BDF: Backward Euler 1st orderGL4: Gauss Legendre 4th order
9.1.2 State<T> structure
Tcan bef64orDualparamis parameter for ODE. Usually it is represented by time.valueis value of each node.derivis value of derivative of each node.
For example,
\[ \frac{dy_n}{dt} = f(t, y_n) \]
- \(t\) is
param - \(y_n\) is
value - \(f(t,y_n)\) is
deriv
Methods for State<T> are as follows.
to_f64(&self) -> State<f64>to_dual(&self) -> State<Dual>new(T, Vec<T>, Vec<T>) -> Self
9.1.3 ExplicitODE struct
ExplicitODE is given as follow :
#[derive(Clone)]
pub struct ExplicitODE {
state: State<f64>,
func: fn(&mut State<f64>),
step_size: f64,
method: ExMethod,
init_cond: State<f64>,
bound_cond1: (State<f64>, BoundaryCondition),
bound_cond2: (State<f64>, BoundaryCondition),
stop_cond: fn(&Self) -> bool,
times: usize,
to_use: HashMap<ToUse, bool>,
}state: Current param, value, derivativefunc: Function to updatestateinit_cond: Initial conditionbound_cond1: If boundary problem, then first boundary conditionbound_cond2: second boundary conditionstop_cond: Stop condition (stop beforetimes)times: How many times do you want to update?to_use: Just check whether information is enough