Local:Gmsh tutorial

From AMCGMedia

Jump to: navigation, search

Contents

AMCG Gmsh tutorial

This document is a tutorial on the GMSH mesh generator. It is aimed towards complete beginners; only some basic knowledge of the Linux terminal and a text editor is assumed. We first define what a mesh is and then introduce the reader to the basics of the GMSH graphical user interface. A basic, two-dimensional, geometry is then constructed within GMSH and a mesh is constructed. A more complicated three-dimesional annulus is also constructed and meshed, demonstrating some more advanced features of GMSH.

Having mastered the basic usage of the graphical user interface, users are introduced to generating simple meshes on the sphere. Knowledge is further built to produce meshes of realistic domains of the oceans to include boundaries extracted from a high resolution shorelines database.

Online Gmsh tutorial

A two dimensional example

This tutorial illustrates the process of meshing a square domain and specifying boundary flags on it.

Getting started

Run gmsh on the command line:

gmsh

The following windows appear:

Gmsh start.png

Setting up the geometry

Choose "Elementary entities" then "Add", "New", "Point"

It is now possible to set points either by clicking the main window or by typing coordinates in the pop-up box. It is generally easier and more accurate to use the latter approach. After adding the points (0,0), (1,0), (0,1) and (1,1) we have:

Gmsh points.png

Now choose "Straight line" and select each pair of points in turn until the outside lines of the square are complete. This figure illustrates the during the adding of the third side:

Gmsh lines.png

Finally, we need to specify the plane surface which we will mesh. Click "Plane surface" and then select any line. The whole edge of the square will be highlighted and you press "e" to complete the selection.

Physical groups: boundaries and regions

In order to apply boundary conditions it is necessary to specify "physical groups" to which the boundaries belong. Since gmsh will only export to the .msh file those elements which are associated with a physical entity, it is also necessary to identify the interior of the domain with a physical group.

Click on "Geometry" to return to the first menu and then select "Physical group", "add", line. Now suppose that we want to run a driven cavity. We'll need one boundary condition on the top and, depending on whether we use free or no slip boundary conditions we might need one boundary for all the rest of the sides together or one for the sides and a different one for the bottom. We therefore make one physical group for the top of the square, one for the sides together and one for the bottom.

Click on the top line and then press "e" to end the selection and form the first physical group. Next do this for the sides. The following figure shows the selected sides immediately before pressing "e":

Gmsh physical.png

Finally select the bottom of the box as a physical group. Next select "Surface" and select the dashed cross in the centre of the figure to identify the interior of the domain as a physical group.

Editing the geometry by hand

Gmsh writes all the elementary entities and physical groups to a human-editable text file with the suffix .geo. This can be useful for cleaning up the results of using the gui, adding comments and, as we shall see later, for more complex options which are difficult or impossible to do via the GUI.

Return to the first menu by clicking on "Geometry". Next select "Edit" and a text editor will open containing the current geometry. In this case the file is as follows:

Point(1) = {0,0,0,0.1};
Point(2) = {0,1,0,0.1};
Point(3) = {1,1,0,0.1};
Point(4) = {1,0,0,0.1};
Line(1) = {3,4};
Line(2) = {4,1};
Line(3) = {1,2};
Line(4) = {2,3};
Line Loop(5) = {1,2,3,4};
Plane Surface(6) = {5};
Physical Line(7) = {4};
Physical Line(8) = {3,1};
Physical Line(9) = {2};
Physical Surface(10) = {6};

The format is basically pretty simple. For most objects the syntax is:

New_entity(id number) = {list of component entities}

So, the fifth line above says that line 1 is composed of points 3 and 4. The exception is Point the format of which is {x, y, z, dx} where dx is the mesh spacing at that point. gmsh determines the mesh spacing during mesh generation by interpolating between these points.

Entity numbers are only required to be unique within that entity type - as evidenced by the reuse of the numbers 1-4 for points and lines above. Suppose we would like different boundary markers (these are the numbers we will access boundaries by in fluidity) and that we would like to double the resolution. We'll also add some comments for the benefit of our fellow creatures:

