Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion devito/finite_differences/derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Derivative(sympy.Derivative, Differentiable, Pickable):
Derivative order.
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).
or right (at x + 1).
transpose : Transpose, optional, default=direct
Forward (matvec=direct) or transpose (matvec=transpose) mode of the
finite difference.
Expand Down
29 changes: 22 additions & 7 deletions devito/finite_differences/differentiable.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ def shift(self, dim, shift):
return self._subs(dim, dim + shift)

@property
def laplace(self):
def laplace(self, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No laplace is a propery and doesn't take kwargs, laplacian is the one with the kwargs

"""
Generates a symbolic expression for the Laplacian, the second
derivative w.r.t all spatial Dimensions.
"""
return self.laplacian()
return self.laplacian(**kwargs)

def laplacian(self, shift=None, order=None, method='FD', **kwargs):
"""
Expand All @@ -329,16 +329,20 @@ def laplacian(self, shift=None, order=None, method='FD', **kwargs):
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 differences.
Custom weights for the finite difference coefficients.
"""
side = kwargs.get("side")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicitly add to def

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, w=w)
method=method, fd_order=order, side=side, w=w)
for i, d in enumerate(derivs)])

def div(self, shift=None, order=None, method='FD', **kwargs):
Expand All @@ -357,15 +361,20 @@ def div(self, shift=None, order=None, method='FD', **kwargs):
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.
"""
side = kwargs.get("side")
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, w=w)
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', **kwargs):
Expand All @@ -384,16 +393,22 @@ def grad(self, shift=None, order=None, method='FD', **kwargs):
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
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

side = kwargs.get("side")
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, w=w)
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,
Expand Down
24 changes: 20 additions & 4 deletions devito/finite_differences/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ def div(func, shift=None, order=None, method='FD', **kwargs):
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.
"""
side = kwargs.get("side")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add to def

w = kwargs.get('weights', kwargs.get('w'))
try:
return func.div(shift=shift, order=order, method=method, w=w)
return func.div(shift=shift, order=order, method=method, side=side, w=w)
except AttributeError:
return 0

Expand Down Expand Up @@ -57,12 +61,16 @@ def grad(func, shift=None, order=None, method='FD', **kwargs):
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.
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
try:
return func.grad(shift=shift, order=order, method=method, w=w)
return func.grad(shift=shift, order=order, method=method, side=side, w=w)
except AttributeError:
raise AttributeError("Gradient not supported for class %s" % func.__class__)

Expand Down Expand Up @@ -100,12 +108,16 @@ def curl(func, shift=None, order=None, method='FD', **kwargs):
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.
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
try:
return func.curl(shift=shift, order=order, method=method, w=w)
return func.curl(shift=shift, order=order, method=method, side=side, w=w)
except AttributeError:
raise AttributeError("Curl only supported for 3D VectorFunction")

Expand Down Expand Up @@ -143,12 +155,16 @@ def laplace(func, shift=None, order=None, method='FD', **kwargs):
Uses `func.space_order` when not specified
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and 'RSFD'
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.
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
try:
return func.laplacian(shift=shift, order=order, method=method, w=w)
return func.laplacian(shift=shift, order=order, method=method, side=side, w=w)
except AttributeError:
return 0

Expand Down
57 changes: 45 additions & 12 deletions devito/types/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,13 @@ def div(self, shift=None, order=None, method='FD', **kwargs):
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 differences.
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
comps = []
func = vec_func(self)
Expand All @@ -247,7 +251,7 @@ def div(self, shift=None, order=None, method='FD', **kwargs):
for i in range(len(self.space_dimensions)):
comps.append(sum([getattr(self[j, i], 'd%s' % d.name)
(x0=shift_x0(shift, d, i, j), fd_order=order,
method=method, w=w)
method=method, side=side, w=w)
for j, d in enumerate(space_dims)]))
return func._new(comps)

