gaussian_beams.py 9.28 KB
Newer Older
1
import pykat.exceptions as pkex
2
import numpy as np
3
import math
4
import copy
5
6
7
import warnings
import cmath
from scipy.special import hermite
8
from pykat.SIfloat import SIfloat
9
10
11

class gauss_param(object):
    """
12
13
    Use beam_param instead, prefer that as a name
    
14
15
16
17
18
19
20
21
22
    Gaussian beam complex parameter
    
    gauss_param is effectively a complex number with extra
    functionality to determine beam parameters.
    
    defaults to 1064e-9m for wavelength and refractive index 1
    usage:
        q = gauss_param(w0=w0, z=z)
        q = gauss_param(z=z, zr=zr)
23
        q = gauss_param(w=w, rc=rc)
24
        q = gauss_param(q=a) # where a is a complex number
25
26
27
28
29
30
31
        
        or change default wavelength and refractive index with:
        
        q = gauss_param(wavelength, nr, w0=w0, zr=zr)
    """
    
    def __init__(self, wavelength=1064e-9, nr=1, *args, **kwargs):
32
33
34
        if self.__class__ != beam_param:
            warnings.warn("Name changed. Use beam_param instead of gauss_param.")
            
35
        self.__q = None
36
37
        self.__lambda = SIfloat(wavelength)
        self.__nr = SIfloat(nr)
38
39
        
        if len(args) == 1:
Daniel Brown's avatar
Daniel Brown committed
40
            self.__q = complex(args[0])
41
42
43
44
45
        
        elif len(kwargs) == 1:
            if "q" in kwargs:
                self.__q = complex(kwargs["q"])        
            else:
46
                raise pkex.BasePyKatException("Must specify: z and w0 or z and zr or rc and w or q, to define the beam parameter")
47
                
48
49
50
        elif len(kwargs) == 2:        
            
            if "w0" in kwargs and "z" in kwargs:
51
                q = SIfloat(kwargs["z"]) + 1j *float(math.pi*SIfloat(kwargs["w0"])**2/(self.__lambda/self.__nr) )
52
            elif "z" in kwargs and "zr" in kwargs:
53
                q = SIfloat(kwargs["z"]) + 1j *SIfloat(kwargs["zr"]) 
54
            elif "rc" in kwargs and "w" in kwargs:
55
                one_q = 1 / SIfloat(kwargs["rc"]) - 1j * self.__lamda / (math.pi * self.__nr * SIfloat(kwargs["w"])**2)
56
57
                q = 1/one_q
            else:
58
                raise pkex.BasePyKatException("Must specify: z and w0 or z and zr or rc and w or q, to define the beam parameter")
59
60
61
62
63
64
65
                
            self.__q = q
        else:
            raise pkex.BasePyKatException("Incorrect usage for gauss_param constructor")
    
    @property
    def wavelength(self): return self.__lambda
66
67
    @wavelength.setter
    def wavelength(self,value): self.__lambda = SIfloat(value)
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    
    @property
    def nr(self): return self.__nr
    
    @property
    def q(self): return self.__q
    
    @property
    def z(self): return self.__q.real
    
    @property
    def zr(self): return self.__q.imag
    
    @property
82
    def w(self):
83
84
        return abs(self.__q)*math.sqrt(self.__lambda / (self.__nr * math.pi * self.__q.imag))
        #return self.w0 * math.sqrt(1 + (self.__q.real/self.__q.imag)**2)
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
    
    @property
    def w0(self):
        return math.sqrt(self.__q.imag * self.__lambda / (self.__nr * math.pi))    

    @property
    def Rc(self):
        if self.__q.real != 0:
            return abs(self.__q) / self.__q.real
        else:
            return float("inf")
    
    def conjugate(self):
        return gauss_param(self.__lambda, self.__nr, self.__q.conjugate())
    
    def __complex__(self):
        return self.__q
    
    def __str__(self):
        return str(self.__q)
    
    def __mul__(self, a):
        return gauss_param(self.__lambda, self.__nr, self.__q * complex(a))
    
    def __imul__(self, a):
110
        self.__q *= complex(a)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        return self
        
    __rmul__ = __mul__
    
    def __add__(self, a):
        return gauss_param(self.__lambda, self.__nr, self.__q + complex(a))
    
    def __iadd__(self, a):
        self.__q += complex(a)
        return self
        
    __radd__ = __add__
    
    def __sub__(self, a):
        return gauss_param(self.__lambda, self.__nr, self.__q - complex(a))
    
    def __isub__(self, a):
        self.__q -= complex(a)
        return self
        
131
132
    def __rsub__(self, a):
        return gauss_param(self.__lambda, self.__nr, complex(a) - self.__q)
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    
    def __div__(self, a):
        return gauss_param(self.__lambda, self.__nr, self.__q / complex(a))
    
    def __idiv__(self, a):
        self.__q /= complex(a)
        return self
    
    def __pow__(self, q):
        return gauss_param(self.__lambda, self.__nr, self.__q**q)

    def __neg__(self, q):
        return gauss_param(self.__lambda, self.__nr, -self.__q)
        
    def __eq__(self, q):
148
149
150
151
152
153
154
155
156
157
        return complex(q) == self.__q
        
    @property
    def real(self): return self.__q.real
    @real.setter
    def real(self, value): self.__q.real = SIfloat(value)
    
    @property
    def imag(self): return self.__q.imag
    @imag.setter
