Skip to content

Commit

Permalink
[Hack] Make Drift Fast
Browse files Browse the repository at this point in the history
  • Loading branch information
ax3l committed Feb 12, 2025
1 parent 285fa94 commit 3c74ef7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
32 changes: 21 additions & 11 deletions src/elements/Drift.H
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ namespace impactx::elements
/** Push all particles */
using BeamOptic::operator();

void calc_constants (RefPart const & refpart)
{
using namespace amrex::literals; // for _rt and _prt

// length of the current slice
m_slice_ds = m_ds / nslice();

amrex::ParticleReal const pt_ref = refpart.pt;
// find beta*gamma^2
m_betgam2 = std::pow(pt_ref, 2) - 1.0_prt;
}

/** This is a drift functor, so that a variable of this type can be used like a drift function.
*
* @param x particle position in x
Expand All @@ -92,7 +104,7 @@ namespace impactx::elements
amrex::ParticleReal & AMREX_RESTRICT py,
amrex::ParticleReal & AMREX_RESTRICT pt,
uint64_t & AMREX_RESTRICT idcpu,
RefPart const & refpart
RefPart const &
) const
{
using namespace amrex::literals; // for _rt and _prt
Expand All @@ -108,19 +120,12 @@ namespace impactx::elements
amrex::ParticleReal pyout = py;
amrex::ParticleReal ptout = pt;

// length of the current slice
amrex::ParticleReal const slice_ds = m_ds / nslice();

// access reference particle values to find beta*gamma^2
amrex::ParticleReal const pt_ref = refpart.pt;
amrex::ParticleReal const betgam2 = std::pow(pt_ref, 2) - 1.0_prt;

// advance position and momentum (drift)
xout = x + slice_ds * px;
xout = x + m_slice_ds * px;
// pxout = px;
yout = y + slice_ds * py;
yout = y + m_slice_ds * py;
// pyout = py;
tout = t + (slice_ds/betgam2) * pt;
tout = t + (m_slice_ds/m_betgam2) * pt;
// ptout = pt;

// assign updated values
Expand Down Expand Up @@ -202,6 +207,11 @@ namespace impactx::elements

return R;
}

private:
// constants
amrex::ParticleReal m_slice_ds; //! m_ds / nslice();
amrex::ParticleReal m_betgam2; //! beta*gamma^2
};

} // namespace impactx
Expand Down
4 changes: 3 additions & 1 deletion src/elements/mixin/beamoptic.H
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


namespace impactx::elements {
struct Drift;
struct Quad;
}

Expand Down Expand Up @@ -137,7 +138,8 @@ namespace detail

uint64_t* const AMREX_RESTRICT part_idcpu = pti.GetStructOfArrays().GetIdCPUData().dataPtr();

if constexpr (std::is_same_v<std::decay_t<T_Element>, elements::Quad>)
if constexpr (std::is_same_v<std::decay_t<T_Element>, elements::Quad> ||
std::is_same_v<std::decay_t<T_Element>, elements::Drift>)
{
element.calc_constants(ref_part);
}
Expand Down

0 comments on commit 3c74ef7

Please sign in to comment.