ImpactX
Loading...
Searching...
No Matches
CFbend.H
Go to the documentation of this file.
1/* Copyright 2022-2026 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
31namespace impactx::elements
32{
33 struct CFbend
34 : public mixin::Named,
35 public mixin::BeamOptic<CFbend>,
36 public mixin::LinearTransport<CFbend>,
37 public mixin::Thick,
38 public mixin::Alignment,
42 // At least on Intel AVX512, there is a small overhead to vectorize this element, 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 = "CFbend";
48
85
87 void reverse () { Thick::reverse(); }
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 // find beta*gamma^2, beta
110 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1_prt;
111 amrex::ParticleReal const bet = refpart.beta();
112 amrex::ParticleReal const ibetgam2 = 1_prt / betgam2;
113 amrex::ParticleReal const b2rc2 = powi<2>(bet) * powi<2>(m_rc);
114
115 // update horizontal and longitudinal phase space variables
116 amrex::ParticleReal const gx = m_k + powi<-2>(m_rc);
117 amrex::ParticleReal const omega_x = std::sqrt(std::abs(gx));
118
119 // update vertical phase space variables
120 amrex::ParticleReal const gy = -m_k;
121 amrex::ParticleReal const omega_y = std::sqrt(std::abs(gy));
122
123 // trigonometry
124 amrex::ParticleReal const cx = gx > 0_prt ? std::cos(omega_x * slice_ds) : std::cosh(omega_x * slice_ds);
125 amrex::ParticleReal const sx = gx > 0_prt ? std::sin(omega_x * slice_ds)/omega_x : std::sinh(omega_x * slice_ds)/omega_x;
126 amrex::ParticleReal const cy = gy > 0_prt ? std::cos(omega_y * slice_ds) : std::cosh(omega_y * slice_ds);
127 amrex::ParticleReal const sy = gy > 0_prt ? std::sin(omega_y * slice_ds)/omega_y : std::sinh(omega_y * slice_ds)/omega_y;
128
129 amrex::ParticleReal const ibrc = 1_prt / (bet * m_rc);
130 amrex::ParticleReal const igbrc = 1_prt / (gx * bet * m_rc);
131 amrex::ParticleReal const igb2r2 = 1_prt / (gx * b2rc2);
132
133 // elements of the linear transport matrix
134 // The matrix elements of R are identical with those found in:
135 // F. C. Iselin, Part. Accel. 17, pp. 143-155 (1985), Section 4,
136 // except the longitudinal variables used here (t,pt) have the opposite sign.
137 m_R11 = cx;
138 m_R12 = sx;
139 m_R16 = -(1_prt - cx) * igbrc;
140 m_R21 = -gx * sx;
141 m_R22 = cx;
142 m_R26 = -sx * ibrc;
143 m_R33 = cy;
144 m_R34 = sy;
145 m_R43 = -gy * sy;
146 m_R44 = cy;
147 m_R51 = -m_R26;
148 m_R52 = -m_R16;
149 m_R56 = slice_ds * ibetgam2 + (sx - slice_ds) * igb2r2;
150
151 // access reference particle values for spin calculation
153 amrex::ParticleReal const gamma = refpart.gamma();
154 amrex::ParticleReal const beta = refpart.beta();
155 amrex::ParticleReal const gyro_const1 = 1_prt + G * gamma;
156 amrex::ParticleReal const gyro_const2 = 1_prt + G * gamma * gamma;
157
158 // trigonometry for spin calculation
159 amrex::ParticleReal const h = (m_rc == 0_prt) ? 0_prt : 1_prt/m_rc;
160 auto const [sinG, cosG] = amrex::Math::sincos(G * h * gamma * slice_ds);
161
162 // intermediate constants for spin calculation
163 amrex::ParticleReal const factor = 1_prt / (-gy + powi<2>(G * h * gamma));
164
165 // nonvanishing components of spin rotation at the design point
166 m_lambday = -G * h * gamma * slice_ds;
167
168 // elements of the spin-orbit coupling matrix
169 m_A13 = (G*h*gyro_const2*gy*cy*sinG + gy*(powi<2>(G*h)*(gamma - 1_prt)*gamma - gy*gyro_const1 )*sy*cosG) * factor;
170 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;
171 m_A21 = -gyro_const1 * gx * sx;
172 m_A22 = -gyro_const1 * (1_prt - cx);
173 m_A26 = G*h*beta*gamma*slice_ds - h*gyro_const1*sx/(beta);
174 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;
175 m_A34 = ((-gy*gyro_const1 + powi<2>(G*h)*gamma*(gamma - 1_prt)) * cy*sinG + G*h*gyro_const2*gy*cosG*sy)*factor;
176 }
177
191 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
194 T_Real & AMREX_RESTRICT x,
195 T_Real & AMREX_RESTRICT y,
196 T_Real & AMREX_RESTRICT t,
197 T_Real & AMREX_RESTRICT px,
198 T_Real & AMREX_RESTRICT py,
199 T_Real const & AMREX_RESTRICT pt,
200 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
201 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
202 ) const
203 {
204 using namespace amrex::literals; // for _rt and _prt
205
206 // initialize output values
207 T_Real xout = x;
208 T_Real yout = y;
209 T_Real tout = t;
210
211 // initialize output values of momenta
212 T_Real pxout = px;
213 T_Real pyout = py;
214 // T_Real ptout = pt;
215
216 // update horizontal and longitudinal phase space variables
217 // advance position and momentum: (de)focusing
218 x = m_R11 * xout + m_R12 * px + m_R16 * pt;
219 pxout = m_R21 * xout + m_R22 * px + m_R26 * pt;
220 t = m_R51 * xout + m_R52 * px + m_R56 * pt + tout;
221 // ptout = pt;
222
223 // update vertical phase space variables
224 // advance position and momentum (de)focusing
225 y = m_R33 * yout + m_R34 * py;
226 pyout = m_R43 * yout + m_R44 * py;
227
228 // assign updated momenta
229 px = pxout;
230 py = pyout;
231 // pt = ptout;
232 }
233
239 void operator() (RefPart & AMREX_RESTRICT refpart) const
240 {
241 using namespace amrex::literals; // for _rt and _prt
242 using amrex::Math::powi;
243
244 // assign input reference particle values
245 amrex::ParticleReal const x = refpart.x;
246 amrex::ParticleReal const px = refpart.px;
247 amrex::ParticleReal const y = refpart.y;
248 amrex::ParticleReal const py = refpart.py;
249 amrex::ParticleReal const z = refpart.z;
250 amrex::ParticleReal const pz = refpart.pz;
251 amrex::ParticleReal const t = refpart.t;
252 amrex::ParticleReal const pt = refpart.pt;
253 amrex::ParticleReal const s = refpart.s;
254
255 // length of the current slice
256 amrex::ParticleReal const slice_ds = m_ds / nslice();
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 // advance integrated path length
277 refpart.s = s + slice_ds;
278 }
279
294 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
297 T_Real & AMREX_RESTRICT x,
298 T_Real & AMREX_RESTRICT y,
299 T_Real & AMREX_RESTRICT t,
300 T_Real & AMREX_RESTRICT px,
301 T_Real & AMREX_RESTRICT py,
302 T_Real const & AMREX_RESTRICT pt,
303 T_Real & AMREX_RESTRICT sx,
304 T_Real & AMREX_RESTRICT sy,
305 T_Real & AMREX_RESTRICT sz,
306 T_IdCpu const & AMREX_RESTRICT idcpu,
307 RefPart const & AMREX_RESTRICT refpart
308 ) const
309 {
310 using namespace amrex::literals; // for _rt and _prt
311 using amrex::Math::powi;
312
313 // initialize the three components of the axis-angle vector
314 T_Real lambdax = 0_prt;
315 T_Real lambday = 0_prt;
316 T_Real lambdaz = 0_prt;
317
318 // set the angle-axis generator based on the phase space variables
319 lambdax = m_A13 * y + m_A14 * py;
320 lambday = m_A21 * x + m_A22 * px + m_A26 * pt;
321 lambdaz = m_A33 * y + m_A34 * py;
322
323 // push the spin vector using the generator just determined
324 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
325
326 // axis-angle vector components generating the reference spin map
327 lambdax = 0.0_prt;
328 lambday = m_lambday;
329 lambdaz = 0.0_prt;
330
331 // push the spin vector using the generator just determined
332 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
333
334 // phase space push
335 (*this)(x, y, t, px, py, pt, idcpu, refpart);
336
337 }
338
340 using LinearTransport::operator();
341
348 Map6x6
349 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
350 {
351 using namespace amrex::literals; // for _rt and _prt
352 using amrex::Math::powi;
353
354 // length of the current slice
355 amrex::ParticleReal const slice_ds = m_ds / nslice();
356
357 // find beta*gamma^2, beta
358 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1_prt;
359 amrex::ParticleReal const bet = refpart.beta();
360 amrex::ParticleReal const ibetgam2 = 1_prt / betgam2;
361 amrex::ParticleReal const b2rc2 = powi<2>(bet) * powi<2>(m_rc);
362
363 // update horizontal and longitudinal phase space variables
364 amrex::ParticleReal const gx = m_k + powi<-2>(m_rc);
365 amrex::ParticleReal const omega_x = std::sqrt(std::abs(gx));
366
367 // update vertical phase space variables
368 amrex::ParticleReal const gy = -m_k;
369 amrex::ParticleReal const omega_y = std::sqrt(std::abs(gy));
370
371 // trigonometry
372 auto const [sinx, cosx] = amrex::Math::sincos(omega_x * slice_ds);
373 amrex::ParticleReal const sinhx = std::sinh(omega_x * slice_ds);
374 amrex::ParticleReal const coshx = std::cosh(omega_x * slice_ds);
375 auto const [siny, cosy] = amrex::Math::sincos(omega_y * slice_ds);
376 amrex::ParticleReal const sinhy = std::sinh(omega_y * slice_ds);
377 amrex::ParticleReal const coshy = std::cosh(omega_y * slice_ds);
378
379 amrex::ParticleReal const igbrc = 1_prt / (gx * bet * m_rc);
380 amrex::ParticleReal const iobrc = 1_prt / (omega_x * bet * m_rc);
381 amrex::ParticleReal const igobr = 1_prt / (gx * omega_x * b2rc2);
382
383 // initialize linear map matrix elements
385
386 R(1,1) = gx > 0_prt ? cosx : coshx;
387 R(1,2) = gx > 0_prt ? sinx / omega_x : sinhx / omega_x;
388 R(1,6) = gx > 0_prt ? -(1_prt - cosx) * igbrc : -(1_prt - coshx) * igbrc;
389 R(2,1) = gx > 0_prt ? -omega_x * sinx : omega_x * sinhx;
390 R(2,2) = gx > 0_prt ? cosx : coshx;
391 R(2,6) = gx > 0_prt ? -sinx * iobrc : -sinhx * iobrc;
392 R(3,3) = gy > 0_prt ? cosy : coshy;
393 R(3,4) = gy > 0_prt ? siny / omega_y : sinhy / omega_y;
394 R(4,3) = gy > 0_prt ? -omega_y * siny : omega_y * sinhy;
395 R(4,4) = gy > 0_prt ? cosy : coshy;
396 R(5,1) = gx > 0_prt ? sinx * iobrc : sinhx * iobrc;
397 R(5,2) = gx > 0_prt ? (1_prt - cosx) * igbrc : (1_prt - coshx) * igbrc;
398 R(5,6) = gx > 0_prt ?
399 slice_ds * ibetgam2 + (sinx - omega_x * slice_ds) * igobr :
400 slice_ds * ibetgam2 + (sinhx - omega_x * slice_ds) * igobr;
401
402 // apply the transverse rotation (roll) alignment error
403 return rotate_aligned_map(R);
404 }
405
408
409 private:
410 // constants that are independent of the individually tracked particle,
411 // see: compute_constants() to refresh
414 };
415
416} // namespace impactx
417
419
420#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
constexpr T powi(T x) noexcept
__host__ __device__ std::pair< double, double > sincos(double x)
Definition All.H:55
@ 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:151
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal gamma() const
Definition ReferenceParticle.H:139
Definition CFbend.H:45
amrex::ParticleReal m_R21
Definition CFbend.H:412
amrex::ParticleReal m_A22
Definition CFbend.H:413
void compute_constants(RefPart const &refpart)
Definition CFbend.H:99
void reverse()
Definition CFbend.H:87
amrex::ParticleReal m_A26
Definition CFbend.H:413
amrex::ParticleReal m_R43
Definition CFbend.H:412
CFbend(amrex::ParticleReal ds, amrex::ParticleReal rc, amrex::ParticleReal k, amrex::ParticleReal dx=DEFAULT_dx, amrex::ParticleReal dy=DEFAULT_dy, amrex::ParticleReal rotation_degree=DEFAULT_rotation_degree, amrex::ParticleReal aperture_x=DEFAULT_aperture_x, amrex::ParticleReal aperture_y=DEFAULT_aperture_y, int nslice=DEFAULT_nslice, std::optional< std::string > name=DEFAULT_name)
Definition CFbend.H:66
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 const &AMREX_RESTRICT pt, T_Real &AMREX_RESTRICT sx, T_Real &AMREX_RESTRICT sy, T_Real &AMREX_RESTRICT sz, T_IdCpu const &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition CFbend.H:296
static constexpr auto type
Definition CFbend.H:46
amrex::ParticleReal m_R11
quadrupole strength in m^(-2)
Definition CFbend.H:412
amrex::ParticleReal m_rc
Definition CFbend.H:406
amrex::ParticleReal m_R44
Definition CFbend.H:412
amrex::ParticleReal m_A21
Definition CFbend.H:413
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 const &AMREX_RESTRICT pt, T_IdCpu const &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition CFbend.H:193
amrex::ParticleReal m_A13
Definition CFbend.H:413
amrex::ParticleReal m_A33
Definition CFbend.H:413
amrex::ParticleReal m_R56
Definition CFbend.H:412
ImpactXParticleContainer::ParticleType PType
Definition CFbend.H:47
amrex::ParticleReal m_R33
Definition CFbend.H:412
amrex::ParticleReal m_k
bend radius in m
Definition CFbend.H:407
amrex::ParticleReal m_R16
Definition CFbend.H:412
amrex::ParticleReal m_lambday
Definition CFbend.H:413
amrex::ParticleReal m_R52
Definition CFbend.H:412
amrex::ParticleReal m_R26
Definition CFbend.H:412
amrex::ParticleReal m_R51
Definition CFbend.H:412
amrex::ParticleReal m_A34
Definition CFbend.H:413
amrex::ParticleReal m_R34
Definition CFbend.H:412
amrex::ParticleReal m_R12
Definition CFbend.H:412
amrex::ParticleReal m_R22
Definition CFbend.H:412
amrex::ParticleReal m_A14
Definition CFbend.H:413
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition CFbend.H:349
Definition alignment.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:193
static constexpr amrex::ParticleReal DEFAULT_dy
Definition alignment.H:34
static constexpr amrex::ParticleReal DEFAULT_dx
Definition alignment.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:183
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 rotate_aligned_map(Map6x6 const &R) const
Definition alignment.H:267
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:43
static constexpr amrex::ParticleReal DEFAULT_rotation_degree
Definition alignment.H:35
Definition beamoptic.H:567
Definition lineartransport.H:50
Definition named.H:29
static constexpr std::nullopt_t DEFAULT_name
Definition named.H:30
AMREX_GPU_HOST Named(std::optional< std::string > name)
Definition named.H:59
AMREX_FORCE_INLINE std::string name() const
Definition named.H:124
Definition nofinalize.H:22
Definition pipeaperture.H:26
static constexpr amrex::ParticleReal DEFAULT_aperture_x
Definition pipeaperture.H:27
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_x() const
Definition pipeaperture.H:93
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal aperture_y() const
Definition pipeaperture.H:104
static constexpr amrex::ParticleReal DEFAULT_aperture_y
Definition pipeaperture.H:28
PipeAperture(amrex::ParticleReal aperture_x, amrex::ParticleReal aperture_y)
Definition pipeaperture.H:35
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
static constexpr int DEFAULT_nslice
Definition thick.H:25
Thick(amrex::ParticleReal ds, int nslice)
Definition thick.H:32
amrex::ParticleReal m_ds
Definition thick.H:70
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal ds() const
Definition thick.H:55
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int nslice() const
Definition thick.H:45