components.py 7.25 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.gui.resources
import pykat
10
import inspect
11
import pykat.gui.graphics
Daniel Brown's avatar
Daniel Brown committed
12
13
14
15
16
from pykat.gui.graphics import *
from pykat.node_network import *
from PyQt4.QtGui import *
from PyQt4.Qt import *

17
class Component(object) :
Daniel Brown's avatar
Daniel Brown committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    def __init__(self, name, kat):
        self.__name = name
        self._svgItem = None
        self.__nodes = []
        self._kat = kat
        
        if not isinstance(kat,pykat.finesse.kat):
            raise exceptions.ValueError("kat argument is not a pykat.finesse.kat object")
            
        kat.add(self)
        
    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:
43
            raise exceptions.RuntimeError("createNode did not return a node for '{0}'".format(name))
Daniel Brown's avatar
Daniel Brown committed
44
        else:
45
46
            n.connect(self)
                
Daniel Brown's avatar
Daniel Brown committed
47
48
49
50
51
52
53
54
55
56
57
58
59
            self.__nodes.append(n)
        
        return n
        
    def getNodes(self):
        """ Returns a copy of the nodes the component has """
        return self.__nodes[:]        
            
    def __getname(self):
        return self.__name      
        
    name = property(__getname)
    
60
61
62
63
class Param(float):
    def __new__(self,name,value):
        return float.__new__(self,value)
         
Daniel Brown's avatar
Daniel Brown committed
64
65
66
    def __init__(self,name,value):
        self.__name = name
        
67
68
    name = property(lambda self: self.__name)
           
Daniel Brown's avatar
Daniel Brown committed
69
70
71
72
73
74
75
76
class mirror(Component):
    def __init__(self,kat,name,node1,node2,R=0,T=0,phi=0,Rcx=0,Rcy=0,xbeta=0,ybeta=0):
        
        Component.__init__(self,name,kat)
        
        self.node1 = self._addNode(node1)
        self.node2 = self._addNode(node2)
        
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
        self.__R = R
        self.__T = T
        self.__phi = phi
        self.__Rcx = Rcx
        self.__Rcy = Rcy
        self.__xbeta = xbeta
        self.__ybeta = ybeta
            
    @property
    def R(self):
        return Param('R',self.__R)
    @R.setter
    def R(self,value):
        self.__R = value
    @property
    def T(self): return Param('T', self.__T)
    @T.setter
    def T(self,value): self.__T = value
        
    @property
    def phi(self): return Param('phi', self.__phi)
    @phi.setter
    def phi(self,value): self.__phi = value
    
    @property
    def Rcx(self): return Param('Rcx', self.__Rcx)
    @Rcx.setter
    def Rcx(self,value): self.__Rcx = value
    @property
    def Rcy(self): return Param('Rcy', self.__Rcy)
    @Rcy.setter
    def Rcy(self,value): self.__Rcy = value
    
    
    @property
    def xbeta(self): return Param('xbeta', self.__xbeta)
    @xbeta.setter
    def xbeta(self,value): self.__xbeta = value
    @property
    def ybeta(self): return Param('ybeta', self.__ybeta)
    @ybeta.setter
    def ybeta(self,value): self.__ybeta = value
    
    @property
    def Rc(self):
Daniel Brown's avatar
Daniel Brown committed
122
123
124
125
126
        if self.Rcx == self.Rcy:
            return self.Rcx
        else:
            return [self.Rcx, self.Rcy]
    
127
128
    @Rc.setter
    def Rc(self,value):
Daniel Brown's avatar
Daniel Brown committed
129
130
131
132
133
134
        self.Rcx = value
        self.Rcy = value
        
    def getFinesseText(self):        
        rtn = []
        rtn.append('m {0} {1} {2} {3} {4} {5}'.format(
135
                self.name, self.__R, self.__T, self.__phi,
Daniel Brown's avatar
Daniel Brown committed
136
137
                self.node1.name, self.node2.name))
            
138
139
140
141
        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
142
143
144
145
146
        
        return rtn
        
    def getQGraphicsItem(self):
        if self._svgItem == None:
147
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/mirror_flat.svg",self
148
                                                ,[(-4,15,self.node1),(14,15,self.node2)])
Daniel Brown's avatar
Daniel Brown committed
149
150
151
152
153
154
155
156
157
158
        return self._svgItem
   
   
class space(Component):
    def __init__(self,kat , name, node1, node2, L=0, n=1):
        Component.__init__(self,name,kat)
        
        self.node1 = self._addNode(node1)
        self.node2 = self._addNode(node2)
        
159
160
161
162
163
164
165
166
167
168
169
170
171
        self.__L = L
        self.__n = n
        self._QItem = None
        
    @property
    def L(self): return Param('L', self.__L)
    @L.setter
    def L(self,value): self.__L = value
    @property
    def n(self): return Param('n', self.__n)
    @n.setter
    def n(self,value): self.__n = value
    
Daniel Brown's avatar
Daniel Brown committed
172
    def getFinesseText(self):
173
174
        if self.__n == 1:
            return 's {0} {1} {2} {3}'.format(self.name, self.__L, self.node1.name, self.node2.name)            
Daniel Brown's avatar
Daniel Brown committed
175
        else:
176
            return 's {0} {1} {2} {3} {4}'.format(self.name, self.__L, self.__n, self.node1.name, self.node2.name)            
Daniel Brown's avatar
Daniel Brown committed
177
       
178
179
    def getQGraphicsItem(self):
        if self._QItem == None:
180
            self._QItem = pykat.gui.graphics.SpaceQGraphicsItem(self)
181
182
183
        
        return self._QItem  

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    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)
        
        if self.node1 == node_old:
            self.node1 = node_new
        
        if self.node2 == node_old:
            self.node2 = node_new

    
Daniel Brown's avatar
Daniel Brown committed
199
200
201
202
203
204
class laser(Component):
    def __init__(self,kat,name,node,P=1,f_offset=0,phase=0):
        Component.__init__(self,name,kat)
                
        self.node = self._addNode(node)
        
205
206
207
        self.__power = P
        self.__f_offset = f_offset
        self.__phase = phase
Daniel Brown's avatar
Daniel Brown committed
208
        
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    @property
    def power(self): return Param('P', self.__power)
    @power.setter
    def power(self,value): self.__power = value
    
    @property
    def f_offset(self): return Param('f', self.__f_offset)
    @f_offset.setter
    def f_offset(self,value): self.__f_offset = value
    
    @property
    def phase(self): return Param('phase', self.__phase)
    @phase.setter
    def phase(self,value): self.__phase = value
    
Daniel Brown's avatar
Daniel Brown committed
224
    def getFinesseText(self):
225
226
        if self.__phase == 0 :
            return 'l {0} {1} {2} {3}'.format(self.name, self.__power, self.__f_offset, self.node.name)            
Daniel Brown's avatar
Daniel Brown committed
227
        else :
228
            return 'l {0} {1} {2} {4} {3}'.format(self.name, self.__power, self.__f_offset, self.__phase, self.node.name)            
Daniel Brown's avatar
Daniel Brown committed
229
230
231
         
    def getQGraphicsItem(self):
        if self._svgItem == None:
232
            self._svgItem = pykat.gui.graphics.ComponentQGraphicsItem(":/resources/laser.svg",
233
                                                   self,[(65,25,self.node)])
Daniel Brown's avatar
Daniel Brown committed
234
235
236
            
        return self._svgItem