Coverage for pySDC/implementations/problem_classes/polynomial_test_problem.py: 97%

29 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-29 09:02 +0000

1import numpy as np 

2 

3from pySDC.core.Problem import ptype 

4from pySDC.implementations.datatype_classes.mesh import mesh, imex_mesh 

5 

6 

7class polynomial_testequation(ptype): 

8 """ 

9 Dummy problem for tests only! In particular, the `solve_system` function just returns the exact solution instead of 

10 solving an appropriate system. This class is indented to be used for tests of operations that are exact on polynomials. 

11 """ 

12 

13 dtype_u = mesh 

14 dtype_f = mesh 

15 

16 def __init__(self, degree=1, seed=26266): 

17 """Initialization routine""" 

18 

19 # invoke super init, passing number of dofs, dtype_u and dtype_f 

20 super().__init__(init=(1, None, np.dtype('float64'))) 

21 

22 self.rng = np.random.RandomState(seed=seed) 

23 self.poly = np.polynomial.Polynomial(self.rng.rand(degree)) 

24 self._makeAttributeAndRegister('degree', 'seed', localVars=locals(), readOnly=True) 

25 

26 def eval_f(self, u, t): 

27 """ 

28 Derivative of the polynomial. 

29 

30 Parameters 

31 ---------- 

32 u : dtype_u 

33 Current values of the numerical solution. 

34 t : float 

35 Current time of the numerical solution is computed. 

36 

37 Returns 

38 ------- 

39 f : dtype_f 

40 The right-hand side of the problem. 

41 """ 

42 

43 f = self.dtype_f(self.init) 

44 f[:] = self.poly.deriv(m=1)(t) 

45 return f 

46 

47 def solve_system(self, rhs, factor, u0, t): 

48 """ 

49 Just return the exact solution... 

50 

51 Parameters 

52 ---------- 

53 rhs : dtype_f 

54 Right-hand side for the linear system. 

55 factor : float 

56 Abbrev. for the local stepsize (or any other factor required). 

57 u0 : dtype_u 

58 Initial guess for the iterative solver. 

59 t : float 

60 Current time (e.g. for time-dependent BCs). 

61 

62 Returns 

63 ------- 

64 me : dtype_u 

65 The solution as mesh. 

66 """ 

67 

68 return self.u_exact(t) 

69 

70 def u_exact(self, t, **kwargs): 

71 """ 

72 Evaluate the polynomial. 

73 

74 Parameters 

75 ---------- 

76 t : float 

77 Time of the exact solution. 

78 u_init : pySDC.problem.testequation0d.dtype_u 

79 Initial solution. 

80 t_init : float 

81 The initial time. 

82 

83 Returns 

84 ------- 

85 me : dtype_u 

86 The exact solution. 

87 """ 

88 me = self.dtype_u(self.init) 

89 me[:] = self.poly(t) 

90 return me 

91 

92 

93class polynomial_testequation_IMEX(polynomial_testequation): 

94 """ 

95 IMEX version of the polynomial test problem that assigns half the derivative to the implicit part and the other half to the explicit part. 

96 Keep in mind that you still cannot Really perform any solves. 

97 """ 

98 

99 dtype_f = imex_mesh 

100 

101 def eval_f(self, u, t): 

102 """ 

103 Derivative of the polynomial. 

104 

105 Parameters 

106 ---------- 

107 u : dtype_u 

108 Current values of the numerical solution. 

109 t : float 

110 Current time of the numerical solution is computed. 

111 

112 Returns 

113 ------- 

114 f : dtype_f 

115 The right-hand side of the problem. 

116 """ 

117 

118 f = self.dtype_f(self.init) 

119 derivative = self.poly.deriv(m=1)(t) 

120 f.impl[:] = derivative / 2 

121 f.expl[:] = derivative / 2 

122 return f