Skip to content

Commit dc2b539

Browse files
committed
Add some extra assertions
1 parent 0063df2 commit dc2b539

19 files changed

+155
-45
lines changed

core/include/detray/builders/detector_builder.hpp

+1
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

+2-18
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

+22-7
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

+5-2
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

+3
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ class direct_navigator {
288288
DETRAY_HOST_DEVICE inline void init(const track_t &track, state &navigation,
289289
const navigation::config &cfg,
290290
const context_type &ctx) const {
291+
assert(!track.is_invalid());
291292

292293
if (navigation.is_complete()) {
293294
navigation.m_heartbeat = false;
@@ -305,6 +306,8 @@ class direct_navigator {
305306
const context_type &ctx = {},
306307
const bool is_before_actor_run = true) const {
307308

309+
assert(!track.is_invalid());
310+
308311
if (navigation.is_complete()) {
309312
navigation.m_heartbeat = false;
310313
return true;

core/include/detray/navigation/navigator.hpp

+13-3
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

@@ -521,6 +524,7 @@ class navigator {
521524
DETRAY_HOST_DEVICE
522525
inline void set_next(dindex pos) {
523526
m_next = pos;
527+
assert(m_next >= 0);
524528
assert(m_next <= m_last + 1);
525529
assert(m_next < static_cast<dist_t>(k_cache_capacity) + 1);
526530
}
@@ -530,6 +534,8 @@ class navigator {
530534
inline void set_next(candidate_itr_t new_next) {
531535
m_next = static_cast<dist_t>(
532536
detray::ranges::distance(m_candidates.begin(), new_next));
537+
assert(m_next >= 0);
538+
assert(m_next <= m_last + 1);
533539
assert(m_next < static_cast<dist_t>(k_cache_capacity));
534540
}
535541

@@ -650,6 +656,8 @@ class navigator {
650656
const auto &det = navigation.detector();
651657
const auto volume = tracking_volume{det, navigation.volume()};
652658

659+
assert(!track.is_invalid());
660+
653661
// Clean up state
654662
navigation.clear();
655663
navigation.m_heartbeat = true;
@@ -670,9 +678,8 @@ class navigator {
670678
update_navigation_state(navigation, cfg);
671679

672680
// If init was not successful, the propagation setup is broken
673-
if (navigation.trust_level() != navigation::trust_level::e_full) {
674-
navigation.m_heartbeat = false;
675-
}
681+
navigation.m_heartbeat =
682+
(navigation.trust_level() == navigation::trust_level::e_full);
676683

677684
navigation.run_inspector(cfg, track.pos(), track.dir(),
678685
"Init complete: ");
@@ -698,6 +705,9 @@ class navigator {
698705
const track_t &track, state &navigation, const navigation::config &cfg,
699706
const context_type &ctx = {},
700707
[[maybe_unused]] const bool is_before_actor = true) const {
708+
709+
assert(!track.is_invalid());
710+
701711
// Candidates are re-evaluated based on the current trust level.
702712
// Should result in 'full trust'
703713
bool is_init = update_kernel(track, navigation, cfg, ctx);

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

+1
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

+2
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

+2
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/propagator.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,15 @@ struct propagator {
179179
auto &stepping = propagation._stepping;
180180
auto &context = propagation._context;
181181
const auto &track = stepping();
182+
assert(!track.is_invalid());
182183

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

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

190192
// Find next candidate
191193
m_navigator.update(track, navigation, m_cfg.navigation, context);
@@ -210,6 +212,7 @@ struct propagator {
210212
auto &stepping = propagation._stepping;
211213
auto &context = propagation._context;
212214
const auto &track = stepping();
215+
assert(!track.is_invalid());
213216

214217
// Set access to the volume material for the stepper
215218
auto vol = navigation.get_volume();
@@ -234,6 +237,7 @@ struct propagator {
234237

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

238242
// And check the status
239243
is_init |= m_navigator.update(track, navigation, m_cfg.navigation,
@@ -353,6 +357,7 @@ struct propagator {
353357
break;
354358
} else {
355359
run_actors(actor_state_refs, propagation);
360+
assert(!track.is_invalid());
356361

357362
// And check the status
358363
is_init |= m_navigator.update(
@@ -365,6 +370,7 @@ struct propagator {
365370

366371
// Synchronized actor
367372
run_actors(actor_state_refs, propagation);
373+
assert(!track.is_invalid());
368374

369375
// And check the status
370376
is_init |= m_navigator.update(track, navigation,

0 commit comments

Comments
 (0)