-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathABCD_python.py
569 lines (462 loc) · 11 KB
/
ABCD_python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
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
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
383
384
385
386
387
388
389
390
391
392
393
394
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
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
560
561
562
563
564
565
566
567
568
569
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 7 13:49:52 2020
@author: schaecl
Script to propagate Gaussian beams via the ABCD formalism.
"""
import numpy as np
class qbeam:
"""
Class to describe a q parameter of a Gaussian beam."""
z_re = 0
zr_im = 0
wlen = 0
name = ""
DBL_MAX = np.finfo(float).max
def __init__(self, z_start: float, zr_start: float, wlen: float, name: str):
self.z_re = z_start
self.zr_im = zr_start
self.wlen = wlen
self.name = name
def set_q(self, z_start: float, zr_start: float, wlen: float):
"""
Sets the parameters.
Parameters
----------
z_start : float
z position at which the beam starts.
zr_start : float
Rayleigh range.
wlen : float
Wavelength.
Returns
-------
None.
"""
self.z_re = z_start
self.zr_im = zr_start
self.wlen = wlen
def get_w0(self):
"""
Returns the beam waist.
Returns
-------
flaot
Beam waist.
"""
return np.sqrt(self.zr_im * self.wlen / np.pi)
def get_z(self):
"""
Returns the position at which the waist is to be found.
Returns
-------
float
Position z0.
"""
return self.z_re
def get_zr(self):
"""
Returns the Rayleigh range.
Returns
-------
float
Rayleigh range.
"""
return self.zr_im
def get_beamrad(self):
"""
Returns the wavefront curvature at z.
TODO Implement function of z.
Returns
-------
float
Wavefront curvature at z.
"""
r = self.z_re
if r != 0.:
r += (self.zr_im**2 / r)
else:
return self.DBL_MAX
return r
def get_wz(self):
"""
Return the beam radius at z.
TODO Implement function of z.
Returns
-------
wz : float
Beam radius at z.
"""
wz = self.zr_im
wz += (self.z_re**2 / self.zr_im)
wz *= (self.wlen / np.pi)
wz = np.sqrt(wz)
return wz
def get_div(self):
"""
Returns the far field divergence of the beam.
Returns
-------
float
Far field divergence (half-angle).
"""
return np.atan(np.sqrt(self.wlen / (self.zr_im * np.pi)))
def q_out(self):
"""
Prints the q parameter and beam properties.
Returns
-------
None.
"""
if self.get_beamrad() == self.DBL_MAX:
print("q.{:6.6s}: {:12s} {:12s}\n{:9s} {:12g} {:12g}\n"
"{:9s} {:12s} {:12s}\n{:9s} {:12s} {:12g}\n".format(
self.name, "z[e-3m]", "w0[e-6m]", "",
self.get_z(), self.get_w0() * 1e3, "",
"R[e-3m]", "wz[e-6m]", "", "inf", self.get_wz() * 1e3))
else:
print("q.{:6.6s}: {:12s} {:12s}\n{:9s} {:12g} {:12g}\n"
"{:9s} {:12s} {:12s}\n{:9s} {:12g} {:12g}\n".format(
self.name, "z[e-3m]", "w0[e-6m]", "",
self.get_z(), self.get_w0() * 1e3, "",
"R[e-3m]", "wz[e-6m]", "", self.get_beamrad(), self.get_wz() * 1e3))
def thin_lens(f):
"""
Returns an ABCD matrix of a thin lens.
Parameters
----------
f : float
Focal length.
Returns
-------
M : array, float
ABCD matrix.
"""
M = np.array([[0., 0.], [0., 0.]])
M[0][0] = 1.
M[0][1] = 0.
M[1][0] = -1. / f
M[1][1] = 1.
return M
def thick_lens(r1, r2, d, n):
"""
Returns an ABCD matrix for a thick lens.
Parameters
----------
r1 : float
First surface ROC.
r2 : float
Second surface ROC.
d : float
Thickness.
n : float
Refractive index.
Returns
-------
M : array, float
ABCD Matrix.
"""
M = np.array([[0., 0.], [0., 0.]])
ni = 1. / n
dr1 = 0.
r1i = 0.
r2i = 0.
if r1 != 0.:
r1i = 1. / r1
dr1 = d * r1i
if r2 != 0.:
r2i = 1. / r2
M[0][0] = 1. + dr1 * (ni - 1.)
M[0][1] = d * ni
M[1][0] = (1. - n) * (r1i - r2i) + dr1 * r2i * (2. - ni - n)
M[1][1] = 1. - d * r2i * (ni - 1.)
return M
def thick_p_ck_lens(r2, d, n):
"""
Returns an ABCD matrix for a plane-concave lens.
Parameters
----------
r2 : float
Second surface ROI.
d : float
Thickness of the lens.
n : float
Refractive index.
Returns
-------
M : array, float
ABCD matrix.
"""
M = np.array([[0., 0.], [0., 0.]])
ni = 1. / n
M[0][0] = 1.
M[0][1] = d * ni
M[1][0] = -(1. - n) / r2
M[1][1] = 1. - d / r2 * (ni - 1.)
return M
def propagation(d):
"""
Returns an ABCD matrix to propagate a beam.
Parameters
----------
d : float
Distance along the beam axis.
Returns
-------
M : array, float
ABCD matrix.
"""
M = np.array([[0., 0.], [0., 0.]])
M[0][0] = 1.
M[0][1] = d
M[1][0] = 0.
M[1][1] = 1.
return M
def refraction(n1, n2, r):
"""
Returns and ABCD matrix for refraction.
Parameters
----------
n1 : float
First index of refraction.
n2 : float
Second index of refraction.
r : float
ROC.
Returns
-------
M : array, float
ABCD matrix.
"""
M = np.array([[0., 0.], [0., 0.]])
M[0][0] = 1.
M[0][1] = 0.
if r != 0.:
M[1][0] = (n1 - n2) / (n2 * r)
else:
M[1][0] = 0.
M[1][1] = n1 / n2
return M
def reflection(r):
"""
Returns an ABCD matrix for reflection.
Parameters
----------
r : float
ROC.
Returns
-------
M : array, float
ABCD matrix.
"""
M = np.array([[0., 0.], [0., 0.]])
M[0][0] = 1.
M[0][1] = 0.
M[1][0] = -2. / r
M[1][1] = 1.
return M
def grin(d, n0, nmin):
"""
Returns an ABCD matrix for a GRIN lens.
Parameters
----------
d : float
Length of the lens.
n0 : float
Nominal refractive index.
nmin : float
Minimum refractive index.
Returns
-------
M : array, float
ABCD matrix.
"""
M = np.array([[0., 0.], [0., 0.]])
t1 = np.sqrt(nmin / n0)
t2 = t1 * d
t3 = np.sin(t2)
M[0][0] = np.cos(t2)
M[0][1] = t3 / t1
M[1][0] = -t1 * t3
M[1][1] = M[0][0]
return M
def qtrans(M, q):
"""
Transports a beam from q to qp via an ABCD matrix.
Parameters
----------
M : float
ABCD matrix.
q : qbeam
Initial q parameter.
Returns
-------
qp : qbeam
Propagated qbeam.
"""
qp = qbeam(0., 0., q.wlen, q.name)
t1 = M[1][0] * q.z_re + M[1][1]
t2 = t1 * t1
t3 = M[0][0] * q.z_re + M[0][1]
t4 = M[1][0]**2
t5 = q.zr_im**2
denom = t2 + t4 * t5
if denom == 0.:
print("singularity in qtrans. healing.")
denom = 1e-15
qp.z_re = t1 * t3 + t5 * M[0][0] * M[1][0]
qp.z_re /= denom
qp.zr_im = q.zr_im * (M[0][0] * t1 - M[1][0] * t3)
qp.zr_im /= denom
return qp
def wz(w0, z, zr):
"""
Returns the beam radius at position z.
Parameters
----------
w0 : float
Beam waist.
z : float
Position along the propagation axis where the radius should be
evaluated.
zr : float
Rayleigh range.
Returns
-------
TYPE
DESCRIPTION.
"""
t = z / zr
t *= t
return w0 * np.sqrt(1. + t)
def rayleighr(w0, lam):
"""
Returns the Rayleigh range.
Parameters
----------
w0 : float
Beam waist.
lam : float
Wavelength.
Returns
-------
float
Rayleigh range.
"""
return np.pi * w0**2 / lam
def stab_parm(M):
"""
Return stability parameter of an ABCD matrix.
Parameters
----------
M : array, float
ABCD matrix.
Returns
-------
float
Stability parameter.
"""
return (M[0][0] + M[1][1]) / 2.
def eigenval_res(M):
"""
Returns the eigenvalue(s) of an ABCD matrix.
Parameters
----------
M : array, float
ABCD matrix.
Returns
-------
complex
First eigenvalue.
complex
Second eigenvalue.
"""
c1_re = c1_im = 0.
c2_re = c2_im = 0.
g = stab_parm(M)
t = g**2 - 1.
if t < 0.:
t = np.sqrt(-t)
c1_re = c2_re = g
c1_im = t
c2_im = -t
elif t > 0.:
t = np.sqrt(t)
c1_im = c2_im = 0.
c1_re = g + t
c2_re = g - t
else:
c1_im = c2_im = 0.
c1_re = c2_re = g
return complex(c1_re, c1_im), complex(c2_re, c2_im)
def eigenmod_res(M):
"""
Return (if any) the eigenmodes of a cavity given by an ABCD matrix.
Parameters
----------
M : array, float
ABCD matrix.
Returns
-------
complex
Eigenmode (value) of the matrix.
"""
c_re = c_im = 0
t1 = M[1][1] - M[0][0]
if t1 != 0.:
t3 = t1**2
else:
c_re = 0.
t3 = M[0][1] / M[1][0]
if t3 >= 0.:
print("\nfound no eigenmode")
c_im = 0.
else:
c_im = np.sqrt(-t3)
print("\nfound eigenmode")
return complex(c_re, c_im)
t2 = 2. * M[1][0]
c_re = t1 / t2
t2 *= t2
t3 = (t3 + 4. * M[0][1] * M[1][0]) / t2
if t3 >= 0.:
print("\nfound no eigenmode")
c_re = c_im = 0.
else:
print("\nfound eigenmode")
c_im = np.sqrt(-t3)
return complex(c_re, c_im)
#%% Tests
if __name__ == "__main__":
# Ray propagation through optical elements
q = qbeam(0., rayleighr(1e-1, 632.8e-6), 632.8e-6, "in")
M = np.matmul(propagation(172.1), \
np.matmul(thin_lens(140.4), \
np.matmul(propagation(1400.), \
np.matmul(thin_lens(258.), \
propagation(400.)))))
qp = qtrans(M, q)
q.q_out()
qp.q_out()
# The output should read
# q.in : z[e-3m] w0[e-6m]
# 0 100
# R[e-3m] wz[e-6m]
# inf 100
# q.out : z[e-3m] w0[e-6m]
# -0.0721956 40.0079
# R[e-3m] wz[e-6m]
# -874.728 40.0095
# A linear resonator, left mirror is plane, distance to right mirror is
# 400 mm, curvature of right mirror is 2 m
qt = qbeam(0., rayleighr(1e-1, 1064e-6), 1064e-6, "res")
M = np.matmul(propagation(400.), np.matmul(reflection(2000.), propagation(400.)))
c = eigenmod_res(M)
qt.z_re = c.real
qt.zr_im = c.imag
qt.q_out()
# The output should read
# q.res : z[e-3m] w0[e-6m]
# 0 520.524
# R[e-3m] wz[e-6m]
# inf 520.524