Error on combine test or trial functions from different spaces

I encountered an error message as follow. I tried some online solutions but they don’t work in my case. Would anyone be able to offer me some guidance or assistance?

Found different Arguments with same number and part.
Did you combine test or trial functions from different spaces?
The Arguments found are:
  v_0
  v_1
  v_1
  v_0
ERROR:UFL:Found different Arguments with same number and part.
Did you combine test or trial functions from different spaces?
The Arguments found are:
  v_0
  v_1
  v_1
  v_0
Traceback (most recent call last):
  File "/Users/victoriachan/Documents/CityU Doc/FYP/python_files/ProgramV3.py", line 53, in <module>
    a = form(inner(kappa * div(w), div(p)) * dx + inner(grad(kappa * div(w)), p - grad(v)) * ds + inner(w - grad(u), grad(kappa * div(p))) * ds + tau/h**2 * inner(kappa*(w - grad(u)), p - grad(v)) * ds)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 176, in form
    return _create_form(form)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 171, in _create_form
    return _form(form)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/dolfinx/fem/forms.py", line 145, in _form
    ufcx_form, module, code = jit.ffcx_jit(mesh.comm, form,
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/dolfinx/jit.py", line 56, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/dolfinx/jit.py", line 204, in ffcx_jit
    r = ffcx.codegeneration.jit.compile_forms([ufl_object], options=p_ffcx, **p_jit)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ffcx/codegeneration/jit.py", line 165, in compile_forms
    ffcx.naming.compute_signature(forms, _compute_option_signature(p)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ffcx/naming.py", line 33, in compute_signature
    object_signature += ufl_object.signature()
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ufl/form.py", line 435, in signature
    self._compute_signature()
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ufl/form.py", line 685, in _compute_signature
    self._compute_renumbering())
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ufl/form.py", line 648, in _compute_renumbering
    cn = self.coefficient_numbering()
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ufl/form.py", line 421, in coefficient_numbering
    self._analyze_form_arguments()
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ufl/form.py", line 635, in _analyze_form_arguments
    arguments, coefficients = extract_arguments_and_coefficients(self)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ufl/algorithms/analysis.py", line 139, in extract_arguments_and_coefficients
    error(msg)
  File "/Users/victoriachan/opt/anaconda3/envs/fenicsdolxconda/lib/python3.10/site-packages/ufl/log.py", line 135, in error
    raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: Found different Arguments with same number and part.
Did you combine test or trial functions from different spaces?
The Arguments found are:
  v_0
  v_1
  v_1
  v_0

My code:

import numpy as np

import ufl
from dolfinx import cpp as _cpp
from dolfinx import fem
from dolfinx.fem import (Constant, Function, FunctionSpace, dirichletbc,
                         extract_function_spaces, form,
                         locate_dofs_geometrical, locate_dofs_topological)
from dolfinx.io import XDMFFile
from dolfinx.mesh import (CellType, GhostMode, create_rectangle,
                          locate_entities_boundary)
from ufl import div, ds, dx, grad, inner

from mpi4py import MPI
from petsc4py import PETSc

# Create mesh
msh = create_rectangle(MPI.COMM_WORLD,
                       [np.array([0, 0]), np.array([1, 1])],
                       [32, 32],
                       CellType.triangle, GhostMode.none)


def noslip_boundary(x):
    return np.logical_or(np.logical_or(np.isclose(x[0], 0.0),
                                       np.isclose(x[0], 1.0)),
                         np.logical_or(np.isclose(x[1], 0.0),
                                       np.isclose(x[1], 1.0)))

P2 = ufl.VectorElement("Lagrange", msh.ufl_cell(), 2)
P1 = ufl.FiniteElement("Lagrange", msh.ufl_cell(), 1)
V, Q = FunctionSpace(msh, P2), FunctionSpace(msh, P1)

noslip = np.zeros(msh.geometry.dim, dtype=PETSc.ScalarType)
facets = locate_entities_boundary(msh, 1, noslip_boundary)
bc0 = dirichletbc(noslip, locate_dofs_topological(V, 1, facets), V)

bcs = [bc0] # For more than 1 boundary conditions: bcs = [bc0, bc1]

# Define variational problem
(w, u) = ufl.TrialFunction(V), ufl.TrialFunction(Q)
(p, v) = ufl.TestFunction(V), ufl.TestFunction(Q)

kappa = 1.0
tau = 1.0
h = 3.0

# a = inner(kappa * div(w), div(p)) * dx + inner(grad(kappa * div(w)), p - grad(v)) * ds + inner(w - grad(u), grad(kappa * div(p))) * ds + tau/h**2 * inner(kappa*(w - grad(u)), p - grad(v)) * ds
a = form(inner(kappa * div(w), div(p)) * dx + inner(grad(kappa * div(w)), p - grad(v)) * ds + inner(w - grad(u), grad(kappa * div(p))) * ds + tau/h**2 * inner(kappa*(w - grad(u)), p - grad(v)) * ds)

A = fem.petsc.assemble_matrix(a, bcs=bcs)
A.assemble()

I’ve already told you that you should use a Mixed-element.

In general, I would strongly advice you to familiarize yourself with the demos
https://docs.fenicsproject.org/dolfinx/v0.6.0/python/demos.html#demos
and if you want more details, consider the tutorial:
https://jsdokken.com/dolfinx-tutorial/

Thanks @dokken. I tried but I can’t find the term “norm_u_0” . Am I missing something?

My code:

import numpy as np

import ufl
from dolfinx import cpp as _cpp
from dolfinx import fem
from dolfinx.fem import (Constant, Function, FunctionSpace, dirichletbc,
                         extract_function_spaces, form,
                         locate_dofs_geometrical, locate_dofs_topological)
from dolfinx.io import XDMFFile
from dolfinx.mesh import (CellType, GhostMode, create_rectangle,
                          locate_entities_boundary)
from ufl import div, ds, dx, grad, inner

from mpi4py import MPI
from petsc4py import PETSc

# Create mesh
msh = create_rectangle(MPI.COMM_WORLD,
                       [np.array([0, 0]), np.array([1, 1])],
                       [32, 32],
                       CellType.triangle, GhostMode.none)

# Create the function space
P2 = ufl.VectorElement("Lagrange", msh.ufl_cell(), 2)
P1 = ufl.FiniteElement("Lagrange", msh.ufl_cell(), 1)
V, Q = FunctionSpace(msh, P2), FunctionSpace(msh, P1)

TH = P2 * P1
W = FunctionSpace(msh, TH)
W0, _ = W.sub(0).collapse()

# Function to mark x = 0, x = 1, y = 0, y = 1
def noslip_boundary(x):
    return np.logical_or(np.logical_or(np.isclose(x[0], 0.0),
                                       np.isclose(x[0], 1.0)),
                         np.logical_or(np.isclose(x[1], 0.0),
                                       np.isclose(x[1], 1.0)))

# No slip boundary condition
noslip = Function(V)
facets = locate_entities_boundary(msh, 1, noslip_boundary)
dofs = locate_dofs_topological((W.sub(0), V), 1, facets)
bc0 = dirichletbc(noslip, dofs, W.sub(0))

# Since for this problem the pressure is only determined up to a
# constant, we pin the pressure at the point (0, 0)
zero = Function(Q)
zero.x.set(0.0)
dofs = locate_dofs_geometrical((W.sub(1), Q), lambda x: np.isclose(x.T, [0, 0, 0]).all(axis=1))
bc1 = dirichletbc(zero, dofs, W.sub(1))

# Collect Dirichlet boundary conditions
bcs = [bc0, bc1]

# Define variational problem
(w, u) = ufl.TrialFunctions(W)
(p, v) = ufl.TestFunctions(W)

kappa = 1.0
tau = 1.0
h = 3.0
a = form(inner(kappa * div(w), div(p)) * dx + inner(grad(kappa * div(w)), p - grad(v)) * ds + inner(w - grad(u), grad(kappa * div(p))) * ds + tau/h**2 * inner(kappa*(w - grad(u)), p - grad(v)) * ds)
L = form(inner(u, v) * dx)

# Assemble LHS matrix and RHS vector
A = fem.petsc.assemble_matrix(a, bcs=bcs)
A.assemble()
b = fem.petsc.assemble_vector(L)

# ========================

# Set Dirichlet boundary condition values in the RHS
fem.petsc.set_bc(b, bcs)

# Create and configure solver
ksp = PETSc.KSP().create(msh.comm)
ksp.setOperators(A)
ksp.setType("preonly")
ksp.getPC().setType("lu")
ksp.getPC().setFactorSolverType("superlu_dist")

# --------------------------

# Compute the solution
U = Function(W)
ksp.solve(b, U.vector)

# Split the mixed solution and collapse
u = U.sub(0).collapse()
p = U.sub(1).collapse()

# Compute norms
norm_u_3 = u.x.norm()
norm_p_3 = p.x.norm()
if MPI.COMM_WORLD.rank == 0:
    print("(D) Norm of velocity coefficient vector (monolithic, direct): {}".format(norm_u_3))
    print("(D) Norm of pressure coefficient vector (monolithic, direct): {}".format(norm_p_3))
assert np.isclose(norm_u_3, norm_u_0)

# Write the solution to file
with XDMFFile(MPI.COMM_WORLD, "out_stokes/new_velocity.xdmf", "w") as ufile_xdmf:
    u.x.scatter_forward()
    ufile_xdmf.write_mesh(msh)
    ufile_xdmf.write_function(u)

with XDMFFile(MPI.COMM_WORLD, "out_stokes/my.xdmf", "w") as pfile_xdmf:
    p.x.scatter_forward()
    pfile_xdmf.write_mesh(msh)
    pfile_xdmf.write_function(p)

The error message:

(D) Norm of velocity coefficient vector (monolithic, direct): nan
(D) Norm of pressure coefficient vector (monolithic, direct): nan
Traceback (most recent call last):
  File "/Users/victoriachan/Documents/CityU Doc/FYP/python_files/ProgramV4.py", line 98, in <module>
    assert np.isclose(norm_u_3, norm_u_0)
NameError: name 'norm_u_0' is not defined. Did you mean: 'norm_u_3'?

Also, I am wondering from which part I can start to solve the problem to get its eigenvalue. Thank you so much for your kind help.

Please look carefully at the demo, as norm_u_0 = u.x.norm() is defined in the code.

Eigenvalue problems are solved with SLEPc.
This has been illustrated in many posts on the forum (for instance: Eigenvalue solver problem) and in the documentation I’ve already referred you to.

Please spend some time reading through the demos and related posts before asking further questions.

1 Like

Ok, thank you so much!