88import matplotlib .pyplot as plt
99
1010step_sizes = {
11- 'RBC3DG4Ra1e5' : [3 , 1e0 , 1e-1 , 8e-2 , 6e-2 ],
12- 'RBC3DG4RKRa1e5' : [1e3 , 5 , 4 , 1e0 , 1e-1 , 8e-2 , 6e-2 ],
1311 'RBC3DG4R4Ra1e5' : [8e-2 , 4e-2 , 2e-2 , 1e-2 , 5e-3 ],
1412 'RBC3DG4R4RKRa1e5' : [8e-2 , 4e-2 , 2e-2 , 1e-2 , 5e-3 ],
1513}
@@ -23,7 +21,6 @@ def no_logging_hook(*args, **kwargs):
2321def get_path (args ):
2422 config = get_config (args )
2523 fname = config .get_file_name ()
26- print (fname .index ('dt' ))
2724 return f'{ fname [:fname .index ('dt' )]} order.pickle'
2825
2926
@@ -42,25 +39,54 @@ def compute_errors(args, dts, Tend):
4239 e_comp = MPI .COMM_WORLD .allreduce (e_comp , op = MPI .MAX )
4340 errors [comp ].append (e_comp )
4441 errors ['dt' ].append (dt )
45- print (errors )
4642
4743 path = get_path (args )
48- with open (path , 'wb' ) as file :
49- pickle .dump (errors , file )
50- print (f'Saved errors to { path } ' , config .get_file_name ())
44+ if MPI .COMM_WORLD .rank == 0 :
45+ with open (path , 'wb' ) as file :
46+ pickle .dump (errors , file )
47+ print (f'Saved errors to { path } ' , flush = True )
5148
5249
53- def plot_errors (args ):
50+ def plot_error_all_components (args ):
51+ fig , ax = plt .subplots ()
52+ print (get_path (args ))
5453 with open (get_path (args ), 'rb' ) as file :
5554 errors = pickle .load (file )
5655
57- for comp in errors .keys ():
58- plt .loglog (errors ['dt' ], errors [comp ], label = comp )
56+ for comp in ['u' , 'v' , 'w' , 'T' , 'p' ]:
57+ e = np .array (errors [comp ])
58+ dt = np .array (errors ['dt' ])
59+ order = np .log (e [1 :] / e [:- 1 ]) / np .log (dt [1 :] / dt [:- 1 ])
60+ ax .loglog (errors ['dt' ], errors [comp ], label = f'{ comp } order { np .mean (order ):.1f} ' )
5961
60- plt .loglog (errors ['dt' ], np .array (errors ['dt' ]) ** 4 , label = 'Order 4' )
61- plt .loglog (errors ['dt' ], np .array (errors ['dt' ]) ** 2 , label = 'Order 2' )
62- plt .legend ()
63- plt .show ()
62+ ax .loglog (errors ['dt' ], np .array (errors ['dt' ]) ** 4 , label = 'Order 4' , ls = '--' )
63+ ax .loglog (errors ['dt' ], np .array (errors ['dt' ]) ** 2 , label = 'Order 2' , ls = '--' )
64+ ax .legend ()
65+ ax .set_xlabel (r'$\Delta t$' )
66+ ax .set_ylabel (r'$e$' )
67+
68+
69+ def compare_order (Ra ):
70+ fig , ax = plt .subplots ()
71+ ls = {'SDC' : '-' , 'RK' : '--' , 'Euler' : '-.' }
72+ if Ra == 1e5 :
73+ paths = [f'./data/RBC3DG4R4{ me } Ra1e5-res-1-order.pickle' for me in ['' , 'RK' , 'Euler' ]]
74+ labels = ['SDC' , 'RK' , 'Euler' ]
75+ else :
76+ raise NotImplementedError
77+
78+ for path , label in zip (paths , labels ):
79+ with open (path , 'rb' ) as file :
80+ errors = pickle .load (file )
81+
82+ e = np .array (errors ['T' ])
83+ dt = np .array (errors ['dt' ])
84+ order = np .log (e [1 :] / e [:- 1 ]) / np .log (dt [1 :] / dt [:- 1 ])
85+ ax .loglog (dt , e , label = f'{ label } order { np .mean (order ):.1f} ' , ls = ls [label ])
86+
87+ ax .legend (frameon = False )
88+ ax .set_xlabel (r'$\Delta t$' )
89+ ax .set_ylabel (r'$e$' )
6490
6591
6692def run (args , dt , Tend ):
@@ -89,5 +115,10 @@ def run(args, dt, Tend):
89115 config = get_config (args )
90116
91117 dts = step_sizes [type (config ).__name__ ]
92- compute_errors (args , dts , max (dts ))
93- plot_errors (args )
118+ if args ['mode' ] == 'run' :
119+ compute_errors (args , dts , max (dts ))
120+
121+ plot_error_all_components (args )
122+ compare_order (1e5 )
123+ if MPI .COMM_WORLD .rank == 0 :
124+ plt .show ()
0 commit comments