Coverage for pySDC/implementations/convergence_controller_classes/estimate_contraction_factor.py: 100%

23 statements  

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

1import numpy as np 

2 

3from pySDC.core.convergence_controller import ConvergenceController 

4from pySDC.implementations.convergence_controller_classes.estimate_embedded_error import EstimateEmbeddedError 

5 

6 

7class EstimateContractionFactor(ConvergenceController): 

8 """ 

9 Estimate the contraction factor by using the evolution of the embedded error estimate across iterations. 

10 """ 

11 

12 def setup(self, controller, params, description, **kwargs): 

13 """ 

14 Add a default value for control order to the parameters. 

15 

16 Args: 

17 controller (pySDC.Controller): The controller 

18 params (dict): Parameters for the convergence controller 

19 description (dict): The description object used to instantiate the controller 

20 

21 Returns: 

22 dict: Updated parameters 

23 """ 

24 return {"control_order": -75, "e_tol": None, **super().setup(controller, params, description, **kwargs)} 

25 

26 def dependencies(self, controller, description, **kwargs): 

27 """ 

28 Load estimator of embedded error. 

29 

30 Args: 

31 controller (pySDC.Controller): The controller 

32 description (dict): The description object used to instantiate the controller 

33 

34 Returns: 

35 None 

36 """ 

37 controller.add_convergence_controller( 

38 EstimateEmbeddedError, 

39 description=description, 

40 ) 

41 

42 def setup_status_variables(self, *args, **kwargs): 

43 """ 

44 Add the embedded error, contraction factor and iterations to convergence variable to the status of the levels. 

45 

46 Returns: 

47 None 

48 """ 

49 self.add_status_variable_to_level('error_embedded_estimate_last_iter') 

50 self.add_status_variable_to_level('contraction_factor') 

51 if self.params.e_tol is not None: 

52 self.add_status_variable_to_level('iter_to_convergence') 

53 

54 def post_iteration_processing(self, controller, S, **kwargs): 

55 """ 

56 Estimate contraction factor here as the ratio of error estimates between iterations and estimate how many more 

57 iterations we need. 

58 

59 Args: 

60 controller (pySDC.controller): The controller 

61 S (pySDC.step): The current step 

62 

63 Returns: 

64 None 

65 """ 

66 for L in S.levels: 

67 if L.status.error_embedded_estimate_last_iter is not None: 

68 L.status.contraction_factor = ( 

69 L.status.error_embedded_estimate / L.status.error_embedded_estimate_last_iter 

70 ) 

71 if self.params.e_tol is not None: 

72 L.status.iter_to_convergence = max( 

73 [ 

74 0, 

75 np.ceil( 

76 np.log(self.params.e_tol / L.status.error_embedded_estimate) 

77 / np.log(L.status.contraction_factor) 

78 ), 

79 ] 

80 ) 

81 

82 def pre_iteration_processing(self, controller, S, **kwargs): 

83 """ 

84 Store the embedded error estimate of the current iteration in a different place so it doesn't get overwritten. 

85 

86 Args: 

87 controller (pySDC.controller): The controller 

88 S (pySDC.Step): The current step 

89 

90 Returns: 

91 None 

92 """ 

93 for L in S.levels: 

94 if L.status.error_embedded_estimate is not None: 

95 L.status.error_embedded_estimate_last_iter = L.status.error_embedded_estimate * 1.0