Coverage for pySDC/projects/AllenCahn_Bayreuth/run_temp_forcing_realistic.py: 0%

74 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-16 14:51 +0000

1from argparse import ArgumentParser 

2import numpy as np 

3from mpi4py import MPI 

4 

5from pySDC.helpers.stats_helper import get_sorted 

6 

7from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI 

8from pySDC.implementations.sweeper_classes.imex_1st_order import imex_1st_order 

9from pySDC.implementations.problem_classes.AllenCahn_Temp_MPIFFT import allencahn_temp_imex 

10 

11from pySDC.projects.AllenCahn_Bayreuth.AllenCahn_dump import dump 

12 

13 

14def run_simulation(name='', spectral=None, nprocs_space=None): 

15 """ 

16 A test program to create reference data for the AC equation with temporal forcing 

17 Args: 

18 name (str): name of the run, will be used to distinguish different setups 

19 spectral (bool): run in real or spectral space 

20 nprocs_space (int): number of processors in space (None if serial) 

21 """ 

22 

23 # set MPI communicator 

24 comm = MPI.COMM_WORLD 

25 

26 world_rank = comm.Get_rank() 

27 world_size = comm.Get_size() 

28 

29 # split world communicator to create space-communicators 

30 if nprocs_space is not None: 

31 color = int(world_rank / nprocs_space) 

32 else: 

33 color = int(world_rank / 1) 

34 space_comm = comm.Split(color=color) 

35 space_rank = space_comm.Get_rank() 

36 space_size = space_comm.Get_size() 

37 

38 assert world_size == space_size, 'This script cannot run parallel-in-time with MPI, only spatial parallelism' 

39 

40 # initialize level parameters 

41 level_params = dict() 

42 level_params['restol'] = 1e-12 

43 level_params['dt'] = 1e-03 

44 level_params['nsweeps'] = [1] 

45 

46 # initialize sweeper parameters 

47 sweeper_params = dict() 

48 sweeper_params['quad_type'] = 'RADAU-RIGHT' 

49 sweeper_params['num_nodes'] = [7] 

50 sweeper_params['QI'] = ['LU'] # For the IMEX sweeper, the LU-trick can be activated for the implicit part 

51 sweeper_params['initial_guess'] = 'spread' 

52 

53 # initialize problem parameters 

54 problem_params = dict() 

55 problem_params['L'] = 1.0 

56 problem_params['nvars'] = [(128, 128)] 

57 problem_params['eps'] = [0.03] 

58 problem_params['radius'] = 0.35682 

59 problem_params['TM'] = 1.0 

60 problem_params['D'] = 10.0 

61 problem_params['dw'] = [1.0] 

62 problem_params['comm'] = space_comm 

63 problem_params['init_type'] = 'circle' 

64 problem_params['spectral'] = spectral 

65 

66 # initialize step parameters 

67 step_params = dict() 

68 step_params['maxiter'] = 50 

69 

70 # initialize controller parameters 

71 controller_params = dict() 

72 controller_params['logger_level'] = 20 if space_rank == 0 else 99 # set level depending on rank 

73 controller_params['hook_class'] = dump 

74 

75 # fill description dictionary for easy step instantiation 

76 description = dict() 

77 description['problem_params'] = problem_params # pass problem parameters 

78 description['sweeper_class'] = imex_1st_order 

79 description['sweeper_params'] = sweeper_params # pass sweeper parameters 

80 description['level_params'] = level_params # pass level parameters 

81 description['step_params'] = step_params # pass step parameters 

82 description['problem_class'] = allencahn_temp_imex 

83 

84 # set time parameters 

85 t0 = 0.0 

86 Tend = 100 * 0.001 

87 

88 if space_rank == 0: 

89 out = f'---------> Running {name} with spectral={spectral} and {space_size} process(es) in space...' 

90 print(out) 

91 

92 # instantiate controller 

93 controller = controller_nonMPI(num_procs=1, controller_params=controller_params, description=description) 

94 

95 # get initial values on finest level 

96 P = controller.MS[0].levels[0].prob 

97 uinit = P.u_exact(t0) 

98 

99 # call main function to get things done... 

100 uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) 

101 

102 if space_rank == 0: 

103 print() 

104 

105 # convert filtered statistics of iterations count, sorted by time 

106 iter_counts = get_sorted(stats, type='niter', sortby='time') 

107 niters = np.mean(np.array([item[1] for item in iter_counts])) 

108 out = f'Mean number of iterations: {niters:.4f}' 

109 print(out) 

110 

111 # get setup time 

112 timing = get_sorted(stats, type='timing_setup', sortby='time') 

113 out = f'Setup time: {timing[0][1]:.4f} sec.' 

114 print(out) 

115 

116 # get running time 

117 timing = get_sorted(stats, type='timing_run', sortby='time') 

118 out = f'Time to solution: {timing[0][1]:.4f} sec.' 

119 print(out) 

120 

121 out = '...Done <---------\n' 

122 print(out) 

123 

124 space_comm.Free() 

125 

126 

127def main(nprocs_space=None): 

128 """ 

129 Little helper routine to run the whole thing 

130 Args: 

131 nprocs_space (int): number of processors in space (None if serial) 

132 """ 

133 name = 'AC-realistic-tempforce' 

134 run_simulation(name=name, spectral=False, nprocs_space=nprocs_space) 

135 # run_simulation(name=name, spectral=True, nprocs_space=nprocs_space) 

136 

137 

138if __name__ == "__main__": 

139 # Add parser to get number of processors in space (have to do this here to enable automatic testing) 

140 parser = ArgumentParser() 

141 parser.add_argument("-n", "--nprocs_space", help='Specifies the number of processors in space', type=int) 

142 args = parser.parse_args() 

143 

144 main(nprocs_space=args.nprocs_space)