Tips of using gmsh

This blog discusses some tips of using the gmsh.

The powerful aspect of gmsh is to create the mesh data sets as needed, with these mesh data, you can then started to add physical dynamicals or start to do the simulation and then processes the data. It is really good for creating some data sets for testing as needed.

Create geometry

In the add geometry section, just start from adding the points. Althoug the gmsh provides all kinds of geometrys such as rectangle or circle, but do not use them if you want to create the complex geometry such as containing a hole somewhere. For example, if we add a rectangle direactly, the surface is fixed within that rectangle, you can not customize it.

Fill in the surface

After adding the points and lines, the next important step is to apply the Surface filling operation, we just need to select the boundy and using the e to end the selection. It is also convenient to select the boundry of the inner hole in this section.

Adding the mesh

After applying the surface filling, we can then adding the mesh to the current surface. For the 2d geometry, just click the Mesh->2D.

The operation of refine mesh is important, we need to go to Mesh->Define->Size at points to control the refinement of the mesh. In the element size tab, we can update the assocaited values to control the granularity of the mesh. We need to select the points in order to control the scope of this value. If we selected one or multiple points, and then update this

Using script to do the interaction

It seems that the advanced users prefer to update the scripts to control how gmsh generate things, click the edit script can open that script. It may need some time to get familiar with how the grammar of the script works.

print out all surfaces:

Printf("Debug surfaces", Surface {:});

Potential issues

“Perhaps you created a higher order mesh?”

do not click saving all elements when exporting the msh file from the gmsh

“output a 2d data set into the msh file”

Openfoam can not proberly process the 2d file, there are some issues when converting the mesh to the openfoam data set.

“Perhaps you have not exported the 3D elements?”

There is also issue if we do not add a volume, this is tricky! Remember to add volume to avoid this issue.

Other tips

Just using the File -> Delete to remove all things in the current workspace.

Exmaples

Remember to use the gmshToFoam command to do that, before having this, adding execute the etc bashrc file

2d case with a circle hole in it

//+
Point(1) = {0,0,0,1.0};
//+
Point(2) = {0,1,0,1.0};
//+
Point(3) = {1,0,0,1.0};
//+
Point(4) = {1,1,0,1.0};
//+
Line(1) = {2, 1};
//+
Line(2) = {1, 3};
//+
Line(3) = {3, 4};
//+
Line(4) = {4, 2};
//+
Point(5) = {0.75, 0.5, 0, 1.0};
//+
Point(6) = {0.25, 0.5, 0, 1.0};
//+
Point(7) = {0.5, 0.5, 0, 1.0};
//+
Point(8) = {0.5, 0.25, 0, 1.0};
//+
Point(9) = {0.5, 0.75, -0, 1.0};//+
Circle(5) = {6, 7, 8};
//+
Circle(6) = {8, 7, 5};
//+
Circle(7) = {5, 7, 9};
//+
Circle(8) = {9, 7, 6};
//+
Curve Loop(1) = {1, 2, 3, 4};
//+
Curve Loop(2) = {8, 5, 6, 7};
//+
Surface(1) = {1, 2};
//+
MeshSize {2, 1, 3, 4, 9, 6, 8, 5} = 1;
//+
MeshSize {2, 1, 3, 4} = 0.03;

The updated version with the extrude operation to become a 3d volume and the selected surface with specific names on it

//+
Point(1) = {0,0,0,1.0};
//+
Point(2) = {0,1,0,1.0};
//+
Point(3) = {1,0,0,1.0};
//+
Point(4) = {1,1,0,1.0};
//+
Line(1) = {2, 1};
//+
Line(2) = {1, 3};
//+
Line(3) = {3, 4};
//+
Line(4) = {4, 2};
//+
Point(5) = {0.75, 0.5, 0, 1.0};
//+
Point(6) = {0.25, 0.5, 0, 1.0};
//+
Point(7) = {0.5, 0.5, 0, 1.0};
//+
Point(8) = {0.5, 0.25, 0, 1.0};
//+
Point(9) = {0.5, 0.75, -0, 1.0};//+
Circle(5) = {6, 7, 8};
//+
Circle(6) = {8, 7, 5};
//+
Circle(7) = {5, 7, 9};
//+
Circle(8) = {9, 7, 6};
//+
Curve Loop(1) = {1, 2, 3, 4};
//+
Curve Loop(2) = {8, 5, 6, 7};
//+
Surface(1) = {1, 2};
//+
MeshSize {2, 1, 3, 4, 9, 6, 8, 5} = 0.03;


Physical Volume("internal") = {1};
Extrude {0, 0, 0.1} {
Surface{1};
Layers{1};
Recombine;
}

