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 [[maybe_unused]] T_IdCpu const & 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 // initialize output values
199 T_Real xout = x;
200 T_Real yout = y;
201 T_Real tout = t;
202 T_Real pxout = px;
203 T_Real pyout = py;
204 T_Real ptout = pt;
205
206 // advance position and momentum
207 xout = m_cos_kxds * x + m_sincx * px;
208 pxout = m_const_x * x + m_cos_kxds * px;
209
210 yout = m_cos_kyds * y + m_sincy * py;
211 pyout = m_const_y * y + m_cos_kyds * py;
212
213 tout = m_cos_ktds * t + m_const_pt * pt;
214 ptout = m_const_t * t + m_cos_ktds * pt;
215
216 // assign updated values
217 x = xout;
218 y = yout;
219 t = tout;
220 px = pxout;
221 py = pyout;
222 pt = ptout;
223 }
224
230 void operator() (RefPart & AMREX_RESTRICT refpart) const {
231
232 using namespace amrex::literals; // for _rt and _prt
233 using amrex::Math::powi;
234
235 // assign input reference particle values
236 amrex::ParticleReal const x = refpart.x;
237 amrex::ParticleReal const px = refpart.px;
238 amrex::ParticleReal const y = refpart.y;
239 amrex::ParticleReal const py = refpart.py;
240 amrex::ParticleReal const z = refpart.z;
241 amrex::ParticleReal const pz = refpart.pz;
242 amrex::ParticleReal const t = refpart.t;
243 amrex::ParticleReal const pt = refpart.pt;
244 amrex::ParticleReal const s = refpart.s;
245
246 // length of the current slice
247 amrex::ParticleReal const slice_ds = m_ds / nslice();
248
249 // assign intermediate parameter
250 amrex::ParticleReal const step = slice_ds / std::sqrt(powi<2>(pt) - 1.0_prt);
251
252 // advance position and momentum (straight element)
253 refpart.x = x + step*px;
254 refpart.y = y + step*py;
255 refpart.z = z + step*pz;
256 refpart.t = t - step*pt;
257
258 // advance integrated path length
259 refpart.s = s + slice_ds;
260
261 }
262
279 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
282 T_Real & AMREX_RESTRICT x,
283 T_Real & AMREX_RESTRICT y,
284 T_Real & AMREX_RESTRICT t,
285 T_Real & AMREX_RESTRICT px,
286 T_Real & AMREX_RESTRICT py,
287 T_Real & AMREX_RESTRICT pt,
288 [[maybe_unused]] T_Real const & AMREX_RESTRICT sx,
289 [[maybe_unused]] T_Real const & AMREX_RESTRICT sy,
290 [[maybe_unused]] T_Real const & AMREX_RESTRICT sz,
291 T_IdCpu const & AMREX_RESTRICT idcpu,
292 RefPart const & AMREX_RESTRICT refpart
293 ) const
294 {
295 // spin is unaffected
296
297 // phase space push
298 (*this)(x, y, t, px, py, pt, idcpu, refpart);
299 }
300
301
303 using LinearTransport::operator();
304
310 Map6x6
311 transport_map ([[maybe_unused]] RefPart const & AMREX_RESTRICT refpart) const
312 {
313 using namespace amrex::literals; // for _rt and _prt
314 using amrex::Math::powi;
315
316 // length of the current slice
317 amrex::ParticleReal const slice_ds = m_ds / nslice();
318
319 // find beta*gamma^2
320 amrex::ParticleReal const betgam2 = powi<2>(refpart.pt) - 1.0_prt;
321
322 // trigo
323 // Note: The convention here is m_kx[1/m] = sign(k)*sqrt(abs(k))
324 // while k[1/m^2] is in the usual quadrupole-like convention
325 amrex::ParticleReal sin_kxds, cos_kxds, const_x, sincx;
326 if (m_kx > 0_prt) {
327 std::tie(sin_kxds, cos_kxds) = amrex::Math::sincos(m_kx * slice_ds);
328 const_x = -m_kx * sin_kxds;
329 sincx = sin_kxds / m_kx;
330 }
331 else if (m_kx < 0_prt) {
332 sin_kxds = std::sinh(-m_kx*slice_ds);
333 cos_kxds = std::cosh(-m_kx*slice_ds);
334 const_x = -m_kx * sin_kxds;
335 sincx = -sin_kxds / m_kx;
336 }
337 else { //m_kx == 0
338 cos_kxds = 1.0_prt;
339 const_x = 0.0_prt;
340 sincx = slice_ds;
341 }
342
343 amrex::ParticleReal sin_kyds, cos_kyds, const_y, sincy;
344 if (m_ky > 0_prt) {
345 std::tie(sin_kyds, cos_kyds) = amrex::Math::sincos(m_ky * slice_ds);
346 const_y = -m_ky * sin_kyds;
347 sincy = sin_kyds / m_ky;
348 }
349 else if (m_ky < 0_prt) {
350 sin_kyds = std::sinh(-m_ky*slice_ds);
351 cos_kyds = std::cosh(-m_ky*slice_ds);
352 const_y = -m_ky * sin_kyds;
353 sincy = -sin_kyds / m_ky;
354 }
355 else { //m_ky == 0
356 cos_kyds = 1.0_prt;
357 const_y = 0.0_prt;
358 sincy = slice_ds;
359 }
360
361 if (m_kt < 0_prt) {
362 throw std::runtime_error(std::string(type) + ": must have kt >= 0!");
363 }
364 auto const [sin_ktds, cos_ktds] = amrex::Math::sincos(m_kt * slice_ds);
365 amrex::ParticleReal const_t = -m_kt * betgam2 * sin_ktds;
366 // intermediate quantities - to avoid division by zero
367 amrex::ParticleReal const sinct = m_kt > 0 ? std::sin(m_kt * slice_ds) / m_kt : slice_ds;
368 amrex::ParticleReal const_pt = sinct / betgam2;
369
370 // assign linear map matrix elements
372
373 R(1,1) = cos_kxds;
374 R(1,2) = sincx;
375 R(2,1) = const_x;
376 R(2,2) = cos_kxds;
377
378 R(3,3) = cos_kyds;
379 R(3,4) = sincy;
380 R(4,3) = const_y;
381 R(4,4) = cos_kyds;
382
383 R(5,5) = cos_ktds;
384 R(5,6) = const_pt;
385 R(6,5) = const_t;
386 R(6,6) = cos_ktds;
387
388 // apply the transverse rotation (roll) alignment error
389 return rotate_aligned_map(R);
390 }
391
395
396 private:
397 // constants that are independent of the individually tracked particle,
398 // see: compute_constants() to refresh
402 };
403
404} // namespace impactx
405
407
408#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: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_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:185
amrex::ParticleReal m_const_pt
Definition ConstF.H:400
amrex::ParticleReal m_cos_kxds
Definition ConstF.H:401
amrex::ParticleReal m_kx
Definition ConstF.H:392
void compute_constants(RefPart const &refpart)
Definition ConstF.H:96
static constexpr auto type
Definition ConstF.H:45
amrex::ParticleReal m_const_t
Definition ConstF.H:400
amrex::ParticleReal m_cos_kyds
Definition ConstF.H:401
amrex::ParticleReal m_kt
focusing y strength in 1/m
Definition ConstF.H:394
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:399
amrex::ParticleReal m_sincy
Definition ConstF.H:399
ImpactXParticleContainer::ParticleType PType
Definition ConstF.H:46
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:281
void reverse()
Definition ConstF.H:84
amrex::ParticleReal m_const_x
Definition ConstF.H:400
amrex::ParticleReal m_const_y
Definition ConstF.H:400
amrex::ParticleReal m_ky
focusing x strength in 1/m
Definition ConstF.H:393
amrex::ParticleReal m_cos_ktds
Definition ConstF.H:401
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ConstF.H:311
Definition alignment.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dy() const
Definition alignment.H:189
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal dx() const
Definition alignment.H:179
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 rotate_aligned_map(Map6x6 const &R) const
Definition alignment.H:263
Alignment(amrex::ParticleReal dx, amrex::ParticleReal dy, amrex::ParticleReal rotation_degree)
Definition alignment.H:39
Definition beamoptic.H:529
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 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