|
| 1 | +# script to run a Lorenz attractor problem |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
| 4 | + |
| 5 | +from pySDC.helpers.stats_helper import get_sorted, get_list_of_types |
| 6 | +from pySDC.implementations.problem_classes.Lorenz import LorenzAttractor |
| 7 | +from pySDC.implementations.sweeper_classes.generic_implicit import generic_implicit |
| 8 | +from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI |
| 9 | +from pySDC.implementations.convergence_controller_classes.adaptivity import Adaptivity |
| 10 | +from pySDC.core.Errors import ProblemError |
| 11 | +from pySDC.projects.Resilience.hook import log_data, hook_collection |
| 12 | + |
| 13 | + |
| 14 | +def run_Lorenz( |
| 15 | + custom_description=None, |
| 16 | + num_procs=1, |
| 17 | + Tend=1.0, |
| 18 | + hook_class=log_data, |
| 19 | + fault_stuff=None, |
| 20 | + custom_controller_params=None, |
| 21 | + custom_problem_params=None, |
| 22 | + use_MPI=False, |
| 23 | + **kwargs, |
| 24 | +): |
| 25 | + """ |
| 26 | + Run a Lorenz attractor problem with default parameters. |
| 27 | +
|
| 28 | + Args: |
| 29 | + custom_description (dict): Overwrite presets |
| 30 | + num_procs (int): Number of steps for MSSDC |
| 31 | + Tend (float): Time to integrate to |
| 32 | + hook_class (pySDC.Hook): A hook to store data |
| 33 | + fault_stuff (dict): A dictionary with information on how to add faults |
| 34 | + custom_controller_params (dict): Overwrite presets |
| 35 | + custom_problem_params (dict): Overwrite presets |
| 36 | + use_MPI (bool): Whether or not to use MPI |
| 37 | +
|
| 38 | + Returns: |
| 39 | + dict: The stats object |
| 40 | + controller: The controller |
| 41 | + Tend: The time that was supposed to be integrated to |
| 42 | + """ |
| 43 | + |
| 44 | + # initialize level parameters |
| 45 | + level_params = dict() |
| 46 | + level_params['dt'] = 1e-2 |
| 47 | + |
| 48 | + # initialize sweeper parameters |
| 49 | + sweeper_params = dict() |
| 50 | + sweeper_params['quad_type'] = 'RADAU-RIGHT' |
| 51 | + sweeper_params['num_nodes'] = 3 |
| 52 | + sweeper_params['QI'] = 'IE' |
| 53 | + |
| 54 | + problem_params = { |
| 55 | + 'newton_tol': 1e-9, |
| 56 | + 'newton_maxiter': 99, |
| 57 | + } |
| 58 | + |
| 59 | + if custom_problem_params is not None: |
| 60 | + problem_params = {**problem_params, **custom_problem_params} |
| 61 | + |
| 62 | + # initialize step parameters |
| 63 | + step_params = dict() |
| 64 | + step_params['maxiter'] = 4 |
| 65 | + |
| 66 | + # initialize controller parameters |
| 67 | + controller_params = dict() |
| 68 | + controller_params['logger_level'] = 30 |
| 69 | + controller_params['hook_class'] = hook_collection + [hook_class] |
| 70 | + controller_params['mssdc_jac'] = False |
| 71 | + |
| 72 | + if custom_controller_params is not None: |
| 73 | + controller_params = {**controller_params, **custom_controller_params} |
| 74 | + |
| 75 | + # fill description dictionary for easy step instantiation |
| 76 | + description = dict() |
| 77 | + description['problem_class'] = LorenzAttractor |
| 78 | + description['problem_params'] = problem_params |
| 79 | + description['sweeper_class'] = generic_implicit |
| 80 | + description['sweeper_params'] = sweeper_params |
| 81 | + description['level_params'] = level_params |
| 82 | + description['step_params'] = step_params |
| 83 | + |
| 84 | + if custom_description is not None: |
| 85 | + for k in custom_description.keys(): |
| 86 | + if k == 'sweeper_class': |
| 87 | + description[k] = custom_description[k] |
| 88 | + continue |
| 89 | + description[k] = {**description.get(k, {}), **custom_description.get(k, {})} |
| 90 | + |
| 91 | + # set time parameters |
| 92 | + t0 = 0.0 |
| 93 | + |
| 94 | + # instantiate controller |
| 95 | + if use_MPI: |
| 96 | + from mpi4py import MPI |
| 97 | + from pySDC.implementations.controller_classes.controller_MPI import controller_MPI |
| 98 | + |
| 99 | + comm = kwargs.get('comm', MPI.COMM_WORLD) |
| 100 | + controller = controller_MPI(controller_params=controller_params, description=description, comm=comm) |
| 101 | + |
| 102 | + # get initial values on finest level |
| 103 | + P = controller.S.levels[0].prob |
| 104 | + uinit = P.u_exact(t0) |
| 105 | + else: |
| 106 | + controller = controller_nonMPI( |
| 107 | + num_procs=num_procs, controller_params=controller_params, description=description |
| 108 | + ) |
| 109 | + |
| 110 | + # get initial values on finest level |
| 111 | + P = controller.MS[0].levels[0].prob |
| 112 | + uinit = P.u_exact(t0) |
| 113 | + |
| 114 | + # insert faults |
| 115 | + if fault_stuff is not None: |
| 116 | + raise NotImplementedError |
| 117 | + |
| 118 | + # call main function to get things done... |
| 119 | + try: |
| 120 | + uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) |
| 121 | + except ProblemError: |
| 122 | + stats = controller.hooks.return_stats() |
| 123 | + |
| 124 | + return stats, controller, Tend |
| 125 | + |
| 126 | + |
| 127 | +def plot_solution(stats): |
| 128 | + """ |
| 129 | + Plot the solution in 3D. |
| 130 | +
|
| 131 | + Args: |
| 132 | + stats (dict): The stats object of the run |
| 133 | +
|
| 134 | + Returns: |
| 135 | + None |
| 136 | + """ |
| 137 | + fig = plt.figure() |
| 138 | + ax = fig.add_subplot(projection='3d') |
| 139 | + |
| 140 | + u = get_sorted(stats, type='u') |
| 141 | + ax.plot([me[1][0] for me in u], [me[1][1] for me in u], [me[1][2] for me in u]) |
| 142 | + ax.set_xlabel('x') |
| 143 | + ax.set_ylabel('y') |
| 144 | + ax.set_zlabel('z') |
| 145 | + plt.show() |
| 146 | + |
| 147 | + |
| 148 | +def check_solution(stats, controller, thresh=5e-4): |
| 149 | + """ |
| 150 | + Check if the global error solution wrt. a scipy reference solution is tolerable. |
| 151 | +
|
| 152 | + Args: |
| 153 | + stats (dict): The stats object of the run |
| 154 | + controller (pySDC.Controller.controller): The controller |
| 155 | + thresh (float): Threshold for accepting the accuracy |
| 156 | +
|
| 157 | + Returns: |
| 158 | + None |
| 159 | + """ |
| 160 | + u = get_sorted(stats, type='u') |
| 161 | + u_exact = controller.MS[0].levels[0].prob.u_exact(t=u[-1][0]) |
| 162 | + error = np.linalg.norm(u[-1][1] - u_exact) |
| 163 | + |
| 164 | + assert error < thresh, f"Error too large, got e={error:.2e}" |
| 165 | + |
| 166 | + |
| 167 | +def main(plotting=True): |
| 168 | + """ |
| 169 | + Make a test run and see if the accuracy checks out. |
| 170 | +
|
| 171 | + Args: |
| 172 | + plotting (bool): Plot the solution or not |
| 173 | +
|
| 174 | + Returns: |
| 175 | + None |
| 176 | + """ |
| 177 | + custom_description = {} |
| 178 | + custom_description['convergence_controllers'] = {Adaptivity: {'e_tol': 1e-5}} |
| 179 | + custom_controller_params = {'logger_level': 30} |
| 180 | + stats, controller, _ = run_Lorenz( |
| 181 | + custom_description=custom_description, custom_controller_params=custom_controller_params, Tend=10 |
| 182 | + ) |
| 183 | + check_solution(stats, controller, 5e-4) |
| 184 | + if plotting: |
| 185 | + plot_solution(stats) |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == "__main__": |
| 189 | + main() |
0 commit comments