#!python3
import gmsh
from math import sin, cos, exp, pi 

H = 2.5

def periodicCurve(x):                        # periodic lower boundary
    return 1.5 + sin(x)/3 - cos(2*x)/4

def localPerturbationCurve(x):
    if x >= 1 or x <= -1:                 
        return 0
    return exp( ( x**2 - 1 )** -1 ) * sin( pi * (x + 1) )

def transformation(x,y):    # transformation of the domain from periodic to locally perturbed domain
    if y>=H:
        return y
    return y + ( (y - H)**3 * (periodicCurve(x) - H)** -3 ) * localPerturbationCurve(x)

def extendMesh():
     tags,coordinates,_ = gmsh.model.mesh.getNodes()

     for tagIdx in range(len(tags)):
        index= 3 * tagIdx
        x = coordinates[index] - 2*pi
        y = coordinates[index + 1])
        gmsh.model.mesh.addNode(tags[tagIdx], (x,y,0), ())

def perturbMesh():
    tags,coordinates,_ = gmsh.model.mesh.getNodes()

    for tagIdx in range(len(tags)):
        index= 3 * tagIdx
        x = coordinates[index]
        y = transformation(x,coordinates[index + 1])
        gmsh.model.mesh.setNode(tags[tagIdx], (x,y,0), ())

def readCustomData(filename):
    Tags = []
    Real = []
    Imag = []
    with open(filename) as Inputfile:
        lines = Inputfile.read().splitlines()
        for line in lines:
            Enteries = line.split()
            Tags.append(int(Enteries[0]))
            Real.append(float(Enteries[1]))
            Imag.append(float(Enteries[2]))
    return {"tags":Tags, "real":Real, "imaginary":Imag}

