# Name:             angledFacet.py
# Author:           Matthew Banham and Tom Davies
# Last Modified:    8th April 2003
# Copyright (c) Photon Design 2003
# Comments:
#       This script sends commands via TCP / IP to an already running version of FIMMWAVE. It generates
#       a project file which consists of a Fimmprop Layout Node. This node describes the
#       structure that is specified by the parameters below.
#
#       NOTE: THE RUNNING COPY OF FIMMWAVE MUST NOT HAVE ANY PROJECTS OPENED BEFORE THIS SCRIPT IS RUN. 
#
#--------------------------------------------------------------------------------------------------    
#       The user can set several parameters of this script
#
#       compWidth:      The total width of the computational window.
#       compHeight:     The total height of the computational window.
#       ncore:          The refractive index of the waveguide cores
#       nclad:          The refractive index of the background material that fills the
#                       the computational window. The waveguides and the mirrors are placed
#                       within this medium.
#       nmirror:        The refractive index of the two mirrors
#       coreWidth:      The width of the waveguide cores
#       mirrorWidth:    The thickness of the mirrors
#       mirrorLength:   The length of the mirrors
#       alength:        The length of the angled waveguide section. See
#                       Diagrams.pdf for more details
#       mirrorFrac:     The fractional distance from the bottom of the facet between the straight
#                       and angled waveguides, to the point of intersection of the facet with the
#                       centre of the top surface of the mirror. See diagrams.pdf for more details
#
#
#       doInputAngledFacetPyParms:  If you set this to 1, then the script asks for the values to
#                                   be inputted rather than the user setting them in the script.


#------------------------------set the parameters here-------------------------------------------

doInputAngledFacetPyParms = 0
    #set this to zero if you want to use the settings given below. Set it to 1 if you want to
    #manually enter the settings during the execution of the script.

compWidth = 100
compHeight = 60
ncore = 1.49745
nclad = 1.49158
nmirror = 1
coreWidth = 8
mirrorWidth = 6
mirrorLength = 45
alength = 30

#------------------------------------------------------------------------------------------------

if doInputAngledFacetPyParms==1:
    compWidth = float(raw_input("computational width (in microns): "))
    compHeight = float(raw_input("computational height (in microns): ")) 
    ncore = float(raw_input("core refractive index: "))
    nclad = float(raw_input("background refractive index: "))
    nmirror = float(raw_input("mirror refractive index: "))
    coreWidth = float(raw_input("core width (in microns): "))
    mirrorWidth = float(raw_input("mirror thickness (in microns): "))
    mirrorLength = float(raw_input("mirror length (in microns): "))
    alength = float(raw_input("alength (in microns): "))
    
mirrorFrac = float(raw_input("mirrorFrac: "))
print
print "Enter a filename for the generated .prj file."
print
print "NOTE: IF THIS IS THE NAME OF A FILE THAT ALREADY EXISTS THIS"
print "FILE WILL BE OVERWRITTEN"
print
projectName = raw_input("")

  

#------------import maths library and library needed to talk to FIMMWAVE-------------------------

from math import *
from pdPythonLib import *

class Point:
    def __init__(self,_x = 0,_y = 0):
        self.x = _x
        self.y = _y

#-------A couple of helper functions (for calc'ing angles, vector addition and vector subtraction

def DegToRad(degAngle):
    degAngle1 = float(degAngle)
    return (degAngle1*pi/180.0)

def RadToDeg(radAngle):
    radAngle1 = float(radAngle)
    return (radAngle1*180.0/pi)

def SubPoints(p1,p2):
    return Point(p1.x - p2.x,p1.y - p2.y)

def AddPoints(p1,p2):
    return Point(p1.x + p2.x,p2.y + p1.y)

#NB: all lengths are measured from the "top" core-cladding interface as reference

#-----------------------first calculate L0 to L7-------------------------------------------------

vartemp = (compHeight - coreWidth - alength*sin(DegToRad(45.0)))/2.0
vartemp1 = coreWidth * tan(DegToRad(22.5))
vartemp2 = (compWidth - vartemp1 - alength*cos(DegToRad(45.0)))/2.0
l0 = Point(0,vartemp)
l1 = Point(0,vartemp + coreWidth)
l2 = Point(vartemp2,vartemp + coreWidth)
l3 = Point(vartemp2 + vartemp1,vartemp)
l4 = Point(l3.x + alength * cos(DegToRad(45.0)),l3.y + alength * sin(DegToRad(45.0)))
l5 = Point(l2.x + alength * cos(DegToRad(45.0)),l2.y + alength * sin(DegToRad(45.0)))
l6 = Point(compWidth,compHeight - vartemp)
l7 = Point(compWidth,l4.y)

#----------------------Now talk to FIMMWAVE and create the Project and Layout node---------------

f = pdApp()
f.ConnectToApp()

f.AddCmd("app.addsubnode(fimmwave_prj,"+projectName+")")
f.AddCmd("Ref& project = app.findnode("+projectName+")")
f.AddCmd("project.addsubnode(FPLayoutNode,\"Layout\")")
f.AddCmd("project.subnodes[1].cdev.width = {compWidth};")
f.AddCmd("project.subnodes[1].cdev.height = {compHeight};")

#----------------------Now insert the 6 shapes into the layout node (5xP, 1xR)---------------

f.AddCmd("project.subnodes[1].cdev.insertshape(1,rectangle,0,0)")
f.AddCmd("project.subnodes[1].cdev.insertshape(1,polygon,0,0)")
f.AddCmd("project.subnodes[1].cdev.insertshape(1,polygon,0,0)")
f.AddCmd("project.subnodes[1].cdev.insertshape(1,polygon,0,0)")
f.AddCmd("project.subnodes[1].cdev.insertshape(1,polygon,0,0)")
f.AddCmd("project.subnodes[1].cdev.insertshape(1,polygon,0,0)")

