How can I resolve a matrix error when converting a 3D GMSH mesh to a 2D mesh and exporting it from .msh to .xdmf format?

Hi! I am a beginner to this environment. In my project, especially in learning, I am facing an error in finding sym(grad(v)) value. I am using an example which is done by using FEniCSx mesh but i replaced the mesh part with my gmsh’s .msh file using meshio. Here I provide my code and error. It will be great if you help me in this learning. My dolfinx version is 0.8.0 and Python version is 3.11.7.

<
E = fem.Constant(domain, 210e3)
nu = fem.Constant(domain, 0.3)

lmbda = E * nu / (1 + nu) / (1 - 2 * nu)
mu = E / 2 / (1 + nu)

def epsilon(v):
return sym(grad(v))

def sigma(v):
return lmbda * tr(epsilon(v)) * Identity(dim) + 2 * mu * epsilon(v)

print(“mu (UFL):\n”, mu)
print(“epsilon (UFL):\n”, epsilon(u_sol))
print(“sigma (UFL):\n”, sigma(u_sol))

<
The error i get is :

ValueError Traceback (most recent call last)
Cell In[33], line 2
1 print(“mu (UFL):\n”, mu)
----> 2 print(“epsilon (UFL):\n”, epsilon(u_sol))
3 print(“sigma (UFL):\n”, sigma(u_sol))

Cell In[32], line 9, in epsilon(v)
8 def epsilon(v):
----> 9 return sym(grad(v))

File ~/anaconda3/envs/fenicsx/lib/python3.12/site-packages/ufl/operators.py:337, in sym(A)
335 “”“Take the symmetric part of A.”“”
336 A = as_ufl(A)
→ 337 return Sym(A)

File ~/anaconda3/envs/fenicsx/lib/python3.12/site-packages/ufl/tensoralgebra.py:544, in Sym.new(cls, A)
542 raise ValueError(“Symmetric part of tensor with rank != 2 is undefined.”)
543 if sh[0] != sh[1]:
→ 544 raise ValueError(
545 f"Cannot take symmetric part of rectangular matrix with dimensions {sh}."
546 )
547 if Afi:
548 raise ValueError(“Not expecting free indices in Sym.”)

ValueError: Cannot take symmetric part of rectangular matrix with dimensions (2, 3).

Please refer images for more clarity.

Suggestion 1: read Read before posting: How do I get my question answered?
Suggestion 2: never post your code as image. Imagine I was reading your post on my cellphone: I would need three pairs of glasses given how small the font is
Suggestion 3: I wouldn’t use meshio anymore. dolfinx has native capabilities to read in a gmsh mesh.
Suggestion 4: learn how to properly format your code with “```”, which would make the code (and also the error) much easier to read.

1 Like

@francesco-ballarin Thank you for addressing my mistakes in posting a question. As I am new to this forum, i didn’t know how to do this. I will correct this from now.

Now i will try to modify my question as per your suggestions.

Thank you.

@francesco-ballarin

Thank you for your Suggestion 3 because that helped me to resolve my problem.

Thank your sir.