-
Notifications
You must be signed in to change notification settings - Fork 213
Add ElementSearchTree, use to speed up PointwiseInterpolator #6571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wthrowe
merged 8 commits into
sxs-collaboration:develop
from
nilsvu:element_search_tree
May 12, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d4cd7a6
Support single target point in IrregularInterpolant
nilsvu 8b6c2fe
PointwiseInterpolator: factor out interpolation in block-logical coords
nilsvu a4b89a5
Add element search tree
nilsvu 73c21bc
Use ElementSearchTree in element_logical_coordinates
nilsvu 50e6588
Fix a bug in interpolate_to_points
nilsvu 59b65e2
Add some benchmarks
nilsvu ac438c4
Use ElementSearchTree to speed up PointwiseInterpolator
nilsvu 9c49fa7
Update benchmark timings
nilsvu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Distributed under the MIT License. | ||
| // See LICENSE.txt for details. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <boost/geometry.hpp> | ||
| #include <boost/geometry/index/rtree.hpp> | ||
| #include <cstddef> | ||
| #include <map> | ||
| #include <utility> | ||
|
|
||
| #include "DataStructures/Tensor/Tensor.hpp" | ||
| #include "Domain/Structure/ElementId.hpp" | ||
|
|
||
| namespace boost::geometry::traits { | ||
| // Make Tensor compatible with Boost.Geometry | ||
nilsdeppe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // This is needed to search for a block-logical coordinate given as a Tensor | ||
| // in the `ElementSearchTree`. | ||
| template <typename DataType, size_t Dim, typename Frame> | ||
| struct tag<tnsr::I<DataType, Dim, Frame>> { | ||
| using type = boost::geometry::point_tag; | ||
| }; | ||
| template <typename DataType, size_t Dim, typename Frame> | ||
| struct coordinate_type<tnsr::I<DataType, Dim, Frame>> { | ||
| using type = DataType; | ||
| }; | ||
| template <typename DataType, size_t Dim, typename Frame> | ||
| struct coordinate_system<tnsr::I<DataType, Dim, Frame>> { | ||
| using type = boost::geometry::cs::cartesian; | ||
| }; | ||
| template <typename DataType, size_t Dim, typename Frame> | ||
| struct dimension<tnsr::I<DataType, Dim, Frame>> | ||
| : std::integral_constant<size_t, Dim> {}; | ||
| template <typename DataType, size_t Dim, typename Frame, size_t GetDimension> | ||
| struct access<tnsr::I<DataType, Dim, Frame>, GetDimension> { | ||
| static constexpr DataType get(const tnsr::I<DataType, Dim, Frame>& point) { | ||
| return ::get<GetDimension>(point); | ||
| } | ||
| static void set(tnsr::I<DataType, Dim, Frame>& point, const DataType& value) { | ||
| ::get<GetDimension>(point) = value; | ||
| } | ||
| }; | ||
| // Make ElementId compatible with Boost.Geometry | ||
| // Each ElementId defines a bounding box in block-logical coordinates, which is | ||
| // used to search for elements in the `ElementSearchTree`. | ||
| template <size_t Dim> | ||
| struct tag<ElementId<Dim>> { | ||
| using type = boost::geometry::box_tag; | ||
| }; | ||
| template <size_t Dim> | ||
| struct coordinate_type<ElementId<Dim>> { | ||
| using type = double; | ||
| }; | ||
| template <size_t Dim> | ||
| struct coordinate_system<ElementId<Dim>> { | ||
| using type = boost::geometry::cs::cartesian; | ||
| }; | ||
| template <size_t Dim> | ||
| struct dimension<ElementId<Dim>> : std::integral_constant<size_t, Dim> {}; | ||
| template <size_t Dim> | ||
| struct point_type<ElementId<Dim>> { | ||
| using type = tnsr::I<double, Dim, ::Frame::BlockLogical>; | ||
| }; | ||
| template <size_t Dim, size_t Index, size_t GetDimension> | ||
| struct indexed_access<ElementId<Dim>, Index, GetDimension> { | ||
| static constexpr double get(const ElementId<Dim>& element_id) { | ||
| if constexpr (Index == 0) { | ||
| return element_id.segment_id(GetDimension).endpoint(Side::Lower); | ||
| } else { | ||
| return element_id.segment_id(GetDimension).endpoint(Side::Upper); | ||
| } | ||
| } | ||
| }; | ||
| } // namespace boost::geometry::traits | ||
|
|
||
| namespace domain { | ||
|
|
||
| /*! | ||
| * \brief Search tree for efficiently looking up elements by their bounding | ||
| * boxes in block-logical coordinates. | ||
| * | ||
| * The search tree is constructed from a list of `ElementId`s, which define | ||
| * bounding boxes in block-logical coordinates. All `ElementId`s must be in the | ||
| * same block. Then, the search tree can be used to efficiently search for the | ||
| * element that contains a given block-logical coordinate (see e.g. | ||
| * `element_logical_coordinates`). | ||
| * | ||
| * Example usage: | ||
| * \snippet Test_ElementSearchTree.cpp element_search_tree_example | ||
| * | ||
| * Use `domain::index_element_ids()` to create one search tree for each block in | ||
| * the domain given the full list of `ElementId`s. | ||
| * | ||
| * \details The search tree is a `boost::geometry::rtree` with a choice of | ||
| * quadratic splitting algorithm and a maximum of 16 elements per node. These | ||
| * choices work well, but haven't been extensively tuned. | ||
| */ | ||
| template <size_t Dim> | ||
| using ElementSearchTree = | ||
| boost::geometry::index::rtree<ElementId<Dim>, | ||
| boost::geometry::index::quadratic<16>>; | ||
|
|
||
| /*! | ||
| * \brief Sorts element IDs into one `ElementSearchTree` per block for efficient | ||
| * searching. | ||
| * | ||
| * Returns a map of search trees indexed by block ID. Each search tree contains | ||
| * all the `ElementId`s for that block. | ||
| * | ||
| * \see ElementSearchTree | ||
| */ | ||
| template <size_t Dim> | ||
| std::map<size_t, ElementSearchTree<Dim>> index_element_ids( | ||
| const std::vector<ElementId<Dim>>& element_ids) { | ||
| std::map<size_t, ElementSearchTree<Dim>> trees{}; | ||
| for (const auto& element_id : element_ids) { | ||
| trees[element_id.block_id()].insert(element_id); | ||
| } | ||
| return trees; | ||
| } | ||
|
|
||
| } // namespace domain | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a bug in the previous commit? Fix it there.