Coverage for pySDC/implementations/problem_classes/polynomial_test_problem.py: 97%
36 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
1import numpy as np
3from pySDC.core.problem import Problem
4from pySDC.implementations.datatype_classes.mesh import mesh, imex_mesh
7class polynomial_testequation(Problem):
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
15 xp = np
17 def __init__(self, degree=1, seed=26266, useGPU=False):
18 """Initialization routine"""
20 if useGPU:
21 import cupy as cp
22 from pySDC.implementations.datatype_classes.cupy_mesh import cupy_mesh
24 type(self).xp = cp
25 type(self).dtype_u = cupy_mesh
26 type(self).dtype_f = cupy_mesh
28 # invoke super init, passing number of dofs, dtype_u and dtype_f
29 super().__init__(init=(1, None, np.dtype('float64')))
31 self.rng = np.random.RandomState(seed=seed)
32 self.poly = np.polynomial.Polynomial(self.rng.rand(degree))
33 self._makeAttributeAndRegister('degree', 'seed', localVars=locals(), readOnly=True)
35 def eval_f(self, u, t):
36 """
37 Derivative of the polynomial.
39 Parameters
40 ----------
41 u : dtype_u
42 Current values of the numerical solution.
43 t : float
44 Current time of the numerical solution is computed.
46 Returns
47 -------
48 f : dtype_f
49 The right-hand side of the problem.
50 """
52 f = self.dtype_f(self.init)
53 f[:] = self.xp.array(self.poly.deriv(m=1)(t))
54 return f
56 def solve_system(self, rhs, factor, u0, t):
57 """
58 Just return the exact solution...
60 Parameters
61 ----------
62 rhs : dtype_f
63 Right-hand side for the linear system.
64 factor : float
65 Abbrev. for the local stepsize (or any other factor required).
66 u0 : dtype_u
67 Initial guess for the iterative solver.
68 t : float
69 Current time (e.g. for time-dependent BCs).
71 Returns
72 -------
73 me : dtype_u
74 The solution as mesh.
75 """
77 return self.u_exact(t)
79 def u_exact(self, t, **kwargs):
80 """
81 Evaluate the polynomial.
83 Parameters
84 ----------
85 t : float
86 Time of the exact solution.
87 u_init : pySDC.problem.testequation0d.dtype_u
88 Initial solution.
89 t_init : float
90 The initial time.
92 Returns
93 -------
94 me : dtype_u
95 The exact solution.
96 """
97 me = self.dtype_u(self.init)
98 me[:] = self.xp.array(self.poly(t))
99 return me
102class polynomial_testequation_IMEX(polynomial_testequation):
103 """
104 IMEX version of the polynomial test problem that assigns half the derivative to the implicit part and the other half to the explicit part.
105 Keep in mind that you still cannot Really perform any solves.
106 """
108 dtype_f = imex_mesh
110 def eval_f(self, u, t):
111 """
112 Derivative of the polynomial.
114 Parameters
115 ----------
116 u : dtype_u
117 Current values of the numerical solution.
118 t : float
119 Current time of the numerical solution is computed.
121 Returns
122 -------
123 f : dtype_f
124 The right-hand side of the problem.
125 """
127 f = self.dtype_f(self.init)
128 derivative = self.xp.array(self.poly.deriv(m=1)(t))
129 f.impl[:] = derivative / 2
130 f.expl[:] = derivative / 2
131 return f