Skip to content

Commit

Permalink
Merge pull request #528 from niermann999/feat-json-grid-reader
Browse files Browse the repository at this point in the history
feat: grid reader
  • Loading branch information
beomki-yeo authored Oct 2, 2023
2 parents e68eca1 + 5c3071b commit 58dead6
Show file tree
Hide file tree
Showing 48 changed files with 1,131 additions and 281 deletions.
7 changes: 7 additions & 0 deletions core/include/detray/core/detail/multi_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ class multi_store {
detail::get<value_types::to_index(id)>(m_tuple_container).reserve(n);
}

/// Resize the underlying container to @param n for a collection given by
/// @tparam id
template <ID id>
DETRAY_HOST void resize(std::size_t n, const context_type & /*ctx*/) {
detail::get<value_types::to_index(id)>(m_tuple_container).resize(n);
}

/// Add a new element to a collection
///
/// @tparam ID is the id of the collection
Expand Down
7 changes: 6 additions & 1 deletion core/include/detray/core/detail/single_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/include/detray/definitions/grid_axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ using bounds_t =
n_axis::closed<axis_label>>;
/// @}

} // namespace detray::n_axis
} // namespace detray::n_axis
2 changes: 1 addition & 1 deletion core/include/detray/geometry/barcode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down
11 changes: 11 additions & 0 deletions core/include/detray/geometry/detail/surface_descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
7 changes: 1 addition & 6 deletions core/include/detray/geometry/surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<dindex>(sf.m_desc.mask().id()) << ", "
<< sf.m_desc.mask().index();
os << " | mat.: " << static_cast<dindex>(sf.m_desc.material().id())
<< ", " << sf.m_desc.material().index();
os << sf.m_desc;
return os;
}

Expand Down
7 changes: 7 additions & 0 deletions core/include/detray/masks/masks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions core/include/detray/surface_finders/brute_force_finder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<surface_t>& { return m_surfaces; }
Expand Down Expand Up @@ -161,6 +168,23 @@ class brute_force_collection {
m_offsets.push_back(static_cast<dindex>(m_surfaces.size()));
}

/// Remove surface from collection
DETRAY_HOST auto erase(
typename vector_type<surface_t>::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<std::size_t>(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 {
Expand Down
19 changes: 17 additions & 2 deletions core/include/detray/surface_finders/grid/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,18 @@ class grid {
return bin(n_axis::multi_bin<Dim>{{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<Dim> &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<Dim> &mbin) {
return bin(m_serializer(m_axes, mbin));
}

/// @param gbin the global bin index - const
DETRAY_HOST_DEVICE
auto bin(const dindex gbin) const {
Expand Down Expand Up @@ -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
///
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions core/include/detray/surface_finders/grid/grid_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,64 @@ class grid_collection<
DETRAY_HOST constexpr auto push_back(
const typename grid_type::template type<true> &gr) noexcept(false)
-> void {
// Current offset into the global bin storage for the new grid
m_offsets.push_back(static_cast<size_type>(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<dindex>(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 <bool ownership, typename local_frame_t, typename... axis_ts>
static auto get_binning(
const n_axis::multi_axis<ownership, local_frame_t, axis_ts...> &axes) {

// Serialize every single axis and construct array from their payloads
std::array<n_axis::binning, sizeof...(axis_ts)> binning_types{
get_binning(axes.template get_axis<axis_ts>())...};

return binning_types;
}

/// @returns the binning enum value of a single axis @param axis
template <typename bounds_t, typename binning_t>
static auto get_binning(
const n_axis::single_axis<bounds_t, binning_t> &axis) {
return axis.binning();
}

/// Offsets for the respective grids into the bin storage
vector_type<size_type> m_offsets{};
/// Contains the bin content for all grids
Expand Down
7 changes: 5 additions & 2 deletions core/include/detray/tools/bin_fillers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion core/include/detray/tools/bounding_volume.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

// System include(s)
#include <cassert>
#include <iostream>
#include <limits>
#include <type_traits>
#include <vector>
Expand Down
6 changes: 3 additions & 3 deletions core/include/detray/tools/detector_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class detector_builder {

/// Decorate a volume builder at position @param volume_idx with more
/// functionality
template <template <typename> class builder_t>
template <class builder_t>
DETRAY_HOST auto decorate(dindex volume_idx)
-> volume_builder_interface<detector_type>* {

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

return m_volumes[volume_idx].get();
}
Expand Down
Loading

0 comments on commit 58dead6

Please sign in to comment.