/
operators.py
74 lines (59 loc) · 1.62 KB
/
operators.py
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
def div(func):
"""
Divergence of the input Function.
Parameters
----------
func : Function or TensorFunction
"""
try:
return func.div
except AttributeError:
return 0
def grad(func):
"""
Gradient of the input Function.
Parameters
----------
func : Function or VectorFunction
"""
try:
return func.grad
except AttributeError:
raise AttributeError("Gradient not supported for class %s" % func.__class__)
def curl(func):
"""
Curl of the input Function.
Parameters
----------
func : VectorFunction
"""
try:
return func.curl
except AttributeError:
raise AttributeError("Curl only supported for 3D VectorFunction")
def laplace(func):
"""
Laplacian of the input Function.
Parameters
----------
func : Function or TensorFunction
"""
try:
return func.laplace
except AttributeError:
return 0
def diag(func, size=None):
"""
Creates a diagonal tensor with func on its diagonal.
Parameters
----------
func : Differentiable or scalar
"""
dim = size or len(func.dimensions)
dim = dim-1 if func.is_TimeDependent else dim
to = getattr(func, 'time_order', 0)
from devito.types.tensor import TensorFunction, TensorTimeFunction
tens_func = TensorTimeFunction if func.is_TimeDependent else TensorFunction
comps = [[func if i == j else 0 for i in range(dim)] for j in range(dim)]
return tens_func(name='diag', grid=func.grid, space_order=func.space_order,
components=comps, time_order=to, diagonal=True)