ImpactX
Loading...
Searching...
No Matches
CFbend.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_CFBEND_H
11#define IMPACTX_CFBEND_H
12
14#include "mixin/alignment.H"
15#include "mixin/pipeaperture.H"
16#include "mixin/beamoptic.H"
17#include "mixin/thick.H"
19#include "mixin/spintransport.H"
20#include "mixin/named.H"
21#include "mixin/nofinalize.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#include <stdexcept>
30
31
32namespace impactx::elements
33{
34 struct CFbend
35 : public mixin::Named,
36 public mixin::BeamOptic<CFbend>,
37 public mixin::LinearTransport<CFbend>,
38 public mixin::Thick,
39 public mixin::Alignment,
43 // At least on Intel AVX512, there is a small overhead to vectorize this element, see
44 // https://github.com/BLAST-ImpactX/impactx/pull/1002
45 // public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
46 {
47 static constexpr auto type = "CFbend";
49
73 amrex::ParticleReal rotation_degree = 0,
76 int nslice = 1,
77 std::optional<std::string> name = std::nullopt
78 )
79 : Named(std::move(name)),
80 Thick(ds, nslice),
81 Alignment(dx, dy, rotation_degree),
83 m_rc(rc), m_k(k)
84 {
85 }
86
88 void reverse () { Thick::reverse(); }
89
91 using BeamOptic::operator();
92
100 void compute_constants (RefPart const & refpart)
101 {
102 using namespace amrex::literals; // for _rt and _prt
103 using amrex::Math::powi;
104
105 Alignment::compute_constants(refpart);
106
107 // length of the current slice
108 amrex::ParticleReal const slice_ds = m_ds / nslice();
109
110 // find beta*gamma^2, beta
111 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1_prt;
112 amrex::ParticleReal const bet = refpart.beta();
113 amrex::ParticleReal const ibetgam2 = 1_prt / betgam2;
114 amrex::ParticleReal const b2rc2 = powi<2>(bet) * powi<2>(m_rc);
115
116 // update horizontal and longitudinal phase space variables
117 amrex::ParticleReal const gx = m_k + powi<-2>(m_rc);
118 amrex::ParticleReal const omega_x = std::sqrt(std::abs(gx));
119
120 // update vertical phase space variables
121 amrex::ParticleReal const gy = -m_k;
122 amrex::ParticleReal const omega_y = std::sqrt(std::abs(gy));
123
124 // trigonometry
125 amrex::ParticleReal const cx = gx > 0_prt ? std::cos(omega_x * slice_ds) : std::cosh(omega_x * slice_ds);
126 amrex::ParticleReal const sx = gx > 0_prt ? std::sin(omega_x * slice_ds)/omega_x : std::sinh(omega_x * slice_ds)/omega_x;
127 amrex::ParticleReal const cy = gy > 0_prt ? std::cos(omega_y * slice_ds) : std::cosh(omega_y * slice_ds);
128 amrex::ParticleReal const sy = gy > 0_prt ? std::sin(omega_y * slice_ds)/omega_y : std::sinh(omega_y * slice_ds)/omega_y;
129
130 amrex::ParticleReal const ibrc = 1_prt / (bet * m_rc);
131 amrex::ParticleReal const igbrc = 1_prt / (gx * bet * m_rc);
132 amrex::ParticleReal const igb2r2 = 1_prt / (gx * b2rc2);
133
134 // elements of the linear transport matrix
135 // The matrix elements of R are identical with those found in:
136 // F. C. Iselin, Part. Accel. 17, pp. 143-155 (1985), Section 4,
137 // except the longitudinal variables used here (t,pt) have the opposite sign.
138 m_R11 = cx;
139 m_R12 = sx;
140 m_R16 = -(1_prt - cx) * igbrc;
141 m_R21 = -gx * sx;
142 m_R22 = cx;
143 m_R26 = -sx * ibrc;
144 m_R33 = cy;
145 m_R34 = sy;
146 m_R43 = -gy * sy;
147 m_R44 = cy;
148 m_R51 = -m_R26;
149 m_R52 = -m_R16;
150 m_R56 = slice_ds * ibetgam2 + (sx - slice_ds) * igb2r2;
151
152 // access reference particle values for spin calculation
154 amrex::ParticleReal const gamma = refpart.gamma();
155 amrex::ParticleReal const beta = refpart.beta();
156 amrex::ParticleReal const gyro_const1 = 1_prt + G * gamma;
157 amrex::ParticleReal const gyro_const2 = 1_prt + G * gamma * gamma;
158
159 // trigonometry for spin calculation
160 amrex::ParticleReal const h = (m_rc == 0_prt) ? 0_prt : 1_prt/m_rc;
161 auto const [sinG, cosG] = amrex::Math::sincos(G * h * gamma * slice_ds);
162
163 // intermediate constants for spin calculation
164 amrex::ParticleReal const factor = 1_prt / (-gy + powi<2>(G * h * gamma));
165
166 // nonvanishing components of spin rotation at the design point
167 m_lambday = -G * h * gamma * slice_ds;
168
169 // elements of the spin-orbit coupling matrix
170 m_A13 = (G*h*gyro_const2*gy*cy*sinG + gy*(powi<2>(G*h)*(gamma - 1_prt)*gamma - gy*gyro_const1 )*sy*cosG) * factor;
171 m_A14 = (G*h*gyro_const2*gy*sy*sinG + (powi<2>(G*h)*(gamma - 1_prt)*gamma - gy*gyro_const1)*(1_prt - cosG*cy))*factor;
172 m_A21 = -gyro_const1 * gx * sx;
173 m_A22 = -gyro_const1 * (1_prt - cx);
174 m_A26 = G*h*beta*gamma*slice_ds - h*gyro_const1*sx/(beta);
175 m_A33 = -((-gy*gyro_const1 + powi<2>(G*h)*gamma*(gamma - 1_prt))*gy*sy*sinG -gy*G*h*gyro_const2*(1_prt - cosG*cy))*factor;
176 m_A34 = ((-gy*gyro_const1 + powi<2>(G*h)*gamma*(gamma - 1_prt)) * cy*sinG + G*h*gyro_const2*gy*cosG*sy)*factor;
177 }
178
192 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
195 T_Real & AMREX_RESTRICT x,
196 T_Real & AMREX_RESTRICT y,
197 T_Real & AMREX_RESTRICT t,
198 T_Real & AMREX_RESTRICT px,
199 T_Real & AMREX_RESTRICT py,
200 T_Real & AMREX_RESTRICT pt,
201 T_IdCpu & AMREX_RESTRICT idcpu,
202 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
203 ) const
204 {
205 using namespace amrex::literals; // for _rt and _prt
206
207 // shift due to alignment errors of the element
208 shift_in(x, y, px, py);
209
210 // initialize output values
211 T_Real xout = x;
212 T_Real yout = y;
213 T_Real tout = t;
214
215 // initialize output values of momenta
216 T_Real pxout = px;
217 T_Real pyout = py;
218 T_Real ptout = pt;
219
220 // update horizontal and longitudinal phase space variables
221 // advance position and momentum: (de)focusing
222 x = m_R11 * xout + m_R12 * px + m_R16 * pt;
223 pxout = m_R21 * xout + m_R22 * px + m_R26 * pt;
224 t = m_R51 * xout + m_R52 * px + m_R56 * pt + tout;
225 ptout = pt;
226
227 // update vertical phase space variables
228 // advance position and momentum (de)focusing
229 y = m_R33 * yout + m_R34 * py;
230 pyout = m_R43 * yout + m_R44 * py;
231
232 // assign updated momenta
233 px = pxout;
234 py = pyout;
235 pt = ptout;
236
237 // apply transverse aperture
238 apply_aperture(x, y, idcpu);
239
240 // undo shift due to alignment errors of the element
241 shift_out(x, y, px, py);
242 }
243
249 void operator() (RefPart & AMREX_RESTRICT refpart) const
250 {
251 using namespace amrex::literals; // for _rt and _prt
252 using amrex::Math::powi;
253
254 // assign input reference particle values
255 amrex::ParticleReal const x = refpart.x;
256 amrex::ParticleReal const px = refpart.px;
257 amrex::ParticleReal const y = refpart.y;
258 amrex::ParticleReal const py = refpart.py;
259 amrex::ParticleReal const z = refpart.z;
260 amrex::ParticleReal const pz = refpart.pz;
261 amrex::ParticleReal const t = refpart.t;
262 amrex::ParticleReal const pt = refpart.pt;
263 amrex::ParticleReal const s = refpart.s;
264
265 // length of the current slice
266 amrex::ParticleReal const slice_ds = m_ds / nslice();
267
268 // assign intermediate parameter
269 amrex::ParticleReal const theta = slice_ds/m_rc;
270 amrex::ParticleReal const B = std::sqrt(powi<2>(pt)-1.0_prt)/m_rc;
271
272 // calculate expensive terms once
273 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
274
275 // advance position and momentum (bend)
276 refpart.px = px*cos_theta - pz*sin_theta;
277 refpart.py = py;
278 refpart.pz = pz*cos_theta + px*sin_theta;
279 refpart.pt = pt;
280
281 refpart.x = x + (refpart.pz - pz)/B;
282 refpart.y = y + (theta/B)*py;
283 refpart.z = z - (refpart.px - px)/B;
284 refpart.t = t - (theta/B)*pt;
285
286 // advance integrated path length
287 refpart.s = s + slice_ds;
288 }
289
290
305 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
308 [[maybe_unused]] T_Real & AMREX_RESTRICT x,
309 [[maybe_unused]] T_Real & AMREX_RESTRICT y,
310 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
311 [[maybe_unused]] T_Real & AMREX_RESTRICT px,
312 [[maybe_unused]] T_Real & AMREX_RESTRICT py,
313 [[maybe_unused]] T_Real & AMREX_RESTRICT pt,
314 [[maybe_unused]] T_Real & AMREX_RESTRICT sx,
315 [[maybe_unused]] T_Real & AMREX_RESTRICT sy,
316 [[maybe_unused]] T_Real & AMREX_RESTRICT sz,
317 [[maybe_unused]] T_IdCpu & AMREX_RESTRICT idcpu,
318 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
319 ) const
320 {
321 using namespace amrex::literals; // for _rt and _prt
322 using amrex::Math::powi;
323
324 // initialize the three components of the axis-angle vector
325 T_Real lambdax = 0_prt;
326 T_Real lambday = 0_prt;
327 T_Real lambdaz = 0_prt;
328
329 // set the angle-axis generator based on the phase space variables
330 lambdax = m_A13 * y + m_A14 * py;
331 lambday = m_A21 * x + m_A22 * px + m_A26 * pt;
332 lambdaz = m_A33 * y + m_A34 * py;
333
334 // push the spin vector using the generator just determined
335 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
336
337 // axis-angle vector components generating the reference spin map
338 lambdax = 0.0_prt;
339 lambday = m_lambday;
340 lambdaz = 0.0_prt;
341
342 // push the spin vector using the generator just determined
343 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
344
345 // phase space push
346 (*this)(x, y, t, px, py, pt, idcpu, refpart);
347
348 }
349
350
352 using LinearTransport::operator();
353
360 Map6x6
361 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
362 {
363 using namespace amrex::literals; // for _rt and _prt
364 using amrex::Math::powi;
365
366 // length of the current slice
367 amrex::ParticleReal const slice_ds = m_ds / nslice();
368
369 // find beta*gamma^2, beta
370 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1_prt;
371 amrex::ParticleReal const bet = refpart.beta();
372 amrex::ParticleReal const ibetgam2 = 1_prt / betgam2;
373 amrex::ParticleReal const b2rc2 = powi<2>(bet) * powi<2>(m_rc);
374
375 // update horizontal and longitudinal phase space variables
376 amrex::ParticleReal const gx = m_k + powi<-2>(m_rc);
377 amrex::ParticleReal const omega_x = std::sqrt(std::abs(gx));
378
379 // update vertical phase space variables
380 amrex::ParticleReal const gy = -m_k;
381 amrex::ParticleReal const omega_y = std::sqrt(std::abs(gy));
382
383 // trigonometry
384 auto const [sinx, cosx] = amrex::Math::sincos(omega_x * slice_ds);
385 amrex::ParticleReal const sinhx = std::sinh(omega_x * slice_ds);
386 amrex::ParticleReal const coshx = std::cosh(omega_x * slice_ds);
387 auto const [siny, cosy] = amrex::Math::sincos(omega_y * slice_ds);
388 amrex::ParticleReal const sinhy = std::sinh(omega_y * slice_ds);
389 amrex::ParticleReal const coshy = std::cosh(omega_y * slice_ds);
390
391 amrex::ParticleReal const igbrc = 1_prt / (gx * bet * m_rc);
392 amrex::ParticleReal const iobrc = 1_prt / (omega_x * bet * m_rc);
393 amrex::ParticleReal const igobr = 1_prt / (gx * omega_x * b2rc2);
394
395 // initialize linear map matrix elements
397
398 R(1,1) = gx > 0_prt ? cosx : coshx;
399 R(1,2) = gx > 0_prt ? sinx / omega_x : sinhx / omega_x;
400 R(1,6) = gx > 0_prt ? -(1_prt - cosx) * igbrc : -(1_prt - coshx) * igbrc;
401 R(2,1) = gx > 0_prt ? -omega_x * sinx : omega_x * sinhx;
402 R(2,2) = gx > 0_prt ? cosx : coshx;
403 R(2,6) = gx > 0_prt ? -sinx * iobrc : -sinhx * iobrc;
404 R(3,3) = gy > 0_prt ? cosy : coshy;
405 R(3,4) = gy > 0_prt ? siny / omega_y : sinhy / omega_y;
406 R(4,3) = gy > 0_prt ? -omega_y * siny : omega_y * sinhy;
407 R(4,4) = gy > 0_prt ? cosy : coshy;
408 R(5,1) = gx > 0_prt ? sinx * iobrc : sinhx * iobrc;
409 R(5,2) = gx > 0_prt ? (1_prt - cosx) * igbrc : (1_prt - coshx) * igbrc;
410 R(5,6) = gx > 0_prt ?
411 slice_ds * ibetgam2 + (sinx - omega_x * slice_ds) * igobr :
412 slice_ds * ibetgam2 + (sinhx - omega_x * slice_ds) * igobr;
413
414 return R;
415 }
416
419
420 private:
421 // constants that are independent of the individually tracked particle,
422 // see: compute_constants() to refresh
425 };
426
427} // namespace impactx
428
430
431#endif // IMPACTX_CFBEND_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::ParticleReal pt
energy, normalized by rest energy
Definition ReferenceParticle.H:42
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 CFbend.H:46
amrex::ParticleReal m_R21
Definition CFbend.H:423
amrex::ParticleReal m_A22
Definition CFbend.H:424
void compute_constants(RefPart const &refpart)
Definition CFbend.H:100
void reverse()
Definition CFbend.H:88
amrex::ParticleReal m_A26
Definition CFbend.H:424
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 CFbend.H:307
amrex::ParticleReal m_R43
Definition CFbend.H:423
static constexpr auto type
Definition CFbend.H:47
amrex::ParticleReal m_R11
quadrupole strength in m^(-2)
Definition CFbend.H:423
amrex::ParticleReal m_rc
Definition CFbend.H:417
amrex::ParticleReal m_R44
Definition CFbend.H:423
amrex::ParticleReal m_A21
Definition CFbend.H:424
amrex::ParticleReal m_A13
Definition CFbend.H:424
CFbend(amrex::ParticleReal ds, amrex::ParticleReal rc, amrex::ParticleReal k, 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 CFbend.H:67
amrex::ParticleReal m_A33
Definition CFbend.H:424
amrex::ParticleReal m_R56
Definition CFbend.H:423
ImpactXParticleContainer::ParticleType PType
Definition CFbend.H:48
amrex::ParticleReal m_R33
Definition CFbend.H:423
amrex::ParticleReal m_k
bend radius in m
Definition CFbend.H:418
amrex::ParticleReal m_R16
Definition CFbend.H:423
amrex::ParticleReal m_lambday
Definition CFbend.H:424
amrex::ParticleReal m_R52
Definition CFbend.H:423
amrex::ParticleReal m_R26
Definition CFbend.H:423
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 CFbend.H:194
amrex::ParticleReal m_R51
Definition CFbend.H:423
amrex::ParticleReal m_A34
Definition CFbend.H:424
amrex::ParticleReal m_R34
Definition CFbend.H:423
amrex::ParticleReal m_R12
Definition CFbend.H:423
amrex::ParticleReal m_R22
Definition CFbend.H:423
amrex::ParticleReal m_A14
Definition CFbend.H:424
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition CFbend.H:361
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