commands.py 7.37 KB
Newer Older
Daniel Brown's avatar
Daniel Brown committed
1
2
3
4
5
6
7
8
9
10
11
12
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 11:58:09 2013

@author: Daniel
"""
import numpy
from numpy import min,max
import exceptions
from components import *
from structs import *
from pykat.param import Param, putter
13
import pykat.exceptions as pkex
Daniel Brown's avatar
Daniel Brown committed
14
from collections import namedtuple
Daniel Brown's avatar
Daniel Brown committed
15
from pykat.utilities.optics.gaussian_beams import gauss_param
Daniel Brown's avatar
Daniel Brown committed
16
17

class Command(object):
18
19
20
21
22
23
    __metaclass__ = abc.ABCMeta
    
    def __init__(self, name):
        self.tag = None
        self.__removed = False
        self.__name = name
24
        
Daniel Brown's avatar
Daniel Brown committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    def getFinesseText(self):
        """ Base class for individual finesse optical components """
        raise NotImplementedError("This function is not implemented")

    @staticmethod
    def parseFinesseText(text):
        raise NotImplementedError("This function is not implemented")

    def _on_kat_add(self, kat):
        """
        Called when this component has been added to a kat object
        """
        self._kat = kat

39
40
41
42
    def remove(self):
        self._kat.remove(self)
        self.__removed = True
    
43
44
45
    @property
    def name(self): return self.__name
    
46
47
48
    @property
    def removed(self): return self.__removed
    
Daniel Brown's avatar
Daniel Brown committed
49
50
class cavity(Command):
    def __init__(self, name, c1, n1, c2, n2):
51
        Command.__init__(self, axis_type)
52
        
Daniel Brown's avatar
Daniel Brown committed
53
54
55
56
57
58
        self.__c1 = c1
        self.__c2 = c2
        self.__n1 = n1
        self.__n2 = n2

    def getFinesseText(self):
59
        return 'cav {0} {1} {2} {3} {4}'.format(self.name, self.__c1, self.__n1, self.__c2, self.__n2);
Daniel Brown's avatar
Daniel Brown committed
60
61
62
63

class gauss(object):
    @staticmethod
    def parseFinesseText(text, kat):
64
        values = text.split()
65
        if not values[0].startswith("gauss") or (len(values) != 6 and len(values) != 8):
66
            raise pkex.BasePyKatException("'{0}' not a valid Finesse gauss command".format(text))        
67
        
68
69
70
71
        name = values[1]
        component = values[2]
        node = values[3]
        
Daniel Brown's avatar
Daniel Brown committed
72
        if values[0]:
73
            if len(values) == 6:
Daniel Brown's avatar
Daniel Brown committed
74
                gp = gauss_param(w0=values[-2], z=values[-1])
75
            elif len(values) == 8:
Daniel Brown's avatar
Daniel Brown committed
76
77
                gpx = gauss_param(w0=values[-4], z=values[-3])
                gpy = gauss_param(w0=values[-2], z=values[-1])
78
79
        elif values[0].endswith("*"):
            if len(values) == 6:
Daniel Brown's avatar
Daniel Brown committed
80
                gp = gauss_param(z=values[-2], zr=values[-1])
81
            elif len(values) == 8:
Daniel Brown's avatar
Daniel Brown committed
82
83
                gpx = gauss_param(z=values[-4], zr=values[-3])
                gpy = gauss_param(z=values[-2], zr=values[-1])
Daniel Brown's avatar
Daniel Brown committed
84
        elif values[0].endswith("**"):
85
            if len(values) == 6:
Daniel Brown's avatar
Daniel Brown committed
86
                gp = gauss_param(w=values[-2], rc=values[-1])
87
            elif len(values) == 8:
Daniel Brown's avatar
Daniel Brown committed
88
89
                gpx = gauss_param(w=values[-4], rc=values[-3])
                gpy = gauss_param(w=values[-2], rc=values[-1])
Daniel Brown's avatar
Daniel Brown committed
90
91
92
        else:
            raise pkex.BasePyKatException("Unexpected ending to gauss command '{0}'".format(text))
            
Daniel Brown's avatar
Daniel Brown committed
93
94
95
96
        if len(values) == 6:
            kat.nodes[node].setGauss(kat.components[component], gp)
        else:
            kat.nodes[node].setGauss(kat.components[component], gpx, gpy)
97
            
Daniel Brown's avatar
Daniel Brown committed
98
class tf(Command):
99
    fQ = namedtuple('fQ', ['f', 'Q'])
Daniel Brown's avatar
Daniel Brown committed
100
101
    
    def __init__(self, name, poles, zeros):
102
        Command.__init__(self, axis_type)
Daniel Brown's avatar
Daniel Brown committed
103
104
        pass
      
Daniel Brown's avatar
Daniel Brown committed
105
class xaxis(Command):
106
107
108
109
110
    """
    The xaxis object is a unique object to each pykat.finesse.kat instance. It provides
    and interface to the xaxis command in FINESSE.
    """
    
111
    def __init__(self, scale, limits, param, steps, comp=None, axis_type="xaxis"):
112
113
114
115
116
117
118
119
120
        """
        Typical usage:
            xaxis(["lin" or "log"], [upper, lower], param, steps)
            
        param must be an object of the type pykat.param.Param whose
        isPutable() member returns true.
        
        steps is the number of points to compute between upper and lower limits.
        """
121
        Command.__init__(self, axis_type)
122
        
Daniel Brown's avatar
Daniel Brown committed
123
124
125
126
127
128
        self._axis_type = axis_type

        self.x = putter("x1")
        self.mx = putter("mx1")

        if scale == "lin":
129
            scale = Scale.linear
Daniel Brown's avatar
Daniel Brown committed
130
131
132
133
        elif scale == "log":
            scale = Scale.logarithmic
        elif isinstance(scale, str):
            # else we have a string but not a recognisable one
134
            raise pkex.BasePyKatException("scale argument '{0}' is not valid, must be 'lin' or 'log'".format(scale))
Daniel Brown's avatar
Daniel Brown committed
135
136

        if scale != Scale.linear and scale != Scale.logarithmic:
137
            raise pkex.BasePyKatException("scale is not Scale.linear or Scale.logarithmic")
Daniel Brown's avatar
Daniel Brown committed
138
139
140
141

        self.scale = scale

        if numpy.size(limits) != 2 :
142
            raise pkex.BasePyKatException("limits input should be a 2x1 vector of limits for the xaxis")
Daniel Brown's avatar
Daniel Brown committed
143
144
145
146

        self.limits = numpy.array(SIfloat(limits)).astype(float)

        if steps <= 0 :
147
            raise pkex.BasePyKatException("steps value should be > 0")
Daniel Brown's avatar
Daniel Brown committed
148
149
150
151
152

        self.steps = int(steps)

        if isinstance(param, str):
            self.__param = param
153
154
155
156
            if comp == None:
                raise pkex.BasePyKatException("If parameter is set with a string, the comp argument must set the component name")
                
            self.__comp = comp
Daniel Brown's avatar
Daniel Brown committed
157
        elif not isinstance(param, Param) :
158
            raise pkex.BasePyKatException("param argument is not of type Param")
Daniel Brown's avatar
Daniel Brown committed
159
160
        else:
            self.__param = param
161
            self.__comp = param._owner()
Daniel Brown's avatar
Daniel Brown committed
162

163
164
165
166
    @property
    def param(self): return self.__param
    @param.setter
    def param(self, value):
167
168
169
170
171
172
        if not isinstance(value, Param):
            raise pkex.BasePyKatException("param argument is not of type Param")
        else:
            self.__param = value
            self.__comp = value._owner()
            
Daniel Brown's avatar
Daniel Brown committed
173
174
    @staticmethod
    def parseFinesseText(text):
175
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
176
177

        if values[0] != "xaxis" and values[0] != "xaxis*":
178
            raise pkex.BasePyKatException("'{0}' not a valid Finesse xaxis command".format(text))
Daniel Brown's avatar
Daniel Brown committed
179
180
181
182
183
184

        axis_type = values[0]

        values.pop(0) # remove initial value

        if len(values) != 6:
185
            raise pkex.BasePyKatException("xaxis Finesse code format incorrect '{0}'".format(text))
Daniel Brown's avatar
Daniel Brown committed
186

187
        return xaxis(values[2], [values[3], values[4]], values[1], values[5], comp=values[0], axis_type=axis_type)
Daniel Brown's avatar
Daniel Brown committed
188
189
190

    def getFinesseText(self):
        # store either the component name of the string provided
191
        comp_name = self.__comp.name if hasattr(self.__comp, "name") else self.__comp
Daniel Brown's avatar
Daniel Brown committed
192
        param_name = self.__param.name if isinstance(self.__param, Param) else self.__param
193
        
Daniel Brown's avatar
Daniel Brown committed
194
195
196
197
198
        return '{axis_type} {0} {1} {2} {3} {4} {5}'.format(
                comp_name, param_name, self.scale,
                min(self.limits), max(self.limits), self.steps, axis_type=self._axis_type);

class x2axis(xaxis):
199
200
    def __init__(self, scale, limits, param, steps, comp=None):
        xaxis.__init__(self, scale, limits, param, steps, comp=comp, axis_type="x2axis")
Daniel Brown's avatar
Daniel Brown committed
201
202
203
204
205
        self.x = putter("x2")
        self.mx = putter("mx2")

    @staticmethod
    def parseFinesseText(text):
206
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
207
208

        if values[0] != "x2axis" and values[0] != "x2axis*":
209
            raise pkex.BasePyKatException("'{0}' not a valid Finesse xaxis command".format(text))
Daniel Brown's avatar
Daniel Brown committed
210
211
212
213
214
215

        axis_type = values[0]

        values.pop(0) # remove initial value

        if len(values) != 6:
216
            raise pkex.BasePyKatException("xaxis Finesse code format incorrect '{0}'".format(text))
Daniel Brown's avatar
Daniel Brown committed
217

218
        return x2axis(values[2], [values[3], values[4]], values[1], values[5], comp=values[0])