File: Cylinder.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 (169 lines) | stat: -rw-r--r-- 5,841 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
__author__ = "Konstantin Klementiev", "Roman Chernikov"
__date__ = "08 Mar 2016"
import os, sys; sys.path.append(os.path.join('..', '..', '..'))  # analysis:ignore
import numpy as np
import matplotlib as mpl

import xrt.backends.raycing as raycing
import xrt.backends.raycing.sources as rs
#import xrt.backends.raycing.apertures as ra
import xrt.backends.raycing.oes as roe
import xrt.backends.raycing.run as rr
#import xrt.backends.raycing.materials as rm
import xrt.plotter as xrtp
import xrt.runner as xrtr
import xrt.backends.raycing.screens as rsc

mGold = None  # rm.Material('Au', rho=19.3)


class Cylinder(roe.OE):
    def __init__(self, *args, **kwargs):
        self.Rm = kwargs.pop('Rm', 10000.)  # R meridional
        roe.OE.__init__(self, *args, **kwargs)

    def local_z(self, x, y):
        return self.Rm - np.sqrt(self.Rm**2 - y**2)

    def local_n(self, x, y):
        a = np.zeros_like(x)  # -dz/dx
        b = -y * (self.Rm**2 - y**2)**(-0.5)  # -dz/dy
        c = 1.
        norm = (b**2 + 1)**0.5
        b /= norm
        c /= norm
        return [a, b, c]


class CylinderP(Cylinder):
    def local_r(self, s, phi):
        return self.Rm

    def local_n(self, s, phi):
        a = np.zeros_like(phi)  # -dz/dx
        b = -np.sin(phi)  # -dz/dy
        c = np.cos(phi)
        return [a, b, c]

    def xyz_to_param(self, x, y, z):  # for flat mirror as example
        return x, np.arctan(y / (Rm - z)), np.sqrt(y**2 + (Rm - z)**2)

    def param_to_xyz(self, s, phi, r):  # for flat mirror as example
        return s, r * np.sin(phi), Rm - r * np.cos(phi)  # x, y, z

E0 = 2000.
L = 190.
Rm = 5000.
isFlat = False
isParametric = True


def build_beamline(nrays=raycing.nrays):
    beamLine = raycing.BeamLine(height=0)
    rs.GeometricSource(
        beamLine, 'GeometricSource', (0, 0, 0),
        nrays=nrays, dx=0., dz=0., dxprime=5e-4, dzprime=1e-5,
        distE='lines', energies=(E0,), polarization='horizontal')
    beamLine.fsm1 = rsc.Screen(beamLine, 'DiamondFSM1', (0, 100, 0))

    if isFlat:
        fName = 'Flat'
        beamLine.cylinder = roe.OE(
            beamLine, 'FlatP', [0, 1000, -0.01],
            pitch=3e-3, material=mGold, isParametric=isParametric)
    else:
        fName = 'Cylinder'
        limPhysX = [-5, 5]
        limPhysY = [0, L]
        if isParametric:
            CylinderClass = CylinderP
        else:
            CylinderClass = Cylinder
        beamLine.cylinder = CylinderClass(
            beamLine, fName, [0, 1000, -0.05],
            pitch=3e-3, material=mGold, limPhysX=limPhysX, limPhysY=limPhysY,
            Rm=Rm, isParametric=isParametric)

    beamLine.fsm2 = rsc.Screen(beamLine, 'DiamondFSM2', (0, 2000, 0))
    return beamLine, fName


def run_process(beamLine, shineOnly1stSource=False):
    beamSource = beamLine.sources[0].shine()
    beamFSM1 = beamLine.fsm1.expose(beamSource)
    beamCylinderGlobal, beamCylinderLocalN = \
        beamLine.cylinder.multiple_reflect(beamSource, maxReflections=100)
#      beamLine.cylinder.reflect(beamSource)
    beamFSM2 = beamLine.fsm2.expose(beamCylinderGlobal)
    outDict = {'beamSource': beamSource, 'beamFSM1': beamFSM1,
               'beamCylinderGlobal': beamCylinderGlobal,
               'beamCylinderLocalN': beamCylinderLocalN,
               'beamFSM2': beamFSM2}
    return outDict
rr.run_process = run_process


def define_plots(beamLine, fName):
#    fwhmFormatStrE = '%.2f'
    plots = []
    pAdd = 'P' if isParametric else ''

    plot = xrtp.XYCPlot(
        'beamFSM1', (1,), xaxis=xrtp.XYCAxis(r'$x$', '$\mu$m'),
        yaxis=xrtp.XYCAxis(r'$z$', '$\mu$m'), title='FSM1_E')
    plot.caxis.fwhmFormatStr = None
#    plot.caxis.limits = [70, 140]
    plots.append(plot)

    plot = xrtp.XYCPlotWithNumerOfReflections(
        'beamCylinderLocalN', (1,),
        xaxis=xrtp.XYCAxis(r'$x$', 'mm'),
        yaxis=xrtp.XYCAxis(r'$y$', 'mm'), aspect='auto',
        caxis=xrtp.XYCAxis('number of reflections', '', bins=32, ppb=8,
                           data=raycing.get_reflection_number), title='local')
    plot.caxis.fwhmFormatStr = None
    plot.xaxis.limits = [-2, 2]
    plot.saveName = ['{0}Local{1}.png'.format(fName, pAdd), ]
    plots.append(plot)

    if isParametric:
        plot = xrtp.XYCPlotWithNumerOfReflections(
            'beamCylinderLocalN', (1,),
            xaxis=xrtp.XYCAxis(r'$s$', 'mm'), aspect='auto',
            yaxis=xrtp.XYCAxis(r'$\phi$', 'mrad'),
            caxis=xrtp.XYCAxis('number of reflections', '', bins=32, ppb=8,
                               data=raycing.get_reflection_number),
            title='local (s, phi)')
#        plot.yaxis.fwhmFormatStr = '%.2f' + r'$ \pi$'
        plot.caxis.fwhmFormatStr = None
        formatter = mpl.ticker.FormatStrFormatter('%g' + r'$ \pi$')
        plot.ax2dHist.xaxis.set_major_formatter(formatter)
        plot.saveName = ['{0}LocalPP.png'.format(fName), ]
        plots.append(plot)

    plot = xrtp.XYCPlotWithNumerOfReflections(
        'beamFSM2', (1, 2),
        xaxis=xrtp.XYCAxis(r'$x$', 'mm'),
        yaxis=xrtp.XYCAxis(r'$z$', 'mm'),
        caxis=xrtp.XYCAxis('number of reflections', '',  bins=32, ppb=8,
                           data=raycing.get_reflection_number),
        title='FSM2_Es')
    plot.xaxis.limits = [-4, 4]
    plot.caxis.fwhmFormatStr = None
    plot.fluxFormatStr = '%.2e'
    plot.saveName = ['{0}Out{1}.png'.format(fName, pAdd), ]
    plots.append(plot)
    return plots


def main():
    beamLine, fName = build_beamline()
    plots = define_plots(beamLine, fName)
    xrtr.run_ray_tracing(plots, repeats=40, updateEvery=1, beamLine=beamLine,
                         processes='half')

#this is necessary to use multiprocessing in Windows, otherwise the new Python
#contexts cannot be initialized:
if __name__ == '__main__':
    main()