Tutorial 2

Transformations, extruded geometries, volumes

import gmsh

If ARGS is passed to gmsh.initialize(), Gmsh will parse the command line in the same way as the standalone Gmsh app:

gmsh.initialize(append!(["gmsh"], ARGS))
gmsh.model.add("t2")

Copied from Tutorial 1:

lc = 1e-2
gmsh.model.geo.addPoint(0, 0, 0, lc, 1)
gmsh.model.geo.addPoint(.1, 0,  0, lc, 2)
gmsh.model.geo.addPoint(.1, .3, 0, lc, 3)
gmsh.model.geo.addPoint(0, .3, 0, lc, 4)
gmsh.model.geo.addLine(1, 2, 1)
gmsh.model.geo.addLine(3, 2, 2)
gmsh.model.geo.addLine(3, 4, 3)
gmsh.model.geo.addLine(4, 1, 4)
gmsh.model.geo.addCurveLoop([4, 1, -2, 3], 1)
gmsh.model.geo.addPlaneSurface([1], 1)
gmsh.model.geo.synchronize()
gmsh.model.addPhysicalGroup(0, [1, 2], 1)
gmsh.model.addPhysicalGroup(1, [1, 2], 2)
gmsh.model.addPhysicalGroup(2, [1], 6)
gmsh.model.setPhysicalName(2, 6, "My surface")

We can then add new points and curves in the same way as we did in t1.jl:

gmsh.model.geo.addPoint(0, .4, 0, lc, 5)
gmsh.model.geo.addLine(4, 5, 5)
5

But Gmsh also provides tools to transform (translate, rotate, etc.) elementary entities or copies of elementary entities. Geometrical transformations take a vector of pairs of integers as first argument, which contains the list of entities, represented by (dimension, tag) pairs. For example, the point 5 (dimension=0, tag=5) can be moved by 0.02 to the left (dx=-0.02, dy=0, dz=0) with

gmsh.model.geo.translate([(0, 5)], -0.02, 0, 0)

And it can be further rotated by -Pi/4 around (0, 0.3, 0) (with the rotation along the z axis) with:

gmsh.model.geo.rotate([(0, 5)], 0,0.3,0, 0,0,1, -pi/4)

Note that there are no units in Gmsh: coordinates are just numbers - it's up to the user to associate a meaning to them.

Point 3 can be duplicated and translated by 0.05 along the y axis by using the copy() function, which takes a vector of (dim, tag) pairs as input, and returns another vector of (dim, tag) pairs:

ov = gmsh.model.geo.copy([(0, 3)])
gmsh.model.geo.translate(ov, 0, 0.05, 0)

The new point tag is available in ov[0][1], and can be used to create new lines:

gmsh.model.geo.addLine(3, ov[1][2], 7)
gmsh.model.geo.addLine(ov[1][2], 5, 8)
gmsh.model.geo.addCurveLoop([5,-8,-7,3], 10)
gmsh.model.geo.addPlaneSurface([10], 11)
11

In the same way, we can translate copies of the two surfaces 1 and 11 to the right with the following command:

ov = gmsh.model.geo.copy([(2, 1), (2, 11)])
gmsh.model.geo.translate(ov, 0.12, 0, 0)
println("New surfaces ", ov[1][2], " and ", ov[2][2])
New surfaces 12 and 17

Volumes are the fourth type of elementary entities in Gmsh. In the same way one defines curve loops to build surfaces, one has to define surface loops (i.e. shells) to build volumes. The following volume does not have holes and thus consists of a single surface loop:

gmsh.model.geo.addPoint(0., 0.3, 0.12, lc, 100)
gmsh.model.geo.addPoint(0.1, 0.3, 0.12, lc, 101)
gmsh.model.geo.addPoint(0.1, 0.35, 0.12, lc, 102)
102

We would like to retrieve the coordinates of point 5 to create point 103, so we synchronize the model, and use getValue()

gmsh.model.geo.synchronize()
xyz = gmsh.model.getValue(0, 5, [])
gmsh.model.geo.addPoint(xyz[1], xyz[2], 0.12, lc, 103)
103
gmsh.model.geo.addLine(4, 100, 110)
gmsh.model.geo.addLine(3, 101, 111)
gmsh.model.geo.addLine(6, 102, 112)
gmsh.model.geo.addLine(5, 103, 113)
gmsh.model.geo.addLine(103, 100, 114)
gmsh.model.geo.addLine(100, 101, 115)
gmsh.model.geo.addLine(101, 102, 116)
gmsh.model.geo.addLine(102, 103, 117)
117
gmsh.model.geo.addCurveLoop([115, -111, 3, 110], 118)
gmsh.model.geo.addPlaneSurface([118], 119)
gmsh.model.geo.addCurveLoop([111, 116, -112, -7], 120)
gmsh.model.geo.addPlaneSurface([120], 121)
gmsh.model.geo.addCurveLoop([112, 117, -113, -8], 122)
gmsh.model.geo.addPlaneSurface([122], 123)
gmsh.model.geo.addCurveLoop([114, -110, 5, 113], 124)
gmsh.model.geo.addPlaneSurface([124], 125)
gmsh.model.geo.addCurveLoop([115, 116, 117, 114], 126)
gmsh.model.geo.addPlaneSurface([126], 127)
127
gmsh.model.geo.addSurfaceLoop([127, 119, 121, 123, 125, 11], 128)
gmsh.model.geo.addVolume([128], 129)
129

When a volume can be extruded from a surface, it is usually easier to use the extrude() function directly instead of creating all the points, curves and surfaces by hand. For example, the following command extrudes the surface 11 along the z axis and automatically creates a new volume (as well as all the needed points, curves and surfaces). As expected, the function takes a vector of (dim, tag) pairs as input as well as the translation vector, and returns a vector of (dim, tag) pairs as output:

ov2 = gmsh.model.geo.extrude([ov[2]], 0, 0, 0.12)
6-element Array{Tuple{Int32,Int32},1}:
 (2, 151)
 (3, 130)
 (2, 138)
 (2, 142)
 (2, 146)
 (2, 150)

Mesh sizes associated to geometrical points can be set by passing a vector of (dim, tag) pairs for the corresponding points:

gmsh.model.geo.mesh.setSize([(0,103), (0,105), (0,109), (0,102), (0,28),
                             (0, 24), (0,6), (0,5)], lc * 3)

We finish by synchronizing the data from the built-in CAD kernel with the Gmsh model:

gmsh.model.geo.synchronize()

We group volumes 129 and 130 in a single physical group with tag 1 and name "The volume":

gmsh.model.addPhysicalGroup(3, [129,130], 1)
gmsh.model.setPhysicalName(3, 1, "The volume")
gmsh.model.mesh.generate(3)
Info    : Meshing 1D...
Info    : [  0%] Meshing curve 1 (Line)
Info    : [ 10%] Meshing curve 2 (Line)
Info    : [ 10%] Meshing curve 3 (Line)
Info    : [ 10%] Meshing curve 4 (Line)
Info    : [ 20%] Meshing curve 5 (Line)
Info    : [ 20%] Meshing curve 7 (Line)
Info    : [ 20%] Meshing curve 8 (Line)
Info    : [ 30%] Meshing curve 13 (Line)
Info    : [ 30%] Meshing curve 14 (Line)
Info    : [ 30%] Meshing curve 15 (Line)
Info    : [ 40%] Meshing curve 16 (Line)
Info    : [ 40%] Meshing curve 18 (Line)
Info    : [ 40%] Meshing curve 19 (Line)
Info    : [ 50%] Meshing curve 20 (Line)
Info    : [ 50%] Meshing curve 110 (Line)
Info    : [ 50%] Meshing curve 111 (Line)
Info    : [ 60%] Meshing curve 112 (Line)
Info    : [ 60%] Meshing curve 113 (Line)
Info    : [ 60%] Meshing curve 114 (Line)
Info    : [ 70%] Meshing curve 115 (Line)
Info    : [ 70%] Meshing curve 116 (Line)
Info    : [ 70%] Meshing curve 117 (Line)
Info    : [ 80%] Meshing curve 131 (Line)
Info    : [ 80%] Meshing curve 132 (Line)
Info    : [ 80%] Meshing curve 133 (Line)
Info    : [ 90%] Meshing curve 134 (Line)
Info    : [ 90%] Meshing curve 136 (Line)
Info    : [ 90%] Meshing curve 137 (Line)
Info    : [100%] Meshing curve 141 (Line)
Info    : [100%] Meshing curve 145 (Line)
Info    : Done meshing 1D (Wall 0.00825501s, CPU 0.008253s)
Info    : Meshing 2D...
Info    : [  0%] Meshing surface 1 (Plane, Frontal-Delaunay)
Info    : [ 10%] Meshing surface 11 (Plane, Frontal-Delaunay)
Info    : [ 20%] Meshing surface 12 (Plane, Frontal-Delaunay)
Info    : [ 30%] Meshing surface 17 (Plane, Frontal-Delaunay)
Info    : [ 30%] Meshing surface 119 (Plane, Frontal-Delaunay)
Info    : [ 40%] Meshing surface 121 (Plane, Frontal-Delaunay)
Info    : [ 50%] Meshing surface 123 (Plane, Frontal-Delaunay)
Info    : [ 50%] Meshing surface 125 (Plane, Frontal-Delaunay)
Info    : [ 60%] Meshing surface 127 (Plane, Frontal-Delaunay)
Info    : [ 70%] Meshing surface 138 (Surface, Frontal-Delaunay)
Info    : [ 80%] Meshing surface 142 (Surface, Frontal-Delaunay)
Info    : [ 80%] Meshing surface 146 (Surface, Frontal-Delaunay)
Info    : [ 90%] Meshing surface 150 (Surface, Frontal-Delaunay)
Info    : [100%] Meshing surface 151 (Plane, Frontal-Delaunay)
Info    : Done meshing 2D (Wall 0.0401073s, CPU 0.040101s)
Info    : Meshing 3D...
Info    : 3D Meshing 2 volumes with 2 connected components
Info    : Tetrahedrizing 317 nodes...
Info    : Done tetrahedrizing 325 nodes (Wall 0.00300734s, CPU 0.003007s)
Info    : Reconstructing mesh...
Info    :  - Creating surface mesh
Info    :  - Identifying boundary edges
Info    :  - Recovering boundary
Info    : Done reconstructing mesh (Wall 0.00612629s, CPU 0.006121s)
Info    : Found volume 129
Info    : It. 0 - 0 nodes created - worst tet radius 2.80713 (nodes removed 0 0)
Info    : 3D refinement terminated (1477 nodes total):
Info    :  - 0 Delaunay cavities modified for star shapeness
Info    :  - 0 nodes could not be inserted
Info    :  - 1254 tetrahedra created in 0.00336385 sec. (372787 tets/s)
Info    : Tetrahedrizing 316 nodes...
Info    : Done tetrahedrizing 324 nodes (Wall 0.00331685s, CPU 0.003317s)
Info    : Reconstructing mesh...
Info    :  - Creating surface mesh
Info    :  - Identifying boundary edges
Info    :  - Recovering boundary
Info    : Done reconstructing mesh (Wall 0.00654139s, CPU 0.006499s)
Info    : Found volume 130
Info    : It. 0 - 0 nodes created - worst tet radius 2.82349 (nodes removed 0 0)
Info    : 3D refinement terminated (1476 nodes total):
Info    :  - 0 Delaunay cavities modified for star shapeness
Info    :  - 0 nodes could not be inserted
Info    :  - 1239 tetrahedra created in 0.00313434 sec. (395298 tets/s)
Info    : Done meshing 3D (Wall 0.049435s, CPU 0.045416s)
Info    : Optimizing mesh...
Info    : Optimizing volume 129
Info    : Optimization starts (volume = 0.000639411) with worst = 0.0417493 / average = 0.722401:
Info    : 0.00 < quality < 0.10 :         6 elements
Info    : 0.10 < quality < 0.20 :        13 elements
Info    : 0.20 < quality < 0.30 :        20 elements
Info    : 0.30 < quality < 0.40 :        40 elements
Info    : 0.40 < quality < 0.50 :        44 elements
Info    : 0.50 < quality < 0.60 :       103 elements
Info    : 0.60 < quality < 0.70 :       213 elements
Info    : 0.70 < quality < 0.80 :       383 elements
Info    : 0.80 < quality < 0.90 :       286 elements
Info    : 0.90 < quality < 1.00 :       146 elements
Info    : 39 edge swaps, 0 node relocations (volume = 0.000639411): worst = 0.204 / average = 0.739076 (Wall 0.000631809s, CPU 0.000632s)
Info    : 40 edge swaps, 0 node relocations (volume = 0.000639411): worst = 0.304861 / average = 0.739687 (Wall 0.00070421s, CPU 0.000704s)
Info    : No ill-shaped tets in the mesh :-)
Info    : 0.00 < quality < 0.10 :         0 elements
Info    : 0.10 < quality < 0.20 :         0 elements
Info    : 0.20 < quality < 0.30 :         0 elements
Info    : 0.30 < quality < 0.40 :        35 elements
Info    : 0.40 < quality < 0.50 :        47 elements
Info    : 0.50 < quality < 0.60 :       106 elements
Info    : 0.60 < quality < 0.70 :       214 elements
Info    : 0.70 < quality < 0.80 :       383 elements
Info    : 0.80 < quality < 0.90 :       289 elements
Info    : 0.90 < quality < 1.00 :       146 elements
Info    : Optimizing volume 130
Info    : Optimization starts (volume = 0.000639411) with worst = 0.0196513 / average = 0.721123:
Info    : 0.00 < quality < 0.10 :         9 elements
Info    : 0.10 < quality < 0.20 :        14 elements
Info    : 0.20 < quality < 0.30 :        18 elements
Info    : 0.30 < quality < 0.40 :        31 elements
Info    : 0.40 < quality < 0.50 :        47 elements
Info    : 0.50 < quality < 0.60 :       105 elements
Info    : 0.60 < quality < 0.70 :       219 elements
Info    : 0.70 < quality < 0.80 :       362 elements
Info    : 0.80 < quality < 0.90 :       288 elements
Info    : 0.90 < quality < 1.00 :       146 elements
Info    : 40 edge swaps, 0 node relocations (volume = 0.000639411): worst = 0.169257 / average = 0.739275 (Wall 0.000599709s, CPU 0.0006s)
Info    : 43 edge swaps, 0 node relocations (volume = 0.000639411): worst = 0.285488 / average = 0.73987 (Wall 0.00072071s, CPU 0.000721s)
Info    : 44 edge swaps, 0 node relocations (volume = 0.000639411): worst = 0.302373 / average = 0.740221 (Wall 0.000791511s, CPU 0.000792s)
Info    : No ill-shaped tets in the mesh :-)
Info    : 0.00 < quality < 0.10 :         0 elements
Info    : 0.10 < quality < 0.20 :         0 elements
Info    : 0.20 < quality < 0.30 :         0 elements
Info    : 0.30 < quality < 0.40 :        24 elements
Info    : 0.40 < quality < 0.50 :        58 elements
Info    : 0.50 < quality < 0.60 :       104 elements
Info    : 0.60 < quality < 0.70 :       211 elements
Info    : 0.70 < quality < 0.80 :       366 elements
Info    : 0.80 < quality < 0.90 :       293 elements
Info    : 0.90 < quality < 1.00 :       146 elements
Info    : Done optimizing mesh (Wall 0.00328115s, CPU 0.003281s)
Info    : 1535 nodes 5438 elements

We finally generate and save the mesh:

gmsh.write("t2.msh")
Info    : Writing 't2.msh'...
Info    : Done writing 't2.msh'

Note that, if the transformation tools are handy to create complex geometries, it is also sometimes useful to generate the flat geometry, with an explicit representation of all the elementary entities.

With the built-in CAD kernel, this can be achieved by saving the model in the Gmsh Unrolled GEO format:

gmsh.write("t2.geo_unrolled");

With the OpenCASCADE CAD kernel, unrolling the geometry can be achieved by exporting in the `OpenCASCADE BRep' format:

gmsh.write("t2.brep");

(OpenCASCADE geometries can also be exported as STEP files.)

It is important to note that Gmsh never translates geometry data into a common representation: all the operations on a geometrical entity are performed natively with the associated CAD kernel. Consequently, one cannot export a geometry constructed with the built-in kernel as an OpenCASCADE BRep file; or export an OpenCASCADE model as an Unrolled GEO file.

gmsh.finalize()

This page was generated using Literate.jl.