ImpactX
Loading...
Searching...
No Matches
Sbend.H
Go to the documentation of this file.
1/* Copyright 2022-2023 The Regents of the University of California, through Lawrence
2 * Berkeley National Laboratory (subject to receipt of any required
3 * approvals from the U.S. Dept. of Energy). All rights reserved.
4 *
5 * This file is part of ImpactX.
6 *
7 * Authors: Chad Mitchell, Axel Huebl
8 * License: BSD-3-Clause-LBNL
9 */
10#ifndef IMPACTX_SBEND_H
11#define IMPACTX_SBEND_H
12
14#include "mixin/alignment.H"
15#include "mixin/pipeaperture.H"
16#include "mixin/beamoptic.H"
17#include "mixin/thick.H"
18#include "mixin/named.H"
19#include "mixin/nofinalize.H"
21
22#include <AMReX_Extension.H>
23#include <AMReX_Math.H>
24#include <AMReX_REAL.H>
25#include <AMReX_SIMD.H>
26
27#include <cmath>
28
29
30namespace impactx::elements
31{
32 struct Sbend
33 : public mixin::Named,
34 public mixin::BeamOptic<Sbend>,
35 public mixin::LinearTransport<Sbend>,
36 public mixin::Thick,
37 public mixin::Alignment,
40 // At least on Intel AVX512, there is a small overhead to vectorize this element in DP, and no benefit in SP, see
41 // https://github.com/BLAST-ImpactX/impactx/pull/1002
42 // public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
43 {
44 static constexpr auto type = "Sbend";
46
60 amrex::ParticleReal ds,
61 amrex::ParticleReal rc,
62 amrex::ParticleReal dx = 0,
63 amrex::ParticleReal dy = 0,
64 amrex::ParticleReal rotation_degree = 0,
65 amrex::ParticleReal aperture_x = 0,
66 amrex::ParticleReal aperture_y = 0,
67 int nslice = 1,
68 std::optional<std::string> name = std::nullopt
69 )
70 : Named(std::move(name)),
71 Thick(ds, nslice),
72 Alignment(dx, dy, rotation_degree),
74 m_rc(rc)
75 {
76 }
77
79 amrex::ParticleReal
80 rc ([[maybe_unused]] RefPart const & refpart) const
81 {
82 using namespace amrex::literals; // for _rt and _prt
83
84 // TODO: as in ExactSbend
85 // return m_B != 0_prt ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
86 return m_rc;
87 }
88
90 using BeamOptic::operator();
91
99 void compute_constants (RefPart const & refpart)
100 {
101 using namespace amrex::literals; // for _rt and _prt
102 using amrex::Math::powi;
103
104 Alignment::compute_constants(refpart);
105
106 // length of the current slice
107 amrex::ParticleReal const slice_ds = m_ds / nslice();
108
109 // access reference particle values
110 amrex::ParticleReal const ibet = 1.0_prt / refpart.beta();
111 amrex::ParticleReal const ibetagam2 = 1_prt / powi<2>(refpart.beta_gamma());
112
113 // treat the special case of zero field (equivalent to a drift)
114 if (m_rc==0_prt) {
115 m_R11 = 1.0_prt;
116 m_R12 = slice_ds;
117 m_R16 = 0.0_prt;
118 m_R21 = 0.0_prt;
119 m_R26 = 0.0_prt;
120 m_R34 = slice_ds;
121 m_R56 = slice_ds * ibetagam2;
122
123 } else {
124
125 // calculate expensive terms once
126 amrex::ParticleReal const theta = slice_ds / m_rc;
127 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
128
129 m_R11 = cos_theta;
130 m_R12 = m_rc * sin_theta;
131 m_R16 = -m_rc * ibet * (1.0_prt - cos_theta);
132 m_R21 = -sin_theta / m_rc;
133 // m_R22 = m_R11
134 m_R26 = -sin_theta * ibet;
135 m_R34 = m_rc * theta;
136 // m_R51 = -m_R26
137 // m_R52 = -m_R16
138 m_R56 = m_rc * (-theta + sin_theta * ibet * ibet);
139 }
140 }
141
155 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
158 T_Real & AMREX_RESTRICT x,
159 T_Real & AMREX_RESTRICT y,
160 T_Real & AMREX_RESTRICT t,
161 T_Real & AMREX_RESTRICT px,
162 T_Real & AMREX_RESTRICT py,
163 T_Real & AMREX_RESTRICT pt,
164 T_IdCpu & AMREX_RESTRICT idcpu,
165 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
166 ) const
167 {
168 using namespace amrex::literals; // for _rt and _prt
169
170 // shift due to alignment errors of the element
171 shift_in(x, y, px, py);
172
173 // initialize output values
174 T_Real xout = x;
175 T_Real yout = y;
176 T_Real tout = t;
177
178 // initialize output values of momenta
179 T_Real pxout = px;
180 T_Real const pyout = py;
181 T_Real const ptout = pt;
182
183 // advance position and momentum (sector bend)
184 T_Real const R22 = m_R11;
185 T_Real const R51 = -m_R26;
186 T_Real const R52 = -m_R16;
187
188 xout = m_R11 * x
189 + m_R12 * px
190 + m_R16 * pt;
191
192 pxout = m_R21 * x
193 + R22 * px
194 + m_R26 * pt;
195
196 yout = y
197 + m_R34 * py;
198
199 // pyout = py;
200
201 tout = R51 * x
202 + R52 * px
203 + t
204 + m_R56 * pt;
205
206 // ptout = pt;
207
208 // assign updated values
209 x = xout;
210 y = yout;
211 t = tout;
212 px = pxout;
213 py = pyout;
214 pt = ptout;
215
216 // apply transverse aperture
217 apply_aperture(x, y, idcpu);
218
219 // undo shift due to alignment errors of the element
220 shift_out(x, y, px, py);
221 }
222
228 void operator() (RefPart & AMREX_RESTRICT refpart) const
229 {
230 using namespace amrex::literals; // for _rt and _prt
231 using amrex::Math::powi;
232
233 // assign input reference particle values
234 amrex::ParticleReal const x = refpart.x;
235 amrex::ParticleReal const px = refpart.px;
236 amrex::ParticleReal const y = refpart.y;
237 amrex::ParticleReal const py = refpart.py;
238 amrex::ParticleReal const z = refpart.z;
239 amrex::ParticleReal const pz = refpart.pz;
240 amrex::ParticleReal const t = refpart.t;
241 amrex::ParticleReal const pt = refpart.pt;
242 amrex::ParticleReal const s = refpart.s;
243
244 // length of the current slice
245 amrex::ParticleReal const slice_ds = m_ds / nslice();
246
247 // treat the special case of zero field (drift)
248 if (m_rc == 0.0_prt) {
249 // advance position and momentum (drift)
250 amrex::ParticleReal const step = slice_ds /std::sqrt(powi<2>(pt)-1.0_prt);
251 refpart.x = x + step*px;
252 refpart.y = y + step*py;
253 refpart.z = z + step*pz;
254 refpart.t = t - step*pt;
255
256 } else {
257
258 // assign intermediate parameter
259 amrex::ParticleReal const theta = slice_ds/m_rc;
260 amrex::ParticleReal const B = std::sqrt(powi<2>(pt)-1.0_prt)/m_rc;
261
262 // calculate expensive terms once
263 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
264
265 // advance position and momentum (bend)
266 refpart.px = px*cos_theta - pz*sin_theta;
267 refpart.py = py;
268 refpart.pz = pz*cos_theta + px*sin_theta;
269 refpart.pt = pt;
270
271 refpart.x = x + (refpart.pz - pz)/B;
272 refpart.y = y + (theta/B)*py;
273 refpart.z = z - (refpart.px - px)/B;
274 refpart.t = t - (theta/B)*pt;
275
276 }
277
278 // advance integrated path length
279 refpart.s = s + slice_ds;
280
281 }
282
284 using LinearTransport::operator();
285
291 Map6x6
292 transport_map (RefPart const & AMREX_RESTRICT refpart) const
293 {
294 using namespace amrex::literals; // for _rt and _prt
295 using amrex::Math::powi;
296
297 // length of the current slice
298 amrex::ParticleReal const slice_ds = m_ds / nslice();
299
300 // access reference particle values to find beta*gamma^2
301 amrex::ParticleReal const pt_ref = refpart.pt;
302 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1.0_prt;
303 amrex::ParticleReal const bet = std::sqrt(betgam2/(1.0_prt + betgam2));
304
305 // initialize linear map matrix elements
307
308 // treat the special case of zero field
309 if (m_rc==0.0_prt) {
310 R(1,2) = slice_ds;
311 R(3,4) = slice_ds;
312 R(5,6) = slice_ds / betgam2;
313
314 } else {
315
316 // calculate expensive terms once
317 amrex::ParticleReal const theta = slice_ds/m_rc;
318 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
319
320 // assign linear map matrix elements
321 R(1,1) = cos_theta;
322 R(1,2) = m_rc*sin_theta;
323 R(1,6) = - (m_rc/bet)*(1.0_prt - cos_theta);
324 R(2,1) = -sin_theta/m_rc;
325 R(2,2) = cos_theta;
326 R(2,6) = - sin_theta/bet;
327 R(3,4) = m_rc*theta;
328 R(5,1) = sin_theta/bet;
329 R(5,2) = m_rc/bet*(1.0_prt - cos_theta);
330 R(5,6) = m_rc*(-theta+sin_theta/(bet*bet));
331
332 }
333
334 return R;
335 }
336
337 amrex::ParticleReal m_rc;
338
339 private:
340 // constants that are independent of the individually tracked particle,
341 // see: compute_constants() to refresh
342 amrex::ParticleReal m_R11, m_R12, m_R16, m_R21, m_R26, m_R34, m_R56;
343 };
344
345} // namespace impactx
346
347#endif // IMPACTX_SBEND_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
__host__ __device__ std::pair< double, double > sincos(double x)
constexpr T powi(T x) noexcept
Definition All.H:54
@ s
fixed s as the independent variable
Definition ImpactXParticleContainer.H:37
@ t
fixed t as the independent variable
Definition ImpactXParticleContainer.H:38
amrex::SmallMatrix< amrex::ParticleReal, 6, 6, amrex::Order::F, 1 > Map6x6
Definition CovarianceMatrix.H:20
static constexpr __host__ __device__ SmallMatrix< T, NRows, NCols, ORDER, StartIndex > Identity() noexcept
Definition ReferenceParticle.H:31
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition ReferenceParticle.H:110
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition ReferenceParticle.H:94
ImpactXParticleContainer::ParticleType PType
Definition Sbend.H:45
amrex::ParticleReal m_R16
Definition Sbend.H:342
static constexpr auto type
Definition Sbend.H:44
amrex::ParticleReal m_R11
bend radius in m
Definition Sbend.H:342
amrex::ParticleReal m_R21
Definition Sbend.H:342
amrex::ParticleReal m_R34
Definition Sbend.H:342
amrex::ParticleReal m_R12
Definition Sbend.H:342
amrex::ParticleReal m_rc
Definition Sbend.H:337
amrex::ParticleReal m_R26
Definition Sbend.H:342
void compute_constants(RefPart const &refpart)
Definition Sbend.H:99
Sbend(amrex::ParticleReal ds, amrex::ParticleReal rc, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, amrex::ParticleReal aperture_x=0, amrex::ParticleReal aperture_y=0, int nslice=1, std::optional< std::string > name=std::nullopt)
Definition Sbend.H:59
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rc(RefPart const &refpart) const
Definition Sbend.H:80
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT t, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py, T_Real &AMREX_RESTRICT pt, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition Sbend.H:157
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Sbend.H:292
amrex::ParticleReal m_R56
Definition Sbend.H:342
Definition alignment.H:27
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_out(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py) const
Definition alignment.H:109
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:146
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:136
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void shift_in(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_Real &AMREX_RESTRICT px, T_Real &AMREX_RESTRICT py) const
Definition alignment.H:78
Definition beamoptic.H:219
Definition lineartransport.H:29
Definition named.H:29
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:57
AMREX_FORCE_INLINE std::string name() const
Definition named.H:122
Definition nofinalize.H:22
Definition pipeaperture.H:26
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void apply_aperture(T_Real &AMREX_RESTRICT x, T_Real &AMREX_RESTRICT y, T_IdCpu &AMREX_RESTRICT idcpu) const
Definition pipeaperture.H:59
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_x() const
Definition pipeaperture.H:90
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_y() const
Definition pipeaperture.H:101
PipeAperture(amrex::ParticleReal aperture_x, amrex::ParticleReal aperture_y)
Definition pipeaperture.H:32
Definition thick.H:24
Thick(amrex::ParticleReal ds, int nslice)
Definition thick.H:30
amrex::ParticleReal m_ds
Definition thick.H:58
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal ds() const
Definition thick.H:53
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int nslice() const
Definition thick.H:43