|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pySDC.helpers.stats_helper import filter_stats, sort_stats |
| 4 | +from pySDC.implementations.collocation_classes.gauss_radau_right import CollGaussRadau_Right |
| 5 | +from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI |
| 6 | +from pySDC.implementations.problem_classes.TestEquation_0D import testequation0d |
| 7 | +from pySDC.implementations.sweeper_classes.generic_implicit import generic_implicit |
| 8 | +from pySDC.playgrounds.Gander.HookClass_error_output import error_output |
| 9 | + |
| 10 | + |
| 11 | +def testequation_setup(prec_type=None, maxiter=None): |
| 12 | + """ |
| 13 | + Setup routine for the test equation |
| 14 | +
|
| 15 | + Args: |
| 16 | + par (float): parameter for controlling stiffness |
| 17 | + """ |
| 18 | + # initialize level parameters |
| 19 | + level_params = dict() |
| 20 | + level_params['restol'] = 0.0 |
| 21 | + level_params['dt'] = 0.25 |
| 22 | + level_params['nsweeps'] = [1] |
| 23 | + |
| 24 | + # initialize sweeper parameters |
| 25 | + sweeper_params = dict() |
| 26 | + sweeper_params['collocation_class'] = CollGaussRadau_Right |
| 27 | + sweeper_params['num_nodes'] = [3] |
| 28 | + sweeper_params['QI'] = prec_type |
| 29 | + sweeper_params['initial_guess'] = 'spread' |
| 30 | + |
| 31 | + # initialize problem parameters |
| 32 | + problem_params = dict() |
| 33 | + problem_params['u0'] = 1.0 # initial value (for all instances) |
| 34 | + # use single values like this... |
| 35 | + # problem_params['lambdas'] = [[-1.0]] |
| 36 | + # .. or a list of values like this ... |
| 37 | + # problem_params['lambdas'] = [[-1.0, -2.0, 1j, -1j]] |
| 38 | + problem_params['lambdas'] = [[-1000]] |
| 39 | + # note: PFASST will do all of those at once, but without interaction (realized via diagonal matrix). |
| 40 | + # The propagation matrix will be diagonal too, corresponding to the respective lambda value. |
| 41 | + |
| 42 | + # initialize step parameters |
| 43 | + step_params = dict() |
| 44 | + step_params['maxiter'] = maxiter |
| 45 | + |
| 46 | + # initialize controller parameters |
| 47 | + controller_params = dict() |
| 48 | + controller_params['logger_level'] = 30 |
| 49 | + controller_params['hook_class'] = error_output |
| 50 | + |
| 51 | + # fill description dictionary for easy step instantiation |
| 52 | + description = dict() |
| 53 | + description['problem_class'] = testequation0d # pass problem class |
| 54 | + description['problem_params'] = problem_params # pass problem parameters |
| 55 | + description['sweeper_class'] = generic_implicit # pass sweeper |
| 56 | + description['sweeper_params'] = sweeper_params # pass sweeper parameters |
| 57 | + description['level_params'] = level_params # pass level parameters |
| 58 | + description['step_params'] = step_params # pass step parameters |
| 59 | + |
| 60 | + return description, controller_params |
| 61 | + |
| 62 | + |
| 63 | +def compare_preconditioners(f=None, list_of_k=None): |
| 64 | + |
| 65 | + # set time parameters |
| 66 | + t0 = 0.0 |
| 67 | + Tend = 1.0 |
| 68 | + |
| 69 | + for k in list_of_k: |
| 70 | + |
| 71 | + description_IE, controller_params_IE = testequation_setup(prec_type='IE', maxiter=k) |
| 72 | + description_LU, controller_params_LU = testequation_setup(prec_type='LU', maxiter=k) |
| 73 | + |
| 74 | + out = f'\nWorking with maxiter = {k}' |
| 75 | + f.write(out + '\n') |
| 76 | + print(out) |
| 77 | + |
| 78 | + # instantiate controller |
| 79 | + controller_IE = controller_nonMPI(num_procs=1, controller_params=controller_params_IE, description=description_IE) |
| 80 | + controller_LU = controller_nonMPI(num_procs=1, controller_params=controller_params_LU, description=description_LU) |
| 81 | + |
| 82 | + # get initial values on finest level |
| 83 | + P = controller_IE.MS[0].levels[0].prob |
| 84 | + uinit = P.u_exact(t0) |
| 85 | + uex = P.u_exact(Tend) |
| 86 | + |
| 87 | + # this is where the iteration is happening |
| 88 | + uend_IE, stats_IE = controller_IE.run(u0=uinit, t0=t0, Tend=Tend) |
| 89 | + uend_LU, stats_LU = controller_LU.run(u0=uinit, t0=t0, Tend=Tend) |
| 90 | + |
| 91 | + diff = abs(uend_IE - uend_LU) |
| 92 | + |
| 93 | + err_IE = abs(uend_IE - uex) |
| 94 | + err_LU = abs(uend_LU - uex) |
| 95 | + |
| 96 | + out = ' Error (IE/LU) vs. exact solution: %6.4e -- %6.4e' % (err_IE, err_LU) |
| 97 | + f.write(out + '\n') |
| 98 | + print(out) |
| 99 | + out = ' Difference between both results: %6.4e' % diff |
| 100 | + f.write(out + '\n') |
| 101 | + print(out) |
| 102 | + |
| 103 | + # filter statistics by type |
| 104 | + filtered_stats_IE = filter_stats(stats_IE, type='error_after_step') |
| 105 | + filtered_stats_LU = filter_stats(stats_LU, type='error_after_step') |
| 106 | + |
| 107 | + # convert filtered statistics to list |
| 108 | + errors_IE = sort_stats(filtered_stats_IE, sortby='time') |
| 109 | + errors_LU = sort_stats(filtered_stats_LU, sortby='time') |
| 110 | + print(errors_IE) |
| 111 | + print(errors_LU) |
| 112 | + |
| 113 | + # out = ' Iteration counts for matrix-based version: %s' % iter_counts_mat |
| 114 | + # f.write(out + '\n') |
| 115 | + # print(out) |
| 116 | + # out = ' Iteration counts for matrix-free version: %s' % iter_counts |
| 117 | + # f.write(out + '\n') |
| 118 | + # print(out) |
| 119 | + # |
| 120 | + # assert iter_counts == iter_counts_mat, \ |
| 121 | + # 'ERROR: number of iterations differ between matrix-based and matrix-free controller' |
| 122 | + |
| 123 | + |
| 124 | +def main(): |
| 125 | + |
| 126 | + f = open('comparison_IE_vs_LU.txt', 'w') |
| 127 | + compare_preconditioners(f=f, list_of_k=[1, 2, 3, 4]) |
| 128 | + f.close() |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + main() |
0 commit comments