components.py 26.1 KB
Newer Older
Daniel Brown's avatar
Daniel Brown committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 11:10:01 2013

@author: Daniel
"""
import exceptions
import pykat.exceptions as pkex
import pykat
from pykat.node_network import *
from pykat.exceptions import *
import abc

import pykat.gui.resources
import pykat.gui.graphics
from pykat.gui.graphics import *
from pykat.SIfloat import *
from pykat.param import Param, AttrParam

20
21
import pykat.exceptions as pkex

Daniel Brown's avatar
Daniel Brown committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
next_component_id = 1

class NodeGaussSetter(object):
    def __init__(self, component, node):                
        self.__comp = component
        self.__node = node
    
    @property
    def node(self):
        return self.__node
    
    @property
    def q(self):
        return self.__node.qx
        
    @q.setter
    def q(self, value):
        self.__node.setGauss(self.__comp, complex(value))
        
    @property
    def qx(self):
        return self.__node.qx
    @qx.setter
    def qx(self, value):
        self.__node.setGauss(self.__comp, complex(value))
    
    @property
    def qy(self):
        return self.__node.qy
    @qy.setter
    def qy(self, value):
        self.__node.setGauss(self.__comp, self.qx, complex(value))
        
class Component(object):
    __metaclass__ = abc.ABCMeta
    
    def __init__(self, name):
        self.__name = name
        self._svgItem = None
        self._requested_node_names = []
        self._kat = None
        self.tag = None
        self._params = []
        
        # store a unique ID for this component
        global next_component_id
        self.__id = next_component_id
        next_component_id += 1
        
        # 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,), {})
    
    def _register_param(self, param):
        self._params.append(param)
        
    def _on_kat_add(self, kat):
        """
        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.
        """
        if self._kat != None:
            raise pkex.BasePyKatException("Component has already been added to a finesse.kat object")
            
        self._kat = kat
        
        kat.nodes.registerComponentNodes(self, self._requested_node_names, self.__on_node_change)
        
    def __on_node_change(self):
        # need to update the node gauss parameter setter members 
        self.__update_node_setters()
        
    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)]
        
        # now we have a list of which to remove
        for key in key_rm:
            ns = self.__dict__[key]
            delattr(self, '__nodesetter_' + ns.node.name)
            delattr(self.__class__, ns.node.name)
        
        for node in self.nodes:
            if type(node) != pykat.node_network.DumpNode:
                ns = NodeGaussSetter(self, node)
                self.__add_node_setter(ns)
        
    def __add_node_setter(self, ns):

        if not isinstance(ns, NodeGaussSetter):
            raise exceptions.ValueError("Argument is not of type NodeGaussSetter")
        
        name = ns.node.name
        fget = lambda self: self.__get_node_setter(name)
        
        setattr(self.__class__, name, property(fget))
        setattr(self, '__nodesetter_' + name, ns)                   

    def __get_node_setter(self, name):
        return getattr(self, '__nodesetter_' + name)   
        
    @staticmethod
    @abc.abstractmethod
    def parseFinesseText(text):
        """Parses Finesse syntax"""
        raise NotImplementedError("This function is not implemented")

    @abc.abstractmethod
    def getFinesseText(self):
        """ Base class for individual Finesse optical components """    
        raise NotImplementedError("This function is not implemented")

    @abc.abstractmethod
    def getQGraphicsItem(self):    
        return None      
    
    @property
    def nodes(self): return self._kat.nodes.getComponentNodes(self) 
    
    @property    
    def name(self): return self.__name      
    
    @property
    def id(self): return self.__id
    
    def __str__(self): return self.name
    
class AbstractMirrorComponent(Component):
    __metaclass__ = abc.ABCMeta
    
158
    def __init__(self, name, R=None, T=None, L=None, phi=0, Rcx=0, Rcy=0, xbeta=0, ybeta=0, mass=0, r_ap=0):
Daniel Brown's avatar
Daniel Brown committed
159
160
        super(AbstractMirrorComponent, self).__init__(name)

Daniel Brown's avatar
Daniel Brown committed
161
        if (L != None and R != None and T != None) and SIfloat(R)+SIfloat(T)+SIfloat(L) != 1: 
162
163
            raise pkex.BasePyKatException('L+R+T must equal 1 if all are specified')
        elif (R != None and L is None and T != None):
Daniel Brown's avatar
Daniel Brown committed
164
            L = 1- (SIfloat(R)+SIfloat(T))
165
        elif (R is None and L != None and T != None):
Daniel Brown's avatar
Daniel Brown committed
166
            R = 1 - (SIfloat(L)+SIfloat(T))
167
        elif (R != None and L != None and T is None):
Daniel Brown's avatar
Daniel Brown committed
168
            T = 1 - (SIfloat(L)+SIfloat(R))
169
170
171
        else:
            raise pkex.BasePyKatException('Must specify at least two of L, R or T')
        
Daniel Brown's avatar
Daniel Brown committed
172
173
        self.__R = Param("R", self, SIfloat(R))
        self.__T = Param("T", self, SIfloat(T))
174
175
        self.__L = Param("L", self, SIfloat(L))
        
176
        self.__phi = Param("phi", self, SIfloat(phi), canFsig=True, fsig_name="phase")
Daniel Brown's avatar
Daniel Brown committed
177
178
        self.__Rcx = AttrParam("Rcx", self, SIfloat(Rcx))
        self.__Rcy = AttrParam("Rcy", self, SIfloat(Rcy))
179
180
        self.__xbeta = AttrParam("xbeta", self, SIfloat(xbeta), canFsig=True, fsig_name="x")
        self.__ybeta = AttrParam("ybeta", self, SIfloat(ybeta), canFsig=True, fsig_name="y")
Daniel Brown's avatar
Daniel Brown committed
181
182
        self.__mass = AttrParam("mass", self, SIfloat(mass))
        self.__r_ap = AttrParam("r_ap", self, SIfloat(r_ap))
183
184
185
186
187
    
    @property
    def L(self): return self.__L
    @L.setter
    def L(self,value): self.__L.value = SIfloat(value)
Daniel Brown's avatar
Daniel Brown committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
        
    @property
    def r_ap(self): return self.__r_ap
    @r_ap.setter
    def r_ap(self,value): self.__r_ap.value = SIfloat(value)

    @property
    def mass(self): return self.__mass
    @mass.setter
    def mass(self,value): self.__mass.value = SIfloat(value)
    
    @property
    def R(self): return self.__R
    @R.setter
    def R(self,value): self.__R.value = SIfloat(value)
    
    @property
    def T(self): return self.__T
    @T.setter
    def T(self,value): self.__T.value = SIfloat(value)
        
    @property
    def phi(self): return self.__phi
    @phi.setter
    def phi(self,value): self.__phi.value = SIfloat(value)
    
    @property
    def Rcx(self): return self.__Rcx
    @Rcx.setter
    def Rcx(self,value): self.__Rcx.value = SIfloat(value)
    
    @property
    def Rcy(self): return self.__Rcy
    @Rcy.setter
    def Rcy(self,value): self.__Rcy.value = SIfloat(value)
    
    @property
    def xbeta(self): return self.__xbeta
    @xbeta.setter
    def xbeta(self,value): self.__xbeta.value = SIfloat(value)
    
    @property
    def ybeta(self): return self.__ybeta
    @ybeta.setter
    def ybeta(self,value): self.__ybeta.value = 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.value = SIfloat(value)
        self.Rcy.value = SIfloat(value)

class mirror(AbstractMirrorComponent):
Daniel Brown's avatar
Daniel Brown committed
247
248
    def __init__(self,name,node1,node2,R=None,T=None,L=None,phi=0,Rcx=0,Rcy=0,xbeta=0,ybeta=0,mass=0, r_ap=0):
        super(mirror, self).__init__(name, R, T, L, phi, Rcx, Rcy, xbeta, ybeta, mass, r_ap)
Daniel Brown's avatar
Daniel Brown committed
249
250
251
252
253
254
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
    
    @staticmethod
    def parseFinesseText(text):
255
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
256
257

        if values[0] != "m" and values[0] != "m1" and values[0] != "m2":
258
            raise pkex.BasePyKatException("'{0}' not a valid Finesse mirror command".format(text))
Daniel Brown's avatar
Daniel Brown committed
259
260
        
        if len(values) != 7:
261
            raise pkex.BasePyKatException("Mirror Finesse code format incorrect '{0}'".format(text))
Daniel Brown's avatar
Daniel Brown committed
262
263
264

        if len(values[0])==1:
            values.pop(0) # remove initial value
Daniel Brown's avatar
Daniel Brown committed
265
            return mirror(values[0], values[4], values[5], L=None, R=values[1], T=values[2], phi=values[3])
Daniel Brown's avatar
Daniel Brown committed
266
267
268
        else:
            if values[0][1]=="1":
                values.pop(0) # remove initial value
Daniel Brown's avatar
Daniel Brown committed
269
                return mirror(values[0], values[4], values[5], R=None, L=values[2], T=values[1], phi=values[3])
Daniel Brown's avatar
Daniel Brown committed
270
271
            else:
                values.pop(0) # remove initial value
Daniel Brown's avatar
Daniel Brown committed
272
                return mirror(values[0], values[4], values[5], T=None, R=values[1], L=values[2], phi=values[3])
Daniel Brown's avatar
Daniel Brown committed
273

274
    def getFinesseText(self):
Daniel Brown's avatar
Daniel Brown committed
275
        if self.R+self.T+self.L > 1:
276
277
            raise pkex.BasePyKatException("Mirror {0} has R+T+L > 1".format(self.name))        
        
Daniel Brown's avatar
Daniel Brown committed
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
        rtn = []
            
        rtn.append('m {0} {1} {2} {3} {4} {5}'.format(
                self.name, self.R.value, self.T.value, self.phi.value,
                self.nodes[0].name, self.nodes[1].name))

        for p in self._params:
            rtn.extend(p.getFinesseText())
                    
        return rtn
        
    def getQGraphicsItem(self):
        if self._svgItem == None:
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/mirror_flat.svg", self ,[(-4,15,self.nodes[0]), (14,15,self.nodes[1])])
            
        return self._svgItem

class beamSplitter(AbstractMirrorComponent):
    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, r_ap = 0):
        super(beamSplitter, self).__init__(name, R, T, phi, Rcx, Rcy, xbeta, ybeta, mass, r_ap)
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
        self._requested_node_names.append(node3)
        self._requested_node_names.append(node4)
             
304
305
        self.__alpha = Param("alpha", self, SIfloat(alpha))
        
Daniel Brown's avatar
Daniel Brown committed
306
307
308
309
310
311
312
    @property
    def alpha(self): return self.__alpha
    @alpha.setter
    def alpha(self,value): self.__alpha.value = SIfloat(value)
    
    @staticmethod
    def parseFinesseText(text):
313
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
314
315

        if values[0] != "bs" and values[0] != "bs1" and values[0] != "bs2":
316
            raise pkex.BasePyKatException("'{0}' not a valid Finesse beam splitter command".format(text))
Daniel Brown's avatar
Daniel Brown committed
317
318
        
        if len(values) != 10:
319
            raise pkex.BasePyKatException("Beam splitter Finesse code format incorrect '{0}'".format(text))
Daniel Brown's avatar
Daniel Brown committed
320

321
        if len(values[0])==2:
Daniel Brown's avatar
Daniel Brown committed
322
323
324
            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:
325
            if values[0][2]=="1":
Daniel Brown's avatar
Daniel Brown committed
326
                values.pop(0) # remove initial value
327
328
329
                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])
Daniel Brown's avatar
Daniel Brown committed
330
331
            else:
                values.pop(0) # remove initial value
332
333
334
335
                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])
Daniel Brown's avatar
Daniel Brown committed
336
337
            
    def getFinesseText(self):
Daniel Brown's avatar
Daniel Brown committed
338
339
340
        if self.R+self.T+self.L > 1:
            raise pkex.BasePyKatException("Beamsplitter {0} has R+T+L > 1".format(self.name))

Daniel Brown's avatar
Daniel Brown committed
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
        rtn = []
            
        rtn.append('bs {0} {1} {2} {3} {4} {5} {6} {7} {8}'.format(
                self.name, self.R.value, self.T.value, self.phi.value,
                self.alpha.value, self.nodes[0].name,
                self.nodes[1].name, self.nodes[2].name,
                self.nodes[3].name))

        for p in self._params:
            rtn.extend(p.getFinesseText())
            
        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
   
class space(Component):
    def __init__(self, name, node1, node2, L=0, n=1):
        Component.__init__(self, name)
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
        self._QItem = None
        self.__L = Param("L", self, SIfloat(L))
        self.__n = Param("n", self, SIfloat(n))
        
    @property
    def L(self): return self.__L
    @L.setter
Andreas Freise's avatar
Andreas Freise committed
374
    def L(self,value): self.__L.value = SIfloat(value)
Daniel Brown's avatar
Daniel Brown committed
375
376
377
378
379
380
381
    @property
    def n(self): return self.__n
    @n.setter
    def n(self,value): self.__n.value = SIfloat(value)
    
    @staticmethod
    def parseFinesseText(text):
382
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
383
384

        if values[0] != "s":
385
            raise pkex.BasePyKatException("'{0}' not a valid Finesse space command".format(text))
Daniel Brown's avatar
Daniel Brown committed
386
387
388
389
390
391
392
393

        values.pop(0) # remove initial value
        
        if len(values) == 5:
            return space(values[0], values[3], values[4], values[1], values[2])
        elif len(values) == 4:
            return space(values[0], values[2], values[3], values[1])
        else:
394
            raise pkex.BasePyKatException("Space Finesse code format incorrect '{0}'".format(text))
Daniel Brown's avatar
Daniel Brown committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
        
    def getFinesseText(self):
        rtn = []
        
        if self.__n == 1:
            rtn.append('s {0} {1} {2} {3}'.format(self.name, self.__L.value, self.nodes[0].name, self.nodes[1].name))
        else:
            rtn.append('s {0} {1} {2} {3} {4}'.format(self.name, self.__L.value, self.__n.value, self.nodes[0].name, self.nodes[1].name))
       
        for p in self._params:
            rtn.extend(p.getFinesseText())
            
        return rtn
        
    def getQGraphicsItem(self):
        if self._QItem == None:
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self)
        
        return self._QItem

class grating(Component):
    def __init__(self, name, node1, node2, node3 = None, node4 = None, n = 2, d = 0, eta_0 = 0, eta_1 = 0, eta_2 = 0, eta_3 = 0, rho_0 = 0, alpha = 0): # TODO: implement Rcx, Rcy and Rc
        Component.__init__(self, name)
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)

        if n > 2:
            if node3 != None:
                self._requested_node_names.append(node3)
            else:
426
                raise pkex.BasePyKatException("Grating node 3 not specified")
Daniel Brown's avatar
Daniel Brown committed
427
428
429
430
431

        if n > 3:
            if node4 != None:
                self._requested_node_names.append(node4)
            else:
432
                raise pkex.BasePyKatException("Grating node 4 not specified")
Daniel Brown's avatar
Daniel Brown committed
433
434

        if n > 4 or n < 2:
435
            raise pkex.BasePyKatException("Grating must have between 2 and 4 ports")
Daniel Brown's avatar
Daniel Brown committed
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
        
        self.__n = n
        self.__d = Param("d", self, SIfloat(d))
        self.__eta_0 = AttrParam("eta_0", self, SIfloat(eta_0))
        self.__eta_1 = AttrParam("eta_1", self, SIfloat(eta_1))
        self.__eta_2 = AttrParam("eta_2", self, SIfloat(eta_2))
        self.__eta_3 = AttrParam("eta_3", self, SIfloat(eta_3))
        self.__rho_0 = AttrParam("rho_0", self, SIfloat(rho_0))
        self.__alpha = AttrParam("alpha", self, SIfloat(alpha))
    
    @property
    def n(self): return Param('n', self.__n)
    @n.setter
    def n(self, value):
        if value < 2 or value > 4:
451
            raise pkex.BasePyKatException("Grating must have between 2 and 4 ports")
Daniel Brown's avatar
Daniel Brown committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
        else:
            self.__n = value
    
    @property
    def d(self): return self.__d
    @d.setter
    def d(self, value): self.__d.value = SIfloat(value)
    
    @property
    def eta_0(self): return self.__eta_0
    @eta_0.setter
    def eta_0(self, value): self.__eta_0.value = SIfloat(value)
    
    @property
    def eta_1(self): return self.__eta_1
    @eta_1.setter
    def eta_1(self, value): self.__eta_1.value = SIfloat(value)
    
    @property
    def eta_2(self): return self.__eta_2
    @eta_2.setter
    def eta_2(self, value): self.__eta_2.value = SIfloat(value)
    
    @property
    def eta_3(self): return self.__eta_3
    @eta_3.setter
    def eta_3(self, value): self.__eta_3.value = SIfloat(value)
    
    @property
    def rho_0(self): return self.__rho_0
    @rho_0.setter
    def rho_0(self, value): self.__rho_0.value = SIfloat(value)
    
    @property
    def alpha(self): return self.__alpha
    @alpha.setter
    def alpha(self, value): self.__alpha.value = SIfloat(value)
    
    @staticmethod
    def parseFinesseText(text):
492
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
493
494

        if values[0][0 : 2] != "gr":
495
            raise pkex.BasePyKatException("'{0}' not a valid Finesse grating command".format(text))
Daniel Brown's avatar
Daniel Brown committed
496
497
498

        if len(values[0]) > 2:
            if int(values[0][2]) > 4 or int(values[0][2]) < 2:
499
                raise pkex.BasePyKatException("Grating must have between 2 and 4 ports")
Daniel Brown's avatar
Daniel Brown committed
500
501
502
503
504
505
506
507
508
            else:
                n = int(values[0][2])
        else:
            n = 2

        values.pop(0) # remove initial value
        
        if n == 2:
            if len(values) != 4:
509
                raise pkex.BasePyKatException("Two port grating must have 2 nodes defined")
Daniel Brown's avatar
Daniel Brown committed
510
511
512
513

            return grating(values[0], values[2], values[3], None, None, n, values[1])
        elif n == 3:
            if len(values) != 5:
514
                raise pkex.BasePyKatException("Three port grating must have 3 nodes defined")
Daniel Brown's avatar
Daniel Brown committed
515
516
517
518
            
            return grating(values[0], values[2], values[3], values[4], None, n, values[1])
        else:
            if len(values) != 6:
519
                raise pkex.BasePyKatException("Four port grating must have 4 nodes defined")
Daniel Brown's avatar
Daniel Brown committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
            
            return grating(values[0], values[2], values[3], values[4], values[5], n, values[1])
        
    def getFinesseText(self):
        rtn = []
        
        if self.__n == 2:
            rtn.append('gr{0} {1} {2} {3} {4}'.format(self.__n, self.name, self.__d.value, self.nodes[0].name, self.nodes[1].name))
        elif self.__n == 3:
            rtn.append('gr{0} {1} {2} {3} {4} {5}'.format(self.__n, self.name, self.__d.value, self.nodes[0].name, self.nodes[1].name, self.nodes[2].name))
        else:
            rtn.append('gr{0} {1} {2} {3} {4} {5} {6}'.format(self.__n, self.name, self.__d.value, self.nodes[0].name, self.nodes[1].name, self.nodes[2].name, self.nodes[3].name))
        
        for p in self._params:
            rtn.extend(p.getFinesseText())
        
        return rtn
       
    def getQGraphicsItem(self):
        if self._QItem == None:
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self) # TODO: make SVG graphic for grating
        
        return self._QItem

class isolator(Component):
    def __init__(self, name, node1, node2, S = 0):
        Component.__init__(self, name)
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
        
        self.__S = Param("S",self,SIfloat(S))
        
    @property
    def S(self): return self.__S
    @S.setter
    def S(self, value): self.__S.value = SIfloat(value)
    
    @staticmethod
    def parseFinesseText(text):
560
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
561
562

        if values[0] != "isol":
563
            raise pkex.BasePyKatException("'{0}' not a valid Finesse isolator command".format(text))
Daniel Brown's avatar
Daniel Brown committed
564
565
566
567
568
569

        values.pop(0) # remove initial value
        
        if len(values) == 4:
            return isolator(values[0], values[2], values[3], values[1])
        else:
570
            raise pkex.BasePyKatException("Isolator Finesse code format incorrect '{0}'".format(text))
Daniel Brown's avatar
Daniel Brown committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
        
    def getFinesseText(self):
        rtn = ['isol {0} {1} {2} {3}'.format(self.name, self.S.value, self.nodes[0].name, self.nodes[1].name)]
        
        for p in self._params:
            rtn.extend(p.getFinesseText())
            
        return rtn

    def getQGraphicsItem(self):
        if self._QItem == None:
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self) # TODO: make isolator graphic
        
        return self._QItem

class lens(Component):
    def __init__(self, name, node1, node2, f):
        Component.__init__(self, name)
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
        
        self.__f = Param("f", self, SIfloat(f))
        
    @property
    def f(self): return self.__f
    @f.setter
    def f(self, value): self.__f.value = SIfloat(value)
    
    @staticmethod
    def parseFinesseText(text):
602
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
603
604

        if values[0] != "lens":
605
            raise pkex.BasePyKatException("'{0}' not a valid Finesse lens command".format(text))
Daniel Brown's avatar
Daniel Brown committed
606
607
608
609
610
611

        values.pop(0) # remove initial value
        
        if len(values) == 4:
            return lens(values[0], values[2], values[3], values[1])
        else:
612
            raise pkex.BasePyKatException("Lens Finesse code format incorrect '{0}'".format(text))
Daniel Brown's avatar
Daniel Brown committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
        
    def getFinesseText(self):
        rtn = ['lens {0} {1} {2} {3}'.format(self.name, self.f.value, self.nodes[0].name, self.nodes[1].name)]
        
        for p in self._params:
            rtn.extend(p.getFinesseText())
            
        return rtn
        
    def getQGraphicsItem(self):
        if self._QItem == None:
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self) # TODO: make lens graphic
        
        return self._QItem
        
class modulator(Component):
    def __init__(self, name, f, midx, order, modulation_type, node1, node2, phase=0):
        Component.__init__(self, name)
        
        self._requested_node_names.append(node1)
        self._requested_node_names.append(node2)
        
        self.__f = Param("f", self, SIfloat(f))
        self.__midx = Param("midx", self, SIfloat(midx))
        self.__phase = Param("phase", self, SIfloat(phase))
        self.__order = order
        self.type = modulation_type
        
            
    @property 
    def f(self): return self.__f
    @f.setter
    def f(self, value): self.__f.value = SIfloat(value)
    
    @property 
    def midx(self): return self.__midx
    @midx.setter
    def midx(self, value): self.__midx.value = SIfloat(value)
    
    @property 
    def phase(self): return self.__phase
    @phase.setter
    def phase(self, value): self.__phase.value = SIfloat(value)
    
    @property 
    def order(self): return self.__order
    @order.setter
    def order(self, value):
        if order <= 1 and order > 6:
            raise pkex.BasePyKatException("modulator order must be between 1 and 6")
            
        self.__order = int(value)
    
    @property 
    def type(self): return self.__type
    @type.setter
    def type(self, value):
        if value != "am" and value != "pm":
            raise pkex.BasePyKatException("Modulator type must be am (amplitude modulation) or pm (phase modulation)")
            
        self.__type = str(value)
    
    @staticmethod
    def parseFinesseText(text):
677
        v = text.split()
Daniel Brown's avatar
Daniel Brown committed
678
679

        if v[0] != "mod":
680
            raise pkex.BasePyKatException("'{0}' not a valid Finesse modulator command".format(text))
Daniel Brown's avatar
Daniel Brown committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710

        v.pop(0) # remove initial value
        
        if len(v) == 7:
            return modulator(v[0], v[1], v[2], v[3], v[4], v[5], v[6])
        if len(v) == 8:
            return modulator(v[0], v[1], v[2], v[3], v[4], v[6], v[7], phase=v[5])
        else:
            raise pkex.BasePyKatException("Modulator Finesse code format incorrect '{0}'".format(text))
        
    def getFinesseText(self):
        rtn = ['mod {0} {1} {2} {3} {4} {5} {6} {7}'.format(self.name, self.f, self.midx, self.order, self.type, self.phase, self.nodes[0].name, self.nodes[1].name)]
        
        for p in self._params:
            rtn.extend(p.getFinesseText())
            
        return rtn
        
    def getQGraphicsItem(self):
        if self._QItem == None:
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self) # TODO: make lens graphic
        
        return self._QItem

class laser(Component):
    def __init__(self,name,node,P=1,f_offset=0,phase=0):
        Component.__init__(self,name)
        
        self._requested_node_names.append(node)
        
711
712
        self.__power = Param("P", self, SIfloat(P), canFsig=True, fsig_name="amp")
        self.__f_offset = Param("f", self, SIfloat(f_offset), canFsig=True, fsig_name="f")
713
        self.__phase = Param("phase", self, SIfloat(phase), canFsig=True, fsig_name="phase")
Daniel Brown's avatar
Daniel Brown committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
        self.__noise = AttrParam("noise", self, 0)
        
    @property
    def power(self): return self.__power
    @power.setter
    def power(self,value): self.__power.value = float(value)
    
    @property
    def f(self): return self.__f_offset
    @f.setter
    def f(self,value): self.__f_offset.value = float(value)
    
    @property
    def phase(self): return self.__phase
    @phase.setter
    def phase(self,value): self.__phase.value = float(value)
    
    @staticmethod
    def parseFinesseText(text):
733
        values = text.split()
Daniel Brown's avatar
Daniel Brown committed
734
735

        if values[0] != "l":
736
            raise pkex.BasePyKatException("'{0}' not a valid Finesse laser command".format(text))
Daniel Brown's avatar
Daniel Brown committed
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760

        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.FinesseParse("Laser Finesse code format incorrect '{0}'".format(text))
    
    def getFinesseText(self):
        rtn = ['l {0} {1} {2} {3} {4}'.format(self.name, self.__power.value, self.__f_offset.value, self.__phase.value, self.nodes[0].name)]
        
        for p in self._params:
            rtn.extend(p.getFinesseText())
        
        return rtn
         
    def getQGraphicsItem(self):
        if self._svgItem == None:
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/laser.svg", self, [(65,25,self.nodes[0])])
            
        return self._svgItem