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
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 Sol
34 : public mixin::Named,
35 public mixin::BeamOptic<Sol>,
36 public mixin::LinearTransport<Sol>,
37 public mixin::Thick,
38 public mixin::Alignment,
41 // At least on Intel AVX512, there is a small overhead to vectorize this element, 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 = "Sol";
47
62 amrex::ParticleReal ds,
63 amrex::ParticleReal ks,
64 amrex::ParticleReal dx = 0,
65 amrex::ParticleReal dy = 0,
66 amrex::ParticleReal rotation_degree = 0,
67 amrex::ParticleReal aperture_x = 0,
68 amrex::ParticleReal aperture_y = 0,
69 int nslice = 1,
70 std::optional<std::string> name = std::nullopt
71 )
72 : Named(std::move(name)),
73 Thick(ds, nslice),
74 Alignment(dx, dy, rotation_degree),
76 m_ks(ks)
77 {
78 }
79
81 using BeamOptic::operator();
82
90 void compute_constants (RefPart const & refpart)
91 {
92 using namespace amrex::literals; // for _rt and _prt
94
95 Alignment::compute_constants(refpart);
96
97 // length of the current slice
98 amrex::ParticleReal const slice_ds = m_ds / nslice();
99
100 // access reference particle values to find beta*gamma^2
101 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
102
103 // compute phase advance per unit length (in rad/m) and
104 // rotation angle (in rad)
105 amrex::ParticleReal const alpha = m_ks * 0.5_prt;
106 amrex::ParticleReal const theta = alpha*slice_ds;
107 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
108
109 m_sin_theta = sin_theta;
110 m_cos_theta = cos_theta;
111 m_sin_theta_ialpha = sin_theta / alpha;
112 m_neg_alpha_sin_theta = -alpha * sin_theta;
113 m_ds_bg2 = slice_ds / betgam2;
114 }
115
129 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
132 T_Real & AMREX_RESTRICT x,
133 T_Real & AMREX_RESTRICT y,
134 T_Real & AMREX_RESTRICT t,
135 T_Real & AMREX_RESTRICT px,
136 T_Real & AMREX_RESTRICT py,
137 T_Real & AMREX_RESTRICT pt,
138 T_IdCpu & AMREX_RESTRICT idcpu,
139 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
140 ) const
141 {
142 using namespace amrex::literals; // for _rt and _prt
143
144 // shift due to alignment errors of the element
145 shift_in(x, y, px, py);
146
147 // initialize output values
148 T_Real xout = x;
149 T_Real yout = y;
150 T_Real tout = t;
151 T_Real pxout = px;
152 T_Real pyout = py;
153 T_Real ptout = pt;
154
155 // advance positions and momenta using map for focusing
156 xout = m_cos_theta * x
157 + m_sin_theta_ialpha * px;
158 pxout = m_neg_alpha_sin_theta * x
159 + m_cos_theta * px;
160
161 yout = m_cos_theta * y
162 + m_sin_theta_ialpha * py;
163 pyout = m_neg_alpha_sin_theta * y
164 + m_cos_theta * py;
165
166 tout = t + m_ds_bg2 * pt;
167 ptout = pt;
168
169 // assign intermediate momenta
170 px = pxout;
171 py = pyout;
172 pt = ptout;
173
174 // advance positions and momenta using map for rotation
175 x = m_cos_theta * xout
176 + m_sin_theta * yout;
177 pxout = m_cos_theta * px
178 + m_sin_theta * py;
179
180 y = -m_sin_theta * xout
181 + m_cos_theta * yout;
182 pyout = -m_sin_theta * px
183 + m_cos_theta * py;
184
185 t = tout;
186 ptout = pt;
187
188 // assign updated values
189 px = pxout;
190 py = pyout;
191 pt = ptout;
192
193 // apply transverse aperture
194 apply_aperture(x, y, idcpu);
195
196 // undo shift due to alignment errors of the element
197 shift_out(x, y, px, py);
198 }
199
205 void operator() (RefPart & AMREX_RESTRICT refpart) const
206 {
207 using namespace amrex::literals; // for _rt and _prt
208 using amrex::Math::powi;
209
210 // assign input reference particle values
211 amrex::ParticleReal const x = refpart.x;
212 amrex::ParticleReal const px = refpart.px;
213 amrex::ParticleReal const y = refpart.y;
214 amrex::ParticleReal const py = refpart.py;
215 amrex::ParticleReal const z = refpart.z;
216 amrex::ParticleReal const pz = refpart.pz;
217 amrex::ParticleReal const t = refpart.t;
218 amrex::ParticleReal const pt = refpart.pt;
219 amrex::ParticleReal const s = refpart.s;
220
221 // length of the current slice
222 amrex::ParticleReal const slice_ds = m_ds / nslice();
223
224 // assign intermediate parameter
225 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
226
227 // advance position and momentum (straight element)
228 refpart.x = x + step*px;
229 refpart.y = y + step*py;
230 refpart.z = z + step*pz;
231 refpart.t = t - step*pt;
232
233 // advance integrated path length
234 refpart.s = s + slice_ds;
235 }
236
238 using LinearTransport::operator();
239
246 Map6x6
247 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
248 {
249 using namespace amrex::literals; // for _rt and _prt
250 using amrex::Math::powi;
251
252 // length of the current slice
253 amrex::ParticleReal const slice_ds = m_ds / nslice();
254
255 // initialize linear map matrix elements
257 Map6x6 Rrotation = Map6x6::Identity();
258 Map6x6 Rfocusing = Map6x6::Identity();
259
260 // access reference particle values to find beta*gamma^2
261 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
262
263 // compute phase advance per unit length (in rad/m) and
264 // rotation angle (in rad)
265 amrex::ParticleReal const alpha = m_ks * 0.5_prt;
266 amrex::ParticleReal const theta = alpha*slice_ds;
267 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
268
269 amrex::ParticleReal const sin_theta_ialpha = sin_theta / alpha;
270 amrex::ParticleReal const neg_alpha_sin_theta = -alpha * sin_theta;
271 amrex::ParticleReal const ds_bg2 = slice_ds / betgam2;
272
273 Rfocusing(1,1) = cos_theta;
274 Rfocusing(1,2) = sin_theta_ialpha;
275 Rfocusing(2,1) = neg_alpha_sin_theta;
276 Rfocusing(2,2) = cos_theta;
277 Rfocusing(3,3) = cos_theta;
278 Rfocusing(3,4) = sin_theta_ialpha;
279 Rfocusing(4,3) = neg_alpha_sin_theta;
280 Rfocusing(4,4) = cos_theta;
281 Rfocusing(5,6) = ds_bg2;
282
283 Rrotation(1,1) = cos_theta;
284 Rrotation(1,3) = sin_theta;
285 Rrotation(2,2) = cos_theta;
286 Rrotation(2,4) = sin_theta;
287 Rrotation(3,1) = -sin_theta;
288 Rrotation(3,3) = cos_theta;
289 Rrotation(4,2) = -sin_theta;
290 Rrotation(4,4) = cos_theta;
291
292 R = Rrotation * Rfocusing;
293
294 return R;
295 }
296
297 amrex::ParticleReal m_ks;
298
299 private:
300 // constants that are independent of the individually tracked particle,
301 // see: compute_constants() to refresh
303 };
304
305} // namespace impactx
306
307#endif // IMPACTX_SOL_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
__host__ __device__ std::pair< double, double > sincos(double x)
constexpr T powi(T x) noexcept
Definition All.H:54
@ 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:31
amrex::ParticleReal pt
energy, normalized by rest energy
Definition ReferenceParticle.H:40
amrex::ParticleReal m_ds_bg2
solenoid strength in 1/m
Definition Sol.H:302
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:61
ImpactXParticleContainer::ParticleType PType
Definition Sol.H:46
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition Sol.H:247
static constexpr auto type
Definition Sol.H:45
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:131
amrex::ParticleReal m_ks
Definition Sol.H:297
amrex::ParticleReal m_cos_theta
Definition Sol.H:302
amrex::ParticleReal m_sin_theta
Definition Sol.H:302
amrex::ParticleReal m_sin_theta_ialpha
Definition Sol.H:302
amrex::ParticleReal m_neg_alpha_sin_theta
Definition Sol.H:302
void compute_constants(RefPart const &refpart)
Definition Sol.H:90
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:219
Definition lineartransport.H:29
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:58
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