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