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

@author: Daniel
"""
import exceptions
import pykat
from pykat.node_network import *
Daniel Brown's avatar
Daniel Brown committed
10
11
12
13

import pykat.gui.resources
import pykat.gui.graphics
from pykat.gui.graphics import *
Daniel Brown's avatar
Daniel Brown committed
14

15
class Component(object) :
16
    def __init__(self, name):
Daniel Brown's avatar
Daniel Brown committed
17
18
        self.__name = name
        self._svgItem = None
Daniel Brown's avatar
Daniel Brown committed
19
        self._nodes = []
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        self._requested_node_names = []
        self._kat = None
    
    def _on_kat_add(self, kat):
        """
        Called when this component has been added to a kat object
        """
        self._kat = kat
        
        for node_name in self._requested_node_names:
            self._addNode(node_name)
        
    @staticmethod
    def parseFinesseText(text):    
        raise NotImplementedError("This function is not implemented")
35
36
37
    
    def setAttr(name, value):    
        raise NotImplementedError("This function is not implemented")
Daniel Brown's avatar
Daniel Brown committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        
    def getFinesseText(self):
        """ Base class for individual finesse optical components """    
        raise NotImplementedError("This function is not implemented")
        
    def getQGraphicsItem(self):    
        return None      
        
    def _addNode(self, name):
        """ Adds a node in sequential order to the component, i.e. add them
        n1, n2, n3, n4... etc. by the name of the node"""
            
        n = self._kat.nodes.createNode(name)
        
        if n == None:
53
            raise exceptions.RuntimeError("createNode did not return a node for '{0}'".format(name))
Daniel Brown's avatar
Daniel Brown committed
54
        else:
55
56
            n.connect(self)
                
Daniel Brown's avatar
Daniel Brown committed
57
            self._nodes.append(n)
Daniel Brown's avatar
Daniel Brown committed
58
59
60
61
62
        
        return n
        
    def getNodes(self):
        """ Returns a copy of the nodes the component has """
Daniel Brown's avatar
Daniel Brown committed
63
        return self._nodes[:]        
Daniel Brown's avatar
Daniel Brown committed
64
65
66
67
68
69
            
    def __getname(self):
        return self.__name      
        
    name = property(__getname)
    
70
71
72
73
class Param(float):
    def __new__(self,name,value):
        return float.__new__(self,value)
         
Daniel Brown's avatar
Daniel Brown committed
74
75
76
    def __init__(self,name,value):
        self.__name = name
        
77
78
    name = property(lambda self: self.__name)
           
Daniel Brown's avatar
Daniel Brown committed
79
class mirror(Component):
Daniel Brown's avatar
Daniel Brown committed
80
    def __init__(self,name,node1,node2,R=0,T=0,phi=0,Rcx=0,Rcy=0,xbeta=0,ybeta=0,mass=0, r_ap=0):
Daniel Brown's avatar
Daniel Brown committed
81
        
82
        Component.__init__(self,name)
Daniel Brown's avatar
Daniel Brown committed
83
        
84
85
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
Daniel Brown's avatar
Daniel Brown committed
86
87

        self.__r_ap = float(r_ap)        
88
        self.__mass = float(mass)
89
90
91
92
93
94
95
        self.__R = float(R)
        self.__T = float(T)
        self.__phi = float(phi)
        self.__Rcx = float(Rcx)
        self.__Rcy = float(Rcy)
        self.__xbeta = float(xbeta)
        self.__ybeta = float(ybeta)
96
    
Daniel Brown's avatar
Daniel Brown committed
97
98
99
100
101
    @property
    def r_ap(self): return Param('r_ap', self.__mass)
    @r_ap.setter
    def r_ap(self,value): self.__aperture = float(value)

102
103
104
105
106
    @property
    def mass(self): return Param('mass', self.__mass)
    @mass.setter
    def mass(self,value): self.__mass = float(value)
    
107
    @property
108
    def R(self): return Param('R', self.__R)
109
    @R.setter
110
111
    def R(self,value): self.__R = float(value)
    
112
113
114
    @property
    def T(self): return Param('T', self.__T)
    @T.setter
115
    def T(self,value): self.__T = float(value)
116
117
118
119
        
    @property
    def phi(self): return Param('phi', self.__phi)
    @phi.setter
120
    def phi(self,value): self.__phi = float(value)
121
122
123
124
    
    @property
    def Rcx(self): return Param('Rcx', self.__Rcx)
    @Rcx.setter
125
126
    def Rcx(self,value): self.__Rcx = float(value)
    
127
128
129
    @property
    def Rcy(self): return Param('Rcy', self.__Rcy)
    @Rcy.setter
130
    def Rcy(self,value): self.__Rcy = float(value)
131
132
133
134
    
    @property
    def xbeta(self): return Param('xbeta', self.__xbeta)
    @xbeta.setter
135
    def xbeta(self,value): self.__xbeta = float(value)
136
    
137
138
139
    @property
    def ybeta(self): return Param('ybeta', self.__ybeta)
    @ybeta.setter
140
    def ybeta(self,value): self.__ybeta = float(value)
141
142
143
    
    @property
    def Rc(self):
Daniel Brown's avatar
Daniel Brown committed
144
145
146
147
148
        if self.Rcx == self.Rcy:
            return self.Rcx
        else:
            return [self.Rcx, self.Rcy]
    
149
150
    @Rc.setter
    def Rc(self,value):
151
152
        self.Rcx = float(value)
        self.Rcy = float(value)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    
    @staticmethod
    def parseFinesseText(text):
        values = text.split(" ")

        if values[0] != "m":
            raise exceptions.RuntimeError("'{0}' not a valid Finesse mirror command".format(text))

        values.pop(0) # remove initial value
        
        if len(values) != 6:
            raise exceptions.RuntimeError("Mirror Finesse code format incorrect '{0}'".format(text))

        return mirror(values[0], values[4], values[5], R=values[1], T=values[2], phi=values[3])
Daniel Brown's avatar
Daniel Brown committed
167
168
169
        
    def getFinesseText(self):        
        rtn = []
170
171
172
173
174
        nodes = self.getNodes()
        
        if len(nodes) != 2:
            raise exceptions.RuntimeError("Not enough nodes for mirror")
            
Daniel Brown's avatar
Daniel Brown committed
175
        rtn.append('m {0} {1} {2} {3} {4} {5}'.format(
176
                self.name, self.__R, self.__T, self.__phi,
177
                nodes[0].name, nodes[1].name))
Daniel Brown's avatar
Daniel Brown committed
178
179

        if self.r_ap != 0: rtn.append("attr {0} r_ap {1}".format(self.name,self.__r_ap))            
180
        if self.mass != 0: rtn.append("attr {0} mass {1}".format(self.name,self.__mass))
181
182
183
184
        if self.Rcx != 0: rtn.append("attr {0} Rcx {1}".format(self.name,self.__Rcx))
        if self.Rcy != 0: rtn.append("attr {0} Rcy {1}".format(self.name,self.__Rcy))
        if self.xbeta != 0: rtn.append("attr {0} xbeta {1}".format(self.name,self.__xbeta))
        if self.ybeta != 0: rtn.append("attr {0} ybeta {1}".format(self.name,self.__ybeta))
Daniel Brown's avatar
Daniel Brown committed
185
186
187
188
189
        
        return rtn
        
    def getQGraphicsItem(self):
        if self._svgItem == None:
190
            nodes = self.getNodes()
Daniel Brown's avatar
Daniel Brown committed
191
192
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/mirror_flat.svg", self ,[(-4,15,nodes[0]), (14,15,nodes[1])])
            
Daniel Brown's avatar
Daniel Brown committed
193
194
195
196
        return self._svgItem
   
   
class space(Component):
197
198
    def __init__(self, name, node1, node2, L=0, n=1):
        Component.__init__(self,name,)
Daniel Brown's avatar
Daniel Brown committed
199
        
200
201
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
Daniel Brown's avatar
Daniel Brown committed
202
        
203
204
        self.__L = float(L)
        self.__n = float(n)
205
206
207
208
209
        self._QItem = None
        
    @property
    def L(self): return Param('L', self.__L)
    @L.setter
210
    def L(self,value): self.__L = float(value)
211
212
213
    @property
    def n(self): return Param('n', self.__n)
    @n.setter
214
    def n(self,value): self.__n = float(value)
215
    
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    @staticmethod
    def parseFinesseText(text):
        values = text.split(" ")

        if values[0] != "s":
            raise exceptions.RuntimeError("'{0}' not a valid Finesse space command".format(text))

        values.pop(0) # remove initial value
        
        if len(values) == 5:
            return space(values[0],values[3],values[4],L=values[1],n=values[2])
        elif len(values) == 4:
            return space(values[0],values[2],values[3],L=values[1])
        else:
            raise exceptions.RuntimeError("Space Finesse code format incorrect '{0}'".format(text))
        
Daniel Brown's avatar
Daniel Brown committed
232
    def getFinesseText(self):
233
234
        nodes = self.getNodes()
        
235
        if self.__n == 1:
236
            return 's {0} {1} {2} {3}'.format(self.name, self.__L, nodes[0].name, nodes[1].name)            
Daniel Brown's avatar
Daniel Brown committed
237
        else:
238
            return 's {0} {1} {2} {3} {4}'.format(self.name, self.__L, self.__n, nodes[0].name, nodes[1].name)            
Daniel Brown's avatar
Daniel Brown committed
239
       
240
241
    def getQGraphicsItem(self):
        if self._QItem == None:
242
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self)
243
244
245
        
        return self._QItem  

246
247
248
249
250
251
252
253
    def changeNode(self, node_old, node_new):
        '''
        Called when a space's node has been connected
        to another components node
        '''
        node_new.connect(self)
        node_old.disconnect(self)
        
Daniel Brown's avatar
Daniel Brown committed
254
255
        if self._nodes[0] == node_old:
            self._nodes[0] = node_new
256
        
Daniel Brown's avatar
Daniel Brown committed
257
258
        if self._nodes[1] == node_old:
            self._nodes[1] = node_new
259
260

    
Daniel Brown's avatar
Daniel Brown committed
261
class laser(Component):
262
263
264
265
    def __init__(self,name,node,P=1,f_offset=0,phase=0):
        Component.__init__(self,name)
        
        self._requested_node_names.append(node)
Daniel Brown's avatar
Daniel Brown committed
266
        
267
268
269
        self.__power = float(P)
        self.__f_offset = float(f_offset)
        self.__phase = float(phase)
Daniel Brown's avatar
Daniel Brown committed
270
        
271
272
273
    @property
    def power(self): return Param('P', self.__power)
    @power.setter
274
    def power(self,value): self.__power = float(value)
275
276
277
278
    
    @property
    def f_offset(self): return Param('f', self.__f_offset)
    @f_offset.setter
279
    def f_offset(self,value): self.__f_offset = float(value)
280
281
282
283
    
    @property
    def phase(self): return Param('phase', self.__phase)
    @phase.setter
284
    def phase(self,value): self.__phase = float(value)
285
    
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    @staticmethod
    def parseFinesseText(text):
        values = text.split(" ")

        if values[0] != "l":
            raise exceptions.RuntimeError("'{0}' not a valid Finesse laser command".format(text))

        values.pop(0) # remove initial value
        
        if len(values) == 5:
            return laser(values[0],values[4],P=values[1],f_offset=values[2],phase=values[3])
        elif len(values) == 4:
            return laser(values[0],values[3],P=values[1],f_offset=values[2], phase=0)
        else:
            raise exceptions.RuntimeError("Laser Finesse code format incorrect '{0}'".format(text))
    
Daniel Brown's avatar
Daniel Brown committed
302
    def getFinesseText(self):
303
304
305
        nodes = self.getNodes()
        
        return 'l {0} {1} {2} {3} {4}'.format(self.name, self.__power, self.__f_offset, self.__phase, nodes[0].name)            
Daniel Brown's avatar
Daniel Brown committed
306
307
308
         
    def getQGraphicsItem(self):
        if self._svgItem == None:
309
            nodes = self.getNodes()
310
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/laser.svg",
311
                                                   self,[(65,25,nodes[0])])
Daniel Brown's avatar
Daniel Brown committed
312
313
314
            
        return self._svgItem