Skip to content

Commit

Permalink
Refactor ParticleState and ParticleParams to SoA (celeritas-project#1012
Browse files Browse the repository at this point in the history
)

* Refactor ParticleState and ParticleParams to SoA

* Removed ParticleRecord

* rename enum

* rename particleparams data member

* change underlying type of MatterType, support Quantity for LdgSpan, use LdgSpan as collection storage when possible

* Move stable_decal_constant to celeritas::constants

---------

Co-authored-by: Seth R. Johnson <johnsonsr@ornl.gov>
  • Loading branch information
esseivaju and sethrj committed Nov 20, 2023
1 parent 82e9d34 commit 5de4bb9
Show file tree
Hide file tree
Showing 22 changed files with 246 additions and 151 deletions.
18 changes: 9 additions & 9 deletions app/demo-interactor/demo-interactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ namespace
std::shared_ptr<ParticleParams> load_params()
{
using namespace celeritas::units;
using namespace constants;
constexpr auto zero = zero_quantity();
constexpr auto stable = ParticleRecord::stable_decay_constant();

return std::make_shared<ParticleParams>(
ParticleParams::Input{{"electron",
pdg::electron(),
MevMass{0.5109989461},
ElementaryCharge{-1},
stable},
{"gamma", pdg::gamma(), zero, zero, stable}});

return std::make_shared<ParticleParams>(ParticleParams::Input{
{"electron",
pdg::electron(),
MevMass{0.5109989461},
ElementaryCharge{-1},
stable_decay_constant},
{"gamma", pdg::gamma(), zero, zero, stable_decay_constant}});
}

//---------------------------------------------------------------------------//
Expand Down
16 changes: 8 additions & 8 deletions app/demo-interactor/host-demo-interactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ namespace app
std::shared_ptr<ParticleParams> load_params()
{
using namespace celeritas::units;
using namespace constants;
constexpr auto zero = zero_quantity();
constexpr auto stable = ParticleRecord::stable_decay_constant();

return std::make_shared<ParticleParams>(
ParticleParams::Input{{"electron",
pdg::electron(),
MevMass{0.5109989461},
ElementaryCharge{-1},
stable},
{"gamma", pdg::gamma(), zero, zero, stable}});
return std::make_shared<ParticleParams>(ParticleParams::Input{
{"electron",
pdg::electron(),
MevMass{0.5109989461},
ElementaryCharge{-1},
stable_decay_constant},
{"gamma", pdg::gamma(), zero, zero, stable_decay_constant}});
}

//---------------------------------------------------------------------------//
Expand Down
5 changes: 5 additions & 0 deletions src/celeritas/Constants.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace constants
* r_electron | classic_electr_radius | Classical electron radius
* kcd_luminous | [none] | Lumens per Watt
* lambdabar_electron | electron_Compton_length | Reduced Compton wavelength
* stable_decay_constant| [none] | Decay for a stable particle
*
* Some experimental physical constants are derived from the other physical
* constants, but for consistency and clarity they are presented numerically
Expand Down Expand Up @@ -81,6 +82,10 @@ CELER_ICRT eh_hartree = 4.3597447222071e-18 / units::meter;
CELER_ICRT lambdabar_electron = 3.8615926796e-13 * units::meter;
//!@}

//!@{
//! \name Other constants
CELER_ICRT stable_decay_constant = 0;
//!@}
#undef CELER_ICRT

