Skip to content

Commit

Permalink
Adds a volume builder for homogeneous volume material and a correspon…
Browse files Browse the repository at this point in the history
…ding unittest
  • Loading branch information
niermann999 committed Mar 19, 2024
1 parent b243746 commit 60bd754
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 83 deletions.
15 changes: 12 additions & 3 deletions core/include/detray/builders/detector_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ class detector_builder {
/// Decorate a volume builder at position @param volume_idx with more
/// functionality
template <class builder_t>
DETRAY_HOST auto decorate(dindex volume_idx)
-> volume_builder_interface<detector_type>* {
DETRAY_HOST auto decorate(dindex volume_idx) -> builder_t* {

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

return m_volumes[volume_idx].get();
// Always works, we set it as this type in the line above
return dynamic_cast<builder_t*>(m_volumes[volume_idx].get());
}

/// Decorate a volume builder @param v_builder with more functionality
template <class builder_t>
DETRAY_HOST auto decorate(
const volume_builder_interface<detector_type>* v_builder)
-> builder_t* {

return decorate<builder_t>(v_builder->vol_index());
}

/// Access a particular volume builder by volume index @param volume_idx
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/** Detray library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Project include(s).
#include "detray/builders/volume_builder.hpp"
#include "detray/builders/volume_builder_interface.hpp"
#include "detray/materials/material.hpp"
#include "detray/materials/predefined_materials.hpp"

// System include(s)
#include <iostream>
#include <map>
#include <memory>

namespace detray {

/// @brief Build a volume with homogeneous volume material.
///
/// Decorator class to a volume builder that adds the material data to the
/// volume while building the volume.
template <typename detector_t>
class homogeneous_volume_material_builder final
: public volume_decorator<detector_t> {

public:
using scalar_type = typename detector_t::scalar_type;

/// @param vol_builder volume builder that should be decorated with volume
/// material
DETRAY_HOST
homogeneous_volume_material_builder(
std::unique_ptr<volume_builder_interface<detector_t>> vol_builder)
: volume_decorator<detector_t>(std::move(vol_builder)) {}

/// Add all necessary compontents for the volume material
///
/// @param mat the material parameters
DETRAY_HOST
void set_material(material<scalar_type> mat) { m_volume_material = mat; }

/// Add the volume and the material to the detector @param det
DETRAY_HOST
auto build(detector_t &det, typename detector_t::geometry_context ctx = {})
-> typename detector_t::volume_type * override {

// Call the underlying volume builder(s)
typename detector_t::volume_type *vol =
volume_decorator<detector_t>::build(det, ctx);

// Nothing left to do
if (m_volume_material == detray::vacuum<scalar_type>{}) {
std::cout << "WARNING: Volume " << this->vol_index()
<< " has vacuum material";

return vol;
}

constexpr auto material_id{detector_t::materials::id::e_raw_material};

// Update the volume material link
dindex coll_size{det.material_store().template size<material_id>()};
vol->set_material(material_id, coll_size);

// Append the material
det.material_store().template push_back<material_id>(m_volume_material);

// Give the volume to the next decorator
return vol;
}

protected:
// Material for this volume
material<scalar_type> m_volume_material{};
};

} // namespace detray
2 changes: 1 addition & 1 deletion core/include/detray/builders/volume_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class volume_builder : public volume_builder_interface<detector_t> {

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

/// Toggles whether sensitive surfaces are added to the brute force method
DETRAY_HOST
Expand Down
4 changes: 2 additions & 2 deletions core/include/detray/builders/volume_builder_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class volume_builder_interface {
/// @returns the global index for the volume
/// @note the correct index is only available after calling @c init_vol
DETRAY_HOST
virtual auto vol_index() -> dindex = 0;
virtual auto vol_index() const -> dindex = 0;

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

DETRAY_HOST
auto vol_index() -> dindex override { return m_builder->vol_index(); }
auto vol_index() const -> dindex override { return m_builder->vol_index(); }

DETRAY_HOST
void has_accel(bool toggle) override { m_builder->has_accel(toggle); };
Expand Down
12 changes: 10 additions & 2 deletions core/include/detray/materials/material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ struct material {
m_z == rhs.Z());
}

/// Equality operator
///
/// @param rhs is the right hand side to be compared to
DETRAY_HOST_DEVICE
constexpr bool operator!=(const material<scalar_t> &rhs) const {
return !(*this == rhs);
}

/// @returns the radition length. Infinity in case of vacuum.
DETRAY_HOST_DEVICE
constexpr scalar_type X0() const { return m_x0; }
Expand Down Expand Up @@ -143,10 +151,10 @@ struct material {
DETRAY_HOST
std::string to_string() const {
std::stringstream strm;
/*if (0.f < m_ar) {
if (m_ar <= 0.f) {
strm << "vacuum";
return strm.str();
}*/
}
strm << "material: ";
strm << " X0: " << m_x0;
strm << " | L0: " << m_l0;
Expand Down
7 changes: 7 additions & 0 deletions core/include/detray/propagator/rk_stepper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class rk_stepper final
/// Material that track is passing through. Usually a volume material
const detray::material<scalar_type>* _mat{nullptr};

/// Access the current volume material
DETRAY_HOST_DEVICE
const auto& volume_material() const {
assert(_mat != nullptr);
return *_mat;
}

/// Update the track state by Runge-Kutta-Nystrom integration.
DETRAY_HOST_DEVICE
inline void advance_track();
Expand Down
10 changes: 5 additions & 5 deletions core/include/detray/propagator/rk_stepper.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,13 @@ detray::rk_stepper<magnetic_field_t, transform3_t, constraint_t, policy_t,
inspector_t, array_t>::state::dqopds(const scalar_type qop)
const -> typename transform3_t::scalar_type {

const auto& mat = *(this->_mat);

// d(qop)ds is zero for empty space
if (this->_mat == nullptr) {
return 0.f;
}

const auto& mat = this->volume_material();

const auto pdg = this->_pdg;
const scalar_type q = this->_track.charge();
const scalar_type p = q / qop;
Expand Down Expand Up @@ -594,6 +594,7 @@ DETRAY_HOST_DEVICE auto detray::rk_stepper<
return 0.f;
}

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

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

// d(qop)/ds = - qop^3 * E * g / q^2
const scalar_t dqopds = this->dqopds(qop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Project include(s)
#include "detray/definitions/units.hpp"
#include "detray/detectors/create_telescope_detector.hpp"
#include "detray/detectors/build_telescope_detector.hpp"
#include "detray/test/detail/register_checks.hpp"
#include "detray/test/detector_consistency.hpp"
#include "detray/test/detector_helix_scan.hpp"
Expand Down Expand Up @@ -41,7 +41,7 @@ int main(int argc, char **argv) {
vecmem::host_memory_resource host_mr;

const auto [tel_det, tel_names] =
create_telescope_detector(host_mr, tel_cfg);
build_telescope_detector(host_mr, tel_cfg);

// General data consistency of the detector
consistency_check<tel_detector_t>::config cfg_cons{};
Expand Down
8 changes: 4 additions & 4 deletions tests/integration_tests/cpu/material/material_interaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "detray/definitions/pdg_particle.hpp"
#include "detray/definitions/units.hpp"
#include "detray/detectors/bfield.hpp"
#include "detray/detectors/create_telescope_detector.hpp"
#include "detray/detectors/build_telescope_detector.hpp"
#include "detray/geometry/mask.hpp"
#include "detray/geometry/shapes/unbounded.hpp"
#include "detray/materials/interaction.hpp"
Expand Down Expand Up @@ -61,7 +61,7 @@ GTEST_TEST(detray_material, telescope_geometry_energy_loss) {
.module_material(mat)
.mat_thickness(thickness);

const auto [det, names] = create_telescope_detector(host_mr, tel_cfg);
const auto [det, names] = build_telescope_detector(host_mr, tel_cfg);

using navigator_t = navigator<decltype(det)>;
using stepper_t = line_stepper<transform3>;
Expand Down Expand Up @@ -240,7 +240,7 @@ GTEST_TEST(detray_material, telescope_geometry_scattering_angle) {
.module_material(mat)
.mat_thickness(thickness);

const auto [det, names] = create_telescope_detector(host_mr, tel_cfg);
const auto [det, names] = build_telescope_detector(host_mr, tel_cfg);

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

for (const auto& mat : vol_mats) {
tel_cfg.volume_material(mat);
const auto [det, names] = create_telescope_detector(host_mr, tel_cfg);
const auto [det, names] = build_telescope_detector(host_mr, tel_cfg);

using navigator_t = navigator<decltype(det)>;
using propagator_t = propagator<stepper_t, navigator_t, actor_chain_t>;
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/cpu/propagator/guided_navigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

const auto [telescope_det, names] =
create_telescope_detector(host_mr, tel_cfg);
build_telescope_detector(host_mr, tel_cfg);

// Inspectors are optional, of course
using detector_t = decltype(telescope_det);
Expand Down
11 changes: 6 additions & 5 deletions tests/integration_tests/cpu/propagator/jacobian_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Project include(s)
#include "detray/definitions/units.hpp"
#include "detray/detectors/bfield.hpp"
#include "detray/detectors/create_telescope_detector.hpp"
#include "detray/detectors/build_telescope_detector.hpp"
#include "detray/navigation/intersection/helix_intersector.hpp"
#include "detray/navigation/navigator.hpp"
#include "detray/propagator/actors/parameter_resetter.hpp"
Expand Down Expand Up @@ -1657,8 +1657,9 @@ int main(int argc, char** argv) {
}
auto gammaF = rand_gamma(mt1);

// Without volume material
auto [rect_det, rect_names] =
create_telescope_detector(host_mr, rectangle_cfg);
build_telescope_detector(host_mr, rectangle_cfg);
const auto [euler_rect_initial, shift_rect_initial] =
tilt_surface<decltype(rect_det),
decltype(rect_det)::masks::id::e_rectangle2>(
Expand All @@ -1672,7 +1673,7 @@ int main(int argc, char** argv) {
// With volume material
rectangle_cfg.volume_material(volume_mat);
auto [rect_det_w_mat, rect_names2] =
create_telescope_detector(host_mr, rectangle_cfg);
build_telescope_detector(host_mr, rectangle_cfg);
[[maybe_unused]] const auto [euler_rect_initial2, shift_rect_initial2] =
tilt_surface<decltype(rect_det_w_mat),
decltype(rect_det_w_mat)::masks::id::e_rectangle2>(
Expand All @@ -1693,7 +1694,7 @@ int main(int argc, char** argv) {

// Without volume material
auto [wire_det, wire_names] =
create_telescope_detector(host_mr, wire_cfg);
build_telescope_detector(host_mr, wire_cfg);
const auto [euler_wire_initial, shift_wire_initial] =
tilt_surface<decltype(wire_det),
decltype(wire_det)::masks::id::e_drift_cell>(
Expand All @@ -1707,7 +1708,7 @@ int main(int argc, char** argv) {
// With volume material
wire_cfg.volume_material(volume_mat);
auto [wire_det_w_mat, wire_names2] =
create_telescope_detector(host_mr, wire_cfg);
build_telescope_detector(host_mr, wire_cfg);
[[maybe_unused]] const auto [euler_wire_initial2, shift_wire_initial2] =
tilt_surface<decltype(wire_det_w_mat),
decltype(wire_det_w_mat)::masks::id::e_drift_cell>(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/io/io_json_detector_roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Project include(s)
#include "detray/definitions/detail/algebra.hpp"
#include "detray/detectors/create_telescope_detector.hpp"
#include "detray/detectors/build_telescope_detector.hpp"
#include "detray/detectors/create_toy_geometry.hpp"
#include "detray/detectors/create_wire_chamber.hpp"
#include "detray/io/common/geometry_reader.hpp"
Expand Down Expand Up @@ -151,7 +151,7 @@ GTEST_TEST(io, json_telescope_detector_reader) {

// Telescope detector
vecmem::host_memory_resource host_mr;
auto [tel_det, tel_names] = create_telescope_detector(host_mr, tel_cfg);
auto [tel_det, tel_names] = build_telescope_detector(host_mr, tel_cfg);

std::map<std::string, std::string> file_names;
file_names["geometry"] = "telescope_detector_geometry.json";
Expand Down
4 changes: 2 additions & 2 deletions tests/tools/src/generate_telescope_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Project include(s)
#include "detray/definitions/units.hpp"
#include "detray/detectors/create_telescope_detector.hpp"
#include "detray/detectors/build_telescope_detector.hpp"
#include "detray/geometry/mask.hpp"
#include "detray/geometry/shapes.hpp"
#include "detray/io/frontend/detector_writer.hpp"
Expand Down Expand Up @@ -53,7 +53,7 @@ void write_telecope(const po::variables_map &vm,

// Build the detector
vecmem::host_memory_resource host_mr;
auto [tel_det, tel_names] = create_telescope_detector(host_mr, tel_cfg);
auto [tel_det, tel_names] = build_telescope_detector(host_mr, tel_cfg);

// Write to file
detray::io::write_detector(tel_det, tel_names, writer_cfg);
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ macro( detray_add_cpu_test algebra )
"coordinates/polar2.cpp"
"builders/detector_builder.cpp"
"builders/grid_builder.cpp"
"builders/homogeneous_volume_material_builder.cpp"
"builders/homogeneous_material_builder.cpp"
"builders/material_map_builder.cpp"
"builders/volume_builder.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "detray/builders/homogeneous_material_builder.hpp"

#include "detray/builders/cuboid_portal_generator.hpp"
#include "detray/builders/detector_builder.hpp"
#include "detray/builders/homogeneous_material_factory.hpp"
#include "detray/builders/surface_factory.hpp"
#include "detray/builders/volume_builder.hpp"
Expand Down
Loading

0 comments on commit 60bd754

Please sign in to comment.