10#ifndef IMPACTX_EXACTCFBEND_H
11#define IMPACTX_EXACTCFBEND_H
48 inline std::map<int, std::vector<amrex::ParticleReal>>
h_k_normal = {};
50 inline std::map<int, std::vector<amrex::ParticleReal>>
h_k_skew = {};
53 inline std::map<int, amrex::Gpu::DeviceVector<amrex::ParticleReal>>
d_k_normal = {};
55 inline std::map<int, amrex::Gpu::DeviceVector<amrex::ParticleReal>>
d_k_skew = {};
69 static constexpr auto type =
"ExactCFbend";
98 amrex::ParticleReal
ds,
99 std::vector<amrex::ParticleReal> k_normal,
100 std::vector<amrex::ParticleReal> k_skew,
102 amrex::ParticleReal
dx = 0,
103 amrex::ParticleReal
dy = 0,
104 amrex::ParticleReal rotation_degree = 0,
110 std::optional<std::string>
name = std::nullopt
123 throw std::runtime_error(
"ExactCFbend: The order used for symplectic integration must be 2, 4 or 6.");
126 m_ncoef = int(k_normal.size());
127 if (
m_ncoef !=
int(k_skew.size()))
128 throw std::runtime_error(
"ExactMultipole: normal and skew coefficient arrays must have same length!");
140 k_normal.begin(), k_normal.end(),
143 k_skew.begin(), k_skew.end(),
154 using BeamOptic::operator();
165 using namespace amrex::literals;
167 Alignment::compute_constants(refpart);
178 amrex::ParticleReal curvature =
m_unit == 1 ? k_normal[0] /
m_brho : k_normal[0];
179 m_rc = curvature == 0.0 ? 0.0_prt : 1.0_prt/curvature;
211 using namespace amrex::literals;
217 amrex::ParticleReal
const zin = 0_prt;
259 void map1 (amrex::ParticleReal
const tau,
261 amrex::ParticleReal & zeval)
const
263 using namespace amrex::literals;
266 amrex::ParticleReal
const x = particle(1);
267 amrex::ParticleReal
const px = particle(2);
268 amrex::ParticleReal
const y = particle(3);
269 amrex::ParticleReal
const py = particle(4);
270 amrex::ParticleReal
const t = particle(5);
271 amrex::ParticleReal
const pt = particle(6);
273 amrex::ParticleReal xout = x;
274 amrex::ParticleReal pxout = px;
275 amrex::ParticleReal yout = y;
276 amrex::ParticleReal pyout = py;
277 amrex::ParticleReal tout =
t;
278 amrex::ParticleReal ptout = pt;
283 amrex::ParticleReal
const inv_pzden = 1_prt / std::sqrt(
291 xout = x + tau * px * inv_pzden;
292 yout = y + tau * py * inv_pzden;
304 amrex::ParticleReal
const px2 =
powi<2>(px);
307 amrex::ParticleReal
const slice_phi = tau /
m_rc;
313 amrex::ParticleReal
const pperp = std::sqrt(pperp2);
314 amrex::ParticleReal
const pzi = std::sqrt(
powi<2>(pperp) -
powi<2>(px));
315 amrex::ParticleReal
const rho =
m_rc + x;
318 pxout = px * cos_phi + (pzi - rho /
m_rc) * sin_phi;
323 amrex::ParticleReal
const px2f =
powi<2>(pxout);
327 amrex::ParticleReal
const pzf = std::sqrt(
powi<2>(pperp)-
powi<2>(pxout));
328 amrex::ParticleReal
const theta = slice_phi + std::asin(px/pperp) - std::asin(pxout/pperp);
331 xout = -
m_rc + rho*cos_phi +
m_rc*(pzf + px*sin_phi - pzi*cos_phi);
332 yout = y + theta *
m_rc * py;
360 void map2 (amrex::ParticleReal
const tau,
362 amrex::ParticleReal & zeval)
const
364 using namespace amrex::literals;
367 amrex::ParticleReal
const x = particle(1);
368 amrex::ParticleReal
const px = particle(2);
369 amrex::ParticleReal
const y = particle(3);
370 amrex::ParticleReal
const py = particle(4);
371 amrex::ParticleReal
const t = particle(5);
372 amrex::ParticleReal
const pt = particle(6);
374 amrex::ParticleReal xout = x;
375 amrex::ParticleReal pxout = px;
376 amrex::ParticleReal yout = y;
377 amrex::ParticleReal pyout = py;
378 amrex::ParticleReal tout =
t;
379 amrex::ParticleReal ptout = pt;
385 amrex::ParticleReal rho = 1_prt + x /
m_rc;
386 amrex::ParticleReal y_scl = y /
m_rc;
387 amrex::ParticleReal ln_rho = std::log(rho);
390 amrex::ParticleReal dpx = 0_prt;
391 amrex::ParticleReal dpy = 0_prt;
394 for (
int m = 1; m <
m_ncoef; m++) {
397 amrex::ParticleReal kn =
m_unit == 1 ? k_normal[m] /
m_brho : k_normal[m];
398 amrex::ParticleReal ks =
m_unit == 1 ? k_skew[m] /
m_brho : k_skew[m];
405 kn = std::pow(
m_rc, m) * kn;
406 ks = std::pow(
m_rc, m) * ks;
415 dpx += -rho*ln_rho*kn + y_scl*rho*ks;
416 dpy += y_scl*kn + 0.5_prt*(rho*rho - 1_prt)*ks;
419 dpx += 0.25_prt*rho*(1_prt + 2_prt*y_scl*y_scl - rho*rho + 2_prt*ln_rho)*kn + y_scl*rho*ln_rho*ks;
420 dpy += 0.5_prt*y_scl*(rho*rho - 1_prt)*kn + 0.25_prt*(1_prt - 2_prt*y_scl*y_scl - rho*rho + 2_prt*rho*rho*ln_rho)*ks;
423 dpx += -0.25_prt*(rho -
powi<3>(rho) + (rho - 2_prt*y_scl*y_scl*rho +
powi<3>(rho))*ln_rho)*kn
424 -1_prt/12_prt*y_scl*rho*(3_prt + 2_prt*y_scl*y_scl - 3_prt*rho*rho + 6_prt*ln_rho)*ks;
425 dpy += -1_prt/12_prt*y_scl*(-3_prt + 2_prt*y_scl*y_scl + 3_prt*rho*rho - 6_prt*rho*rho*ln_rho)*kn
426 -1_prt/16_prt*((-1_prt + 4_prt*y_scl*y_scl - rho*rho)*(-1_prt + rho*rho) + 4_prt*rho*rho*ln_rho)*ks;
430 dpx += -1_prt/192_prt*rho*(8_prt*
powi<4>(y_scl) - 24_prt*
powi<2>(y_scl)*(-1_prt + rho*rho)
432 +12_prt*(-1_prt + 4_prt*y_scl*y_scl - 2_prt*rho*rho)*ln_rho)*kn
433 +1_prt/12_prt*y_scl*rho*(3_prt*(-1_prt + rho*rho) + (2_prt*y_scl*y_scl - 3_prt*(1_prt + rho*rho))*ln_rho)*ks;
434 dpy += -1_prt/48_prt*y_scl*((-1_prt + rho*rho)*(4_prt*y_scl*y_scl - 3_prt*(1_prt + rho*rho)) + 12_prt*rho*rho*ln_rho)*kn
435 + 1_prt/192_prt*(3_prt + 8_prt*
powi<4>(y_scl) + 12_prt*rho*rho
436 - 15_prt*
powi<4>(rho) + 24_prt*y_scl*y_scl*(-1_prt + rho*rho)
437 +12_prt*rho*rho*(2_prt - 4_prt*y_scl*y_scl + rho*rho)*ln_rho)*ks;
446 pxout = pxout + tau * dpx;
447 pyout = pyout + tau * dpy;
468 using namespace amrex::literals;
471 amrex::ParticleReal
const x = refpart.x;
472 amrex::ParticleReal
const px = refpart.px;
473 amrex::ParticleReal
const y = refpart.y;
474 amrex::ParticleReal
const py = refpart.py;
475 amrex::ParticleReal
const z = refpart.z;
476 amrex::ParticleReal
const pz = refpart.pz;
477 amrex::ParticleReal
const t = refpart.t;
478 amrex::ParticleReal
const pt = refpart.pt;
479 amrex::ParticleReal
const s = refpart.s;
480 amrex::ParticleReal
const brho = refpart.rigidity_Tm();
482#if AMREX_DEVICE_COMPILE
488 amrex::ParticleReal
const curvature =
m_unit == 1 ? k_normal[0] / brho : k_normal[0];
489 amrex::ParticleReal
const rc = curvature == 0.0 ? 0.0_prt : 1.0_prt/curvature;
492 amrex::ParticleReal
const slice_ds =
m_ds /
nslice();
498 refpart.x = x + step*px;
499 refpart.y = y + step*py;
500 refpart.z = z + step*pz;
501 refpart.t =
t - step*pt;
506 amrex::ParticleReal
const B = refpart.beta_gamma() /rc;
507 amrex::ParticleReal
const theta = slice_ds / rc;
513 refpart.px = px*cos_theta - pz*sin_theta;
515 refpart.pz = pz*cos_theta + px*sin_theta;
518 refpart.x = x + (refpart.pz - pz)/B;
519 refpart.y = y + (theta/B)*py;
520 refpart.z = z - (refpart.px - px)/B;
521 refpart.t =
t - (theta/B)*pt;
526 refpart.s =
s + slice_ds;
539 using namespace amrex::literals;
542 amrex::ParticleReal
const slice_ds =
m_ds /
nslice();
546 amrex::ParticleReal
const bet = refpart.beta();
547 amrex::ParticleReal
const ibetgam2 = 1_prt / betgam2;
552 amrex::ParticleReal
const curvature =
m_unit == 1 ? k_normal[0] /
m_brho : k_normal[0];
553 amrex::ParticleReal
const rc = curvature == 0.0 ? 0.0_prt : 1.0_prt/curvature;
554 amrex::ParticleReal
const kn =
m_unit == 1 ? k_normal[1] /
m_brho : k_normal[1];
559 amrex::ParticleReal
const omega_x = std::sqrt(std::abs(gx));
562 amrex::ParticleReal
const gy = -kn;
563 amrex::ParticleReal
const omega_y = std::sqrt(std::abs(gy));
567 amrex::ParticleReal
const sinhx = std::sinh(omega_x * slice_ds);
568 amrex::ParticleReal
const coshx = std::cosh(omega_x * slice_ds);
570 amrex::ParticleReal
const sinhy = std::sinh(omega_y * slice_ds);
571 amrex::ParticleReal
const coshy = std::cosh(omega_y * slice_ds);
573 amrex::ParticleReal
const igbrc = 1_prt / (gx * bet * rc);
574 amrex::ParticleReal
const iobrc = 1_prt / (omega_x * bet * rc);
575 amrex::ParticleReal
const igobr = 1_prt / (gx * omega_x * b2rc2);
580 R(1,1) = gx > 0_prt ? cosx : coshx;
581 R(1,2) = gx > 0_prt ? sinx / omega_x : sinhx / omega_x;
582 R(1,6) = gx > 0_prt ? -(1_prt - cosx) * igbrc : -(1_prt - coshx) * igbrc;
583 R(2,1) = gx > 0_prt ? -omega_x * sinx : omega_x * sinhx;
584 R(2,2) = gx > 0_prt ? cosx : coshx;
585 R(2,6) = gx > 0_prt ? -sinx * iobrc : -sinhx * iobrc;
586 R(3,3) = gy > 0_prt ? cosy : coshy;
587 R(3,4) = gy > 0_prt ? siny / omega_y : sinhy / omega_y;
588 R(4,3) = gy > 0_prt ? -omega_y * siny : omega_y * sinhy;
589 R(4,4) = gy > 0_prt ? cosy : coshy;
590 R(5,1) = gx > 0_prt ? sinx * iobrc : sinhx * iobrc;
591 R(5,2) = gx > 0_prt ? (1_prt - cosx) * igbrc : (1_prt - coshx) * igbrc;
592 R(5,6) = gx > 0_prt ?
593 slice_ds * ibetgam2 + (sinx - omega_x * slice_ds) * igobr :
594 slice_ds * ibetgam2 + (sinhx - omega_x * slice_ds) * igobr;
600 using LinearTransport::operator();
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
T_ParticleType ParticleType
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
static constexpr HostToDevice hostToDevice
void streamSynchronize() noexcept
PODVector< T, ArenaAllocator< T > > DeviceVector
__host__ __device__ std::pair< double, double > sincos(double x)
constexpr T powi(T x) noexcept
SmallMatrix< T, N, 1, Order::F, StartIndex > SmallVector
Definition ExactCFbend.H:43
std::map< int, amrex::Gpu::DeviceVector< amrex::ParticleReal > > d_k_normal
device: normal multipole coefficients of the magnetic field
Definition ExactCFbend.H:53
int next_id
last used id for a created ExactCFbend
Definition ExactCFbend.H:45
std::map< int, amrex::Gpu::DeviceVector< amrex::ParticleReal > > d_k_skew
device: skew multipole coefficients of the magnetic field
Definition ExactCFbend.H:55
std::map< int, std::vector< amrex::ParticleReal > > h_k_normal
host: normal multipole coefficients of the magnetic field
Definition ExactCFbend.H:48
std::map< int, std::vector< amrex::ParticleReal > > h_k_skew
host: skew multipole coefficients of the magnetic field
Definition ExactCFbend.H:50
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void symp4_integrate_particle(amrex::SmallVector< T_Real, 6, 1 > &particle, amrex::ParticleReal const zin, amrex::ParticleReal const zout, int const nsteps, T_Element const &element)
Definition Integrators.H:225
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void symp6_integrate_particle(amrex::SmallVector< T_Real, 6, 1 > &particle, amrex::ParticleReal const zin, amrex::ParticleReal const zout, int const nsteps, T_Element const &element)
Definition Integrators.H:284
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void symp2_integrate_particle(amrex::SmallVector< T_Real, 6, 1 > &particle, amrex::ParticleReal const zin, amrex::ParticleReal const zout, int const nsteps, T_Element const &element)
Definition Integrators.H:178
@ 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:31
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta_gamma() const
Definition ReferenceParticle.H:110
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal rigidity_Tm() const
Definition ReferenceParticle.H:203
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::ParticleReal beta() const
Definition ReferenceParticle.H:94
ImpactXParticleContainer::ParticleType PType
Definition ExactCFbend.H:70
int m_int_order
unit specification for Multipole strength
Definition ExactCFbend.H:621
amrex::ParticleReal m_ibeta
beta
Definition ExactCFbend.H:637
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map1(amrex::ParticleReal const tau, amrex::SmallVector< amrex::ParticleReal, 6, 1 > &particle, amrex::ParticleReal &zeval) const
Definition ExactCFbend.H:259
amrex::ParticleReal * m_k_skew_h_data
non-owning pointer to host cosine coefficients
Definition ExactCFbend.H:627
int m_id
number of integration steps per slice
Definition ExactCFbend.H:623
amrex::ParticleReal m_rc
magnetic ridigity in T-m
Definition ExactCFbend.H:639
int m_mapsteps
order used for the symplectic integration (2 or 4)
Definition ExactCFbend.H:622
amrex::ParticleReal m_beta
1 / m_betgam2
Definition ExactCFbend.H:636
amrex::ParticleReal m_slice_ds
non-owning pointer to device sine coefficients
Definition ExactCFbend.H:634
int m_ncoef
unique ExactMultipole id used for data lookup map
Definition ExactCFbend.H:625
amrex::ParticleReal * m_k_normal_d_data
non-owning pointer to host sine coefficients
Definition ExactCFbend.H:628
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void map2(amrex::ParticleReal const tau, amrex::SmallVector< amrex::ParticleReal, 6, 1 > &particle, amrex::ParticleReal &zeval) const
Definition ExactCFbend.H:360
amrex::ParticleReal * m_k_normal_h_data
number of Fourier coefficients
Definition ExactCFbend.H:626
amrex::ParticleReal * m_k_skew_d_data
non-owning pointer to device cosine coefficients
Definition ExactCFbend.H:629
amrex::ParticleReal m_ibetgam2
m_ds / nslice();
Definition ExactCFbend.H:635
void compute_constants(RefPart const &refpart)
Definition ExactCFbend.H:163
void finalize()
Definition ExactCFbend.H:606
AMREX_GPU_HOST AMREX_FORCE_INLINE Map6x6 transport_map(RefPart const &AMREX_RESTRICT refpart) const
Definition ExactCFbend.H:537
amrex::ParticleReal m_brho
1 / m_beta
Definition ExactCFbend.H:638
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator()(amrex::ParticleReal &AMREX_RESTRICT x, amrex::ParticleReal &AMREX_RESTRICT y, amrex::ParticleReal &AMREX_RESTRICT t, amrex::ParticleReal &AMREX_RESTRICT px, amrex::ParticleReal &AMREX_RESTRICT py, amrex::ParticleReal &AMREX_RESTRICT pt, uint64_t &AMREX_RESTRICT idcpu, RefPart const &AMREX_RESTRICT refpart) const
Definition ExactCFbend.H:200
ExactCFbend(amrex::ParticleReal ds, std::vector< amrex::ParticleReal > k_normal, std::vector< amrex::ParticleReal > k_skew, int unit, amrex::ParticleReal dx=0, amrex::ParticleReal dy=0, amrex::ParticleReal rotation_degree=0, amrex::ParticleReal aperture_x=0, amrex::ParticleReal aperture_y=0, int int_order=2, int mapsteps=1, int nslice=1, std::optional< std::string > name=std::nullopt)
Definition ExactCFbend.H:97
int m_unit
Definition ExactCFbend.H:620
static constexpr auto type
Definition ExactCFbend.H:69
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:219
Definition lineartransport.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
Thick(amrex::ParticleReal ds, int nslice)
Definition thick.H:30
amrex::ParticleReal m_ds
Definition thick.H:58
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