-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poincare_sphere.py
282 lines (231 loc) · 9.11 KB
/
Poincare_sphere.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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.widgets import Button, Slider, RadioButtons
from functools import partial
# Define horizontally polarised light (theta = 0, phi = 0) and
# a HWP (eta = pi, varphi = 0) whose fast axes is oriented at
# (vartheta = ) 22.5° w.r.t. the optical table in the laboratory frame
q, p, e, vq, vp = 0, 0, np.pi, np.pi/8, 0
fig = plt.figure(constrained_layout = True)
ax = fig.add_subplot(111, projection='3d')
# Define sphere
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 50)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
# Plot sphere
ax.plot_surface(x, y, z, color='purple', alpha=0.1, rstride=5, cstride=5)
# Remove axes
ax.set_axis_off()
# Define aspect ratio
ax.set_aspect('equal')
# Draw x, y, z axes from the origin using quiver
ax.quiver(-1, 0, 0, 2, 0, 0, color='k', arrow_length_ratio=0., lw = 0.5) # x-axis
ax.quiver(0, -1, 0, 0, 2, 0, color='k', arrow_length_ratio=0, lw = 0.5) # y-axis
ax.quiver(0, 0, -1, 0, 0, 2, color='k', arrow_length_ratio=0, lw = 0.5) # z-axis
# Plot equator
equator_u = np.linspace(0, 2 * np.pi, 50)
equator_x = np.cos(equator_u)
equator_y = np.sin(equator_u)
equator_z = np.zeros_like(equator_u)
ax.plot(equator_x, equator_y, equator_z, color='k', ls = 'dotted', lw = 0.5)
# Plot prime meridian
meridian_v = np.linspace(0, 2 * np.pi, 50)
meridian_x = np.sin(meridian_v)
meridian_y = np.zeros_like(meridian_v)
meridian_z = np.cos(meridian_v)
ax.plot(meridian_x, meridian_y, meridian_z, color='k', ls = 'dotted', lw = 0.5)
# Plot other meridian at 90 degrees
meridian_x = np.zeros_like(meridian_v)
meridian_y = np.sin(meridian_v)
meridian_z = np.cos(meridian_v)
ax.plot(meridian_x, meridian_y, meridian_z, color='k', ls = 'dotted', lw = 0.5)
# Plot points (example points on the sphere)
points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, -1, 0], [0, 0, -1]])
for point in points:
ax.scatter(point[0], point[1], point[2], color='k', s=20)
# Point labels
ax.text(1.2, 0, 0, 'H', color='k')
ax.text(-1.1, 0, 0, 'V', color='k')
ax.text(0, 1.1, 0, 'D', color='k')
ax.text(0, -1.2, 0, 'A', color='k')
ax.text(0, 0, 1.1, 'R', color='k')
ax.text(0, 0, -1.2, 'L', color='k')
# Set plot label
ax.set_title('Wave plate in the Poincaré sphere')
# Define colour notation (x, y, z)
fig.text(0.01, 0.9, r"Green: initial polarisation state defined by $\theta$ and $\phi$", color='g', fontsize=8)
fig.text(0.01, 0.85, r"Red: wave plate rotation axis defined by $\vartheta$", color='r', fontsize=8)
fig.text(0.01, 0.8, r"Blue: intermediate and final polarisation states", color='b', fontsize=8)
# Set viewing angle (elevation, azimuth)
ax.view_init(elev=30, azim=60)
# Define radial variable to plot vector lines
r = np.linspace(0,1,10)
# Define vector line coordinates
def vector(x, y, z):
p = np.arctan2(y,x)
q = np.arccos(z)
xl = r*np.sin(q)*np.cos(p)
yl = r*np.sin(q)*np.sin(p)
zl = r*np.cos(q)
return [xl, yl, zl]
# Define WP rotation axis
xwp = np.cos(2*vq)*np.cos(vp)
ywp = np.sin(2*vq)*np.cos(vp)
zwp = np.cos(np.pi/2-vp)
xwpl, ywpl, zwpl = vector(xwp, ywp, zwp)
point_wp, = ax.plot(xwp, ywp, zwp, ls = "", marker = "o", color='r', markersize = 6)
line_wp, = ax.plot(xwpl, ywpl, zwpl, ls = "-", color='r', lw = 2)
# Get Stokes parameters S1, S2, and S3 from the Jones vector components alpha and beta after the action of a wave plate
def S(q, p, e, vq, vp):
alpha = (np.cos(e/2)-1j*np.sin(e/2)*np.cos(2*vq))*np.cos(q/2)+(-np.sin(e/2)*np.sin(vp)*np.sin(2*vq)-1j*np.sin(e/2)*np.cos(vp)*np.sin(2*vq))*np.exp(1j*p)*np.sin(q/2)
beta = (np.sin(e/2)*np.sin(vp)*np.sin(2*vq)-1j*np.sin(e/2)*np.cos(vp)*np.sin(2*vq))*np.cos(q/2)+(np.cos(e/2)+1j*np.sin(e/2)*np.cos(2*vq))*np.exp(1j*p)*np.sin(q/2)
S1 = np.abs(alpha)**2-np.abs(beta)**2
S2 = 2*np.real(alpha*np.conj(beta))
S3 = 2*np.imag(alpha*np.conj(beta))
return [S1, S2, S3]
# Define intermediate points
t = np.linspace(0,e,10)
points = []
for i in range(0,len(t)):
x, y, z = S(q, p, t[i], vq, vp)
pts, = ax.plot(x, y, z, ls = "", marker = "o", color='b', markersize = 6)
points.append(pts)
# Define initial Jones vector
xi, yi, zi = S(q, p, 0, 0, 0)
xil, yil, zil = vector(xi, yi, zi)
pointi, = ax.plot(xi, yi, zi, ls = "", marker = "o", color='g', markersize = 6)
linei, = ax.plot(xil, yil, zil, ls = "-", color='g', lw = 2)
# Define final Jones vector
xf, yf, zf = S(q, p, e, vq, vp)
xfl, yfl, zfl = vector(xf, yf, zf)
pointf, = ax.plot(xf, yf, zf, ls = "", marker = "o", color='b', markersize = 6)
linef, = ax.plot(xfl, yfl, zfl, ls = "-", color='b', lw = 2)
# Create sliders for q, p, and vq
ax_q = fig.add_axes([0.25, 0.15, 0.65, 0.05])
ax_p = fig.add_axes([0.25, 0.1, 0.65, 0.05])
ax_vq = fig.add_axes([0.25, 0.05, 0.65, 0.05])
qs = Slider(
ax_q, r"$\theta$ (°)", 0., 180,
valinit = 0, valstep = 1,
color="green",
initcolor='none',
handle_style = {'facecolor': 'white', 'edgecolor': '0.5', 'size': 10}
)
ps = Slider(
ax_p, r"$\phi$ (°)", -180, 180,
valinit = 0, valstep = 2,
color="green",
initcolor='none',
handle_style = {'facecolor': 'white', 'edgecolor': '0.5', 'size': 10}
)
vqs = Slider(
ax_vq, r"$\vartheta$ (°)", -45, 45,
valinit = 22.5, valstep = 0.5,
color="red",
initcolor='none',
handle_style = {'facecolor': 'white', 'edgecolor': '0.5', 'size': 10}
)
# Define vectors and points update function
def update_intermediate_points(points, xf, yf, zf):
points.set_data([xf], [yf])
points.set_3d_properties(zf)
def update_point_and_line(point, line, xf, yf, zf, xfl, yfl, zfl):
point.set_data([xf], [yf])
point.set_3d_properties(zf)
line.set_data(xfl, yfl)
line.set_3d_properties(zfl)
def update(val, e):
q = np.pi/180*qs.val
p = np.pi/180*ps.val
vq = np.pi/180*vqs.val
# Define WP rotation axis
xwp = np.cos(2*vq)*np.cos(vp)
ywp = np.sin(2*vq)*np.cos(vp)
zwp = np.cos(np.pi/2-vp)
xwpl, ywpl, zwpl = vector(xwp, ywp, zwp)
update_point_and_line(point_wp, line_wp, xwp, ywp, zwp, xwpl, ywpl, zwpl)
# Define intermediate points
t = np.linspace(0,e,10)
for i in range(0,len(t)):
x, y, z = S(q, p, t[i], vq, vp)
update_intermediate_points(points[i], x, y, z)
# Define initial Jones vector
xi, yi, zi = S(q, p, 0, 0, 0)
xil, yil, zil = vector(xi, yi, zi)
update_point_and_line(pointi,linei,xi, yi, zi, xil, yil, zil)
# Define final Jones vector
xf, yf, zf = S(q, p, e, vq, vp)
xfl, yfl, zfl = vector(xf, yf, zf)
update_point_and_line(pointf,linef,xf, yf, zf, xfl, yfl, zfl)
fig.canvas.draw_idle()
# Set sliders action
qs.on_changed(partial(update, e = np.pi))
ps.on_changed(partial(update, e = np.pi))
vqs.on_changed(partial(update, e = np.pi))
# Create WP buttons
ax_WP = fig.add_axes([0.05, 0.07, 0.1, 0.1])
radio_WP = RadioButtons(ax_WP, labels = ['HWP', 'QWP'], active = 0, activecolor = 'r')
# Define WP update function
def updatePR(label):
if label == 'HWP':
qs.reset()
ps.reset()
vqs.reset()
e = np.pi
# Define intermediate points
t = np.linspace(0,e,10)
for i in range(0,len(t)):
x, y, z = S(q, p, t[i], vq, vp)
update_intermediate_points(points[i], x, y, z)
# Define initial Jones vector
xi, yi, zi = S(q, p, 0, 0, 0)
xil, yil, zil = vector(xi, yi, zi)
update_point_and_line(pointi,linei,xi, yi, zi, xil, yil, zil)
# Define final Jones vector
xf, yf, zf = S(q, p, e, vq, vp)
xfl, yfl, zfl = vector(xf, yf, zf)
update_point_and_line(pointf,linef,xf, yf, zf, xfl, yfl, zfl)
fig.canvas.draw_idle()
# Set sliders action
qs.on_changed(partial(update, e = np.pi))
ps.on_changed(partial(update, e = np.pi))
vqs.on_changed(partial(update, e = np.pi))
elif label == 'QWP':
qs.reset()
ps.reset()
vqs.reset()
e = np.pi/2
# Define intermediate points
t = np.linspace(0,e,10)
for i in range(0,len(t)):
x, y, z = S(q, p, t[i], vq, vp)
update_intermediate_points(points[i], x, y, z)
# Define initial Jones vector
xi, yi, zi = S(q, p, 0, 0, 0)
xil, yil, zil = vector(xi, yi, zi)
update_point_and_line(pointi,linei,xi, yi, zi, xil, yil, zil)
# Define final Jones vector
xf, yf, zf = S(q, p, e, vq, vp)
xfl, yfl, zfl = vector(xf, yf, zf)
update_point_and_line(pointf,linef,xf, yf, zf, xfl, yfl, zfl)
fig.canvas.draw_idle()
# Set sliders action
qs.on_changed(partial(update, e = np.pi/2))
ps.on_changed(partial(update, e = np.pi/2))
vqs.on_changed(partial(update, e = np.pi/2))
# Set WP buttons action
radio_WP.on_clicked(updatePR)
# Create reset button
ax_reset = fig.add_axes([0.8, 0.005, 0.1, 0.04])
button = Button(ax_reset, 'Reset', hovercolor='0.975')
# Define reset function
def reset(event):
qs.reset()
ps.reset()
vqs.reset()
# Set reset button action
button.on_clicked(reset)
plt.show()