|
5 | 5 |
|
6 | 6 | import sympy |
7 | 7 |
|
8 | | -from .finite_difference import generic_derivative, cross_derivative |
9 | | -from .differentiable import Differentiable, diffify, interp_for_fd, Add, Mul |
10 | | -from .tools import direct, transpose |
11 | | -from .rsfd import d45 |
12 | | -from devito.tools import (as_mapper, as_tuple, frozendict, is_integer, |
13 | | - Pickable) |
| 8 | +from devito.tools import Pickable, as_mapper, as_tuple, frozendict, is_integer |
14 | 9 | from devito.types.dimension import Dimension |
15 | 10 | from devito.types.utils import DimensionTuple |
16 | 11 | from devito.warnings import warn |
17 | 12 |
|
| 13 | +from .differentiable import Add, Differentiable, Mul, diffify, interp_for_fd |
| 14 | +from .finite_difference import cross_derivative, generic_derivative |
| 15 | +from .rsfd import d45 |
| 16 | +from .tools import direct, transpose |
| 17 | + |
18 | 18 | __all__ = ['Derivative'] |
19 | 19 |
|
20 | 20 |
|
@@ -112,27 +112,19 @@ def __new__(cls, expr, *dims, **kwargs): |
112 | 112 |
|
113 | 113 | # It is also possible that the expression itself is just a |
114 | 114 | # `devito.Dimension` type which is: |
115 | | - # - derivative 1 if the dimension coincides and the number of derivatives |
| 115 | + # - derivative 1 if the Dimension coincides and the number of derivatives |
116 | 116 | # is 1 ie: `Derivative(x, (x, 1)) == 1`. |
117 | | - # - derivative 0 if the dimension coincides and the total number of |
| 117 | + # - derivative 0 if the Dimension coincides and the total number of |
118 | 118 | # derivatives is greater than 1 ie: `Derivative(x, (x, 2)) == 0` and |
119 | 119 | # `Derivative(x, x, y) == 0`. |
120 | | - # - An error otherwise. |
121 | | - if isinstance(expr, Dimension): |
122 | | - if expr in dcounter.keys(): |
123 | | - if dcounter[expr] == 0: |
124 | | - raise ValueError( |
125 | | - f'Cannot interpolate a dimension `{expr}` onto itself' |
126 | | - ) |
127 | | - elif dcounter.pop(expr) == 1 and not dcounter: |
128 | | - return 1 |
129 | | - else: |
130 | | - return 0 |
| 120 | + # - An unevaluated expression otherwise. |
| 121 | + if isinstance(expr, Dimension) and expr in dcounter: |
| 122 | + if dcounter[expr] == 0: |
| 123 | + pass |
| 124 | + elif dcounter.pop(expr) == 1 and not dcounter: |
| 125 | + return 1 |
131 | 126 | else: |
132 | | - raise ValueError( |
133 | | - f'Cannot differentiate one dimension `{expr}` with respect to' |
134 | | - f' another {tuple(dcounter.keys())}' |
135 | | - ) |
| 127 | + return 0 |
136 | 128 |
|
137 | 129 | # Validate the finite difference order `fd_order` |
138 | 130 | fd_order = cls._validate_fd_order(kwargs.get('fd_order'), expr, dims, dcounter) |
@@ -182,12 +174,12 @@ def _validate_expr(expr): |
182 | 174 | convertible to "differentiable" type. |
183 | 175 | """ |
184 | 176 | if type(expr) is sympy.Derivative: |
185 | | - raise ValueError('Cannot nest sympy.Derivative with devito.Derivative') |
| 177 | + raise ValueError("Cannot nest sympy.Derivative with devito.Derivative") |
186 | 178 | if not isinstance(expr, Differentiable): |
187 | 179 | try: |
188 | 180 | expr = diffify(expr) |
189 | 181 | except Exception as e: |
190 | | - raise ValueError('`expr` must be a `Differentiable` type object') from e |
| 182 | + raise ValueError("`expr` must be a `Differentiable` type object") from e |
191 | 183 | return expr |
192 | 184 |
|
193 | 185 | @staticmethod |
@@ -257,7 +249,11 @@ def _validate_fd_order(fd_order, expr, dims, dcounter): |
257 | 249 | Required: `expr`, `dims`, and the derivative counter to validate. |
258 | 250 | If not provided, the maximum supported order will be used. |
259 | 251 | """ |
260 | | - if fd_order is not None: |
| 252 | + if isinstance(expr, Dimension): |
| 253 | + # If the expression is just a dimension `expr.time_order` and |
| 254 | + # `expr.space_order` are not defined |
| 255 | + fd_order = (99,)*len(dcounter) |
| 256 | + elif fd_order is not None: |
261 | 257 | # If `fd_order` is specified, then validate |
262 | 258 | fcounter = defaultdict(int) |
263 | 259 | # First create a dictionary mapping variable wrt which to differentiate |
|
0 commit comments