Coverage for pySDC/projects/Hamiltonian/hamiltonian_and_energy_output.py: 100%

31 statements  

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

1from pySDC.core.hooks import Hooks 

2 

3 

4class hamiltonian_and_energy_output(Hooks): 

5 def __init__(self): 

6 """ 

7 Initialization of particles output 

8 """ 

9 super(hamiltonian_and_energy_output, self).__init__() 

10 self.ham_init = None 

11 self.energy_init = None 

12 

13 def pre_run(self, step, level_number): 

14 # some abbreviations 

15 L = step.levels[0] 

16 P = L.prob 

17 super(hamiltonian_and_energy_output, self).pre_run(step, level_number) 

18 self.ham_init = P.eval_hamiltonian(L.u[0]) 

19 self.energy_init = P.eval_mode_energy(L.u[0]) 

20 

21 def post_iteration(self, step, level_number): 

22 """ 

23 Overwrite standard post iteration hook 

24 

25 Args: 

26 step (pySDC.Step.step): the current step 

27 level_number (int): the current level number 

28 """ 

29 super(hamiltonian_and_energy_output, self).post_iteration(step, level_number) 

30 

31 # some abbreviations 

32 L = step.levels[0] 

33 P = L.prob 

34 

35 L.sweep.compute_end_point() 

36 H = P.eval_hamiltonian(L.uend) 

37 E = P.eval_mode_energy(L.uend) 

38 

39 self.add_to_stats( 

40 process=step.status.slot, 

41 time=L.time, 

42 level=-1, 

43 iter=step.status.iter, 

44 sweep=L.status.sweep, 

45 type='hamiltonian', 

46 value=H, 

47 ) 

48 

49 self.add_to_stats( 

50 process=step.status.slot, 

51 time=L.time, 

52 level=-1, 

53 iter=step.status.iter, 

54 sweep=L.status.sweep, 

55 type='err_hamiltonian', 

56 value=abs(self.ham_init - H), 

57 ) 

58 

59 self.add_to_stats( 

60 process=step.status.slot, 

61 time=L.time, 

62 level=-1, 

63 iter=step.status.iter, 

64 sweep=L.status.sweep, 

65 type='energy_iter', 

66 value=E, 

67 ) 

68 

69 return None 

70 

71 def post_step(self, step, level_number): 

72 """ 

73 Overwrite standard post iteration hook 

74 

75 Args: 

76 step (pySDC.Step.step): the current step 

77 level_number (int): the current level number 

78 """ 

79 super(hamiltonian_and_energy_output, self).post_step(step, level_number) 

80 

81 # some abbreviations 

82 L = step.levels[0] 

83 P = L.prob 

84 

85 E = P.eval_mode_energy(L.uend) 

86 

87 self.add_to_stats( 

88 process=step.status.slot, 

89 time=L.time, 

90 level=-1, 

91 iter=step.status.iter, 

92 sweep=L.status.sweep, 

93 type='position', 

94 value=L.uend.pos, 

95 ) 

96 

97 self.add_to_stats( 

98 process=step.status.slot, 

99 time=L.time, 

100 level=-1, 

101 iter=step.status.iter, 

102 sweep=L.status.sweep, 

103 type='energy_step', 

104 value=E, 

105 ) 

106 

107 return None