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

Fix fieldsplit with Cofunction right hand side. #3932

Merged
merged 12 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
68 changes: 67 additions & 1 deletion firedrake/formmanipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def argument(self, o):
from ufl import split
from firedrake import MixedFunctionSpace, FunctionSpace
V = o.function_space()

if len(V) == 1:
# Not on a mixed space, just return ourselves.
return o
Expand Down Expand Up @@ -127,6 +128,56 @@ def argument(self, o):
for j in numpy.ndindex(V_is[i].value_shape)]
return self._arg_cache.setdefault(o, as_vector(args))

def cofunction(self, o):
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved
from firedrake import Cofunction, FunctionSpace, MixedFunctionSpace
from pyop2 import MixedDat
from ufl.classes import ZeroBaseForm
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved

V = o.function_space()

# Not on a mixed space, just return ourselves.
if len(V) == 1:
return o

# We only need the test space for Cofunction
indices = self.blocks[0]
V_is = V.subfunctions

try:
indices = tuple(indices)
nidx = len(indices)
except TypeError: # Only one index
indices = (indices, )
nidx = 1
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved

# the cofunction should only be returned on the
# diagonal elements, so if we are off-diagonal
# on a two-form then we return a zero form,
# analogously to the off components of arguments.
if len(self.blocks) == 2:
itest, itrial = self.blocks
on_diag = (itest == itrial)
pbrubeck marked this conversation as resolved.
Show resolved Hide resolved
else:
on_diag = True

# if we are on the diagonal, then return a Cofunction
# in the relevant subspace that points to the data in
# the full space. This means that the right hand side
# of the fieldsplit problem will be correct.
if on_diag:
if nidx == 1:
i = indices[0]
W = V_is[i]
W = FunctionSpace(W.mesh(), W.ufl_element()) # primal space
c = Cofunction(W.dual(), val=o.subfunctions[i].dat)
else:
W = MixedFunctionSpace([V_is[i] for i in indices]) # dual space
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved
c = Cofunction(W, val=MixedDat(o.dat[i] for i in indices))
else:
c = ZeroBaseForm(o.arguments())

return c


SplitForm = collections.namedtuple("SplitForm", ["indices", "form"])

Expand Down Expand Up @@ -160,6 +211,8 @@ def split_form(form, diagonal=False):
compiler will remove these in its more complex simplification
stages.
"""
from firedrake import Cofunction
from ufl import FormSum, Form
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved
splitter = ExtractSubBlock()
args = form.arguments()
shape = tuple(len(a.function_space()) for a in args)
Expand All @@ -168,7 +221,20 @@ def split_form(form, diagonal=False):
assert len(shape) == 2
for idx in numpy.ndindex(shape):
f = splitter.split(form, idx)
if len(f.integrals()) > 0:

# does f actually contain anything?
if isinstance(f, Cofunction):
flen = 1
elif isinstance(f, FormSum):
flen = len(f.components())
elif isinstance(f, Form):
flen = len(f.integrals())
else: # Form
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(
"ExtractSubBlock.split should have returned an instance of "
"either Form, FormSum, or Cofunction")

if flen > 0:
if diagonal:
i, j = idx
if i != j:
Expand Down
10 changes: 8 additions & 2 deletions tests/firedrake/regression/test_matrix_free.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def test_matrixfree_action(a, V, bcs):

@pytest.mark.parametrize("preassembled", [False, True],
ids=["variational", "preassembled"])
@pytest.mark.parametrize("rhs", ["func_rhs", "cofunc_rhs"])
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize("parameters",
[{"ksp_type": "preonly",
"pc_type": "python",
Expand Down Expand Up @@ -168,7 +169,7 @@ def test_matrixfree_action(a, V, bcs):
"fieldsplit_1_fieldsplit_1_pc_type": "python",
"fieldsplit_1_fieldsplit_1_pc_python_type": "firedrake.AssembledPC",
"fieldsplit_1_fieldsplit_1_assembled_pc_type": "lu"}])
def test_fieldsplitting(mesh, preassembled, parameters):
def test_fieldsplitting(mesh, preassembled, parameters, rhs):
V = FunctionSpace(mesh, "CG", 1)
P = FunctionSpace(mesh, "DG", 0)
Q = VectorFunctionSpace(mesh, "DG", 1)
Expand All @@ -184,7 +185,12 @@ def test_fieldsplitting(mesh, preassembled, parameters):

a = inner(u, v)*dx

L = inner(expect, v)*dx
if rhs == 'func_rhs':
L = inner(expect, v)*dx
elif rhs == 'cofunc_rhs':
L = expect.riesz_representation()
else:
raise ValueError("Unknown right hand side type")
JHopeCollins marked this conversation as resolved.
Show resolved Hide resolved

f = Function(W)

Expand Down
Loading