ImpactX
Loading...
Searching...
No Matches
Sol.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_SOL_H
11#define IMPACTX_SOL_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#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#include <stdexcept>
30
31
32namespace impactx::elements
33{
34 struct Sol
35 : public mixin::Named,
36 public mixin::BeamOptic<Sol>,
37 public mixin::LinearTransport<Sol>,
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 = "Sol";
49
68 amrex::ParticleReal rotation_degree = 0,
71 int nslice = 1,
72 std::optional<std::string> name = std::nullopt
73 )
74 : Named(std::move(name)),
75 Thick(ds, nslice),
76 Alignment(dx, dy, rotation_degree),
78 m_ks(ks)
79 {
80 }
81
83 void reverse () { Thick::reverse(); }
84
86 using BeamOptic::operator();
87
95 void compute_constants (RefPart const & refpart)
96 {
97 using namespace amrex::literals; // for _rt and _prt
99
100 Alignment::compute_constants(refpart);
101
102 // length of the current slice
103 amrex::ParticleReal const slice_ds = m_ds / nslice();
104
105 // access reference particle values to find beta*gamma^2
106 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
107 m_ibeta = 1.0_prt / refpart.beta();
108
109 // compute phase advance per unit length (in rad/m) and
110 // rotation angle (in rad)
111 amrex::ParticleReal const alpha = m_ks * 0.5_prt;
112 amrex::ParticleReal const theta = alpha*slice_ds;
113 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
114
115 m_sin_theta = sin_theta;
116 m_cos_theta = cos_theta;
117 m_sin_theta_ialpha = sin_theta / alpha;
118 m_neg_alpha_sin_theta = -alpha * sin_theta;
119 m_ds_bg2 = slice_ds / betgam2;
120
121 // access reference particle values required for spin push
123 amrex::ParticleReal const gamma = refpart.gamma();
124 m_rel_const = 0.5_prt * (gamma - 1_prt);
125 auto const [sin_G, cos_G] = amrex::Math::sincos(2_prt * G * theta);
126 m_sin_G = sin_G;
127 m_1cos_G = 1_prt - cos_G;
128 m_neg2_theta_1pG = -2_prt * theta * (1_prt + G);
129
130 }
131
145 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
148 T_Real & AMREX_RESTRICT x,
149 T_Real & AMREX_RESTRICT y,
150 T_Real & AMREX_RESTRICT t,
151 T_Real & AMREX_RESTRICT px,
152 T_Real & AMREX_RESTRICT py,
153 T_Real & AMREX_RESTRICT pt,
154 T_IdCpu & AMREX_RESTRICT idcpu,
155 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
156 ) const
157 {
158 using namespace amrex::literals; // for _rt and _prt
159
160 // shift due to alignment errors of the element
161 shift_in(x, y, px, py);
162
163 // initialize output values
164 T_Real xout = x;
165 T_Real yout = y;
166 T_Real tout = t;
167 T_Real pxout = px;
168 T_Real pyout = py;
169 T_Real ptout = pt;
170
171 // advance positions and momenta using map for focusing
172 xout = m_cos_theta * x
173 + m_sin_theta_ialpha * px;
174 pxout = m_neg_alpha_sin_theta * x
175 + m_cos_theta * px;
176
177 yout = m_cos_theta * y
178 + m_sin_theta_ialpha * py;
179 pyout = m_neg_alpha_sin_theta * y
180 + m_cos_theta * py;
181
182 tout = t + m_ds_bg2 * pt;
183 ptout = pt;
184
185 // assign intermediate momenta
186 px = pxout;
187 py = pyout;
188 pt = ptout;
189
190 // advance positions and momenta using map for rotation
191 x = m_cos_theta * xout
192 + m_sin_theta * yout;
193 pxout = m_cos_theta * px
194 + m_sin_theta * py;
195
196 y = -m_sin_theta * xout
197 + m_cos_theta * yout;
198 pyout = -m_sin_theta * px
199 + m_cos_theta * py;
200
201 t = tout;
202 ptout = pt;
203
204 // assign updated values
205 px = pxout;
206 py = pyout;
207 pt = ptout;
208
209 // apply transverse aperture
210 apply_aperture(x, y, idcpu);
211
212 // undo shift due to alignment errors of the element
213 shift_out(x, y, px, py);
214 }
215
221 void operator() (RefPart & AMREX_RESTRICT refpart) const
222 {
223 using namespace amrex::literals; // for _rt and _prt
224 using amrex::Math::powi;
225
226 // assign input reference particle values
227 amrex::ParticleReal const x = refpart.x;
228 amrex::ParticleReal const px = refpart.px;
229 amrex::ParticleReal const y = refpart.y;
230 amrex::ParticleReal const py = refpart.py;
231 amrex::ParticleReal const z = refpart.z;
232 amrex::ParticleReal const pz = refpart.pz;
233 amrex::ParticleReal const t = refpart.t;
234 amrex::ParticleReal const pt = refpart.pt;
235 amrex::ParticleReal const s = refpart.s;
236
237 // length of the current slice
238 amrex::ParticleReal const slice_ds = m_ds / nslice();
239
240 // assign intermediate parameter
241 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
242
243 // advance position and momentum (straight element)
244 refpart.x = x + step*px;
245 refpart.y = y + step*py;
246 refpart.z = z + step*pz;
247 refpart.t = t - step*pt;
248
249 // advance integrated path length
250 refpart.s = s + slice_ds;
251 }
252
253
268 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
271 [[maybe_unused]] T_Real & AMREX_RESTRICT x,
272 [[maybe_unused]] T_Real & AMREX_RESTRICT y,
273 [[maybe_unused]] T_Real & AMREX_RESTRICT t,
274 [[maybe_unused]] T_Real & AMREX_RESTRICT px,
275 [[maybe_unused]] T_Real & AMREX_RESTRICT py,
276 [[maybe_unused]] T_Real & AMREX_RESTRICT pt,
277 [[maybe_unused]] T_Real & AMREX_RESTRICT sx,
278 [[maybe_unused]] T_Real & AMREX_RESTRICT sy,
279 [[maybe_unused]] T_Real & AMREX_RESTRICT sz,
280 [[maybe_unused]] T_IdCpu & AMREX_RESTRICT idcpu,
281 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
282 ) const
283 {
284 using namespace amrex::literals; // for _rt and _prt
285
286 // initialize the three components of the axis-angle vector
287 T_Real lambdax = 0_prt;
288 T_Real lambday = 0_prt;
289 T_Real lambdaz = 0_prt;
290
291 // axis-angle vector components generating the remainder spin map
292 lambdax = m_rel_const * ((2_prt*px + m_ks*y)*m_sin_G - (2_prt*py - m_ks*x)*m_1cos_G);
293 lambday = m_rel_const * ((2_prt*py - m_ks*x)*m_sin_G + (2_prt*px + m_ks*y)*m_1cos_G);
294 lambdaz = m_neg2_theta_1pG * pt * m_ibeta;
295
296 // push the spin vector using the generator just determined
297 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
298
299 // axis-angle vector components generating the reference spin map
300 lambdax = 0.0_prt;
301 lambday = 0.0_prt;
302 lambdaz = m_neg2_theta_1pG;
303
304 // push the spin vector using the generator just determined
305 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
306
307 // phase space push
308 (*this)(x, y, t, px, py, pt, idcpu, refpart);
309
310 }
311
312
314 using LinearTransport::operator();
315
322 Map6x6
323 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
324 {
325 using namespace amrex::literals; // for _rt and _prt
326 using amrex::Math::powi;
327
328 // length of the current slice
329 amrex::ParticleReal const slice_ds = m_ds / nslice();
330
331 // initialize linear map matrix elements
333 Map6x6 Rrotation = Map6x6::Identity();
334 Map6x6 Rfocusing = Map6x6::Identity();
335
336 // access reference particle values to find beta*gamma^2
337 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
338
339 // compute phase advance per unit length (in rad/m) and
340 // rotation angle (in rad)
341 amrex::ParticleReal const alpha = m_ks * 0.5_prt;
342 amrex::ParticleReal const theta = alpha*slice_ds;
343 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
344
345 amrex::ParticleReal const sin_theta_ialpha = sin_theta / alpha;
346 amrex::ParticleReal const neg_alpha_sin_theta = -alpha * sin_theta;
347 amrex::ParticleReal const ds_bg2 = slice_ds / betgam2;
348
349 Rfocusing(1,1) = cos_theta;
350 Rfocusing(1,2) = sin_theta_ialpha;
351 Rfocusing(2,1) = neg_alpha_sin_theta;
352 Rfocusing(2,2) = cos_theta;
353 Rfocusing(3,3) = cos_theta;
354 Rfocusing(3,4) = sin_theta_ialpha;
355 Rfocusing(4,3) = neg_alpha_sin_theta;
356 Rfocusing(4,4) = cos_theta;
357 Rfocusing(5,6) = ds_bg2;
358
359 Rrotation(1,1) = cos_theta;
360 Rrotation(1,3) = sin_theta;
361 Rrotation(2,2) = cos_theta;
362 Rrotation(2,4) = sin_theta;
363 Rrotation(3,1) = -sin_theta;
364 Rrotation(3,3) = cos_theta;
365 Rrotation(4,2) = -sin_theta;
366 Rrotation(4,4) = cos_theta;
367
368 R = Rrotation * Rfocusing;
369
370 return R;
371 }
372
374
375 private:
376 // constants that are independent of the individually tracked particle,
377 // see: compute_constants() to refresh
380 };
381
382} // namespace impactx
383
385
386#endif // IMPACTX_SOL_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 Sol.H:46
amrex::ParticleReal m_ibeta
Definition Sol.H:379
amrex::ParticleReal m_ds_bg2
solenoid strength in 1/m
Definition Sol.H:378
Sol(amrex::ParticleReal ds, amrex::ParticleReal ks, 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 Sol.H:63
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 Sol.H:270
ImpactXParticleContainer::ParticleType PType
Definition Sol.H:48
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Sol.H:323
void reverse()
Definition Sol.H:83
static constexpr auto type
Definition Sol.H:47
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 Sol.H:147
amrex::ParticleReal m_rel_const
Definition Sol.H:379
amrex::ParticleReal m_neg2_theta_1pG
Definition Sol.H:379
amrex::ParticleReal m_ks
Definition Sol.H:373
amrex::ParticleReal m_cos_theta
Definition Sol.H:378
amrex::ParticleReal m_sin_G
Definition Sol.H:379
amrex::ParticleReal m_sin_theta
Definition Sol.H:378
amrex::ParticleReal m_sin_theta_ialpha
Definition Sol.H:378
amrex::ParticleReal m_neg_alpha_sin_theta
Definition Sol.H:378
void compute_constants(RefPart const &refpart)
Definition Sol.H:95
amrex::ParticleReal m_1cos_G
Definition Sol.H:379
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