88from pySDC .implementations .hooks .plotting import PlotPostStep
99
1010
11- def run_heat (dt = 1e-1 , Tend = 4 , plotting = False ):
11+ def run_heat (dt = 1e-1 , Tend = 4 , kmax = 5 , ft = np . pi , plotting = False ):
1212 level_params = {}
1313 level_params ['dt' ] = dt
1414
@@ -17,13 +17,13 @@ def run_heat(dt=1e-1, Tend=4, plotting=False):
1717 sweeper_params ['num_nodes' ] = 3
1818 sweeper_params ['QI' ] = 'LU'
1919
20- problem_params = {}
20+ problem_params = {'ft' : ft , 'spectral_space' : False }
2121
2222 step_params = {}
23- step_params ['maxiter' ] = 4
23+ step_params ['maxiter' ] = kmax
2424
2525 controller_params = {}
26- controller_params ['logger_level' ] = 15
26+ controller_params ['logger_level' ] = 30
2727 if plotting :
2828 controller_params ['hook_class' ] = PlotPostStep
2929
@@ -44,9 +44,57 @@ def run_heat(dt=1e-1, Tend=4, plotting=False):
4444
4545 uend , stats = controller .run (u0 = uinit , t0 = t0 , Tend = Tend )
4646
47- return stats , controller
47+ return uend , stats , controller
48+
49+
50+ def compute_errors (Tend , N , ft , kmax ):
51+ solutions = []
52+ dts = []
53+
54+ for n in range (N ):
55+ dt = Tend / (2 ** n )
56+ u , _ , _ = run_heat (dt = dt , Tend = Tend , ft = ft , kmax = kmax , plotting = False )
57+
58+ solutions .append (u )
59+ dts .append (dt )
60+
61+ errors = [abs (solutions [i ] - solutions [- 1 ]) / abs (solutions [- 1 ]) for i in range (N - 1 )]
62+ _dts = [dts [i ] for i in range (N - 1 )]
63+
64+ return _dts , errors
65+
66+
67+ def compute_order (dts , errors ):
68+ orders = []
69+ for i in range (len (errors ) - 1 ):
70+ if errors [i + 1 ] < 1e-12 :
71+ break
72+ orders .append (np .log (errors [i ] / errors [i + 1 ]) / np .log (dts [i ] / dts [i + 1 ]))
73+ return np .median (orders )
74+
75+
76+ def plot_order (): # pragma: no cover
77+ fig , axs = plt .subplots (1 , 2 , sharex = True , sharey = True , figsize = (9 , 4 ))
78+
79+ for k in range (1 , 10 ):
80+ dts , errors = compute_errors (4 , 8 , 0 , k )
81+ order = compute_order (dts , errors )
82+ axs [0 ].loglog (dts , errors , label = f'{ k = } , { order = :.1f} ' )
83+
84+ dts , errors = compute_errors (4 , 8 , 1 , k )
85+ order = compute_order (dts , errors )
86+ axs [1 ].loglog (dts , errors , label = f'{ k = } , { order = :.1f} ' )
87+
88+ axs [0 ].set_ylabel (r'relative global error' )
89+ axs [0 ].set_xlabel (r'$\Delta t$' )
90+ axs [1 ].set_xlabel (r'$\Delta t$' )
91+ axs [0 ].legend (frameon = False )
92+ axs [1 ].legend (frameon = False )
93+ axs [0 ].set_title ('Constant in time BCs' )
94+ axs [1 ].set_title ('Time-dependent BCs' )
95+ fig .tight_layout ()
4896
4997
5098if __name__ == '__main__' :
51- run_heat ( plotting = True )
99+ plot_order ( )
52100 plt .show ()
0 commit comments