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
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-29 09:02 +0000
1import numpy as np
3from pySDC.core.Problem import ptype
4from pySDC.implementations.datatype_classes.mesh import mesh, imex_mesh
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 """
13 dtype_u = mesh
14 dtype_f = mesh
16 def __init__(self, degree=1, seed=26266):
17 """Initialization routine"""
19 # invoke super init, passing number of dofs, dtype_u and dtype_f
20 super().__init__(init=(1, None, np.dtype('float64')))
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)
26 def eval_f(self, u, t):
27 """
28 Derivative of the polynomial.
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.
37 Returns
38 -------
39 f : dtype_f
40 The right-hand side of the problem.
41 """
43 f = self.dtype_f(self.init)
44 f[:] = self.poly.deriv(m=1)(t)
45 return f
47 def solve_system(self, rhs, factor, u0, t):
48 """
49 Just return the exact solution...
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).
62 Returns
63 -------
64 me : dtype_u
65 The solution as mesh.
66 """
68 return self.u_exact(t)
70 def u_exact(self, t, **kwargs):
71 """
72 Evaluate the polynomial.
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.
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
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 """
99 dtype_f = imex_mesh
101 def eval_f(self, u, t):
102 """
103 Derivative of the polynomial.
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.
112 Returns
113 -------
114 f : dtype_f
115 The right-hand side of the problem.
116 """
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