Skip to content

Commit 3ccf3fd

Browse files
committed
Make Drift Fast
1 parent e9e1ff3 commit 3ccf3fd

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/elements/Drift.H

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,31 @@ namespace impactx::elements
7272
/** Push all particles */
7373
using BeamOptic::operator();
7474

75+
/** Compute and cache the constants for the push.
76+
*
77+
* In particular, used to pre-compute and cache variables that are
78+
* independent of the individually tracked particle.
79+
*
80+
* @param refpart reference particle
81+
*/
82+
void compute_constants (RefPart const & refpart)
83+
{
84+
using namespace amrex::literals; // for _rt and _prt
85+
86+
Alignment::compute_constants(refpart);
87+
88+
// length of the current slice
89+
m_slice_ds = m_ds / nslice();
90+
91+
// find beta*gamma^2
92+
m_betgam2 = std::pow(refpart.pt, 2) - 1.0_prt;
93+
94+
m_slice_bg = m_slice_ds / m_betgam2;
95+
}
96+
7597
/** This is a drift functor, so that a variable of this type can be used like a drift function.
98+
*
99+
* The @see compute_constants method must be called before pushing particles through this operator.
76100
*
77101
* @param x particle position in x
78102
* @param y particle position in y
@@ -81,7 +105,7 @@ namespace impactx::elements
81105
* @param py particle momentum in y
82106
* @param pt particle momentum in t
83107
* @param idcpu particle global index
84-
* @param refpart reference particle
108+
* @param refpart reference particle (unused)
85109
*/
86110
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
87111
void operator() (
@@ -92,7 +116,7 @@ namespace impactx::elements
92116
amrex::ParticleReal & AMREX_RESTRICT py,
93117
amrex::ParticleReal & AMREX_RESTRICT pt,
94118
uint64_t & AMREX_RESTRICT idcpu,
95-
RefPart const & refpart
119+
[[maybe_unused]] RefPart const & AMREX_RESTRICT refpart
96120
) const
97121
{
98122
using namespace amrex::literals; // for _rt and _prt
@@ -108,19 +132,12 @@ namespace impactx::elements
108132
amrex::ParticleReal pyout = py;
109133
amrex::ParticleReal ptout = pt;
110134

111-
// length of the current slice
112-
amrex::ParticleReal const slice_ds = m_ds / nslice();
113-
114-
// access reference particle values to find beta*gamma^2
115-
amrex::ParticleReal const pt_ref = refpart.pt;
116-
amrex::ParticleReal const betgam2 = std::pow(pt_ref, 2) - 1.0_prt;
117-
118135
// advance position and momentum (drift)
119-
xout = x + slice_ds * px;
136+
xout = x + m_slice_ds * px;
120137
// pxout = px;
121-
yout = y + slice_ds * py;
138+
yout = y + m_slice_ds * py;
122139
// pyout = py;
123-
tout = t + (slice_ds/betgam2) * pt;
140+
tout = t + m_slice_bg * pt;
124141
// ptout = pt;
125142

126143
// assign updated values
@@ -202,6 +219,13 @@ namespace impactx::elements
202219

203220
return R;
204221
}
222+
223+
private:
224+
// constants that are independent of the individually tracked particle,
225+
// see: compute_constants() to refresh
226+
amrex::ParticleReal m_slice_ds; //! m_ds / nslice();
227+
amrex::ParticleReal m_betgam2; //! beta*gamma^2
228+
amrex::ParticleReal m_slice_bg; //! m_slice_ds / m_betgam2
205229
};
206230

207231
} // namespace impactx

0 commit comments

Comments
 (0)