|
| 1 | +r""" |
| 2 | +FEniCS problem with a node-local correction solve. |
| 3 | +
|
| 4 | +Kept in its own module because importing it requires ``dolfin``, which :mod:`problems` does not. |
| 5 | +
|
| 6 | +The correction problem is posed variationally, in the same shape as the stock ``solve_system``: |
| 7 | +find :math:`\delta` such that |
| 8 | +
|
| 9 | +.. math:: |
| 10 | + \langle \delta, q\rangle - factor\,\big[F(w+\delta; q) - F(w; q)\big] = \langle r, q\rangle . |
| 11 | +
|
| 12 | +The increment :math:`F(w+\delta) - F(w)` is **expanded analytically** rather than written as a |
| 13 | +difference of two assembled forms. Assembling both and subtracting would cancel two |
| 14 | +:math:`\mathcal{O}(|F|)` vectors and reinstate an absolute error of order |
| 15 | +:math:`\varepsilon |F|`, which is exactly the failure mode the correction form exists to avoid. |
| 16 | +
|
| 17 | +For Gray-Scott, with :math:`w = (w_1, w_2)` the base and :math:`\delta = (\delta_1, \delta_2)`: |
| 18 | +
|
| 19 | +* diffusion is linear, so its increment is the same form evaluated at :math:`\delta`; |
| 20 | +* :math:`A(1 - u_1) \to -A\delta_1` and :math:`B u_2 \to B\delta_2`; |
| 21 | +* the reaction term expands as |
| 22 | +
|
| 23 | + .. math:: |
| 24 | + (w_1+\delta_1)(w_2+\delta_2)^2 - w_1 w_2^2 |
| 25 | + = w_1\left(2 w_2 \delta_2 + \delta_2^2\right) + \delta_1 (w_2 + \delta_2)^2 , |
| 26 | +
|
| 27 | + in which every term carries an explicit factor :math:`\delta`. |
| 28 | +
|
| 29 | +Reduced precision is **emulated**: DOLFIN inherits PETSc's build-time scalar type, so values are |
| 30 | +rounded through the requested working precision and written back, capping the *information* while |
| 31 | +the arithmetic stays at the backend type. That is optimistic about iteration counts and attainable |
| 32 | +accuracy compared with a real single-precision build. |
| 33 | +""" |
| 34 | + |
| 35 | +import dolfin as df |
| 36 | +import numpy as np |
| 37 | + |
| 38 | +from pySDC.implementations.problem_classes.GrayScott_1D_FEniCS_implicit import fenics_grayscott |
| 39 | + |
| 40 | + |
| 41 | +def quantize_function(function, work_precision): |
| 42 | + """ |
| 43 | + Round a DOLFIN function's coefficients through the working precision, in place. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + function : dolfin.Function |
| 48 | + Function whose vector is quantized. |
| 49 | + work_precision : numpy.dtype or None |
| 50 | + Working precision. ``None`` leaves the function untouched. |
| 51 | +
|
| 52 | + Returns |
| 53 | + ------- |
| 54 | + dolfin.Function |
| 55 | + The same function, for convenience. |
| 56 | + """ |
| 57 | + if work_precision is None: |
| 58 | + return function |
| 59 | + values = function.vector().get_local() |
| 60 | + function.vector().set_local(values.astype(np.dtype(work_precision)).astype(values.dtype)) |
| 61 | + function.vector().apply('insert') |
| 62 | + return function |
| 63 | + |
| 64 | + |
| 65 | +class fenics_grayscott_delta(fenics_grayscott): |
| 66 | + """ |
| 67 | + Gray-Scott exposing ``solve_system_delta`` alongside the stock ``solve_system``. |
| 68 | +
|
| 69 | + Parameters |
| 70 | + ---------- |
| 71 | + solve_precision : dtype-like or None, optional |
| 72 | + Working precision to emulate for the node-local solve. ``None`` keeps backend precision. |
| 73 | + **kwargs |
| 74 | + Forwarded to :class:`fenics_grayscott`. |
| 75 | + """ |
| 76 | + |
| 77 | + def __init__(self, solve_precision=None, **kwargs): |
| 78 | + """Initialization routine""" |
| 79 | + super().__init__(**kwargs) |
| 80 | + self.solve_precision = None if solve_precision is None else np.dtype(solve_precision) |
| 81 | + |
| 82 | + # base state of the correction, assigned per solve |
| 83 | + self.base = df.Function(self.V) |
| 84 | + self.delta = df.Function(self.V) |
| 85 | + |
| 86 | + def _increment_forms(self, test_functions): |
| 87 | + r""" |
| 88 | + Build the analytically expanded weak form of :math:`F(w+\delta) - F(w)`. |
| 89 | +
|
| 90 | + Parameters |
| 91 | + ---------- |
| 92 | + test_functions : tuple |
| 93 | + The two test functions of the mixed space. |
| 94 | +
|
| 95 | + Returns |
| 96 | + ------- |
| 97 | + ufl.Form |
| 98 | + The increment form, every term carrying an explicit factor of the correction. |
| 99 | + """ |
| 100 | + q1, q2 = test_functions |
| 101 | + b1, b2 = df.split(self.base) |
| 102 | + d1, d2 = df.split(self.delta) |
| 103 | + |
| 104 | + # (b1+d1)(b2+d2)^2 - b1 b2^2, expanded so nothing cancels |
| 105 | + reaction = b1 * (2 * b2 * d2 + d2**2) + d1 * (b2 + d2) ** 2 |
| 106 | + |
| 107 | + increment1 = ( |
| 108 | + -self.Du * df.inner(df.nabla_grad(d1), df.nabla_grad(q1)) - reaction * q1 - self.A * d1 * q1 |
| 109 | + ) * df.dx |
| 110 | + increment2 = ( |
| 111 | + -self.Dv * df.inner(df.nabla_grad(d2), df.nabla_grad(q2)) + reaction * q2 - self.B * d2 * q2 |
| 112 | + ) * df.dx |
| 113 | + return increment1 + increment2 |
| 114 | + |
| 115 | + def solve_system_delta(self, r, factor, base, f_base, t): |
| 116 | + r""" |
| 117 | + Solve :math:`\delta - factor\,[f(base+\delta) - f(base)] = r` for the correction. |
| 118 | +
|
| 119 | + Parameters |
| 120 | + ---------- |
| 121 | + r : dtype_u |
| 122 | + Right-hand side of the correction equation. |
| 123 | + factor : float |
| 124 | + Implicit prefactor assembled by the sweeper. |
| 125 | + base : dtype_u |
| 126 | + Base state :math:`w`. |
| 127 | + f_base : dtype_f |
| 128 | + ``f`` evaluated at ``base``; accepted so no extra evaluation is needed. |
| 129 | + t : float |
| 130 | + Physical time, accepted for interface compatibility. |
| 131 | +
|
| 132 | + Returns |
| 133 | + ------- |
| 134 | + dtype_u |
| 135 | + The correction. |
| 136 | + """ |
| 137 | + self.base.assign(base.values) |
| 138 | + self.delta.assign(df.Function(self.V)) # start from the zero correction |
| 139 | + |
| 140 | + q1, q2 = df.TestFunctions(self.V) |
| 141 | + d1, d2 = df.split(self.delta) |
| 142 | + r1, r2 = df.split(r.values) |
| 143 | + |
| 144 | + residual = (d1 * q1 + d2 * q2) * df.dx - factor * self._increment_forms((q1, q2)) |
| 145 | + residual -= (r1 * q1 + r2 * q2) * df.dx |
| 146 | + |
| 147 | + trial = df.TrialFunction(self.V) |
| 148 | + jacobian = df.derivative(residual, self.delta, trial) |
| 149 | + |
| 150 | + problem = df.NonlinearVariationalProblem(residual, self.delta, [], jacobian) |
| 151 | + solver = df.NonlinearVariationalSolver(problem) |
| 152 | + prm = solver.parameters['newton_solver'] |
| 153 | + prm['absolute_tolerance'] = 1e-09 |
| 154 | + prm['relative_tolerance'] = 1e-08 |
| 155 | + prm['maximum_iterations'] = 100 |
| 156 | + prm['relaxation_parameter'] = 1.0 |
| 157 | + solver.solve() |
| 158 | + |
| 159 | + quantize_function(self.delta, self.solve_precision) |
| 160 | + |
| 161 | + me = self.dtype_u(self.V) |
| 162 | + me.values.assign(self.delta) |
| 163 | + return me |
0 commit comments