|
| 1 | +import matplotlib |
| 2 | + |
| 3 | +matplotlib.use('Agg') |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from matplotlib import pyplot as plt |
| 7 | +from pylab import rcParams |
| 8 | + |
| 9 | +from projects.FastWaveSlowWave.HookClass_acoustic import dump_energy |
| 10 | +from pySDC.implementations.collocation_classes.gauss_radau_right import CollGaussRadau_Right |
| 11 | +from pySDC.implementations.datatype_classes.mesh import mesh, rhs_imex_mesh |
| 12 | +from pySDC.implementations.problem_classes.AcousticAdvection_1D_FD_imex import acoustic_1d_imex |
| 13 | +from pySDC.implementations.sweeper_classes.imex_1st_order import imex_1st_order |
| 14 | +from pySDC.implementations.controller_classes.allinclusive_classic_nonMPI import allinclusive_classic_nonMPI |
| 15 | + |
| 16 | +from pySDC.helpers.stats_helper import filter_stats |
| 17 | + |
| 18 | + |
| 19 | +def compute_and_plot_itererror(): |
| 20 | + """ |
| 21 | + Routine to compute and plot the error over the iterations for difference cs values |
| 22 | + """ |
| 23 | + |
| 24 | + num_procs = 1 |
| 25 | + |
| 26 | + t0 = 0.0 |
| 27 | + Tend = 0.025 |
| 28 | + |
| 29 | + # initialize level parameters |
| 30 | + level_params = dict() |
| 31 | + level_params['restol'] = 1E-10 |
| 32 | + level_params['dt'] = Tend |
| 33 | + |
| 34 | + # This comes as read-in for the step class |
| 35 | + step_params = dict() |
| 36 | + step_params['maxiter'] = 15 |
| 37 | + |
| 38 | + # This comes as read-in for the problem class |
| 39 | + problem_params = dict() |
| 40 | + problem_params['cadv'] = 0.1 |
| 41 | + problem_params['nvars'] = [(2, 300)] |
| 42 | + problem_params['order_adv'] = 5 |
| 43 | + problem_params['waveno'] = 5 |
| 44 | + |
| 45 | + # This comes as read-in for the sweeper class |
| 46 | + sweeper_params = dict() |
| 47 | + sweeper_params['collocation_class'] = CollGaussRadau_Right |
| 48 | + sweeper_params['do_coll_update'] = True |
| 49 | + |
| 50 | + # initialize controller parameters |
| 51 | + controller_params = dict() |
| 52 | + controller_params['logger_level'] = 30 |
| 53 | + |
| 54 | + # Fill description dictionary for easy hierarchy creation |
| 55 | + description = dict() |
| 56 | + description['problem_class'] = acoustic_1d_imex |
| 57 | + description['dtype_u'] = mesh |
| 58 | + description['dtype_f'] = rhs_imex_mesh |
| 59 | + description['sweeper_class'] = imex_1st_order |
| 60 | + |
| 61 | + description['hook_class'] = dump_energy |
| 62 | + description['step_params'] = step_params |
| 63 | + description['level_params'] = level_params |
| 64 | + |
| 65 | + cs_v = [0.5, 1.0, 1.5, 5.0] |
| 66 | + nodes_v = [3] |
| 67 | + |
| 68 | + residual = np.zeros((np.size(cs_v), np.size(nodes_v), step_params['maxiter'])) |
| 69 | + convrate = np.zeros((np.size(cs_v), np.size(nodes_v), step_params['maxiter'] - 1)) |
| 70 | + lastiter = np.zeros((np.size(cs_v), np.size(nodes_v))) + step_params['maxiter'] |
| 71 | + avg_convrate = np.zeros((np.size(cs_v), np.size(nodes_v))) |
| 72 | + |
| 73 | + P = None |
| 74 | + for cs_ind in range(0, np.size(cs_v)): |
| 75 | + problem_params['cs'] = cs_v[cs_ind] |
| 76 | + description['problem_params'] = problem_params |
| 77 | + |
| 78 | + for nodes_ind in np.arange(np.size(nodes_v)): |
| 79 | + |
| 80 | + sweeper_params['num_nodes'] = nodes_v[nodes_ind] |
| 81 | + description['sweeper_params'] = sweeper_params |
| 82 | + |
| 83 | + # instantiate the controller |
| 84 | + controller = allinclusive_classic_nonMPI(num_procs=num_procs, controller_params=controller_params, |
| 85 | + description=description) |
| 86 | + # get initial values on finest level |
| 87 | + P = controller.MS[0].levels[0].prob |
| 88 | + uinit = P.u_exact(t0) |
| 89 | + |
| 90 | + print("Fast CFL number: %4.2f" % (problem_params['cs'] * level_params['dt'] / P.dx)) |
| 91 | + print("Slow CFL number: %4.2f" % (problem_params['cadv'] * level_params['dt'] / P.dx)) |
| 92 | + |
| 93 | + # call main function to get things done... |
| 94 | + uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) |
| 95 | + |
| 96 | + extract_stats = filter_stats(stats, type='residual_post_iteration') |
| 97 | + |
| 98 | + for k, v in extract_stats.items(): |
| 99 | + iter = getattr(k, 'iter') |
| 100 | + if iter is not -1: |
| 101 | + residual[cs_ind, nodes_ind, iter - 1] = v |
| 102 | + |
| 103 | + # Compute convergence rates |
| 104 | + for iter in range(0, step_params['maxiter'] - 1): |
| 105 | + if residual[cs_ind, nodes_ind, iter] < level_params['restol']: |
| 106 | + lastiter[cs_ind, nodes_ind] = iter |
| 107 | + else: |
| 108 | + convrate[cs_ind, nodes_ind, iter] = residual[cs_ind, nodes_ind, iter + 1] / \ |
| 109 | + residual[cs_ind, nodes_ind, iter] |
| 110 | + avg_convrate[cs_ind, nodes_ind] = np.sum(convrate[cs_ind, nodes_ind, :]) / \ |
| 111 | + float(lastiter[cs_ind, nodes_ind]) |
| 112 | + |
| 113 | + # Plot the results |
| 114 | + fs = 8 |
| 115 | + color = ['r', 'b', 'g', 'c'] |
| 116 | + shape = ['o-', 'd-', 's-', '>-'] |
| 117 | + rcParams['figure.figsize'] = 2.5, 2.5 |
| 118 | + fig = plt.figure() |
| 119 | + for ii in range(0, np.size(cs_v)): |
| 120 | + x = np.arange(1, lastiter[ii, 0]) |
| 121 | + y = convrate[ii, 0, 0:lastiter[ii, 0] - 1] |
| 122 | + plt.plot(x, y, shape[ii], markersize=fs - 2, color=color[ii], |
| 123 | + label=r'$C_{\rm fast}$=%4.2f' % (cs_v[ii] * level_params['dt'] / P.dx)) |
| 124 | + # plt.plot(x, 0.0*y+avg_convrate[ii,0], '--', color=color[ii]) |
| 125 | + |
| 126 | + plt.legend(loc='upper right', fontsize=fs, prop={'size': fs - 2}) |
| 127 | + plt.xlabel('Iteration', fontsize=fs) |
| 128 | + plt.ylabel(r'$|| r^{k+1} ||_{\infty}/|| r^k ||_{\infty}$', fontsize=fs, labelpad=2) |
| 129 | + plt.xlim([0, step_params['maxiter']]) |
| 130 | + plt.ylim([0, 1.0]) |
| 131 | + plt.yticks(fontsize=fs) |
| 132 | + plt.xticks(fontsize=fs) |
| 133 | + filename = 'iteration.png' |
| 134 | + fig.savefig(filename, bbox_inches='tight') |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + compute_and_plot_itererror() |
0 commit comments