# Problem when combining test and trial functions from different spaces

**URL:** <https://fenicsproject.discourse.group/t/problem-when-combining-test-and-trial-functions-from-different-spaces/7083>\
**Category:** variational formulation\
**Created:** [November 14, 2021, 11:22pm UTC](https://fenicsproject.discourse.group/t/problem-when-combining-test-and-trial-functions-from-different-spaces/7083 "2021-11-14T23:22:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [November 14, 2021, 11:22pm UTC](https://fenicsproject.discourse.group/t/problem-when-combining-test-and-trial-functions-from-different-spaces/7083/1 "2021-11-14T23:22:50Z")

</div>

Hi all,

I am trying to solve a set of equations that involve velocity, pressure and temperature.  
The finite element spaces that I am using for the different variables are the following:

> # Finite element spaces
> 
> import ufl  
> v\_cg2 = ufl.VectorElement(“CG”, mesh.ufl\_cell(), 2)  
> p\_cg1 = ufl.FiniteElement(“CG”, mesh.ufl\_cell(), 1)  
> T\_cg2 = ufl.FiniteElement(“CG”, mesh.ufl\_cell(), 2)  
> V = dolfinx.FunctionSpace(mesh, v\_cg2) # Velocity function space  
> Q = dolfinx.FunctionSpace(mesh, p\_cg1) # Pressure function space  
> W = dolfinx.FunctionSpace(mesh, T\_cg2) # Temperature function space

> # Trial and test functions
> 
> u = ufl.TrialFunction(V) # Velocity (trial)  
> v = ufl.TestFunction(V) # Velocity (test)  
> p = ufl.TrialFunction(Q) # Pressure (trial)  
> q = ufl.TestFunction(Q) # Pressure (test)  
> T = ufl.TrialFunction(W) # Temperature (trial)  
> w = ufl.TestFunction(W) # Temperature (test)

The problem is that the equations are coupled, so I need to solve them simultaneously. However, I am getting errors when I try to build the bilinear form, since I need to combine test and trial functions from different element spaces.  
For example, I need to compute the following term:

> L1 = inner(q,(gamma\*p-T))\*dx  
> A = dolfinx.fem.assemble\_matrix(L1)  
> A.assemble()

which gives me the following error:

> Found different Arguments with same number and part.  
> Did you combine test or trial functions from different spaces?  
> The Arguments found are:  
> v\_1  
> v\_0  
> v\_1  
> 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\_1  
> v\_0  
> v\_1  
> Traceback (most recent call last):  
> File “/root/dolfinX/Test.py”, line 110, in   
> A = dolfinx.fem.assemble\_matrix(L1)  
> File “/usr/lib/python3.9/functools.py”, line 877, in wrapper  
> return dispatch(args[0].\_\_class\_\_)(\*args, \*\*kw)  
> File “/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/fem/assemble.py”, line 283, in assemble\_matrix  
> A = cpp.fem.create\_matrix(\_create\_cpp\_form(a))  
> File “/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/fem/assemble.py”, line 35, in _create\_cpp\_form  
> return Form(form).cpp\_object  
> File “/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/fem/form.py”, line 56, in \_\_init_  
> self.\_ufc\_form, module, self.\_code = jit.ffcx\_jit(  
> File “/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/jit.py”, line 54, in mpi\_jit  
> return local\_jit(\*args, \*\*kwargs)  
> File “/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/jit.py”, line 204, in ffcx\_jit  
> r = ffcx.codegeneration.jit.compile\_forms([ufl\_object], parameters=p\_ffcx, \*\*p\_jit)  
> File “/usr/local/lib/python3.9/dist-packages/ffcx/codegeneration/jit.py”, line 145, in compile\_forms  
> ffcx.naming.compute\_signature(forms, \_compute\_parameter\_signature(p)  
> File “/usr/local/lib/python3.9/dist-packages/ffcx/naming.py”, line 24, in compute\_signature  
> object\_signature += ufl\_object.signature()  
> File “/usr/local/lib/python3.9/dist-packages/ufl/form.py”, line 243, in signature  
> self.\_compute\_signature()  
> File “/usr/local/lib/python3.9/dist-packages/ufl/form.py”, line 487, in \_compute\_signature  
> self.\_compute\_renumbering())  
> File “/usr/local/lib/python3.9/dist-packages/ufl/form.py”, line 460, in \_compute\_renumbering  
> cn = self.coefficient\_numbering()  
> File “/usr/local/lib/python3.9/dist-packages/ufl/form.py”, line 234, in coefficient\_numbering  
> self.\_analyze\_form\_arguments()  
> File “/usr/local/lib/python3.9/dist-packages/ufl/form.py”, line 447, in \_analyze\_form\_arguments  
> arguments, coefficients = extract\_arguments\_and\_coefficients(self)  
> File “/usr/local/lib/python3.9/dist-packages/ufl/algorithms/analysis.py”, line 127, in extract\_arguments\_and\_coefficients  
> error(msg)  
> File “/usr/local/lib/python3.9/dist-packages/ufl/log.py”, line 158, 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\_1  
> v\_0  
> v\_1

What I understand then is that I cannot combine test and trial functions from different spaces. Therefore, I defined a mixed finite element space to try to fix this error:

> # Mixed finite element space
> 
> import ufl  
> v\_cg2 = ufl.VectorElement(“CG”, mesh.ufl\_cell(), 2)  
> p\_cg1 = ufl.FiniteElement(“CG”, mesh.ufl\_cell(), 1)  
> T\_cg2 = ufl.FiniteElement(“CG”, mesh.ufl\_cell(), 2)  
> mel = ufl.MixedElement([v\_cg2, p\_cg1, T\_cg2])  
> Y = dolfinx.FunctionSpace(mesh, mel) # Mixed function space

and redefined the trial and test functions as follows:

> # Trial and test functions
> 
> u = ufl.TrialFunction(Y) # Velocity (trial)  
> v = ufl.TestFunction(Y) # Velocity (test)  
> p = ufl.TrialFunction(Y) # Pressure (trial)  
> q = ufl.TestFunction(Y) # Pressure (test)  
> T = ufl.TrialFunction(Y) # Temperature (trial)  
> w = ufl.TestFunction(Y) # Temperature (test)

This approach fixes the previous error, but now I am getting another one when I try to compute another term:

> L2 = inner(w,div(u))\*dx  
> A = dolfinx.fem.assemble\_matrix(L2)  
> A.assemble()

The error now is:

> Shapes do not match: \<Argument id=140363992421472\> and \<Div id=140364133793920\>.  
> ERROR:UFL:Shapes do not match: \<Argument id=140363992421472\> and \<Div id=140364133793920\>.  
> Traceback (most recent call last):  
> File “/root/dolfinX/Test.py”, line 105, in \<module\>  
> L2 = inner(w,div(u))\*dx  
> File “/usr/local/lib/python3.9/dist-packages/ufl/operators.py”, line 158, in inner  
> return Inner(a, b)  
> File “/usr/local/lib/python3.9/dist-packages/ufl/tensoralgebra.py”, line 147, in \_\_new\_\_  
> error(“Shapes do not match: %s and %s.” % (ufl\_err\_str(a), ufl\_err\_str(b)))  
> File “/usr/local/lib/python3.9/dist-packages/ufl/log.py”, line 158, in error  
> raise self.\_exception\_type(self.\_format\_raw(\*message))  
> ufl.log.UFLException: Shapes do not match: \<Argument id=140363992421472\> and \<Div id=140364133793920\>.

This means that the shape of w is different from the shape of div(u), which is understandable since now w and u belong to the same mixed space W (and when taking the divergence of u, the space changes).

Consequently, my question would be how can I combine test and trial functions that belong to different spaces to build the bilinear form. Does anybody have any idea?

My minimal example is:

> # Import  
> from mpi4py import MPI  
> rank = MPI.COMM\_WORLD.rank  
> import dolfinx  
> from ufl import Identity, div, dot, ds, dx, inner, lhs, nabla\_grad, rhs, sym, Dn, grad
> 
> ‘’’ Generation of geometry and mesh ‘’’  
> # Domain dimensions  
> L = 3  
> H = 0.5  
> gdim = 3
> 
> # Initialise meshing process  
> import gmsh  
> gmsh.initialize()
> 
> # Geometry  
> if rank == 0:  
> gmsh.model.add(“TVA Channel 3D”)  
> fluid = gmsh.model.occ.addBox(0, 0, 0, L, H, H)  
> gmsh.model.occ.synchronize()
> 
> # Generate mesh  
> if rank == 0:  
> gmsh.model.mesh.generate(gdim)  
> gmsh.write(“meshTVAchannel.msh”)
> 
> # Domain marker  
> fluid\_marker = 1  
> if rank == 0:  
> volumes = gmsh.model.getEntities(dim=gdim)  
> gmsh.model.addPhysicalGroup(volumes[0][0], [volumes[0][1]], fluid\_marker)  
> gmsh.model.setPhysicalName(volumes[0][0], fluid\_marker, “Fluid”)
> 
> # Boundary markers  
> import numpy as np  
> inlet\_marker, outlet\_marker, wall\_marker = 2, 3, 4  
> inflow, outflow, walls = , ,   
> if rank == 0:  
> boundaries = gmsh.model.getBoundary(volumes)  
> for boundary in boundaries:  
> center\_of\_mass = gmsh.model.occ.getCenterOfMass(boundary[0], boundary[1])  
> if np.allclose(center\_of\_mass, [0, H/2, 0]):  
> inflow.append(boundary[1])  
> elif np.allclose(center\_of\_mass, [L, H/2, 0]):  
> outflow.append(boundary[1])  
> else:  
> walls.append(boundary[1])  
> gmsh.model.addPhysicalGroup(1, walls, wall\_marker)  
> gmsh.model.setPhysicalName(1, wall\_marker, “Walls”)  
> gmsh.model.addPhysicalGroup(1, inflow, inlet\_marker)  
> gmsh.model.setPhysicalName(1, inlet\_marker, “Inlet”)  
> gmsh.model.addPhysicalGroup(1, outflow, outlet\_marker)  
> gmsh.model.setPhysicalName(1, outlet\_marker, “Outlet”)
> 
> # Import mesh to dolfinX  
> from gmsh\_helpers import gmsh\_model\_to\_mesh  
> mesh, facet\_tags = gmsh\_model\_to\_mesh(gmsh.model, cell\_data=True, facet\_data=False, gdim=3)
> 
> ‘’’ Generation of finite element spaces ‘’’  
> # Finite element spaces  
> import ufl  
> v\_cg2 = ufl.VectorElement(“CG”, mesh.ufl\_cell(), 2)  
> p\_cg1 = ufl.FiniteElement(“CG”, mesh.ufl\_cell(), 1)  
> T\_cg2 = ufl.FiniteElement(“CG”, mesh.ufl\_cell(), 2)  
> mel = ufl.MixedElement([v\_cg2, p\_cg1, T\_cg2])  
> V = dolfinx.FunctionSpace(mesh, v\_cg2)  
> Q = dolfinx.FunctionSpace(mesh, p\_cg1)  
> W = dolfinx.FunctionSpace(mesh, T\_cg2)  
> Y = dolfinx.FunctionSpace(mesh, mel)
> 
> # Trial and test functions  
> u = ufl.TrialFunction(V)  
> v = ufl.TestFunction(V)  
> p = ufl.TrialFunction(Q)  
> q = ufl.TestFunction(Q)  
> T = ufl.TrialFunction(W)  
> w = ufl.TestFunction(W)  
> # # Trial and test functions  
> # u = ufl.TrialFunction(Y)  
> # v = ufl.TestFunction(Y)  
> # p = ufl.TrialFunction(Y)  
> # q = ufl.TestFunction(Y)  
> # T = ufl.TrialFunction(Y)  
> # w = ufl.TestFunction(Y)
> 
> # Numerical parameters  
> gamma = 1.017
> 
> ‘’‘’ Left-hand side ‘’’  
> # First component  
> L1 = inner(q,(gamma\*p-T))\*dx
> 
> # Second component  
> L2 = inner(w,div(u))\*dx
> 
> L = L1 + L2
> 
> A = dolfinx.fem.assemble\_matrix(L)  
> A.assemble()

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 15, 2021, 10:16am UTC](https://fenicsproject.discourse.group/t/problem-when-combining-test-and-trial-functions-from-different-spaces/7083/2 "2021-11-15T10:16:20Z")

</div>

Please make sure that your code is properly formatted (indentation preserved) using 3x` encapsulation, and make sure to remove all code not needed for reproducibility.

---

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [November 15, 2021, 11:04am UTC](https://fenicsproject.discourse.group/t/problem-when-combining-test-and-trial-functions-from-different-spaces/7083/3 "2021-11-15T11:04:29Z")

</div>

Sorry, I did not paste the code in the correct format. Please find below a minimal example for the first error and a minimal example for the second error.

# Minimal example for first error

```auto
# Import
import dolfinx
from ufl import Identity, div, dot, ds, dx, inner, lhs, nabla_grad, rhs, sym, Dn, grad

# Domain dimensions
L = 3
H = 0.5
gdim = 3

# Initialise meshing process
import gmsh
gmsh.initialize()

# Geometry
fluid = gmsh.model.occ.addBox(0, 0, 0, L, H, H)
gmsh.model.occ.synchronize()

# Generate mesh
gmsh.model.mesh.generate(gdim)

# Domain marker
fluid_marker = 1
volumes = gmsh.model.getEntities(dim=gdim)
gmsh.model.addPhysicalGroup(volumes[0][0], [volumes[0][1]], fluid_marker)
gmsh.model.setPhysicalName(volumes[0][0], fluid_marker, 'Fluid')

# Import mesh to dolfinX
from gmsh_helpers import gmsh_model_to_mesh
mesh, facet_tags = gmsh_model_to_mesh(gmsh.model, cell_data=True, facet_data=False, gdim=3)

# Finite element spaces
import ufl
v_cg2 = ufl.VectorElement('CG', mesh.ufl_cell(), 2)
p_cg1 = ufl.FiniteElement('CG', mesh.ufl_cell(), 1)
T_cg2 = ufl.FiniteElement('CG', mesh.ufl_cell(), 2)
mel = ufl.MixedElement([v_cg2, p_cg1, T_cg2])
V = dolfinx.FunctionSpace(mesh, v_cg2)
Q = dolfinx.FunctionSpace(mesh, p_cg1)
W = dolfinx.FunctionSpace(mesh, T_cg2)
Y = dolfinx.FunctionSpace(mesh, mel)

# Trial and test functions
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
p = ufl.TrialFunction(Q)
q = ufl.TestFunction(Q)
T = ufl.TrialFunction(W)
w = ufl.TestFunction(W)

# Numerical parameters
gamma = 1.017

# First component
L1 = inner(q,(gamma*p-T))*dx

# Assemble bilinear form
A = dolfinx.fem.assemble_matrix(L1)
A.assemble()

```

# Minimal example for the second error

```auto
# Import
import dolfinx
from ufl import Identity, div, dot, ds, dx, inner, lhs, nabla_grad, rhs, sym, Dn, grad

# Domain dimensions
L = 3
H = 0.5
gdim = 3

# Initialise meshing process
import gmsh
gmsh.initialize()

# Geometry
fluid = gmsh.model.occ.addBox(0, 0, 0, L, H, H)
gmsh.model.occ.synchronize()

# Generate mesh
gmsh.model.mesh.generate(gdim)

# Domain marker
fluid_marker = 1
volumes = gmsh.model.getEntities(dim=gdim)
gmsh.model.addPhysicalGroup(volumes[0][0], [volumes[0][1]], fluid_marker)
gmsh.model.setPhysicalName(volumes[0][0], fluid_marker, 'Fluid')

# Import mesh to dolfinX
from gmsh_helpers import gmsh_model_to_mesh
mesh, facet_tags = gmsh_model_to_mesh(gmsh.model, cell_data=True, facet_data=False, gdim=3)

# Finite element spaces
import ufl
v_cg2 = ufl.VectorElement('CG', mesh.ufl_cell(), 2)
p_cg1 = ufl.FiniteElement('CG', mesh.ufl_cell(), 1)
T_cg2 = ufl.FiniteElement('CG', mesh.ufl_cell(), 2)
mel = ufl.MixedElement([v_cg2, p_cg1, T_cg2])
V = dolfinx.FunctionSpace(mesh, v_cg2)
Q = dolfinx.FunctionSpace(mesh, p_cg1)
W = dolfinx.FunctionSpace(mesh, T_cg2)
Y = dolfinx.FunctionSpace(mesh, mel)

# Trial and test functions
u = ufl.TrialFunction(Y)
v = ufl.TestFunction(Y)
p = ufl.TrialFunction(Y)
q = ufl.TestFunction(Y)
T = ufl.TrialFunction(Y)
w = ufl.TestFunction(Y)

# Numerical parameters
gamma = 1.017

# Second component
L2 = inner(w,div(u))*dx

# Assemble bilinear form
A = dolfinx.fem.assemble_matrix(L2)
A.assemble()

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 15, 2021, 4:57pm UTC](https://fenicsproject.discourse.group/t/problem-when-combining-test-and-trial-functions-from-different-spaces/7083/4 "2021-11-15T16:57:28Z")

</div>

You need to split the trial function from the mixed space for each component, which can either be done by

```python
y = ufl.TrialFunction(Y)
u, p, T = ufl.split(y)

```

or use `ufl.TrialFunctions`, as shown below:

```python
# Import
import dolfinx
from ufl import div, dx, inner, FiniteElement, VectorElement, TrialFunctions, TestFunctions, MixedElement
from mpi4py import MPI
mesh = dolfinx.UnitCubeMesh(MPI.COMM_WORLD, 10, 10, 10)
v_cg2 = VectorElement('CG', mesh.ufl_cell(), 2)
p_cg1 = FiniteElement('CG', mesh.ufl_cell(), 1)
T_cg2 = FiniteElement('CG', mesh.ufl_cell(), 2)
mel = MixedElement([v_cg2, p_cg1, T_cg2])
V = dolfinx.FunctionSpace(mesh, v_cg2)
Q = dolfinx.FunctionSpace(mesh, p_cg1)
W = dolfinx.FunctionSpace(mesh, T_cg2)
Y = dolfinx.FunctionSpace(mesh, mel)

# Trial and test functions
u, p, T = TrialFunctions(Y)
v, q, w = TestFunctions(Y)

# Numerical parameters
gamma = 1.017

# Second component
L2 = inner(w,div(u))*dx

# Assemble bilinear form
A = dolfinx.fem.assemble_matrix(L2)
A.assemble()

```

---

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [November 15, 2021, 5:31pm UTC](https://fenicsproject.discourse.group/t/problem-when-combining-test-and-trial-functions-from-different-spaces/7083/5 "2021-11-15T17:31:10Z")

</div>

Thank you very much! It’s working now.
