Began adding the rosenbrock integrator
This commit is contained in:
40
src/ode.rs
40
src/ode.rs
@@ -4,37 +4,32 @@ use nalgebra::SVector;
|
||||
///
|
||||
/// The user will have to define their own system. They are free to add params to their system
|
||||
/// definition and use those in the derivative function
|
||||
pub trait SystemTrait<T,const D: usize> {
|
||||
pub trait SystemTrait<T, const D: usize> {
|
||||
fn derivative(&self, t: T, y: SVector<T,D>) -> SVector<T,D>;
|
||||
}
|
||||
|
||||
/// The basic ODE object that will be passed around. The type (T) and the size (D) will be
|
||||
/// determined upon creation of the object
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ODE<T,F, const D: usize> where F: SystemTrait<T,D>{
|
||||
pub f: F,
|
||||
pub y: SVector<T,D>,
|
||||
pub t: T,
|
||||
pub t0: T,
|
||||
pub t_end: T,
|
||||
pub h: T,
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ODE<'a, const D: usize> {
|
||||
pub f: &'a dyn Fn(f64, SVector<f64,D>) -> SVector<f64,D>,
|
||||
pub y: SVector<f64,D>,
|
||||
pub t: f64,
|
||||
pub t0: f64,
|
||||
pub t_end: f64,
|
||||
pub h: f64,
|
||||
pub finished: bool,
|
||||
}
|
||||
|
||||
impl<T, F, const D: usize> ODE<T,F,D>
|
||||
where
|
||||
F: SystemTrait<T,D>,
|
||||
T: Copy + From<f64>,
|
||||
f64: From<T>,
|
||||
{
|
||||
pub fn new(f: F, t0: T, t_end: T, y0: SVector<T,D>) -> Self {
|
||||
impl<'a, const D: usize> ODE<'a,D> {
|
||||
pub fn new(f: &'a (dyn Fn(f64, SVector<f64,D>) -> SVector<f64,D>), t0: f64, t_end: f64, y0: SVector<f64,D>) -> Self {
|
||||
Self {
|
||||
f: f,
|
||||
y: y0,
|
||||
t: t0,
|
||||
t0: t0,
|
||||
t_end: t_end,
|
||||
h: 0.001.into(),
|
||||
h: 0.001,
|
||||
finished: false,
|
||||
}
|
||||
}
|
||||
@@ -48,17 +43,12 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_ode_creation() {
|
||||
struct System {}
|
||||
fn derivative(_t: f64, y: Vector3<f64>) -> Vector3<f64> { -y }
|
||||
|
||||
impl SystemTrait<f64,3> for System {
|
||||
fn derivative(&self, _t: f64, y: Vector3<f64>) -> Vector3<f64> { -y }
|
||||
}
|
||||
|
||||
let system = System {};
|
||||
let y0 = Vector3::new(1.0, 0.0, 0.0);
|
||||
let ode = ODE::new(system, 0.0, 10.0, y0);
|
||||
let ode = ODE::new(&derivative, 0.0, 10.0, y0);
|
||||
|
||||
assert!(ode.f.derivative(0.0, y0) == Vector3::new(-1.0, 0.0, 0.0));
|
||||
assert!((ode.f)(0.0, y0) == Vector3::new(-1.0, 0.0, 0.0));
|
||||
assert!(ode.y == Vector3::new(1.0, 0.0, 0.0));
|
||||
assert!(ode.t == 0.0);
|
||||
assert!(!ode.finished);
|
||||
|
||||
Reference in New Issue
Block a user