Skip to content

Commit 60bd754

Browse files
committed
Adds a volume builder for homogeneous volume material and a corresponding unittest
1 parent b243746 commit 60bd754

21 files changed

+265
-83
lines changed

core/include/detray/builders/detector_builder.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,22 @@ class detector_builder {
6363
/// Decorate a volume builder at position @param volume_idx with more
6464
/// functionality
6565
template <class builder_t>
66-
DETRAY_HOST auto decorate(dindex volume_idx)
67-
-> volume_builder_interface<detector_type>* {
66+
DETRAY_HOST auto decorate(dindex volume_idx) -> builder_t* {
6867

6968
m_volumes[volume_idx] =
7069
std::make_unique<builder_t>(std::move(m_volumes[volume_idx]));
7170

72-
return m_volumes[volume_idx].get();
71+
// Always works, we set it as this type in the line above
72+
return dynamic_cast<builder_t*>(m_volumes[volume_idx].get());
73+
}
74+
75+
/// Decorate a volume builder @param v_builder with more functionality
76+
template <class builder_t>
77+
DETRAY_HOST auto decorate(
78+
const volume_builder_interface<detector_type>* v_builder)
79+
-> builder_t* {
80+
81+
return decorate<builder_t>(v_builder->vol_index());
7382
}
7483

7584
/// Access a particular volume builder by volume index @param volume_idx
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/** Detray library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2024 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Project include(s).
11+
#include "detray/builders/volume_builder.hpp"
12+
#include "detray/builders/volume_builder_interface.hpp"
13+
#include "detray/materials/material.hpp"
14+
#include "detray/materials/predefined_materials.hpp"
15+
16+
// System include(s)
17+
#include <iostream>
18+
#include <map>
19+
#include <memory>
20+
21+
namespace detray {
22+
23+
/// @brief Build a volume with homogeneous volume material.
24+
///
25+
/// Decorator class to a volume builder that adds the material data to the
26+
/// volume while building the volume.
27+
template <typename detector_t>
28+
class homogeneous_volume_material_builder final
29+
: public volume_decorator<detector_t> {
30+
31+
public:
32+
using scalar_type = typename detector_t::scalar_type;
33+
34+
/// @param vol_builder volume builder that should be decorated with volume
35+
/// material
36+
DETRAY_HOST
37+
homogeneous_volume_material_builder(
38+
std::unique_ptr<volume_builder_interface<detector_t>> vol_builder)
39+
: volume_decorator<detector_t>(std::move(vol_builder)) {}
40+
41+
/// Add all necessary compontents for the volume material
42+
///
43+
/// @param mat the material parameters
44+
DETRAY_HOST
45+
void set_material(material<scalar_type> mat) { m_volume_material = mat; }
46+
47+
/// Add the volume and the material to the detector @param det
48+
DETRAY_HOST
49+
auto build(detector_t &det, typename detector_t::geometry_context ctx = {})
50+
-> typename detector_t::volume_type * override {
51+
52+
// Call the underlying volume builder(s)
53+
typename detector_t::volume_type *vol =
54+
volume_decorator<detector_t>::build(det, ctx);
55+
56+
// Nothing left to do
57+
if (m_volume_material == detray::vacuum<scalar_type>{}) {
58+
std::cout << "WARNING: Volume " << this->vol_index()
59+
<< " has vacuum material";
60+
61+
return vol;
62+
}
63+
64+
constexpr auto material_id{detector_t::materials::id::e_raw_material};
65+
66+
// Update the volume material link
67+
dindex coll_size{det.material_store().template size<material_id>()};
68+
vol->set_material(material_id, coll_size);
69+
70+
// Append the material
71+
det.material_store().template push_back<material_id>(m_volume_material);
72+
73+
// Give the volume to the next decorator
74+
return vol;
75+
}
76+
77+
protected:
78+
// Material for this volume
79+
material<scalar_type> m_volume_material{};
80+
};
81+
82+
} // namespace detray

core/include/detray/builders/volume_builder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class volume_builder : public volume_builder_interface<detector_t> {
6262

6363
/// @returns the volume index in the detector volume container
6464
DETRAY_HOST
65-
auto vol_index() -> dindex override { return m_volume.index(); }
65+
auto vol_index() const -> dindex override { return m_volume.index(); }
6666

6767
/// Toggles whether sensitive surfaces are added to the brute force method
6868
DETRAY_HOST

core/include/detray/builders/volume_builder_interface.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class volume_builder_interface {
3838
/// @returns the global index for the volume
3939
/// @note the correct index is only available after calling @c init_vol
4040
DETRAY_HOST
41-
virtual auto vol_index() -> dindex = 0;
41+
virtual auto vol_index() const -> dindex = 0;
4242

4343
/// Toggles whether sensitive surfaces are added to the brute force method
4444
DETRAY_HOST
@@ -125,7 +125,7 @@ class volume_decorator : public volume_builder_interface<detector_t> {
125125
}
126126

127127
DETRAY_HOST
128-
auto vol_index() -> dindex override { return m_builder->vol_index(); }
128+
auto vol_index() const -> dindex override { return m_builder->vol_index(); }
129129

130130
DETRAY_HOST
131131
void has_accel(bool toggle) override { m_builder->has_accel(toggle); };

core/include/detray/materials/material.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ struct material {
7979
m_z == rhs.Z());
8080
}
8181

82+
/// Equality operator
83+
///
84+
/// @param rhs is the right hand side to be compared to
85+
DETRAY_HOST_DEVICE
86+
constexpr bool operator!=(const material<scalar_t> &rhs) const {
87+
return !(*this == rhs);
88+
}
89+
8290
/// @returns the radition length. Infinity in case of vacuum.
8391
DETRAY_HOST_DEVICE
8492
constexpr scalar_type X0() const { return m_x0; }
@@ -143,10 +151,10 @@ struct material {
143151
DETRAY_HOST
144152
std::string to_string() const {
145153
std::stringstream strm;
146-
/*if (0.f < m_ar) {
154+
if (m_ar <= 0.f) {
147155
strm << "vacuum";
148156
return strm.str();
149-
}*/
157+
}
150158
strm << "material: ";
151159
strm << " X0: " << m_x0;
152160
strm << " | L0: " << m_l0;

core/include/detray/propagator/rk_stepper.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ class rk_stepper final
9393
/// Material that track is passing through. Usually a volume material
9494
const detray::material<scalar_type>* _mat{nullptr};
9595

96+
/// Access the current volume material
97+
DETRAY_HOST_DEVICE
98+
const auto& volume_material() const {
99+
assert(_mat != nullptr);
100+
return *_mat;
101+
}
102+
96103
/// Update the track state by Runge-Kutta-Nystrom integration.
97104
DETRAY_HOST_DEVICE
98105
inline void advance_track();

core/include/detray/propagator/rk_stepper.ipp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,13 @@ detray::rk_stepper<magnetic_field_t, transform3_t, constraint_t, policy_t,
554554
inspector_t, array_t>::state::dqopds(const scalar_type qop)
555555
const -> typename transform3_t::scalar_type {
556556

557-
const auto& mat = *(this->_mat);
558-
559557
// d(qop)ds is zero for empty space
560558
if (this->_mat == nullptr) {
561559
return 0.f;
562560
}
563561

562+
const auto& mat = this->volume_material();
563+
564564
const auto pdg = this->_pdg;
565565
const scalar_type q = this->_track.charge();
566566
const scalar_type p = q / qop;
@@ -594,6 +594,7 @@ DETRAY_HOST_DEVICE auto detray::rk_stepper<
594594
return 0.f;
595595
}
596596

597+
const auto& mat = this->volume_material();
597598
auto& track = this->_track;
598599
const scalar_t q = track.charge();
599600
const scalar_t p = q / qop;
@@ -608,13 +609,12 @@ DETRAY_HOST_DEVICE auto detray::rk_stepper<
608609
// g = dE/ds = -1 * (-dE/ds) = -1 * stopping power
609610
const detail::relativistic_quantities<scalar_t> rq(mass, qop, q);
610611
// We assume that stopping power ~ mean ionization eloss per pathlength
611-
const scalar_type bethe =
612-
I.compute_bethe_bloch(*(this->_mat), this->_pdg, rq);
612+
const scalar_type bethe = I.compute_bethe_bloch(mat, this->_pdg, rq);
613613
const scalar_type g = -1.f * bethe;
614614

615615
// dg/d(qop) = -1 * derivation of stopping power
616616
const scalar_t dgdqop = -1.f * interaction<scalar_t>().derive_bethe_bloch(
617-
*(this->_mat), this->_pdg, rq, bethe);
617+
mat, this->_pdg, rq, bethe);
618618

