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
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);
sourcepub fn new() -> Self
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
pub fn sensitivities(self, sensitivities: bool) -> Self
pub fn sensitivities_error_control( self, sensitivities_error_control: bool ) -> Self
sourcepub fn use_coloring(self, use_coloring: bool) -> Self
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.
sourcepub 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>>>>
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>>>>
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 theMatrix
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),
);
sourcepub 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>>>>
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>>>>
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);
}
);
sourcepub 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>>>>
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>>>>
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 theMatrix
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),
);
sourcepub 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>>>>
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>>>>
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),
);
sourcepub 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>>>>
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>>>>
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 they
arg inroot
), an event is triggered when any of the roots changes sign.
§Generic Arguments
M
: Type that implements theMatrix
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,
);
sourcepub 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>>>>
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>>>>
Build an ODE problem using the default dense matrix (see Self::build_ode).
sourcepub fn build_diffsl(
self,
context: &DiffSlContext
) -> Result<OdeSolverProblem<DiffSl<'_>>>
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§
Auto Trait Implementations§
impl Freeze for OdeBuilder
impl RefUnwindSafe for OdeBuilder
impl Send for OdeBuilder
impl Sync for OdeBuilder
impl Unpin for OdeBuilder
impl UnwindSafe for OdeBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moresource§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.