Coverage for pySDC / projects / parallelSDC_reloaded / scripts / fig01_conv.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-27 07:06 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3""" 

4Created on Tue Jan 9 16:00:41 2024 

5 

6Convergence plots (on Dahlquist) for the article 

7""" 

8 

9import os 

10import numpy as np 

11 

12from pySDC.projects.parallelSDC_reloaded.utils import getParamsSDC, solutionSDC, plt 

13from pySDC.helpers.testing import DataChecker 

14 

15data = DataChecker(__file__) 

16 

17PATH = '/' + os.path.join(*__file__.split('/')[:-1]) 

18SCRIPT = __file__.split('/')[-1].split('.')[0] 

19 

20# Script parameters 

21lam = 1j 

22tEnd = 2 * np.pi 

23nStepsList = np.array([2, 5, 10, 20, 50, 100, 200, 500, 1000]) 

24dtVals = tEnd / nStepsList 

25 

26# Collocation parameters 

27nodeType = "LEGENDRE" 

28 

29 

30def getError(uNum, uRef): 

31 if uNum is None: # pragma: no cover 

32 return np.inf 

33 return np.linalg.norm(uRef - uNum[:, 0], np.inf) 

34 

35 

36# Configuration 

37# (nNodes, quadType, sweepType) 

38config = [ 

39 (4, "RADAU-RIGHT", "MIN-SR-NS"), 

40 (5, "LOBATTO", "MIN-SR-NS"), 

41 (4, "RADAU-RIGHT", "MIN-SR-S"), 

42 (5, "LOBATTO", "MIN-SR-S"), 

43 (4, "RADAU-RIGHT", "MIN-SR-FLEX"), 

44 (5, "LOBATTO", "MIN-SR-FLEX"), 

45] 

46 

47# ----------------------------------------------------------------------------- 

48# Script execution 

49# ----------------------------------------------------------------------------- 

50for nNodes, quadType, sweepType in config: 

51 # Schemes parameters 

52 schemes = [ 

53 # ("RK4", None), ("ESDIRK53", None), 

54 *[(sweepType, i) for i in range(1, nNodes + 1)] 

55 ] 

56 

57 # Plot styles 

58 styles = [ 

59 # dict(ls="--", c="gray"), dict(ls="-.", c="gray"), 

60 dict(ls="-", marker='o'), 

61 dict(ls="-", marker='>'), 

62 dict(ls="-", marker='s'), 

63 dict(ls="-", marker='^'), 

64 dict(ls="-", marker='*'), 

65 ] 

66 

67 # Figure generation 

68 figName = f"{sweepType}_{quadType}" 

69 plt.figure(f"{sweepType}_{quadType}") 

70 for (qDelta, nSweeps), style in zip(schemes, styles, strict=False): 

71 params = getParamsSDC(quadType, nNodes, qDelta, nSweeps, nodeType) 

72 label = f"$K={nSweeps}$" 

73 errors = [] 

74 

75 for nSteps in nStepsList: 

76 uNum, counters, parallel = solutionSDC(tEnd, nSteps, params, 'DAHLQUIST', lambdas=np.array([lam])) 

77 

78 tVals = np.linspace(0, tEnd, nSteps + 1) 

79 uExact = np.exp(lam * tVals) 

80 

81 err = getError(uNum, uExact) 

82 errors.append(err) 

83 

84 plt.loglog(dtVals, errors, **style, label=label) 

85 

86 data.storeAndCheck(f"{SCRIPT}_{figName}_{label}", errors) 

87 

88 if nSweeps is not None: 

89 plt.loglog(dtVals, (0.1 * dtVals) ** nSweeps, '--', c='gray', lw=1.5) 

90 

91 plt.legend() 

92 plt.xlabel(r"$\Delta{t}$") 

93 plt.ylabel(r"$L_\infty$ error") 

94 plt.grid(True) 

95 plt.tight_layout() 

96 plt.savefig(f"{PATH}/{SCRIPT}_{figName}.pdf") 

97 

98data.writeToJSON()