22import matplotlib .pyplot as plt
33import numpy as np
44from scipy import integrate
5+ from pySDC .helpers .plot_helper import figsize_by_journal , setup_mpl
6+
7+ setup_mpl ()
58
69
710def get_pySDC_data (Ra , RK = False , res = - 1 , dt = - 1 , config_name = 'RBC3DG4' ):
@@ -282,11 +285,12 @@ def interpolate_NuV_to_reference_times(data, reference_data, order=12):
282285
283286
284287def compare_Nusselt_over_time1e5 ():
285- fig , Nu_ax = plt .subplots ()
286- _ , axs = plt .subplots (1 , 3 , figsize = (10 , 3 ))
287- prof_ax = axs [0 ]
288- rms_ax = axs [1 ]
289- spectrum_ax = axs [2 ]
288+ Nu_fig , Nu_axs = plt .subplots (4 , 1 , sharex = True , sharey = True , figsize = figsize_by_journal ('Nature_CS' , 1 , 1.4 ))
289+ # _, axs = plt.subplots(1, 3, figsize=(10, 3))
290+ # prof_ax = axs[0]
291+ # rms_ax = axs[1]
292+ # spectrum_ax = axs[2]
293+ # deviation_fig, deviation_ax = plt.subplots(figsize=figsize_by_journal('Nature_CS', 0.7, 0.7))
290294
291295 Ra = '1e5'
292296 res = 32
@@ -296,89 +300,151 @@ def compare_Nusselt_over_time1e5():
296300 linestyles = []
297301
298302 ref_data = get_pySDC_data (Ra , res = res , dt = 0.01 , config_name = 'RBC3DG4R4' )
303+ # deviations = {'SDC 3': [], 'SDC': [], 'RK': [], 'Euler': []}
304+ # Nu_all = {'SDC 3': [], 'SDC': [], 'RK': [], 'Euler': []}
305+
306+ _Nu_axs = {'SDC 3' : Nu_axs [1 ], 'SDC' : Nu_axs [0 ], 'RK' : Nu_axs [2 ], 'Euler' : Nu_axs [3 ]}
307+
308+ def _plot_Nu_ (Ra , res , dts , config_name , ref , ax , title ):
309+ ax .plot (ref ['t' ], ref ['Nu' ]['V' ], color = 'black' , ls = '--' )
310+ ax .set_title (title )
311+ Nu_ref = np .array (ref ['Nu' ]['V' ])
312+ t = ref ['t' ]
313+
314+ for dt in dts :
315+ data = get_pySDC_data (Ra = Ra , res = res , dt = dt , config_name = config_name )
316+ t_i , Nu_i = interpolate_NuV_to_reference_times (data , ref )
317+ ax .plot (t_i , Nu_i , label = rf'$\Delta t={{{ dt } }}$' )
318+
319+ # error = np.maximum.accumulate(np.abs(Nu_ref[: Nu_i.shape[0]] - Nu_i) / np.abs(Nu_ref[: Nu_i.shape[0]]))
320+ error = np .abs (Nu_ref [: Nu_i .shape [0 ]] - Nu_i ) / np .abs (Nu_ref [: Nu_i .shape [0 ]])
321+
322+ # compute mean Nu
323+ mask = np .logical_and (t_i >= 100 , t_i <= 200 )
324+ Nu_mean = np .mean (Nu_i [mask ])
325+ Nu_std = np .std (Nu_i [mask ])
326+
327+ last_line = ax .get_lines ()[- 1 ]
328+ if any (error > 1e-2 ):
329+ deviates = min (t_i [error > 1e-2 ])
330+ ax .axvline (deviates , color = last_line .get_color (), ls = ':' )
331+ print (f'{ title } dt={ dt } Nu={ Nu_mean :.3f} +={ Nu_std :.3f} , deviates more than 1% from t={ deviates :.2f} ' )
332+ else :
333+ print (f'{ title } dt={ dt } Nu={ Nu_mean :.3f} +={ Nu_std :.3f} ' )
334+ ax .legend (frameon = True , loc = 'upper left' )
335+
336+ _plot_Nu_ ('1e5' , 32 , [0.06 , 0.04 , 0.02 ], 'RBC3DG4R4' , ref_data , Nu_axs [0 ], 'SDC44' )
337+ _plot_Nu_ (
338+ '1e5' ,
339+ 32 ,
340+ [
341+ 0.06 ,
342+ 0.04 ,
343+ 0.02 ,
344+ ],
345+ 'RBC3DG4R4O4' ,
346+ ref_data ,
347+ Nu_axs [0 ],
348+ 'SDC34' ,
349+ )
350+ _plot_Nu_ ('1e5' , 32 , [0.06 , 0.05 , 0.02 , 0.01 ], 'RBC3DG4R4O3' , ref_data , Nu_axs [1 ], 'SDC23' )
351+ _plot_Nu_ ('1e5' , 32 , [0.05 , 0.04 , 0.02 , 0.01 , 0.005 ], 'RBC3DG4R4RK' , ref_data , Nu_axs [2 ], 'RK443' )
352+ _plot_Nu_ ('1e5' , 32 , [0.02 , 0.01 , 0.005 ], 'RBC3DG4R4Euler' , ref_data , Nu_axs [3 ], 'RK111' )
353+
354+ # for dt in [0.06, 0.02, 0.01]:
355+ # data.append(get_pySDC_data(Ra, res=res, dt=dt, config_name='RBC3DG4R4'))
356+ # labels.append(f'SDC, dt={dt:.4f}')
357+ # linestyles.append('-')
358+
359+ # for dt in [0.06, 0.05, 0.02, 0.01]:
360+ # data.append(get_pySDC_data(Ra, res=res, dt=dt, config_name='RBC3DG4R4O3'))
361+ # labels.append(f'SDC 3, dt={dt:.4f}')
362+ # linestyles.append('-')
299363
300- _ , ting_ax = plt .subplots ()
301-
302- for dt in [0.06 , 0.02 , 0.01 ]:
303- data .append (get_pySDC_data (Ra , res = res , dt = dt , config_name = 'RBC3DG4R4' ))
304- labels .append (f'SDC, dt={ dt :.4f} ' )
305- linestyles .append ('-' )
306-
307- # ----------------- RK ------------------------
308-
309- for dt in [0.05 , 0.04 , 0.02 , 0.01 , 0.005 ]:
310- data .append (get_pySDC_data (Ra , res = res , dt = dt , RK = True , config_name = 'RBC3DG4R4' ))
311- labels .append (f'RK, dt={ dt :.3f} ' )
312- linestyles .append ('--' )
313-
314- # ----------------- Euler ---------------------
315-
316- for dt in [0.02 , 0.005 ]:
317- data .append (get_pySDC_data (Ra , res = res , dt = dt , config_name = 'RBC3DG4R4Euler' ))
318- labels .append (f'Euler, dt={ dt :.3f} ' )
319- linestyles .append ('-.' )
320-
321- for dat , label , linestyle in zip (data , labels , linestyles ):
322- Nu = np .array (dat ['Nu' ]['V' ])
323- t = dat ['t' ]
324- Nu_ref = np .array (ref_data ['Nu' ]['V' ])
325- t_i , Nu_i = interpolate_NuV_to_reference_times (dat , ref_data )
326- Nu_ax .plot (t , Nu , label = label , ls = linestyle )
327-
328- error = np .maximum .accumulate (np .abs (Nu_ref [: Nu_i .shape [0 ]] - Nu_i ) / np .abs (Nu_ref [: Nu_i .shape [0 ]]))
329-
330- # compute mean Nu
331- mask = np .logical_and (t >= 20 , t <= 200 )
332- Nu_mean = np .mean (Nu [mask ])
333- Nu_std = np .std (Nu [mask ])
334-
335- last_line = Nu_ax .get_lines ()[- 1 ]
336- if any (error > 1e-2 ):
337- deviates = min (t_i [error > 1e-2 ])
338- Nu_ax .axvline (deviates , color = last_line .get_color (), ls = last_line .get_linestyle ())
339- print (f'{ label } Nu={ Nu_mean :.3f} +={ Nu_std :.3f} , deviates more than 1% from t={ deviates :.2f} ' )
340- else :
341- print (f'{ label } Nu={ Nu_mean :.3f} +={ Nu_std :.3f} ' )
342-
343- k = dat ['k' ]
344- spectrum = np .array (dat ['spectrum' ])
345- u_spectrum = np .mean (spectrum , axis = 0 )[1 ]
346- idx = dat ['res_in_boundary_layer' ]
347- _s = u_spectrum [idx ]
348- spectrum_ax .loglog (
349- k [_s > 1e-16 ], _s [_s > 1e-16 ], color = last_line .get_color (), ls = last_line .get_linestyle (), label = label
350- )
351-
352- prof_ax .plot (dat ['profile_T' ], dat ['z' ], color = last_line .get_color (), ls = last_line .get_linestyle (), label = label )
353- rms_ax .plot (
354- dat ['rms_profile_T' ], dat ['z' ], color = last_line .get_color (), ls = last_line .get_linestyle (), label = label
355- )
356-
357- print (dat ['avg_Nu' ])
358- try :
359- ting_ax .scatter (abs (dat ['avg_Nu' ]['V' ] - dat ['avg_Nu' ]['thermal' ]), dat ['avg_Nu' ]['b' ], label = label )
360- except :
361- pass
362- ting_ax .set_xscale ('log' )
363- ting_ax .legend ()
364-
365- # if 'dissipation' in dat.keys():
366-
367- # mean_ke = np.mean([me[1] for me in dat['dissipation']])
368- # print(
369- # f'Energy error: {(dat["dissipation"][-1][1] - np.sum([me[0] for me in dat["dissipation"]])/2)/mean_ke:.2e}'
370- # )
371- # ting_ax.plot(t, np.cumsum([me[0] for me in dat['dissipation']]) / 2, label=label)
372- # ting_ax.plot(t, [me[1] for me in dat['dissipation']], label=label, ls='--')
373- # ting_ax.legend()
374-
375- Nu_ax .legend (frameon = True )
376- Nu_ax .set_xlabel ('t' )
377- Nu_ax .set_ylabel ('Nu' )
364+ # # ----------------- RK ------------------------
378365
379- spectrum_ax .legend (frameon = False )
380- spectrum_ax .set_xlabel ('$k$' )
381- spectrum_ax .set_ylabel (r'$\|\hat{u}_x\|$' )
366+ # for dt in [0.05, 0.04, 0.02, 0.01, 0.005]:
367+ # data.append(get_pySDC_data(Ra, res=res, dt=dt, RK=True, config_name='RBC3DG4R4'))
368+ # labels.append(f'RK, dt={dt:.3f}')
369+ # linestyles.append('--')
370+
371+ # # ----------------- Euler ---------------------
372+
373+ # for dt in [0.02, 0.01, 0.005]:
374+ # data.append(get_pySDC_data(Ra, res=res, dt=dt, config_name='RBC3DG4R4Euler'))
375+ # labels.append(f'Euler, dt={dt:.3f}')
376+ # linestyles.append('-.')
377+
378+ # for dat, label, linestyle in zip(data, labels, linestyles):
379+ # Nu_ax = _Nu_axs[label[:label.index('dt')-2]]
380+ # Nu = np.array(dat['Nu']['V'])
381+ # t = dat['t']
382+ # Nu_ref = np.array(ref_data['Nu']['V'])
383+ # t_i, Nu_i = interpolate_NuV_to_reference_times(dat, ref_data)
384+ # Nu_ax.plot(t, Nu, label=label, ls=linestyle)
385+ # dt = float(label[label.index('dt')+3:])
386+
387+ # error = np.maximum.accumulate(np.abs(Nu_ref[: Nu_i.shape[0]] - Nu_i) / np.abs(Nu_ref[: Nu_i.shape[0]]))
388+
389+ # # compute mean Nu
390+ # mask = np.logical_and(t >= 20, t <= 200)
391+ # Nu_mean = np.mean(Nu[mask])
392+ # Nu_std = np.std(Nu[mask])
393+
394+ # for key in Nu_all.keys():
395+ # if str(key) in label:
396+ # Nu_all[key].append((Nu_mean, Nu_std, dt))
397+ # break
398+
399+ # last_line = Nu_ax.get_lines()[-1]
400+ # if any(error > 1e-2):
401+ # deviates = min(t_i[error > 1e-2])
402+ # for key in deviations.keys():
403+ # if str(key) in label:
404+ # deviations[key].append((dt, deviates))
405+ # break
406+ # Nu_ax.axvline(deviates, color=last_line.get_color(), ls=last_line.get_linestyle())
407+ # print(f'{label} Nu={Nu_mean:.3f}+={Nu_std:.3f}, deviates more than 1% from t={deviates:.2f}')
408+ # else:
409+ # print(f'{label} Nu={Nu_mean:.3f}+={Nu_std:.3f}')
410+
411+ # k = dat['k']
412+ # spectrum = np.array(dat['spectrum'])
413+ # u_spectrum = np.mean(spectrum, axis=0)[1]
414+ # idx = dat['res_in_boundary_layer']
415+ # _s = u_spectrum[idx]
416+ # spectrum_ax.loglog(
417+ # k[_s > 1e-16], _s[_s > 1e-16], color=last_line.get_color(), ls=last_line.get_linestyle(), label=label
418+ # )
419+
420+ # prof_ax.plot(dat['profile_T'], dat['z'], color=last_line.get_color(), ls=last_line.get_linestyle(), label=label)
421+ # rms_ax.plot(
422+ # dat['rms_profile_T'], dat['z'], color=last_line.get_color(), ls=last_line.get_linestyle(), label=label
423+ # )
424+
425+ # for key, value in deviations.items():
426+ # deviation_ax.plot([me[0] for me in value], [me[1] for me in value], label=key, marker='.')
427+ # deviation_ax.set_xscale('log')
428+ # deviation_ax.legend(frameon=False)
429+ # deviation_ax.set_xlabel(r'$\Delta t$')
430+ # deviation_ax.set_ylabel('Nu deviates from reference')
431+
432+ # _, Nu_summary_ax = plt.subplots()
433+ # for key in list(Nu_all.keys())[::-1]:
434+ # value = Nu_all[key]
435+ # Nu_summary_ax.errorbar([me[2] for me in value], [me[0] for me in value], yerr=[me[1] for me in value], label=key, marker='.')
436+ # Nu_summary_ax.legend(frameon=False)
437+
438+ # spectrum_ax.legend(frameon=False)
439+ # spectrum_ax.set_xlabel('$k$')
440+ # spectrum_ax.set_ylabel(r'$\|\hat{u}_x\|$')
441+
442+ Nu_axs [- 1 ].set_xlabel ('$t$' )
443+ Nu_axs [- 1 ].set_ylabel ('$Nu$' )
444+
445+ Nu_fig .tight_layout ()
446+ Nu_fig .savefig ('./plots/Nu_over_time_Ra1e5.pdf' , bbox_inches = 'tight' )
447+ # deviation_fig.savefig('./plots/Nu_deviation_Ra1e5.pdf', bbox_inches='tight')
382448
383449
384450def compare_Nusselt_over_time1e6 ():
0 commit comments