ImpactX
Loading...
Searching...
No Matches
ConstF.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_CONSTF_H
11#define IMPACTX_CONSTF_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 ConstF
34 : public mixin::Named,
35 public mixin::BeamOptic<ConstF>,
36 public mixin::LinearTransport<ConstF>,
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 = "ConstF";
45
61 amrex::ParticleReal ds,
62 amrex::ParticleReal kx,
63 amrex::ParticleReal ky,
64 amrex::ParticleReal kt,
65 amrex::ParticleReal dx = 0,
66 amrex::ParticleReal dy = 0,
67 amrex::ParticleReal rotation_degree = 0,
68 amrex::ParticleReal aperture_x = 0,
69 amrex::ParticleReal aperture_y = 0,
70 int nslice = 1,
71 std::optional<std::string> name = std::nullopt
72 )
73 : Named(std::move(name)),
74 Thick(ds, nslice),
75 Alignment(dx, dy, rotation_degree),
77 m_kx(kx), m_ky(ky), m_kt(kt)
78 {
79 }
80
82 using BeamOptic::operator();
83
91 void compute_constants (RefPart const & refpart)
92 {
93 using namespace amrex::literals; // for _rt and _prt
95
96 Alignment::compute_constants(refpart);
97
98 // length of the current slice
99 amrex::ParticleReal const slice_ds = m_ds / nslice();
100
101 // find beta*gamma^2
102 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
103
104 // trigo
105 auto const [sin_kxds, cos_kxds] = amrex::Math::sincos(m_kx * slice_ds);
106 m_cos_kxds = cos_kxds;
107 m_const_x = -m_kx * sin_kxds;
108 auto const [sin_kyds, cos_kyds] = amrex::Math::sincos(m_ky * slice_ds);
109 m_cos_kyds = cos_kyds;
110 m_const_y = -m_ky * sin_kyds;
111 auto const [sin_ktds, cos_ktds] = amrex::Math::sincos(m_kt * slice_ds);
112 m_cos_ktds = cos_ktds;
113 m_const_t = -m_kt * betgam2 * sin_ktds;
114
115 // intermediate quantities - to avoid division by zero
116 m_sincx = m_kx > 0 ? std::sin(m_kx * slice_ds) / m_kx : slice_ds;
117 m_sincy = m_ky > 0 ? std::sin(m_ky * slice_ds) / m_ky : slice_ds;
118 amrex::ParticleReal const sinct = m_kt > 0 ? std::sin(m_kt * slice_ds) / m_kt : slice_ds;
119 m_const_pt = sinct / betgam2;
120 }
121
135 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
138 T_Real & AMREX_RESTRICT x,
139 T_Real & AMREX_RESTRICT y,
140 T_Real & AMREX_RESTRICT t,
141 T_Real & AMREX_RESTRICT px,
142 T_Real & AMREX_RESTRICT py,
143 T_Real & AMREX_RESTRICT pt,
144 T_IdCpu & AMREX_RESTRICT idcpu,
145 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
146 ) const
147 {
148 using namespace amrex::literals; // for _rt and _prt
149
150 // shift due to alignment errors of the element
151 shift_in(x, y, px, py);
152
153 // initialize output values
154 T_Real xout = x;
155 T_Real yout = y;
156 T_Real tout = t;
157 T_Real pxout = px;
158 T_Real pyout = py;
159 T_Real ptout = pt;
160
161 // advance position and momentum
162 xout = m_cos_kxds * x + m_sincx * px;
163 pxout = m_const_x * x + m_cos_kxds * px;
164
165 yout = m_cos_kyds * y + m_sincy * py;
166 pyout = m_const_y * y + m_cos_kyds * py;
167
168 tout = m_cos_ktds * t + m_const_pt * pt;
169 ptout = m_const_t * t + m_cos_ktds * pt;
170
171 // assign updated values
172 x = xout;
173 y = yout;
174 t = tout;
175 px = pxout;
176 py = pyout;
177 pt = ptout;
178
179 // apply transverse aperture
180 apply_aperture(x, y, idcpu);
181
182 // undo shift due to alignment errors of the element
183 shift_out(x, y, px, py);
184 }
185
191 void operator() (RefPart & AMREX_RESTRICT refpart) const {
192
193 using namespace amrex::literals; // for _rt and _prt
194 using amrex::Math::powi;
195
196 // assign input reference particle values
197 amrex::ParticleReal const x = refpart.x;
198 amrex::ParticleReal const px = refpart.px;
199 amrex::ParticleReal const y = refpart.y;
200 amrex::ParticleReal const py = refpart.py;
201 amrex::ParticleReal const z = refpart.z;
202 amrex::ParticleReal const pz = refpart.pz;
203 amrex::ParticleReal const t = refpart.t;
204 amrex::ParticleReal const pt = refpart.pt;
205 amrex::ParticleReal const s = refpart.s;
206
207 // length of the current slice
208 amrex::ParticleReal const slice_ds = m_ds / nslice();
209
210 // assign intermediate parameter
211 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
212
213 // advance position and momentum (straight element)
214 refpart.x = x + step*px;
215 refpart.y = y + step*py;
216 refpart.z = z + step*pz;
217 refpart.t = t - step*pt;
218
219 // advance integrated path length
220 refpart.s = s + slice_ds;
221
222 }
223
225 using LinearTransport::operator();
226
232 Map6x6
233 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
234 {
235 using namespace amrex::literals; // for _rt and _prt
236 using amrex::Math::powi;
237
238 // length of the current slice
239 amrex::ParticleReal const slice_ds = m_ds / nslice();
240
241 // find beta*gamma^2
242 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
243
244 // trigo
245 auto const [sin_kxds, cos_kxds] = amrex::Math::sincos(m_kx * slice_ds);
246 amrex::ParticleReal const_x = -m_kx * sin_kxds;
247 auto const [sin_kyds, cos_kyds] = amrex::Math::sincos(m_ky * slice_ds);
248 amrex::ParticleReal const_y = -m_ky * sin_kyds;
249 auto const [sin_ktds, cos_ktds] = amrex::Math::sincos(m_kt * slice_ds);
250 amrex::ParticleReal const_t = -m_kt * betgam2 * sin_ktds;
251
252 // intermediate quantities - to avoid division by zero
253 amrex::ParticleReal sincx = m_kx > 0 ? std::sin(m_kx * slice_ds) / m_kx : slice_ds;
254 amrex::ParticleReal sincy = m_ky > 0 ? std::sin(m_ky * slice_ds) / m_ky : slice_ds;
255 amrex::ParticleReal const sinct = m_kt > 0 ? std::sin(m_kt * slice_ds) / m_kt : slice_ds;
256 amrex::ParticleReal const_pt = sinct / betgam2;
257
258 // assign linear map matrix elements
260 R(1,1) = cos_kxds;
261 R(1,2) = sincx;
262 R(2,1) = const_x;
263 R(2,2) = cos_kxds;
264 R(3,3) = cos_kyds;
265 R(3,4) = sincy;
266 R(4,3) = const_y;
267 R(4,4) = cos_kyds;
268 R(5,5) = cos_ktds;
269 R(5,6) = const_pt;
270 R(6,5) = const_t;
271 R(6,6) = cos_ktds;
272
273 return R;
274 }
275
276 amrex::ParticleReal m_kx;
277 amrex::ParticleReal m_ky;
278 amrex::ParticleReal m_kt;
279
280 private:
281 // constants that are independent of the individually tracked particle,
282 // see: compute_constants() to refresh
283 amrex::ParticleReal m_sincx, m_sincy;
284 amrex::ParticleReal m_const_x, m_const_y, m_const_t, m_const_pt;
285 amrex::ParticleReal m_cos_kxds, m_cos_kyds, m_cos_ktds;
286 };
287
288} // namespace impactx
289
290#endif // IMPACTX_CONSTF_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_const_pt
Definition ConstF.H:284
amrex::ParticleReal m_cos_kxds
Definition ConstF.H:285
amrex::ParticleReal m_kx
Definition ConstF.H:276
void compute_constants(RefPart const &refpart)
Definition ConstF.H:91
static constexpr auto type
Definition ConstF.H:43
amrex::ParticleReal m_const_t
Definition ConstF.H:284
amrex::ParticleReal m_cos_kyds
Definition ConstF.H:285
amrex::ParticleReal m_kt
focusing y strength in 1/m
Definition ConstF.H:278
ConstF(amrex::ParticleReal ds, amrex::ParticleReal kx, amrex::ParticleReal ky, amrex::ParticleReal kt, 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 ConstF.H:60
amrex::ParticleReal m_sincx
focusing t strength in 1/m
Definition ConstF.H:283
amrex::ParticleReal m_sincy
Definition ConstF.H:283
ImpactXParticleContainer::ParticleType PType
Definition ConstF.H:44
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 ConstF.H:137
amrex::ParticleReal m_const_x
Definition ConstF.H:284
amrex::ParticleReal m_const_y
Definition ConstF.H:284
amrex::ParticleReal m_ky
focusing x strength in 1/m
Definition ConstF.H:277
amrex::ParticleReal m_cos_ktds
Definition ConstF.H:285
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ConstF.H:233
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