Skip to content

feat: Add some extra assertions #971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/include/detray/builders/detector_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class detector_builder {
/// functionality
template <class builder_t>
DETRAY_HOST auto decorate(dindex volume_idx) -> builder_t* {
assert(has_volume(volume_idx));

m_volumes[volume_idx] =
std::make_unique<builder_t>(std::move(m_volumes[volume_idx]));
Expand Down
20 changes: 2 additions & 18 deletions core/include/detray/builders/volume_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ class volume_builder : public volume_builder_interface<detector_t> {
std::size_t n_portals{0u};
for (auto& sf_desc : m_surfaces) {

const auto sf = geometry::surface{det, sf_desc};

sf.template visit_mask<detail::mask_index_update>(sf_desc);
det._masks.template visit<detail::mask_index_update>(sf_desc.mask(),
sf_desc);
sf_desc.set_volume(m_volume.index());
sf_desc.update_transform(trf_offset);
sf_desc.set_index(sf_offset++);
Expand Down Expand Up @@ -297,21 +296,6 @@ struct mask_index_update {
}
};

/// TODO: Remove once the material builder is used everywhere
/// A functor to update the material index in surface objects
struct material_index_update {

template <typename group_t, typename index_t, typename surface_t>
DETRAY_HOST inline void operator()(
[[maybe_unused]] const group_t& group,
[[maybe_unused]] const index_t& /*index*/,
[[maybe_unused]] surface_t& sf) const {
if constexpr (!concepts::grid<typename group_t::value_type>) {
sf.update_material(static_cast<dindex>(group.size()));
}
}
};

} // namespace detail

} // namespace detray
29 changes: 22 additions & 7 deletions core/include/detray/geometry/surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class surface {
/// @param desc from that detector.
DETRAY_HOST_DEVICE
constexpr surface(const detector_t &det, const descr_t &desc)
: m_detector{det}, m_desc{desc} {}
: m_detector{det}, m_desc{desc} {
assert(!m_desc.barcode().is_invalid());
assert(m_desc.index() < det.surfaces().size());
assert(m_desc.transform() < det.transform_store().size());
}

/// Constructor from detector @param det and barcode @param bcd in
/// that detector.
Expand Down Expand Up @@ -84,20 +88,30 @@ class surface {
/// @returns the surface barcode
DETRAY_HOST_DEVICE
constexpr auto barcode() const -> geometry::barcode {
assert(!m_desc.barcode().is_invalid());
return m_desc.barcode();
}

/// @returns the index of the mother volume
DETRAY_HOST_DEVICE
constexpr auto volume() const -> dindex { return barcode().volume(); }
constexpr auto volume() const -> dindex {
assert(barcode().volume() < m_detector.volumes().size());
return barcode().volume();
}

/// @returns the index of the surface in the detector surface lookup
DETRAY_HOST_DEVICE
constexpr auto index() const -> dindex { return barcode().index(); }
constexpr auto index() const -> dindex {
assert(barcode().index() < m_detector.surfaces().size());
return barcode().index();
}

/// @returns the surface id (sensitive, passive or portal)
DETRAY_HOST_DEVICE
constexpr auto id() const -> surface_id { return barcode().id(); }
constexpr auto id() const -> surface_id {
assert(barcode().id() != surface_id::e_unknown);
return barcode().id();
}

/// @returns the extra bits in the barcode
DETRAY_HOST_DEVICE
Expand All @@ -110,7 +124,7 @@ class surface {
/// @returns the surface source link
DETRAY_HOST_DEVICE
constexpr auto source() const {
return m_detector.surface(m_desc.barcode()).source;
return m_detector.surface(barcode()).source;
}

/// @returns true if the surface is a senstive detector module.
Expand Down Expand Up @@ -156,6 +170,7 @@ class surface {
DETRAY_HOST_DEVICE
constexpr auto transform(const context &ctx) const
-> const transform3_type & {
assert(m_desc.transform() < m_detector.transform_store().size());
return m_detector.transform_store().at(m_desc.transform(), ctx);
}

Expand Down Expand Up @@ -239,8 +254,8 @@ class surface {
/// @tparam Args types of additional arguments to the functor
template <typename functor_t, typename... Args>
DETRAY_HOST_DEVICE constexpr auto visit_mask(Args &&... args) const {
assert(!m_desc.mask().is_invalid());
const auto &masks = m_detector.mask_store();

return masks.template visit<functor_t>(m_desc.mask(),
std::forward<Args>(args)...);
}
Expand All @@ -251,8 +266,8 @@ class surface {
/// @tparam Args types of additional arguments to the functor
template <typename functor_t, typename... Args>
DETRAY_HOST_DEVICE constexpr auto visit_material(Args &&... args) const {
assert(has_material());
const auto &materials = m_detector.material_store();

return materials.template visit<functor_t>(m_desc.material(),
std::forward<Args>(args)...);
}
Expand Down
7 changes: 5 additions & 2 deletions core/include/detray/geometry/tracking_volume.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class tracking_volume {
/// Constructor from detector @param det and volume descriptor
/// @param vol_idx from that detector.
constexpr tracking_volume(const detector_t &det, const descr_t &desc)
: m_detector{det}, m_desc{desc} {}
: m_detector{det}, m_desc{desc} {
assert(m_desc.index() < det.volumes().size());
assert(m_desc.id() != volume_id::e_unknown);
}

/// Constructor from detector @param det and volume index @param vol_idx in
/// that detector.
Expand Down Expand Up @@ -168,8 +171,8 @@ class tracking_volume {
/// @tparam Args types of additional arguments to the functor
template <typename functor_t, typename... Args>
DETRAY_HOST_DEVICE constexpr auto visit_material(Args &&... args) const {
assert(has_material());
const auto &materials = m_detector.material_store();

return materials.template visit<functor_t>(m_desc.material(),
std::forward<Args>(args)...);
}
Expand Down
3 changes: 3 additions & 0 deletions core/include/detray/navigation/direct_navigator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class direct_navigator {
const context_type &ctx) const {
// Do not resurrect a failed/finished navigation state
assert(navigation.status() > navigation::status::e_on_target);
assert(!track.is_invalid());

if (navigation.is_complete()) {
navigation.m_heartbeat = false;
Expand All @@ -323,6 +324,8 @@ class direct_navigator {
const context_type &ctx = {},
const bool is_before_actor_run = true) const {

assert(!track.is_invalid());

if (navigation.is_complete()) {
navigation.m_heartbeat = false;
return true;
Expand Down
9 changes: 9 additions & 0 deletions core/include/detray/navigation/navigator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,23 @@ class navigator {
DETRAY_HOST_DEVICE
inline auto target() const -> const candidate_t & {
assert(!is_exhausted());
assert(m_next >= 0);
return m_candidates[static_cast<std::size_t>(m_next)];
}

/// @returns last valid candidate (by position in the cache) - const
DETRAY_HOST_DEVICE
inline auto last() const -> const candidate_t & {
assert(!is_exhausted());
assert(m_next >= 0);
return m_candidates[static_cast<std::size_t>(m_last)];
}

/// Scalar representation of the navigation state,
/// @returns distance to next
DETRAY_HOST_DEVICE
scalar_type operator()() const {
assert(math::isfinite(target().path));
return static_cast<scalar_type>(direction()) * target().path;
}

Expand Down Expand Up @@ -573,6 +576,7 @@ class navigator {
DETRAY_HOST_DEVICE
inline void set_next(dindex pos) {
m_next = pos;
assert(m_next >= 0);
assert(m_next <= m_last + 1);
assert(m_next < static_cast<dist_t>(k_cache_capacity) + 1);
}
Expand All @@ -582,6 +586,8 @@ class navigator {
inline void set_next(candidate_itr_t new_next) {
m_next = static_cast<dist_t>(
detray::ranges::distance(m_candidates.begin(), new_next));
assert(m_next >= 0);
assert(m_next <= m_last + 1);
assert(m_next < static_cast<dist_t>(k_cache_capacity));
}

Expand Down Expand Up @@ -718,6 +724,7 @@ class navigator {

// Do not resurrect a failed/finished navigation state
assert(navigation.status() > navigation::status::e_on_target);
assert(!track.is_invalid());

// Clean up state
navigation.clear();
Expand Down Expand Up @@ -777,6 +784,8 @@ class navigator {
const context_type &ctx = {},
const bool /*is_before_actor*/ = true) const {

assert(!track.is_invalid());

// Candidates are re-evaluated based on the current trust level.
// Should result in 'full trust'
bool is_init = update_kernel(track, navigation, cfg, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct parameter_resetter : actor {
const auto sf = navigation.get_surface();
stepping() = sf.bound_to_free_vector(propagation._context,
stepping.bound_params());
assert(!stepping().is_invalid());

// Reset jacobian transport to identity matrix
stepping.reset_transport_jacobian();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ struct parameter_transporter : actor {

// Set surface link
bound_params.set_surface_link(sf.barcode());

assert(!bound_params.is_invalid());
}

template <typename propagator_state_t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ struct pointwise_material_interactor : actor {
interactor_state.projected_scattering_angle);
}
}

assert(!bound_params.is_invalid());
}

/// @brief Update the q over p of bound track parameter
Expand Down
14 changes: 14 additions & 0 deletions core/include/detray/propagator/base_stepper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class base_stepper {

// An invalid barcode - should not be used
m_bound_params.set_surface_link(geometry::barcode{});

assert(!m_bound_params.is_invalid());
}

/// Sets track parameters from bound track parameter.
Expand All @@ -84,11 +86,23 @@ class base_stepper {
const typename detector_t::geometry_context &ctx)
: m_bound_params(bound_params) {

assert(!m_bound_params.is_invalid());
assert(!m_bound_params.surface_link().is_invalid());

// Departure surface
const auto sf = tracking_surface{det, bound_params.surface_link()};

// Set free track parameters for stepping/navigation
m_track = sf.bound_to_free_vector(ctx, bound_params);

assert(!m_track.is_invalid());

// Bad seed: track direction points back at the IP
// (only test high mom. tracks, as low mom. tracks can be looping)
assert(math::fabs(m_track.qop()) > 0.5f ||
vector::dot(m_track.pos(), m_track.dir()) > 0.f ||
algebra::approx_equal(vector::norm(m_track.pos()),
scalar_type{0.f}, scalar_type{1e-5f}));
}

/// @returns free track parameters - non-const access
Expand Down
6 changes: 6 additions & 0 deletions core/include/detray/propagator/propagator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ struct propagator {
auto &stepping = propagation._stepping;
auto &context = propagation._context;
const auto &track = stepping();
assert(!track.is_invalid());

// Initialize the navigation
m_navigator.init(track, navigation, m_cfg.navigation, context);
propagation._heartbeat = navigation.is_alive();

// Run all registered actors/aborters after init
run_actors(actor_state_refs, propagation);
assert(!track.is_invalid());

// Find next candidate
m_navigator.update(track, navigation, m_cfg.navigation, context);
Expand All @@ -233,6 +235,7 @@ struct propagator {
auto &stepping = propagation._stepping;
auto &context = propagation._context;
const auto &track = stepping();
assert(!track.is_invalid());

// Set access to the volume material for the stepper
auto vol = navigation.get_volume();
Expand All @@ -257,6 +260,7 @@ struct propagator {

// Run all registered actors/aborters after update
run_actors(actor_state_refs, propagation);
assert(!track.is_invalid());

// And check the status
is_init |= m_navigator.update(track, navigation, m_cfg.navigation,
Expand Down Expand Up @@ -366,6 +370,7 @@ struct propagator {
break;
} else {
run_actors(actor_state_refs, propagation);
assert(!track.is_invalid());

// And check the status
is_init |= m_navigator.update(
Expand All @@ -378,6 +383,7 @@ struct propagator {

// Synchronized actor
run_actors(actor_state_refs, propagation);
assert(!track.is_invalid());

// And check the status
is_init |= m_navigator.update(track, navigation,
Expand Down
Loading
Loading