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/spintransport.H"
20#include "mixin/named.H"
21#include "mixin/nofinalize.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 ConstF
35 : public mixin::Named,
36 public mixin::BeamOptic<ConstF>,
37 public mixin::LinearTransport<ConstF>,
38 public mixin::Thick,
39 public mixin::Alignment,
42 public mixin::NoFinalize,
43 public amrex::simd::Vectorized<amrex::simd::native_simd_size_particlereal>
44 {
45 static constexpr auto type = "ConstF";
47
69 amrex::ParticleReal rotation_degree = 0,
72 int nslice = 1,
73 std::optional<std::string> name = std::nullopt
74 )
75 : Named(std::move(name)),
76 Thick(ds, nslice),
77 Alignment(dx, dy, rotation_degree),
79 m_kx(kx), m_ky(ky), m_kt(kt)
80 {
81 }
82
84 void reverse () { Thick::reverse(); }
85
87 using BeamOptic::operator();
88
96 void compute_constants (RefPart const & refpart)
97 {
98 using namespace amrex::literals; // for _rt and _prt
100
101 Alignment::compute_constants(refpart);
102
103 // length of the current slice
104 amrex::ParticleReal const slice_ds = m_ds / nslice();
105
106 // find beta*gamma^2
107 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
108
109 // trigo
110 // Note: The convention here is m_kx[1/m] = sign(k)*sqrt(abs(k))
111 // while k[1/m^2] is in the usual quadrupole-like convention
112 if (m_kx > 0_prt)
113 {
114 auto const [sin_kxds, cos_kxds] = amrex::Math::sincos(m_kx * slice_ds);
115 m_cos_kxds = cos_kxds;
116 m_const_x = -m_kx * sin_kxds;
117 m_sincx = sin_kxds / m_kx;
118 }
119 else if (m_kx < 0_prt)
120 {
121 auto const sinh_kxds = std::sinh(-m_kx*slice_ds);
122 auto const cosh_kxds = std::cosh(-m_kx*slice_ds);
123 m_cos_kxds = cosh_kxds;
124 m_const_x = -m_kx * sinh_kxds;
125 m_sincx = -sinh_kxds / m_kx;
126 }
127 else //m_kx == 0
128 {
129 m_cos_kxds = 1.0_prt;
130 m_const_x = 0.0_prt;
131 m_sincx = slice_ds;
132 }
133
134 if (m_ky > 0_prt)
135 {
136 auto const [sin_kyds, cos_kyds] = amrex::Math::sincos(m_ky * slice_ds);
137 m_cos_kyds = cos_kyds;
138 m_const_y = -m_ky * sin_kyds;
139 m_sincy = sin_kyds / m_ky;
140 }
141 else if (m_ky < 0_prt)
142 {
143 auto const sinh_kyds = std::sinh(-m_ky*slice_ds);
144 auto const cosh_kyds = std::cosh(-m_ky*slice_ds);
145 m_cos_kyds = cosh_kyds;
146 m_const_y = -m_ky * sinh_kyds;
147 m_sincy = -sinh_kyds / m_ky;
148 }
149 else //m_ky == 0
150 {
151 m_cos_kyds = 1.0_prt;
152 m_const_y = 0.0_prt;
153 m_sincy = slice_ds;
154 }
155
156 if (m_kt < 0_prt)
157 {
158 throw std::runtime_error(std::string(type) + ": must have kt >= 0!");
159 }
160 auto const [sin_ktds, cos_ktds] = amrex::Math::sincos(m_kt * slice_ds);
161 m_cos_ktds = cos_ktds;
162 // In SP, this O(beta*gamma^2) coefficient pairs with the inverse-scaled term below.
163 m_const_t = -m_kt * betgam2 * sin_ktds;
164 // intermediate quantity - to avoid division by zero
165 amrex::ParticleReal const sinct = m_kt > 0 ? std::sin(m_kt * slice_ds) / m_kt : slice_ds;
166 // In SP, the longitudinal (t, pt) block is the numerically sensitive part of ConstF.
167 m_const_pt = sinct / betgam2;
168 }
169
183 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
186 T_Real & AMREX_RESTRICT x,
187 T_Real & AMREX_RESTRICT y,
188 T_Real & AMREX_RESTRICT t,
189 T_Real & AMREX_RESTRICT px,
190 T_Real & AMREX_RESTRICT py,
191 T_Real & AMREX_RESTRICT pt,
192 T_IdCpu & AMREX_RESTRICT idcpu,
193 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
194 ) const
195 {
196 using namespace amrex::literals; // for _rt and _prt
197
198 // shift due to alignment errors of the element
199 shift_in(x, y, px, py);
200
201 // initialize output values
202 T_Real xout = x;
203 T_Real yout = y;
204 T_Real tout = t;
205 T_Real pxout = px;
206 T_Real pyout = py;
207 T_Real ptout = pt;
208
209 // advance position and momentum
210 xout = m_cos_kxds * x + m_sincx * px;
211 pxout = m_const_x * x + m_cos_kxds * px;
212
213 yout = m_cos_kyds * y + m_sincy * py;
214 pyout = m_const_y * y + m_cos_kyds * py;
215
216 tout = m_cos_ktds * t + m_const_pt * pt;
217 ptout = m_const_t * t + m_cos_ktds * pt;
218
219 // assign updated values
220 x = xout;
221 y = yout;
222 t = tout;
223 px = pxout;
224 py = pyout;
225 pt = ptout;
226
227 // apply transverse aperture
228 apply_aperture(x, y, idcpu);
229
230 // undo shift due to alignment errors of the element
231 shift_out(x, y, px, py);
232 }
233
239 void operator() (RefPart & AMREX_RESTRICT refpart) const {
240
241 using namespace amrex::literals; // for _rt and _prt
242 using amrex::Math::powi;
243
244 // assign input reference particle values
245 amrex::ParticleReal const x = refpart.x;
246 amrex::ParticleReal const px = refpart.px;
247 amrex::ParticleReal const y = refpart.y;
248 amrex::ParticleReal const py = refpart.py;
249 amrex::ParticleReal const z = refpart.z;
250 amrex::ParticleReal const pz = refpart.pz;
251 amrex::ParticleReal const t = refpart.t;
252 amrex::ParticleReal const pt = refpart.pt;
253 amrex::ParticleReal const s = refpart.s;
254
255 // length of the current slice
256 amrex::ParticleReal const slice_ds = m_ds / nslice();
257
258 // assign intermediate parameter
259 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
260
261 // advance position and momentum (straight element)
262 refpart.x = x + step*px;
263 refpart.y = y + step*py;
264 refpart.z = z + step*pz;
265 refpart.t = t - step*pt;
266
267 // advance integrated path length
268 refpart.s = s + slice_ds;
269
270 }
271
288 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
291 T_Real & AMREX_RESTRICT x,
292 T_Real & AMREX_RESTRICT y,
293 T_Real & AMREX_RESTRICT t,
294 T_Real & AMREX_RESTRICT px,
295 T_Real & AMREX_RESTRICT py,
296 T_Real & AMREX_RESTRICT pt,
297 [[maybe_unused]] T_Real & AMREX_RESTRICT sx,
298 [[maybe_unused]] T_Real & AMREX_RESTRICT sy,
299 [[maybe_unused]] T_Real & AMREX_RESTRICT sz,
300 T_IdCpu & AMREX_RESTRICT idcpu,
301 RefPart const & AMREX_RESTRICT refpart
302 ) const
303 {
304 // spin is unaffected
305
306 // phase space push
307 (*this)(x, y, t, px, py, pt, idcpu, refpart);
308 }
309
310
312 using LinearTransport::operator();
313
319 Map6x6
320 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
321 {
322 using namespace amrex::literals; // for _rt and _prt
323 using amrex::Math::powi;
324
325 // length of the current slice
326 amrex::ParticleReal const slice_ds = m_ds / nslice();
327
328 // find beta*gamma^2
329 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
330
331 // trigo
332 // Note: The convention here is m_kx[1/m] = sign(k)*sqrt(abs(k))
333 // while k[1/m^2] is in the usual quadrupole-like convention
334 amrex::ParticleReal sin_kxds, cos_kxds, const_x, sincx;
335 if (m_kx > 0_prt) {
336 std::tie(sin_kxds, cos_kxds) = amrex::Math::sincos(m_kx * slice_ds);
337 const_x = -m_kx * sin_kxds;
338 sincx = sin_kxds / m_kx;
339 }
340 else if (m_kx < 0_prt) {
341 sin_kxds = std::sinh(-m_kx*slice_ds);
342 cos_kxds = std::cosh(-m_kx*slice_ds);
343 const_x = -m_kx * sin_kxds;
344 sincx = -sin_kxds / m_kx;
345 }
346 else { //m_kx == 0
347 cos_kxds = 1.0_prt;
348 const_x = 0.0_prt;
349 sincx = slice_ds;
350 }
351
352 amrex::ParticleReal sin_kyds, cos_kyds, const_y, sincy;
353 if (m_ky > 0_prt) {
354 std::tie(sin_kyds, cos_kyds) = amrex::Math::sincos(m_ky * slice_ds);
355 const_y = -m_ky * sin_kyds;
356 sincy = sin_kyds / m_ky;
357 }
358 else if (m_ky < 0_prt) {
359 sin_kyds = std::sinh(-m_ky*slice_ds);
360 cos_kyds = std::cosh(-m_ky*slice_ds);
361 const_y = -m_ky * sin_kyds;
362 sincy = -sin_kyds / m_ky;
363 }
364 else { //m_ky == 0
365 cos_kyds = 1.0_prt;
366 const_y = 0.0_prt;
367 sincy = slice_ds;
368 }
369
370 if (m_kt < 0_prt) {
371 throw std::runtime_error(std::string(type) + ": must have kt >= 0!");
372 }
373 auto const [sin_ktds, cos_ktds] = amrex::Math::sincos(m_kt * slice_ds);
374 amrex::ParticleReal const_t = -m_kt * betgam2 * sin_ktds;
375 // intermediate quantities - to avoid division by zero
376 amrex::ParticleReal const sinct = m_kt > 0 ? std::sin(m_kt * slice_ds) / m_kt : slice_ds;
377 amrex::ParticleReal const_pt = sinct / betgam2;
378
379 // assign linear map matrix elements
381
382 R(1,1) = cos_kxds;
383 R(1,2) = sincx;
384 R(2,1) = const_x;
385 R(2,2) = cos_kxds;
386
387 R(3,3) = cos_kyds;
388 R(3,4) = sincy;
389 R(4,3) = const_y;
390 R(4,4) = cos_kyds;
391
392 R(5,5) = cos_ktds;
393 R(5,6) = const_pt;
394 R(6,5) = const_t;
395 R(6,6) = cos_ktds;
396
397 return R;
398 }
399
403
404 private:
405 // constants that are independent of the individually tracked particle,
406 // see: compute_constants() to refresh
410 };
411
412} // namespace impactx
413
415
416#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:44
amrex::ParticleReal m_const_pt
Definition ConstF.H:408
amrex::ParticleReal m_cos_kxds
Definition ConstF.H:409
amrex::ParticleReal m_kx
Definition ConstF.H:400
void compute_constants(RefPart const &refpart)
Definition ConstF.H:96
static constexpr auto type
Definition ConstF.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 &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 ConstF.H:290
amrex::ParticleReal m_const_t
Definition ConstF.H:408
amrex::ParticleReal m_cos_kyds
Definition ConstF.H:409
amrex::ParticleReal m_kt
focusing y strength in 1/m
Definition ConstF.H:402
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:62
amrex::ParticleReal m_sincx
focusing t strength in 1/m
Definition ConstF.H:407
amrex::ParticleReal m_sincy
Definition ConstF.H:407
ImpactXParticleContainer::ParticleType PType
Definition ConstF.H:46
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:185
void reverse()
Definition ConstF.H:84
amrex::ParticleReal m_const_x
Definition ConstF.H:408
amrex::ParticleReal m_const_y
Definition ConstF.H:408
amrex::ParticleReal m_ky
focusing x strength in 1/m
Definition ConstF.H:401
amrex::ParticleReal m_cos_ktds
Definition ConstF.H:409
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ConstF.H:320
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
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