Skip to content

Commit 160b083

Browse files
authored
Add some extra assertions (acts-project#971)
Add some assertions for the next candidate index and make sure the track parameters are valid. Also tries to catch potential divisions by zero in the stepper and catch invalid surface/volume descriptors early. This lead to some minor followup corrections in the tests
1 parent 6e249bf commit 160b083

29 files changed

Lines changed: 240 additions & 79 deletions

core/include/detray/builders/detector_builder.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class detector_builder {
6969
/// functionality
7070
template <class builder_t>
7171
DETRAY_HOST auto decorate(dindex volume_idx) -> builder_t* {
72+
assert(has_volume(volume_idx));
7273

7374
m_volumes[volume_idx] =
7475
std::make_unique<builder_t>(std::move(m_volumes[volume_idx]));

core/include/detray/builders/volume_builder.hpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ class volume_builder : public volume_builder_interface<detector_t> {
211211
std::size_t n_portals{0u};
212212
for (auto& sf_desc : m_surfaces) {
213213

214-
const auto sf = geometry::surface{det, sf_desc};
215-
216-
sf.template visit_mask<detail::mask_index_update>(sf_desc);
214+
det._masks.template visit<detail::mask_index_update>(sf_desc.mask(),
215+
sf_desc);
217216
sf_desc.set_volume(m_volume.index());
218217
sf_desc.update_transform(trf_offset);
219218
sf_desc.set_index(sf_offset++);
@@ -297,21 +296,6 @@ struct mask_index_update {
297296
}
298297
};
299298

300-
/// TODO: Remove once the material builder is used everywhere
301-
/// A functor to update the material index in surface objects
302-
struct material_index_update {
303-
304-
template <typename group_t, typename index_t, typename surface_t>
305-
DETRAY_HOST inline void operator()(
306-
[[maybe_unused]] const group_t& group,
307-
[[maybe_unused]] const index_t& /*index*/,
308-
[[maybe_unused]] surface_t& sf) const {
309-
if constexpr (!concepts::grid<typename group_t::value_type>) {
310-
sf.update_material(static_cast<dindex>(group.size()));
311-
}
312-
}
313-
};
314-
315299
} // namespace detail
316300

317301
} // namespace detray

core/include/detray/geometry/surface.hpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class surface {
5353
/// @param desc from that detector.
5454
DETRAY_HOST_DEVICE
5555
constexpr surface(const detector_t &det, const descr_t &desc)
56-
: m_detector{det}, m_desc{desc} {}
56+
: m_detector{det}, m_desc{desc} {
57+
assert(!m_desc.barcode().is_invalid());
58+
assert(m_desc.index() < det.surfaces().size());
59+
assert(m_desc.transform() < det.transform_store().size());
60+
}
5761

5862
/// Constructor from detector @param det and barcode @param bcd in
5963
/// that detector.
@@ -84,20 +88,30 @@ class surface {
8488
/// @returns the surface barcode
8589
DETRAY_HOST_DEVICE
8690
constexpr auto barcode() const -> geometry::barcode {
91+
assert(!m_desc.barcode().is_invalid());
8792
return m_desc.barcode();
8893
}
8994

9095
/// @returns the index of the mother volume
9196
DETRAY_HOST_DEVICE
92-
constexpr auto volume() const -> dindex { return barcode().volume(); }
97+
constexpr auto volume() const -> dindex {
98+
assert(barcode().volume() < m_detector.volumes().size());
99+
return barcode().volume();
100+
}
93101

94102
/// @returns the index of the surface in the detector surface lookup
95103
DETRAY_HOST_DEVICE
96-
constexpr auto index() const -> dindex { return barcode().index(); }
104+
constexpr auto index() const -> dindex {
105+
assert(barcode().index() < m_detector.surfaces().size());
106+
return barcode().index();
107+
}
97108

98109
/// @returns the surface id (sensitive, passive or portal)
99110
DETRAY_HOST_DEVICE
100-
constexpr auto id() const -> surface_id { return barcode().id(); }
111+
constexpr auto id() const -> surface_id {
112+
assert(barcode().id() != surface_id::e_unknown);
113+
return barcode().id();
114+
}
101115

102116
/// @returns the extra bits in the barcode
103117
DETRAY_HOST_DEVICE
@@ -110,7 +124,7 @@ class surface {
110124
/// @returns the surface source link
111125
DETRAY_HOST_DEVICE
112126
constexpr auto source() const {
113-
return m_detector.surface(m_desc.barcode()).source;
127+
return m_detector.surface(barcode()).source;
114128
}
115129

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

@@ -239,8 +254,8 @@ class surface {
239254
/// @tparam Args types of additional arguments to the functor
240255
template <typename functor_t, typename... Args>
241256
DETRAY_HOST_DEVICE constexpr auto visit_mask(Args &&... args) const {
257+
assert(!m_desc.mask().is_invalid());
242258
const auto &masks = m_detector.mask_store();
243-
244259
return masks.template visit<functor_t>(m_desc.mask(),
245260
std::forward<Args>(args)...);
246261
}
@@ -251,8 +266,8 @@ class surface {
251266
/// @tparam Args types of additional arguments to the functor
252267
template <typename functor_t, typename... Args>
253268
DETRAY_HOST_DEVICE constexpr auto visit_material(Args &&... args) const {
269+
assert(has_material());
254270
const auto &materials = m_detector.material_store();
255-
256271
return materials.template visit<functor_t>(m_desc.material(),
257272
std::forward<Args>(args)...);
258273
}

core/include/detray/geometry/tracking_volume.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ class tracking_volume {
5757
/// Constructor from detector @param det and volume descriptor
5858
/// @param vol_idx from that detector.
5959
constexpr tracking_volume(const detector_t &det, const descr_t &desc)
60-
: m_detector{det}, m_desc{desc} {}
60+
: m_detector{det}, m_desc{desc} {
61+
assert(m_desc.index() < det.volumes().size());
62+
assert(m_desc.id() != volume_id::e_unknown);
63+
}
6164

6265
/// Constructor from detector @param det and volume index @param vol_idx in
6366
/// that detector.
@@ -168,8 +171,8 @@ class tracking_volume {
168171
/// @tparam Args types of additional arguments to the functor
169172
template <typename functor_t, typename... Args>
170173
DETRAY_HOST_DEVICE constexpr auto visit_material(Args &&... args) const {
174+
assert(has_material());
171175
const auto &materials = m_detector.material_store();
172-
173176
return materials.template visit<functor_t>(m_desc.material(),
174177
std::forward<Args>(args)...);
175178
}

core/include/detray/navigation/direct_navigator.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class direct_navigator {
306306
const context_type &ctx) const {
307307
// Do not resurrect a failed/finished navigation state
308308
assert(navigation.status() > navigation::status::e_on_target);
309+
assert(!track.is_invalid());
309310

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

327+
assert(!track.is_invalid());
328+
326329
if (navigation.is_complete()) {
327330
navigation.m_heartbeat = false;
328331
return true;

core/include/detray/navigation/navigator.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,23 @@ class navigator {
223223
DETRAY_HOST_DEVICE
224224
inline auto target() const -> const candidate_t & {
225225
assert(!is_exhausted());
226+
assert(m_next >= 0);
226227
return m_candidates[static_cast<std::size_t>(m_next)];
227228
}
228229

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

236238
/// Scalar representation of the navigation state,
237239
/// @returns distance to next
238240
DETRAY_HOST_DEVICE
239241
scalar_type operator()() const {
242+
assert(math::isfinite(target().path));
240243
return static_cast<scalar_type>(direction()) * target().path;
241244
}
242245

@@ -573,6 +576,7 @@ class navigator {
573576
DETRAY_HOST_DEVICE
574577
inline void set_next(dindex pos) {
575578
m_next = pos;
579+
assert(m_next >= 0);
576580
assert(m_next <= m_last + 1);
577581
assert(m_next < static_cast<dist_t>(k_cache_capacity) + 1);
578582
}
@@ -582,6 +586,8 @@ class navigator {
582586
inline void set_next(candidate_itr_t new_next) {
583587
m_next = static_cast<dist_t>(
584588
detray::ranges::distance(m_candidates.begin(), new_next));
589+
assert(m_next >= 0);
590+
assert(m_next <= m_last + 1);
585591
assert(m_next < static_cast<dist_t>(k_cache_capacity));
586592
}
587593

@@ -718,6 +724,7 @@ class navigator {
718724

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

722729
// Clean up state
723730
navigation.clear();
@@ -777,6 +784,8 @@ class navigator {
777784
const context_type &ctx = {},
778785
const bool /*is_before_actor*/ = true) const {
779786

787+
assert(!track.is_invalid());
788+
780789
// Candidates are re-evaluated based on the current trust level.
781790
// Should result in 'full trust'
782791
bool is_init = update_kernel(track, navigation, cfg, ctx);

core/include/detray/propagator/actors/parameter_resetter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct parameter_resetter : actor {
3535
const auto sf = navigation.get_surface();
3636
stepping() = sf.bound_to_free_vector(propagation._context,
3737
stepping.bound_params());
38+
assert(!stepping().is_invalid());
3839

3940
// Reset jacobian transport to identity matrix
4041
stepping.reset_transport_jacobian();

core/include/detray/propagator/actors/parameter_transporter.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ struct parameter_transporter : actor {
111111

112112
// Set surface link
113113
bound_params.set_surface_link(sf.barcode());
114+
115+
assert(!bound_params.is_invalid());
114116
}
115117

116118
template <typename propagator_state_t>

core/include/detray/propagator/actors/pointwise_material_interactor.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ struct pointwise_material_interactor : actor {
185185
interactor_state.projected_scattering_angle);
186186
}
187187
}
188+
189+
assert(!bound_params.is_invalid());
188190
}
189191

190192
/// @brief Update the q over p of bound track parameter

core/include/detray/propagator/base_stepper.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class base_stepper {
7474

7575
// An invalid barcode - should not be used
7676
m_bound_params.set_surface_link(geometry::barcode{});
77+
78+
assert(!m_bound_params.is_invalid());
7779
}
7880

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

89+
assert(!m_bound_params.is_invalid());
90+
assert(!m_bound_params.surface_link().is_invalid());
91+
8792
// Departure surface
8893
const auto sf = tracking_surface{det, bound_params.surface_link()};
8994

9095
// Set free track parameters for stepping/navigation
9196
m_track = sf.bound_to_free_vector(ctx, bound_params);
97+
98+
assert(!m_track.is_invalid());
99+
100+
// Bad seed: track direction points back at the IP
101+
// (only test high mom. tracks, as low mom. tracks can be looping)
102+
assert(math::fabs(m_track.qop()) > 0.5f ||
103+
vector::dot(m_track.pos(), m_track.dir()) > 0.f ||
104+
algebra::approx_equal(vector::norm(m_track.pos()),
105+
scalar_type{0.f}, scalar_type{1e-5f}));
92106
}
93107

94108
/// @returns free track parameters - non-const access

0 commit comments

Comments
 (0)