Source code for implementations.problem_classes.polynomial_test_problem

import numpy as np

from pySDC.core.problem import Problem
from pySDC.implementations.datatype_classes.mesh import mesh, imex_mesh


[docs] class polynomial_testequation(Problem): """ Dummy problem for tests only! In particular, the `solve_system` function just returns the exact solution instead of solving an appropriate system. This class is indented to be used for tests of operations that are exact on polynomials. """ dtype_u = mesh dtype_f = mesh xp = np def __init__(self, degree=1, seed=26266, useGPU=False): """Initialization routine""" if useGPU: import cupy as cp from pySDC.implementations.datatype_classes.cupy_mesh import cupy_mesh type(self).xp = cp type(self).dtype_u = cupy_mesh type(self).dtype_f = cupy_mesh # invoke super init, passing number of dofs, dtype_u and dtype_f super().__init__(init=(1, None, np.dtype('float64'))) self.rng = np.random.RandomState(seed=seed) self.poly = np.polynomial.Polynomial(self.rng.rand(degree)) self._makeAttributeAndRegister('degree', 'seed', localVars=locals(), readOnly=True)
[docs] def eval_f(self, u, t): """ Derivative of the polynomial. Parameters ---------- u : dtype_u Current values of the numerical solution. t : float Current time of the numerical solution is computed. Returns ------- f : dtype_f The right-hand side of the problem. """ f = self.dtype_f(self.init) f[:] = self.xp.array(self.poly.deriv(m=1)(t)) return f
[docs] def solve_system(self, rhs, factor, u0, t): """ Just return the exact solution... Parameters ---------- rhs : dtype_f Right-hand side for the linear system. factor : float Abbrev. for the local stepsize (or any other factor required). u0 : dtype_u Initial guess for the iterative solver. t : float Current time (e.g. for time-dependent BCs). Returns ------- me : dtype_u The solution as mesh. """ return self.u_exact(t)
[docs] def u_exact(self, t, **kwargs): """ Evaluate the polynomial. Parameters ---------- t : float Time of the exact solution. u_init : pySDC.problem.testequation0d.dtype_u Initial solution. t_init : float The initial time. Returns ------- me : dtype_u The exact solution. """ me = self.dtype_u(self.init) me[:] = self.xp.array(self.poly(t)) return me
[docs] class polynomial_testequation_IMEX(polynomial_testequation): """ IMEX version of the polynomial test problem that assigns half the derivative to the implicit part and the other half to the explicit part. Keep in mind that you still cannot Really perform any solves. """ dtype_f = imex_mesh
[docs] def eval_f(self, u, t): """ Derivative of the polynomial. Parameters ---------- u : dtype_u Current values of the numerical solution. t : float Current time of the numerical solution is computed. Returns ------- f : dtype_f The right-hand side of the problem. """ f = self.dtype_f(self.init) derivative = self.xp.array(self.poly.deriv(m=1)(t)) f.impl[:] = derivative / 2 f.expl[:] = derivative / 2 return f