2.4.9 Solver comparison

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

Deviation: 8.7e-05, 0.00019, 9.3e-05, 9.3e-05, 9.5e-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.0017076809999991838, 'tracker': 5.7760000004236645e-05, 'compilation': 6.06819981000001}, 'solver_start': '2026-06-05 16:50:06.432946+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.001758', '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.0005140209999865419, 'tracker': 5.434900000977905e-05, 'compilation': 7.200091993000001}, 'solver_start': '2026-06-05 16:50:13.640641+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.000561', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'RungeKuttaSolver', 'pde_class': 'DiffusionPDE', 'dt': 0.21029057783580457, 'steps': 12, 'dt_adaptive': True, 'stochastic': False, 'backend': {'name': 'numba', 'implementation': 'numba'}, 'dt_statistics': {'min': 0.001, 'max': 0.17543377063788704, 'mean': 0.08333333333333334, 'std': 0.05311505097948038, '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.008133946000000947, 'tracker': 4.7010000002956076e-05, 'compilation': 4.174442333000002}, 'solver_start': '2026-06-05 16:50:17.824619+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.008173', '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.012587799999991489, 'tracker': 4.813900001465754e-05, 'compilation': 6.182391334999991}, 'solver_start': '2026-06-05 16:50:24.022869+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.012630', '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.782680257999999, 'tracker': 5.536000000461172e-05, 'compilation': 1.8624716749999948}, 'solver_start': '2026-06-05 16:50:25.906271+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:03.783324', '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.6616843700000032, 'tracker': 4.180099999473441e-05, 'compilation': 0.001385550999998486}, 'solver_start': '2026-06-05 16:50:29.693538+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.661845', '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 30.668 seconds)