Skip to content

Commit cb20bc7

Browse files
committed
Add some extra assertions
1 parent 62f9895 commit cb20bc7

19 files changed

+147
-39
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

+8
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ class navigator {
521521
DETRAY_HOST_DEVICE
522522
inline void set_next(dindex pos) {
523523
m_next = pos;
524+
assert(m_next >= 0);
524525
assert(m_next <= m_last + 1);
525526
assert(m_next < static_cast<dist_t>(k_cache_capacity) + 1);
526527
}
@@ -530,6 +531,7 @@ class navigator {
530531
inline void set_next(candidate_itr_t new_next) {
531532
m_next = static_cast<dist_t>(
532533
detray::ranges::distance(m_candidates.begin(), new_next));
534+
assert(m_next >= 0);
533535
assert(m_next < static_cast<dist_t>(k_cache_capacity));
534536
}
535537

@@ -616,6 +618,7 @@ class navigator {
616618
const scalar_type mask_tol_scalor,
617619
const scalar_type overstep_tol) const {
618620

621+
assert(!sf_descr.barcode().is_invalid());
619622
const auto sf = geometry::surface{det, sf_descr};
620623

621624
sf.template visit_mask<intersection_initialize<ray_intersector>>(
@@ -650,6 +653,8 @@ class navigator {
650653
const auto &det = navigation.detector();
651654
const auto volume = tracking_volume{det, navigation.volume()};
652655

656+
assert(!track.is_invalid());
657+
653658
// Clean up state
654659
navigation.clear();
655660
navigation.m_heartbeat = true;
@@ -698,6 +703,9 @@ class navigator {
698703
const track_t &track, state &navigation, const navigation::config &cfg,
699704
const context_type &ctx = {},
700705
[[maybe_unused]] const bool is_before_actor = true) const {
706+
707+
assert(!track.is_invalid());
708+
701709
// Candidates are re-evaluated based on the current trust level.
702710
// Should result in 'full trust'
703711
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,

core/include/detray/propagator/rk_stepper.ipp

+23-2
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ DETRAY_HOST_DEVICE inline auto detray::rk_stepper<
415415
bvec2[1u] = bvec2_tmp[1u];
416416
bvec2[2u] = bvec2_tmp[2u];
417417

418+
assert(delta != 0.f);
418419
const vector3_type gradient = (bvec1 - bvec2) * (1.f / (2.f * delta));
419420

420421
getter::element(dBdr, 0u, i) = gradient[0u];
@@ -475,6 +476,7 @@ DETRAY_HOST_DEVICE auto detray::rk_stepper<
475476
return 0.f;
476477
}
477478

479+
assert(qop != 0.f);
478480
const scalar_type q = this->particle_hypothesis().charge();
479481
const scalar_type p = q / qop;
480482
const scalar_type mass = this->particle_hypothesis().mass();
@@ -487,6 +489,7 @@ DETRAY_HOST_DEVICE auto detray::rk_stepper<
487489

488490
// Assert that a momentum is a positive value
489491
assert(p >= 0.f);
492+
assert(q != 0.f);
490493

491494
// d(qop)ds, which is equal to (qop) * E * (-dE/ds) / p^2
492495
// or equal to (qop)^3 * E * (-dE/ds) / q^2
@@ -532,6 +535,8 @@ detray::rk_stepper<magnetic_field_t, algebra_t, constraint_t, policy_t,
532535

533536
// Check Eq 3.12 of
534537
// (https://iopscience.iop.org/article/10.1088/1748-0221/4/04/P04016/meta)
538+
assert(E2 != 0.f);
539+
assert(g != 0.f);
535540
return dqopds * (1.f / qop * (3.f - p2 / E2) + 1.f / g * dgdqop);
536541
}
537542

@@ -545,6 +550,10 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
545550
const detray::stepping::config& cfg, const bool do_reset,
546551
const material<scalar_type>* vol_mat_ptr) const {
547552

553+
// Check navigator and actor results
554+
assert(dist_to_next != 0.f);
555+
assert(!stepping().is_invalid());
556+
548557
// Get stepper and navigator states
549558
auto& magnetic_field = stepping.m_magnetic_field;
550559

@@ -564,6 +573,9 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
564573

565574
// First Runge-Kutta point
566575
const auto bvec = magnetic_field.at(pos[0], pos[1], pos[2]);
576+
assert(math::isfinite(bvec[0]));
577+
assert(math::isfinite(bvec[1]));
578+
assert(math::isfinite(bvec[2]));
567579
sd.b_first[0] = bvec[0];
568580
sd.b_first[1] = bvec[1];
569581
sd.b_first[2] = bvec[2];
@@ -577,6 +589,7 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
577589

578590
/// RKN step trial and error estimation
579591
const auto estimate_error = [&](const scalar_type& h) {
592+
assert(h != 0);
580593
// State the square and half of the step size
581594
const scalar_type h2{h * h};
582595
const scalar_type half_h{h * 0.5f};
@@ -587,6 +600,9 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
587600
const point3_type pos1 =
588601
pos + half_h * sd.t[0u] + h2 * 0.125f * sd.dtds[0u];
589602
const auto bvec1 = magnetic_field.at(pos1[0], pos1[1], pos1[2]);
603+
assert(math::isfinite(bvec1[0]));
604+
assert(math::isfinite(bvec1[1]));
605+
assert(math::isfinite(bvec1[2]));
590606
sd.b_middle[0] = bvec1[0];
591607
sd.b_middle[1] = bvec1[1];
592608
sd.b_middle[2] = bvec1[2];
@@ -609,6 +625,9 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
609625
// Eq (84) of https://doi.org/10.1016/0029-554X(81)90063-X
610626
const point3_type pos2 = pos + h * sd.t[0u] + h2 * 0.5f * sd.dtds[2u];
611627
const auto bvec2 = magnetic_field.at(pos2[0], pos2[1], pos2[2]);
628+
assert(math::isfinite(bvec2[0]));
629+
assert(math::isfinite(bvec2[1]));
630+
assert(math::isfinite(bvec2[2]));
612631
sd.b_last[0] = bvec2[0];
613632
sd.b_last[1] = bvec2[1];
614633
sd.b_last[2] = bvec2[2];
@@ -620,8 +639,7 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
620639

621640
// Compute and check the local integration error estimate
622641
// @Todo
623-
constexpr const scalar_type one_sixth{
624-
static_cast<scalar_type>(1. / 6.)};
642+
constexpr auto one_sixth{static_cast<scalar_type>(1. / 6.)};
625643
const vector3_type err_vec =
626644
one_sixth * h2 *
627645
(sd.dtds[0u] - sd.dtds[1u] - sd.dtds[2u] + sd.dtds[3u]);
@@ -633,6 +651,7 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
633651
/// estimate @param err
634652
const auto step_size_scaling =
635653
[&cfg](const scalar_type& err) -> scalar_type {
654+
assert(err != 0.f);
636655
return static_cast<scalar_type>(
637656
math::min(math::max(math::sqrt(math::sqrt(cfg.rk_error_tol / err)),
638657
static_cast<scalar_type>(0.25)),
@@ -649,6 +668,7 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
649668

650669
error = math::max(estimate_error(stepping.step_size()),
651670
static_cast<scalar_type>(1e-20));
671+
assert(math::isfinite(error));
652672

653673
// Error is small enough
654674
// ---> break and advance track
@@ -683,6 +703,7 @@ DETRAY_HOST_DEVICE inline bool detray::rk_stepper<
683703

684704
// Advance track state
685705
stepping.advance_track(sd, vol_mat_ptr);
706+
assert(!stepping().is_invalid());
686707

687708
// Advance jacobian transport
688709
if (cfg.do_covariance_transport) {

0 commit comments

Comments
 (0)