-
Notifications
You must be signed in to change notification settings - Fork 37
More plots for the RBC paper #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
76724ad
Added order script
brownbaerchen b54285d
Fixes
5a0d1a7
Fixed plotting
brownbaerchen 8773225
Fixed recording
d19a4ee
Added order plot for 3D RBC
brownbaerchen 6cbb58b
Added spectrum plot
brownbaerchen 10efb23
Refactor
brownbaerchen 32d081e
Refactor
brownbaerchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import os | ||
| import pickle | ||
| import numpy as np | ||
| from pySDC.helpers.fieldsIO import FieldsIO | ||
| from pySDC.projects.GPU.configs.base_config import get_config | ||
| from pySDC.implementations.problem_classes.RayleighBenard3D import RayleighBenard3D | ||
| from mpi4py import MPI | ||
| import matplotlib.pyplot as plt | ||
| from pySDC.helpers.plot_helper import figsize_by_journal | ||
| from pySDC.projects.GPU.analysis_scripts.RBC3D_plotting_utils import get_plotting_style, savefig | ||
|
|
||
| step_sizes = { | ||
| 'RBC3DG4R4Ra1e5': [8e-2, 4e-2, 2e-2, 1e-2, 5e-3], | ||
| 'RBC3DG4R4SDC23Ra1e5': [5e-3 * 2**i for i in range(8)], | ||
| 'RBC3DG4R4SDC34Ra1e5': [5e-3 * 2**i for i in range(8)], | ||
| 'RBC3DG4R4SDC44Ra1e5': [5e-3 * 2**i for i in range(8)], | ||
| 'RBC3DG4R4RKRa1e5': [5e-3 * 2**i for i in range(8)], | ||
| 'RBC3DG4R4EulerRa1e5': [5e-3 * 2**i for i in range(8)], | ||
| } | ||
| n_freefall_times = {} | ||
|
|
||
|
|
||
| def no_logging_hook(*args, **kwargs): | ||
| return None | ||
|
|
||
|
|
||
| def get_path(args): | ||
| config = get_config(args) | ||
| fname = config.get_file_name() | ||
| return f'{fname[:fname.index('dt')]}order.pickle' | ||
|
|
||
|
|
||
| def compute_errors(args, dts, Tend): | ||
| errors = {'u': [], 'v': [], 'w': [], 'T': [], 'p': [], 'dt': []} | ||
| prob = RayleighBenard3D(nx=4, ny=4, nz=4, comm=MPI.COMM_SELF) | ||
|
|
||
| dts = np.sort(dts)[::-1] | ||
| ref = run(args, dts[-1], Tend) | ||
| for dt in dts[:-1]: | ||
| u = run(args, dt, Tend) | ||
| e = u - ref | ||
| for comp in ['u', 'v', 'w', 'T', 'p']: | ||
| i = prob.index(comp) | ||
| e_comp = np.max(np.abs(e[i])) / np.max(np.abs(ref[i])) | ||
| e_comp = MPI.COMM_WORLD.allreduce(e_comp, op=MPI.MAX) | ||
| errors[comp].append(float(e_comp)) | ||
| errors['dt'].append(dt) | ||
|
|
||
| path = get_path(args) | ||
| if MPI.COMM_WORLD.rank == 0: | ||
| with open(path, 'wb') as file: | ||
| pickle.dump(errors, file) | ||
| print(f'Saved errors to {path}', flush=True) | ||
|
|
||
|
|
||
| def plot_error_all_components(args): # pragma: no cover | ||
| fig, ax = plt.subplots() | ||
| with open(get_path(args), 'rb') as file: | ||
| errors = pickle.load(file) | ||
|
|
||
| for comp in ['u', 'v', 'w', 'T', 'p']: | ||
| e = np.array(errors[comp]) | ||
| dt = np.array(errors['dt']) | ||
| order = np.log(e[1:] / e[:-1]) / np.log(dt[1:] / dt[:-1]) | ||
| ax.loglog(errors['dt'], errors[comp], label=f'{comp} order {np.mean(order):.1f}') | ||
|
|
||
| ax.loglog(errors['dt'], np.array(errors['dt']) ** 4, label='Order 4', ls='--') | ||
| ax.loglog(errors['dt'], np.array(errors['dt']) ** 2, label='Order 2', ls='--') | ||
| ax.legend() | ||
| ax.set_xlabel(r'$\Delta t$') | ||
| ax.set_ylabel(r'$e$') | ||
|
|
||
|
|
||
| def compare_order(Ra): # pragma: no cover | ||
| fig, ax = plt.subplots(figsize=figsize_by_journal('Nature_CS', 1, 0.6)) | ||
| if Ra == 1e5: | ||
| names = ['RK', 'Euler', 'SDC23', 'SDC34', 'SDC44'][::-1] | ||
| configs = [f'RBC3DG4R4{me}Ra1e5' for me in names] | ||
| paths = [f'./data/RBC3DG4R4{me}Ra1e5-res-1-order.pickle' for me in names] | ||
|
|
||
| else: | ||
| raise NotImplementedError | ||
|
|
||
| for path, config in zip(paths, configs, strict=True): | ||
| with open(path, 'rb') as file: | ||
| errors = pickle.load(file) | ||
|
|
||
| e = np.array(errors['T']) | ||
| dt = np.array(errors['dt']) | ||
| order = np.log(e[1:] / e[:-1]) / np.log(dt[1:] / dt[:-1]) | ||
| print(f'{config}: order: mean={np.mean(order):.1f} / median={np.median(order):.1f}') | ||
| ax.loglog(dt, e, **get_plotting_style(config)) | ||
|
|
||
| for _dt in dt: | ||
| for i in [1, 3, 4]: | ||
| ax.text(_dt, _dt**i, i, fontweight='bold', fontsize=14, ha='center', va='center') | ||
| ax.loglog(dt, dt**i, ls=':', color='black') | ||
|
|
||
| ax.legend(frameon=False) | ||
| ax.set_xlabel(r'$\Delta t$') | ||
| ax.set_ylabel(r'$e$') | ||
| savefig(fig, 'RBC3D_order_Ra1e5') | ||
|
|
||
|
|
||
| def run(args, dt, Tend): | ||
| from pySDC.projects.GPU.run_experiment import run_experiment | ||
| from pySDC.core.errors import ConvergenceError | ||
|
|
||
| args['mode'] = 'run' | ||
| args['dt'] = dt | ||
|
|
||
| config = get_config(args) | ||
| config.Tend = n_freefall_times.get(type(config).__name__, 3) | ||
|
|
||
| desc = config.get_description(res=args['res']) | ||
| prob = desc['problem_class'](**desc['problem_params']) | ||
|
|
||
| ic_config_name = type(config).__name__ | ||
| for name in ['RK', 'Euler', 'O3', 'O4', 'SDC23', 'SDC34', 'SDC44']: | ||
| ic_config_name = ic_config_name.replace(name, 'SDC34') | ||
|
|
||
| ic_config = get_config({**args, 'config': ic_config_name}) | ||
| config.ic_config['config'] = type(ic_config) | ||
| config.ic_config['res'] = ic_config.res | ||
| config.ic_config['dt'] = ic_config.dt | ||
|
|
||
| config.get_LogToFile = no_logging_hook | ||
| config.Tend = Tend | ||
|
|
||
| u_hat = run_experiment(args, config) | ||
| u = prob.itransform(u_hat) | ||
|
|
||
| return u | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| from pySDC.projects.GPU.run_experiment import parse_args | ||
|
|
||
| args = parse_args() | ||
|
|
||
| if args['mode'] == 'run': | ||
| config = get_config(args) | ||
| dts = step_sizes[type(config).__name__] | ||
| compute_errors(args, dts, max(dts)) | ||
|
|
||
| if args['config'] is not None: | ||
| plot_error_all_components(args) | ||
|
|
||
| compare_order(1e5) | ||
| if MPI.COMM_WORLD.rank == 0: | ||
| plt.show() |
45 changes: 45 additions & 0 deletions
45
pySDC/projects/GPU/analysis_scripts/RBC3D_plotting_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from pySDC.helpers.plot_helper import figsize_by_journal, setup_mpl | ||
| import warnings | ||
|
|
||
| setup_mpl() | ||
|
|
||
|
|
||
| def get_plotting_style(config): # pragma: no cover | ||
|
|
||
| args = {'color': None, 'ls': None, 'marker': None, 'markersize': 6, 'label': None} | ||
|
|
||
| if config == 'RBC3DG4R4SDC23Ra1e5': | ||
| args['color'] = 'tab:blue' | ||
| args['ls'] = '-' | ||
| args['marker'] = 'o' | ||
| args['label'] = 'SDC23' | ||
| elif config == 'RBC3DG4R4SDC34Ra1e5': | ||
| args['color'] = 'tab:orange' | ||
| args['ls'] = '-' | ||
| args['marker'] = '<' | ||
| args['label'] = 'SDC34' | ||
| elif config in ['RBC3DG4R4SDC44Ra1e5', 'RBC3DG4R4Ra1e5']: | ||
| args['color'] = 'tab:green' | ||
| args['ls'] = '-' | ||
| args['marker'] = 'x' | ||
| args['label'] = 'SDC44' | ||
| elif config == 'RBC3DG4R4EulerRa1e5': | ||
| args['color'] = 'tab:purple' | ||
| args['ls'] = '--' | ||
| args['marker'] = '.' | ||
| args['label'] = 'Euler' | ||
| elif config == 'RBC3DG4R4RKRa1e5': | ||
| args['color'] = 'tab:red' | ||
| args['ls'] = '--' | ||
| args['marker'] = '>' | ||
| args['label'] = 'RK444' | ||
| else: | ||
| warnings.warn(f'No plotting style for {config=!r}') | ||
|
|
||
| return args | ||
|
|
||
|
|
||
| def savefig(fig, name, format='pdf', base_path='./plots', **kwargs): # pragma: no cover | ||
| path = f'{base_path}/{name}.{format}' | ||
| fig.savefig(path, bbox_inches='tight', **kwargs) | ||
| print(f'Saved figure {path!r}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from pySDC.projects.GPU.analysis_scripts.process_RBC3D_data import get_pySDC_data | ||
| from pySDC.projects.GPU.analysis_scripts.RBC3D_plotting_utils import figsize_by_journal, get_plotting_style, savefig | ||
| import matplotlib.pyplot as plt | ||
|
|
||
|
|
||
| def plot_spectrum(res, dt, config_name, ax): # pragma: no cover | ||
| data = get_pySDC_data(res=res, dt=dt, config_name=config_name) | ||
|
|
||
| spectrum = data['avg_spectrum'] | ||
| k = data['k'] | ||
| ax.loglog(k[spectrum > 1e-16], spectrum[spectrum > 1e-16], **get_plotting_style(config_name), markevery=5) | ||
| ax.set_xlabel('$k$') | ||
| ax.set_ylabel(r'$\|\hat{u}_x\|$') | ||
|
|
||
|
|
||
| def plot_spectra_Ra1e5(): # pragma: no cover | ||
| fig, ax = plt.subplots(figsize=figsize_by_journal('Nature_CS', 1, 0.6)) | ||
|
|
||
| configs = [f'RBC3DG4R4{name}Ra1e5' for name in ['SDC34', 'SDC23', '', 'Euler', 'RK']] | ||
| dts = [0.06, 0.06, 0.06, 0.02, 0.04] | ||
| res = 32 | ||
|
|
||
| for config, dt in zip(configs, dts, strict=True): | ||
| plot_spectrum(res, dt, config, ax) | ||
|
|
||
| ax.legend(frameon=False) | ||
| savefig(fig, 'RBC3D_spectrum_Ra1e5') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| plot_spectra_Ra1e5() | ||
|
|
||
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are there no decorators here to flag the tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The project tests are all run in the project specific environment. So in the current CI, environment markers for project tests have no impact.
The GPU project is tested on JUWELS using the environment we set up there, so it's not even using micromamba.
Now that I think about it, it may make sense to put the 3D RBC stuff in its own project so the tests can be run on GitHub and CPUs. For testing purposes, CPUs should be fine.
What do you think? New project or tests on JUWELS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both, since we need the GPU tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we really need GPU tests for this. We have tests that the GPU port of spectral discretizations generally works, so CPU only testing for this should be fine. While a test of course doesn't hurt, I think the work involved here would not be worth while.
I'll set up a new project then and do CPU tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK! What should we do with this PR, then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we merge it and then I do the move in a separate PR to keep the diff small.