Skip to content

Commit 3c74ef7

Browse files
committed
[Hack] Make Drift Fast
1 parent 285fa94 commit 3c74ef7

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/elements/Drift.H

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

75+
void calc_constants (RefPart const & refpart)
76+
{
77+
using namespace amrex::literals; // for _rt and _prt
78+
79+
// length of the current slice
80+
m_slice_ds = m_ds / nslice();
81+
82+
amrex::ParticleReal const pt_ref = refpart.pt;
83+
// find beta*gamma^2
84+
m_betgam2 = std::pow(pt_ref, 2) - 1.0_prt;
85+
}
86+
7587
/** This is a drift functor, so that a variable of this type can be used like a drift function.
7688
*
7789
* @param x particle position in x
@@ -92,7 +104,7 @@ namespace impactx::elements
92104
amrex::ParticleReal & AMREX_RESTRICT py,
93105
amrex::ParticleReal & AMREX_RESTRICT pt,
94106
uint64_t & AMREX_RESTRICT idcpu,
95-
RefPart const & refpart
107+
RefPart const &
96108
) const
97109
{
98110
using namespace amrex::literals; // for _rt and _prt
@@ -108,19 +120,12 @@ namespace impactx::elements
108120
amrex::ParticleReal pyout = py;
109121
amrex::ParticleReal ptout = pt;
110122

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-
118123
// advance position and momentum (drift)
119-
xout = x + slice_ds * px;
124+
xout = x + m_slice_ds * px;
120125
// pxout = px;
121-
yout = y + slice_ds * py;
126+
yout = y + m_slice_ds * py;
122127
// pyout = py;
123-
tout = t + (slice_ds/betgam2) * pt;
128+
tout = t + (m_slice_ds/m_betgam2) * pt;
124129
// ptout = pt;
125130

126131
// assign updated values
@@ -202,6 +207,11 @@ namespace impactx::elements
202207

203208
return R;
204209
}
210+
211+
private:
212+
// constants
213+
amrex::ParticleReal m_slice_ds; //! m_ds / nslice();
214+
amrex::ParticleReal m_betgam2; //! beta*gamma^2
205215
};
206216

207217
} // namespace impactx

src/elements/mixin/beamoptic.H

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
namespace impactx::elements {
23+
struct Drift;
2324
struct Quad;
2425
}
2526

@@ -137,7 +138,8 @@ namespace detail
137138

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

140-
if constexpr (std::is_same_v<std::decay_t<T_Element>, elements::Quad>)
141+
if constexpr (std::is_same_v<std::decay_t<T_Element>, elements::Quad> ||
142+
std::is_same_v<std::decay_t<T_Element>, elements::Drift>)
141143
{
142144
element.calc_constants(ref_part);
143145
}

0 commit comments

Comments
 (0)