Cannot find common parent mesh in legacy FEniCS

Hello everyone,

When I run the following code of Stokes equations on a new machine:

from fenics import *

mesh = UnitSquareMesh(10, 10)
V = VectorFunctionSpace(mesh, "P", 2)
Q = FunctionSpace(mesh, "P", 1)
VQ = MixedFunctionSpace(V, Q)
(u, p) = TrialFunctions(VQ)
(v, q) = TestFunctions(VQ)
sol = Function(VQ)

mesh0 = Mesh(mesh)
V0 = VectorFunctionSpace(mesh0, "P", 2)
u0 = Function(V0)
u0.set_allow_extrapolation(True)

dx = Measure("dx", domain=mesh)
a = inner(grad(u), grad(v))*dx - p*div(v)*dx + q*div(u)*dx + Constant(0)*p*q*dx
L = dot(Constant((0, 1)), v)*dx + dot(u0, v)*dx
bc = DirichletBC(V, Constant((0, 0)), "on_boundary")

system = assemble_mixed_system(a == L, sol, bc)

I get the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[7], line 21
     18 L = dot(Constant((0, 1)), v)*dx + dot(u0, v)*dx
     19 bc = DirichletBC(V, Constant((0, 0)), "on_boundary")
---> 21 system = assemble_mixed_system(a == L, sol, bc)

File /usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py:478, in assemble_mixed_system(*args, **kwargs)
    476     # Create solver and call solve
    477     solver = MixedLinearVariationalSolver(problem)
--> 478     system = solver.assemble_system()
    479     return system
    480 else:

RuntimeError: Cannot find common parent mesh

After testing, I believe the error comes from the term dot(u0, v)*dx, as the functions u0 and u, v are considered unable to find a common parent mesh, even though their meshes are actually the same, and u0 has already been allowed to extrapolate. However, this error does not occur on my old machine.

Therefore, I suspect this might be related to the version of legacy FEniCS. The legacy FEniCS version for Ubuntu 24.04.1 LTS on my new machine is

fenics-dijitso:     2019.2.0.dev0
fenics-dolfin:      2019.2.0.64.dev0
fenics-ffc:         2019.2.0.dev0
fenics-fiat:        2019.2.0.dev0
fenics-ufl-legacy:  2022.3.0

while the legacy FEniCS version for Ubuntu 24.04.1 LTS on my old machine is

fenics-dijitso:     2019.2.0.dev0
fenics-dolfin:      2019.2.0.dev0
fenics-ffc:         2019.2.0.dev0
fenics-fiat:        2019.2.0.dev0
fenics-ufl-legacy:  2022.2.0

Is there any way to fix the error in the code above, or install a specific version of legacy FEniCS? I would appreciate any help.

Why are you using assemble_mixed_system, and not just assemble for this?

Why are you making a copy of the mesh?
Assemble_mixed_system was made to support MeshView, with mixed function spaces, not what you are trying to do above.

Thank you very much for your response.

Could you please explain this with some example code? Do you mean assembling the matrices and vectors of a == L separately and then combining them into a complete linear system? For example:

A11 = assemble(inner(grad(u), grad(v))*dx)
A12 = assemble(-p*div(v)*dx)
A21 = assemble(q*div(u)*dx)
A22 = assemble(Constant(0)*p*q*dx)
b1 = assemble(dot(Constant((0, 1)), v)*dx + dot(u0, v)*dx)

However, a warning message will also appear when assembling b1:

*** Warning: assemble() with forms involving integrands belonging to different meshes might be inappropriate.
 If you are using MeshView, please use assemble_mixed() instead

In the code, I intend to use u0 to represent any function from another mesh, which may appear in some iterations related to moving meshes. The mesh0 can also be any mesh, not necessarily a copy of mesh. The code above is a MWE of the issue I’m encountering. How should I modify my code to make it run successfully?

Even if I replace assemble_mixed_system(a == L, sol, bc) with solve(a == L, sol, bc), I still get an error message about

RuntimeError: Cannot find common parent mesh

I noticed that legacy DOLFIN seemed to have some major fixes last year, and the above code runs successfully in the version of fenics-dolfin: 2019.2.0.dev0 but throws an error in the version of fenics-dolfin: 2019.2.0.64.dev0. Could this issue be related to the latest updates in legacy FEniCS?