Coverage for pySDC / projects / parallelSDC_reloaded / lorenz_accuracy.py: 100%

57 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 Dec 5 11:02:39 2023 

5 

6Script to investigate diagonal SDC on Lorenz system 

7 

8- error VS time-step 

9- error VS computation cost 

10 

11Note : implementation in progress ... 

12""" 

13 

14import numpy as np 

15import matplotlib.pyplot as plt 

16 

17from pySDC.projects.parallelSDC_reloaded.utils import getParamsSDC, getParamsRK, solutionSDC, solutionExact 

18 

19tEnd = 1.24 

20 

21 

22def getError(uNum, uRef): 

23 if uNum is None: # pragma: no cover 

24 return np.inf 

25 return np.linalg.norm(np.linalg.norm(uRef - uNum, np.inf, axis=-1), np.inf) 

26 

27 

28def getCost(counters): 

29 nNewton, nRHS, tComp = counters 

30 return nNewton + nRHS 

31 

32 

33# Base variable parameters 

34nNodes = 4 

35quadType = 'RADAU-RIGHT' 

36nodeType = 'LEGENDRE' 

37parEfficiency = 0.8 # 1/nNodes 

38 

39qDeltaList = [ 

40 'RK4', 

41 'ESDIRK53', 

42 'VDHS', 

43 'MIN', 

44 # 'IE', 'LU', 'IEpar', 'PIC', 

45 'MIN-SR-NS', 

46 'MIN-SR-S', 

47 'MIN-SR-FLEX', 

48 "PIC", 

49 # "MIN3", 

50] 

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

52nSweepList = [1, 2, 3, 4] 

53 

54 

55symList = ['o', '^', 's', '>', '*', '<', 'p', '>'] * 10 

56 

57qDeltaList = ['MIN-SR-S', 'RK4'] 

58# nSweepList = [4] 

59 

60fig, axs = plt.subplots(1, 2) 

61 

62 

63dtVals = tEnd / nStepsList 

64 

65i = 0 

66for qDelta in qDeltaList: 

67 for nSweeps in nSweepList: 

68 sym = symList[i] 

69 i += 1 

70 

71 name = f"{qDelta}({nSweeps})" 

72 try: 

73 params = getParamsRK(qDelta) 

74 name = name[:-3] 

75 if nSweeps != nSweepList[0]: 

76 continue 

77 except KeyError: 

78 params = getParamsSDC( 

79 quadType=quadType, numNodes=nNodes, nodeType=nodeType, qDeltaI=qDelta, nSweeps=nSweeps 

80 ) 

81 print(f'computing for {name} ...') 

82 

83 errors = [] 

84 costs = [] 

85 

86 for nSteps in nStepsList: 

87 print(f' -- nSteps={nSteps} ...') 

88 

89 uRef = solutionExact(tEnd, nSteps, "LORENZ", u0=(5, -5, 20)) 

90 

91 uSDC, counters, parallel = solutionSDC(tEnd, nSteps, params, "LORENZ", u0=(5, -5, 20)) 

92 

93 err = getError(uSDC, uRef) 

94 errors.append(err) 

95 

96 cost = getCost(counters) 

97 if parallel: 

98 cost /= nNodes * parEfficiency 

99 costs.append(cost) 

100 

101 # error VS dt 

102 axs[0].loglog(dtVals, errors, sym + '-', label=name) 

103 # error VS cost 

104 axs[1].loglog(costs, errors, sym + '-', label=name) 

105 

106x = dtVals[4:] 

107for k in [1, 2, 3, 4, 5]: 

108 axs[0].loglog(x, 1e4 * x**k, "--", color="gray", linewidth=0.8) 

109 

110for i in range(2): 

111 axs[i].set( 

112 xlabel=r"$\Delta{t}$" if i == 0 else "cost", 

113 ylabel=r"$L_\infty$ error", 

114 ylim=(8.530627786509715e-12, 372.2781393394293), 

115 ) 

116 axs[i].legend(loc="lower right" if i == 0 else "lower left") 

117 axs[i].grid() 

118 

119fig.set_size_inches(12, 5) 

120fig.tight_layout()