Struct diffsol::ode_solver::equations::OdeSolverEquations

source ·
pub struct OdeSolverEquations<M, Rhs, Init, Mass = UnitCallable<M>, Root = UnitCallable<M>>
where M: Matrix, Rhs: NonLinearOp<M = M, V = M::V, T = M::T>, Mass: LinearOp<M = M, V = M::V, T = M::T>, Root: NonLinearOp<M = M, V = M::V, T = M::T>, Init: ConstantOp<M = M, V = M::V, T = M::T>,
{ /* private fields */ }
Expand description

This struct implements the ODE equation trait OdeEquations for a given right-hand side op, mass op, optional root op, and initial condition function. While the crate::OdeBuilder struct is the easiest way to define an ODE problem, occasionally a user might want to use their own structs that define the equations instead of closures or the DiffSL languave, and this can be done using OdeSolverEquations.

The main traits that you need to implement are the crate::Op and NonLinearOp trait, which define a nonlinear operator or function F that maps an input vector x to an output vector y, (i.e. y = F(x)). Once you have implemented this trait, you can then pass an instance of your struct to the rhs argument of the Self::new method. Once you have created an instance of OdeSolverEquations, you can then use crate::OdeSolverProblem::new to create a problem.

For example:

use std::rc::Rc;
use diffsol::{Bdf, OdeSolverState, OdeSolverMethod, NonLinearOp, OdeSolverEquations, OdeSolverProblem, Op, UnitCallable, ConstantClosure};
type M = nalgebra::DMatrix<f64>;
type V = nalgebra::DVector<f64>;

struct MyProblem;
impl Op for MyProblem {
   type V = V;
   type T = f64;
   type M = M;
   fn nstates(&self) -> usize {
      1
   }
   fn nout(&self) -> usize {
      1
   }
}
   
// implement rhs equations for the problem
impl NonLinearOp for MyProblem {
   fn call_inplace(&self, x: &V, _t: f64, y: &mut V) {
      y[0] = -0.1 * x[0];
  }
   fn jac_mul_inplace(&self, x: &V, _t: f64, v: &V, y: &mut V) {
     y[0] = -0.1 * v[0];
  }
}


let rhs = Rc::new(MyProblem);

// use the provided constant closure to define the initial condition
let init_fn = |p: &V, _t: f64| V::from_vec(vec![1.0]);
let init = Rc::new(ConstantClosure::new(init_fn, Rc::new(V::from_vec(vec![]))));

// we don't have a mass matrix or root function, so we can set to None
let mass: Option<Rc<UnitCallable<M>>> = None;
let root: Option<Rc<UnitCallable<M>>> = None;

let p = Rc::new(V::from_vec(vec![]));
let eqn = OdeSolverEquations::new(rhs, mass, root, init, p);

let rtol = 1e-6;
let atol = V::from_vec(vec![1e-6]);
let t0 = 0.0;
let h0 = 0.1;
let with_sensitivity = false;
let sensitivity_error_control = false;
let problem = OdeSolverProblem::new(eqn, rtol, atol, t0, h0, with_sensitivity, sensitivity_error_control).unwrap();

let mut solver = Bdf::default();
let t = 0.4;
let state = OdeSolverState::new(&problem, &solver).unwrap();
solver.set_problem(state, &problem);
while solver.state().unwrap().t <= t {
   solver.step().unwrap();
}
let y = solver.interpolate(t);

Implementations§

source§

impl<M, Rhs, Init, Mass, Root> OdeSolverEquations<M, Rhs, Init, Mass, Root>
where M: Matrix, Rhs: NonLinearOp<M = M, V = M::V, T = M::T>, Mass: LinearOp<M = M, V = M::V, T = M::T>, Root: NonLinearOp<M = M, V = M::V, T = M::T>, Init: ConstantOp<M = M, V = M::V, T = M::T>,

source

pub fn new( rhs: Rc<Rhs>, mass: Option<Rc<Mass>>, root: Option<Rc<Root>>, init: Rc<Init>, p: Rc<M::V> ) -> Self

Trait Implementations§

source§

impl<M, Rhs, Init, Mass, Root> OdeEquations for OdeSolverEquations<M, Rhs, Init, Mass, Root>
where M: Matrix, Rhs: NonLinearOp<M = M, V = M::V, T = M::T>, Mass: LinearOp<M = M, V = M::V, T = M::T>, Root: NonLinearOp<M = M, V = M::V, T = M::T>, Init: ConstantOp<M = M, V = M::V, T = M::T>,

§

type T = <M as MatrixCommon>::T

§

type V = <M as MatrixCommon>::V

§

type M = M

§

type Rhs = Rhs

§

type Mass = Mass

§

type Root = Root

§

type Init = Init

source§

fn rhs(&self) -> &Rc<Self::Rhs>

returns the right-hand side function F(t, y) as a NonLinearOp
source§

fn mass(&self) -> Option<&Rc<Self::Mass>>

returns the mass matrix M as a LinearOp
source§

fn root(&self) -> Option<&Rc<Self::Root>>

source§

fn init(&self) -> &Rc<Self::Init>

returns the initial condition, i.e. y(t), where t is the initial time
source§

fn set_params(&mut self, p: Self::V)

The parameters of the ODE equations are assumed to be constant. This function sets the parameters to the given value before solving the ODE. Note that set_params must always be called before calling any of the other functions in this trait.

Auto Trait Implementations§

§

impl<M, Rhs, Init, Mass, Root> Freeze for OdeSolverEquations<M, Rhs, Init, Mass, Root>

§

impl<M, Rhs, Init, Mass, Root> RefUnwindSafe for OdeSolverEquations<M, Rhs, Init, Mass, Root>

§

impl<M, Rhs, Init, Mass = UnitCallable<M>, Root = UnitCallable<M>> !Send for OdeSolverEquations<M, Rhs, Init, Mass, Root>

§

impl<M, Rhs, Init, Mass = UnitCallable<M>, Root = UnitCallable<M>> !Sync for OdeSolverEquations<M, Rhs, Init, Mass, Root>

§

impl<M, Rhs, Init, Mass, Root> Unpin for OdeSolverEquations<M, Rhs, Init, Mass, Root>

§

impl<M, Rhs, Init, Mass, Root> UnwindSafe for OdeSolverEquations<M, Rhs, Init, Mass, Root>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.