2.4.8 Solver comparison

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

Deviation: 8.4e-05, 0.00018, 8.9e-05, 8.9e-05, 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.0017936620000043035, 'tracker': 4.8878999997725714e-05, 'compilation': 5.921704141999996}, 'solver_start': '2026-02-03 08:19:46.416967+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.001836', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'EulerSolver', 'pde_class': 'DiffusionPDE', 'backend': 'numba', 'dt_statistics': {'min': inf, 'max': -inf, 'mean': 0.0, 'std': nan, 'count': 0.0}, 'scheme': 'euler', 'dt': 0.001, 'dt_adaptive': False, 'steps': 1000, 'post_step_data': None, 'stochastic': False}}

Diagnostic information for explicit, adaptive Runge-Kutta solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.0004166899999944462, 'tracker': 4.827000000773296e-05, 'compilation': 7.034187852999999}, 'solver_start': '2026-02-03 08:19:53.461735+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.000457', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'RungeKuttaSolver', 'pde_class': 'DiffusionPDE', 'backend': 'numba', 'dt_statistics': {'min': 0.001, 'max': 0.16476085818834368, 'mean': 0.08333333333333334, 'std': 0.05105445007591206, 'count': 12.0}, 'dt': 0.001, 'dt_adaptive': True, 'steps': 12, 'stochastic': False, 'post_step_data': None}}

Diagnostic information for implicit solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.008225766000009571, 'tracker': 4.051999999887812e-05, 'compilation': 4.234018426999995}, 'solver_start': '2026-02-03 08:19:57.706698+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.008260', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'ImplicitSolver', 'pde_class': 'DiffusionPDE', 'backend': 'numba', 'dt_statistics': {'min': inf, 'max': -inf, 'mean': 0.0, 'std': nan, 'count': 0.0}, 'function_evaluations': 0, 'scheme': 'implicit-euler', 'stochastic': False, 'dt': 0.001, 'dt_adaptive': False, 'steps': 1000, 'post_step_data': None}}

Diagnostic information for Crank-Nicolson solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.012790199999997753, 'tracker': 4.4639000009283336e-05, 'compilation': 5.683239778000001}, 'solver_start': '2026-02-03 08:20:03.406491+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.012830', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'CrankNicolsonSolver', 'pde_class': 'DiffusionPDE', 'backend': 'numba', 'dt_statistics': {'min': inf, 'max': -inf, 'mean': 0.0, 'std': nan, 'count': 0.0}, 'function_evaluations': 0, 'stochastic': False, 'dt': 0.001, 'dt_adaptive': False, 'steps': 1000, 'post_step_data': None}}

Diagnostic information for Adam-Bashforth solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 3.743914658999998, 'tracker': 4.8180999996816354e-05, 'compilation': 1.8471216070000054}, 'solver_start': '2026-02-03 08:20:05.275794+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:03.747946', 't_final': 1.0}, 'package_version': 'unknown', 'solver': {'class': 'AdamsBashforthSolver', 'pde_class': 'DiffusionPDE', 'backend': 'numba', 'dt_statistics': {'min': inf, 'max': -inf, 'mean': 0.0, 'std': nan, 'count': 0.0}, 'dt': 0.001, 'dt_adaptive': False, 'steps': 1000, 'post_step_data': None, 'stochastic': False}}

Diagnostic information for scipy solver:
{'controller': {'mpi_run': False, 't_start': 0, 't_end': 1.0, 'profiler': {'solver': 0.6483843089999937, 'tracker': 3.818000000421762e-05, 'compilation': 0.0008016210000079127}, 'solver_start': '2026-02-03 08:20:09.027034+00:00', 'successful': True, 'stop_reason': 'Reached final time', 'solver_duration': '0:00:00.648555', 't_final': np.float64(1.0)}, 'package_version': 'unknown', 'solver': {'class': 'ScipySolver', 'pde_class': 'DiffusionPDE', 'backend': 'numba', 'dt': 0.001, 'steps': 55, 'stochastic': False}}

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 29.823 seconds)