619619
// d(qop)/ds = - qop^3 * E * g / q^2
620620
const scalar_t dqopds = this->dqopds(qop);

tests/integration_tests/cpu/detectors/telescope_detector_navigation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// Project include(s)
99
#include "detray/definitions/units.hpp"
10-
#include "detray/detectors/create_telescope_detector.hpp"
10+
#include "detray/detectors/build_telescope_detector.hpp"
1111
#include "detray/test/detail/register_checks.hpp"
1212
#include "detray/test/detector_consistency.hpp"
1313
#include "detray/test/detector_helix_scan.hpp"
@@ -41,7 +41,7 @@ int main(int argc, char **argv) {
4141
vecmem::host_memory_resource host_mr;
4242

4343
const auto [tel_det, tel_names] =
44-
create_telescope_detector(host_mr, tel_cfg);
44+
build_telescope_detector(host_mr, tel_cfg);
4545

4646
// General data consistency of the detector
4747
consistency_check<tel_detector_t>::config cfg_cons{};

tests/integration_tests/cpu/material/material_interaction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "detray/definitions/pdg_particle.hpp"
1010
#include "detray/definitions/units.hpp"
1111
#include "detray/detectors/bfield.hpp"
12-
#include "detray/detectors/create_telescope_detector.hpp"
12+
#include "detray/detectors/build_telescope_detector.hpp"
1313
#include "detray/geometry/mask.hpp"
1414
#include "detray/geometry/shapes/unbounded.hpp"
1515
#include "detray/materials/interaction.hpp"
@@ -61,7 +61,7 @@ GTEST_TEST(detray_material, telescope_geometry_energy_loss) {
6161
.module_material(mat)
6262
.mat_thickness(thickness);
6363

64-
const auto [det, names] = create_telescope_detector(host_mr, tel_cfg);
64+
const auto [det, names] = build_telescope_detector(host_mr, tel_cfg);
6565

6666
using navigator_t = navigator<decltype(det)>;
6767
using stepper_t = line_stepper<transform3>;
@@ -240,7 +240,7 @@ GTEST_TEST(detray_material, telescope_geometry_scattering_angle) {
240240
.module_material(mat)
241241
.mat_thickness(thickness);
242242

243-
const auto [det, names] = create_telescope_detector(host_mr, tel_cfg);
243+
const auto [det, names] = build_telescope_detector(host_mr, tel_cfg);
244244

245245
using navigator_t = navigator<decltype(det)>;
246246
using stepper_t = line_stepper<transform3>;
@@ -379,7 +379,7 @@ GTEST_TEST(detray_material, telescope_geometry_volume_material) {
379379

380380
for (const auto& mat : vol_mats) {
381381
tel_cfg.volume_material(mat);
382-
const auto [det, names] = create_telescope_detector(host_mr, tel_cfg);
382+
const auto [det, names] = build_telescope_detector(host_mr, tel_cfg);
383383

384384
using navigator_t = navigator<decltype(det)>;
385385
using propagator_t = propagator<stepper_t, navigator_t, actor_chain_t>;

tests/integration_tests/cpu/propagator/guided_navigator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "detray/definitions/units.hpp"
99
#include "detray/detectors/bfield.hpp"
10-
#include "detray/detectors/create_telescope_detector.hpp"
10+
#include "detray/detectors/build_telescope_detector.hpp"
1111
#include "detray/geometry/mask.hpp"
1212
#include "detray/geometry/shapes/unbounded.hpp"
1313
#include "detray/navigation/navigator.hpp"
@@ -46,7 +46,7 @@ GTEST_TEST(detray_navigation, guided_navigator) {
4646
tel_cfg.positions(positions).envelope(0.2f * unit<scalar>::mm);
4747

4848
const auto [telescope_det, names] =
49-
create_telescope_detector(host_mr, tel_cfg);
49+
build_telescope_detector(host_mr, tel_cfg);
5050

5151
// Inspectors are optional, of course
5252
using detector_t = decltype(telescope_det);

tests/integration_tests/cpu/propagator/jacobian_validation.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Project include(s)
99
#include "detray/definitions/units.hpp"
1010
#include "detray/detectors/bfield.hpp"
11-
#include "detray/detectors/create_telescope_detector.hpp"
11+
#include "detray/detectors/build_telescope_detector.hpp"
1212
#include "detray/navigation/intersection/helix_intersector.hpp"
1313
#include "detray/navigation/navigator.hpp"
1414
#include "detray/propagator/actors/parameter_resetter.hpp"
@@ -1657,8 +1657,9 @@ int main(int argc, char** argv) {
16571657
}
16581658
auto gammaF = rand_gamma(mt1);
16591659

1660+
// Without volume material
16601661
auto [rect_det, rect_names] =
1661-
create_telescope_detector(host_mr, rectangle_cfg);
1662+
build_telescope_detector(host_mr, rectangle_cfg);
16621663
const auto [euler_rect_initial, shift_rect_initial] =
16631664
tilt_surface<decltype(rect_det),
16641665
decltype(rect_det)::masks::id::e_rectangle2>(
@@ -1672,7 +1673,7 @@ int main(int argc, char** argv) {
16721673
// With volume material
16731674
rectangle_cfg.volume_material(volume_mat);
16741675
auto [rect_det_w_mat, rect_names2] =
1675-
create_telescope_detector(host_mr, rectangle_cfg);
1676+
build_telescope_detector(host_mr, rectangle_cfg);
16761677
[[maybe_unused]] const auto [euler_rect_initial2, shift_rect_initial2] =
16771678
tilt_surface<decltype(rect_det_w_mat),
16781679
decltype(rect_det_w_mat)::masks::id::e_rectangle2>(
@@ -1693,7 +1694,7 @@ int main(int argc, char** argv) {
16931694

16941695
// Without volume material
16951696
auto [wire_det, wire_names] =
1696-
create_telescope_detector(host_mr, wire_cfg);
1697+
build_telescope_detector(host_mr, wire_cfg);
16971698
const auto [euler_wire_initial, shift_wire_initial] =
16981699
tilt_surface<decltype(wire_det),
16991700
decltype(wire_det)::masks::id::e_drift_cell>(
@@ -1707,7 +1708,7 @@ int main(int argc, char** argv) {
17071708
// With volume material
17081709
wire_cfg.volume_material(volume_mat);
17091710
auto [wire_det_w_mat, wire_names2] =
1710-
create_telescope_detector(host_mr, wire_cfg);
1711+
build_telescope_detector(host_mr, wire_cfg);
17111712
[[maybe_unused]] const auto [euler_wire_initial2, shift_wire_initial2] =
17121713
tilt_surface<decltype(wire_det_w_mat),
17131714
decltype(wire_det_w_mat)::masks::id::e_drift_cell>(

tests/integration_tests/io/io_json_detector_roundtrip.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// Project include(s)
99
#include "detray/definitions/detail/algebra.hpp"
10-
#include "detray/detectors/create_telescope_detector.hpp"
10+
#include "detray/detectors/build_telescope_detector.hpp"
1111
#include "detray/detectors/create_toy_geometry.hpp"
1212
#include "detray/detectors/create_wire_chamber.hpp"
1313
#include "detray/io/common/geometry_reader.hpp"
@@ -151,7 +151,7 @@ GTEST_TEST(io, json_telescope_detector_reader) {
151151

152152
// Telescope detector
153153
vecmem::host_memory_resource host_mr;
154-
auto [tel_det, tel_names] = create_telescope_detector(host_mr, tel_cfg);
154+
auto [tel_det, tel_names] = build_telescope_detector(host_mr, tel_cfg);
155155

156156
std::map<std::string, std::string> file_names;
157157
file_names["geometry"] = "telescope_detector_geometry.json";

tests/tools/src/generate_telescope_detector.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// Project include(s)
99
#include "detray/definitions/units.hpp"
10-
#include "detray/detectors/create_telescope_detector.hpp"
10+
#include "detray/detectors/build_telescope_detector.hpp"
1111
#include "detray/geometry/mask.hpp"
1212
#include "detray/geometry/shapes.hpp"
1313
#include "detray/io/frontend/detector_writer.hpp"
@@ -53,7 +53,7 @@ void write_telecope(const po::variables_map &vm,
5353

5454
// Build the detector
5555
vecmem::host_memory_resource host_mr;
56-
auto [tel_det, tel_names] = create_telescope_detector(host_mr, tel_cfg);
56+
auto [tel_det, tel_names] = build_telescope_detector(host_mr, tel_cfg);
5757

5858
// Write to file
5959
detray::io::write_detector(tel_det, tel_names, writer_cfg);

tests/unit_tests/cpu/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ macro( detray_add_cpu_test algebra )
3131
"coordinates/polar2.cpp"
3232
"builders/detector_builder.cpp"
3333
"builders/grid_builder.cpp"
34+
"builders/homogeneous_volume_material_builder.cpp"
3435
"builders/homogeneous_material_builder.cpp"
3536
"builders/material_map_builder.cpp"
3637
"builders/volume_builder.cpp"

tests/unit_tests/cpu/builders/homogeneous_material_builder.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "detray/builders/homogeneous_material_builder.hpp"
1010

1111
#include "detray/builders/cuboid_portal_generator.hpp"
12-
#include "detray/builders/detector_builder.hpp"
1312
#include "detray/builders/homogeneous_material_factory.hpp"
1413
#include "detray/builders/surface_factory.hpp"
1514
#include "detray/builders/volume_builder.hpp"

0 commit comments

Comments
 (0)