@@ -436,9 +436,37 @@ where we have a more elegant solution in terms of a class: the `error`
436436variable is not a class attribute and there is no need for a global
437437error (which is always considered an advantage).
438438
439- ```{.python include="../../../src/wave/wave1D/wave1D_dn_vc.py" start-after="def convergence_rates" end-before="def test_convrate_sincos"}
439+ ```python
440+ def convergence_rates(
441+ u_exact, # Function for exact solution
442+ I, V, f, c, L, # Problem parameters
443+ dt_values, # List of dt values to test
444+ solver_function, # Solver to test
445+ ):
446+ """
447+ Compute convergence rates for a wave equation solver.
448+ Returns list of observed convergence rates.
449+ """
450+ E_values = []
451+ for dt in dt_values:
452+ # Run solver and compute error
453+ u, x, t = solver_function(I, V, f, c, L, dt, ...)
454+ u_e = u_exact(x, t[-1])
455+ E = np.sqrt(dt * np.sum((u_e - u) ** 2))
456+ E_values.append(E)
457+
458+ # Compute convergence rates
459+ r = [np.log(E_values[i] / E_values[i-1]) /
460+ np.log(dt_values[i] / dt_values[i-1])
461+ for i in range(1, len(dt_values))]
462+ return r
440463```
441464
465+ ::: {.callout-note}
466+ For a complete, tested implementation of convergence rate testing with Devito,
467+ see `src/wave/wave1D_devito.py` and `tests/test_wave_devito.py`.
468+ :::
469+
442470The returned sequence `r` should converge to 2 since the error
443471analysis in @sec-wave-pde1-analysis predicts various error measures to behave
444472like $\Oof{\Delta t^2} + \Oof{\Delta x^2}$. We can easily run
0 commit comments