//+
Physical Surface("inlet", 51) = {21};
//+
Physical Surface("outlet", 52) = {29};
//+
Physical Surface("frontandback", 53) = {50, 1};
//+
Physical Surface("top", 54) = {33};
//+
Physical Surface("bottom", 55) = {25};
//+
Physical Surface("cylinderwall", 56) = {49, 45, 41, 37};

Another complicated mesh, two circles and one square, remember to choose the surface plan before extrude, after extruding, also selelct the volume

//+
Point(1) = {0,0,0,1.0};
//+
Point(2) = {0,2,0,1.0};
//+
Point(3) = {6,0,0,1.0};
//+
Point(4) = {6,2,0,1.0};
//+
Line(1) = {2, 1};
//+
Line(2) = {1, 3};
//+
Line(3) = {3, 4};
//+
Line(4) = {4, 2};
//+
Point(5) = {0.75, 0.5, 0, 1.0};
//+
Point(6) = {0.25, 0.5, 0, 1.0};
//+
Point(7) = {0.5, 0.5, 0, 1.0};
//+
Point(8) = {0.5, 0.25, 0, 1.0};
//+
Point(9) = {0.5, 0.75, -0, 1.0};
//+
Circle(5) = {6, 7, 8};
//+
Circle(6) = {8, 7, 5};
//+
Circle(7) = {5, 7, 9};
//+
Circle(8) = {9, 7, 6};

//+
Curve Loop(1) = {1, 2, 3, 4};


//+
Point(10) = {0.75, 1.5, 0, 1.0};
//+
Point(11) = {0.25, 1.5, 0, 1.0};
//+
Point(12) = {0.5, 1.5, 0, 1.0};
//+
Point(13) = {0.5, 1.25, 0, 1.0};
//+
Point(14) = {0.5, 1.75, -0, 1.0};
//+
Circle(45) = {14, 12, 11};
//+
Circle(46) = {11, 12, 13};
//+
Circle(47) = {13, 12, 10};
//+
Circle(48) = {10, 12, 14};

//+
Point(15) = {0.4, 1.1, 0, 1.0};
//+
Point(16) = {0.6, 1.1, 0, 1.0};
//+
Point(17) = {0.4, 0.9, 0, 1.0};
//+
Point(18) = {0.6, 0.9, 0, 1.0};
//+
Line(49) = {15, 17};
//+
Line(50) = {17, 18};
//+
Line(51) = {18, 16};
//+
Line(52) = {16, 15};
//+
Curve Loop(2) = {45, 46, 47, 48};
//+
Curve Loop(3) = {49, 50, 51, 52};
//+
Curve Loop(4) = {8, 5, 6, 7};
//+
Plane Surface(1) = {1, 2, 3, 4};
//+
Extrude {0, 0, 0.25} {
Surface{1};
}
//+
Physical Surface("left", 135) = {73};
//+
Physical Surface("right", 136) = {81};
//+
Physical Surface("top", 137) = {85};
//+
Physical Surface("bottom", 138) = {77};
//+
Physical Surface("frontandback", 139) = {134, 1};
//+
MeshSize {19, 2, 1, 20, 28, 4, 3, 24, 55, 15, 17, 64, 60, 18, 56, 16, 71, 9, 83, 6, 78, 8, 73, 5, 35, 14, 11, 47, 42, 13, 37, 10} = 0.05;
//+
Physical Surface("cylinderwall", 140) = {133, 129, 125, 121, 117, 113, 109, 105, 101, 97, 93, 89};
//+
Physical Volume("internal", 141) = {1};

Another example

//+
Point(1) = {0,0,0,1.0};
//+
Point(2) = {0,1,0,1.0};
//+
Point(3) = {1,0,0,1.0};
//+
Point(4) = {1,1,0,1.0};
//+
Line(1) = {2, 1};
//+
Line(2) = {1, 3};
//+
Line(3) = {3, 4};
//+
Line(4) = {4, 2};
//+
Point(5) = {0.25, 0.4, 0, 1.0};
//+
Point(6) = {0.25, 0.5, 0, 1.0};
//+
Point(7) = {0.25, 0.6, 0, 1.0};
//+
Circle(5) = {7, 6, 5};
//+
Circle(6) = {5, 6, 7};
//+
Curve Loop(1) = {1, 2, 3, 4};
//+
Curve Loop(2) = {5, 6};
//+
Plane Surface(1) = {1, 2};
//+
MeshSize {2, 1, 3, 4, 7, 5} = 0.03;

this is a good tutorial:
https://www.youtube.com/watch?v=kk5DHIZa21k
this is also good
https://www.youtube.com/watch?v=kk5DHIZa21k&t=1255s

A example that create the structured grid for a volume

 //+
