|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pySDC.core.Errors import ParameterError, ProblemError |
| 4 | +from pySDC.core.Problem import ptype |
| 5 | +from pySDC.implementations.datatype_classes.mesh import mesh |
| 6 | + |
| 7 | + |
| 8 | +# noinspection PyUnusedLocal |
| 9 | +class logistics_equation(ptype): |
| 10 | + """ |
| 11 | + Example implementing the logistic equation, taken from |
| 12 | + https://www-users.cse.umn.edu/~olver/ln_/odq.pdf (Example 2.2) |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, problem_params, dtype_u=mesh, dtype_f=mesh): |
| 16 | + """ |
| 17 | + Initialization routine |
| 18 | +
|
| 19 | + Args: |
| 20 | + problem_params (dict): custom parameters for the example |
| 21 | + dtype_u: mesh data type (will be passed parent class) |
| 22 | + dtype_f: mesh data type (will be passed parent class) |
| 23 | + """ |
| 24 | + |
| 25 | + # these parameters will be used later, so assert their existence |
| 26 | + essential_keys = ['u0', 'lam', 'newton_maxiter', 'newton_tol', 'direct'] |
| 27 | + for key in essential_keys: |
| 28 | + if key not in problem_params: |
| 29 | + msg = 'need %s to instantiate problem, only got %s' % (key, str(problem_params.keys())) |
| 30 | + raise ParameterError(msg) |
| 31 | + problem_params['nvars'] = 1 |
| 32 | + |
| 33 | + if 'stop_at_nan' not in problem_params: |
| 34 | + problem_params['stop_at_nan'] = True |
| 35 | + |
| 36 | + # invoke super init, passing dtype_u and dtype_f, plus setting number of elements to 2 |
| 37 | + super(logistics_equation, self).__init__((problem_params['nvars'], None, np.dtype('float64')), |
| 38 | + dtype_u, dtype_f, problem_params) |
| 39 | + |
| 40 | + def u_exact(self, t): |
| 41 | + """ |
| 42 | + Exact solution |
| 43 | +
|
| 44 | + Args: |
| 45 | + t (float): current time |
| 46 | + Returns: |
| 47 | + dtype_u: mesh type containing the values |
| 48 | + """ |
| 49 | + |
| 50 | + me = self.dtype_u(self.init) |
| 51 | + me[:] = self.params.u0 * np.exp(self.params.lam * t) / (1 - self.params.u0 + |
| 52 | + self.params.u0 * np.exp(self.params.lam * t)) |
| 53 | + return me |
| 54 | + |
| 55 | + def eval_f(self, u, t): |
| 56 | + """ |
| 57 | + Routine to compute the RHS |
| 58 | +
|
| 59 | + Args: |
| 60 | + u (dtype_u): the current values |
| 61 | + t (float): current time (not used here) |
| 62 | + Returns: |
| 63 | + dtype_f: RHS, 1 component |
| 64 | + """ |
| 65 | + |
| 66 | + f = self.dtype_f(self.init) |
| 67 | + f[:] = self.params.lam * u * (1 - u) |
| 68 | + return f |
| 69 | + |
| 70 | + def solve_system(self, rhs, dt, u0, t): |
| 71 | + """ |
| 72 | + Simple Newton solver for the nonlinear equation |
| 73 | +
|
| 74 | + Args: |
| 75 | + rhs (dtype_f): right-hand side for the nonlinear system |
| 76 | + dt (float): abbrev. for the node-to-node stepsize (or any other factor required) |
| 77 | + u0 (dtype_u): initial guess for the iterative solver |
| 78 | + t (float): current time (e.g. for time-dependent BCs) |
| 79 | +
|
| 80 | + Returns: |
| 81 | + dtype_u: solution u |
| 82 | + """ |
| 83 | + # create new mesh object from u0 and set initial values for iteration |
| 84 | + u = self.dtype_u(u0) |
| 85 | + |
| 86 | + if self.params.direct: |
| 87 | + |
| 88 | + d = (1 - dt * self.params.lam) ** 2 + 4 * dt * self.params.lam * rhs |
| 89 | + u = (- (1 - dt * self.params.lam) + np.sqrt(d)) / (2 * dt * self.params.lam) |
| 90 | + return u |
| 91 | + |
| 92 | + else: |
| 93 | + |
| 94 | + # start newton iteration |
| 95 | + n = 0 |
| 96 | + res = 99 |
| 97 | + while n < self.params.newton_maxiter: |
| 98 | + |
| 99 | + # form the function g with g(u) = 0 |
| 100 | + g = u - dt * self.params.lam * u * (1 - u) - rhs |
| 101 | + |
| 102 | + # if g is close to 0, then we are done |
| 103 | + res = np.linalg.norm(g, np.inf) |
| 104 | + if res < self.params.newton_tol or np.isnan(res): |
| 105 | + break |
| 106 | + |
| 107 | + # assemble dg/du |
| 108 | + dg = 1 - dt * self.params.lam * (1 - 2 * u) |
| 109 | + # newton update: u1 = u0 - g/dg |
| 110 | + u -= 1.0 / dg * g |
| 111 | + |
| 112 | + # increase iteration count |
| 113 | + n += 1 |
| 114 | + |
| 115 | + if np.isnan(res) and self.params.stop_at_nan: |
| 116 | + raise ProblemError('Newton got nan after %i iterations, aborting...' % n) |
| 117 | + elif np.isnan(res): |
| 118 | + self.logger.warning('Newton got nan after %i iterations...' % n) |
| 119 | + |
| 120 | + if n == self.params.newton_maxiter: |
| 121 | + raise ProblemError('Newton did not converge after %i iterations, error is %s' % (n, res)) |
| 122 | + |
| 123 | + return u |
0 commit comments