-
Notifications
You must be signed in to change notification settings - Fork 166
Move __future__
interpolation into interpolation.py
#4346
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5f34b01
replace `Interpolator.__new__`
leo-collins 629259d
change `Interpolator._interpolate_future` to `Interpolator.interpolate`
leo-collins c52b530
replace `interpolate` function
leo-collins 3399e32
update `interpolate` docstring
leo-collins 782f737
remove `firedrake.__future__` imports
leo-collins 52a9399
delete contents of `__future__`
leo-collins 47cea69
remove instruction to import `__future__.interpolate` from interpolat…
leo-collins cfffebb
update `Interpolator.interpolate` docstring
leo-collins 9ee45b2
update benny_luke demo
leo-collins 3d189a0
update full_waveform_inversion demo
leo-collins f440237
fixed typo
leo-collins bb32905
fix
leo-collins 0278146
wrap `interpolate` inside `__future__`
leo-collins 182010c
fix `__new__` method of `Interpolator`
leo-collins ac81b56
add warning to `__future__.interpolate`
leo-collins 42202d5
add deprecation warning to `__future__.interpolate`
leo-collins 5372efa
wrap `Interpolate` class in `__future__` with deprecation warning
leo-collins 32aa021
update warning message
leo-collins 87b0174
change warning text
leo-collins 70169a4
fix notebooks
leo-collins 89565e7
lint
leo-collins 74686c8
use `warnings.warn` instead of `warnings.deprecated`
leo-collins a48fd7c
lint
leo-collins f4b5280
fix import in `hypre_ams.py`
leo-collins 4ab4347
more import fixes
leo-collins d7b8b2f
add clarification on adjoint interpolation
leo-collins 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
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
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
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
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 |
---|---|---|
@@ -1,46 +1,16 @@ | ||
from ufl.domain import as_domain, extract_unique_domain | ||
from ufl.algorithms import extract_arguments | ||
from firedrake.mesh import MeshTopology, VertexOnlyMeshTopology | ||
from firedrake.interpolation import (interpolate as interpolate_old, | ||
Interpolator as InterpolatorOld, | ||
SameMeshInterpolator as SameMeshInterpolatorOld, | ||
CrossMeshInterpolator as CrossMeshInterpolatorOld) | ||
from firedrake.cofunction import Cofunction | ||
from functools import wraps | ||
from firedrake.interpolation import interpolate as interpolate_default, Interpolator as Interpolator_default | ||
import warnings | ||
|
||
|
||
__all__ = ("interpolate", "Interpolator") | ||
|
||
|
||
class Interpolator(InterpolatorOld): | ||
def __new__(cls, expr, V, **kwargs): | ||
target_mesh = as_domain(V) | ||
source_mesh = extract_unique_domain(expr) or target_mesh | ||
if target_mesh is source_mesh or all(isinstance(m.topology, MeshTopology) for m in [target_mesh, source_mesh]) and target_mesh.submesh_ancesters[-1] is source_mesh.submesh_ancesters[-1]: | ||
return object.__new__(SameMeshInterpolator) | ||
else: | ||
if isinstance(target_mesh.topology, VertexOnlyMeshTopology): | ||
return object.__new__(SameMeshInterpolator) | ||
else: | ||
return object.__new__(CrossMeshInterpolator) | ||
|
||
interpolate = InterpolatorOld._interpolate_future | ||
|
||
|
||
class SameMeshInterpolator(Interpolator, SameMeshInterpolatorOld): | ||
pass | ||
|
||
|
||
class CrossMeshInterpolator(Interpolator, CrossMeshInterpolatorOld): | ||
pass | ||
def interpolate(expr, V, *args, **kwargs): | ||
warnings.warn("""The symbolic `interpolate` has been moved into `firedrake` | ||
and is now the default method for interpolating in Firedrake. The need to | ||
`from firedrake.__future__ import interpolate` is now deprecated.""", FutureWarning) | ||
return interpolate_default(expr, V, *args, **kwargs) | ||
|
||
|
||
@wraps(interpolate_old) | ||
def interpolate(expr, V, *args, **kwargs): | ||
default_missing_val = kwargs.pop("default_missing_val", None) | ||
if isinstance(V, Cofunction): | ||
adjoint = bool(extract_arguments(expr)) | ||
return Interpolator( | ||
expr, V.function_space().dual(), *args, **kwargs | ||
).interpolate(V, adjoint=adjoint, default_missing_val=default_missing_val) | ||
return Interpolator(expr, V, *args, **kwargs).interpolate(default_missing_val=default_missing_val) | ||
class Interpolator(Interpolator_default): | ||
def __new__(cls, *args, **kwargs): | ||
warnings.warn("""The symbolic `Interpolator` has been moved into `firedrake`. | ||
The need to `from firedrake.__future__ import Interpolator` is now deprecated.""", FutureWarning) | ||
return Interpolator_default(*args, **kwargs) |
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 |
---|---|---|
|
@@ -24,9 +24,10 @@ | |
import firedrake | ||
from firedrake import tsfc_interface, utils, functionspaceimpl | ||
from firedrake.ufl_expr import Argument, action, adjoint as expr_adjoint | ||
from firedrake.mesh import MissingPointsBehaviour, VertexOnlyMeshMissingPointsError | ||
from firedrake.mesh import MissingPointsBehaviour, VertexOnlyMeshMissingPointsError, VertexOnlyMeshTopology | ||
from firedrake.petsc import PETSc | ||
from firedrake.halo import _get_mtype as get_dat_mpi_type | ||
from firedrake.cofunction import Cofunction | ||
from mpi4py import MPI | ||
|
||
from pyadjoint import stop_annotating, no_annotations | ||
|
@@ -142,20 +143,13 @@ def _ufl_expr_reconstruct_(self, expr, v=None, **interp_data): | |
|
||
|
||
@PETSc.Log.EventDecorator() | ||
def interpolate( | ||
expr, | ||
V, | ||
subset=None, | ||
access=op2.WRITE, | ||
allow_missing_dofs=False, | ||
default_missing_val=None, | ||
ad_block_tag=None | ||
): | ||
"""Interpolate an expression onto a new function in V. | ||
def interpolate(expr, V, *args, **kwargs): | ||
"""Returns a UFL expression for the interpolation operation of ``expr`` into ``V``. | ||
|
||
:arg expr: a UFL expression. | ||
:arg V: the :class:`.FunctionSpace` to interpolate into (or else | ||
an existing :class:`.Function`). | ||
an existing :class:`.Function` or :class:`.Cofunction`). | ||
Adjoint interpolation requires ``V`` to be a :class:`.Cofunction`. | ||
:kwarg subset: An optional :class:`pyop2.types.set.Subset` to apply the | ||
interpolation over. Cannot, at present, be used when interpolating | ||
across meshes unless the target mesh is a :func:`.VertexOnlyMesh`. | ||
|
@@ -182,8 +176,7 @@ def interpolate( | |
to zero. Ignored if interpolating within the same mesh or onto a | ||
:func:`.VertexOnlyMesh`. | ||
:kwarg ad_block_tag: An optional string for tagging the resulting assemble block on the Pyadjoint tape. | ||
:returns: a new :class:`.Function` in the space ``V`` (or ``V`` if | ||
it was a Function). | ||
:returns: A synbolic :class:`.Interpolate` object | ||
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. Typo |
||
|
||
.. note:: | ||
|
||
|
@@ -204,9 +197,13 @@ def interpolate( | |
performance by using an :class:`Interpolator` instead. | ||
|
||
""" | ||
return Interpolator( | ||
expr, V, subset=subset, access=access, allow_missing_dofs=allow_missing_dofs | ||
).interpolate(default_missing_val=default_missing_val, ad_block_tag=ad_block_tag) | ||
default_missing_val = kwargs.pop("default_missing_val", None) | ||
if isinstance(V, Cofunction): | ||
adjoint = bool(extract_arguments(expr)) | ||
return Interpolator( | ||
expr, V.function_space().dual(), *args, **kwargs | ||
).interpolate(V, adjoint=adjoint, default_missing_val=default_missing_val) | ||
return Interpolator(expr, V, *args, **kwargs).interpolate(default_missing_val=default_missing_val) | ||
|
||
|
||
class Interpolator(abc.ABC): | ||
|
@@ -264,7 +261,7 @@ def __new__(cls, expr, V, **kwargs): | |
if target_mesh is source_mesh or submesh_interp_implemented: | ||
return object.__new__(SameMeshInterpolator) | ||
else: | ||
if isinstance(target_mesh.topology, firedrake.mesh.VertexOnlyMeshTopology): | ||
if isinstance(target_mesh.topology, VertexOnlyMeshTopology): | ||
return object.__new__(SameMeshInterpolator) | ||
else: | ||
return object.__new__(CrossMeshInterpolator) | ||
|
@@ -296,7 +293,7 @@ def __init__( | |
expr = replace(expr, {v: v.reconstruct(number=1)}) | ||
self.expr_renumbered = expr | ||
|
||
def _interpolate_future(self, *function, transpose=None, adjoint=False, default_missing_val=None): | ||
def interpolate(self, *function, transpose=None, adjoint=False, default_missing_val=None): | ||
"""Define the :class:`Interpolate` object corresponding to the interpolation operation of interest. | ||
|
||
Parameters | ||
|
@@ -322,11 +319,6 @@ def _interpolate_future(self, *function, transpose=None, adjoint=False, default_ | |
------- | ||
firedrake.interpolation.Interpolate or ufl.action.Action or ufl.adjoint.Adjoint | ||
The symbolic object representing the interpolation operation. | ||
|
||
Notes | ||
----- | ||
This method is the default future behaviour of interpolation. In a future release, the | ||
``Interpolator.interpolate`` method will be replaced by this method. | ||
""" | ||
|
||
V = self.V | ||
|
@@ -351,79 +343,6 @@ def _interpolate_future(self, *function, transpose=None, adjoint=False, default_ | |
# Return the `ufl.Interpolate` object | ||
return interp | ||
|
||
@PETSc.Log.EventDecorator() | ||
def interpolate(self, *function, output=None, transpose=None, adjoint=False, default_missing_val=None, | ||
ad_block_tag=None): | ||
"""Compute the interpolation by assembling the appropriate :class:`Interpolate` object. | ||
|
||
Parameters | ||
---------- | ||
*function: firedrake.function.Function or firedrake.cofunction.Cofunction | ||
If the expression being interpolated contains an argument, | ||
then the function value to interpolate. | ||
output: firedrake.function.Function or firedrake.cofunction.Cofunction | ||
A function to contain the output. | ||
transpose : bool | ||
Deprecated, use adjoint instead. | ||
adjoint: bool | ||
Set to true to apply the adjoint of the interpolation | ||
operator. | ||
default_missing_val: bool | ||
For interpolation across meshes: the | ||
optional value to assign to DoFs in the target mesh that are | ||
outside the source mesh. If this is not set then the values are | ||
either (a) unchanged if some ``output`` is specified to the | ||
:meth:`interpolate` method or (b) set to zero. This does not affect | ||
adjoint interpolation. Ignored if interpolating within the same | ||
mesh or onto a :func:`.VertexOnlyMesh`. | ||
ad_block_tag: str | ||
An optional string for tagging the resulting assemble block on the Pyadjoint tape. | ||
|
||
Returns | ||
------- | ||
firedrake.function.Function or firedrake.cofunction.Cofunction | ||
The resulting interpolated function. | ||
""" | ||
from firedrake.assemble import assemble | ||
|
||
warnings.warn("""The use of `interpolate` to perform the numerical interpolation is deprecated. | ||
This feature will be removed very shortly. | ||
|
||
Instead, import `interpolate` from the `firedrake.__future__` module to update | ||
the interpolation's behaviour to return the symbolic `ufl.Interpolate` object associated | ||
with this interpolation. | ||
|
||
You can then assemble the resulting object to get the interpolated quantity | ||
of interest. For example, | ||
|
||
``` | ||
from firedrake.__future__ import interpolate | ||
... | ||
|
||
assemble(interpolate(expr, V)) | ||
``` | ||
|
||
Alternatively, you can also perform other symbolic operations on the interpolation operator, such as taking | ||
the derivative, and then assemble the resulting form. | ||
""", FutureWarning) | ||
if transpose is not None: | ||
warnings.warn("'transpose' argument is deprecated, use 'adjoint' instead", FutureWarning) | ||
adjoint = transpose or adjoint | ||
|
||
# Get the Interpolate object | ||
interp = self._interpolate_future(*function, adjoint=adjoint, | ||
default_missing_val=default_missing_val) | ||
|
||
if isinstance(self.V, firedrake.Function) and not output: | ||
# V can be the Function to interpolate into (e.g. see `Function.interpolate``). | ||
output = self.V | ||
|
||
# Assemble the `ufl.Interpolate` object, which will then call `Interpolator._interpolate` | ||
# to perform the interpolation. Having this structure ensures consistency between | ||
# `Interpolator` and `Interp`. This mechanism handles annotation since performing interpolation will drop an | ||
# `AssembleBlock` on the tape. | ||
return assemble(interp, tensor=output, ad_block_tag=ad_block_tag) | ||
|
||
@abc.abstractmethod | ||
def _interpolate(self, *args, **kwargs): | ||
""" | ||
|
@@ -715,10 +634,10 @@ def _interpolate( | |
# so the sub_interpolators are already prepared to interpolate | ||
# without needing to be given a Function | ||
assert not self.nargs | ||
interp = sub_interpolator._interpolate_future(adjoint=adjoint, **kwargs) | ||
interp = sub_interpolator.interpolate(adjoint=adjoint, **kwargs) | ||
assemble(interp, tensor=output_sub_func) | ||
else: | ||
interp = sub_interpolator._interpolate_future(adjoint=adjoint, **kwargs) | ||
interp = sub_interpolator.interpolate(adjoint=adjoint, **kwargs) | ||
assemble(action(interp, f_src_sub_func), tensor=output_sub_func) | ||
return output | ||
|
||
|
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.
Uh oh!
There was an error while loading. Please reload this page.