Coverage for pySDC/projects/parallelSDC_reloaded/scripts/fig01_conv.py: 100%
41 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Created on Tue Jan 9 16:00:41 2024
6Convergence plots (on Dahlquist) for the article
7"""
8import os
9import numpy as np
11from pySDC.projects.parallelSDC_reloaded.utils import getParamsSDC, solutionSDC, plt
12from pySDC.helpers.testing import DataChecker
14data = DataChecker(__file__)
16PATH = '/' + os.path.join(*__file__.split('/')[:-1])
17SCRIPT = __file__.split('/')[-1].split('.')[0]
19# Script parameters
20lam = 1j
21tEnd = 2 * np.pi
22nStepsList = np.array([2, 5, 10, 20, 50, 100, 200, 500, 1000])
23dtVals = tEnd / nStepsList
25# Collocation parameters
26nodeType = "LEGENDRE"
29def getError(uNum, uRef):
30 if uNum is None: # pragma: no cover
31 return np.inf
32 return np.linalg.norm(uRef - uNum[:, 0], np.inf)
35# Configuration
36# (nNodes, quadType, sweepType)
37config = [
38 (4, "RADAU-RIGHT", "MIN-SR-NS"),
39 (5, "LOBATTO", "MIN-SR-NS"),
40 (4, "RADAU-RIGHT", "MIN-SR-S"),
41 (5, "LOBATTO", "MIN-SR-S"),
42 (4, "RADAU-RIGHT", "MIN-SR-FLEX"),
43 (5, "LOBATTO", "MIN-SR-FLEX"),
44]
46# -----------------------------------------------------------------------------
47# Script execution
48# -----------------------------------------------------------------------------
49for nNodes, quadType, sweepType in config:
50 # Schemes parameters
51 schemes = [
52 # ("RK4", None), ("ESDIRK53", None),
53 *[(sweepType, i) for i in range(1, nNodes + 1)]
54 ]
56 # Plot styles
57 styles = [
58 # dict(ls="--", c="gray"), dict(ls="-.", c="gray"),
59 dict(ls="-", marker='o'),
60 dict(ls="-", marker='>'),
61 dict(ls="-", marker='s'),
62 dict(ls="-", marker='^'),
63 dict(ls="-", marker='*'),
64 ]
66 # Figure generation
67 figName = f"{sweepType}_{quadType}"
68 plt.figure(f"{sweepType}_{quadType}")
69 for (qDelta, nSweeps), style in zip(schemes, styles):
70 params = getParamsSDC(quadType, nNodes, qDelta, nSweeps, nodeType)
71 label = f"$K={nSweeps}$"
72 errors = []
74 for nSteps in nStepsList:
75 uNum, counters, parallel = solutionSDC(tEnd, nSteps, params, 'DAHLQUIST', lambdas=np.array([lam]))
77 tVals = np.linspace(0, tEnd, nSteps + 1)
78 uExact = np.exp(lam * tVals)
80 err = getError(uNum, uExact)
81 errors.append(err)
83 plt.loglog(dtVals, errors, **style, label=label)
85 data.storeAndCheck(f"{SCRIPT}_{figName}_{label}", errors)
87 if nSweeps is not None:
88 plt.loglog(dtVals, (0.1 * dtVals) ** nSweeps, '--', c='gray', lw=1.5)
90 plt.legend()
91 plt.xlabel(r"$\Delta{t}$")
92 plt.ylabel(r"$L_\infty$ error")
93 plt.grid(True)
94 plt.tight_layout()
95 plt.savefig(f"{PATH}/{SCRIPT}_{figName}.pdf")
97data.writeToJSON()