Expand Down Expand Up @@ -277,9 +281,13 @@ def laplacian(self, shift=None, order=None, method='FD', **kwargs):
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
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
comps = []
func = vec_func(self)
Expand All @@ -290,7 +298,7 @@ def laplacian(self, shift=None, order=None, method='FD', **kwargs):
for j in range(ndim):
comps.append(sum([getattr(self[j, i], 'd%s2' % d.name)
(x0=shift_x0(shift, d, j, i), fd_order=order,
method=method, w=w)
method=method, side=side, w=w)
for i, d in enumerate(space_dims)]))
return func._new(comps)

Expand Down Expand Up @@ -372,15 +380,20 @@ def div(self, shift=None, order=None, method='FD', **kwargs):
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.
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
shift_x0 = make_shift_x0(shift, (len(self.space_dimensions),))
order = order or self.space_order
space_dims = self.root_dimensions
return sum([getattr(self[i], 'd%s' % d.name)(x0=shift_x0(shift, d, None, i),
fd_order=order, method=method, w=w)
fd_order=order, method=method,
side=side, w=w)
for i, d in enumerate(space_dims)])

@property
Expand All @@ -404,16 +417,21 @@ def laplacian(self, shift=None, order=None, method='FD', **kwargs):
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
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
func = vec_func(self)
shift_x0 = make_shift_x0(shift, (len(self.space_dimensions),))
order = order or self.space_order
space_dims = self.root_dimensions
comps = [sum([getattr(s, 'd%s2' % d.name)(x0=shift_x0(shift, d, None, i),
fd_order=order, w=w, method=method)
fd_order=order, method=method,
side=side, w=w)
for i, d in enumerate(space_dims)])
for s in self]
return func._new(comps)
Expand All @@ -432,29 +450,39 @@ def curl(self, shift=None, order=None, method='FD', **kwargs):
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.
"""
if len(self.space_dimensions) != 3:
raise AttributeError("Curl only supported for 3D VectorFunction")
# The curl of a VectorFunction is a VectorFunction
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
dims = self.root_dimensions
derivs = ['d%s' % d.name for d in dims]
shift_x0 = make_shift_x0(shift, (len(dims), len(dims)))
order = order or self.space_order
comp1 = (getattr(self[2], derivs[1])(x0=shift_x0(shift, dims[1], 2, 1),
fd_order=order, method=method, w=w) -
fd_order=order, method=method,
side=side, w=w) -
getattr(self[1], derivs[2])(x0=shift_x0(shift, dims[2], 1, 2),
fd_order=order, method=method, w=w))
fd_order=order, method=method,
side=side, w=w))
comp2 = (getattr(self[0], derivs[2])(x0=shift_x0(shift, dims[2], 0, 2),
fd_order=order, method=method, w=w) -
fd_order=order, method=method,
side=side, w=w) -
getattr(self[2], derivs[0])(x0=shift_x0(shift, dims[0], 2, 0),
fd_order=order, method=method, w=w))
fd_order=order, method=method,
side=side, w=w))
comp3 = (getattr(self[1], derivs[0])(x0=shift_x0(shift, dims[0], 1, 0),
fd_order=order, method=method, w=w) -
fd_order=order, method=method,
side=side, w=w) -
getattr(self[0], derivs[1])(x0=shift_x0(shift, dims[1], 0, 1),
fd_order=order, method=method, w=w))
fd_order=order, method=method,
side=side, w=w))
func = vec_func(self)
return func._new(3, 1, [comp1, comp2, comp3])

Expand All @@ -472,17 +500,22 @@ def grad(self, shift=None, order=None, method='FD', **kwargs):
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.
"""
side = kwargs.get("side")
w = kwargs.get('weights', kwargs.get('w'))
func = tens_func(self)
ndim = len(self.space_dimensions)
shift_x0 = make_shift_x0(shift, (ndim, ndim))
order = order or self.space_order
space_dims = self.root_dimensions
comps = [[getattr(f, 'd%s' % d.name)(x0=shift_x0(shift, d, i, j), w=w,
fd_order=order, method=method)
comps = [[getattr(f, 'd%s' % d.name)(x0=shift_x0(shift, d, i, j),
fd_order=order, method=method,
side=side, w=w)
for j, d in enumerate(space_dims)]
for i, f in enumerate(self)]
return func._new(comps)
Expand Down
Loading
Loading