Hi all, I want to assign values to the mixed elements in FEniCSX. Since there are not too many tutorials about mixed elements FEniCSX, I do it according to the tutorials of FEniCS, but the results seem to be incorrect.
Here is my minimal working example:
import dolfinx
import numpy as np
from mpi4py import MPI
from dolfinx.cpp.mesh import CellType
import ufl
mesh = dolfinx.RectangleMesh(MPI.COMM_WORLD, [np.array([0, 0, 0]), np.array([2, 2, 0])], \
[1, 2], CellType.quadrilateral)
v_el = ufl.VectorElement("CG", mesh.ufl_cell(), 1)
s_el = ufl.FiniteElement("CG", mesh.ufl_cell(), 1)
mixed_el = ufl.MixedElement([v_el, s_el]) # mixed element
VP = dolfinx.FunctionSpace(mesh, mixed_el) # mixed function space
q = dolfinx.Function(VP)
u, v = ufl.split(q) # trial functions
(w, y) = ufl.TestFunctions(VP) # test functions
# we assign arbitrary values to u and v as solutions
NNode = mesh.geometry.x.shape[0] # number of nodes
u0, v0 = q.split()
with u0.vector.localForm() as u0_loc:
u0_loc.setValues((np.arange(2*NNode)).tolist(), np.arange(2*NNode).tolist())
with v0.vector.localForm() as v0_loc:
v0_loc.setValues((2*NNode+np.arange(NNode)).tolist(), np.arange(NNode).tolist())
print(u0.compute_point_values().round(2))
print(v0.compute_point_values().round(2))
This code will give us the wrong result:
[[-0. 1.]
[ 2. 3.]
[ 4. 5.]
[ 6. 7.]
[ 0. 1.]
[ 2. 3.]]
[[ 8.]
[ 9.]
[10.]
[11.]
[ 4.]
[ 5.]]
If we use the simple elements
V = dolfinx.VectorFunctionSpace(mesh, ("CG", 1))
S = dolfinx.FunctionSpace(mesh, ("CG", 1))
u = dolfinx.Function(V)
v = dolfinx.Function(S)
NNode = mesh.geometry.x.shape[0] # number of nodes
with u.vector.localForm() as u_loc:
u_loc.setValues(np.arange(2*NNode).tolist(), np.arange(2*NNode).tolist())
with v.vector.localForm() as v_loc:
v_loc.setValues(np.arange(NNode).tolist(), np.arange(NNode).tolist())
print(u.compute_point_values().round(2))
print(v.compute_point_values().round(2))
we can obtain the correct results:
[[-0. 1.]
[ 2. 3.]
[ 4. 5.]
[ 6. 7.]
[ 8. 9.]
[10. 11.]]
[[-0.]
[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ 5.]]
If we reduce the element size,
mesh = dolfinx.RectangleMesh(MPI.COMM_WORLD, [np.array([0, 0, 0]), np.array([2, 2, 0])], \
[1, 1], CellType.quadrilateral)
We can obtain the same results from simple and mixed elements now:
[[0. 1.]
[2. 3.]
[4. 5.]
[6. 7.]]
[[0.]
[1.]
[2.]
[3.]]
So could anyone please tell me how to correctly assign values to mixed elements in FEniCS-X?
Thanks!