Skip to content

Commit

Permalink
Add a test that would have failed before
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrubeck committed Feb 19, 2025
1 parent 3af144b commit 926db4e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
15 changes: 15 additions & 0 deletions test/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Adjoint,
Argument,
Coefficient,
Cofunction,
FunctionSpace,
Mesh,
TestFunction,
Expand Down Expand Up @@ -69,6 +70,20 @@ def test_symbolic(V1, V2):
assert Iu.ufl_operands == (u,)


def test_symbolic_adjoint(V1, V2):
# Set dual of V2
V2_dual = V2.dual()

u = Argument(V1, 1)
vstar = Cofunction(V2_dual)
Iu = Interpolate(u, vstar)

assert Iu.ufl_function_space() == V2_dual
assert Iu.argument_slots() == (vstar, u)
assert Iu.arguments() == (u,)
assert Iu.ufl_operands == (u,)


def test_action_adjoint(V1, V2):
# Set dual of V2
V2_dual = V2.dual()
Expand Down
2 changes: 1 addition & 1 deletion ufl/core/base_form_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def ufl_function_space(self):
arg, *_ = self.argument_slots()
if not isinstance(arg, BaseCoefficient) and isinstance(arg, (BaseForm, Coargument)):
arg, *_ = arg.arguments()
return arg._ufl_function_space
return arg.ufl_function_space()

def _ufl_expr_reconstruct_(
self, *operands, function_space=None, derivatives=None, argument_slots=None
Expand Down
17 changes: 6 additions & 11 deletions ufl/core/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
#
# Modified by Nacime Bouziani, 2021-2022

from ufl.action import Action
from ufl.argument import Argument, Coargument
from ufl.coefficient import Cofunction
from ufl.constantvalue import as_ufl
from ufl.core.base_form_operator import BaseFormOperator
from ufl.core.ufl_type import ufl_type
from ufl.duals import is_dual
from ufl.form import BaseForm, Form
from ufl.form import BaseForm
from ufl.functionspace import AbstractFunctionSpace


Expand All @@ -35,16 +33,15 @@ def __init__(self, expr, v):
v: the FunctionSpace to interpolate into or the Coargument
defined on the dual of the FunctionSpace to interpolate into.
"""
# This check could be more rigorous.
dual_args = (Coargument, Cofunction, Form, Action, BaseFormOperator)
dual_args = (Coargument, BaseForm)

if isinstance(v, AbstractFunctionSpace):
if is_dual(v):
raise ValueError("Expecting a primal function space.")
v = Argument(v.dual(), 0)
elif not isinstance(v, dual_args):
raise ValueError(
"Expecting the second argument to be FunctionSpace, FiniteElement or dual."
"Expecting the second argument to be FunctionSpace, Coargument, or BaseForm."
)

expr = as_ufl(expr)
Expand All @@ -54,11 +51,9 @@ def __init__(self, expr, v):
# Reversed order convention
argument_slots = (v, expr)
# Get the primal space (V** = V)
if isinstance(v, BaseForm):
arg, *_ = v.arguments()
function_space = arg.ufl_function_space()
else:
function_space = v.ufl_function_space().dual()
arg, *_ = v.arguments()
function_space = arg.ufl_function_space()

# Set the operand as `expr` for DAG traversal purpose.
operand = expr
BaseFormOperator.__init__(
Expand Down

0 comments on commit 926db4e

Please sign in to comment.