Newer
Older
# -*- coding: utf-8 -*-
""" TZPG simple calculator for SCS.
Interactive widget to calculate beam sizes and position at the sample and
detector planes for the SCS instrument.
Copyright (2019) SCS Team.
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Polygon
from matplotlib.colors import hsv_to_rgb
import ipywidgets as widgets
from ipywidgets import HBox, VBox, Layout
from IPython.display import display
# zone plate focal length
F = 250*1e-3 # [m]
# Z position of the zone plate optic from the first interaction point (sample Z stage at 0 mm)
Z0 = 230*1e-3 # [m]
# zone plate nominal focal sorthened by the KBS focusing
d = 3.3 - Z0 # distance between HFM and TZPG
f1 = 7.3 # HFM focus 2 m behind second interaction point
F = F*(d-f1)/(d-f1-F)
KBS_F = f1 - d # KBS focus distance from TZPG
# number of membrane to show
SampleN = 7
class TZPGcalc():
def __init__(self):
self.initFig()
self.initWidgets()
self.UpdateFig()
display(self.control)
def initFig(self):
""" Creates a figure for the sample plane and detector plane images with all necessary drawings.
"""
plt.close('TZPGcalc')
fig, (self.ax_sam, self.ax_det) = plt.subplots(1, 2, num='TZPGcalc', figsize=(6,3))
# display scale
self.scale = 1e3 # displayed distances in [mm]
self.ax_sam.set_title('Sample plane')
self.ax_det.set_title('Detector plane')
self.ax_sam.set_aspect('equal')
self.ax_det.set_aspect('equal')
self.ax_sam.set_xlim([-2, 2])
self.ax_sam.set_ylim([-2, 2])
self.ax_det.set_xlim([-35, 35])
self.ax_det.set_ylim([-20, 50])
# red and blue shifted color of the beams
c_rr = hsv_to_rgb([0/360, 50/100, 100/100])
c_rb = hsv_to_rgb([40/360, 50/100, 100/100])
c_gr = hsv_to_rgb([95/360, 60/100, 100/100])
c_gb = hsv_to_rgb([145/360, 60/100, 100/100])
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
self.samBeamsL = {
'F0G0': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G-1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F1G0': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor=c_rr, alpha=0.7, lw=None)),
'F1G1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor=c_gr, alpha=0.7, lw=None)),
'F1G-1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor=c_gr, alpha=0.7, lw=None))
}
self.detBeamsL = {
'F0G0': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G-1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F1G0': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor=c_rr, alpha=0.7, lw=None)),
'F1G1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor=c_gr, alpha=0.7, lw=None)),
'F1G-1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor=c_gr, alpha=0.7, lw=None))
}
self.samBeamsH = {
'F0G0': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G-1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F1G0': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor=c_rb, alpha=0.7, lw=None)),
'F1G1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor=c_gb, alpha=0.7, lw=None)),
'F1G-1': self.ax_sam.add_patch(
Polygon([(0, 0)], facecolor=c_gb, alpha=0.7, lw=None))
}
self.detBeamsH = {
'F0G0': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F0G-1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor="black", alpha=0.4, lw=None)),
'F1G0': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor=c_rb, alpha=0.7, lw=None)),
'F1G1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor=c_gb, alpha=0.7, lw=None)),
'F1G-1': self.ax_det.add_patch(
Polygon([(0, 0)], facecolor=c_gb, alpha=0.7, lw=None))
}
self.detLines = {
'module': self.ax_det.add_patch(
Rectangle((0, 0), 1, 1, fill=False, facecolor='k')),
'Vfilter': self.ax_det.add_patch(
Rectangle((0, 0), 1, 1, facecolor="blue", alpha=0.4)),
'Hfilter': self.ax_det.add_patch(
Rectangle((0, 0), 1, 1, facecolor="blue", alpha=0.4)),
'diamond': self.ax_det.add_patch(
Rectangle((-8, -8), 16, 16, facecolor="blue", alpha=0.4, angle=45))
}
# 5x5 membranes
self.sampleLines = {}
self.etchLines = {}
for k in range(SampleN*SampleN):
self.sampleLines[k] = self.ax_sam.add_patch(
Rectangle((0, 0), 1, 1, fill=False, facecolor='k'))
self.etchLines[k] = self.ax_sam.add_patch(
Rectangle((0, 0), 1, 1, fill=False, facecolor='k', alpha=0.4, ls='--'))
def RectUpdate(self, rect, xLeft, yBottom, xRight, yTop):
""" Updates the position and size of the given Rectangle.
rect: Rectangle to update
xLeft: x position of the left corner
yBottom: y position of the bottom corner
xRight: x position of the right corner
yTop: y position of the top corner
"""
xw = np.abs(xLeft - xRight)
yw = np.abs(yTop - yBottom)
rect.set_xy((self.scale*xLeft, self.scale*yBottom))
rect.set_height(self.scale*yw)
rect.set_width(self.scale*xw)
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
def PolyUpdate(self, poly, xLeft, yBottom, xRight, yTop):
""" Updates the corner position of a Polygon.
poly: regular Polygon to update
xLeft: x position of the left corner
yBottom: y position of the bottom corner
xRight: x position of the right corner
yTop: y position of the top corner
"""
xy = self.scale*np.array([
[xLeft, yBottom],
[xLeft, yTop],
[xRight, yTop],
[xRight, yBottom]])
poly.set_xy(xy)
def LinePlaneIntersection(self, l1, l2, p0, n):
""" Calculate the intersection point in space between a line (beam)
passing through 2 points l1 and l2 and a (sample) plane passing by
p0 with normal n
l1: [x,y,z] point on line
l2: [x,y,z] point on line
p0: [x,y,z] point on plane
n: plane normal vector
"""
# plane parametrized as (p - p0).n = 0
# line parametrized as p = l1 + l12*d with d Real
l12 = l2 - l1
if np.dot(l12,n) == 0:
return [0,0,0] # line is either in the plane or outside the plane
else:
d = np.dot((p0 - l1), n)/np.dot(l12,n)
return l1 + l12*d
def UpdateBeams(self, Beams, Z, incidence, conf):
Beams: dictionary of f'F{f}G{g}' Polygon for f = 0 and 1 zone
plate order and g = +1, 0 and -1 grating order
Z: distance Z between the zone plate and the current imaging plane
incidence: incidence angle of the imaging plane in rad
conf: dictionnary of distance for calculation {'F', 'TZPGwH',
'TZPGwV', 'TZPGo', 'theta_grating'}
"""
F = conf['F']
wH = conf['TZPGwH']
wV = conf['TZPGwV']
o = conf['TZPGo']
offaxis = wV/2 + o
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
# imaging plane
n = np.array([np.sin(incidence), 0, np.cos(incidence)])
p0 = np.array([0,0,Z])
# zone plate 4 corner points
l1_list = [
np.array([wH/2,wV/2,0]),
np.array([wH/2,-wV/2,0]),
np.array([-wH/2,-wV/2,0]),
np.array([-wH/2,wV/2,0])
]
# 6 beam focus point
l2_list = {
'F0G0': np.array([0,0,KBS_F]),
'F0G1': np.array([KBS_F*np.arctan(conf['theta_grating']),0,KBS_F]),
'F0G-1': np.array([-KBS_F*np.arctan(conf['theta_grating']),0,KBS_F]),
'F1G0': np.array([0,offaxis,F]),
'F1G1': np.array([F*np.arctan(conf['theta_grating']),offaxis,F]),
'F1G-1': np.array([-F*np.arctan(conf['theta_grating']),offaxis,F])
}
for beam in l2_list.keys():
l2 = l2_list[beam]
corners = []
for l1 in l1_list:
corners.append(self.LinePlaneIntersection(l1, l2, p0, n)[:2])
Beams[beam].set_xy(self.scale*np.array(corners))
def DetectorUpdate(self, Xoff, Yoff):
""" Draw DSSC detector module with filter mask.
Xoff: x offset
Yoff: y offset
"""
# x module axis is vertical, y module axis is horizontal
# the module 15 is +0.91 mm vertical from the beam and 4.233 mm horizontal from the beam
offset_h = 4.233e-3 #[m]
offset_v = 0.91e-3 #[m]
moduleHw = 256*0.236e-3 #[m]
moduleVw = 128*0.204e-3 #[m]
filterW = 7e-3 #[m]
filterL = 160e-3 #[m]
diamondW = 16e-3 #[m]
self.RectUpdate(self.detLines['module'],
-moduleHw - offset_h + Xoff, offset_v + Yoff, -offset_h + Xoff, moduleVw + offset_v + Yoff)
self.RectUpdate(self.detLines['Vfilter'],
-filterW/2 + Xoff, -filterL/2 + Yoff, filterW/2 + Xoff, filterL/2 + Yoff)
self.RectUpdate(self.detLines['Hfilter'],
-filterL/2 + Xoff, -filterW/2 + Yoff, filterL/2 + Xoff, filterW/2 + Yoff)
# moving rotated rectangles is a pain in matplotlib
self.detLines['diamond'].set_xy((self.scale*Xoff, self.scale*(Yoff - diamondW/2*np.sqrt(2))))
def SampleUpdate(self, w, p, Xoff, Yoff, thickness=0.525,
incidence=0, etch_angle=54.74):
w: membrane width
p: membrane pitch
Xoff: sample x offset
Yoff: sample y offset
thickness: sample thickness used to calculate the etched facets
incidence: incidence angle in rad
etch_angle: etching angle from surface in rad
wp = w +2*thickness/np.tan(etch_angle)
# incidence angle squeezes sample and etch lines
# and induces an apparent shift off the etch lines
ci = np.cos(incidence)
thsi = thickness*np.sin(incidence)
for k in range(-(SampleN-1)//2, (SampleN-1)//2+1):
for l in range(-(SampleN-1)//2, (SampleN-1)//2+1):
self.RectUpdate(self.sampleLines[j],
ci*(k*p - w/2 + Xoff), l*p - w/2 - Yoff,
ci*(k*p + w/2 + Xoff), l*p + w/2 - Yoff)
self.RectUpdate(self.etchLines[j],
ci*(k*p - wp/2 + Xoff)+thsi, l*p - wp/2 - Yoff,
ci*(k*p + wp/2 + Xoff)+thsi, l*p + wp/2 - Yoff)
j+=1
def UpdateFig(self):
""" Update the figure with the current slider values.
# we calculate the optics for the central wavelength
nrjL, nrjH = self.nrj_slider.value # [eV]
wlL = 1240/nrjL*1e-9
wlH = 1240/nrjH*1e-9
nrjD = self.design_nrj_slider.value # [eV]
wl = 1240/nrjD*1e-9
theta_grating = self.grating_slider.value*1e-3 # [rad]
sampleZ = self.samz_slider.value*1e-3 # [m]
samIncidence = np.deg2rad(self.samIncidence_slider.value) # [rad]
detectorZ = self.det_slider.value*1e-3 # [m]
TZPGwH = self.TZPGwH_slider.value*1e-3 #[m]
TZPGwV = self.TZPGwV_slider.value*1e-3 #[m]
TZPGo = self.TZPGoffaxis_slider.value*1e-3 - TZPGwV/2 #[m]
d_nominal = wl/np.sin(theta_grating)
self.d_label.value = f'Grating Pitch:{int(np.round(d_nominal*1e9))} nm'
rn = TZPGwV + TZPGo
dr_nominal = wl * F / (2*rn)
self.dr_label.value = f'Outer Zone Plate width dr:{int(np.round(dr_nominal*1e9))} nm'
# configuration for the low energy and high energy photon
confL = {'F':(2*rn)*dr_nominal/wlL,
'theta_grating':np.arcsin(wlL/d_nominal),
'TZPGwH':TZPGwH, 'TZPGwV':TZPGwV, 'TZPGo':TZPGo}
confH = {'F':(2*rn)*dr_nominal/wlH,
'theta_grating':np.arcsin(wlH/d_nominal),
'TZPGwH':TZPGwH, 'TZPGwV':TZPGwV, 'TZPGo':TZPGo}
# update the beams
self.UpdateBeams(self.samBeamsL, Z0 + sampleZ, samIncidence, confL)
self.UpdateBeams(self.detBeamsL, Z0 + detectorZ, 0, confL)
self.UpdateBeams(self.samBeamsH, Z0 + sampleZ, samIncidence, confH)
self.UpdateBeams(self.detBeamsH, Z0 + detectorZ, 0, confH)
# update the detector
detXoff = self.detX_slider.value*1e-3 #[m]
detYoff = self.detY_slider.value*1e-3 #[m]
self.DetectorUpdate(detXoff, detYoff)
# update the sample
samw = self.samw_slider.value*1e-3 #[m]
samp = self.samp_slider.value*1e-3 #[m]
samXoff = self.samX_slider.value*1e-3 #[m]
samYoff = self.samY_slider.value*1e-3 #[m]
samthickness = self.samthickness_slider.value*1e-6 #[m]
samEtchAngle = np.deg2rad(self.samEtchAngle_slider.value) #[rad]
self.SampleUpdate(samw, samp, samXoff, samYoff, samthickness,
samIncidence, samEtchAngle)
def initWidgets(self):
""" Creates the necessary interactive widget controls.
"""
self.button = widgets.Button(
description='Update',
)
@self.button.on_click
def plot_on_click(b):
self.UpdateFig()
# TZPG part
self.nrj_slider = widgets.FloatRangeSlider(
value=[840., 880.],
min=450.,
max=3200.0,
step=1,
readout_format='.2f',
)
self.design_nrj_slider = widgets.FloatSlider(
value=860.,
min=450.,
max=3200.0,
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
step=1,
readout_format='.2f',
)
self.TZPGwH_slider = widgets.FloatSlider(
value=1.0,
min=.1,
max=3.0,
step=0.05,
readout_format='.2f',
)
self.TZPGwV_slider = widgets.FloatSlider(
value=1.0,
min=.1,
max=3.0,
step=0.05,
readout_format='.2f',
)
self.TZPGoffaxis_slider = widgets.FloatSlider(
value=0.75,
min=.0,
max=2.0,
step=0.05,
readout_format='.2f',
)
self.grating_slider = widgets.FloatSlider(
value=3.8,
min=1.,
max=10.0,
step=0.05,
readout_format='.2f',
)
self.dr_label = widgets.Label(value='dr')
self.d_label = widgets.Label(value='dr')
TZPGTab = VBox(children=[HBox([widgets.Label(value='Energy (eV):'), self.nrj_slider]),
HBox([widgets.Label(value='Design Energy (eV):'), self.design_nrj_slider]),
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
HBox([widgets.Label(value=r'Grating $\theta$ (mrad):'), self.grating_slider]),
self.d_label, self.dr_label,
HBox([widgets.Label(value='TZPG horiz. width (mm):'), self.TZPGwH_slider]),
HBox(children=[HBox([widgets.Label(value='TZPG vert. width (mm):'), self.TZPGwV_slider]),
HBox([widgets.Label(value='TZPG off axis (mm):'), self.TZPGoffaxis_slider])
])])
# sample part
self.samz_slider = widgets.FloatSlider(
value=30.,
min=-10.,
max=180.0,
step=1,
readout_format='.2f',
)
self.samw_slider = widgets.FloatSlider(
value=.5,
min=0.01,
max=2.0,
step=.01,
readout_format='.2f',
)
self.samp_slider = widgets.FloatSlider(
value=1.0,
min=0.01,
max=2.0,
step=.01,
readout_format='.2f',
)
self.samX_slider = widgets.FloatSlider(
value=0.,
min=-10,
max=10,
step=0.01,
readout_format='.2f',
)
self.samY_slider = widgets.FloatSlider(
value=0.,
min=-10,
max=10,
step=0.01,
readout_format='.2f',
)
self.samthickness_slider = widgets.FloatSlider(
value=381,
min=1,
max=1000,
step=1,
readout_format='.0f',
)
self.samIncidence_slider = widgets.FloatSlider(
value=0,
min=0,
max=90,
step=1,
readout_format='.0f',
)
self.samEtchAngle_slider = widgets.FloatSlider(
value=54.74,
min=0,
max=90,
step=0.01,
readout_format='.2f',
)
samTab = VBox(children=[HBox([widgets.Label(value='Sample Z (mm):'), self.samz_slider]),
HBox(children=[HBox([widgets.Label(value='Membrane width (mm):'), self.samw_slider]),
HBox([widgets.Label(value='Membrane pitch (mm):'), self.samp_slider])]),
HBox(children=[HBox([widgets.Label(value='Sample X-Offset (mm):'), self.samX_slider]),
HBox([widgets.Label(value='Sample Y-Offset (mm):'), self.samY_slider])]),
HBox([widgets.Label(value='Substrate thickness (um):'), self.samthickness_slider]),
HBox([HBox([widgets.Label(value='Normal incidence (deg):'), self.samIncidence_slider]),
HBox([widgets.Label(value='Etch angle from surface (deg):'), self.samEtchAngle_slider])])
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
])
#detector tab
self.det_slider = widgets.FloatSlider(
value=2000.,
min=1000,
max=5800,
step=1,
description='',
readout_format='.2f',
)
self.detX_slider = widgets.FloatSlider(
value=20.,
min=-50,
max=50,
step=0.5,
readout_format='.2f',
)
self.detY_slider = widgets.FloatSlider(
value=0.,
min=-50,
max=50,
step=0.5,
readout_format='.2f',
)
detTab = VBox(children=[HBox([widgets.Label(value='Detector Z (m):'), self.det_slider]),
HBox(children=[HBox([widgets.Label(value='Detector X-Offset (mm):'), self.detX_slider]),
HBox([widgets.Label(value='Detector Y-Offset (mm):'), self.detY_slider])])])
tab1 = widgets.Accordion(children=[TZPGTab])
tab1.set_title(0, 'TZPG')
tab1.selected_index = None
tab2 = widgets.Accordion(children=[samTab])
tab2.set_title(0, 'sample')
tab2.selected_index = None
tab3 = widgets.Accordion(children=[detTab])
tab3.set_title(0, 'detector')
tab3.selected_index = None
self.control = VBox(children=[tab1, tab2, tab3, self.button])