ImpactX
Loading...
Searching...
No Matches
Sbend.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_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
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,
41 // At least on Intel AVX512, there is a small overhead to vectorize this element in DP, and no benefit in SP, see
42 // https://github.com/BLAST-ImpactX/impactx/pull/1002
43 // public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
44 {
45 static constexpr auto type = "Sbend";
47
78
81 rc ([[maybe_unused]] RefPart const & refpart) const
82 {
83 using namespace amrex::literals; // for _rt and _prt
84
85 // TODO: as in ExactSbend
86 // return m_B != 0_prt ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
87 return m_rc;
88 }
89
91 void reverse () { Thick::reverse(); }
92
94 using BeamOptic::operator();
95
103 void compute_constants (RefPart const & refpart)
104 {
105 using namespace amrex::literals; // for _rt and _prt
106 using amrex::Math::powi;
107
108 Alignment::compute_constants(refpart);
109
110 // length of the current slice
111 amrex::ParticleReal const slice_ds = m_ds / nslice();
112
113 // access reference particle values
114 amrex::ParticleReal const ibet = 1.0_prt / refpart.beta();
115 amrex::ParticleReal const ibetagam2 = 1_prt / powi<2>(refpart.beta_gamma());
116
117 // compute intermediate constants (curvature, bend angle, trigonometry)
118 amrex::ParticleReal const h = (m_rc == 0_prt) ? 0_prt : 1_prt/m_rc;
119 amrex::ParticleReal const theta = h * slice_ds;
120 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
121
122 // nonvanishing linear matrix elements (if h = 0, this is equivalent to a drift)
123 m_R11 = (h == 0_prt) ? 1_prt : cos_theta;
124 m_R12 = (h == 0_prt) ? slice_ds : m_rc * sin_theta;
125 m_R16 = (h == 0_prt) ? 0_prt : -m_rc * ibet * (1_prt - cos_theta);
126 m_R21 = (h == 0_prt) ? 0_prt :-sin_theta * h;
127 // m_R22 = m_R11
128 m_R26 = (h == 0_prt) ? 0_prt : -sin_theta * ibet;
129 m_R34 = (h == 0_prt) ? slice_ds : m_rc * theta;
130 // m_R51 = -m_R26
131 // m_R52 = -m_R16
132 m_R56 = (h == 0_prt) ? slice_ds * ibetagam2 : m_rc * (-theta + sin_theta * ibet * ibet);
133
134 // access reference particle values for spin calculation
136 amrex::ParticleReal const gamma = refpart.gamma();
137 amrex::ParticleReal const beta = refpart.beta();
138 amrex::ParticleReal const gyro_const = 1_prt + G * gamma;
139
140 // trigonometry for spin calculation
141 amrex::ParticleReal const spin_ref_angle = G * gamma * theta;
142 auto const [sin_Gtheta, cos_Gtheta] = amrex::Math::sincos(spin_ref_angle);
143
144 // nonvanishing components of spin rotation at the design point
145 m_lambday = -spin_ref_angle;
146
147 // elements of the spin-orbit coupling matrix
148 m_A14 = (gamma - 1_prt)/gamma * (1_prt - cos_Gtheta);
149 m_A21 = -gyro_const * h * sin_theta;
150 m_A22 = -gyro_const * (1_prt - cos_theta);
151 m_A26 = -gyro_const * ibet * sin_theta + G * beta * gamma * theta;
152 m_A34 = (gamma - 1_prt)/gamma * sin_Gtheta;
153
154 }
155
169 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
172 T_Real & AMREX_RESTRICT x,
173 T_Real & AMREX_RESTRICT y,
174 T_Real & AMREX_RESTRICT t,
175 T_Real & AMREX_RESTRICT px,
176 T_Real const & AMREX_RESTRICT py,
177 T_Real const & AMREX_RESTRICT pt,
178 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
179 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
180 ) const
181 {
182 using namespace amrex::literals; // for _rt and _prt
183
184 // initialize output values
185 T_Real xout = x;
186 T_Real yout = y;
187 T_Real tout = t;
188
189 // initialize output values of momenta
190 T_Real pxout = px;
191 // T_Real const pyout = py;
192 // T_Real const ptout = pt;
193
194 // advance position and momentum (sector bend)
195 T_Real const R22 = m_R11;
196 T_Real const R51 = -m_R26;
197 T_Real const R52 = -m_R16;
198
199 xout = m_R11 * x
200 + m_R12 * px
201 + m_R16 * pt;
202
203 pxout = m_R21 * x
204 + R22 * px
205 + m_R26 * pt;
206
207 yout = y
208 + m_R34 * py;
209
210 // pyout = py;
211
212 tout = R51 * x
213 + R52 * px
214 + t
215 + m_R56 * pt;
216
217 // ptout = pt;
218
219 // assign updated values
220 x = xout;
221 y = yout;
222 t = tout;
223 px = pxout;
224 // py = pyout;
225 // pt = ptout;
226 }
227
233 void operator() (RefPart & AMREX_RESTRICT refpart) const
234 {
235 using namespace amrex::literals; // for _rt and _prt
236 using amrex::Math::powi;
237
238 // assign input reference particle values
239 amrex::ParticleReal const x = refpart.x;
240 amrex::ParticleReal const px = refpart.px;
241 amrex::ParticleReal const y = refpart.y;
242 amrex::ParticleReal const py = refpart.py;
243 amrex::ParticleReal const z = refpart.z;
244 amrex::ParticleReal const pz = refpart.pz;
245 amrex::ParticleReal const t = refpart.t;
246 amrex::ParticleReal const pt = refpart.pt;
247 amrex::ParticleReal const s = refpart.s;
248
249 // length of the current slice
250 amrex::ParticleReal const slice_ds = m_ds / nslice();
251
252 // treat the special case of zero field (drift)
253 if (m_rc == 0.0_prt) {
254 // advance position and momentum (drift)
255 amrex::ParticleReal const step = slice_ds /std::sqrt(powi<2>(pt)-1.0_prt);
256 refpart.x = x + step*px;
257 refpart.y = y + step*py;
258 refpart.z = z + step*pz;
259 refpart.t = t - step*pt;
260
261 } else {
262
263 // assign intermediate parameter
264 amrex::ParticleReal const theta = slice_ds/m_rc;
265 amrex::ParticleReal const B = std::sqrt(powi<2>(pt)-1.0_prt)/m_rc;
266
267 // calculate expensive terms once
268 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
269
270 // advance position and momentum (bend)
271 refpart.px = px*cos_theta - pz*sin_theta;
272 refpart.py = py;
273 refpart.pz = pz*cos_theta + px*sin_theta;
274 refpart.pt = pt;
275
276 refpart.x = x + (refpart.pz - pz)/B;
277 refpart.y = y + (theta/B)*py;
278 refpart.z = z - (refpart.px - px)/B;
279 refpart.t = t - (theta/B)*pt;
280
281 }
282
283 // advance integrated path length
284 refpart.s = s + slice_ds;
285
286 }
287
289 using LinearTransport::operator();
290
296 Map6x6
297 transport_map (RefPart const & AMREX_RESTRICT refpart) const
298 {
299 using namespace amrex::literals; // for _rt and _prt
300 using amrex::Math::powi;
301
302 // length of the current slice
303 amrex::ParticleReal const slice_ds = m_ds / nslice();
304
305 // access reference particle values to find beta*gamma^2
306 amrex::ParticleReal const pt_ref = refpart.pt;
307 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1.0_prt;
308 amrex::ParticleReal const bet = std::sqrt(betgam2/(1.0_prt + betgam2));
309
310 // initialize linear map matrix elements
312
313 // treat the special case of zero field
314 if (m_rc==0.0_prt) {
315 R(1,2) = slice_ds;
316 R(3,4) = slice_ds;
317 R(5,6) = slice_ds / betgam2;
318
319 } else {
320
321 // calculate expensive terms once
322 amrex::ParticleReal const theta = slice_ds/m_rc;
323 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
324
325 // assign linear map matrix elements
326 R(1,1) = cos_theta;
327 R(1,2) = m_rc*sin_theta;
328 R(1,6) = - (m_rc/bet)*(1.0_prt - cos_theta);
329 R(2,1) = -sin_theta/m_rc;
330 R(2,2) = cos_theta;
331 R(2,6) = - sin_theta/bet;
332 R(3,4) = m_rc*theta;
333 R(5,1) = sin_theta/bet;
334 R(5,2) = m_rc/bet*(1.0_prt - cos_theta);
335 R(5,6) = m_rc*(-theta+sin_theta/(bet*bet));
336
337 }
338
339 // apply the transverse rotation (roll) alignment error
340 return rotate_aligned_map(R);
341 }
342
357 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
360 T_Real & AMREX_RESTRICT x,
361 T_Real & AMREX_RESTRICT y,
362 T_Real & AMREX_RESTRICT t,
363 T_Real & AMREX_RESTRICT px,
364 T_Real const & AMREX_RESTRICT py,
365 T_Real const & AMREX_RESTRICT pt,
366 T_Real & AMREX_RESTRICT sx,
367 T_Real & AMREX_RESTRICT sy,
368 T_Real & AMREX_RESTRICT sz,
369 T_IdCpu const & AMREX_RESTRICT idcpu,
370 RefPart const & AMREX_RESTRICT refpart
371 ) const
372 {
373 using namespace amrex::literals; // for _rt and _prt
374
375 // initialize the three components of the axis-angle vector
376 T_Real lambdax = 0_prt;
377 T_Real lambday = 0_prt;
378 T_Real lambdaz = 0_prt;
379
380 // set the angle-axis generator based on the phase space variables
381 lambdax = m_A14 * py;
382 lambday = m_A21 * x + m_A22 * px + m_A26 * pt;
383 lambdaz = m_A34 * py;
384
385 // push the spin vector using the generator just determined
386 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
387
388 // axis-angle vector components generating the reference spin map
389 lambdax = 0.0_prt;
390 lambday = m_lambday;
391 lambdaz = 0.0_prt;
392
393 // push the spin vector using the generator just determined
394 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
395
396 // phase space push
397 (*this)(x, y, t, px, py, pt, idcpu, refpart);
398 }
399
401
402 private:
403 // constants that are independent of the individually tracked particle,
404 // see: compute_constants() to refresh
407 };
408
409} // namespace impactx
410
412
413#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
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_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition ReferenceParticle.H:167
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 Sbend.H:44
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 const &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 Sbend.H:359
ImpactXParticleContainer::ParticleType PType
Definition Sbend.H:46
amrex::ParticleReal m_R16
Definition Sbend.H:405
amrex::ParticleReal m_lambday
Definition Sbend.H:406
static constexpr auto type
Definition Sbend.H:45
amrex::ParticleReal m_A14
Definition Sbend.H:406
amrex::ParticleReal m_R11
bend radius in m
Definition Sbend.H:405
amrex::ParticleReal m_R21
Definition Sbend.H:405
Sbend(amrex::ParticleReal ds, amrex::ParticleReal rc, 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 Sbend.H:60
amrex::ParticleReal m_R34
Definition Sbend.H:405
amrex::ParticleReal m_R12
Definition Sbend.H:405
amrex::ParticleReal m_A22
Definition Sbend.H:406
amrex::ParticleReal m_rc
Definition Sbend.H:400
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 const &AMREX_RESTRICT py, T_Real const &AMREX_RESTRICT pt, T_IdCpu const &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition Sbend.H:171
amrex::ParticleReal m_R26
Definition Sbend.H:405
amrex::ParticleReal m_A34
Definition Sbend.H:406
void compute_constants(RefPart const &refpart)
Definition Sbend.H:103
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rc(RefPart const &refpart) const
Definition Sbend.H:81
amrex::ParticleReal m_A26
Definition Sbend.H:406
amrex::ParticleReal m_A21
Definition Sbend.H:406
void reverse()
Definition Sbend.H:91
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Sbend.H:297
amrex::ParticleReal m_R56
Definition Sbend.H:405
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