Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solving the problem

Each solver implements the OdeSolverMethod trait, which provides a number of high-level methods to solve the problem.

  • solve - solve the problem from an initial state up to a specified time, returning the solution at all the internal timesteps used by the solver.
  • solve_dense - solve the problem from an initial state, returning the solution at a Vec of times provided by the user.

If the equations define both a root function and a reset operator, these two methods apply the reset automatically and continue the forward solve.

The following example shows how to solve a simple ODE problem up to \(t=10\) using the solve method on the OdeSolverMethod trait. The solve method returns a tuple (ys, ts, stop_reason): the solution at all the internal timesteps used by the solver (_ys), the timesteps used by the solver (_ts), and the reason that the solver stopped (_stop_reason). The solution is returned as a matrix whose columns are the solution at each timestep.

use crate::{T, V};
use diffsol::{OdeEquations, OdeSolverMethod};

pub fn solve<'a, Solver, Eqn>(solver: &mut Solver)
where
    Solver: OdeSolverMethod<'a, Eqn>,
    Eqn: OdeEquations<T = T, V = V> + 'a,
{
    let (_ys, _ts, _stop_reason) = solver.solve(10.0).unwrap();
}

solve_dense will solve a problem from an initial state. It returns a tuple (ys, stop_reason), where ys is a matrix whose columns are the solution at each timestep in times. If a root is found and the equations do not define a reset operator, the solver stops at the root. In this case, the last column of ys is the solution at the root time, and the columns for the remaining times in times are removed. See Stopping for more information.

use crate::{T, V};
use diffsol::{OdeEquations, OdeSolverMethod};

pub fn solve_dense<'a, Solver, Eqn>(solver: &mut Solver)
where
    Solver: OdeSolverMethod<'a, Eqn>,
    Eqn: OdeEquations<T = T, V = V> + 'a,
{
    let times = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
    let _soln = solver.solve_dense(&times).unwrap();
}