diff --git a/bin/test_plot.py b/bin/test_plot.py index 851d55079f0ac2d3f57b9b5416559de7f65bcdca..65a466e55fa275728fad25f1b38cae14cfcb2f56 100644 --- a/bin/test_plot.py +++ b/bin/test_plot.py @@ -20,6 +20,8 @@ yaxis abs:deg pd pd_cav n3 cav c1 m1 n3 m2 n4 + +attr m1 Rc 1 """ kat = finesse.kat() diff --git a/pykat/components.py b/pykat/components.py index bc203fb9eb541735eb823ceb3af7f7950e22bc1c..50897522d5f937727d889ec896e840ecf8b7789e 100644 --- a/pykat/components.py +++ b/pykat/components.py @@ -243,13 +243,43 @@ class AbstractMirrorComponent(Component): self.Rcx.value = SIfloat(value) self.Rcy.value = SIfloat(value) + def parseAttribute(self, key, value): + if key in ["Rcx", "Rx", "ROCx", "rx", "rcx", "rocx"]: + self.Rcx = value + elif key in ["Rcy", "Ry", "ROCy", "ry", "rcy", "rocy"]: + self.Rcy = value + elif key in ["Rc", "ROC", "r", "rc", "roc"]: + self.Rc = value + elif key in ["M, m, Mass, mass"]: + self.mass = value + elif key in ["xbeta, xBeta"]: + self.xbeta = value + elif key in ["ybeta, yBeta"]: + self.ybeta = value + elif key in ["x_off"]: + self.x_offset = value + elif key in ["y_off"]: + self.y_offset = value + elif key in ["r_ap"]: + self.r_ap = value + else: + return False + + return True + class mirror(AbstractMirrorComponent): def __init__(self,name,node1,node2,R=None,T=None,L=None,phi=0,Rcx=None,Rcy=None,xbeta=None,ybeta=None,mass=None, r_ap=None): super(mirror, self).__init__(name, R, T, L, phi, Rcx, Rcy, xbeta, ybeta, mass, r_ap) self._requested_node_names.append(node1) self._requested_node_names.append(node2) - + + def parseAttributes(self, values): + + for key in values.keys(): + if not self.parseAttribute(key, values[key]): + raise pkex.BasePyKatException("No attribute {0} for mirrors".format(key)) + @staticmethod def parseFinesseText(text): values = text.split() @@ -301,13 +331,22 @@ class beamSplitter(AbstractMirrorComponent): self._requested_node_names.append(node3) self._requested_node_names.append(node4) - self.__alpha = Param("alpha", self, SIfloat(alpha)) + self.__alpha = AttrParam("alpha", self, SIfloat(alpha)) @property def alpha(self): return self.__alpha @alpha.setter def alpha(self,value): self.__alpha.value = SIfloat(value) + def parseAttributes(self, values): + + for key in values.keys(): + if not self.parseAttribute(key, values[key]): + if key == "alpha": + self.alpha = values[key] + else: + raise pkex.BasePyKatException("No attribute {0} for mirrors".format(key)) + @staticmethod def parseFinesseText(text): values = text.split() @@ -365,7 +404,6 @@ class space(Component): self.__L = Param("L", self, SIfloat(L)) self.__n = Param("n", self, SIfloat(n)) - self.__g = AttrParam("g", self, g) self.__gx = AttrParam("gx", self, gx) self.__gy = AttrParam("gy", self, gy) @@ -379,10 +417,17 @@ class space(Component): def n(self,value): self.__n.value = SIfloat(value) @property - def g(self): return self.__g + def g(self): + if self.__gx.value == self.__gy.value: + return self.__g + else: + raise pkex.BasePyKatException("Gouy phase in x and y directions are different, use gx and gy properties instead") + @g.setter - def g(self,value): self.__g.value = SIfloat(value) - + def g(self,value): + self.__gx.value = SIfloat(value) + self.__gy.value = SIfloat(value) + @property def gx(self): return self.__gx @gx.setter @@ -393,6 +438,19 @@ class space(Component): @gy.setter def gy(self,value): self.__gy.value = SIfloat(value) + def parseAttributes(self, values): + + for key in values.keys(): + if key in ["gx","gouyx"]: + self.__gx.value = values[key] + elif key in ["gy", "gouyy"]: + self.__gy.value = values[key] + elif key in ["g, gouy"]: + self.__gx.value = values[key] + self.__gy.value = values[key] + else: + raise pkex.BasePyKatException("No attribute {0} for spaces".format(key)) + @staticmethod def parseFinesseText(text): values = text.split() @@ -747,6 +805,14 @@ class laser(Component): @phase.setter def phase(self,value): self.__phase.value = float(value) + def parseAttributes(self, values): + + for key in values.keys(): + if key == "noise": + self.__noise.value = values[key] + else: + raise pkex.BasePyKatException("No attribute {0} at laser".format(key)) + @staticmethod def parseFinesseText(text): values = text.split() diff --git a/pykat/finesse.py b/pykat/finesse.py index e34f30a100b1072d47c45962670946eeaab14e15..4ea91c747345ee71122a873c5fbca5d0440c8ca1 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -34,6 +34,9 @@ import pykat import warnings import re +import itertools + +import collections from collections import namedtuple, OrderedDict from pykat.node_network import NodeNetwork @@ -452,6 +455,8 @@ class kat(object): after_process.append(line) elif(first == "pdtype"): after_process.append(line) + elif(first == "attr"): + after_process.append(line) elif(first == "noxaxis"): self.noxaxis = True elif(first == "phase"): @@ -522,6 +527,27 @@ class kat(object): raise pkex.BasePyKatException("pdtype command `{0}` refers to non-existing detector".format(component_name)) else: raise pkex.BasePyKatException("pdtype command `{0}` is incorrect.".format(line)) + elif(first == "attr"): + v = line.split() + + if len(v) < 4: + raise pkex.BasePyKatException("attr command `{0}` is incorrect.".format(line)) + else: + # get the component/detector in question + if v[1] in self.__components: + comp = self.__components[v[1]] + elif v[1] in self.__detectors: + comp = self.__detectors[v[1]] + else: + raise pkex.BasePyKatException("Could not find the component '{0}' for attr command in line '{1}'".format(v[1], line)) + + if len(v[2:]) % 2 == 1: + raise pkex.BasePyKatException("Attr command '{0}' must specify both parameter and value pairs".format(line)) + + # convert split list to key value pairs + kv = dict(itertools.izip_longest(*[iter(v[2:])] * 2, fillvalue=None)) + + comp.parseAttributes(kv) self.__currentTag = NO_BLOCK