Using Gmsh¶
If you want to create many meshes using Gmsh, you may first generate a mesh manually.
Then you can create a Python script which uses this mesh as a template to quickly create a mesh for a different set of parameters.
Here is a script which shows how to do so. The mesh file (geo) has been enclosed between quotes """
and some of the values for the points coordinates have been substituted with strings that the Python script substitutes with real values.
Note that we use Mesh.CharacteristicLengthFactor = 5.0;
to control the discretisation of the mesh. We also use Physical Volume(1) = {1};
to make sure that the mesh region is labeled starting from region number 1.
mesh = """ cl1 = 1; Point(1) = {$x2$, 0, 0, cl1}; Point(2) = {$x2$, $x2$, 0, cl1}; Point(3) = {0, $x2$, 0, cl1}; Point(4) = {0, $x1$, 0, cl1}; Point(5) = {$x1$, 0, 0, cl1}; Point(6) = {$x0$, $x0$, 0, cl1}; Point(7) = {$x0$, $x1$, 0, cl1}; Point(8) = {$x1$, $x0$, 0, cl1}; Point(9) = {$x2$, 0, $y1$, cl1}; Point(10) = {$x1$, 0, $y1$, cl1}; Point(14) = {$x1$, $x0$, $y1$, cl1}; Point(18) = {$x0$, $x0$, $y1$, cl1}; Point(19) = {$x0$, $x1$, $y1$, cl1}; Point(23) = {0, $x1$, $y1$, cl1}; Point(27) = {0, $x2$, $y1$, cl1}; Point(31) = {$x2$, $x2$, $y1$, cl1}; Line(1) = {1, 5}; Line(2) = {5, 8}; Circle(3) = {8, 6, 7}; Line(4) = {7, 4}; Line(5) = {4, 3}; Line(6) = {3, 2}; Line(7) = {2, 1}; Line(11) = {9, 10}; Line(12) = {10, 14}; Circle(13) = {14, 18, 19}; Line(14) = {19, 23}; Line(15) = {23, 27}; Line(16) = {27, 31}; Line(17) = {31, 9}; Line(19) = {1, 9}; Line(20) = {5, 10}; Line(24) = {8, 14}; Line(28) = {7, 19}; Line(32) = {4, 23}; Line(36) = {3, 27}; Line(40) = {2, 31}; Line Loop(9) = {1, 2, 3, 4, 5, 6, 7}; Plane Surface(9) = {9}; Line Loop(21) = {1, 20, -11, -19}; Ruled Surface(21) = {21}; Line Loop(25) = {2, 24, -12, -20}; Ruled Surface(25) = {25}; Line Loop(29) = {3, 28, -13, -24}; Ruled Surface(29) = {29}; Line Loop(33) = {4, 32, -14, -28}; Ruled Surface(33) = {33}; Line Loop(37) = {5, 36, -15, -32}; Ruled Surface(37) = {37}; Line Loop(41) = {6, 40, -16, -36}; Ruled Surface(41) = {41}; Line Loop(45) = {7, 19, -17, -40}; Ruled Surface(45) = {45}; Line Loop(46) = {11, 12, 13, 14, 15, 16, 17}; Plane Surface(46) = {46}; Surface Loop(1) = {9, 46, 21, 25, 29, 33, 37, 41, 45}; Volume(1) = {1}; Physical Volume(1) = {1}; Mesh.CharacteristicLengthFactor = $discret$; """ def create_mesh(filename, inner_size=100.0, curvature=5.0, width=10.0, thickness=20.0, discretisation=2.5): global mesh s = str(mesh) x = 0.5*inner_size variables = [("x0", x - curvature), ("x1", x), ("x2", x + 0.5*width), ("y1", thickness), ("discret", discretisation)] for variable_name, variable_value in variables: s = s.replace("$%s$" % variable_name, str(variable_value)) f = open(filename, "w") f.write(s) f.close() create_mesh("dots.geo")