CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Open Source Meshers: Gmsh, Netgen, CGNS, ... (
-   -   Scripted version of "2D Mesh Generation Tutorial for GMSH" (

laubeg April 2, 2013 09:02

Scripted version of "2D Mesh Generation Tutorial for GMSH"

I came across the 2d mesh generation tutorial for gmsh by aeroslacker:
which was very useful as a first step with gmsh/openfoam.

I just didn't like the thought of using both the graphical user interface (GUI) and the Editor so I changed it a tiny bit to make scripting possible. The main difference is the usage of "vector[] = Extrude {...}{...}" instead of "Extrude {...}{...}", where "vector[]" is a vector containing the surfaces and volumes created by the Extrude-command. This way, you get rid of the intransparent numbering of the surfaces.

I also added an optional structuring of the mesh, just as a hint that structuring is possible. Here is all the code:


// sripted version of "2D Mesh Tutorial using GMSH" by CFD-online Member "aeroslacker", taken from
// All numbering counterclockwise from bottom-left corner
Point(1) = {-100, -100, 0, 1e+22};
Point(2) = {100, -100, 0, 1e+22};
Point(3) = {100, 100, 0, 1e+22};
Point(4) = {-100, 100, 0, 1e+22};
Line(1) = {1, 2};                // bottom line
Line(2) = {2, 3};                // right line
Line(3) = {3, 4};                // top line
Line(4) = {4, 1};                // left line
Line Loop(5) = {1, 2, 3, 4};   
// the order of lines in Line Loop is used again in surfaceVector[]
Plane Surface(6) = {5};

/* start optional: structured mesh */
// Transfinite Surface{surface}={edge points}; forces later meshing to contain structured triangles
Transfinite Surface{6} = {1,2,3,4};
Recombine Surface{6}; //combine triangles to quadrangles
/* end optional */

surfaceVector[] = Extrude {0, 0, 10} {
/* surfaceVector contains in the following order:
[0] - front surface (opposed to source surface)
[1] - extruded volume
[2] - bottom surface (belonging to 1st line in "Line Loop (6)")
[3] - right surface (belonging to 2nd line in "Line Loop (6)")
[4] - top surface (belonging to 3rd line in "Line Loop (6)")
[5] - left surface (belonging to 4th line in "Line Loop (6)") */
Physical Surface("front") = surfaceVector[0];
Physical Volume("internal") = surfaceVector[1];
Physical Surface("bottom") = surfaceVector[2];
Physical Surface("right") = surfaceVector[3];
Physical Surface("top") = surfaceVector[4];
Physical Surface("left") = surfaceVector[5];
Physical Surface("back") = {6}; // from Plane Surface (6) ...

// File must end with a free line to avoid errors! So insert line below this comment!

Do you think I should add this "scripted" version of the Tutorial as an optional way to the openfoamwiki?

In my opinion programming geometry has some advantages over using the GUI, especially when you keep in mind, that the .geo-format is more of a programming language (including loops, if/else...) than just a point-list. In my case, one boundary is described by a sine-wave, which is hard to implement via GUI.


wyldckat April 14, 2013 08:32

Greetings Gerrit,

Many thanks for adding this to the wiki page!

Best regards,

All times are GMT -4. The time now is 00:40.