Skip to content

Commit

Permalink
Add more assertions and minor fixes (#730)
Browse files Browse the repository at this point in the history
* Check for valid track initializer
* Add more assertions
* Require track initializer to have positive energy
* Add a should-be-unnecessary assertion to the StackAllocator
* Fix comparison
* Fix CELER_UNLIKELY macro for when __builtin_expect() isn't available
  • Loading branch information
amandalund authored Apr 21, 2023
1 parent 98d7ef8 commit 2f3d45a
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/celeritas/ext/VecgeomTrackView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ VecgeomTrackView::VecgeomTrackView(ParamsRef const& params,
CELER_FUNCTION VecgeomTrackView&
VecgeomTrackView::operator=(Initializer_t const& init)
{
CELER_EXPECT(is_soft_unit_vector(init.dir));

// Initialize position/direction
pos_ = init.pos;
dir_ = init.dir;
Expand Down Expand Up @@ -213,6 +215,8 @@ VecgeomTrackView::operator=(Initializer_t const& init)
CELER_FUNCTION
VecgeomTrackView& VecgeomTrackView::operator=(DetailedInitializer const& init)
{
CELER_EXPECT(is_soft_unit_vector(init.dir));

if (this != &init.other)
{
// Copy the navigation state and position from the parent state
Expand Down
6 changes: 3 additions & 3 deletions src/celeritas/mat/MaterialParamsOutput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Construct from shared geometry data.
* Construct from shared material data.
*/
MaterialParamsOutput::MaterialParamsOutput(SPConstMaterialParams geo)
: material_(std::move(geo))
MaterialParamsOutput::MaterialParamsOutput(SPConstMaterialParams material)
: material_(std::move(material))
{
CELER_EXPECT(material_);
}
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/mat/MaterialParamsOutput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MaterialParamsOutput final : public OutputInterface
//!@}

public:
// Construct from shared materialmetry data
// Construct from shared material data
explicit MaterialParamsOutput(SPConstMaterialParams material);

//! Category of data to write
Expand Down
6 changes: 6 additions & 0 deletions src/celeritas/phys/ParticleData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ struct ParticleTrackInitializer
{
ParticleId particle_id; //!< Type of particle (electron, gamma, ...)
units::MevEnergy energy; //!< Kinetic energy [MeV]

//! True if assigned and valid
explicit CELER_FUNCTION operator bool() const
{
return particle_id && energy > zero_quantity();
}
};

//---------------------------------------------------------------------------//
Expand Down
6 changes: 6 additions & 0 deletions src/celeritas/track/SimData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ struct SimTrackInitializer

TrackStatus status{TrackStatus::inactive};
StepLimit step_limit;

//! True if assigned and valid
explicit CELER_FUNCTION operator bool() const
{
return track_id && event_id;
}
};

//---------------------------------------------------------------------------//
Expand Down
6 changes: 6 additions & 0 deletions src/celeritas/track/TrackInitData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ struct TrackInitializer
SimTrackInitializer sim;
GeoTrackInitializer geo;
ParticleTrackInitializer particle;

//! True if assigned and valid
explicit CELER_FUNCTION operator bool() const
{
return sim && geo && particle;
}
};

//---------------------------------------------------------------------------//
Expand Down
4 changes: 4 additions & 0 deletions src/celeritas/track/detail/ProcessSecondariesLauncher.hh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ ProcessSecondariesLauncher<M>::operator()(TrackSlotId tid) const
{
if (secondary)
{
CELER_ASSERT(secondary.energy > zero_quantity()
&& is_soft_unit_vector(secondary.direction));

// Particles should not be making secondaries while crossing a
// surface
GeoTrackView geo(params_.geometry, states_.geometry, tid);
Expand All @@ -128,6 +131,7 @@ ProcessSecondariesLauncher<M>::operator()(TrackSlotId tid) const
ti.geo.dir = secondary.direction;
ti.particle.particle_id = secondary.particle_id;
ti.particle.energy = secondary.energy;
CELER_ASSERT(ti);

if (!initialized && sim.status() != TrackStatus::alive)
{
Expand Down
2 changes: 1 addition & 1 deletion src/corecel/Macros.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
# define CELER_UNLIKELY(COND) __builtin_expect(!!(COND), 0)
#else
// No other compilers seem to have a similar builtin
# define CELER_UNLIKELY(COND) COND
# define CELER_UNLIKELY(COND) (COND)
#endif

/*!
Expand Down
3 changes: 3 additions & 0 deletions src/corecel/data/StackAllocator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ CELER_FUNCTION auto StackAllocator<T>::operator()(size_type count)
for (size_type i = 1; i < count; ++i)
{
// Initialize remaining values
// XXX see #639: something is causing \c start to change unexpectedly,
// which leads to values being initialized at the wrong memory location
CELER_ASSERT(&data_.storage[StorageId{start + i}] == result + i);
new (&data_.storage[StorageId{start + i}]) value_type;
}
return result;
Expand Down
7 changes: 7 additions & 0 deletions src/orange/Types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "corecel/OpaqueId.hh"
#include "corecel/Types.hh"
#include "corecel/cont/Array.hh"
#include "corecel/math/ArrayUtils.hh"

namespace celeritas
{
Expand Down Expand Up @@ -51,6 +52,12 @@ struct GeoTrackInitializer
{
Real3 pos;
Real3 dir;

//! True if assigned and valid
explicit CELER_FUNCTION operator bool() const
{
return is_soft_unit_vector(dir);
}
};

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

0 comments on commit 2f3d45a

Please sign in to comment.