#*******************************************************************************************************************
#                       Parameter Set for FIMMPROP-3D by Matthew Banham
#                      ---------------------------------------------------
#                                 Copyright Photon Design 2001
#
# This script takes any given component and allows you to change a variety of parameters for every section (or joint)
# in the component. For example, you might want to change the number of modes for every section in your component to
# a different value. This script will do this for you. It is all menu driven and pretty self explanatory. You must
# have the project open at the time. Note that if the project contains periodic elements or references other
# components, then these will also be changed.
#
# Variables
# ----------
#
# NB: ALL OF THE FOLLOWING VARIABLES CAN BE CHANGED OUTSIDE OF THIS SCRIPT!!!
#
# _portNo:    change this to the port number if you wish to use one other than the default (5101)
# _hostname:  change this to the name of the host on which FIMMWAVE is serving (default = "localhost")
# _compname:  change this to the name of the component of interest.
# _setOpt:  change this to the menu value of the parameter you wish to change (e.g. 1 for "number of (2d) modes",
#             2 for "mode profile resolution (nx)", 3 for "mode profile resolution (ny)", etc.) See the menu
#             printed out for the option numbers.
# _setVal:  change this to the value which you want to set the parameter to. For example, if I want to set all of
#             the sections of my component to have an nx of 60, then I would set _setOpt to 2 and _setVal to 60.

try:
    _portNo #might have been defined outside of the script
except:
    _portNo = 5101  #<==== CHANGE THIS TO YOUR PORT NUMBER

try:
    _hostname #might have been defined outside of the script
except:
    _hostname = "localhost"  #<===== CHANGE THIS TO YOUR HOSTNAME

try:
    _compname #might have been defined outside of the script
except:
    _compname = "" #<==== CHANGE THIS TO THE NAME OF YOUR COMPONENT

try:
    _setOpt #might have been defined outside of the script
except:
    _setOpt = 0 #<==== CHANGE THIS TO THE DESIRED MENU VALUE

try:
    _setVal
except:
    _setVal = None #<==== CHANGE THIS TO THE DESIRED VALUE OF THE PARAMETER

#*******************************************************************************************************************

from string import *
from pdPythonLib import *

#First check if component exists
f = pdApp()
f.ConnectToApp(_hostname,_portNo)

print ""
print "*****************************************************************"
print "*             Parameter Set for FIMMPROP-3D                     *"
print "*          -----------------------------------                  *"
print "*        by Matthew Banham (c) Photon Design 2001               *"
print "*                                                               *"
print "*****************************************************************"
print ""

hasPrevSet = 1

if _compname=="":
    print "Enter the node name of the component."
    print "For example if I have a component called comp1"
    print "which is stored in the root node under a project called 'project'"
    print "then I would enter 'project/comp1':   "
    
    _compname = raw_input("")

f.AddCmd("Ref& mainComp=app.findnode({_compname})")
retval1 = f.Exec("mainComp")
if retval1==None:
    raise RuntimeError,"Component doesn't exist"

