Struct diffsol::ode_solver::builder::OdeBuilder

source ·
pub struct OdeBuilder { /* private fields */ }
Expand description

Builder for ODE problems. Use methods to set parameters and then call one of the build methods when done.

Implementations§

source§

impl OdeBuilder

Builder for ODE problems. Use methods to set parameters and then call one of the build methods when done.

§Example

use diffsol::{OdeBuilder, Bdf, OdeSolverState, OdeSolverMethod};
type M = nalgebra::DMatrix<f64>;

let problem = OdeBuilder::new()
  .rtol(1e-6)
  .p([0.1])
  .build_ode::<M, _, _, _>(
    // dy/dt = -ay
    |x, p, t, y| {
      y[0] = -p[0] * x[0];
    },
    // Jv = -av
    |x, p, t, v, y| {
      y[0] = -p[0] * v[0];
    },
    // y(0) = 1
   |p, t| {
      nalgebra::DVector::from_vec(vec![1.0])
   },
  ).unwrap();

let mut solver = Bdf::default();
let t = 0.4;
let mut 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);
source

pub fn new() -> Self

Create a new builder with default parameters:

  • t0 = 0.0
  • h0 = 1.0
  • rtol = 1e-6
  • atol = [1e-6]
  • p = []
  • use_coloring = false
  • constant_mass = false
source

pub fn t0(self, t0: f64) -> Self

Set the initial time.

source

pub fn sensitivities(self, sensitivities: bool) -> Self

source

pub fn sensitivities_error_control( self, sensitivities_error_control: bool ) -> Self

source

pub fn h0(self, h0: f64) -> Self

Set the initial step size.

source

pub fn rtol(self, rtol: f64) -> Self

Set the relative tolerance.

source

pub fn atol<V, T>(self, atol: V) -> Self
where V: IntoIterator<Item = T>, f64: From<T>,

Set the absolute tolerance.

source

pub fn p<V, T>(self, p: V) -> Self
where V: IntoIterator<Item = T>, f64: From<T>,

Set the parameters.

source

pub fn use_coloring(self, use_coloring: bool) -> Self

Set whether to use coloring when computing the Jacobian. This can speed up the computation of the Jacobian for large sparse systems. However, it relys on the sparsity of the Jacobian being constant, and for certain systems it may detect the wrong sparsity pattern.

source

pub fn build_ode_with_mass<M, F, G, H, I>( self, rhs: F, rhs_jac: G, mass: H, init: I ) -> Result<OdeSolverProblem<OdeSolverEquations<M, Closure<M, F, G>, ConstantClosure<M, I>, LinearClosure<M, H>>>>
where M: Matrix, F: Fn(&M::V, &M::V, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V), H: Fn(&M::V, &M::V, M::T, M::T, &mut M::V), I: Fn(&M::V, M::T) -> M::V,

Build an ODE problem with a mass matrix.

§Arguments
  • rhs: Function of type Fn(x: &V, p: &V, t: S, y: &mut V) that computes the right-hand side of the ODE.
  • rhs_jac: Function of type Fn(x: &V, p: &V, t: S, v: &V, y: &mut V) that computes the multiplication of the Jacobian of the right-hand side with the vector v.
  • mass: Function of type Fn(v: &V, p: &V, t: S, beta: S, y: &mut V) that computes a gemv multiplication of the mass matrix with the vector v (i.e. y = M * v + beta * y).
  • init: Function of type Fn(p: &V, t: S) -> V that computes the initial state.
§Generic Arguments
  • M: Type that implements the Matrix trait. Often this must be provided explicitly (i.e. type M = DMatrix<f64>; builder.build_ode::<M, _, _, _>).
§Example
use diffsol::OdeBuilder;
use nalgebra::DVector;
type M = nalgebra::DMatrix<f64>;

// dy/dt = y
// 0 = z - y
// y(0) = 0.1
// z(0) = 0.1
let problem = OdeBuilder::new()
  .build_ode_with_mass::<M, _, _, _, _>(
      |x, _p, _t, y| {
          y[0] = x[0];
          y[1] = x[1] - x[0];
      },
      |x, _p, _t, v, y|  {
          y[0] = v[0];
          y[1] = v[1] - v[0];
      },
      |v, _p, _t, beta, y| {
          y[0] = v[0] + beta * y[0];
          y[1] = beta * y[1];
      },
      |p, _t| DVector::from_element(2, 0.1),
);
source

