|
11 | 11 | # Modified by Cecile Daversin-Catty, 2018.
|
12 | 12 | # Modified by Ignacia Fierro-Piccardo 2023.
|
13 | 13 |
|
| 14 | +import numpy as np |
14 | 15 | from ufl.argument import Argument
|
| 16 | +from ufl.core.expr import Expr |
15 | 17 | from ufl.core.terminal import FormArgument
|
16 | 18 | from ufl.core.ufl_type import ufl_type
|
17 | 19 | from ufl.duals import is_dual, is_primal
|
|
20 | 22 | from ufl.split_functions import split
|
21 | 23 | from ufl.utils.counted import Counted
|
22 | 24 |
|
| 25 | + |
23 | 26 | # --- The Coefficient class represents a coefficient in a form ---
|
24 | 27 |
|
25 | 28 |
|
@@ -201,6 +204,74 @@ def __repr__(self):
|
201 | 204 | """Representation."""
|
202 | 205 | return self._repr
|
203 | 206 |
|
| 207 | + @Expr.traverse_dag |
| 208 | + @Expr.traverse_dag_apply_coefficient_split_cache |
| 209 | + def traverse_dag_apply_coefficient_split( |
| 210 | + self, |
| 211 | + coefficient_split, |
| 212 | + reference_value=False, |
| 213 | + reference_grad=0, |
| 214 | + restricted=None, |
| 215 | + cache=None, |
| 216 | + ): |
| 217 | + from ufl.classes import ( |
| 218 | + Terminal, |
| 219 | + ComponentTensor, |
| 220 | + MultiIndex, |
| 221 | + NegativeRestricted, |
| 222 | + PositiveRestricted, |
| 223 | + ReferenceGrad, |
| 224 | + ReferenceValue, |
| 225 | + Zero, |
| 226 | + ) |
| 227 | + from ufl.core.multiindex import indices |
| 228 | + from ufl.checks import is_cellwise_constant |
| 229 | + from ufl.domain import extract_unique_domain |
| 230 | + from ufl.tensors import as_tensor |
| 231 | + |
| 232 | + if self not in coefficient_split: |
| 233 | + return Terminal.traverse_dag_apply_coefficient_split( |
| 234 | + self, |
| 235 | + coefficient_split, |
| 236 | + reference_value=reference_value, |
| 237 | + reference_grad=reference_grad, |
| 238 | + restricted=restricted, |
| 239 | + cache=cache, |
| 240 | + ) |
| 241 | + # Reference value expected |
| 242 | + if not reference_value: |
| 243 | + raise RuntimeError(f"ReferenceValue expected: got {o}") |
| 244 | + # Derivative indices |
| 245 | + beta = indices(reference_grad) |
| 246 | + components = [] |
| 247 | + for subcoeff in coefficient_split[self]: |
| 248 | + c = subcoeff |
| 249 | + # Apply terminal modifiers onto the subcoefficient |
| 250 | + if reference_value: |
| 251 | + c = ReferenceValue(c) |
| 252 | + for _ in range(reference_grad): |
| 253 | + # Return zero if expression is trivially constant. This has to |
| 254 | + # happen here because ReferenceGrad has no access to the |
| 255 | + # topological dimension of a literal zero. |
| 256 | + if is_cellwise_constant(c): |
| 257 | + dim = extract_unique_domain(subcoeff).topological_dimension() |
| 258 | + c = Zero(c.ufl_shape + (dim,), c.ufl_free_indices, c.ufl_index_dimensions) |
| 259 | + else: |
| 260 | + c = ReferenceGrad(c) |
| 261 | + if restricted == "+": |
| 262 | + c = PositiveRestricted(c) |
| 263 | + elif restricted == "-": |
| 264 | + c = NegativeRestricted(c) |
| 265 | + elif restricted is not None: |
| 266 | + raise RuntimeError(f"Got unknown restriction: {restricted}") |
| 267 | + # Collect components of the subcoefficient |
| 268 | + for alpha in np.ndindex(subcoeff.ufl_element().reference_value_shape): |
| 269 | + # New modified terminal: component[alpha + beta] |
| 270 | + components.append(c[alpha + beta]) |
| 271 | + # Repack derivative indices to shape |
| 272 | + i, = indices(1) |
| 273 | + return ComponentTensor(as_tensor(components)[i], MultiIndex((i,) + beta)) |
| 274 | + |
204 | 275 |
|
205 | 276 | # --- Helper functions for subfunctions on mixed elements ---
|
206 | 277 |
|
|
0 commit comments