pub struct ODEProblem<T, V, D, F>{
pub ode: F,
pub t0: T,
pub tf: T,
pub y0: V,
/* private fields */
}Expand description
Initial Value Problem for Ordinary Differential Equations (ODEs)
The Initial Value Problem takes the form: y’ = f(t, y), a <= t <= b, y(a) = alpha
§Overview
The ODEProblem struct provides a simple interface for solving differential equations:
§Example
use differential_equations::prelude::*;
struct LinearEquation {
pub a: f32,
pub b: f32,
}
impl ODE<f32, f32> for LinearEquation {
fn diff(&self, _t: f32, y: &f32, dydt: &mut f32) {
*dydt = self.a + self.b * y;
}
}
// Create the ode and initial conditions
let ode = LinearEquation { a: 1.0, b: 2.0 };
let t0 = 0.0;
let tf = 1.0;
let y0 = 1.0;
let mut solver = ExplicitRungeKutta::dop853().rtol(1e-8).atol(1e-6);
// Basic usage:
let problem = ODEProblem::new(ode, t0, tf, y0);
let solution = problem.solve(&mut solver).unwrap();
// Advanced output control:
let solution = problem.even(0.1).solve(&mut solver).unwrap();§Fields
ode- ODE implementing the differential equationt0- Initial timetf- Final timey0- Initial state vector
§Basic Usage
new(ode, t0, tf, y0)- Create a new ODEProblemsolve(&mut solver)- Solve using default output (solver step points)
§Output Control Methods
These methods configure how solution points are generated and returned:
even(dt)- Generate evenly spaced output points with intervaldtdense(n)- Includeninterpolated points between each solver stept_eval(points)- Evaluate solution at specific time pointssolout(custom_solout)- Use a custom output handler
Each returns a solver configuration that can be executed with .solve(&mut solver).
§Example 2
use differential_equations::prelude::*;
use nalgebra::{SVector, vector};
struct HarmonicOscillator { k: f64 }
impl ODE<f64, SVector<f64, 2>> for HarmonicOscillator {
fn diff(&self, _t: f64, y: &SVector<f64, 2>, dydt: &mut SVector<f64, 2>) {
dydt[0] = y[1];
dydt[1] = -self.k * y[0];
}
}
let ode = HarmonicOscillator { k: 1.0 };
let mut method = ExplicitRungeKutta::dop853().rtol(1e-12).atol(1e-12);
// Basic usage with default output points
let problem = ODEProblem::new(ode, 0.0, 10.0, vector![1.0, 0.0]);
let results = problem.solve(&mut method).unwrap();
// Advanced: evenly spaced output with 0.1 time intervals
let results = problem.dense(4).solve(&mut method).unwrap();Fields§
§ode: F§t0: T§tf: T§y0: VImplementations§
Source§impl<T, V, D, F> ODEProblem<T, V, D, F>
impl<T, V, D, F> ODEProblem<T, V, D, F>
Sourcepub fn solve<S>(&self, solver: &mut S) -> Result<Solution<T, V, D>, Error<T, V>>where
S: OrdinaryNumericalMethod<T, V, D> + Interpolation<T, V>,
pub fn solve<S>(&self, solver: &mut S) -> Result<Solution<T, V, D>, Error<T, V>>where
S: OrdinaryNumericalMethod<T, V, D> + Interpolation<T, V>,
Solve the ODEProblem using a default solout, e.g. outputting solutions at calculated steps.
§Returns
Result<Solution<T, V, D>, Status<T, V, D>>-Ok(Solution)if successful or interrupted by events,Err(Status)if an errors or issues such as stiffness are encountered.
Sourcepub fn solout<'a, O: Solout<T, V, D>>(
&'a self,
solout: &'a mut O,
) -> ODEProblemMutRefSoloutPair<'a, T, V, D, F, O>
pub fn solout<'a, O: Solout<T, V, D>>( &'a self, solout: &'a mut O, ) -> ODEProblemMutRefSoloutPair<'a, T, V, D, F, O>
Returns an ODEProblem OrdinaryNumericalMethod with the provided solout function for outputting points.
§Returns
- ODEProblem OrdinaryNumericalMethod with the provided solout function ready for .solve() method.
Sourcepub fn even(&self, dt: T) -> ODEProblemSoloutPair<'_, T, V, D, F, EvenSolout<T>>
pub fn even(&self, dt: T) -> ODEProblemSoloutPair<'_, T, V, D, F, EvenSolout<T>>
Uses the an Even Solout implementation to output evenly spaced points between the initial and final time. Note that this does not include the solution of the calculated steps.
§Arguments
dt- Interval between each output point.
§Returns
- ODEProblem OrdinaryNumericalMethod with Even Solout function ready for .solve() method.
Sourcepub fn dense(
&self,
n: usize,
) -> ODEProblemSoloutPair<'_, T, V, D, F, DenseSolout>
pub fn dense( &self, n: usize, ) -> ODEProblemSoloutPair<'_, T, V, D, F, DenseSolout>
Uses the Dense Output method to output n number of interpolation points between each step. Note this includes the solution of the calculated steps.
§Arguments
n- Number of interpolation points between each step.
§Returns
- ODEProblem OrdinaryNumericalMethod with Dense Output function ready for .solve() method.
Sourcepub fn t_eval(
&self,
points: Vec<T>,
) -> ODEProblemSoloutPair<'_, T, V, D, F, TEvalSolout<T>>
pub fn t_eval( &self, points: Vec<T>, ) -> ODEProblemSoloutPair<'_, T, V, D, F, TEvalSolout<T>>
Sourcepub fn crossing(
&self,
component_idx: usize,
threshhold: T,
direction: CrossingDirection,
) -> ODEProblemSoloutPair<'_, T, V, D, F, CrossingSolout<T>>
pub fn crossing( &self, component_idx: usize, threshhold: T, direction: CrossingDirection, ) -> ODEProblemSoloutPair<'_, T, V, D, F, CrossingSolout<T>>
Uses the CrossingSolout method to output points when a specific component crosses a threshold. Note this does not include the solution of the calculated steps.
§Arguments
component_idx- Index of the component to monitor for crossing.threshhold- Value to cross.direction- Direction of crossing (positive or negative).
§Returns
- ODEProblem OrdinaryNumericalMethod with CrossingSolout function ready for .solve() method.
Sourcepub fn hyperplane_crossing<V1>(
&self,
point: V1,
normal: V1,
extractor: fn(&V) -> V1,
direction: CrossingDirection,
) -> ODEProblemSoloutPair<'_, T, V, D, F, HyperplaneCrossingSolout<T, V1, V>>where
V1: State<T>,
pub fn hyperplane_crossing<V1>(
&self,
point: V1,
normal: V1,
extractor: fn(&V) -> V1,
direction: CrossingDirection,
) -> ODEProblemSoloutPair<'_, T, V, D, F, HyperplaneCrossingSolout<T, V1, V>>where
V1: State<T>,
Uses the HyperplaneCrossingSolout method to output points when a specific hyperplane is crossed. Note this does not include the solution of the calculated steps.
§Arguments
point- Point on the hyperplane.normal- Normal vector of the hyperplane.extractor- Function to extract the component from the state vector.direction- Direction of crossing (positive or negative).
§Returns
- ODEProblem OrdinaryNumericalMethod with HyperplaneCrossingSolout function ready for .solve() method.
Trait Implementations§
Source§impl<T, V, D, F> Clone for ODEProblem<T, V, D, F>
impl<T, V, D, F> Clone for ODEProblem<T, V, D, F>
Source§fn clone(&self) -> ODEProblem<T, V, D, F>
fn clone(&self) -> ODEProblem<T, V, D, F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<T, V, D, F> Freeze for ODEProblem<T, V, D, F>
impl<T, V, D, F> RefUnwindSafe for ODEProblem<T, V, D, F>
impl<T, V, D, F> Send for ODEProblem<T, V, D, F>
impl<T, V, D, F> Sync for ODEProblem<T, V, D, F>
impl<T, V, D, F> Unpin for ODEProblem<T, V, D, F>
impl<T, V, D, F> UnwindSafe for ODEProblem<T, V, D, F>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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.