//---------------------------------------------------------------------------//
Expand Down
78 changes: 41 additions & 37 deletions src/celeritas/phys/ParticleData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,21 @@ namespace celeritas
//---------------------------------------------------------------------------//
// PARAMS
//---------------------------------------------------------------------------//
/*!
* Fundamental (static) properties of a particle type.
*
* These should only be fundamental physical properties. Setting particles is
* done through the ParticleParams. Physical state of a particle
* (kinetic energy, ...) is part of a ParticleState.
*
* Particle definitions are accessed via the ParticleParams: using PDGs
* to look up particle IDs, etc.
*/
struct ParticleRecord
{
units::MevMass mass; //!< Rest mass [MeV / c^2]
units::ElementaryCharge charge; //!< Charge in units of [e]
real_type decay_constant; //!< Decay constant [1/s]
bool is_antiparticle; //!< Antiparticle (negative PDG number)

//! Value of decay_constant for a stable particle
static CELER_CONSTEXPR_FUNCTION real_type stable_decay_constant()
{
return 0;
}
enum class MatterType : char
{
particle,
antiparticle
};

//---------------------------------------------------------------------------//
/*!
* Access particle definitions on the device.
*
* This view is created from \c ParticleParams. The size of the \c defs data
* member is the number of particle types (accessed by \c ParticleId).
* Fundamental (static) properties of a particle type. Physical state of a
* particle (kinetic energy, ...) is part of a ParticleState. This view is
* created from \c ParticleParams. The size of the \c defs data member is the
* number of particle types (accessed by \c ParticleId).
*
* \sa ParticleParams (owns the pointed-to data)
* \sa ParticleTrackView (uses the pointed-to data in a kernel)
Expand All @@ -59,26 +44,40 @@ struct ParticleParamsData
//// TYPES ////

template<class T>
using Items = celeritas::Collection<T, W, M>;
using Items = celeritas::Collection<T, W, M, ParticleId>;

//// DATA ////

Items<ParticleRecord> particles;
Items<units::MevMass> mass; //!< Rest mass [MeV / c^2]
Items<units::ElementaryCharge> charge; //!< Charge in units of [e]
Items<real_type> decay_constant; //!< Decay constant [1/s]
Items<MatterType> matter; //!< Antiparticle (negative PDG
//!< number)

//// METHODS ////

//! Whether the data is assigned
explicit CELER_FUNCTION operator bool() const
{
return !particles.empty();
return !mass.empty() && !charge.empty() && !decay_constant.empty()
&& !matter.empty();
}

//! Params size
CELER_FUNCTION typename Items<real_type>::size_type size() const
{
return decay_constant.size();
}

//! Assign from another set of data
template<Ownership W2, MemSpace M2>
ParticleParamsData& operator=(ParticleParamsData<W2, M2> const& other)
{
CELER_EXPECT(other);
particles = other.particles;
mass = other.mass;
charge = other.charge;
decay_constant = other.decay_constant;
matter = other.matter;
return *this;
}
};
Expand All @@ -98,11 +97,6 @@ struct ParticleParamsData
* immutable: collisions and other interactions should return changes to the
* particle state.
*/
struct ParticleTrackState
{
ParticleId particle_id; //!< Type of particle (electron, gamma, ...)
real_type energy; //!< Kinetic energy [MeV]
};

struct ParticleTrackInitializer
{
Expand Down Expand Up @@ -132,22 +126,31 @@ struct ParticleStateData

//// DATA ////

Items<ParticleTrackState> state;
Items<ParticleId> particle_id; //!< Type of particle (electron, gamma,
//!< ...)
Items<real_type> particle_energy; //!< Kinetic energy [MeV]

//// METHODS ////

//! Whether the interface is assigned
explicit CELER_FUNCTION operator bool() const { return !state.empty(); }
explicit CELER_FUNCTION operator bool() const
{
return !particle_id.empty() && !particle_energy.empty();
}

//! State size
CELER_FUNCTION TrackSlotId::size_type size() const { return state.size(); }
CELER_FUNCTION TrackSlotId::size_type size() const
{
return particle_id.size();
}

//! Assign from another set of data
template<Ownership W2, MemSpace M2>
ParticleStateData& operator=(ParticleStateData<W2, M2>& other)
{
CELER_EXPECT(other);
state = other.state;
particle_id = other.particle_id;
particle_energy = other.particle_energy;
return *this;
}
};
Expand All @@ -162,7 +165,8 @@ inline void resize(ParticleStateData<Ownership::value, M>* data,
size_type size)
{
CELER_EXPECT(size > 0);
resize(&data->state, size);
resize(&data->particle_id, size);
resize(&data->particle_energy, size);
}

//---------------------------------------------------------------------------//
Expand Down
27 changes: 16 additions & 11 deletions src/celeritas/phys/ParticleParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ParticleParams::from_import(ImportData const& data)
defs[i].mass = units::MevMass(particle.mass);
defs[i].charge = units::ElementaryCharge(particle.charge);
defs[i].decay_constant = (particle.is_stable
? ParticleRecord::stable_decay_constant()
? constants::stable_decay_constant
: 1. / particle.lifetime);
}

