|
| 1 | +import matplotlib |
| 2 | +matplotlib.use('Agg') |
| 3 | + |
| 4 | +import pickle |
| 5 | +import os |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from pySDC.implementations.datatype_classes.mesh import mesh |
| 10 | +from projects.parallelSDC.linearized_implicit_fixed_parallel import linearized_implicit_fixed_parallel |
| 11 | +from projects.parallelSDC.linearized_implicit_fixed_parallel_prec import linearized_implicit_fixed_parallel_prec |
| 12 | +from pySDC.implementations.sweeper_classes.generic_implicit import generic_implicit |
| 13 | +from pySDC.implementations.collocation_classes.gauss_radau_right import CollGaussRadau_Right |
| 14 | +from pySDC.implementations.controller_classes.allinclusive_classic_nonMPI import allinclusive_classic_nonMPI |
| 15 | + |
| 16 | +from projects.parallelSDC.GeneralizedFisher_1D_FD_implicit_Jac import generalized_fisher_jac |
| 17 | +from projects.parallelSDC.ErrReductionHook import err_reduction_hook |
| 18 | + |
| 19 | +from pySDC.helpers.stats_helper import filter_stats, sort_stats |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + # initialize level parameters |
| 24 | + level_params = dict() |
| 25 | + level_params['restol'] = 1E-12 |
| 26 | + |
| 27 | + # This comes as read-in for the step class (this is optional!) |
| 28 | + step_params = dict() |
| 29 | + step_params['maxiter'] = 20 |
| 30 | + |
| 31 | + # This comes as read-in for the problem class |
| 32 | + problem_params = dict() |
| 33 | + problem_params['nu'] = 1 |
| 34 | + problem_params['nvars'] = 2047 |
| 35 | + problem_params['lambda0'] = 5.0 |
| 36 | + problem_params['newton_maxiter'] = 50 |
| 37 | + problem_params['newton_tol'] = 1E-12 |
| 38 | + problem_params['interval'] = (-5, 5) |
| 39 | + |
| 40 | + # This comes as read-in for the sweeper class |
| 41 | + sweeper_params = dict() |
| 42 | + sweeper_params['collocation_class'] = CollGaussRadau_Right |
| 43 | + sweeper_params['num_nodes'] = 5 |
| 44 | + sweeper_params['QI'] = 'LU' |
| 45 | + sweeper_params['fixed_time_in_jacobian'] = 0 |
| 46 | + |
| 47 | + # initialize controller parameters |
| 48 | + controller_params = dict() |
| 49 | + controller_params['logger_level'] = 30 |
| 50 | + controller_params['hook_class'] = err_reduction_hook |
| 51 | + |
| 52 | + # Fill description dictionary for easy hierarchy creation |
| 53 | + description = dict() |
| 54 | + description['problem_class'] = generalized_fisher_jac |
| 55 | + description['problem_params'] = problem_params |
| 56 | + description['dtype_u'] = mesh |
| 57 | + description['dtype_f'] = mesh |
| 58 | + description['sweeper_params'] = sweeper_params |
| 59 | + description['step_params'] = step_params |
| 60 | + |
| 61 | + # setup parameters "in time" |
| 62 | + t0 = 0 |
| 63 | + Tend = 0.1 |
| 64 | + |
| 65 | + sweeper_list = [generic_implicit, linearized_implicit_fixed_parallel, linearized_implicit_fixed_parallel_prec] |
| 66 | + dt_list = [Tend/2**i for i in range(1,5)] |
| 67 | + |
| 68 | + results = dict() |
| 69 | + results['sweeper_list'] = [sweeper.__name__ for sweeper in sweeper_list] |
| 70 | + results['dt_list'] = dt_list |
| 71 | + |
| 72 | + # f = open('parallelSDC_nonlinear_out.txt', 'w') |
| 73 | + # uinit = None |
| 74 | + # uex = None |
| 75 | + # uend = None |
| 76 | + # P = None |
| 77 | + |
| 78 | + # loop over the different sweepers and check results |
| 79 | + for sweeper in sweeper_list: |
| 80 | + description['sweeper_class'] = sweeper |
| 81 | + error_reduction = [] |
| 82 | + for dt in dt_list: |
| 83 | + |
| 84 | + print('Working with sweeper %s and dt = %s...' %(sweeper.__name__,dt)) |
| 85 | + |
| 86 | + level_params['dt'] = dt |
| 87 | + description['level_params'] = level_params |
| 88 | + |
| 89 | + # instantiate the controller |
| 90 | + controller = allinclusive_classic_nonMPI(num_procs=1, controller_params=controller_params, |
| 91 | + description=description) |
| 92 | + |
| 93 | + # get initial values on finest level |
| 94 | + P = controller.MS[0].levels[0].prob |
| 95 | + uinit = P.u_exact(t0) |
| 96 | + |
| 97 | + # call main function to get things done... |
| 98 | + uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) |
| 99 | + |
| 100 | + # filter statistics |
| 101 | + filtered_stats = filter_stats(stats, type='error_pre_iteration') |
| 102 | + error_pre = sort_stats(filtered_stats, sortby='iter')[0][1] |
| 103 | + |
| 104 | + filtered_stats = filter_stats(stats, type='error_post_iteration') |
| 105 | + error_post = sort_stats(filtered_stats, sortby='iter')[0][1] |
| 106 | + |
| 107 | + error_reduction.append(error_post/error_pre) |
| 108 | + |
| 109 | + print('error and reduction rate at time %s: %6.4e -- %6.4e' % (Tend, error_post, error_reduction[-1])) |
| 110 | + |
| 111 | + results[sweeper.__name__] = error_reduction |
| 112 | + print() |
| 113 | + |
| 114 | + file = open('data/error_reduction_data.pkl', 'wb') |
| 115 | + pickle.dump(results, file) |
| 116 | + file.close() |
| 117 | + |
| 118 | + |
| 119 | +def plot_graphs(): |
| 120 | + """ |
| 121 | + Helper function to plot graphs of initial and final values |
| 122 | + """ |
| 123 | + |
| 124 | + file = open('data/error_reduction_data.pkl', 'rb') |
| 125 | + results = pickle.load(file) |
| 126 | + |
| 127 | + sweeper_list = results['sweeper_list'] |
| 128 | + dt_list = results['dt_list'] |
| 129 | + |
| 130 | + color_list = ['red', 'blue', 'green'] |
| 131 | + marker_list = ['o', 's', 'd'] |
| 132 | + label_list = [] |
| 133 | + for sweeper in sweeper_list: |
| 134 | + if sweeper == 'generic_implicit': |
| 135 | + label_list.append('SDC') |
| 136 | + elif sweeper == 'linearized_implicit_fixed_parallel': |
| 137 | + label_list.append('Simplified Newton') |
| 138 | + elif sweeper == 'linearized_implicit_fixed_parallel_prec': |
| 139 | + label_list.append('Inexact Newton') |
| 140 | + |
| 141 | + setups = zip(sweeper_list, color_list, marker_list, label_list) |
| 142 | + |
| 143 | + # Set up plotting parameters |
| 144 | + params = {'legend.fontsize': 20, |
| 145 | + 'figure.figsize': (12, 8), |
| 146 | + 'axes.labelsize': 20, |
| 147 | + 'axes.titlesize': 20, |
| 148 | + 'xtick.labelsize': 16, |
| 149 | + 'ytick.labelsize': 16, |
| 150 | + 'lines.linewidth': 3 |
| 151 | + } |
| 152 | + plt.rcParams.update(params) |
| 153 | + |
| 154 | + # set up figure |
| 155 | + plt.figure() |
| 156 | + plt.xlabel('dt') |
| 157 | + plt.ylabel('error reduction') |
| 158 | + # plt.xlim((interval[0] - 0.01, interval[1] + 0.01)) |
| 159 | + # plt.ylim((-0.1, 1.1)) |
| 160 | + plt.grid() |
| 161 | + |
| 162 | + # compute values for x-axis and plot |
| 163 | + |
| 164 | + for sweeper, color, marker, label in setups: |
| 165 | + |
| 166 | + plt.loglog(dt_list, results[sweeper], lw=3, ls='-', color=color, marker=marker, markersize=10, label=label) |
| 167 | + |
| 168 | + plt.loglog(dt_list, [dt*2 for dt in dt_list], lw=2, ls='--', color='k', label='linear') |
| 169 | + plt.loglog(dt_list, [dt*dt/dt_list[0]*2 for dt in dt_list], lw=2, ls='-.', color='k', label='quadratic') |
| 170 | + |
| 171 | + plt.legend(loc=1, ncol=1, numpoints=1) |
| 172 | + |
| 173 | + plt.gca().invert_xaxis() |
| 174 | + plt.xlim([dt_list[0]*1.1, dt_list[-1]/1.1]) |
| 175 | + plt.ylim([4E-03,1E0]) |
| 176 | + plt.xticks(dt_list,dt_list) |
| 177 | + |
| 178 | + # plt.show() |
| 179 | + |
| 180 | + # save plot as PDF, beautify |
| 181 | + fname = 'data/parallelSDC_fisher_newton.png' |
| 182 | + plt.savefig(fname, rasterized=True, bbox_inches='tight') |
| 183 | + |
| 184 | + # assert os.path.isfile(fname), 'ERROR: plotting did not create file' |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + # main() |
| 189 | + plot_graphs() |
0 commit comments