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

@author: Daniel
"""
import exceptions
8
import pykat.exceptions as pkex
Daniel Brown's avatar
Daniel Brown committed
9
10
import pykat
from pykat.node_network import *
Andreas Freise's avatar
Andreas Freise committed
11
from pykat.exceptions import *
Daniel Brown's avatar
Daniel Brown committed
12
13
14
15

import pykat.gui.resources
import pykat.gui.graphics
from pykat.gui.graphics import *
Andreas Freise's avatar
Andreas Freise committed
16
from pykat.SIfloat import *
Daniel Brown's avatar
Daniel Brown committed
17

18
19
next_component_id = 1

20
21
class NodeGaussSetter(object):
    def __init__(self, component, node):                
Daniel Brown's avatar
Daniel Brown committed
22
23
24
25
        self.__comp = component
        self.__node = node
    
    @property
26
27
    def node(self):
        return self.__node
Daniel Brown's avatar
Daniel Brown committed
28
29
    
    @property
30
31
32
    def q(self):
        return self.__node.qx
        
Daniel Brown's avatar
Daniel Brown committed
33
34
    @q.setter
    def q(self, value):
35
        self.__node.setGauss(self.__comp, complex(value))
Daniel Brown's avatar
Daniel Brown committed
36
37
        
    @property
38
39
    def qx(self):
        return self.__node.qx
Daniel Brown's avatar
Daniel Brown committed
40
41
    @qx.setter
    def qx(self, value):
42
        self.__node.setGauss(self.__comp, complex(value))
Daniel Brown's avatar
Daniel Brown committed
43
44
    
    @property
45
46
    def qy(self):
        return self.__node.qy
Daniel Brown's avatar
Daniel Brown committed
47
48
    @qy.setter
    def qy(self, value):
49
        self.__node.setGauss(self.__comp, self.qx, complex(value))
50
        
51
class Component(object) :
52
    def __init__(self, name):
Daniel Brown's avatar
Daniel Brown committed
53
54
        self.__name = name
        self._svgItem = None
55
56
        self._requested_node_names = []
        self._kat = None
Daniel Brown's avatar
Daniel Brown committed
57
        self.tag = None
58
59
60
61
62
63
        
        # store a unique ID for this component
        global next_component_id
        self.__id = next_component_id
        next_component_id += 1
        
64
65
66
67
68
69
        # This creates an instance specific class for the component
        # this enables us to add properties to instances rather than
        # all classes
        cls = type(self)
        self.__class__ = type(cls.__name__, (cls,), {})
            
70
71
    def _on_kat_add(self, kat):
        """
72
73
74
75
        Called when this component has been added to a kat object.
        kat is the finesse.kat object which it now belongs to and
        node_array is an array specific to this object which contains
        references to the nodes that are attached to it.
