-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathdifferentiable.py
More file actions
1311 lines (1026 loc) · 42.1 KB
/
Copy pathdifferentiable.py
File metadata and controls
1311 lines (1026 loc) · 42.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import ChainMap
from functools import cached_property, singledispatch
from itertools import product
import numpy as np
import sympy
from sympy.core.add import _addsort
from sympy.core.decorators import call_highest_priority
from sympy.core.evalf import evalf_table
from sympy.core.mul import _keep_coeff, _mulsort
try:
from sympy.core.core import ordering_of_classes
except ImportError:
# Moved in 1.13
from sympy.core.basic import ordering_of_classes
from devito.finite_differences.interpolation import interp_at, post_x0_indices
from devito.finite_differences.tools import coeff_priority, make_shift_x0
from devito.logger import warning
from devito.tools import (
as_tuple, extract_dtype, filter_ordered, flatten, frozendict, infer_dtype, is_integer,
is_number, split
)
from devito.types import Array, DimensionTuple, Evaluable, StencilDimension
from devito.types.basic import AbstractFunction, Indexed
__all__ = [
'Conj',
'DiffDerivative',
'Differentiable',
'EvalDerivative',
'Imag',
'IndexDerivative',
'Real',
'Weights',
]
class Differentiable(sympy.Expr, Evaluable):
"""
A Differentiable is an algebraic expression involving Functions, which can
be derived w.r.t. one or more Dimensions.
"""
# Set the operator priority higher than SymPy (10.0) to force the overridden
# operators to be used
_op_priority = sympy.Expr._op_priority + 1.
__rkwargs__ = ('space_order', 'interp_order', 'time_order', 'indices')
@cached_property
def _functions(self):
return frozenset().union(*[i._functions for i in self._args_diff])
@cached_property
def _args_diff(self):
ret = [i for i in self.args if isinstance(i, Differentiable)]
ret.extend([i.function for i in self.args if i.is_Indexed])
return tuple(ret)
@cached_property
def space_order(self):
# Default 100 is for "infinitely" differentiable
return min([getattr(i, 'space_order', 100) or 100 for i in self._args_diff],
default=100)
@cached_property
def interp_order(self):
# Default 2 is a reasonable default for interpolation
return min([getattr(i, 'interp_order', 2) or 2 for i in self._args_diff],
default=2)
@cached_property
def time_order(self):
# Default 100 is for "infinitely" differentiable
return min([getattr(i, 'time_order', 100) or 100 for i in self._args_diff],
default=100)
@cached_property
def grid(self):
grids = {getattr(i, 'grid', None) for i in self._args_diff} - {None}
grids = {g.root for g in grids}
if len(grids) > 1:
warning("Expression contains multiple grids, returning first found")
try:
return grids.pop()
except KeyError:
return None
@cached_property
def dtype(self):
dtypes = {f.dtype for f in self._functions} - {None}
return infer_dtype(dtypes)
@cached_property
def indices(self):
if not self._args_diff:
return DimensionTuple()
# Get indices of all args and merge them
mapper = {}
for a in self._args_diff:
for d, i in a.indices.getters.items():
mapper.setdefault(d, []).append(i)
# Filter unique indices
mapper = {k: v[0] if len(v) == 1 else tuple(filter_ordered(v))
for k, v in mapper.items()}
return DimensionTuple(*mapper.values(), getters=tuple(mapper.keys()))
@cached_property
def dimensions(self):
if not self._args_diff:
return DimensionTuple()
# Use the staggering of the highest priority function
return highest_priority(self).dimensions
@cached_property
def staggered(self):
if not self._args_diff:
return None
# Use the staggering of the highest priority function
return highest_priority(self).staggered
@cached_property
def root_dimensions(self):
"""Tuple of root Dimensions of the physical space Dimensions."""
return tuple(d.root for d in self.dimensions if d.is_Space)
@property
def indices_ref(self):
"""The reference indices of the object (indices at first creation)."""
if len(self._args_diff) == 1:
return self._args_diff[0].indices_ref
elif len(self._args_diff) == 0:
return DimensionTuple(*self.dimensions, getters=self.dimensions)
return highest_priority(self).indices_ref
@cached_property
def is_TimeDependent(self):
return any(i.is_Time for i in self.dimensions)
@cached_property
def _fd(self):
# Filter out all args with fd order too high
fd_args = []
for f in self._args_diff:
try:
if f.space_order <= self.space_order and \
(not f.is_TimeDependent or f.time_order <= self.time_order):
fd_args.append(f)
except AttributeError:
pass
return dict(ChainMap(*[getattr(i, '_fd', {}) for i in fd_args]))
@cached_property
def _symbolic_functions(self):
return frozenset([i for i in self._functions if i.coefficients == 'symbolic'])
@cached_property
def function(self):
if len(self._functions) == 1:
return set(self._functions).pop()
else:
return None
@cached_property
def _uses_symbolic_coefficients(self):
return bool(self._symbolic_functions)
@cached_property
def coefficients(self):
coefficients = {f.coefficients for f in self._functions}
# If there is multiple ones, we have to revert to the highest priority
# i.e we have to remove symbolic
key = lambda x: coeff_priority.get(x, -1)
return sorted(coefficients, key=key, reverse=True)[0]
def _eval_at(self, func, **kwargs):
return self.func(*[
getattr(a, '_eval_at', lambda x, **kw: a)(func, **kwargs) # noqa: B023
for a in self.args # false positive: lambda is invoked in-place
])
def _subs(self, old, new, **hints):
if old == self:
return new
if old == new:
return self
args = list(self.args)
for i, arg in enumerate(args):
try:
args[i] = arg._subs(old, new, **hints)
except AttributeError:
continue
return self.func(*args, evaluate=False)
@property
def _eval_deriv(self):
return self.func(*[getattr(a, '_eval_deriv', a) for a in self.args])
@property
def _fd_priority(self):
return .75 if self.is_TimeDependent else .5
def __hash__(self):
return super().__hash__()
def __getattr__(self, name):
"""
Try calling a dynamically created FD shortcut.
Notes
-----
This method acts as a fallback for __getattribute__
"""
if name in self._fd:
return self._fd[name][0](self)
raise AttributeError(f"{self.__class__!r} object has no attribute {name!r}")
# Override SymPy arithmetic operators
@call_highest_priority('__radd__')
def __add__(self, other):
return Add(self, other)
@call_highest_priority('__add__')
def __iadd__(self, other):
return Add(self, other)
@call_highest_priority('__add__')
def __radd__(self, other):
return Add(other, self)
@call_highest_priority('__rsub__')
def __sub__(self, other):
return Add(self, -other)
@call_highest_priority('__sub__')
def __isub__(self, other):
return Add(self, -other)
@call_highest_priority('__sub__')
def __rsub__(self, other):
return Add(other, -self)
@call_highest_priority('__rmul__')
def __mul__(self, other):
return Mul(self, other)
@call_highest_priority('__mul__')
def __imul__(self, other):
return Mul(self, other)
@call_highest_priority('__mul__')
def __rmul__(self, other):
return Mul(other, self)
def __pow__(self, other):
return Pow(self, other)
def __rpow__(self, other):
return Pow(other, self)
@call_highest_priority('__rdiv__')
def __div__(self, other):
return Mul(self, Pow(other, sympy.S.NegativeOne))
@call_highest_priority('__div__')
def __rdiv__(self, other):
return Mul(other, Pow(self, sympy.S.NegativeOne))
__truediv__ = __div__
__rtruediv__ = __rdiv__
def __floordiv__(self, other):
from .elementary import floor
return floor(self / other)
def __rfloordiv__(self, other):
from .elementary import floor
return floor(other / self)
def _inv(self, ref, safe=False):
if safe:
return SafeInv(self, ref or self)
else:
return 1 / self
def __mod__(self, other):
return Mod(self, other)
def __rmod__(self, other):
return Mod(other, self)
def __neg__(self):
return Mul(sympy.S.NegativeOne, self)
def __eq__(self, other):
ret = super().__eq__(other)
if ret is NotImplemented or not ret:
# Non comparable or not equal as sympy objects
return False
return all(getattr(self, i, None) == getattr(other, i, None)
for i in self.__rkwargs__)
def _hashable_content(self):
# SymPy computes the hash of all Basic objects as:
# `hash((type(self).__name__,) + self._hashable_content())`
# However, our subclasses will be named after the main SymPy classes,
# for example sympy.Add -> differentiable.Add, so we need to override
# the hashable content to specify it's our own subclasses
return super()._hashable_content() + ('differentiable',)
@property
def name(self):
return "".join(f.name for f in self._functions)
def shift(self, dim, shift):
"""
Shift expression by `shift` along the Dimension `dim`.
For example u.shift(x, x.spacing) = u(x + h_x).
"""
return self._subs(dim, dim + shift)
@property
def laplace(self):
"""
Generates a symbolic expression for the Laplacian, the second
derivative w.r.t all spatial Dimensions.
"""
return self.laplacian()
def laplacian(self, shift=None, order=None, method='FD', side=None, **kwargs):
"""
Laplacian of the Differentiable with shifted derivatives and custom
FD order.
Each second derivative is left-right (i.e D^T D with D the first derivative ):
`(self.dx(x0=dim+shift*dim.spacing,
fd_order=order)).dx(x0=dim-shift*dim.spacing, fd_order=order)`
Parameters
----------
shift: Number, optional, default=None
Shift for the center point of the derivative in number of gridpoints
order: int, optional, default=None
Discretization order for the finite differences.
Uses `func.space_order` when not specified
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
side : Side or tuple of Side, optional, default=centered
Side of the finite difference location, centered (at x), left (at x - 1)
or right (at x + 1).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = kwargs.get('weights', kwargs.get('w'))
order = order or self.space_order
space_dims = self.root_dimensions
shift_x0 = make_shift_x0(shift, (len(space_dims),))
derivs = tuple(f'd{d.name}2' for d in space_dims)
return Add(*[getattr(self, d)(x0=shift_x0(shift, space_dims[i], None, i),
method=method, fd_order=order, side=side, w=w)
for i, d in enumerate(derivs)])
def div(self, shift=None, order=None, method='FD', side=None, **kwargs):
"""
Divergence of the input Function.
Parameters
----------
func : Function or TensorFunction
Function to take the divergence of
shift: Number, optional, default=None
Shift for the center point of the derivative in number of gridpoints
order: int, optional, default=None
Discretization order for the finite differences.
Uses `func.space_order` when not specified
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
side : Side or tuple of Side, optional, default=centered
Side of the finite difference location, centered (at x), left (at x - 1)
or right (at x + 1).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = kwargs.get('weights', kwargs.get('w'))
space_dims = self.root_dimensions
shift_x0 = make_shift_x0(shift, (len(space_dims),))
order = order or self.space_order
return Add(*[getattr(self, f'd{d.name}')(x0=shift_x0(shift, d, None, i),
fd_order=order, method=method, side=side,
w=w)
for i, d in enumerate(space_dims)])
def grad(self, shift=None, order=None, method='FD', side=None, **kwargs):
"""
Gradient of the input Function.
Parameters
----------
func : Function or TensorFunction
Function to take the gradient of
shift: Number, optional, default=None
Shift for the center point of the derivative in number of gridpoints
order: int, optional, default=None
Discretization order for the finite differences.
Uses `func.space_order` when not specified
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
side : Side or tuple of Side, optional, default=centered
Side of the finite difference location, centered (at x), left (at x - 1)
or right (at x + 1).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
from devito.types.tensor import VectorFunction, VectorTimeFunction
space_dims = self.root_dimensions
shift_x0 = make_shift_x0(shift, (len(space_dims),))
order = order or self.space_order
w = kwargs.get('weights', kwargs.get('w'))
comps = [getattr(self, f'd{d.name}')(x0=shift_x0(shift, d, None, i),
fd_order=order, method=method, side=side,
w=w)
for i, d in enumerate(space_dims)]
vec_func = VectorTimeFunction if self.is_TimeDependent else VectorFunction
return vec_func(name=f'grad_{self.name}', time_order=self.time_order,
space_order=self.space_order, components=comps, grid=self.grid)
def biharmonic(self, weight=1):
"""
Generates a symbolic expression for the weighted biharmonic operator w.r.t.
all spatial Dimensions Laplace(weight * Laplace (self))
"""
space_dims = self.root_dimensions
derivs = tuple(f'd{d.name}2' for d in space_dims)
return Add(*[getattr(self.laplace * weight, d) for d in derivs])
def diff(self, *symbols, **assumptions):
"""
Like ``sympy.diff``, but return a ``devito.Derivative`` instead of a
``sympy.Derivative``.
"""
from devito.finite_differences.derivative import Derivative
return Derivative(self, *symbols, **assumptions)
def has(self, *pattern):
"""
Unlike generic SymPy use cases, in Devito the majority of calls to `has`
occur through the finite difference routines passing `sympy.core.symbol.Symbol`
as `pattern`. Since the generic `_has` can be prohibitively expensive,
we here quickly handle this special case, while using the superclass' `has`
as fallback.
"""
for p in pattern:
# Following sympy convention, return True if any is found
if isinstance(p, type) \
and issubclass(p, sympy.Symbol) \
and any(isinstance(i, p) for i in self.free_symbols):
# Symbols (and subclasses) are the leaves of an expression, and they
# are promptly available via `free_symbols`. So this is super quick
return True
return super().has(*pattern)
def has_free(self, *patterns):
"""
Return True if self has object(s) `patterns` as a free expression,
False otherwise.
Notes
-----
This is overridden in SymPy 1.10, but not in previous versions.
"""
try:
return super().has_free(*patterns)
except AttributeError:
return all(i in self.free_symbols for i in patterns)
def highest_priority(diff_op):
if not diff_op._args_diff:
return diff_op
# We want to get the object with highest priority
# We also need to make sure that the object with the largest
# set of dimensions is used when multiple ones with the same
# priority appear
prio = lambda x: (getattr(x, '_fd_priority', 0), len(x.dimensions))
prio_func = sorted(diff_op._args_diff, key=prio, reverse=True)[0]
# The highest priority must be a Function
if not isinstance(prio_func, AbstractFunction):
return highest_priority(prio_func)
return prio_func
class DifferentiableOp(Differentiable):
__sympy_class__ = None
def __new__(cls, *args, **kwargs):
# Do not re-evaluate if any of the args is an EvalDerivative,
# since the integrity of these objects must be preserved
if any(isinstance(i, EvalDerivative) for i in args):
kwargs['evaluate'] = False
obj = cls.__base__.__new__(cls, *args, **kwargs)
# Unfortunately SymPy may build new sympy.core objects (e.g., sympy.Add),
# so here we have to rebuild them as devito.core objects
if kwargs.get('evaluate', True):
obj = diffify(obj)
return obj
def subs(self, *args, **kwargs):
return self.func(
*[getattr(a, 'subs', lambda x: a)(*args, **kwargs) # noqa: B023
for a in self.args], evaluate=False
) # false positive
_subs = Differentiable._subs
@property
def _gather_for_diff(self):
return self
# Bypass useless expensive SymPy _eval_ methods, for which we either already
# know or don't care about the answer, because it'd have ~zero impact on our
# average expressions
def _eval_is_even(self):
return None
def _eval_is_odd(self):
return None
def _eval_is_integer(self):
return None
def _eval_is_negative(self):
return None
def _eval_is_extended_negative(self):
return None
def _eval_is_positive(self):
return None
def _eval_is_extended_positive(self):
return None
def _eval_is_zero(self):
return None
class DifferentiableFunction(DifferentiableOp):
def __new__(cls, *args, **kwargs):
return cls.__sympy_class__.__new__(cls, *args, **kwargs)
@property
def _fd_priority(self):
if highest_priority(self) is self:
return super()._fd_priority
return highest_priority(self)._fd_priority
class Add(DifferentiableOp, sympy.Add):
__sympy_class__ = sympy.Add
def __new__(cls, *args, **kwargs):
# Here, often we get `evaluate=False` to prevent SymPy evaluation (e.g.,
# when `cls==EvalDerivative`), but in all cases we at least apply a small
# set of basic simplifications
# (a+b)+c -> a+b+c (flattening)
# TODO: use symbolics.flatten_args; not using it to avoid a circular import
nested, others = split(args, lambda e: isinstance(e, Add))
args = flatten(e.args for e in nested) + list(others)
# a+0 -> a
args = [i for i in args if i != 0]
# Reorder for homogeneity with pure SymPy types
_addsort(args)
return super().__new__(cls, *args, **kwargs)
class Mul(DifferentiableOp, sympy.Mul):
__sympy_class__ = sympy.Mul
def __new__(cls, *args, **kwargs):
# A Mul, being a DifferentiableOp, may not trigger evaluation upon
# construction (e.g., when an EvalDerivative is present among its
# arguments), so here we apply a small set of basic simplifications
# to avoid generating functional, but ugly, code
# (a*b)*c -> a*b*c (flattening)
# TODO: use symbolics.flatten_args; not using it to avoid a circular import
nested, others = split(args, lambda e: isinstance(e, Mul))
args = flatten(e.args for e in nested) + list(others)
# Gather all numbers and simplify
nums, others = split(args, lambda e: is_number(e))
scalar = sympy.Mul(*nums)
# a*0 -> 0
if scalar == 0:
return sympy.S.Zero
# a*1 -> a
args = others if scalar - 1 == 0 else [scalar] + others
# Reorder for homogeneity with pure SymPy types
_mulsort(args)
# `sympy.Mul.flatten(coeff, Add)` flattens out nested Adds within Add,
# which would destroy `EvalDerivative`s if present. So here we perform
# a similar thing, but cautiously construct an evaluated Add, which
# will preserve the integrity of `EvalDerivative`s, if any
try:
a, b = args
if a.is_Rational:
r, b = b.as_coeff_Mul()
if r is sympy.S.One and type(b) is Add:
return Add(*[_keep_coeff(a, bi) for bi in b.args], evaluate=False)
except (AttributeError, ValueError):
pass
return super().__new__(cls, *args, **kwargs)
@property
def _gather_for_diff(self):
"""
We handle Mul arguments by hand in case of staggered inputs
such as `f(x)*g(x + h_x/2)` that will be transformed into
f(x + h_x/2)*g(x + h_x/2) and priority of indexing is applied
to have single indices as in this example.
The priority is from least to most:
- param
- NODE
- staggered
"""
if len(set(f.staggered for f in self._args_diff)) == 1:
return self
derivs, other = split(self.args, lambda a: isinstance(a, sympy.Derivative))
if len(derivs) == 0:
return self._eval_at(highest_priority(self))
else:
other = self.func(*other)._eval_at(highest_priority(self))
return self.func(other, *derivs)
def _eval_at(self, func, interp_mode='direct', **kwargs):
"""
Evaluate a Mul at the location of `func`.
Two modes:
- `interp_mode='direct'` (default): per-arg evaluation; each factor is
independently evaluated at `func`'s location via
`Differentiable._eval_at`.
- `interp_mode='symmetric'`: when every Differentiable factor has a
staggering different from `func`'s, apply the `I * (a * I^T * b)`
form:
1. Pick a `block` location -- the highest-priority factor's
staggering (NODE is the highest priority, so coefficient-like
NODE factors win, as in the `I * C * I^T` elastic stiffness
pattern). Each factor not at the block is brought there via
`I^T` (an explicit 0-order FD interpolation operator).
Derivatives additionally set `x0` on their own derivative
dimensions to `func`'s indices.
2. The product is formed at `block`'s location.
3. The whole product is interpolated to `func` via `I` (an
explicit 0-order FD operator).
When the trigger does not hold (e.g. some factor already matches
`func`'s staggering), we fall back to `direct`.
"""
if interp_mode != 'symmetric':
return super()._eval_at(func, **kwargs)
diff, other = split(self.args, lambda a: isinstance(a, Differentiable))
# Symmetric form requires every Differentiable factor to differ from
# func; otherwise direct evaluation is cleaner and equivalent.
if len(diff) < 2 or \
any(a.staggered == func.staggered for a in diff):
return super()._eval_at(func, **kwargs)
block_indices = highest_priority(self).indices_ref
# Bring each factor to block's location (I^T where needed)
new_factors = list(other)
for a in diff:
if isinstance(a, sympy.Derivative):
source = post_x0_indices(a, func)
a = a._rebuild(x0={dim: func.indices_ref[dim] for dim in a.dims
if dim in func.indices_ref.getters})
else:
source = a.indices_ref
new_factors.append(interp_at(a, source, block_indices,
self.interp_order))
# Final I from block's location to func
return interp_at(self.func(*new_factors), block_indices,
func.indices_ref, self.interp_order)
class Pow(DifferentiableOp, sympy.Pow):
_fd_priority = 0
__sympy_class__ = sympy.Pow
class Mod(DifferentiableOp, sympy.Mod):
__sympy_class__ = sympy.Mod
class SafeInv(Differentiable, sympy.core.function.Application):
_fd_priority = 0
@property
def base(self):
return self.args[1]
@property
def val(self):
return self.args[0]
@property
def is_commutative(self):
return self.val.is_commutative and self.base.is_commutative
def __str__(self):
return Pow(self.args[0], -1).__str__()
__repr__ = __str__
class ComplexPart(Differentiable, sympy.core.function.Application):
"""Abstract class for `Real`, `Imag`, or `Conj` of an expression"""
_name = None
def __new__(cls, *args, **kwargs):
if len(args) != 1:
raise ValueError(f"{cls.__name__} expects exactly one arg;"
f" {len(args)} were supplied instead.")
return super().__new__(cls, *args, **kwargs)
def __str__(self):
return f"{self.__class__.__name__}({self.args[0]})"
__repr__ = __str__
class RealComplexPart(ComplexPart):
@cached_property
def dtype(self):
dtype = extract_dtype(self)
return dtype(0).real.__class__
class Real(RealComplexPart):
"""Get the real part of an expression"""
_name = 'real'
class Imag(RealComplexPart):
"""Get the imaginary part of an expression"""
_name = 'imag'
class Conj(ComplexPart):
"""Get the complex conjugate of an expression"""
_name = 'conj'
class IndexSum(sympy.Expr, Evaluable):
"""
Represent the summation over a multiindex, that is a collection of
Dimensions, of an indexed expression.
"""
__rargs__ = ('expr', 'dimensions')
is_commutative = True
def __new__(cls, expr, dimensions, **kwargs):
dimensions = as_tuple(dimensions)
if not dimensions:
return expr
for d in dimensions:
try:
if d.is_Dimension and is_integer(d.symbolic_size):
continue
except AttributeError:
pass
raise ValueError("Expected Dimension with numeric size, "
f"got `{d}` instead")
# TODO: `has_free` only available with SymPy v>=1.10
# We should start using `not expr.has_free(*dimensions)` once we drop
# support for SymPy 1.8<=v<1.0
if not all(d in expr.free_symbols for d in dimensions):
raise ValueError(f"All Dimensions `{str(dimensions)}` must appear in `expr` "
"as free variables")
for i in expr.find(IndexSum):
for d in dimensions:
if d in i.dimensions:
raise ValueError(f"Dimension `{d}` already appears in a "
"nested tensor contraction")
obj = sympy.Expr.__new__(cls, expr)
obj._expr = expr
obj._dimensions = dimensions
return obj
def __repr__(self):
return "{}({}, ({}))".format(
self.__class__.__name__,
self.expr,
', '.join(d.name for d in self.dimensions)
)
__str__ = __repr__
def _sympystr(self, printer):
return str(self)
_latex = _sympystr
def _hashable_content(self):
return super()._hashable_content() + (self.dimensions,)
@property
def expr(self):
return self._expr
@property
def dimensions(self):
return self._dimensions
def _evaluate(self, **kwargs):
try:
expr = self.expr._evaluate(**kwargs)
except AttributeError:
# There are rare circumstances in which `self.expr` is a plain
# SymPy object rather than an Evaluable
expr = Evaluable._evaluate_maybe_nested(self.expr, **kwargs)
if not kwargs.get('expand', True):
return self._rebuild(expr)
values = product(*[list(d.range) for d in self.dimensions])
terms = []
for i in values:
mapper = dict(zip(self.dimensions, i, strict=True))
terms.append(expr.xreplace(mapper))
return sum(terms)
@property
def free_symbols(self):
return super().free_symbols - set(self.dimensions)
func = DifferentiableOp._rebuild
class WeightsIndexed(Indexed):
@property
def dimension(self):
return self.function.dimension
class Weights(Array):
"""
The weights (or coefficients) of a finite-difference expansion.
"""
# Use IndexedWeights for the underlying Indexed objects because they
# are guaranteed to appear at the end on an expression's .args.
# This makes it dramatically easier to implement substutions. It also makes
# it easier to visually parse IndexDerivatives when looking at them
_indexed_cls = WeightsIndexed
def __init_finalize__(self, *args, **kwargs):
dimensions = as_tuple(kwargs.get('dimensions'))
weights = kwargs.get('initvalue')
assert len(dimensions) == 1
d = dimensions[0]
assert isinstance(d, StencilDimension) and d.symbolic_size == len(weights)
assert isinstance(weights, (list, tuple, np.ndarray))
# Normalize `weights`
from devito.symbolics import pow_to_mul
weights = tuple(pow_to_mul(sympy.sympify(i)) for i in weights)
kwargs['scope'] = kwargs.get('scope', 'stack')
kwargs['initvalue'] = weights
super().__init_finalize__(*args, **kwargs)
@classmethod
def class_key(cls):
# Ensure Weights appear before any other AbstractFunction
p, v, _ = Array.class_key()
return p, v - 1, cls.__name__
def __eq__(self, other):
return (isinstance(other, Weights) and
self.name == other.name and
self.dimension == other.dimension and
self.indices == other.indices and
self.weights == other.weights)
__hash__ = sympy.Basic.__hash__
def _hashable_content(self):
return (self.name, self.dimension, str(self.weights), self.scope)
@property
def dimension(self):
return self.dimensions[0]
weights = Array.initvalue
def _xreplace(self, rule):
if self in rule:
return rule[self], True
elif not rule:
return self, False
else:
try:
weights, flags = zip(
*[i._xreplace(rule) for i in self.weights], strict=True
)
if any(flags):
return self.func(initvalue=weights, function=None), True
except AttributeError:
# `float` weights
pass
return super()._xreplace(rule)
@cached_property
def _npweights(self):
# NOTE: `self.weights` cannot just be an array or SymPy will fail
# internally at `__eq__` since numpy arrays requite .all, not ==,
# for equality comparison
return np.array(self.weights)
def value(self, idx):
try:
v = self.weights[idx]
except TypeError:
# E.g., `idx` is a tuple
v = self._npweights[idx]
if v.is_Number or v.is_Indexed:
return sympy.sympify(v)
else:
return self[idx]
class IndexDerivative(IndexSum):
__rargs__ = ('expr', 'mapper')
__rkwargs__ = IndexSum.__rkwargs__ + ('deriv_order',)
def __new__(cls, expr, mapper, deriv_order=None, **kwargs):
dimensions = as_tuple(set(mapper.values()))
# Detect the Weights among the arguments
weightss = []
for a in expr.args:
try:
f = a.function
except AttributeError:
continue
if isinstance(f, Weights):