Commit 9358872b authored by Daniel Brown's avatar Daniel Brown

Changing put commands to be written near the object putting the value rather...

Changing put commands to be written near the object putting the value rather than the target. Fixing some constant issues when removed.
parent 390203b4
......@@ -7,6 +7,7 @@ import os
import re
import pykat.exceptions as pkex
import numpy as np
import pykat
__suffix = {'y': 'e-24', # yocto
'z': 'e-21', # zepto
......@@ -28,6 +29,9 @@ __suffix = {'y': 'e-24', # yocto
__exceptions = ["$fs", "$mfs"]
def SIfloat(value):
if isinstance(value, pykat.commands.Constant):
return '$' + value.name
if str(value).startswith('$'):
return value
......
......@@ -107,6 +107,10 @@ class Constant(Command):
self.usedBy = []
self._freeze()
def _on_kat_remove(self):
Command._on_kat_remove(self)
self._kat.constants.pop(self.name)
def getFinesseText(self):
return "const {name} {value}".format(name=self.name, value=self.value)
......@@ -124,6 +128,8 @@ class Constant(Command):
@value.setter
def value(self, Value): self.__value = SIfloat(Value)
def __str__(self): return "$"+self.name
class variable(Command):
def __init__(self, name, value):
Command.__init__(self, name, False)
......@@ -172,7 +178,7 @@ class func(Command):
rtn.append("noplot " + self.name)
rtn.append("func {name} = {value}".format(name=self.name, value=str(self.value)))
rtn.extend(self.output._getPutFinesseText())
return rtn
@staticmethod
......@@ -217,15 +223,20 @@ class lock(Command):
def getFinesseText(self):
if self.enabled:
rtn = []
cmds = "{name} {var} {gain} {accuracy}".format( name=self.name,
var=str(self.variable),
gain=str(self.gain),
accuracy=str(self.accuracy))
if self.singleLock:
return "lock* %s" % cmds
rtn.append("lock* %s" % cmds)
else:
return "lock %s" % cmds
rtn.append("lock %s" % cmds)
rtn.extend(self.output._getPutFinesseText())
return rtn
else:
return None
......
......@@ -824,8 +824,11 @@ class Signals(object):
rtn.extend(p.getFinesseText())
return rtn
class Block:
class Placeholder(object):
pass
def __init__(self, name):
self.__name = name
self.contents = [] # List of objects and strings of finesse code
......@@ -1342,7 +1345,7 @@ class kat(object):
after_process = ([], [])
for line in commands:
if len(line.strip()) >= 2:
if len(line.strip()) >= 1:
line = line.strip()
# Looking for block start or end
......@@ -1430,6 +1433,11 @@ class kat(object):
first = line.split(" ",1)[0]
obj = None
def get_ph():
ph = Block.Placeholder()
self.__blocks[self.__currentTag].contents.append(ph)
return ph
if(first == "m" or first == "m1" or first == "m2"):
obj = pykat.components.mirror.parseFinesseText(line)
......@@ -1487,13 +1495,13 @@ class kat(object):
elif(first == "pdtype"):
after_process[0].append((line, self.__currentTag))
elif(first == "cav"):
after_process[0].append((line, self.__currentTag))
after_process[0].append((line, self.__currentTag, get_ph()))
elif(first == "func"):
after_process[0].append((line, self.__currentTag))
after_process[0].append((line, self.__currentTag, get_ph()))
elif(first == "variable"):
after_process[0].append((line, self.__currentTag))
after_process[0].append((line, self.__currentTag, get_ph()))
elif(first == "lock"):
after_process[0].append((line, self.__currentTag))
after_process[0].append((line, self.__currentTag, get_ph()))
elif(first == "attr"):
after_process[0].append((line, self.__currentTag))
elif(first == "noxaxis"):
......@@ -1541,7 +1549,7 @@ class kat(object):
if self.verbose:
print ("Ignoring Gnuplot/Python terminal command '{0}'".format(line))
elif(first == "fsig"):
after_process[0].append((line, self.__currentTag))
after_process[0].append((line, self.__currentTag, get_ph()))
elif(first == "noplot"):
after_process[1].append((line, self.__currentTag))
elif(first == "put" or first == "put*"):
......@@ -1579,24 +1587,33 @@ class kat(object):
# components to exist first before they can be processed
for _ in after_process:
for item in _:
line = item[0]
first, rest = line.split(" ",1)
block = item[1]
if len(item) == 2:
line = item[0]
first, rest = line.split(" ",1)
block = item[1]
plc_holder = None
elif len(item) == 3:
line = item[0]
first, rest = line.split(" ",1)
block = item[1]
plc_holder = item[2]
else:
pkex.printWarning("Unexpected number of items")
if first == "gauss" or first == "gauss*" or first == "gauss**":
pykat.commands.gauss.parseFinesseText(line, self)
elif (first == "cav"):
self.add(pykat.commands.cavity.parseFinesseText(line, self), block=block)
self.add(pykat.commands.cavity.parseFinesseText(line, self), block=block, placeholder=plc_holder)
elif (first == "lock"):
self.add(pykat.commands.lock.parseFinesseText(line, self), block=block)
self.add(pykat.commands.lock.parseFinesseText(line, self), block=block, placeholder=plc_holder)
elif (first == "func"):
self.add(pykat.commands.func.parseFinesseText(line, self), block=block)
self.add(pykat.commands.func.parseFinesseText(line, self), block=block, placeholder=plc_holder)
elif (first == "variable"):
self.add(pykat.commands.variable.parseFinesseText(line, self), block=block)
self.add(pykat.commands.variable.parseFinesseText(line, self), block=block, placeholder=plc_holder)
elif (first == "noplot"):
if not hasattr(self, rest):
......@@ -2281,12 +2298,29 @@ class kat(object):
def hasNamedObject(self, name):
return name in self.__components or name in self.__detectors or name in self.__commands
def add(self, obj, block=NO_BLOCK):
def add(self, obj, block=NO_BLOCK, placeholder=None):
"""
Adds an object into the kat object. The object added should be a pykat object, such as
a component, detector, command, etc.
block: Name of the block to put this component into to
placeholder: The position in the block can be reserved by adding in a Block.Placeholder object.
This placeholder object will be replaced with the added object.
"""
try:
self._unfreeze()
obj.tag = block
self.__blocks[block].contents.append(obj)
if placeholder is None:
self.__blocks[block].contents.append(obj)
else:
c = self.__blocks[block].contents
try:
c[c.index(placeholder)] = obj
del placeholder
except ValueError as ex:
raise pkex.BasePyKatException("Placeholder for {} was no found in block {}".format(obj, block))
if isinstance(obj, Component):
if obj.name in self.__components :
......
......@@ -377,7 +377,7 @@ def assert_aligo_ifo_kat(kat):
if not isinstance(kat.IFO, ALIGO_IFO):
raise pkex.BasePyKatException("\033[91mkat file is not an ALIGO_IFO compatiable kat\033[0m")
def make_kat(name="design", katfile=None, verbose = False, debug=False):
def make_kat(name="design", katfile=None, verbose = False, debug=False, keepComments=False, preserveConstants=False):
"""
Returns a kat object and fills in the kat.IFO property for storing
the associated interferometer data.
......@@ -390,6 +390,9 @@ def make_kat(name="design", katfile=None, verbose = False, debug=False):
- design_low_power: A file based on the design parameters for the final aLIGO setup.
20W input, T_SRM = 35%. The higher SRM transmission mirror is used for low power
operation. 20W input power from O1 observation.
keepComments: If true it will keep the original comments from the file
preserveComments: If true it will keep the const commands in the kat
"""
names = ['design', 'design_low_power']
......@@ -421,7 +424,7 @@ def make_kat(name="design", katfile=None, verbose = False, debug=False):
katkile = os.path.join(kat.IFO._data_path, name+".kat")
kat.load(katkile)
kat.load(katkile, keepComments=keepComments, preserveConstants=preserveConstants)
kat.IFO.rawBlocks.read(katkile)
# ----------------------------------------------------------------------
......
%%% FTblock header
#--------------------------------------------------------------------------
# an aLIGO file to introduce a PI to one arm
# based on
# aLIGO_IFO_AWC_tuning_DCoff_maxtem4.kat (details in the original file header)
# An aLIGO design file base on older aLIGO_IFO_AWC_tuning_DCoff_maxtem4.kat
# (details in the original file header)
#
# updated to re-include locks
# (errsig, powers and locks blocks, plus EOMs 1 and 2 (9 and 45kHz))
......@@ -88,7 +87,6 @@ s lmod3 1 n4 nREFL # TODO
# PRM
# AR surface
m2 PRMAR 0 40u 0 nREFL nPRMs1
#bs2 PRMAR 35u 4.5u 0 0 nREFL nPRMAR1 nPRMs1 nPRMAR2
# Substrate
s sPRMsub1 0.0737 $nsilica nPRMs1 nPRMs2
# HR surface
......@@ -155,7 +153,6 @@ s ITMY_th2 0 nITM_th3 nITMY1
# Y arm input mirror
m2 ITMYAR 0 20u 0 nITMY1 nITMYs1
#bs2 ITMYAR 20u 0 0 0 nITMY1 nITMYAR1 nITMYs1 nITMYAR2
s ITMYsub 0.2 $nsilica nITMYs1 nITMYs2
m1 ITMY 0.014 $Mloss $phi_ITMY nITMYs2 nITMY2
attr ITMY Rc -1934
......@@ -167,7 +164,6 @@ s LY $Larm nITMY2 nETMY1
m1 ETMY 5u $Mloss $phi_ETMY nETMY1 nETMYs1
s ETMYsub 0.2 $nsilica nETMYs1 nETMYs2
m2 ETMYAR 0 500u 0 nETMYs2 nPTY
#bs2 ETMYAR 500u 0 0 0 nETMYs2 nETMYAR1 nPTY nETMYAR1
attr ETMY Rc 2245
attr ETMY mass 40
attr ITMY mass 40
......@@ -189,7 +185,6 @@ s ITMX_th2 0 nITMX_th3 nITMX1
# X arm input mirror
m2 ITMXAR 0 20u 0 nITMX1 nITMXs1
#bs2 ITMXAR 20u 0 0 0 nITMX1 nITMXAR1 nITMXs1 nITMXAR2
s ITMXsub 0.2 $nsilica nITMXs1 nITMXs2
m1 ITMX 0.014 $Mloss $phi_ITMX nITMXs2 nITMX2
attr ITMX Rc -1934
......@@ -200,7 +195,6 @@ s LX $Larm nITMX2 nETMX1
# X arm end mirror
m1 ETMX 5u $Mloss $phi_ETMX nETMX1 nETMXs1
s ETMXsub 0.2 $nsilica nETMXs1 nETMXs2
#bs2 ETMXAR 500u 0 0 0 nETMXs2 nETMXAR1 nPTX nETMXAR1
m2 ETMXAR 0 500u 0 nETMXs2 nPTX
attr ETMX Rc 2245
attr ETMX mass 40
......@@ -275,7 +269,6 @@ const phi_BS 0
%%% FTblock powers
###########################################################################
pd P_DC_AS nSRM2
# pd P_DC_OMC nOMC_HROC_trans
pd PIMCtrans nREFL*
pd Px nITMX2
pd Py nITMY2
......@@ -285,7 +278,6 @@ ad prc0 0 nPRM2
ad prcf1 $f1 nPRM2
ad prcf2 $f2 nPRM2
#ad aoc0 0 nOMC_HROC_trans
ad asc0 0 nSRM2
ad asf1 $f1 nSRM2
ad asf2 $f2 nSRM2
......
......@@ -47,25 +47,25 @@ class putable(object):
if var is not None:
self._putter.register(self)
def _getPutFinesseText(self):
rtn = []
if self._isPutable and self._putter is not None:
putter_enabled = True
if hasattr(self._putter.owner, 'enabled'):
putter_enabled = self._putter.owner.enabled
if putter_enabled:
if self._alt:
alt = '*'
else:
alt = ''
# if something is being put to this
rtn.append("put{alt} {comp} {param} ${value}".format(alt=alt, comp=self._component_name, param=self._parameter_name, value=self._putter.put_name()))
return rtn
# def _getPutFinesseText(self):
# rtn = []
#
# if self._isPutable and self._putter is not None:
# putter_enabled = True
#
# if hasattr(self._putter.owner, 'enabled'):
# putter_enabled = self._putter.owner.enabled
#
# if putter_enabled:
# if self._alt:
# alt = '*'
# else:
# alt = ''
#
# # if something is being put to this
# rtn.append("put{alt} {comp} {param} ${value}".format(alt=alt, comp=self._component_name, param=self._parameter_name, value=self._putter.put_name()))
#
# return rtn
class putter(object):
......@@ -120,6 +120,28 @@ class putter(object):
def put_name(self): return self._put_name
def _getPutFinesseText(self):
rtn = []
has = hasattr(self.owner, 'enabled')
if (has and not self.owner.enabled):
return rtn
if self.isPutter and len(self.putees) > 0:
for _ in self.putees:
has = hasattr(_.owner, 'enabled')
if (has and _.owner.enabled) or not has:
if _._alt:
alt = '*'
else:
alt = ''
# if something is being put to this
rtn.append("put{alt} {comp} {param} ${value}".format(alt=alt, comp=_._component_name, param=_._parameter_name, value=self.put_name()))
return rtn
@canFreeze
class Param(putable, putter):
......@@ -195,6 +217,8 @@ class Param(putable, putter):
raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))
else:
if self._isConst:
if self._constName[1:] not in self.owner._kat.constants:
raise pkex.BasePyKatException("Parameter {}.{} could not find a Finesse constant called `{}`".format(self.owner.name, self.name, self._constName))
return self.owner._kat.constants[self._constName[1:]].value
else:
return self._value
......@@ -234,8 +258,8 @@ class Param(putable, putter):
raise pkex.BasePyKatException("{0} has been removed from the simulation".format(self._owner().name))
rtn = []
if self.isPutable: rtn.extend(self._getPutFinesseText())
#if self.isPutable: rtn.extend(self._getPutFinesseText())
if self.isPutter: rtn.extend(self._getPutFinesseText())
# if this parameter is being put somewhere then we need to
# set it as a variable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment