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, Kyrre Sjobak
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
67 amrex::ParticleReal rotation_degree = 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 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 // find beta*gamma^2
105 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
106
107 // trigo
108 // Note: The convention here is m_kx[1/m] = sign(k)*sqrt(abs(k))
109 // while k[1/m^2] is in the usual quadrupole-like convention
110 if (m_kx > 0_prt)
111 {
112 auto const [sin_kxds, cos_kxds] = amrex::Math::sincos(m_kx * slice_ds);
113 m_cos_kxds = cos_kxds;
114 m_const_x = -m_kx * sin_kxds;
115 m_sincx = sin_kxds / m_kx;
116 }
117 else if (m_kx < 0_prt)
118 {
119 auto const sinh_kxds = std::sinh(-m_kx*slice_ds);
120 auto const cosh_kxds = std::cosh(-m_kx*slice_ds);
121 m_cos_kxds = cosh_kxds;
122 m_const_x = -m_kx * sinh_kxds;
123 m_sincx = -sinh_kxds / m_kx;
124 }
125 else //m_kx == 0
126 {
127 m_cos_kxds = 1.0_prt;
128 m_const_x = 0.0_prt;
129 m_sincx = slice_ds;
130 }
131
132 if (m_ky > 0_prt)
133 {
134 auto const [sin_kyds, cos_kyds] = amrex::Math::sincos(m_ky * slice_ds);
135 m_cos_kyds = cos_kyds;
136 m_const_y = -m_ky * sin_kyds;
137 m_sincy = sin_kyds / m_ky;
138 }
139 else if (m_ky < 0_prt)
140 {
141 auto const sinh_kyds = std::sinh(-m_ky*slice_ds);
142 auto const cosh_kyds = std::cosh(-m_ky*slice_ds);
143 m_cos_kyds = cosh_kyds;
144 m_const_y = -m_ky * sinh_kyds;
145 m_sincy = -sinh_kyds / m_ky;
146 }
147 else //m_ky == 0
148 {
149 m_cos_kyds = 1.0_prt;
150 m_const_y = 0.0_prt;
151 m_sincy = slice_ds;
152 }
153
154 if (m_kt < 0_prt)
155 {
156 throw std::runtime_error(std::string(type) + ": must have kt >= 0!");
157 }
158 auto const [sin_ktds, cos_ktds] = amrex::Math::sincos(m_kt * slice_ds);
159 m_cos_ktds = cos_ktds;
160 // In SP, this O(beta*gamma^2) coefficient pairs with the inverse-scaled term below.
161 m_const_t = -m_kt * betgam2 * sin_ktds;
162 // intermediate quantity - to avoid division by zero
163 amrex::ParticleReal const sinct = m_kt > 0 ? std::sin(m_kt * slice_ds) / m_kt : slice_ds;
164 // In SP, the longitudinal (t, pt) block is the numerically sensitive part of ConstF.
165 m_const_pt = sinct / betgam2;
166 }
167
181 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
184 T_Real & AMREX_RESTRICT x,
185 T_Real & AMREX_RESTRICT y,
186 T_Real & AMREX_RESTRICT t,
187 T_Real & AMREX_RESTRICT px,
188 T_Real & AMREX_RESTRICT py,
189 T_Real & AMREX_RESTRICT pt,
190 T_IdCpu & AMREX_RESTRICT idcpu,
191 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
192 ) const
193 {
194 using namespace amrex::literals; // for _rt and _prt
195
196 // shift due to alignment errors of the element
197 shift_in(x, y, px, py);
198
199 // initialize output values
200 T_Real xout = x;
201 T_Real yout = y;
202 T_Real tout = t;
203 T_Real pxout = px;
204 T_Real pyout = py;
205 T_Real ptout = pt;
206
207 // advance position and momentum
208 xout = m_cos_kxds * x + m_sincx * px;
209 pxout = m_const_x * x + m_cos_kxds * px;
210
211 yout = m_cos_kyds * y + m_sincy * py;
212 pyout = m_const_y * y + m_cos_kyds * py;
213
214 tout = m_cos_ktds * t + m_const_pt * pt;
215 ptout = m_const_t * t + m_cos_ktds * pt;
216
217 // assign updated values
218 x = xout;
219 y = yout;
220 t = tout;
221 px = pxout;
222 py = pyout;
223 pt = ptout;
224
225 // apply transverse aperture
226 apply_aperture(x, y, idcpu);
227
228 // undo shift due to alignment errors of the element
229 shift_out(x, y, px, py);
230 }
231
237 void operator() (RefPart & AMREX_RESTRICT refpart) const {
238
239 using namespace amrex::literals; // for _rt and _prt
240 using amrex::Math::powi;
241
242 // assign input reference particle values
243 amrex::ParticleReal const x = refpart.x;
244 amrex::ParticleReal const px = refpart.px;
245 amrex::ParticleReal const y = refpart.y;
246 amrex::ParticleReal const py = refpart.py;
247 amrex::ParticleReal const z = refpart.z;
248 amrex::ParticleReal const pz = refpart.pz;
249 amrex::ParticleReal const t = refpart.t;
250 amrex::ParticleReal const pt = refpart.pt;
251 amrex::ParticleReal const s = refpart.s;
252
253 // length of the current slice
254 amrex::ParticleReal const slice_ds = m_ds / nslice();
255
256 // assign intermediate parameter
257 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
258
259 // advance position and momentum (straight element)
260 refpart.x = x + step*px;
261 refpart.y = y + step*py;
262 refpart.z = z + step*pz;
263 refpart.t = t - step*pt;
264
265 // advance integrated path length
266 refpart.s = s + slice_ds;
267
268 }
269
271 using LinearTransport::operator();
272
278 Map6x6
279 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
280 {
281 using namespace amrex::literals; // for _rt and _prt
282 using amrex::Math::powi;
283
284 // length of the current slice
285 amrex::ParticleReal const slice_ds = m_ds / nslice();
286
287 // find beta*gamma^2
288 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
289
290 // trigo
291 // Note: The convention here is m_kx[1/m] = sign(k)*sqrt(abs(k))
292 // while k[1/m^2] is in the usual quadrupole-like convention
293 amrex::ParticleReal sin_kxds, cos_kxds, const_x, sincx;
294 if (m_kx > 0_prt) {
295 std::tie(sin_kxds, cos_kxds) = amrex::Math::sincos(m_kx * slice_ds);
296 const_x = -m_kx * sin_kxds;
297 sincx = sin_kxds / m_kx;
298 }
299 else if (m_kx < 0_prt) {
300 sin_kxds = std::sinh(-m_kx*slice_ds);
301 cos_kxds = std::cosh(-m_kx*slice_ds);
302 const_x = -m_kx * sin_kxds;
303 sincx = -sin_kxds / m_kx;
304 }
305 else { //m_kx == 0
306 cos_kxds = 1.0_prt;
307 const_x = 0.0_prt;
308 sincx = slice_ds;
309 }
310
311 amrex::ParticleReal sin_kyds, cos_kyds, const_y, sincy;
312 if (m_ky > 0_prt) {
313 std::tie(sin_kyds, cos_kyds) = amrex::Math::sincos(m_ky * slice_ds);
314 const_y = -m_ky * sin_kyds;
315 sincy = sin_kyds / m_ky;
316 }
317 else if (m_ky < 0_prt) {
318 sin_kyds = std::sinh(-m_ky*slice_ds);
319 cos_kyds = std::cosh(-m_ky*slice_ds);
320 const_y = -m_ky * sin_kyds;
321 sincy = -sin_kyds / m_ky;
322 }
323 else { //m_ky == 0
324 cos_kyds = 1.0_prt;
325 const_y = 0.0_prt;
326 sincy = slice_ds;
327 }
328
329 if (m_kt < 0_prt) {
330 throw std::runtime_error(std::string(type) + ": must have kt >= 0!");
331 }
332 auto const [sin_ktds, cos_ktds] = amrex::Math::sincos(m_kt * slice_ds);
333 amrex::ParticleReal const_t = -m_kt * betgam2 * sin_ktds;
334 // intermediate quantities - to avoid division by zero
335 amrex::ParticleReal const sinct = m_kt > 0 ? std::sin(m_kt * slice_ds) / m_kt : slice_ds;
336 amrex::ParticleReal const_pt = sinct / betgam2;
337
338 // assign linear map matrix elements
340
341 R(1,1) = cos_kxds;
342 R(1,2) = sincx;
343 R(2,1) = const_x;
344 R(2,2) = cos_kxds;
345
346 R(3,3) = cos_kyds;
347 R(3,4) = sincy;
348 R(4,3) = const_y;
349 R(4,4) = cos_kyds;
350
351 R(5,5) = cos_ktds;
352 R(5,6) = const_pt;
353 R(6,5) = const_t;
354 R(6,6) = cos_ktds;
355
356 return R;
357 }
358
362
363 private:
364 // constants that are independent of the individually tracked particle,
365 // see: compute_constants() to refresh
369 };
370
371} // namespace impactx
372
374
375#endif // IMPACTX_CONSTF_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
Definition ConstF.H:42
amrex::ParticleReal m_const_pt
Definition ConstF.H:367
amrex::ParticleReal m_cos_kxds
Definition ConstF.H:368
amrex::ParticleReal m_kx
Definition ConstF.H:359
void compute_constants(RefPart const &refpart)
Definition ConstF.H:94
static constexpr auto type
Definition ConstF.H:43
amrex::ParticleReal m_const_t
Definition ConstF.H:367
amrex::ParticleReal m_cos_kyds
Definition ConstF.H:368
amrex::ParticleReal m_kt
focusing y strength in 1/m
Definition ConstF.H:361
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:366
amrex::ParticleReal m_sincy
Definition ConstF.H:366
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:183
void reverse()
Definition ConstF.H:82
amrex::ParticleReal m_const_x
Definition ConstF.H:367
amrex::ParticleReal m_const_y
Definition ConstF.H:367
amrex::ParticleReal m_ky
focusing x strength in 1/m
Definition ConstF.H:360
amrex::ParticleReal m_cos_ktds
Definition ConstF.H:368
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ConstF.H:279
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