Uniting a mesh using Python an Gmsh

Hi Guys

I am new to Gmsh. Please refer to the below Python code.
The code develops 2 squares with a mesh, how would I go about uniting the two surfaces, so that when a mesh is developed there is a straight line along the intersection?

Kind regards
Mishal Mohanlal

import gmsh

import sys

gmsh.initialize()

lc = 1

point1 = gmsh.model.geo.add_point(0, 0, 0, lc)

point2 = gmsh.model.geo.add_point(0, 10, 0, lc)

point3 = gmsh.model.geo.add_point(10, 10, 0, lc)

point4 = gmsh.model.geo.add_point(10, 0, 0, lc)

point5 = gmsh.model.geo.add_point(0, 5, 0, lc)

point6 = gmsh.model.geo.add_point(5, 5, 0, lc)

point7 = gmsh.model.geo.add_point(5, 0, 0, lc)

line1 = gmsh.model.geo.add_line(point1, point2)

line2 = gmsh.model.geo.add_line(point2, point3)

line3 = gmsh.model.geo.add_line(point3, point4)

line4 = gmsh.model.geo.add_line(point4, point1)

line5 = gmsh.model.geo.add_line(point1, point5)

line6 = gmsh.model.geo.add_line(point5, point6)

line7 = gmsh.model.geo.add_line(point6, point7)

line8 = gmsh.model.geo.add_line(point7, point1)

face1 = gmsh.model.geo.add_curve_loop([line1, line2, line3, line4])

face2 = gmsh.model.geo.add_curve_loop([line5, line6, line7, line8])

gmsh.model.geo.add_plane_surface([face1])

gmsh.model.geo.add_plane_surface([face2])

gmsh.model.geo.synchronize()

gmsh.model.mesh.generate()

gmsh.write(“GFG.msh”)

if ‘close’ not in sys.argv:

gmsh.fltk.run()

gmsh.finalize()

Please use 3x` to encapsulate the code, i.e.

```
// Add gmsh code here
```

For your other question, i would suggest you have a look at the Fragments command: Mesh, Submesh , Torus, Boundary - #5 by dokken

I’ve also explained this in:

Hi Dokken

Based on the documentation this can only be implemented for a 3D mesh and not 2D

I dont believe that is true.
See tutorials/t21.geo ¡ gmsh_4_11_1 ¡ gmsh / gmsh ¡ GitLab

I’ve got the below code, I cannot figure out how to pass information to the fragments function. Can you please assist?

geom = pygmsh.geo.Geometry()
C1 = geom.add_circle([0.0, 0.0], 10, mesh_size=0.1)
C2 = geom.add_circle([0.0, 0.0], 20, mesh_size=0.1)
C3 = geom.add_circle([0.0, 0.0], 30, mesh_size=0.1)
frags = pygmsh.occ.geometry.Geometry.boolean_fragments(C3,[C1,C2])

What about the following? Note that entities must be passed as “vectors of pairs of integers”, which can be translated into Python as “lists of tuples of integers”. Gmsh 4.11.1

import gmsh
import numpy as np

gmsh.initialize()

C1 = gmsh.model.occ.addRectangle(0, 0, 0, 10, 10)
C2 = gmsh.model.occ.addRectangle(0, 0, 0, 5, 5)

#                              |-list of-|
#                              |         |
frag = gmsh.model.occ.fragment([ (2, C1) ], [(2, C2)])
#                                ^^^^^^^
#                            pair of integers

print(frag)
points = gmsh.model.occ.getEntities(0)
gmsh.model.occ.mesh.setSize(points, 1)
gmsh.model.occ.synchronize()

gmsh.model.mesh.generate()
gmsh.write("squares.msh")

gmsh.finalize()
2 Likes

Thanks for the help.
I am struggling to understand how the arguments need to be passed to the “fragment” command.
In the previous code what is the meaning of [2,C1] and [2,C2]?

My below code develops the two circles however does not perform the fragment command correctly

import gmsh
import sys


gmsh.initialize()


C1 = gmsh.model.occ.addCircle(0.0, 0.0, 0.0,100.0,1)
gmsh.model.occ.addCurveLoop([1] ,2)
gmsh.model.occ.addPlaneSurface([2],1)


C2 = gmsh.model.occ.addCircle(0.0, 0.0, 0.0,50.0,3)
gmsh.model.occ.addCurveLoop([3] ,4)
gmsh.model.occ.addPlaneSurface([4],2)

#                              {what should go here}
frag = gmsh.model.occ.fragment([(2, 1)],[(1, C2)])
print(frag)

gmsh.model.occ.synchronize()
gmsh.model.mesh.generate(2)
gmsh.write("Circle.msh")
if '-nopopup' not in sys.argv:
    gmsh.fltk.run()

gmsh.finalize()

Every entity in GMSH gets a tag (Which is the return value of the constructor of an entity):

As entities can be of different dimensions, once one want to use boolean operators, one has to supply a list of tuples (dim_of_entity, entity_tag)

As @dokken mentioned, the pairs of integers must have the form (dimension_of_entity, tag_of_entity). Since you are working with surfaces, the dimension_of_entity should be 2 for each surface. In your calls to addPlaneSurface, you have tagged the entities as 1 and 2. Therefore, the correct call to the fragment command would be

#                                Dimension of larger circle
#                                |  Tag of larger circle
#                                |  |
#                                v  v
frag = gmsh.model.occ.fragment([(2, 1)], [(2, 2)])
#                                          ^  ^
#                                          |  |
#                                          |  Tag of smaller circle
#                                          Dimension of smaller circle
2 Likes

Hi Guys

Thanks for the clarification!