# Generate a triangulation for the domain [-pi,pi]*[f(t),H+L] by Gmsh
# Inputs:  meshsize and definition of the periodic bottom surface
import gmsh
import numpy as np          


gmsh.initialize()          


size = 0.08;   # meshsize   
pi = np.pi 
n  = 100
H  = 2.5
L  = 4

# Define the function for the bottom surface

def f(t): return 3/2 + np.sin(t)/3 - np.cos(2*t)/4


tag1 = gmsh.model.geo.addPoint(-pi, f(-pi), 0, size)
tag2 = gmsh.model.geo.addPoint(-pi, H, 0, size)
tag3 = gmsh.model.geo.addPoint(-pi, L, 0, size) 
tag4 = gmsh.model.geo.addPoint( pi, f(pi), 0, size)
tag5 = gmsh.model.geo.addPoint( pi, H, 0, size)
tag6 = gmsh.model.geo.addPoint( pi, L, 0, size)

# Define the geometry on the bottom surface

listofpoints = [tag1]

dx = (2*pi) / (n-1)                                
for i in range(n):
    x = -pi + (i) * dx
    y = f(x)
    
     # Add the generated points (x, y, 0) to the model

    tag = gmsh.model.geo.addPoint(x, y, 0, size)   

    listofpoints.append(tag)

listofpoints.append(tag4)

# Add Lines to connect points 

linetag1 = gmsh.model.geo.addLine(tag1,tag2,1)
linetag2 = gmsh.model.geo.addLine(tag2,tag3,2)  
linetag3 = gmsh.model.geo.addLine(tag4,tag5,3)
linetag4 = gmsh.model.geo.addLine(tag5,tag6,4)
linetag5 = gmsh.model.geo.addLine(tag2,tag5,5)
linetag6 = gmsh.model.geo.addLine(tag3,tag6,6)

# Add Spline for all points on bottom surface

linetag7 = gmsh.model.geo.addSpline(listofpoints,7)

# Add Lines to CurveLoop 

looptag1 = gmsh.model.geo.addCurveLoop([linetag7,linetag3,linetag5,linetag1],reorient = True)
looptag2 = gmsh.model.geo.addCurveLoop([linetag5,linetag2,linetag6,linetag4],reorient = True)


# Add Lines to PlaneSurface 

gmsh.model.geo.addPlaneSurface([looptag1])
gmsh.model.geo.addPlaneSurface([looptag2])

# Synchronize the model

gmsh.model.geo.synchronize()

# Generate the mesh

gmsh.model.mesh.generate(2)


# Specify the format of Gmsh file: 
# The version 2.2 is easy to be read in MATLAB (In MATLAB: don't forget to delete the nodes on the Spline)
gmsh.option.setNumber("Mesh.MshFileVersion",2.2)  


gmsh.write("Mesh_08_test2.msh")
gmsh.fltk.initialize()
gmsh.fltk.run()

gmsh.finalize()