Point(1) = {0,0,0,0.05};
Point(2) = {0,1,0,0.05};
Point(3) = {1,1,0,0.05};
Point(4) = {1,0,0,0.05};
Line(1) = {3,4};
Line(2) = {4,1};
Line(3) = {1,2};
Line(4) = {2,3};
Line Loop(5) = {1,2,3,4};
Plane Surface(6) = {5};
// Top of the box
Physical Line(666) = {4};
// Box sides
Physical Line(333) = {3,1};
// Box bottom
Physical Line(444) = {2};
// This is just to ensure all the interior
// elements get written out. 
Physical Surface(10) = {6};

Now save the file and quit the editor. Now click "Reload" to load the changes you just made into the gui.

Producing a mesh

To produce the mesh simply select "Mesh" from the dropdown menu and click "2D". The result looks something like this:

Gmsh 2dmesh.png

Now select "save mesh" from the "file" menu to save your masterpiece and you are done!

Converting the mesh to triangle for use in fluidity

Fluidity can directly read gmsh output if mesh/from_file/format(gmsh) is specified in the options file instead of format(triangle). Otherwise, it is possible convert the mesh into triangle format. This is easily accomplished using the gmsh2triangle script. The following example assumes that your mesh is in the src directory of a test case. In other cases you'll need to adjust paths accordingly:

../../tools/gmsh2triangle --2d src/driven_cavity.msh

In case you want to convert a 3D mesh, type:

../../tools/gmsh2triangle src/driven_cavity.msh

Extruding a pseudo-2D mesh

Many fluidity users run pseudo-2D simulations in a very thin 3D domain. gmsh supports this sort of mesh using extrusion. Starting from the geometry above we can select "Elementary entities", "Extrude", "Translate". Set the Z component to the width of the extruded dimension (I chose 0.01) and click on the surface to extrude followed by "e".

Editing the extrusion

The extrusion commands above only extrude the domain, to cause the mesh generator to produce an extruded mesh in which the surface triangles on the front and back of the slice line up we have to edit the .geo file a little.

The extrusion is the command:

Extrude {0,0,0.01} {
 Surface{6};
}

(your surface ID may be different). You need to change this to:

Extrude {0,0,0.01} {
 Surface{6}; Layers{1};
}

which specifies that there is 1 layer of elements in the Z direction (specifying a different number of layers works in the obvious way).

Setting the physical groups

For a 3d mesh, including this pseudo-2D domain, the boundaries are obviously faces and the interior is a volume. Otherwise it's just like the 2D case. Don't forget to specify the boundaries on the front and back of the domain!

Generating the mesh

Obviously this time you use the "3D" button on the meshing menu which results in the following mesh:

Gmsh-pseudo2d.png

A structured mesh

One can also use extrusion to generate a structured mesh. Rather than step through the gui, which is the same process as for the pseudo-2D mesh, we can just detail the steps:

  1. Set up a point at the origin.
  2. Extrude in the direction (1,0,0).
  3. Extrude in the direction (0,1,0).
  4. Extrude in the direction (0,0,1).
  5. Set the physical boundaries.

This approach results in the following .geo file:

Point(1) = {0,0,0,0.1};
Extrude {1,0,0} {
  Point{1}; Layers{5};
}
Extrude {0,1,0} {
  Line{1}; Layers{5};
}
Extrude {0,0,0.1} {
  Surface{5}; Layers{5};
}
Physical Surface(28) = {27,5};
Physical Surface(29) = {14,22};
Physical Surface(30) = {26};
Physical Surface(31) = {18};

and the following structured mesh:

Gmsh-3d.png

Hexahedral mesh

To generate a hexahedral mesh the command 'Recombine' is added as a part of line and surface extrusions. Taking the structured tetrahedral example above the line and surface extrusions are edited like so:

Extrude {0,1,0} {
  Line{1}; Layers{5}; Recombine;
}
Extrude {0,0,1} {
  Surface{5}; Layers{5}; Recombine;
}

A three dimensional structured example

The following tutorial explains how to generate a structured 3D annulus mesh using gmsh.

Getting started

Run gmsh on the command line:

 gmsh

Create a new gmsh geometry file, by selecting "New" from the "File" menu and choosing an appropriate filename.

Setting up the geometry: Creating a radial line

We will start by adding a radial line. In thermally driven rotating annulus experiments it is often a good idea to start with resolution concentrated towards the annulus boundaries. We will concentrate resolution towards the inner and outer walls. This can be achieved by setting the "Characteristic lengths" of points.

