How to Generate mesh in Python with Gmsh module?
In this article, we will cover how to Generate meshes using Gmsh module in Python.
What is Mesh?
A connected 2D, 3D, or multi-dimension structure that is made up of points, lines, and curves is called mesh. In real-world meshes are made up of many different materials such as metals, fibers, and ductile materials. For example, the image is shown below:
|initialize()||None||Initialize the gmsh|
|add_point ()||x, y, z, lc||Create a point at (x, y, z) with the target mesh size (lc) close to point|
|add_line()||pointA, pointB||Create a line from pointA, to pointB|
|add_curve_loop()||list of lines||Create a face that is formed by lines|
|add_plane_suface()||list of faces||Create a surface on the face and connect face surfaces|
|synchronize()||None||Create the relevant Gmsh data structures from the Gmsh model. Synchronizations can be called at any time, but to reduce processing time it’s better to call after adding points, lines, curves, surfaces etc.|
|generate()||None||Generate the mesh|
|write()||Model name||Create a mesh of given model name for example “GFG.msh”|
|run()||None||Creates graphical user interface|
|finalize()||None||It finalizes the Gmsh API|
After understanding mesh and functions it’s time to create the above mesh. So without further delay, let’s jump right in.
Gmsh library: It is a script of promotion and API Python wrapper for gmsh.
pip install gmsh
Sys library: It is a script of promotion and API Python wrapper for sys.
pip install sys
Step1: Import modules, initialize gmsh.
Step2: The above mesh is made up of three shapes a cube and two pentagons. So first we create a cube then both pentagons. To create a cube we need to create 8 points, 12 edges/lines, 6 faces, and their surfaces. To create points use the code given below.
Step3: After creating points, we are able to create lines from these points using the add_line() method. To create lines use the code given below.
Step 4: Next we create faces and surfaces but before that one should understand two methods more clearly that are add_curve_loop() and add_plane_surface(). In the above output, it looks like faces are generated but it is not true. So to create solid faces we use the add_curve_loop() then we use the add_plane_surface() method so that we get a solid face with different colors.
- add_curve_loop([line1, line2, line3, line4]): It required a list of lines in close loop format. For example: [line1(point1 to point2) -> line2(point2 to point3) -> line3(point3 to point4) -> line4(point4 to point1)], In this way point1 is connected to point4. So a face of the above square is produced. If we are moving in opposite directions of line i.e line1 = (point1 to point2) and if we are moving from (point2 to point1) then line1 is written as “-line1” in add_curve_loop() function because of the opposite direction.
- add_plane_surface(): If only one face is passed as a parameter then the function creates an only surface on the face. If two faces are passed as parameters in the list then the function connects faces if it is possible i.e if the faces lie in the same plane.
After understanding both methods cube can be created using the given code below.
Step5: Now the final step is to create both pentagons and connect them. To do so one should need to create their points and lines using proper measurement it is better to draw figures on paper first then code points according. Here we create two pentagons one bigger and one smaller using add_point(), add_line() functions. After that, we will create surfaces of both the pentagons using add_curve_loop() and then connect the bigger and smaller pentagon face with the cube face using add_plane_surface. At last, we will connect both pentagon’s faces with each other using the add_plane_surface() function.
Please Login to comment...