pub fn build_ode_with_mass_and_sens<M, F, G, H, I, J, K, L>( self, rhs: F, rhs_jac: G, rhs_sens: J, mass: H, mass_sens: L, init: I, init_sens: K ) -> Result<OdeSolverProblem<OdeSolverEquations<M, ClosureWithSens<M, F, G, J>, ConstantClosureWithSens<M, I, K>, LinearClosureWithSens<M, H, L>>>>
where M: Matrix, F: Fn(&M::V, &M::V, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V), H: Fn(&M::V, &M::V, M::T, M::T, &mut M::V), I: Fn(&M::V, M::T) -> M::V, J: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V), K: Fn(&M::V, M::T, &M::V, &mut M::V), L: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V),

Build an ODE problem with a mass matrix and sensitivities.

§Arguments
  • rhs: Function of type Fn(x: &V, p: &V, t: S, y: &mut V) that computes the right-hand side of the ODE.
  • rhs_jac: Function of type Fn(x: &V, p: &V, t: S, v: &V, y: &mut V) that computes the multiplication of the Jacobian of the right-hand side with the vector v.
  • rhs_sens: Function of type Fn(x: &V, p: &V, t: S, v: &V, y: &mut V) that computes the multiplication of the partial derivative of the rhs wrt the parameters, with the vector v.
  • mass: Function of type Fn(v: &V, p: &V, t: S, beta: S, y: &mut V) that computes a gemv multiplication of the mass matrix with the vector v (i.e. y = M * v + beta * y).
  • mass_sens: Function of type Fn(v: &V, p: &V, t: S, y: &mut V) that computes the multiplication of the partial derivative of the mass matrix wrt the parameters, with the vector v.
  • init: Function of type Fn(p: &V, t: S) -> V that computes the initial state.
  • init_sens: Function of type Fn(p: &V, t: S, y: &mut V) that computes the multiplication of the partial derivative of the initial state wrt the parameters, with the vector v.
§Example
use diffsol::OdeBuilder;
use nalgebra::DVector;
type M = nalgebra::DMatrix<f64>;

// dy/dt = a y
// 0 = z - y
// y(0) = 0.1
// z(0) = 0.1
let problem = OdeBuilder::new()
  .build_ode_with_mass_and_sens::<M, _, _, _, _, _, _, _>(
      |x, p, _t, y| {
          y[0] = p[0] * x[0];
          y[1] = x[1] - x[0];
      },
      |x, p, _t, v, y|  {
          y[0] = p[0] * v[0];
          y[1] = v[1] - v[0];
      },
      |x, _p, _t, v, y| {
          y[0] = v[0] * x[0];
          y[1] = 0.0;
      },
      |x, _p, _t, beta, y| {
          y[0] = x[0] + beta * y[0];
          y[1] = beta * y[1];
      },
      |x, p, t, v, y| {
          y.fill(0.0);
      },
      |p, _t| DVector::from_element(2, 0.1),
      |p, t, v, y| {
          y.fill(0.0);
      }
);
source

pub fn build_ode<M, F, G, I>( self, rhs: F, rhs_jac: G, init: I ) -> Result<OdeSolverProblem<OdeSolverEquations<M, Closure<M, F, G>, ConstantClosure<M, I>>>>
where M: Matrix, F: Fn(&M::V, &M::V, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V), I: Fn(&M::V, M::T) -> M::V,

Build an ODE problem with a mass matrix that is the identity matrix.

§Arguments
  • rhs: Function of type Fn(x: &V, p: &V, t: S, y: &mut V) that computes the right-hand side of the ODE.
  • rhs_jac: Function of type Fn(x: &V, p: &V, t: S, v: &V, y: &mut V) that computes the multiplication of the Jacobian of the right-hand side with the vector v.
  • init: Function of type Fn(p: &V, t: S) -> V that computes the initial state.
§Generic Arguments
  • M: Type that implements the Matrix trait. Often this must be provided explicitly (i.e. type M = DMatrix<f64>; builder.build_ode::<M, _, _, _>).
§Example
use diffsol::OdeBuilder;
use nalgebra::DVector;
type M = nalgebra::DMatrix<f64>;


// dy/dt = y
// y(0) = 0.1
let problem = OdeBuilder::new()
   .build_ode::<M, _, _, _>(
       |x, _p, _t, y| y[0] = x[0],
       |x, _p, _t, v , y| y[0] = v[0],
       |p, _t| DVector::from_element(1, 0.1),
   );
source

