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

Solver options

The default options are suitable for most problems. You can change them at two levels:

  • The problem. The fields ode_options (OdeSolverOptions) and ic_options (InitialConditionSolverOptions) of the OdeSolverProblem apply to all the solvers that you create from the problem. Set them before you create a solver.
  • The solver. When you create a solver, it copies its limits from ode_options into its config: BdfConfig, SdirkConfig or ExplicitRkConfig. Use solver.config_mut() to change the limits of one solver after you create it.

The tolerances are not in these options. See Tolerances.

use crate::{problem_implicit, LS};
use diffsol::OdeSolverMethod;

pub fn solver_options() {
    let mut problem = problem_implicit();

    // options for every solver that you create from this problem
    problem.ode_options.max_number_timesteps = 10_000;
    problem.ode_options.max_nonlinear_solver_iterations = 5;
    problem.ic_options.max_newton_iterations = 20;

    let mut solver = problem.bdf::<LS>().unwrap();

    // limits for this solver only, after you create it
    solver.config_mut().maximum_timestep_growth = 5.0;

    let (_ys, _ts, _stop_reason) = solver.solve(10.0).unwrap();

    let stats = solver.get_statistics();
    println!(
        "steps: {}, error test failures: {}, nonlinear iterations: {}, linear solver setups: {}",
        stats.number_of_steps,
        stats.number_of_error_test_failures,
        stats.number_of_nonlinear_solver_iterations,
        stats.number_of_linear_solver_setups,
    );
}

ODE solver options

These are the main fields of OdeSolverOptions. The API documentation describes all the fields, including the step-size controller.

FieldDefaultDescription
max_number_timesteps5000The maximum number of steps. All the solve* calls with the same solver share this limit.
min_timestepmachine epsilonThe minimum step size.
max_error_test_failures40The maximum number of rejected attempts in one step.
max_nonlinear_solver_failures10The maximum number of Newton failures in one step (implicit solvers only).
max_nonlinear_solver_iterations10The maximum number of Newton iterations in one nonlinear solve (implicit solvers only).
nonlinear_solver_tolerance0.2A scale factor for the Newton convergence test. A smaller value gives a tighter tolerance.
derivative_solve_tolerance0.2A scale factor for the convergence test of the linear solves of the forward sensitivities and the discrete adjoint (BDF and SDIRK only).
max_timestep_growth2The maximum increase of the step size in one step, as a ratio.
min_timestep_shrink0.5The maximum decrease of the step size in one step, as a ratio.
update_jacobian_after_steps20The maximum number of steps between two setups of the linear solver.
update_rhs_jacobian_after_steps50The maximum number of steps between two calculations of the Jacobian of the right-hand side.
fixed_stepfalseTurn off error control and take steps of a constant size. You can use this only with manual time-stepping, and you cannot set a stop time.

Initial condition options

If the problem has a singular mass matrix, the implicit solvers solve for the algebraic variables when you create the solver (see Initialisation). ic_options controls this Newton solve:

FieldDefaultDescription
use_linesearchtrueUse a backtracking line search.
max_linesearch_iterations10The maximum number of line-search iterations.
max_newton_iterations10The maximum number of Newton iterations.
max_linear_solver_setups4The maximum number of setups of the linear solver.
step_reduction_factor0.5The factor that decreases the step in the line search.
armijo_constant1e-4The Armijo constant of the line search.

If this solve does not converge, the solver constructor returns an InitialConditionDidNotConverge error.

Statistics

solver.get_statistics() returns an OdeSolverStatistics struct, which counts what the solver did. The example above prints some of its fields:

  • number_of_steps: the number of steps.
  • number_of_error_test_failures: the number of rejected attempts.
  • number_of_nonlinear_solver_iterations and number_of_nonlinear_solver_fails: the Newton iterations and failures.
  • number_of_linear_solver_setups: the number of Jacobian and LU setups. Other fields give the cause of each setup.

Use these values to find why a solve is slow, or to compare solvers. For example, if there are many error test failures, the problem is possibly stiff, and an implicit solver can be better than an explicit solver (see Stiff vs Non-Stiff).