ImpactX
Loading...
Searching...
No Matches
Sol.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_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
31namespace impactx::elements
32{
33 struct Sol
34 : public mixin::Named,
35 public mixin::BeamOptic<Sol>,
36 public mixin::LinearTransport<Sol>,
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 = "Sol";
48
80
82 void reverse () { Thick::reverse(); }
83
85 using BeamOptic::operator();
86
94 void compute_constants (RefPart const & refpart)
95 {
96 using namespace amrex::literals; // for _rt and _prt
98
99 Alignment::compute_constants(refpart);
100
101 // length of the current slice
102 amrex::ParticleReal const slice_ds = m_ds / nslice();
103
104 // access reference particle values to find beta*gamma^2
105 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
106 m_ibeta = 1.0_prt / refpart.beta();
107
108 // compute phase advance per unit length (in rad/m) and
109 // rotation angle (in rad)
110 amrex::ParticleReal const alpha = m_ks * 0.5_prt;
111 amrex::ParticleReal const theta = alpha*slice_ds;
112 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
113
114 m_sin_theta = sin_theta;
115 m_cos_theta = cos_theta;
116 m_sin_theta_ialpha = sin_theta / alpha;
117 m_neg_alpha_sin_theta = -alpha * sin_theta;
118 m_ds_bg2 = slice_ds / betgam2;
119
120 // access reference particle values required for spin push
122 amrex::ParticleReal const gamma = refpart.gamma();
123 m_rel_const = 0.5_prt * (gamma - 1_prt);
124 auto const [sin_G, cos_G] = amrex::Math::sincos(2_prt * G * theta);
125 m_sin_G = sin_G;
126 m_1cos_G = 1_prt - cos_G;
127 m_neg2_theta_1pG = -2_prt * theta * (1_prt + G);
128
129 }
130
144 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
147 T_Real & AMREX_RESTRICT x,
148 T_Real & AMREX_RESTRICT y,
149 T_Real & AMREX_RESTRICT t,
150 T_Real & AMREX_RESTRICT px,
151 T_Real & AMREX_RESTRICT py,
152 T_Real const & AMREX_RESTRICT pt,
153 [[maybe_unused]] T_IdCpu const & AMREX_RESTRICT idcpu,
154 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
155 ) const
156 {
157 using namespace amrex::literals; // for _rt and _prt
158
159 // initialize output values
160 T_Real xout = x;
161 T_Real yout = y;
162 T_Real tout = t;
163 T_Real pxout = px;
164 T_Real pyout = py;
165 // T_Real ptout = pt;
166
167 // advance positions and momenta using map for focusing
168 xout = m_cos_theta * x
169 + m_sin_theta_ialpha * px;
170 pxout = m_neg_alpha_sin_theta * x
171 + m_cos_theta * px;
172
173 yout = m_cos_theta * y
174 + m_sin_theta_ialpha * py;
175 pyout = m_neg_alpha_sin_theta * y
176 + m_cos_theta * py;
177
178 tout = t + m_ds_bg2 * pt;
179 // ptout = pt;
180
181 // assign intermediate momenta
182 px = pxout;
183 py = pyout;
184 // pt = ptout;
185
186 // advance positions and momenta using map for rotation
187 x = m_cos_theta * xout
188 + m_sin_theta * yout;
189 pxout = m_cos_theta * px
190 + m_sin_theta * py;
191
192 y = -m_sin_theta * xout
193 + m_cos_theta * yout;
194 pyout = -m_sin_theta * px
195 + m_cos_theta * py;
196
197 t = tout;
198 // ptout = pt;
199
200 // assign updated values
201 px = pxout;
202 py = pyout;
203 // pt = ptout;
204 }
205
211 void operator() (RefPart & AMREX_RESTRICT refpart) const
212 {
213 using namespace amrex::literals; // for _rt and _prt
214 using amrex::Math::powi;
215
216 // assign input reference particle values
217 amrex::ParticleReal const x = refpart.x;
218 amrex::ParticleReal const px = refpart.px;
219 amrex::ParticleReal const y = refpart.y;
220 amrex::ParticleReal const py = refpart.py;
221 amrex::ParticleReal const z = refpart.z;
222 amrex::ParticleReal const pz = refpart.pz;
223 amrex::ParticleReal const t = refpart.t;
224 amrex::ParticleReal const pt = refpart.pt;
225 amrex::ParticleReal const s = refpart.s;
226
227 // length of the current slice
228 amrex::ParticleReal const slice_ds = m_ds / nslice();
229
230 // assign intermediate parameter
231 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
232
233 // advance position and momentum (straight element)
234 refpart.x = x + step*px;
235 refpart.y = y + step*py;
236 refpart.z = z + step*pz;
237 refpart.t = t - step*pt;
238
239 // advance integrated path length
240 refpart.s = s + slice_ds;
241 }
242
257 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
260 T_Real & AMREX_RESTRICT x,
261 T_Real & AMREX_RESTRICT y,
262 T_Real & AMREX_RESTRICT t,
263 T_Real & AMREX_RESTRICT px,
264 T_Real & AMREX_RESTRICT py,
265 T_Real const & AMREX_RESTRICT pt,
266 T_Real & AMREX_RESTRICT sx,
267 T_Real & AMREX_RESTRICT sy,
268 T_Real & AMREX_RESTRICT sz,
269 T_IdCpu const & AMREX_RESTRICT idcpu,
270 RefPart const & AMREX_RESTRICT refpart
271 ) const
272 {
273 using namespace amrex::literals; // for _rt and _prt
274
275 // initialize the three components of the axis-angle vector
276 T_Real lambdax = 0_prt;
277 T_Real lambday = 0_prt;
278 T_Real lambdaz = 0_prt;
279
280 // axis-angle vector components generating the remainder spin map
281 lambdax = m_rel_const * ((2_prt*px + m_ks*y)*m_sin_G - (2_prt*py - m_ks*x)*m_1cos_G);
282 lambday = m_rel_const * ((2_prt*py - m_ks*x)*m_sin_G + (2_prt*px + m_ks*y)*m_1cos_G);
283 lambdaz = m_neg2_theta_1pG * pt * m_ibeta;
284
285 // push the spin vector using the generator just determined
286 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
287
288 // axis-angle vector components generating the reference spin map
289 lambdax = 0.0_prt;
290 lambday = 0.0_prt;
291 lambdaz = m_neg2_theta_1pG;
292
293 // push the spin vector using the generator just determined
294 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
295
296 // phase space push
297 (*this)(x, y, t, px, py, pt, idcpu, refpart);
298
299 }
300
302 using LinearTransport::operator();
303
310 Map6x6
311 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
312 {
313 using namespace amrex::literals; // for _rt and _prt
314 using amrex::Math::powi;
315
316 // length of the current slice
317 amrex::ParticleReal const slice_ds = m_ds / nslice();
318
319 // initialize linear map matrix elements
321 Map6x6 Rrotation = Map6x6::Identity();
322 Map6x6 Rfocusing = Map6x6::Identity();
323
324 // access reference particle values to find beta*gamma^2
325 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
326
327 // compute phase advance per unit length (in rad/m) and
328 // rotation angle (in rad)
329 amrex::ParticleReal const alpha = m_ks * 0.5_prt;
330 amrex::ParticleReal const theta = alpha*slice_ds;
331 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
332
333 amrex::ParticleReal const sin_theta_ialpha = sin_theta / alpha;
334 amrex::ParticleReal const neg_alpha_sin_theta = -alpha * sin_theta;
335 amrex::ParticleReal const ds_bg2 = slice_ds / betgam2;
336
337 Rfocusing(1,1) = cos_theta;
338 Rfocusing(1,2) = sin_theta_ialpha;
339 Rfocusing(2,1) = neg_alpha_sin_theta;
340 Rfocusing(2,2) = cos_theta;
341 Rfocusing(3,3) = cos_theta;
342 Rfocusing(3,4) = sin_theta_ialpha;
343 Rfocusing(4,3) = neg_alpha_sin_theta;
344 Rfocusing(4,4) = cos_theta;
345 Rfocusing(5,6) = ds_bg2;
346
347 Rrotation(1,1) = cos_theta;
348 Rrotation(1,3) = sin_theta;
349 Rrotation(2,2) = cos_theta;
350 Rrotation(2,4) = sin_theta;
351 Rrotation(3,1) = -sin_theta;
352 Rrotation(3,3) = cos_theta;
353 Rrotation(4,2) = -sin_theta;
354 Rrotation(4,4) = cos_theta;
355
356 R = Rrotation * Rfocusing;
357
358 // apply the transverse rotation (roll) alignment error
359 return rotate_aligned_map(R);
360 }
361
363
364 private:
365 // constants that are independent of the individually tracked particle,
366 // see: compute_constants() to refresh
369 };
370
371} // namespace impactx
372
374
375#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
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 Sol.H:45
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 Sol.H:259
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 Sol.H:146
amrex::ParticleReal m_ibeta
Definition Sol.H:368
amrex::ParticleReal m_ds_bg2
solenoid strength in 1/m
Definition Sol.H:367
ImpactXParticleContainer::ParticleType PType
Definition Sol.H:47
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Sol.H:311
void reverse()
Definition Sol.H:82
static constexpr auto type
Definition Sol.H:46
amrex::ParticleReal m_rel_const
Definition Sol.H:368
amrex::ParticleReal m_neg2_theta_1pG
Definition Sol.H:368
amrex::ParticleReal m_ks
Definition Sol.H:362
amrex::ParticleReal m_cos_theta
Definition Sol.H:367
Sol(amrex::ParticleReal ds, amrex::ParticleReal ks, 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 Sol.H:62
amrex::ParticleReal m_sin_G
Definition Sol.H:368
amrex::ParticleReal m_sin_theta
Definition Sol.H:367
amrex::ParticleReal m_sin_theta_ialpha
Definition Sol.H:367
amrex::ParticleReal m_neg_alpha_sin_theta
Definition Sol.H:367
void compute_constants(RefPart const &refpart)
Definition Sol.H:94
amrex::ParticleReal m_1cos_G
Definition Sol.H:368
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