Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dsl: Overhaul custom fd weights #1644

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion devito/finite_differences/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
from .finite_difference import * # noqa
from .derivative import * # noqa
from .tools import generate_fd_shortcuts # noqa
from .coefficients import * # noqa
from .operators import * # noqa
291 changes: 0 additions & 291 deletions devito/finite_differences/coefficients.py

This file was deleted.

26 changes: 18 additions & 8 deletions devito/finite_differences/derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Derivative(sympy.Derivative, Differentiable):
x0 : dict, optional
Origin (where the finite-difference is evaluated at) for the finite-difference
scheme, e.g. {x: x, y: y + h_y/2}.
coeffs : iterable or Function, optional
Set of custom finite-difference weights.

Examples
--------
Expand Down Expand Up @@ -85,7 +87,7 @@ class Derivative(sympy.Derivative, Differentiable):
evaluation are `x0`, `fd_order` and `side`.
"""

_state = ('expr', 'dims', 'side', 'fd_order', 'transpose', '_ppsubs', 'x0')
_state = ('expr', 'dims', 'side', 'fd_order', 'transpose', '_ppsubs', 'x0', 'coeffs')
_fd_priority = 3

def __new__(cls, expr, *dims, **kwargs):
Expand All @@ -108,6 +110,7 @@ def __new__(cls, expr, *dims, **kwargs):
obj._transpose = kwargs.get("transpose", direct)
obj._ppsubs = as_tuple(frozendict(i) for i in kwargs.get("subs", []))
obj._x0 = frozendict(kwargs.get('x0', {}))
obj._coeffs = kwargs.get('coeffs', None)
return obj

@classmethod
Expand Down Expand Up @@ -172,18 +175,21 @@ def _process_kwargs(cls, expr, *dims, **kwargs):
for s in filter_ordered(new_dims)]
return new_dims, orders, fd_orders, variable_count

def __call__(self, x0=None, fd_order=None, side=None):
def __call__(self, x0=None, coeffs=None, fd_order=None, side=None):
if self.ndims == 1:
_fd_order = fd_order or self._fd_order
_side = side or self._side
new_x0 = {self.dims[0]: x0} if x0 is not None else self.x0
return self._new_from_self(fd_order=_fd_order, side=_side, x0=new_x0)
new_coeffs = coeffs
Copy link
Contributor

Choose a reason for hiding this comment

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

coeffs or self._coeffs ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need to check - something wasn't working last night so wrote it in this way for now but quite a few things in there need checking and fixing still.

return self._new_from_self(fd_order=_fd_order, side=_side, x0=new_x0,
coeffs=new_coeffs)

if side is not None:
raise TypeError("Side only supported for first order single"
"Dimension derivative such as `.dxl` or .dx(side=left)")
# Cross derivative
_x0 = dict(self._x0)
_coeffs = coeffs
_fd_order = dict(self.fd_order._getters)
try:
_fd_order.update(**(fd_order or {}))
Expand All @@ -193,13 +199,13 @@ def __call__(self, x0=None, fd_order=None, side=None):
except AttributeError:
raise TypeError("Multi-dimensional Derivative, input expected as a dict")

return self._new_from_self(fd_order=_fd_order, x0=_x0)
return self._new_from_self(fd_order=_fd_order, x0=_x0, coeffs=_coeffs)

def _new_from_self(self, **kwargs):
expr = kwargs.pop('expr', self.expr)
_kwargs = {'deriv_order': self.deriv_order, 'fd_order': self.fd_order,
'side': self.side, 'transpose': self.transpose, 'subs': self._ppsubs,
'x0': self.x0, 'preprocessed': True}
'x0': self.x0, 'coeffs': self.coeffs, 'preprocessed': True}
_kwargs.update(**kwargs)
return Derivative(expr, *self.dims, **_kwargs)

Expand Down Expand Up @@ -254,6 +260,10 @@ def fd_order(self):
def deriv_order(self):
return self._deriv_order

@property
def coeffs(self):
return self._coeffs

@property
def side(self):
return self._side
Expand Down Expand Up @@ -341,13 +351,13 @@ def _eval_fd(self, expr):
if self.side is not None and self.deriv_order == 1:
res = first_derivative(expr, self.dims[0], self.fd_order,
side=self.side, matvec=self.transpose,
x0=self.x0)
x0=self.x0, coeffs=self.coeffs)
elif len(self.dims) > 1:
res = cross_derivative(expr, self.dims, self.fd_order, self.deriv_order,
matvec=self.transpose, x0=self.x0)
matvec=self.transpose, x0=self.x0, coeffs=self.coeffs)
else:
res = generic_derivative(expr, *self.dims, self.fd_order, self.deriv_order,
matvec=self.transpose, x0=self.x0)
matvec=self.transpose, x0=self.x0, coeffs=self.coeffs)

# Step 3: Evaluate remaining part of expression
res = res.evaluate
Expand Down
Loading