Need some help in converting gmsh file into the form which can be used in fenics code

Hello everyone

Help me in resolving issue which is attached below. Also tell me how we can use refinement here.

from fenics import *
import sympy as sym
import numpy as np
import math
import matplotlib.pyplot as plt
import meshio
mesh = meshio.read("hole.msh")
# Define Function Spaces
V=VectorElement("Lagrange",mesh.ufl_cell(),2)
W=FiniteElement("Lagrange",mesh.ufl_cell(),1)
LM=FiniteElement('R',mesh.ufl_cell(),0)
TH=MixedElement([V, W, LM])
Z=FunctionSpace(mesh,TH)
# Define Test and trial function
up = Function(Z)
u, p, rho   = split(up)
v, q, lamda = TestFunctions(Z)

My hole.geo file syntax-

Point(1) = {-6,  2, 0, 0.5};
Point(2) = {-6, -2, 0, 0.5};
Point(3) = { 6, -2, 0, 0.5};
Point(4) = { 6,  2, 0, 0.5};
Point(5) = { 0,  0, 0, 0.1};
Point(6) = { 1,  0, 0, 0.1};
Point(7) = {-1,  0, 0, 0.1};
Point(8) = { 0,  1, 0, 0.1};
Point(9) = { 0, -1, 0, 0.1};
Line(1) = {1, 4};
Line(2) = {4, 3};
Line(3) = {3, 2};
Line(4) = {2, 1};
Circle(5) = {8, 5, 6};
Circle(6) = {6, 5, 9};
Circle(7) = {9, 5, 7};
Circle(8) = {7, 5, 8};
Curve Loop( 9) = {1, 2, 3, 4};
Curve Loop(10) = {8, 5, 6, 7};
Plane Surface(1) = {9, 10};
Plane Surface(2) = {10};
Physical Curve("HorEdges", 11) = {1, 3};
Physical Curve("VerEdges", 12) = {2, 4};
Physical Curve("Circle", 13) = {8, 7, 6, 5};
Physical Surface("PunchedDom", 3) = {1};
Physical Surface("Disc", 4) = {2};

the syntax that convert this .geo syntax to .msh-

gmsh -2 hole.geo -format msh2

THE error in the output is

hwloc/linux: Ignoring PCI device with non-16bit domain.
Pass --enable-32bits-pci-domain to configure to support such devices
(warning: it would break the library ABI, don't enable unless really needed).
hwloc/linux: Ignoring PCI device with non-16bit domain.
Pass --enable-32bits-pci-domain to configure to support such devices
(warning: it would break the library ABI, don't enable unless really needed).

Traceback (most recent call last):
  File "/home/ayush/Desktop/a.py", line 11, in <module>
    V=VectorElement("Lagrange",mesh.ufl_cell(),2)
AttributeError: 'Mesh' object has no attribute 'ufl_cell'

You cannot read msh files directly into legacy dolfin.

See: What is wrong with my mesh? - #13 by dokken

You could also move to DOLFINx, which can read msh directly:

@aparna
Isn’t your mesh 2D? Tetra is for 3D mesh.
You should use

line_mesh = create_mesh(msh, "line", prune_z=True)
triangle_mesh = create_mesh(msh, "triangle", prune_z=True)
1 Like

You would need to include the code you are executing here, as it seems like you have not defined the mesh variable in your script (mesh in the call above is referring to the dolfin.mesh module which is loaded when you call from dolfin import *)

from fenics import *
import sympy as sym
import numpy as np
import math
import matplotlib.pyplot as plt
import meshio
def create_mesh(mesh: meshio.Mesh, cell_type: str, data_name: str = "name_to_read",
                prune_z: bool = False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    points = mesh.points[:, :2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
                           data_name: [cell_data]})
    return out_mesh
# import and convert msh file to xdmf
msh = meshio.read('hole.msh')
facet_mesh = create_mesh(msh, "triangle")
meshio.write("test_facet.xdmf", facet_mesh)
triangle_mesh = create_mesh(msh, "triangle",prune_z=True)
meshio.write("test_mesh.xdmf", triangle_mesh)
# Define Function Spaces

V=VectorElement("Lagrange",mesh.ufl_cell(),2)
W=FiniteElement("Lagrange",mesh.ufl_cell(),1)
LM=FiniteElement('R',mesh.ufl_cell(),0)
TH=MixedElement([V, W, LM])
Z=FunctionSpace(mesh,TH)
# Define Test and trial function
up = Function(Z)
u, p, rho   = split(up)
v, q, lamda = TestFunctions(Z)

You need to read the mesh in from xdmf after creating the "test_mesh.xdmf" file.