ImpactX
Loading...
Searching...
No Matches
ChrUniformAcc.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_CHRACC_H
11#define IMPACTX_CHRACC_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/named.H"
20#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#include <stdexcept>
29
30namespace impactx::elements
31{
32 struct ChrAcc
33 : public mixin::Named,
34 public mixin::BeamOptic<ChrAcc>,
35 public mixin::LinearTransport<ChrAcc>,
36 public mixin::Thick,
37 public mixin::Alignment,
39 public mixin::NoFinalize,
40 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
41 {
42 static constexpr auto type = "ChrAcc";
44
82
84 void reverse () { Thick::reverse(); }
85
87 using BeamOptic::operator();
88
96 void compute_constants (RefPart const & refpart)
97 {
98 using namespace amrex::literals; // for _rt and _prt
100
101 Alignment::compute_constants(refpart);
102
103 // length of the current slice
104 m_slice_ds = m_ds / nslice();
105
106 // access reference particle values (final, initial):
107 m_ptf_ref = refpart.pt;
109 m_bgf = std::sqrt(powi<2>(m_ptf_ref) - 1.0_prt);
110 m_bgi = std::sqrt(powi<2>(m_pti_ref) - 1.0_prt);
111
112 // compute focusing constant (1/m) and rotation angle (in rad)
113 m_alpha = m_bz * 0.5_prt;
115 }
116
130 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
133 T_Real & AMREX_RESTRICT x,
134 T_Real & AMREX_RESTRICT y,
135 T_Real & AMREX_RESTRICT t,
136 T_Real & AMREX_RESTRICT px,
137 T_Real & AMREX_RESTRICT py,
138 T_Real & AMREX_RESTRICT pt,
139 T_IdCpu & AMREX_RESTRICT idcpu,
140 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
141 ) const
142 {
143 using namespace amrex::literals; // for _rt and _prt
144 using amrex::Math::powi;
145 using namespace std; // for cmath(float)
146 namespace stdx = amrex::simd::stdx;
147
148 // initial conversion from static to dynamic units:
149 px = px * m_bgi;
150 py = py * m_bgi;
151 pt = pt * m_bgi;
152
153 // compute intermediate quantities related to acceleration
154 T_Real const pti_tot = m_pti_ref + pt;
155 T_Real const ptf_tot = m_ptf_ref + pt;
156 T_Real const pti_tot2 = powi<2>(pti_tot);
157 T_Real const ptf_tot2 = powi<2>(ptf_tot);
158
159 // check whether particle lies within the domain of map definition
160 auto const mask = pti_tot2 <= 1_prt || ptf_tot2 <= 1_prt;
162 {
163 T_Real const pzi_tot = sqrt(powi<2>(pti_tot) - 1_prt);
164 T_Real const pzf_tot = sqrt(powi<2>(ptf_tot) - 1_prt);
165 T_Real const pzi_ref = sqrt(powi<2>(m_pti_ref) - 1_prt);
166 T_Real const pzf_ref = sqrt(powi<2>(m_ptf_ref) - 1_prt);
167
168 T_Real const numer = -ptf_tot + pzf_tot;
169 T_Real const denom = -pti_tot + pzi_tot;
170
171 // compute focusing constant (1/m) and rotation angle (in rad)
172 T_Real const theta = m_alpha_iez * log(numer / denom);
173 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
174
175 // initialize output values
176 T_Real xout = x;
177 T_Real yout = y;
178 T_Real tout = t;
179 T_Real pxout = px;
180 T_Real pyout = py;
181 T_Real ptout = pt;
182
183 // advance positions and momenta using map for focusing
184 xout = cos_theta * x + sin_theta / m_alpha * px;
185 pxout = -m_alpha * sin_theta * x + cos_theta * px;
186
187 yout = cos_theta * y + sin_theta / m_alpha * py;
188 pyout = -m_alpha * sin_theta * y + cos_theta * py;
189
190 // the correct symplectic update for t
191 tout = t + (pzf_tot - pzf_ref - pzi_tot + pzi_ref) / m_ez;
192 tout += (1_prt/pzi_tot - 1_prt/pzf_tot)
193 * (powi<2>(py - m_alpha * x) +
194 powi<2>(px + m_alpha * y))
195 / (2_prt * m_ez);
196 ptout = pt;
197
198 // assign intermediate momenta
199 px = pxout;
200 py = pyout;
201 pt = ptout;
202
203 // advance positions and momenta using map for rotation
204 x = cos_theta * xout + sin_theta * yout;
205 pxout = cos_theta * px + sin_theta * py;
206
207 y = -sin_theta * xout + cos_theta * yout;
208 pyout = -sin_theta * px + cos_theta * py;
209
210 t = tout;
211 ptout = pt;
212
213 // assign updated momenta
214 px = pxout;
215 py = pyout;
216 pt = ptout;
217
218 }
219
220 // final conversion from dynamic to static units:
221 px = px / m_bgf;
222 py = py / m_bgf;
223 pt = pt / m_bgf;
224 }
225
231 void operator() (RefPart & AMREX_RESTRICT refpart) const
232 {
233 using namespace amrex::literals; // for _rt and _prt
234 using amrex::Math::powi;
235
236 // assign input reference particle values
237 amrex::ParticleReal const x = refpart.x;
238 amrex::ParticleReal const px = refpart.px;
239 amrex::ParticleReal const y = refpart.y;
240 amrex::ParticleReal const py = refpart.py;
241 amrex::ParticleReal const z = refpart.z;
242 amrex::ParticleReal const pz = refpart.pz;
243 amrex::ParticleReal const t = refpart.t;
244 amrex::ParticleReal const pt = refpart.pt;
245 amrex::ParticleReal const s = refpart.s;
246
247 // length of the current slice
248 amrex::ParticleReal const slice_ds = m_ds / nslice();
249
250 // compute initial value of beta*gamma
251 amrex::ParticleReal const bgi = std::sqrt(powi<2>(pt) - 1.0_prt);
252
253 // advance pt (uniform acceleration)
254 refpart.pt = pt - m_ez*slice_ds;
255
256 // compute final value of beta*gamma
257 amrex::ParticleReal const ptf = refpart.pt;
258 amrex::ParticleReal const bgf = std::sqrt(powi<2>(ptf) - 1.0_prt);
259
260 // update t
261 refpart.t = t + (bgf - bgi)/m_ez;
262
263 // advance position (x,y,z)
264 refpart.x = x + slice_ds*px/bgi;
265 refpart.y = y + slice_ds*py/bgi;
266 refpart.z = z + slice_ds*pz/bgi;
267
268 // advance momentum (px,py,pz)
269 refpart.px = px*bgf/bgi;
270 refpart.py = py*bgf/bgi;
271 refpart.pz = pz*bgf/bgi;
272
273 // advance integrated path length
274 refpart.s = s + slice_ds;
275 }
276
278 using LinearTransport::operator();
279
337 Map6x6
338 transport_map (RefPart const & AMREX_RESTRICT refpart) const
339 {
340 using namespace amrex::literals; // for _rt and _prt
341 using amrex::Math::powi;
342
343 // slice length and initial/final reference pt:
344 // transport_map is called after the reference advance, so
345 // refpart.pt holds the post-slice (final) pt.
346 amrex::ParticleReal const slice_ds = m_ds / nslice();
347 amrex::ParticleReal const ptf_ref = refpart.pt;
348 amrex::ParticleReal const pti_ref = ptf_ref + m_ez * slice_ds;
349
350 amrex::ParticleReal const pzf_ref = std::sqrt(powi<2>(ptf_ref) - 1.0_prt);
351 amrex::ParticleReal const pzi_ref = std::sqrt(powi<2>(pti_ref) - 1.0_prt);
352 amrex::ParticleReal const bgf = pzf_ref; // |beta*gamma|_final
353 amrex::ParticleReal const bgi = pzi_ref; // |beta*gamma|_initial
354
355 // Solenoid focusing constant alpha = Bz / 2. The "log"
356 // factor is the integrated drift length in dynamic units and
357 // is well-defined even when alpha = 0 (no solenoid).
358 amrex::ParticleReal const alpha = 0.5_prt * m_bz;
359 amrex::ParticleReal const drift_log =
360 std::log((pzf_ref - ptf_ref) / (pzi_ref - pti_ref)) / m_ez;
361 amrex::ParticleReal const rbg = bgi / bgf;
362
364
365 // t couples to pt only: symplectic correction to the
366 // time-of-flight, linearized at pt = 0.
367 R(5,6) = bgi * (ptf_ref / pzf_ref - pti_ref / pzi_ref) / m_ez;
368 // pt scales by the static<->dynamic unit change bgi/bgf.
369 R(6,6) = rbg;
370
371 if (alpha == 0.0_prt)
372 {
373 // No solenoid: pure accelerating "drift" with adiabatic
374 // damping on the transverse momenta.
375 R(1,2) = bgi * drift_log;
376 R(2,2) = rbg;
377 R(3,4) = bgi * drift_log;
378 R(4,4) = rbg;
379 }
380 else
381 {
382 // Solenoid + acceleration: rotation * focusing, bracketed
383 // by static<->dynamic unit conversions on (px, py).
384 amrex::ParticleReal const theta0 = alpha * drift_log;
385 auto const [s, c] = amrex::Math::sincos(theta0);
386
387 amrex::ParticleReal const c2 = c * c;
388 amrex::ParticleReal const s2 = s * s;
389 amrex::ParticleReal const cs = c * s;
390
391 R(1,1) = c2; R(1,2) = bgi * cs / alpha;
392 R(1,3) = cs; R(1,4) = bgi * s2 / alpha;
393 R(2,1) = -alpha * cs / bgf; R(2,2) = rbg * c2;
394 R(2,3) = -alpha * s2 / bgf; R(2,4) = rbg * cs;
395 R(3,1) = -cs; R(3,2) = -bgi * s2 / alpha;
396 R(3,3) = c2; R(3,4) = bgi * cs / alpha;
397 R(4,1) = alpha * s2 / bgf; R(4,2) = -rbg * cs;
398 R(4,3) = -alpha * cs / bgf; R(4,4) = rbg * c2;
399 }
400
401 // apply the transverse rotation (roll) alignment error
402 return rotate_aligned_map(R);
403 }
404
407
408 private:
409 // constants that are independent of the individually tracked particle,
410 // see: compute_constants() to refresh
414 };
415
416} // namespace impactx
417
419
420#endif // IMPACTX_CHRACC_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
Array4< int const > mask
#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)
__host__ __device__ GpuComplex< T > log(const GpuComplex< T > &a_z) noexcept
__host__ __device__ GpuComplex< T > sqrt(const GpuComplex< T > &a_z) noexcept
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
__host__ __device__ void make_invalid() const noexcept
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
Definition ChrUniformAcc.H:41
amrex::ParticleReal m_alpha
Definition ChrUniformAcc.H:413
amrex::ParticleReal m_pti_ref
Definition ChrUniformAcc.H:412
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ChrUniformAcc.H:338
void compute_constants(RefPart const &refpart)
Definition ChrUniformAcc.H:96
amrex::ParticleReal m_ez
Definition ChrUniformAcc.H:405
amrex::ParticleReal m_bgi
Definition ChrUniformAcc.H:412
void reverse()
Definition ChrUniformAcc.H:84
amrex::ParticleReal m_ptf_ref
m_ds / nslice();
Definition ChrUniformAcc.H:412
amrex::ParticleReal m_alpha_iez
Definition ChrUniformAcc.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 &AMREX_RESTRICT pt, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition ChrUniformAcc.H:132
amrex::ParticleReal m_slice_ds
magnetic field strength in 1/m
Definition ChrUniformAcc.H:411
ChrAcc(amrex::ParticleReal ds, amrex::ParticleReal ez, amrex::ParticleReal bz, 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 ChrUniformAcc.H:63
static constexpr auto type
Definition ChrUniformAcc.H:42
ImpactXParticleContainer::ParticleType PType
Definition ChrUniformAcc.H:43
amrex::ParticleReal m_bgf
Definition ChrUniformAcc.H:412
amrex::ParticleReal m_bz
electric field strength in 1/m
Definition ChrUniformAcc.H:406
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 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