From f668d8eaccd16295806a338c62d43a9620fa38e7 Mon Sep 17 00:00:00 2001 From: mloubout Date: Wed, 13 Nov 2024 15:13:23 -0500 Subject: [PATCH] api: cleanup weight kwargs --- devito/finite_differences/derivative.py | 6 ++-- devito/finite_differences/differentiable.py | 18 ++++++++-- devito/finite_differences/operators.py | 24 ++++++------- devito/types/tensor.py | 38 +++++++++++++++++---- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/devito/finite_differences/derivative.py b/devito/finite_differences/derivative.py index 6a3fdf42e7..1019ba8672 100644 --- a/devito/finite_differences/derivative.py +++ b/devito/finite_differences/derivative.py @@ -218,8 +218,8 @@ 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 @@ -227,8 +227,6 @@ def __call__(self, x0=None, fd_order=None, side=None, method=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) diff --git a/devito/finite_differences/differentiable.py b/devito/finite_differences/differentiable.py index 63e44370e1..f8b2faddd0 100644 --- a/devito/finite_differences/differentiable.py +++ b/devito/finite_differences/differentiable.py @@ -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. @@ -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),)) @@ -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. @@ -334,7 +340,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')) 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 @@ -342,7 +351,7 @@ def div(self, shift=None, order=None, method='FD', w=None): 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. @@ -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)] diff --git a/devito/finite_differences/operators.py b/devito/finite_differences/operators.py index 29c9779a22..c19a9cdfce 100644 --- a/devito/finite_differences/operators.py +++ b/devito/finite_differences/operators.py @@ -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. @@ -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: @@ -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. @@ -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: @@ -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 @@ -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: @@ -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. @@ -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: diff --git a/devito/types/tensor.py b/devito/types/tensor.py index 0b87b185c0..e7a37897c7 100644 --- a/devito/types/tensor.py +++ b/devito/types/tensor.py @@ -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). @@ -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) @@ -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. @@ -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 @@ -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): @@ -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. @@ -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), @@ -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. @@ -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 @@ -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. @@ -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))) @@ -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. @@ -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))