Solver options
The default options are suitable for most problems. You can change them at two levels:
- The problem. The fields
ode_options(OdeSolverOptions) andic_options(InitialConditionSolverOptions) of theOdeSolverProblemapply 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_optionsinto its config:BdfConfig,SdirkConfigorExplicitRkConfig. Usesolver.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.
| Field | Default | Description |
|---|---|---|
max_number_timesteps | 5000 | The maximum number of steps. All the solve* calls with the same solver share this limit. |
min_timestep | machine epsilon | The minimum step size. |
max_error_test_failures | 40 | The maximum number of rejected attempts in one step. |
max_nonlinear_solver_failures | 10 | The maximum number of Newton failures in one step (implicit solvers only). |
max_nonlinear_solver_iterations | 10 | The maximum number of Newton iterations in one nonlinear solve (implicit solvers only). |
nonlinear_solver_tolerance | 0.2 | A scale factor for the Newton convergence test. A smaller value gives a tighter tolerance. |
derivative_solve_tolerance | 0.2 | A scale factor for the convergence test of the linear solves of the forward sensitivities and the discrete adjoint (BDF and SDIRK only). |
max_timestep_growth | 2 | The maximum increase of the step size in one step, as a ratio. |
min_timestep_shrink | 0.5 | The maximum decrease of the step size in one step, as a ratio. |
update_jacobian_after_steps | 20 | The maximum number of steps between two setups of the linear solver. |
update_rhs_jacobian_after_steps | 50 | The maximum number of steps between two calculations of the Jacobian of the right-hand side. |
fixed_step | false | Turn 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:
| Field | Default | Description |
|---|---|---|
use_linesearch | true | Use a backtracking line search. |
max_linesearch_iterations | 10 | The maximum number of line-search iterations. |
max_newton_iterations | 10 | The maximum number of Newton iterations. |
max_linear_solver_setups | 4 | The maximum number of setups of the linear solver. |
step_reduction_factor | 0.5 | The factor that decreases the step in the line search. |
armijo_constant | 1e-4 | The 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_iterationsandnumber_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).