From 5c3071befb8b77fbb408af06b8950d3cb7a82633 Mon Sep 17 00:00:00 2001 From: Joana Niermann Date: Wed, 2 Aug 2023 20:41:49 +0200 Subject: [PATCH] Add the grid reader to the detector reader. It generates all possible grid types and then selects the ones to be added to the detector by switch case statements on the type ids that are in the payloads. In the grid builder, the missing information is added to the surface descriptors, once the basic volume geometry has been build. It contains some other fixes, like correctly adapting the cylinder grid radius to the volume, updating the index offsets when pushing a grid into a grid collection, as well as maintaining correct surface sorting during the IO, so that the surface indices saved in the grid match still match the content of the surface_lookup.O --- .../detray/core/detail/multi_store.hpp | 7 + .../detray/core/detail/single_store.hpp | 7 +- core/include/detray/definitions/grid_axis.hpp | 2 +- core/include/detray/geometry/barcode.hpp | 2 +- .../geometry/detail/surface_descriptor.hpp | 11 + core/include/detray/geometry/surface.hpp | 7 +- core/include/detray/masks/masks.hpp | 7 + .../surface_finders/brute_force_finder.hpp | 24 ++ .../detray/surface_finders/grid/grid.hpp | 19 +- .../surface_finders/grid/grid_collection.hpp | 43 +++ core/include/detray/tools/bin_fillers.hpp | 7 +- core/include/detray/tools/bounding_volume.hpp | 1 - .../include/detray/tools/detector_builder.hpp | 6 +- core/include/detray/tools/grid_builder.hpp | 145 +++---- core/include/detray/tools/grid_factory.hpp | 57 ++- core/include/detray/tools/surface_factory.hpp | 47 ++- .../tools/surface_factory_interface.hpp | 26 +- core/include/detray/tools/volume_builder.hpp | 2 +- .../detray/utils/consistency_checker.hpp | 1 + core/include/detray/utils/inspectors.hpp | 1 - core/include/detray/utils/type_list.hpp | 170 +++++++++ .../common/detail/detector_components_io.hpp | 12 +- .../detray/io/common/detector_reader.hpp | 13 +- .../detray/io/common/geometry_reader.hpp | 21 +- .../detray/io/common/geometry_writer.hpp | 10 +- io/include/detray/io/common/grid_reader.hpp | 356 ++++++++++++++++++ io/include/detray/io/common/grid_writer.hpp | 43 ++- .../io/common/homogeneous_material_reader.hpp | 5 +- io/include/detray/io/common/payloads.hpp | 12 +- .../detray/io/json/json_geometry_io.hpp | 6 + io/include/detray/io/json/json_grids_io.hpp | 28 +- io/include/detray/io/json/json_reader.hpp | 16 +- io/include/detray/io/json/json_writer.hpp | 12 +- .../plugins/svgtools/conversion/surface.hpp | 3 +- .../plugins/svgtools/utils/mask_utils.hpp | 3 +- .../tests/common/test_toy_detector.hpp | 51 +-- .../include/tests/common/tools/hash_tree.hpp | 1 - tests/unit_tests/core/CMakeLists.txt | 1 + tests/unit_tests/core/tools_hash_tree.cpp | 1 - tests/unit_tests/core/utils_type_list.cpp | 40 ++ tests/unit_tests/cpu/grid_grid_builder.cpp | 35 +- .../unit_tests/cpu/tools_material_builder.cpp | 3 +- .../unit_tests/io/io_json_detector_reader.cpp | 12 +- tests/unit_tests/io/io_json_payload.cpp | 12 +- .../detray/detectors/create_toy_geometry.hpp | 84 +++-- .../detray/detectors/create_wire_chamber.hpp | 34 +- .../detray/detectors/detector_helper.hpp | 4 +- .../include/detray/detectors/toy_metadata.hpp | 2 +- 48 files changed, 1131 insertions(+), 281 deletions(-) create mode 100644 core/include/detray/utils/type_list.hpp create mode 100644 io/include/detray/io/common/grid_reader.hpp create mode 100644 tests/unit_tests/core/utils_type_list.cpp diff --git a/core/include/detray/core/detail/multi_store.hpp b/core/include/detray/core/detail/multi_store.hpp index 718df5027..83a9fcd62 100644 --- a/core/include/detray/core/detail/multi_store.hpp +++ b/core/include/detray/core/detail/multi_store.hpp @@ -207,6 +207,13 @@ class multi_store { detail::get(m_tuple_container).reserve(n); } + /// Resize the underlying container to @param n for a collection given by + /// @tparam id + template + DETRAY_HOST void resize(std::size_t n, const context_type & /*ctx*/) { + detail::get(m_tuple_container).resize(n); + } + /// Add a new element to a collection /// /// @tparam ID is the id of the collection diff --git a/core/include/detray/core/detail/single_store.hpp b/core/include/detray/core/detail/single_store.hpp index 9ba70059b..8c7f3893c 100644 --- a/core/include/detray/core/detail/single_store.hpp +++ b/core/include/detray/core/detail/single_store.hpp @@ -164,11 +164,16 @@ class single_store { m_container.clear(); } - /// Reserve memory of size @param n for a collection given by @tparam id + /// Reserve memory of size @param n for a given geometry context DETRAY_HOST void reserve(std::size_t n, const context_type & /*ctx*/) { m_container.reserve(n); } + /// Resize the underlying container to @param n for a given geometry context + DETRAY_HOST void resize(std::size_t n, const context_type & /*ctx*/) { + m_container.resize(n); + } + /// Add a new element to the collection - copy /// /// @tparam U type that can be converted to T diff --git a/core/include/detray/definitions/grid_axis.hpp b/core/include/detray/definitions/grid_axis.hpp index 08e9285cf..0524bc54d 100644 --- a/core/include/detray/definitions/grid_axis.hpp +++ b/core/include/detray/definitions/grid_axis.hpp @@ -62,4 +62,4 @@ using bounds_t = n_axis::closed>; /// @} -} // namespace detray::n_axis \ No newline at end of file +} // namespace detray::n_axis diff --git a/core/include/detray/geometry/barcode.hpp b/core/include/detray/geometry/barcode.hpp index 7c48d5d4b..e2e48941f 100644 --- a/core/include/detray/geometry/barcode.hpp +++ b/core/include/detray/geometry/barcode.hpp @@ -142,7 +142,7 @@ class barcode { DETRAY_HOST friend std::ostream& operator<<(std::ostream& os, const barcode c) { if (c.is_invalid()) { - return (os << "undefined"); + os << "INVALID: "; } static const char* const names[] = { diff --git a/core/include/detray/geometry/detail/surface_descriptor.hpp b/core/include/detray/geometry/detail/surface_descriptor.hpp index 3af3cc509..9c4515d13 100644 --- a/core/include/detray/geometry/detail/surface_descriptor.hpp +++ b/core/include/detray/geometry/detail/surface_descriptor.hpp @@ -192,6 +192,17 @@ class surface_descriptor { return m_barcode.id() == surface_id::e_passive; } + /// @returns a string stream that prints the surface details + DETRAY_HOST + friend std::ostream &operator<<(std::ostream &os, + const surface_descriptor &sf) { + os << sf.m_barcode; + os << " | trf.: " << sf._trf; + os << " | mask: " << sf._mask; + os << " | mat.: " << sf._material; + return os; + } + private: geometry::barcode m_barcode{}; mask_link _mask{}; diff --git a/core/include/detray/geometry/surface.hpp b/core/include/detray/geometry/surface.hpp index 2a2ee1927..a3f66cdd0 100644 --- a/core/include/detray/geometry/surface.hpp +++ b/core/include/detray/geometry/surface.hpp @@ -357,12 +357,7 @@ class surface { /// @returns a string stream that prints the surface details DETRAY_HOST friend std::ostream &operator<<(std::ostream &os, const surface &sf) { - os << sf.barcode(); - os << " | trf.: " << sf.m_desc.transform(); - os << " | mask: " << static_cast(sf.m_desc.mask().id()) << ", " - << sf.m_desc.mask().index(); - os << " | mat.: " << static_cast(sf.m_desc.material().id()) - << ", " << sf.m_desc.material().index(); + os << sf.m_desc; return os; } diff --git a/core/include/detray/masks/masks.hpp b/core/include/detray/masks/masks.hpp index fe0ba6ae8..778cc07f0 100644 --- a/core/include/detray/masks/masks.hpp +++ b/core/include/detray/masks/masks.hpp @@ -244,6 +244,13 @@ class mask { return ss.str(); } + /// @returns a string stream that prints the mask details + DETRAY_HOST + friend std::ostream& operator<<(std::ostream& os, const mask& m) { + os << m.to_string(); + return os; + } + private: shape _shape; mask_values _values; diff --git a/core/include/detray/surface_finders/brute_force_finder.hpp b/core/include/detray/surface_finders/brute_force_finder.hpp index 62efa7026..50ad35b0e 100644 --- a/core/include/detray/surface_finders/brute_force_finder.hpp +++ b/core/include/detray/surface_finders/brute_force_finder.hpp @@ -11,6 +11,7 @@ #include "detray/core/detail/container_buffers.hpp" #include "detray/core/detail/container_views.hpp" #include "detray/definitions/containers.hpp" +#include "detray/definitions/detail/algorithms.hpp" #include "detray/definitions/indexing.hpp" #include "detray/definitions/qualifiers.hpp" #include "detray/utils/ranges.hpp" @@ -131,6 +132,12 @@ class brute_force_collection { return size() == size_type{0}; } + /// @returns access to the volume offsets - const + DETRAY_HOST const auto& offsets() const { return m_offsets; } + + /// @returns access to the volume offsets + DETRAY_HOST auto& offsets() { return m_offsets; } + /// @return access to the surface container - const. DETRAY_HOST_DEVICE auto all() const -> const vector_type& { return m_surfaces; } @@ -161,6 +168,23 @@ class brute_force_collection { m_offsets.push_back(static_cast(m_surfaces.size())); } + /// Remove surface from collection + DETRAY_HOST auto erase( + typename vector_type::iterator pos) noexcept(false) { + // Remove one element + auto next = m_surfaces.erase(pos); + + // Update the upper bound of the range and all following ranges + const auto idx{static_cast(pos - m_surfaces.begin())}; + auto offset = + detail::upper_bound(m_offsets.begin(), m_offsets.end(), idx); + for (auto itr = offset; itr != m_offsets.end(); ++itr) { + --(*itr); + } + + return next; + } + /// @return the view on the brute force finders - non-const DETRAY_HOST constexpr auto get_data() noexcept -> view_type { diff --git a/core/include/detray/surface_finders/grid/grid.hpp b/core/include/detray/surface_finders/grid/grid.hpp index 33d07a9bb..17939a75d 100644 --- a/core/include/detray/surface_finders/grid/grid.hpp +++ b/core/include/detray/surface_finders/grid/grid.hpp @@ -202,12 +202,18 @@ class grid { return bin(n_axis::multi_bin{{indices...}}); } - /// @param mbin the multi-index of bins over all axes + /// @param mbin the multi-index of bins over all axes - const DETRAY_HOST_DEVICE auto bin(const n_axis::multi_bin &mbin) const { return bin(m_serializer(m_axes, mbin)); } + /// @param mbin the multi-index of bins over all axes + DETRAY_HOST_DEVICE + auto bin(const n_axis::multi_bin &mbin) { + return bin(m_serializer(m_axes, mbin)); + } + /// @param gbin the global bin index - const DETRAY_HOST_DEVICE auto bin(const dindex gbin) const { @@ -276,7 +282,7 @@ class grid { return all(); } - /// Find the value of a single bin + /// Find the value of a single bin - const /// /// @param p is point in the local frame /// @@ -286,6 +292,15 @@ class grid { return bin(m_axes.bins(p)); } + /// Find the value of a single bin + /// + /// @param p is point in the local frame + /// + /// @return the iterable view of the bin content + DETRAY_HOST_DEVICE auto search(const typename local_frame_type::point3 &p) { + return bin(m_axes.bins(p)); + } + /// @brief Return a neighborhood of values from the grid /// /// The lookup is done with a neighborhood around the bin which contains the diff --git a/core/include/detray/surface_finders/grid/grid_collection.hpp b/core/include/detray/surface_finders/grid/grid_collection.hpp index 29ea44597..9e86f901f 100644 --- a/core/include/detray/surface_finders/grid/grid_collection.hpp +++ b/core/include/detray/surface_finders/grid/grid_collection.hpp @@ -181,21 +181,64 @@ class grid_collection< DETRAY_HOST constexpr auto push_back( const typename grid_type::template type &gr) noexcept(false) -> void { + // Current offset into the global bin storage for the new grid m_offsets.push_back(static_cast(m_bins.size())); + // Add the bins of the new grid to the collection const auto *grid_bins = gr.data().bin_data(); m_bins.insert(m_bins.end(), grid_bins->begin(), grid_bins->end()); + // Add the axes data of the new grid to the collection + // (how to lookup the axis bin edges) const auto *axes_data = gr.axes().data().axes_data(); m_axes_data.insert(m_axes_data.end(), axes_data->begin(), axes_data->end()); + // Current offset into the global bin edges storage + dindex bin_edges_offset = static_cast(m_bin_edges.size()); + // The binning types of the axes + const auto binnings = get_binning(gr.axes()); + + // Update the bin edges index range for the axes in the grid collection + for (std::size_t i = m_axes_data.size() - grid_type::Dim; + i < m_axes_data.size(); ++i) { + auto &bin_entry_range = m_axes_data[i]; + bin_entry_range[0] += bin_edges_offset; + // If the axis has irregular binning, the second entry is the index + // of the last bin edge value instead of the number of bins + if (binnings[i] == n_axis::binning::e_irregular) { + bin_entry_range[1] += bin_edges_offset; + } + } + + // Add the bin edges of the new grid to the collection const auto *bin_edges = gr.axes().data().edges(); + m_bin_edges.insert(m_bin_edges.end(), bin_edges->begin(), bin_edges->end()); } private: + /// @returns an array that contians the binning enum values of the + /// corresponding single axes in @param axes + template + static auto get_binning( + const n_axis::multi_axis &axes) { + + // Serialize every single axis and construct array from their payloads + std::array binning_types{ + get_binning(axes.template get_axis())...}; + + return binning_types; + } + + /// @returns the binning enum value of a single axis @param axis + template + static auto get_binning( + const n_axis::single_axis &axis) { + return axis.binning(); + } + /// Offsets for the respective grids into the bin storage vector_type m_offsets{}; /// Contains the bin content for all grids diff --git a/core/include/detray/tools/bin_fillers.hpp b/core/include/detray/tools/bin_fillers.hpp index f1f8d7c98..7d9c7c742 100644 --- a/core/include/detray/tools/bin_fillers.hpp +++ b/core/include/detray/tools/bin_fillers.hpp @@ -69,10 +69,13 @@ struct fill_by_pos { // Fill the volumes surfaces into the grid for (const auto [idx, sf] : detray::views::enumerate(surfaces)) { // no portals in grids allowed - assert(not sf.is_portal()); + if (not sf.is_sensitive() or sf.volume() != vol.index()) { + continue; + } - const auto &sf_trf = transforms.at(idx, ctx); + const auto &sf_trf = transforms.at(sf.transform(), ctx); const auto &t = sf_trf.translation(); + // transform to axis coordinate system const auto loc_pos = grid.global_to_local(vol.transform(), t, t); diff --git a/core/include/detray/tools/bounding_volume.hpp b/core/include/detray/tools/bounding_volume.hpp index 5b0661af2..1c973aaa7 100644 --- a/core/include/detray/tools/bounding_volume.hpp +++ b/core/include/detray/tools/bounding_volume.hpp @@ -15,7 +15,6 @@ // System include(s) #include -#include #include #include #include diff --git a/core/include/detray/tools/detector_builder.hpp b/core/include/detray/tools/detector_builder.hpp index 6528c25c1..f07f1675f 100644 --- a/core/include/detray/tools/detector_builder.hpp +++ b/core/include/detray/tools/detector_builder.hpp @@ -59,12 +59,12 @@ class detector_builder { /// Decorate a volume builder at position @param volume_idx with more /// functionality - template