-
Notifications
You must be signed in to change notification settings - Fork 245
api: Extend side API to all derivative operator shortcuts #2811
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
Open
EdCaunt
wants to merge
2
commits into
main
Choose a base branch
from
grad-kwargs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,12 +303,12 @@ def shift(self, dim, shift): | |
| return self._subs(dim, dim + shift) | ||
|
|
||
| @property | ||
| def laplace(self): | ||
| def laplace(self, **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): | ||
| """ | ||
|
|
@@ -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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
|
@@ -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): | ||
|
|
@@ -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, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
|
|
@@ -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__) | ||
|
|
||
|
|
@@ -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") | ||
|
|
||
|
|
@@ -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 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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