158
    def imag(self, value): self.__q.imag = SIfloat(value)
159
160
161
162

    # reverse beam direction 
    def reverse(self):
        self.__q = -1.0 * self.__q.real + 1j * self.__q.imag
163
164
165
166
167


class beam_param(gauss_param):
    pass

168
    
169
class HG_beam(object):
170
171
172
173
174
    
    def __init__(self, qx, qy=None, n=0, m=0):
        self._qx = copy.deepcopy(qx)
        self._2pi_qrt = math.pow(2.0/math.pi, 0.25)
        
175
176
        if qy.__class__ == beam_param:
            self._qy = copy.deepcopy(qx)
177
        else:
178
            self._qy = copy.deepcopy(qy)
179
    
180
181
182
183
        self._n = n
        self._m = m
        self._hn = hermite(n)
        self._hm = hermite(m)
184
185
186
187
188
189
        self._calc_constants()
        
    @property
    def n(self): return self._n
    @n.setter
    def n(self,value): 
190
        self._n = int(value)
191
        self._calc_constants()
192
        self._hn = hermite(self._n)
193
194
195
196
197

    @property
    def m(self): return self._m
    @m.setter
    def m(self,value): 
198
        self._m = int(value)
199
        self._calc_constants()
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
        self._hm = hermite(self._m)
            
    @property
    def q(self):
        if self._qx.q == self._qy.q:
            return self._qx.q
        else:
            return (self._qx.q, self._qy.q)
    @q.setter
    def q(self, value):
        if value.__class__ == beam_param:
            self._qx = copy.deepcopy(value)
            self._qy = copy.deepcopy(value)
        else:
            self._qx = beam_param(q=complex(value))
            self._qy = beam_param(q=complex(value))
    
    @property
    def qx(self):
        return self._qx.q
        
    @qx.setter
    def qx(self, value):
        if value.__class__ == beam_param:
            self._qx = copy.deepcopy(value)
        else:
            self._qx = beam_param(q=complex(value))
    
    @property
    def qy(self):
        return self._qy.q
231
        
232
233
234
235
236
237
238
    @qy.setter
    def qy(self, value):
        if value.__class__ == beam_param:
            self._qy = copy.deepcopy(value)
        else:
            self._qy = beam_param(q=complex(value))
            
239
240
    def _calc_constants(self):
        self.__xpre_const = math.pow(2.0/math.pi, 0.25)
241
242
243
        self.__xpre_const *= math.sqrt(1.0/(2**self._n * math.factorial(self._n)))
        self.__xpre_const *= cmath.sqrt(1j*self._qx.imag / self._qx.q)
        self.__xpre_const *= ((1j*self._qx.imag * self._qx.q.conjugate())/(-1j*self._qx.imag * self._qx.q)) ** ( self._n/2.0)
244
245
        
        self.__ypre_const = math.pow(2.0/math.pi, 0.25)
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
        self.__ypre_const *= math.sqrt(1.0/(2**self._m * math.factorial(self._m)))
        self.__ypre_const *= cmath.sqrt(1j*self._qy.imag / self._qy.q)
        self.__ypre_const *= ((1j*self._qy.imag * self._qy.conjugate())/(-1j*self._qy.imag * self._qy.q)) **(self._m/2.0)
    
        self.__sqrt2_wxz = math.sqrt(2) / self._qx.w
        self.__sqrt2_wyz = math.sqrt(2) / self._qy.w
        
        self.__kx =  2*math.pi / self._qx.wavelength
        self.__ky =  2*math.pi / self._qy.wavelength
        
        self.__invqx = 1/ self._qx.q
        self.__invqy = 1/ self._qy.q
        
    def _Un(self, x):
        return self.__xpre_const * self._hn(self.__sqrt2_wxz * x) * np.exp(-0.5j * self.__kx * x*x * self.__invqx)
    
    def _Um(self, y):
        return  self.__ypre_const  * self._hm(self.__sqrt2_wyz * y) * np.exp(-0.5j * self.__ky * y*y * self.__invqy)

    def Un(self, x):  
        vec = np.vectorize(self._Un, otypes=[np.complex64])
        return vec(x=x)

    def Um(self, y):  
        vec = np.vectorize(self._Um, otypes=[np.complex64])
        return vec(y=y)
            
    def _unm(self, x, y):
        return self._Un(x) * self._Um(y)
        
    def Unm(self, x, y):  
        vec = np.vectorize(self._unm, otypes=[np.complex64])
        return vec(x=x,y=y)
        
    def plot(self, ndx=100, ndy=100, xscale=4, yscale=4):
        import pylab
        
        xrange = xscale * np.linspace(-self._qx.w, self._qx.w, ndx)
        yrange = yscale * np.linspace(-self._qy.w, self._qy.w, ndy)

        dx = xrange[1]-xrange[0]
        dy = yrange[1]-yrange[0]

        xx, yy = np.meshgrid(xrange,yrange)

        data = self.Unm(xx, yy)

        fig = pylab.figure()
        axes = pylab.imshow(np.abs(data), aspect=dx/dy, extent=[min(xrange),max(xrange),min(yrange),max(yrange)])
        pylab.xlabel('x [m]')
        pylab.ylabel('y [m]')
        cbar = fig.colorbar(axes)
        pylab.show()
299
        
300