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