From ef88961e9e1159905be28ed7c5ec85ec69d8933f Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 26 May 2023 16:05:31 +0200 Subject: [PATCH 001/145] WIP --- .../spatial_search_result_container.cpp | 126 +++++++++++++++ .../spatial_search_result_container.h | 153 ++++++++++++++++++ 2 files changed, 279 insertions(+) create mode 100644 kratos/spatial_containers/spatial_search_result_container.cpp create mode 100644 kratos/spatial_containers/spatial_search_result_container.h diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp new file mode 100644 index 000000000000..e855c88acd4e --- /dev/null +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -0,0 +1,126 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +// System includes + +// External includes + +// Project includes +#include "includes/data_communicator.h" +#include "includes/geometrical_object.h" +#include "utilities/global_pointer_utilities.h" +#include "spatial_containers/spatial_search_result_container.h" + +namespace Kratos +{ + +template +SpatialSearchResultContainer::SpatialSearchResultContainer(const DataCommunicator& rDataCommunicator) + : mrDataCommunicator(rDataCommunicator) +{ + // TODO: Add something if required +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainer::AddResult(const SpatialSearchResult& rResult) +{ + //mLocalPointers.push_back(); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainer::Clear() +{ + // Clear pointer + mpGlobalPointerCommunicator = nullptr; + + // Clear local pointers + mLocalPointers.clear(); + + // Clear global pointers + mGlobalPointers.clear(); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainer::Synchronize() +{ + // Synchronize local pointers to global pointers // TODO: Fix + mGlobalPointers = GlobalPointerUtilities::GlobalRetrieveGlobalPointers(mLocalPointers, mrDataCommunicator); + + // Generate the communicator + mpGlobalPointerCommunicator = Kratos::make_shared>(mrDataCommunicator, mGlobalPointers.ptr_begin(), mGlobalPointers.ptr_end()); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +std::string SpatialSearchResultContainer::Info() const +{ + std::stringstream buffer; + buffer << "SpatialSearchResultContainer" ; + return buffer.str(); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainer::PrintInfo(std::ostream& rOStream) const +{ + rOStream << "SpatialSearchResultContainer"; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainer::PrintData(std::ostream& rOStream) const +{ +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainer::save(Serializer& rSerializer) const +{ + rSerializer.save("LocalPointers", mLocalPointers); + rSerializer.save("GlobalPointers", mGlobalPointers); + //rSerializer.save("GlobalPointerCommunicator", mpGlobalPointerCommunicator); // Not necessary, is created and filled during use +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainer::load(Serializer& rSerializer) +{ + rSerializer.load("LocalPointers", mLocalPointers); + rSerializer.load("GlobalPointers", mGlobalPointers); + //rSerializer.load("GlobalPointerCommunicator", mpGlobalPointerCommunicator); // Not necessary, is created and filled during use +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template class SpatialSearchResultContainer; + +} // namespace Kratos \ No newline at end of file diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h new file mode 100644 index 000000000000..76a0fdb22fca --- /dev/null +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -0,0 +1,153 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +#pragma once + +// System includes + +// External includes + +// Project includes +#include "utilities/pointer_communicator.h" +#include "spatial_containers/spatial_search_result.h" +#include "containers/pointer_vector_set.h" +#include "includes/indexed_object.h" + +namespace Kratos +{ +///@addtogroup KratosCore +///@{ + +///@name Kratos Classes +///@{ + +class DataCommunicator; // forward declaration + +/** + * @class SpatialSearchResultContainer + * @brief Spatial search result container + * @details This class is used to store the results of a spatial search + * @tparam TObjectType The type of the object + * @ingroup KratosCore + * @author Vicente Mataix Ferrandiz + */ +template +class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer +{ +public: + ///@name Type Definitions + ///@{ + + /// Pointer definition of SpatialSearchResultContainer + KRATOS_CLASS_POINTER_DEFINITION(SpatialSearchResultContainer); + + /// Global pointer definition of TObjectType + using TPointerType = GlobalPointer; + + ///@} + ///@name Life Cycle + ///@{ + + /** + * @brief Constructor + * @param rDataCommunicator The data communicator + */ + SpatialSearchResultContainer(const DataCommunicator& rDataCommunicator); + + /// Destructor. + virtual ~SpatialSearchResultContainer() = default; + + ///@} + ///@name Operators + ///@{ + + ///@} + ///@name Operations + ///@{ + + /** + * @brief Add a result to the container + * @param rResult The result + */ + void AddResult(const SpatialSearchResult& rResult); + + /** + * @brief Clear the containers + * @details This method clears the containers + */ + void Clear(); + + /** + * @brief Synchronize the container between partitions + * @details This method synchronizes the container between partitions + */ + void Synchronize(); + + ///@} + ///@name Input and output + ///@{ + + /// Turn back information as a string. + virtual std::string Info() const; + + /// Print information about this object. + virtual void PrintInfo(std::ostream& rOStream) const; + + /// Print object's data. + virtual void PrintData(std::ostream& rOStream) const; + + ///@} +private: + ///@name Member Variables + ///@{ + + const DataCommunicator& mrDataCommunicator; /// The data communicator + PointerVectorSet, + std::equal_to, + typename TObjectType::Pointer, + std::vector< typename TObjectType::Pointer > + > mLocalPointers; /// Local pointers of the container + GlobalPointersVector mGlobalPointers; /// Global pointers of the container + typename GlobalPointerCommunicator::Pointer mpGlobalPointerCommunicator = nullptr; /// Global pointer to the communicator + + ///@} + ///@name Private Operations + ///@{ + + ///@} + ///@name Serialization + ///@{ + + friend class Serializer; + + /** + * @brief Save the object in a serializer + * @param rSerializer The serializer + */ + void save(Serializer& rSerializer) const; + + /** + * @brief Load the object in a serializer + * @param rSerializer The serializer + */ + void load(Serializer& rSerializer); + + ///@} +}; // Class SpatialSearchResultContainer + +///@} + +///@} addtogroup block + +} // namespace Kratos. \ No newline at end of file From 9bad4a6ecad493e4e5846114e0671d05ff029084 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 26 May 2023 16:35:18 +0200 Subject: [PATCH 002/145] Improve --- .../spatial_search_result_container.cpp | 9 +++++---- .../spatial_containers/spatial_search_result_container.h | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index e855c88acd4e..a8b02db86a8d 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -34,9 +34,10 @@ SpatialSearchResultContainer::SpatialSearchResultContainer(const Da /***********************************************************************************/ template -void SpatialSearchResultContainer::AddResult(const SpatialSearchResult& rResult) +void SpatialSearchResultContainer::AddResult(SpatialSearchResult& rResult) { - //mLocalPointers.push_back(); + // Push_back in local pointers + mLocalPointers.push_back(Kratos::intrusive_ptr(rResult.Get().get())); } /***********************************************************************************/ @@ -59,9 +60,9 @@ void SpatialSearchResultContainer::Clear() /***********************************************************************************/ template -void SpatialSearchResultContainer::Synchronize() +void SpatialSearchResultContainer::SynchronizeAll() { - // Synchronize local pointers to global pointers // TODO: Fix + // Synchronize local pointers to global pointers mGlobalPointers = GlobalPointerUtilities::GlobalRetrieveGlobalPointers(mLocalPointers, mrDataCommunicator); // Generate the communicator diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 76a0fdb22fca..fe8a76cb8dd5 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -78,7 +78,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer * @brief Add a result to the container * @param rResult The result */ - void AddResult(const SpatialSearchResult& rResult); + void AddResult(SpatialSearchResult& rResult); /** * @brief Clear the containers @@ -90,7 +90,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer * @brief Synchronize the container between partitions * @details This method synchronizes the container between partitions */ - void Synchronize(); + void SynchronizeAll(); ///@} ///@name Input and output From 986580cfb639f98c7b39dffb5b85d0cc6b66e74d Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 26 May 2023 17:23:00 +0200 Subject: [PATCH 003/145] Extend --- .../spatial_search_result_container.cpp | 7 +++++- .../spatial_search_result_container.h | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index a8b02db86a8d..b0b4be4390ec 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -37,7 +37,12 @@ template void SpatialSearchResultContainer::AddResult(SpatialSearchResult& rResult) { // Push_back in local pointers - mLocalPointers.push_back(Kratos::intrusive_ptr(rResult.Get().get())); + TObjectType* p_local_result = rResult.Get().get(); + mLocalPointers.push_back(Kratos::intrusive_ptr(p_local_result)); + + // Add distances + const std::size_t id = p_local_result->Id(); + mLocalDistances.insert({id, rResult.GetDistance()}); } /***********************************************************************************/ diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index fe8a76cb8dd5..21611f289a29 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -13,6 +13,7 @@ #pragma once // System includes +#include // External includes @@ -92,6 +93,27 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer */ void SynchronizeAll(); + /** + * @brief Applies a user-provided function to the global pointers and return a proxy to the results. + * @tparam TFunctorType Functor type. + * @param UserFunctor The user-provided function. + * @return A proxy to the results. + */ + template + ResultsProxy< + TObjectType, + TFunctorType // TODO: Unfortunately this is deprecated in c++17, so we will have to change this call in the future + > Apply(TFunctorType&& UserFunctor) + { + // Check if the communicator has been created + if (mpGlobalPointerCommunicator == nullptr) { + SynchronizeAll(); + } + + // Apply the user-provided function + return mpGlobalPointerCommunicator->Apply(std::forward(UserFunctor)); + } + ///@} ///@name Input and output ///@{ @@ -119,6 +141,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer std::vector< typename TObjectType::Pointer > > mLocalPointers; /// Local pointers of the container GlobalPointersVector mGlobalPointers; /// Global pointers of the container + std::unordered_map mLocalDistances; /// The local distances typename GlobalPointerCommunicator::Pointer mpGlobalPointerCommunicator = nullptr; /// Global pointer to the communicator ///@} From d31ff1d62c14c995a22cabfc19a9bc56e8e0a758 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 26 May 2023 18:30:24 +0200 Subject: [PATCH 004/145] More advances --- .../spatial_search_result_container.cpp | 120 +++++++++++- .../spatial_search_result_container.h | 174 +++++++++++++++++- 2 files changed, 278 insertions(+), 16 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index b0b4be4390ec..f77d1688084c 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -11,6 +11,7 @@ // // System includes +#include // External includes @@ -24,8 +25,7 @@ namespace Kratos { template -SpatialSearchResultContainer::SpatialSearchResultContainer(const DataCommunicator& rDataCommunicator) - : mrDataCommunicator(rDataCommunicator) +SpatialSearchResultContainer::SpatialSearchResultContainer() { // TODO: Add something if required } @@ -41,7 +41,7 @@ void SpatialSearchResultContainer::AddResult(SpatialSearchResult(p_local_result)); // Add distances - const std::size_t id = p_local_result->Id(); + const IndexType id = p_local_result->Id(); mLocalDistances.insert({id, rResult.GetDistance()}); } @@ -65,13 +65,13 @@ void SpatialSearchResultContainer::Clear() /***********************************************************************************/ template -void SpatialSearchResultContainer::SynchronizeAll() +void SpatialSearchResultContainer::SynchronizeAll(const DataCommunicator& rDataCommunicator) { // Synchronize local pointers to global pointers - mGlobalPointers = GlobalPointerUtilities::GlobalRetrieveGlobalPointers(mLocalPointers, mrDataCommunicator); + mGlobalPointers = GlobalPointerUtilities::GlobalRetrieveGlobalPointers(mLocalPointers, rDataCommunicator); // Generate the communicator - mpGlobalPointerCommunicator = Kratos::make_shared>(mrDataCommunicator, mGlobalPointers.ptr_begin(), mGlobalPointers.ptr_end()); + mpGlobalPointerCommunicator = Kratos::make_shared>(rDataCommunicator, mGlobalPointers.ptr_begin(), mGlobalPointers.ptr_end()); } /***********************************************************************************/ @@ -100,6 +100,9 @@ void SpatialSearchResultContainer::PrintInfo(std::ostream& rOStream template void SpatialSearchResultContainer::PrintData(std::ostream& rOStream) const { + rOStream << "SpatialSearchResultContainer data summary: " << "\n"; + rOStream << "\tNumber of local pointers: " << mLocalPointers.size() << "\n"; + rOStream << "\tNumber of global pointers: " << mGlobalPointers.size() << "\n"; } /***********************************************************************************/ @@ -110,6 +113,7 @@ void SpatialSearchResultContainer::save(Serializer& rSerializer) co { rSerializer.save("LocalPointers", mLocalPointers); rSerializer.save("GlobalPointers", mGlobalPointers); + rSerializer.save("LocalDistances", mLocalDistances); //rSerializer.save("GlobalPointerCommunicator", mpGlobalPointerCommunicator); // Not necessary, is created and filled during use } @@ -121,6 +125,7 @@ void SpatialSearchResultContainer::load(Serializer& rSerializer) { rSerializer.load("LocalPointers", mLocalPointers); rSerializer.load("GlobalPointers", mGlobalPointers); + rSerializer.load("LocalDistances", mLocalDistances); //rSerializer.load("GlobalPointerCommunicator", mpGlobalPointerCommunicator); // Not necessary, is created and filled during use } @@ -129,4 +134,107 @@ void SpatialSearchResultContainer::load(Serializer& rSerializer) template class SpatialSearchResultContainer; +/***********************************************************************************/ +/***********************************************************************************/ + +template +SpatialSearchResultContainer& SpatialSearchResultContainerMap::InitializeResult(const array_1d& rCoordinates) +{ + const HashType hash = Hash(rCoordinates); + // If doesn't exists, create it + if (!HasResult(rCoordinates)) { + mPointResults.insert({hash, SpatialSearchResultContainer()}); + } + return mPointResults[hash]; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +bool SpatialSearchResultContainerMap::HasResult(const array_1d& rCoordinates) const +{ + const HashType hash = Hash(rCoordinates); + if (mPointResults.find(hash) != mPointResults.end()) { + return true; + } + return false; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainerMap::Clear() +{ + mPointResults.clear(); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +typename SpatialSearchResultContainerMap::HashType SpatialSearchResultContainerMap::Hash(const array_1d& rCoordinates) const +{ + std::hash> hasher; + return hasher(rCoordinates); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +std::string SpatialSearchResultContainerMap::Info() const +{ + std::stringstream buffer; + buffer << "SpatialSearchResultContainerMap" ; + return buffer.str(); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainerMap::PrintInfo(std::ostream& rOStream) const +{ + rOStream << "SpatialSearchResultContainerMap"; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainerMap::PrintData(std::ostream& rOStream) const +{ + // Print results + rOStream << "SpatialSearchResultContainerMap data summary: " << "\n"; + for (auto it = mPointResults.begin(); it != mPointResults.end(); ++it) { + rOStream << "Hash " << it->first << ":\n"; + it->second.PrintData(rOStream); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainerMap::save(Serializer& rSerializer) const +{ + rSerializer.save("PointResults", mPointResults); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpatialSearchResultContainerMap::load(Serializer& rSerializer) +{ + rSerializer.load("PointResults", mPointResults); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template class SpatialSearchResultContainerMap; + } // namespace Kratos \ No newline at end of file diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 21611f289a29..4d6d84eaf16d 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -22,6 +22,7 @@ #include "spatial_containers/spatial_search_result.h" #include "containers/pointer_vector_set.h" #include "includes/indexed_object.h" +#include "containers/array_1d.h" namespace Kratos { @@ -51,8 +52,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer /// Pointer definition of SpatialSearchResultContainer KRATOS_CLASS_POINTER_DEFINITION(SpatialSearchResultContainer); - /// Global pointer definition of TObjectType - using TPointerType = GlobalPointer; + /// The index type + using IndexType = std::size_t; ///@} ///@name Life Cycle @@ -60,9 +61,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer /** * @brief Constructor - * @param rDataCommunicator The data communicator */ - SpatialSearchResultContainer(const DataCommunicator& rDataCommunicator); + SpatialSearchResultContainer(); /// Destructor. virtual ~SpatialSearchResultContainer() = default; @@ -90,8 +90,9 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer /** * @brief Synchronize the container between partitions * @details This method synchronizes the container between partitions + * @param rDataCommunicator The data communicator */ - void SynchronizeAll(); + void SynchronizeAll(const DataCommunicator& rDataCommunicator); /** * @brief Applies a user-provided function to the global pointers and return a proxy to the results. @@ -106,9 +107,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer > Apply(TFunctorType&& UserFunctor) { // Check if the communicator has been created - if (mpGlobalPointerCommunicator == nullptr) { - SynchronizeAll(); - } + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; // Apply the user-provided function return mpGlobalPointerCommunicator->Apply(std::forward(UserFunctor)); @@ -132,7 +131,6 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@name Member Variables ///@{ - const DataCommunicator& mrDataCommunicator; /// The data communicator PointerVectorSet, @@ -141,7 +139,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer std::vector< typename TObjectType::Pointer > > mLocalPointers; /// Local pointers of the container GlobalPointersVector mGlobalPointers; /// Global pointers of the container - std::unordered_map mLocalDistances; /// The local distances + std::unordered_map mLocalDistances; /// The local distances typename GlobalPointerCommunicator::Pointer mpGlobalPointerCommunicator = nullptr; /// Global pointer to the communicator ///@} @@ -169,6 +167,162 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@} }; // Class SpatialSearchResultContainer +/** + * @class SpatialSearchResultContainerMap + * @brief Spatial search result container map + * @details This class is used to store the results of a spatial search, in a map to be identify results for a given coordinates + * @tparam TObjectType The type of the object + * @ingroup KratosCore + * @author Vicente Mataix Ferrandiz + */ +template +class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap +{ +public: + ///@name Type Definitions + ///@{ + + /// Pointer definition of SpatialSearchResultContainerMap + KRATOS_CLASS_POINTER_DEFINITION(SpatialSearchResultContainerMap); + + /// The hash type + using HashType = std::size_t; + + ///@} + ///@name Life Cycle + ///@{ + + /// Constructor. + SpatialSearchResultContainerMap() = default; + + /// Destructor. + virtual ~SpatialSearchResultContainerMap() = default; + + ///@} + ///@name Operators + ///@{ + + /** + * @brief Operator [] + * @param rCoordinates The coordinates + * @return The result container + */ + SpatialSearchResultContainer& operator[](const array_1d& rCoordinates) + { + return mPointResults[Hash(rCoordinates)]; + } + + /** + * @brief Operator [] + * @param rCoordinates The coordinates + * @return The result container + */ + const SpatialSearchResultContainer& operator[](const array_1d& rCoordinates) const + { + const HashType hash = Hash(rCoordinates); + const auto it = mPointResults.find(hash); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist." << std::endl; + return it->second; + } + + /** + * @brief Operator () + * @param rCoordinates The coordinates + * @return The result container + */ + SpatialSearchResultContainer& operator()(const array_1d& rCoordinates) + { + return mPointResults[Hash(rCoordinates)]; + } + + /** + * @brief Operator () + * @param rCoordinates The coordinates + * @return The result container + */ + const SpatialSearchResultContainer& operator()(const array_1d& rCoordinates) const + { + const HashType hash = Hash(rCoordinates); + const auto it = mPointResults.find(hash); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist." << std::endl; + return it->second; + } + + ///@} + ///@name Operations + ///@{ + + /** + * @brief Initialize the container + * @param rCoordinates The coordinates + */ + SpatialSearchResultContainer& InitializeResult(const array_1d& rCoordinates); + + /** + * @brief Check if coordinates are initialized + * @param rCoordinates The coordinates + * @return True if coordinates are initialized, false otherwise + */ + bool HasResult(const array_1d& rCoordinates) const; + + /** + * @brief Clear the containers + * @details This method clears the containers + */ + void Clear(); + + ///@} + ///@name Input and output + ///@{ + + /// Turn back information as a string. + virtual std::string Info() const; + + /// Print information about this object. + virtual void PrintInfo(std::ostream& rOStream) const; + + /// Print object's data. + virtual void PrintData(std::ostream& rOStream) const; + + ///@} +private: + ///@name Member Variables + ///@{ + + std::unordered_map> mPointResults; /// The results of each point + + ///@} + ///@name Private Operations + ///@{ + + /** + * @brief Hash the coordinates + * @param rCoordinates The coordinates + * @return The hash + */ + HashType Hash(const array_1d& rCoordinates) const; + + ///@} + ///@name Serialization + ///@{ + + friend class Serializer; + + /** + * @brief Save the object in a serializer + * @param rSerializer The serializer + */ + void save(Serializer& rSerializer) const; + + /** + * @brief Load the object in a serializer + * @param rSerializer The serializer + */ + void load(Serializer& rSerializer); + + ///@} +}; // Class SpatialSearchResultContainer + ///@} ///@} addtogroup block From 00e8287a33447c4913b165c2d7b5317167c36ca4 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 10:12:39 +0200 Subject: [PATCH 005/145] Adding test for spatial search result --- .../test_spatial_search_result.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp new file mode 100644 index 000000000000..1ed47d0e340c --- /dev/null +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp @@ -0,0 +1,64 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// +// + +// System includes + +// External includes + +// Project includes +#include "testing/testing.h" +#include "includes/element.h" +#include "spatial_containers/spatial_search_result_container.h" + +namespace Kratos::Testing +{ + +KRATOS_TEST_CASE_IN_SUITE(TestDefaultConstruction, KratosCoreFastSuite) +{ + auto result = SpatialSearchResult(); + KRATOS_CHECK_EQUAL(result.IsObjectFound(), false); + KRATOS_CHECK_EQUAL(result.IsDistanceCalculated(), false); + //KRATOS_CHECK_EQUAL(result.Get(), nullptr); // operator== does not work with GlobalPointer nullptr + KRATOS_CHECK_EQUAL(result.GetDistance(), 0.0); +} + +KRATOS_TEST_CASE_IN_SUITE(TestObjectFound, KratosCoreFastSuite) +{ + auto result = SpatialSearchResult(); + result.SetIsObjectFound(true); + KRATOS_CHECK_EQUAL(result.IsObjectFound(), true); +} + +KRATOS_TEST_CASE_IN_SUITE(TestDistanceCalculated, KratosCoreFastSuite) +{ + auto result = SpatialSearchResult(); + result.SetIsDistanceCalculated(true); + KRATOS_CHECK_EQUAL(result.IsDistanceCalculated(), true); +} + +KRATOS_TEST_CASE_IN_SUITE(TestPointer, KratosCoreFastSuite) +{ + Element element = Element(); + auto result = SpatialSearchResult(&element); + GeometricalObject* ptr = &element; + KRATOS_CHECK_EQUAL(result.Get().get(), ptr); +} + +KRATOS_TEST_CASE_IN_SUITE(TestDistance, KratosCoreFastSuite) +{ + auto result = SpatialSearchResult(); + result.SetDistance(3.14); + KRATOS_CHECK_EQUAL(result.GetDistance(), 3.14); +} + +} // namespace Kratos::Testing \ No newline at end of file From 7e4126bfb3c821e99048dd529784e7ea46ba5d75 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 10:57:24 +0200 Subject: [PATCH 006/145] Adding missing == operator --- kratos/includes/global_pointer.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/kratos/includes/global_pointer.h b/kratos/includes/global_pointer.h index 11a85f0f2877..8e27a8d194e2 100644 --- a/kratos/includes/global_pointer.h +++ b/kratos/includes/global_pointer.h @@ -196,6 +196,17 @@ class GlobalPointer { return mDataPointer; } + /** + * @brief Overloads the '==' operator to compare two GlobalPointer objects of the + * same template type. Returns true if the underlying pointers are equal. + * @param rOther The GlobalPointer object to be compared. + * @return true if the underlying pointers are equal, false otherwise. + */ + bool operator==(const GlobalPointer& rOther) + { + return this->get() == rOther.get(); + } + /** Fills buffer with the GlobalPoiter data * Fills buffer with the GlobalPoiter data * @param buffer Object data buffer @@ -228,8 +239,6 @@ class GlobalPointer { friend class Serializer; - - void save(Serializer& rSerializer) const { if(rSerializer.Is(Serializer::SHALLOW_GLOBAL_POINTERS_SERIALIZATION)) From eaa9db02d204f4e829b6f5649d22abd45ddf3507 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 13:54:23 +0200 Subject: [PATCH 007/145] Missing --- .../cpp_tests/spatial_containers/test_spatial_search_result.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp index 1ed47d0e340c..af9d601392bf 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp @@ -28,7 +28,7 @@ KRATOS_TEST_CASE_IN_SUITE(TestDefaultConstruction, KratosCoreFastSuite) auto result = SpatialSearchResult(); KRATOS_CHECK_EQUAL(result.IsObjectFound(), false); KRATOS_CHECK_EQUAL(result.IsDistanceCalculated(), false); - //KRATOS_CHECK_EQUAL(result.Get(), nullptr); // operator== does not work with GlobalPointer nullptr + KRATOS_CHECK_EQUAL(result.Get(), nullptr); KRATOS_CHECK_EQUAL(result.GetDistance(), 0.0); } From 56e1c6d1c442f9aeb91510fd1c528cf49fb5ebbb Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 14:38:31 +0200 Subject: [PATCH 008/145] Adding getting methods --- .../spatial_search_result_container.cpp | 91 +++++++++++++++++++ .../spatial_search_result_container.h | 19 ++++ 2 files changed, 110 insertions(+) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index f77d1688084c..9ec7ebd0f891 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -77,6 +77,97 @@ void SpatialSearchResultContainer::SynchronizeAll(const DataCommuni /***********************************************************************************/ /***********************************************************************************/ +template +std::vector SpatialSearchResultContainer::GetResultShapeFunctions(array_1d& rPoint) +{ + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; + + // Define the coordinates vector + const std::size_t number_of_gp = mGlobalPointers.size(); + std::vector shape_functions(number_of_gp); + + // Call Apply to get the proxy + auto proxy = this->Apply([&rPoint](GlobalPointer& rGP) -> Vector { + auto& r_geometry = rGP->GetGeometry(); + Vector N(r_geometry.size()); + array_1d local_coordinates; + r_geometry.PointLocalCoordinates(local_coordinates, rPoint); + r_geometry.ShapeFunctionsValues(N, local_coordinates); + return N; + }); + + // Get the shape functions + for(std::size_t i=0; i +std::vector SpatialSearchResultContainer::GetResultIndices() +{ + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; + + // Define the indices vector + const std::size_t number_of_gp = mGlobalPointers.size(); + std::vector indices(number_of_gp); + + // Call Apply to get the proxy + auto proxy = this->Apply([](GlobalPointer& rGP) -> std::size_t { + return rGP->Id(); + }); + + // Get the indices + for(std::size_t i=0; i +std::vector>> SpatialSearchResultContainer::GetResultCoordinates() +{ + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; + + // Define the coordinates vector + const std::size_t number_of_gp = mGlobalPointers.size(); + std::vector>> coordinates(number_of_gp); + + // Call Apply to get the proxy + auto proxy = this->Apply([](GlobalPointer& rGP) -> std::vector> { + auto& r_geometry = rGP->GetGeometry(); + std::vector> coordinates(r_geometry.size()); + for (unsigned int i = 0; i < r_geometry.size(); ++i) { + coordinates[i] = r_geometry[i].Coordinates(); + } + return coordinates; + }); + + // Get the coordinates + for(std::size_t i=0; i std::string SpatialSearchResultContainer::Info() const { diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 4d6d84eaf16d..3581da51477b 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -113,6 +113,25 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer return mpGlobalPointerCommunicator->Apply(std::forward(UserFunctor)); } + /** + * @brief Considers the global pointer communicator to get the shape functions of the resulting object + * @param rPoint The point coordinates + * @return A vector containing all the shape functions + */ + std::vector GetResultShapeFunctions(array_1d& rPoint); + + /** + * @brief Considers the global pointer communicator to get the indices of the resulting object + * @return A vector containing all the indices + */ + std::vector GetResultIndices(); + + /** + * @brief Considers the global pointer communicator to get the coordinates of the resulting object + * @return A vector containing all the coordinates + */ + std::vector>> GetResultCoordinates(); + ///@} ///@name Input and output ///@{ From 0a56c939c8c7b2ad4a29702f8f15edeb3b45286d Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 14:38:52 +0200 Subject: [PATCH 009/145] WIP test --- .../test_spatial_search_result_container.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp new file mode 100644 index 000000000000..5bdd5495e27d --- /dev/null +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -0,0 +1,27 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// +// + +// System includes + +// External includes + +// Project includes +#include "testing/testing.h" +#include "spatial_containers/spatial_search_result.h" + +namespace Kratos::Testing +{ + + + +} // namespace Kratos::Testing \ No newline at end of file From c176b7e8f15c3ffc28cfdd3f60cf3ef54abe68ca Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 14:38:31 +0200 Subject: [PATCH 010/145] Adding getting methods From a4d965b7dc86eab3ac3cab05f88ecfecce8ff438 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 14:38:52 +0200 Subject: [PATCH 011/145] WIP test From 5823e0fa543abaf734d2b4ccef51fd74e2a246c9 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 16:20:56 +0200 Subject: [PATCH 012/145] Adding accessing methods --- .../spatial_search_result_container.h | 66 ++++++++++++++++--- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 3581da51477b..12083356b460 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -55,6 +55,18 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer /// The index type using IndexType = std::size_t; + /// The local pointer vector type + using LocalPointerVector = PointerVectorSet, + std::equal_to, + typename TObjectType::Pointer, + std::vector< typename TObjectType::Pointer > + >; + + /// The global pointer communicator + using PointerCommunicatorPointer = typename GlobalPointerCommunicator::Pointer; + ///@} ///@name Life Cycle ///@{ @@ -132,6 +144,46 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer */ std::vector>> GetResultCoordinates(); + ///@} + ///@name Access + ///@{ + + /** + * @brief Accessor for mLocalPointers. + * This method returns a reference to the LocalPointerVector mLocalPointers. + * @return A reference to the LocalPointerVector mLocalPointers. + */ + LocalPointerVector& GetLocalPointers() { + return mLocalPointers; + } + + /** + * @brief Accessor for mGlobalPointers. + * This method returns a reference to the GlobalPointersVector mGlobalPointers. + * @return A reference to the GlobalPointersVector mGlobalPointers. + */ + GlobalPointersVector& GetGlobalPointers() { + return mGlobalPointers; + } + + /** + * @brief Accessor for mLocalDistances. + * This method returns a reference to the std::unordered_map mLocalDistances. + * @return A reference to the std::unordered_map mLocalDistances. + */ + std::unordered_map& GetLocalDistances() { + return mLocalDistances; + } + + /** + * @brief Accessor for mpGlobalPointerCommunicator. + * This method returns the PointerCommunicatorPointer mpGlobalPointerCommunicator. + * @return The PointerCommunicatorPointer mpGlobalPointerCommunicator. + */ + PointerCommunicatorPointer GetGlobalPointerCommunicator() { + return mpGlobalPointerCommunicator; + } + ///@} ///@name Input and output ///@{ @@ -150,16 +202,10 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@name Member Variables ///@{ - PointerVectorSet, - std::equal_to, - typename TObjectType::Pointer, - std::vector< typename TObjectType::Pointer > - > mLocalPointers; /// Local pointers of the container - GlobalPointersVector mGlobalPointers; /// Global pointers of the container - std::unordered_map mLocalDistances; /// The local distances - typename GlobalPointerCommunicator::Pointer mpGlobalPointerCommunicator = nullptr; /// Global pointer to the communicator + LocalPointerVector mLocalPointers; /// Local pointers of the container + GlobalPointersVector mGlobalPointers; /// Global pointers of the container + std::unordered_map mLocalDistances; /// The local distances + PointerCommunicatorPointer mpGlobalPointerCommunicator = nullptr; /// Global pointer to the communicator ///@} ///@name Private Operations From d268863cfc57fde788b423ceab3c6d6b470d5949 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 16:34:02 +0200 Subject: [PATCH 013/145] WIP test --- .../test_spatial_search_result_container.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 5bdd5495e27d..99890ba15171 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -18,10 +18,34 @@ // Project includes #include "testing/testing.h" #include "spatial_containers/spatial_search_result.h" +#include "spatial_containers/spatial_search_result_container.h" namespace Kratos::Testing { +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(); + object.SetId(1); + SpatialSearchResult result(&object); + result.SetDistance(0.5); + + // Add the result to the container + container.AddResult(result); + + // Check that the result was added correctly + auto& r_local_pointers = container.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + + auto& r_distances = container.GetLocalDistances(); + KRATOS_CHECK_EQUAL(r_distances.size(), 1); + KRATOS_CHECK_EQUAL(r_distances[1], 0.5); + // TODO: Memory error with destructor +} } // namespace Kratos::Testing \ No newline at end of file From f72970bd2e0e1e0bc68a77f96bd7020b1a74fb57 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 17:45:29 +0200 Subject: [PATCH 014/145] Clear distances --- kratos/spatial_containers/spatial_search_result_container.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index 9ec7ebd0f891..b65e51565633 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -57,6 +57,9 @@ void SpatialSearchResultContainer::Clear() // Clear local pointers mLocalPointers.clear(); + // Clear distances + mLocalDistances.clear(); + // Clear global pointers mGlobalPointers.clear(); } From eca2e1fad2b0e35d68668d50703326325990af9b Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 17:45:29 +0200 Subject: [PATCH 015/145] Clear distances From 147fe2ef7cd392798ff622693b6a7187c7018299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Mon, 29 May 2023 18:33:25 +0200 Subject: [PATCH 016/145] Fix memmory leak --- kratos/spatial_containers/spatial_search_result_container.cpp | 2 +- kratos/spatial_containers/spatial_search_result_container.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index b65e51565633..683513548085 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -38,7 +38,7 @@ void SpatialSearchResultContainer::AddResult(SpatialSearchResult(p_local_result)); + mLocalPointers.push_back(p_local_result); // Add distances const IndexType id = p_local_result->Id(); diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 12083356b460..79d75fbc958f 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -60,8 +60,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer IndexedObject, std::less, std::equal_to, - typename TObjectType::Pointer, - std::vector< typename TObjectType::Pointer > + TObjectType*, + std::vector >; /// The global pointer communicator From 0f983c913c3f16508d5a05d294db9c0e8590ac9c Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 18:30:34 +0200 Subject: [PATCH 017/145] Clear TODO --- .../spatial_containers/test_spatial_search_result_container.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 99890ba15171..83357acaf547 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -44,8 +44,6 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS auto& r_distances = container.GetLocalDistances(); KRATOS_CHECK_EQUAL(r_distances.size(), 1); KRATOS_CHECK_EQUAL(r_distances[1], 0.5); - - // TODO: Memory error with destructor } } // namespace Kratos::Testing \ No newline at end of file From af73855dcb2db66d28cbf8342ba47583d25736b2 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 18:38:44 +0200 Subject: [PATCH 018/145] Adding Clear method --- .../test_spatial_search_result_container.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 83357acaf547..6c36dc68339d 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -46,4 +46,26 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS KRATOS_CHECK_EQUAL(r_distances[1], 0.5); } +KRATOS_TEST_CASE_IN_SUITE(TestSpatialSearchResultContainerClear, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(); + object.SetId(1); + SpatialSearchResult result(&object); + result.SetDistance(0.5); + + // Add the result to the container + container.AddResult(result); + + // Clear + container.Clear(); + + // Check that the result was added correctly + auto& r_local_pointers = container.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); +} + } // namespace Kratos::Testing \ No newline at end of file From d1cca7950a7cac4663ba354561e5d06605faf0a6 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 18:38:44 +0200 Subject: [PATCH 019/145] Adding Clear method From 414d52320d0d36b658c777023724ee6220b87717 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 18:44:38 +0200 Subject: [PATCH 020/145] Adding test for SynchronizeAll --- .../test_spatial_search_result_container.cpp | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 6c36dc68339d..bb9b50a0e90d 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -41,12 +41,17 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS auto& r_local_pointers = container.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + // Check distances auto& r_distances = container.GetLocalDistances(); KRATOS_CHECK_EQUAL(r_distances.size(), 1); KRATOS_CHECK_EQUAL(r_distances[1], 0.5); + + // Check global pointers + auto& r_global_pointers = container.GetGlobalPointers(); + KRATOS_CHECK_EQUAL(r_global_pointers.size(), 0); // It should be empty as we have not synchronized } -KRATOS_TEST_CASE_IN_SUITE(TestSpatialSearchResultContainerClear, KratosCoreFastSuite) +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerClear, KratosCoreFastSuite) { // Create a test object SpatialSearchResultContainer container; @@ -68,4 +73,30 @@ KRATOS_TEST_CASE_IN_SUITE(TestSpatialSearchResultContainerClear, KratosCoreFastS KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); } +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(); + object.SetId(1); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + DataCommunicator data_communicator; + container.SynchronizeAll(data_communicator); + + // Check that the result was added correctly + auto& r_local_pointers = container.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + + // Check global pointers + auto& r_global_pointers = container.GetGlobalPointers(); + KRATOS_CHECK_EQUAL(r_global_pointers.size(), 1); +} + } // namespace Kratos::Testing \ No newline at end of file From 3573b0b3a58a58c576da90317ec3ec1ee8f3d238 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 29 May 2023 19:45:03 +0200 Subject: [PATCH 021/145] Adding test SpatialSearchResultContainerGetResultShapeFunctions --- .../spatial_search_result_container.cpp | 2 + .../test_spatial_search_result_container.cpp | 41 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index 683513548085..7e59e7cc2c94 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -226,6 +226,7 @@ void SpatialSearchResultContainer::load(Serializer& rSerializer) /***********************************************************************************/ /***********************************************************************************/ +/// Template instantiation template class SpatialSearchResultContainer; /***********************************************************************************/ @@ -329,6 +330,7 @@ void SpatialSearchResultContainerMap::load(Serializer& rSerializer) /***********************************************************************************/ /***********************************************************************************/ +/// Template instantiation template class SpatialSearchResultContainerMap; } // namespace Kratos \ No newline at end of file diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index bb9b50a0e90d..a1d099716f5c 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -17,6 +17,7 @@ // Project includes #include "testing/testing.h" +#include "geometries/line_2d_2.h" #include "spatial_containers/spatial_search_result.h" #include "spatial_containers/spatial_search_result_container.h" @@ -29,8 +30,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS SpatialSearchResultContainer container; // Create a test result - GeometricalObject object = GeometricalObject(); - object.SetId(1); + GeometricalObject object = GeometricalObject(1); SpatialSearchResult result(&object); result.SetDistance(0.5); @@ -57,8 +57,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerClear, KratosCoreFastSuite SpatialSearchResultContainer container; // Create a test result - GeometricalObject object = GeometricalObject(); - object.SetId(1); + GeometricalObject object = GeometricalObject(1); SpatialSearchResult result(&object); result.SetDistance(0.5); @@ -79,8 +78,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCore SpatialSearchResultContainer container; // Create a test result - GeometricalObject object = GeometricalObject(); - object.SetId(1); + GeometricalObject object = GeometricalObject(1); SpatialSearchResult result(&object); // Add the result to the container @@ -99,4 +97,35 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCore KRATOS_CHECK_EQUAL(r_global_pointers.size(), 1); } +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultShapeFunctions, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainer container; + + // Generate a geometry + auto p_node1 = Kratos::make_intrusive(1, 0.0, 0.0, 0.0); + auto p_node2 = Kratos::make_intrusive(2, 1.0, 0.0, 0.0); + Geometry::Pointer p_geom = Kratos::make_shared>(p_node1, p_node2); + + // Create a test result + GeometricalObject object = GeometricalObject(1, p_geom); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + DataCommunicator data_communicator; + container.SynchronizeAll(data_communicator); + + // Compute shape functions + Point point = Point(0.5, 0.0, 0.0); + auto shape_functions = container.GetResultShapeFunctions(point); + + // Check shape functions + KRATOS_CHECK_EQUAL(shape_functions.size(), 1); + KRATOS_CHECK_NEAR(shape_functions[0][0], 0.5, 1.0e-12); + KRATOS_CHECK_NEAR(shape_functions[0][1], 0.5, 1.0e-12); +} + } // namespace Kratos::Testing \ No newline at end of file From 9bc81791813b25b33bdfefba8f249e9a9aa446d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 11:47:12 +0200 Subject: [PATCH 022/145] Extend tests --- .../test_spatial_search_result_container.cpp | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index a1d099716f5c..5f00da206da3 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -128,4 +128,59 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultShapeFunctions, K KRATOS_CHECK_NEAR(shape_functions[0][1], 0.5, 1.0e-12); } +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultIndices, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(1); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + DataCommunicator data_communicator; + container.SynchronizeAll(data_communicator); + + // Compute shape functions + auto indixes = container.GetResultIndices(); + + // Check shape functions + KRATOS_CHECK_EQUAL(indixes.size(), 1); + KRATOS_CHECK_EQUAL(indixes[0], object.Id()); +} + +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainer container; + + // Generate a geometry + auto p_node1 = Kratos::make_intrusive(1, 0.0, 0.0, 0.0); + auto p_node2 = Kratos::make_intrusive(2, 1.0, 0.0, 0.0); + Geometry::Pointer p_geom = Kratos::make_shared>(p_node1, p_node2); + + // Create a test result + GeometricalObject object = GeometricalObject(1, p_geom); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + DataCommunicator data_communicator; + container.SynchronizeAll(data_communicator); + + // Compute shape functions + auto coordinates = container.GetResultCoordinates(); + + // Check shape functions + KRATOS_CHECK_EQUAL(coordinates.size(), 1); + KRATOS_CHECK_EQUAL(coordinates[0].size(), 2); + KRATOS_CHECK_VECTOR_NEAR(coordinates[0][2], p_node1->Coordinates(), 1.0e-12); + KRATOS_CHECK_VECTOR_NEAR(coordinates[0][1], p_node2->Coordinates(), 1.0e-12); +} + } // namespace Kratos::Testing \ No newline at end of file From 4dea51a6ec3293104c419bf6dd6dfa0386cea2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 12:20:51 +0200 Subject: [PATCH 023/145] Extend tests --- .../test_spatial_search_result_container.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 5f00da206da3..446a660272dc 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -183,4 +183,49 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, Krat KRATOS_CHECK_VECTOR_NEAR(coordinates[0][1], p_node2->Coordinates(), 1.0e-12); } +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapInitializeResult, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainerMap container_map; + + // Initialize result + Point point = Point(0.5, 0.0, 0.0); + container_map.InitializeResult(point); + + // Check that the result was added correctly + KRATOS_CHECK(container_map.HasResult(point)); + Point fake_point = Point(1.5, 0.0, 0.0); + KRATOS_CHECK_IS_FALSE(container_map.HasResult(fake_point)); +} + +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapClear, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainerMap container_map; + + // Initialize result + Point point = Point(0.5, 0.0, 0.0); + container_map.InitializeResult(point); + + // Check that the result was added correctly + KRATOS_CHECK(container_map.HasResult(point)); + container_map.Clear(); + KRATOS_CHECK_IS_FALSE(container_map.HasResult(point)); +} + +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapOperators, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainerMap container_map; + + // Initialize result + Point point = Point(0.5, 0.0, 0.0); + container_map.InitializeResult(point); + + // Check that the result was added correctly + auto& r_result = container_map[point]; + auto& r_local_pointers = r_result.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); +} + } // namespace Kratos::Testing \ No newline at end of file From f124196f6bd20c37339120f265d08b8a1fb29c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 13:28:44 +0200 Subject: [PATCH 024/145] Adding MPI tests --- .../test_spatial_search_result_container.cpp | 251 ++++++++++++++++++ .../test_spatial_search_result_container.cpp | 2 +- 2 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp diff --git a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp new file mode 100644 index 000000000000..9eb02c1ac3da --- /dev/null +++ b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -0,0 +1,251 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// +// + +// System includes + +// External includes + +// Project includes +#include "testing/testing.h" +#include "geometries/line_2d_2.h" +#include "spatial_containers/spatial_search_result.h" +#include "spatial_containers/spatial_search_result_container.h" + +namespace Kratos::Testing +{ + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerAddResult, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(r_data_comm.Rank() + 1); + SpatialSearchResult result(&object); + result.SetDistance(0.5); + + // Add the result to the container + container.AddResult(result); + + // Check that the result was added correctly + auto& r_local_pointers = container.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + + // Check distances + auto& r_distances = container.GetLocalDistances(); + KRATOS_CHECK_EQUAL(r_distances.size(), 1); + KRATOS_CHECK_EQUAL(r_distances[r_data_comm.Rank() + 1], 0.5); + + // Check global pointers + auto& r_global_pointers = container.GetGlobalPointers(); + KRATOS_CHECK_EQUAL(r_global_pointers.size(), 0); // It should be empty as we have not synchronized +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerClear, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(r_data_comm.Rank() + 1); + SpatialSearchResult result(&object); + result.SetDistance(0.5); + + // Add the result to the container + container.AddResult(result); + + // Clear + container.Clear(); + + // Check that the result was added correctly + auto& r_local_pointers = container.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerSynchronizeAll, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(r_data_comm.Rank() + 1); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + container.SynchronizeAll(r_data_comm); + + // Check that the result was added correctly + auto& r_local_pointers = container.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + + // Check global pointers + auto& r_global_pointers = container.GetGlobalPointers(); + KRATOS_CHECK_EQUAL(r_global_pointers.size(), r_data_comm.Size()); +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultShapeFunctions, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // Create a test object + SpatialSearchResultContainer container; + + // Generate a geometry + auto p_node1 = Kratos::make_intrusive(1, 0.0, 0.0, 0.0); + auto p_node2 = Kratos::make_intrusive(2, 1.0, 0.0, 0.0); + Geometry::Pointer p_geom = Kratos::make_shared>(p_node1, p_node2); + + // Create a test result + GeometricalObject object = GeometricalObject(r_data_comm.Rank() + 1, p_geom); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + container.SynchronizeAll(r_data_comm); + + // Compute shape functions + Point point = Point(0.5, 0.0, 0.0); + auto shape_functions = container.GetResultShapeFunctions(point); + + // Check shape functions + KRATOS_CHECK_EQUAL(shape_functions.size(), r_data_comm.Size()); + for (unsigned int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { + KRATOS_CHECK_NEAR(shape_functions[i_rank][0], 0.5, 1.0e-12); + KRATOS_CHECK_NEAR(shape_functions[i_rank][1], 0.5, 1.0e-12); + } +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultIndices, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // Create a test object + SpatialSearchResultContainer container; + + // Create a test result + GeometricalObject object = GeometricalObject(r_data_comm.Rank() + 1); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + container.SynchronizeAll(r_data_comm); + + // Compute shape functions + auto indixes = container.GetResultIndices(); + + // Check shape functions + KRATOS_CHECK_EQUAL(indixes.size(), r_data_comm.Size()); + for (unsigned int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { + KRATOS_CHECK_EQUAL(indixes[i_rank], i_rank + 1); + } +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultCoordinates, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // Create a test object + SpatialSearchResultContainer container; + + // Generate a geometry + auto p_node1 = Kratos::make_intrusive(1, 0.0, 0.0, 0.0); + auto p_node2 = Kratos::make_intrusive(2, 1.0, 0.0, 0.0); + Geometry::Pointer p_geom = Kratos::make_shared>(p_node1, p_node2); + + // Create a test result + GeometricalObject object = GeometricalObject(r_data_comm.Rank() + 1, p_geom); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + container.SynchronizeAll(r_data_comm); + + // Compute shape functions + auto coordinates = container.GetResultCoordinates(); + + // Check shape functions + KRATOS_CHECK_EQUAL(coordinates.size(), r_data_comm.Size()); + for (unsigned int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { + KRATOS_CHECK_EQUAL(coordinates[i_rank].size(), 2); + KRATOS_CHECK_VECTOR_NEAR(coordinates[i_rank][0], p_node1->Coordinates(), 1.0e-12); + KRATOS_CHECK_VECTOR_NEAR(coordinates[i_rank][1], p_node2->Coordinates(), 1.0e-12); + } +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapInitializeResult, KratosMPICoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainerMap container_map; + + // Initialize result + Point point = Point(0.5, 0.0, 0.0); + container_map.InitializeResult(point); + + // Check that the result was added correctly + KRATOS_CHECK(container_map.HasResult(point)); + Point fake_point = Point(1.5, 0.0, 0.0); + KRATOS_CHECK_IS_FALSE(container_map.HasResult(fake_point)); +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapClear, KratosMPICoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainerMap container_map; + + // Initialize result + Point point = Point(0.5, 0.0, 0.0); + container_map.InitializeResult(point); + + // Check that the result was added correctly + KRATOS_CHECK(container_map.HasResult(point)); + container_map.Clear(); + KRATOS_CHECK_IS_FALSE(container_map.HasResult(point)); +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapOperators, KratosMPICoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainerMap container_map; + + // Initialize result + Point point = Point(0.5, 0.0, 0.0); + container_map.InitializeResult(point); + + // Check that the result was added correctly + auto& r_result = container_map[point]; + auto& r_local_pointers = r_result.GetLocalPointers(); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); +} + +} // namespace Kratos::Testing \ No newline at end of file diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 446a660272dc..01422fdd305c 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -179,7 +179,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, Krat // Check shape functions KRATOS_CHECK_EQUAL(coordinates.size(), 1); KRATOS_CHECK_EQUAL(coordinates[0].size(), 2); - KRATOS_CHECK_VECTOR_NEAR(coordinates[0][2], p_node1->Coordinates(), 1.0e-12); + KRATOS_CHECK_VECTOR_NEAR(coordinates[0][0], p_node1->Coordinates(), 1.0e-12); KRATOS_CHECK_VECTOR_NEAR(coordinates[0][1], p_node2->Coordinates(), 1.0e-12); } From 6604b835973d8fe5df59b5901d2d432e77736f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 14:07:19 +0200 Subject: [PATCH 025/145] Expose to python --- .../add_search_strategies_to_python.cpp | 56 +++++++++++++++++++ .../spatial_search_result_container.cpp | 2 +- .../spatial_search_result_container.h | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 376caca8cff9..76516bb1eb26 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -24,6 +24,7 @@ #include "spatial_containers/specialized_spatial_search_factory.h" #include "spatial_containers/geometrical_objects_bins.h" #include "spatial_containers/spatial_search_result.h" +#include "spatial_containers/spatial_search_result_container.h" namespace Kratos::Python { @@ -707,6 +708,61 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("IsDistanceCalculated", &ResultType::IsDistanceCalculated) ; + using ResultTypeContainer = SpatialSearchResultContainer; + + py::class_(m, "ResultTypeContainer") + .def(py::init< >()) + .def("AddResult", &ResultTypeContainer::AddResult) + .def("Clear", &ResultTypeContainer::Clear) + .def("SynchronizeAll", &ResultTypeContainer::SynchronizeAll) + .def("GetResultShapeFunctions", [&](ResultTypeContainer& self, const array_1d& rPoint) { + auto results = self.GetResultShapeFunctions(rPoint); + + // Copy the results to the python list + py::list list_results; + for (auto& r_result : results) { + list_results.append(r_result); + } + return list_results; + }) + .def("GetResultIndices", [&](ResultTypeContainer& self) { + std::vector results = self.GetResultIndices(); + + // Copy the results to the python list + py::list list_results; + for (auto& r_result : results) { + list_results.append(r_result); + } + return list_results; + }) + .def("GetResultCoordinates", [&](ResultTypeContainer& self) { + auto results = self.GetResultCoordinates(); + + // Copy the results to the python list + py::list list_results; + for (auto& r_result : results) { + py::list i_list_results; + for (auto& r_sub_result : r_result) { + i_list_results.append(r_sub_result); + } + list_results.append(i_list_results); + } + return list_results; + }) + ; + + using ResultTypeContainerMap = SpatialSearchResultContainerMap; + + py::class_(m, "ResultTypeContainerMap") + .def(py::init< >()) + .def("InitializeResult", &ResultTypeContainerMap::InitializeResult) + .def("HasResult", &ResultTypeContainerMap::HasResult) + .def("Clear", &ResultTypeContainerMap::Clear) + .def("__getitem__", [&](ResultTypeContainerMap& self, const array_1d& rCoordinates) { + return self[rCoordinates]; + }) + ; + using NodesContainerType = ModelPart::NodesContainerType; using ElementsContainerType = ModelPart::ElementsContainerType; using ConditionsContainerType = ModelPart::ConditionsContainerType; diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index 7e59e7cc2c94..b9901bbfe232 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -81,7 +81,7 @@ void SpatialSearchResultContainer::SynchronizeAll(const DataCommuni /***********************************************************************************/ template -std::vector SpatialSearchResultContainer::GetResultShapeFunctions(array_1d& rPoint) +std::vector SpatialSearchResultContainer::GetResultShapeFunctions(const array_1d& rPoint) { // Check if the communicator has been created KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 79d75fbc958f..716d503249a5 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -130,7 +130,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer * @param rPoint The point coordinates * @return A vector containing all the shape functions */ - std::vector GetResultShapeFunctions(array_1d& rPoint); + std::vector GetResultShapeFunctions(const array_1d& rPoint); /** * @brief Considers the global pointer communicator to get the indices of the resulting object From 3b6956b60dabd1ddf9768ecc05962bd7d204f4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 15:13:33 +0200 Subject: [PATCH 026/145] Extend tests and python interface --- .../test_spatial_search_result_container.cpp | 4 ++ .../add_search_strategies_to_python.cpp | 9 +++ .../spatial_search_result_container.h | 64 +++++++++++++++++++ .../test_spatial_search_result_container.cpp | 6 ++ 4 files changed, 83 insertions(+) diff --git a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 9eb02c1ac3da..bc1ed677b365 100644 --- a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -52,6 +52,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerAddResult, // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); KRATOS_CHECK_EQUAL(r_global_pointers.size(), 0); // It should be empty as we have not synchronized + KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); // It should be empty as we have not synchronized } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerClear, KratosMPICoreFastSuite) @@ -76,6 +77,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerClear, Krat // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerSynchronizeAll, KratosMPICoreFastSuite) @@ -103,6 +105,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerSynchronize // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); KRATOS_CHECK_EQUAL(r_global_pointers.size(), r_data_comm.Size()); + KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultShapeFunctions, KratosMPICoreFastSuite) @@ -246,6 +249,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapOperator auto& r_result = container_map[point]; auto& r_local_pointers = r_result.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), r_result.NumberOfLocalResults()); } } // namespace Kratos::Testing \ No newline at end of file diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 76516bb1eb26..d40bf31d0f47 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -749,6 +749,12 @@ void AddSearchStrategiesToPython(pybind11::module& m) } return list_results; }) + .def("__getitem__", [&](ResultTypeContainer& self, const std::size_t Index) { + return self[Index]; + }) + .def("__call__", [&](ResultTypeContainer& self, const std::size_t Index) { + return self(Index); + }) ; using ResultTypeContainerMap = SpatialSearchResultContainerMap; @@ -761,6 +767,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("__getitem__", [&](ResultTypeContainerMap& self, const array_1d& rCoordinates) { return self[rCoordinates]; }) + .def("__call__", [&](ResultTypeContainerMap& self, const array_1d& rCoordinates) { + return self(rCoordinates); + }) ; using NodesContainerType = ModelPart::NodesContainerType; diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 716d503249a5..0e76a1a47b6c 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -83,10 +83,74 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@name Operators ///@{ + /** + * @brief Operator [] + * @param Index The index + * @return The result container + */ + TObjectType& operator[](const std::size_t Index) + { + return mLocalPointers[Index]; + } + + /** + * @brief Operator [] const version + * @param Index The index + * @return The result container + */ + const TObjectType& operator[](const std::size_t Index) const + { + const auto it = mLocalPointers.find(Index); + KRATOS_ERROR_IF(it == mLocalPointers.end()) << "The result does not exist." << std::endl; + return *it; + } + + /** + * @brief Operator () + * @param Index The index + * @return The result container + */ + TObjectType& operator()(const std::size_t Index) + { + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created. Therefore is not synchronized" << std::endl; + return mGlobalPointers[Index]; + } + + /** + * @brief Operator () const version + * @param Index The index + * @return The result container + */ + const TObjectType& operator()(const std::size_t Index) const + { + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created. Therefore is not synchronized" << std::endl; + return mGlobalPointers[Index]; + } + ///@} ///@name Operations ///@{ + /** + * @brief Returns the local pointers size + * @return The local pointers size + */ + std::size_t NumberOfLocalResults() const + { + return mLocalPointers.size(); + } + + /** + * @brief Returns the global pointers size + * @return The global pointers size + */ + std::size_t NumberOfGlobalResults() const + { + return mGlobalPointers.size(); + } + /** * @brief Add a result to the container * @param rResult The result diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 01422fdd305c..f367b5e45d35 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -40,6 +40,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); // Check distances auto& r_distances = container.GetLocalDistances(); @@ -49,6 +50,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); KRATOS_CHECK_EQUAL(r_global_pointers.size(), 0); // It should be empty as we have not synchronized + KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); // It should be empty as we have not synchronized } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerClear, KratosCoreFastSuite) @@ -70,6 +72,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerClear, KratosCoreFastSuite // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCoreFastSuite) @@ -91,10 +94,12 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCore // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); KRATOS_CHECK_EQUAL(r_global_pointers.size(), 1); + KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultShapeFunctions, KratosCoreFastSuite) @@ -226,6 +231,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapOperators, KratosCoreFa auto& r_result = container_map[point]; auto& r_local_pointers = r_result.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), r_result.NumberOfLocalResults()); } } // namespace Kratos::Testing \ No newline at end of file From dff6ab97d8043f9dad9910f68ad724d4fa1c29d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 19:54:29 +0200 Subject: [PATCH 027/145] Adapting to new structure --- .../test_spatial_search_result_container.cpp | 2 + .../add_search_strategies_to_python.cpp | 60 +++------ .../geometrical_objects_bins.cpp | 16 +++ .../geometrical_objects_bins.h | 118 ++++++++++++++++++ .../spatial_search_result_container.h | 9 ++ 5 files changed, 161 insertions(+), 44 deletions(-) diff --git a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index bc1ed677b365..e619931cd517 100644 --- a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -43,6 +43,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerAddResult, // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); // Check distances auto& r_distances = container.GetLocalDistances(); @@ -101,6 +102,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerSynchronize // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); + KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index d40bf31d0f47..a9937199cef4 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -712,6 +712,8 @@ void AddSearchStrategiesToPython(pybind11::module& m) py::class_(m, "ResultTypeContainer") .def(py::init< >()) + .def("NumberOfLocalResults", &ResultTypeContainer::NumberOfLocalResults) + .def("NumberOfGlobalResults", &ResultTypeContainer::NumberOfGlobalResults) .def("AddResult", &ResultTypeContainer::AddResult) .def("Clear", &ResultTypeContainer::Clear) .def("SynchronizeAll", &ResultTypeContainer::SynchronizeAll) @@ -761,6 +763,7 @@ void AddSearchStrategiesToPython(pybind11::module& m) py::class_(m, "ResultTypeContainerMap") .def(py::init< >()) + .def("NumberOfPointsResults", &ResultTypeContainerMap::NumberOfPointsResults) .def("InitializeResult", &ResultTypeContainerMap::InitializeResult) .def("HasResult", &ResultTypeContainerMap::HasResult) .def("Clear", &ResultTypeContainerMap::Clear) @@ -785,31 +788,15 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("GetTotalNumberOfCells", &GeometricalObjectsBins::GetTotalNumberOfCells) .def("SearchInRadius", [&](GeometricalObjectsBins& self, const Point& rPoint, const double Radius) { // Perform the search - std::vector results; + ResultTypeContainer results; self.SearchInRadius(rPoint, Radius, results); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; + return results; }) .def("SearchInRadius", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes, const double Radius) { // Perform the search - std::vector> results; + ResultTypeContainerMap results; self.SearchInRadius(rNodes.begin(), rNodes.end(), Radius, results); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - py::list sub_list_results; - for (auto& r_sub_result : r_result) { - sub_list_results.append(r_sub_result); - } - list_results.append(sub_list_results); - } - return list_results; + return results; }) .def("SearchNearestInRadius", [&](GeometricalObjectsBins& self, const Point& rPoint, const double Radius) { // Perform the search @@ -817,14 +804,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) }) .def("SearchNearestInRadius", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes, const double Radius) { // Perform the search - std::vector results = self.SearchNearestInRadius(rNodes.begin(), rNodes.end(), Radius); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; + ResultTypeContainerMap results; + self.SearchNearestInRadius(rNodes.begin(), rNodes.end(), Radius, results); + return results; }) .def("SearchNearest", [&](GeometricalObjectsBins& self, const Point& rPoint) { // Perform the search @@ -832,14 +814,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) }) .def("SearchNearest", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes) { // Perform the search - std::vector results = self.SearchNearest(rNodes.begin(), rNodes.end()); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; + ResultTypeContainerMap results; + self.SearchNearest(rNodes.begin(), rNodes.end(), results); + return results; }) .def("SearchIsInside", [&](GeometricalObjectsBins& self, const Point& rPoint) { // Perform the search @@ -847,14 +824,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) }) .def("SearchIsInside", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes) { // Perform the search - std::vector results = self.SearchIsInside(rNodes.begin(), rNodes.end()); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; + ResultTypeContainerMap results; + self.SearchIsInside(rNodes.begin(), rNodes.end(), results); + return results; }) ; } diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index f68c707d7b63..d194c7f49fd0 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -98,6 +98,22 @@ void GeometricalObjectsBins::SearchInRadius( /***********************************************************************************/ /***********************************************************************************/ +void GeometricalObjectsBins::SearchInRadius( + const Point& rPoint, + const double Radius, + ResultTypeContainer& rResults + ) +{ + std::vector results; + SearchInRadius(rPoint, Radius, results); + for (auto& r_result : results) { + rResults.AddResult(r_result); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchNearestInRadius( const Point& rPoint, const double Radius diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index dec3166c1037..cc5158cc296f 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -21,6 +21,7 @@ #include "geometries/bounding_box.h" #include "geometries/point.h" #include "spatial_containers/spatial_search_result.h" +#include "spatial_containers/spatial_search_result_container.h" namespace Kratos { @@ -54,6 +55,8 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins /// The type of geometrical object to be stored in the bins using CellType = std::vector; using ResultType = SpatialSearchResult; + using ResultTypeContainer = SpatialSearchResultContainer; + using ResultTypeContainerMap = SpatialSearchResultContainerMap; ///@} ///@name Life Cycle @@ -146,6 +149,19 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins std::vector& rResults ); + /** + * @brief This method takes a point and finds all of the objects in the given radius to it. + * @details The result contains the object and also its distance to the point. + * @param rPoint The point to be checked + * @param Radius The radius to be checked + * @param rResults The results of the search + */ + void SearchInRadius( + const Point& rPoint, + const double Radius, + ResultTypeContainer& rResults + ); + /** * @brief This method takes a point and finds all of the objects in the given radius to it (iterative version). * @details The result contains the object and also its distance to the point. @@ -170,6 +186,30 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins } } + /** + * @brief This method takes a point and finds all of the objects in the given radius to it (iterative version). + * @details The result contains the object and also its distance to the point. + * @param itPointBegin The first point iterator + * @param itPointEnd The last point iterator + * @param Radius The radius to be checked + * @param rResults The results of the search + * @tparam TPointIteratorType The type of the point iterator + */ + template + void SearchInRadius( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const double Radius, + ResultTypeContainerMap& rResults + ) + { + for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ + auto& r_partial_result = rResults.InitializeResult(it_point->Coordinates()); + SearchInRadius(*it_point, Radius, r_partial_result); + } + } + + /** * @brief This method takes a point and finds the nearest object to it in a given radius. * @details If there are more than one object in the same minimum distance only one is returned @@ -212,6 +252,34 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins return results; } + /** + * @brief This method takes a point and finds the nearest object to it in a given radius (iterative version). + * @details If there are more than one object in the same minimum distance only one is returned + * If there are no objects in that radius the result will be set to not found. + * Result contains a flag is the object has been found or not. + * @param itPointBegin The first point iterator + * @param itPointEnd The last point iterator + * @param Radius The radius to be checked + * @param rResults The result of the search + * @tparam TPointIteratorType The type of the point iterator + */ + template + void SearchNearestInRadius( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const double Radius, + ResultTypeContainerMap& rResults + ) + { + // Adding the results to the container + for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ + auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); + auto result = SearchNearestInRadius(*it_point, Radius); + r_point_result.AddResult(result); + } + } + + /** * @brief This method takes a point and finds the nearest object to it. * @details If there are more than one object in the same minimum distance only one is returned @@ -246,6 +314,30 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins return results; } + /** + * @brief This method takes a point and finds the nearest object to it (iterative version). + * @details If there are more than one object in the same minimum distance only one is returned + * Result contains a flag is the object has been found or not. + * @param itPointBegin The first point iterator + * @param itPointEnd The last point iterator + * @param rResults The result of the search + * @tparam TPointIteratorType The type of the point iterator + */ + template + void SearchNearest( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + ResultTypeContainerMap& rResults + ) + { + // Adding the results to the container + for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ + auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); + auto result = SearchNearest(*it_point); + r_point_result.AddResult(result); + } + } + /** * @brief This method takes a point and search if it's inside an geometrical object of the domain. * @details If it is inside an object, it returns it, and search distance is set to zero. @@ -284,6 +376,32 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins return results; } + /** + * @brief This method takes a point and search if it's inside an geometrical object of the domain (iterative version). + * @details If it is inside an object, it returns it, and search distance is set to zero. + * If there is no object, the result will be set to not found. + * Result contains a flag is the object has been found or not. + * This method is a simplified and faster method of SearchNearest. + * @param itPointBegin The first point iterator + * @param itPointEnd The last point iterator + * @param rResults The result of the search + * @tparam TPointIteratorType The type of the point iterator + */ + template + void SearchIsInside( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + ResultTypeContainerMap& rResults + ) + { + // Adding the results to the container + for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ + auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); + auto result = SearchIsInside(*it_point); + r_point_result.AddResult(result); + } + } + ///@} ///@name Access ///@{ diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 0e76a1a47b6c..79f2f4c9c23e 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -381,6 +381,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap ///@name Operations ///@{ + /** + * @brief Returns the number of points results + * @return The number of points results + */ + std::size_t NumberOfPointsResults() const + { + return mPointResults.size(); + } + /** * @brief Initialize the container * @param rCoordinates The coordinates From 2498e1df206ef4bb66e7a1cd4ddea2d0172dd109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 19:55:45 +0200 Subject: [PATCH 028/145] Update test (WIP) --- kratos/tests/test_geometrical_object_bins.py | 95 ++++++++++---------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index 5a185917edd2..717ba0b9c410 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -42,17 +42,15 @@ def tearDownClass(cls): def setUp(self): # Create search - + self.data_comm = self.model_part.GetCommunicator().GetDataCommunicator() if KM.IsDistributedRun(): raise Exception("MPI version comming in a future PR") - self.search = KM.GeometricalObjectsBinsMPI(self.model_part.Conditions, self.model_part.GetCommunicator().GetDataCommunicator()) else: self.search = KM.GeometricalObjectsBins(self.model_part.Conditions) # Create node for search if KM.IsDistributedRun(): import KratosMultiphysics.mpi as KratosMPI - self.data_comm = self.mpi_model_part.GetCommunicator().GetDataCommunicator() # Only added to first rank to actualy check it works in all ranks if self.data_comm.Rank() == 0: self.node = self.sub_model_part.CreateNewNode(100000, 0.0, 0.0, 0.15) @@ -62,15 +60,6 @@ def setUp(self): else: self.node = self.sub_model_part.CreateNewNode(100000, 0.0, 0.0, 0.15) - def is_first_partition(self): - if KM.IsDistributedRun(): - if self.data_comm.Rank() == 0: - return True - else: - return False - else: - return True - def test_GeometricalObjectsBins_SearchInRadius(self): # Define radius radius = 0.35 @@ -79,57 +68,67 @@ def test_GeometricalObjectsBins_SearchInRadius(self): cond_id_ref = [125,78,117,18,68,1,41,119] # One node search - if not KM.IsDistributedRun(): - results = self.search.SearchInRadius(self.node, radius) - self.assertEqual(len(results), 8) - for result in results: - self.assertTrue(result.Get().Id in cond_id_ref) + results = self.search.SearchInRadius(self.node, radius) + results.SynchronizeAll(self.data_comm) + self.assertEqual(results.NumberOfGlobalResults(), 8) + ids = results.GetResultIndices() + for id in ids: + self.assertTrue(id in cond_id_ref) # Nodes array search results = self.search.SearchInRadius(self.sub_model_part.Nodes, radius) - if self.is_first_partition(): - self.assertEqual(len(results), 1) - self.assertEqual(len(results[0]), 8) - for result in results[0]: - self.assertTrue(result.Get().Id in cond_id_ref) + self.assertEqual(results.NumberOfPointsResults(), 1) + node_results = results[self.node] + node_results.SynchronizeAll(self.data_comm) + self.assertEqual(node_results.NumberOfGlobalResults(), 8) + ids = node_results.GetResultIndices() + for id in ids: + self.assertTrue(id in cond_id_ref) def test_GeometricalObjectsBins_SearchNearestInRadius(self): radius = 0.35 # One node search - if not KM.IsDistributedRun(): - result = self.search.SearchNearestInRadius(self.node, radius) - self.assertEqual(result.Get().Id, 1) + result = self.search.SearchNearestInRadius(self.node, radius) + self.assertEqual(result.Get().Id, 1) # Nodes array search - results = self.search.SearchNearestInRadius(self.sub_model_part.Nodes, radius) - if self.is_first_partition(): - self.assertEqual(len(results), 1) - self.assertEqual(results[0].Get().Id, 1) + results = self.search.SearchNearestInRadius(self.sub_model_part.Nodes, radius) + self.assertEqual(results.NumberOfPointsResults(), 1) + node_results = results[self.node] + node_results.SynchronizeAll(self.data_comm) + self.assertEqual(node_results.NumberOfGlobalResults(), 1) + if self.data_comm.Rank() == 0: + self.assertEqual(node_results[1].Id, 1) # Local result + #self.assertEqual(node_results(1).Id, 1) # Global result # TODO: Failing def test_GeometricalObjectsBins_SearchNearest(self): # One node search - if not KM.IsDistributedRun(): - result = self.search.SearchNearest(self.node) - self.assertEqual(result.Get().Id, 1) - - # Nodes array search - results = self.search.SearchNearest(self.sub_model_part.Nodes) - if self.is_first_partition(): - self.assertEqual(len(results), 1) - self.assertEqual(results[0].Get().Id, 1) - - def test_GeometricalObjectsBins_SearchIsInside(self): - # One node search - if not KM.IsDistributedRun(): - result = self.search.SearchIsInside(self.node) - self.assertEqual(result.Get(), None) + result = self.search.SearchNearest(self.node) + self.assertEqual(result.Get().Id, 1) # Nodes array search - results = self.search.SearchIsInside(self.sub_model_part.Nodes) - if self.is_first_partition(): - self.assertEqual(len(results), 1) - self.assertEqual(results[0].Get(), None) + results = self.search.SearchNearest(self.sub_model_part.Nodes) + self.assertEqual(results.NumberOfPointsResults(), 1) + node_results = results[self.node] + node_results.SynchronizeAll(self.data_comm) + self.assertEqual(node_results.NumberOfGlobalResults(), 1) + if self.data_comm.Rank() == 0: + self.assertEqual(node_results[1].Id, 1) # Local result + #self.assertEqual(node_results(1).Id, 1) # Global result # TODO: Failing + + # def test_GeometricalObjectsBins_SearchIsInside(self): # TODO: Failing + # # One node search + # result = self.search.SearchIsInside(self.node) + # self.assertEqual(result.Get(), None) + + # # Nodes array search + # results = self.search.SearchIsInside(self.sub_model_part.Nodes) + # self.assertEqual(results.NumberOfPointsResults(), 1) + # node_results = results[self.node] + # node_results.SynchronizeAll(self.data_comm) + # self.assertEqual(node_results.NumberOfGlobalResults(), 1) + # self.assertEqual(node_results[1].Get(), None) if __name__ == '__main__': KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) From d4e3e99740c8ce21dee1fd8c7d3f998c61b39050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 30 May 2023 23:00:45 +0200 Subject: [PATCH 029/145] Update definitions --- .../spatial_containers/spatial_search_result_container.h | 6 ++---- kratos/tests/test_geometrical_object_bins.py | 8 ++++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 79f2f4c9c23e..acd1bcc2b04f 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -90,7 +90,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer */ TObjectType& operator[](const std::size_t Index) { - return mLocalPointers[Index]; + return *(mLocalPointers.begin() + Index); } /** @@ -100,9 +100,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer */ const TObjectType& operator[](const std::size_t Index) const { - const auto it = mLocalPointers.find(Index); - KRATOS_ERROR_IF(it == mLocalPointers.end()) << "The result does not exist." << std::endl; - return *it; + return *(mLocalPointers.begin() + Index); } /** diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index 717ba0b9c410..9aefa70aa49f 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -99,8 +99,8 @@ def test_GeometricalObjectsBins_SearchNearestInRadius(self): node_results.SynchronizeAll(self.data_comm) self.assertEqual(node_results.NumberOfGlobalResults(), 1) if self.data_comm.Rank() == 0: - self.assertEqual(node_results[1].Id, 1) # Local result - #self.assertEqual(node_results(1).Id, 1) # Global result # TODO: Failing + self.assertEqual(node_results[0].Id, 1) # Local result + self.assertEqual(node_results(0).Id, 1) # Global result def test_GeometricalObjectsBins_SearchNearest(self): # One node search @@ -114,8 +114,8 @@ def test_GeometricalObjectsBins_SearchNearest(self): node_results.SynchronizeAll(self.data_comm) self.assertEqual(node_results.NumberOfGlobalResults(), 1) if self.data_comm.Rank() == 0: - self.assertEqual(node_results[1].Id, 1) # Local result - #self.assertEqual(node_results(1).Id, 1) # Global result # TODO: Failing + self.assertEqual(node_results[0].Id, 1) # Local result + self.assertEqual(node_results(0).Id, 1) # Global result # def test_GeometricalObjectsBins_SearchIsInside(self): # TODO: Failing # # One node search From dfffd9cf68a75c2d462e587425490e5a9f4ea5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Wed, 31 May 2023 00:56:33 +0200 Subject: [PATCH 030/145] Update definition --- kratos/spatial_containers/geometrical_objects_bins.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index cc5158cc296f..3455dbae4d2a 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -398,7 +398,9 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); auto result = SearchIsInside(*it_point); - r_point_result.AddResult(result); + if (result.GetIsObjectFound()){ + r_point_result.AddResult(result); + } } } From 2c92fd093f613e6a82691cde7ea4a7992adf82dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Wed, 31 May 2023 00:56:38 +0200 Subject: [PATCH 031/145] Update test --- kratos/tests/test_geometrical_object_bins.py | 23 ++++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index 9aefa70aa49f..2828e9df5bd0 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -117,18 +117,17 @@ def test_GeometricalObjectsBins_SearchNearest(self): self.assertEqual(node_results[0].Id, 1) # Local result self.assertEqual(node_results(0).Id, 1) # Global result - # def test_GeometricalObjectsBins_SearchIsInside(self): # TODO: Failing - # # One node search - # result = self.search.SearchIsInside(self.node) - # self.assertEqual(result.Get(), None) - - # # Nodes array search - # results = self.search.SearchIsInside(self.sub_model_part.Nodes) - # self.assertEqual(results.NumberOfPointsResults(), 1) - # node_results = results[self.node] - # node_results.SynchronizeAll(self.data_comm) - # self.assertEqual(node_results.NumberOfGlobalResults(), 1) - # self.assertEqual(node_results[1].Get(), None) + def test_GeometricalObjectsBins_SearchIsInside(self): + # One node search + result = self.search.SearchIsInside(self.node) + self.assertEqual(result.Get(), None) + + # Nodes array search + results = self.search.SearchIsInside(self.sub_model_part.Nodes) + self.assertEqual(results.NumberOfPointsResults(), 1) + node_results = results[self.node] + node_results.SynchronizeAll(self.data_comm) + self.assertEqual(node_results.NumberOfGlobalResults(), 0) if __name__ == '__main__': KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) From 63d035c58fbd322848141dfa9db9f1e309da2fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Wed, 31 May 2023 10:10:25 +0200 Subject: [PATCH 032/145] Checking in container --- .../geometrical_objects_bins.h | 4 +--- .../spatial_search_result_container.cpp | 17 ++++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index 3455dbae4d2a..cc5158cc296f 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -398,9 +398,7 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); auto result = SearchIsInside(*it_point); - if (result.GetIsObjectFound()){ - r_point_result.AddResult(result); - } + r_point_result.AddResult(result); } } diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index b9901bbfe232..c3f1d62abb53 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -36,13 +36,16 @@ SpatialSearchResultContainer::SpatialSearchResultContainer() template void SpatialSearchResultContainer::AddResult(SpatialSearchResult& rResult) { - // Push_back in local pointers - TObjectType* p_local_result = rResult.Get().get(); - mLocalPointers.push_back(p_local_result); - - // Add distances - const IndexType id = p_local_result->Id(); - mLocalDistances.insert({id, rResult.GetDistance()}); + // Check if the object has been found + if (rResult.GetIsObjectFound()) { + // Push_back in local pointers + TObjectType* p_local_result = rResult.Get().get(); + mLocalPointers.push_back(p_local_result); + + // Add distances + const IndexType id = p_local_result->Id(); + mLocalDistances.insert({id, rResult.GetDistance()}); + } } /***********************************************************************************/ From a8d5f9804e9c7c544f542858a962cb2817e06875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Wed, 31 May 2023 10:42:35 +0200 Subject: [PATCH 033/145] Adding iterators --- .../add_search_strategies_to_python.cpp | 6 + .../spatial_search_result_container.h | 395 +++++++++++++++++- 2 files changed, 398 insertions(+), 3 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index a9937199cef4..e8a7421f20f2 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -757,6 +757,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("__call__", [&](ResultTypeContainer& self, const std::size_t Index) { return self(Index); }) + .def("__iter__", [](ResultTypeContainer& self) { + return py::make_iterator(self.begin(), self.end()); + }, py::keep_alive<0, 1>()) /* Keep object alive while iterator is used */ ; using ResultTypeContainerMap = SpatialSearchResultContainerMap; @@ -773,6 +776,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("__call__", [&](ResultTypeContainerMap& self, const array_1d& rCoordinates) { return self(rCoordinates); }) + .def("__iter__", [](ResultTypeContainerMap& self) { + return py::make_iterator(self.begin(), self.end()); + }, py::keep_alive<0, 1>()) /* Keep object alive while iterator is used */ ; using NodesContainerType = ModelPart::NodesContainerType; diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index acd1bcc2b04f..11858071d823 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -67,6 +67,9 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer /// The global pointer communicator using PointerCommunicatorPointer = typename GlobalPointerCommunicator::Pointer; + /// The global pointer vector type + using GPVector = GlobalPointersVector; + ///@} ///@name Life Cycle ///@{ @@ -131,6 +134,186 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@name Operations ///@{ + /** + * @brief Returns an iterator pointing to the beginning of the container. + * @return Iterator pointing to the beginning of the container. + */ + typename GPVector::iterator begin() + { + return mGlobalPointers.begin(); + } + + /** + * @brief Returns a constant iterator pointing to the beginning of the container. + * @return Constant iterator pointing to the beginning of the container. + */ + typename GPVector::const_iterator begin() const + { + return mGlobalPointers.begin(); + } + + /** + * @brief Returns an iterator pointing to the end of the container. + * @return Iterator pointing to the end of the container. + */ + typename GPVector::iterator end() + { + return mGlobalPointers.end(); + } + + /** + * @brief Returns a constant iterator pointing to the end of the container. + * @return Constant iterator pointing to the end of the container. + */ + typename GPVector::const_iterator end() const + { + return mGlobalPointers.end(); + } + + /** + * @brief Returns a reverse iterator pointing to the last element of the container. + * @return Reverse iterator pointing to the last element of the container. + */ + typename GPVector::reverse_iterator rbegin() + { + return mGlobalPointers.rbegin(); + } + + /** + * @brief Returns a constant reverse iterator pointing to the last element of the container. + * @return Constant reverse iterator pointing to the last element of the container. + */ + typename GPVector::const_reverse_iterator rbegin() const + { + return mGlobalPointers.rbegin(); + } + + /** + * @brief Returns a reverse iterator pointing to the theoretical element preceding the first element of the container. + * @return Reverse iterator pointing to the theoretical element preceding the first element. + */ + typename GPVector::reverse_iterator rend() + { + return mGlobalPointers.rend(); + } + + /** + * @brief Returns a constant reverse iterator pointing to the theoretical element preceding the first element of the container. + * @return Constant reverse iterator pointing to the theoretical element preceding the first element. + */ + typename GPVector::const_reverse_iterator rend() const + { + return mGlobalPointers.rend(); + } + + /** + * @brief Returns a pointer iterator pointing to the beginning of the container. + * @return Pointer iterator pointing to the beginning of the container. + */ + typename GPVector::ptr_iterator ptr_begin() + { + return mGlobalPointers.ptr_begin(); + } + + /** + * @brief Returns a constant pointer iterator pointing to the beginning of the container. + * @return Constant pointer iterator pointing to the beginning of the container. + */ + typename GPVector::ptr_const_iterator ptr_begin() const + { + return mGlobalPointers.ptr_begin(); + } + + /** + * @brief Returns a pointer iterator pointing to the end of the container. + * @return Pointer iterator pointing to the end of the container. + */ + typename GPVector::ptr_iterator ptr_end() + { + return mGlobalPointers.ptr_end(); + } + + /** + * @brief Returns a constant pointer iterator pointing to the end of the container. + * @return Constant pointer iterator pointing to the end of the container. + */ + typename GPVector::ptr_const_iterator ptr_end() const + { + return mGlobalPointers.ptr_end(); + } + + /** + * @brief Returns a reverse pointer iterator pointing to the last element of the container. + * @return Reverse pointer iterator pointing to the last element of the container. + */ + typename GPVector::ptr_reverse_iterator ptr_rbegin() + { + return mGlobalPointers.ptr_rbegin(); + } + + /** + * @brief Returns a constant reverse pointer iterator pointing to the last element of the container. + * @return Constant reverse pointer iterator pointing to the last element of the container. + */ + typename GPVector::ptr_const_reverse_iterator ptr_rbegin() const + { + return mGlobalPointers.ptr_rbegin(); + } + + /** + * @brief Returns a reverse pointer iterator pointing to the theoretical element preceding the first element of the container. + * @return Reverse pointer iterator pointing to the theoretical element preceding the first element. + */ + typename GPVector::ptr_reverse_iterator ptr_rend() + { + return mGlobalPointers.ptr_rend(); + } + + /** + * @brief Returns a constant reverse pointer iterator pointing to the theoretical element preceding the first element of the container. + * @return Constant reverse pointer iterator pointing to the theoretical element preceding the first element. + */ + typename GPVector::ptr_const_reverse_iterator ptr_rend() const + { + return mGlobalPointers.ptr_rend(); + } + + /** + * @brief Returns a reference to the first element of the container. + * @return Reference to the first element of the container. + */ + typename GPVector::reference front() /* nothrow */ + { + return mGlobalPointers.front(); + } + + /** + * @brief Returns a constant reference to the first element of the container. + * @return Constant reference to the first element of the container. + */ + typename GPVector::const_reference front() const /* nothrow */ + { + return mGlobalPointers.front(); + } + + /** + * @brief Returns a reference to the last element of the container. + * @return Reference to the last element of the container. + */ + typename GPVector::reference back() /* nothrow */ + { + return mGlobalPointers.back(); + } + + /** + * @brief Returns a constant reference to the last element of the container. + * @return Constant reference to the last element of the container. + */ + typename GPVector::const_reference back() const /* nothrow */ + { + return mGlobalPointers.back(); + } + /** * @brief Returns the local pointers size * @return The local pointers size @@ -151,10 +334,19 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer /** * @brief Add a result to the container - * @param rResult The result + * @param rResult The result to be added */ void AddResult(SpatialSearchResult& rResult); + /** + * @brief Pushes back a result to the container + * @param rResult The result to be added + */ + void push_back(SpatialSearchResult& rResult) + { + AddResult(rResult); + } + /** * @brief Clear the containers * @details This method clears the containers @@ -265,7 +457,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@{ LocalPointerVector mLocalPointers; /// Local pointers of the container - GlobalPointersVector mGlobalPointers; /// Global pointers of the container + GPVector mGlobalPointers; /// Global pointers of the container std::unordered_map mLocalDistances; /// The local distances PointerCommunicatorPointer mpGlobalPointerCommunicator = nullptr; /// Global pointer to the communicator @@ -315,6 +507,171 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap /// The hash type using HashType = std::size_t; + /// The container type + using ContainerType = std::unordered_map>; + + // Define the iterator class + class iterator { + public: + /// The category of the iterator, indicating forward iteration. + using iterator_category = std::forward_iterator_tag; + + /// The type of the value pointed to by the iterator. + using value_type = std::pair>; + + /// The difference type between two iterators. + using difference_type = std::ptrdiff_t; + + /// A pointer to the value type. + using pointer = value_type*; + + /// A reference to the value type. + using reference = value_type&; + + /** + * @brief Constructs an iterator pointing to the specified position. + * @param iter The iterator to initialize from. + */ + iterator(typename ContainerType::iterator iter) : iter_(iter) {} + + /** + * @brief Prefix increment operator. + * @return Reference to the incremented iterator. + */ + iterator& operator++() { + ++iter_; + return *this; + } + + /** + * @brief Postfix increment operator. + * @return An iterator before increment. + */ + iterator operator++(int) { + iterator temp = *this; + ++(*this); + return temp; + } + + /** + * @brief Equality comparison operator. + * @param other The iterator to compare with. + * @return True if the iterators are equal, false otherwise. + */ + bool operator==(const iterator& other) const { + return iter_ == other.iter_; + } + + /** + * @brief Inequality comparison operator. + * @param other The iterator to compare with. + * @return True if the iterators are not equal, false otherwise. + */ + bool operator!=(const iterator& other) const { + return !(*this == other); + } + + /** + * @brief Dereference operator. + * @return Reference to the value pointed to by the iterator. + */ + reference operator*() const { + return *iter_; + } + + /** + * @brief Member access operator. + * @return Pointer to the value pointed to by the iterator. + */ + pointer operator->() const { + return &(*iter_); + } + + private: + typename ContainerType::iterator iter_; + }; + + // Define the const_iterator class + class const_iterator { + public: + /// The category of the iterator, indicating forward iteration. + using iterator_category = std::forward_iterator_tag; + + /// The type of the value pointed to by the iterator. + using value_type = std::pair>; + + /// The difference type between two iterators. + using difference_type = std::ptrdiff_t; + + /// A pointer to the value type. + using pointer = const value_type*; + + /// A reference to the value type. + using reference = const value_type&; + + /** + * @brief Constructs a constant iterator pointing to the specified position. + * @param iter The constant iterator to initialize from. + */ + const_iterator(typename ContainerType::const_iterator iter) : iter_(iter) {} + + /** + * @brief Prefix increment operator. + * @return Reference to the incremented constant iterator. + */ + const_iterator& operator++() { + ++iter_; + return *this; + } + + /** + * @brief Postfix increment operator. + * @return A constant iterator before increment. + */ + const_iterator operator++(int) { + const_iterator temp = *this; + ++(*this); + return temp; + } + + /** + * @brief Equality comparison operator. + * @param other The constant iterator to compare with. + * @return True if the constant iterators are equal, false otherwise. + */ + bool operator==(const const_iterator& other) const { + return iter_ == other.iter_; + } + + /** + * @brief Inequality comparison operator. + * @param other The constant iterator to compare with. + * @return True if the constant iterators are not equal, false otherwise. + */ + bool operator!=(const const_iterator& other) const { + return !(*this == other); + } + + /** + * @brief Dereference operator. + * @return Reference to the value pointed to by the constant iterator. + */ + reference operator*() const { + return *iter_; + } + + /** + * @brief Member access operator. + * @return Pointer to the value pointed to by the constant iterator. + */ + pointer operator->() const { + return &(*iter_); + } + + private: + typename ContainerType::const_iterator iter_; + }; + ///@} ///@name Life Cycle ///@{ @@ -379,6 +736,38 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap ///@name Operations ///@{ + /** + * @brief Returns an iterator pointing to the beginning of the container. + * @return An iterator pointing to the beginning of the container. + */ + iterator begin() { + return iterator(mPointResults.begin()); + } + + /** + * @brief Returns an iterator pointing to the end of the container. + * @return An iterator pointing to the end of the container. + */ + iterator end() { + return iterator(mPointResults.end()); + } + + /** + * @brief Returns a constant iterator pointing to the beginning of the container. + * @return A constant iterator pointing to the beginning of the container. + */ + const_iterator begin() const { + return const_iterator(mPointResults.begin()); + } + + /** + * @brief Returns a constant iterator pointing to the end of the container. + * @return A constant iterator pointing to the end of the container. + */ + const_iterator end() const { + return const_iterator(mPointResults.end()); + } + /** * @brief Returns the number of points results * @return The number of points results @@ -425,7 +814,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap ///@name Member Variables ///@{ - std::unordered_map> mPointResults; /// The results of each point + ContainerType mPointResults; /// The results of each point ///@} ///@name Private Operations From fa21f837eea7ab1eac8e93990a6eacf6cf34a495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Wed, 31 May 2023 11:09:32 +0200 Subject: [PATCH 034/145] Rename tests --- .../spatial_containers/test_spatial_search_result.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp index af9d601392bf..a9d9b689558f 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp @@ -23,7 +23,7 @@ namespace Kratos::Testing { -KRATOS_TEST_CASE_IN_SUITE(TestDefaultConstruction, KratosCoreFastSuite) +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultDefaultConstruction, KratosCoreFastSuite) { auto result = SpatialSearchResult(); KRATOS_CHECK_EQUAL(result.IsObjectFound(), false); @@ -32,21 +32,21 @@ KRATOS_TEST_CASE_IN_SUITE(TestDefaultConstruction, KratosCoreFastSuite) KRATOS_CHECK_EQUAL(result.GetDistance(), 0.0); } -KRATOS_TEST_CASE_IN_SUITE(TestObjectFound, KratosCoreFastSuite) +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultObjectFound, KratosCoreFastSuite) { auto result = SpatialSearchResult(); result.SetIsObjectFound(true); KRATOS_CHECK_EQUAL(result.IsObjectFound(), true); } -KRATOS_TEST_CASE_IN_SUITE(TestDistanceCalculated, KratosCoreFastSuite) +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultDistanceCalculated, KratosCoreFastSuite) { auto result = SpatialSearchResult(); result.SetIsDistanceCalculated(true); KRATOS_CHECK_EQUAL(result.IsDistanceCalculated(), true); } -KRATOS_TEST_CASE_IN_SUITE(TestPointer, KratosCoreFastSuite) +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultPointer, KratosCoreFastSuite) { Element element = Element(); auto result = SpatialSearchResult(&element); @@ -54,7 +54,7 @@ KRATOS_TEST_CASE_IN_SUITE(TestPointer, KratosCoreFastSuite) KRATOS_CHECK_EQUAL(result.Get().get(), ptr); } -KRATOS_TEST_CASE_IN_SUITE(TestDistance, KratosCoreFastSuite) +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultDistance, KratosCoreFastSuite) { auto result = SpatialSearchResult(); result.SetDistance(3.14); From 407a32650be016e73c93d5765bdb638cef74545e Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 17 May 2023 20:16:04 +0200 Subject: [PATCH 035/145] New methods for SpatialSearchResult --- .../spatial_search_result.h | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result.h b/kratos/spatial_containers/spatial_search_result.h index 1f1341ce5e90..2222506ee7dc 100644 --- a/kratos/spatial_containers/spatial_search_result.h +++ b/kratos/spatial_containers/spatial_search_result.h @@ -136,18 +136,38 @@ class SpatialSearchResult mIsDistanceCalculated = true; } + /// Getting if the object is found + bool GetIsObjectFound() const { + return mIsObjectFound; + } + + /// Getting if the ibject is found + void SetIsObjectFound(const bool IsObjectFound) { + mIsObjectFound = IsObjectFound; + } + + /// Getting if the distance is calculated + bool GetIsDistanceCalculated() const { + return mIsDistanceCalculated; + } + + /// Setting if the distance is calculated + void SetIsDistanceCalculated(const bool IsDistanceCalculated) { + mIsDistanceCalculated = IsDistanceCalculated; + } + ///@} ///@name Inquiry ///@{ - /// Returns true if the object is set - bool IsObjectFound() const { - return mIsObjectFound; + /// Returns true if the object is set + bool IsObjectFound() const { + return mIsObjectFound; } /// Returns true if the distance is set - bool IsDistanceCalculated() const { - return mIsDistanceCalculated; + bool IsDistanceCalculated() const { + return mIsDistanceCalculated; } ///@} From 259cabbe15e2ad195ae90243971542ef7b9c26b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Wed, 31 May 2023 14:26:46 +0200 Subject: [PATCH 036/145] Fix include --- .../spatial_containers/test_spatial_search_result.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp index a9d9b689558f..25cb804068ab 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result.cpp @@ -18,7 +18,7 @@ // Project includes #include "testing/testing.h" #include "includes/element.h" -#include "spatial_containers/spatial_search_result_container.h" +#include "spatial_containers/spatial_search_result.h" namespace Kratos::Testing { @@ -61,4 +61,4 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultDistance, KratosCoreFastSuite) KRATOS_CHECK_EQUAL(result.GetDistance(), 3.14); } -} // namespace Kratos::Testing \ No newline at end of file +} // namespace Kratos::Testing From 93875472e070702e3e7cc399dd6afd1f2130bbe0 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 31 May 2023 14:45:34 +0200 Subject: [PATCH 037/145] Fix comparisons integers --- .../test_spatial_search_result_container.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index e619931cd517..1bb0f641c8a6 100644 --- a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -106,7 +106,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerSynchronize // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); - KRATOS_CHECK_EQUAL(r_global_pointers.size(), r_data_comm.Size()); + KRATOS_CHECK_EQUAL(static_cast(r_global_pointers.size()), r_data_comm.Size()); KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); } @@ -138,8 +138,8 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultSh auto shape_functions = container.GetResultShapeFunctions(point); // Check shape functions - KRATOS_CHECK_EQUAL(shape_functions.size(), r_data_comm.Size()); - for (unsigned int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { + KRATOS_CHECK_EQUAL(static_cast(shape_functions.size()), r_data_comm.Size()); + for (int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { KRATOS_CHECK_NEAR(shape_functions[i_rank][0], 0.5, 1.0e-12); KRATOS_CHECK_NEAR(shape_functions[i_rank][1], 0.5, 1.0e-12); } @@ -167,8 +167,8 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultIn auto indixes = container.GetResultIndices(); // Check shape functions - KRATOS_CHECK_EQUAL(indixes.size(), r_data_comm.Size()); - for (unsigned int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { + KRATOS_CHECK_EQUAL(static_cast(indixes.size()), r_data_comm.Size()); + for (int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { KRATOS_CHECK_EQUAL(indixes[i_rank], i_rank + 1); } } @@ -200,8 +200,8 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultCo auto coordinates = container.GetResultCoordinates(); // Check shape functions - KRATOS_CHECK_EQUAL(coordinates.size(), r_data_comm.Size()); - for (unsigned int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { + KRATOS_CHECK_EQUAL(static_cast(coordinates.size()), r_data_comm.Size()); + for (int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { KRATOS_CHECK_EQUAL(coordinates[i_rank].size(), 2); KRATOS_CHECK_VECTOR_NEAR(coordinates[i_rank][0], p_node1->Coordinates(), 1.0e-12); KRATOS_CHECK_VECTOR_NEAR(coordinates[i_rank][1], p_node2->Coordinates(), 1.0e-12); From 485cf4bd391ab19209fb1512a3a07b5aa910c0d1 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 31 May 2023 17:06:52 +0200 Subject: [PATCH 038/145] Missing static_cast --- .../spatial_containers/test_spatial_search_result_container.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 1bb0f641c8a6..79a11a4867ab 100644 --- a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -169,7 +169,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultIn // Check shape functions KRATOS_CHECK_EQUAL(static_cast(indixes.size()), r_data_comm.Size()); for (int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { - KRATOS_CHECK_EQUAL(indixes[i_rank], i_rank + 1); + KRATOS_CHECK_EQUAL(static_cast(indixes[i_rank]), i_rank + 1); } } From 217d605f3f5d030a74c453b17411befd051c107a Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 31 May 2023 17:26:14 +0200 Subject: [PATCH 039/145] Consistency --- .../geometrical_objects_bins.h | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index cc5158cc296f..29e0ace66e8f 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -193,6 +193,7 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param itPointEnd The last point iterator * @param Radius The radius to be checked * @param rResults The results of the search + * @param ClearSolution Clear the current solution * @tparam TPointIteratorType The type of the point iterator */ template @@ -200,9 +201,16 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, const double Radius, - ResultTypeContainerMap& rResults + ResultTypeContainerMap& rResults, + const bool ClearSolution = true ) { + // Clear current solution + if (ClearSolution) { + rResults.Clear(); + } + + // Adding the results to the container for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ auto& r_partial_result = rResults.InitializeResult(it_point->Coordinates()); SearchInRadius(*it_point, Radius, r_partial_result); @@ -261,6 +269,7 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param itPointEnd The last point iterator * @param Radius The radius to be checked * @param rResults The result of the search + * @param ClearSolution Clear the current solution * @tparam TPointIteratorType The type of the point iterator */ template @@ -268,9 +277,15 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, const double Radius, - ResultTypeContainerMap& rResults + ResultTypeContainerMap& rResults, + const bool ClearSolution = true ) { + // Clear current solution + if (ClearSolution) { + rResults.Clear(); + } + // Adding the results to the container for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); @@ -321,15 +336,22 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param itPointBegin The first point iterator * @param itPointEnd The last point iterator * @param rResults The result of the search + * @param ClearSolution Clear the current solution * @tparam TPointIteratorType The type of the point iterator */ template void SearchNearest( TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, - ResultTypeContainerMap& rResults + ResultTypeContainerMap& rResults, + const bool ClearSolution = true ) { + // Clear current solution + if (ClearSolution) { + rResults.Clear(); + } + // Adding the results to the container for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); @@ -385,15 +407,22 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param itPointBegin The first point iterator * @param itPointEnd The last point iterator * @param rResults The result of the search + * @param ClearSolution Clear the current solution * @tparam TPointIteratorType The type of the point iterator */ template void SearchIsInside( TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, - ResultTypeContainerMap& rResults + ResultTypeContainerMap& rResults, + const bool ClearSolution = true ) { + // Clear current solution + if (ClearSolution) { + rResults.Clear(); + } + // Adding the results to the container for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++){ auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); From fb5481bb5f939fbc4504d251582ed9b3104dd456 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 31 May 2023 17:38:21 +0200 Subject: [PATCH 040/145] Not technmically a typo, but changed --- .../test_spatial_search_result_container.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index f367b5e45d35..57a72eb521ea 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -150,11 +150,11 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultIndices, KratosCo container.SynchronizeAll(data_communicator); // Compute shape functions - auto indixes = container.GetResultIndices(); + auto indices = container.GetResultIndices(); // Check shape functions - KRATOS_CHECK_EQUAL(indixes.size(), 1); - KRATOS_CHECK_EQUAL(indixes[0], object.Id()); + KRATOS_CHECK_EQUAL(indices.size(), 1); + KRATOS_CHECK_EQUAL(indices[0], object.Id()); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, KratosCoreFastSuite) From c7d082c233ae0d95934c505302a841822265155b Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 31 May 2023 17:39:05 +0200 Subject: [PATCH 041/145] Correction --- .../test_spatial_search_result_container.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 57a72eb521ea..7b9ebccc634f 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -149,10 +149,10 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultIndices, KratosCo DataCommunicator data_communicator; container.SynchronizeAll(data_communicator); - // Compute shape functions + // Compute indices auto indices = container.GetResultIndices(); - // Check shape functions + // Check indices KRATOS_CHECK_EQUAL(indices.size(), 1); KRATOS_CHECK_EQUAL(indices[0], object.Id()); } @@ -178,10 +178,10 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, Krat DataCommunicator data_communicator; container.SynchronizeAll(data_communicator); - // Compute shape functions + // Compute result coordinates auto coordinates = container.GetResultCoordinates(); - // Check shape functions + // Check result coordinates KRATOS_CHECK_EQUAL(coordinates.size(), 1); KRATOS_CHECK_EQUAL(coordinates[0].size(), 2); KRATOS_CHECK_VECTOR_NEAR(coordinates[0][0], p_node1->Coordinates(), 1.0e-12); From 269f067a4eeb98ec26bbbee78f74bbb2fc291128 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 31 May 2023 22:12:28 +0200 Subject: [PATCH 042/145] Minor --- kratos/spatial_containers/spatial_search_result_container.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index c3f1d62abb53..1c75156a17af 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -37,7 +37,7 @@ template void SpatialSearchResultContainer::AddResult(SpatialSearchResult& rResult) { // Check if the object has been found - if (rResult.GetIsObjectFound()) { + if (rResult.IsObjectFound()) { // Push_back in local pointers TObjectType* p_local_result = rResult.Get().get(); mLocalPointers.push_back(p_local_result); From fb5fa272d6d1ebc7952f6d83be17623702ae8ded Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 31 May 2023 23:02:00 +0200 Subject: [PATCH 043/145] Improve operators --- .../spatial_search_result_container.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 11858071d823..116fbc70c7e8 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -693,7 +693,10 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap */ SpatialSearchResultContainer& operator[](const array_1d& rCoordinates) { - return mPointResults[Hash(rCoordinates)]; + const HashType hash = Hash(rCoordinates); + const auto it = mPointResults.find(hash); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for point: " << rCoordinates[0] << ", " << rCoordinates[1] << ", " << rCoordinates[2] << std::endl; + return it->second; } /** @@ -705,7 +708,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap { const HashType hash = Hash(rCoordinates); const auto it = mPointResults.find(hash); - KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist." << std::endl; + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for point: " << rCoordinates[0] << ", " << rCoordinates[1] << ", " << rCoordinates[2] << std::endl; return it->second; } @@ -716,7 +719,10 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap */ SpatialSearchResultContainer& operator()(const array_1d& rCoordinates) { - return mPointResults[Hash(rCoordinates)]; + const HashType hash = Hash(rCoordinates); + const auto it = mPointResults.find(hash); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for point: " << rCoordinates[0] << ", " << rCoordinates[1] << ", " << rCoordinates[2] << std::endl; + return it->second; } /** @@ -728,7 +734,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap { const HashType hash = Hash(rCoordinates); const auto it = mPointResults.find(hash); - KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist." << std::endl; + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for point: " << rCoordinates[0] << ", " << rCoordinates[1] << ", " << rCoordinates[2] << std::endl; return it->second; } From 10f6b6da1461ee403cb3f61372e6f256013d1b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Thu, 1 Jun 2023 13:01:43 +0200 Subject: [PATCH 044/145] Suggestion Co-authored-by: Carlos Roig --- kratos/includes/global_pointer.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kratos/includes/global_pointer.h b/kratos/includes/global_pointer.h index 8e27a8d194e2..76cedd2bd70f 100644 --- a/kratos/includes/global_pointer.h +++ b/kratos/includes/global_pointer.h @@ -204,7 +204,11 @@ class GlobalPointer { */ bool operator==(const GlobalPointer& rOther) { - return this->get() == rOther.get(); +#ifdef KRATOS_USING_MPI + return this->get() == rOther.get() && this->GetRank() == rOther.GetRank(); +#else + return this->get() == rOther.get(); +#endif } /** Fills buffer with the GlobalPoiter data From 8f9ced9c93eedb04ea1ee39b8a010e3cc9e5bdd3 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 1 Jun 2023 18:02:15 +0200 Subject: [PATCH 045/145] Adding IsObjectFound --- .../spatial_containers/spatial_search_result_container.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 116fbc70c7e8..c0c0a6cf2f5d 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -314,6 +314,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer return mGlobalPointers.back(); } + /** + * @brief Returns if at least one result is found + * @return If at least one result is found + */ + bool IsObjectFound() const + { + return static_cast(mGlobalPointers.size()); + } + /** * @brief Returns the local pointers size * @return The local pointers size From 26662de24413700d214b63fdebc5e1452af7e722 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 1 Jun 2023 18:02:53 +0200 Subject: [PATCH 046/145] Consistency --- .../add_search_strategies_to_python.cpp | 13 +++-- .../geometrical_objects_bins.cpp | 53 +++++++++++++++++++ .../geometrical_objects_bins.h | 53 ++++++++++++++++++- 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index e8a7421f20f2..1ad9ee732104 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -712,6 +712,7 @@ void AddSearchStrategiesToPython(pybind11::module& m) py::class_(m, "ResultTypeContainer") .def(py::init< >()) + .def("IsObjectFound", &ResultTypeContainer::IsObjectFound) .def("NumberOfLocalResults", &ResultTypeContainer::NumberOfLocalResults) .def("NumberOfGlobalResults", &ResultTypeContainer::NumberOfGlobalResults) .def("AddResult", &ResultTypeContainer::AddResult) @@ -806,7 +807,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) }) .def("SearchNearestInRadius", [&](GeometricalObjectsBins& self, const Point& rPoint, const double Radius) { // Perform the search - return self.SearchNearestInRadius(rPoint, Radius); + ResultTypeContainer results; + self.SearchNearestInRadius(rPoint, Radius, results); + return results; }) .def("SearchNearestInRadius", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes, const double Radius) { // Perform the search @@ -816,7 +819,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) }) .def("SearchNearest", [&](GeometricalObjectsBins& self, const Point& rPoint) { // Perform the search - return self.SearchNearest(rPoint); + ResultTypeContainer results; + self.SearchNearest(rPoint, results); + return results; }) .def("SearchNearest", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes) { // Perform the search @@ -826,7 +831,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) }) .def("SearchIsInside", [&](GeometricalObjectsBins& self, const Point& rPoint) { // Perform the search - return self.SearchIsInside(rPoint); + ResultTypeContainer results; + self.SearchIsInside(rPoint, results); + return results; }) .def("SearchIsInside", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes) { // Perform the search diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index d194c7f49fd0..6ff21df76a86 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -104,11 +104,15 @@ void GeometricalObjectsBins::SearchInRadius( ResultTypeContainer& rResults ) { + // Search std::vector results; SearchInRadius(rPoint, Radius, results); for (auto& r_result : results) { rResults.AddResult(r_result); } + + // Synchronize + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); } /***********************************************************************************/ @@ -155,6 +159,23 @@ GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchNearestInRadius /***********************************************************************************/ /***********************************************************************************/ +void GeometricalObjectsBins::SearchNearestInRadius( + const Point& rPoint, + const double Radius, + ResultTypeContainer& rResults + ) +{ + // Search + auto result = SearchNearestInRadius(rPoint, Radius); + rResults.AddResult(result); + + // Synchronize + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); +} + +/***********************************************************************************/ +/***********************************************************************************/ + GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchNearest(const Point& rPoint) { ResultType current_result; @@ -168,6 +189,22 @@ GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchNearest(const P /***********************************************************************************/ /***********************************************************************************/ +void GeometricalObjectsBins::SearchNearest( + const Point& rPoint, + ResultTypeContainer& rResults + ) +{ + // Search + auto result = SearchNearest(rPoint); + rResults.AddResult(result); + + // Synchronize + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); +} + +/***********************************************************************************/ +/***********************************************************************************/ + GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchIsInside(const Point& rPoint) { ResultType current_result; @@ -187,6 +224,22 @@ GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchIsInside(const /***********************************************************************************/ /***********************************************************************************/ +void GeometricalObjectsBins::SearchIsInside( + const Point& rPoint, + ResultTypeContainer& rResults + ) +{ + // Search + auto result = SearchIsInside(rPoint); + rResults.AddResult(result); + + // Synchronize + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void GeometricalObjectsBins::CalculateCellSize(const std::size_t NumberOfCells) { const std::size_t avarage_number_of_cells = static_cast(std::pow(static_cast(NumberOfCells), 1.00 / Dimension)); diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index 29e0ace66e8f..cc4dc9001255 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -18,6 +18,7 @@ // External includes // Project includes +#include "includes/parallel_environment.h" #include "geometries/bounding_box.h" #include "geometries/point.h" #include "spatial_containers/spatial_search_result.h" @@ -217,7 +218,6 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins } } - /** * @brief This method takes a point and finds the nearest object to it in a given radius. * @details If there are more than one object in the same minimum distance only one is returned @@ -232,6 +232,21 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins const double Radius ); + /** + * @brief This method takes a point and finds the nearest object to it in a given radius. + * @details If there are more than one object in the same minimum distance only one is returned + * If there are no objects in that radius the result will be set to not found. + * Result contains a flag is the object has been found or not. + * @param rPoint The point to be checked + * @param Radius The radius to be checked + * @param rResults The results of the search + */ + void SearchNearestInRadius( + const Point& rPoint, + const double Radius, + ResultTypeContainer& rResults + ); + /** * @brief This method takes a point and finds the nearest object to it in a given radius (iterative version). * @details If there are more than one object in the same minimum distance only one is returned @@ -291,10 +306,12 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); auto result = SearchNearestInRadius(*it_point, Radius); r_point_result.AddResult(result); + + // Synchronize + r_point_result.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); } } - /** * @brief This method takes a point and finds the nearest object to it. * @details If there are more than one object in the same minimum distance only one is returned @@ -304,6 +321,18 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins */ ResultType SearchNearest(const Point& rPoint); + /** + * @brief This method takes a point and finds the nearest object to it. + * @details If there are more than one object in the same minimum distance only one is returned + * Result contains a flag is the object has been found or not. + * @param rPoint The point to be checked + * @param rResults The results of the search + */ + void SearchNearest( + const Point& rPoint, + ResultTypeContainer& rResults + ); + /** * @brief This method takes a point and finds the nearest object to it (iterative version). * @details If there are more than one object in the same minimum distance only one is returned @@ -357,6 +386,9 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); auto result = SearchNearest(*it_point); r_point_result.AddResult(result); + + // Synchronize + r_point_result.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); } } @@ -371,6 +403,20 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins */ ResultType SearchIsInside(const Point& rPoint); + /** + * @brief This method takes a point and search if it's inside an geometrical object of the domain. + * @details If it is inside an object, it returns it, and search distance is set to zero. + * If there is no object, the result will be set to not found. + * Result contains a flag is the object has been found or not. + * This method is a simplified and faster method of SearchNearest. + * @param rPoint The point to be checked + * @param rResults The results of the search + */ + void SearchIsInside( + const Point& rPoint, + ResultTypeContainer& rResults + ); + /** * @brief This method takes a point and search if it's inside an geometrical object of the domain (iterative version). * @details If it is inside an object, it returns it, and search distance is set to zero. @@ -428,6 +474,9 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins auto& r_point_result = rResults.InitializeResult(it_point->Coordinates()); auto result = SearchIsInside(*it_point); r_point_result.AddResult(result); + + // Synchronize + r_point_result.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); } } From e3cd60bdd71b89674531476617ba1de7198ba13d Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 1 Jun 2023 18:04:03 +0200 Subject: [PATCH 047/145] Update test --- .../spatial_containers/test_spatial_search_result_container.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 7b9ebccc634f..bec80ec4d4f6 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -48,6 +48,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS KRATOS_CHECK_EQUAL(r_distances[1], 0.5); // Check global pointers + KRATOS_CHECK_IS_FALSE(container.IsObjectFound()); auto& r_global_pointers = container.GetGlobalPointers(); KRATOS_CHECK_EQUAL(r_global_pointers.size(), 0); // It should be empty as we have not synchronized KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); // It should be empty as we have not synchronized @@ -97,6 +98,7 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCore KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); // Check global pointers + KRATOS_CHECK(container.IsObjectFound()); auto& r_global_pointers = container.GetGlobalPointers(); KRATOS_CHECK_EQUAL(r_global_pointers.size(), 1); KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); From 72fc5d8ae2bd52893849c9929b0a6c19d0bb7261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Thu, 1 Jun 2023 18:08:10 +0200 Subject: [PATCH 048/145] More test update --- kratos/tests/test_geometrical_object_bins.py | 42 +++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index 2828e9df5bd0..891659c52649 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -1,6 +1,10 @@ # Importing the Kratos Library import KratosMultiphysics as KM +# Importing the MPI +if KM.IsDistributedRun(): + import KratosMultiphysics.mpi as KratosMPI + # Import KratosUnittest import KratosMultiphysics.KratosUnittest as KratosUnittest @@ -49,16 +53,16 @@ def setUp(self): self.search = KM.GeometricalObjectsBins(self.model_part.Conditions) # Create node for search + self.node_coordinates = KM.Point(0.0, 0.0, 0.15) if KM.IsDistributedRun(): - import KratosMultiphysics.mpi as KratosMPI # Only added to first rank to actualy check it works in all ranks if self.data_comm.Rank() == 0: - self.node = self.sub_model_part.CreateNewNode(100000, 0.0, 0.0, 0.15) + self.node = self.sub_model_part.CreateNewNode(100000, self.node_coordinates.X, self.node_coordinates.Y, self.node_coordinates.Z) self.node.SetSolutionStepValue(KM.PARTITION_INDEX, 0) ParallelFillCommunicator = KratosMPI.ParallelFillCommunicator(self.model_part) ParallelFillCommunicator.Execute() else: - self.node = self.sub_model_part.CreateNewNode(100000, 0.0, 0.0, 0.15) + self.node = self.sub_model_part.CreateNewNode(100000, self.node_coordinates.X, self.node_coordinates.Y, self.node_coordinates.Z) def test_GeometricalObjectsBins_SearchInRadius(self): # Define radius @@ -68,8 +72,7 @@ def test_GeometricalObjectsBins_SearchInRadius(self): cond_id_ref = [125,78,117,18,68,1,41,119] # One node search - results = self.search.SearchInRadius(self.node, radius) - results.SynchronizeAll(self.data_comm) + results = self.search.SearchInRadius(self.node_coordinates, radius) self.assertEqual(results.NumberOfGlobalResults(), 8) ids = results.GetResultIndices() for id in ids: @@ -78,7 +81,7 @@ def test_GeometricalObjectsBins_SearchInRadius(self): # Nodes array search results = self.search.SearchInRadius(self.sub_model_part.Nodes, radius) self.assertEqual(results.NumberOfPointsResults(), 1) - node_results = results[self.node] + node_results = results[self.node_coordinates] node_results.SynchronizeAll(self.data_comm) self.assertEqual(node_results.NumberOfGlobalResults(), 8) ids = node_results.GetResultIndices() @@ -89,14 +92,15 @@ def test_GeometricalObjectsBins_SearchNearestInRadius(self): radius = 0.35 # One node search - result = self.search.SearchNearestInRadius(self.node, radius) - self.assertEqual(result.Get().Id, 1) + result = self.search.SearchNearestInRadius(self.node_coordinates, radius) + self.assertEqual(result.NumberOfGlobalResults(), 1) + ids = result.GetResultIndices() + self.assertTrue(1 in ids) # Nodes array search results = self.search.SearchNearestInRadius(self.sub_model_part.Nodes, radius) self.assertEqual(results.NumberOfPointsResults(), 1) - node_results = results[self.node] - node_results.SynchronizeAll(self.data_comm) + node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) if self.data_comm.Rank() == 0: self.assertEqual(node_results[0].Id, 1) # Local result @@ -104,14 +108,15 @@ def test_GeometricalObjectsBins_SearchNearestInRadius(self): def test_GeometricalObjectsBins_SearchNearest(self): # One node search - result = self.search.SearchNearest(self.node) - self.assertEqual(result.Get().Id, 1) + result = self.search.SearchNearest(self.node_coordinates) + self.assertEqual(result.NumberOfGlobalResults(), 1) + ids = result.GetResultIndices() + self.assertTrue(1 in ids) # Nodes array search results = self.search.SearchNearest(self.sub_model_part.Nodes) self.assertEqual(results.NumberOfPointsResults(), 1) - node_results = results[self.node] - node_results.SynchronizeAll(self.data_comm) + node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) if self.data_comm.Rank() == 0: self.assertEqual(node_results[0].Id, 1) # Local result @@ -119,15 +124,14 @@ def test_GeometricalObjectsBins_SearchNearest(self): def test_GeometricalObjectsBins_SearchIsInside(self): # One node search - result = self.search.SearchIsInside(self.node) - self.assertEqual(result.Get(), None) + result = self.search.SearchIsInside(self.node_coordinates) + self.assertFalse(result.IsObjectFound()) # Nodes array search results = self.search.SearchIsInside(self.sub_model_part.Nodes) self.assertEqual(results.NumberOfPointsResults(), 1) - node_results = results[self.node] - node_results.SynchronizeAll(self.data_comm) - self.assertEqual(node_results.NumberOfGlobalResults(), 0) + node_results = results[self.node_coordinates] + self.assertFalse(node_results.IsObjectFound()) if __name__ == '__main__': KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) From 9b95a0ef1b06c06d3021120414233985bfd6731f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Thu, 1 Jun 2023 20:20:52 +0200 Subject: [PATCH 049/145] Adding virtual --- kratos/spatial_containers/geometrical_objects_bins.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index cc4dc9001255..7732da13904a 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -157,7 +157,7 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param Radius The radius to be checked * @param rResults The results of the search */ - void SearchInRadius( + virtual void SearchInRadius( const Point& rPoint, const double Radius, ResultTypeContainer& rResults @@ -241,7 +241,7 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param Radius The radius to be checked * @param rResults The results of the search */ - void SearchNearestInRadius( + virtual void SearchNearestInRadius( const Point& rPoint, const double Radius, ResultTypeContainer& rResults @@ -328,7 +328,7 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param rPoint The point to be checked * @param rResults The results of the search */ - void SearchNearest( + virtual void SearchNearest( const Point& rPoint, ResultTypeContainer& rResults ); @@ -412,7 +412,7 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param rPoint The point to be checked * @param rResults The results of the search */ - void SearchIsInside( + virtual void SearchIsInside( const Point& rPoint, ResultTypeContainer& rResults ); From 4683903b491f343349fc3b820b4c96bf2a333ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Fri, 2 Jun 2023 10:01:32 +0200 Subject: [PATCH 050/145] Extend test --- .../test_geometrical_objects_bins.cpp | 280 ++++++++++++++++-- 1 file changed, 262 insertions(+), 18 deletions(-) diff --git a/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp b/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp index fd2f9e888816..e425b01e62cb 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp @@ -22,9 +22,16 @@ #include "containers/model.h" #include "geometries/triangle_3d_3.h" -namespace Kratos::Testing { +namespace Kratos::Testing +{ -ModelPart& CreateCubeSkinModelPart(Model& rCurrentModel, const double HalfX, const double HalfY, const double HalfZ){ +ModelPart& CreateCubeSkinModelPart( + Model& rCurrentModel, + const double HalfX = 0.6, + const double HalfY = 0.9, + const double HalfZ = 0.3 + ) +{ // Generate the cube skin ModelPart& r_skin_part = rCurrentModel.CreateModelPart("Skin"); r_skin_part.CreateNewNode(1, -HalfX, -HalfY, -HalfZ); @@ -52,6 +59,34 @@ ModelPart& CreateCubeSkinModelPart(Model& rCurrentModel, const double HalfX, con return r_skin_part; } +ModelPart& CreateCubeModelPart(Model& rCurrentModel) +{ + // Generate the cube skin + ModelPart& r_model_part = rCurrentModel.CreateModelPart("Cube"); + + // Create properties + auto p_properties = r_model_part.CreateNewProperties(1, 0); + + r_model_part.CreateNewNode(1 , 0.0 , 1.0 , 1.0); + r_model_part.CreateNewNode(2 , 0.0 , 1.0 , 0.0); + r_model_part.CreateNewNode(3 , 0.0 , 0.0 , 1.0); + r_model_part.CreateNewNode(4 , 0.5 , 1.0 , 1.0); + r_model_part.CreateNewNode(5 , 0.0 , 0.0 , 0.0); + r_model_part.CreateNewNode(6 , 0.5 , 1.0 , 0.0); + r_model_part.CreateNewNode(7 , 0.5 , 0.0 , 1.0); + r_model_part.CreateNewNode(8 , 0.5 , 0.0 , 0.0); + r_model_part.CreateNewNode(9 , 1.0 , 1.0 , 1.0); + r_model_part.CreateNewNode(10 , 1.0 , 1.0 , 0.0); + r_model_part.CreateNewNode(11 , 1.0 , 0.0 , 1.0); + r_model_part.CreateNewNode(12 , 1.0 , 0.0 , 0.0); + + // Create elements + r_model_part.CreateNewElement("Element3D8N", 1, {{5,8,6,2,3,7,4,1}}, p_properties); + r_model_part.CreateNewElement("Element3D8N", 2, {{8,12,10,6,7,11,9,4}}, p_properties); + + return r_model_part; +} + /** Checks bins bounding box */ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsBoundingBox, KratosFastSuite) @@ -114,12 +149,8 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsAddObjectsToCells, KratosFastSui { Model current_model; - const double cube_x = 0.6; - const double cube_y = 0.9; - const double cube_z = 0.3; - // Generate the cube skin - ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, cube_x, cube_y, cube_z); + ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model); GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); @@ -144,12 +175,8 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchInRadius, KratosFastSuite) { Model current_model; - const double cube_x = 0.6; - const double cube_y = 0.9; - const double cube_z = 0.3; - // Generate the cube skin - ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, cube_x, cube_y, cube_z); + ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model); GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); @@ -175,6 +202,62 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchInRadius, KratosFastSuite) KRATOS_CHECK_EQUAL(results.size(), 12); } +/** Checks bins search in radius +*/ +KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchInRadiusContainer, KratosFastSuite) +{ + Model current_model; + + // Generate the cube skin + ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model); + + GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); + + // Generate new model part + ModelPart& r_point_model_part = current_model.CreateModelPart("PointModelPart"); + Point point(0.0, 0.0, 0.0); + r_point_model_part.CreateNewNode(1, point.X(), point.Y(), point.Z()); + auto& r_array_nodes = r_point_model_part.Nodes(); + + GeometricalObjectsBins::ResultTypeContainerMap results; + + // 0.29 radius + bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.29, results); + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_IS_FALSE(results[point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 0); + + // 0.3 radius + bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.3, results); + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 4); + + // 0.4 radius + bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.4, results); + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 4); + + // 0.6 radius + bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.6, results); + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 8); + + // 0.7 radius + bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.7, results); + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 8); + + // 0.9 radius + bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.9, results); + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 12); +} + /** Checks bins search nearest */ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestInRadius, KratosFastSuite) @@ -183,12 +266,11 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestInRadius, KratosFas Model current_model; - const double cube_x = 0.6; - const double cube_y = 0.9; + // Cube coordinates const double cube_z = 0.3; // Generate the cube skin - ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, cube_x, cube_y, cube_z); + ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, 0.6, 0.9, cube_z); GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); @@ -205,6 +287,52 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestInRadius, KratosFas KRATOS_CHECK(id == 3); } +/** Checks bins search nearest +*/ +KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestInRadiusContainer, KratosFastSuite) +{ + constexpr double tolerance = 1e-12; + + Model current_model; + + // Cube coordinates + const double cube_z = 0.3; + + // Generate the cube skin + ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, 0.6, 0.9, cube_z); + + GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); + + double epsilon = 1.0e-6; + Point near_point{epsilon,epsilon,epsilon}; + + // Generate new model part + ModelPart& r_point_model_part = current_model.CreateModelPart("PointModelPart"); + r_point_model_part.CreateNewNode(1, epsilon,epsilon,epsilon); + auto& r_array_nodes = r_point_model_part.Nodes(); + + GeometricalObjectsBins::ResultTypeContainerMap results; + bins.SearchNearestInRadius(r_array_nodes.begin(), r_array_nodes.end(), cube_z - 1.e-4, results); + + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_IS_FALSE(results[near_point].IsObjectFound()); + + bins.SearchNearestInRadius(r_array_nodes.begin(), r_array_nodes.end(), cube_z + 1.e-4, results); + + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[near_point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[near_point].NumberOfGlobalResults(), 1); + + // Distances + auto& r_distances = results[near_point].GetLocalDistances(); + KRATOS_CHECK_NEAR(r_distances.begin()->second, (cube_z - epsilon), tolerance); + + // Compute indices + auto indices = results[near_point].GetResultIndices(); + const std::size_t id = indices[0]; + KRATOS_CHECK_EQUAL(id, 3); +} + /** Checks bins search nearest */ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearest, KratosFastSuite) @@ -213,12 +341,11 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearest, KratosFastSuite) Model current_model; - const double cube_x = 0.6; - const double cube_y = 0.9; + // Cube coordinates const double cube_z = 0.3; // Generate the cube skin - ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, cube_x, cube_y, cube_z); + ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, 0.6, 0.9, cube_z); GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); @@ -232,6 +359,47 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearest, KratosFastSuite) KRATOS_CHECK(id == 3); } +/** Checks bins search nearest +*/ +KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestContainer, KratosFastSuite) +{ + constexpr double tolerance = 1e-12; + + Model current_model; + + // Cube coordinates + const double cube_z = 0.3; + + // Generate the cube skin + ModelPart& r_skin_part = CreateCubeSkinModelPart(current_model, 0.6, 0.9, cube_z); + + GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); + + double epsilon = 1.0e-6; + Point near_point{epsilon,epsilon,epsilon}; + + // Generate new model part + ModelPart& r_point_model_part = current_model.CreateModelPart("PointModelPart"); + r_point_model_part.CreateNewNode(1, epsilon,epsilon,epsilon); + auto& r_array_nodes = r_point_model_part.Nodes(); + + GeometricalObjectsBins::ResultTypeContainerMap results; + bins.SearchNearest(r_array_nodes.begin(), r_array_nodes.end(), results); + + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[near_point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[near_point].NumberOfGlobalResults(), 1); + + // Distances + auto& r_distances = results[near_point].GetLocalDistances(); + KRATOS_CHECK_NEAR(r_distances.begin()->second, (cube_z - epsilon), tolerance); + + // Compute indices + auto indices = results[near_point].GetResultIndices(); + const std::size_t id = indices[0]; + KRATOS_CHECK_EQUAL(id, 3); +} + /** Checks bins empty search nearest */ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsEmptySearchNearest, KratosFastSuite) @@ -249,6 +417,31 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsEmptySearchNearest, KratosFastSu KRATOS_CHECK_IS_FALSE(result.IsObjectFound()); } +/** Checks bins empty search nearest +*/ +KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsEmptySearchNearestContainer, KratosFastSuite) +{ + Model current_model; + + // Generate the cube skin + ModelPart& r_skin_part = current_model.CreateModelPart("Skin"); + + GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); + + Point point{0.0,0.0,0.0}; + + // Generate new model part + ModelPart& r_point_model_part = current_model.CreateModelPart("PointModelPart"); + r_point_model_part.CreateNewNode(1, 0.0,0.0,0.0); + auto& r_array_nodes = r_point_model_part.Nodes(); + + GeometricalObjectsBins::ResultTypeContainerMap results; + bins.SearchNearest(r_array_nodes.begin(), r_array_nodes.end(), results); + + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_IS_FALSE(results[point].IsObjectFound()); +} + /** Checks bins search is inside */ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsInside, KratosFastSuite) @@ -275,6 +468,32 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsInside, KratosFastSuite) KRATOS_CHECK_NEAR(result.GetDistance(), 0.0, tolerance); } +/** Checks bins search is inside +*/ +KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsInsideContainer, KratosFastSuite) +{ + Model current_model; + + // Generate the cube skin + ModelPart& r_skin_part = CreateCubeModelPart(current_model); + + GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); + + Point inside_point{0.5,0.5,0.5}; + + // Generate new model part + ModelPart& r_point_model_part = current_model.CreateModelPart("PointModelPart"); + r_point_model_part.CreateNewNode(1, 0.5,0.5,0.5); + auto& r_array_nodes = r_point_model_part.Nodes(); + + GeometricalObjectsBins::ResultTypeContainerMap results; + bins.SearchIsInside(r_array_nodes.begin(), r_array_nodes.end(), results); + + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK(results[inside_point].IsObjectFound()); + KRATOS_CHECK_EQUAL(results[inside_point].NumberOfGlobalResults(), 1); +} + /** Checks bins search is inside = not found */ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsNotInside, KratosFastSuite) @@ -298,4 +517,29 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsNotInside, KratosFastSui KRATOS_CHECK_IS_FALSE(result.IsObjectFound()); } +/** Checks bins search is inside = not found +*/ +KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsNotInsideContainer, KratosFastSuite) +{ + Model current_model; + + // Generate the cube skin + ModelPart& r_skin_part = CreateCubeModelPart(current_model); + + GeometricalObjectsBins bins(r_skin_part.ElementsBegin(), r_skin_part.ElementsEnd()); + + Point outside_point{100.0,100.0,100.0}; + + // Generate new model part + ModelPart& r_point_model_part = current_model.CreateModelPart("PointModelPart"); + r_point_model_part.CreateNewNode(1, 100.0,100.0,100.0); + auto& r_array_nodes = r_point_model_part.Nodes(); + + GeometricalObjectsBins::ResultTypeContainerMap results; + bins.SearchIsInside(r_array_nodes.begin(), r_array_nodes.end(), results); + + KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_IS_FALSE(results[outside_point].IsObjectFound()); +} + } // namespace Kratos::Testing. From 7a84b90a3b1138e58471e20f52a02450e666ba17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Fri, 2 Jun 2023 10:15:26 +0200 Subject: [PATCH 051/145] Update check --- kratos/tests/test_geometrical_object_bins.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index 891659c52649..a1497f201054 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -102,7 +102,7 @@ def test_GeometricalObjectsBins_SearchNearestInRadius(self): self.assertEqual(results.NumberOfPointsResults(), 1) node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) - if self.data_comm.Rank() == 0: + if node_results.NumberOfLocalResults() == 1: self.assertEqual(node_results[0].Id, 1) # Local result self.assertEqual(node_results(0).Id, 1) # Global result @@ -118,7 +118,7 @@ def test_GeometricalObjectsBins_SearchNearest(self): self.assertEqual(results.NumberOfPointsResults(), 1) node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) - if self.data_comm.Rank() == 0: + if node_results.NumberOfLocalResults() == 1: self.assertEqual(node_results[0].Id, 1) # Local result self.assertEqual(node_results(0).Id, 1) # Global result From 47517bbe3a036299d456a08849565ee74a92cb2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Fri, 2 Jun 2023 10:20:50 +0200 Subject: [PATCH 052/145] Update checks --- kratos/tests/test_geometrical_object_bins.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index a1497f201054..9ba0b4f9d8e3 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -85,6 +85,7 @@ def test_GeometricalObjectsBins_SearchInRadius(self): node_results.SynchronizeAll(self.data_comm) self.assertEqual(node_results.NumberOfGlobalResults(), 8) ids = node_results.GetResultIndices() + self.assertEqual(len(ids), 8) for id in ids: self.assertTrue(id in cond_id_ref) @@ -102,9 +103,13 @@ def test_GeometricalObjectsBins_SearchNearestInRadius(self): self.assertEqual(results.NumberOfPointsResults(), 1) node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) + # Local result if node_results.NumberOfLocalResults() == 1: - self.assertEqual(node_results[0].Id, 1) # Local result - self.assertEqual(node_results(0).Id, 1) # Global result + self.assertEqual(node_results[0].Id, 1) + # Global result + ids = node_results.GetResultIndices() + self.assertEqual(len(ids), 1) + self.assertTrue(1 in ids) def test_GeometricalObjectsBins_SearchNearest(self): # One node search @@ -118,9 +123,13 @@ def test_GeometricalObjectsBins_SearchNearest(self): self.assertEqual(results.NumberOfPointsResults(), 1) node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) + # Local result if node_results.NumberOfLocalResults() == 1: - self.assertEqual(node_results[0].Id, 1) # Local result - self.assertEqual(node_results(0).Id, 1) # Global result + self.assertEqual(node_results[0].Id, 1) + # Global result + ids = node_results.GetResultIndices() + self.assertEqual(len(ids), 1) + self.assertTrue(1 in ids) def test_GeometricalObjectsBins_SearchIsInside(self): # One node search From 442cf143803375c48e6604fabde4fb7a7792e082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Fri, 2 Jun 2023 16:28:45 +0200 Subject: [PATCH 053/145] Adding local BB check --- .../geometrical_objects_bins.cpp | 36 +++++++++++++++++++ .../geometrical_objects_bins.h | 20 ++++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index 6ff21df76a86..fb329f84db60 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -240,6 +240,42 @@ void GeometricalObjectsBins::SearchIsInside( /***********************************************************************************/ /***********************************************************************************/ +bool GeometricalObjectsBins::PointIsInsideBoundingBox(const array_1d& rCoords) +{ + // Get the bounding box points + const auto& r_max_point = mBoundingBox.GetMaxPoint(); + const auto& r_min_point = mBoundingBox.GetMinPoint(); + + // The Bounding Box should have some tolerance already! + if (rCoords[0] < r_max_point[0] && rCoords[0] > r_min_point[1]) // check x-direction + if (rCoords[1] < r_max_point[2] && rCoords[1] > r_min_point[3]) // check y-direction + if (rCoords[2] < r_max_point[4] && rCoords[2] > r_min_point[5]) // check z-direction + return true; + return false; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +bool GeometricalObjectsBins::PointIsInsideBoundingBoxWithTolerance( + const array_1d& rCoords, + const double Tolerance + ) +{ + // Create the coords with tolerance + array_1d coords_with_tolerance(rCoords); + + // Apply Tolerances + for (IndexType i=0; i<3; ++i) { + coords_with_tolerance[2 * i ] += Tolerance; + coords_with_tolerance[2 * i + 1] -= Tolerance; + } + return PointIsInsideBoundingBox(coords_with_tolerance); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void GeometricalObjectsBins::CalculateCellSize(const std::size_t NumberOfCells) { const std::size_t avarage_number_of_cells = static_cast(std::pow(static_cast(NumberOfCells), 1.00 / Dimension)); diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index 7732da13904a..3ac51504396d 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -568,15 +568,27 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins array_1d mInverseOfCellSize; /// The inverse of the size of each cell in each direction std::vector mCells; /// The cells of the domain - ///@} ///@name Protected Operations ///@{ + /** + * @brief This method checks if a point is inside any bounding box of the global bounding boxes + * @param rCoords The coordinates of the point + * @return True if the point is inside the bounding box + */ + bool PointIsInsideBoundingBox(const array_1d& rCoords); - ///@} - ///@name Private Operations - ///@{ + /** + * @brief This method checks if a point is inside any bounding box of the global bounding boxes considering a certain tolerance + * @param rCoords The coordinates of the point + * @param Tolerance The tolerance + * @return True if the point is inside the bounding box + */ + bool PointIsInsideBoundingBoxWithTolerance( + const array_1d& rCoords, + const double Tolerance + ); /** * @brief Calculate the cell sizes to be as equilateral as possible and tries to approximate (roughly) the given number of cells From ca61a70e8d7a5b288a0f8a5a5851dff7765b120e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Fri, 2 Jun 2023 17:02:34 +0200 Subject: [PATCH 054/145] Minor fix --- .../geometrical_objects_bins.cpp | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index fb329f84db60..efd30d81cdeb 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -245,11 +245,11 @@ bool GeometricalObjectsBins::PointIsInsideBoundingBox(const array_1d& // Get the bounding box points const auto& r_max_point = mBoundingBox.GetMaxPoint(); const auto& r_min_point = mBoundingBox.GetMinPoint(); - - // The Bounding Box should have some tolerance already! - if (rCoords[0] < r_max_point[0] && rCoords[0] > r_min_point[1]) // check x-direction - if (rCoords[1] < r_max_point[2] && rCoords[1] > r_min_point[3]) // check y-direction - if (rCoords[2] < r_max_point[4] && rCoords[2] > r_min_point[5]) // check z-direction + + // The Bounding Box check + if (rCoords[0] < r_max_point[0] && rCoords[0] > r_min_point[0]) // check x-direction + if (rCoords[1] < r_max_point[1] && rCoords[1] > r_min_point[1]) // check y-direction + if (rCoords[2] < r_max_point[2] && rCoords[2] > r_min_point[2]) // check z-direction return true; return false; } @@ -262,15 +262,22 @@ bool GeometricalObjectsBins::PointIsInsideBoundingBoxWithTolerance( const double Tolerance ) { - // Create the coords with tolerance - array_1d coords_with_tolerance(rCoords); + // Get the bounding box points + auto max_point = mBoundingBox.GetMaxPoint(); + auto min_point = mBoundingBox.GetMinPoint(); // Apply Tolerances - for (IndexType i=0; i<3; ++i) { - coords_with_tolerance[2 * i ] += Tolerance; - coords_with_tolerance[2 * i + 1] -= Tolerance; + for (unsigned int i=0; i<3; ++i) { + max_point[i] += Tolerance; + min_point[i] -= Tolerance; } - return PointIsInsideBoundingBox(coords_with_tolerance); + + // The Bounding Box check + if (rCoords[0] < max_point[0] && rCoords[0] > min_point[0]) // check x-direction + if (rCoords[1] < max_point[1] && rCoords[1] > min_point[1]) // check y-direction + if (rCoords[2] < max_point[2] && rCoords[2] > min_point[2]) // check z-direction + return true; + return false; } /***********************************************************************************/ From 465b240f83b1cc323f51a93721567fb248d28d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Fri, 2 Jun 2023 17:02:52 +0200 Subject: [PATCH 055/145] Update constructor of ParallelFillCommunciator --- kratos/tests/test_geometrical_object_bins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index 9ba0b4f9d8e3..5842c837d049 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -59,7 +59,7 @@ def setUp(self): if self.data_comm.Rank() == 0: self.node = self.sub_model_part.CreateNewNode(100000, self.node_coordinates.X, self.node_coordinates.Y, self.node_coordinates.Z) self.node.SetSolutionStepValue(KM.PARTITION_INDEX, 0) - ParallelFillCommunicator = KratosMPI.ParallelFillCommunicator(self.model_part) + ParallelFillCommunicator = KratosMPI.ParallelFillCommunicator(self.model_part, self.data_comm) ParallelFillCommunicator.Execute() else: self.node = self.sub_model_part.CreateNewNode(100000, self.node_coordinates.X, self.node_coordinates.Y, self.node_coordinates.Z) From 64737581ec4f55430d39bc1f6729f274ed811fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Fri, 2 Jun 2023 17:46:49 +0200 Subject: [PATCH 056/145] Adding flag --- .../geometrical_objects_bins.cpp | 36 ++++++++++++------- .../geometrical_objects_bins.h | 16 ++++++--- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index efd30d81cdeb..e4b8d4b96f51 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -101,7 +101,8 @@ void GeometricalObjectsBins::SearchInRadius( void GeometricalObjectsBins::SearchInRadius( const Point& rPoint, const double Radius, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults ) { // Search @@ -111,8 +112,10 @@ void GeometricalObjectsBins::SearchInRadius( rResults.AddResult(r_result); } - // Synchronize - rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + } } /***********************************************************************************/ @@ -162,15 +165,18 @@ GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchNearestInRadius void GeometricalObjectsBins::SearchNearestInRadius( const Point& rPoint, const double Radius, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults ) { // Search auto result = SearchNearestInRadius(rPoint, Radius); rResults.AddResult(result); - // Synchronize - rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + } } /***********************************************************************************/ @@ -191,15 +197,18 @@ GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchNearest(const P void GeometricalObjectsBins::SearchNearest( const Point& rPoint, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults ) { // Search auto result = SearchNearest(rPoint); rResults.AddResult(result); - // Synchronize - rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + } } /***********************************************************************************/ @@ -226,15 +235,18 @@ GeometricalObjectsBins::ResultType GeometricalObjectsBins::SearchIsInside(const void GeometricalObjectsBins::SearchIsInside( const Point& rPoint, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults ) { // Search auto result = SearchIsInside(rPoint); rResults.AddResult(result); - // Synchronize - rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(ParallelEnvironment::GetDefaultDataCommunicator()); + } } /***********************************************************************************/ diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index 3ac51504396d..fd1f8afe4e70 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -156,11 +156,13 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param rPoint The point to be checked * @param Radius The radius to be checked * @param rResults The results of the search + * @param SyncronizeResults If the results should be synchronized or not */ virtual void SearchInRadius( const Point& rPoint, const double Radius, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults = true ); /** @@ -240,11 +242,13 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * @param rPoint The point to be checked * @param Radius The radius to be checked * @param rResults The results of the search + * @param SyncronizeResults If the results should be synchronized or not */ virtual void SearchNearestInRadius( const Point& rPoint, const double Radius, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults = true ); /** @@ -327,10 +331,12 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * Result contains a flag is the object has been found or not. * @param rPoint The point to be checked * @param rResults The results of the search + * @param SyncronizeResults If the results should be synchronized or not */ virtual void SearchNearest( const Point& rPoint, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults = true ); /** @@ -411,10 +417,12 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins * This method is a simplified and faster method of SearchNearest. * @param rPoint The point to be checked * @param rResults The results of the search + * @param SyncronizeResults If the results should be synchronized or not */ virtual void SearchIsInside( const Point& rPoint, - ResultTypeContainer& rResults + ResultTypeContainer& rResults, + const bool SyncronizeResults = true ); /** From 8130d62890341177fe3e2383ba866d63fe5060bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Sat, 3 Jun 2023 01:21:44 +0200 Subject: [PATCH 057/145] Adding `SynchronizeAll` to `SpatialSearchResultContainerMap` --- .../spatial_search_result_container.cpp | 12 ++++++++++++ .../spatial_search_result_container.h | 19 +++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index 1c75156a17af..e08375f32249 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -271,6 +271,18 @@ void SpatialSearchResultContainerMap::Clear() /***********************************************************************************/ /***********************************************************************************/ +template +void SpatialSearchResultContainerMap::SynchronizeAll(const DataCommunicator& rDataCommunicator) +{ + // Synchronize all the results + for (auto& r_point_result : mPointResults) { + r_point_result.second.SynchronizeAll(rDataCommunicator); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + template typename SpatialSearchResultContainerMap::HashType SpatialSearchResultContainerMap::Hash(const array_1d& rCoordinates) const { diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index c0c0a6cf2f5d..5eb7188dfd8f 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -755,7 +755,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap * @brief Returns an iterator pointing to the beginning of the container. * @return An iterator pointing to the beginning of the container. */ - iterator begin() { + iterator begin() + { return iterator(mPointResults.begin()); } @@ -763,7 +764,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap * @brief Returns an iterator pointing to the end of the container. * @return An iterator pointing to the end of the container. */ - iterator end() { + iterator end() + { return iterator(mPointResults.end()); } @@ -771,7 +773,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap * @brief Returns a constant iterator pointing to the beginning of the container. * @return A constant iterator pointing to the beginning of the container. */ - const_iterator begin() const { + const_iterator begin() const + { return const_iterator(mPointResults.begin()); } @@ -779,7 +782,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap * @brief Returns a constant iterator pointing to the end of the container. * @return A constant iterator pointing to the end of the container. */ - const_iterator end() const { + const_iterator end() const + { return const_iterator(mPointResults.end()); } @@ -811,6 +815,13 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap */ void Clear(); + /** + * @brief Synchronize all container between partitions + * @details This method synchronizes all the container between partitions + * @param rDataCommunicator The data communicator + */ + void SynchronizeAll(const DataCommunicator& rDataCommunicator); + ///@} ///@name Input and output ///@{ From 0c8821491f95f3113ec4b6ac1f5a581da4e98078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Sat, 3 Jun 2023 01:46:14 +0200 Subject: [PATCH 058/145] Fix tolerance --- kratos/spatial_containers/geometrical_objects_bins.cpp | 9 ++++++--- kratos/spatial_containers/geometrical_objects_bins.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index e4b8d4b96f51..c46447b5d7a2 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -279,9 +279,12 @@ bool GeometricalObjectsBins::PointIsInsideBoundingBoxWithTolerance( auto min_point = mBoundingBox.GetMinPoint(); // Apply Tolerances - for (unsigned int i=0; i<3; ++i) { - max_point[i] += Tolerance; - min_point[i] -= Tolerance; + const double epsilon = std::numeric_limits::epsilon(); + if (norm_2(max_point) > epsilon && norm_2(min_point) > epsilon) { + for (unsigned int i=0; i<3; ++i) { + max_point[i] += Tolerance; + min_point[i] -= Tolerance; + } } // The Bounding Box check diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index fd1f8afe4e70..1b8d3709df23 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -81,8 +81,8 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins for (TIteratorType i_object = GeometricalObjectsBegin ; i_object != GeometricalObjectsEnd ; i_object++){ mBoundingBox.Extend(i_object->GetGeometry().begin() , i_object->GetGeometry().end()); } + mBoundingBox.Extend(Tolerance); } - mBoundingBox.Extend(Tolerance); CalculateCellSize(number_of_objects); mCells.resize(GetTotalNumberOfCells()); AddObjectsToCells(GeometricalObjectsBegin, GeometricalObjectsEnd); From 194b9e74d7d11795302ad6bd9e300d60a5b3f9d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Sat, 3 Jun 2023 01:47:31 +0200 Subject: [PATCH 059/145] Minor comment --- kratos/spatial_containers/geometrical_objects_bins.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index c46447b5d7a2..525660ad9da4 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -278,7 +278,7 @@ bool GeometricalObjectsBins::PointIsInsideBoundingBoxWithTolerance( auto max_point = mBoundingBox.GetMaxPoint(); auto min_point = mBoundingBox.GetMinPoint(); - // Apply Tolerances + // Apply Tolerances (only in non zero BB cases) const double epsilon = std::numeric_limits::epsilon(); if (norm_2(max_point) > epsilon && norm_2(min_point) > epsilon) { for (unsigned int i=0; i<3; ++i) { From 7310310bf6cb4531c29f0db235df50d2e181c0be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Mon, 5 Jun 2023 15:44:28 +0200 Subject: [PATCH 060/145] Adding new method to `AddResult` --- .../spatial_search_result_container.cpp | 12 ++++++++++++ .../spatial_search_result_container.h | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index e08375f32249..d52abdb7558f 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -51,6 +51,18 @@ void SpatialSearchResultContainer::AddResult(SpatialSearchResult +void SpatialSearchResultContainer::AddResult(TObjectType* pResult) +{ + // Check if the object has been found (not nullptr) + if (pResult != nullptr) { + mLocalPointers.push_back(pResult); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + template void SpatialSearchResultContainer::Clear() { diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 5eb7188dfd8f..5e28a5c62a39 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -356,6 +356,21 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer AddResult(rResult); } + /** + * @brief Add a result to the container + * @param pResult The result to be added + */ + void AddResult(TObjectType* pResult); + + /** + * @brief Pushes back a result to the container + * @param pResult The result to be added + */ + void push_back(TObjectType* pResult) + { + AddResult(pResult); + } + /** * @brief Clear the containers * @details This method clears the containers From b7a66e05c34089a944a592a7af68c3eff54125c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Mon, 5 Jun 2023 15:44:55 +0200 Subject: [PATCH 061/145] Instantioate for `Node` too --- .../add_search_strategies_to_python.cpp | 223 +++++++++++------- .../spatial_search_result_container.cpp | 44 +++- 2 files changed, 167 insertions(+), 100 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 1ad9ee732104..f2d7cc4567a8 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -29,6 +29,42 @@ namespace Kratos::Python { +/** + * @brief Converts a vector to a Python list using pybind11. + * @details This function is generic enough to be moved to a more general place. + * @tparam T The type of the vector. + * @param results The vector to convert. + * @return The converted Python list. + */ +template +pybind11::list VectorToPyList(const T& results) { + pybind11::list list_results; + for (auto& r_result : results) { + list_results.append(r_result); + } + return list_results; +} + +/** + * @brief Converts a matrix to a nested Python list using pybind11. + * @details This function is generic enough to be moved to a more general place. + * @tparam T The type of the matrix. + * @param results The matrix to convert. + * @return The converted nested Python list. + */ +template +pybind11::list MatrixToPyList(const T& results) { + pybind11::list list_results; + for (auto& r_result : results) { + pybind11::list i_list_results; + for (auto& r_sub_result : r_result) { + i_list_results.append(r_sub_result); + } + list_results.append(i_list_results); + } + return list_results; +} + /** * @brief Generates a list of lists from a vector of vectors * @param rList The list to be filled @@ -75,6 +111,79 @@ void CopyRadiusArrayToPython( }); } +/** + * @brief Binds a SpatialSearchResultContainer class to Python using pybind11. + * @tparam TObjectType The type of object stored in the container. + * @param m The pybind11 module to bind the class to. + * @param rClassName The name of the class. + */ +template +void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rClassName) { + using ContainerType = SpatialSearchResultContainer; + auto cls = pybind11::class_(m, rClassName.c_str()) + .def(pybind11::init<>()) + .def("IsObjectFound", &ContainerType::IsObjectFound) + .def("NumberOfLocalResults", &ContainerType::NumberOfLocalResults) + .def("NumberOfGlobalResults", &ContainerType::NumberOfGlobalResults) + .def("AddResult", [](ContainerType& self, TObjectType* pObject) { + self.AddResult(pObject); + }) + .def("Clear", &ContainerType::Clear) + .def("SynchronizeAll", &ContainerType::SynchronizeAll) + .def("GetResultShapeFunctions", [&](ContainerType& self, const array_1d& rPoint) { + return VectorToPyList(self.GetResultShapeFunctions(rPoint)); + }) + .def("GetResultIndices", [&](ContainerType& self) { + return VectorToPyList(self.GetResultIndices()); + }) + .def("GetResultCoordinates", [&](ContainerType& self) { + return MatrixToPyList(self.GetResultCoordinates()); + }) + + .def("__iter__", [](ContainerType& self) { + return pybind11::make_iterator(self.begin(), self.end()); + }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ + + // Add the specific methods for the GeometricalObject + if constexpr (std::is_same::value) { + cls.def("AddResult", [](ContainerType& self, SpatialSearchResult& rObject) { + self.AddResult(rObject); + }); + cls.def("__getitem__", [](ContainerType& self, const std::size_t Index) { + return self[Index]; + }); + cls.def("__call__", [](ContainerType& self, const std::size_t Index) { + return self(Index); + }); + } +} + +/** + * @brief Binds a SpatialSearchResultContainerMap to a Python module. + * @tparam T The type parameter of the SpatialSearchResultContainerMap. + * @param m The Python module to bind the class to. + * @param rClassName The name of the class in Python. + */ +template +void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& rClassName) { + using ContainerMapType = SpatialSearchResultContainerMap; + pybind11::class_(m, rClassName.c_str()) + .def(pybind11::init<>()) + .def("NumberOfPointsResults", &ContainerMapType::NumberOfPointsResults) + .def("InitializeResult", &ContainerMapType::InitializeResult) + .def("HasResult", &ContainerMapType::HasResult) + .def("Clear", &ContainerMapType::Clear) + .def("__getitem__", [](ContainerMapType& self, const array_1d& rCoordinates) { + return self[rCoordinates]; + }) + .def("__call__", [](ContainerMapType& self, const array_1d& rCoordinates) { + return self(rCoordinates); + }) + .def("__iter__", [](ContainerMapType& self) { + return pybind11::make_iterator(self.begin(), self.end()); + }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ +} + void AddSearchStrategiesToPython(pybind11::module& m) { namespace py = pybind11; @@ -694,94 +803,30 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def(py::init()) ; - using ResultType = SpatialSearchResult; + using ResultTypeGeometricalObject = SpatialSearchResult; - py::class_(m, "ResultType") + py::class_(m, "ResultTypeGeometricalObject") .def(py::init< >()) .def(py::init()) - .def("Reset", &ResultType::Reset) - .def("Get", [&](ResultType& self) {return self.Get().get();}) - .def("Set", &ResultType::Set) - .def("GetDistance", &ResultType::GetDistance) - .def("SetDistance", &ResultType::SetDistance) - .def("IsObjectFound", &ResultType::IsObjectFound) - .def("IsDistanceCalculated", &ResultType::IsDistanceCalculated) + .def("Reset", &ResultTypeGeometricalObject::Reset) + .def("Get", [&](ResultTypeGeometricalObject& self) {return self.Get().get();}) + .def("Set", &ResultTypeGeometricalObject::Set) + .def("GetDistance", &ResultTypeGeometricalObject::GetDistance) + .def("SetDistance", &ResultTypeGeometricalObject::SetDistance) + .def("IsObjectFound", &ResultTypeGeometricalObject::IsObjectFound) + .def("IsDistanceCalculated", &ResultTypeGeometricalObject::IsDistanceCalculated) ; - using ResultTypeContainer = SpatialSearchResultContainer; + // Containers + BindSpatialSearchResultContainer(m, "ResultTypeContainerNode"); + BindSpatialSearchResultContainer(m, "ResultTypeContainerGeometricalObject"); - py::class_(m, "ResultTypeContainer") - .def(py::init< >()) - .def("IsObjectFound", &ResultTypeContainer::IsObjectFound) - .def("NumberOfLocalResults", &ResultTypeContainer::NumberOfLocalResults) - .def("NumberOfGlobalResults", &ResultTypeContainer::NumberOfGlobalResults) - .def("AddResult", &ResultTypeContainer::AddResult) - .def("Clear", &ResultTypeContainer::Clear) - .def("SynchronizeAll", &ResultTypeContainer::SynchronizeAll) - .def("GetResultShapeFunctions", [&](ResultTypeContainer& self, const array_1d& rPoint) { - auto results = self.GetResultShapeFunctions(rPoint); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; - }) - .def("GetResultIndices", [&](ResultTypeContainer& self) { - std::vector results = self.GetResultIndices(); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; - }) - .def("GetResultCoordinates", [&](ResultTypeContainer& self) { - auto results = self.GetResultCoordinates(); - - // Copy the results to the python list - py::list list_results; - for (auto& r_result : results) { - py::list i_list_results; - for (auto& r_sub_result : r_result) { - i_list_results.append(r_sub_result); - } - list_results.append(i_list_results); - } - return list_results; - }) - .def("__getitem__", [&](ResultTypeContainer& self, const std::size_t Index) { - return self[Index]; - }) - .def("__call__", [&](ResultTypeContainer& self, const std::size_t Index) { - return self(Index); - }) - .def("__iter__", [](ResultTypeContainer& self) { - return py::make_iterator(self.begin(), self.end()); - }, py::keep_alive<0, 1>()) /* Keep object alive while iterator is used */ - ; - - using ResultTypeContainerMap = SpatialSearchResultContainerMap; - - py::class_(m, "ResultTypeContainerMap") - .def(py::init< >()) - .def("NumberOfPointsResults", &ResultTypeContainerMap::NumberOfPointsResults) - .def("InitializeResult", &ResultTypeContainerMap::InitializeResult) - .def("HasResult", &ResultTypeContainerMap::HasResult) - .def("Clear", &ResultTypeContainerMap::Clear) - .def("__getitem__", [&](ResultTypeContainerMap& self, const array_1d& rCoordinates) { - return self[rCoordinates]; - }) - .def("__call__", [&](ResultTypeContainerMap& self, const array_1d& rCoordinates) { - return self(rCoordinates); - }) - .def("__iter__", [](ResultTypeContainerMap& self) { - return py::make_iterator(self.begin(), self.end()); - }, py::keep_alive<0, 1>()) /* Keep object alive while iterator is used */ - ; + // Containers map + BindSpatialSearchResultContainerMap(m, "ResultTypeContainerMapNode"); + BindSpatialSearchResultContainerMap(m, "ResultTypeContainerMapGeometricalObject"); + using ResultTypeContainerGeometricalObject = SpatialSearchResultContainer; + using ResultTypeContainerMapGeometricalObject = SpatialSearchResultContainerMap; using NodesContainerType = ModelPart::NodesContainerType; using ElementsContainerType = ModelPart::ElementsContainerType; using ConditionsContainerType = ModelPart::ConditionsContainerType; @@ -795,49 +840,49 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("GetTotalNumberOfCells", &GeometricalObjectsBins::GetTotalNumberOfCells) .def("SearchInRadius", [&](GeometricalObjectsBins& self, const Point& rPoint, const double Radius) { // Perform the search - ResultTypeContainer results; + ResultTypeContainerGeometricalObject results; self.SearchInRadius(rPoint, Radius, results); return results; }) .def("SearchInRadius", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes, const double Radius) { // Perform the search - ResultTypeContainerMap results; + ResultTypeContainerMapGeometricalObject results; self.SearchInRadius(rNodes.begin(), rNodes.end(), Radius, results); return results; }) .def("SearchNearestInRadius", [&](GeometricalObjectsBins& self, const Point& rPoint, const double Radius) { // Perform the search - ResultTypeContainer results; + ResultTypeContainerGeometricalObject results; self.SearchNearestInRadius(rPoint, Radius, results); return results; }) .def("SearchNearestInRadius", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes, const double Radius) { // Perform the search - ResultTypeContainerMap results; + ResultTypeContainerMapGeometricalObject results; self.SearchNearestInRadius(rNodes.begin(), rNodes.end(), Radius, results); return results; }) .def("SearchNearest", [&](GeometricalObjectsBins& self, const Point& rPoint) { // Perform the search - ResultTypeContainer results; + ResultTypeContainerGeometricalObject results; self.SearchNearest(rPoint, results); return results; }) .def("SearchNearest", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes) { // Perform the search - ResultTypeContainerMap results; + ResultTypeContainerMapGeometricalObject results; self.SearchNearest(rNodes.begin(), rNodes.end(), results); return results; }) .def("SearchIsInside", [&](GeometricalObjectsBins& self, const Point& rPoint) { // Perform the search - ResultTypeContainer results; + ResultTypeContainerGeometricalObject results; self.SearchIsInside(rPoint, results); return results; }) .def("SearchIsInside", [&](GeometricalObjectsBins& self, const NodesContainerType& rNodes) { // Perform the search - ResultTypeContainerMap results; + ResultTypeContainerMapGeometricalObject results; self.SearchIsInside(rNodes.begin(), rNodes.end(), results); return results; }) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index d52abdb7558f..bcf51cbc5679 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -17,6 +17,7 @@ // Project includes #include "includes/data_communicator.h" +#include "includes/node.h" #include "includes/geometrical_object.h" #include "utilities/global_pointer_utilities.h" #include "spatial_containers/spatial_search_result_container.h" @@ -107,12 +108,22 @@ std::vector SpatialSearchResultContainer::GetResultShapeFun // Call Apply to get the proxy auto proxy = this->Apply([&rPoint](GlobalPointer& rGP) -> Vector { - auto& r_geometry = rGP->GetGeometry(); - Vector N(r_geometry.size()); - array_1d local_coordinates; - r_geometry.PointLocalCoordinates(local_coordinates, rPoint); - r_geometry.ShapeFunctionsValues(N, local_coordinates); - return N; + if constexpr (std::is_same::value) { + auto& r_geometry = rGP->GetGeometry(); + Vector N(r_geometry.size()); + array_1d local_coordinates; + r_geometry.PointLocalCoordinates(local_coordinates, rPoint); + r_geometry.ShapeFunctionsValues(N, local_coordinates); + return N; + } else if constexpr (std::is_same::value) { + KRATOS_ERROR << "Nodes do not provide shape functions. Not possible to compute shape functions for point: " << rPoint[0]<< "\t" << rPoint[1] << "\t" << rPoint[2] << std::endl; + Vector N; + return N; + } else { + KRATOS_ERROR << "Not implemented yet. Not possible to compute shape functions for point: " << rPoint[0]<< "\t" << rPoint[1] << "\t" << rPoint[2] << std::endl; + Vector N; + return N; + } }); // Get the shape functions @@ -166,12 +177,21 @@ std::vector>> SpatialSearchResultContainerApply([](GlobalPointer& rGP) -> std::vector> { - auto& r_geometry = rGP->GetGeometry(); - std::vector> coordinates(r_geometry.size()); - for (unsigned int i = 0; i < r_geometry.size(); ++i) { - coordinates[i] = r_geometry[i].Coordinates(); + if constexpr (std::is_same::value) { + auto& r_geometry = rGP->GetGeometry(); + std::vector> coordinates(r_geometry.size()); + for (unsigned int i = 0; i < r_geometry.size(); ++i) { + coordinates[i] = r_geometry[i].Coordinates(); + } + return coordinates; + } else if constexpr (std::is_same::value) { + std::vector> coordinates(1, rGP->Coordinates()); + return coordinates; + } else { + KRATOS_ERROR << "Not implemented yet" << std::endl; + std::vector> coordinates; + return coordinates; } - return coordinates; }); // Get the coordinates @@ -242,6 +262,7 @@ void SpatialSearchResultContainer::load(Serializer& rSerializer) /***********************************************************************************/ /// Template instantiation +template class SpatialSearchResultContainer; template class SpatialSearchResultContainer; /***********************************************************************************/ @@ -358,6 +379,7 @@ void SpatialSearchResultContainerMap::load(Serializer& rSerializer) /***********************************************************************************/ /// Template instantiation +template class SpatialSearchResultContainerMap; template class SpatialSearchResultContainerMap; } // namespace Kratos \ No newline at end of file From ebb394b2405d97e40fc06310d25ad6366f929dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Mon, 5 Jun 2023 22:09:59 +0200 Subject: [PATCH 062/145] Reactivate for node --- kratos/python/add_search_strategies_to_python.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index f2d7cc4567a8..8ccd2373290e 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -139,7 +139,14 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC .def("GetResultCoordinates", [&](ContainerType& self) { return MatrixToPyList(self.GetResultCoordinates()); }) - + .def("__getitem__", [](ContainerType& self, const std::size_t Index) { + return *(self.GetLocalPointers().GetContainer().begin() + Index); + }) + .def("__call__", [](ContainerType& self, const std::size_t Index) { + // Check if the communicator has been created + KRATOS_ERROR_IF(self.GetGlobalPointerCommunicator() == nullptr) << "The communicator has not been created. Therefore is not synchronized" << std::endl; + return *(self.GetGlobalPointers().GetContainer().begin() + Index); + }) .def("__iter__", [](ContainerType& self) { return pybind11::make_iterator(self.begin(), self.end()); }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ @@ -149,12 +156,6 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC cls.def("AddResult", [](ContainerType& self, SpatialSearchResult& rObject) { self.AddResult(rObject); }); - cls.def("__getitem__", [](ContainerType& self, const std::size_t Index) { - return self[Index]; - }); - cls.def("__call__", [](ContainerType& self, const std::size_t Index) { - return self(Index); - }); } } From 90c4b76b3a3960e7e2a34e9e769bfabd24d3344e Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 6 Jun 2023 14:06:46 +0200 Subject: [PATCH 063/145] [Core] Reduce code duplication in python binding --- .../add_search_strategies_to_python.cpp | 417 +++++------------- 1 file changed, 104 insertions(+), 313 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 8ccd2373290e..46d742b22f3c 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -185,12 +185,43 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ } +/** + * @brief Copies a Python list of radii to a C++ radius array. + * @param rListOfRadius list of radii to copy + * @return Radius array with copied radius + */ +SpatialSearch::RadiusArrayType CopyRadiusArrayToPython(const pybind11::list& rListOfRadius) +{ + // Create the radius array + SpatialSearch::RadiusArrayType radius_array(rListOfRadius.size()); + CopyRadiusArrayToPython(rListOfRadius, radius_array); + return radius_array; +} + +/** + * @brief Generates a tuple of two python lists containing the elements and distances of a spatial search result. + * @param rResults a reference to a vector containing the result elements to be copied to the python list + * @param rDistances a reference to a vector containing the distances to be copied to the python list + * @return A tuple containing two python lists, the first one containing the result elements and the second one containing the distances + * @tparam TClass The type of the class. + */ +template +std::tuple GenerateSpatialSearchSolutionTuple( + const TClass& rResults, + SpatialSearch::VectorDistanceType& rDistances + ) +{ + std::tuple results_tuple; + GenerateListFromVectorOfVector(std::get<0>(results_tuple), rResults); + GenerateListFromVectorOfVector(std::get<1>(results_tuple), rDistances); + return results_tuple; +} + void AddSearchStrategiesToPython(pybind11::module& m) { namespace py = pybind11; /// The variables of the spatial search - using RadiusArrayType = SpatialSearch::RadiusArrayType; using VectorResultElementsContainerType = SpatialSearch::VectorResultElementsContainerType; using VectorResultNodesContainerType = SpatialSearch::VectorResultNodesContainerType; using VectorResultConditionsContainerType = SpatialSearch::VectorResultConditionsContainerType; @@ -204,578 +235,338 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rModelPart.NumberOfElements()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultElementsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusExclusive(rModelPart, radius_array, results, distances); + self.SearchElementsInRadiusExclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultElementsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusExclusive(rModelPart, rInputElements, radius_array, results, distances); + self.SearchElementsInRadiusExclusive(rModelPart, rInputElements, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rStructureElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultElementsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusExclusive(rStructureElements, radius_array, results, distances); + self.SearchElementsInRadiusExclusive(rStructureElements, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultElementsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusExclusive(rStructureElements, rInputElements, radius_array, results, distances); + self.SearchElementsInRadiusExclusive(rStructureElements, rInputElements, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rModelPart.NumberOfElements()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusInclusive(rModelPart, radius_array, results, distances); + self.SearchElementsInRadiusInclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusInclusive(rModelPart, rInputElements, radius_array, results, distances); + self.SearchElementsInRadiusInclusive(rModelPart, rInputElements, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rStructureElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusInclusive(rStructureElements, radius_array, results, distances); + self.SearchElementsInRadiusInclusive(rStructureElements, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchElementsInRadiusInclusive(rStructureElements, rInputElements, radius_array, results, distances); + self.SearchElementsInRadiusInclusive(rStructureElements, rInputElements, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rModelPart.NumberOfNodes()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusExclusive(rModelPart, radius_array, results, distances); + self.SearchNodesInRadiusExclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusExclusive(rModelPart, rInputNodes, radius_array, results, distances); + self.SearchNodesInRadiusExclusive(rModelPart, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rStructureNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusExclusive(rStructureNodes, radius_array, results, distances); + self.SearchNodesInRadiusExclusive(rStructureNodes, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusExclusive(rStructureNodes, rInputNodes, radius_array, results, distances); + self.SearchNodesInRadiusExclusive(rStructureNodes, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rModelPart.NumberOfNodes()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusInclusive(rModelPart, radius_array, results, distances); + self.SearchNodesInRadiusInclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusInclusive(rModelPart, rInputNodes, radius_array, results, distances); + self.SearchNodesInRadiusInclusive(rModelPart, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rStructureNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusInclusive(rStructureNodes, radius_array, results, distances); + self.SearchNodesInRadiusInclusive(rStructureNodes, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchNodesInRadiusInclusive(rStructureNodes, rInputNodes, radius_array, results, distances); + self.SearchNodesInRadiusInclusive(rStructureNodes, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rModelPart.NumberOfConditions()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultConditionsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusExclusive(rModelPart, radius_array, results, distances); + self.SearchConditionsInRadiusExclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultConditionsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusExclusive(rModelPart, rInputConditions, radius_array, results, distances); + self.SearchConditionsInRadiusExclusive(rModelPart, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rStructureConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultConditionsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusExclusive(rStructureConditions, radius_array, results, distances); + self.SearchConditionsInRadiusExclusive(rStructureConditions, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultConditionsContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusExclusive(rStructureConditions, rInputConditions, radius_array, results, distances); + self.SearchConditionsInRadiusExclusive(rStructureConditions, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rModelPart.NumberOfConditions()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusInclusive(rModelPart, radius_array, results, distances); + self.SearchConditionsInRadiusInclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusInclusive(rModelPart, rInputConditions, radius_array, results, distances); + self.SearchConditionsInRadiusInclusive(rModelPart, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rStructureConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusInclusive(rStructureConditions, radius_array, results, distances); + self.SearchConditionsInRadiusInclusive(rStructureConditions, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); - KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; - // Create the radius array - RadiusArrayType radius_array(size_array); - CopyRadiusArrayToPython(rListOfRadius, radius_array); - - // Create the results and distances arrays + // Perform the search VectorResultNodesContainerType results(size_array); VectorDistanceType distances(size_array); - - // Perform the search - self.SearchConditionsInRadiusInclusive(rStructureConditions, rInputConditions, radius_array, results, distances); + self.SearchConditionsInRadiusInclusive(rStructureConditions, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), results, distances); // Copy the results to the python list - std::tuple results_tuple; - GenerateListFromVectorOfVector(std::get<0>(results_tuple), results); - GenerateListFromVectorOfVector(std::get<1>(results_tuple), distances); - return results_tuple; + return GenerateSpatialSearchSolutionTuple(results, distances); }) ; From 7ab7fa55226c5b57f1ca4de3e8d920822134ec17 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 6 Jun 2023 14:13:07 +0200 Subject: [PATCH 064/145] Minor --- kratos/python/add_search_strategies_to_python.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 46d742b22f3c..6e376626c8d0 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -186,8 +186,8 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& } /** - * @brief Copies a Python list of radii to a C++ radius array. - * @param rListOfRadius list of radii to copy + * @brief Copies a Python list of radius to a C++ radius array. + * @param rListOfRadius list of radius to copy * @return Radius array with copied radius */ SpatialSearch::RadiusArrayType CopyRadiusArrayToPython(const pybind11::list& rListOfRadius) From e000bd40ca998278c5d7a640bd27cc31b42c3bee Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 6 Jun 2023 17:45:04 +0200 Subject: [PATCH 065/145] Adding Global distances --- .../add_search_strategies_to_python.cpp | 3 ++ .../spatial_search_result_container.cpp | 30 +++++++++++++- .../spatial_search_result_container.h | 39 +++++++++++++++++-- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 6e376626c8d0..25637eb1c240 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -130,6 +130,9 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC }) .def("Clear", &ContainerType::Clear) .def("SynchronizeAll", &ContainerType::SynchronizeAll) + .def("GetDistances", [&](ContainerType& self) { + return VectorToPyList(self.GetDistances()); + }) .def("GetResultShapeFunctions", [&](ContainerType& self, const array_1d& rPoint) { return VectorToPyList(self.GetResultShapeFunctions(rPoint)); }) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index bcf51cbc5679..e8bb1b36f208 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -11,6 +11,7 @@ // // System includes +#include #include // External includes @@ -73,11 +74,14 @@ void SpatialSearchResultContainer::Clear() // Clear local pointers mLocalPointers.clear(); - // Clear distances + // Clear local distances mLocalDistances.clear(); // Clear global pointers mGlobalPointers.clear(); + + // Clear global distances + mGlobalDistances.clear(); } /***********************************************************************************/ @@ -89,6 +93,28 @@ void SpatialSearchResultContainer::SynchronizeAll(const DataCommuni // Synchronize local pointers to global pointers mGlobalPointers = GlobalPointerUtilities::GlobalRetrieveGlobalPointers(mLocalPointers, rDataCommunicator); + // Synchronize local distances to global distances + std::vector local_values; + for (const auto& r_pair : mLocalDistances) { + local_values.push_back(r_pair.second); + } + + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Generate vectors with sizes for AllGatherv + std::vector recv_sizes(world_size); + std::vector send_points_per_partition(1, mLocalPointers.size()); + rDataCommunicator.AllGather(send_points_per_partition, recv_sizes); + std::vector recv_offsets(world_size, 0); + for (int i_rank = 1; i_rank < world_size; ++i_rank) { + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + } + + // Invoque AllGatherv + mGlobalDistances.resize(std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0)); + rDataCommunicator.AllGatherv(local_values, mGlobalDistances, recv_sizes, recv_offsets); + // Generate the communicator mpGlobalPointerCommunicator = Kratos::make_shared>(rDataCommunicator, mGlobalPointers.ptr_begin(), mGlobalPointers.ptr_end()); } @@ -243,6 +269,7 @@ void SpatialSearchResultContainer::save(Serializer& rSerializer) co rSerializer.save("LocalPointers", mLocalPointers); rSerializer.save("GlobalPointers", mGlobalPointers); rSerializer.save("LocalDistances", mLocalDistances); + rSerializer.save("GlobalDistances", mGlobalDistances); //rSerializer.save("GlobalPointerCommunicator", mpGlobalPointerCommunicator); // Not necessary, is created and filled during use } @@ -255,6 +282,7 @@ void SpatialSearchResultContainer::load(Serializer& rSerializer) rSerializer.load("LocalPointers", mLocalPointers); rSerializer.load("GlobalPointers", mGlobalPointers); rSerializer.load("LocalDistances", mLocalDistances); + rSerializer.load("GlobalDistances", mGlobalDistances); //rSerializer.load("GlobalPointerCommunicator", mpGlobalPointerCommunicator); // Not necessary, is created and filled during use } diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 5e28a5c62a39..d17fff53f3fc 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -403,6 +403,22 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer return mpGlobalPointerCommunicator->Apply(std::forward(UserFunctor)); } + /** + * @brief Retrieves the global distances + * @return A vector containing all the distances + */ + std::vector& GetDistances() + { + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; + // Manually resize the vector if it is empty + if (mGlobalDistances.size() == 0) { + mGlobalDistances.resize(mGlobalPointers.size(), 0.0); + } + // Return the vector + return mGlobalDistances; + } + /** * @brief Considers the global pointer communicator to get the shape functions of the resulting object * @param rPoint The point coordinates @@ -431,7 +447,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer * This method returns a reference to the LocalPointerVector mLocalPointers. * @return A reference to the LocalPointerVector mLocalPointers. */ - LocalPointerVector& GetLocalPointers() { + LocalPointerVector& GetLocalPointers() + { return mLocalPointers; } @@ -440,7 +457,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer * This method returns a reference to the GlobalPointersVector mGlobalPointers. * @return A reference to the GlobalPointersVector mGlobalPointers. */ - GlobalPointersVector& GetGlobalPointers() { + GlobalPointersVector& GetGlobalPointers() + { return mGlobalPointers; } @@ -449,16 +467,28 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer * This method returns a reference to the std::unordered_map mLocalDistances. * @return A reference to the std::unordered_map mLocalDistances. */ - std::unordered_map& GetLocalDistances() { + std::unordered_map& GetLocalDistances() + { return mLocalDistances; } + /** + * @brief Accessor for mGlobalDistances. + * This method returns a reference to the std::vector mGlobalDistances. + * @return A reference to the std::vector mGlobalDistances. + */ + std::vector& GetGlobalDistances() + { + return mGlobalDistances; + } + /** * @brief Accessor for mpGlobalPointerCommunicator. * This method returns the PointerCommunicatorPointer mpGlobalPointerCommunicator. * @return The PointerCommunicatorPointer mpGlobalPointerCommunicator. */ - PointerCommunicatorPointer GetGlobalPointerCommunicator() { + PointerCommunicatorPointer GetGlobalPointerCommunicator() + { return mpGlobalPointerCommunicator; } @@ -483,6 +513,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer LocalPointerVector mLocalPointers; /// Local pointers of the container GPVector mGlobalPointers; /// Global pointers of the container std::unordered_map mLocalDistances; /// The local distances + std::vector mGlobalDistances; /// The global distances PointerCommunicatorPointer mpGlobalPointerCommunicator = nullptr; /// Global pointer to the communicator ///@} From 0862c2780d65a4ad7471775a86f44be12613c27c Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 6 Jun 2023 17:51:18 +0200 Subject: [PATCH 066/145] Move to cpp --- .../spatial_search_result_container.cpp | 16 ++++++++++++++++ .../spatial_search_result_container.h | 12 +----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index e8bb1b36f208..e9b6625ae4e7 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -122,6 +122,22 @@ void SpatialSearchResultContainer::SynchronizeAll(const DataCommuni /***********************************************************************************/ /***********************************************************************************/ +template +std::vector& SpatialSearchResultContainer::GetDistances() +{ + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; + // Manually resize the vector if it is empty + if (mGlobalDistances.size() == 0) { + mGlobalDistances.resize(mGlobalPointers.size(), 0.0); + } + // Return the vector + return mGlobalDistances; +} + +/***********************************************************************************/ +/***********************************************************************************/ + template std::vector SpatialSearchResultContainer::GetResultShapeFunctions(const array_1d& rPoint) { diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index d17fff53f3fc..799687c86038 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -407,17 +407,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer * @brief Retrieves the global distances * @return A vector containing all the distances */ - std::vector& GetDistances() - { - // Check if the communicator has been created - KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; - // Manually resize the vector if it is empty - if (mGlobalDistances.size() == 0) { - mGlobalDistances.resize(mGlobalPointers.size(), 0.0); - } - // Return the vector - return mGlobalDistances; - } + std::vector& GetDistances(); /** * @brief Considers the global pointer communicator to get the shape functions of the resulting object From 8154bf99fc75f8e105508b82c2e0e35dd3d502d5 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 6 Jun 2023 18:22:19 +0200 Subject: [PATCH 067/145] Extend `AddResult` method --- .../add_search_strategies_to_python.cpp | 3 +++ .../spatial_search_result_container.cpp | 21 +++++++++++++++++++ .../spatial_search_result_container.h | 10 +++++++++ 3 files changed, 34 insertions(+) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 25637eb1c240..0fc3b115f737 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -128,6 +128,9 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC .def("AddResult", [](ContainerType& self, TObjectType* pObject) { self.AddResult(pObject); }) + .def("AddResult", [](ContainerType& self, TObjectType* pObject, const double Distance) { + self.AddResult(pObject, Distance); + }) .def("Clear", &ContainerType::Clear) .def("SynchronizeAll", &ContainerType::SynchronizeAll) .def("GetDistances", [&](ContainerType& self) { diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index e9b6625ae4e7..a93c702cf867 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -58,6 +58,7 @@ void SpatialSearchResultContainer::AddResult(TObjectType* pResult) { // Check if the object has been found (not nullptr) if (pResult != nullptr) { + // Push_back in local pointers mLocalPointers.push_back(pResult); } } @@ -65,6 +66,26 @@ void SpatialSearchResultContainer::AddResult(TObjectType* pResult) /***********************************************************************************/ /***********************************************************************************/ +template +void SpatialSearchResultContainer::AddResult( + TObjectType* pResult, + const double Distance + ) +{ + // Check if the object has been found (not nullptr) + if (pResult != nullptr) { + // Push_back in local pointers + mLocalPointers.push_back(pResult); + + // Add distances + const IndexType id = pResult->Id(); + mLocalDistances.insert({id, Distance}); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + template void SpatialSearchResultContainer::Clear() { diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 799687c86038..5347d1ca6791 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -362,6 +362,16 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer */ void AddResult(TObjectType* pResult); + /** + * @brief Add a result to the container + * @param pResult The result to be added + * @param Distance The distance to be added + */ + void AddResult( + TObjectType* pResult, + const double Distance + ); + /** * @brief Pushes back a result to the container * @param pResult The result to be added From 649ef9636fc35441be678ebd5dee25b918a9b185 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 11:38:27 +0200 Subject: [PATCH 068/145] Extend API --- .../add_search_strategies_to_python.cpp | 20 +++++- .../spatial_search_result_container.cpp | 25 ++++++++ .../spatial_search_result_container.h | 61 +++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 0fc3b115f737..a385bdc35fa8 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -177,12 +177,28 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& pybind11::class_(m, rClassName.c_str()) .def(pybind11::init<>()) .def("NumberOfPointsResults", &ContainerMapType::NumberOfPointsResults) - .def("InitializeResult", &ContainerMapType::InitializeResult) - .def("HasResult", &ContainerMapType::HasResult) + .def("InitializeResult", [](ContainerMapType& self, const std::size_t Index) { + self.InitializeResult(Index); + }) + .def("InitializeResult", [](ContainerMapType& self, const array_1d& rCoordinates) { + self.InitializeResult(rCoordinates); + }) + .def("HasResult", [](ContainerMapType& self, const std::size_t Index) { + self.HasResult(Index); + }) + .def("InitializeResult", [](ContainerMapType& self, const array_1d& rCoordinates) { + self.HasResult(rCoordinates); + }) .def("Clear", &ContainerMapType::Clear) + .def("__getitem__", [](ContainerMapType& self, const std::size_t Index) { + return self[Index]; + }) .def("__getitem__", [](ContainerMapType& self, const array_1d& rCoordinates) { return self[rCoordinates]; }) + .def("__call__", [](ContainerMapType& self, const std::size_t Index) { + return self(Index); + }) .def("__call__", [](ContainerMapType& self, const array_1d& rCoordinates) { return self(rCoordinates); }) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index a93c702cf867..86b8e1dcf530 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -333,6 +333,19 @@ template class SpatialSearchResultContainer; /***********************************************************************************/ /***********************************************************************************/ +template +SpatialSearchResultContainer& SpatialSearchResultContainerMap::InitializeResult(const IndexType Index) +{ + // If doesn't exists, create it + if (!HasResult(Index)) { + mPointResults.insert({Index, SpatialSearchResultContainer()}); + } + return mPointResults[Index]; +} + +/***********************************************************************************/ +/***********************************************************************************/ + template SpatialSearchResultContainer& SpatialSearchResultContainerMap::InitializeResult(const array_1d& rCoordinates) { @@ -347,6 +360,18 @@ SpatialSearchResultContainer& SpatialSearchResultContainerMap +bool SpatialSearchResultContainerMap::HasResult(const IndexType Index) const +{ + if (mPointResults.find(Index) != mPointResults.end()) { + return true; + } + return false; +} + +/***********************************************************************************/ +/***********************************************************************************/ + template bool SpatialSearchResultContainerMap::HasResult(const array_1d& rCoordinates) const { diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 5347d1ca6791..a77e5bf3ac22 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -741,6 +741,54 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap ///@name Operators ///@{ + /** + * @brief Operator [] + * @param Index The index to be initialized + * @return The result container + */ + SpatialSearchResultContainer& operator[](const IndexType Index) + { + const auto it = mPointResults.find(Index); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for index: " << Index << std::endl; + return it->second; + } + + /** + * @brief Operator [] + * @param Index The index to be initialized + * @return The result container + */ + const SpatialSearchResultContainer& operator[](const IndexType Index) const + { + const auto it = mPointResults.find(Index); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for index: " << Index << std::endl; + return it->second; + } + + /** + * @brief Operator () + * @param Index The index to be initialized + * @return The result container + */ + SpatialSearchResultContainer& operator()(const IndexType Index) + { + const auto it = mPointResults.find(Index); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for index: " << Index << std::endl; + return it->second; + } + + /** + * @brief Operator () + * @param Index The index to be initialized + * @return The result container + */ + const SpatialSearchResultContainer& operator()(const IndexType Index) const + { + const auto it = mPointResults.find(Index); + KRATOS_ERROR_IF(it == mPointResults.end()) << "The result container does not exist for index: " << Index << std::endl; + return it->second; + } + /** * @brief Operator [] * @param rCoordinates The coordinates @@ -842,12 +890,25 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap return mPointResults.size(); } + /** + * @brief Initialize the container + * @param Index The index to be initialized + */ + SpatialSearchResultContainer& InitializeResult(const IndexType Index); + /** * @brief Initialize the container * @param rCoordinates The coordinates */ SpatialSearchResultContainer& InitializeResult(const array_1d& rCoordinates); + /** + * @brief Check if coordinates are initialized + * @param Index The index to be initialized + * @return True if hash is initialized, false otherwise + */ + bool HasResult(const IndexType Index) const; + /** * @brief Check if coordinates are initialized * @param rCoordinates The coordinates From 2f745cf009f099d1f66c735f349dc16926bd3438 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 14:25:17 +0200 Subject: [PATCH 069/145] Extend `SpatialSearch` --- kratos/spatial_containers/spatial_search.cpp | 662 +++++++++++++--- kratos/spatial_containers/spatial_search.h | 748 +++++++++++++----- .../specialized_spatial_search.h | 21 +- 3 files changed, 1112 insertions(+), 319 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 5a12aaf763f2..671cafee16a4 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -35,40 +35,77 @@ void SpatialSearch::SearchElementsInRadiusExclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ElementSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusExclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchElementsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), rModelPart.GetCommunicator().LocalMesh().Elements(), rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusExclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchElementsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), - InputElements, + rInputElements, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ElementSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusExclusive ( + ModelPart& rModelPart, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchElementsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), rInputElements, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ) { - this->SearchElementsInRadiusExclusive(StructureElements, - StructureElements, + this->SearchElementsInRadiusExclusive(rStructureElements, + rStructureElements, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ElementSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchElementsInRadiusExclusive(rStructureElements, rStructureElements, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -80,6 +117,45 @@ void SpatialSearch::SearchElementsInRadiusExclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ElementSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // First search in the structure + VectorResultElementsContainerType results; + VectorDistanceType distance; + this->SearchElementsInRadiusExclusive(rStructureElements, rInputElements, rRadius, results, distance); + + // Now pass this results to the container + ElementSpatialSearchResultContainerMapType result; + const SizeType number_of_results = results.size(); + for (IndexType i = 0; i < number_of_results; ++i) { + // Partial results + auto& r_partial_results = results[i]; + auto& r_partial_distances = distance[i]; + + // Getting id of the element + const IndexType id = (rInputElements.begin() + i)->Id(); + + // Adding partial results + auto result_i = result.InitializeResult(id); + for (IndexType j = 0; j < r_partial_results.size(); ++j) { + auto p_element = (*((r_partial_results.begin() + j).base())).get(); + result_i.AddResult(p_element, r_partial_distances[j]); + } + } + + // Synchronize all the results + result.SynchronizeAll(rDataCommunicator); + return result; +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusInclusive ( ModelPart& rModelPart, const RadiusArrayType& rRadius, @@ -95,40 +171,77 @@ void SpatialSearch::SearchElementsInRadiusInclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusInclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchElementsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), rModelPart.GetCommunicator().LocalMesh().Elements(), rRadius,rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusInclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchElementsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), - InputElements, + rInputElements, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusInclusive ( + ModelPart& rModelPart, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchElementsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), rInputElements, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { - this->SearchElementsInRadiusInclusive(StructureElements, - StructureElements, + this->SearchElementsInRadiusInclusive(rStructureElements, + rStructureElements, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusInclusive ( + const ElementsContainerType& rStructureElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchElementsInRadiusInclusive(rStructureElements, rStructureElements, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance @@ -140,6 +253,45 @@ void SpatialSearch::SearchElementsInRadiusInclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchElementsInRadiusInclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // First search in the structure + VectorResultNodesContainerType results; + VectorDistanceType distance; + this->SearchElementsInRadiusInclusive(rStructureElements, rInputElements, rRadius, results, distance); + + // Now pass this results to the container + NodeSpatialSearchResultContainerMapType result; + const SizeType number_of_results = results.size(); + for (IndexType i = 0; i < number_of_results; ++i) { + // Partial results + auto& r_partial_results = results[i]; + auto& r_partial_distances = distance[i]; + + // Getting id of the element + const IndexType id = (rInputElements.begin() + i)->Id(); + + // Adding partial results + auto result_i = result.InitializeResult(id); + for (IndexType j = 0; j < r_partial_results.size(); ++j) { + auto p_node = (*((r_partial_results.begin() + j).base())).get(); + result_i.AddResult(p_node, r_partial_distances[j]); + } + } + + // Synchronize all the results + result.SynchronizeAll(rDataCommunicator); + return result; +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchElementsInRadiusExclusive ( ModelPart& rModelPart, const RadiusArrayType& rRadius, @@ -156,13 +308,13 @@ void SpatialSearch::SearchElementsInRadiusExclusive ( void SpatialSearch::SearchElementsInRadiusExclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults ) { this->SearchElementsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), - InputElements, + rInputElements, rRadius,rResults); } @@ -170,13 +322,13 @@ void SpatialSearch::SearchElementsInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults ) { - this->SearchElementsInRadiusExclusive(StructureElements, - StructureElements, + this->SearchElementsInRadiusExclusive(rStructureElements, + rStructureElements, rRadius,rResults); } @@ -184,8 +336,8 @@ void SpatialSearch::SearchElementsInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults ) @@ -212,13 +364,13 @@ void SpatialSearch::SearchElementsInRadiusInclusive ( void SpatialSearch::SearchElementsInRadiusInclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { this->SearchElementsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), - InputElements, + rInputElements, rRadius,rResults); } @@ -226,13 +378,13 @@ void SpatialSearch::SearchElementsInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { - this->SearchElementsInRadiusInclusive(StructureElements, - StructureElements, + this->SearchElementsInRadiusInclusive(rStructureElements, + rStructureElements, rRadius,rResults); } @@ -240,8 +392,8 @@ void SpatialSearch::SearchElementsInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) @@ -260,8 +412,20 @@ void SpatialSearch::SearchNodesInRadiusExclusive ( ) { this->SearchNodesInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), - rModelPart.GetCommunicator().LocalMesh().Nodes(), - rRadius,rResults,rResultsDistance); + rModelPart.GetCommunicator().LocalMesh().Nodes(), + rRadius,rResults,rResultsDistance); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusExclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchNodesInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), rModelPart.GetCommunicator().LocalMesh().Nodes(), rRadius, rDataCommunicator); } /***********************************************************************************/ @@ -269,38 +433,63 @@ void SpatialSearch::SearchNodesInRadiusExclusive ( void SpatialSearch::SearchNodesInRadiusExclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchNodesInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), - InputNodes, - rRadius,rResults,rResultsDistance); + rInputNodes, + rRadius,rResults,rResultsDistance); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusExclusive ( + ModelPart& rModelPart, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchNodesInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), rInputNodes, rRadius, rDataCommunicator); } /***********************************************************************************/ /***********************************************************************************/ void SpatialSearch::SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { - this->SearchNodesInRadiusExclusive(StructureNodes, - StructureNodes, + this->SearchNodesInRadiusExclusive(rStructureNodes, + rStructureNodes, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + this->SearchNodesInRadiusExclusive(rStructureNodes, rStructureNodes, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance @@ -312,6 +501,45 @@ void SpatialSearch::SearchNodesInRadiusExclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // First search in the structure + VectorResultNodesContainerType results; + VectorDistanceType distance; + this->SearchNodesInRadiusExclusive(rStructureNodes, rInputNodes, rRadius, results, distance); + + // Now pass this results to the container + NodeSpatialSearchResultContainerMapType result; + const SizeType number_of_results = results.size(); + for (IndexType i = 0; i < number_of_results; ++i) { + // Partial results + auto& r_partial_results = results[i]; + auto& r_partial_distances = distance[i]; + + // Getting id of the element + const IndexType id = (rInputNodes.begin() + i)->Id(); + + // Adding partial results + auto result_i = result.InitializeResult(id); + for (IndexType j = 0; j < r_partial_results.size(); ++j) { + auto p_node = (*((r_partial_results.begin() + j).base())).get(); + result_i.AddResult(p_node, r_partial_distances[j]); + } + } + + // Synchronize all the results + result.SynchronizeAll(rDataCommunicator); + return result; +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchNodesInRadiusInclusive ( ModelPart& rModelPart, const RadiusArrayType& rRadius, @@ -327,40 +555,77 @@ void SpatialSearch::SearchNodesInRadiusInclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusInclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchNodesInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), rModelPart.GetCommunicator().LocalMesh().Nodes(), rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchNodesInRadiusInclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchNodesInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), - InputNodes, + rInputNodes, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusInclusive ( + ModelPart& rModelPart, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchNodesInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), rInputNodes, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { - this->SearchNodesInRadiusInclusive(StructureNodes, - StructureNodes, + this->SearchNodesInRadiusInclusive(rStructureNodes, + rStructureNodes, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusInclusive ( + const NodesContainerType& rStructureNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchNodesInRadiusInclusive(rStructureNodes, rStructureNodes, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance @@ -372,6 +637,45 @@ void SpatialSearch::SearchNodesInRadiusInclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNodesInRadiusInclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // First search in the structure + VectorResultNodesContainerType results; + VectorDistanceType distance; + this->SearchNodesInRadiusInclusive(rStructureNodes, rInputNodes, rRadius, results, distance); + + // Now pass this results to the container + NodeSpatialSearchResultContainerMapType result; + const SizeType number_of_results = results.size(); + for (IndexType i = 0; i < number_of_results; ++i) { + // Partial results + auto& r_partial_results = results[i]; + auto& r_partial_distances = distance[i]; + + // Getting id of the element + const IndexType id = (rInputNodes.begin() + i)->Id(); + + // Adding partial results + auto result_i = result.InitializeResult(id); + for (IndexType j = 0; j < r_partial_results.size(); ++j) { + auto p_node = (*((r_partial_results.begin() + j).base())).get(); + result_i.AddResult(p_node, r_partial_distances[j]); + } + } + + // Synchronize all the results + result.SynchronizeAll(rDataCommunicator); + return result; +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchNodesInRadiusExclusive ( ModelPart& rModelPart, const RadiusArrayType& rRadius, @@ -388,13 +692,13 @@ void SpatialSearch::SearchNodesInRadiusExclusive ( void SpatialSearch::SearchNodesInRadiusExclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { this->SearchNodesInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), - InputNodes, + rInputNodes, rRadius,rResults); } @@ -402,13 +706,13 @@ void SpatialSearch::SearchNodesInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { - this->SearchNodesInRadiusExclusive(StructureNodes, - StructureNodes, + this->SearchNodesInRadiusExclusive(rStructureNodes, + rStructureNodes, rRadius,rResults); } @@ -416,8 +720,8 @@ void SpatialSearch::SearchNodesInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) @@ -444,13 +748,13 @@ void SpatialSearch::SearchNodesInRadiusInclusive ( void SpatialSearch::SearchNodesInRadiusInclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { this->SearchNodesInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Nodes(), - InputNodes, + rInputNodes, rRadius,rResults); } @@ -458,13 +762,13 @@ void SpatialSearch::SearchNodesInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { - this->SearchNodesInRadiusInclusive(StructureNodes, - StructureNodes, + this->SearchNodesInRadiusInclusive(rStructureNodes, + rStructureNodes, rRadius,rResults); } @@ -472,8 +776,8 @@ void SpatialSearch::SearchNodesInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) @@ -499,40 +803,77 @@ void SpatialSearch::SearchConditionsInRadiusExclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ConditionSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusExclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchConditionsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), rModelPart.GetCommunicator().LocalMesh().Conditions(), rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusExclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchConditionsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), - InputConditions, + rInputConditions, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ConditionSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusExclusive ( + ModelPart& rModelPart, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchConditionsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), rInputConditions, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults, VectorDistanceType& rResultsDistance ) { - this->SearchConditionsInRadiusExclusive(StructureConditions, - StructureConditions, + this->SearchConditionsInRadiusExclusive(rStructureConditions, + rStructureConditions, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ConditionSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchConditionsInRadiusExclusive(rStructureConditions, rStructureConditions, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -544,6 +885,45 @@ void SpatialSearch::SearchConditionsInRadiusExclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::ConditionSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // First search in the structure + VectorResultConditionsContainerType results; + VectorDistanceType distance; + this->SearchConditionsInRadiusExclusive(rStructureConditions, rInputConditions, rRadius, results, distance); + + // Now pass this results to the container + ConditionSpatialSearchResultContainerMapType result; + const SizeType number_of_results = results.size(); + for (IndexType i = 0; i < number_of_results; ++i) { + // Partial results + auto& r_partial_results = results[i]; + auto& r_partial_distances = distance[i]; + + // Getting id of the element + const IndexType id = (rInputConditions.begin() + i)->Id(); + + // Adding partial results + auto result_i = result.InitializeResult(id); + for (IndexType j = 0; j < r_partial_results.size(); ++j) { + auto p_condition = (*((r_partial_results.begin() + j).base())).get(); + result_i.AddResult(p_condition, r_partial_distances[j]); + } + } + + // Synchronize all the results + result.SynchronizeAll(rDataCommunicator); + return result; +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusInclusive ( ModelPart& rModelPart, const RadiusArrayType& rRadius, @@ -559,40 +939,77 @@ void SpatialSearch::SearchConditionsInRadiusInclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusInclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchConditionsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), rModelPart.GetCommunicator().LocalMesh().Conditions(), rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusInclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchConditionsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), - InputConditions, + rInputConditions, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusInclusive ( + ModelPart& rModelPart, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchConditionsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), rInputConditions, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ) { - this->SearchConditionsInRadiusInclusive(StructureConditions, - StructureConditions, + this->SearchConditionsInRadiusInclusive(rStructureConditions, + rStructureConditions, rRadius,rResults,rResultsDistance); } /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusInclusive ( + const ConditionsContainerType& rStructureConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return this->SearchConditionsInRadiusInclusive(rStructureConditions, rStructureConditions, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance @@ -604,6 +1021,45 @@ void SpatialSearch::SearchConditionsInRadiusInclusive ( /***********************************************************************************/ /***********************************************************************************/ +SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchConditionsInRadiusInclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // First search in the structure + VectorResultNodesContainerType results; + VectorDistanceType distance; + this->SearchConditionsInRadiusInclusive(rStructureConditions, rInputConditions, rRadius, results, distance); + + // Now pass this results to the container + NodeSpatialSearchResultContainerMapType result; + const SizeType number_of_results = results.size(); + for (IndexType i = 0; i < number_of_results; ++i) { + // Partial results + auto& r_partial_results = results[i]; + auto& r_partial_distances = distance[i]; + + // Getting id of the element + const IndexType id = (rInputConditions.begin() + i)->Id(); + + // Adding partial results + auto result_i = result.InitializeResult(id); + for (IndexType j = 0; j < r_partial_results.size(); ++j) { + auto p_node = (*((r_partial_results.begin() + j).base())).get(); + result_i.AddResult(p_node, r_partial_distances[j]); + } + } + + // Synchronize all the results + result.SynchronizeAll(rDataCommunicator); + return result; +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SpatialSearch::SearchConditionsInRadiusExclusive ( ModelPart& rModelPart, const RadiusArrayType& rRadius, @@ -620,13 +1076,13 @@ void SpatialSearch::SearchConditionsInRadiusExclusive ( void SpatialSearch::SearchConditionsInRadiusExclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults ) { this->SearchConditionsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), - InputConditions, + rInputConditions, rRadius,rResults); } @@ -634,13 +1090,13 @@ void SpatialSearch::SearchConditionsInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults ) { - this->SearchConditionsInRadiusExclusive(StructureConditions, - StructureConditions, + this->SearchConditionsInRadiusExclusive(rStructureConditions, + rStructureConditions, rRadius,rResults); } @@ -648,8 +1104,8 @@ void SpatialSearch::SearchConditionsInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults ) @@ -676,13 +1132,13 @@ void SpatialSearch::SearchConditionsInRadiusInclusive ( void SpatialSearch::SearchConditionsInRadiusInclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { this->SearchConditionsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), - InputConditions, + rInputConditions, rRadius,rResults); } @@ -690,13 +1146,13 @@ void SpatialSearch::SearchConditionsInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) { - this->SearchConditionsInRadiusInclusive(StructureConditions, - StructureConditions, + this->SearchConditionsInRadiusInclusive(rStructureConditions, + rStructureConditions, rRadius,rResults); } @@ -704,8 +1160,8 @@ void SpatialSearch::SearchConditionsInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ) @@ -733,14 +1189,14 @@ void SpatialSearch::SearchConditionsOverElementsInRadiusExclusive ( void SpatialSearch::SearchConditionsOverElementsInRadiusExclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchConditionsOverElementsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), - InputConditions, + rInputConditions, rRadius,rResults,rResultsDistance); } @@ -748,8 +1204,8 @@ void SpatialSearch::SearchConditionsOverElementsInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchConditionsOverElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, - ConditionsContainerType const& InputConditions, + const ElementsContainerType& rStructureElements, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -778,14 +1234,14 @@ void SpatialSearch::SearchConditionsOverElementsInRadiusInclusive ( void SpatialSearch::SearchConditionsOverElementsInRadiusInclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchConditionsOverElementsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Elements(), - InputConditions, + rInputConditions, rRadius,rResults,rResultsDistance); } @@ -793,8 +1249,8 @@ void SpatialSearch::SearchConditionsOverElementsInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchConditionsOverElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, - ConditionsContainerType const& InputConditions, + const ElementsContainerType& rStructureElements, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -823,14 +1279,14 @@ void SpatialSearch::SearchElementsOverConditionsInRadiusExclusive ( void SpatialSearch::SearchElementsOverConditionsInRadiusExclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchElementsOverConditionsInRadiusExclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), - InputElements, + rInputElements, rRadius,rResults,rResultsDistance); } @@ -838,8 +1294,8 @@ void SpatialSearch::SearchElementsOverConditionsInRadiusExclusive ( /***********************************************************************************/ void SpatialSearch::SearchElementsOverConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ConditionsContainerType& rStructureConditions, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -868,14 +1324,14 @@ void SpatialSearch::SearchElementsOverConditionsInRadiusInclusive ( void SpatialSearch::SearchElementsOverConditionsInRadiusInclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ) { this->SearchElementsOverConditionsInRadiusInclusive(rModelPart.GetCommunicator().LocalMesh().Conditions(), - InputElements, + rInputElements, rRadius,rResults,rResultsDistance); } @@ -883,8 +1339,8 @@ void SpatialSearch::SearchElementsOverConditionsInRadiusInclusive ( /***********************************************************************************/ void SpatialSearch::SearchElementsOverConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ConditionsContainerType& rStructureConditions, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance diff --git a/kratos/spatial_containers/spatial_search.h b/kratos/spatial_containers/spatial_search.h index 3d1ff566ca0a..d488f3aa6bbd 100644 --- a/kratos/spatial_containers/spatial_search.h +++ b/kratos/spatial_containers/spatial_search.h @@ -18,6 +18,7 @@ // Project includes #include "includes/model_part.h" +#include "spatial_containers/spatial_search_result_container.h" namespace Kratos { @@ -55,6 +56,12 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /// Pointer definition of SpatialSearch KRATOS_CLASS_POINTER_DEFINITION(SpatialSearch); + /// The size type definition + using SizeType = std::size_t; + + /// The index type definition + using IndexType = std::size_t; + /// Defining dimension static constexpr std::size_t Dimension = 3; @@ -64,33 +71,35 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /// Minimum level of the tree static constexpr std::size_t MIN_LEVEL = 2; - /// Common Defines + /// The point type using PointType = Point; - using ElementsContainerType = ModelPart::ElementsContainerType; - using ElementType = ModelPart::ElementType; - using ElementPointerType = ModelPart::ElementType::Pointer; - using ResultElementsContainerType = ElementsContainerType::ContainerType; - using VectorResultElementsContainerType = std::vector; + /// The node type + using NodeType = Node; + /// Nodes classes using NodesContainerType = ModelPart::NodesContainerType; - using NodeType = ModelPart::NodeType; - using NodePointerType = ModelPart::NodeType::Pointer; using ResultNodesContainerType = NodesContainerType::ContainerType; using VectorResultNodesContainerType = std::vector; + using NodeSpatialSearchResultContainerMapType = SpatialSearchResultContainerMap; + + /// Elements classes + using ElementsContainerType = ModelPart::ElementsContainerType; + using ResultElementsContainerType = ElementsContainerType::ContainerType; + using VectorResultElementsContainerType = std::vector; + using ElementSpatialSearchResultContainerMapType = SpatialSearchResultContainerMap; + /// Conditions classes using ConditionsContainerType = ModelPart::ConditionsContainerType; - using ConditionType = ModelPart::ConditionType; - using ConditionPointerType = ModelPart::ConditionType::Pointer; using ResultConditionsContainerType = ConditionsContainerType::ContainerType; using VectorResultConditionsContainerType = std::vector; + using ConditionSpatialSearchResultContainerMapType = SpatialSearchResultContainerMap; + /// Input/output types using RadiusArrayType = std::vector; using DistanceType = std::vector; using VectorDistanceType = std::vector; - using ResultIteratorType = ElementsContainerType::ContainerType::iterator; - ///@} ///@name Life Cycle ///@{ @@ -116,7 +125,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ @@ -127,52 +136,108 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "rModelpart" excluding itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every element in "Inputelements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsInRadiusExclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "Inputelements" excluding itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( + ModelPart& rModelPart, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every element in "StructureElements" excluding itself - * @param StructureElements List of elements modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements modelpart against which the neighbours are searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "StructureElements" excluding itself + * @param rStructureElements List of elements modelpart against which the neighbours are searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every element in "Inputelements" excluding itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "Inputelements" excluding itself + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + //************************************************************************ // Elemental Inclusive search with distance calculation //************************************************************************ @@ -180,7 +245,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ @@ -191,52 +256,108 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "rModelpart" including itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchElementsInRadiusInclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every element in "InputElements" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsInRadiusInclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "InputElements" including itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchElementsInRadiusInclusive ( + ModelPart& rModelPart, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every element in "StructureElements" including itself - * @param StructureElements List of elements against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "StructureElements" including itself + * @param rStructureElements List of elements against which the neighbours are searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchElementsInRadiusInclusive ( + const ElementsContainerType& rStructureElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every element in "InputElements" including itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every element in "InputElements" including itself + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchElementsInRadiusInclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + //************************************************************************ // Elemental Exclusive search without distance calculation //************************************************************************ @@ -244,7 +365,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusExclusive ( @@ -256,39 +377,39 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusExclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults ); /** * @brief Search neighbours for every element in "StructureElements" excluding itself - * @param StructureElements List of nodes against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rStructureElements List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults ); /** * @brief Search neighbours for every element in "Inputelements" excluding itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults ); @@ -300,7 +421,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusInclusive ( @@ -312,39 +433,39 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "InputElements" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusInclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); /** * @brief Search neighbours for every element in "StructureElements" including itself - * @param StructureElements List of elements against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, + const ElementsContainerType& rStructureElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); /** * @brief Search neighbours for every element in "InputElements" including itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element */ virtual void SearchElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); @@ -356,7 +477,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every node in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every node + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ @@ -367,52 +488,108 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every node in "rModelpart" excluding itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusExclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every node in "InputNodes" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ virtual void SearchNodesInRadiusExclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); /** - * @brief Search neighbours for every node in "StructureNodes" excluding itself - * @param StructureNodes Lis of nodes against which the neighbours are searched - * @param Radius List of search radius for every node + * @brief Search neighbours for every node in "InputNodes" excluding itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusExclusive ( + ModelPart& rModelPart, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search neighbours for every node in "rStructureNodes" excluding itself + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ virtual void SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every node in "rStructureNodes" excluding itself + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every node in "InputNodes" excluding itself * @param rModelPart List of nodes against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ virtual void SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every node in "InputNodes" excluding itself + * @param rModelPart List of nodes against which the neighbours are searched + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + //************************************************************************ // Nodal Inclusive search with distance calculation //************************************************************************ @@ -420,7 +597,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every node in "rModelpart" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every node + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ @@ -431,52 +608,107 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every node in "rModelpart" including itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusInclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every node in "InputNodes" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ virtual void SearchNodesInRadiusInclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); /** - * @brief Search neighbours for every node in "StructureNodes" including itself - * @param StructureNodes List of nodes against which the neighbours are searched - * @param Radius List of search radius for every node + * @brief Search neighbours for every node in "InputNodes" including itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusInclusive ( + ModelPart& rModelPart, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search neighbours for every node in "rStructureNodes" including itself + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ virtual void SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every node in "rStructureNodes" including itself + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusInclusive ( + const NodesContainerType& rStructureNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every node in "InputNodes" including itself - * @param StructureNodes List of nodes against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node * @param rResultDistance Array of distances for each result of each node */ virtual void SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every node in "InputNodes" including itself + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchNodesInRadiusInclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); //************************************************************************ // Nodal Exclusive search without distance calculation @@ -485,7 +717,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every node in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every node + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusExclusive ( @@ -497,25 +729,25 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every node in "InputNodes" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusExclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); /** - * @brief Search neighbours for every node in "StructureNodes" excluding itself - * @param StructureNodes Lis of nodes against which the neighbours are searched - * @param Radius List of search radius for every node + * @brief Search neighbours for every node in "rStructureNodes" excluding itself + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); @@ -523,13 +755,13 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every node in "InputNodes" excluding itself * @param rModelPart List of nodes against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusExclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); @@ -541,7 +773,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every node in "rModelpart" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every node + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusInclusive ( @@ -553,39 +785,39 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every node in "InputNodes" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusInclusive ( ModelPart& rModelPart, - NodesContainerType const& InputNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); /** - * @brief Search neighbours for every node in "StructureNodes" including itself - * @param StructureNodes List of nodes against which the neighbours are searched - * @param Radius List of search radius for every node + * @brief Search neighbours for every node in "rStructureNodes" including itself + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, + const NodesContainerType& rStructureNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); /** * @brief Search neighbours for every node in "InputNodes" including itself - * @param StructureNodes List of nodes against which the neighbours are searched - * @param InputNodes List of nodes to be searched - * @param Radius List of search radius for every node + * @param rStructureNodes List of nodes against which the neighbours are searched + * @param rInputNodes List of nodes to be searched + * @param rRadius List of search radius for every node * @param rResults Array of results for each node */ virtual void SearchNodesInRadiusInclusive ( - NodesContainerType const& StructureNodes, - NodesContainerType const& InputNodes, + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); @@ -597,7 +829,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every Condition in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ @@ -608,52 +840,108 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every Condition in "rModelpart" excluding itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every Condition in "InputConditions" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ virtual void SearchConditionsInRadiusExclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults, VectorDistanceType& rResultsDistance ); /** - * @brief Search neighbours for every Condition in "StructureConditions" excluding itself - * @param StructureConditions List of Conditions modelpart against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @brief Search neighbours for every Condition in "InputConditions" excluding itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( + ModelPart& rModelPart, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search neighbours for every Condition in "rStructureConditions" excluding itself + * @param rStructureConditions List of conditions modelpart against which the neighbours are searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ virtual void SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every Condition in "rStructureConditions" excluding itself + * @param rStructureConditions List of conditions modelpart against which the neighbours are searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every Condition in "InputConditions" excluding itself - * @param StructureConditions List of Conditions against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ virtual void SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every Condition in "InputConditions" excluding itself + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + //************************************************************************ // Conditional Inclusive search with distance calculation //************************************************************************ @@ -661,7 +949,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every Condition in "rModelpart" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ @@ -672,52 +960,108 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every Condition in "rModelpart" including itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchConditionsInRadiusInclusive ( + ModelPart& rModelPart, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every Condition in "InputConditions" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ virtual void SearchConditionsInRadiusInclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); /** - * @brief Search neighbours for every Condition in "StructureConditions" including itself - * @param StructureConditions List of Conditions against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @brief Search neighbours for every Condition in "InputConditions" including itself + * @param rModelPart Input modelpart against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchConditionsInRadiusInclusive ( + ModelPart& rModelPart, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search neighbours for every Condition in "rStructureConditions" including itself + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ virtual void SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every Condition in "rStructureConditions" including itself + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchConditionsInRadiusInclusive ( + const ConditionsContainerType& rStructureConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + /** * @brief Search neighbours for every Condition in "InputConditions" including itself - * @param StructureConditions List of Conditions against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition * @param rResultDistance Array of distances for each result of each Condition */ virtual void SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults, VectorDistanceType& rResultsDistance ); + /** + * @brief Search neighbours for every Condition in "InputConditions" including itself + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition + * @param rDataCommunicator The data communicator + * @return The results maps + */ + virtual NodeSpatialSearchResultContainerMapType SearchConditionsInRadiusInclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ); + //************************************************************************ // Conditional Exclusive search without distance calculation //************************************************************************ @@ -725,7 +1069,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every Condition in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusExclusive ( @@ -737,39 +1081,39 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every Condition in "InputConditions" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusExclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults ); /** - * @brief Search neighbours for every Condition in "StructureConditions" excluding itself - * @param StructureConditions List of nodes against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @brief Search neighbours for every Condition in "rStructureConditions" excluding itself + * @param rStructureConditions List of nodes against which the neighbours are searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults ); /** * @brief Search neighbours for every Condition in "InputConditions" excluding itself - * @param StructureConditions List of Conditions against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultConditionsContainerType& rResults ); @@ -781,7 +1125,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every Condition in "rModelpart" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusInclusive ( @@ -793,39 +1137,39 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every Condition in "InputConditions" including itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusInclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); /** - * @brief Search neighbours for every Condition in "StructureConditions" including itself - * @param StructureConditions List of Conditions against which the neighbours are searched - * @param Radius List of search radius for every Condition + * @brief Search neighbours for every Condition in "rStructureConditions" including itself + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, + const ConditionsContainerType& rStructureConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); /** * @brief Search neighbours for every Condition in "InputConditions" including itself - * @param StructureConditions List of Conditions against which the neighbours are searched - * @param InputConditions List of Conditions to be searched - * @param Radius List of search radius for every Condition + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every Condition * @param rResults Array of results for each Condition */ virtual void SearchConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureConditions, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultNodesContainerType& rResults ); @@ -837,7 +1181,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ @@ -851,14 +1195,14 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputConditions List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchConditionsOverElementsInRadiusExclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -866,15 +1210,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputConditions List of elements to be searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchConditionsOverElementsInRadiusExclusive ( - ElementsContainerType const& StructureElements, - ConditionsContainerType const& InputConditions, + const ElementsContainerType& rStructureElements, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -887,7 +1231,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ @@ -901,14 +1245,14 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputConditions List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchConditionsOverElementsInRadiusInclusive ( ModelPart& rModelPart, - ConditionsContainerType const& InputConditions, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -916,15 +1260,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputConditions List of elements to be searched - * @param Radius List of search radius for every element + * @param rStructureElements List of elements against which the neighbours are searched + * @param rInputConditions List of conditions to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchConditionsOverElementsInRadiusInclusive ( - ElementsContainerType const& StructureElements, - ConditionsContainerType const& InputConditions, + const ElementsContainerType& rStructureElements, + const ConditionsContainerType& rInputConditions, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -937,7 +1281,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ @@ -951,14 +1295,14 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsOverConditionsInRadiusExclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -966,15 +1310,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element - * @param rResults Array of results for each element - * @param rResultDistance Array of distances for each result of each element + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param InputElements List of elements to be searched + * @param rRadius List of search radius for every element + * @param rResults Array of results for each element + * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsOverConditionsInRadiusExclusive ( - ConditionsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ConditionsContainerType& rStructureConditions, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -987,7 +1331,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "rModelpart" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param Radius List of search radius for every element + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ @@ -1001,14 +1345,14 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element + * @param rInputElements List of elements to be searched + * @param rRadius List of search radius for every element * @param rResults Array of results for each element * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsOverConditionsInRadiusInclusive ( ModelPart& rModelPart, - ElementsContainerType const& InputElements, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance @@ -1016,15 +1360,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch /** * @brief Search neighbours for every element in "Inputelements" excluding itself - * @param StructureElements List of elements against which the neighbours are searched - * @param InputElements List of elements to be searched - * @param Radius List of search radius for every element - * @param rResults Array of results for each element - * @param rResultDistance Array of distances for each result of each element + * @param rStructureConditions List of conditions against which the neighbours are searched + * @param InputElements List of elements to be searched + * @param rRadius List of search radius for every element + * @param rResults Array of results for each element + * @param rResultDistance Array of distances for each result of each element */ virtual void SearchElementsOverConditionsInRadiusInclusive ( - ConditionsContainerType const& StructureElements, - ElementsContainerType const& InputElements, + const ConditionsContainerType& rStructureConditions, + const ElementsContainerType& rInputElements, const RadiusArrayType& rRadius, VectorResultElementsContainerType& rResults, VectorDistanceType& rResultsDistance diff --git a/kratos/spatial_containers/specialized_spatial_search.h b/kratos/spatial_containers/specialized_spatial_search.h index 5ce6b2ec2811..ac939353f9c5 100644 --- a/kratos/spatial_containers/specialized_spatial_search.h +++ b/kratos/spatial_containers/specialized_spatial_search.h @@ -167,33 +167,26 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch /// The base type using BaseType = SpatialSearch; - /// Common Defines - using BaseType::PointType; + /// Nodes classes + using BaseType::NodesContainerType; + using BaseType::ResultNodesContainerType; + using BaseType::VectorResultNodesContainerType; + /// Elements classes using BaseType::ElementsContainerType; - using BaseType::ElementType; - using BaseType::ElementPointerType; using BaseType::ResultElementsContainerType; using BaseType::VectorResultElementsContainerType; - using BaseType::NodesContainerType; - using BaseType::NodeType; - using BaseType::NodePointerType; - using BaseType::ResultNodesContainerType; - using BaseType::VectorResultNodesContainerType; - + /// Conditions classes using BaseType::ConditionsContainerType; - using BaseType::ConditionType; - using BaseType::ConditionPointerType; using BaseType::ResultConditionsContainerType; using BaseType::VectorResultConditionsContainerType; + /// Input/output types using BaseType::RadiusArrayType; using BaseType::DistanceType; using BaseType::VectorDistanceType; - using BaseType::ResultIteratorType; - ///@} ///@name Life Cycle ///@{ From 24a100d163a628c83359c161aa5c2cbd482ad4b4 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 15:20:38 +0200 Subject: [PATCH 070/145] Expose to python --- .../add_search_strategies_to_python.cpp | 129 ++++++++++++------ 1 file changed, 89 insertions(+), 40 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index a385bdc35fa8..4232579f0bd4 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -267,8 +267,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, - const ElementsContainerType& rInputElements, py::list& rListOfRadius) { + .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusExclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; @@ -281,8 +283,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, - const ElementsContainerType& rStructureElements, py::list& rListOfRadius) { + .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ElementsContainerType& rInputElements, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusExclusive(rModelPart, rInputElements, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rStructureElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; @@ -295,9 +299,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, - const ElementsContainerType& rStructureElements, - const ElementsContainerType& rInputElements, py::list& rListOfRadius) { + .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusExclusive(rStructureElements, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; @@ -310,6 +315,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) + .def("SearchElementsInRadiusExclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const ElementsContainerType& rInputElements, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusExclusive(rStructureElements, rInputElements, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); @@ -323,6 +331,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) + .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusInclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array @@ -337,8 +348,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, - const ElementsContainerType& rStructureElements, py::list& rListOfRadius) { + .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ElementsContainerType& rInputElements, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusInclusive(rModelPart, rInputElements, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rStructureElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; @@ -351,9 +364,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, - const ElementsContainerType& rStructureElements, - const ElementsContainerType& rInputElements, py::list& rListOfRadius) { + .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusInclusive(rStructureElements, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const ElementsContainerType& rInputElements, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputElements.size()) << "The size of the radius array must be equal to the size of the input elements array" << std::endl; @@ -366,6 +380,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) + .def("SearchElementsInRadiusInclusive", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const ElementsContainerType& rInputElements, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsInRadiusInclusive(rStructureElements, rInputElements, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); @@ -379,8 +396,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, - const NodesContainerType& rInputNodes, py::list& rListOfRadius) { + .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusExclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; @@ -393,8 +412,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, - const NodesContainerType& rStructureNodes, py::list& rListOfRadius) { + .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusExclusive(rModelPart, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rStructureNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; @@ -407,9 +428,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, - const NodesContainerType& rStructureNodes, - const NodesContainerType& rInputNodes, py::list& rListOfRadius) { + .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusExclusive(rStructureNodes, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; @@ -422,6 +444,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) + .def("SearchNodesInRadiusExclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusExclusive(rStructureNodes, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); @@ -435,8 +460,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, - const NodesContainerType& rInputNodes, py::list& rListOfRadius) { + .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusInclusive(rStructureNodes, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; @@ -449,8 +476,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, - const NodesContainerType& rStructureNodes, py::list& rListOfRadius) { + .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusInclusive(rModelPart, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rStructureNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; @@ -463,9 +492,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, - const NodesContainerType& rStructureNodes, - const NodesContainerType& rInputNodes, py::list& rListOfRadius) { + .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusInclusive(rStructureNodes, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputNodes.size()) << "The size of the radius array must be equal to the size of the input nodes array" << std::endl; @@ -478,6 +508,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) + .def("SearchNodesInRadiusInclusive", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesInRadiusInclusive(rStructureNodes, rInputNodes, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); @@ -491,8 +524,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, - const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { + .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusExclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; @@ -505,8 +540,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, - const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius) { + .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusExclusive(rModelPart, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rStructureConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; @@ -519,9 +556,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, - const ConditionsContainerType& rStructureConditions, - const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { + .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusExclusive(rStructureConditions, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; @@ -534,6 +572,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) + .def("SearchConditionsInRadiusExclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusExclusive(rStructureConditions, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); @@ -547,8 +588,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, - const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { + .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusInclusive(rModelPart, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; @@ -561,8 +604,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, - const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius) { + .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, ModelPart& rModelPart, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusInclusive(rModelPart, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rStructureConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; @@ -575,9 +620,10 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) - .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, - const ConditionsContainerType& rStructureConditions, - const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { + .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusInclusive(rStructureConditions, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius) { // Get the size of the radius array const std::size_t size_array = rListOfRadius.size(); KRATOS_DEBUG_ERROR_IF(size_array != rInputConditions.size()) << "The size of the radius array must be equal to the size of the input conditions array" << std::endl; @@ -590,6 +636,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) // Copy the results to the python list return GenerateSpatialSearchSolutionTuple(results, distances); }) + .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsInRadiusInclusive(rStructureConditions, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) ; py::class_(m, "SpecializedSpatialSearch") From 3f0c0e1862a0c9e0a9c5d07c68189320e47f0351 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 16:04:07 +0200 Subject: [PATCH 071/145] Rename method --- .../add_search_strategies_to_python.cpp | 2 +- .../spatial_search_result_container.h | 2 +- .../test_geometrical_objects_bins.cpp | 24 +++++++++---------- kratos/tests/test_geometrical_object_bins.py | 8 +++---- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 4232579f0bd4..0af24be78fae 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -176,7 +176,7 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& using ContainerMapType = SpatialSearchResultContainerMap; pybind11::class_(m, rClassName.c_str()) .def(pybind11::init<>()) - .def("NumberOfPointsResults", &ContainerMapType::NumberOfPointsResults) + .def("NumberOfSearchResults", &ContainerMapType::NumberOfSearchResults) .def("InitializeResult", [](ContainerMapType& self, const std::size_t Index) { self.InitializeResult(Index); }) diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index a77e5bf3ac22..05758b475e64 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -885,7 +885,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap * @brief Returns the number of points results * @return The number of points results */ - std::size_t NumberOfPointsResults() const + std::size_t NumberOfSearchResults() const { return mPointResults.size(); } diff --git a/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp b/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp index e425b01e62cb..625e7933b915 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp @@ -223,37 +223,37 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchInRadiusContainer, KratosF // 0.29 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.29, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK_IS_FALSE(results[point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 0); // 0.3 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.3, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 4); // 0.4 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.4, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 4); // 0.6 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.6, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 8); // 0.7 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.7, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 8); // 0.9 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.9, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 12); } @@ -314,12 +314,12 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestInRadiusContainer, GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchNearestInRadius(r_array_nodes.begin(), r_array_nodes.end(), cube_z - 1.e-4, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK_IS_FALSE(results[near_point].IsObjectFound()); bins.SearchNearestInRadius(r_array_nodes.begin(), r_array_nodes.end(), cube_z + 1.e-4, results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[near_point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[near_point].NumberOfGlobalResults(), 1); @@ -386,7 +386,7 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestContainer, KratosFa GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchNearest(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[near_point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[near_point].NumberOfGlobalResults(), 1); @@ -438,7 +438,7 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsEmptySearchNearestContainer, Kra GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchNearest(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK_IS_FALSE(results[point].IsObjectFound()); } @@ -489,7 +489,7 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsInsideContainer, KratosF GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchIsInside(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK(results[inside_point].IsObjectFound()); KRATOS_CHECK_EQUAL(results[inside_point].NumberOfGlobalResults(), 1); } @@ -538,7 +538,7 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsNotInsideContainer, Krat GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchIsInside(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfPointsResults(), 1); + KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); KRATOS_CHECK_IS_FALSE(results[outside_point].IsObjectFound()); } diff --git a/kratos/tests/test_geometrical_object_bins.py b/kratos/tests/test_geometrical_object_bins.py index 5842c837d049..72dbe318636e 100644 --- a/kratos/tests/test_geometrical_object_bins.py +++ b/kratos/tests/test_geometrical_object_bins.py @@ -80,7 +80,7 @@ def test_GeometricalObjectsBins_SearchInRadius(self): # Nodes array search results = self.search.SearchInRadius(self.sub_model_part.Nodes, radius) - self.assertEqual(results.NumberOfPointsResults(), 1) + self.assertEqual(results.NumberOfSearchResults(), 1) node_results = results[self.node_coordinates] node_results.SynchronizeAll(self.data_comm) self.assertEqual(node_results.NumberOfGlobalResults(), 8) @@ -100,7 +100,7 @@ def test_GeometricalObjectsBins_SearchNearestInRadius(self): # Nodes array search results = self.search.SearchNearestInRadius(self.sub_model_part.Nodes, radius) - self.assertEqual(results.NumberOfPointsResults(), 1) + self.assertEqual(results.NumberOfSearchResults(), 1) node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) # Local result @@ -120,7 +120,7 @@ def test_GeometricalObjectsBins_SearchNearest(self): # Nodes array search results = self.search.SearchNearest(self.sub_model_part.Nodes) - self.assertEqual(results.NumberOfPointsResults(), 1) + self.assertEqual(results.NumberOfSearchResults(), 1) node_results = results[self.node_coordinates] self.assertEqual(node_results.NumberOfGlobalResults(), 1) # Local result @@ -138,7 +138,7 @@ def test_GeometricalObjectsBins_SearchIsInside(self): # Nodes array search results = self.search.SearchIsInside(self.sub_model_part.Nodes) - self.assertEqual(results.NumberOfPointsResults(), 1) + self.assertEqual(results.NumberOfSearchResults(), 1) node_results = results[self.node_coordinates] self.assertFalse(node_results.IsObjectFound()) From 69b0ca9dad497fe7e7f92698f89d05922c0c57dc Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 16:30:26 +0200 Subject: [PATCH 072/145] WIP test container interface --- kratos/tests/test_spatial_search.py | 150 ++++++++++++++++++---------- 1 file changed, 97 insertions(+), 53 deletions(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index 38b0cddcb040..07b9bfe467b6 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -1,6 +1,10 @@ # Importing the Kratos Library import KratosMultiphysics as KM +# Importing the MPI +if KM.IsDistributedRun(): + import KratosMultiphysics.mpi as KratosMPI + # Import KratosUnittest import KratosMultiphysics.KratosUnittest as KratosUnittest @@ -29,6 +33,9 @@ def setUpClass(cls): cls.model_part.AddNodalSolutionStepVariable(KM.NODAL_VAUX) cls.model_part.AddNodalSolutionStepVariable(KM.EXTERNAL_FORCES_VECTOR) cls.model_part.AddNodalSolutionStepVariable(KM.LOCAL_AXES_MATRIX) + # Adding PARTITION_INDEX + if KM.IsDistributedRun(): + cls.model_part.AddNodalSolutionStepVariable(KM.PARTITION_INDEX) cls.mdpa_name = GetFilePath("auxiliar_files_for_python_unittest/mdpa_files/coarse_sphere_with_conditions") ReadModelPart(cls.mdpa_name, cls.model_part) @@ -46,86 +53,123 @@ def setUp(self): } } """) + + # Get the data communicator + self.data_comm = self.model_part.GetCommunicator().GetDataCommunicator() - def test_KDTree_nodes(self): - # Create search - self.settings["container_type"].SetString("KDTree") - self.search = KM.SpecializedSpatialSearch(self.settings) + def GenerateSearch(self, container_type = "KDTree"): + self.settings["container_type"].SetString(container_type) + if KM.IsDistributedRun(): + raise Exception("MPI version comming in a future PR") + else: + self.search = KM.SpecializedSpatialSearch(self.settings) # Create node for search - second_model_part = self.current_model.CreateModelPart("KDTree") - second_model_part.CreateNewNode(100000, 0.0, 0.0, 0.0) - radius_list = [0.3] - [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, second_model_part.Nodes, radius_list) + self.new_node_id = 100000 + self.second_model_part = self.current_model.CreateModelPart(container_type) + self.second_model_part.CreateNewNode(self.new_node_id, 0.0, 0.0, 0.0) - self.assertEqual(len(results), 1) - self.assertEqual(len(results[0]), 7) + def test_KDTree_nodes(self): + # Create search + self.GenerateSearch("KDTree") + + # Reference solution + radius_list = [0.3] distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] - for distance in distances[0]: - self.assertTrue(distance in distance_ref) - for node in results[0]: - self.assertTrue(node.Id in node_id_ref) + + # Serial interface + if not KM.IsDistributedRun(): + [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list) + + # Assert results + self.assertEqual(len(results), len(radius_list)) + self.assertEqual(len(results[0]), len(distance_ref)) + + for distance in distances[0]: + self.assertTrue(distance in distance_ref) + for node in results[0]: + self.assertTrue(node.Id in node_id_ref) + + # Parallel interface (also works in serial mode) + results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) + self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) def test_Octree_nodes(self): # Create search - self.settings["container_type"].SetString("Octree") - self.search = KM.SpecializedSpatialSearch(self.settings) + self.GenerateSearch("Octree") - # Create node for search - second_model_part = self.current_model.CreateModelPart("Octree") - second_model_part.CreateNewNode(100000, 0.0, 0.0, 0.0) + # Reference solution radius_list = [0.3] - [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, second_model_part.Nodes, radius_list) - - self.assertEqual(len(results), 1) - self.assertEqual(len(results[0]), 7) distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] - for distance in distances[0]: - self.assertTrue(distance in distance_ref) - for node in results[0]: - self.assertTrue(node.Id in node_id_ref) + + # Serial interface + if not KM.IsDistributedRun(): + [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list) + + # Assert results + self.assertEqual(len(results), len(radius_list)) + self.assertEqual(len(results[0]), len(distance_ref)) + + for distance in distances[0]: + self.assertTrue(distance in distance_ref) + for node in results[0]: + self.assertTrue(node.Id in node_id_ref) + + # Parallel interface (also works in serial mode) + results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) + self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) def test_BinsStatic_nodes(self): # Create search - self.settings["container_type"].SetString("BinsStatic") - self.search = KM.SpecializedSpatialSearch(self.settings) + self.GenerateSearch("BinsStatic") - # Create node for search - second_model_part = self.current_model.CreateModelPart("BinsStatic") - second_model_part.CreateNewNode(100000, 0.0, 0.0, 0.0) + # Reference solution radius_list = [0.3] - [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, second_model_part.Nodes, radius_list) - - self.assertEqual(len(results), 1) - self.assertEqual(len(results[0]), 7) distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] - for distance in distances[0]: - self.assertTrue(distance in distance_ref) - for node in results[0]: - self.assertTrue(node.Id in node_id_ref) + + # Serial interface + if not KM.IsDistributedRun(): + [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list) + + # Assert results + self.assertEqual(len(results), len(radius_list)) + self.assertEqual(len(results[0]), len(distance_ref)) + for distance in distances[0]: + self.assertTrue(distance in distance_ref) + for node in results[0]: + self.assertTrue(node.Id in node_id_ref) + + # Parallel interface (also works in serial mode) + results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) + self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) def test_BinsDynamic_nodes(self): # Create search - self.settings["container_type"].SetString("BinsDynamic") - self.search = KM.SpecializedSpatialSearch(self.settings) + self.GenerateSearch("BinsDynamic") - # Create node for search - second_model_part = self.current_model.CreateModelPart("BinsDynamic") - second_model_part.CreateNewNode(100000, 0.0, 0.0, 0.0) + # Reference solution radius_list = [0.3] - [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, second_model_part.Nodes, radius_list) - - self.assertEqual(len(results), 1) - self.assertEqual(len(results[0]), 7) distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] - for distance in distances[0]: - self.assertTrue(distance in distance_ref) - for node in results[0]: - self.assertTrue(node.Id in node_id_ref) + + # Serial interface + if not KM.IsDistributedRun(): + [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list) + + # Assert results + self.assertEqual(len(results), len(radius_list)) + self.assertEqual(len(results[0]), len(distance_ref)) + for distance in distances[0]: + self.assertTrue(distance in distance_ref) + for node in results[0]: + self.assertTrue(node.Id in node_id_ref) + + # Parallel interface (also works in serial mode) + results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) + self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) if __name__ == '__main__': KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) From 6fe5b0ad3ee0d1eb0f9ea5b099ccd18a549f4225 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 19:30:31 +0200 Subject: [PATCH 073/145] Missing return --- kratos/spatial_containers/spatial_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 671cafee16a4..22bb02ff9a3a 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -481,7 +481,7 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode const DataCommunicator& rDataCommunicator ) { - this->SearchNodesInRadiusExclusive(rStructureNodes, rStructureNodes, rRadius, rDataCommunicator); + return this->SearchNodesInRadiusExclusive(rStructureNodes, rStructureNodes, rRadius, rDataCommunicator); } /***********************************************************************************/ From 27f3b3429367126d11468d07ade168395bc91734 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 19:47:21 +0200 Subject: [PATCH 074/145] Acess method --- .../spatial_search_result_container.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 05758b475e64..17e0febb73dc 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -929,6 +929,19 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap */ void SynchronizeAll(const DataCommunicator& rDataCommunicator); + ///@} + ///@name Access + ///@{ + + /** + * @brief Get the container + * @return The container + */ + ContainerType& GetContainer() + { + return mPointResults; + } + ///@} ///@name Input and output ///@{ From 7dde2a292981450a94298520df3f06cb93c0ded1 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 7 Jun 2023 19:51:50 +0200 Subject: [PATCH 075/145] Fix python binding --- kratos/python/add_search_strategies_to_python.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 0af24be78fae..eb217576e181 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -178,16 +178,16 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& .def(pybind11::init<>()) .def("NumberOfSearchResults", &ContainerMapType::NumberOfSearchResults) .def("InitializeResult", [](ContainerMapType& self, const std::size_t Index) { - self.InitializeResult(Index); + return self.InitializeResult(Index); }) .def("InitializeResult", [](ContainerMapType& self, const array_1d& rCoordinates) { - self.InitializeResult(rCoordinates); + return self.InitializeResult(rCoordinates); }) .def("HasResult", [](ContainerMapType& self, const std::size_t Index) { - self.HasResult(Index); + return self.HasResult(Index); }) - .def("InitializeResult", [](ContainerMapType& self, const array_1d& rCoordinates) { - self.HasResult(rCoordinates); + .def("HasResult", [](ContainerMapType& self, const array_1d& rCoordinates) { + return self.HasResult(rCoordinates); }) .def("Clear", &ContainerMapType::Clear) .def("__getitem__", [](ContainerMapType& self, const std::size_t Index) { From 00c375423397e74eed499e19a0e7fbcd6e0d0ba3 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 10:39:15 +0200 Subject: [PATCH 076/145] Adding print method --- kratos/python/add_search_strategies_to_python.cpp | 10 ++++++++++ .../spatial_search_result_container.cpp | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index eb217576e181..ce6bbc23f97a 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -153,6 +153,11 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC KRATOS_ERROR_IF(self.GetGlobalPointerCommunicator() == nullptr) << "The communicator has not been created. Therefore is not synchronized" << std::endl; return *(self.GetGlobalPointers().GetContainer().begin() + Index); }) + .def("__repr__", [](ContainerType& self) { + std::ostringstream os; + self.PrintData(os); + return os.str(); + }) .def("__iter__", [](ContainerType& self) { return pybind11::make_iterator(self.begin(), self.end()); }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ @@ -202,6 +207,11 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& .def("__call__", [](ContainerMapType& self, const array_1d& rCoordinates) { return self(rCoordinates); }) + .def("__repr__", [](ContainerMapType& self) { + std::ostringstream os; + self.PrintData(os); + return os.str(); + }) .def("__iter__", [](ContainerMapType& self) { return pybind11::make_iterator(self.begin(), self.end()); }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index 86b8e1dcf530..5b3fa68dac96 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -283,7 +283,7 @@ std::string SpatialSearchResultContainer::Info() const template void SpatialSearchResultContainer::PrintInfo(std::ostream& rOStream) const { - rOStream << "SpatialSearchResultContainer"; + rOStream << "SpatialSearchResultContainer" << "\n"; } /***********************************************************************************/ @@ -430,7 +430,7 @@ std::string SpatialSearchResultContainerMap::Info() const template void SpatialSearchResultContainerMap::PrintInfo(std::ostream& rOStream) const { - rOStream << "SpatialSearchResultContainerMap"; + rOStream << "SpatialSearchResultContainerMap" << "\n"; } /***********************************************************************************/ From 3b0d17f20bd307c8b7246f289a41a1ff078d295c Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 10:59:10 +0200 Subject: [PATCH 077/145] Adding << operator --- .../add_search_strategies_to_python.cpp | 4 +- kratos/spatial_containers/spatial_search.cpp | 3 ++ .../spatial_search_result_container.h | 53 ++++++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index ce6bbc23f97a..dca52f12b827 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -155,7 +155,7 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC }) .def("__repr__", [](ContainerType& self) { std::ostringstream os; - self.PrintData(os); + os << self << std::endl; return os.str(); }) .def("__iter__", [](ContainerType& self) { @@ -209,7 +209,7 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& }) .def("__repr__", [](ContainerMapType& self) { std::ostringstream os; - self.PrintData(os); + os << self << std::endl; return os.str(); }) .def("__iter__", [](ContainerMapType& self) { diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 22bb02ff9a3a..3aeccf8eb8df 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -534,6 +534,9 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode // Synchronize all the results result.SynchronizeAll(rDataCommunicator); + + KRATOS_WATCH(result) + return result; } diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 17e0febb73dc..ae51cead0dc7 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -86,6 +86,17 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@name Operators ///@{ + /** + * @brief Operator << + * @param os The output stream + * @return The output stream + */ + std::ostream& operator<<(std::ostream& os) + { + this->PrintData(os); + return os; + } + /** * @brief Operator [] * @param Index The index @@ -541,6 +552,26 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer ///@} }; // Class SpatialSearchResultContainer +/// input stream function +template +inline std::istream& operator>>(std::istream& rIStream, + SpatialSearchResultContainer& rThis) +{ + return rIStream; +} + +/// output stream function +template +inline std::ostream& operator<<(std::ostream& rOStream, + const SpatialSearchResultContainer& rThis) +{ + rThis.PrintInfo(rOStream); + rOStream << std::endl; + rThis.PrintData(rOStream); + + return rOStream; +} + /** * @class SpatialSearchResultContainerMap * @brief Spatial search result container map @@ -992,7 +1023,27 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainerMap void load(Serializer& rSerializer); ///@} -}; // Class SpatialSearchResultContainer +}; // Class SpatialSearchResultContainerMap + +/// input stream function +template +inline std::istream& operator>>(std::istream& rIStream, + SpatialSearchResultContainerMap& rThis) +{ + return rIStream; +} + +/// output stream function +template +inline std::ostream& operator<<(std::ostream& rOStream, + const SpatialSearchResultContainerMap& rThis) +{ + rThis.PrintInfo(rOStream); + rOStream << std::endl; + rThis.PrintData(rOStream); + + return rOStream; +} ///@} From 54a6499b3936d0eff5edda2c1dfef3c047fdede9 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 11:07:46 +0200 Subject: [PATCH 078/145] Minor revert --- kratos/spatial_containers/spatial_search.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 3aeccf8eb8df..6fdd39caf74c 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -535,8 +535,6 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode // Synchronize all the results result.SynchronizeAll(rDataCommunicator); - KRATOS_WATCH(result) - return result; } From 569c60db597c495e68445092d3fff787e274dd81 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 11:08:10 +0200 Subject: [PATCH 079/145] Minor --- kratos/spatial_containers/spatial_search.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 6fdd39caf74c..22bb02ff9a3a 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -534,7 +534,6 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode // Synchronize all the results result.SynchronizeAll(rDataCommunicator); - return result; } From bafed25bc8a873522623e303d1a436ea6a1358f9 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 11:32:05 +0200 Subject: [PATCH 080/145] Missing references --- kratos/spatial_containers/spatial_search.cpp | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 22bb02ff9a3a..c5e9db8323ef 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -141,10 +141,10 @@ SpatialSearch::ElementSpatialSearchResultContainerMapType SpatialSearch::SearchE const IndexType id = (rInputElements.begin() + i)->Id(); // Adding partial results - auto result_i = result.InitializeResult(id); + auto& r_result_i = result.InitializeResult(id); for (IndexType j = 0; j < r_partial_results.size(); ++j) { auto p_element = (*((r_partial_results.begin() + j).base())).get(); - result_i.AddResult(p_element, r_partial_distances[j]); + r_result_i.AddResult(p_element, r_partial_distances[j]); } } @@ -277,10 +277,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchElem const IndexType id = (rInputElements.begin() + i)->Id(); // Adding partial results - auto result_i = result.InitializeResult(id); + auto& r_result_i = result.InitializeResult(id); for (IndexType j = 0; j < r_partial_results.size(); ++j) { auto p_node = (*((r_partial_results.begin() + j).base())).get(); - result_i.AddResult(p_node, r_partial_distances[j]); + r_result_i.AddResult(p_node, r_partial_distances[j]); } } @@ -525,10 +525,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode const IndexType id = (rInputNodes.begin() + i)->Id(); // Adding partial results - auto result_i = result.InitializeResult(id); + auto& r_result_i = result.InitializeResult(id); for (IndexType j = 0; j < r_partial_results.size(); ++j) { auto p_node = (*((r_partial_results.begin() + j).base())).get(); - result_i.AddResult(p_node, r_partial_distances[j]); + r_result_i.AddResult(p_node, r_partial_distances[j]); } } @@ -661,10 +661,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode const IndexType id = (rInputNodes.begin() + i)->Id(); // Adding partial results - auto result_i = result.InitializeResult(id); + auto& r_result_i = result.InitializeResult(id); for (IndexType j = 0; j < r_partial_results.size(); ++j) { auto p_node = (*((r_partial_results.begin() + j).base())).get(); - result_i.AddResult(p_node, r_partial_distances[j]); + r_result_i.AddResult(p_node, r_partial_distances[j]); } } @@ -909,10 +909,10 @@ SpatialSearch::ConditionSpatialSearchResultContainerMapType SpatialSearch::Searc const IndexType id = (rInputConditions.begin() + i)->Id(); // Adding partial results - auto result_i = result.InitializeResult(id); + auto& r_result_i = result.InitializeResult(id); for (IndexType j = 0; j < r_partial_results.size(); ++j) { auto p_condition = (*((r_partial_results.begin() + j).base())).get(); - result_i.AddResult(p_condition, r_partial_distances[j]); + r_result_i.AddResult(p_condition, r_partial_distances[j]); } } @@ -1045,10 +1045,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchCond const IndexType id = (rInputConditions.begin() + i)->Id(); // Adding partial results - auto result_i = result.InitializeResult(id); + auto& r_result_i = result.InitializeResult(id); for (IndexType j = 0; j < r_partial_results.size(); ++j) { auto p_node = (*((r_partial_results.begin() + j).base())).get(); - result_i.AddResult(p_node, r_partial_distances[j]); + r_result_i.AddResult(p_node, r_partial_distances[j]); } } From 9a16cd6fc0cbd6d95e2d38b11637e98f654c2e13 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 11:36:26 +0200 Subject: [PATCH 081/145] Update the test --- kratos/tests/test_spatial_search.py | 58 ++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index 07b9bfe467b6..6a58ab40f97f 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -64,10 +64,20 @@ def GenerateSearch(self, container_type = "KDTree"): else: self.search = KM.SpecializedSpatialSearch(self.settings) + # Creating submodel part + self.second_model_part = self.current_model.CreateModelPart(container_type) + # Create node for search self.new_node_id = 100000 - self.second_model_part = self.current_model.CreateModelPart(container_type) - self.second_model_part.CreateNewNode(self.new_node_id, 0.0, 0.0, 0.0) + if KM.IsDistributedRun(): + # Only added to first rank to actualy check it works in all ranks + if self.data_comm.Rank() == 0: + self.node = self.second_model_part.CreateNewNode(self.new_node_id, 0.0, 0.0, 0.0) + self.node.SetSolutionStepValue(KM.PARTITION_INDEX, 0) + ParallelFillCommunicator = KratosMPI.ParallelFillCommunicator(self.model_part, self.data_comm) + ParallelFillCommunicator.Execute() + else: + self.node = self.second_model_part.CreateNewNode(self.new_node_id, 0.0, 0.0, 0.0) def test_KDTree_nodes(self): # Create search @@ -94,6 +104,17 @@ def test_KDTree_nodes(self): # Parallel interface (also works in serial mode) results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) + self.assertTrue(results.HasResult(self.new_node_id)) + node_results = results[self.new_node_id] + if not KM.IsDistributedRun(): + self.assertEqual(node_results.NumberOfLocalResults(), len(distance_ref)) + self.assertEqual(node_results.NumberOfGlobalResults(), len(distance_ref)) + ids = node_results.GetResultIndices() + for id in ids: + self.assertTrue(id in node_id_ref) + distances = node_results.GetDistances() + for distance in distances: + self.assertTrue(distance in distance_ref) def test_Octree_nodes(self): # Create search @@ -120,6 +141,17 @@ def test_Octree_nodes(self): # Parallel interface (also works in serial mode) results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) + self.assertTrue(results.HasResult(self.new_node_id)) + node_results = results[self.new_node_id] + if not KM.IsDistributedRun(): + self.assertEqual(node_results.NumberOfLocalResults(), len(distance_ref)) + self.assertEqual(node_results.NumberOfGlobalResults(), len(distance_ref)) + ids = node_results.GetResultIndices() + for id in ids: + self.assertTrue(id in node_id_ref) + distances = node_results.GetDistances() + for distance in distances: + self.assertTrue(distance in distance_ref) def test_BinsStatic_nodes(self): # Create search @@ -145,6 +177,17 @@ def test_BinsStatic_nodes(self): # Parallel interface (also works in serial mode) results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) + self.assertTrue(results.HasResult(self.new_node_id)) + node_results = results[self.new_node_id] + if not KM.IsDistributedRun(): + self.assertEqual(node_results.NumberOfLocalResults(), len(distance_ref)) + self.assertEqual(node_results.NumberOfGlobalResults(), len(distance_ref)) + ids = node_results.GetResultIndices() + for id in ids: + self.assertTrue(id in node_id_ref) + distances = node_results.GetDistances() + for distance in distances: + self.assertTrue(distance in distance_ref) def test_BinsDynamic_nodes(self): # Create search @@ -170,6 +213,17 @@ def test_BinsDynamic_nodes(self): # Parallel interface (also works in serial mode) results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) + self.assertTrue(results.HasResult(self.new_node_id)) + node_results = results[self.new_node_id] + if not KM.IsDistributedRun(): + self.assertEqual(node_results.NumberOfLocalResults(), len(distance_ref)) + self.assertEqual(node_results.NumberOfGlobalResults(), len(distance_ref)) + ids = node_results.GetResultIndices() + for id in ids: + self.assertTrue(id in node_id_ref) + distances = node_results.GetDistances() + for distance in distances: + self.assertTrue(distance in distance_ref) if __name__ == '__main__': KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) From 78ddf5591a2b1f246e9f401452aa6c5ed2a3e21b Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 11:44:03 +0200 Subject: [PATCH 082/145] Standard PrintObject --- kratos/python/add_search_strategies_to_python.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index dca52f12b827..1d9d553adb88 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -153,11 +153,7 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC KRATOS_ERROR_IF(self.GetGlobalPointerCommunicator() == nullptr) << "The communicator has not been created. Therefore is not synchronized" << std::endl; return *(self.GetGlobalPointers().GetContainer().begin() + Index); }) - .def("__repr__", [](ContainerType& self) { - std::ostringstream os; - os << self << std::endl; - return os.str(); - }) + .def("__str__", PrintObject) .def("__iter__", [](ContainerType& self) { return pybind11::make_iterator(self.begin(), self.end()); }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ @@ -207,11 +203,7 @@ void BindSpatialSearchResultContainerMap(pybind11::module& m, const std::string& .def("__call__", [](ContainerMapType& self, const array_1d& rCoordinates) { return self(rCoordinates); }) - .def("__repr__", [](ContainerMapType& self) { - std::ostringstream os; - os << self << std::endl; - return os.str(); - }) + .def("__str__", PrintObject) .def("__iter__", [](ContainerMapType& self) { return pybind11::make_iterator(self.begin(), self.end()); }, pybind11::keep_alive<0, 1>()); /* Keep object alive while iterator is used */ From 9050247e072bc9cebff7d6f998c3e8d6f3646163 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 11:51:44 +0200 Subject: [PATCH 083/145] [Core] Extend definitions from `define_python`, adding `VectorToPyList` and `MatrixToPyList` --- kratos/includes/define_python.h | 53 ++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/kratos/includes/define_python.h b/kratos/includes/define_python.h index 91e3a220496d..83c8da3c98f3 100644 --- a/kratos/includes/define_python.h +++ b/kratos/includes/define_python.h @@ -16,6 +16,8 @@ // External includes #include +#include +#include // Project includes #include "intrusive_ptr/intrusive_ptr.hpp" @@ -23,9 +25,6 @@ // Always needed for custom holder types PYBIND11_DECLARE_HOLDER_TYPE(T, Kratos::intrusive_ptr); -#include -#include - #include "includes/define.h" @@ -103,11 +102,13 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, Kratos::intrusive_ptr); #define KRATOS_REGISTER_IN_PYTHON_FLAG(module,flag) \ KRATOS_REGISTER_IN_PYTHON_FLAG_IMPLEMENTATION(module,flag); -// This function is used to print the ofstream-operator -// i.e. printing an object will give the same result in Python as in C++ -// To be defined as the "__str__" function -// e.g. ".def("__str__", PrintObject)" -// It replicates the function "self_ns::str(self))" of boost-python +/** + * @brief This function is used to print the ofstream-operator + * @details i.e. printing an object will give the same result in Python as in C++ + * To be defined as the "__str__" function + * e.g. ".def("__str__", PrintObject)" + * It replicates the function "self_ns::str(self))" of boost-python + */ template< class T> std::string PrintObject(const T& rObject) { @@ -115,3 +116,39 @@ std::string PrintObject(const T& rObject) ss << rObject; return ss.str(); } + +/** + * @brief Converts a vector to a Python list using pybind11. + * @details This function is generic enough to be moved to a more general place. + * @tparam T The type of the vector. + * @param results The vector to convert. + * @return The converted Python list. + */ +template +pybind11::list VectorToPyList(const T& results) { + pybind11::list list_results; + for (auto& r_result : results) { + list_results.append(r_result); + } + return list_results; +} + +/** + * @brief Converts a matrix to a nested Python list using pybind11. + * @details This function is generic enough to be moved to a more general place. + * @tparam T The type of the matrix. + * @param results The matrix to convert. + * @return The converted nested Python list. + */ +template +pybind11::list MatrixToPyList(const T& results) { + pybind11::list list_results; + for (auto& r_result : results) { + pybind11::list i_list_results; + for (auto& r_sub_result : r_result) { + i_list_results.append(r_sub_result); + } + list_results.append(i_list_results); + } + return list_results; +} \ No newline at end of file From 9dfc683dc805a6f04e41f27b69589638364bc4ea Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 11:51:56 +0200 Subject: [PATCH 084/145] Moving methods --- .../add_search_strategies_to_python.cpp | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 1d9d553adb88..e24d2b51afc9 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -29,42 +29,6 @@ namespace Kratos::Python { -/** - * @brief Converts a vector to a Python list using pybind11. - * @details This function is generic enough to be moved to a more general place. - * @tparam T The type of the vector. - * @param results The vector to convert. - * @return The converted Python list. - */ -template -pybind11::list VectorToPyList(const T& results) { - pybind11::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; -} - -/** - * @brief Converts a matrix to a nested Python list using pybind11. - * @details This function is generic enough to be moved to a more general place. - * @tparam T The type of the matrix. - * @param results The matrix to convert. - * @return The converted nested Python list. - */ -template -pybind11::list MatrixToPyList(const T& results) { - pybind11::list list_results; - for (auto& r_result : results) { - pybind11::list i_list_results; - for (auto& r_sub_result : r_result) { - i_list_results.append(r_sub_result); - } - list_results.append(i_list_results); - } - return list_results; -} - /** * @brief Generates a list of lists from a vector of vectors * @param rList The list to be filled From 7158b9b7683041c51e94ceebf6c3695d1d364a03 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 16:11:01 +0200 Subject: [PATCH 085/145] Remve unused --- .../custom_utilities/discrete_particle_configure.h | 1 - .../custom_utilities/geometrical_object_configure.h | 2 -- .../custom_utilities/rigid_face_geometrical_object_configure.h | 2 -- .../permeability_tensor_communicator_utility.hpp | 1 - .../custom_utilities/search/point_configure.h | 1 - 5 files changed, 7 deletions(-) diff --git a/applications/DEMApplication/custom_utilities/discrete_particle_configure.h b/applications/DEMApplication/custom_utilities/discrete_particle_configure.h index 1784993beccf..3c4c7c3d93d5 100644 --- a/applications/DEMApplication/custom_utilities/discrete_particle_configure.h +++ b/applications/DEMApplication/custom_utilities/discrete_particle_configure.h @@ -64,7 +64,6 @@ class DiscreteParticleConfigure typedef SearchType::ElementsContainerType ElementsContainerType; typedef SearchType::NodesContainerType NodesContainerType; - typedef SearchType::ElementType ElementType; typedef ContainerType::value_type PointerType; typedef ContainerType::iterator IteratorType; typedef ElementsContainerType::iterator ElementIteratorType; diff --git a/applications/DEMApplication/custom_utilities/geometrical_object_configure.h b/applications/DEMApplication/custom_utilities/geometrical_object_configure.h index cf202943d29e..ab2afd180982 100644 --- a/applications/DEMApplication/custom_utilities/geometrical_object_configure.h +++ b/applications/DEMApplication/custom_utilities/geometrical_object_configure.h @@ -60,7 +60,6 @@ class GeometricalConfigure // typedef SearchType::ElementsContainerType::ContainerType ContainerType; // typedef SearchType::ElementsContainerType ElementsContainerType; -// typedef SearchType::ElementType ElementType; // typedef ContainerType::value_type PointerType; // typedef ContainerType::iterator IteratorType; // typedef ElementsContainerType::iterator ElementIteratorType; @@ -84,7 +83,6 @@ class GeometricalConfigure typedef PointerVectorSet::ContainerType ContainerType; typedef PointerVectorSet ElementsContainerType; - typedef SearchType::ElementType ElementType; typedef ContainerType::value_type PointerType; typedef ContainerType::iterator IteratorType; // typedef ElementsContainerType::iterator ElementIteratorType; diff --git a/applications/DEMApplication/custom_utilities/rigid_face_geometrical_object_configure.h b/applications/DEMApplication/custom_utilities/rigid_face_geometrical_object_configure.h index 2d718cd0a88b..c8790c6764e8 100644 --- a/applications/DEMApplication/custom_utilities/rigid_face_geometrical_object_configure.h +++ b/applications/DEMApplication/custom_utilities/rigid_face_geometrical_object_configure.h @@ -78,8 +78,6 @@ class RigidFaceGeometricalObjectConfigure //typedef PointerVectorSet::ContainerType ContainerType; typedef typename ElementsContainerType::ContainerType ContainerType; - - typedef SearchType::ElementType ElementType; typedef ContainerType::value_type PointerType; typedef ContainerType::iterator IteratorType; diff --git a/applications/DemStructuresCouplingApplication/custom_utilities/permeability_tensor_communicator_utility.hpp b/applications/DemStructuresCouplingApplication/custom_utilities/permeability_tensor_communicator_utility.hpp index 5ab04f6270ea..dcbf805d96d0 100644 --- a/applications/DemStructuresCouplingApplication/custom_utilities/permeability_tensor_communicator_utility.hpp +++ b/applications/DemStructuresCouplingApplication/custom_utilities/permeability_tensor_communicator_utility.hpp @@ -50,7 +50,6 @@ namespace Kratos { typedef SearchType::ElementsContainerType ElementsContainerType; typedef SearchType::ElementsContainerType::ContainerType ContainerType; typedef SearchType::NodesContainerType NodesContainerType; - typedef SearchType::ElementType ElementType; typedef ContainerType::value_type PointerType; typedef ContainerType::iterator IteratorType; typedef ElementsContainerType::iterator ElementIteratorType; diff --git a/applications/SwimmingDEMApplication/custom_utilities/search/point_configure.h b/applications/SwimmingDEMApplication/custom_utilities/search/point_configure.h index 1a216aabb58f..a63b428a6324 100644 --- a/applications/SwimmingDEMApplication/custom_utilities/search/point_configure.h +++ b/applications/SwimmingDEMApplication/custom_utilities/search/point_configure.h @@ -60,7 +60,6 @@ typedef SearchType::PointType Poin typedef PointerVectorSet::ContainerType ContainerType; typedef PointerVectorSet PointsContainerType; -typedef SearchType::ElementType ElementType; typedef ContainerType::value_type PointerType; typedef ContainerType::iterator IteratorType; From 8d8fb70565bb8d86ed66a25f514b6ea9f7a33c67 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 16:32:58 +0200 Subject: [PATCH 086/145] Automatic conversion --- .../python/add_search_strategies_to_python.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index e24d2b51afc9..193a2c0896bc 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -97,18 +97,10 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC }) .def("Clear", &ContainerType::Clear) .def("SynchronizeAll", &ContainerType::SynchronizeAll) - .def("GetDistances", [&](ContainerType& self) { - return VectorToPyList(self.GetDistances()); - }) - .def("GetResultShapeFunctions", [&](ContainerType& self, const array_1d& rPoint) { - return VectorToPyList(self.GetResultShapeFunctions(rPoint)); - }) - .def("GetResultIndices", [&](ContainerType& self) { - return VectorToPyList(self.GetResultIndices()); - }) - .def("GetResultCoordinates", [&](ContainerType& self) { - return MatrixToPyList(self.GetResultCoordinates()); - }) + .def("GetDistances", &ContainerType::GetDistances) + .def("GetResultShapeFunctions", &ContainerType::GetResultShapeFunctions) + .def("GetResultIndices", &ContainerType::GetResultIndices) + .def("GetResultCoordinates", &ContainerType::GetResultCoordinates) .def("__getitem__", [](ContainerType& self, const std::size_t Index) { return *(self.GetLocalPointers().GetContainer().begin() + Index); }) From eaad518f4adb155e3836881f7f2c5aa70607ff8a Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 16:33:00 +0200 Subject: [PATCH 087/145] Revert "[Core] Extend definitions from `define_python`, adding `VectorToPyList` and `MatrixToPyList`" This reverts commit 13dee6782a6a4fce6db4f27eb9ba04436145b5fa. --- kratos/includes/define_python.h | 53 +++++---------------------------- 1 file changed, 8 insertions(+), 45 deletions(-) diff --git a/kratos/includes/define_python.h b/kratos/includes/define_python.h index 83c8da3c98f3..91e3a220496d 100644 --- a/kratos/includes/define_python.h +++ b/kratos/includes/define_python.h @@ -16,8 +16,6 @@ // External includes #include -#include -#include // Project includes #include "intrusive_ptr/intrusive_ptr.hpp" @@ -25,6 +23,9 @@ // Always needed for custom holder types PYBIND11_DECLARE_HOLDER_TYPE(T, Kratos::intrusive_ptr); +#include +#include + #include "includes/define.h" @@ -102,13 +103,11 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, Kratos::intrusive_ptr); #define KRATOS_REGISTER_IN_PYTHON_FLAG(module,flag) \ KRATOS_REGISTER_IN_PYTHON_FLAG_IMPLEMENTATION(module,flag); -/** - * @brief This function is used to print the ofstream-operator - * @details i.e. printing an object will give the same result in Python as in C++ - * To be defined as the "__str__" function - * e.g. ".def("__str__", PrintObject)" - * It replicates the function "self_ns::str(self))" of boost-python - */ +// This function is used to print the ofstream-operator +// i.e. printing an object will give the same result in Python as in C++ +// To be defined as the "__str__" function +// e.g. ".def("__str__", PrintObject)" +// It replicates the function "self_ns::str(self))" of boost-python template< class T> std::string PrintObject(const T& rObject) { @@ -116,39 +115,3 @@ std::string PrintObject(const T& rObject) ss << rObject; return ss.str(); } - -/** - * @brief Converts a vector to a Python list using pybind11. - * @details This function is generic enough to be moved to a more general place. - * @tparam T The type of the vector. - * @param results The vector to convert. - * @return The converted Python list. - */ -template -pybind11::list VectorToPyList(const T& results) { - pybind11::list list_results; - for (auto& r_result : results) { - list_results.append(r_result); - } - return list_results; -} - -/** - * @brief Converts a matrix to a nested Python list using pybind11. - * @details This function is generic enough to be moved to a more general place. - * @tparam T The type of the matrix. - * @param results The matrix to convert. - * @return The converted nested Python list. - */ -template -pybind11::list MatrixToPyList(const T& results) { - pybind11::list list_results; - for (auto& r_result : results) { - pybind11::list i_list_results; - for (auto& r_sub_result : r_result) { - i_list_results.append(r_sub_result); - } - list_results.append(i_list_results); - } - return list_results; -} \ No newline at end of file From 048653ee22f6b4b97cc75c18e3c49ac679706702 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 8 Jun 2023 18:23:48 +0200 Subject: [PATCH 088/145] [Core][MPI] Initial add of `MPISearchUtilities` --- kratos/mpi/utilities/mpi_search_utilities.h | 170 ++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 kratos/mpi/utilities/mpi_search_utilities.h diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h new file mode 100644 index 000000000000..77924b84cc63 --- /dev/null +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -0,0 +1,170 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +#pragma once + +// System includes +#include + +// External includes + +// Project includes +#include "includes/data_communicator.h" + +namespace Kratos +{ + +/** + * @class MPISearchUtilities + * @ingroup KratosCore + * @brief This class provides the utilities for MPI search + * @author Vicente Mataix Ferrandiz + */ +class MPISearchUtilities +{ +public: + ///@name Type Definitions + ///@{ + + /// Define zero tolerance as Epsilon + static constexpr double ZeroTolerance = std::numeric_limits::epsilon(); + + ///@} + ///@name Operations + ///@{ + + /** + * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. + * @param itPointBegin Iterator to the beginning of the points range + * @param itPointEnd Iterator to the end of the points range + * @param rAllPointsCoordinates vector where the computed coordinates will be stored + * @return The number of points in the range + * @tparam TPointIteratorType The type of the point iterator + */ + template + static int MPISynchronousPointSynchronization( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + std::vector& rAllPointsCoordinates, + const DataCommunicator& rDataCommunicator + ) + { + // First check that the points are the same in all processes + int number_of_points, total_number_of_points; + const bool all_points_are_the_same = CheckAllPointsAreTheSame(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); + + // If all points are the same + if (all_points_are_the_same) { + rAllPointsCoordinates.resize(number_of_points * 3); + std::size_t counter = 0; + array_1d coordinates; + unsigned int i_coord; + for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++) { + noalias(coordinates) = it_point->Coordinates(); + for (i_coord = 0; i_coord < 3; ++i_coord) { + rAllPointsCoordinates[3 * counter + i_coord] = coordinates[i_coord]; + } + ++counter; + } + } else { // If not + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Getting global number of points + std::vector points_per_partition(world_size); + std::vector send_points_per_partition(1, number_of_points); + rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); + + // Getting global coordinates + rAllPointsCoordinates.resize(total_number_of_points * 3); + std::vector send_points_coordinates(number_of_points * 3); + std::size_t counter = 0; + array_1d coordinates; + unsigned int i_coord; + for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++) { + noalias(coordinates) = it_point->Coordinates(); + for (i_coord = 0; i_coord < 3; ++i_coord) { + send_points_coordinates[3 * counter + i_coord] = coordinates[i_coord]; + } + ++counter; + } + + // Generate vectors with sizes for AllGatherv + std::vector recv_sizes(world_size, 0); + for (int i_rank = 0; i_rank < world_size; ++i_rank) { + recv_sizes[i_rank] = 3 * points_per_partition[i_rank]; + } + std::vector recv_offsets(world_size, 0); + for (int i_rank = 1; i_rank < world_size; ++i_rank) { + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + } + + // Invoque AllGatherv + rDataCommunicator.AllGatherv(send_points_coordinates, rAllPointsCoordinates, recv_sizes, recv_offsets); + } + + return number_of_points; + } + ///@} +private: + ///@name Private Operations + ///@{ + + /** + * @brief This method checks if all nodes are the same across all partitions + * @param itPointBegin Iterator to the beginning of the points range + * @param itPointEnd Iterator to the end of the points range + * @param rNumberOfPoints Number of points in the range + * @param rTotalNumberOfPoints Total number of points in all partitions + * @return true if all points are the same in all partitions + * @tparam TPointIteratorType The type of the point iterator + */ + template + static bool CheckAllPointsAreTheSame( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + int& rNumberOfPoints, + int& rTotalNumberOfPoints, + const DataCommunicator& rDataCommunicator + ) + { + // Get the World Size in MPI + const int world_size = rDataCommunicator.Size(); + + // Getting local number of points + rNumberOfPoints = std::distance(itPointBegin, itPointEnd); + + // First we check if all the partitions have the same number of points + rTotalNumberOfPoints = rDataCommunicator.SumAll(rNumberOfPoints); + if (rNumberOfPoints * world_size != rTotalNumberOfPoints) { + return false; + } + + // Now we check if all the points are the same (we are going to do a gross assumtion that the sum of coordinates in al partitions does not compensate between them) + double x_sum, y_sum, z_sum; + array_1d coordinates; + for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++) { + noalias(coordinates) = it_point->Coordinates(); + x_sum = rDataCommunicator.SumAll(coordinates[0]); + if (std::abs(coordinates[0] - (x_sum/world_size)) > ZeroTolerance) return false; + y_sum = rDataCommunicator.SumAll(coordinates[1]); + if (std::abs(coordinates[1] - (y_sum/world_size)) > ZeroTolerance) return false; + z_sum = rDataCommunicator.SumAll(coordinates[2]); + if (std::abs(coordinates[2] - (z_sum/world_size)) > ZeroTolerance) return false; + } + + return true; + } + ///@} +}; + +} // namespace Kratos \ No newline at end of file From 7fcb68bd53abb47d010622883f4d6512e877b14b Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 9 Jun 2023 10:16:07 +0200 Subject: [PATCH 089/145] Adding tests for MPISearchUtilities --- .../utilities/test_mpi_search_utilities.cpp | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp diff --git a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp new file mode 100644 index 000000000000..4aa84989dec7 --- /dev/null +++ b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp @@ -0,0 +1,74 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// +// + +// System includes + +// External includes + +// Project includes +#include "testing/testing.h" +#include "geometries/point.h" +#include "mpi/utilities/mpi_search_utilities.h" + +namespace Kratos::Testing +{ + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPointsAreTheSame, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // Define test data + std::vector points = {Point(0.0, 0.0, 0.0), Point(1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0)}; + std::vector expected_coordinates = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0}; + + // Call the function + std::vector all_points_coordinates; + const int number_of_points = MPISearchUtilities::MPISynchronousPointSynchronization(points.begin(), points.end(), all_points_coordinates, r_data_comm); + + // Check the results + KRATOS_CHECK_EQUAL(number_of_points, 3); + KRATOS_CHECK_EQUAL(all_points_coordinates, expected_coordinates); +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPointsAreDifferent, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // MPI data communicator + const int rank = r_data_comm.Rank(); + const int world_size = r_data_comm.Size(); + + // Define test data + double value = static_cast(rank); + double value2 = 2 * value; + std::vector points({Point(value, value, value), Point(value2, value2, value2)}); + + // Call the function + std::vector all_points_coordinates; + MPISearchUtilities::MPISynchronousPointSynchronization(points.begin(), points.end(), all_points_coordinates, r_data_comm); + + // Check the results + KRATOS_CHECK_EQUAL(static_cast(all_points_coordinates.size()), 2 * 3 * world_size); + for (int i_rank = 0; i_rank < world_size; ++i_rank) { + for (int j = 0; j < 3; ++j) { + value = static_cast(i_rank); + value2 = 2 * value; + KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j ], value ); + KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j + 3], value2); + } + } +} + +} // namespace Kratos::Testing. \ No newline at end of file From fe5e8eeac020e552acf1743680e8b4a1d164b7fa Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 9 Jun 2023 13:32:08 +0200 Subject: [PATCH 090/145] Fix sync issue --- kratos/mpi/utilities/mpi_search_utilities.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 77924b84cc63..03d3ce51a395 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -152,17 +152,24 @@ class MPISearchUtilities // Now we check if all the points are the same (we are going to do a gross assumtion that the sum of coordinates in al partitions does not compensate between them) double x_sum, y_sum, z_sum; array_1d coordinates; + bool all_points_are_the_same = true; for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++) { noalias(coordinates) = it_point->Coordinates(); x_sum = rDataCommunicator.SumAll(coordinates[0]); - if (std::abs(coordinates[0] - (x_sum/world_size)) > ZeroTolerance) return false; + if (std::abs(coordinates[0] - (x_sum/world_size)) > ZeroTolerance) all_points_are_the_same = false; + all_points_are_the_same = rDataCommunicator.AndReduceAll(all_points_are_the_same); + if (!all_points_are_the_same) break; y_sum = rDataCommunicator.SumAll(coordinates[1]); - if (std::abs(coordinates[1] - (y_sum/world_size)) > ZeroTolerance) return false; + if (std::abs(coordinates[1] - (y_sum/world_size)) > ZeroTolerance) all_points_are_the_same = false; + all_points_are_the_same = rDataCommunicator.AndReduceAll(all_points_are_the_same); + if (!all_points_are_the_same) break; z_sum = rDataCommunicator.SumAll(coordinates[2]); - if (std::abs(coordinates[2] - (z_sum/world_size)) > ZeroTolerance) return false; + if (std::abs(coordinates[2] - (z_sum/world_size)) > ZeroTolerance) all_points_are_the_same = false; + all_points_are_the_same = rDataCommunicator.AndReduceAll(all_points_are_the_same); + if (!all_points_are_the_same) break; } - return true; + return all_points_are_the_same; } ///@} }; From 05df2200a492412ad1b4199c1b47f31f60b11ba5 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 9 Jun 2023 13:42:09 +0200 Subject: [PATCH 091/145] Windows fix? --- kratos/spatial_containers/spatial_search.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index c5e9db8323ef..4ffe3027b449 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -142,8 +142,9 @@ SpatialSearch::ElementSpatialSearchResultContainerMapType SpatialSearch::SearchE // Adding partial results auto& r_result_i = result.InitializeResult(id); + Element* p_element = nullptr; for (IndexType j = 0; j < r_partial_results.size(); ++j) { - auto p_element = (*((r_partial_results.begin() + j).base())).get(); + p_element = (*((r_partial_results.begin() + j).base())).get(); r_result_i.AddResult(p_element, r_partial_distances[j]); } } @@ -278,8 +279,9 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchElem // Adding partial results auto& r_result_i = result.InitializeResult(id); + Node* p_node = nullptr; for (IndexType j = 0; j < r_partial_results.size(); ++j) { - auto p_node = (*((r_partial_results.begin() + j).base())).get(); + p_node = (*((r_partial_results.begin() + j).base())).get(); r_result_i.AddResult(p_node, r_partial_distances[j]); } } @@ -526,8 +528,9 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode // Adding partial results auto& r_result_i = result.InitializeResult(id); + Node* p_node = nullptr; for (IndexType j = 0; j < r_partial_results.size(); ++j) { - auto p_node = (*((r_partial_results.begin() + j).base())).get(); + p_node = (*((r_partial_results.begin() + j).base())).get(); r_result_i.AddResult(p_node, r_partial_distances[j]); } } @@ -662,8 +665,9 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode // Adding partial results auto& r_result_i = result.InitializeResult(id); + Node* p_node = nullptr; for (IndexType j = 0; j < r_partial_results.size(); ++j) { - auto p_node = (*((r_partial_results.begin() + j).base())).get(); + p_node = (*((r_partial_results.begin() + j).base())).get(); r_result_i.AddResult(p_node, r_partial_distances[j]); } } @@ -910,8 +914,9 @@ SpatialSearch::ConditionSpatialSearchResultContainerMapType SpatialSearch::Searc // Adding partial results auto& r_result_i = result.InitializeResult(id); + Condition* p_condition = nullptr; for (IndexType j = 0; j < r_partial_results.size(); ++j) { - auto p_condition = (*((r_partial_results.begin() + j).base())).get(); + p_condition = (*((r_partial_results.begin() + j).base())).get(); r_result_i.AddResult(p_condition, r_partial_distances[j]); } } @@ -1046,8 +1051,9 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchCond // Adding partial results auto& r_result_i = result.InitializeResult(id); + Node* p_node = nullptr; for (IndexType j = 0; j < r_partial_results.size(); ++j) { - auto p_node = (*((r_partial_results.begin() + j).base())).get(); + p_node = (*((r_partial_results.begin() + j).base())).get(); r_result_i.AddResult(p_node, r_partial_distances[j]); } } From 1a24e03c3fd79d4c593cdf1467afa8843c4f3f14 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 9 Jun 2023 15:02:05 +0200 Subject: [PATCH 092/145] [Core][MPI] Adding required files for `AddMPISearchStrategiesToPython` --- kratos/mpi/CMakeLists.txt | 1 + .../add_mpi_search_strategies_to_python.cpp | 32 +++++++++++++++++++ .../add_mpi_search_strategies_to_python.h | 27 ++++++++++++++++ kratos/mpi/python/kratos_mpi_python.cpp | 2 ++ 4 files changed, 62 insertions(+) create mode 100644 kratos/mpi/python/add_mpi_search_strategies_to_python.cpp create mode 100644 kratos/mpi/python/add_mpi_search_strategies_to_python.h diff --git a/kratos/mpi/CMakeLists.txt b/kratos/mpi/CMakeLists.txt index 28724d7bc5f1..a20117bb553c 100644 --- a/kratos/mpi/CMakeLists.txt +++ b/kratos/mpi/CMakeLists.txt @@ -8,6 +8,7 @@ include_directories( ${KRATOS_SOURCE_DIR}/kratos ) ## Kratos MPI extension sources file(GLOB_RECURSE KRATOS_MPI_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sources/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/spatial_containers/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/utilities/*.cpp ) diff --git a/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp new file mode 100644 index 000000000000..e231a705f796 --- /dev/null +++ b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp @@ -0,0 +1,32 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +// System includes + +// External includes + +// Project includes +#include "includes/define_python.h" +#include "includes/model_part.h" +#include "mpi/python/add_mpi_search_strategies_to_python.h" + +namespace Kratos::Python +{ + +void AddMPISearchStrategiesToPython(pybind11::module& m) +{ + namespace py = pybind11; + + +} + +} // namespace Kratos::Python \ No newline at end of file diff --git a/kratos/mpi/python/add_mpi_search_strategies_to_python.h b/kratos/mpi/python/add_mpi_search_strategies_to_python.h new file mode 100644 index 000000000000..31a82f94e7b1 --- /dev/null +++ b/kratos/mpi/python/add_mpi_search_strategies_to_python.h @@ -0,0 +1,27 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +#pragma once + +// System includes + +// External includes +#include + +// Project includes + +namespace Kratos::Python +{ + +void AddMPISearchStrategiesToPython(pybind11::module& m); + +} // namespace Kratos::Python. \ No newline at end of file diff --git a/kratos/mpi/python/kratos_mpi_python.cpp b/kratos/mpi/python/kratos_mpi_python.cpp index 9df0816ad2d0..ce9722595935 100644 --- a/kratos/mpi/python/kratos_mpi_python.cpp +++ b/kratos/mpi/python/kratos_mpi_python.cpp @@ -25,6 +25,7 @@ #include "add_mpi_debug_utilities_to_python.h" #include "add_distributed_sparse_matrices_to_python.h" #include "includes/parallel_environment.h" +#include "add_mpi_search_strategies_to_python.h" namespace Kratos::Python { @@ -71,6 +72,7 @@ PYBIND11_MODULE(KratosMPI, m) AddMPIUtilitiesToPython(m); AddMPIDebugUtilitiesToPython(m); AddDistributedSparseMatricesToPython(m); + AddMPISearchStrategiesToPython(m); } } From 42374e83d9d3c5f9a82515227671f26b85c03411 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 9 Jun 2023 16:47:52 +0200 Subject: [PATCH 093/145] Another try to fix Windows compilation --- kratos/spatial_containers/spatial_search.cpp | 48 ++++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 4ffe3027b449..2f73608ecaf7 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -142,10 +142,10 @@ SpatialSearch::ElementSpatialSearchResultContainerMapType SpatialSearch::SearchE // Adding partial results auto& r_result_i = result.InitializeResult(id); - Element* p_element = nullptr; - for (IndexType j = 0; j < r_partial_results.size(); ++j) { - p_element = (*((r_partial_results.begin() + j).base())).get(); - r_result_i.AddResult(p_element, r_partial_distances[j]); + IndexType j = 0; + for (auto& r_partial_result : r_partial_results) { + r_result_i.AddResult(r_partial_result.get(), r_partial_distances[j]); + ++j; } } @@ -279,10 +279,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchElem // Adding partial results auto& r_result_i = result.InitializeResult(id); - Node* p_node = nullptr; - for (IndexType j = 0; j < r_partial_results.size(); ++j) { - p_node = (*((r_partial_results.begin() + j).base())).get(); - r_result_i.AddResult(p_node, r_partial_distances[j]); + IndexType j = 0; + for (auto& r_partial_result : r_partial_results) { + r_result_i.AddResult(r_partial_result.get(), r_partial_distances[j]); + ++j; } } @@ -528,10 +528,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode // Adding partial results auto& r_result_i = result.InitializeResult(id); - Node* p_node = nullptr; - for (IndexType j = 0; j < r_partial_results.size(); ++j) { - p_node = (*((r_partial_results.begin() + j).base())).get(); - r_result_i.AddResult(p_node, r_partial_distances[j]); + IndexType j = 0; + for (auto& r_partial_result : r_partial_results) { + r_result_i.AddResult(r_partial_result.get(), r_partial_distances[j]); + ++j; } } @@ -665,10 +665,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchNode // Adding partial results auto& r_result_i = result.InitializeResult(id); - Node* p_node = nullptr; - for (IndexType j = 0; j < r_partial_results.size(); ++j) { - p_node = (*((r_partial_results.begin() + j).base())).get(); - r_result_i.AddResult(p_node, r_partial_distances[j]); + IndexType j = 0; + for (auto& r_partial_result : r_partial_results) { + r_result_i.AddResult(r_partial_result.get(), r_partial_distances[j]); + ++j; } } @@ -914,10 +914,10 @@ SpatialSearch::ConditionSpatialSearchResultContainerMapType SpatialSearch::Searc // Adding partial results auto& r_result_i = result.InitializeResult(id); - Condition* p_condition = nullptr; - for (IndexType j = 0; j < r_partial_results.size(); ++j) { - p_condition = (*((r_partial_results.begin() + j).base())).get(); - r_result_i.AddResult(p_condition, r_partial_distances[j]); + IndexType j = 0; + for (auto& r_partial_result : r_partial_results) { + r_result_i.AddResult(r_partial_result.get(), r_partial_distances[j]); + ++j; } } @@ -1051,10 +1051,10 @@ SpatialSearch::NodeSpatialSearchResultContainerMapType SpatialSearch::SearchCond // Adding partial results auto& r_result_i = result.InitializeResult(id); - Node* p_node = nullptr; - for (IndexType j = 0; j < r_partial_results.size(); ++j) { - p_node = (*((r_partial_results.begin() + j).base())).get(); - r_result_i.AddResult(p_node, r_partial_distances[j]); + IndexType j = 0; + for (auto& r_partial_result : r_partial_results) { + r_result_i.AddResult(r_partial_result.get(), r_partial_distances[j]); + ++j; } } From 6ff9b9217144dfb2d2a50ed0379651ef963910b5 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 14 Jun 2023 14:29:50 +0200 Subject: [PATCH 094/145] Adding missing Reserve method --- kratos/python/add_search_strategies_to_python.cpp | 1 + .../spatial_search_result_container.h | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 193a2c0896bc..9df51b3659ee 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -89,6 +89,7 @@ void BindSpatialSearchResultContainer(pybind11::module& m, const std::string& rC .def("IsObjectFound", &ContainerType::IsObjectFound) .def("NumberOfLocalResults", &ContainerType::NumberOfLocalResults) .def("NumberOfGlobalResults", &ContainerType::NumberOfGlobalResults) + .def("Reserve", &ContainerType::Reserve) .def("AddResult", [](ContainerType& self, TObjectType* pObject) { self.AddResult(pObject); }) diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index ae51cead0dc7..175681448312 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -352,6 +352,17 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer return mGlobalPointers.size(); } + /** + * @brief Reserves the container + * @details Only local + * @param Size The size of the container + */ + void Reserve(const std::size_t Size) + { + // Only local + mLocalPointers.reserve(Size); + } + /** * @brief Add a result to the container * @param rResult The result to be added From a9345b38bd5646ec051703789af2a10a205e0c92 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 15 Jun 2023 14:53:42 +0200 Subject: [PATCH 095/145] Direct points methods --- kratos/spatial_containers/spatial_search.cpp | 81 ++++ kratos/spatial_containers/spatial_search.h | 253 ++++++++++ .../specialized_spatial_search.cpp | 432 ++++++++++++++++-- .../specialized_spatial_search.h | 224 ++++++++- .../specialized_spatial_search_factory.h | 81 ++++ 5 files changed, 1001 insertions(+), 70 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 2f73608ecaf7..c6ac08e32d23 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -1355,5 +1355,86 @@ void SpatialSearch::SearchElementsOverConditionsInRadiusInclusive ( KRATOS_ERROR << "Direct call of an abstract method" << std::endl; } +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + } diff --git a/kratos/spatial_containers/spatial_search.h b/kratos/spatial_containers/spatial_search.h index d488f3aa6bbd..1134a3db8926 100644 --- a/kratos/spatial_containers/spatial_search.h +++ b/kratos/spatial_containers/spatial_search.h @@ -81,18 +81,21 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch using NodesContainerType = ModelPart::NodesContainerType; using ResultNodesContainerType = NodesContainerType::ContainerType; using VectorResultNodesContainerType = std::vector; + using NodeSpatialSearchResultContainerType = SpatialSearchResultContainer; using NodeSpatialSearchResultContainerMapType = SpatialSearchResultContainerMap; /// Elements classes using ElementsContainerType = ModelPart::ElementsContainerType; using ResultElementsContainerType = ElementsContainerType::ContainerType; using VectorResultElementsContainerType = std::vector; + using ElementSpatialSearchResultContainerType = SpatialSearchResultContainer; using ElementSpatialSearchResultContainerMapType = SpatialSearchResultContainerMap; /// Conditions classes using ConditionsContainerType = ModelPart::ConditionsContainerType; using ResultConditionsContainerType = ConditionsContainerType::ContainerType; using VectorResultConditionsContainerType = std::vector; + using ConditionSpatialSearchResultContainerType = SpatialSearchResultContainer; using ConditionSpatialSearchResultContainerMapType = SpatialSearchResultContainerMap; /// Input/output types @@ -1374,6 +1377,256 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch VectorDistanceType& rResultsDistance ); + //************************************************************************ + // Point vs Entities (these are new interfaces and already use the new containers) + //************************************************************************ + + /** + * @brief Search neighbours nodes for one point in a given radius + * @param rStructureNodes List of nodes to be searched + * @param rPoint Point to be searched + * @param Radius Radius of the search + * @param rResults Results of the search + * @param rDataCommunicator The data communicator + */ + virtual void SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search neighbours nodes for several points in a given radius + * @param rStructureNodes List of nodes to be searched + * @param itPointBegin Iterator to the first point + * @param itPointEnd Iterator to the last point + * @param rRadius List of search radius for every node + * @param rDataCommunicator The data communicator + */ + template + NodeSpatialSearchResultContainerMapType SearchNodesOverPointsInRadius ( + const NodesContainerType& rStructureNodes, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + NodeSpatialSearchResultContainerMapType results; + int i = 0; + for (auto it_point = itPointBegin; it_point != itPointEnd; ++it_point) { + const Point& r_point = *it_point; + auto& r_partial_result = results.InitializeResult(r_point); + SearchNodesOverPointInRadius(rStructureNodes, r_point, rRadius[i], r_partial_result, rDataCommunicator); + ++i; + } + return results; + } + + /** + * @brief Search nearest neighbour node for one point + * @param rStructureNodes List of nodes to be searched + * @param rPoint Point to be searched + * @param rResults Results of the search + * @param rDataCommunicator The data communicator + */ + virtual void SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search nearest neighbour node for several points + * @param rStructureNodes List of nodes to be searched + * @param itPointBegin Iterator to the first point + * @param itPointEnd Iterator to the last point + * @param rDataCommunicator The data communicator + */ + template + NodeSpatialSearchResultContainerMapType SearchNodesOverPointsNearestPoint ( + const NodesContainerType& rStructureNodes, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + NodeSpatialSearchResultContainerMapType results; + for (auto it_point = itPointBegin; it_point != itPointEnd; ++it_point) { + const Point& r_point = *it_point; + auto& r_partial_result = results.InitializeResult(r_point); + SearchNodesOverPointNearestPoint(rStructureNodes, r_point, r_partial_result, rDataCommunicator); + } + return results; + } + + /** + * @brief Search neighbours elements for one point in a given radius + * @param rStructureElements List of elements to be searched + * @param rPoint Point to be searched + * @param Radius Radius of the search + * @param rResults Results of the search + * @param rDataCommunicator The data communicator + */ + virtual void SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search neighbours elements for several points in a given radius + * @param rStructureElements List of elements to be searched + * @param itPointBegin Iterator to the first point + * @param itPointEnd Iterator to the last point + * @param rRadius List of search radius for every element + * @param rDataCommunicator The data communicator + */ + template + ElementSpatialSearchResultContainerMapType SearchElementsOverPointsInRadius ( + const ElementsContainerType& rStructureElements, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + ElementSpatialSearchResultContainerMapType results; + int i = 0; + for (auto it_point = itPointBegin; it_point != itPointEnd; ++it_point) { + const Point& r_point = *it_point; + auto& r_partial_result = results.InitializeResult(r_point); + SearchElementsOverPointInRadius(rStructureElements, r_point, rRadius[i], r_partial_result, rDataCommunicator); + ++i; + } + return results; + } + + /** + * @brief Search nearest neighbour element for one point + * @param rStructureElements List of elements to be searched + * @param rPoint Point to be searched + * @param rResults Results of the search + * @param rDataCommunicator The data communicator + */ + virtual void SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search nearest neighbour element for several points + * @param rStructureElements List of elements to be searched + * @param itPointBegin Iterator to the first point + * @param itPointEnd Iterator to the last point + * @param rDataCommunicator The data communicator + */ + template + ElementSpatialSearchResultContainerMapType SearchElementsOverPointsNearestPoint ( + const ElementsContainerType& rStructureElements, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + ElementSpatialSearchResultContainerMapType results; + for (auto it_point = itPointBegin; it_point != itPointEnd; ++it_point) { + const Point& r_point = *it_point; + auto& r_partial_result = results.InitializeResult(r_point); + SearchElementsOverPointNearestPoint(rStructureElements, r_point, r_partial_result, rDataCommunicator); + } + return results; + } + + /** + * @brief Search neighbours conditions for one point in a given radius + * @param rStructureConditions List of conditions to be searched + * @param rPoint Point to be searched + * @param Radius Radius of the search + * @param rResults Results of the search + * @param rDataCommunicator The data communicator + */ + virtual void SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search neighbours conditions for several points in a given radius + * @param rStructureConditions List of conditions to be searched + * @param itPointBegin Iterator to the first point + * @param itPointEnd Iterator to the last point + * @param rRadius List of search radius for every condition + * @param rDataCommunicator The data communicator + */ + template + ConditionSpatialSearchResultContainerMapType SearchConditionsOverPointsInRadius ( + const ConditionsContainerType& rStructureConditions, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + ConditionSpatialSearchResultContainerMapType results; + int i = 0; + for (auto it_point = itPointBegin; it_point != itPointEnd; ++it_point) { + const Point& r_point = *it_point; + auto& r_partial_result = results.InitializeResult(r_point); + SearchConditionsOverPointInRadius(rStructureConditions, r_point, rRadius[i], r_partial_result, rDataCommunicator); + ++i; + } + return results; + } + + /** + * @brief Search nearest neighbour condition for one point + * @param rStructureConditions List of conditions to be searched + * @param rPoint Point to be searched + * @param rResults Results of the search + * @param rDataCommunicator The data communicator + */ + virtual void SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ); + + /** + * @brief Search nearest neighbour condition for several points + * @param rStructureConditions List of conditions to be searched + * @param itPointBegin Iterator to the first point + * @param itPointEnd Iterator to the last point + * @param rDataCommunicator The data communicator + */ + template + ConditionSpatialSearchResultContainerMapType SearchConditionsOverPointsNearestPoint ( + const ConditionsContainerType& rStructureConditions, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + ConditionSpatialSearchResultContainerMapType results; + for (auto it_point = itPointBegin; it_point != itPointEnd; ++it_point) { + const Point& r_point = *it_point; + auto& r_partial_result = results.InitializeResult(r_point); + SearchConditionsOverPointNearestPoint(rStructureConditions, r_point, r_partial_result, rDataCommunicator); + } + return results; + } + ///@} ///@name Access ///@{ diff --git a/kratos/spatial_containers/specialized_spatial_search.cpp b/kratos/spatial_containers/specialized_spatial_search.cpp index b14f6a1a7701..c6bfba107f89 100644 --- a/kratos/spatial_containers/specialized_spatial_search.cpp +++ b/kratos/spatial_containers/specialized_spatial_search.cpp @@ -70,41 +70,32 @@ void SpecializedSpatialSearch::SearchElementsInRadiusExclusive( // Defining the search structure if constexpr (TSearchBackend == SpatialContainer::KDTree) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// KDtree definitions using KDTree = Tree>>; // Creating the tree - KDTree kd_tree(points.begin(), points.end(), bucket_size); + KDTree kd_tree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputElements, rRadius, kd_tree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputElements, rRadius, kd_tree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::Octree) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// Octree definitions using Octree = Tree>>; // Creating the tree - Octree octree(points.begin(), points.end(), bucket_size); + Octree octree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputElements, rRadius, octree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputElements, rRadius, octree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// StaticBins definitions using StaticBinsTree = Tree>; // Creating the tree - StaticBinsTree static_bins_tree(points.begin(), points.end(), bucket_size); + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputElements, rRadius, static_bins_tree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputElements, rRadius, static_bins_tree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { /// BinsDynamic definitions using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; @@ -113,7 +104,7 @@ void SpecializedSpatialSearch::SearchElementsInRadiusExclusive( DynamicBins dynamic_bins(points.begin(), points.end()); // Performing search - ParallelSearch(rInputElements, rRadius, dynamic_bins, rResults, rResultsDistance); + ParallelSearchInRadius(rInputElements, rRadius, dynamic_bins, rResults, rResultsDistance); } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } @@ -155,41 +146,32 @@ void SpecializedSpatialSearch::SearchNodesInRadiusExclusive( // Defining the search structure if constexpr (TSearchBackend == SpatialContainer::KDTree) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// KDtree definitions using KDTree = Tree>>; // Creating the tree - KDTree kd_tree(points.begin(), points.end(), bucket_size); + KDTree kd_tree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputNodes, rRadius, kd_tree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputNodes, rRadius, kd_tree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::Octree) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// Octree definitions using Octree = Tree>>; // Creating the tree - Octree octree(points.begin(), points.end(), bucket_size); + Octree octree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputNodes, rRadius, octree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputNodes, rRadius, octree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// StaticBins definitions using StaticBinsTree = Tree>; // Creating the tree - StaticBinsTree static_bins_tree(points.begin(), points.end(), bucket_size); + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputNodes, rRadius, static_bins_tree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputNodes, rRadius, static_bins_tree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { /// BinsDynamic definitions using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; @@ -198,7 +180,7 @@ void SpecializedSpatialSearch::SearchNodesInRadiusExclusive( DynamicBins dynamic_bins(points.begin(), points.end()); // Performing search - ParallelSearch(rInputNodes, rRadius, dynamic_bins, rResults, rResultsDistance); + ParallelSearchInRadius(rInputNodes, rRadius, dynamic_bins, rResults, rResultsDistance); } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } @@ -240,41 +222,32 @@ void SpecializedSpatialSearch::SearchConditionsInRadiusExclusive // Defining the search structure if constexpr (TSearchBackend == SpatialContainer::KDTree) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// KDtree definitions using KDTree = Tree>>; // Creating the tree - KDTree kd_tree(points.begin(), points.end(), bucket_size); + KDTree kd_tree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputConditions, rRadius, kd_tree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputConditions, rRadius, kd_tree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::Octree) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// Octree definitions using Octree = Tree>>; // Creating the tree - Octree octree(points.begin(), points.end(), bucket_size); + Octree octree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputConditions, rRadius, octree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputConditions, rRadius, octree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { - // Retrieving parameters - const int bucket_size = mParameters["bucket_size"].GetInt(); - /// StaticBins definitions using StaticBinsTree = Tree>; // Creating the tree - StaticBinsTree static_bins_tree(points.begin(), points.end(), bucket_size); + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); // Performing search - ParallelSearch(rInputConditions, rRadius, static_bins_tree, rResults, rResultsDistance); + ParallelSearchInRadius(rInputConditions, rRadius, static_bins_tree, rResults, rResultsDistance); } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { /// BinsDynamic definitions using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; @@ -283,7 +256,7 @@ void SpecializedSpatialSearch::SearchConditionsInRadiusExclusive DynamicBins dynamic_bins(points.begin(), points.end()); // Performing search - ParallelSearch(rInputConditions, rRadius, dynamic_bins, rResults, rResultsDistance); + ParallelSearchInRadius(rInputConditions, rRadius, dynamic_bins, rResults, rResultsDistance); } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } @@ -307,6 +280,369 @@ void SpecializedSpatialSearch::SearchConditionsInRadiusExclusive /***********************************************************************************/ /***********************************************************************************/ +template +void SpecializedSpatialSearch::SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + // Defining the point type for the search + using PointType = PointObject; + using PointVector = std::vector; + + // Defining the PointVector + PointVector points = PrepareSearchPoints(rStructureNodes); + + // Defining the search structure + if constexpr (TSearchBackend == SpatialContainer::KDTree) { + /// KDtree definitions + using KDTree = Tree>>; + + // Creating the tree + KDTree kd_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureNodes, rPoint, Radius, kd_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::Octree) { + /// Octree definitions + using Octree = Tree>>; + + // Creating the tree + Octree octree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureNodes, rPoint, Radius, octree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { + /// StaticBins definitions + using StaticBinsTree = Tree>; + + // Creating the tree + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureNodes, rPoint, Radius, static_bins_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { + /// BinsDynamic definitions + using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; + + // Creating the bins + DynamicBins dynamic_bins(points.begin(), points.end()); + + // Performing search + SearchInRadius(rStructureNodes, rPoint, Radius, dynamic_bins, rResults, rDataCommunicator); + } else { + static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearch::SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + // Defining the point type for the search + using PointType = PointObject; + using PointVector = std::vector; + + // Defining the PointVector + PointVector points = PrepareSearchPoints(rStructureNodes); + + // Defining the search structure + if constexpr (TSearchBackend == SpatialContainer::KDTree) { + /// KDtree definitions + using KDTree = Tree>>; + + // Creating the tree + KDTree kd_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureNodes, rPoint, kd_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::Octree) { + /// Octree definitions + using Octree = Tree>>; + + // Creating the tree + Octree octree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureNodes, rPoint, octree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { + /// StaticBins definitions + using StaticBinsTree = Tree>; + + // Creating the tree + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureNodes, rPoint, static_bins_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { + /// BinsDynamic definitions + using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; + + // Creating the bins + DynamicBins dynamic_bins(points.begin(), points.end()); + + // Performing search + SearchNearestPoint(rStructureNodes, rPoint, dynamic_bins, rResults, rDataCommunicator); + } else { + static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearch::SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + // Defining the point type for the search + using PointType = PointObject; + using PointVector = std::vector; + + // Defining the PointVector + PointVector points = PrepareSearchPoints(rStructureElements); + + // Defining the search structure + if constexpr (TSearchBackend == SpatialContainer::KDTree) { + /// KDtree definitions + using KDTree = Tree>>; + + // Creating the tree + KDTree kd_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureElements, rPoint, Radius, kd_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::Octree) { + /// Octree definitions + using Octree = Tree>>; + + // Creating the tree + Octree octree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureElements, rPoint, Radius, octree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { + /// StaticBins definitions + using StaticBinsTree = Tree>; + + // Creating the tree + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureElements, rPoint, Radius, static_bins_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { + /// BinsDynamic definitions + using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; + + // Creating the bins + DynamicBins dynamic_bins(points.begin(), points.end()); + + // Performing search + SearchInRadius(rStructureElements, rPoint, Radius, dynamic_bins, rResults, rDataCommunicator); + } else { + static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearch::SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + // Defining the point type for the search + using PointType = PointObject; + using PointVector = std::vector; + + // Defining the PointVector + PointVector points = PrepareSearchPoints(rStructureElements); + + // Defining the search structure + if constexpr (TSearchBackend == SpatialContainer::KDTree) { + /// KDtree definitions + using KDTree = Tree>>; + + // Creating the tree + KDTree kd_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureElements, rPoint, kd_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::Octree) { + /// Octree definitions + using Octree = Tree>>; + + // Creating the tree + Octree octree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureElements, rPoint, octree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { + /// StaticBins definitions + using StaticBinsTree = Tree>; + + // Creating the tree + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureElements, rPoint, static_bins_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { + /// BinsDynamic definitions + using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; + + // Creating the bins + DynamicBins dynamic_bins(points.begin(), points.end()); + + // Performing search + SearchNearestPoint(rStructureElements, rPoint, dynamic_bins, rResults, rDataCommunicator); + } else { + static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearch::SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + // Defining the point type for the search + using PointType = PointObject; + using PointVector = std::vector; + + // Defining the PointVector + PointVector points = PrepareSearchPoints(rStructureConditions); + + // Defining the search structure + if constexpr (TSearchBackend == SpatialContainer::KDTree) { + /// KDtree definitions + using KDTree = Tree>>; + + // Creating the tree + KDTree kd_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureConditions, rPoint, Radius, kd_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::Octree) { + /// Octree definitions + using Octree = Tree>>; + + // Creating the tree + Octree octree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureConditions, rPoint, Radius, octree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { + /// StaticBins definitions + using StaticBinsTree = Tree>; + + // Creating the tree + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchInRadius(rStructureConditions, rPoint, Radius, static_bins_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { + /// BinsDynamic definitions + using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; + + // Creating the bins + DynamicBins dynamic_bins(points.begin(), points.end()); + + // Performing search + SearchInRadius(rStructureConditions, rPoint, Radius, dynamic_bins, rResults, rDataCommunicator); + } else { + static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearch::SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + // Defining the point type for the search + using PointType = PointObject; + using PointVector = std::vector; + + // Defining the PointVector + PointVector points = PrepareSearchPoints(rStructureConditions); + + // Defining the search structure + if constexpr (TSearchBackend == SpatialContainer::KDTree) { + /// KDtree definitions + using KDTree = Tree>>; + + // Creating the tree + KDTree kd_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureConditions, rPoint, kd_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::Octree) { + /// Octree definitions + using Octree = Tree>>; + + // Creating the tree + Octree octree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureConditions, rPoint, octree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsStatic) { + /// StaticBins definitions + using StaticBinsTree = Tree>; + + // Creating the tree + StaticBinsTree static_bins_tree(points.begin(), points.end(), mBucketSize); + + // Performing search + SearchNearestPoint(rStructureConditions, rPoint, static_bins_tree, rResults, rDataCommunicator); + } else if constexpr (TSearchBackend == SpatialContainer::BinsDynamic) { + /// BinsDynamic definitions + using DynamicBins = BinsDynamic<3ul, PointType, PointVector>; + + // Creating the bins + DynamicBins dynamic_bins(points.begin(), points.end()); + + // Performing search + SearchNearestPoint(rStructureConditions, rPoint, dynamic_bins, rResults, rDataCommunicator); + } else { + static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + template Parameters SpecializedSpatialSearch::GetDefaultParameters() const { diff --git a/kratos/spatial_containers/specialized_spatial_search.h b/kratos/spatial_containers/specialized_spatial_search.h index ac939353f9c5..b4d6ab25d495 100644 --- a/kratos/spatial_containers/specialized_spatial_search.h +++ b/kratos/spatial_containers/specialized_spatial_search.h @@ -91,6 +91,18 @@ class PointObject { } + /// Default constructors with coordinates + PointObject(const array_1d& rCoordinates): + BaseType(rCoordinates) + { + } + + /// Default constructors with point + PointObject(const Point& rPoint): + BaseType(rPoint) + { + } + /** * @brief Constructor with object * @param pObject The pointer to the object @@ -198,6 +210,10 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch { // We asssign default parameters mParameters = GetDefaultParameters(); + + // The allocation and bucket size + mAllocationSize = mParameters["allocation_size"].GetInt(); + mBucketSize = mParameters["bucket_size"].GetInt(); } /** @@ -212,6 +228,10 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch // We update the parameters mParameters.RecursivelyValidateAndAssignDefaults(default_parameters); + + // The allocation and bucket size + mAllocationSize = mParameters["allocation_size"].GetInt(); + mBucketSize = mParameters["bucket_size"].GetInt(); } /// Destructor. @@ -288,6 +308,69 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch VectorResultConditionsContainerType& rResults ) override; + /** + * @brief Search neighbours nodes for one point in a given radius + */ + void SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search nearest neighbour node for one point + */ + void SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search neighbours elements for one point in a given radius + */ + void SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search nearest neighbour element for one point + */ + void SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search neighbours conditions for one point in a given radius + */ + void SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search nearest neighbour condition for one point + */ + void SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + ///@} ///@name Input and output ///@{ @@ -328,6 +411,9 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch Parameters mParameters; /// The configuration parameters + int mAllocationSize; /// The allocation size + int mBucketSize; /// The bucket size + ///@} ///@name Protected Operations ///@{ @@ -351,22 +437,13 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch ///@name Private Operations ///@{ - /** - * @brief This method prepares the search + /** + * @brief This method prepares the search points * @param rStructure The structure to be searched - * @param rInput The input to be searched - * @param rResults The results - * @param rResultsDistance The results distance * @tparam TContainer The container type - * @tparam TResultType The result type */ - template - std::vector::Pointer> PrepareSearch( - const TContainer& rStructure, - const TContainer& rInput, - TResultType& rResults, - VectorDistanceType& rResultsDistance - ) + template + std::vector::Pointer> PrepareSearchPoints(const TContainer& rStructure) { // Some definitions using ObjectType = typename TContainer::value_type; @@ -384,6 +461,26 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch points.push_back(PointTypePointer(new PointType(*(it.base())))); } + return points; + } + + /** + * @brief This method prepares the search + * @param rStructure The structure to be searched + * @param rInput The input to be searched + * @param rResults The results + * @param rResultsDistance The results distance + * @tparam TContainer The container type + * @tparam TResultType The result type + */ + template + std::vector::Pointer> PrepareSearch( + const TContainer& rStructure, + const TContainer& rInput, + TResultType& rResults, + VectorDistanceType& rResultsDistance + ) + { // Resizing the results const std::size_t input_size = rInput.size(); if (rResults.size() != input_size) { @@ -393,11 +490,97 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch rResultsDistance.resize(input_size); } - return points; + return PrepareSearchPoints(rStructure); + } + + /** + * @brief This method performs the search in radius + * @param rPoint The point to be searched + * @param Radius The search radius + * @param rSearch The spatial search + * @param rResults The results + * @tparam TContainer The container type + * @tparam TSpatialContainer The spatial container type + * @tparam TResultType The result type + */ + template + void SearchInRadius( + const TContainer& rStructure, + const array_1d& rPoint, + const double Radius, + TSpatialContainer& rSearch, + TResultType& rResults, + const DataCommunicator& rDataCommunicator + ) + { + // Some definitions + using ObjectType = typename TContainer::value_type; + using PointType = PointObject; + using PointTypePointer = typename PointType::Pointer; + using PointVector = std::vector; + using DistanceVector = std::vector; + + // The allocation size + const int allocation_size = mAllocationSize > static_cast(rStructure.size()) ? rStructure.size() : mAllocationSize; + + // Perform search + PointType aux_point(rPoint); + PointVector results(allocation_size); + DistanceVector results_distances(allocation_size); + const std::size_t number_of_results = rSearch.SearchInRadius(aux_point, Radius, results.begin(), results_distances.begin(), allocation_size); + + // Adding the results + if (number_of_results > 0) { + rResults.Reserve(number_of_results); + for (std::size_t i = 0; i < number_of_results; ++i) { + auto p_point = results[i]; + rResults.AddResult((p_point->pGetObject()).get(), results_distances[i]); + } + } + + // Synchronize results + rResults.SynchronizeAll(rDataCommunicator); + } + + /** + * @brief This method performs the search in radius + * @param rPoint The point to be searched + * @param Radius The search radius + * @param rSearch The spatial search + * @param rResults The results + * @tparam TContainer The container type + * @tparam TSpatialContainer The spatial container type + * @tparam TResultType The result type + */ + template + void SearchNearestPoint( + const TContainer& rStructure, + const array_1d& rPoint, + TSpatialContainer& rSearch, + TResultType& rResults, + const DataCommunicator& rDataCommunicator + ) + { + // Some definitions + using ObjectType = typename TContainer::value_type; + using PointType = PointObject; + + // Perform search + PointType aux_point(rPoint); + double distance = 0.0; + auto p_point = rSearch.SearchNearestPoint(aux_point, distance); + + // Adding the results + if (p_point != nullptr) { + rResults.AddResult((p_point->pGetObject()).get(), distance); + } + + // Synchronize results + rResults.SynchronizeAll(rDataCommunicator); } /** - * @brief This method performs the search in parallel + * @brief This method performs the search in radius in parallel * @param rInput The input container * @param rRadius The radius array * @param rSearch The spatial search @@ -408,7 +591,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch * @tparam TResultType The result type */ template - void ParallelSearch( + void ParallelSearchInRadius( const TContainer& rInput, const RadiusArrayType& rRadius, TSpatialContainer& rSearch, @@ -424,16 +607,13 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch using DistanceVector = std::vector; const std::size_t input_size = rInput.size(); - // Retrieving parameters - const int allocation_size = mParameters["allocation_size"].GetInt(); - // Performing search IndexPartition(input_size).for_each([&](std::size_t i) { auto it = rInput.begin() + i; PointType aux_point(*(it.base())); - PointVector results(allocation_size); - DistanceVector results_distances(allocation_size); - const std::size_t number_of_results = rSearch.SearchInRadius(aux_point, rRadius[i], results.begin(), results_distances.begin(), allocation_size); + PointVector results(mAllocationSize); + DistanceVector results_distances(mAllocationSize); + const std::size_t number_of_results = rSearch.SearchInRadius(aux_point, rRadius[i], results.begin(), results_distances.begin(), mAllocationSize); if (number_of_results > 0) { auto& r_results = rResults[i]; auto& r_results_distance = rResultsDistance[i]; diff --git a/kratos/spatial_containers/specialized_spatial_search_factory.h b/kratos/spatial_containers/specialized_spatial_search_factory.h index ef369245eaaf..336924cf404c 100644 --- a/kratos/spatial_containers/specialized_spatial_search_factory.h +++ b/kratos/spatial_containers/specialized_spatial_search_factory.h @@ -175,6 +175,87 @@ class SpecializedSpatialSearchFactory mpSpatialSearch->SearchConditionsInRadiusExclusive(rStructureConditions, rInputConditions, rRadius, rResults); } + /** + * @brief Search neighbours nodes for one point in a given radius + */ + void SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator); + } + + /** + * @brief Search nearest neighbour node for one point + */ + void SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, rResults, rDataCommunicator); + } + + /** + * @brief Search neighbours elements for one point in a given radius + */ + void SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator); + } + + /** + * @brief Search nearest neighbour element for one point + */ + void SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchElementsOverPointNearestPoint(rStructureElements, rPoint, rResults, rDataCommunicator); + } + + /** + * @brief Search neighbours conditions for one point in a given radius + */ + void SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator); + } + + /** + * @brief Search nearest neighbour condition for one point + */ + void SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, rResults, rDataCommunicator); + } + ///@} ///@name Input and output ///@{ From 7b45f413ee3790566fe9ea57a289c4afc6638774 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 15 Jun 2023 14:53:59 +0200 Subject: [PATCH 096/145] Expose to python --- .../add_search_strategies_to_python.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 9df51b3659ee..79f8188c17ac 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -210,6 +210,9 @@ void AddSearchStrategiesToPython(pybind11::module& m) using ElementsContainerType = SpatialSearch::ElementsContainerType; using NodesContainerType = SpatialSearch::NodesContainerType; using ConditionsContainerType = SpatialSearch::ConditionsContainerType; + using NodeSpatialSearchResultContainerType = SpatialSearchResultContainer; + using ElementSpatialSearchResultContainerType = SpatialSearchResultContainer; + using ConditionSpatialSearchResultContainerType = SpatialSearchResultContainer; py::class_(m, "SpatialSearch") .def(py::init< >()) @@ -598,6 +601,54 @@ void AddSearchStrategiesToPython(pybind11::module& m) .def("SearchConditionsInRadiusInclusive", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const ConditionsContainerType& rInputConditions, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { return self.SearchConditionsInRadiusInclusive(rStructureConditions, rInputConditions, CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); }) + .def("SearchNodesOverPointInRadius", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const array_1d& rPoint, const double Radius, const DataCommunicator& rDataCommunicator) { + NodeSpatialSearchResultContainerType results; + self.SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, results, rDataCommunicator); + return results; + }) + .def("SearchNodesOverPointsInRadius", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesOverPointsInRadius(rStructureNodes, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchNodesOverPointNearestPoint", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const array_1d& rPoint, const DataCommunicator& rDataCommunicator) { + NodeSpatialSearchResultContainerType results; + self.SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, results, rDataCommunicator); + return results; + }) + .def("SearchNodesOverPointsNearestPoint", [&](SpatialSearch& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesOverPointsNearestPoint(rStructureNodes, rInputNodes.begin(), rInputNodes.end(), rDataCommunicator); + }) + .def("SearchElementsOverPointInRadius", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const array_1d& rPoint, const double Radius, const DataCommunicator& rDataCommunicator) { + ElementSpatialSearchResultContainerType results; + self.SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, results, rDataCommunicator); + return results; + }) + .def("SearchElementsOverPointsInRadius", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsOverPointsInRadius(rStructureElements, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsOverPointNearestPoint", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const array_1d& rPoint, const DataCommunicator& rDataCommunicator) { + ElementSpatialSearchResultContainerType results; + self.SearchElementsOverPointNearestPoint(rStructureElements, rPoint, results, rDataCommunicator); + return results; + }) + .def("SearchElementsOverPointsNearestPoint", [&](SpatialSearch& self, const ElementsContainerType& rStructureElements, const NodesContainerType& rInputNodes, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsOverPointsNearestPoint(rStructureElements, rInputNodes.begin(), rInputNodes.end(), rDataCommunicator); + }) + .def("SearchConditionsOverPointInRadius", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, const double Radius, const DataCommunicator& rDataCommunicator) { + ConditionSpatialSearchResultContainerType results; + self.SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, results, rDataCommunicator); + return results; + }) + .def("SearchConditionsOverPointsInRadius", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsOverPointsInRadius(rStructureConditions, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsOverPointNearestPoint", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, const DataCommunicator& rDataCommunicator) { + ConditionSpatialSearchResultContainerType results; + self.SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, results, rDataCommunicator); + return results; + }) + .def("SearchConditionsOverPointsNearestPoint", [&](SpatialSearch& self, const ConditionsContainerType& rStructureConditions, const NodesContainerType& rInputNodes, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsOverPointsNearestPoint(rStructureConditions, rInputNodes.begin(), rInputNodes.end(), rDataCommunicator); + }) ; py::class_(m, "SpecializedSpatialSearch") From b572c4170a242c8606d6539b89681182d724b270 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 15 Jun 2023 14:54:07 +0200 Subject: [PATCH 097/145] Update test --- kratos/tests/test_spatial_search.py | 115 ++++------------------------ 1 file changed, 17 insertions(+), 98 deletions(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index 6a58ab40f97f..64bc07f38e84 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -69,20 +69,17 @@ def GenerateSearch(self, container_type = "KDTree"): # Create node for search self.new_node_id = 100000 + self.point = KM.Point(0.0, 0.0, 0.0) if KM.IsDistributedRun(): # Only added to first rank to actualy check it works in all ranks if self.data_comm.Rank() == 0: - self.node = self.second_model_part.CreateNewNode(self.new_node_id, 0.0, 0.0, 0.0) + self.node = self.second_model_part.CreateNewNode(self.new_node_id, self.point.X, self.point.Y, self.point.Z) self.node.SetSolutionStepValue(KM.PARTITION_INDEX, 0) ParallelFillCommunicator = KratosMPI.ParallelFillCommunicator(self.model_part, self.data_comm) ParallelFillCommunicator.Execute() else: - self.node = self.second_model_part.CreateNewNode(self.new_node_id, 0.0, 0.0, 0.0) + self.node = self.second_model_part.CreateNewNode(self.new_node_id, self.point.X, self.point.Y, self.point.Z) - def test_KDTree_nodes(self): - # Create search - self.GenerateSearch("KDTree") - # Reference solution radius_list = [0.3] distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] @@ -95,12 +92,11 @@ def test_KDTree_nodes(self): # Assert results self.assertEqual(len(results), len(radius_list)) self.assertEqual(len(results[0]), len(distance_ref)) - for distance in distances[0]: self.assertTrue(distance in distance_ref) for node in results[0]: self.assertTrue(node.Id in node_id_ref) - + # Parallel interface (also works in serial mode) results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) @@ -116,33 +112,12 @@ def test_KDTree_nodes(self): for distance in distances: self.assertTrue(distance in distance_ref) - def test_Octree_nodes(self): - # Create search - self.GenerateSearch("Octree") - - # Reference solution - radius_list = [0.3] - distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] - node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] - - # Serial interface - if not KM.IsDistributedRun(): - [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list) - - # Assert results - self.assertEqual(len(results), len(radius_list)) - self.assertEqual(len(results[0]), len(distance_ref)) - - for distance in distances[0]: - self.assertTrue(distance in distance_ref) - for node in results[0]: - self.assertTrue(node.Id in node_id_ref) - - # Parallel interface (also works in serial mode) - results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) + # New interface (works in serial and parallel mode). Interface is very similar to previous one, but it is because a node is type of point and this interface is purely for points (nodes in python) + results = self.search.SearchNodesOverPointsInRadius(self.model_part.Nodes, self.second_model_part.Nodes, radius_list, self.data_comm) self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) - self.assertTrue(results.HasResult(self.new_node_id)) - node_results = results[self.new_node_id] + self.assertFalse(results.HasResult(self.new_node_id)) # This is false because the hash is with coordinates + self.assertTrue(results.HasResult(self.point)) + node_results = results[self.point] if not KM.IsDistributedRun(): self.assertEqual(node_results.NumberOfLocalResults(), len(distance_ref)) self.assertEqual(node_results.NumberOfGlobalResults(), len(distance_ref)) @@ -153,78 +128,22 @@ def test_Octree_nodes(self): for distance in distances: self.assertTrue(distance in distance_ref) - def test_BinsStatic_nodes(self): + def test_KDTree_nodes(self): # Create search - self.GenerateSearch("BinsStatic") - - # Reference solution - radius_list = [0.3] - distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] - node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] + self.GenerateSearch("KDTree") - # Serial interface - if not KM.IsDistributedRun(): - [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list) + def test_Octree_nodes(self): + # Create search + self.GenerateSearch("Octree") - # Assert results - self.assertEqual(len(results), len(radius_list)) - self.assertEqual(len(results[0]), len(distance_ref)) - for distance in distances[0]: - self.assertTrue(distance in distance_ref) - for node in results[0]: - self.assertTrue(node.Id in node_id_ref) - - # Parallel interface (also works in serial mode) - results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) - self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) - self.assertTrue(results.HasResult(self.new_node_id)) - node_results = results[self.new_node_id] - if not KM.IsDistributedRun(): - self.assertEqual(node_results.NumberOfLocalResults(), len(distance_ref)) - self.assertEqual(node_results.NumberOfGlobalResults(), len(distance_ref)) - ids = node_results.GetResultIndices() - for id in ids: - self.assertTrue(id in node_id_ref) - distances = node_results.GetDistances() - for distance in distances: - self.assertTrue(distance in distance_ref) + def test_BinsStatic_nodes(self): + # Create search + self.GenerateSearch("BinsStatic") def test_BinsDynamic_nodes(self): # Create search self.GenerateSearch("BinsDynamic") - # Reference solution - radius_list = [0.3] - distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] - node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] - - # Serial interface - if not KM.IsDistributedRun(): - [results, distances] = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list) - - # Assert results - self.assertEqual(len(results), len(radius_list)) - self.assertEqual(len(results[0]), len(distance_ref)) - for distance in distances[0]: - self.assertTrue(distance in distance_ref) - for node in results[0]: - self.assertTrue(node.Id in node_id_ref) - - # Parallel interface (also works in serial mode) - results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) - self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) - self.assertTrue(results.HasResult(self.new_node_id)) - node_results = results[self.new_node_id] - if not KM.IsDistributedRun(): - self.assertEqual(node_results.NumberOfLocalResults(), len(distance_ref)) - self.assertEqual(node_results.NumberOfGlobalResults(), len(distance_ref)) - ids = node_results.GetResultIndices() - for id in ids: - self.assertTrue(id in node_id_ref) - distances = node_results.GetDistances() - for distance in distances: - self.assertTrue(distance in distance_ref) - if __name__ == '__main__': KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) KratosUnittest.main() \ No newline at end of file From 3b71c9ebb25741c9fca833b1774ae7a08e27d2c0 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 15 Jun 2023 14:53:42 +0200 Subject: [PATCH 098/145] WIP --- .../specialized_spatial_search_mpi.cpp | 17 +++++++++++++++++ .../specialized_spatial_search_mpi.h | 19 +++++++++++++++++++ .../specialized_spatial_search_mpi_factory.h | 19 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp create mode 100644 kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h create mode 100644 kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp new file mode 100644 index 000000000000..e0b481c22847 --- /dev/null +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -0,0 +1,17 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +// System includes + +// External includes + +// Project includes \ No newline at end of file diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h new file mode 100644 index 000000000000..cb5f56e3e995 --- /dev/null +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -0,0 +1,19 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +#pragma once + +// System includes + +// External includes + +// Project includes \ No newline at end of file diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h new file mode 100644 index 000000000000..cb5f56e3e995 --- /dev/null +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h @@ -0,0 +1,19 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Vicente Mataix Ferrandiz +// + +#pragma once + +// System includes + +// External includes + +// Project includes \ No newline at end of file From f556a3ad2ac96d6f7edbf8ec5c9af29b79f563ee Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 19 Jun 2023 16:46:21 +0200 Subject: [PATCH 099/145] Minor --- kratos/spatial_containers/spatial_search.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.h b/kratos/spatial_containers/spatial_search.h index 1134a3db8926..4602b6677566 100644 --- a/kratos/spatial_containers/spatial_search.h +++ b/kratos/spatial_containers/spatial_search.h @@ -905,8 +905,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @brief Search neighbours for every Condition in "rStructureConditions" excluding itself * @param rStructureConditions List of conditions modelpart against which the neighbours are searched * @param rRadius List of search radius for every Condition - * @param rDataCommunicator The data communicator - * @return The results maps + * @param rDataCommunicator The data communicator + * @return The results maps */ virtual ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( const ConditionsContainerType& rStructureConditions, @@ -935,8 +935,8 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @param rStructureConditions List of conditions against which the neighbours are searched * @param rInputConditions List of conditions to be searched * @param rRadius List of search radius for every Condition - * @param rDataCommunicator The data communicator - * @return The results maps + * @param rDataCommunicator The data communicator + * @return The results maps */ virtual ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( const ConditionsContainerType& rStructureConditions, From 5806ed3cd23bf34bb6b21a3e860777f917680dd1 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 19 Jun 2023 17:41:51 +0200 Subject: [PATCH 100/145] [Core] Moving more BB methods to `SearchUtilities` --- .../geometrical_objects_bins.cpp | 46 -------------- .../geometrical_objects_bins.h | 11 ---- kratos/utilities/search_utilities.h | 62 ++++++++++++++++++- 3 files changed, 61 insertions(+), 58 deletions(-) diff --git a/kratos/spatial_containers/geometrical_objects_bins.cpp b/kratos/spatial_containers/geometrical_objects_bins.cpp index 525660ad9da4..4241b35a2298 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.cpp +++ b/kratos/spatial_containers/geometrical_objects_bins.cpp @@ -252,52 +252,6 @@ void GeometricalObjectsBins::SearchIsInside( /***********************************************************************************/ /***********************************************************************************/ -bool GeometricalObjectsBins::PointIsInsideBoundingBox(const array_1d& rCoords) -{ - // Get the bounding box points - const auto& r_max_point = mBoundingBox.GetMaxPoint(); - const auto& r_min_point = mBoundingBox.GetMinPoint(); - - // The Bounding Box check - if (rCoords[0] < r_max_point[0] && rCoords[0] > r_min_point[0]) // check x-direction - if (rCoords[1] < r_max_point[1] && rCoords[1] > r_min_point[1]) // check y-direction - if (rCoords[2] < r_max_point[2] && rCoords[2] > r_min_point[2]) // check z-direction - return true; - return false; -} - -/***********************************************************************************/ -/***********************************************************************************/ - -bool GeometricalObjectsBins::PointIsInsideBoundingBoxWithTolerance( - const array_1d& rCoords, - const double Tolerance - ) -{ - // Get the bounding box points - auto max_point = mBoundingBox.GetMaxPoint(); - auto min_point = mBoundingBox.GetMinPoint(); - - // Apply Tolerances (only in non zero BB cases) - const double epsilon = std::numeric_limits::epsilon(); - if (norm_2(max_point) > epsilon && norm_2(min_point) > epsilon) { - for (unsigned int i=0; i<3; ++i) { - max_point[i] += Tolerance; - min_point[i] -= Tolerance; - } - } - - // The Bounding Box check - if (rCoords[0] < max_point[0] && rCoords[0] > min_point[0]) // check x-direction - if (rCoords[1] < max_point[1] && rCoords[1] > min_point[1]) // check y-direction - if (rCoords[2] < max_point[2] && rCoords[2] > min_point[2]) // check z-direction - return true; - return false; -} - -/***********************************************************************************/ -/***********************************************************************************/ - void GeometricalObjectsBins::CalculateCellSize(const std::size_t NumberOfCells) { const std::size_t avarage_number_of_cells = static_cast(std::pow(static_cast(NumberOfCells), 1.00 / Dimension)); diff --git a/kratos/spatial_containers/geometrical_objects_bins.h b/kratos/spatial_containers/geometrical_objects_bins.h index 1b8d3709df23..6966d7215f76 100644 --- a/kratos/spatial_containers/geometrical_objects_bins.h +++ b/kratos/spatial_containers/geometrical_objects_bins.h @@ -587,17 +587,6 @@ class KRATOS_API(KRATOS_CORE) GeometricalObjectsBins */ bool PointIsInsideBoundingBox(const array_1d& rCoords); - /** - * @brief This method checks if a point is inside any bounding box of the global bounding boxes considering a certain tolerance - * @param rCoords The coordinates of the point - * @param Tolerance The tolerance - * @return True if the point is inside the bounding box - */ - bool PointIsInsideBoundingBoxWithTolerance( - const array_1d& rCoords, - const double Tolerance - ); - /** * @brief Calculate the cell sizes to be as equilateral as possible and tries to approximate (roughly) the given number of cells * @details This method calculates the cell sizes to be as equilateral as possible and tries to approximate (roughly) the given number of cells diff --git a/kratos/utilities/search_utilities.h b/kratos/utilities/search_utilities.h index 60e13f597439..262726312584 100644 --- a/kratos/utilities/search_utilities.h +++ b/kratos/utilities/search_utilities.h @@ -18,7 +18,8 @@ // External includes // Project includes -#include "containers/array_1d.h" +#include "geometries/bounding_box.h" +#include "geometries/point.h" namespace Kratos { @@ -73,6 +74,31 @@ class SearchUtilities /** * @brief Check if a point is inside a bounding box + * @details Bounding box class implementation + * @param rBoundingBox The bounding box + * @param rCoords The point + * @return true if the point is inside the bounding box + */ + static bool PointIsInsideBoundingBox( + const BoundingBox& rBoundingBox, + const array_1d& rCoords + ) + { + // Get the bounding box points + const auto& r_max_point = rBoundingBox.GetMaxPoint(); + const auto& r_min_point = rBoundingBox.GetMinPoint(); + + // The Bounding Box check + if (rCoords[0] < r_max_point[0] && rCoords[0] > r_min_point[0]) // check x-direction + if (rCoords[1] < r_max_point[1] && rCoords[1] > r_min_point[1]) // check y-direction + if (rCoords[2] < r_max_point[2] && rCoords[2] > r_min_point[2]) // check z-direction + return true; + return false; + } + + /** + * @brief Check if a point is inside a bounding box + * @details Bounding box array of 6 doubles implementation * @param rBoundingBox The bounding box * @param rCoords The point * @return true if the point is inside the bounding box @@ -102,6 +128,40 @@ class SearchUtilities std::vector& rBoundingBoxesWithTolerance ); + /** + * @brief This method checks if a point is inside any bounding box of the global bounding boxes considering a certain tolerance + * @param rBoundingBox The bounding box + * @param rCoords The coordinates of the point + * @param Tolerance The tolerance + * @return True if the point is inside the bounding box + */ + static bool PointIsInsideBoundingBoxWithTolerance( + const BoundingBox& rBoundingBox, + const array_1d& rCoords, + const double Tolerance + ) + { + // Get the bounding box points + auto max_point = rBoundingBox.GetMaxPoint(); + auto min_point = rBoundingBox.GetMinPoint(); + + // Apply Tolerances (only in non zero BB cases) + const double epsilon = std::numeric_limits::epsilon(); + if (norm_2(max_point) > epsilon && norm_2(min_point) > epsilon) { + for (unsigned int i=0; i<3; ++i) { + max_point[i] += Tolerance; + min_point[i] -= Tolerance; + } + } + + // The Bounding Box check + if (rCoords[0] < max_point[0] && rCoords[0] > min_point[0]) // check x-direction + if (rCoords[1] < max_point[1] && rCoords[1] > min_point[1]) // check y-direction + if (rCoords[2] < max_point[2] && rCoords[2] > min_point[2]) // check z-direction + return true; + return false; + } + /** * @brief Compute the bounding boxes of the given bounding boxes from a given tolerance, additionally checking if the bounding boxes are initialized * @details This method is used when the bounding boxes are not initialized From 2b618023937f986b546f7e09241541dbdc395abc Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 19 Jun 2023 17:54:00 +0200 Subject: [PATCH 101/145] Moving to source --- kratos/utilities/search_utilities.cpp | 66 +++++++++++++++++++++++++ kratos/utilities/search_utilities.h | 69 ++++++--------------------- 2 files changed, 80 insertions(+), 55 deletions(-) diff --git a/kratos/utilities/search_utilities.cpp b/kratos/utilities/search_utilities.cpp index 38086fd5ef1d..a61eb2a42ef3 100644 --- a/kratos/utilities/search_utilities.cpp +++ b/kratos/utilities/search_utilities.cpp @@ -21,6 +21,72 @@ namespace Kratos { +bool SearchUtilities::PointIsInsideBoundingBox( + const BoundingBox& rBoundingBox, + const array_1d& rCoords + ) +{ + // Get the bounding box points + const auto& r_max_point = rBoundingBox.GetMaxPoint(); + const auto& r_min_point = rBoundingBox.GetMinPoint(); + + // The Bounding Box check + if (rCoords[0] < r_max_point[0] && rCoords[0] > r_min_point[0]) // check x-direction + if (rCoords[1] < r_max_point[1] && rCoords[1] > r_min_point[1]) // check y-direction + if (rCoords[2] < r_max_point[2] && rCoords[2] > r_min_point[2]) // check z-direction + return true; + return false; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +bool SearchUtilities::PointIsInsideBoundingBox( + const BoundingBoxType& rBoundingBox, + const array_1d& rCoords + ) +{ + // The Bounding Box should have some tolerance already! + if (rCoords[0] < rBoundingBox[0] && rCoords[0] > rBoundingBox[1]) // check x-direction + if (rCoords[1] < rBoundingBox[2] && rCoords[1] > rBoundingBox[3]) // check y-direction + if (rCoords[2] < rBoundingBox[4] && rCoords[2] > rBoundingBox[5]) // check z-direction + return true; + return false; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +bool SearchUtilities::PointIsInsideBoundingBoxWithTolerance( + const BoundingBox& rBoundingBox, + const array_1d& rCoords, + const double Tolerance + ) +{ + // Get the bounding box points + auto max_point = rBoundingBox.GetMaxPoint(); + auto min_point = rBoundingBox.GetMinPoint(); + + // Apply Tolerances (only in non zero BB cases) + const double epsilon = std::numeric_limits::epsilon(); + if (norm_2(max_point) > epsilon && norm_2(min_point) > epsilon) { + for (unsigned int i=0; i<3; ++i) { + max_point[i] += Tolerance; + min_point[i] -= Tolerance; + } + } + + // The Bounding Box check + if (rCoords[0] < max_point[0] && rCoords[0] > min_point[0]) // check x-direction + if (rCoords[1] < max_point[1] && rCoords[1] > min_point[1]) // check y-direction + if (rCoords[2] < max_point[2] && rCoords[2] > min_point[2]) // check z-direction + return true; + return false; +} + +/***********************************************************************************/ +/***********************************************************************************/ + void SearchUtilities::ComputeBoundingBoxesWithTolerance( const std::vector& rBoundingBoxes, const double Tolerance, diff --git a/kratos/utilities/search_utilities.h b/kratos/utilities/search_utilities.h index 262726312584..a8b3c2869c1f 100644 --- a/kratos/utilities/search_utilities.h +++ b/kratos/utilities/search_utilities.h @@ -82,19 +82,7 @@ class SearchUtilities static bool PointIsInsideBoundingBox( const BoundingBox& rBoundingBox, const array_1d& rCoords - ) - { - // Get the bounding box points - const auto& r_max_point = rBoundingBox.GetMaxPoint(); - const auto& r_min_point = rBoundingBox.GetMinPoint(); - - // The Bounding Box check - if (rCoords[0] < r_max_point[0] && rCoords[0] > r_min_point[0]) // check x-direction - if (rCoords[1] < r_max_point[1] && rCoords[1] > r_min_point[1]) // check y-direction - if (rCoords[2] < r_max_point[2] && rCoords[2] > r_min_point[2]) // check z-direction - return true; - return false; - } + ); /** * @brief Check if a point is inside a bounding box @@ -106,26 +94,6 @@ class SearchUtilities static bool PointIsInsideBoundingBox( const BoundingBoxType& rBoundingBox, const array_1d& rCoords - ) - { - // The Bounding Box should have some tolerance already! - if (rCoords[0] < rBoundingBox[0] && rCoords[0] > rBoundingBox[1]) // check x-direction - if (rCoords[1] < rBoundingBox[2] && rCoords[1] > rBoundingBox[3]) // check y-direction - if (rCoords[2] < rBoundingBox[4] && rCoords[2] > rBoundingBox[5]) // check z-direction - return true; - return false; - } - - /** - * @brief Compute the bounding boxes of the given bounding boxes from a given tolerance - * @param rBoundingBoxes The bounding boxes - * @param Tolerance The tolerance - * @param rBoundingBoxesWithTolerance The resulting bounding boxes with the applied tolerance - */ - static void ComputeBoundingBoxesWithTolerance( - const std::vector& rBoundingBoxes, - const double Tolerance, - std::vector& rBoundingBoxesWithTolerance ); /** @@ -139,28 +107,19 @@ class SearchUtilities const BoundingBox& rBoundingBox, const array_1d& rCoords, const double Tolerance - ) - { - // Get the bounding box points - auto max_point = rBoundingBox.GetMaxPoint(); - auto min_point = rBoundingBox.GetMinPoint(); - - // Apply Tolerances (only in non zero BB cases) - const double epsilon = std::numeric_limits::epsilon(); - if (norm_2(max_point) > epsilon && norm_2(min_point) > epsilon) { - for (unsigned int i=0; i<3; ++i) { - max_point[i] += Tolerance; - min_point[i] -= Tolerance; - } - } - - // The Bounding Box check - if (rCoords[0] < max_point[0] && rCoords[0] > min_point[0]) // check x-direction - if (rCoords[1] < max_point[1] && rCoords[1] > min_point[1]) // check y-direction - if (rCoords[2] < max_point[2] && rCoords[2] > min_point[2]) // check z-direction - return true; - return false; - } + ); + + /** + * @brief Compute the bounding boxes of the given bounding boxes from a given tolerance + * @param rBoundingBoxes The bounding boxes + * @param Tolerance The tolerance + * @param rBoundingBoxesWithTolerance The resulting bounding boxes with the applied tolerance + */ + static void ComputeBoundingBoxesWithTolerance( + const std::vector& rBoundingBoxes, + const double Tolerance, + std::vector& rBoundingBoxesWithTolerance + ); /** * @brief Compute the bounding boxes of the given bounding boxes from a given tolerance, additionally checking if the bounding boxes are initialized From b82067ac34d8ccafed5d8daea93ff35f595be71f Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 19 Jun 2023 18:26:25 +0200 Subject: [PATCH 102/145] Adding `MPISynchronousPointSynchronizationWithDistances` --- .../utilities/test_mpi_search_utilities.cpp | 37 +++++++- kratos/mpi/utilities/mpi_search_utilities.h | 87 +++++++++++++++++-- 2 files changed, 113 insertions(+), 11 deletions(-) diff --git a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp index 4aa84989dec7..29fbea57a3cb 100644 --- a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp +++ b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp @@ -62,9 +62,42 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPoint // Check the results KRATOS_CHECK_EQUAL(static_cast(all_points_coordinates.size()), 2 * 3 * world_size); for (int i_rank = 0; i_rank < world_size; ++i_rank) { + value = static_cast(i_rank); + value2 = 2 * value; + for (int j = 0; j < 3; ++j) { + KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j ], value ); + KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j + 3], value2); + } + } +} + +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPointsAreDifferentWithDistances, KratosMPICoreFastSuite) +{ + // The data communicator + const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); + + // MPI data communicator + const int rank = r_data_comm.Rank(); + const int world_size = r_data_comm.Size(); + + // Define test data + double value = static_cast(rank); + double value2 = 2 * value; + std::vector points({Point(value, value, value), Point(value2, value2, value2)}); + std::vector local_distances = {value, value2}; + + // Call the function + std::vector all_points_coordinates; + auto distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(points.begin(), points.end(), all_points_coordinates, local_distances, r_data_comm); + + // Check the results + KRATOS_CHECK_EQUAL(static_cast(all_points_coordinates.size()), 2 * 3 * world_size); + for (int i_rank = 0; i_rank < world_size; ++i_rank) { + value = static_cast(i_rank); + value2 = 2 * value; + KRATOS_CHECK_DOUBLE_EQUAL(distances[i_rank * 2 ], value ); + KRATOS_CHECK_DOUBLE_EQUAL(distances[i_rank * 2 + 1], value2); for (int j = 0; j < 3; ++j) { - value = static_cast(i_rank); - value2 = 2 * value; KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j ], value ); KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j + 3], value2); } diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 03d3ce51a395..5924ca700ff9 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -47,6 +47,10 @@ class MPISearchUtilities * @param itPointBegin Iterator to the beginning of the points range * @param itPointEnd Iterator to the end of the points range * @param rAllPointsCoordinates vector where the computed coordinates will be stored + * @param rDataCommunicator The data communicator + * @param AllPointsAreTheSame If all points are the same (-1 to be computed, 0 if not, 1 if yes) + * @param NumberOfPoints The number of points in the range + * @param TotalNumberOfPoints The total number of points in all the ranges * @return The number of points in the range * @tparam TPointIteratorType The type of the point iterator */ @@ -55,16 +59,23 @@ class MPISearchUtilities TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, std::vector& rAllPointsCoordinates, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + int AllPointsAreTheSame = -1, + int NumberOfPoints = -1, + int TotalNumberOfPoints = -1 ) { // First check that the points are the same in all processes - int number_of_points, total_number_of_points; - const bool all_points_are_the_same = CheckAllPointsAreTheSame(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); + if (AllPointsAreTheSame < 0) { + AllPointsAreTheSame = static_cast(CheckAllPointsAreTheSame(itPointBegin, itPointEnd, NumberOfPoints, TotalNumberOfPoints, rDataCommunicator)); + } + + KRATOS_DEBUG_ERROR_IF(NumberOfPoints < 0) << "The number of points is negative" << std::endl; + KRATOS_DEBUG_ERROR_IF(TotalNumberOfPoints < 0) << "The total number of points is negative" << std::endl; // If all points are the same - if (all_points_are_the_same) { - rAllPointsCoordinates.resize(number_of_points * 3); + if (AllPointsAreTheSame) { + rAllPointsCoordinates.resize(NumberOfPoints * 3); std::size_t counter = 0; array_1d coordinates; unsigned int i_coord; @@ -81,12 +92,12 @@ class MPISearchUtilities // Getting global number of points std::vector points_per_partition(world_size); - std::vector send_points_per_partition(1, number_of_points); + std::vector send_points_per_partition(1, NumberOfPoints); rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); // Getting global coordinates - rAllPointsCoordinates.resize(total_number_of_points * 3); - std::vector send_points_coordinates(number_of_points * 3); + rAllPointsCoordinates.resize(TotalNumberOfPoints * 3); + std::vector send_points_coordinates(NumberOfPoints * 3); std::size_t counter = 0; array_1d coordinates; unsigned int i_coord; @@ -112,8 +123,66 @@ class MPISearchUtilities rDataCommunicator.AllGatherv(send_points_coordinates, rAllPointsCoordinates, recv_sizes, recv_offsets); } - return number_of_points; + return NumberOfPoints; } + + /** + * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. + * @details With radius + * @param itPointBegin Iterator to the beginning of the points range + * @param itPointEnd Iterator to the end of the points range + * @param rAllPointsCoordinates vector where the computed coordinates will be stored + * @return The resulting whole radius vector + * @tparam TPointIteratorType The type of the point iterator + */ + template + static std::vector MPISynchronousPointSynchronizationWithDistances( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + std::vector& rAllPointsCoordinates, + const std::vector& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + // First check that the points are the same in all processes + int number_of_points, total_number_of_points; + const bool all_points_are_the_same = CheckAllPointsAreTheSame(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); + + // Synchronize points + MPISynchronousPointSynchronization(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, static_cast(all_points_are_the_same), number_of_points, total_number_of_points); + + // Synchonize radius + if (all_points_are_the_same) { // If all points are the same + return rRadius; + } else { // If not + // The resulting distances + std::vector all_points_distances(total_number_of_points); + + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Getting global number of points + std::vector points_per_partition(world_size); + std::vector send_points_per_partition(1, number_of_points); + rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); + + // Generate vectors with sizes for AllGatherv + std::vector recv_sizes(world_size, 0); + for (int i_rank = 0; i_rank < world_size; ++i_rank) { + recv_sizes[i_rank] = points_per_partition[i_rank]; + } + std::vector recv_offsets(world_size, 0); + for (int i_rank = 1; i_rank < world_size; ++i_rank) { + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + } + + // Invoque AllGatherv + rDataCommunicator.AllGatherv(rRadius, all_points_distances, recv_sizes, recv_offsets); + + return all_points_distances; + } + } + ///@} private: ///@name Private Operations From 3c90e33262ee2824e70a36e74fe06117fea9e57f Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 19 Jun 2023 18:47:30 +0200 Subject: [PATCH 103/145] More WIP --- .../add_mpi_search_strategies_to_python.cpp | 26 + .../specialized_spatial_search_mpi.cpp | 202 ++++++- .../specialized_spatial_search_mpi.h | 525 +++++++++++++++++- .../specialized_spatial_search_mpi_factory.h | 240 +++++++- 4 files changed, 990 insertions(+), 3 deletions(-) diff --git a/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp index e231a705f796..9421fee1c4ae 100644 --- a/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp +++ b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp @@ -18,6 +18,8 @@ #include "includes/define_python.h" #include "includes/model_part.h" #include "mpi/python/add_mpi_search_strategies_to_python.h" +#include "mpi/spatial_containers/specialized_spatial_search_mpi.h" +#include "mpi/spatial_containers/specialized_spatial_search_mpi_factory.h" namespace Kratos::Python { @@ -26,6 +28,30 @@ void AddMPISearchStrategiesToPython(pybind11::module& m) { namespace py = pybind11; + py::class_(m, "SpecializedSpatialSearch") + .def(py::init< >()) + .def(py::init()) + ; + + py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchKDTree") + .def(py::init< >()) + .def(py::init()) + ; + + py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchOctree") + .def(py::init< >()) + .def(py::init()) + ; + + py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchBinsStatic") + .def(py::init< >()) + .def(py::init()) + ; + + py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchBinsDynamic") + .def(py::init< >()) + .def(py::init()) + ; } diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp index e0b481c22847..8b2cccc58d16 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -14,4 +14,204 @@ // External includes -// Project includes \ No newline at end of file +// Project includes +#include "mpi/spatial_containers/specialized_spatial_search_mpi.h" + +namespace Kratos +{ + +template +typename SpecializedSpatialSearchMPI::ElementSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // The points vector + std::vector points(rInputElements.size()); + for (std::size_t i = 0; i < rInputElements.size(); ++i) { + points[i] = Point((rInputElements.begin() + i)->GetGeometry().Center()); + } + + return SearchElementsOverPointsInRadius(rStructureElements, points.begin(), points.end(), rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchElementsInRadiusInclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + NodeSpatialSearchResultContainerMapType results; + + return results; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return SearchNodesOverPointsInRadius(rStructureNodes, rInputNodes.begin(), rInputNodes.end(), rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchNodesInRadiusInclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + NodeSpatialSearchResultContainerMapType results; + + return results; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + // The points vector + std::vector points(rInputConditions.size()); + for (std::size_t i = 0; i < rInputConditions.size(); ++i) { + points[i] = Point((rInputConditions.begin() + i)->GetGeometry().Center()); + } + + return SearchConditionsOverPointsInRadius(rStructureConditions, points.begin(), points.end(), rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchConditionsInRadiusInclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + NodeSpatialSearchResultContainerMapType results; + + return results; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) +{ + +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template class SpecializedSpatialSearchMPI; +template class SpecializedSpatialSearchMPI; +template class SpecializedSpatialSearchMPI; +template class SpecializedSpatialSearchMPI; + +} // namespace Kratos \ No newline at end of file diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index cb5f56e3e995..f2380ceabbe8 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -16,4 +16,527 @@ // External includes -// Project includes \ No newline at end of file +// Project includes +#include "mpi/utilities/mpi_search_utilities.h" +#include "spatial_containers/specialized_spatial_search.h" + +namespace Kratos +{ +///@name Kratos Globals +///@{ + +///@} +///@name Type Definitions +///@{ + +///@} +///@name Enum's +///@{ + +///@} +///@name Functions +///@{ + +///@} +///@name Kratos Classes +///@{ + +/** +* @class SpecializedSpatialSearchMPI +* @ingroup KratosCore +* @brief This class is used to search for elements, conditions and nodes in a given model part (MPI version) +* @details In order to perform the search it uses as backend some of the the spatial containers defined `spatial_containers` folder +* @tparam TSearchBackend The spatial container to be used as backend +* @author Vicente Mataix Ferrandiz +*/ +template +class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI + : public SpecializedSpatialSearch +{ +public: + ///@name Type Definitions + ///@{ + + /// Pointer definition of SpecializedSpatialSearchMPI + KRATOS_CLASS_POINTER_DEFINITION(SpecializedSpatialSearchMPI); + + /// The base type + using BaseType = SpecializedSpatialSearch; + + /// Nodes classes + using NodesContainerType = typename BaseType::NodesContainerType; + using ResultNodesContainerType = typename BaseType::ResultNodesContainerType; + using VectorResultNodesContainerType = typename BaseType::VectorResultNodesContainerType; + using NodeSpatialSearchResultContainerType = typename BaseType::NodeSpatialSearchResultContainerType; + using NodeSpatialSearchResultContainerMapType = typename SpatialSearch::NodeSpatialSearchResultContainerMapType; + + /// Elements classes + using ElementsContainerType = typename BaseType::ElementsContainerType; + using ResultElementsContainerType = typename BaseType::ResultElementsContainerType; + using VectorResultElementsContainerType = typename BaseType::VectorResultElementsContainerType; + using ElementSpatialSearchResultContainerType = typename BaseType::ElementSpatialSearchResultContainerType; + using ElementSpatialSearchResultContainerMapType = typename SpatialSearch::ElementSpatialSearchResultContainerMapType; + + /// Conditions classes + using ConditionsContainerType = typename BaseType::ConditionsContainerType; + using ResultConditionsContainerType = typename BaseType::ResultConditionsContainerType; + using VectorResultConditionsContainerType = typename BaseType::VectorResultConditionsContainerType; + using ConditionSpatialSearchResultContainerType = typename BaseType::ConditionSpatialSearchResultContainerType; + using ConditionSpatialSearchResultContainerMapType = typename SpatialSearch::ConditionSpatialSearchResultContainerMapType; + + /// Input/output types + using RadiusArrayType = typename BaseType::RadiusArrayType; + using DistanceType = typename BaseType::DistanceType; + using VectorDistanceType = typename BaseType::VectorDistanceType; + + ///@} + ///@name Life Cycle + ///@{ + + /** + * @brief Default constructor. + */ + SpecializedSpatialSearchMPI() + : BaseType() + { + } + + /** + * @brief Constructor with parameters + * @param ThisParameters The parameters to be considered + */ + SpecializedSpatialSearchMPI(Parameters ThisParameters) + : BaseType(ThisParameters) + { + } + + /// Destructor. + ~SpecializedSpatialSearchMPI() override = default; + + ///@} + ///@name Operators + ///@{ + + ///@} + ///@name Operations + ///@{ + + //************************************************************************ + // Elemental Exclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every element in "Inputelements" excluding itself + */ + ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + + //************************************************************************ + // Elemental Inclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every element in "InputElements" including itself + */ + NodeSpatialSearchResultContainerMapType SearchElementsInRadiusInclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + + //************************************************************************ + // Nodal Exclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every node in "InputNodes" excluding itself + */ + NodeSpatialSearchResultContainerMapType SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + + //************************************************************************ + // Nodal Inclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every node in "InputNodes" including itself + */ + NodeSpatialSearchResultContainerMapType SearchNodesInRadiusInclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + + //************************************************************************ + // Conditional Exclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every Condition in "InputConditions" excluding itself + */ + ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + + //************************************************************************ + // Conditional Inclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every Condition in "InputConditions" including itself + */ + NodeSpatialSearchResultContainerMapType SearchConditionsInRadiusInclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + + //************************************************************************ + // Point vs Entities (these are new interfaces and already use the new containers) + //************************************************************************ + + /** + * @brief Search neighbours nodes for one point in a given radius + */ + void SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search neighbours nodes for several points in a given radius + */ + template + NodeSpatialSearchResultContainerMapType SearchNodesOverPointsInRadius ( + const NodesContainerType& rStructureNodes, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + // Prepare MPI search + std::vector all_points_coordinates; + const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + + // Perform the corresponding searchs + NodeSpatialSearchResultContainerMapType results; + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + SearchNodesOverPointInRadius(rStructureNodes, point, all_points_distances[i_node], r_partial_result, rDataCommunicator); + } + return results; + } + + /** + * @brief Search nearest neighbour node for one point + */ + void SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search nearest neighbour node for several points + */ + template + NodeSpatialSearchResultContainerMapType SearchNodesOverPointsNearestPoint ( + const NodesContainerType& rStructureNodes, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + // Prepare MPI search + std::vector all_points_coordinates; + MPISearchUtilities::MPISynchronousPointSynchronization(itPointBegin, itPointEnd, all_points_coordinates, rDataCommunicator); + + // Perform the corresponding searchs + NodeSpatialSearchResultContainerMapType results; + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + SearchNodesOverPointNearestPoint(rStructureNodes, point, r_partial_result, rDataCommunicator); + } + return results; + } + + /** + * @brief Search neighbours elements for one point in a given radius + */ + void SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search neighbours elements for several points in a given radius + */ + template + ElementSpatialSearchResultContainerMapType SearchElementsOverPointsInRadius ( + const ElementsContainerType& rStructureElements, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + // Prepare MPI search + std::vector all_points_coordinates; + const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + + // Perform the corresponding searchs + ElementSpatialSearchResultContainerMapType results; + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + SearchElementsOverPointInRadius(rStructureElements, point, all_points_distances[i_node], r_partial_result, rDataCommunicator); + } + return results; + } + + /** + * @brief Search nearest neighbour element for one point + */ + void SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search nearest neighbour element for several points + */ + template + ElementSpatialSearchResultContainerMapType SearchElementsOverPointsNearestPoint ( + const ElementsContainerType& rStructureElements, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + // Prepare MPI search + std::vector all_points_coordinates; + MPISearchUtilities::MPISynchronousPointSynchronization(itPointBegin, itPointEnd, all_points_coordinates, rDataCommunicator); + + // Perform the corresponding searchs + ElementSpatialSearchResultContainerMapType results; + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + SearchElementsOverPointNearestPoint(rStructureElements, point, r_partial_result, rDataCommunicator); + } + return results; + } + + /** + * @brief Search neighbours conditions for one point in a given radius + */ + void SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search neighbours conditions for several points in a given radius + */ + template + ConditionSpatialSearchResultContainerMapType SearchConditionsOverPointsInRadius ( + const ConditionsContainerType& rStructureConditions, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + // Prepare MPI search + std::vector all_points_coordinates; + const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + + // Perform the corresponding searchs + ConditionSpatialSearchResultContainerMapType results; + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + SearchConditionsOverPointInRadius(rStructureConditions, point, all_points_distances[i_node], r_partial_result, rDataCommunicator); + } + return results; + } + + /** + * @brief Search nearest neighbour condition for one point + */ + void SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override; + + /** + * @brief Search nearest neighbour condition for several points + */ + template + ConditionSpatialSearchResultContainerMapType SearchConditionsOverPointsNearestPoint ( + const ConditionsContainerType& rStructureConditions, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + // Prepare MPI search + std::vector all_points_coordinates; + MPISearchUtilities::MPISynchronousPointSynchronization(itPointBegin, itPointEnd, all_points_coordinates, rDataCommunicator); + + // Perform the corresponding searchs + ConditionSpatialSearchResultContainerMapType results; + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + SearchConditionsOverPointNearestPoint(rStructureConditions, point, r_partial_result, rDataCommunicator); + } + return results; + } + + + ///@} + ///@name Input and output + ///@{ + + /// Turn back information as a string. + std::string Info() const override + { + std::stringstream buffer; + buffer << "SpecializedSpatialSearchMPI" ; + + return buffer.str(); + } + + /// Print information about this object. + void PrintInfo(std::ostream& rOStream) const override + { + rOStream << "SpecializedSpatialSearchMPI"; + } + + /// Print object's data. + void PrintData(std::ostream& rOStream) const override + { + + } + + ///@} + ///@name Friends + ///@{ + + ///@} +protected: + ///@name Protected static Member Variables + ///@{ + + ///@} + ///@name Protected member Variables + ///@{ + + ///@} + ///@name Protected Operations + ///@{ + + /** + * @brief This method returns the default parameters + * @return The default parameters + */ + Kratos::Parameters GetDefaultParameters() const + { + return BaseType::GetDefaultParameters(); + } + + ///@} +private: + ///@name Static Member Variables + ///@{ + + ///@} + ///@name Member Variables + ///@{ + + ///@} + ///@name Private Operations + ///@{ + + ///@} + ///@name Un accessible methods + ///@{ + + /// Assignment operator. + SpecializedSpatialSearchMPI& operator=(SpecializedSpatialSearchMPI const& rOther) + { + return *this; + } + + /// Copy constructor. + SpecializedSpatialSearchMPI(SpecializedSpatialSearchMPI const& rOther) + { + *this = rOther; + } + + ///@} +}; // Class SpecializedSpatialSearchMPI + +///@} +///@name Type Definitions +///@{ + +///@} +///@name Input and output +///@{ + +/// output stream function +template +inline std::ostream& operator << (std::ostream& rOStream, + const SpecializedSpatialSearchMPI& rThis) +{ + rThis.PrintInfo(rOStream); + rOStream << std::endl; + rThis.PrintData(rOStream); + + return rOStream; +} + +///@} + +///@}addtogroup block + +} // namespace Kratos. \ No newline at end of file diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h index cb5f56e3e995..903e93fbb767 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h @@ -16,4 +16,242 @@ // External includes -// Project includes \ No newline at end of file +// Project includes +#include "mpi/spatial_containers/specialized_spatial_search_mpi.h" + +namespace Kratos +{ +///@name Kratos Globals +///@{ + +///@} +///@name Type Definitions +///@{ + +///@} +///@name Enum's +///@{ + +///@} +///@name Functions +///@{ + +///@} +///@name Kratos Classes +///@{ + +/** +* @class SpecializedSpatialSearchMPIFactory +* @ingroup KratosCore +* @brief Factory for the specialized spatial search +* @author Vicente Mataix Ferrandiz +*/ +class SpecializedSpatialSearchMPIFactory + : public SpatialSearch +{ +public: + ///@name Type Definitions + ///@{ + + KRATOS_CLASS_POINTER_DEFINITION(SpecializedSpatialSearchMPIFactory); + + ///@} + ///@name Life Cycle + ///@{ + + /// Default constructor. + SpecializedSpatialSearchMPIFactory() + { + Parameters default_parameters = GetDefaultParameters(); + Parameters search_parameters(default_parameters["search_parameters"].WriteJsonString()); + mpSpatialSearch = SpatialSearch::Pointer(new SpecializedSpatialSearchMPI(search_parameters)); + } + + /// Constructor with parameters + SpecializedSpatialSearchMPIFactory(Parameters ThisParameters) + { + Parameters default_parameters = GetDefaultParameters(); + ThisParameters.RecursivelyValidateAndAssignDefaults(default_parameters); + const std::string& r_container_type = ThisParameters["container_type"].GetString(); + Parameters search_parameters(ThisParameters["search_parameters"].WriteJsonString()); + if (r_container_type == "KDTree" || r_container_type == "kd_tree") { + mpSpatialSearch = SpatialSearch::Pointer(new SpecializedSpatialSearchMPI(search_parameters)); + } else if (r_container_type == "Octree" || r_container_type == "octree") { + mpSpatialSearch = SpatialSearch::Pointer(new SpecializedSpatialSearchMPI(search_parameters)); + } else if (r_container_type == "BinsStatic" || r_container_type == "bins_static") { + mpSpatialSearch = SpatialSearch::Pointer(new SpecializedSpatialSearchMPI(search_parameters)); + } else if (r_container_type == "BinsDynamic" || r_container_type == "bins_dynamic") { + mpSpatialSearch = SpatialSearch::Pointer(new SpecializedSpatialSearchMPI(search_parameters)); + } else { + KRATOS_ERROR << "Unknown container type: " << r_container_type << std::endl; + } + } + + /// Destructor. + ~SpecializedSpatialSearchMPIFactory() override = default; + + ///@} + ///@name Operations + ///@{ + + /** + * @brief Search neighbours nodes for one point in a given radius + */ + void SearchNodesOverPointInRadius ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + const double Radius, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator); + } + + /** + * @brief Search nearest neighbour node for one point + */ + void SearchNodesOverPointNearestPoint ( + const NodesContainerType& rStructureNodes, + const array_1d& rPoint, + NodeSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, rResults, rDataCommunicator); + } + + /** + * @brief Search neighbours elements for one point in a given radius + */ + void SearchElementsOverPointInRadius ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + const double Radius, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator); + } + + /** + * @brief Search nearest neighbour element for one point + */ + void SearchElementsOverPointNearestPoint ( + const ElementsContainerType& rStructureElements, + const array_1d& rPoint, + ElementSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchElementsOverPointNearestPoint(rStructureElements, rPoint, rResults, rDataCommunicator); + } + + /** + * @brief Search neighbours conditions for one point in a given radius + */ + void SearchConditionsOverPointInRadius ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + const double Radius, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator); + } + + /** + * @brief Search nearest neighbour condition for one point + */ + void SearchConditionsOverPointNearestPoint ( + const ConditionsContainerType& rStructureConditions, + const array_1d& rPoint, + ConditionSpatialSearchResultContainerType& rResults, + const DataCommunicator& rDataCommunicator + ) override + { + mpSpatialSearch->SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, rResults, rDataCommunicator); + } + + ///@} + ///@name Input and output + ///@{ + + /// Turn back information as a string. + std::string Info() const override + { + std::stringstream buffer; + buffer << "SpecializedSpatialSearchMPIFactory" ; + + return buffer.str(); + } + + /// Print information about this object. + void PrintInfo(std::ostream& rOStream) const override + { + rOStream << "SpecializedSpatialSearchMPIFactory"; + } + + /// Print object's data. + void PrintData(std::ostream& rOStream) const override + { + + } + + ///@} +private: + ///@name Member Variables + ///@{ + + SpatialSearch::Pointer mpSpatialSearch = nullptr; /// The spatial search object + + ///@} + ///@name Private Operations + ///@{ + + /** + * @brief This method returns the default parameters + * @return The default parameters + */ + Parameters GetDefaultParameters() const + { + Parameters default_parameters = Parameters(R"( + { "container_type" : "KDTree", + "search_parameters" : { + "bucket_size" : 4, + "allocation_size" : 1000 + } + })" ); + + return default_parameters; + } + + ///@} +}; // Class SpecializedSpatialSearchMPIFactory + +///@} +///@name Type Definitions +///@{ + +///@} +///@name Input and output +///@{ + +/// output stream function +inline std::ostream& operator << (std::ostream& rOStream, + const SpecializedSpatialSearchMPIFactory& rThis) +{ + rThis.PrintInfo(rOStream); + rOStream << std::endl; + rThis.PrintData(rOStream); + + return rOStream; +} + +///@} + +///@}addtogroup block + +} // namespace Kratos. \ No newline at end of file From 2efe20444064c3d65db850e905d606f26e19c653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 20 Jun 2023 09:30:48 +0200 Subject: [PATCH 104/145] Partial revert --- kratos/utilities/search_utilities.cpp | 16 ---------------- kratos/utilities/search_utilities.h | 10 +++++++++- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/kratos/utilities/search_utilities.cpp b/kratos/utilities/search_utilities.cpp index a61eb2a42ef3..583fd008275b 100644 --- a/kratos/utilities/search_utilities.cpp +++ b/kratos/utilities/search_utilities.cpp @@ -41,22 +41,6 @@ bool SearchUtilities::PointIsInsideBoundingBox( /***********************************************************************************/ /***********************************************************************************/ -bool SearchUtilities::PointIsInsideBoundingBox( - const BoundingBoxType& rBoundingBox, - const array_1d& rCoords - ) -{ - // The Bounding Box should have some tolerance already! - if (rCoords[0] < rBoundingBox[0] && rCoords[0] > rBoundingBox[1]) // check x-direction - if (rCoords[1] < rBoundingBox[2] && rCoords[1] > rBoundingBox[3]) // check y-direction - if (rCoords[2] < rBoundingBox[4] && rCoords[2] > rBoundingBox[5]) // check z-direction - return true; - return false; -} - -/***********************************************************************************/ -/***********************************************************************************/ - bool SearchUtilities::PointIsInsideBoundingBoxWithTolerance( const BoundingBox& rBoundingBox, const array_1d& rCoords, diff --git a/kratos/utilities/search_utilities.h b/kratos/utilities/search_utilities.h index a8b3c2869c1f..029393c1aa12 100644 --- a/kratos/utilities/search_utilities.h +++ b/kratos/utilities/search_utilities.h @@ -94,7 +94,15 @@ class SearchUtilities static bool PointIsInsideBoundingBox( const BoundingBoxType& rBoundingBox, const array_1d& rCoords - ); + ) + { + // The Bounding Box should have some tolerance already! + if (rCoords[0] < rBoundingBox[0] && rCoords[0] > rBoundingBox[1]) // check x-direction + if (rCoords[1] < rBoundingBox[2] && rCoords[1] > rBoundingBox[3]) // check y-direction + if (rCoords[2] < rBoundingBox[4] && rCoords[2] > rBoundingBox[5]) // check z-direction + return true; + return false; + } /** * @brief This method checks if a point is inside any bounding box of the global bounding boxes considering a certain tolerance From 91a83a5b3b16efe33e78464f2f5ad8d85231fe74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 20 Jun 2023 09:33:21 +0200 Subject: [PATCH 105/145] Correct comment --- kratos/utilities/search_utilities.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/utilities/search_utilities.h b/kratos/utilities/search_utilities.h index 029393c1aa12..4ef0c1ea740f 100644 --- a/kratos/utilities/search_utilities.h +++ b/kratos/utilities/search_utilities.h @@ -105,7 +105,7 @@ class SearchUtilities } /** - * @brief This method checks if a point is inside any bounding box of the global bounding boxes considering a certain tolerance + * @brief This method checks if a point is inside a bounding box considering a certain tolerance * @param rBoundingBox The bounding box * @param rCoords The coordinates of the point * @param Tolerance The tolerance From 56d804a68eb4606ce18d5599c72c3d30dd9493e8 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 09:43:32 +0200 Subject: [PATCH 106/145] Rename method --- kratos/utilities/search_utilities.cpp | 2 +- kratos/utilities/search_utilities.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/utilities/search_utilities.cpp b/kratos/utilities/search_utilities.cpp index 583fd008275b..5f61a1e88235 100644 --- a/kratos/utilities/search_utilities.cpp +++ b/kratos/utilities/search_utilities.cpp @@ -41,7 +41,7 @@ bool SearchUtilities::PointIsInsideBoundingBox( /***********************************************************************************/ /***********************************************************************************/ -bool SearchUtilities::PointIsInsideBoundingBoxWithTolerance( +bool SearchUtilities::PointIsInsideBoundingBox( const BoundingBox& rBoundingBox, const array_1d& rCoords, const double Tolerance diff --git a/kratos/utilities/search_utilities.h b/kratos/utilities/search_utilities.h index 4ef0c1ea740f..3c382e9d2dc1 100644 --- a/kratos/utilities/search_utilities.h +++ b/kratos/utilities/search_utilities.h @@ -111,7 +111,7 @@ class SearchUtilities * @param Tolerance The tolerance * @return True if the point is inside the bounding box */ - static bool PointIsInsideBoundingBoxWithTolerance( + static bool PointIsInsideBoundingBox( const BoundingBox& rBoundingBox, const array_1d& rCoords, const double Tolerance From 9c0cfd8bb5c94c62cce9631e4b4fa7e6122bfefe Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 13:24:56 +0200 Subject: [PATCH 107/145] Cleaning unused --- .../specialized_spatial_search_mpi.cpp | 48 ------------------- .../specialized_spatial_search_mpi.h | 42 ---------------- 2 files changed, 90 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp index 8b2cccc58d16..8c0b7a1becdc 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -40,22 +40,6 @@ typename SpecializedSpatialSearchMPI::ElementSpatialSearchResult /***********************************************************************************/ /***********************************************************************************/ -template -typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchElementsInRadiusInclusive ( - const ElementsContainerType& rStructureElements, - const ElementsContainerType& rInputElements, - const RadiusArrayType& rRadius, - const DataCommunicator& rDataCommunicator - ) -{ - NodeSpatialSearchResultContainerMapType results; - - return results; -} - -/***********************************************************************************/ -/***********************************************************************************/ - template typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchNodesInRadiusExclusive ( const NodesContainerType& rStructureNodes, @@ -70,22 +54,6 @@ typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultCon /***********************************************************************************/ /***********************************************************************************/ -template -typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchNodesInRadiusInclusive ( - const NodesContainerType& rStructureNodes, - const NodesContainerType& rInputNodes, - const RadiusArrayType& rRadius, - const DataCommunicator& rDataCommunicator - ) -{ - NodeSpatialSearchResultContainerMapType results; - - return results; -} - -/***********************************************************************************/ -/***********************************************************************************/ - template typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchConditionsInRadiusExclusive ( const ConditionsContainerType& rStructureConditions, @@ -106,22 +74,6 @@ typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResu /***********************************************************************************/ /***********************************************************************************/ -template -typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearchMPI::SearchConditionsInRadiusInclusive ( - const ConditionsContainerType& rStructureConditions, - const ConditionsContainerType& rInputConditions, - const RadiusArrayType& rRadius, - const DataCommunicator& rDataCommunicator - ) -{ - NodeSpatialSearchResultContainerMapType results; - - return results; -} - -/***********************************************************************************/ -/***********************************************************************************/ - template void SpecializedSpatialSearchMPI::SearchNodesOverPointInRadius ( const NodesContainerType& rStructureNodes, diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index f2380ceabbe8..900fa9c69dc5 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -135,20 +135,6 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const DataCommunicator& rDataCommunicator ) override; - //************************************************************************ - // Elemental Inclusive search with distance calculation - //************************************************************************ - - /** - * @brief Search neighbours for every element in "InputElements" including itself - */ - NodeSpatialSearchResultContainerMapType SearchElementsInRadiusInclusive ( - const ElementsContainerType& rStructureElements, - const ElementsContainerType& rInputElements, - const RadiusArrayType& rRadius, - const DataCommunicator& rDataCommunicator - ) override; - //************************************************************************ // Nodal Exclusive search with distance calculation //************************************************************************ @@ -163,20 +149,6 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const DataCommunicator& rDataCommunicator ) override; - //************************************************************************ - // Nodal Inclusive search with distance calculation - //************************************************************************ - - /** - * @brief Search neighbours for every node in "InputNodes" including itself - */ - NodeSpatialSearchResultContainerMapType SearchNodesInRadiusInclusive ( - const NodesContainerType& rStructureNodes, - const NodesContainerType& rInputNodes, - const RadiusArrayType& rRadius, - const DataCommunicator& rDataCommunicator - ) override; - //************************************************************************ // Conditional Exclusive search with distance calculation //************************************************************************ @@ -191,20 +163,6 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const DataCommunicator& rDataCommunicator ) override; - //************************************************************************ - // Conditional Inclusive search with distance calculation - //************************************************************************ - - /** - * @brief Search neighbours for every Condition in "InputConditions" including itself - */ - NodeSpatialSearchResultContainerMapType SearchConditionsInRadiusInclusive ( - const ConditionsContainerType& rStructureConditions, - const ConditionsContainerType& rInputConditions, - const RadiusArrayType& rRadius, - const DataCommunicator& rDataCommunicator - ) override; - //************************************************************************ // Point vs Entities (these are new interfaces and already use the new containers) //************************************************************************ From caf4c12619d81bf3a0fe9f6765bff83c42e49451 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 13:29:00 +0200 Subject: [PATCH 108/145] Using parallel utilities --- .../specialized_spatial_search_mpi.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp index 8c0b7a1becdc..84446a3fc9eb 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -15,6 +15,7 @@ // External includes // Project includes +#include "utilities/parallel_utilities.h" #include "mpi/spatial_containers/specialized_spatial_search_mpi.h" namespace Kratos @@ -29,10 +30,11 @@ typename SpecializedSpatialSearchMPI::ElementSpatialSearchResult ) { // The points vector + const auto it_elem_begin = rInputElements.begin(); std::vector points(rInputElements.size()); - for (std::size_t i = 0; i < rInputElements.size(); ++i) { - points[i] = Point((rInputElements.begin() + i)->GetGeometry().Center()); - } + IndexPartition(rInputElements.size()).for_each([&](std::size_t i) { + points[i] = Point((it_elem_begin + i)->GetGeometry().Center()); + }); return SearchElementsOverPointsInRadius(rStructureElements, points.begin(), points.end(), rRadius, rDataCommunicator); } @@ -63,10 +65,11 @@ typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResu ) { // The points vector + const auto it_cond_begin = rInputConditions.begin(); std::vector points(rInputConditions.size()); - for (std::size_t i = 0; i < rInputConditions.size(); ++i) { - points[i] = Point((rInputConditions.begin() + i)->GetGeometry().Center()); - } + IndexPartition(rInputConditions.size()).for_each([&](std::size_t i) { + points[i] = Point((it_cond_begin + i)->GetGeometry().Center()); + }); return SearchConditionsOverPointsInRadius(rStructureConditions, points.begin(), points.end(), rRadius, rDataCommunicator); } From cca07434f216793b25c2c1bb4d904651125a0aff Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 14:59:41 +0200 Subject: [PATCH 109/145] Add synchhronize flag --- kratos/spatial_containers/spatial_search.cpp | 18 +++++--- kratos/spatial_containers/spatial_search.h | 24 ++++++++--- .../specialized_spatial_search.cpp | 43 ++++++++++++++++--- .../specialized_spatial_search.h | 18 +++++--- .../specialized_spatial_search_factory.h | 30 +++++++------ 5 files changed, 97 insertions(+), 36 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index c6ac08e32d23..2f3d93e5d8e0 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -1363,7 +1363,8 @@ void SpatialSearch::SearchNodesOverPointInRadius ( const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { KRATOS_ERROR << "Direct call of an abstract method" << std::endl; @@ -1376,7 +1377,8 @@ void SpatialSearch::SearchNodesOverPointNearestPoint ( const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { KRATOS_ERROR << "Direct call of an abstract method" << std::endl; @@ -1390,7 +1392,8 @@ void SpatialSearch::SearchElementsOverPointInRadius ( const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { KRATOS_ERROR << "Direct call of an abstract method" << std::endl; @@ -1403,7 +1406,8 @@ void SpatialSearch::SearchElementsOverPointNearestPoint ( const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { KRATOS_ERROR << "Direct call of an abstract method" << std::endl; @@ -1417,7 +1421,8 @@ void SpatialSearch::SearchConditionsOverPointInRadius ( const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { KRATOS_ERROR << "Direct call of an abstract method" << std::endl; @@ -1430,7 +1435,8 @@ void SpatialSearch::SearchConditionsOverPointNearestPoint ( const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { KRATOS_ERROR << "Direct call of an abstract method" << std::endl; diff --git a/kratos/spatial_containers/spatial_search.h b/kratos/spatial_containers/spatial_search.h index 4602b6677566..df5cd1ebb396 100644 --- a/kratos/spatial_containers/spatial_search.h +++ b/kratos/spatial_containers/spatial_search.h @@ -1388,13 +1388,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @param Radius Radius of the search * @param rResults Results of the search * @param rDataCommunicator The data communicator + * @param SyncronizeResults If true, the results are synchronized */ virtual void SearchNodesOverPointInRadius ( const NodesContainerType& rStructureNodes, const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ); /** @@ -1431,12 +1433,14 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @param rPoint Point to be searched * @param rResults Results of the search * @param rDataCommunicator The data communicator + * @param SyncronizeResults If true, the results are synchronized */ virtual void SearchNodesOverPointNearestPoint ( const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ); /** @@ -1470,13 +1474,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @param Radius Radius of the search * @param rResults Results of the search * @param rDataCommunicator The data communicator + * @param SyncronizeResults If true, the results are synchronized */ virtual void SearchElementsOverPointInRadius ( const ElementsContainerType& rStructureElements, const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ); /** @@ -1513,12 +1519,14 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @param rPoint Point to be searched * @param rResults Results of the search * @param rDataCommunicator The data communicator + * @param SyncronizeResults If true, the results are synchronized */ virtual void SearchElementsOverPointNearestPoint ( const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ); /** @@ -1552,13 +1560,15 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @param Radius Radius of the search * @param rResults Results of the search * @param rDataCommunicator The data communicator + * @param SyncronizeResults If true, the results are synchronized */ virtual void SearchConditionsOverPointInRadius ( const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ); /** @@ -1595,12 +1605,14 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch * @param rPoint Point to be searched * @param rResults Results of the search * @param rDataCommunicator The data communicator + * @param SyncronizeResults If true, the results are synchronized */ virtual void SearchConditionsOverPointNearestPoint ( const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ); /** diff --git a/kratos/spatial_containers/specialized_spatial_search.cpp b/kratos/spatial_containers/specialized_spatial_search.cpp index c6bfba107f89..42044da75678 100644 --- a/kratos/spatial_containers/specialized_spatial_search.cpp +++ b/kratos/spatial_containers/specialized_spatial_search.cpp @@ -286,7 +286,8 @@ void SpecializedSpatialSearch::SearchNodesOverPointInRadius ( const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { // Defining the point type for the search @@ -336,6 +337,11 @@ void SpecializedSpatialSearch::SearchNodesOverPointInRadius ( } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -346,7 +352,8 @@ void SpecializedSpatialSearch::SearchNodesOverPointNearestPoint const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { // Defining the point type for the search @@ -396,6 +403,11 @@ void SpecializedSpatialSearch::SearchNodesOverPointNearestPoint } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -407,7 +419,8 @@ void SpecializedSpatialSearch::SearchElementsOverPointInRadius ( const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { // Defining the point type for the search @@ -457,6 +470,11 @@ void SpecializedSpatialSearch::SearchElementsOverPointInRadius ( } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -467,7 +485,8 @@ void SpecializedSpatialSearch::SearchElementsOverPointNearestPoi const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { // Defining the point type for the search @@ -517,6 +536,11 @@ void SpecializedSpatialSearch::SearchElementsOverPointNearestPoi } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -528,7 +552,8 @@ void SpecializedSpatialSearch::SearchConditionsOverPointInRadius const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { // Defining the point type for the search @@ -578,6 +603,11 @@ void SpecializedSpatialSearch::SearchConditionsOverPointInRadius } else { static_assert(TSearchBackend == SpatialContainer::KDTree || TSearchBackend == SpatialContainer::Octree || TSearchBackend == SpatialContainer::BinsStatic || TSearchBackend == SpatialContainer::BinsDynamic, "Unknown search backend"); } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -588,7 +618,8 @@ void SpecializedSpatialSearch::SearchConditionsOverPointNearestP const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { // Defining the point type for the search diff --git a/kratos/spatial_containers/specialized_spatial_search.h b/kratos/spatial_containers/specialized_spatial_search.h index b4d6ab25d495..76bf1292741a 100644 --- a/kratos/spatial_containers/specialized_spatial_search.h +++ b/kratos/spatial_containers/specialized_spatial_search.h @@ -316,7 +316,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -326,7 +327,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -337,7 +339,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -347,7 +350,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -358,7 +362,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -368,7 +373,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; ///@} diff --git a/kratos/spatial_containers/specialized_spatial_search_factory.h b/kratos/spatial_containers/specialized_spatial_search_factory.h index 336924cf404c..c92289680802 100644 --- a/kratos/spatial_containers/specialized_spatial_search_factory.h +++ b/kratos/spatial_containers/specialized_spatial_search_factory.h @@ -183,10 +183,11 @@ class SpecializedSpatialSearchFactory const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator); + mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -196,10 +197,11 @@ class SpecializedSpatialSearchFactory const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, rResults, rDataCommunicator); + mpSpatialSearch->SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -210,10 +212,11 @@ class SpecializedSpatialSearchFactory const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator); + mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -223,10 +226,11 @@ class SpecializedSpatialSearchFactory const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchElementsOverPointNearestPoint(rStructureElements, rPoint, rResults, rDataCommunicator); + mpSpatialSearch->SearchElementsOverPointNearestPoint(rStructureElements, rPoint, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -237,10 +241,11 @@ class SpecializedSpatialSearchFactory const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator); + mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -250,10 +255,11 @@ class SpecializedSpatialSearchFactory const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, rResults, rDataCommunicator); + mpSpatialSearch->SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, rResults, rDataCommunicator, SyncronizeResults); } ///@} From c55937a90819717b97a1db6a2a4f4a1caa32199a Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 16:44:09 +0200 Subject: [PATCH 110/145] Many advances --- .../specialized_spatial_search_mpi.cpp | 213 +++++++++++++++++- .../specialized_spatial_search_mpi.h | 80 ++++++- .../specialized_spatial_search_mpi_factory.h | 30 ++- 3 files changed, 291 insertions(+), 32 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp index 84446a3fc9eb..6ac3c7b77121 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -15,8 +15,10 @@ // External includes // Project includes +#include "utilities/search_utilities.h" #include "utilities/parallel_utilities.h" #include "mpi/spatial_containers/specialized_spatial_search_mpi.h" +#include "mpi/includes/mpi_data_communicator.h" namespace Kratos { @@ -83,10 +85,25 @@ void SpecializedSpatialSearchMPI::SearchNodesOverPointInRadius ( const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) -{ - +{ + // Initialize the BB is required + if (!mBoundingBoxesInitialized) { + InitializeLocalBoundingBox(rStructureNodes); + } + + // Check if the point is inside the set + if (SearchUtilities::PointIsInsideBoundingBox(mLocalBoundingBox, rPoint, Radius)) { + // Call local search + BaseType::SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator, false); + } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -97,10 +114,54 @@ void SpecializedSpatialSearchMPI::SearchNodesOverPointNearestPoi const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) -{ +{ + // Initialize the BB is required + if (!mBoundingBoxesInitialized) { + InitializeLocalBoundingBox(rStructureNodes); + } + + // Compute max radius + const array_1d box_size = mLocalBoundingBox.GetMaxPoint() - mLocalBoundingBox.GetMinPoint(); + const double max_radius= *std::max_element(box_size.begin(), box_size.end()); + + // Get the rank + const int current_rank = rDataCommunicator.Rank(); + + // Check if the point is inside the set + NodeSpatialSearchResultContainerType local_result; + if (SearchUtilities::PointIsInsideBoundingBox(mLocalBoundingBox, rPoint, max_radius)) { + // Call local search + BaseType::SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, local_result, rDataCommunicator, false); + } + + /* Now sync results between partitions */ + + // Get the distance + const double local_distance = local_result.NumberOfLocalResults() > 0 ? local_result.GetLocalDistances().begin()->second : std::numeric_limits::max(); + // Find the minimum value and the rank that holds it + struct { + double value; + int rank; + } local_min, global_min; + + local_min.value = local_distance; + local_min.rank = current_rank; + MPI_Allreduce(&local_min, &global_min, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPIDataCommunicator::GetMPICommunicator(rDataCommunicator)); + + // Get the solution from the computed_rank + if (global_min.rank == current_rank) { + // Add the local search + rResults.AddResult(*(local_result.GetLocalPointers().begin().base())); + } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -112,10 +173,25 @@ void SpecializedSpatialSearchMPI::SearchElementsOverPointInRadiu const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) -{ +{ + // Initialize the BB is required + if (!mBoundingBoxesInitialized) { + InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + } + + // Check if the point is inside the set + if (SearchUtilities::PointIsInsideBoundingBox(mLocalBoundingBox, rPoint, Radius)) { + // Call local search + BaseType::SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator, false); + } + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -126,10 +202,54 @@ void SpecializedSpatialSearchMPI::SearchElementsOverPointNearest const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { + // Initialize the BB is required + if (!mBoundingBoxesInitialized) { + InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + } + + // Compute max radius + const array_1d box_size = mLocalBoundingBox.GetMaxPoint() - mLocalBoundingBox.GetMinPoint(); + const double max_radius= *std::max_element(box_size.begin(), box_size.end()); + + // Get the rank + const int current_rank = rDataCommunicator.Rank(); + + // Check if the point is inside the set + ElementSpatialSearchResultContainerType local_result; + if (SearchUtilities::PointIsInsideBoundingBox(mLocalBoundingBox, rPoint, max_radius)) { + // Call local search + BaseType::SearchElementsOverPointNearestPoint(rStructureElements, rPoint, local_result, rDataCommunicator, false); + } + + /* Now sync results between partitions */ + // Get the distance + const double local_distance = local_result.NumberOfLocalResults() > 0 ? local_result.GetLocalDistances().begin()->second : std::numeric_limits::max(); + + // Find the minimum value and the rank that holds it + struct { + double value; + int rank; + } local_min, global_min; + + local_min.value = local_distance; + local_min.rank = current_rank; + MPI_Allreduce(&local_min, &global_min, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPIDataCommunicator::GetMPICommunicator(rDataCommunicator)); + + // Get the solution from the computed_rank + if (global_min.rank == current_rank) { + // Add the local search + rResults.AddResult(*(local_result.GetLocalPointers().begin().base())); + } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -141,10 +261,25 @@ void SpecializedSpatialSearchMPI::SearchConditionsOverPointInRad const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { + // Initialize the BB is required + if (!mBoundingBoxesInitialized) { + InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + } + + // Check if the point is inside the set + if (SearchUtilities::PointIsInsideBoundingBox(mLocalBoundingBox, rPoint, Radius)) { + // Call local search + BaseType::SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator, false); + } + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } } /***********************************************************************************/ @@ -155,10 +290,68 @@ void SpecializedSpatialSearchMPI::SearchConditionsOverPointNeare const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults ) { + // Initialize the BB is required + if (!mBoundingBoxesInitialized) { + InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + } + + // Compute max radius + const array_1d box_size = mLocalBoundingBox.GetMaxPoint() - mLocalBoundingBox.GetMinPoint(); + const double max_radius= *std::max_element(box_size.begin(), box_size.end()); + + // Get the rank + const int current_rank = rDataCommunicator.Rank(); + + // Check if the point is inside the set + ConditionSpatialSearchResultContainerType local_result; + if (SearchUtilities::PointIsInsideBoundingBox(mLocalBoundingBox, rPoint, max_radius)) { + // Call local search + BaseType::SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, local_result, rDataCommunicator, false); + } + + /* Now sync results between partitions */ + // Get the distance + const double local_distance = local_result.NumberOfLocalResults() > 0 ? local_result.GetLocalDistances().begin()->second : std::numeric_limits::max(); + + // Find the minimum value and the rank that holds it + struct { + double value; + int rank; + } local_min, global_min; + + local_min.value = local_distance; + local_min.rank = current_rank; + MPI_Allreduce(&local_min, &global_min, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPIDataCommunicator::GetMPICommunicator(rDataCommunicator)); + + // Get the solution from the computed_rank + if (global_min.rank == current_rank) { + // Add the local search + rResults.AddResult(*(local_result.GetLocalPointers().begin().base())); + } + + // Synchronize if needed + if (SyncronizeResults) { + rResults.SynchronizeAll(rDataCommunicator); + } +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::InitializeLocalBoundingBox(const NodesContainerType& rStructureNodes) +{ + const std::size_t number_of_nodes = rStructureNodes.size(); + if (number_of_nodes > 0) { + mLocalBoundingBox.Set(rStructureNodes.begin(), rStructureNodes.end()); + mLocalBoundingBox.Extend(Tolerance); + } + mBoundingBoxesInitialized = true; } /***********************************************************************************/ diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index 900fa9c69dc5..2e058ec9dbd1 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -17,6 +17,8 @@ // External includes // Project includes +#include "geometries/bounding_box.h" +#include "geometries/point.h" #include "mpi/utilities/mpi_search_utilities.h" #include "spatial_containers/specialized_spatial_search.h" @@ -175,7 +177,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -190,6 +193,9 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureNodes); + // Prepare MPI search std::vector all_points_coordinates; const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); @@ -212,7 +218,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -226,6 +233,9 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureNodes); + // Prepare MPI search std::vector all_points_coordinates; MPISearchUtilities::MPISynchronousPointSynchronization(itPointBegin, itPointEnd, all_points_coordinates, rDataCommunicator); @@ -249,7 +259,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -264,6 +275,9 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + // Prepare MPI search std::vector all_points_coordinates; const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); @@ -286,7 +300,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -300,6 +315,9 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + // Prepare MPI search std::vector all_points_coordinates; MPISearchUtilities::MPISynchronousPointSynchronization(itPointBegin, itPointEnd, all_points_coordinates, rDataCommunicator); @@ -323,7 +341,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -337,7 +356,10 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const RadiusArrayType& rRadius, const DataCommunicator& rDataCommunicator ) - { + { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + // Prepare MPI search std::vector all_points_coordinates; const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); @@ -360,7 +382,8 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override; /** @@ -373,7 +396,10 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI TPointIteratorType itPointEnd, const DataCommunicator& rDataCommunicator ) - { + { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + // Prepare MPI search std::vector all_points_coordinates; MPISearchUtilities::MPISynchronousPointSynchronization(itPointBegin, itPointEnd, all_points_coordinates, rDataCommunicator); @@ -443,17 +469,51 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ///@} private: - ///@name Static Member Variables + ///@name Private Static Member Variables ///@{ + static constexpr double Tolerance = 1e-12; /// The tolerance considered + ///@} - ///@name Member Variables + ///@name Private Member Variables ///@{ + bool mBoundingBoxesInitialized = false; /// Flag to check if the bounding boxes have been initialized + BoundingBox mLocalBoundingBox; /// The local bounding box of the domain + ///@} ///@name Private Operations ///@{ + /** + * @brief This method allows to initialize the local bounding box (for nodes) + * @param rStructureNodes The container of nodes + */ + void InitializeLocalBoundingBox(const NodesContainerType& rStructureNodes); + + /** + * @brief This method allows to initialize the local bounding box (for geometrical objects) + * @param GeometricalObjectsBegin The begin iterator of the geometries to be stored + * @param GeometricalObjectsEnd The end iterator of the geometries to be stored + * @tparam TIteratorType The type of the iterator + */ + template + void InitializeLocalBoundingBox( + TIteratorType GeometricalObjectsBegin, + TIteratorType GeometricalObjectsEnd + ) + { + const std::size_t number_of_objects = std::distance(GeometricalObjectsBegin, GeometricalObjectsEnd); + if (number_of_objects > 0) { + mLocalBoundingBox.Set(GeometricalObjectsBegin->GetGeometry().begin(), GeometricalObjectsBegin->GetGeometry().end()); + for (TIteratorType i_object = GeometricalObjectsBegin ; i_object != GeometricalObjectsEnd ; i_object++){ + mLocalBoundingBox.Extend(i_object->GetGeometry().begin() , i_object->GetGeometry().end()); + } + mLocalBoundingBox.Extend(Tolerance); + } + mBoundingBoxesInitialized = true; + } + ///@} ///@name Un accessible methods ///@{ diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h index 903e93fbb767..dd11aa6198df 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h @@ -102,10 +102,11 @@ class SpecializedSpatialSearchMPIFactory const array_1d& rPoint, const double Radius, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator); + mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -115,10 +116,11 @@ class SpecializedSpatialSearchMPIFactory const NodesContainerType& rStructureNodes, const array_1d& rPoint, NodeSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, rResults, rDataCommunicator); + mpSpatialSearch->SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -129,10 +131,11 @@ class SpecializedSpatialSearchMPIFactory const array_1d& rPoint, const double Radius, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator); + mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -142,10 +145,11 @@ class SpecializedSpatialSearchMPIFactory const ElementsContainerType& rStructureElements, const array_1d& rPoint, ElementSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchElementsOverPointNearestPoint(rStructureElements, rPoint, rResults, rDataCommunicator); + mpSpatialSearch->SearchElementsOverPointNearestPoint(rStructureElements, rPoint, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -156,10 +160,11 @@ class SpecializedSpatialSearchMPIFactory const array_1d& rPoint, const double Radius, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator); + mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } /** @@ -169,10 +174,11 @@ class SpecializedSpatialSearchMPIFactory const ConditionsContainerType& rStructureConditions, const array_1d& rPoint, ConditionSpatialSearchResultContainerType& rResults, - const DataCommunicator& rDataCommunicator + const DataCommunicator& rDataCommunicator, + const bool SyncronizeResults = true ) override { - mpSpatialSearch->SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, rResults, rDataCommunicator); + mpSpatialSearch->SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, rResults, rDataCommunicator, SyncronizeResults); } ///@} From a47cff1575d50062bb2cdeb6a94301709029495c Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 16:56:58 +0200 Subject: [PATCH 111/145] More methods --- .../specialized_spatial_search_mpi.h | 1 - .../specialized_spatial_search_mpi_factory.h | 99 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index 2e058ec9dbd1..53874fb20201 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -415,7 +415,6 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI return results; } - ///@} ///@name Input and output ///@{ diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h index dd11aa6198df..05c5b0e1d3fa 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h @@ -109,6 +109,23 @@ class SpecializedSpatialSearchMPIFactory mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } + /** + * @brief Search neighbours nodes for several points in a given radius + */ + template + NodeSpatialSearchResultContainerMapType SearchNodesOverPointsInRadius ( + const NodesContainerType& rStructureNodes, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + NodeSpatialSearchResultContainerMapType results; + mpSpatialSearch->SearchNodesOverPointsInRadius(rStructureNodes, itPointBegin, itPointEnd, rRadius, results, rDataCommunicator); + return results; + } + /** * @brief Search nearest neighbour node for one point */ @@ -123,6 +140,22 @@ class SpecializedSpatialSearchMPIFactory mpSpatialSearch->SearchNodesOverPointNearestPoint(rStructureNodes, rPoint, rResults, rDataCommunicator, SyncronizeResults); } + /** + * @brief Search nearest neighbour node for several points + */ + template + NodeSpatialSearchResultContainerMapType SearchNodesOverPointsNearestPoint ( + const NodesContainerType& rStructureNodes, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + NodeSpatialSearchResultContainerMapType results; + mpSpatialSearch->SearchNodesOverPointsNearestPoint(rStructureNodes, itPointBegin, itPointEnd, results, rDataCommunicator); + return results; + } + /** * @brief Search neighbours elements for one point in a given radius */ @@ -138,6 +171,23 @@ class SpecializedSpatialSearchMPIFactory mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } + /** + * @brief Search neighbours elements for several points in a given radius + */ + template + ElementSpatialSearchResultContainerMapType SearchElementsOverPointsInRadius ( + const ElementsContainerType& rStructureElements, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + ElementSpatialSearchResultContainerMapType results; + mpSpatialSearch->SearchElementsOverPointsInRadius(rStructureElements, itPointBegin, itPointEnd, rRadius, results, rDataCommunicator); + return results; + } + /** * @brief Search nearest neighbour element for one point */ @@ -152,6 +202,22 @@ class SpecializedSpatialSearchMPIFactory mpSpatialSearch->SearchElementsOverPointNearestPoint(rStructureElements, rPoint, rResults, rDataCommunicator, SyncronizeResults); } + /** + * @brief Search nearest neighbour element for several points + */ + template + ElementSpatialSearchResultContainerMapType SearchElementsOverPointsNearestPoint ( + const ElementsContainerType& rStructureElements, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + ElementSpatialSearchResultContainerMapType results; + mpSpatialSearch->SearchElementsOverPointsNearestPoint(rStructureElements, itPointBegin, itPointEnd, results, rDataCommunicator); + return results; + } + /** * @brief Search neighbours conditions for one point in a given radius */ @@ -167,6 +233,23 @@ class SpecializedSpatialSearchMPIFactory mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, rPoint, Radius, rResults, rDataCommunicator, SyncronizeResults); } + /** + * @brief Search neighbours conditions for several points in a given radius + */ + template + ConditionSpatialSearchResultContainerMapType SearchConditionsOverPointsInRadius ( + const ConditionsContainerType& rStructureConditions, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + ConditionSpatialSearchResultContainerMapType results; + mpSpatialSearch->SearchConditionsOverPointsInRadius(rStructureConditions, itPointBegin, itPointEnd, rRadius, results, rDataCommunicator); + return results; + } + /** * @brief Search nearest neighbour condition for one point */ @@ -181,6 +264,22 @@ class SpecializedSpatialSearchMPIFactory mpSpatialSearch->SearchConditionsOverPointNearestPoint(rStructureConditions, rPoint, rResults, rDataCommunicator, SyncronizeResults); } + /** + * @brief Search nearest neighbour condition for several points + */ + template + ConditionSpatialSearchResultContainerMapType SearchConditionsOverPointsNearestPoint ( + const ConditionsContainerType& rStructureConditions, + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + const DataCommunicator& rDataCommunicator + ) + { + ConditionSpatialSearchResultContainerMapType results; + mpSpatialSearch->SearchConditionsOverPointsNearestPoint(rStructureConditions, itPointBegin, itPointEnd, results, rDataCommunicator); + return results; + } + ///@} ///@name Input and output ///@{ From 1c9eb9fff795a357d0344f53c73376f7992a6a02 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 19:11:37 +0200 Subject: [PATCH 112/145] [Core] Reduce code duplication in python register --- .../add_search_strategies_to_python.cpp | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/kratos/python/add_search_strategies_to_python.cpp b/kratos/python/add_search_strategies_to_python.cpp index 79f8188c17ac..6f1f88f714cd 100644 --- a/kratos/python/add_search_strategies_to_python.cpp +++ b/kratos/python/add_search_strategies_to_python.cpp @@ -198,6 +198,24 @@ std::tuple GenerateSpatialSearchSolutionTuple( return results_tuple; } +/** + * @brief Defines a specialized spatial search module in Pybind11. + * @param m The Pybind11 module to define the specialized spatial search in. + * @param rClassName The name of the specialized spatial search class. + */ +template +void DefineSpecializedSpatialSearch(pybind11::module& m, const std::string& rClassName) +{ + using SpatialSearchType = SpecializedSpatialSearch; + using SpatialSearchPointerType = typename SpecializedSpatialSearch::Pointer; + using BaseSpatialSearchType = SpatialSearch; + + pybind11::class_(m, rClassName.c_str()) + .def(pybind11::init<>()) + .def(pybind11::init()) + ; +} + void AddSearchStrategiesToPython(pybind11::module& m) { namespace py = pybind11; @@ -651,30 +669,17 @@ void AddSearchStrategiesToPython(pybind11::module& m) }) ; + // The factory of the search strategies py::class_(m, "SpecializedSpatialSearch") .def(py::init< >()) .def(py::init()) ; - py::class_, SpecializedSpatialSearch::Pointer, SpatialSearch>(m, "SpatialSearchKDTree") - .def(py::init< >()) - .def(py::init()) - ; - - py::class_, SpecializedSpatialSearch::Pointer, SpatialSearch>(m, "SpatialSearchOctree") - .def(py::init< >()) - .def(py::init()) - ; - - py::class_, SpecializedSpatialSearch::Pointer, SpatialSearch>(m, "SpatialSearchBinsStatic") - .def(py::init< >()) - .def(py::init()) - ; - - py::class_, SpecializedSpatialSearch::Pointer, SpatialSearch>(m, "SpatialSearchBinsDynamic") - .def(py::init< >()) - .def(py::init()) - ; + // Register the specializations + DefineSpecializedSpatialSearch(m, "SpatialSearchKDTree"); + DefineSpecializedSpatialSearch(m, "SpatialSearchOctree"); + DefineSpecializedSpatialSearch(m, "SpatialSearchBinsStatic"); + DefineSpecializedSpatialSearch(m, "SpatialSearchBinsDynamic"); using ResultTypeGeometricalObject = SpatialSearchResult; From 9e3f834dbe50c95dd2c0baeaa3e5a09eb5652ebe Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 19:12:00 +0200 Subject: [PATCH 113/145] Reduce code duplication --- .../add_mpi_search_strategies_to_python.cpp | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp index 9421fee1c4ae..9c361842482d 100644 --- a/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp +++ b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp @@ -24,35 +24,34 @@ namespace Kratos::Python { -void AddMPISearchStrategiesToPython(pybind11::module& m) +template +void DefineSpecializedSpatialSearchMPI(pybind11::module& m, const std::string& rClassName) { - namespace py = pybind11; - - py::class_(m, "SpecializedSpatialSearch") - .def(py::init< >()) - .def(py::init()) - ; + using SpatialSearchType = SpecializedSpatialSearchMPI; + using SpatialSearchPointerType = typename SpecializedSpatialSearchMPI::Pointer; + using BaseSpatialSearchType = SpecializedSpatialSearch; - py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchKDTree") - .def(py::init< >()) - .def(py::init()) - ; - - py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchOctree") - .def(py::init< >()) - .def(py::init()) + pybind11::class_(m, rClassName.c_str()) + .def(pybind11::init<>()) + .def(pybind11::init()) ; +} - py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchBinsStatic") - .def(py::init< >()) - .def(py::init()) - ; +void AddMPISearchStrategiesToPython(pybind11::module& m) +{ + namespace py = pybind11; - py::class_, SpecializedSpatialSearchMPI::Pointer, SpecializedSpatialSearch>(m, "SpatialSearchBinsDynamic") + // The factory of the MPI search strategies + py::class_(m, "SpecializedSpatialSearchMPI") .def(py::init< >()) .def(py::init()) ; + // Register the specializations + DefineSpecializedSpatialSearchMPI(m, "SpatialSearchKDTreeMPI"); + DefineSpecializedSpatialSearchMPI(m, "SpatialSearchOctreeMPI"); + DefineSpecializedSpatialSearchMPI(m, "SpatialSearchBinsStaticMPI"); + DefineSpecializedSpatialSearchMPI(m, "SpatialSearchBinsDynamicMPI"); } } // namespace Kratos::Python \ No newline at end of file From 7a67788463bdc467d77799ee13cffb7b2e4529ad Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 19:25:57 +0200 Subject: [PATCH 114/145] [Core] Missing test to be register in `test_KratosCore` --- kratos/tests/test_KratosCore.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kratos/tests/test_KratosCore.py b/kratos/tests/test_KratosCore.py index 520a789c809f..a1e6b3ed50a8 100644 --- a/kratos/tests/test_KratosCore.py +++ b/kratos/tests/test_KratosCore.py @@ -84,6 +84,7 @@ import test_kratos_globals import test_container_expression import test_model_part_operation_utilities +import test_spatial_search if sympy_available: import test_sympy_fe_utilities @@ -188,6 +189,7 @@ def AssembleTestSuites(): smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_container_expression.TestConditionContainerExpression])) smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_container_expression.TestElementContainerExpression])) smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_model_part_operation_utilities.TestModelPartOperationUtilities])) + smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_spatial_search.TestSpatialSearchSphere])) if sympy_available: smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_sympy_fe_utilities.TestSympyFEUtilities])) From e7ead334ad28215cfc536e05547445c0182c3a85 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 19:52:55 +0200 Subject: [PATCH 115/145] Register MPI tests --- kratos/mpi/tests/test_KratosMPICore.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kratos/mpi/tests/test_KratosMPICore.py b/kratos/mpi/tests/test_KratosMPICore.py index 12bef16d1591..d4c408afb84a 100644 --- a/kratos/mpi/tests/test_KratosMPICore.py +++ b/kratos/mpi/tests/test_KratosMPICore.py @@ -32,6 +32,7 @@ import test_variable_redistribution import test_container_expression from test_model_part_operation_utilities import TestModelPartOperationUtilities + import test_spatial_search def AssembleTestSuites(): ''' Populates the test suites to run. @@ -79,6 +80,7 @@ def AssembleTestSuites(): smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([TestModelPartIOMPI])) smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_variable_redistribution.TestVariableRedistributionUtility])) smallSuite.addTest(TestModelPartOperationUtilities("test_Sum")) + smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_spatial_search.TestSpatialSearchSphere])) # Create a test suite with the selected tests plus all small tests nightSuite = suites['mpi_nightly'] From 2d062b595218caa9ac339d592583b55ebbad3da8 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 20:01:59 +0200 Subject: [PATCH 116/145] New class --- kratos/tests/test_spatial_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index 64bc07f38e84..9b4fb854c5a6 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -60,7 +60,7 @@ def setUp(self): def GenerateSearch(self, container_type = "KDTree"): self.settings["container_type"].SetString(container_type) if KM.IsDistributedRun(): - raise Exception("MPI version comming in a future PR") + self.search = KratosMPI.SpecializedSpatialSearchMPI(self.settings) else: self.search = KM.SpecializedSpatialSearch(self.settings) From 3bcb1ee6f2206a1030e383cb2da007246df0fa83 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Tue, 20 Jun 2023 20:08:59 +0200 Subject: [PATCH 117/145] Missing variable --- kratos/tests/test_spatial_search.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index 9b4fb854c5a6..f67988eaf494 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -66,6 +66,9 @@ def GenerateSearch(self, container_type = "KDTree"): # Creating submodel part self.second_model_part = self.current_model.CreateModelPart(container_type) + # Adding PARTITION_INDEX + if KM.IsDistributedRun(): + self.second_model_part.AddNodalSolutionStepVariable(KM.PARTITION_INDEX) # Create node for search self.new_node_id = 100000 From a0786f3467e21a8b22e2d7c1223c7501cd50e309 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 14:28:11 +0200 Subject: [PATCH 118/145] More methods override --- .../specialized_spatial_search_mpi_factory.h | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h index 05c5b0e1d3fa..12a154723f5e 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h @@ -94,6 +94,61 @@ class SpecializedSpatialSearchMPIFactory ///@name Operations ///@{ + //************************************************************************ + // Elemental Exclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every element in "rModelpart" excluding itself + */ + ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override + { + return mpSpatialSearch->SearchElementsInRadiusExclusive(rStructureElements, rInputElements, rRadius, rDataCommunicator); + } + + //************************************************************************ + // Nodal Exclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every node in "rModelpart" excluding itself + */ + NodeSpatialSearchResultContainerMapType SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override + { + return mpSpatialSearch->SearchNodesInRadiusExclusive(rStructureNodes, rInputNodes, rRadius, rDataCommunicator); + } + + //************************************************************************ + // Conditional Exclusive search with distance calculation + //************************************************************************ + + /** + * @brief Search neighbours for every Condition in "InputConditions" excluding itself + */ + ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override + { + return mpSpatialSearch->SearchConditionsInRadiusExclusive(rStructureConditions, rInputConditions, rRadius, rDataCommunicator); + } + + //************************************************************************ + // Point vs Entities (these are new interfaces and already use the new containers) + //************************************************************************ + /** * @brief Search neighbours nodes for one point in a given radius */ From 681067f07d9a2643007ddf5268f054dc51998880 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 16:26:23 +0200 Subject: [PATCH 119/145] Rename method --- .../utilities/test_mpi_search_utilities.cpp | 8 ++++---- kratos/mpi/utilities/mpi_search_utilities.h | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp index 29fbea57a3cb..95ac924f88d6 100644 --- a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp +++ b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp @@ -84,19 +84,19 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPoint double value = static_cast(rank); double value2 = 2 * value; std::vector points({Point(value, value, value), Point(value2, value2, value2)}); - std::vector local_distances = {value, value2}; + std::vector local_radius = {value, value2}; // Call the function std::vector all_points_coordinates; - auto distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(points.begin(), points.end(), all_points_coordinates, local_distances, r_data_comm); + auto radius = MPISearchUtilities::MPISynchronousPointSynchronizationWithRadius(points.begin(), points.end(), all_points_coordinates, local_radius, r_data_comm); // Check the results KRATOS_CHECK_EQUAL(static_cast(all_points_coordinates.size()), 2 * 3 * world_size); for (int i_rank = 0; i_rank < world_size; ++i_rank) { value = static_cast(i_rank); value2 = 2 * value; - KRATOS_CHECK_DOUBLE_EQUAL(distances[i_rank * 2 ], value ); - KRATOS_CHECK_DOUBLE_EQUAL(distances[i_rank * 2 + 1], value2); + KRATOS_CHECK_DOUBLE_EQUAL(radius[i_rank * 2 ], value ); + KRATOS_CHECK_DOUBLE_EQUAL(radius[i_rank * 2 + 1], value2); for (int j = 0; j < 3; ++j) { KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j ], value ); KRATOS_CHECK_DOUBLE_EQUAL(all_points_coordinates[i_rank * 6 + j + 3], value2); diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 5924ca700ff9..cc4927191c0d 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -132,11 +132,13 @@ class MPISearchUtilities * @param itPointBegin Iterator to the beginning of the points range * @param itPointEnd Iterator to the end of the points range * @param rAllPointsCoordinates vector where the computed coordinates will be stored - * @return The resulting whole radius vector + * @param rRadius The radius of the points + * @param rDataCommunicator The data communicator + * @return The resulting whole radius vector * @tparam TPointIteratorType The type of the point iterator */ template - static std::vector MPISynchronousPointSynchronizationWithDistances( + static std::vector MPISynchronousPointSynchronizationWithRadius( TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, std::vector& rAllPointsCoordinates, @@ -156,7 +158,7 @@ class MPISearchUtilities return rRadius; } else { // If not // The resulting distances - std::vector all_points_distances(total_number_of_points); + std::vector all_points_radius(total_number_of_points); // MPI information const int world_size = rDataCommunicator.Size(); @@ -177,9 +179,9 @@ class MPISearchUtilities } // Invoque AllGatherv - rDataCommunicator.AllGatherv(rRadius, all_points_distances, recv_sizes, recv_offsets); + rDataCommunicator.AllGatherv(rRadius, all_points_radius, recv_sizes, recv_offsets); - return all_points_distances; + return all_points_radius; } } From a522e5398d47ce484f8a13367fdc5a2612b360d4 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 16:47:50 +0200 Subject: [PATCH 120/145] New method to more flexibility --- .../utilities/test_mpi_search_utilities.cpp | 2 +- kratos/mpi/utilities/mpi_search_utilities.h | 62 ++++++++++++++----- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp index 95ac924f88d6..3223fc0fe088 100644 --- a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp +++ b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp @@ -71,7 +71,7 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPoint } } -KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPointsAreDifferentWithDistances, KratosMPICoreFastSuite) +KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPointsAreDifferentWithRadius, KratosMPICoreFastSuite) { // The data communicator const DataCommunicator& r_data_comm = Testing::GetDefaultDataCommunicator(); diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index cc4927191c0d..6c875987c891 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -14,6 +14,7 @@ // System includes #include +#include // External includes @@ -128,21 +129,19 @@ class MPISearchUtilities /** * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. - * @details With radius + * @details With recv sizes * @param itPointBegin Iterator to the beginning of the points range * @param itPointEnd Iterator to the end of the points range * @param rAllPointsCoordinates vector where the computed coordinates will be stored - * @param rRadius The radius of the points * @param rDataCommunicator The data communicator * @return The resulting whole radius vector * @tparam TPointIteratorType The type of the point iterator */ template - static std::vector MPISynchronousPointSynchronizationWithRadius( + static std::vector MPISynchronousPointSynchronizationWithRecvSizes( TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, std::vector& rAllPointsCoordinates, - const std::vector& rRadius, const DataCommunicator& rDataCommunicator ) { @@ -153,26 +152,61 @@ class MPISearchUtilities // Synchronize points MPISynchronousPointSynchronization(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, static_cast(all_points_are_the_same), number_of_points, total_number_of_points); - // Synchonize radius - if (all_points_are_the_same) { // If all points are the same - return rRadius; - } else { // If not - // The resulting distances - std::vector all_points_radius(total_number_of_points); - - // MPI information - const int world_size = rDataCommunicator.Size(); + // MPI information + const int world_size = rDataCommunicator.Size(); + // Define recv_sizes + std::vector recv_sizes(world_size, 0); + if (!all_points_are_the_same) { // If not all points are the same // Getting global number of points std::vector points_per_partition(world_size); std::vector send_points_per_partition(1, number_of_points); rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); // Generate vectors with sizes for AllGatherv - std::vector recv_sizes(world_size, 0); for (int i_rank = 0; i_rank < world_size; ++i_rank) { recv_sizes[i_rank] = points_per_partition[i_rank]; } + } + + return recv_sizes; + } + + /** + * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. + * @details With radius + * @param itPointBegin Iterator to the beginning of the points range + * @param itPointEnd Iterator to the end of the points range + * @param rAllPointsCoordinates vector where the computed coordinates will be stored + * @param rRadius The radius of the points + * @param rDataCommunicator The data communicator + * @return The resulting whole radius vector + * @tparam TPointIteratorType The type of the point iterator + */ + template + static std::vector MPISynchronousPointSynchronizationWithRadius( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + std::vector& rAllPointsCoordinates, + const std::vector& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + // First get recv_sizes + std::vector recv_sizes = MPISynchronousPointSynchronizationWithRecvSizes(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator); + const int total_number_of_points = std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0); + + // Synchonize radius + if (total_number_of_points == 0) { // If all points are the same + return rRadius; + } else { // If not + // The resulting radius + std::vector all_points_radius(total_number_of_points); + + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Generate vectors with sizes for AllGatherv std::vector recv_offsets(world_size, 0); for (int i_rank = 1; i_rank < world_size; ++i_rank) { recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; From ab956f829d0ddc0cb0b9cc1e99e501ea97e69268 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 17:00:28 +0200 Subject: [PATCH 121/145] Update method calls --- .../specialized_spatial_search_mpi.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index 53874fb20201..8ff1f9005b29 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -198,7 +198,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI // Prepare MPI search std::vector all_points_coordinates; - const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + const auto all_points_radius = MPISearchUtilities::MPISynchronousPointSynchronizationWithRadius(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); // Perform the corresponding searchs NodeSpatialSearchResultContainerMapType results; @@ -206,7 +206,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI for (int i_node = 0; i_node < total_number_of_points; ++i_node) { const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); auto& r_partial_result = results.InitializeResult(point); - SearchNodesOverPointInRadius(rStructureNodes, point, all_points_distances[i_node], r_partial_result, rDataCommunicator); + SearchNodesOverPointInRadius(rStructureNodes, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); } return results; } @@ -280,7 +280,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI // Prepare MPI search std::vector all_points_coordinates; - const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + const auto all_points_radius = MPISearchUtilities::MPISynchronousPointSynchronizationWithRadius(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); // Perform the corresponding searchs ElementSpatialSearchResultContainerMapType results; @@ -288,7 +288,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI for (int i_node = 0; i_node < total_number_of_points; ++i_node) { const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); auto& r_partial_result = results.InitializeResult(point); - SearchElementsOverPointInRadius(rStructureElements, point, all_points_distances[i_node], r_partial_result, rDataCommunicator); + SearchElementsOverPointInRadius(rStructureElements, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); } return results; } @@ -362,7 +362,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI // Prepare MPI search std::vector all_points_coordinates; - const auto all_points_distances = MPISearchUtilities::MPISynchronousPointSynchronizationWithDistances(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + const auto all_points_radius = MPISearchUtilities::MPISynchronousPointSynchronizationWithRadius(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); // Perform the corresponding searchs ConditionSpatialSearchResultContainerMapType results; @@ -370,7 +370,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI for (int i_node = 0; i_node < total_number_of_points; ++i_node) { const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); auto& r_partial_result = results.InitializeResult(point); - SearchConditionsOverPointInRadius(rStructureConditions, point, all_points_distances[i_node], r_partial_result, rDataCommunicator); + SearchConditionsOverPointInRadius(rStructureConditions, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); } return results; } From b17a751b7048380f4aaeb12639d6e7fbd527ebf1 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 19:00:57 +0200 Subject: [PATCH 122/145] Missing check --- kratos/mpi/utilities/mpi_search_utilities.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 6c875987c891..f601155af01c 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -167,6 +167,8 @@ class MPISearchUtilities for (int i_rank = 0; i_rank < world_size; ++i_rank) { recv_sizes[i_rank] = points_per_partition[i_rank]; } + } else if (world_size == 1) { // In case only one process is used + recv_sizes[0] = number_of_points; } return recv_sizes; From a6c6e1ab22ec12159b599d17a88211b7521635f3 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 19:16:41 +0200 Subject: [PATCH 123/145] Missing implementations --- .../specialized_spatial_search_mpi.cpp | 151 +++++++++++++++++- 1 file changed, 148 insertions(+), 3 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp index 6ac3c7b77121..944482ac487a 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -31,6 +31,9 @@ typename SpecializedSpatialSearchMPI::ElementSpatialSearchResult const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + // The points vector const auto it_elem_begin = rInputElements.begin(); std::vector points(rInputElements.size()); @@ -38,7 +41,52 @@ typename SpecializedSpatialSearchMPI::ElementSpatialSearchResult points[i] = Point((it_elem_begin + i)->GetGeometry().Center()); }); - return SearchElementsOverPointsInRadius(rStructureElements, points.begin(), points.end(), rRadius, rDataCommunicator); + // Prepare MPI search + std::vector all_points_coordinates; + const auto recv_sizes = MPISearchUtilities::MPISynchronousPointSynchronizationWithRecvSizes(points.begin(), points.end(), all_points_coordinates, rDataCommunicator); + + // The total number of points + const int total_number_of_points = std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0); + + // Generate local points ids + std::vector local_points_ids(rInputElements.size()); + IndexPartition(rInputElements.size()).for_each([&](std::size_t i) { + local_points_ids[i] = (it_elem_begin + i)->Id(); + }); + + // Synchonize radius and ids + std::vector all_points_radius; + std::vector all_points_ids; + if (total_number_of_points == 0) { // If all points are the same + all_points_radius = rRadius; + all_points_ids = local_points_ids; + } else { // If not + // The resulting radius and ids + all_points_radius.resize(total_number_of_points); + all_points_ids.resize(total_number_of_points); + + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Generate vectors with sizes for AllGatherv + std::vector recv_offsets(world_size, 0); + for (int i_rank = 1; i_rank < world_size; ++i_rank) { + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + } + + // Invoque AllGatherv + rDataCommunicator.AllGatherv(rRadius, all_points_radius, recv_sizes, recv_offsets); + rDataCommunicator.AllGatherv(local_points_ids, all_points_ids, recv_sizes, recv_offsets); + } + + // Perform the corresponding searchs + ElementSpatialSearchResultContainerMapType results; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(local_points_ids[i_node]); + SearchElementsOverPointInRadius(rStructureElements, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); + } + return results; } /***********************************************************************************/ @@ -52,7 +100,56 @@ typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultCon const DataCommunicator& rDataCommunicator ) { - return SearchNodesOverPointsInRadius(rStructureNodes, rInputNodes.begin(), rInputNodes.end(), rRadius, rDataCommunicator); + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureNodes); + + // Prepare MPI search + std::vector all_points_coordinates; + const auto recv_sizes = MPISearchUtilities::MPISynchronousPointSynchronizationWithRecvSizes(rInputNodes.begin(), rInputNodes.end(), all_points_coordinates, rDataCommunicator); + + // The total number of points + const int total_number_of_points = std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0); + + // Generate local points ids + std::vector local_points_ids(rInputNodes.size()); + const auto it_node_begin = rInputNodes.begin(); + IndexPartition(rInputNodes.size()).for_each([&](std::size_t i) { + local_points_ids[i] = (it_node_begin + i)->Id(); + }); + + // Synchonize radius and ids + std::vector all_points_radius; + std::vector all_points_ids; + if (total_number_of_points == 0) { // If all points are the same + all_points_radius = rRadius; + all_points_ids = local_points_ids; + } else { // If not + // The resulting radius and ids + all_points_radius.resize(total_number_of_points); + all_points_ids.resize(total_number_of_points); + + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Generate vectors with sizes for AllGatherv + std::vector recv_offsets(world_size, 0); + for (int i_rank = 1; i_rank < world_size; ++i_rank) { + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + } + + // Invoque AllGatherv + rDataCommunicator.AllGatherv(rRadius, all_points_radius, recv_sizes, recv_offsets); + rDataCommunicator.AllGatherv(local_points_ids, all_points_ids, recv_sizes, recv_offsets); + } + + // Perform the corresponding searchs + NodeSpatialSearchResultContainerMapType results; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(local_points_ids[i_node]); + SearchNodesOverPointInRadius(rStructureNodes, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); + } + return results; } /***********************************************************************************/ @@ -66,6 +163,9 @@ typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResu const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + // The points vector const auto it_cond_begin = rInputConditions.begin(); std::vector points(rInputConditions.size()); @@ -73,7 +173,52 @@ typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResu points[i] = Point((it_cond_begin + i)->GetGeometry().Center()); }); - return SearchConditionsOverPointsInRadius(rStructureConditions, points.begin(), points.end(), rRadius, rDataCommunicator); + // Prepare MPI search + std::vector all_points_coordinates; + const auto recv_sizes = MPISearchUtilities::MPISynchronousPointSynchronizationWithRecvSizes(points.begin(), points.end(), all_points_coordinates, rDataCommunicator); + + // The total number of points + const int total_number_of_points = std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0); + + // Generate local points ids + std::vector local_points_ids(rInputConditions.size()); + IndexPartition(rInputConditions.size()).for_each([&](std::size_t i) { + local_points_ids[i] = (it_cond_begin + i)->Id(); + }); + + // Synchonize radius and ids + std::vector all_points_radius; + std::vector all_points_ids; + if (total_number_of_points == 0) { // If all points are the same + all_points_radius = rRadius; + all_points_ids = local_points_ids; + } else { // If not + // The resulting radius and ids + all_points_radius.resize(total_number_of_points); + all_points_ids.resize(total_number_of_points); + + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Generate vectors with sizes for AllGatherv + std::vector recv_offsets(world_size, 0); + for (int i_rank = 1; i_rank < world_size; ++i_rank) { + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + } + + // Invoque AllGatherv + rDataCommunicator.AllGatherv(rRadius, all_points_radius, recv_sizes, recv_offsets); + rDataCommunicator.AllGatherv(local_points_ids, all_points_ids, recv_sizes, recv_offsets); + } + + // Perform the corresponding searchs + ConditionSpatialSearchResultContainerMapType results; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(local_points_ids[i_node]); + SearchConditionsOverPointInRadius(rStructureConditions, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); + } + return results; } /***********************************************************************************/ From 9cb3342b5934fd0c53f379bc3d94959becb0d31b Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 19:49:52 +0200 Subject: [PATCH 124/145] Minor refactor test --- kratos/tests/test_spatial_search.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index f67988eaf494..4ee6288e8753 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -78,8 +78,8 @@ def GenerateSearch(self, container_type = "KDTree"): if self.data_comm.Rank() == 0: self.node = self.second_model_part.CreateNewNode(self.new_node_id, self.point.X, self.point.Y, self.point.Z) self.node.SetSolutionStepValue(KM.PARTITION_INDEX, 0) - ParallelFillCommunicator = KratosMPI.ParallelFillCommunicator(self.model_part, self.data_comm) - ParallelFillCommunicator.Execute() + #KratosMPI.ParallelFillCommunicator(self.model_part, self.data_comm).Execute() + KratosMPI.ParallelFillCommunicator(self.second_model_part, self.data_comm).Execute() else: self.node = self.second_model_part.CreateNewNode(self.new_node_id, self.point.X, self.point.Y, self.point.Z) From 33bf6e54a34e86c305f8dea23c74fd33612f552c Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 20:08:48 +0200 Subject: [PATCH 125/145] Refactor test --- kratos/tests/test_spatial_search.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index 4ee6288e8753..73c6b7ac5743 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -83,8 +83,17 @@ def GenerateSearch(self, container_type = "KDTree"): else: self.node = self.second_model_part.CreateNewNode(self.new_node_id, self.point.X, self.point.Y, self.point.Z) + # Radius list + if KM.IsDistributedRun(): + # Only added to first rank to actualy check it works in all ranks + if self.data_comm.Rank() == 0: + radius_list = [0.3] + else: + radius_list = [] + else: + radius_list = [0.3] + # Reference solution - radius_list = [0.3] distance_ref = [0.077385615, 0.0008331217999999999, 0.0899807529, 0.0627019979, 0.07703137859999999, 0.0789991779, 0.0708403121] node_id_ref = [7, 17, 18, 23, 33, 39, 44, 56] From 7f33f31d11b76df0d261bf14afed5b0b95d9f42d Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 20:23:00 +0200 Subject: [PATCH 126/145] Typo (well, a bug) --- .../spatial_containers/specialized_spatial_search_mpi.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp index 944482ac487a..b90e9ccaa48b 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -83,7 +83,7 @@ typename SpecializedSpatialSearchMPI::ElementSpatialSearchResult ElementSpatialSearchResultContainerMapType results; for (int i_node = 0; i_node < total_number_of_points; ++i_node) { const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); - auto& r_partial_result = results.InitializeResult(local_points_ids[i_node]); + auto& r_partial_result = results.InitializeResult(all_points_ids[i_node]); SearchElementsOverPointInRadius(rStructureElements, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); } return results; @@ -146,7 +146,7 @@ typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultCon NodeSpatialSearchResultContainerMapType results; for (int i_node = 0; i_node < total_number_of_points; ++i_node) { const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); - auto& r_partial_result = results.InitializeResult(local_points_ids[i_node]); + auto& r_partial_result = results.InitializeResult(all_points_ids[i_node]); SearchNodesOverPointInRadius(rStructureNodes, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); } return results; @@ -215,7 +215,7 @@ typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResu ConditionSpatialSearchResultContainerMapType results; for (int i_node = 0; i_node < total_number_of_points; ++i_node) { const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); - auto& r_partial_result = results.InitializeResult(local_points_ids[i_node]); + auto& r_partial_result = results.InitializeResult(all_points_ids[i_node]); SearchConditionsOverPointInRadius(rStructureConditions, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); } return results; From 4e255f326f8d639ae1f9f0e0b9411e69d278b8a6 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 21 Jun 2023 20:27:57 +0200 Subject: [PATCH 127/145] Minor --- kratos/tests/test_spatial_search.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index 73c6b7ac5743..aa90752f1564 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -111,7 +111,7 @@ def GenerateSearch(self, container_type = "KDTree"): # Parallel interface (also works in serial mode) results = self.search.SearchNodesInRadiusExclusive(self.model_part, self.second_model_part.Nodes, radius_list, self.data_comm) - self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) + self.assertEqual(results.NumberOfSearchResults(), 1) self.assertTrue(results.HasResult(self.new_node_id)) node_results = results[self.new_node_id] if not KM.IsDistributedRun(): @@ -126,7 +126,7 @@ def GenerateSearch(self, container_type = "KDTree"): # New interface (works in serial and parallel mode). Interface is very similar to previous one, but it is because a node is type of point and this interface is purely for points (nodes in python) results = self.search.SearchNodesOverPointsInRadius(self.model_part.Nodes, self.second_model_part.Nodes, radius_list, self.data_comm) - self.assertEqual(results.NumberOfSearchResults(), len(radius_list)) + self.assertEqual(results.NumberOfSearchResults(), 1) self.assertFalse(results.HasResult(self.new_node_id)) # This is false because the hash is with coordinates self.assertTrue(results.HasResult(self.point)) node_results = results[self.point] From a67eeee2ebef0f7fd5fe44762eb4a5117204f032 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 11:22:22 +0200 Subject: [PATCH 128/145] [Core] Adding initialization BB methods that may be required in derived classes --- kratos/spatial_containers/spatial_search.cpp | 24 ++++++++++++++++++++ kratos/spatial_containers/spatial_search.h | 22 ++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/kratos/spatial_containers/spatial_search.cpp b/kratos/spatial_containers/spatial_search.cpp index 2f3d93e5d8e0..8ab50bd9e637 100644 --- a/kratos/spatial_containers/spatial_search.cpp +++ b/kratos/spatial_containers/spatial_search.cpp @@ -1442,5 +1442,29 @@ void SpatialSearch::SearchConditionsOverPointNearestPoint ( KRATOS_ERROR << "Direct call of an abstract method" << std::endl; } +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::InitializeBoundingBox(const NodesContainerType& rStructureNodes) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::InitializeBoundingBox(const ElementsContainerType& rStructureElements) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + +/***********************************************************************************/ +/***********************************************************************************/ + +void SpatialSearch::InitializeBoundingBox(const ConditionsContainerType& rStructureConditions) +{ + KRATOS_ERROR << "Direct call of an abstract method" << std::endl; +} + } diff --git a/kratos/spatial_containers/spatial_search.h b/kratos/spatial_containers/spatial_search.h index df5cd1ebb396..7fe2f8b6d833 100644 --- a/kratos/spatial_containers/spatial_search.h +++ b/kratos/spatial_containers/spatial_search.h @@ -1639,6 +1639,28 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch return results; } + //************************************************************************ + // Bounding box methods + //************************************************************************ + + /** + * @brief This method allows to initialize the local bounding box (for nodes) + * @param rStructureNodes The container of nodes + */ + virtual void InitializeBoundingBox(const NodesContainerType& rStructureNodes); + + /** + * @brief This method allows to initialize the local bounding box (for elements) + * @param rStructureElements The container of elements + */ + virtual void InitializeBoundingBox(const ElementsContainerType& rStructureElements); + + /** + * @brief This method allows to initialize the local bounding box (for conditions) + * @param rStructureConditions The container of conditions + */ + virtual void InitializeBoundingBox(const ConditionsContainerType& rStructureConditions); + ///@} ///@name Access ///@{ From 4d09f31c4d847499c8dfec5288dbdd3a0e660dee Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 12:00:14 +0200 Subject: [PATCH 129/145] Using new methods --- .../specialized_spatial_search_mpi.cpp | 38 ++++++++++++++----- .../specialized_spatial_search_mpi.h | 38 ++++++++++++------- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp index b90e9ccaa48b..3197ea5b69de 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.cpp @@ -32,7 +32,7 @@ typename SpecializedSpatialSearchMPI::ElementSpatialSearchResult ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + InitializeBoundingBox(rStructureElements); // The points vector const auto it_elem_begin = rInputElements.begin(); @@ -101,7 +101,7 @@ typename SpecializedSpatialSearchMPI::NodeSpatialSearchResultCon ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureNodes); + InitializeBoundingBox(rStructureNodes); // Prepare MPI search std::vector all_points_coordinates; @@ -164,7 +164,7 @@ typename SpecializedSpatialSearchMPI::ConditionSpatialSearchResu ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + InitializeBoundingBox(rStructureConditions); // The points vector const auto it_cond_begin = rInputConditions.begin(); @@ -236,7 +236,7 @@ void SpecializedSpatialSearchMPI::SearchNodesOverPointInRadius ( { // Initialize the BB is required if (!mBoundingBoxesInitialized) { - InitializeLocalBoundingBox(rStructureNodes); + InitializeBoundingBox(rStructureNodes); } // Check if the point is inside the set @@ -265,7 +265,7 @@ void SpecializedSpatialSearchMPI::SearchNodesOverPointNearestPoi { // Initialize the BB is required if (!mBoundingBoxesInitialized) { - InitializeLocalBoundingBox(rStructureNodes); + InitializeBoundingBox(rStructureNodes); } // Compute max radius @@ -324,7 +324,7 @@ void SpecializedSpatialSearchMPI::SearchElementsOverPointInRadiu { // Initialize the BB is required if (!mBoundingBoxesInitialized) { - InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + InitializeBoundingBox(rStructureElements); } // Check if the point is inside the set @@ -353,7 +353,7 @@ void SpecializedSpatialSearchMPI::SearchElementsOverPointNearest { // Initialize the BB is required if (!mBoundingBoxesInitialized) { - InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + InitializeBoundingBox(rStructureElements); } // Compute max radius @@ -412,7 +412,7 @@ void SpecializedSpatialSearchMPI::SearchConditionsOverPointInRad { // Initialize the BB is required if (!mBoundingBoxesInitialized) { - InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + InitializeBoundingBox(rStructureConditions); } // Check if the point is inside the set @@ -441,7 +441,7 @@ void SpecializedSpatialSearchMPI::SearchConditionsOverPointNeare { // Initialize the BB is required if (!mBoundingBoxesInitialized) { - InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + InitializeBoundingBox(rStructureConditions); } // Compute max radius @@ -489,7 +489,7 @@ void SpecializedSpatialSearchMPI::SearchConditionsOverPointNeare /***********************************************************************************/ template -void SpecializedSpatialSearchMPI::InitializeLocalBoundingBox(const NodesContainerType& rStructureNodes) +void SpecializedSpatialSearchMPI::InitializeBoundingBox(const NodesContainerType& rStructureNodes) { const std::size_t number_of_nodes = rStructureNodes.size(); if (number_of_nodes > 0) { @@ -502,6 +502,24 @@ void SpecializedSpatialSearchMPI::InitializeLocalBoundingBox(con /***********************************************************************************/ /***********************************************************************************/ +template +void SpecializedSpatialSearchMPI::InitializeBoundingBox(const ElementsContainerType& rStructureElements) +{ + InitializeBoundingBox(rStructureElements.begin(), rStructureElements.end()); +} + +/***********************************************************************************/ +/***********************************************************************************/ + +template +void SpecializedSpatialSearchMPI::InitializeBoundingBox(const ConditionsContainerType& rStructureConditions) +{ + InitializeBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); +} + +/***********************************************************************************/ +/***********************************************************************************/ + template class SpecializedSpatialSearchMPI; template class SpecializedSpatialSearchMPI; template class SpecializedSpatialSearchMPI; diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index 8ff1f9005b29..6be2ae7da8ce 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -194,7 +194,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureNodes); + InitializeBoundingBox(rStructureNodes); // Prepare MPI search std::vector all_points_coordinates; @@ -234,7 +234,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureNodes); + InitializeBoundingBox(rStructureNodes); // Prepare MPI search std::vector all_points_coordinates; @@ -276,7 +276,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + InitializeBoundingBox(rStructureElements.begin(), rStructureElements.end()); // Prepare MPI search std::vector all_points_coordinates; @@ -316,7 +316,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureElements.begin(), rStructureElements.end()); + InitializeBoundingBox(rStructureElements.begin(), rStructureElements.end()); // Prepare MPI search std::vector all_points_coordinates; @@ -358,7 +358,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + InitializeBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); // Prepare MPI search std::vector all_points_coordinates; @@ -398,7 +398,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeLocalBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + InitializeBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); // Prepare MPI search std::vector all_points_coordinates; @@ -415,6 +415,24 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI return results; } + /** + * @brief This method allows to initialize the local bounding box (for nodes) + * @param rStructureNodes The container of nodes + */ + void InitializeBoundingBox(const NodesContainerType& rStructureNodes) override; + + /** + * @brief This method allows to initialize the local bounding box (for elements) + * @param rStructureElements The container of elements + */ + void InitializeBoundingBox(const ElementsContainerType& rStructureElements) override; + + /** + * @brief This method allows to initialize the local bounding box (for conditions) + * @param rStructureConditions The container of conditions + */ + void InitializeBoundingBox(const ConditionsContainerType& rStructureConditions) override; + ///@} ///@name Input and output ///@{ @@ -484,12 +502,6 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ///@name Private Operations ///@{ - /** - * @brief This method allows to initialize the local bounding box (for nodes) - * @param rStructureNodes The container of nodes - */ - void InitializeLocalBoundingBox(const NodesContainerType& rStructureNodes); - /** * @brief This method allows to initialize the local bounding box (for geometrical objects) * @param GeometricalObjectsBegin The begin iterator of the geometries to be stored @@ -497,7 +509,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI * @tparam TIteratorType The type of the iterator */ template - void InitializeLocalBoundingBox( + void InitializeBoundingBox( TIteratorType GeometricalObjectsBegin, TIteratorType GeometricalObjectsEnd ) From 72534e440f0da7c54e6a3d98a6074ec3a39e2e19 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 12:49:29 +0200 Subject: [PATCH 130/145] Using local meshes --- kratos/tests/test_spatial_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/tests/test_spatial_search.py b/kratos/tests/test_spatial_search.py index aa90752f1564..25f7494b1691 100644 --- a/kratos/tests/test_spatial_search.py +++ b/kratos/tests/test_spatial_search.py @@ -125,7 +125,7 @@ def GenerateSearch(self, container_type = "KDTree"): self.assertTrue(distance in distance_ref) # New interface (works in serial and parallel mode). Interface is very similar to previous one, but it is because a node is type of point and this interface is purely for points (nodes in python) - results = self.search.SearchNodesOverPointsInRadius(self.model_part.Nodes, self.second_model_part.Nodes, radius_list, self.data_comm) + results = self.search.SearchNodesOverPointsInRadius(self.model_part.GetCommunicator().LocalMesh().Nodes, self.second_model_part.Nodes, radius_list, self.data_comm) self.assertEqual(results.NumberOfSearchResults(), 1) self.assertFalse(results.HasResult(self.new_node_id)) # This is false because the hash is with coordinates self.assertTrue(results.HasResult(self.point)) From ee1c79b431642d2a453c0b238cbc4a09501b2670 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 13:01:34 +0200 Subject: [PATCH 131/145] Simplify --- .../spatial_containers/specialized_spatial_search_mpi.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index 6be2ae7da8ce..7dcc2729cdba 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -276,7 +276,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeBoundingBox(rStructureElements.begin(), rStructureElements.end()); + InitializeBoundingBox(rStructureElements); // Prepare MPI search std::vector all_points_coordinates; @@ -316,7 +316,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeBoundingBox(rStructureElements.begin(), rStructureElements.end()); + InitializeBoundingBox(rStructureElements); // Prepare MPI search std::vector all_points_coordinates; @@ -358,7 +358,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + InitializeBoundingBox(rStructureConditions); // Prepare MPI search std::vector all_points_coordinates; @@ -398,7 +398,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI ) { // Initialize local bounding box - InitializeBoundingBox(rStructureConditions.begin(), rStructureConditions.end()); + InitializeBoundingBox(rStructureConditions); // Prepare MPI search std::vector all_points_coordinates; From 0ab8827f1adecd4f923325142d966001b902b064 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 13:07:52 +0200 Subject: [PATCH 132/145] Missing methods to register --- .../add_mpi_search_strategies_to_python.cpp | 68 ++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp index 9c361842482d..8a5e8e5e8ff3 100644 --- a/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp +++ b/kratos/mpi/python/add_mpi_search_strategies_to_python.cpp @@ -24,16 +24,65 @@ namespace Kratos::Python { +/** + * @brief Copies a list of doubles to a radius array + * @param rListOfRadius The list of doubles to be copied + * @param rRadiusArray The radius array to be filled + */ +void CopyRadiusArrayToPython( + const pybind11::list& rListOfRadius, + SpatialSearch::RadiusArrayType& rRadiusArray + ) +{ + // Get the size of the radius array + const std::size_t size_array = rListOfRadius.size(); + + // Create the radius array + rRadiusArray.resize(size_array); + IndexPartition(size_array).for_each([&](std::size_t i) { + rRadiusArray[i] = rListOfRadius[i].cast(); + //rRadiusArray[i] = rListOfRadius[i]; + }); +} + +/** + * @brief Copies a Python list of radius to a C++ radius array. + * @param rListOfRadius list of radius to copy + * @return Radius array with copied radius + */ +SpatialSearch::RadiusArrayType CopyRadiusArrayToPython(const pybind11::list& rListOfRadius) +{ + // Create the radius array + SpatialSearch::RadiusArrayType radius_array(rListOfRadius.size()); + CopyRadiusArrayToPython(rListOfRadius, radius_array); + return radius_array; +} + template void DefineSpecializedSpatialSearchMPI(pybind11::module& m, const std::string& rClassName) { + namespace py = pybind11; + using SpatialSearchType = SpecializedSpatialSearchMPI; using SpatialSearchPointerType = typename SpecializedSpatialSearchMPI::Pointer; using BaseSpatialSearchType = SpecializedSpatialSearch; - pybind11::class_(m, rClassName.c_str()) - .def(pybind11::init<>()) - .def(pybind11::init()) + using NodesContainerType = SpatialSearch::NodesContainerType; + using ElementsContainerType = SpatialSearch::ElementsContainerType; + using ConditionsContainerType = SpatialSearch::ConditionsContainerType; + + py::class_(m, rClassName.c_str()) + .def(py::init<>()) + .def(py::init()) + .def("SearchNodesOverPointsInRadius", [&](SpatialSearchType& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesOverPointsInRadius(rStructureNodes, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsOverPointsInRadius", [&](SpatialSearchType& self, const ElementsContainerType& rStructureElements, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsOverPointsInRadius(rStructureElements, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsOverPointsInRadius", [&](SpatialSearchType& self, const ConditionsContainerType& rStructureConditions, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsOverPointsInRadius(rStructureConditions, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) ; } @@ -41,10 +90,23 @@ void AddMPISearchStrategiesToPython(pybind11::module& m) { namespace py = pybind11; + using NodesContainerType = SpatialSearch::NodesContainerType; + using ElementsContainerType = SpatialSearch::ElementsContainerType; + using ConditionsContainerType = SpatialSearch::ConditionsContainerType; + // The factory of the MPI search strategies py::class_(m, "SpecializedSpatialSearchMPI") .def(py::init< >()) .def(py::init()) + .def("SearchNodesOverPointsInRadius", [&](SpecializedSpatialSearchMPIFactory& self, const NodesContainerType& rStructureNodes, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchNodesOverPointsInRadius(rStructureNodes, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchElementsOverPointsInRadius", [&](SpecializedSpatialSearchMPIFactory& self, const ElementsContainerType& rStructureElements, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchElementsOverPointsInRadius(rStructureElements, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) + .def("SearchConditionsOverPointsInRadius", [&](SpecializedSpatialSearchMPIFactory& self, const ConditionsContainerType& rStructureConditions, const NodesContainerType& rInputNodes, py::list& rListOfRadius, const DataCommunicator& rDataCommunicator) { + return self.SearchConditionsOverPointsInRadius(rStructureConditions, rInputNodes.begin(), rInputNodes.end(), CopyRadiusArrayToPython(rListOfRadius), rDataCommunicator); + }) ; // Register the specializations From 7176478b32f6cbdd9a0d36d70eecacb383303ae3 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 13:13:15 +0200 Subject: [PATCH 133/145] Missing proper definitions of methods --- .../specialized_spatial_search_mpi_factory.h | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h index 12a154723f5e..f6b9598a72c3 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi_factory.h @@ -176,8 +176,21 @@ class SpecializedSpatialSearchMPIFactory const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + mpSpatialSearch->InitializeBoundingBox(rStructureNodes); + + // Prepare MPI search + std::vector all_points_coordinates; + const auto all_points_radius = MPISearchUtilities::MPISynchronousPointSynchronizationWithRadius(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + + // Perform the corresponding searchs NodeSpatialSearchResultContainerMapType results; - mpSpatialSearch->SearchNodesOverPointsInRadius(rStructureNodes, itPointBegin, itPointEnd, rRadius, results, rDataCommunicator); + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + mpSpatialSearch->SearchNodesOverPointInRadius(rStructureNodes, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); + } return results; } @@ -238,8 +251,21 @@ class SpecializedSpatialSearchMPIFactory const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + mpSpatialSearch->InitializeBoundingBox(rStructureElements); + + // Prepare MPI search + std::vector all_points_coordinates; + const auto all_points_radius = MPISearchUtilities::MPISynchronousPointSynchronizationWithRadius(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + + // Perform the corresponding searchs ElementSpatialSearchResultContainerMapType results; - mpSpatialSearch->SearchElementsOverPointsInRadius(rStructureElements, itPointBegin, itPointEnd, rRadius, results, rDataCommunicator); + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + mpSpatialSearch->SearchElementsOverPointInRadius(rStructureElements, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); + } return results; } @@ -300,8 +326,21 @@ class SpecializedSpatialSearchMPIFactory const DataCommunicator& rDataCommunicator ) { + // Initialize local bounding box + mpSpatialSearch->InitializeBoundingBox(rStructureConditions); + + // Prepare MPI search + std::vector all_points_coordinates; + const auto all_points_radius = MPISearchUtilities::MPISynchronousPointSynchronizationWithRadius(itPointBegin, itPointEnd, all_points_coordinates, rRadius, rDataCommunicator); + + // Perform the corresponding searchs ConditionSpatialSearchResultContainerMapType results; - mpSpatialSearch->SearchConditionsOverPointsInRadius(rStructureConditions, itPointBegin, itPointEnd, rRadius, results, rDataCommunicator); + const int total_number_of_points = all_points_coordinates.size()/3; + for (int i_node = 0; i_node < total_number_of_points; ++i_node) { + const Point point(all_points_coordinates[i_node * 3 + 0], all_points_coordinates[i_node * 3 + 1], all_points_coordinates[i_node * 3 + 2]); + auto& r_partial_result = results.InitializeResult(point); + mpSpatialSearch->SearchConditionsOverPointInRadius(rStructureConditions, point, all_points_radius[i_node], r_partial_result, rDataCommunicator); + } return results; } From 4d7e34d05b7f7b76dd9a081c9a286e2412676570 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 9 Jun 2023 10:24:21 +0200 Subject: [PATCH 134/145] Missing cleaning --- kratos/spatial_containers/dem_search.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kratos/spatial_containers/dem_search.h b/kratos/spatial_containers/dem_search.h index 76931e9d1d86..3bd5a3f4042a 100644 --- a/kratos/spatial_containers/dem_search.h +++ b/kratos/spatial_containers/dem_search.h @@ -75,7 +75,7 @@ class RadiusPoint RadiusPoint() {} virtual ~RadiusPoint(){} - void Initialize(SpatialSearch::ElementPointerType baseElem) + void Initialize(ModelPart::ElementPointerType baseElem) { for(std::size_t i = 0; i < Dimension; i++) coord[i] = baseElem->GetGeometry()[0][i]; @@ -85,7 +85,7 @@ class RadiusPoint // mRadius = baseElem->GetGeometry()[0].FastGetSolutionStepValue(RADIUS); } - void Initialize(SpatialSearch::ElementPointerType baseElem, double Radius) + void Initialize(ModelPart::ElementPointerType baseElem, double Radius) { for(std::size_t i = 0; i < Dimension; i++) coord[i] = baseElem->GetGeometry()[0][i]; @@ -104,7 +104,7 @@ class RadiusPoint double & operator[](std::size_t i) {return coord[i];} double const & operator[](std::size_t i) const {return coord[i];} - SpatialSearch::ElementPointerType pNaseElem; + ModelPart::ElementPointerType pNaseElem; void operator=(Point const& Other){ for(std::size_t i = 0; i < Dimension; i++) From a49e4ed50a04e3d2e35089ffa9afcfc599039c63 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Fri, 9 Jun 2023 10:27:09 +0200 Subject: [PATCH 135/145] Avoid ModelPart dependency --- kratos/spatial_containers/dem_search.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kratos/spatial_containers/dem_search.h b/kratos/spatial_containers/dem_search.h index 3bd5a3f4042a..0650185e3d8e 100644 --- a/kratos/spatial_containers/dem_search.h +++ b/kratos/spatial_containers/dem_search.h @@ -75,7 +75,7 @@ class RadiusPoint RadiusPoint() {} virtual ~RadiusPoint(){} - void Initialize(ModelPart::ElementPointerType baseElem) + void Initialize(Element::Pointer baseElem) { for(std::size_t i = 0; i < Dimension; i++) coord[i] = baseElem->GetGeometry()[0][i]; @@ -85,7 +85,7 @@ class RadiusPoint // mRadius = baseElem->GetGeometry()[0].FastGetSolutionStepValue(RADIUS); } - void Initialize(ModelPart::ElementPointerType baseElem, double Radius) + void Initialize(Element::Pointer baseElem, double Radius) { for(std::size_t i = 0; i < Dimension; i++) coord[i] = baseElem->GetGeometry()[0][i]; @@ -104,7 +104,7 @@ class RadiusPoint double & operator[](std::size_t i) {return coord[i];} double const & operator[](std::size_t i) const {return coord[i];} - ModelPart::ElementPointerType pNaseElem; + Element::Pointer pNaseElem; void operator=(Point const& Other){ for(std::size_t i = 0; i < Dimension; i++) From 1eaf8fd61eefedc0c405922a6211ca8e47fb5523 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 21:03:01 +0200 Subject: [PATCH 136/145] Typo --- kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h index 7dcc2729cdba..0561abe74944 100644 --- a/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h +++ b/kratos/mpi/spatial_containers/specialized_spatial_search_mpi.h @@ -128,7 +128,7 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearchMPI //************************************************************************ /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself */ ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( const ElementsContainerType& rStructureElements, From c1eb96e5c9173957ccac8dd76551656f8deca290 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 22 Jun 2023 21:13:59 +0200 Subject: [PATCH 137/145] Making CLang compiler happy with override (a priori not required) --- kratos/spatial_containers/spatial_search.h | 28 ++++++------- .../specialized_spatial_search.cpp | 42 +++++++++++++++++++ .../specialized_spatial_search.h | 30 +++++++++++++ 3 files changed, 86 insertions(+), 14 deletions(-) diff --git a/kratos/spatial_containers/spatial_search.h b/kratos/spatial_containers/spatial_search.h index 4db5aea2b825..bab2b60401f0 100644 --- a/kratos/spatial_containers/spatial_search.h +++ b/kratos/spatial_containers/spatial_search.h @@ -153,7 +153,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -169,7 +169,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -211,7 +211,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rStructureElements List of elements against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -227,7 +227,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rStructureElements List of elements against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -378,7 +378,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -404,7 +404,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rStructureElements List of elements against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -1196,7 +1196,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched * @param rInputConditions List of conditions to be searched * @param rRadius List of search radius for every element @@ -1212,7 +1212,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rStructureElements List of elements against which the neighbours are searched * @param rInputConditions List of conditions to be searched * @param rRadius List of search radius for every element @@ -1246,7 +1246,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched * @param rInputConditions List of conditions to be searched * @param rRadius List of search radius for every element @@ -1262,7 +1262,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rStructureElements List of elements against which the neighbours are searched * @param rInputConditions List of conditions to be searched * @param rRadius List of search radius for every element @@ -1296,7 +1296,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -1312,7 +1312,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rStructureConditions List of conditions against which the neighbours are searched * @param InputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -1346,7 +1346,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rModelPart Input modelpart against which the neighbours are searched * @param rInputElements List of elements to be searched * @param rRadius List of search radius for every element @@ -1362,7 +1362,7 @@ class KRATOS_API(KRATOS_CORE) SpatialSearch ); /** - * @brief Search neighbours for every element in "Inputelements" excluding itself + * @brief Search neighbours for every element in "rInputElements" excluding itself * @param rStructureConditions List of conditions against which the neighbours are searched * @param InputElements List of elements to be searched * @param rRadius List of search radius for every element diff --git a/kratos/spatial_containers/specialized_spatial_search.cpp b/kratos/spatial_containers/specialized_spatial_search.cpp index 42044da75678..8b6e50e61905 100644 --- a/kratos/spatial_containers/specialized_spatial_search.cpp +++ b/kratos/spatial_containers/specialized_spatial_search.cpp @@ -128,6 +128,20 @@ void SpecializedSpatialSearch::SearchElementsInRadiusExclusive( /***********************************************************************************/ /***********************************************************************************/ +template +typename SpecializedSpatialSearch::ElementSpatialSearchResultContainerMapType SpecializedSpatialSearch::SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return BaseType::SearchElementsInRadiusExclusive(rStructureElements, rInputElements, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + template void SpecializedSpatialSearch::SearchNodesInRadiusExclusive( const NodesContainerType& rStructureNodes, @@ -204,6 +218,20 @@ void SpecializedSpatialSearch::SearchNodesInRadiusExclusive( /***********************************************************************************/ /***********************************************************************************/ +template +typename SpecializedSpatialSearch::NodeSpatialSearchResultContainerMapType SpecializedSpatialSearch::SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return BaseType::SearchNodesInRadiusExclusive(rStructureNodes, rInputNodes, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + template void SpecializedSpatialSearch::SearchConditionsInRadiusExclusive( const ConditionsContainerType& rStructureConditions, @@ -280,6 +308,20 @@ void SpecializedSpatialSearch::SearchConditionsInRadiusExclusive /***********************************************************************************/ /***********************************************************************************/ +template +typename SpecializedSpatialSearch::ConditionSpatialSearchResultContainerMapType SpecializedSpatialSearch::SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) +{ + return BaseType::SearchConditionsInRadiusExclusive(rStructureConditions, rInputConditions, rRadius, rDataCommunicator); +} + +/***********************************************************************************/ +/***********************************************************************************/ + template void SpecializedSpatialSearch::SearchNodesOverPointInRadius ( const NodesContainerType& rStructureNodes, diff --git a/kratos/spatial_containers/specialized_spatial_search.h b/kratos/spatial_containers/specialized_spatial_search.h index 76bf1292741a..acc8e5124449 100644 --- a/kratos/spatial_containers/specialized_spatial_search.h +++ b/kratos/spatial_containers/specialized_spatial_search.h @@ -266,6 +266,16 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch VectorResultElementsContainerType& rResults ) override; + /** + * @brief Search neighbours for every element in "rInputElements" excluding itself + */ + ElementSpatialSearchResultContainerMapType SearchElementsInRadiusExclusive ( + const ElementsContainerType& rStructureElements, + const ElementsContainerType& rInputElements, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + /** * @brief Search neighbours for every node in "rInputNodes" excluding itself */ @@ -287,6 +297,16 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch VectorResultNodesContainerType& rResults ) override; + /** + * @brief Search neighbours for every condition in "rInputConditions" excluding itself + */ + ConditionSpatialSearchResultContainerMapType SearchConditionsInRadiusExclusive ( + const ConditionsContainerType& rStructureConditions, + const ConditionsContainerType& rInputConditions, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + /** * @brief Search neighbours for every Condition in "rInputConditions" excluding itself */ @@ -308,6 +328,16 @@ class KRATOS_API(KRATOS_CORE) SpecializedSpatialSearch VectorResultConditionsContainerType& rResults ) override; + /** + * @brief Search neighbours for every condition in "rInputConditions" excluding itself + */ + NodeSpatialSearchResultContainerMapType SearchNodesInRadiusExclusive ( + const NodesContainerType& rStructureNodes, + const NodesContainerType& rInputNodes, + const RadiusArrayType& rRadius, + const DataCommunicator& rDataCommunicator + ) override; + /** * @brief Search neighbours nodes for one point in a given radius */ From a482be47d90c1699bff1d0f796035b5ef4553644 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Sun, 25 Jun 2023 19:43:22 +0200 Subject: [PATCH 138/145] Adding new methods --- .../spatial_search_result_container.cpp | 82 +++++++++++++++++++ .../spatial_search_result_container.h | 12 +++ .../test_spatial_search_result_container.cpp | 30 +++++++ 3 files changed, 124 insertions(+) diff --git a/kratos/spatial_containers/spatial_search_result_container.cpp b/kratos/spatial_containers/spatial_search_result_container.cpp index 5b3fa68dac96..cfbbe2f5c4e8 100644 --- a/kratos/spatial_containers/spatial_search_result_container.cpp +++ b/kratos/spatial_containers/spatial_search_result_container.cpp @@ -228,6 +228,88 @@ std::vector SpatialSearchResultContainer::GetResultInd /***********************************************************************************/ /***********************************************************************************/ +template +std::vector> SpatialSearchResultContainer::GetResultNodeIndices() +{ + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; + + // Define the coordinates vector + const std::size_t number_of_gp = mGlobalPointers.size(); + std::vector> indices(number_of_gp); + + // Call Apply to get the proxy + auto proxy = this->Apply([](GlobalPointer& rGP) -> std::vector { + if constexpr (std::is_same::value) { + auto& r_geometry = rGP->GetGeometry(); + std::vector gp_indices(r_geometry.size()); + for (unsigned int i = 0; i < r_geometry.size(); ++i) { + gp_indices[i] = r_geometry[i].Id(); + } + return gp_indices; + } else if constexpr (std::is_same::value) { + std::vector gp_indices(1, rGP->Id()); + return gp_indices; + } else { + KRATOS_ERROR << "Not implemented yet" << std::endl; + std::vector gp_indices; + return gp_indices; + } + }); + + // Get the coordinates + for(std::size_t i=0; i +std::vector> SpatialSearchResultContainer::GetResultPartitionIndices() +{ + // Check if the communicator has been created + KRATOS_ERROR_IF(mpGlobalPointerCommunicator == nullptr) << "The communicator has not been created." << std::endl; + + // Define the coordinates vector + const std::size_t number_of_gp = mGlobalPointers.size(); + std::vector> indices(number_of_gp); + + // Call Apply to get the proxy + auto proxy = this->Apply([](GlobalPointer& rGP) -> std::vector { + if constexpr (std::is_same::value) { + auto& r_geometry = rGP->GetGeometry(); + std::vector gp_indices(r_geometry.size()); + for (unsigned int i = 0; i < r_geometry.size(); ++i) { + gp_indices[i] = r_geometry[i].FastGetSolutionStepValue(PARTITION_INDEX); + } + return gp_indices; + } else if constexpr (std::is_same::value) { + std::vector gp_indices(1, rGP->FastGetSolutionStepValue(PARTITION_INDEX)); + return gp_indices; + } else { + KRATOS_ERROR << "Not implemented yet" << std::endl; + std::vector gp_indices; + return gp_indices; + } + }); + + // Get the coordinates + for(std::size_t i=0; i std::vector>> SpatialSearchResultContainer::GetResultCoordinates() { diff --git a/kratos/spatial_containers/spatial_search_result_container.h b/kratos/spatial_containers/spatial_search_result_container.h index 175681448312..dee24cde68a9 100644 --- a/kratos/spatial_containers/spatial_search_result_container.h +++ b/kratos/spatial_containers/spatial_search_result_container.h @@ -454,6 +454,18 @@ class KRATOS_API(KRATOS_CORE) SpatialSearchResultContainer */ std::vector GetResultIndices(); + /** + * @brief Considers the global pointer communicator to get the indices of the nodes of the resulting object + * @return A vector containing all the indices + */ + std::vector> GetResultNodeIndices(); + + /** + * @brief Considers the global pointer communicator to get the partition indices of the nodes of the resulting object + * @return A vector containing all the indices + */ + std::vector> GetResultPartitionIndices(); + /** * @brief Considers the global pointer communicator to get the coordinates of the resulting object * @return A vector containing all the coordinates diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index bec80ec4d4f6..7c90f5064ae1 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -159,6 +159,36 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultIndices, KratosCo KRATOS_CHECK_EQUAL(indices[0], object.Id()); } +KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultNodeIndices, KratosCoreFastSuite) +{ + // Create a test object + SpatialSearchResultContainer container; + + // Generate a geometry + auto p_node1 = Kratos::make_intrusive(1, 0.0, 0.0, 0.0); + auto p_node2 = Kratos::make_intrusive(2, 1.0, 0.0, 0.0); + Geometry::Pointer p_geom = Kratos::make_shared>(p_node1, p_node2); + + // Create a test result + GeometricalObject object = GeometricalObject(1, p_geom); + SpatialSearchResult result(&object); + + // Add the result to the container + container.AddResult(result); + + // Synchronize the container between partitions + DataCommunicator data_communicator; + container.SynchronizeAll(data_communicator); + + // Compute indices + auto indices = container.GetResultNodeIndices(); + + // Check indices + KRATOS_CHECK_EQUAL(indices.size(), 1); + KRATOS_CHECK_EQUAL(indices[0][0], 1); + KRATOS_CHECK_EQUAL(indices[0][1], 2); +} + KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, KratosCoreFastSuite) { // Create a test object From 618c271bdd4bc16dba59568876274c6a24e1e4dc Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Wed, 12 Jul 2023 11:51:22 +0200 Subject: [PATCH 139/145] Remove duplicated definition --- kratos/spatial_containers/specialized_spatial_search.h | 9 --------- 1 file changed, 9 deletions(-) diff --git a/kratos/spatial_containers/specialized_spatial_search.h b/kratos/spatial_containers/specialized_spatial_search.h index a9a5f62b6839..7b1dc3d8ea5e 100644 --- a/kratos/spatial_containers/specialized_spatial_search.h +++ b/kratos/spatial_containers/specialized_spatial_search.h @@ -103,15 +103,6 @@ class PointObject { } - /** - * @brief Constructor that takes the coordinates of the point. - * @param Coords The coordinates of the point. - */ - PointObject(const array_1d& Coords) - :BaseType(Coords) - {} - - /** * @brief Constructor with object * @param pObject The pointer to the object From a413c8a72cdc7de4b27059a1e054d92c635a9eb6 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 17 Jul 2023 12:40:33 +0200 Subject: [PATCH 140/145] Minor fix --- kratos/mpi/utilities/mpi_search_utilities.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index f601155af01c..228ee7122449 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -80,7 +80,7 @@ class MPISearchUtilities std::size_t counter = 0; array_1d coordinates; unsigned int i_coord; - for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++) { + for (auto it_point = itPointBegin ; it_point != itPointEnd ; ++it_point) { noalias(coordinates) = it_point->Coordinates(); for (i_coord = 0; i_coord < 3; ++i_coord) { rAllPointsCoordinates[3 * counter + i_coord] = coordinates[i_coord]; From dfbed90a4db906fa76dba143b7b59cb943843aca Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 17 Jul 2023 12:56:30 +0200 Subject: [PATCH 141/145] Refactor block --- kratos/mpi/utilities/mpi_search_utilities.h | 35 ++++++++------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 228ee7122449..2bc65201e344 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -74,19 +74,21 @@ class MPISearchUtilities KRATOS_DEBUG_ERROR_IF(NumberOfPoints < 0) << "The number of points is negative" << std::endl; KRATOS_DEBUG_ERROR_IF(TotalNumberOfPoints < 0) << "The total number of points is negative" << std::endl; + std::size_t counter = 0; + array_1d coordinates; + unsigned int i_coord; + std::vector send_points_coordinates(NumberOfPoints * 3); + for (auto it_point = itPointBegin ; it_point != itPointEnd ; ++it_point) { + noalias(coordinates) = it_point->Coordinates(); + for (i_coord = 0; i_coord < 3; ++i_coord) { + send_points_coordinates[3 * counter + i_coord] = coordinates[i_coord]; + } + ++counter; + } + // If all points are the same if (AllPointsAreTheSame) { - rAllPointsCoordinates.resize(NumberOfPoints * 3); - std::size_t counter = 0; - array_1d coordinates; - unsigned int i_coord; - for (auto it_point = itPointBegin ; it_point != itPointEnd ; ++it_point) { - noalias(coordinates) = it_point->Coordinates(); - for (i_coord = 0; i_coord < 3; ++i_coord) { - rAllPointsCoordinates[3 * counter + i_coord] = coordinates[i_coord]; - } - ++counter; - } + rAllPointsCoordinates = send_points_coordinates; } else { // If not // MPI information const int world_size = rDataCommunicator.Size(); @@ -98,17 +100,6 @@ class MPISearchUtilities // Getting global coordinates rAllPointsCoordinates.resize(TotalNumberOfPoints * 3); - std::vector send_points_coordinates(NumberOfPoints * 3); - std::size_t counter = 0; - array_1d coordinates; - unsigned int i_coord; - for (auto it_point = itPointBegin ; it_point != itPointEnd ; it_point++) { - noalias(coordinates) = it_point->Coordinates(); - for (i_coord = 0; i_coord < 3; ++i_coord) { - send_points_coordinates[3 * counter + i_coord] = coordinates[i_coord]; - } - ++counter; - } // Generate vectors with sizes for AllGatherv std::vector recv_sizes(world_size, 0); From 05cb4461db58760ac043214376c102d538630d23 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 17 Jul 2023 13:02:01 +0200 Subject: [PATCH 142/145] Better name --- kratos/mpi/utilities/mpi_search_utilities.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 2bc65201e344..83ae44cb7090 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -51,7 +51,7 @@ class MPISearchUtilities * @param rDataCommunicator The data communicator * @param AllPointsAreTheSame If all points are the same (-1 to be computed, 0 if not, 1 if yes) * @param NumberOfPoints The number of points in the range - * @param TotalNumberOfPoints The total number of points in all the ranges + * @param SumNumberOfPointsInAllPartitions The total number of points in all the ranges * @return The number of points in the range * @tparam TPointIteratorType The type of the point iterator */ @@ -63,16 +63,16 @@ class MPISearchUtilities const DataCommunicator& rDataCommunicator, int AllPointsAreTheSame = -1, int NumberOfPoints = -1, - int TotalNumberOfPoints = -1 + int SumNumberOfPointsInAllPartitions = -1 ) { // First check that the points are the same in all processes if (AllPointsAreTheSame < 0) { - AllPointsAreTheSame = static_cast(CheckAllPointsAreTheSame(itPointBegin, itPointEnd, NumberOfPoints, TotalNumberOfPoints, rDataCommunicator)); + AllPointsAreTheSame = static_cast(CheckAllPointsAreTheSameAndCalculateNumberOfPoints(itPointBegin, itPointEnd, NumberOfPoints, SumNumberOfPointsInAllPartitions, rDataCommunicator)); } KRATOS_DEBUG_ERROR_IF(NumberOfPoints < 0) << "The number of points is negative" << std::endl; - KRATOS_DEBUG_ERROR_IF(TotalNumberOfPoints < 0) << "The total number of points is negative" << std::endl; + KRATOS_DEBUG_ERROR_IF(SumNumberOfPointsInAllPartitions < 0) << "The total number of points is negative" << std::endl; std::size_t counter = 0; array_1d coordinates; @@ -99,7 +99,7 @@ class MPISearchUtilities rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); // Getting global coordinates - rAllPointsCoordinates.resize(TotalNumberOfPoints * 3); + rAllPointsCoordinates.resize(SumNumberOfPointsInAllPartitions * 3); // Generate vectors with sizes for AllGatherv std::vector recv_sizes(world_size, 0); @@ -138,7 +138,7 @@ class MPISearchUtilities { // First check that the points are the same in all processes int number_of_points, total_number_of_points; - const bool all_points_are_the_same = CheckAllPointsAreTheSame(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); + const bool all_points_are_the_same = CheckAllPointsAreTheSameAndCalculateNumberOfPoints(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); // Synchronize points MPISynchronousPointSynchronization(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, static_cast(all_points_are_the_same), number_of_points, total_number_of_points); @@ -227,7 +227,7 @@ class MPISearchUtilities * @tparam TPointIteratorType The type of the point iterator */ template - static bool CheckAllPointsAreTheSame( + static bool CheckAllPointsAreTheSameAndCalculateNumberOfPoints( TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, int& rNumberOfPoints, From a8e8daa940efe7e8fe9b6aace936ee972bf9ab99 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 17 Jul 2023 18:37:12 +0200 Subject: [PATCH 143/145] Additional refactor --- kratos/mpi/utilities/mpi_search_utilities.h | 98 +++++++++++++++------ 1 file changed, 72 insertions(+), 26 deletions(-) diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 83ae44cb7090..9624ad40e0ab 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -143,15 +143,73 @@ class MPISearchUtilities // Synchronize points MPISynchronousPointSynchronization(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, static_cast(all_points_are_the_same), number_of_points, total_number_of_points); + // Get recv_sizes + const auto recv_sizes = SyncRecvSizes(number_of_points, all_points_are_the_same, rDataCommunicator); + return recv_sizes; + } + + /** + * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. + * @details With radius + * @param itPointBegin Iterator to the beginning of the points range + * @param itPointEnd Iterator to the end of the points range + * @param rAllPointsCoordinates vector where the computed coordinates will be stored + * @param rRadius The radius of the points + * @param rDataCommunicator The data communicator + * @return The resulting whole radius vector + * @tparam TPointIteratorType The type of the point iterator + */ + template + static std::vector MPISynchronousPointSynchronizationWithRadius( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + std::vector& rAllPointsCoordinates, + const std::vector& rRadius, + const DataCommunicator& rDataCommunicator + ) + { + // First check that the points are the same in all processes + int number_of_points, total_number_of_points; + const bool all_points_are_the_same = CheckAllPointsAreTheSameAndCalculateNumberOfPoints(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); + + // Synchronize points + MPISynchronousPointSynchronization(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, static_cast(all_points_are_the_same), number_of_points, total_number_of_points); + + // Get recv_sizes + const auto recv_sizes = SyncRecvSizes(number_of_points, all_points_are_the_same, rDataCommunicator); + + // Get radius + const auto radius = SyncRadius(recv_sizes, rRadius, rDataCommunicator); + return radius; + } + + ///@} +private: + ///@name Private Operations + ///@{ + + /** + * @brief Synchronizes the sizes of data among multiple processes using MPI. + * @param NumberOfPoints The number of local points in the data + * @param AllPointsAreTheSame Flag indicating if all points are the same + * @param rDataCommunicator The data communicator for MPI communication + * @return A vector containing the sizes of data for each process + */ + static std::vector SyncRecvSizes( + const int NumberOfPoints, + const bool AllPointsAreTheSame, + const DataCommunicator& rDataCommunicator + ) + { // MPI information const int world_size = rDataCommunicator.Size(); // Define recv_sizes std::vector recv_sizes(world_size, 0); - if (!all_points_are_the_same) { // If not all points are the same + if (!AllPointsAreTheSame) { // If not all points are the same // Getting global number of points std::vector points_per_partition(world_size); - std::vector send_points_per_partition(1, number_of_points); + std::vector send_points_per_partition(1, NumberOfPoints); rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); // Generate vectors with sizes for AllGatherv @@ -159,35 +217,27 @@ class MPISearchUtilities recv_sizes[i_rank] = points_per_partition[i_rank]; } } else if (world_size == 1) { // In case only one process is used - recv_sizes[0] = number_of_points; + recv_sizes[0] = NumberOfPoints; } return recv_sizes; } /** - * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. - * @details With radius - * @param itPointBegin Iterator to the beginning of the points range - * @param itPointEnd Iterator to the end of the points range - * @param rAllPointsCoordinates vector where the computed coordinates will be stored - * @param rRadius The radius of the points - * @param rDataCommunicator The data communicator - * @return The resulting whole radius vector - * @tparam TPointIteratorType The type of the point iterator + * @brief Synchronizes the radius of all points in a distributed system. + * @param rRecvSizes a vector containing the number of points to be received from each rank + * @param rRadius a vector containing the radius of each point + * @param rDataCommunicator the communication object used for data exchange + * @return A vector containing the synchronized radius of all points */ - template - static std::vector MPISynchronousPointSynchronizationWithRadius( - TPointIteratorType itPointBegin, - TPointIteratorType itPointEnd, - std::vector& rAllPointsCoordinates, + static std::vector SyncRadius( + const std::vector& rRecvSizes, const std::vector& rRadius, const DataCommunicator& rDataCommunicator ) { - // First get recv_sizes - std::vector recv_sizes = MPISynchronousPointSynchronizationWithRecvSizes(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator); - const int total_number_of_points = std::accumulate(recv_sizes.begin(), recv_sizes.end(), 0); + // First we calculate the total number of points to communicate + const int total_number_of_points = std::accumulate(rRecvSizes.begin(), rRecvSizes.end(), 0); // Synchonize radius if (total_number_of_points == 0) { // If all points are the same @@ -202,20 +252,16 @@ class MPISearchUtilities // Generate vectors with sizes for AllGatherv std::vector recv_offsets(world_size, 0); for (int i_rank = 1; i_rank < world_size; ++i_rank) { - recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + rRecvSizes[i_rank - 1]; } // Invoque AllGatherv - rDataCommunicator.AllGatherv(rRadius, all_points_radius, recv_sizes, recv_offsets); + rDataCommunicator.AllGatherv(rRadius, all_points_radius, rRecvSizes, recv_offsets); return all_points_radius; } } - ///@} -private: - ///@name Private Operations - ///@{ /** * @brief This method checks if all nodes are the same across all partitions From 09ecc48ee917475522db58d2961409208ca5e871 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Mon, 17 Jul 2023 19:16:22 +0200 Subject: [PATCH 144/145] Suggested refactored code --- .../utilities/test_mpi_search_utilities.cpp | 4 +- kratos/mpi/utilities/mpi_search_utilities.h | 158 ++++++++++-------- 2 files changed, 93 insertions(+), 69 deletions(-) diff --git a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp index 3223fc0fe088..5ad6de0c6fa6 100644 --- a/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp +++ b/kratos/mpi/tests/cpp_tests/utilities/test_mpi_search_utilities.cpp @@ -34,10 +34,10 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISynchronousPointSynchronizationAllPoint // Call the function std::vector all_points_coordinates; - const int number_of_points = MPISearchUtilities::MPISynchronousPointSynchronization(points.begin(), points.end(), all_points_coordinates, r_data_comm); + MPISearchUtilities::MPISynchronousPointSynchronization(points.begin(), points.end(), all_points_coordinates, r_data_comm); // Check the results - KRATOS_CHECK_EQUAL(number_of_points, 3); + KRATOS_CHECK_EQUAL(all_points_coordinates.size(), 9); KRATOS_CHECK_EQUAL(all_points_coordinates, expected_coordinates); } diff --git a/kratos/mpi/utilities/mpi_search_utilities.h b/kratos/mpi/utilities/mpi_search_utilities.h index 9624ad40e0ab..7b54b28620c3 100644 --- a/kratos/mpi/utilities/mpi_search_utilities.h +++ b/kratos/mpi/utilities/mpi_search_utilities.h @@ -49,77 +49,29 @@ class MPISearchUtilities * @param itPointEnd Iterator to the end of the points range * @param rAllPointsCoordinates vector where the computed coordinates will be stored * @param rDataCommunicator The data communicator - * @param AllPointsAreTheSame If all points are the same (-1 to be computed, 0 if not, 1 if yes) - * @param NumberOfPoints The number of points in the range - * @param SumNumberOfPointsInAllPartitions The total number of points in all the ranges - * @return The number of points in the range * @tparam TPointIteratorType The type of the point iterator */ template - static int MPISynchronousPointSynchronization( + static void MPISynchronousPointSynchronization( TPointIteratorType itPointBegin, TPointIteratorType itPointEnd, std::vector& rAllPointsCoordinates, - const DataCommunicator& rDataCommunicator, - int AllPointsAreTheSame = -1, - int NumberOfPoints = -1, - int SumNumberOfPointsInAllPartitions = -1 + const DataCommunicator& rDataCommunicator ) { // First check that the points are the same in all processes - if (AllPointsAreTheSame < 0) { - AllPointsAreTheSame = static_cast(CheckAllPointsAreTheSameAndCalculateNumberOfPoints(itPointBegin, itPointEnd, NumberOfPoints, SumNumberOfPointsInAllPartitions, rDataCommunicator)); - } - - KRATOS_DEBUG_ERROR_IF(NumberOfPoints < 0) << "The number of points is negative" << std::endl; - KRATOS_DEBUG_ERROR_IF(SumNumberOfPointsInAllPartitions < 0) << "The total number of points is negative" << std::endl; - - std::size_t counter = 0; - array_1d coordinates; - unsigned int i_coord; - std::vector send_points_coordinates(NumberOfPoints * 3); - for (auto it_point = itPointBegin ; it_point != itPointEnd ; ++it_point) { - noalias(coordinates) = it_point->Coordinates(); - for (i_coord = 0; i_coord < 3; ++i_coord) { - send_points_coordinates[3 * counter + i_coord] = coordinates[i_coord]; - } - ++counter; - } - - // If all points are the same - if (AllPointsAreTheSame) { - rAllPointsCoordinates = send_points_coordinates; - } else { // If not - // MPI information - const int world_size = rDataCommunicator.Size(); - - // Getting global number of points - std::vector points_per_partition(world_size); - std::vector send_points_per_partition(1, NumberOfPoints); - rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); - - // Getting global coordinates - rAllPointsCoordinates.resize(SumNumberOfPointsInAllPartitions * 3); - - // Generate vectors with sizes for AllGatherv - std::vector recv_sizes(world_size, 0); - for (int i_rank = 0; i_rank < world_size; ++i_rank) { - recv_sizes[i_rank] = 3 * points_per_partition[i_rank]; - } - std::vector recv_offsets(world_size, 0); - for (int i_rank = 1; i_rank < world_size; ++i_rank) { - recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; - } + int number_of_points, total_number_of_points; + const bool all_points_are_the_same = CheckAllPointsAreTheSameAndCalculateNumberOfPoints(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); - // Invoque AllGatherv - rDataCommunicator.AllGatherv(send_points_coordinates, rAllPointsCoordinates, recv_sizes, recv_offsets); - } + KRATOS_DEBUG_ERROR_IF(number_of_points < 0) << "The number of points is negative" << std::endl; + KRATOS_DEBUG_ERROR_IF(total_number_of_points < 0) << "The total number of points is negative" << std::endl; - return NumberOfPoints; + // We synchronize the points + SynchronizePoints(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, all_points_are_the_same, number_of_points, total_number_of_points); } /** - * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. + * @brief MPISynchronousPointSynchronizationWithRecvSizes prepares synchronously the coordinates of the points for MPI search including the recv sizes * @details With recv sizes * @param itPointBegin Iterator to the beginning of the points range * @param itPointEnd Iterator to the end of the points range @@ -140,16 +92,19 @@ class MPISearchUtilities int number_of_points, total_number_of_points; const bool all_points_are_the_same = CheckAllPointsAreTheSameAndCalculateNumberOfPoints(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); - // Synchronize points - MPISynchronousPointSynchronization(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, static_cast(all_points_are_the_same), number_of_points, total_number_of_points); + KRATOS_DEBUG_ERROR_IF(number_of_points < 0) << "The number of points is negative" << std::endl; + KRATOS_DEBUG_ERROR_IF(total_number_of_points < 0) << "The total number of points is negative" << std::endl; + + // We synchronize the points + SynchronizePoints(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, all_points_are_the_same, number_of_points, total_number_of_points); // Get recv_sizes - const auto recv_sizes = SyncRecvSizes(number_of_points, all_points_are_the_same, rDataCommunicator); + const auto recv_sizes = SynchronizeRecvSizes(number_of_points, all_points_are_the_same, rDataCommunicator); return recv_sizes; } /** - * @brief MPISynchronousPointSynchronization prepares synchronously the coordinates of the points for MPI search. + * @brief MPISynchronousPointSynchronizationWithRadius prepares synchronously the coordinates of the points for MPI search including radius * @details With radius * @param itPointBegin Iterator to the beginning of the points range * @param itPointEnd Iterator to the end of the points range @@ -172,14 +127,17 @@ class MPISearchUtilities int number_of_points, total_number_of_points; const bool all_points_are_the_same = CheckAllPointsAreTheSameAndCalculateNumberOfPoints(itPointBegin, itPointEnd, number_of_points, total_number_of_points, rDataCommunicator); - // Synchronize points - MPISynchronousPointSynchronization(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, static_cast(all_points_are_the_same), number_of_points, total_number_of_points); + KRATOS_DEBUG_ERROR_IF(number_of_points < 0) << "The number of points is negative" << std::endl; + KRATOS_DEBUG_ERROR_IF(total_number_of_points < 0) << "The total number of points is negative" << std::endl; + + // We synchronize the points + SynchronizePoints(itPointBegin, itPointEnd, rAllPointsCoordinates, rDataCommunicator, all_points_are_the_same, number_of_points, total_number_of_points); // Get recv_sizes - const auto recv_sizes = SyncRecvSizes(number_of_points, all_points_are_the_same, rDataCommunicator); + const auto recv_sizes = SynchronizeRecvSizes(number_of_points, all_points_are_the_same, rDataCommunicator); // Get radius - const auto radius = SyncRadius(recv_sizes, rRadius, rDataCommunicator); + const auto radius = SynchronizeRadius(recv_sizes, rRadius, rDataCommunicator); return radius; } @@ -188,6 +146,72 @@ class MPISearchUtilities ///@name Private Operations ///@{ + /** + * @details Synchronizes points between different processes. + * @details Synchonously + * @param itPointBegin Iterator pointing to the beginning of the range of points + * @param itPointEnd Iterator pointing to the end of the range of points + * @param rAllPointsCoordinates Vector to store the synchronized points' coordinates + * @param rDataCommunicator Object for data communication between processes + * @param AllPointsAreTheSame Flag indicating if all points are the same + * @param NumberOfPoints Local number of points to be synchronized + * @param TotalNumberOfPoints Total number of points across all processes + * @tparam TPointIteratorType The type of the point iterator + */ + template + static void SynchronizePoints( + TPointIteratorType itPointBegin, + TPointIteratorType itPointEnd, + std::vector& rAllPointsCoordinates, + const DataCommunicator& rDataCommunicator, + const bool AllPointsAreTheSame, + const int NumberOfPoints, + const int TotalNumberOfPoints + ) + { + // Initialize local points coordinates + std::size_t counter = 0; + array_1d coordinates; + unsigned int i_coord; + std::vector send_points_coordinates(NumberOfPoints * 3); + for (auto it_point = itPointBegin ; it_point != itPointEnd ; ++it_point) { + noalias(coordinates) = it_point->Coordinates(); + for (i_coord = 0; i_coord < 3; ++i_coord) { + send_points_coordinates[3 * counter + i_coord] = coordinates[i_coord]; + } + ++counter; + } + + // If all points are the same + if (AllPointsAreTheSame) { + rAllPointsCoordinates = send_points_coordinates; + } else { // If not + // MPI information + const int world_size = rDataCommunicator.Size(); + + // Getting global number of points + std::vector points_per_partition(world_size); + std::vector send_points_per_partition(1, NumberOfPoints); + rDataCommunicator.AllGather(send_points_per_partition, points_per_partition); + + // Getting global coordinates + rAllPointsCoordinates.resize(TotalNumberOfPoints * 3); + + // Generate vectors with sizes for AllGatherv + std::vector recv_sizes(world_size, 0); + for (int i_rank = 0; i_rank < world_size; ++i_rank) { + recv_sizes[i_rank] = 3 * points_per_partition[i_rank]; + } + std::vector recv_offsets(world_size, 0); + for (int i_rank = 1; i_rank < world_size; ++i_rank) { + recv_offsets[i_rank] = recv_offsets[i_rank - 1] + recv_sizes[i_rank - 1]; + } + + // Invoque AllGatherv + rDataCommunicator.AllGatherv(send_points_coordinates, rAllPointsCoordinates, recv_sizes, recv_offsets); + } + } + /** * @brief Synchronizes the sizes of data among multiple processes using MPI. * @param NumberOfPoints The number of local points in the data @@ -195,7 +219,7 @@ class MPISearchUtilities * @param rDataCommunicator The data communicator for MPI communication * @return A vector containing the sizes of data for each process */ - static std::vector SyncRecvSizes( + static std::vector SynchronizeRecvSizes( const int NumberOfPoints, const bool AllPointsAreTheSame, const DataCommunicator& rDataCommunicator @@ -230,7 +254,7 @@ class MPISearchUtilities * @param rDataCommunicator the communication object used for data exchange * @return A vector containing the synchronized radius of all points */ - static std::vector SyncRadius( + static std::vector SynchronizeRadius( const std::vector& rRecvSizes, const std::vector& rRadius, const DataCommunicator& rDataCommunicator From e197eb0e54a8dcfdfe68f8306d44352da8b92632 Mon Sep 17 00:00:00 2001 From: Vicente Mataix Ferrandiz Date: Thu, 31 Aug 2023 14:20:40 +0200 Subject: [PATCH 145/145] Fix macros tests --- .../test_spatial_search_result_container.cpp | 54 +++++++------- .../test_geometrical_objects_bins.cpp | 74 +++++++++---------- .../test_spatial_search_result_container.cpp | 64 ++++++++-------- 3 files changed, 96 insertions(+), 96 deletions(-) diff --git a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 79a11a4867ab..779bd6622ce9 100644 --- a/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/mpi/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -42,18 +42,18 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerAddResult, // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 1); + KRATOS_EXPECT_EQ(r_local_pointers.size(), container.NumberOfLocalResults()); // Check distances auto& r_distances = container.GetLocalDistances(); - KRATOS_CHECK_EQUAL(r_distances.size(), 1); - KRATOS_CHECK_EQUAL(r_distances[r_data_comm.Rank() + 1], 0.5); + KRATOS_EXPECT_EQ(r_distances.size(), 1); + KRATOS_EXPECT_EQ(r_distances[r_data_comm.Rank() + 1], 0.5); // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); - KRATOS_CHECK_EQUAL(r_global_pointers.size(), 0); // It should be empty as we have not synchronized - KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); // It should be empty as we have not synchronized + KRATOS_EXPECT_EQ(r_global_pointers.size(), 0); // It should be empty as we have not synchronized + KRATOS_EXPECT_EQ(r_global_pointers.size(), container.NumberOfGlobalResults()); // It should be empty as we have not synchronized } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerClear, KratosMPICoreFastSuite) @@ -77,8 +77,8 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerClear, Krat // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 0); + KRATOS_EXPECT_EQ(r_local_pointers.size(), container.NumberOfLocalResults()); } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerSynchronizeAll, KratosMPICoreFastSuite) @@ -101,13 +101,13 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerSynchronize // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 1); + KRATOS_EXPECT_EQ(r_local_pointers.size(), container.NumberOfLocalResults()); // Check global pointers auto& r_global_pointers = container.GetGlobalPointers(); - KRATOS_CHECK_EQUAL(static_cast(r_global_pointers.size()), r_data_comm.Size()); - KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); + KRATOS_EXPECT_EQ(static_cast(r_global_pointers.size()), r_data_comm.Size()); + KRATOS_EXPECT_EQ(r_global_pointers.size(), container.NumberOfGlobalResults()); } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultShapeFunctions, KratosMPICoreFastSuite) @@ -138,10 +138,10 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultSh auto shape_functions = container.GetResultShapeFunctions(point); // Check shape functions - KRATOS_CHECK_EQUAL(static_cast(shape_functions.size()), r_data_comm.Size()); + KRATOS_EXPECT_EQ(static_cast(shape_functions.size()), r_data_comm.Size()); for (int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { - KRATOS_CHECK_NEAR(shape_functions[i_rank][0], 0.5, 1.0e-12); - KRATOS_CHECK_NEAR(shape_functions[i_rank][1], 0.5, 1.0e-12); + KRATOS_EXPECT_NEAR(shape_functions[i_rank][0], 0.5, 1.0e-12); + KRATOS_EXPECT_NEAR(shape_functions[i_rank][1], 0.5, 1.0e-12); } } @@ -167,9 +167,9 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultIn auto indixes = container.GetResultIndices(); // Check shape functions - KRATOS_CHECK_EQUAL(static_cast(indixes.size()), r_data_comm.Size()); + KRATOS_EXPECT_EQ(static_cast(indixes.size()), r_data_comm.Size()); for (int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { - KRATOS_CHECK_EQUAL(static_cast(indixes[i_rank]), i_rank + 1); + KRATOS_EXPECT_EQ(static_cast(indixes[i_rank]), i_rank + 1); } } @@ -200,11 +200,11 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerGetResultCo auto coordinates = container.GetResultCoordinates(); // Check shape functions - KRATOS_CHECK_EQUAL(static_cast(coordinates.size()), r_data_comm.Size()); + KRATOS_EXPECT_EQ(static_cast(coordinates.size()), r_data_comm.Size()); for (int i_rank = 0; i_rank < r_data_comm.Size(); ++i_rank) { - KRATOS_CHECK_EQUAL(coordinates[i_rank].size(), 2); - KRATOS_CHECK_VECTOR_NEAR(coordinates[i_rank][0], p_node1->Coordinates(), 1.0e-12); - KRATOS_CHECK_VECTOR_NEAR(coordinates[i_rank][1], p_node2->Coordinates(), 1.0e-12); + KRATOS_EXPECT_EQ(coordinates[i_rank].size(), 2); + KRATOS_EXPECT_VECTOR_NEAR(coordinates[i_rank][0], p_node1->Coordinates(), 1.0e-12); + KRATOS_EXPECT_VECTOR_NEAR(coordinates[i_rank][1], p_node2->Coordinates(), 1.0e-12); } } @@ -218,9 +218,9 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapInitiali container_map.InitializeResult(point); // Check that the result was added correctly - KRATOS_CHECK(container_map.HasResult(point)); + KRATOS_EXPECT_TRUE(container_map.HasResult(point)); Point fake_point = Point(1.5, 0.0, 0.0); - KRATOS_CHECK_IS_FALSE(container_map.HasResult(fake_point)); + KRATOS_EXPECT_FALSE(container_map.HasResult(fake_point)); } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapClear, KratosMPICoreFastSuite) @@ -233,9 +233,9 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapClear, K container_map.InitializeResult(point); // Check that the result was added correctly - KRATOS_CHECK(container_map.HasResult(point)); + KRATOS_EXPECT_TRUE(container_map.HasResult(point)); container_map.Clear(); - KRATOS_CHECK_IS_FALSE(container_map.HasResult(point)); + KRATOS_EXPECT_FALSE(container_map.HasResult(point)); } KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapOperators, KratosMPICoreFastSuite) @@ -250,8 +250,8 @@ KRATOS_DISTRIBUTED_TEST_CASE_IN_SUITE(MPISpatialSearchResultContainerMapOperator // Check that the result was added correctly auto& r_result = container_map[point]; auto& r_local_pointers = r_result.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), r_result.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 0); + KRATOS_EXPECT_EQ(r_local_pointers.size(), r_result.NumberOfLocalResults()); } } // namespace Kratos::Testing \ No newline at end of file diff --git a/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp b/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp index 3a07638a4cd7..db786a0f1f84 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_geometrical_objects_bins.cpp @@ -223,39 +223,39 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchInRadiusContainer, KratosC // 0.29 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.29, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK_IS_FALSE(results[point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 0); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_FALSE(results[point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[point].NumberOfGlobalResults(), 0); // 0.3 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.3, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 4); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[point].NumberOfGlobalResults(), 4); // 0.4 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.4, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 4); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[point].NumberOfGlobalResults(), 4); // 0.6 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.6, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 8); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[point].NumberOfGlobalResults(), 8); // 0.7 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.7, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 8); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[point].NumberOfGlobalResults(), 8); // 0.9 radius bins.SearchInRadius(r_array_nodes.begin(), r_array_nodes.end(), 0.9, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[point].NumberOfGlobalResults(), 12); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[point].NumberOfGlobalResults(), 12); } /** Checks bins search nearest @@ -314,23 +314,23 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestInRadiusContainer, GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchNearestInRadius(r_array_nodes.begin(), r_array_nodes.end(), cube_z - 1.e-4, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK_IS_FALSE(results[near_point].IsObjectFound()); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_FALSE(results[near_point].IsObjectFound()); bins.SearchNearestInRadius(r_array_nodes.begin(), r_array_nodes.end(), cube_z + 1.e-4, results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[near_point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[near_point].NumberOfGlobalResults(), 1); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[near_point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[near_point].NumberOfGlobalResults(), 1); // Distances auto& r_distances = results[near_point].GetLocalDistances(); - KRATOS_CHECK_NEAR(r_distances.begin()->second, (cube_z - epsilon), tolerance); + KRATOS_EXPECT_NEAR(r_distances.begin()->second, (cube_z - epsilon), tolerance); // Compute indices auto indices = results[near_point].GetResultIndices(); const std::size_t id = indices[0]; - KRATOS_CHECK_EQUAL(id, 3); + KRATOS_EXPECT_EQ(id, 3); } /** Checks bins search nearest @@ -386,18 +386,18 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchNearestContainer, KratosCo GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchNearest(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[near_point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[near_point].NumberOfGlobalResults(), 1); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[near_point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[near_point].NumberOfGlobalResults(), 1); // Distances auto& r_distances = results[near_point].GetLocalDistances(); - KRATOS_CHECK_NEAR(r_distances.begin()->second, (cube_z - epsilon), tolerance); + KRATOS_EXPECT_NEAR(r_distances.begin()->second, (cube_z - epsilon), tolerance); // Compute indices auto indices = results[near_point].GetResultIndices(); const std::size_t id = indices[0]; - KRATOS_CHECK_EQUAL(id, 3); + KRATOS_EXPECT_EQ(id, 3); } /** Checks bins empty search nearest @@ -438,8 +438,8 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsEmptySearchNearestContainer, Kra GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchNearest(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK_IS_FALSE(results[point].IsObjectFound()); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_FALSE(results[point].IsObjectFound()); } /** Checks bins search is inside @@ -489,9 +489,9 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsInsideContainer, KratosC GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchIsInside(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK(results[inside_point].IsObjectFound()); - KRATOS_CHECK_EQUAL(results[inside_point].NumberOfGlobalResults(), 1); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_TRUE(results[inside_point].IsObjectFound()); + KRATOS_EXPECT_EQ(results[inside_point].NumberOfGlobalResults(), 1); } /** Checks bins search is inside = not found @@ -538,8 +538,8 @@ KRATOS_TEST_CASE_IN_SUITE(GeometricalObjectsBinsSearchIsNotInsideContainer, Krat GeometricalObjectsBins::ResultTypeContainerMap results; bins.SearchIsInside(r_array_nodes.begin(), r_array_nodes.end(), results); - KRATOS_CHECK_EQUAL(results.NumberOfSearchResults(), 1); - KRATOS_CHECK_IS_FALSE(results[outside_point].IsObjectFound()); + KRATOS_EXPECT_EQ(results.NumberOfSearchResults(), 1); + KRATOS_EXPECT_FALSE(results[outside_point].IsObjectFound()); } } // namespace Kratos::Testing. diff --git a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp index 7c90f5064ae1..58dcd96bf7d7 100644 --- a/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp +++ b/kratos/tests/cpp_tests/spatial_containers/test_spatial_search_result_container.cpp @@ -39,19 +39,19 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerAddResult, KratosCoreFastS // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 1); + KRATOS_EXPECT_EQ(r_local_pointers.size(), container.NumberOfLocalResults()); // Check distances auto& r_distances = container.GetLocalDistances(); - KRATOS_CHECK_EQUAL(r_distances.size(), 1); - KRATOS_CHECK_EQUAL(r_distances[1], 0.5); + KRATOS_EXPECT_EQ(r_distances.size(), 1); + KRATOS_EXPECT_EQ(r_distances[1], 0.5); // Check global pointers - KRATOS_CHECK_IS_FALSE(container.IsObjectFound()); + KRATOS_EXPECT_FALSE(container.IsObjectFound()); auto& r_global_pointers = container.GetGlobalPointers(); - KRATOS_CHECK_EQUAL(r_global_pointers.size(), 0); // It should be empty as we have not synchronized - KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); // It should be empty as we have not synchronized + KRATOS_EXPECT_EQ(r_global_pointers.size(), 0); // It should be empty as we have not synchronized + KRATOS_EXPECT_EQ(r_global_pointers.size(), container.NumberOfGlobalResults()); // It should be empty as we have not synchronized } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerClear, KratosCoreFastSuite) @@ -72,8 +72,8 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerClear, KratosCoreFastSuite // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 0); + KRATOS_EXPECT_EQ(r_local_pointers.size(), container.NumberOfLocalResults()); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCoreFastSuite) @@ -94,14 +94,14 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerSynchronizeAll, KratosCore // Check that the result was added correctly auto& r_local_pointers = container.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 1); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), container.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 1); + KRATOS_EXPECT_EQ(r_local_pointers.size(), container.NumberOfLocalResults()); // Check global pointers - KRATOS_CHECK(container.IsObjectFound()); + KRATOS_EXPECT_TRUE(container.IsObjectFound()); auto& r_global_pointers = container.GetGlobalPointers(); - KRATOS_CHECK_EQUAL(r_global_pointers.size(), 1); - KRATOS_CHECK_EQUAL(r_global_pointers.size(), container.NumberOfGlobalResults()); + KRATOS_EXPECT_EQ(r_global_pointers.size(), 1); + KRATOS_EXPECT_EQ(r_global_pointers.size(), container.NumberOfGlobalResults()); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultShapeFunctions, KratosCoreFastSuite) @@ -130,9 +130,9 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultShapeFunctions, K auto shape_functions = container.GetResultShapeFunctions(point); // Check shape functions - KRATOS_CHECK_EQUAL(shape_functions.size(), 1); - KRATOS_CHECK_NEAR(shape_functions[0][0], 0.5, 1.0e-12); - KRATOS_CHECK_NEAR(shape_functions[0][1], 0.5, 1.0e-12); + KRATOS_EXPECT_EQ(shape_functions.size(), 1); + KRATOS_EXPECT_NEAR(shape_functions[0][0], 0.5, 1.0e-12); + KRATOS_EXPECT_NEAR(shape_functions[0][1], 0.5, 1.0e-12); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultIndices, KratosCoreFastSuite) @@ -155,8 +155,8 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultIndices, KratosCo auto indices = container.GetResultIndices(); // Check indices - KRATOS_CHECK_EQUAL(indices.size(), 1); - KRATOS_CHECK_EQUAL(indices[0], object.Id()); + KRATOS_EXPECT_EQ(indices.size(), 1); + KRATOS_EXPECT_EQ(indices[0], object.Id()); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultNodeIndices, KratosCoreFastSuite) @@ -184,9 +184,9 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultNodeIndices, Krat auto indices = container.GetResultNodeIndices(); // Check indices - KRATOS_CHECK_EQUAL(indices.size(), 1); - KRATOS_CHECK_EQUAL(indices[0][0], 1); - KRATOS_CHECK_EQUAL(indices[0][1], 2); + KRATOS_EXPECT_EQ(indices.size(), 1); + KRATOS_EXPECT_EQ(indices[0][0], 1); + KRATOS_EXPECT_EQ(indices[0][1], 2); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, KratosCoreFastSuite) @@ -214,10 +214,10 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerGetResultCoordinates, Krat auto coordinates = container.GetResultCoordinates(); // Check result coordinates - KRATOS_CHECK_EQUAL(coordinates.size(), 1); - KRATOS_CHECK_EQUAL(coordinates[0].size(), 2); - KRATOS_CHECK_VECTOR_NEAR(coordinates[0][0], p_node1->Coordinates(), 1.0e-12); - KRATOS_CHECK_VECTOR_NEAR(coordinates[0][1], p_node2->Coordinates(), 1.0e-12); + KRATOS_EXPECT_EQ(coordinates.size(), 1); + KRATOS_EXPECT_EQ(coordinates[0].size(), 2); + KRATOS_EXPECT_VECTOR_NEAR(coordinates[0][0], p_node1->Coordinates(), 1.0e-12); + KRATOS_EXPECT_VECTOR_NEAR(coordinates[0][1], p_node2->Coordinates(), 1.0e-12); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapInitializeResult, KratosCoreFastSuite) @@ -230,9 +230,9 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapInitializeResult, Krato container_map.InitializeResult(point); // Check that the result was added correctly - KRATOS_CHECK(container_map.HasResult(point)); + KRATOS_EXPECT_TRUE(container_map.HasResult(point)); Point fake_point = Point(1.5, 0.0, 0.0); - KRATOS_CHECK_IS_FALSE(container_map.HasResult(fake_point)); + KRATOS_EXPECT_FALSE(container_map.HasResult(fake_point)); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapClear, KratosCoreFastSuite) @@ -245,9 +245,9 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapClear, KratosCoreFastSu container_map.InitializeResult(point); // Check that the result was added correctly - KRATOS_CHECK(container_map.HasResult(point)); + KRATOS_EXPECT_TRUE(container_map.HasResult(point)); container_map.Clear(); - KRATOS_CHECK_IS_FALSE(container_map.HasResult(point)); + KRATOS_EXPECT_FALSE(container_map.HasResult(point)); } KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapOperators, KratosCoreFastSuite) @@ -262,8 +262,8 @@ KRATOS_TEST_CASE_IN_SUITE(SpatialSearchResultContainerMapOperators, KratosCoreFa // Check that the result was added correctly auto& r_result = container_map[point]; auto& r_local_pointers = r_result.GetLocalPointers(); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), 0); - KRATOS_CHECK_EQUAL(r_local_pointers.size(), r_result.NumberOfLocalResults()); + KRATOS_EXPECT_EQ(r_local_pointers.size(), 0); + KRATOS_EXPECT_EQ(r_local_pointers.size(), r_result.NumberOfLocalResults()); } } // namespace Kratos::Testing \ No newline at end of file