Expand Down Expand Up @@ -82,8 +82,14 @@ ParticleParams::ParticleParams(Input const& input)

// Build elements and materials on host.
HostVal<ParticleParamsData> host_data;
auto particles = make_builder(&host_data.particles);
particles.reserve(input.size());
auto mass = make_builder(&host_data.mass);
auto charge = make_builder(&host_data.charge);
auto decay_constant = make_builder(&host_data.decay_constant);
auto matter = make_builder(&host_data.matter);
mass.reserve(input.size());
charge.reserve(input.size());
decay_constant.reserve(input.size());
matter.reserve(input.size());

for (auto const& particle : input)
{
Expand All @@ -105,12 +111,11 @@ ParticleParams::ParticleParams(Input const& input)
md_.push_back({particle.name, particle.pdg_code});

// Save the definitions on the host
ParticleRecord host_def;
host_def.mass = particle.mass;
host_def.charge = particle.charge;
host_def.decay_constant = particle.decay_constant;
host_def.is_antiparticle = particle.pdg_code.get() < 0;
particles.push_back(std::move(host_def));
mass.push_back(particle.mass);
charge.push_back(particle.charge);
decay_constant.push_back(particle.decay_constant);
matter.push_back(particle.pdg_code.get() < 0 ? MatterType::antiparticle
: MatterType::particle);
}

// Move to mirrored data, copying to device
Expand All @@ -119,7 +124,7 @@ ParticleParams::ParticleParams(Input const& input)
CELER_ENSURE(md_.size() == input.size());
CELER_ENSURE(name_to_id_.size() == input.size());
CELER_ENSURE(pdg_to_id_.size() == input.size());
CELER_ENSURE(this->host_ref().particles.size() == input.size());
CELER_ENSURE(this->host_ref().size() == input.size());
}

//---------------------------------------------------------------------------//
Expand All @@ -128,7 +133,7 @@ ParticleParams::ParticleParams(Input const& input)
*/
ParticleView ParticleParams::get(ParticleId id) const
{
CELER_EXPECT(id < this->host_ref().particles.size());
CELER_EXPECT(id < this->host_ref().size());
return ParticleView(this->host_ref(), id);
}

Expand Down
18 changes: 9 additions & 9 deletions src/celeritas/phys/ParticleTrackView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ ParticleTrackView::ParticleTrackView(ParticleParamsRef const& params,
CELER_FUNCTION ParticleTrackView&
ParticleTrackView::operator=(Initializer_t const& other)
{
CELER_EXPECT(other.particle_id < params_.particles.size());
CELER_EXPECT(other.particle_id < params_.size());
CELER_EXPECT(other.energy >= zero_quantity());
states_.state[track_slot_].particle_id = other.particle_id;
states_.state[track_slot_].energy = other.energy.value();
states_.particle_id[track_slot_] = other.particle_id;
states_.particle_energy[track_slot_] = other.energy.value();
return *this;
}

Expand All @@ -153,7 +153,7 @@ void ParticleTrackView::energy(Energy quantity)
{
CELER_EXPECT(this->particle_id());
CELER_EXPECT(quantity >= zero_quantity());
states_.state[track_slot_].energy = quantity.value();
states_.particle_energy[track_slot_] = quantity.value();
}

//---------------------------------------------------------------------------//
Expand All @@ -165,7 +165,7 @@ CELER_FUNCTION void ParticleTrackView::subtract_energy(Energy eloss)
CELER_EXPECT(eloss >= zero_quantity());
CELER_EXPECT(eloss <= this->energy());
// TODO: save a read/write by only saving if eloss is positive?
states_.state[track_slot_].energy -= eloss.value();
states_.particle_energy[track_slot_] -= eloss.value();
}

