Skip to content

Commit

Permalink
feat: fill name map (#509)
Browse files Browse the repository at this point in the history
This enables the IO of the volume names in the geometry json file and also fills and returns a valid name map for the telescope and toy detectors
  • Loading branch information
niermann999 authored Jul 24, 2023
1 parent 907863b commit 7f2774e
Show file tree
Hide file tree
Showing 47 changed files with 206 additions and 125 deletions.
12 changes: 9 additions & 3 deletions core/include/detray/geometry/surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,24 @@ class surface {
/// Not allowed: always needs a detector and a descriptor.
surface() = delete;

/// Constructor from detector @param det and volume descriptor
/// @param vol_idx from that detector.
/// Constructor from detector @param det and surface descriptor
/// @param desc from that detector.
DETRAY_HOST_DEVICE
constexpr surface(const detector_t &det, const descr_t &desc)
: m_detector{det}, m_desc{desc} {}

/// Constructor from detector @param det and volume index @param vol_idx in
/// Constructor from detector @param det and barcode @param bcd in
/// that detector.
DETRAY_HOST_DEVICE
constexpr surface(const detector_t &det, const geometry::barcode bcd)
: surface(det, det.surface(bcd)) {}

/// Conversion to surface interface around constant detector type
DETRAY_HOST_DEVICE
constexpr operator surface<const detector_t>() const {
return surface<const detector_t>{this->m_detector, this->m_desc};
}

/// Equality operator
///
/// @param rhs is the right hand side to be compared to
Expand Down
10 changes: 5 additions & 5 deletions core/include/detray/propagator/navigator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,17 @@ class navigator {

/// @returns the next surface the navigator intends to reach
DETRAY_HOST_DEVICE
inline auto next_surface() const -> const surface<const detector_type> {
return surface<const detector_type>{*m_detector,
m_next->sf_desc.barcode()};
inline auto next_surface() const {
return surface<detector_type>{*m_detector,
m_next->sf_desc.barcode()};
}

/// @returns current detector surface the navigator is on
/// (cannot be used when not on surface) - const
DETRAY_HOST_DEVICE
inline auto get_surface() const -> const surface<const detector_type> {
inline auto get_surface() const {
assert(is_on_module() or is_on_portal());
return surface<const detector_type>{*m_detector, barcode()};
return surface<detector_type>{*m_detector, barcode()};
}

/// @returns current navigation status - const
Expand Down
4 changes: 2 additions & 2 deletions core/include/detray/tools/volume_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class volume_builder : public volume_builder_interface<detector_t> {
/// Adds the @param name of the volume to a name map
template <typename name_map>
DETRAY_HOST void add_name(const name_map& names, std::string&& name) {
names.at(get_vol_index()) = std::move(name);
names.at(vol_index()) = std::move(name);
}

DETRAY_HOST
Expand All @@ -55,7 +55,7 @@ class volume_builder : public volume_builder_interface<detector_t> {
};

DETRAY_HOST
auto get_vol_index() -> dindex override { return m_volume->index(); }
auto vol_index() -> dindex override { return m_volume->index(); }

DETRAY_HOST
auto operator()() const -> const
Expand Down
6 changes: 2 additions & 4 deletions core/include/detray/tools/volume_builder_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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 get_vol_index() -> dindex = 0;
virtual auto vol_index() -> dindex = 0;

/// @returns reading access to the volume
DETRAY_HOST
Expand Down Expand Up @@ -131,9 +131,7 @@ class volume_decorator : public volume_builder_interface<detector_t> {
}

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

DETRAY_HOST
auto build(detector_t &det,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class detector_component_readers final {
/// Reads the full detector into @param det by calling the readers, while
/// using the name map @param names for to write the volume names.
virtual void read(const detector_t& det,
const typename detector_t::name_map& names,
typename detector_t::name_map& names,
const std::vector<std::string>& file_names) {
// We have to at least read a geometry
assert(m_readers.size() != 0u &&
Expand Down
5 changes: 4 additions & 1 deletion io/include/detray/io/common/geometry_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class geometry_reader : public reader_interface<detector_t> {
/// Deserialize a detector @param det from its io payload @param det_data
/// and add the volume names to @param name_map
static void deserialize(detector_t& det,
typename detector_t::name_map& /*name_map*/,
typename detector_t::name_map& name_map,
const detector_payload& det_data) {

// @todo Add volume grid
Expand All @@ -81,6 +81,9 @@ class geometry_reader : public reader_interface<detector_t> {

vbuilder.init_vol(det, static_cast<volume_id>(vol_data.type));

// Set the volume name if available
name_map[vbuilder.vol_index() + 1u] = vol_data.name;

// @todo add the volume placement, once it can be checked for the
// test detectors

Expand Down
16 changes: 12 additions & 4 deletions io/include/detray/io/common/geometry_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ class geometry_writer : public writer_interface<detector_t> {
}

/// Serialize a detector @param det into its io payload
static detector_payload serialize(const detector_t& det) {
static detector_payload serialize(
const detector_t& det, const typename detector_t::name_map& names) {
detector_payload det_data;
det_data.volumes.reserve((det.volumes().size()));

for (const auto& vol : det.volumes()) {
det_data.volumes.push_back(serialize(vol, det));
const auto map_itr = names.find(vol.index() + 1u);
if (map_itr == names.end()) {
det_data.volumes.push_back(serialize(vol, det, ""));
} else {
det_data.volumes.push_back(
serialize(vol, det, map_itr->second));
}
}

return det_data;
Expand Down Expand Up @@ -162,11 +169,12 @@ class geometry_writer : public writer_interface<detector_t> {

/// Serialize a detector portal @param sf into its io payload
static volume_payload serialize(
const typename detector_t::volume_type& vol_desc,
const detector_t& det) {
const typename detector_t::volume_type& vol_desc, const detector_t& det,
const std::string& name) {
volume_payload vol_data;

vol_data.index = serialize(vol_desc.index());
vol_data.name = name;
vol_data.transform =
serialize(det.transform_store()[vol_desc.transform()]);
vol_data.type = vol_desc.id();
Expand Down
3 changes: 2 additions & 1 deletion io/include/detray/io/common/grid_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class grid_writer : public writer_interface<detector_t> {

/// Serialize the grid collections of a detector @param det into their io
/// payload
static detector_grids_payload serialize(const detector_t& det) {
static detector_grids_payload serialize(
const detector_t& det, const typename detector_t::name_map&) {

detector_grids_payload grids_data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class homogeneous_material_writer : public writer_interface<detector_t> {
/// Serialize the material description of a detector @param det into its io
/// payload
static detector_homogeneous_material_payload serialize(
const detector_t& det) {
const detector_t& det, const typename detector_t::name_map&) {
using mat_types = typename detector_t::material_container::value_types;

detector_homogeneous_material_payload dm_data;
Expand Down
13 changes: 10 additions & 3 deletions io/include/detray/io/json/json_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// System include(s)
#include <cassert>
#include <ios>
#include <stdexcept>
#include <string>

namespace detray {
Expand Down Expand Up @@ -49,17 +50,23 @@ class json_writer final : public common_writer_t<detector_t> {
(mode == (std::ios_base::out | std::ios_base::trunc))) &&
"Illegal file mode for json writer");

// By convention the name of the detector is the first element
std::string det_name = "";
if (not names.empty()) {
det_name = names.at(0);
}

// Create a new file
std::string file_stem{names.at(0) + "_" + base_writer::tag};
std::string file_stem{det_name + "_" + base_writer::tag};
io::detail::file_handle file{file_stem, this->m_file_extension, mode};

// Write some general information
nlohmann::ordered_json out_json;
out_json["header"] = base_writer::write_header(det, names.at(0));
out_json["header"] = base_writer::write_header(det, det_name);

// Write the detector data into the json stream by using the
// conversion functions defined in "detray/io/json/json_io.hpp"
out_json["data"] = base_writer::serialize(det);
out_json["data"] = base_writer::serialize(det, names);

// Write to file
*file << std::setw(4) << out_json << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmarks/cpu/find_volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void BM_FIND_VOLUMES(benchmark::State &state) {
static constexpr unsigned int n_brl_layers{4u};
static constexpr unsigned int n_edc_layers{7u};
vecmem::host_memory_resource host_mr;
auto d = create_toy_geometry(host_mr, n_brl_layers, n_edc_layers);
auto [d, names] = create_toy_geometry(host_mr, n_brl_layers, n_edc_layers);

static const unsigned int itest = 10000u;

Expand Down
2 changes: 1 addition & 1 deletion tests/benchmarks/cpu/intersect_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void BM_INTERSECT_ALL(benchmark::State &state) {
static constexpr std::size_t n_brl_layers{4u};
static constexpr std::size_t n_edc_layers{7u};
vecmem::host_memory_resource host_mr;
auto d = create_toy_geometry(host_mr, n_brl_layers, n_edc_layers);
auto [d, names] = create_toy_geometry(host_mr, n_brl_layers, n_edc_layers);

using detector_t = decltype(d);
detector_t::geometry_context geo_context;
Expand Down
4 changes: 2 additions & 2 deletions tests/benchmarks/cuda/benchmark_propagator_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template <propagate_option opt>
static void BM_PROPAGATOR_CPU(benchmark::State &state) {

// Create the toy geometry
detector_host_type det = create_toy_geometry<host_container_types>(
auto [det, names] = create_toy_geometry<host_container_types>(
host_mr,
field_type(field_type::backend_t::configuration_t{
0.f, 0.f, 2.f * unit<scalar>::T}),
Expand Down Expand Up @@ -106,7 +106,7 @@ template <propagate_option opt>
static void BM_PROPAGATOR_CUDA(benchmark::State &state) {

// Create the toy geometry
detector_host_type det = create_toy_geometry<host_container_types>(
auto [det, names] = create_toy_geometry<host_container_types>(
bp_mng_mr, n_brl_layers, n_edc_layers);

// Get detector data
Expand Down
Loading

0 comments on commit 7f2774e

Please sign in to comment.