Error handling
All the diffsol methods that can fail return a Result<_, DiffsolError>. DiffsolError implements std::error::Error, so you can use the ? operator in a function that returns a Result<_, Box<dyn std::error::Error>>.
DiffsolError has these variants:
| Variant | Cause |
|---|---|
OdeSolverError | The ODE solver failed, or you used it incorrectly. It holds an OdeSolverError, see below. |
NonLinearSolverError | A Newton solve failed, for example the initial condition solve (InitialConditionDidNotConverge). |
LaError | A linear algebra operation failed, for example an LU factorisation. |
DiffslParserError, DiffslCompilerError | The DiffSL code has an error. |
Other | Any other error. |
Errors during a solve
These OdeSolverError variants tell you that the solver could not continue. The Solver options page gives the options that control each limit.
TooManyTimesteps: the solver tookmax_number_timestepssteps (default 5000). Increase the limit. If the problem is stiff, use an implicit solver, which takes fewer steps.StepSizeTooSmall: the step size became smaller thanmin_timestep. The solution possibly has a singularity or a discontinuity. For a singularity, check the equations and the parameters. For a discontinuity, stop the solver at the discontinuity, then continue the solve (see Discrete Events).TooManyErrorTestFailures: one step was rejected more thanmax_error_test_failurestimes. Frequent causes are discontinuities, or functions that are not smooth, in the solution or in the right-hand side. Stop the solver at each discontinuity, then continue the solve.TooManyNonlinearSolverFailures: the Newton solve failed more thanmax_nonlinear_solver_failurestimes in one step. Check the Jacobian. An incorrect Jacobian is a frequent cause of Newton failures.
Each of these errors gives the time time at which the solver stopped.
Errors from incorrect use
Other OdeSolverError variants tell you that the solver was used incorrectly. Some examples are:
StopTimeBeforeCurrentTimeandStopTimeAtCurrentTime: the stop time must be after the current time.InvalidTEval: the times given tosolve_densemust increase, and they must not be before the current time.InterpolationTimeAfterCurrentTimeandInterpolationTimeOutsideCurrentStep: you can interpolate only inside the last step.MassMatrixNotSupported: the explicit Runge-Kutta solver does not support a mass matrix.FailedToGetMutableReference: a solver still borrows the problem. Drop the solver, or calltake_state(), before you change the problem.
Example: continue after too many steps
A solver keeps the state of its last step after a TooManyTimesteps error. In this example, the limit is too small. The code matches on the error, increases the limit with config_mut(), and continues the solve from the last step:
use crate::{problem_implicit, LS};
use diffsol::{error::OdeSolverError, DiffsolError, OdeSolverMethod};
pub fn solve_error_handling() {
let mut problem = problem_implicit();
problem.ode_options.max_number_timesteps = 10;
let mut solver = problem.bdf::<LS>().unwrap();
match solver.solve(10.0) {
Ok((_ys, ts, _stop_reason)) => println!("solved to t = {}", ts.last().unwrap()),
Err(DiffsolError::OdeSolverError(OdeSolverError::TooManyTimesteps { time, num_steps })) => {
println!("stopped after {num_steps} steps at t = {time}");
// the solver keeps the state of its last step: increase the limit and continue
solver.config_mut().maximum_number_timesteps = 5000;
let (_ys, ts, _stop_reason) = solver.solve(10.0).unwrap();
println!(
"continued from t = {} to t = {}",
ts.first().unwrap(),
ts.last().unwrap()
);
}
Err(e) => panic!("solve failed: {e}"),
}
}