ArityMismatch with complex PETSc and MixedFunctionSpace

Hi,
I am unable to compile forms on mixed function spaces when using complex numbers. The following minimal working example throws no error when running it with the real numbers build of PETSc:

from mpi4py import MPI
from dolfinx import mesh, io, fem
import ufl
import gmsh

comm = MPI.COMM_WORLD

if comm.rank == 0:
    gmsh.initialize()
    air = gmsh.model.occ.add_sphere(0,0,0,0.1)
    gmsh.model.occ.synchronize()
    gmsh.model.mesh.generate(3)
    fluid_tag = gmsh.model.add_physical_group(3,[air])
    tags = [fluid_tag]
else:
    tags = None
fluid_tag, = comm.bcast(tags)

# import from gmsh, define domain and subdomains
partitioner = mesh.create_cell_partitioner(mesh.GhostMode.shared_facet,None)
mesh_data = io.gmsh.model_to_mesh(gmsh.model, comm, 0, partitioner=partitioner)
domain = mesh_data.mesh
ct = mesh_data.cell_tags

tdim = domain.topology.dim
fdim = tdim - 1

# make subdomain(s)
domain_f, entity_map_f, vertex_map_f, _ = mesh.create_submesh(domain, tdim, ct.find(fluid_tag))
ct_f = mesh.transfer_meshtags_to_submesh(ct,domain_f,vertex_map_f,entity_map_f)

# define functionspace(s)
Q = fem.functionspace(domain_f, ('Lagrange', 2))
W = ufl.MixedFunctionSpace(Q)

p, = ufl.TestFunctions(W)
dp, = ufl.TrialFunctions(W)

dx = ufl.Measure("dx",domain,subdomain_data=ct)

# make form(s)
k_f = ufl.inner(ufl.grad(p), ufl.grad(dp)) * dx(fluid_tag)

k_block = ufl.extract_blocks(k_f) 
k = fem.form(k_block,entity_maps=[entity_map_f,])

However when using complex numbers, the following error is raised:

Traceback (most recent call last):
  File "MWE.py", line 45, in <module>
    k = fem.form(k_block,entity_maps=[entity_map_f,])
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/fem/forms.py", line 479, in form
    return _create_form(form)
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/fem/forms.py", line 475, in _create_form
    return list(map(lambda sub_form: _create_form(sub_form), form))
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/fem/forms.py", line 475, in <lambda>
    return list(map(lambda sub_form: _create_form(sub_form), form))
                                     ~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/fem/forms.py", line 475, in _create_form
    return list(map(lambda sub_form: _create_form(sub_form), form))
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/fem/forms.py", line 475, in <lambda>
    return list(map(lambda sub_form: _create_form(sub_form), form))
                                     ~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/fem/forms.py", line 471, in _create_form
    return _form(form)
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/fem/forms.py", line 387, in _form
    ufcx_form, module, code = jit.ffcx_jit(
                              ~~~~~~~~~~~~^
        comm, form, form_compiler_options=form_compiler_options, jit_options=jit_options
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/jit.py", line 61, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/usr/lib/petscdir/petsc-complex/lib/python3/dist-packages/dolfinx/jit.py", line 221, in ffcx_jit
    r = ffcx.codegeneration.jit.compile_forms([ufl_object], options=p_ffcx, **p_jit)
  File "/usr/lib/python3/dist-packages/ffcx/codegeneration/jit.py", line 224, in compile_forms
    impl = _compile_objects(
        decl,
    ...<9 lines>...
        visualise=visualise,
    )
  File "/usr/lib/python3/dist-packages/ffcx/codegeneration/jit.py", line 349, in _compile_objects
    code, _ = ffcx.compiler.compile_ufl_objects(
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        ufl_objects, namespace=module_name, options=options, visualise=visualise
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/lib/python3/dist-packages/ffcx/compiler.py", line 111, in compile_ufl_objects
    analysis = analyze_ufl_objects(ufl_objects, options["scalar_type"])  # type: ignore
  File "/usr/lib/python3/dist-packages/ffcx/analysis.py", line 91, in analyze_ufl_objects
    form_data = tuple(_analyze_form(form, scalar_type) for form in forms)
                ^^^^^
  File "/usr/lib/python3/dist-packages/ffcx/analysis.py", line 91, in <genexpr>
    form_data = tuple(_analyze_form(form, scalar_type) for form in forms)
                      ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/ffcx/analysis.py", line 175, in _analyze_form
    form_data: FormData = ufl.algorithms.compute_form_data(
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        form,
        ^^^^^
    ...<6 lines>...
        complex_mode=complex_mode,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/lib/python3/dist-packages/ufl/algorithms/compute_form_data.py", line 197, in compute_form_data
    return FormData(
        original_form,
    ...<5 lines>...
        complex_mode=complex_mode,
    )
  File "/usr/lib/python3/dist-packages/ufl/algorithms/formdata.py", line 341, in __init__
    _check_form_arity(self.integral_data, self.original_form.arguments(), complex_mode)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/ufl/algorithms/formdata.py", line 36, in _check_form_arity
    check_integrand_arity(itg.integrand(), arguments, complex_mode)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/ufl/algorithms/check_arities.py", line 205, in check_integrand_arity
    raise ArityMismatch("Failure to conjugate test function in complex Form")
ufl.algorithms.check_arities.ArityMismatch: Failure to conjugate test function in complex Form

In my practical use cases there are two subdomains but I noticed while trying to fix my code that the same error was raised regardless of them or of the forms I put in ufl.extract_blocks(). I should also point out that my installation has no issues running code with complex PETSc when dealing with problems that do not require mixed function spaces, and I am also able to solve problems with mixed function spaces when using real PETSc.

I am running Dolfinx version 0.11.0.post0, installed using apt on Debian sid.

Thanks !

You need to conjugate the test function, not the trial. Therefore, the test function should be the second argument of ufl.inner:

k_f = ufl.inner(ufl.grad(dp), ufl.grad(p)) * dx(fluid_tag)

I can’t believe i didn’t double check for that ! Thank you very much, that did indeed solve it.