File: test_laue_bent.py

package info (click to toggle)
python-xrt 1.6.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,572 kB
  • sloc: python: 59,424; xml: 4,786; lisp: 4,082; sh: 22; javascript: 18; makefile: 17
file content (285 lines) | stat: -rw-r--r-- 8,476 bytes parent folder | download
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
# -*- coding: utf-8 -*-
"""
__author__ = "Konstantin Klementiev", "Roman Chernikov"
__date__ = "2017-02-01"

Created with xrtQook
"""

import numpy as np
import sys
import os, sys; sys.path.append(os.path.join('..', '..'))  # analysis:ignore
import xrt.backends.raycing.sources as rsources
import xrt.backends.raycing.screens as rscreens
import xrt.backends.raycing.materials as rmats
import xrt.backends.raycing.oes as roes
# import xrt.backends.raycing.apertures as rapts
import xrt.backends.raycing.run as rrun
import xrt.backends.raycing as raycing
import xrt.plotter as xrtplot
import xrt.runner as xrtrun

Emin, Emax = 8998, 9002

crystalSi01 = rmats.CrystalSi(
    t=0.1,
    hkl=[1, 1, 1],
    useTT=True,
    # calcBorrmann='TT',
    geom=r"Laue reflected")


def build_beamline():
    beamLine = raycing.BeamLine()

    beamLine.geometricSource01 = rsources.GeometricSource(
        bl=beamLine,
        name=None,
        center=[0, 0, 0],
        nrays=1000,
        distE=r"flat",
        dx=0.001, dz=0.001,
        dxprime=0, dzprime=0,
        energies=[Emin, Emax])

    beamLine.screen02 = rscreens.Screen(
        bl=beamLine,
        center=[0, 10000, 0])

    beamLine.lauePlate01 = roes.BentLaueCylinder(
#    beamLine.lauePlate01 = roes.LauePlate(
        bl=beamLine,
        name=None,
        center=[0, 10000, 0],
        pitch=0.0,
        R=1e4,
        crossSection='parabolic',
        material=crystalSi01,
        targetOpenCL='auto')

    beamLine.screen01 = rscreens.Screen(
        bl=beamLine,
        name=None,
        center=[0.0, 20000, 0.0])

    return beamLine


def run_process(beamLine):
    geometricSource01beamGlobal01 = beamLine.geometricSource01.shine()

    screen02beamLocal01 = beamLine.screen02.expose(
        beam=geometricSource01beamGlobal01)

    lauePlate01beamGlobal01, lauePlate01beamLocal01 = beamLine.lauePlate01.reflect(
        beam=geometricSource01beamGlobal01)

    screen01beamLocal01 = beamLine.screen01.expose(
        beam=lauePlate01beamGlobal01)

    outDict = {
        'geometricSource01beamGlobal01': geometricSource01beamGlobal01,
        'lauePlate01beamGlobal01': lauePlate01beamGlobal01,
        'lauePlate01beamLocal01': lauePlate01beamLocal01,
        'screen01beamLocal01': screen01beamLocal01,
        'screen02beamLocal01': screen02beamLocal01}
    return outDict


rrun.run_process = run_process


def align_beamline(beamLine, energy):
    geometricSource01beamGlobal01 = rsources.Beam(nrays=2)
    geometricSource01beamGlobal01.a[:] = 0
    geometricSource01beamGlobal01.b[:] = 1
    geometricSource01beamGlobal01.c[:] = 0
    geometricSource01beamGlobal01.x[:] = 0
    geometricSource01beamGlobal01.y[:] = 0
    geometricSource01beamGlobal01.z[:] = 0
    geometricSource01beamGlobal01.E[:] = energy
    geometricSource01beamGlobal01.state[:] = 1

    tmpy = beamLine.screen02.center[1]
    newx = beamLine.screen02.center[0]
    newz = beamLine.screen02.center[2]
    beamLine.screen02.center = (newx, tmpy, newz)
    print("screen02.center:", beamLine.screen02.center)

    screen02beamLocal01 = beamLine.screen02.expose(
        beam=geometricSource01beamGlobal01)
    tmpy = beamLine.lauePlate01.center[1]
    newx = beamLine.lauePlate01.center[0]
    newz = beamLine.lauePlate01.center[2]
    beamLine.lauePlate01.center = (newx, tmpy, newz)
    print("lauePlate01.center:", beamLine.lauePlate01.center)

    braggT = crystalSi01.get_Bragg_angle(energy)
    alphaT = 0 if beamLine.lauePlate01.alpha is None else beamLine.lauePlate01.alpha
    lauePitch = 0
    print("bragg, alpha:", np.degrees(braggT), np.degrees(alphaT), "degrees")

    braggT += -crystalSi01.get_dtheta(energy, alphaT)
    if crystalSi01.geom.startswith('Laue'):
        lauePitch = 0.5 * np.pi
    print("braggT:", np.degrees(braggT), "degrees")

    loBeam = rsources.Beam(copyFrom=geometricSource01beamGlobal01)
    raycing.global_to_virgin_local(
        beamLine,
        geometricSource01beamGlobal01,
        loBeam,
        center=beamLine.lauePlate01.center)
    raycing.rotate_beam(
        loBeam,
        roll=-(beamLine.lauePlate01.positionRoll + beamLine.lauePlate01.roll),
        yaw=-beamLine.lauePlate01.yaw,
        pitch=0)
    theta0 = np.arctan2(-loBeam.c[0], loBeam.b[0])
    th2pitch = np.sqrt(1. - loBeam.a[0]**2)
    targetPitch = np.arcsin(np.sin(braggT) / th2pitch) -\
        theta0
    targetPitch += alphaT + lauePitch
    beamLine.lauePlate01.pitch = targetPitch
    print("lauePlate01.pitch:", np.degrees(beamLine.lauePlate01.pitch), "degrees")

    lauePlate01beamGlobal01, lauePlate01beamLocal01 = beamLine.lauePlate01.reflect(
        beam=geometricSource01beamGlobal01)
    print("Laue Plate exit point")
    print(lauePlate01beamGlobal01.x, lauePlate01beamGlobal01.y,
          lauePlate01beamGlobal01.z)
    tmpy = beamLine.screen01.center[1]
    newx = lauePlate01beamGlobal01.x[0] +\
        lauePlate01beamGlobal01.a[0] * (tmpy - lauePlate01beamGlobal01.y[0]) /\
        lauePlate01beamGlobal01.b[0]
    newz = lauePlate01beamGlobal01.z[0] +\
        lauePlate01beamGlobal01.c[0] * (tmpy - lauePlate01beamGlobal01.y[0]) /\
        lauePlate01beamGlobal01.b[0]
    beamLine.screen01.center = (newx, tmpy, newz)
    print("screen01.center:", beamLine.screen01.center)

    screen01beamLocal01 = beamLine.screen01.expose(
        beam=lauePlate01beamGlobal01)


