ArityMismatch error with complex PETSc when using conjugated test functions in nonlinear Poisson equation

Hi everyone, I am working through the nonlinear Poisson equation tutorial and encountering an ArityMismatch error when trying to use conjugated test functions with the complex PETSc library.

Setup

  • FEniCS version: DOLFINx 0.8.0.
  • Installation: mamba install -c conda-forge fenics-dolfinx mpich pyvista followed by complex PETSc installation.
  • Platform: Linux (Ubuntu 24.04.2 LTS).
  • PETSc: Complex version.

Problem Description

I need to use complex PETSc for a complex-valued non-linear PDE, which requires conjugated test functions in the variational form. However, when I try to create a NonlinearProblem with ufl.conj(v) in the residual form, I get an ArityMismatch error, when trying to reproduce the nonlinear Poisson equation.

...

uh = fem.Function(V)
v = ufl.TestFunction(V)

# This works with real PETSc but fails with complex PETSc
F = q(uh) * ufl.inner(ufl.grad(uh), ufl.grad(ufl.conj(v))) * ufl.dx - f * ufl.conj(v) * ufl.dx

# This line triggers the error
problem = NonlinearProblem(F, uh, bcs=[bc])

Error Message

ArityMismatch                             Traceback (most recent call last)
Cell In[5], line 2
      1 # Create and solve nonlinear problem
----> 2 problem = NonlinearProblem(F, uh, bcs=[bc])

[... traceback continues ...]

File ~/anaconda3/envs/fenics_env/lib/python3.11/site-packages/ufl/algorithms/check_arities.py:57, in ArityChecker.sum(self, o, a, b)
     55 """Apply to sum."""
     56 if a != b:
---> 57     raise ArityMismatch(
     58         f"Adding expressions with non-matching form arguments {_afmt(a)} vs {_afmt(b)}."
     59     )
     60 return a

ArityMismatch: Adding expressions with non-matching form arguments ('conj(v_0)',) vs ('v_0',)

What I’ve Tried

  1. Without conjugation: F = q(uh) * ufl.inner(ufl.grad(uh), ufl.grad(v)) * ufl.dx - f * v * ufl.dx, raising similar error.
  2. Consistent conjugation: Ensuring all test function terms use ufl.conj(v).
  3. Real part wrapper: F = ufl.real(...) around the entire form.
  4. Separated forms: Defining bilinear and linear parts separately.
  5. Cache clearing: Removed ~/.cache/fenics/* to resolve subsequent timeout errors.

The error suggests the FFCx form compiler is seeing inconsistent argument structures when mixing conjugated and non-conjugated terms in the Newton’s method, but I’m not sure how to resolve this properly while maintaining the mathematical correctness for complex PETSc.

Is there a proper way to handle conjugated test functions in nonlinear forms with complex PETSc in DOLFINx 0.8.0 (e.g., providing explicitly the variational form of the Jacobian)?

Should I be using a different approach entirely for complex-valued nonlinear problems?

Any guidance would be greatly appreciated!

As your code is not sufficient to reproduce the problem, it is hard for me to give any guidance.
I am assuming that you are using q(u) from: Implementation — FEniCSx tutorial
In your code, you are using inner and conj, while inner applies the conj already, i.e.

F = q(uh) * ufl.inner(ufl.grad(uh), ufl.grad(v)) * ufl.dx - ufl.inner(f, v) * ufl.dx

works nicely for me.

It worked.
It was not clear to me that the inner method assumes sesquilinear forms without explicitly defining the complex conjugate test function, I should have checked the UFL documentation.
Thank you for your fast and concise reply.