Skip to content

Commit

Permalink
api: cleanup weight kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
mloubout committed Nov 14, 2024
1 parent 0028eb5 commit f668d8e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
6 changes: 2 additions & 4 deletions devito/finite_differences/derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,15 @@ def _process_weights(cls, **kwargs):
else:
return as_tuple(weights)

def __call__(self, x0=None, fd_order=None, side=None, method=None,
weights=None, w=None):
def __call__(self, x0=None, fd_order=None, side=None, method=None, **kwargs):
weights = kwargs.get('weights', kwargs.get('w'))
rkw = {}
if side is not None:
rkw['side'] = side
if method is not None:
rkw['method'] = method
if weights is not None:
rkw['weights'] = weights
if w is not None:
rkw['weights'] = w

if x0 is not None:
x0 = self._process_x0(self.dims, x0=x0)
Expand Down
18 changes: 15 additions & 3 deletions devito/finite_differences/differentiable.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def laplace(self):
"""
return self.laplacian()

def laplacian(self, shift=None, order=None, method='FD', w=None):
def laplacian(self, shift=None, order=None, method='FD', **kwargs):
"""
Laplacian of the Differentiable with shifted derivatives and custom
FD order.
Expand All @@ -309,7 +309,13 @@ def laplacian(self, shift=None, order=None, method='FD', w=None):
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).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite differences.
"""
w = kwargs.get('weights', kwargs.get('w'))
order = order or self.space_order
space_dims = [d for d in self.dimensions if d.is_Space]
shift_x0 = make_shift_x0(shift, (len(space_dims),))
Expand All @@ -318,7 +324,7 @@ def laplacian(self, shift=None, order=None, method='FD', w=None):
method=method, fd_order=order, w=w)
for i, d in enumerate(derivs)])

def div(self, shift=None, order=None, method='FD', w=None):
def div(self, shift=None, order=None, method='FD', **kwargs):
"""
Divergence of the input Function.
Expand All @@ -334,15 +340,18 @@ def div(self, shift=None, order=None, method='FD', w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
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 = [d for d in self.dimensions if d.is_Space]
shift_x0 = make_shift_x0(shift, (len(space_dims),))
order = order or self.space_order
return Add(*[getattr(self, 'd%s' % d.name)(x0=shift_x0(shift, d, None, i),
fd_order=order, method=method, w=w)
for i, d in enumerate(space_dims)])

def grad(self, shift=None, order=None, method='FD', w=None):
def grad(self, shift=None, order=None, method='FD', **kwargs):
"""
Gradient of the input Function.
Expand All @@ -358,11 +367,14 @@ def grad(self, shift=None, order=None, method='FD', w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite
"""
from devito.types.tensor import VectorFunction, VectorTimeFunction
space_dims = [d for d in self.dimensions if d.is_Space]
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, 'd%s' % d.name)(x0=shift_x0(shift, d, None, i),
fd_order=order, method=method, w=w)
for i, d in enumerate(space_dims)]
Expand Down
24 changes: 12 additions & 12 deletions devito/finite_differences/operators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def div(func, shift=None, order=None, method='FD', weights=None, w=None):
def div(func, shift=None, order=None, method='FD', **kwargs):
"""
Divergence of the input Function.
Expand All @@ -14,10 +14,10 @@ def div(func, shift=None, order=None, method='FD', weights=None, w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
weights: list, tupe, or dict, optional, default=None
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = weights or w
w = kwargs.get('weights', kwargs.get('w'))
try:
return func.div(shift=shift, order=order, method=method, w=w)
except AttributeError:
Expand All @@ -41,7 +41,7 @@ def div45(func, shift=None, order=None):
return div(func, shift=shift, order=order, method='RSFD')


def grad(func, shift=None, order=None, method='FD', weights=None, w=None):
def grad(func, shift=None, order=None, method='FD', **kwargs):
"""
Gradient of the input Function.
Expand All @@ -57,10 +57,10 @@ def grad(func, shift=None, order=None, method='FD', weights=None, w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
weights: list, tupe, or dict, optional, default=None
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = weights or w
w = kwargs.get('weights', kwargs.get('w'))
try:
return func.grad(shift=shift, order=order, method=method, w=w)
except AttributeError:
Expand All @@ -84,7 +84,7 @@ def grad45(func, shift=None, order=None):
return grad(func, shift=shift, order=order, method='RSFD')


def curl(func, shift=None, order=None, method='FD', weights=None, w=None):
def curl(func, shift=None, order=None, method='FD', **kwargs):
"""
Curl of the input Function. Only supported for VectorFunction
Expand All @@ -100,10 +100,10 @@ def curl(func, shift=None, order=None, method='FD', weights=None, w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
weights: list, tupe, or dict, optional, default=None
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = weights or w
w = kwargs.get('weights', kwargs.get('w'))
try:
return func.curl(shift=shift, order=order, method=method, w=w)
except AttributeError:
Expand All @@ -128,7 +128,7 @@ def curl45(func, shift=None, order=None):
return curl(func, shift=shift, order=order, method='RSFD')


def laplace(func, shift=None, order=None, method='FD', weights=None, w=None):
def laplace(func, shift=None, order=None, method='FD', **kwargs):
"""
Laplacian of the input Function.
Expand All @@ -143,10 +143,10 @@ def laplace(func, shift=None, order=None, method='FD', weights=None, w=None):
Uses `func.space_order` when not specified
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and 'RSFD'
weights: list, tupe, or dict, optional, default=None
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = weights or w
w = kwargs.get('weights', kwargs.get('w'))
try:
return func.laplacian(shift=shift, order=order, method=method, w=w)
except AttributeError:
Expand Down
38 changes: 31 additions & 7 deletions devito/types/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def values(self):
else:
return super().values()

def div(self, shift=None, order=None, method='FD', w=None):
def div(self, shift=None, order=None, method='FD', **kwargs):
"""
Divergence of the TensorFunction (is a VectorFunction).
Expand All @@ -202,7 +202,10 @@ def div(self, shift=None, order=None, method='FD', w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite differences.
"""
w = kwargs.get('weights', kwargs.get('w'))
comps = []
func = vec_func(self)
ndim = len(self.space_dimensions)
Expand All @@ -222,7 +225,7 @@ def laplace(self):
"""
return self.laplacian()

def laplacian(self, shift=None, order=None, method='FD', w=None):
def laplacian(self, shift=None, order=None, method='FD', **kwargs):
"""
Laplacian of the TensorFunction with shifted derivatives and custom
FD order.
Expand All @@ -238,7 +241,13 @@ def laplacian(self, shift=None, order=None, method='FD', w=None):
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).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite
"""
w = kwargs.get('weights', kwargs.get('w'))
comps = []
func = vec_func(self)
order = order or self.space_order
Expand All @@ -251,7 +260,7 @@ def laplacian(self, shift=None, order=None, method='FD', w=None):
for i, d in enumerate(self.space_dimensions)]))
return func._new(comps)

def grad(self, shift=None, order=None, method=None, w=None):
def grad(self, shift=None, order=None, method=None, **kwargs):
raise AttributeError("Gradient of a second order tensor not supported")

def new_from_mat(self, mat):
Expand Down Expand Up @@ -314,7 +323,7 @@ def __str__(self):

__repr__ = __str__

def div(self, shift=None, order=None, method='FD', w=None):
def div(self, shift=None, order=None, method='FD', **kwargs):
"""
Divergence of the VectorFunction, creates the divergence Function.
Expand All @@ -328,7 +337,10 @@ def div(self, shift=None, order=None, method='FD', w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = kwargs.get('weights', kwargs.get('w'))
shift_x0 = make_shift_x0(shift, (len(self.space_dimensions),))
order = order or self.space_order
return sum([getattr(self[i], 'd%s' % d.name)(x0=shift_x0(shift, d, None, i),
Expand All @@ -342,7 +354,7 @@ def laplace(self):
"""
return self.laplacian()

def laplacian(self, shift=None, order=None, method='FD', w=None):
def laplacian(self, shift=None, order=None, method='FD', **kwargs):
"""
Laplacian of the VectorFunction, creates the Laplacian VectorFunction.
Expand All @@ -353,7 +365,13 @@ def laplacian(self, shift=None, order=None, method='FD', w=None):
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).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite
"""
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
Expand All @@ -363,7 +381,7 @@ def laplacian(self, shift=None, order=None, method='FD', w=None):
for s in self]
return func._new(comps)

def curl(self, shift=None, order=None, method='FD', w=None):
def curl(self, shift=None, order=None, method='FD', **kwargs):
"""
Gradient of the (3D) VectorFunction, creates the curl VectorFunction.
Expand All @@ -377,10 +395,13 @@ def curl(self, shift=None, order=None, method='FD', w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
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
w = kwargs.get('weights', kwargs.get('w'))
dims = self.space_dimensions
derivs = ['d%s' % d.name for d in dims]
shift_x0 = make_shift_x0(shift, (len(dims), len(dims)))
Expand All @@ -400,7 +421,7 @@ def curl(self, shift=None, order=None, method='FD', w=None):
func = vec_func(self)
return func._new(3, 1, [comp1, comp2, comp3])

def grad(self, shift=None, order=None, method='FD', w=None):
def grad(self, shift=None, order=None, method='FD', **kwargs):
"""
Gradient of the VectorFunction, creates the gradient TensorFunction.
Expand All @@ -414,7 +435,10 @@ def grad(self, shift=None, order=None, method='FD', w=None):
method: str, optional, default='FD'
Discretization method. Options are 'FD' (default) and
'RSFD' (rotated staggered grid finite-difference).
weights/w: list, tuple, or dict, optional, default=None
Custom weights for the finite difference coefficients.
"""
w = kwargs.get('weights', kwargs.get('w'))
func = tens_func(self)
ndim = len(self.space_dimensions)
shift_x0 = make_shift_x0(shift, (ndim, ndim))
Expand Down

0 comments on commit f668d8e

Please sign in to comment.