/
TestSuite.py
186 lines (169 loc) · 5.95 KB
/
TestSuite.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
#!/usr/bin/env python
import unittest
import numpy as np
import matplotlib
matplotlib.use('AGG')
import matplotlib.mlab as ml
import pylab as pl
import time
from GaussianBeam import *
class GaussianBeamTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testFunScalarArgs(self):
w0 = 8.0
lam = 3.0
k = 2*np.pi/lam
zc = confocalDistance(w0, k)
self.assertEqual(zc, np.pi*w0**2/lam)
z = 0.0;
q = complexBeamParameter(zc, z)
self.assertEqual(q.real, z)
self.assertEqual(q.imag, zc)
R = radiusOfCurvature(q)
self.assertEqual(R, np.inf)
w = beamRadius(q, k)
self.assertAlmostEqual(w, w0)
def testFunArrayArgs(self):
w0 = 8.0
lam = 3.0
k = 2*np.pi/lam
zc = confocalDistance(w0, k)
z = np.array([0, zc])
q = complexBeamParameter(zc, z)
self.assertEqual(q.real[0], z[0])
self.assertEqual(q.real[1], zc)
self.assertEqual(q.imag[0], zc)
self.assertEqual(q.imag[1], zc)
R = radiusOfCurvature(q)
self.assertEqual(R[0], np.inf)
self.assertEqual(R[1], 2*zc)
w = beamRadius(q, k)
self.assertAlmostEqual(w[0], w0)
self.assertAlmostEqual(w[1], np.sqrt(2.0)*w0)
self.assertAlmostEqual(beamWaistRadius(q[1], k), w0)
#if not dryTest: pl.figure(); pl.plot(z, w)
#print "Running field tests ..."
r = np.array([0, w0, 2*w0])
A = fieldAmplitude(q[0], k, r)
self.assertAlmostEqual(abs(A[0]), 1)
self.assertAlmostEqual(abs(A[1]), np.exp(-1.0))
self.assertAlmostEqual(abs(A[2]), np.exp(-4.0))
P = fieldPhase(q[0], k, r)
self.assertAlmostEqual(abs(P[0]), 0)
self.assertAlmostEqual(abs(P[1]), 0)
# TODO: add some characteristic points along Z
def testClasses(self):
w0 = 8.0
lam = 3.0
k = 2*np.pi/lam
zc = confocalDistance(w0, k)
z = np.array([0, zc])
q = complexBeamParameter(zc, z)
#print "Testing GaussianBeam class"
gb = GaussianBeam(beamWaist(q[0], k), k)
(R,Z)= pl.meshgrid(np.arange(-24,24), np.arange(0,100))
#if not dryTest: pl.figure(); pl.imshow(abs(gb.field(R, Z)))
d = 10.0
q = gb.q(0)
abcd = np.matrix([[1, d],[0, 1]], dtype=float)
qo = abcd*q
self.assertEqual(qo, gb.q(d))
#print "Testing ParaxialElement class"
el = ParaxialElement(abcd, 0)
gb2 = el*gb
#print gb2.q(0)
#print gb.q(d)
self.assertEqual(gb2.q(0), gb.q(d))
def testTelescope(self):
# import matplotlib
# matplotlib.use('AGG')
# import matplotlib.mlab as ml
# import pylab as pl
# import time
w0 = 8.0
k = 2*np.pi/3.0
gb = GaussianBeam(w0, k)
lens = ThinLens(150, 150)
gb2 = lens*gb
self.assertAlmostEqual(gb2._z0, gb._z0 + 2*150.0)
lens2 = ThinLens(300, 600)
gb3 = lens2*gb2
self.assertAlmostEqual(gb3._z0, gb2._z0 + 2*300.0)
self.assertAlmostEqual(gb._w0, gb3._w0/2.0)
z = np.arange(0, 150)
z2 = np.arange(150, 600)
z3 = np.arange(600, 900)
pl.plot(z, gb.w(z, k), z2, gb2.w(z2, k), z3, gb3.w(z3, k))
pl.grid()
pl.xlabel('z')
pl.ylabel('w')
pl.savefig('testTelescope1.png')
time.sleep(0.1)
pl.close('all')
def testTelescope2(self):
# import matplotlib
# matplotlib.use('AGG')
# import matplotlib.mlab as ml
# import pylab as pl
# import time
w0 = 1.0
k = 1000*2*np.pi/1.064
gb = GaussianBeam(w0, k)
lens = ThinLens(50, 50)
gb2 = lens*gb
#self.assertAlmostEqual(gb2._z0, gb._z0 + 2*151.0)
lens2 = ThinLens(100, 160)
gb3 = lens2*gb2
#self.assertAlmostEqual(gb3._z0, gb2._z0 + 2*300.0)
#self.assertAlmostEqual(gb._w0, gb3._w0/2.0)
lens3 = ThinLens(18.58, 380)
gb4 = lens3*gb3
z = np.arange(0, 50)
z2 = np.arange(50, 160)
z3 = np.arange(160, 380)
z4 = np.arange(380, 450)
pl.plot(z, gb.w(z, k), 'g', z2, gb2.w(z2, k), 'g', z3, gb3.w(z3, k), 'g', z4, gb4.w(z4, k), 'g')
#pl.grid()
w_0 = min(gb4.w(z4,k))
z_0 = [i for i,x in enumerate(gb4.w(z4,k)) if x == w_0]
pl.title("Example of brilliant optical design!" + "w_0 = " + str(w_0) + " z_0 = " + str(z_0[0]+380))
pl.xlabel('z')
pl.ylabel('w')
pl.savefig('testTelescope2.png')
time.sleep(0.1)
pl.close('all')
class ParaxialElementTest(unittest.TestCase):
def setUp(self):
self.gb = GaussianBeam((8.0, -50.0), 2*np.pi/3.0)
def testThinLens(self):
# import matplotlib
# matplotlib.use('AGG')
# import matplotlib.mlab as ml
# import pylab as pl
# import time
lens = ThinLens(150.0, 100.0)
gbOut = lens*self.gb
self.assertAlmostEqual(gbOut._z0, 250.0)
(w1, z1) = lens.transformBeamWaist((self.gb._w0, self.gb._z0), self.gb._k)
self.assertAlmostEqual(gbOut._w0, w1)
f = 150.0
lens = ThinLens(f, 0)
w0, z0 = np.meshgrid([4.0, 8.0, 16.0], np.arange(-3*f, f))
(w1, z1) = lens.transformBeamWaist((w0, z0), self.gb._k)
h = pl.figure()
pl.plot(z0/f, z1/f)
pl.grid()
pl.xlabel('d_{in} [f]')
pl.ylabel('d_{out} [f]')
pl.savefig('testThinLens1.png')
time.sleep(0.1)
pl.close('all')
if __name__ == '__main__':
#unittest.main()
suiteGB = unittest.TestLoader().loadTestsFromTestCase(GaussianBeamTest)
suitePE = unittest.TestLoader().loadTestsFromTestCase(ParaxialElementTest)
suite = unittest.TestSuite([suiteGB, suitePE])
unittest.TextTestRunner(descriptions=2, verbosity=2).run(suite)