#----------------------Background rectangle---------------

f.AddCmd("project.subnodes[1].cdev.shapes[6].yalign=1")
f.AddCmd("project.subnodes[1].cdev.shapes[6].width = {compWidth};")
f.AddCmd("project.subnodes[1].cdev.shapes[6].height = {compHeight};")
f.AddCmd("project.subnodes[1].cdev.shapes[6].nr11 = {nclad};")
f.AddCmd("project.subnodes[1].cdev.shapes[6].nr22 = {nclad};")

#--------------------left-hand side waveguide----------------------------------------------------

f.AddCmd("project.subnodes[1].cdev.shapes[3].xalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[3].yalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[3].xposn = {l0.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].yposn = {l0.y-compHeight/2};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[1].x = 0;")
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[1].y = 0;")
l0l1 = SubPoints(l1,l0)
l0l2 = SubPoints(l2,l0)
l0l3 = SubPoints(l3,l0)
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[2].x = {l0l1.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[2].y = {l0l1.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[3].x = {l0l2.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[3].y = {l0l2.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[4].x = {l0l3.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].plist[4].y = {l0l3.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].nr11 = {ncore};")
f.AddCmd("project.subnodes[1].cdev.shapes[3].nr22 = {ncore};")

#--------------------angled waveguide------------------------------------------------------------
f.AddCmd("project.subnodes[1].cdev.shapes[4].xalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[4].yalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[4].xposn = {l3.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].yposn = {l3.y-compHeight/2};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[1].x = 0;")
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[1].y = 0;")
l3l2 = SubPoints(l2,l3)
l3l5 = SubPoints(l5,l3)
l3l4 = SubPoints(l4,l3)
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[2].x = {l3l2.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[2].y = {l3l2.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[3].x = {l3l5.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[3].y = {l3l5.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[4].x = {l3l4.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].plist[4].y = {l3l4.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].nr11 = {ncore};")
f.AddCmd("project.subnodes[1].cdev.shapes[4].nr22 = {ncore};")

#---------------------rhs waveguide--------------------------------------------------------------
f.AddCmd("project.subnodes[1].cdev.shapes[5].xalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[5].yalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[5].xposn = {l4.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].yposn = {l4.y-compHeight/2};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[1].x = 0;")
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[1].y = 0;")
l4l5 = SubPoints(l5,l4)
l4l6 = SubPoints(l6,l4)
l4l7 = SubPoints(l7,l4)
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[2].x = {l4l5.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[2].y = {l4l5.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[3].x = {l4l6.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[3].y = {l4l6.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[4].x = {l4l7.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].plist[4].y = {l4l7.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].nr11 = {ncore};")
f.AddCmd("project.subnodes[1].cdev.shapes[5].nr22 = {ncore};")

#---------------------Now the mirrors - lhs------------------------------------------------------

m0 = Point((mirrorLength/2.0) * cos(DegToRad(22.5)),(mirrorLength/2.0) * sin(DegToRad(22.5)))
m1 = Point((mirrorWidth) * sin(DegToRad(22.5)),(mirrorWidth) * cos(DegToRad(22.5)))
m3 = Point(mirrorFrac*coreWidth*tan(DegToRad(22.5)),mirrorFrac*coreWidth)

f.AddCmd("project.subnodes[1].cdev.shapes[1].xalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[1].yalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[1].xposn = {l3.x - m3.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].yposn = {l3.y + m3.y-compHeight/2};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[1].x = {-m0.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[1].y = {-m0.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[2].x = {m0.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[2].y = {m0.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[3].x = {m0.x + m1.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[3].y = {m0.y - m1.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[4].x = {-m0.x + m1.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].plist[4].y = {-m0.y - m1.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].nr11 = {nmirror};")
f.AddCmd("project.subnodes[1].cdev.shapes[1].nr22 = {nmirror};")

#---------------------rhs mirror-----------------------------------------------------------------

f.AddCmd("project.subnodes[1].cdev.shapes[2].xalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[2].yalign=3")
f.AddCmd("project.subnodes[1].cdev.shapes[2].xposn = {l5.x + m3.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].yposn = {l5.y - m3.y-compHeight/2};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[1].x = {-m0.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[1].y = {-m0.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[2].x = {m0.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[2].y = {m0.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[3].x = {m0.x - m1.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[3].y = {m0.y + m1.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[4].x = {-m0.x - m1.x};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].plist[4].y = {-m0.y + m1.y};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].nr11 = {nmirror};")
f.AddCmd("project.subnodes[1].cdev.shapes[2].nr22 = {nmirror};")

#---------------------Solver Parameters----------------------------------------------------------

f.AddCmd("project.subnodes[1].svp.lambda=1.55")
f.AddCmd("project.subnodes[1].svp.solvid=6")
f.AddCmd("project.subnodes[1].svp.buff=V3 1 500 500 40 40 3 5 5")
f.AddCmd("project.subnodes[1].mlp.mintefrac=100")
f.AddCmd("project.subnodes[1].mlp.maxtefrac=100")
#f.AddCmd("project.subnodes[1].cdev.lhsbc.pmlpar=0.1")
#f.AddCmd("project.subnodes[1].cdev.rhsbc.pmlpar=0.1")

f.Exec("project.savetofile(d:\\temp\\"+"{projectName}"+".prj)")
del f








