ImpactX
Loading...
Searching...
No Matches
ExactSbend.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_EXACTSBEND_H
11#define IMPACTX_EXACTSBEND_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{
35 : public mixin::Named,
36 public mixin::BeamOptic<ExactSbend>,
37 public mixin::LinearTransport<ExactSbend>,
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 = "ExactSbend";
47
77 amrex::ParticleReal rotation_degree = 0,
80 int nslice = 1,
81 std::optional<std::string> name = std::nullopt
82 )
83 : Named(std::move(name)),
84 Thick(ds, nslice),
85 Alignment(dx, dy, rotation_degree),
87 m_phi(phi * degree2rad), m_B(B)
88 {
89 }
90
93 rc (RefPart const & refpart) const
94 {
95 using namespace amrex::literals; // for _rt and _prt
96
97 return m_B != 0_prt ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
98 }
99
101 void reverse () { Thick::reverse(); m_phi = -m_phi; }
102
104 using BeamOptic::operator();
105
106
114 void compute_constants (RefPart const & refpart)
115 {
116 using namespace amrex::literals; // for _rt and _prt
117 using amrex::Math::powi;
118
119 Alignment::compute_constants(refpart);
120
121 // reference particle's orbital radius
122 m_rc = this->rc(refpart);
123
124 // arc length and angle of the current slice
125 m_slice_ds = m_ds / nslice();
126 amrex::ParticleReal const bend_sign = m_ds * m_rc / std::abs(m_ds * m_rc);
127 m_slice_phi = bend_sign * std::abs(m_phi) / nslice(); // the sign is determined by rc/ds
128
129 // trigonometric evaluations
130 auto const [sin_phi, cos_phi] = amrex::Math::sincos(m_slice_phi);
131 m_sin_phi = sin_phi;
132 m_cos_phi = cos_phi;
133
134 // access reference particle values to find relativistic factors
135 m_ibeta = 1_prt / refpart.beta();
136 m_ibetagam2 = 1_prt / powi<2>(refpart.beta_gamma());
137
138 // gyromagnetic constant (used during spin push)
140 m_beta_gamma = refpart.beta_gamma();
141 m_beta = refpart.beta();
142
143 }
144
156 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
159 T_Real & AMREX_RESTRICT x,
160 T_Real & AMREX_RESTRICT y,
161 T_Real & AMREX_RESTRICT t,
162 T_Real & AMREX_RESTRICT px,
163 T_Real const & AMREX_RESTRICT py,
164 T_Real const & AMREX_RESTRICT pt,
165 T_IdCpu & AMREX_RESTRICT idcpu,
166 [[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
167 ) const
168 {
169 using namespace amrex::literals; // for _rt and _prt
170 using amrex::Math::powi;
171 using namespace std; // for cmath(float)
172 namespace stdx = amrex::simd::stdx;
173
174 // access position data
175 T_Real const xout = x;
176 T_Real const yout = y;
177 T_Real const tout = t;
178
179 // initialize output values of momenta
180 T_Real pxout = px;
181 // T_Real pyout = py;
182 // T_Real ptout = pt;
183
184 // treat the special case of zero field
185 if (m_phi==0_prt && m_B==0_prt) {
186 // compute the radical in the denominator (= pz):
187 T_Real const inv_pzden = 1_prt / sqrt(
188 powi<2>(pt - m_ibeta) -
190 powi<2>(px) -
191 powi<2>(py)
192 );
193
194 // advance position and momentum (exact drift)
195 x = xout + m_slice_ds * px * inv_pzden;
196 // pxout = px;
197 y = yout + m_slice_ds * py * inv_pzden;
198 // pyout = py;
199 t = tout - m_slice_ds * (m_ibeta + (pt - m_ibeta) * inv_pzden);
200 // ptout = pt;
201
202 // assign updated momenta
203 // px = pxout;
204 // py = pyout;
205 // pt = ptout;
206
207 } else {
208
209 // assign intermediate quantities
210 T_Real const pperp2 = powi<2>(pt)-2.0_prt * m_ibeta * pt - powi<2>(py)+1.0_prt;
211 T_Real const px2 = powi<2>(px);
212 // determine if particle lies within the domain of map definition
213 auto const mask = pperp2 <= px2;
215 {
216 T_Real const pperp = sqrt(pperp2);
217 T_Real const pzi = sqrt(powi<2>(pperp) - powi<2>(px));
218 T_Real const rho = m_rc + xout;
219
220 // update momenta
221 pxout = px * m_cos_phi + (pzi - rho / m_rc) * m_sin_phi;
222 // pyout = py;
223 // ptout = pt;
224
225 // angle of momentum rotation
226 T_Real const px2f = powi<2>(pxout);
227 // determine if particle lies within domain of map definition
228 auto const mask2 = pperp2 <= px2f;
230 {
231 T_Real const pzf = sqrt(powi<2>(pperp)-powi<2>(pxout));
232 T_Real const theta = m_slice_phi + asin(px/pperp) - asin(pxout/pperp);
233
234 // update position coordinates
235 x = -m_rc + rho*m_cos_phi + m_rc*(pzf + px*m_sin_phi - pzi*m_cos_phi);
236 y = yout + theta * m_rc * py;
237 t = tout - theta * m_rc * (pt - 1.0_prt * m_ibeta) - m_slice_phi * m_rc * m_ibeta;
238
239 // assign updated momenta
240 px = pxout;
241 // py = pyout;
242 // pt = ptout;
243 }
244 }
245 }
246 }
247
253 void operator() (RefPart & AMREX_RESTRICT refpart) const
254 {
255 using namespace amrex::literals; // for _rt and _prt
256 using amrex::Math::powi;
257
258 // assign input reference particle values
259 amrex::ParticleReal const x = refpart.x;
260 amrex::ParticleReal const px = refpart.px;
261 amrex::ParticleReal const y = refpart.y;
262 amrex::ParticleReal const py = refpart.py;
263 amrex::ParticleReal const z = refpart.z;
264 amrex::ParticleReal const pz = refpart.pz;
265 amrex::ParticleReal const t = refpart.t;
266 amrex::ParticleReal const pt = refpart.pt;
267 amrex::ParticleReal const s = refpart.s;
268
269 // length of the current slice
270 amrex::ParticleReal const slice_ds = m_ds / nslice();
271
272 // special case of zero field = an exact drift
273 if (m_phi==0_prt && m_B==0_prt) {
274 // advance position and momentum (drift)
275 amrex::ParticleReal const step = slice_ds /std::sqrt(powi<2>(pt)-1.0_prt);
276 refpart.x = x + step*px;
277 refpart.y = y + step*py;
278 refpart.z = z + step*pz;
279 refpart.t = t - step*pt;
280
281 } else {
282
283 // assign intermediate parameters
284 amrex::ParticleReal const rc = (m_B != 0_prt) ? refpart.rigidity_Tm() / m_B : m_ds / m_phi;
285 amrex::ParticleReal const B = refpart.beta_gamma() /rc;
286 amrex::ParticleReal const bend_sign = m_ds * rc / std::abs(m_ds * rc);
287 amrex::ParticleReal const theta = bend_sign * std::abs(m_phi) / nslice(); // sign is determined by rc*ds
288
289 // calculate expensive terms once
290 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
291
292 // advance position and momentum (bend)
293 refpart.px = px*cos_theta - pz*sin_theta;
294 refpart.py = py;
295 refpart.pz = pz*cos_theta + px*sin_theta;
296 refpart.pt = pt;
297
298 refpart.x = x + (refpart.pz - pz)/B;
299 refpart.y = y + (theta/B)*py;
300 refpart.z = z - (refpart.px - px)/B;
301 refpart.t = t - (theta/B)*pt;
302
303 }
304
305 // advance integrated path length
306 refpart.s = s + slice_ds;
307 }
308
310 using LinearTransport::operator();
311
318 Map6x6
319 transport_map (RefPart const & AMREX_RESTRICT refpart) const
320 {
321 using namespace amrex::literals; // for _rt and _prt
322 using amrex::Math::powi;
323
324 // length of the current slice
325 amrex::ParticleReal const slice_ds = m_ds / nslice();
326
327 // access reference particle values to find beta*gamma^2
328 amrex::ParticleReal const pt_ref = refpart.pt;
329 amrex::ParticleReal const betgam2 = powi<2>(pt_ref) - 1_prt;
330 amrex::ParticleReal const bet = refpart.beta();
331
332 // initialize linear map matrix elements
334
335 // treat the special case of zero field
336 if (m_rc==0.0_prt) {
337 R(1,2) = slice_ds;
338 R(3,4) = slice_ds;
339 R(5,6) = slice_ds / betgam2;
340
341 } else {
342
343 // calculate expensive terms once
344 amrex::ParticleReal const theta = slice_ds/m_rc;
345 auto const [sin_theta, cos_theta] = amrex::Math::sincos(theta);
346
347 // assign linear map matrix elements
348 R(1,1) = cos_theta;
349 R(1,2) = m_rc * sin_theta;
350 R(1,6) = - ( m_rc / bet) * (1_prt - cos_theta);
351 R(2,1) = -sin_theta / m_rc;
352 R(2,2) = cos_theta;
353 R(2,6) = - sin_theta / bet;
354 R(3,4) = m_rc * theta;
355 R(5,1) = sin_theta / bet;
356 R(5,2) = m_rc / bet * (1_prt - cos_theta);
357 R(5,6) = m_rc * (-theta + sin_theta / (bet * bet));
358 }
359
360 // apply the transverse rotation (roll) alignment error
361 return rotate_aligned_map(R);
362 }
363
364
379 template<typename T_Real=amrex::ParticleReal, typename T_IdCpu=uint64_t>
382 T_Real & AMREX_RESTRICT x,
383 T_Real & AMREX_RESTRICT y,
384 T_Real & AMREX_RESTRICT t,
385 T_Real & AMREX_RESTRICT px,
386 T_Real const & AMREX_RESTRICT py,
387 T_Real const & AMREX_RESTRICT pt,
388 T_Real & AMREX_RESTRICT sx,
389 T_Real & AMREX_RESTRICT sy,
390 T_Real & AMREX_RESTRICT sz,
391 T_IdCpu & AMREX_RESTRICT idcpu,
392 RefPart const & AMREX_RESTRICT refpart
393 ) const
394 {
395 using namespace amrex::literals; // for _rt and _prt
396 using namespace std; // for cmath(float)
397
398 // special case of zero field: exact drift, spin is unaffected
399 if (m_phi == 0_prt && m_B == 0_prt) {
400 (*this)(x, y, t, px, py, pt, idcpu, refpart);
401 return;
402 }
403
404 // initialize the three components of the axis-angle vector
405 T_Real lambdax = 0_prt;
406 T_Real lambday = 0_prt;
407 T_Real lambdaz = 0_prt;
408
409 // store initial variables needed for spin push
410 T_Real const ti = t;
411 T_Real const Pnorm = sqrt(1_prt + pt*pt - 2_prt*pt*m_ibeta);
412 T_Real const gamma = (1_prt - pt*m_beta)*refpart.gamma();
413 T_Real const ux = px/Pnorm;
414 T_Real const uy = py/Pnorm;
415 T_Real const uz = sqrt(1_prt - ux*ux - uy*uy);
416
417 // For this element, note that the time-of-flight from entry to
418 // exit is needed for evaluation of the spin map, so the phase
419 // space push is applied first, in order to obtain the change in t.
420
421 // phase space push
422 (*this)(x, y, t, px, py, pt, idcpu, refpart);
423
424 // particle transit time through the slice
425 T_Real const dt = t - ti + m_slice_phi * m_rc * m_ibeta;
426
427 // advance in cyclotron phase
428 T_Real const theta = dt / m_rc * m_beta_gamma / gamma;
429
430 lambdax = m_gyro * theta * (gamma - 1_prt) * uy * ux;
431 lambday = m_gyro * theta * ( -gamma + (gamma - 1_prt) * uy * uy );
432 lambdaz = m_gyro * theta * (gamma - 1_prt) * uy * uz;
433
434 // push the spin vector using the generator just determined (first factor)
435 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
436
437 lambdax = 0_prt;
438 lambday = m_slice_phi - theta;
439 lambdaz = 0_prt;
440
441 // push the spin vector using the generator just determined (second factor)
442 rotate_spin(lambdax,lambday,lambdaz,sx,sy,sz);
443
444 }
445
446
449
450 private:
451 // constants that are independent of the individually tracked particle,
452 // see: compute_constants() to refresh
455
456 };
457
458} // namespace impactx
459
461
462#endif // IMPACTX_EXACTSBEND_H
#define AMREX_FORCE_INLINE
#define AMREX_RESTRICT
#define AMREX_GPU_HOST_DEVICE
#define AMREX_GPU_HOST
Array4< int const > mask
#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)
__host__ __device__ GpuComplex< T > sqrt(const GpuComplex< T > &a_z) 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
__host__ __device__ void make_invalid() const noexcept
static constexpr __host__ __device__ SmallMatrix< T, NRows, NCols, ORDER, StartIndex > Identity() noexcept
Definition ReferenceParticle.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition ReferenceParticle.H:167
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rigidity_Tm() const
Definition ReferenceParticle.H:260
amrex::ParticleReal gyromagnetic_anomaly
anomalous magnetic moment [unitless]
Definition ReferenceParticle.H:45
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition ReferenceParticle.H:151
Definition ExactSbend.H:44
amrex::ParticleReal m_rc
Definition ExactSbend.H:453
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ExactSbend.H:319
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rc(RefPart const &refpart) const
Definition ExactSbend.H:93
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 const &AMREX_RESTRICT py, T_Real const &AMREX_RESTRICT pt, T_IdCpu &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition ExactSbend.H:158
amrex::ParticleReal m_cos_phi
Definition ExactSbend.H:453
amrex::ParticleReal m_slice_ds
magnetic field in T
Definition ExactSbend.H:453
amrex::ParticleReal m_beta_gamma
Definition ExactSbend.H:454
amrex::ParticleReal m_beta
Definition ExactSbend.H:454
void reverse()
Definition ExactSbend.H:101
amrex::ParticleReal m_sin_phi
Definition ExactSbend.H:453
ImpactXParticleContainer::ParticleType PType
Definition ExactSbend.H:46
ExactSbend(amrex::ParticleReal ds, amrex::ParticleReal phi, amrex::ParticleReal B, 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 ExactSbend.H:71
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 const &AMREX_RESTRICT py, T_Real const &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 ExactSbend.H:381
amrex::ParticleReal m_gyro
Definition ExactSbend.H:454
amrex::ParticleReal m_ibetagam2
Definition ExactSbend.H:453
amrex::ParticleReal m_ibeta
Definition ExactSbend.H:453
void compute_constants(RefPart const &refpart)
Definition ExactSbend.H:114
amrex::ParticleReal m_slice_phi
Definition ExactSbend.H:453
static constexpr auto type
Definition ExactSbend.H:45
amrex::ParticleReal m_B
bend angle in radians
Definition ExactSbend.H:448
amrex::ParticleReal m_phi
Definition ExactSbend.H:447
Definition alignment.H:29
static constexpr amrex::ParticleReal degree2rad
Definition alignment.H:30
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
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void rotate_spin(T_Real const &AMREX_RESTRICT lambdax, T_Real const &AMREX_RESTRICT lambday, T_Real const &AMREX_RESTRICT lambdaz, T_Real &AMREX_RESTRICT sx, T_Real &AMREX_RESTRICT sy, T_Real &AMREX_RESTRICT sz) const
Definition spintransport.H:48
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