Point(1) = {0, 0, 0, 1.0};
//+
Point(2) = {0, 1, 0, 1.0};
//+
Point(3) = {1, 0, 0, 1.0};
//+
Point(4) = {1, 1, 0, 1.0};
//+
Line(1) = {2, 1};
//+
Line(2) = {1, 3};
//+
Line(3) = {3, 4};
//+
Line(4) = {4, 2};
//+
Curve Loop(1) = {1, 2, 3, 4};
//+
Surface(1) = {1};
//+
Transfinite Surface {1} = {2, 4, 3, 1};
//+
Transfinite Curve {1, 3, 4, 2} = 20 Using Progression 1;
//+
Recombine Surface {1};
//+
Extrude {0, 0, 1} {
Surface{1};
}
//+
Transfinite Volume{1};
//+
Transfinite Curve {9, 11, 4, 20, 3, 1, 2, 6, 12, 7, 16} = 20 Using Progression 1;
//+
Transfinite Surface {26};
//+
Transfinite Surface {26} = {5, 6, 10, 14};
//+
Transfinite Surface {13} = {5, 2, 1, 6};
//+
Transfinite Surface {25} = {2, 5, 14, 4};
//+
Transfinite Surface {1} = {2, 1, 3, 4};
//+
Transfinite Surface {21} = {4, 14, 3, 10};
//+
Transfinite Surface {17} = {1, 6, 3, 10};
//+
Transfinite Surface {25} = {2, 5, 14, 4};
//+
Recombine Surface {13};
//+
Recombine Surface {25};
//+
Recombine Surface {17};
//+
Transfinite Surface {26} = {5, 6, 10, 14};
//+
Transfinite Curve {9, 6, 7, 8} = 20 Using Progression 1;
//+
Recombine Surface {26};
//+
Recombine Surface {1, 26, 21};

Notes about adding structured grid for a 2d surface.

click transfinite surface, select surface, select associated point poins.
click transfinite curve, select curve and how many points are needed here.
click mesh 1d and then click mesh 2d to check the results
click the recombine, select the surface.

example of the curve space for using the structured mesh

//+
Point(1) = {0, 0, 0, 1.0};
//+
Point(2) = {3, 0, 0, 1.0};
//+
Point(3) = {3, 2, 0, 1.0};
//+
Point(4) = {3, 2.5, 0, 1.0};
//+
Point(5) = {0, 2.5, 0, 1.0};
//+
Point(6) = {2.5, 2.5, 0, 1.0};
//+
Line(1) = {5, 1};
//+
Line(2) = {1, 2};
//+
Line(3) = {2, 3};
//+
Line(4) = {5, 6};
//+
Circle(5) = {6, 4, 3};
//+
Curve Loop(1) = {1, 2, 3, -5, -4};
//+
Line(6) = {3, 1};
//+
Curve Loop(2) = {4, 5, 6, -1};
//+
Surface(1) = {2};
//+
Transfinite Curve {4, 1, 6, 5} = 10 Using Progression 1;
//+
Transfinite Surface {1} = {5, 1, 3, 6};
//+
Recombine Surface {1};

A example with a small rectangle in the large rectangle

//+
Point(1) = {0, 0, 0, 1.0};
//+
Point(2) = {8, 0, 0, 1.0};
//+
Point(3) = {8, 5, 0, 1.0};
//+
Point(4) = {0, 5, 0, 1.0};

//+
Point(5) = {3, 1.25, 0, 1.0};
//+
Point(6) = {3, 3.75, 0, 1.0};
//+
Line(1) = {6, 5};
//+
Line(2) = {4, 1};
//+
Line(4) = {2, 3};
//+
Line(5) = {3, 4};
//+
Point(7) = {2.9, 1.25, 0, 1.0};
//+
Point(8) = {2.9, 3.75, 0, 1.0};
//+
Line(6) = {7, 8};
//+
Line(7) = {8, 6};


//+
Line(8) = {7, 5};
//+
Line(9) = {1, 2};
//+
Curve Loop(1) = {2, 9, 4, 5};
//+
Curve Loop(2) = {6, 7, 1, -8};
//+
Surface(1) = {1, 2};
//+
Extrude {0, 0, 0.1} {
Surface{1};
Layers{1};
Recombine;
}
//+
MeshSize {9, 4, 10, 1, 5, 7, 25, 26, 8, 6, 34, 30, 14, 2, 18, 3} = 0.05;
//+
Physical Surface("wall", 52) = {50, 42, 38, 46};
//+
Physical Surface("left", 53) = {22};
//+
Physical Surface("right", 54) = {30};
//+
Physical Surface("frontandback", 55) = {51, 1};
//+
Physical Surface("top", 56) = {34};
//+
Physical Surface("bottom", 57) = {26};
//+
Physical Volume("internal", 58) = {1};

References

good tutorial for the gmsh example

https://openfoamwiki.net/index.php/2D_Mesh_Tutorial_using_GMSH

推荐文章