ImpactX
Loading...
Searching...
No Matches
ChrUniformAcc.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_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
30
31namespace impactx::elements
32{
33 struct ChrAcc
34 : public mixin::Named,
35 public mixin::BeamOptic<ChrAcc>,
36 public mixin::LinearTransport<ChrAcc, false>,
37 public mixin::Thick,
38 public mixin::Alignment,
40 public mixin::NoFinalize,
41 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
42 {
43 static constexpr auto type = "ChrAcc";
45
70 amrex::ParticleReal rotation_degree = 0,
73 int nslice = 1,
74 std::optional<std::string> name = std::nullopt
75 )
76 : Named(std::move(name)),
77 Thick(ds, nslice),
78 Alignment(dx, dy, rotation_degree),
80 m_ez(ez), m_bz(bz)
81 {
82 }
83
85 void reverse () { Thick::reverse(); }
86
88 using BeamOptic::operator();
89
97 void compute_constants (RefPart const & refpart)
98 {
99 using namespace amrex::literals; // for _rt and _prt
100 using amrex::Math::powi;
101
102 Alignment::compute_constants(refpart);
103
104 // length of the current slice
105 m_slice_ds = m_ds / nslice();
106
107 // access reference particle values (final, initial):
108 m_ptf_ref = refpart.pt;
110 m_bgf = std::sqrt(powi<2>(m_ptf_ref) - 1.0_prt);
111 m_bgi = std::sqrt(powi<2>(m_pti_ref) - 1.0_prt);
112
113 // compute focusing constant (1/m) and rotation angle (in rad)
114 m_alpha = m_bz * 0.5_prt;
116 }
117
131 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
134 T_Real & AMREX_RESTRICT x,
135 T_Real & AMREX_RESTRICT y,
136 T_Real & AMREX_RESTRICT t,
137 T_Real & AMREX_RESTRICT px,
138 T_Real & AMREX_RESTRICT py,
139 T_Real & AMREX_RESTRICT pt,
140 T_IdCpu & AMREX_RESTRICT idcpu,
141 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
142 ) const
143 {
144 using namespace amrex::literals; // for _rt and _prt
145 using amrex::Math::powi;
146 using namespace std; // for cmath(float)
147 namespace stdx = amrex::simd::stdx;
148
149 // shift due to alignment errors of the element
150 shift_in(x, y, px, py);
151
152 // initial conversion from static to dynamic units:
153 px = px * m_bgi;
154 py = py * m_bgi;
155 pt = pt * m_bgi;
156
157 // compute intermediate quantities related to acceleration
158 T_Real const pti_tot = m_pti_ref + pt;
159 T_Real const ptf_tot = m_ptf_ref + pt;
160 T_Real const pti_tot2 = powi<2>(pti_tot);
161 T_Real const ptf_tot2 = powi<2>(ptf_tot);
162
163 // check whether particle lies within the domain of map definition
164 auto const mask = pti_tot2 <= 1_prt || ptf_tot2 <= 1_prt;
166 {
167 T_Real const pzi_tot = sqrt(powi<2>(pti_tot) - 1_prt);
168 T_Real const pzf_tot = sqrt(powi<2>(ptf_tot) - 1_prt);
169 T_Real const pzi_ref = sqrt(powi<2>(m_pti_ref) - 1_prt);
170 T_Real const pzf_ref = sqrt(powi<2>(m_ptf_ref) - 1_prt);
171
172 T_Real const numer = -ptf_tot + pzf_tot;
173 T_Real const denom = -pti_tot + pzi_tot;
174
175 // compute focusing constant (1/m) and rotation angle (in rad)
176 T_Real const theta = m_alpha_iez * log(numer / denom);
177 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
178
179 // initialize output values
180 T_Real xout = x;
181 T_Real yout = y;
182 T_Real tout = t;
183 T_Real pxout = px;
184 T_Real pyout = py;
185 T_Real ptout = pt;
186
187 // advance positions and momenta using map for focusing
188 xout = cos_theta * x + sin_theta / m_alpha * px;
189 pxout = -m_alpha * sin_theta * x + cos_theta * px;
190
191 yout = cos_theta * y + sin_theta / m_alpha * py;
192 pyout = -m_alpha * sin_theta * y + cos_theta * py;
193
194 // the correct symplectic update for t
195 tout = t + (pzf_tot - pzf_ref - pzi_tot + pzi_ref) / m_ez;
196 tout += (1_prt/pzi_tot - 1_prt/pzf_tot)
197 * (powi<2>(py - m_alpha * x) +
198 powi<2>(px + m_alpha * y))
199 / (2_prt * m_ez);
200 ptout = pt;
201
202 // assign intermediate momenta
203 px = pxout;
204 py = pyout;
205 pt = ptout;
206
207 // advance positions and momenta using map for rotation
208 x = cos_theta * xout + sin_theta * yout;
209 pxout = cos_theta * px + sin_theta * py;
210
211 y = -sin_theta * xout + cos_theta * yout;
212 pyout = -sin_theta * px + cos_theta * py;
213
214 t = tout;
215 ptout = pt;
216
217 // assign updated momenta
218 px = pxout;
219 py = pyout;
220 pt = ptout;
221
222 }
223
224 // final conversion from dynamic to static units:
225 px = px / m_bgf;
226 py = py / m_bgf;
227 pt = pt / m_bgf;
228
229 // apply transverse aperture
230 apply_aperture(x, y, idcpu);
231
232 // undo shift due to alignment errors of the element
233 shift_out(x, y, px, py);
234 }
235
241 void operator() (RefPart & AMREX_RESTRICT refpart) const
242 {
243 using namespace amrex::literals; // for _rt and _prt
244 using amrex::Math::powi;
245
246 // assign input reference particle values
247 amrex::ParticleReal const x = refpart.x;
248 amrex::ParticleReal const px = refpart.px;
249 amrex::ParticleReal const y = refpart.y;
250 amrex::ParticleReal const py = refpart.py;
251 amrex::ParticleReal const z = refpart.z;
252 amrex::ParticleReal const pz = refpart.pz;
253 amrex::ParticleReal const t = refpart.t;
254 amrex::ParticleReal const pt = refpart.pt;
255 amrex::ParticleReal const s = refpart.s;
256
257 // length of the current slice
258 amrex::ParticleReal const slice_ds = m_ds / nslice();
259
260 // compute initial value of beta*gamma
261 amrex::ParticleReal const bgi = std::sqrt(powi<2>(pt) - 1.0_prt);
262
263 // advance pt (uniform acceleration)
264 refpart.pt = pt - m_ez*slice_ds;
265
266 // compute final value of beta*gamma
267 amrex::ParticleReal const ptf = refpart.pt;
268 amrex::ParticleReal const bgf = std::sqrt(powi<2>(ptf) - 1.0_prt);
269
270 // update t
271 refpart.t = t + (bgf - bgi)/m_ez;
272
273 // advance position (x,y,z)
274 refpart.x = x + slice_ds*px/bgi;
275 refpart.y = y + slice_ds*py/bgi;
276 refpart.z = z + slice_ds*pz/bgi;
277
278 // advance momentum (px,py,pz)
279 refpart.px = px*bgf/bgi;
280 refpart.py = py*bgf/bgi;
281 refpart.pz = pz*bgf/bgi;
282
283 // advance integrated path length
284 refpart.s = s + slice_ds;
285 }
286
288 using LinearTransport::operator();
289
296 Map6x6
297 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
298 {
299 throw std::runtime_error(std::string(type) + ": Envelope tracking is not yet implemented!");
300 return Map6x6::Identity();
301 }
302
305
306 private:
307 // constants that are independent of the individually tracked particle,
308 // see: compute_constants() to refresh
312 };
313
314} // namespace impactx
315
317
318#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
__host__ __device__ std::pair< double, double > sincos(double x)
constexpr T powi(T x) noexcept
__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: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
__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:42
amrex::ParticleReal m_alpha
Definition ChrUniformAcc.H:311
amrex::ParticleReal m_pti_ref
Definition ChrUniformAcc.H:310
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ChrUniformAcc.H:297
void compute_constants(RefPart const &refpart)
Definition ChrUniformAcc.H:97
amrex::ParticleReal m_ez
Definition ChrUniformAcc.H:303
amrex::ParticleReal m_bgi
Definition ChrUniformAcc.H:310
void reverse()
Definition ChrUniformAcc.H:85
amrex::ParticleReal m_ptf_ref
m_ds / nslice();
Definition ChrUniformAcc.H:310
amrex::ParticleReal m_alpha_iez
Definition ChrUniformAcc.H:311
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:133
amrex::ParticleReal m_slice_ds
magnetic field strength in 1/m
Definition ChrUniformAcc.H:309
ChrAcc(amrex::ParticleReal ds, amrex::ParticleReal ez, amrex::ParticleReal bz, 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 ChrUniformAcc.H:64
static constexpr auto type
Definition ChrUniformAcc.H:43
ImpactXParticleContainer::ParticleType PType
Definition ChrUniformAcc.H:44
amrex::ParticleReal m_bgf
Definition ChrUniformAcc.H:310
amrex::ParticleReal m_bz
electric field strength in 1/m
Definition ChrUniformAcc.H:304
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 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