From the gmsh control window, select "Geometry" (or press "G"). Select "Elementary entities", "Add", "New", "Point". Create three points as follows (by entering the values in the relevant boxes and clicking "Add").


x = 2.5, y = 0.0, z = 0.0, Characteristic length = 0.1

x = 5.25, y = 0.0, z = 0.0, Characteristic length = 1.0

x = 8.0, y = 0.0, z = 0.0, Characteristic length = 0.1


The snapping grid spacing can be left at 0.1 in all directions.


Annulus1.png


Select "Geometry", "Elementary entities", "Add", "New", "Straight line", and join the three points with two lines to form an annulus radial.


Annulus2.png

Setting up the geometry: Forming an annulus with extrusions

This radial line can now be used to form an annulus, by first extruding in the vertical to form a vertical slice, and then using a rotational extusion.

Select "Geometry", "Elementary entities", "Extrude", "Translate", "Line". Select the two lines forming the annulus radial, and enter the following values for the extrusion:

x = 0.0, y = 0.0, z = 14.0


Annulus3.png


Select "Geometry", "Elementary entities", "Extrude", "Rotate", "Surface". Select the two surfaces in the annulus slice, and enter the following values for the extrusion:

Axis point x = 0.0, Axis point y = 0.0, Axis point z = 0.0 Axis direction x = 0.0, Axis direction y = 0.0, Axis direction z = 1.0 Angle = Pi/2

Repeat this four times to form an annulus. Note that on the final extrusion a true annulus geometry is created (rather than an annulus with two nearby radial walls).


Annulus4.png

Physical groups

As in earlier examples physical surfaces and volumes must be specified. Select "Geometry", "Physical groups", "Add", "Surface". Select the 8 surfaces comprising the top of the annulus and press "e". Repeat for the bottom of the annulus, the inner wall, and the outer wall (in order).


Annulus5.png


Select "Geometry", "Physical groups", "Add", "Volume", select all volumes comprising the annulus and press "e".

Final customisation of the geometry

At this point the geometry can be used to generate a mesh. However, as we are aiming to create a structed mesh, the extusions performed earlier must be converted to layered extrusions.

From the gmsh control window select "Geometry", "Edit". This will open the gmsh geometry file in a text editor. Make the alterations highlighted in red below and save the edited file.

 // Gmsh project created on Fri Feb 08 14:43:37 2008
 Point(1) = {2.5,0.0,0.0,0.1};
 Point(2) = {5.25,0.0,0.0,1.0};
 Point(3) = {8.0,0.0,0.0,0.1};
 Line(1) = {1,2};
 Line(2) = {2,3};
 Extrude {0,0,14} {
   Line{1,2}; Layers{25};
 }
 Extrude {{0,0,1}, {0,0,0}, Pi/2} {
   Surface{10,6}; Layers{30};
 }Extrude {{0,0,1}, {0,0,0}, Pi/2} {
   Surface{32,54}; Layers{30};
 }
 Extrude {{0,0,1}, {0,0,0}, Pi/2} {
   Surface{76,98}; Layers{30};
 }
 Extrude {{0,0,1}, {0,0,0}, Pi/2} {
   Surface{120,142}; Layers{30};
 }
 // Top
 Physical Surface(185) = {41,19,151,172,129,107,63,85};
 // Bottom
 Physical Surface(186) = {180,49,93,137,115,71,159,27};
 // Inner wall
 Physical Surface(187) = {53,184,141,97};
 // Outer wall
 Physical Surface(189) = {111,67,23,155};
 Physical Volume(190) = {1,2,8,7,6,5,4,3};

This converts the vertical extrusion to a layered extrusion with 25 intervals (equivalent to a "zone" command with 25 intervals in GEM) and the rotational extrusions to layered rotational extrusions with 30 intervals per pi/2 of arc. The comments before the physical surface definitions are a good idea, as the physical surface numbers in brackets, (185) - (189), are the IDs that will be entered in Diamond to assign boundary conditions.

Select "Geometry", "Reload" to apply your changes. If gmsh displays an error then there is a mistake somewhere in your gmsh geometry file.

Producing a mesh

From the gmsh control window select "Mesh" and click "3D" to generate the mesh.


Annulus6.png


This mesh can now be exported by selecting "Save Mesh" from the "File" menu, and can then be converted to triangle format for use with Fluidity.

This page was last modified on 8 February 2013, at 14:52. This page has been accessed 19,798 times.