pub fn build_ode_with_sens<M, F, G, I, J, K>( self, rhs: F, rhs_jac: G, rhs_sens: J, init: I, init_sens: K ) -> Result<OdeSolverProblem<OdeSolverEquations<M, ClosureWithSens<M, F, G, J>, ConstantClosureWithSens<M, I, K>>>>
where M: Matrix, F: Fn(&M::V, &M::V, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V), I: Fn(&M::V, M::T) -> M::V, J: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V), K: Fn(&M::V, M::T, &M::V, &mut M::V),

Build an ODE problem with a mass matrix that is the identity matrix and sensitivities.

§Arguments
  • rhs: Function of type Fn(x: &V, p: &V, t: S, y: &mut V) that computes the right-hand side of the ODE.
  • rhs_jac: Function of type Fn(x: &V, p: &V, t: S, v: &V, y: &mut V) that computes the multiplication of the Jacobian of the right-hand side with the vector v.
  • rhs_sens: Function of type Fn(x: &V, p: &V, t: S, v: &V, y: &mut V) that computes the multiplication of the partial derivative of the rhs wrt the parameters, with the vector v.
  • init: Function of type Fn(p: &V, t: S) -> V that computes the initial state.
  • init_sens: Function of type Fn(p: &V, t: S, y: &mut V) that computes the multiplication of the partial derivative of the initial state wrt the parameters, with the vector v.
§Example
use diffsol::OdeBuilder;
use nalgebra::DVector;
type M = nalgebra::DMatrix<f64>;


// dy/dt = a y
// y(0) = 0.1
let problem = OdeBuilder::new()
   .build_ode_with_sens::<M, _, _, _, _, _>(
       |x, p, _t, y| y[0] = p[0] * x[0],
       |x, p, _t, v, y| y[0] = p[0] * v[0],
       |x, p, _t, v, y| y[0] = v[0] * x[0],
       |p, _t| DVector::from_element(1, 0.1),
       |p, t, v, y| y.fill(0.0),
   );
source

pub fn build_ode_with_root<M, F, G, I, H>( self, rhs: F, rhs_jac: G, init: I, root: H, nroots: usize ) -> Result<OdeSolverProblem<OdeSolverEquations<M, Closure<M, F, G>, ConstantClosure<M, I>, UnitCallable<M>, ClosureNoJac<M, H>>>>
where M: Matrix, F: Fn(&M::V, &M::V, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V), H: Fn(&M::V, &M::V, M::T, &mut M::V), I: Fn(&M::V, M::T) -> M::V,

Build an ODE problem with an event.

§Arguments
  • rhs: Function of type Fn(x: &V, p: &V, t: S, y: &mut V) that computes the right-hand side of the ODE.
  • rhs_jac: Function of type Fn(x: &V, p: &V, t: S, v: &V, y: &mut V) that computes the multiplication of the Jacobian of the right-hand side with the vector v.
  • init: Function of type Fn(p: &V, t: S) -> V that computes the initial state.
  • root: Function of type Fn(x: &V, p: &V, t: S, y: &mut V) that computes the root function.
  • nroots: Number of roots (i.e. number of elements in the y arg in root), an event is triggered when any of the roots changes sign.
§Generic Arguments
  • M: Type that implements the Matrix trait. Often this must be provided explicitly (i.e. type M = DMatrix<f64>; builder.build_ode::<M, _, _, _, _>).
§Example
use diffsol::OdeBuilder;
use nalgebra::DVector;
type M = nalgebra::DMatrix<f64>;


// dy/dt = y
// y(0) = 0.1
// event at y = 0.5
let problem = OdeBuilder::new()
   .build_ode_with_root::<M, _, _, _, _>(
       |x, _p, _t, y| y[0] = x[0],
       |x, _p, _t, v , y| y[0] = v[0],
       |p, _t| DVector::from_element(1, 0.1),
       |x, _p, _t, y| y[0] = x[0] - 0.5,
       1,
   );
source

pub fn build_ode_dense<V, F, G, I>( self, rhs: F, rhs_jac: G, init: I ) -> Result<OdeSolverProblem<OdeSolverEquations<V::M, Closure<V::M, F, G>, ConstantClosure<V::M, I>>>>
where V: Vector + DefaultDenseMatrix, F: Fn(&V, &V, V::T, &mut V), G: Fn(&V, &V, V::T, &V, &mut V), I: Fn(&V, V::T) -> V,

Build an ODE problem using the default dense matrix (see Self::build_ode).

source

pub fn build_diffsl( self, context: &DiffSlContext ) -> Result<OdeSolverProblem<DiffSl<'_>>>

Build an ODE problem using the DiffSL language (requires the diffsl feature). The source code is provided as a string, please see the DiffSL documentation for more information.

Trait Implementations§

source§

impl Default for OdeBuilder

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.