# Gmsh generator under loop

**URL:** <https://discourse.julialang.org/t/gmsh-generator-under-loop/91726>\
**Category:** New to Julia\
**Tags:** question, gmsh\
**Created:** [December 16, 2022, 5:09am UTC](https://discourse.julialang.org/t/gmsh-generator-under-loop/91726 "2022-12-16T05:09:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AniMondal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/animondal/32/45121_2.png) [@AniMondal](https://discourse.julialang.org/u/AniMondal)\
**Post date:** [December 16, 2022, 5:09am UTC](https://discourse.julialang.org/t/gmsh-generator-under-loop/91726/1 "2022-12-16T05:09:04Z")

</div>

Hello all,

I am a begineer to Julia!  
I want to create three .vtk files. To do that, first I want to create three .msh files. The three .msh files should be generated under a `for` loop. To do so, I am running this Julia code in Jupyter Notebook:

```julia
xh = XLSX.readdata("LHS_sampling.xlsx", "Sheet1!A1:C100")
xh_c1 = xh[:,1]
xh_c2 = xh[:,2]
xh_c3 = xh[:,3]

const L = 8.0
const LL = 0.475.*L
const LR = 0.525.*L
const ls = 0.03
const hfc = ls/100 #Mesh size parameter
const hf = ls/2.1 #Mesh size parameter
const h = 100*hf #Mesh size parameter
const H = 4.0

for i in 1:3
    using Gmsh: gmsh
#const H = 2.0
#const CH = 0.4 #Crack height
  CH = xh_c3[i] #Crack height    
#const CW = 0.2 #Crack Width
  CW = xh_c2[i] #Crack Width    

gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 1)
#gmsh.model.geo.addPoint((L/10)+(CW/2), 0.0 , 0.0, hf ,1)
gmsh.model.geo.addPoint((xh_c1[i])+(xh_c2[i]/2), 0.0 , 0.0, hf ,1)    
gmsh.model.geo.addPoint(L, 0.0, 0.0, h, 2)
gmsh.model.geo.addPoint(L, H, 0.0, h, 3)
gmsh.model.geo.addPoint(LR , H, 0.0, hf, 4)
gmsh.model.geo.addPoint(LL , H, 0.0, hf, 5)
gmsh.model.geo.addPoint(0.0, H, 0.0, h, 6)
gmsh.model.geo.addPoint(0.0, 0.0, 0.0, h, 7)
#gmsh.model.geo.addPoint((L/10) -(CW/2), 0.0 , 0.0, hf ,8)
gmsh.model.geo.addPoint((xh_c1[i])-(xh_c2[i]/2), 0.0 , 0.0, hf ,8)    
#gmsh.model.geo.addPoint((L/10), CH , 0.0, hfc , 9)
gmsh.model.geo.addPoint(xh_c1[i], CH , 0.0, hfc , 9)    
gmsh.model.geo.addLine(1, 2, 1)
gmsh.model.geo.addLine(2, 3, 2)
gmsh.model.geo.addLine(3, 4, 3)
gmsh.model.geo.addLine(4, 5, 4)
gmsh.model.geo.addLine(5, 6, 5)
gmsh.model.geo.addLine(6, 7, 6)
gmsh.model.geo.addLine(7, 8, 7)
gmsh.model.geo.addLine(8, 9, 8)
gmsh.model.geo.addLine(9, 1, 9)

gmsh.model.geo.addCurveLoop([1,2,3,4,5,6,7,8,9],1)
gmsh.model.geo.addPlaneSurface([1], 1)
gmsh.model.addPhysicalGroup(2, [1],1)
gmsh.model.addPhysicalGroup(1, [4],1)
gmsh.model.addPhysicalGroup(0, [7],2)
gmsh.model.addPhysicalGroup(0, [2],3)
gmsh.model.setPhysicalName(2, 1, "Domain")
gmsh.model.setPhysicalName(1, 1, "LoadLine")
gmsh.model.setPhysicalName(0, 2, "LeftSupport")
gmsh.model.setPhysicalName(0, 3, "RightSupport")
gmsh.model.mesh.field.add("Box", 10)
gmsh.model.mesh.field.setNumber(10, "VIn", hf)
gmsh.model.mesh.field.setNumber(10, "VOut", h)
#gmsh.model.mesh.field.setNumber(10, "XMin", (L/8)-CW)
gmsh.model.mesh.field.setNumber(10, "XMin", xh_c1[i]-xh_c2[i])    
#gmsh.model.mesh.field.setNumber(10, "XMax", (L/8)+CW)
gmsh.model.mesh.field.setNumber(10, "XMax", xh_c1[i]+xh_c2[i])    
gmsh.model.mesh.field.setNumber(10, "YMin", 0)
gmsh.model.mesh.field.setNumber(10, "YMax", H)
gmsh.model.mesh.field.setAsBackgroundMesh(10)
gmsh.model.geo.synchronize()
gmsh.model.mesh.generate(2)
gmsh.write("BeamWithNotchSymThreePtBending$i.msh")
gmsh.finalize()
end

```

I am getting this error:

```julia
Warning : Gmsh has aleady been initialized
Error : GEO point with tag 1 already exists
GEO point with tag 1 already exists

Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] addPoint(x::Float64, y::Float64, z::Float64, meshSize::Float64, tag::Int64)
   @ Gmsh.gmsh.model.geo C:\Users\Anirban\.julia\artifacts\f93bed7ed0bd321ab0b0ae233750c27274ad54b2\lib\gmsh.jl:3693
 [3] top-level scope
   @ In[60]:21
 [4] eval
   @ .\boot.jl:373 [inlined]
 [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1196

```

```julia
model = GmshDiscreteModel("BeamWithNotchSymThreePtBending.msh") 
writevtk(model ,"BeamWithNotchSymThreePtBending")

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 16, 2022, 10:04am UTC](https://discourse.julialang.org/t/gmsh-generator-under-loop/91726/2 "2022-12-16T10:04:38Z")

</div>

I suggest you start by doing the initialization _once_ before the loop `using Gmsh; Gmsh.initializie()`, and for each loop iteration you use `gmsh.clear()` and/or `gmsh.open()` (see their docstrings). Finally, outside of the loop you can call `Gmsh.finalize()`.

---

<div class="post-metadata">

**Author:** ![Guanglin\_Du](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guanglin_du/32/43088_2.png) [@Guanglin\_Du](https://discourse.julialang.org/u/Guanglin_Du)\
**Post date:** [December 20, 2023, 11:42am UTC](https://discourse.julialang.org/t/gmsh-generator-under-loop/91726/3 "2023-12-20T11:42:21Z")

</div>

The location of function **gmsh.clear()** is vital. See the Python example, **A.22 x1: Geometry and mesh data** , on page 161 of the Gmsh Reference Manual for Gmsh 4.8.4, 28 April 2021, function **gmsh.clear()** should be immediately before function **gmsh.finalize()**.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [December 20, 2023, 11:48am UTC](https://discourse.julialang.org/t/gmsh-generator-under-loop/91726/4 "2023-12-20T11:48:09Z")

</div>

My 2 cent worth…  
Why is the using Gmsh: gmsh inside a loop? It should be called once before the loop

I would also create a function containing all the gmsh.model lines - it would make the main loop more readable.  
I may be wrong of course.  
Nothing wrong with a loop of course, but if you had a function you could call it three times.  
ps. there is no penalty in using loops in Julia, so I am not advising you to not use loops. Just suggesting for readability