//---------------------------------------------------------------------------//
Expand All @@ -176,7 +176,7 @@ CELER_FUNCTION void ParticleTrackView::subtract_energy(Energy eloss)
*/
CELER_FUNCTION ParticleId ParticleTrackView::particle_id() const
{
return states_.state[track_slot_].particle_id;
return states_.particle_id[track_slot_];
}

//---------------------------------------------------------------------------//
Expand All @@ -185,7 +185,7 @@ CELER_FUNCTION ParticleId ParticleTrackView::particle_id() const
*/
CELER_FUNCTION auto ParticleTrackView::energy() const -> Energy
{
return Energy{states_.state[track_slot_].energy};
return Energy{states_.particle_energy[track_slot_]};
}

//---------------------------------------------------------------------------//
Expand All @@ -205,7 +205,7 @@ CELER_FUNCTION bool ParticleTrackView::is_stopped() const
*/
CELER_FUNCTION ParticleView ParticleTrackView::particle_view() const
{
return ParticleView(params_, states_.state[track_slot_].particle_id);
return ParticleView(params_, states_.particle_id[track_slot_]);
}

//---------------------------------------------------------------------------//
Expand Down Expand Up @@ -250,7 +250,7 @@ CELER_FUNCTION bool ParticleTrackView::is_antiparticle() const
*/
CELER_FUNCTION bool ParticleTrackView::is_stable() const
{
return this->decay_constant() == ParticleRecord::stable_decay_constant();
return this->decay_constant() == constants::stable_decay_constant;
}

//---------------------------------------------------------------------------//
Expand Down
10 changes: 5 additions & 5 deletions src/celeritas/phys/ParticleView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ CELER_FUNCTION
ParticleView::ParticleView(ParticleParamsRef const& params, ParticleId id)
: params_(params), particle_(id)
{
CELER_EXPECT(particle_ < params_.particles.size());
CELER_EXPECT(particle_ < params_.size());
}

//---------------------------------------------------------------------------//
Expand All @@ -78,7 +78,7 @@ CELER_FUNCTION ParticleId ParticleView::particle_id() const
*/
CELER_FUNCTION units::MevMass ParticleView::mass() const
{
return params_.particles[particle_].mass;
return params_.mass[particle_];
}

//---------------------------------------------------------------------------//
Expand All @@ -87,7 +87,7 @@ CELER_FUNCTION units::MevMass ParticleView::mass() const
*/
CELER_FUNCTION units::ElementaryCharge ParticleView::charge() const
{
return params_.particles[particle_].charge;
return params_.charge[particle_];
}

//---------------------------------------------------------------------------//
Expand All @@ -96,7 +96,7 @@ CELER_FUNCTION units::ElementaryCharge ParticleView::charge() const
*/
CELER_FUNCTION real_type ParticleView::decay_constant() const
{
return params_.particles[particle_].decay_constant;
return params_.decay_constant[particle_];
}

//---------------------------------------------------------------------------//
Expand All @@ -105,7 +105,7 @@ CELER_FUNCTION real_type ParticleView::decay_constant() const
*/
CELER_FUNCTION bool ParticleView::is_antiparticle() const
{
return params_.particles[particle_].is_antiparticle;
return params_.matter[particle_] == MatterType::antiparticle;
}

//---------------------------------------------------------------------------//
Expand Down

0 comments on commit 5de4bb9

Please sign in to comment.