How to import quad mesh (generated in GMSH) with dolfin-converter


84
views
2
24 days ago by
Hello,

Apparently, dolfin-converter is not able to convert quad mesh generated with GMSH. What is the workaround to do so?

Thanks in advance,
Leonardo
Community: FEniCS Project

1 Answer


3
24 days ago by
Leo  
Hello
I will try to explain how it is possible. I faced with the same problem and I came up with this idea:

Lets say we have a very simple quad mesh generated in GMSH. Here is the mesh:



This is the text file for this mesh:

Point(1) = {0, 0, 0, 1.0};
Point(2) = {0, 1, 0, 1.0};
Point(3) = {1, 1, 0, 1.0};
Point(4) = {1, 0, 0, 1.0};


Line(1) = {1, 4};
Line(2) = {4, 3};
Line(3) = {3, 2};
Line(4) = {2, 1};
Line Loop(5) = {3, 4, 1, 2};
Plane Surface(6) = {5};


Recombine Surface {6};
Transfinite Surface{6};
Transfinite Line {1,2,3,4} = 3;


First of all it should be saved it in MSH format. If you open it you will see this:

$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
9
1 0 0 0
2 0 1 0
3 1 1 0
4 1 0 0
5 0.4999999999986921 0 0
6 1 0.4999999999986921 0
7 0.5000000000020595 1 0
8 0 0.5000000000020595 0
9 0.5000000000003757 0.5000000000003757 0
$EndNodes
$Elements
16
1 15 2 0 1 1
2 15 2 0 2 2
3 15 2 0 3 3
4 15 2 0 4 4
5 1 2 0 1 1 5
6 1 2 0 1 5 4
7 1 2 0 2 4 6
8 1 2 0 2 6 3
9 1 2 0 3 3 7
10 1 2 0 3 7 2
11 1 2 0 4 2 8
12 1 2 0 4 8 1
13 3 2 0 6 3 7 9 6
14 3 2 0 6 6 9 5 4
15 3 2 0 6 7 2 8 9
16 3 2 0 6 9 8 1 5
$EndElements


The MSH file includes the nodes and elements numbers and some additional information that should be removed from the file. In the above file for the element section, if the second column is equal to 3 it corresponds to the quad elements. So the additional numbers should be deleted. In addition only the first column(element number) and the last 4 columns (The nodes making the element) should be kept in the file. To do this, you can open the MSH file with excel and remove the additional numbers. In general you should edit the MSH file 2 times. First time for the nodes and the second time for the elements. Then save the files separately in txt format. I have already done it for this example. Here is the modification for the nodes (nodes.txt):

1 0 0
2 0 1
3 1 1
4 1 0
5 0.5 0
6 1 0.5
7 0.5 1
8 0 0.5
9 0.5 0.5

Here is the modification of the MSH file for the elements (element.txt):

13 3 7 9 6
14 6 9 5 4
15 7 2 8 9
16 9 8 1 5


I wrote a code in Python that could generate the same quad mesh (generated in GMSH) in FEniCS without using dolfin-converter. Now open Python. Copy and paste this:

file = open('meshfile.py', 'w')

file.write('from dolfin import *;\n')
file.write('mesh = Mesh();\n')
file.write('editor = MeshEditor();\n')
file.write("editor.open(mesh,'quadrilateral', 2, 2);\n")
file.write('editor.init_vertices(X);\n') #X = number of the nodes in your mesh. In this example X=9
file.write('editor.init_cells(Y);\n')    #Y = number of the elements in your mesh. In this example Y=4

file.close()


lines = [line.rstrip('\n') for line in open('nodes.txt')]
output = open('meshfile.py', 'a')

for line in lines:
    line = line.split()
    line[0] = int(line[0]) - 1

    output.write('editor.add_vertex({},{},{});\n'.format(line[0], line[1], line[2]))

output.close()


lines = [line.rstrip('\n') for line in open('element.txt')]
output = open('meshfile.py', 'a')

for line in lines:
    line = line.split()
    line[0] = int(line[0]) - Z     #Z = The first number for the elements start with 3 in the second column in MSH file
                                   # In this example Z=13

    for i in range (1,5):

        line[i] =int(line[i]) - 1

        # subtract 1 from all of the components in the rest of the columns
    output.write('editor.add_cell({},{},{},{},{});\n'.format(line[0], line[1], line[2], line[4], line[3]))

output.close()

file = open('meshfile.py', 'a')

file.write('editor.close()\n')
file.write("File('mesh22.pvd') << mesh\n")
file.close()

The above file creates a file called: meshfile.py. Here is the file:

from dolfin import *;
mesh = Mesh();
editor = MeshEditor();
editor.open(mesh,'quadrilateral', 2, 2);
editor.init_vertices(9);
editor.init_cells(4);
editor.add_vertex(0,0,0);
editor.add_vertex(1,0,1);
editor.add_vertex(2,1,1);
editor.add_vertex(3,1,0);
editor.add_vertex(4,0.5,0);
editor.add_vertex(5,1,0.5);
editor.add_vertex(6,0.5,1);
editor.add_vertex(7,0,0.5);
editor.add_vertex(8,0.5,0.5);
editor.add_cell(0,2,6,5,8);
editor.add_cell(1,5,8,3,4);
editor.add_cell(2,6,1,8,7);
editor.add_cell(3,8,7,4,0);
editor.close()
File('mesh22.pvd') << mesh


Just open it and run it! A new file is generated in PVD.format in the same folder. Open it with Paraview:



Here you go! I would like to say that this method could be implemented for way more complicated mesh. For example I made the below mesh with the same method in FEniCS:


I hope it was helpful!
Leo



Please login to add an answer/comment or follow this question.

Similar posts:
Search »