2.4.9 Solver comparison

This example shows how to set up solvers explicitly and how to extract diagnostic information.

Deviation: 9.2e-05, 0.00019, 9.7e-05, 9.7e-05, 9.9e-05, explicit Euler solver, explicit, adaptive Runge-Kutta solver, implicit solver, Crank-Nicolson solver, Adam-Bashforth solver, scipy solver
Diagnostic information for explicit Euler solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.0015768639999933498, 'tracker': 4.2037000000050284e-05, 'compilation': 4.799435843000005}, 'solver_start': '2026-04-24 09:58:50.266596+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.001612', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'EulerSolver', 'pde_class': 'DiffusionPDE', 'dt': 0.001, 'steps': 1000, 'dt_adaptive': False, 'stochastic': False, 'backend': {'name': 'numba', 'implementation': 'numba'}, 'post_step_data': None}}

Diagnostic information for explicit, adaptive Runge-Kutta solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.00045216899999900306, 'tracker': 4.75450000010369e-05, 'compilation': 5.735869158}, 'solver_start': '2026-04-24 09:58:56.010244+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.000493', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'RungeKuttaSolver', 'pde_class': 'DiffusionPDE', 'dt': 0.1966886275836582, 'steps': 12, 'dt_adaptive': True, 'stochastic': False, 'backend': {'name': 'numba', 'implementation': 'numba'}, 'dt_statistics': {'min': 0.001, 'max': 0.1656525105870269, 'mean': 0.08333333333333334, 'std': 0.051311265504958084, 'count': 12.0}, 'post_step_data': None}}

Diagnostic information for implicit solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.007830759000000853, 'tracker': 4.137499999501415e-05, 'compilation': 3.222981992000001}, 'solver_start': '2026-04-24 09:58:59.241994+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.007865', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'ImplicitSolver', 'pde_class': 'DiffusionPDE', 'dt': 0.001, 'steps': 1000, 'dt_adaptive': False, 'stochastic': False, 'backend': {'name': 'numba', 'implementation': 'numba'}, 'function_evaluations': 0, 'post_step_data': None}}

Diagnostic information for Crank-Nicolson solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.011184378999999467, 'tracker': 4.139800000757532e-05, 'compilation': 4.5695728110000005}, 'solver_start': '2026-04-24 09:59:03.826600+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.011219', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'CrankNicolsonSolver', 'pde_class': 'DiffusionPDE', 'dt': 0.001, 'steps': 1000, 'dt_adaptive': False, 'stochastic': False, 'backend': {'name': 'numba', 'implementation': 'numba'}, 'function_evaluations': 0, 'post_step_data': None}}

Diagnostic information for Adam-Bashforth solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 3.012746362999991, 'tracker': 4.9712000020463165e-05, 'compilation': 1.4868771559999914}, 'solver_start': '2026-04-24 09:59:05.333868+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:03.014354', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'AdamsBashforthSolver', 'pde_class': 'DiffusionPDE', 'dt': 0.001, 'steps': 1000, 'dt_adaptive': False, 'stochastic': False, 'backend': {'name': 'numba', 'implementation': 'numba'}, 'post_step_data': None}}

Diagnostic information for scipy solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.5371237780000087, 'tracker': 3.965899999514022e-05, 'compilation': 0.0010584009999945465}, 'solver_start': '2026-04-24 09:59:08.352100+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.537199', 't_final': np.float64(1.0)}, 'package_version': 'unknown', 'solver': {'class': 'ScipySolver', 'pde_class': 'DiffusionPDE', 'dt': 0.001, 'steps': 61, 'stochastic': False, 'backend': {'name': 'numba', 'implementation': 'numba'}}}

import pde

# initialize the grid, an initial condition, and the PDE
grid = pde.UnitGrid([32, 32])
field = pde.ScalarField.random_uniform(grid, -1, 1)
eq = pde.DiffusionPDE()


def run_solver(solver, label):
    """Helper function testing the solver."""
    controller = pde.Controller(solver, t_range=1, tracker=None)
    sol = controller.run(field, dt=1e-3)
    sol.label = label + " solver"
    print(f"Diagnostic information for {sol.label}:")
    print(controller.diagnostics)
    print()
    return sol


# try different solvers
solutions = [
    run_solver(pde.EulerSolver(eq), "explicit Euler"),
    run_solver(
        pde.RungeKuttaSolver(eq, adaptive=True), "explicit, adaptive Runge-Kutta"
    ),
    run_solver(pde.ImplicitSolver(eq), "implicit"),
    run_solver(pde.CrankNicolsonSolver(eq), "Crank-Nicolson"),
    run_solver(pde.AdamsBashforthSolver(eq), "Adam-Bashforth"),
    run_solver(pde.ScipySolver(eq), "scipy"),
]

# plot both fields and give the deviation as the title
deviations = [(solutions[0] - sol).fluctuations for sol in solutions]
title = "Deviation: " + ", ".join(f"{deviation:.2g}" for deviation in deviations[1:])
pde.FieldCollection(solutions).plot(title=title, arrangement=(2, 3))

Total running time of the script: (0 minutes 23.855 seconds)