|
| 1 | +# script to run a Gray-Scott problem |
| 2 | +from pySDC.implementations.problem_classes.GrayScott_MPIFFT import grayscott_imex_diffusion |
| 3 | +from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI |
| 4 | +from pySDC.projects.Resilience.hook import hook_collection, LogData |
| 5 | +from pySDC.projects.Resilience.strategies import merge_descriptions |
| 6 | +from pySDC.projects.Resilience.sweepers import imex_1st_order_efficient |
| 7 | +from pySDC.core.convergence_controller import ConvergenceController |
| 8 | +from pySDC.implementations.convergence_controller_classes.estimate_extrapolation_error import ( |
| 9 | + EstimateExtrapolationErrorNonMPI, |
| 10 | +) |
| 11 | +from pySDC.implementations.convergence_controller_classes.check_convergence import CheckConvergence |
| 12 | +from pySDC.projects.Resilience.reachTendExactly import ReachTendExactly |
| 13 | + |
| 14 | +from pySDC.core.errors import ConvergenceError |
| 15 | + |
| 16 | +import numpy as np |
| 17 | + |
| 18 | + |
| 19 | +def u_exact(self, t, u_init=None, t_init=None, recompute=False, _t0=None): |
| 20 | + import pickle |
| 21 | + import os |
| 22 | + |
| 23 | + path = f'data/stats/GS-u_init-{t:.8f}.pickle' |
| 24 | + if os.path.exists(path) and not recompute and t_init is None: |
| 25 | + with open(path, 'rb') as file: |
| 26 | + data = pickle.load(file) |
| 27 | + else: |
| 28 | + from pySDC.helpers.stats_helper import get_sorted |
| 29 | + from pySDC.implementations.convergence_controller_classes.adaptivity import Adaptivity |
| 30 | + |
| 31 | + convergence_controllers = { |
| 32 | + Adaptivity: {'e_tol': 1e-8}, |
| 33 | + } |
| 34 | + desc = {'convergence_controllers': convergence_controllers} |
| 35 | + |
| 36 | + u0 = self._u_exact(0) if u_init is None else u_init |
| 37 | + t0 = 0 if t_init is None else t_init |
| 38 | + |
| 39 | + if t == t0: |
| 40 | + return u0 |
| 41 | + else: |
| 42 | + u0 = u0 if _t0 is None else self.u_exact(_t0) |
| 43 | + _t0 = t0 if _t0 is None else _t0 |
| 44 | + |
| 45 | + stats, _, _ = run_GS(Tend=t, u0=u0, t0=_t0, custom_description=desc) |
| 46 | + |
| 47 | + u = get_sorted(stats, type='u', recomputed=False)[-1] |
| 48 | + data = u[1] |
| 49 | + |
| 50 | + if t0 == 0: |
| 51 | + with open(path, 'wb') as file: |
| 52 | + pickle.dump(data, file) |
| 53 | + |
| 54 | + return data |
| 55 | + |
| 56 | + |
| 57 | +if not hasattr(grayscott_imex_diffusion, '_u_exact'): |
| 58 | + grayscott_imex_diffusion._u_exact = grayscott_imex_diffusion.u_exact |
| 59 | + grayscott_imex_diffusion.u_exact = u_exact |
| 60 | + |
| 61 | + |
| 62 | +def run_GS( |
| 63 | + custom_description=None, |
| 64 | + num_procs=1, |
| 65 | + Tend=1e2, |
| 66 | + hook_class=LogData, |
| 67 | + fault_stuff=None, |
| 68 | + custom_controller_params=None, |
| 69 | + u0=None, |
| 70 | + t0=0, |
| 71 | + use_MPI=False, |
| 72 | + **kwargs, |
| 73 | +): |
| 74 | + """ |
| 75 | + Args: |
| 76 | + custom_description (dict): Overwrite presets |
| 77 | + num_procs (int): Number of steps for MSSDC |
| 78 | + Tend (float): Time to integrate to |
| 79 | + hook_class (pySDC.Hook): A hook to store data |
| 80 | + fault_stuff (dict): A dictionary with information on how to add faults |
| 81 | + custom_controller_params (dict): Overwrite presets |
| 82 | + u0 (dtype_u): Initial value |
| 83 | + t0 (float): Starting time |
| 84 | + use_MPI (bool): Whether or not to use MPI |
| 85 | +
|
| 86 | + Returns: |
| 87 | + dict: The stats object |
| 88 | + controller: The controller |
| 89 | + bool: If the code crashed |
| 90 | + """ |
| 91 | + |
| 92 | + level_params = {} |
| 93 | + level_params['dt'] = 1e0 |
| 94 | + level_params['restol'] = -1 |
| 95 | + |
| 96 | + sweeper_params = {} |
| 97 | + sweeper_params['quad_type'] = 'RADAU-RIGHT' |
| 98 | + sweeper_params['num_nodes'] = 3 |
| 99 | + sweeper_params['QI'] = 'MIN-SR-S' |
| 100 | + sweeper_params['QE'] = 'PIC' |
| 101 | + sweeper_params['initial_guess'] = 'copy' |
| 102 | + |
| 103 | + from mpi4py import MPI |
| 104 | + |
| 105 | + problem_params = { |
| 106 | + 'comm': MPI.COMM_SELF, |
| 107 | + 'num_blobs': -48, |
| 108 | + 'L': 2, |
| 109 | + 'nvars': (128,) * 2, |
| 110 | + 'A': 0.062, |
| 111 | + 'B': 0.1229, |
| 112 | + 'Du': 2e-5, |
| 113 | + 'Dv': 1e-5, |
| 114 | + } |
| 115 | + |
| 116 | + step_params = {} |
| 117 | + step_params['maxiter'] = 5 |
| 118 | + |
| 119 | + convergence_controllers = {} |
| 120 | + convergence_controllers[ReachTendExactly] = {'Tend': Tend} |
| 121 | + |
| 122 | + controller_params = {} |
| 123 | + controller_params['logger_level'] = 15 |
| 124 | + controller_params['hook_class'] = hook_collection + (hook_class if type(hook_class) == list else [hook_class]) |
| 125 | + controller_params['mssdc_jac'] = False |
| 126 | + |
| 127 | + if custom_controller_params is not None: |
| 128 | + controller_params = {**controller_params, **custom_controller_params} |
| 129 | + |
| 130 | + description = {} |
| 131 | + description['problem_class'] = grayscott_imex_diffusion |
| 132 | + description['problem_params'] = problem_params |
| 133 | + description['sweeper_class'] = imex_1st_order_efficient |
| 134 | + description['sweeper_params'] = sweeper_params |
| 135 | + description['level_params'] = level_params |
| 136 | + description['step_params'] = step_params |
| 137 | + description['convergence_controllers'] = convergence_controllers |
| 138 | + |
| 139 | + if custom_description is not None: |
| 140 | + description = merge_descriptions(description, custom_description) |
| 141 | + |
| 142 | + controller_args = { |
| 143 | + 'controller_params': controller_params, |
| 144 | + 'description': description, |
| 145 | + } |
| 146 | + if use_MPI: |
| 147 | + from mpi4py import MPI |
| 148 | + from pySDC.implementations.controller_classes.controller_MPI import controller_MPI |
| 149 | + |
| 150 | + comm = kwargs.get('comm', MPI.COMM_WORLD) |
| 151 | + controller = controller_MPI(**controller_args, comm=comm) |
| 152 | + P = controller.S.levels[0].prob |
| 153 | + else: |
| 154 | + controller = controller_nonMPI(**controller_args, num_procs=num_procs) |
| 155 | + P = controller.MS[0].levels[0].prob |
| 156 | + |
| 157 | + t0 = 0.0 if t0 is None else t0 |
| 158 | + uinit = P.u_exact(t0) if u0 is None else u0 |
| 159 | + |
| 160 | + if fault_stuff is not None: |
| 161 | + from pySDC.projects.Resilience.fault_injection import prepare_controller_for_faults |
| 162 | + |
| 163 | + prepare_controller_for_faults(controller, fault_stuff) |
| 164 | + |
| 165 | + crash = False |
| 166 | + try: |
| 167 | + uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) |
| 168 | + except ConvergenceError as e: |
| 169 | + print(f'Warning: Premature termination!: {e}') |
| 170 | + stats = controller.return_stats() |
| 171 | + crash = True |
| 172 | + return stats, controller, crash |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == '__main__': |
| 176 | + stats, _, _ = run_GS() |
0 commit comments