|
| 1 | +from __future__ import division |
| 2 | +from collections import defaultdict |
| 3 | +import os |
| 4 | +import dill |
| 5 | + |
| 6 | +import pySDC.helpers.plot_helper as plt_helper |
| 7 | + |
| 8 | +from pySDC.implementations.collocation_classes.gauss_lobatto import CollGaussLobatto |
| 9 | +from pySDC.implementations.controller_classes.allinclusive_classic_nonMPI import allinclusive_classic_nonMPI |
| 10 | +from pySDC.implementations.sweeper_classes.verlet import verlet |
| 11 | +from pySDC.implementations.datatype_classes.particles import particles, acceleration |
| 12 | +from pySDC.implementations.problem_classes.OuterSolarSystem import outer_solar_system |
| 13 | +from pySDC.implementations.transfer_classes.TransferParticles_NoCoarse import particles_to_particles |
| 14 | + |
| 15 | +from pySDC.helpers.stats_helper import filter_stats |
| 16 | +from pySDC.projects.Hamiltonian.hamiltonian_output import hamiltonian_output |
| 17 | + |
| 18 | + |
| 19 | +def setup_outer_solar_system(): |
| 20 | + """ |
| 21 | + Helper routine for setting up everything for the outer solar system problem |
| 22 | +
|
| 23 | + Returns: |
| 24 | + description (dict): description of the controller |
| 25 | + controller_params (dict): controller parameters |
| 26 | + """ |
| 27 | + |
| 28 | + # initialize level parameters |
| 29 | + level_params = dict() |
| 30 | + level_params['restol'] = 1E-10 |
| 31 | + level_params['dt'] = 100.0 |
| 32 | + |
| 33 | + # initialize sweeper parameters |
| 34 | + sweeper_params = dict() |
| 35 | + sweeper_params['collocation_class'] = CollGaussLobatto |
| 36 | + sweeper_params['num_nodes'] = [5] |
| 37 | + sweeper_params['spread'] = False |
| 38 | + |
| 39 | + # initialize problem parameters for the Penning trap |
| 40 | + problem_params = dict() |
| 41 | + |
| 42 | + # initialize step parameters |
| 43 | + step_params = dict() |
| 44 | + step_params['maxiter'] = 50 |
| 45 | + |
| 46 | + # initialize controller parameters |
| 47 | + controller_params = dict() |
| 48 | + controller_params['hook_class'] = hamiltonian_output # specialized hook class for more statistics and output |
| 49 | + controller_params['logger_level'] = 30 |
| 50 | + controller_params['predict'] = False |
| 51 | + |
| 52 | + # Fill description dictionary for easy hierarchy creation |
| 53 | + description = dict() |
| 54 | + description['problem_class'] = outer_solar_system |
| 55 | + description['problem_params'] = problem_params |
| 56 | + description['dtype_u'] = particles |
| 57 | + description['dtype_f'] = acceleration |
| 58 | + description['sweeper_class'] = verlet |
| 59 | + description['sweeper_params'] = sweeper_params |
| 60 | + description['level_params'] = level_params |
| 61 | + description['step_params'] = step_params |
| 62 | + # description['space_transfer_class'] = particles_to_particles |
| 63 | + |
| 64 | + return description, controller_params |
| 65 | + |
| 66 | + |
| 67 | +def run_simulation(prob=None): |
| 68 | + """ |
| 69 | + Routine to run the simulation of a second order problem |
| 70 | +
|
| 71 | + Args: |
| 72 | + prob (str): name of the problem |
| 73 | +
|
| 74 | + """ |
| 75 | + |
| 76 | + if prob == 'outer_solar_system': |
| 77 | + description, controller_params = setup_outer_solar_system() |
| 78 | + # set time parameters |
| 79 | + t0 = 0.0 |
| 80 | + Tend = 100000.0 |
| 81 | + num_procs = 1 |
| 82 | + else: |
| 83 | + raise NotImplemented('Problem type not implemented, got %s' % prob) |
| 84 | + |
| 85 | + # instantiate the controller |
| 86 | + controller = allinclusive_classic_nonMPI(num_procs=num_procs, controller_params=controller_params, |
| 87 | + description=description) |
| 88 | + |
| 89 | + # get initial values on finest level |
| 90 | + P = controller.MS[0].levels[0].prob |
| 91 | + uinit = P.u_exact(t=t0) |
| 92 | + |
| 93 | + # call main function to get things done... |
| 94 | + uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) |
| 95 | + |
| 96 | + fname = 'data/' + prob + '.dat' |
| 97 | + f = open(fname, 'wb') |
| 98 | + dill.dump(stats, f) |
| 99 | + f.close() |
| 100 | + |
| 101 | + assert os.path.isfile(fname), 'Run for %s did not create stats file' % prob |
| 102 | + |
| 103 | + |
| 104 | +def show_results(prob=None, cwd=''): |
| 105 | + """ |
| 106 | + Helper function to plot the error of the Hamiltonian |
| 107 | +
|
| 108 | + Args: |
| 109 | + prob (str): name of the problem |
| 110 | + cwd (str): current working directory |
| 111 | + """ |
| 112 | + f = open(cwd + 'data/' + prob + '.dat', 'rb') |
| 113 | + stats = dill.load(f) |
| 114 | + f.close() |
| 115 | + |
| 116 | + extract_stats = filter_stats(stats, type='err_hamiltonian') |
| 117 | + result = defaultdict(list) |
| 118 | + for k, v in extract_stats.items(): |
| 119 | + result[k.iter].append((k.time, v)) |
| 120 | + for k, v in result.items(): |
| 121 | + # assert k <= 6, 'Number of iterations is too high for %s, got %s' % (prob, k) |
| 122 | + result[k] = sorted(result[k], key=lambda x: x[0]) |
| 123 | + |
| 124 | + plt_helper.mpl.style.use('classic') |
| 125 | + plt_helper.setup_mpl() |
| 126 | + plt_helper.newfig(textwidth=238.96, scale=0.89) |
| 127 | + |
| 128 | + err_ham = 1 |
| 129 | + for k, v in result.items(): |
| 130 | + time = [item[0] for item in v] |
| 131 | + ham = [item[1] for item in v] |
| 132 | + err_ham = ham[-1] |
| 133 | + plt_helper.plt.semilogy(time, ham, '-', lw=1, label='Iter ' + str(k)) |
| 134 | + |
| 135 | + # assert err_ham < 2.3E-08, 'Error in the Hamiltonian is too large for %s, got %s' % (prob, err_ham) |
| 136 | + |
| 137 | + plt_helper.plt.xlabel('Time') |
| 138 | + plt_helper.plt.ylabel('Error in Hamiltonian') |
| 139 | + plt_helper.plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) |
| 140 | + |
| 141 | + fname = 'data/' + prob + '_hamiltonian' |
| 142 | + plt_helper.savefig(fname) |
| 143 | + |
| 144 | + assert os.path.isfile(fname + '.pdf'), 'ERROR: plotting did not create PDF file' |
| 145 | + assert os.path.isfile(fname + '.pgf'), 'ERROR: plotting did not create PGF file' |
| 146 | + assert os.path.isfile(fname + '.png'), 'ERROR: plotting did not create PNG file' |
| 147 | + |
| 148 | + |
| 149 | +def main(): |
| 150 | + prob = 'outer_solar_system' |
| 151 | + run_simulation(prob) |
| 152 | + show_results(prob) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + main() |
0 commit comments