def define_plots():
    plots = []

    plot01 = xrtplot.XYCPlot(
        beam=r"lauePlate01beamLocal01",
        xaxis=xrtplot.XYCAxis(
            label=r"x",
            unit='$\mu$m',
            fwhmFormatStr="%.1f",
            bins=256,
            ppb=1,
#            factor=1000,
            limits=[-5, 5]),
        yaxis=xrtplot.XYCAxis(
            label=r"y",
            unit='$\mu$m',
            fwhmFormatStr="%.1f",
#            factor=1000,
            bins=256,
            ppb=1,
            limits=[-50, 50]),
        caxis=xrtplot.XYCAxis(
            label=r"energy",
            unit=r"eV",
            bins=256,
            ppb=1,
            limits=[Emin, Emax]),
        aspect=r"auto",
        title=r"01 - Laue crystal Fooprint")
    plots.append(plot01)

    plot01a = xrtplot.XYCPlot(
        beam=r"lauePlate01beamLocal01",
        xaxis=xrtplot.XYCAxis(
            label=r"y",
            unit='$\mu$m',
            fwhmFormatStr="%.1f",
#            factor=1000,
            bins=256,
            ppb=1,
            limits=[-50, 50]),
        yaxis=xrtplot.XYCAxis(
            label=r"z",
            unit='$\mu$m',
            fwhmFormatStr="%.1f",
            bins=256,
            ppb=1),
        caxis=xrtplot.XYCAxis(
            label=r"energy",
            unit=r"eV",
            bins=256,
            ppb=1,
            limits=[Emin, Emax]),
        aspect=r"auto",
        title=r"01a - Laue crystal depth profile")
    plots.append(plot01a)

    plot02 = xrtplot.XYCPlot(
        beam=r"screen01beamLocal01",
        xaxis=xrtplot.XYCAxis(
            label=r"x",
            fwhmFormatStr="%.1f",
            bins=256,
            ppb=1),
        yaxis=xrtplot.XYCAxis(
            label=r"z",
            fwhmFormatStr="%.3f",
            bins=256,
            ppb=1),
        caxis=xrtplot.XYCAxis(
            label=r"energy",
            unit=r"eV",
            limits=[Emin, Emax],
            bins=256,
            ppb=1),
        aspect=r"auto",
        title=r"02 - screen")
    plots.append(plot02)

    plot03 = xrtplot.XYCPlot(
        beam=r"screen02beamLocal01",
        xaxis=xrtplot.XYCAxis(
            label=r"x",
            bins=256,
            ppb=1,
            unit='$\mu$m',
            fwhmFormatStr="%.1f"),
        yaxis=xrtplot.XYCAxis(
            label=r"z",
            bins=256,
            ppb=1,
            unit='$\mu$m',
            fwhmFormatStr="%.1f"),
        caxis=xrtplot.XYCAxis(
            label=r"energy",
            bins=256,
            ppb=1,
            unit=r"eV",
            limits=[Emin, Emax]),
        aspect=r"auto",
        title=r"00 - Laue crystyal - incoming beam")
    plots.append(plot03)
    for plot in plots:
        plot.saveName = plot.title + "R{0}m_t{1}mm.png".format(10, 0.1)
    return plots


def main():
    beamLine = build_beamline()
    E0 = (Emin+Emax)*0.5
    align_beamline(beamLine, E0)
    plots = define_plots()
    xrtrun.run_ray_tracing(
        plots=plots,
        repeats=10,
        backend=r"raycing",
        beamLine=beamLine)


if __name__ == '__main__':
    main()