Skip to content
Merged
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
35 changes: 27 additions & 8 deletions firedrake/cofunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,33 @@ def __imul__(self, expr):
return self
return NotImplemented

def interpolate(self, expression):
r"""Interpolate an expression onto this :class:`Cofunction`.

:param expression: a UFL expression to interpolate
:returns: this :class:`firedrake.cofunction.Cofunction` object"""
from firedrake import interpolation
interp = interpolation.Interpolate(ufl_expr.Argument(self.function_space().dual(), 0), expression)
return firedrake.assemble(interp, tensor=self)
@PETSc.Log.EventDecorator()
def interpolate(self,
expression: ufl.BaseForm,
ad_block_tag: str | None = None,
**kwargs):
"""Interpolate a dual expression onto this :class:`Cofunction`.

Parameters
----------
expression
A dual UFL expression to interpolate.
ad_block_tag
An optional string for tagging the resulting assemble
block on the Pyadjoint tape.
**kwargs
Any extra kwargs are passed on to the interpolate function.
For details see `firedrake.interpolation.interpolate`.

Returns
-------
firedrake.cofunction.Cofunction
Returns `self`
"""
from firedrake import interpolation, assemble
v, = self.arguments()
interp = interpolation.Interpolate(v, expression, **kwargs)
return assemble(interp, tensor=self, ad_block_tag=ad_block_tag)

@property
def cell_set(self):
Expand Down
58 changes: 23 additions & 35 deletions firedrake/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,43 +360,31 @@ def function_space(self):
return self._function_space

@PETSc.Log.EventDecorator()
def interpolate(
self,
expression,
subset=None,
allow_missing_dofs=False,
default_missing_val=None,
ad_block_tag=None
):
r"""Interpolate an expression onto this :class:`Function`.

:param expression: a UFL expression to interpolate
: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`.
:kwarg allow_missing_dofs: For interpolation across meshes: allow
degrees of freedom (aka DoFs/nodes) in the target mesh that cannot be
defined on the source mesh. For example, where nodes are point
evaluations, points in the target mesh that are not in the source mesh.
When ``False`` this raises a ``ValueError`` should this occur. When
``True`` the corresponding values are set to zero or to the value
``default_missing_val`` if given. Ignored if interpolating within the
same mesh or onto a :func:`.VertexOnlyMesh` (the behaviour of a
:func:`.VertexOnlyMesh` in this scenario is, at present, set when
it is created).
:kwarg default_missing_val: 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 zero is used. 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: this :class:`Function` object"""
def interpolate(self,
expression: ufl.classes.Expr,
ad_block_tag: str | None = None,
**kwargs):
"""Interpolate an expression onto this :class:`Function`.

Parameters
----------
expression
A UFL expression to interpolate.
ad_block_tag
An optional string for tagging the resulting assemble
block on the Pyadjoint tape.
**kwargs
Any extra kwargs are passed on to the interpolate function.
For details see `firedrake.interpolation.interpolate`.

Returns
-------
firedrake.function.Function
Returns `self`
"""
from firedrake import interpolation, assemble
V = self.function_space()
interp = interpolation.Interpolate(expression, V,
subset=subset,
allow_missing_dofs=allow_missing_dofs,
default_missing_val=default_missing_val)
interp = interpolation.Interpolate(expression, V, **kwargs)
return assemble(interp, tensor=self, ad_block_tag=ad_block_tag)

def zero(self, subset=None):
Expand Down