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#include "mixin/spintransport.H"
22
23#include <AMReX_Extension.H>
24#include <AMReX_Math.H>
25#include <AMReX_REAL.H>
26#include <AMReX_SIMD.H>
27
28#include <cmath>
29
30
31namespace impactx::elements
32{
33 struct Sbend
34 : public mixin::Named,
35 public mixin::BeamOptic<Sbend>,
36 public mixin::LinearTransport<Sbend>,
37 public mixin::Thick,
38 public mixin::Alignment,
42 // At least on Intel AVX512, there is a small overhead to vectorize this element in DP, and no benefit in SP, see
43 // https://github.com/BLAST-ImpactX/impactx/pull/1002
44 // public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
45 {
46 static constexpr auto type = "Sbend";
48
66 amrex::ParticleReal rotation_degree = 0,
69 int nslice = 1,
70 std::optional<std::string> name = std::nullopt
71 )
72 : Named(std::move(name)),
73 Thick(ds, nslice),
74 Alignment(dx, dy, rotation_degree),
76 m_rc(rc)
77 {
78 }
79
82 rc ([[maybe_unused]] RefPart const & refpart) const
83 {
84 using namespace amrex::literals; // for _rt and _prt
85
86 // TODO: as in ExactSbend
87 // return m_B != 0_prt ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
88 return m_rc;
89 }
90
92 void reverse () { Thick::reverse(); }
93
95 using BeamOptic::operator();
96
104 void compute_constants (RefPart const & refpart)
105 {
106 using namespace amrex::literals; // for _rt and _prt
107 using amrex::Math::powi;
108
109 Alignment::compute_constants(refpart);
110
111 // length of the current slice
112 amrex::ParticleReal const slice_ds = m_ds / nslice();
113
114 // access reference particle values
115 amrex::ParticleReal const ibet = 1.0_prt / refpart.beta();
116 amrex::ParticleReal const ibetagam2 = 1_prt / powi<2>(refpart.beta_gamma());
117
118 // compute intermediate constants (curvature, bend angle, trigonometry)
119 amrex::ParticleReal const h = (m_rc == 0_prt) ? 0_prt : 1_prt/m_rc;
120 amrex::ParticleReal const theta = h * slice_ds;
121 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
122
123 // nonvanishing linear matrix elements (if h = 0, this is equivalent to a drift)
124 m_R11 = (h == 0_prt) ? 1_prt : cos_theta;
125 m_R12 = (h == 0_prt) ? slice_ds : m_rc * sin_theta;
126 m_R16 = (h == 0_prt) ? 0_prt : -m_rc * ibet * (1_prt - cos_theta);
127 m_R21 = (h == 0_prt) ? 0_prt :-sin_theta * h;
128 // m_R22 = m_R11
129 m_R26 = (h == 0_prt) ? 0_prt : -sin_theta * ibet;
130 m_R34 = (h == 0_prt) ? slice_ds : m_rc * theta;
131 // m_R51 = -m_R26
132 // m_R52 = -m_R16
133 m_R56 = (h == 0_prt) ? slice_ds * ibetagam2 : m_rc * (-theta + sin_theta * ibet * ibet);
134
135 // access reference particle values for spin calculation
137 amrex::ParticleReal const gamma = refpart.gamma();
138 amrex::ParticleReal const beta = refpart.beta();
139 amrex::ParticleReal const gyro_const = 1_prt + G * gamma;
140
141 // trigonometry for spin calculation
142 amrex::ParticleReal const spin_ref_angle = G * gamma * theta;
143 auto const [sin_Gtheta, cos_Gtheta] = amrex::Math::sincos(spin_ref_angle);
144
145 // nonvanishing components of spin rotation at the design point
146 m_lambday = -spin_ref_angle;
147
148 // elements of the spin-orbit coupling matrix
149 m_A14 = (gamma - 1_prt)/gamma * (1_prt - cos_Gtheta);
150 m_A21 = -gyro_const * h * sin_theta;
151 m_A22 = -gyro_const * (1_prt - cos_theta);
152 m_A26 = -gyro_const * ibet * sin_theta + G * beta * gamma * theta;
153 m_A34 = (gamma - 1_prt)/gamma * sin_Gtheta;
154
155 }
156
170 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
173 T_Real & AMREX_RESTRICT x,
174 T_Real & AMREX_RESTRICT y,
175 T_Real & AMREX_RESTRICT t,
176 T_Real & AMREX_RESTRICT px,
177 T_Real & AMREX_RESTRICT py,
178 T_Real & AMREX_RESTRICT pt,
179 T_IdCpu & AMREX_RESTRICT idcpu,
180 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
181 ) const
182 {
183 using namespace amrex::literals; // for _rt and _prt
184
185 // shift due to alignment errors of the element
186 shift_in(x, y, px, py);
187
188 // initialize output values
189 T_Real xout = x;
190 T_Real yout = y;
191 T_Real tout = t;
192
193 // initialize output values of momenta
194 T_Real pxout = px;
195 T_Real const pyout = py;
196 T_Real const ptout = pt;
197
198 // advance position and momentum (sector bend)
199 T_Real const R22 = m_R11;
200 T_Real const R51 = -m_R26;
201 T_Real const R52 = -m_R16;
202
203 xout = m_R11 * x
204 + m_R12 * px
205 + m_R16 * pt;
206
207 pxout = m_R21 * x
208 + R22 * px
209 + m_R26 * pt;
210
211 yout = y
212 + m_R34 * py;
213
214 // pyout = py;
215
216 tout = R51 * x
217 + R52 * px
218 + t
219 + m_R56 * pt;
220
221 // ptout = pt;
222
223 // assign updated values
224 x = xout;
225 y = yout;
226 t = tout;
227 px = pxout;
228 py = pyout;
229 pt = ptout;
230
231 // apply transverse aperture
232 apply_aperture(x, y, idcpu);
233
234 // undo shift due to alignment errors of the element
235 shift_out(x, y, px, py);
236 }
237
243 void operator() (RefPart & AMREX_RESTRICT refpart) const
244 {
245 using namespace amrex::literals; // for _rt and _prt
246 using amrex::Math::powi;
247
248 // assign input reference particle values
249 amrex::ParticleReal const x = refpart.x;
250 amrex::ParticleReal const px = refpart.px;
251 amrex::ParticleReal const y = refpart.y;
252 amrex::ParticleReal const py = refpart.py;
253 amrex::ParticleReal const z = refpart.z;
254 amrex::ParticleReal const pz = refpart.pz;
255 amrex::ParticleReal const t = refpart.t;
256 amrex::ParticleReal const pt = refpart.pt;
257 amrex::ParticleReal const s = refpart.s;
258
259 // length of the current slice
260 amrex::ParticleReal const slice_ds = m_ds / nslice();
261
262 // treat the special case of zero field (drift)
263 if (m_rc == 0.0_prt) {
264 // advance position and momentum (drift)
265 amrex::ParticleReal const step = slice_ds /std::sqrt(powi<2>(pt)-1.0_prt);
266 refpart.x = x + step*px;
267 refpart.y = y + step*py;
268 refpart.z = z + step*pz;
269 refpart.t = t - step*pt;
270
271 } else {
272
273 // assign intermediate parameter
274 amrex::ParticleReal const theta = slice_ds/m_rc;
275 amrex::ParticleReal const B = std::sqrt(powi<2>(pt)-1.0_prt)/m_rc;
276
277 // calculate expensive terms once
278 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
279
280 // advance position and momentum (bend)
281 refpart.px = px*cos_theta - pz*sin_theta;
282 refpart.py = py;
283 refpart.pz = pz*cos_theta + px*sin_theta;
284 refpart.pt = pt;
285
286 refpart.x = x + (refpart.pz - pz)/B;
287 refpart.y = y + (theta/B)*py;
288 refpart.z = z - (refpart.px - px)/B;
289 refpart.t = t - (theta/B)*pt;
290
291 }
292
293 // advance integrated path length
294 refpart.s = s + slice_ds;
295
296 }
297
299 using LinearTransport::operator();
300
306 Map6x6
307 transport_map (RefPart const & AMREX_RESTRICT refpart) const
308 {
309 using namespace amrex::literals; // for _rt and _prt
310 using amrex::Math::powi;
311
312 // length of the current slice
313 amrex::ParticleReal const slice_ds = m_ds / nslice();
314
315 // access reference particle values to find beta*gamma^2
316 amrex::ParticleReal const pt_ref = refpart.pt;
317 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1.0_prt;
318 amrex::ParticleReal const bet = std::sqrt(betgam2/(1.0_prt + betgam2));
319
320 // initialize linear map matrix elements
322
323 // treat the special case of zero field
324 if (m_rc==0.0_prt) {
325 R(1,2) = slice_ds;
326 R(3,4) = slice_ds;
327 R(5,6) = slice_ds / betgam2;
328
329 } else {
330
331 // calculate expensive terms once
332 amrex::ParticleReal const theta = slice_ds/m_rc;
333 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
334
335 // assign linear map matrix elements
336 R(1,1) = cos_theta;
337 R(1,2) = m_rc*sin_theta;
338 R(1,6) = - (m_rc/bet)*(1.0_prt - cos_theta);
339 R(2,1) = -sin_theta/m_rc;
340 R(2,2) = cos_theta;
341 R(2,6) = - sin_theta/bet;
342 R(3,4) = m_rc*theta;
343 R(5,1) = sin_theta/bet;
344 R(5,2) = m_rc/bet*(1.0_prt - cos_theta);
345 R(5,6) = m_rc*(-theta+sin_theta/(bet*bet));
346
347 }
348
349 return R;
350 }
351
366 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
369 [[maybe_unused]] T_Real & AMREX_RESTRICT x,
370 [[maybe_unused]] T_Real & AMREX_RESTRICT y,
371 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
372 [[maybe_unused]] T_Real & AMREX_RESTRICT px,
373 [[maybe_unused]] T_Real & AMREX_RESTRICT py,
374 [[maybe_unused]] T_Real & AMREX_RESTRICT pt,
375 [[maybe_unused]] T_Real & AMREX_RESTRICT sx,
376 [[maybe_unused]] T_Real & AMREX_RESTRICT sy,
377 [[maybe_unused]] T_Real & AMREX_RESTRICT sz,
378 [[maybe_unused]] T_IdCpu & AMREX_RESTRICT idcpu,
379 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
380 ) const
381 {
382 using namespace amrex::literals; // for _rt and _prt
383
384 // initialize the three components of the axis-angle vector
385 T_Real lambdax = 0_prt;
386 T_Real lambday = 0_prt;
387 T_Real lambdaz = 0_prt;
388
389 // set the angle-axis generator based on the phase space variables
390 lambdax = m_A14 * py;
391 lambday = m_A21 * x + m_A22 * px + m_A26 * pt;
392 lambdaz = m_A34 * py;
393
394 // push the spin vector using the generator just determined
395 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
396
397 // axis-angle vector components generating the reference spin map
398 lambdax = 0.0_prt;
399 lambday = m_lambday;
400 lambdaz = 0.0_prt;
401
402 // push the spin vector using the generator just determined
403 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
404
405 // phase space push
406 (*this)(x, y, t, px, py, pt, idcpu, refpart);
407 }
408
410
411 private:
412 // constants that are independent of the individually tracked particle,
413 // see: compute_constants() to refresh
416 };
417
418} // namespace impactx
419
421
422#endif // IMPACTX_SBEND_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
#define IMPACTX_PUSH_EXTERN_TEMPLATE(ElementType)
Definition PushAll.H:78
amrex_particle_real ParticleReal
__host__ __device__ std::pair< double, double > sincos(double x)
constexpr T powi(T x) noexcept
Definition All.H:56
@ 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:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition ReferenceParticle.H:168
amrex::ParticleReal gyromagnetic_anomaly
anomalous magnetic moment [unitless]
Definition ReferenceParticle.H:45
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition ReferenceParticle.H:152
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal gamma() const
Definition ReferenceParticle.H:140
Definition Sbend.H:45
ImpactXParticleContainer::ParticleType PType
Definition Sbend.H:47
amrex::ParticleReal m_R16
Definition Sbend.H:414
amrex::ParticleReal m_lambday
Definition Sbend.H:415
static constexpr auto type
Definition Sbend.H:46
amrex::ParticleReal m_A14
Definition Sbend.H:415
amrex::ParticleReal m_R11
bend radius in m
Definition Sbend.H:414
amrex::ParticleReal m_R21
Definition Sbend.H:414
amrex::ParticleReal m_R34
Definition Sbend.H:414
amrex::ParticleReal m_R12
Definition Sbend.H:414
amrex::ParticleReal m_A22
Definition Sbend.H:415
amrex::ParticleReal m_rc
Definition Sbend.H:409
amrex::ParticleReal m_R26
Definition Sbend.H:414
amrex::ParticleReal m_A34
Definition Sbend.H:415
void compute_constants(RefPart const &refpart)
Definition Sbend.H:104
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:61
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rc(RefPart const &refpart) const
Definition Sbend.H:82
amrex::ParticleReal m_A26
Definition Sbend.H:415
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:172
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void spin_and_phasespace_push(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_Real &AMREX_RESTRICT sx, T_Real &AMREX_RESTRICT sy, T_Real &AMREX_RESTRICT sz, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition Sbend.H:368
amrex::ParticleReal m_A21
Definition Sbend.H:415
void reverse()
Definition Sbend.H:92
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Sbend.H:307
amrex::ParticleReal m_R56
Definition Sbend.H:414
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:436
Definition lineartransport.H:50
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 spintransport.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void rotate_spin(T_Real const &AMREX_RESTRICT lambdax, T_Real const &AMREX_RESTRICT lambday, T_Real const &AMREX_RESTRICT lambdaz, T_Real &AMREX_RESTRICT sx, T_Real &AMREX_RESTRICT sy, T_Real &AMREX_RESTRICT sz) const
Definition spintransport.H:48
Definition thick.H:24
Thick(amrex::ParticleReal ds, int nslice)
Definition thick.H:30
amrex::ParticleReal m_ds
Definition thick.H:68
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