while 1:
   if _setOpt==0:
       hasPrevSet = 0
       print ""
       print ""
       print "Menu - Select the parameter you wish to change:"
       print "-----------------------------------------------"
       print ""
       print "MOLAB Options"
       print "-------------"
       print ""
       print "   1.  Number of (2d) modes"
       print "   2.  Mode Profile Resolution (nx)"
       print "   3.  Mode Profile Resolution (ny)"
       print "   4.  Min. TE Frac"
       print "   5.  Max. TE Frac"
       print "   6.  neff/beta scan range: start"
       print "   7.  neff/beta scan range: end"
       print "   8.  Mode Solver used (e.g. FMM Solver (real), Eff. Idx (real), etc.)"
       print ""
       print "FMM Solver Options:"
       print "-------------------"
       print ""
       print "   9.  Number of (1d) modes"
       print "   10. mmatch"
       print "   11. Solver Type (e.g. semiVectorial TE, semiVectorial TM, etc.)"
       print ""
       print "Effective Index Solver Options:"
       print "-------------------------------"
       print ""
       print "   12. Dimension (e.g. 3D, Quasi 2D (X-Z), Quasi 2D (Y-Z)"
       print ""
       print "Joint and Overlap Properties:"
       print "-----------------------------"
       print ""
       print "   13. Overlap Method (e.g. Complete, Normal Fresnel, etc.)"
       print ""
       print "Boundary Conditions:"
       print "--------------------"
       print ""
       print "   14. Change the top boundary condition"
       print "   15. Change the bottom boundary condition"
       print "   16. Change the left-hand side boundary condition"
       print "   17. Change the right-hand side boundary condition"
       print ""
       tempstr = raw_input(">>> ")
       _setOpt = atoi(tempstr)

       #Now find out what they want
       fExec=1
       if _setOpt==1:
           tempstr = raw_input("Enter the desired number of (2d) modes: ")
           _setVal = atoi(tempstr)
       elif _setOpt==2:
           tempstr = raw_input("Enter the desired value for nx: ")
           _setVal = atoi(tempstr)
       elif _setOpt==3:
           tempstr = raw_input("Enter the desired value for ny: ")
           _setVal = atoi(tempstr)
       elif _setOpt==4:
           tempstr = raw_input("Enter the Min. TE Frac required: ")
           _setVal = atof(tempstr)
       elif _setOpt==5:
           tempstr = raw_input("Enter the Max. TE Frac required: ")
           _setVal = atof(tempstr)
       elif _setOpt==6:
           tempstr = raw_input("Enter the neff/beta scan start value: ")
           _setVal = atof(tempstr)
       elif _setOpt==7:
           tempstr = raw_input("Enter the neff/beta scan end value: ")
           _setVal = atof(tempstr)
       elif _setOpt==8:
           print "Select one of the following options:"
           print ""
           print "   1. FMM Solver (real)"
           print "   2. FMM Solver (complex)"
           print "   3. Eff. Idx (real)"
           print "   4. Eff. Idx (complex)"
           print ""
           tempstr = raw_input(">>> ")
           _setVal = atoi(tempstr)
       elif _setOpt==9:
           tempstr = raw_input("Enter the desired number of modes per slice: ")
           _setVal = atoi(tempstr)
       elif _setOpt==10:
           tempstr = raw_input("Enter a value for mmatch: ")
           _setVal = atof(tempstr)
       elif _setOpt==11:
           print "Select one of the following options:"
           print ""
           print "   1. Vectorial"
           print "   2. Semivec TE"
           print "   3. Semivec TM"
           print ""
           tempstr = raw_input(">>> ")
           _setVal = atoi(tempstr)
       elif _setOpt==12:
           print "Select one of the following options:"
           print ""
           print "   1. 3D"
           print "   2. Quasi 2D (X-Z)"
           print "   3. Quasi 2D (Y-Z)"
           print ""
           tempstr = raw_input(">>> ")
           _setVal = atoi(tempstr)
       elif _setOpt==13:
           print "Select one of the following options:"
           print ""
           print "   0. Complete"
           print "   1. Normal Fresnel"
           print "   2. Oblique Fresnel"
           print "   3. Special Complete"
           print ""
           tempstr = raw_input(">>> ")
           _setVal = atoi(tempstr)
       elif (_setOpt==14) or (_setOpt==15) or (_setOpt==16) or (_setOpt==17):
           print "Select one of the following options:"
           print ""
           print "   1. Electric wall boundary condition"
           print "   2. Magnetic wall boundary condition"
           print "   3. Periodic wall boundary condition"
           print ""
           tempstr = raw_input(">>> ")
           _setVal = atoi(tempstr)
       else:
           #raise RuntimeError,"Parameter Not Found"
           print ("invalid option")
           _setVal=100
           fExec=0#if one will call Parameter sweep
           

   if _setVal==None:
       raise RuntimeError,"Parameter Value not set up"

   print "Working.....Please Wait...."

   #Now get the version number (used later)
   versStr = f.Exec("app.progversion")
   a = rfind(versStr,".")
   versNoMain = atof(versStr[0:a])
   if versNoMain<3.41:
       raise RuntimeError,"To run this script, a later of version of FIMMWAVE is required. Please contact Photon Design"

   versNo = atoi(versStr[a+1:a+2])
   if _setOpt>13 and versNoMain==3.41 and versNo<5:
       print ""
       print "PLEASE NOTE THAT ANY TAPERS IN YOUR DEVICE WILL NOT BE CHANGED."
       print "FOR MORE INFORMATION PLEASE CONTACT PHOTON DESIGN"
       print "THIS MESSAGE APPEARED BECAUSE YOU HAVE A VERSION OF FIMMWAVE"
       print "THAT IS EARLIER THAN 3.41.5"

   simpleSecStr = ""
   taperStr = ""      
   
   def ConstructStrings():
       #Now construct a set of command strings which will be recursively applied to each appropriate section or joint
       global simpleSecStr
       global taperStr


       
       if _setOpt==1: #number of 2d modes
           simpleSecStr = "{stem}.wg.evlist.mlp.maxnmodes="+repr(_setVal) #can also use this for bends
           taperStr = "{stem}.mlp.maxnmodes="+repr(_setVal)
        
       elif _setOpt==2: #mode profile res (nx)
           simpleSecStr = "{stem}.wg.evlist.mlp.nx="+repr(_setVal)
           taperStr = "{stem}.mlp.nx="+repr(_setVal)

       elif _setOpt==3: #mode profile res (ny)
           simpleSecStr = "{stem}.wg.evlist.mlp.ny="+repr(_setVal)
           taperStr = "{stem}.mlp.ny="+repr(_setVal)
        
       elif _setOpt==4: #min TE frac.
           simpleSecStr = "{stem}.wg.evlist.mlp.mintefrac="+repr(_setVal)
           taperStr = "{stem}.mlp.mintefrac="+repr(_setVal)
        
       elif _setOpt==5: #max TE frac.
           simpleSecStr = "{stem}.wg.evlist.mlp.maxtefrac="+repr(_setVal)
           taperStr = "{stem}.mlp.maxtefrac="+repr(_setVal)
        
       elif _setOpt==6: #neff start
           simpleSecStr = "{stem}.wg.evlist.mlp.evstart="+repr(_setVal)
           taperStr = "{stem}.mlp.evstart="+repr(_setVal)
        
       elif _setOpt==7: #neff end
           simpleSecStr = "{stem}.wg.evlist.mlp.evend="+repr(_setVal)
           taperStr = "{stem}.mlp.evend="+repr(_setVal)
        
       elif _setOpt==8: #Mode Solver used (e.g. FMM Solver (real))
           if _setVal==1: #FMM (real)
               simpleSecStr = "{stem}.wg.evlist.svp.solvid=65;{stem}.wg.evlist.svp.buff=V2 30 0 1 300 300 15 25 0 5 5"
               taperStr = "{stem}.svp.solvid=65;{stem}.svp.buff=V2 30 0 1 300 300 15 25 0 5 5"
            
           elif _setVal==2: #FMM (complex)
               simpleSecStr = "{stem}.wg.evlist.svp.solvid=73;{stem}.wg.evlist.svp.buff=V2 30 0 1 300 300 15 25 0 5 5"
               taperStr = "{stem}.svp.solvid=73;{stem}.svp.buff=V2 30 0 1 300 300 15 25 0 5 5"
            
           elif _setVal==3: #Eff Idx (real)
               #need ...buff= to force the "Dimension" parameter to be valid
               simpleSecStr = "{stem}.wg.evlist.svp.solvid=6;{stem}.wg.evlist.svp.buff="
               taperStr = "{stem}.svp.solvid=6;{stem}.svp.buff="
            
           elif _setVal==4: #Eff Idx (complex)
               #need ...buff= to force the "Dimension" parameter to be valid
               simpleSecStr = "{stem}.wg.evlist.svp.solvid=14;{stem}.wg.evlist.svp.buff="
               taperStr = "{stem}.svp.solvid=14;{stem}.svp.buff="
            
           else:
               raise RuntimeError,"Incorrect Solver selection"
        
       elif ((_setOpt==9) or (_setOpt==10) or (_setOpt==11)): #No. of 1d modes per slice, mmatch, solverType)
           #Can only construct a template string the rest must be done on the fly
           simpleSecStr = "{stem}.wg.evlist.svp.buff"
           taperStr = "{stem}.svp.buff"
        
       elif _setOpt==12: #Dimension (e.g. Quasi 2D X-Z)
           if _setVal==1: #3D
               simpleSecStr = "{stem}.wg.evlist.svp.buff=V3 0 300 300 15 25 0 5 5" #don't care about other parameters
               taperStr = "{stem}.svp.buff=V3 0 300 300 15 25 0 5 5"
            
           elif _setVal==2: #Quasi 2D X-Z
               simpleSecStr = "{stem}.wg.evlist.svp.buff=V3 1 300 300 15 25 0 5 5" #don't care about other parameters
               taperStr = "{stem}.svp.buff=V3 1 300 300 15 25 0 5 5"
            
           elif _setVal==3: #Quasi 2D Y-Z
               simpleSecStr = "{stem}.wg.evlist.svp.buff=V3 2 300 300 15 25 0 5 5" #don't care about other parameters
               taperStr = "{stem}.svp.buff=V3 2 300 300 15 25 0 5 5"
            
           else:
               raise RuntimeError,"Incorrect Dimension Selection"

       elif _setOpt==16: #left-hand side boundary condition
           simpleSecStr = "{stem}.wg.lhsbc.type="+repr(_setVal)
           taperStr = "{stem}.wg1.lhsbc.type="+repr(_setVal)+";{stem}.wg2.lhsbc.type="+repr(_setVal)

       elif _setOpt==17: #right-hand side boundary condition
           simpleSecStr = "{stem}.wg.rhsbc.type="+repr(_setVal)
           taperStr = "{stem}.wg1.rhsbc.type="+repr(_setVal)+";{stem}.wg2.rhsbc.type="+repr(_setVal)

       elif _setOpt==14 or _setOpt==15: #top and bottom boundary conditions
           simpleSecStr = "" #these are set later
           taperStr = ""

       elif _setOpt!=13: #Joint Overlap method has to be set on the fly!
           raise RuntimeError,"Parameter Not Found"
       return

   #Now we can sweep through and change each element
   def ReplaceStem(comp,thestring):
       while(1):
           a = find(thestring,"{stem}")
           if a==-1:
               break
           thestring = thestring[0:a] + comp + thestring[a+6:]
       return thestring

   def FindWhitespace(thestring,nspace):
       #returns the index of the nspaceth whitespace in thestring
       #returns -1 if it doesn't find the nspaceth whitespace
       curidx = 0
       for i in range(0,nspace):
           if curidx>=len(thestring):
               return -1
           mystring = thestring[curidx:]
           a = find(mystring," ")
           if a==-1:
               return -1
           curidx = curidx + a + 1 #the index after the next space
       return curidx-1

   def ContainsStr(container,containee):
       a = find(container,containee)
       if a==-1:
           return 0
       return 1

   def CheckCurrentBC(compname,comptype,side):
       #Checks if the current condition is periodic
       #Sets Flag to skip Joints
       v=0;        
       if ((ContainsStr(comptype,"Joint"))):
           return 0
       else:
           if side==14:
               v=f.Exec(compname+".wg.topbc.type")
           elif side==15:
               v=f.Exec(compname+".wg.botbc.type")
           elif side==16:
               v=f.Exec(compname+".wg.lhsbc.type")
           elif side==17:
               v=f.Exec(compname+".wg.rhsbc.type")
           if v==3:
               print("Warning:Periodic Boundary condtion must be the same in opposite (top and bottom, left and right walls.")
               print("Remember to change both walls of each pair")
           return 1
           

   def ChangeFMMparametersWGSection(eltstr,eltType):
       CurrentSolverType=f.Exec(eltstr+".wg.evlist.svp.solvid")
       if (CurrentSolverType != 65 and CurrentSolverType != 73 and
           CurrentSolverType !=17 and CurrentSolverType !=33 and
           CurrentSolverType !=25 and CurrentSolverType !=41 and
           CurrentSolverType>0):
           print("Not using FMM solver. You have to choose an FMM solver for these options")                 
       elif  (CurrentSolverType == 65 or CurrentSolverType == 73 or
           CurrentSolverType ==17 or CurrentSolverType ==33 or
           CurrentSolverType ==25 or CurrentSolverType ==41):
           tempstr = ""
           if ((ContainsStr(eltType,"FPWGsection")) or (ContainsStr(eltType,"FPbend"))):
               tempstr = simpleSecStr
           elif ContainsStr(eltType,"FPtaper"):
               tempstr = taperStr
           
           retval = ReplaceStem(eltstr,tempstr)
           buffstr = f.Exec(retval)
           newbuffstr = ""
           a = find(buffstr,"V2")
           if a==-1:
               raise RuntimeError,"FMM Solver not selected - cannot sweep parameter"
           #format of buffstr is 'V2 <nmodes> <mmatch> <solverType> ......'            
           if (_setOpt==9):
               a = FindWhitespace(buffstr,2) #after <nmodes>
               newbuffstr = "V2 "+repr(_setVal)+buffstr[a:]
           elif (_setOpt==10):
               a0 = FindWhitespace(buffstr,2) #before <mmatch>
               a1 = FindWhitespace(buffstr,3) #after <mmatch>
               newbuffstr = buffstr[0:a0+1] + repr(_setVal) + buffstr[a1:]
           #this one is tricky! We need to change the solver id and reinsert buff!                
           elif (_setOpt==11):
               tempstr1 = retval[:-4] #remove 'buff'
               tempstr1 = tempstr1+'solvid'
               svid = f.Exec(tempstr1)
               if ((svid==65) or (svid==17) or (svid==33)): #real
                   if _setVal==1:
                       svid = 65
                   elif _setVal==2:
                       svid = 17
                   elif _setVal==3:
                       svid = 33
                   else:
                       raise RuntimeError,"Incorrect Solver Type selected"
               elif ((svid==73) or (svid==25) or (svid==41)): #complex
                   if _setVal==1:
                       svid = 73
                   elif _setVal==2:
                       svid = 25
                   elif _setVal==3:
                       svid = 41
                   else:
                       raise RuntimeError,"Incorrect Solver Type selected"
               f.AddCmd(tempstr1+"="+repr(svid))
               f.AddCmd(retval+"="+buffstr)
               
           f.AddCmd(retval+"="+newbuffstr)
          
       return
           
   def ChangeFMMparametersTaper(eltstr,eltType):
       CurrentSolverType=f.Exec(eltstr+".svp.solvid")
       if (CurrentSolverType != 65 and CurrentSolverType != 73 and
           CurrentSolverType !=17 and CurrentSolverType !=33 and
           CurrentSolverType !=25 and CurrentSolverType !=41 and
           CurrentSolverType>0):
           print("Not using FMM solver. You have to choose an FMM solver for these options")                 
       elif  (CurrentSolverType == 65 or CurrentSolverType == 73 or
           CurrentSolverType ==17 or CurrentSolverType ==33 or
           CurrentSolverType ==25 or CurrentSolverType ==41):
           tempstr = ""
           if ((ContainsStr(eltType,"FPWGsection")) or (ContainsStr(eltType,"FPbend"))):
               tempstr = simpleSecStr
           elif (ContainsStr(eltType,"FPtaper") or ContainsStr(eltType,"Ridge")):
               tempstr = taperStr
           
           retval = ReplaceStem(eltstr,tempstr)
           buffstr = f.Exec(retval)
           newbuffstr = ""
           a = find(buffstr,"V2")
           if a==-1:
               raise RuntimeError,"FMM Solver not selected - cannot sweep parameter"
           #format of buffstr is 'V2 <nmodes> <mmatch> <solverType> ......'            
           if (_setOpt==9):
               a = FindWhitespace(buffstr,2) #after <nmodes>
               newbuffstr = "V2 "+repr(_setVal)+buffstr[a:]
           elif (_setOpt==10):
               a0 = FindWhitespace(buffstr,2) #before <mmatch>
               a1 = FindWhitespace(buffstr,3) #after <mmatch>
               newbuffstr = buffstr[0:a0+1] + repr(_setVal) + buffstr[a1:]
           #this one is tricky! We need to change the solver id and reinsert buff!                
           elif (_setOpt==11):
               tempstr1 = retval[:-4] #remove 'buff'
               tempstr1 = tempstr1+'solvid'
               svid = f.Exec(tempstr1)
               if ((svid==65) or (svid==17) or (svid==33)): #real
                   if _setVal==1:
                       svid = 65
                   elif _setVal==2:
                       svid = 17
                   elif _setVal==3:
                       svid = 33
                   else:
                       raise RuntimeError,"Incorrect Solver Type selected"
               elif ((svid==73) or (svid==25) or (svid==41)): #complex
                   if _setVal==1:
                       svid = 73
                   elif _setVal==2:
                       svid = 25
                   elif _setVal==3:
                       svid = 41
                   else:
                       raise RuntimeError,"Incorrect Solver Type selected"
               f.AddCmd(tempstr1+"="+repr(svid))
               f.AddCmd(retval+"="+buffstr)
               
           f.AddCmd(retval+"="+newbuffstr)
          
       return       



   def ChangeBoundaryConditions(eltstr,eltType):
      global f
      global _setOpt
      global _setVal
      q=CheckCurrentBC(eltstr,eltType,_setOpt)
      if q!=0:
           if ((ContainsStr(eltType,"FPWGsection")) or (ContainsStr(eltType,"FPbend"))):
               #nslice = f.Exec(eltstr+".wg.nslice")
               chstr = ""
               if _setVal==1 or _setVal==2:
                   if _setOpt==14:
                       chstr = eltstr+".wg.topbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
                   elif _setOpt==15:
                       chstr = eltstr+".wg.botbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
                   elif _setOpt==16:
                       chstr = eltstr+".wg.lhsbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
                   elif _setOpt==17:
                       chstr = eltstr+".wg.rhsbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
               elif _setVal==3:
                   if _setOpt==14 or _setOpt==15:
                       chstr = eltstr+".wg.botbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
                       chstr = eltstr+".wg.topbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
                       print ("Both top and bottom BC changed to periodic")
                       print ("Periodic BC must be the same in opposite walls")
                   elif _setOpt==16 or _setOpt==17:
                       chstr = eltstr+".wg.lhsbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
                       chstr = eltstr+".wg.rhsbc.type="+repr(_setVal)
                       f.AddCmd(chstr)
                       print ("Both top and bottom BC changed to periodic")
                       print ("Periodic BC must be the same in opposite walls")
               #for sl in range(1,nslice+1):
               #,[sl])
               #flush message queue
               #f.Exec("")
               print("Element BC changed")
           #Taper case    
           elif (ContainsStr(eltType,"FPtaper")):
              if _setVal==1 or _setVal==2:
                  if _setOpt==14:
                      chstr = eltstr+".wg1.topbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.topbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                  elif _setOpt==15:
                      chstr = eltstr+".wg1.botbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.botbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                  elif _setOpt==16:
                      chstr = eltstr+".wg1.lhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.lhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                  elif _setOpt==17:
                      chstr = eltstr+".wg1.rhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.rhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
              elif _setVal==3:
                  if _setOpt==14 or _setOpt==15:
                      chstr = eltstr+".wg1.botbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.botbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg1.topbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.topbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      print ("Both top and bottom BC changed to periodic")
                      print ("Periodic BC must be the same in opposite walls")
                  elif _setOpt==16 or _setOpt==17:
                      chstr = eltstr+".wg1.lhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.lhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg1.rhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".wg2.rhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      print ("Both left and right BC changed to periodic")
                      print ("Periodic BC must be the same in opposite walls")
              print ("Taper  BC will be changed when taper is reevaluated")
              
           #Ridge Case   
           elif (ContainsStr(eltType,"Ridge")):
              if _setVal==1 or _setVal==2:
                 if _setOpt==14:
                    chstr = eltstr+".topbc.type="+repr(_setVal)
                    f.AddCmd(chstr)
                 elif _setOpt==15:
                    chstr = eltstr+".botbc.type="+repr(_setVal)
                    f.AddCmd(chstr)
                 elif _setOpt==16:
                    chstr = eltstr+".lhsbc.type="+repr(_setVal)
                    f.AddCmd(chstr)
                 elif _setOpt==17:
                    chstr = eltstr+".rhsbc.type="+repr(_setVal)
                    f.AddCmd(chstr)
                 print ("Ridge BC changed")
              elif _setVal==3:
                 if _setOpt==14 or _setOpt==15:
                      chstr = eltstr+".botbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".topbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      print ("Both top and bottom BC changed to periodic")
                      print ("Periodic BC must be the same in opposite walls")
                 elif _setOpt==16 or _setOpt==17:
                      chstr = eltstr+".lhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      chstr = eltstr+".rhsbc.type="+repr(_setVal)
                      f.AddCmd(chstr)
                      print ("Both left and right BC changed to periodic")
                      print ("Periodic BC must be the same in opposite walls")
      elif q==0:
           print ("Element skipped: Possibly Joint Section")                    
           
      return   
   
   def parameterSweep(comp):
       global f
       global _setOpt
       global _setVal
       global simpleSecStr
       global taperStr
       isComponent=0
       nelts = 1
       a = f.Exec(comp+".objtype")
       if ContainsStr(a,"FPcomponent"):
           isComponent=1        
           nelts = int(f.Exec("{%}.nelts",[comp]))
       for i in range(1,nelts+1):
           if isComponent==1:
               eltstr = comp+".eltlist["+repr(i)+"]"
           else:
               eltstr = comp
           eltType = f.Exec(eltstr+".objtype")

           #for component types, recurse
           if ContainsStr(eltType,"FPDeviceNode"):
               parameterSweep(eltstr)
        
           #for periodic sections, if they contain FPcomponent, recurse
           if ((ContainsStr(eltType,"FPperiodic")) or (ContainsStr(eltType,"FPsimplePeriodic"))):
               compstr = eltstr+".subsect"
               a = f.Exec(compstr)
               parameterSweep(compstr)
        
           #different rules for joint overlaps        
           if _setOpt==13:
               joinstr = ".joint_method"
               if ContainsStr(eltType,"FPsimpleJoint"):
                   joinstr = ".method"
               elif (ContainsStr(eltType,"FPperiodic")):
                  joinstr=".subjoint.method"
               if ((ContainsStr(eltType,"FPsimpleJoint")==0) and (ContainsStr(eltType,"FPbend")==0)
               and (ContainsStr(eltType,"FPtaper")==0)
               and (ContainsStr(eltType,"FPperiodic")==0)
               and (ContainsStr(eltType,"FPsimplePeriodic")==0)
                   and (ContainsStr(eltType,"Ridge")==0)):
                   continue
               if (ContainsStr(eltType,"FPbend")) and (_setVal==3): #bends do not support "Special complete"
                   continue
               f.AddCmd(eltstr+joinstr+"="+repr(_setVal))
               continue

           #Now the top and bottom boundary conditions
           elif _setOpt==14 or _setOpt==15 or _setOpt==16 or _setOpt==17:
              ChangeBoundaryConditions(eltstr,eltType)
        
           elif ((_setOpt==9) or (_setOpt==10) or (_setOpt==11)):
               if ((ContainsStr(eltType,"FPWGsection")) or (ContainsStr(eltType,"FPbend"))):
                   ChangeFMMparametersWGSection(eltstr,eltType)
               elif (ContainsStr(eltType,"FPtaper") or ContainsStr(eltType,"Ridge")):
                   ChangeFMMparametersTaper(eltstr,eltType)
        
           elif _setOpt==12:
               if ((ContainsStr(eltType,"FPWGsection")) or (ContainsStr(eltType,"FPbend"))):
                   CurrentSolverType=f.Exec(eltstr+".wg.evlist.svp.solvid")
               elif (ContainsStr(eltType,"FPtaper") or ContainsStr(eltType,"Ridge")):
                   CurrentSolverType=f.Exec(eltstr+".svp.solvid")
               elif ContainsStr(eltType,"Joint"):
                   CurrentSolverType=0
               if CurrentSolverType != 6 and CurrentSolverType != 14 and CurrentSolverType>0:
                   print("Not using effective index solver. You have to choose an effective index solver for these options")
               elif CurrentSolverType == 6 or CurrentSolverType == 14:
                   if ((ContainsStr(eltType,"FPWGsection")) or (ContainsStr(eltType,"FPbend"))):
                       retval = ReplaceStem(eltstr,simpleSecStr)
                       f.AddCmd(retval)
                   elif (ContainsStr(eltType,"FPtaper") or ContainsStr(eltType,"Ridge")):
                       retval = ReplaceStem(eltstr,taperStr)
                       f.AddCmd(retval)
                   continue
           else:
               if ((ContainsStr(eltType,"FPWGsection")) or (ContainsStr(eltType,"FPbend"))):
                   retval = ReplaceStem(eltstr,simpleSecStr)
                   f.AddCmd(retval)
               elif (ContainsStr(eltType,"FPtaper") or ContainsStr(eltType,"Ridge")):
                   retval = ReplaceStem(eltstr,taperStr)
                   f.AddCmd(retval)
               continue
             
            

   if fExec!=0:
       ConstructStrings()
       parameterSweep("mainComp.cdev")
       #flush message queue
       f.Exec("")

   print ("Done")    
   if hasPrevSet==1:
   	break
   aj1 = raw_input("Another Parameter? (y/n): ")
   if aj1=="N" or aj1=="n":
        print("Goodbye")
        break
   _setOpt = 0

#clean up variables
del _portNo
del _hostname
del _compname
del _setVal
del _setOpt
del f





