Struct OdeBuilder

Source
pub struct OdeBuilder<M: Matrix = NalgebraMat<f64>, Rhs = UnitCallable<M>, Init = UnitCallable<M>, Mass = UnitCallable<M>, Root = UnitCallable<M>, Out = UnitCallable<M>> { /* 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<M, Rhs, Init, Mass, Root, Out> OdeBuilder<M, Rhs, Init, Mass, Root, Out>
where M: Matrix,

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

§Example

use diffsol::{OdeBuilder, NalgebraLU, Bdf, OdeSolverState, OdeSolverMethod, NalgebraMat};
type M = NalgebraMat<f64>;
type LS = NalgebraLU<f64>;

let problem = OdeBuilder::<M>::new()
  .rtol(1e-6)
  .p([0.1])
  .rhs_implicit(
    // 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];
    },
  )
  .init(
    // y(0) = 1
   |p, t, y| y[0] = 1.0,
   1,
  )
  .build()
  .unwrap();

let mut solver = problem.bdf::<LS>().unwrap();
let t = 0.4;
while solver.state().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 rhs<F>( self, rhs: F, ) -> OdeBuilder<M, ClosureNoJac<M, F>, Init, Mass, Root, Out>
where F: Fn(&M::V, &M::V, M::T, &mut M::V),

Set the right-hand side of the ODE.

§Arguments
  • rhs: Function of type Fn(x: &V, p: &V, t: S, y: &mut V) that computes the right-hand side of the ODE.
Source

pub fn rhs_implicit<F, G>( self, rhs: F, rhs_jac: G, ) -> OdeBuilder<M, Closure<M, F, G>, Init, Mass, Root, Out>
where F: Fn(&M::V, &M::V, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V),

Set the right-hand side of the ODE.

§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.
Source

pub fn rhs_sens_implicit<F, G, H>( self, rhs: F, rhs_jac: G, rhs_sens: H, ) -> OdeBuilder<M, ClosureWithSens<M, F, G, H>, Init, Mass, Root, Out>
where 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::V, &mut M::V),

Set the right-hand side of the ODE for forward sensitivity analysis.

§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.
Source

pub fn rhs_adjoint_implicit<F, G, H, I>( self, rhs: F, rhs_jac: G, rhs_adjoint: H, rhs_sens_adjoint: I, ) -> OdeBuilder<M, ClosureWithAdjoint<M, F, G, H, I>, Init, Mass, Root, Out>
where 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::V, &mut M::V), I: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V),

Source

pub fn init<F>( self, init: F, nstates: usize, ) -> OdeBuilder<M, Rhs, ConstantClosure<M, F>, Mass, Root, Out>
where F: Fn(&M::V, M::T, &mut M::V),

Set the initial condition of the ODE.

§Arguments
  • init: Function of type Fn(p: &V, t: S) -> V that computes the initial state.
Source

pub fn init_sens<F, G>( self, init: F, init_sens: G, nstates: usize, ) -> OdeBuilder<M, Rhs, ConstantClosureWithSens<M, F, G>, Mass, Root, Out>
where F: Fn(&M::V, M::T, &mut M::V), G: Fn(&M::V, M::T, &M::V, &mut M::V),

Set the initial condition of the ODE for forward sensitivity analysis.

§Arguments
  • 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.
Source

pub fn init_adjoint<F, G>( self, init: F, init_sens_adjoint: G, nstates: usize, ) -> OdeBuilder<M, Rhs, ConstantClosureWithAdjoint<M, F, G>, Mass, Root, Out>
where F: Fn(&M::V, M::T, &mut M::V), G: Fn(&M::V, M::T, &M::V, &mut M::V),

Set the initial condition of the ODE for adjoint sensitivity analysis.

§Arguments
  • init: Function of type Fn(p: &V, t: S) -> V that computes the initial state.
  • init_sens_adjoint: Function of type Fn(p: &V, t: S, y: &V, y_adj: &mut V) that computes the multiplication of the partial derivative of the initial state wrt the parameters, with the vector v.
Source

pub fn mass<F>( self, mass: F, ) -> OdeBuilder<M, Rhs, Init, LinearClosure<M, F>, Root, Out>
where F: Fn(&M::V, &M::V, M::T, M::T, &mut M::V),

Set the mass matrix of the ODE.

§Arguments
  • 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).
Source

pub fn mass_adjoint<F, G>( self, mass: F, mass_adjoint: G, ) -> OdeBuilder<M, Rhs, Init, LinearClosureWithAdjoint<M, F, G>, Root, Out>
where F: Fn(&M::V, &M::V, M::T, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, M::T, &mut M::V),

Set the mass matrix of the ODE for adjoint sensitivity analysis.

§Arguments
  • 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_adjoint: Function of type Fn(v: &V, p: &V, t: S, beta: S, y: &mut V) that computes a gemv multiplication of the transpose of the mass matrix with the vector v (i.e. y = M^T * v + beta * y).
Source

pub fn root<F>( self, root: F, nroots: usize, ) -> OdeBuilder<M, Rhs, Init, Mass, ClosureNoJac<M, F>, Out>
where F: Fn(&M::V, &M::V, M::T, &mut M::V),

Set a root equation for the ODE.

§Arguments
  • 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.
Source

pub fn out_implicit<F, G>( self, out: F, out_jac: G, nout: usize, ) -> OdeBuilder<M, Rhs, Init, Mass, Root, Closure<M, F, G>>
where F: Fn(&M::V, &M::V, M::T, &mut M::V), G: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V),

Source

pub fn out_adjoint_implicit<F, G, H, I>( self, out: F, out_jac: G, out_adjoint: H, out_sens_adjoint: I, nout: usize, ) -> OdeBuilder<M, Rhs, Init, Mass, Root, ClosureWithAdjoint<M, F, G, H, I>>
where 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::V, &mut M::V), I: Fn(&M::V, &M::V, M::T, &M::V, &mut M::V),

Source

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

Set the initial time.

Source

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

Source

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

Source

pub fn turn_off_sensitivities_error_control(self) -> Self

Source

pub fn turn_off_output_error_control(self) -> Self

Source

pub fn turn_off_param_error_control(self) -> Self

Source

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

Source

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

Source

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

Source

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

Source

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

Set whether to integrate the output. If true, the output will be integrated using the same method as the ODE.

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>, M::T: From<T>,

Set the absolute tolerance.

Source

pub fn p<V, T>(self, p: V) -> Self
where V: IntoIterator<Item = T>, M::T: 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 is always true if matrix type is sparse, but can be set to true for dense matrices as well. 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( self, ) -> Result<OdeSolverProblem<OdeSolverEquations<M, Rhs, Init, Mass, Root, Out>>, DiffsolError>
where M: Matrix, Rhs: BuilderOp<V = M::V, T = M::T, M = M, C = M::C>, Init: BuilderOp<V = M::V, T = M::T, M = M, C = M::C>, Mass: BuilderOp<V = M::V, T = M::T, M = M, C = M::C>, Root: BuilderOp<V = M::V, T = M::T, M = M, C = M::C>, Out: BuilderOp<V = M::V, T = M::T, M = M, C = M::C>, for<'a> ParameterisedOp<'a, Rhs>: NonLinearOp<M = M, V = M::V, T = M::T, C = M::C>, for<'a> ParameterisedOp<'a, Init>: ConstantOp<M = M, V = M::V, T = M::T, C = M::C>, for<'a> ParameterisedOp<'a, Mass>: LinearOp<M = M, V = M::V, T = M::T, C = M::C>, for<'a> ParameterisedOp<'a, Root>: NonLinearOp<M = M, V = M::V, T = M::T, C = M::C>, for<'a> ParameterisedOp<'a, Out>: NonLinearOp<M = M, V = M::V, T = M::T, C = M::C>,

Source

pub fn build_from_diffsl<CG: CodegenModuleJit + CodegenModuleCompile>( self, code: &str, ) -> Result<OdeSolverProblem<DiffSl<M, CG>>, DiffsolError>
where M: Matrix<V: VectorHost, T = f64>,

Source

pub fn build_from_eqn<Eqn>( self, eqn: Eqn, ) -> Result<OdeSolverProblem<Eqn>, DiffsolError>
where Eqn: OdeEquations<M = M, V = M::V, T = M::T>,

Build an ODE problem from a set of equations

Trait Implementations§

Source§

impl Default for OdeBuilder

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<M, Rhs, Init, Mass, Root, Out> Freeze for OdeBuilder<M, Rhs, Init, Mass, Root, Out>
where <M as MatrixCommon>::T: Freeze, <M as MatrixCommon>::C: Freeze, Rhs: Freeze, Init: Freeze, Mass: Freeze, Root: Freeze, Out: Freeze,

§

impl<M, Rhs, Init, Mass, Root, Out> RefUnwindSafe for OdeBuilder<M, Rhs, Init, Mass, Root, Out>

§

impl<M, Rhs, Init, Mass, Root, Out> Send for OdeBuilder<M, Rhs, Init, Mass, Root, Out>
where <M as MatrixCommon>::C: Send, Rhs: Send, Init: Send, Mass: Send, Root: Send, Out: Send,

§

impl<M, Rhs, Init, Mass, Root, Out> Sync for OdeBuilder<M, Rhs, Init, Mass, Root, Out>
where <M as MatrixCommon>::C: Sync, Rhs: Sync, Init: Sync, Mass: Sync, Root: Sync, Out: Sync,

§

impl<M, Rhs, Init, Mass, Root, Out> Unpin for OdeBuilder<M, Rhs, Init, Mass, Root, Out>
where <M as MatrixCommon>::T: Unpin, <M as MatrixCommon>::C: Unpin, Rhs: Unpin, Init: Unpin, Mass: Unpin, Root: Unpin, Out: Unpin,

§

impl<M, Rhs, Init, Mass, Root, Out> UnwindSafe for OdeBuilder<M, Rhs, Init, Mass, Root, Out>
where <M as MatrixCommon>::T: UnwindSafe, <M as MatrixCommon>::C: UnwindSafe, Rhs: UnwindSafe, Init: UnwindSafe, Mass: UnwindSafe, Root: UnwindSafe, Out: UnwindSafe,

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> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

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

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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.
Source§

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

Source§

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>,

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V