76
        """
77
78
79
        if self._kat != None:
            raise pkex.BasePyKatException("Component has already been added to a finesse.kat object")
            
80
81
        self._kat = kat
        
82
        kat.nodes.registerComponentNodes(self, self._requested_node_names, self.__on_node_change)
83
        
84
    def __on_node_change(self):
85
        # need to update the node gauss parameter setter members 
Daniel Brown's avatar
Daniel Brown committed
86
        self.__update_node_setters()
87
88
89
90
91
92
        
    def __update_node_setters(self):
        # check if any node setters have already been added. If so we
        # need to remove them. This function should get called if the nodes
        # are updated, either by some function call or the GUI
        key_rm = [k for k in self.__dict__ if k.startswith("__nodesetter_", 0, 13)]
Daniel Brown's avatar
Daniel Brown committed
93
        
94
95
96
        # now we have a list of which to remove
        for key in key_rm:
            ns = self.__dict__[key]
97
98
            delattr(self, '__nodesetter_' + ns.node.name)
            delattr(self.__class__, ns.node.name)
Daniel Brown's avatar
Daniel Brown committed
99
        
100
        for node in self.nodes:
Daniel Brown's avatar
Daniel Brown committed
101
102
103
            if type(node) != pykat.node_network.DumpNode:
                ns = NodeGaussSetter(self, node)
                self.__add_node_setter(ns)
104
105
106
107
108
109
        
    def __add_node_setter(self, ns):

        if not isinstance(ns, NodeGaussSetter):
            raise exceptions.ValueError("Argument is not of type NodeGaussSetter")
        
Daniel Brown's avatar
Daniel Brown committed
110
        name = ns.node.name
111
112
        fget = lambda self: self.__get_node_setter(name)
        
113
        setattr(self.__class__, name, property(fget))
114
115
116
117
        setattr(self, '__nodesetter_' + name, ns)                   

    def __get_node_setter(self, name):
        return getattr(self, '__nodesetter_' + name)   
118
119
120
121
        
    @staticmethod
    def parseFinesseText(text):    
        raise NotImplementedError("This function is not implemented")
122
            
Daniel Brown's avatar
Daniel Brown committed
123
124
125
126
127
128
    def getFinesseText(self):
        """ Base class for individual finesse optical components """    
        raise NotImplementedError("This function is not implemented")
        
    def getQGraphicsItem(self):    
        return None      
129
130
131
    
    @property
    def nodes(self): return self._kat.nodes.getComponentNodes(self) 
132
133
134
135
136
137
138
139
    
    @property    
    def name(self): return self.__name      
    
    @property
    def id(self): return self.__id
    
    
Daniel Brown's avatar
Daniel Brown committed
140
    
141
142
class Param(float):
    def __new__(self,name,value):
143
        return float.__new__(self,SIfloat(value))
144
         
Daniel Brown's avatar
Daniel Brown committed
145
146
147
    def __init__(self,name,value):
        self.__name = name
        
148
149
    name = property(lambda self: self.__name)
           
Daniel Brown's avatar
Daniel Brown committed
150
class mirror(Component):
Daniel Brown's avatar
Daniel Brown committed
151
    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
152
        
153
        Component.__init__(self,name)
Daniel Brown's avatar
Daniel Brown committed
154
        
155
156
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
Daniel Brown's avatar
Daniel Brown committed
157

158
159
160
161
162
163
164
165
166
167
        
        self.__r_ap = SIfloat(r_ap)        
        self.__mass = SIfloat(mass)
        self.__R = SIfloat(R)
        self.__T = SIfloat(T)
        self.__phi = SIfloat(phi)
        self.__Rcx = SIfloat(Rcx)
        self.__Rcy = SIfloat(Rcy)
        self.__xbeta = SIfloat(xbeta)
        self.__ybeta = SIfloat(ybeta)
168
    
Daniel Brown's avatar
Daniel Brown committed
169
    @property
170
    def r_ap(self): return Param('r_ap', self.__r_ap)
Daniel Brown's avatar
Daniel Brown committed
171
    @r_ap.setter
172
    def r_ap(self,value): self.__aperture = SIfloat(value)
Daniel Brown's avatar
Daniel Brown committed
173

174
175
176
    @property
    def mass(self): return Param('mass', self.__mass)
    @mass.setter
177
    def mass(self,value): self.__mass = SIfloat(value)
178
    
179
    @property
180
    def R(self): return Param('R', self.__R)
181
    @R.setter
182
    def R(self,value): self.__R = SIfloat(value)
183
    
184
185
186
    @property
    def T(self): return Param('T', self.__T)
    @T.setter
187
    def T(self,value): self.__T = SIfloat(value)
188
189
190
191
        
    @property
    def phi(self): return Param('phi', self.__phi)
    @phi.setter
192
    def phi(self,value): self.__phi = SIfloat(value)
193
194
195
196
    
    @property
    def Rcx(self): return Param('Rcx', self.__Rcx)
    @Rcx.setter
197
    def Rcx(self,value): self.__Rcx = SIfloat(value)
198
    
199
200
201
    @property
    def Rcy(self): return Param('Rcy', self.__Rcy)
    @Rcy.setter
202
    def Rcy(self,value): self.__Rcy = SIfloat(value)
203
204
205
206
    
    @property
    def xbeta(self): return Param('xbeta', self.__xbeta)
    @xbeta.setter
207
    def xbeta(self,value): self.__xbeta = SIfloat(value)
208
    
209
210
211
    @property
    def ybeta(self): return Param('ybeta', self.__ybeta)
    @ybeta.setter
212
    def ybeta(self,value): self.__ybeta = SIfloat(value)
213
214
215
    
    @property
    def Rc(self):
Daniel Brown's avatar
Daniel Brown committed
216
217
218
219
220
        if self.Rcx == self.Rcy:
            return self.Rcx
        else:
            return [self.Rcx, self.Rcy]
    
221
222
    @Rc.setter
    def Rc(self,value):
223
224
        self.Rcx = SIfloat(value)
        self.Rcy = SIfloat(value)
225
226
227
228
229
    
    @staticmethod
    def parseFinesseText(text):
        values = text.split(" ")

230
        if values[0] != "m" and values[0] != "m1" and values[0] != "m2":
231
232
            raise exceptions.RuntimeError("'{0}' not a valid Finesse mirror command".format(text))
        
Andreas Freise's avatar
Andreas Freise committed
233
        if len(values) != 7:
234
235
            raise exceptions.RuntimeError("Mirror Finesse code format incorrect '{0}'".format(text))

236
        if len(values[0])==1:
Andreas Freise's avatar
Andreas Freise committed
237
            values.pop(0) # remove initial value
238
239
            return mirror(values[0], values[4], values[5], R=values[1], T=values[2], phi=values[3])
        else:
Andreas Freise's avatar
Andreas Freise committed
240
241
            if values[0][1]=="1":
                values.pop(0) # remove initial value
242
243
                return mirror(values[0], values[4], values[5], R=1.0-SIfloat(values[1])-SIfloat(values[2]), T=values[1], phi=values[3])
            else:
Andreas Freise's avatar
Andreas Freise committed
244
                values.pop(0) # remove initial value
245
246
247
                return mirror(values[0], values[4], values[5], R=values[1], T=1.0-SIfloat(values[1])-SIfloat(values[2]), phi=values[3])

            
Daniel Brown's avatar
Daniel Brown committed
248
249
    def getFinesseText(self):        
        rtn = []
250
            
Daniel Brown's avatar
Daniel Brown committed
251
        rtn.append('m {0} {1} {2} {3} {4} {5}'.format(
252
                self.name, self.__R, self.__T, self.__phi,
253
                self.nodes[0].name, self.nodes[1].name))
Daniel Brown's avatar
Daniel Brown committed
254
255

        if self.r_ap != 0: rtn.append("attr {0} r_ap {1}".format(self.name,self.__r_ap))            
256
        if self.mass != 0: rtn.append("attr {0} mass {1}".format(self.name,self.__mass))
257
258
259
260
        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
261
262
263
264
265
        
        return rtn
        
    def getQGraphicsItem(self):
        if self._svgItem == None:
266
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/mirror_flat.svg", self ,[(-4,15,self.nodes[0]), (14,15,self.nodes[1])])
Daniel Brown's avatar
Daniel Brown committed
267
            
Daniel Brown's avatar
Daniel Brown committed
268
        return self._svgItem
Sean Leavey's avatar
Sean Leavey committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382

class beamSplitter(Component):
    def __init__(self,name,node1,node2,node3,node4,R=0,T=0,phi=0,alpha=0,Rcx=0,Rcy=0,xbeta=0,ybeta=0,mass=0):
        Component.__init__(self,name)
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
        self._requested_node_names.append(node3)
        self._requested_node_names.append(node4)
             
        self.__R = SIfloat(R)
        self.__T = SIfloat(T)
        self.__alpha = SIfloat(alpha)
        self.__phi = SIfloat(phi)
        self.__Rcx = SIfloat(Rcx)
        self.__Rcy = SIfloat(Rcy)
        self.__xbeta = SIfloat(xbeta)
        self.__ybeta = SIfloat(ybeta)
    
    @property
    def R(self): return Param('R', self.__R)
    @R.setter
    def R(self,value): self.__R = SIfloat(value)
    
    @property
    def T(self): return Param('T', self.__T)
    @T.setter
    def T(self,value): self.__T = SIfloat(value)
        
    @property
    def phi(self): return Param('phi', self.__phi)
    @phi.setter
    def phi(self,value): self.__phi = SIfloat(value)
    
    @property
    def alpha(self): return Param('alpha', self.__alpha)
    @alpha.setter
    def alpha(self,value): self.__alpha = SIfloat(value)
    
    @property
    def Rcx(self): return Param('Rcx', self.__Rcx)
    @Rcx.setter
    def Rcx(self,value): self.__Rcx = SIfloat(value)
    
    @property
    def Rcy(self): return Param('Rcy', self.__Rcy)
    @Rcy.setter
    def Rcy(self,value): self.__Rcy = SIfloat(value)
    
    @property
    def xbeta(self): return Param('xbeta', self.__xbeta)
    @xbeta.setter
    def xbeta(self,value): self.__xbeta = SIfloat(value)
    
    @property
    def ybeta(self): return Param('ybeta', self.__ybeta)
    @ybeta.setter
    def ybeta(self,value): self.__ybeta = SIfloat(value)
    
    @property
    def Rc(self):
        if self.Rcx == self.Rcy:
            return self.Rcx
        else:
            return [self.Rcx, self.Rcy]
    
    @Rc.setter
    def Rc(self,value):
        self.Rcx = SIfloat(value)
        self.Rcy = SIfloat(value)
    
    @staticmethod
    def parseFinesseText(text):
        values = text.split(" ")

        if values[0] != "bs" and values[0] != "bs1" and values[0] != "bs2":
            raise exceptions.RuntimeError("'{0}' not a valid Finesse beam splitter command".format(text))
        
        if len(values) != 10:
            raise exceptions.RuntimeError("Beam splitter Finesse code format incorrect '{0}'".format(text))

        if len(values[0])==1:
            values.pop(0) # remove initial value
            return beamSplitter(values[0], values[5], values[6], values[7], values[8], values[1], values[2], values[3], values[4])
        else:
            if values[0][1]=="1":
                values.pop(0) # remove initial value
                return beamSplitter(values[0], values[5], values[6], values[7], values[8], 1.0 - SIfloat(values[1]) - SIfloat(values[2]), values[1], values[3], values[4])
            else:
                values.pop(0) # remove initial value
                return beamSplitter(values[0], values[5], values[6], values[7], values[8], values[1], 1.0 - SIfloat(values[1]) - SIfloat(values[2]), values[3], values[4])
            
    def getFinesseText(self):        
        rtn = []
            
        rtn.append('bs {0} {1} {2} {3} {4} {5} {6} {7} {8}'.format(
                self.name, self.__R, self.__T, self.__phi,
                self.__alpha, self.nodes[0].name,
                self.nodes[1].name, self.nodes[2].name,
                self.nodes[3].name))

        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))
        
        return rtn
        
    def getQGraphicsItem(self):
        if self._svgItem == None:
            # FIXME: make proper SVG component for beam splitter
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/mirror_flat.svg", self ,[(-4,15,self.nodes[0]), (14,15,self.nodes[1])])
            
        return self._svgItem
Daniel Brown's avatar
Daniel Brown committed
383
384
   
class space(Component):
385
386
    def __init__(self, name, node1, node2, L=0, n=1):
        Component.__init__(self,name,)
Daniel Brown's avatar
Daniel Brown committed
387
        
388
389
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
Daniel Brown's avatar
Daniel Brown committed
390
        
Andreas Freise's avatar
Andreas Freise committed
391
392
        self.__L = SIfloat(L)
        self.__n = SIfloat(n)
393
394
395
396
397
        self._QItem = None
        
    @property
    def L(self): return Param('L', self.__L)
    @L.setter
Andreas Freise's avatar
Andreas Freise committed
398
    def L(self,value): self.__L = SIfloat(value)
399
400
401
    @property
    def n(self): return Param('n', self.__n)
    @n.setter
Andreas Freise's avatar
Andreas Freise committed
402
    def n(self,value): self.__n = SIfloat(value)
403
    
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
    @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
420
    def getFinesseText(self):
421
        if self.__n == 1:
422
            return 's {0} {1} {2} {3}'.format(self.name, self.__L, self.nodes[0].name, self.nodes[1].name)            
Daniel Brown's avatar
Daniel Brown committed
423
        else:
424
            return 's {0} {1} {2} {3} {4}'.format(self.name, self.__L, self.__n, self.nodes[0].name, self.nodes[1].name)            
Daniel Brown's avatar
Daniel Brown committed
425
       
426
427
    def getQGraphicsItem(self):
        if self._QItem == None:
428
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self)
429
        
430
        return self._QItem          
431
    
Daniel Brown's avatar
Daniel Brown committed
432
class laser(Component):
433
434
435
436
    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
437
        
438
439
440
        self.__power = float(P)
        self.__f_offset = float(f_offset)
        self.__phase = float(phase)
Daniel Brown's avatar
Daniel Brown committed
441
        
442
443
444
    @property
    def power(self): return Param('P', self.__power)
    @power.setter
445
    def power(self,value): self.__power = float(value)
446
447
448
449
    
    @property
    def f_offset(self): return Param('f', self.__f_offset)
    @f_offset.setter
450
    def f_offset(self,value): self.__f_offset = float(value)
451
452
453
454
    
    @property
    def phase(self): return Param('phase', self.__phase)
    @phase.setter
455
    def phase(self,value): self.__phase = float(value)
456
    
457
458
459
460
461
462
463
464
465
466
467
468
469
470
    @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:
Andreas Freise's avatar
Andreas Freise committed
471
            raise exceptions.FinesseParse("Laser Finesse code format incorrect '{0}'".format(text))
472
    
Daniel Brown's avatar
Daniel Brown committed
473
    def getFinesseText(self):
474
        return 'l {0} {1} {2} {3} {4}'.format(self.name, self.__power, self.__f_offset, self.__phase, self.nodes[0].name)            
Daniel Brown's avatar
Daniel Brown committed
475
476
477
         
    def getQGraphicsItem(self):
        if self._svgItem == None:
478
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/laser.svg", self, [(65,25,self.nodes[0])])
Daniel Brown's avatar
Daniel Brown committed
479
480
481
            
        return self._svgItem