# Uniting a mesh using Python an Gmsh

**URL:** <https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474>\
**Category:** mesh\
**Created:** [June 12, 2023, 10:17am UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474 "2023-06-12T10:17:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mishal](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mishal/32/7396_2.png) [@Mishal](https://fenicsproject.discourse.group/u/Mishal)\
**Post date:** [June 12, 2023, 10:17am UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/1 "2023-06-12T10:17:17Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 12, 2023, 12:04pm UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/2 "2023-06-12T12:04:17Z")

</div>

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

````auto
```
// 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](https://fenicsproject.discourse.group/t/mesh-submesh-torus-boundary/6378/5)

I’ve also explained this in:

> **[Mesh generation and conversion with GMSH and PYGMSH](https://jsdokken.com/src/pygmsh_tutorial.html)**
>
> Personal webpage

---

<div class="post-metadata">

**Author:** ![Mishal](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mishal/32/7396_2.png) [@Mishal](https://fenicsproject.discourse.group/u/Mishal)\
**Post date:** [June 12, 2023, 2:59pm UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/3 "2023-06-12T14:59:03Z")

</div>

Hi Dokken

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

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 12, 2023, 4:15pm UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/4 "2023-06-12T16:15:34Z")

</div>

I dont believe that is true.  
See [tutorials/t21.geo · gmsh\_4\_11\_1 · gmsh / gmsh · GitLab](https://gitlab.onelab.info/gmsh/gmsh/blob/gmsh_4_11_1/tutorials/t21.geo)

---

<div class="post-metadata">

**Author:** ![Mishal](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mishal/32/7396_2.png) [@Mishal](https://fenicsproject.discourse.group/u/Mishal)\
**Post date:** [June 12, 2023, 7:49pm UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/5 "2023-06-12T19:49:08Z")

</div>

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

```auto
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])

```

---

<div class="post-metadata">

**Author:** ![conpierce8](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/conpierce8/32/2464_2.png) [@conpierce8](https://fenicsproject.discourse.group/u/conpierce8)\
**Post date:** [June 12, 2023, 9:30pm UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/6 "2023-06-12T21:30:49Z")

</div>

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](http://gmsh.info/doc/texinfo/gmsh.html#index-gmsh_002fmodel_002focc_002ffragment)

```python
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()

```

---

<div class="post-metadata">

**Author:** ![Mishal](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mishal/32/7396_2.png) [@Mishal](https://fenicsproject.discourse.group/u/Mishal)\
**Post date:** [June 13, 2023, 8:17am UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/7 "2023-06-13T08:17:49Z")

</div>

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

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 13, 2023, 8:27am UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/8 "2023-06-13T08:27:52Z")

</div>

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

> [@Mishal](#):
>
> `C2 = gmsh.model.occ.addCircle(0.0, 0.0, 0.0,50.0,3)`

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)

---

<div class="post-metadata">

**Author:** ![conpierce8](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/conpierce8/32/2464_2.png) [@conpierce8](https://fenicsproject.discourse.group/u/conpierce8)\
**Post date:** [June 13, 2023, 1:14pm UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/9 "2023-06-13T13:14:49Z")

</div>

> [@Mishal](#):
>
> ```auto
> # {what should go here}
> frag = gmsh.model.occ.fragment([(2, 1)],[(1, C2)])
> 
> ```

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

```python
# 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

```

---

<div class="post-metadata">

**Author:** ![Mishal](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mishal/32/7396_2.png) [@Mishal](https://fenicsproject.discourse.group/u/Mishal)\
**Post date:** [June 13, 2023, 1:54pm UTC](https://fenicsproject.discourse.group/t/uniting-a-mesh-using-python-an-gmsh/11474/10 "2023-06-13T13:54:23Z")

</div>

Hi Guys

Thanks for the clarification!
