Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ROM Utilities: Introduce FindNearestNeighbors Utility for ROMs. #12278

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void AddCustomUtilitiesToPython(pybind11::module& m)
.def_static("ProjectRomSolutionIncrementToNodes", &RomAuxiliaryUtilities::ProjectRomSolutionIncrementToNodes)
.def_static("GetElementIdsInModelPart", &RomAuxiliaryUtilities::GetElementIdsInModelPart)
.def_static("GetConditionIdsInModelPart", &RomAuxiliaryUtilities::GetConditionIdsInModelPart)
.def("FindNearestNeighbors", &RomAuxiliaryUtilities::FindNearestNeighbors)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void RomAuxiliaryUtilities::SetHRomComputingModelPartWithNeighbours(

const auto& r_elem_weights = HRomWeights["Elements"];
const auto& r_cond_weights = HRomWeights["Conditions"];

hrom_elems_vect.reserve(rOriginModelPart.NumberOfElements());
hrom_conds_vect.reserve(rOriginModelPart.NumberOfConditions());

Expand Down Expand Up @@ -254,7 +254,7 @@ void RomAuxiliaryUtilities::SetHRomComputingModelPartWithNeighbours(
}
}
}

for (auto it = r_cond_weights.begin(); it != r_cond_weights.end(); ++it) {
// Get the condition from origin mesh
const IndexType cond_id = stoi(it.name());
Expand Down Expand Up @@ -791,4 +791,26 @@ void RomAuxiliaryUtilities::GetJPhiElemental(
}
}

std::unordered_set<int> RomAuxiliaryUtilities::FindNearestNeighbors(
NodesPointerSetType& rMasterStructureNodes,
std::vector<Node::Pointer> nodesVector)
{
KRATOS_TRY;

NodesPointerSetType::ContainerType& nodes_model_part = rMasterStructureNodes.GetContainer();
mpBins = Kratos::make_unique<NodeBinsType>(nodes_model_part.begin(), nodes_model_part.end());

std::unordered_set<int> nearestNodeIds; // Using unordered_set for unique node IDs

for (auto& p_current_node : nodesVector) {
NodeType::Pointer found_node = mpBins->SearchNearestPoint(*p_current_node); // Search for nearest node
nearestNodeIds.insert(found_node->Id()); // Insert the found node's ID
}

return nearestNodeIds;

KRATOS_CATCH("");
}


} // namespace Kratos
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "includes/ublas_interface.h"
#include "modified_shape_functions/modified_shape_functions.h"
#include "processes/find_nodal_neighbours_process.h"
#include "spatial_containers/bins_dynamic.h"

// Application includes
#include "rom_application_variables.h"
Expand Down Expand Up @@ -58,6 +59,11 @@ class KRATOS_API(ROM_APPLICATION) RomAuxiliaryUtilities

using NodesPointerSetType = ModelPart::NodesContainerType;

using ResultNodesContainerType = NodesPointerSetType::ContainerType;

//Bin
using NodeBinsType = BinsDynamic<3, NodeType, NodesPointerSetType::ContainerType>;

using ElementFacesMapType = std::unordered_map<
std::vector<IndexType>,
std::pair<bool, GeometryPointerType>,
Expand Down Expand Up @@ -266,23 +272,45 @@ class KRATOS_API(ROM_APPLICATION) RomAuxiliaryUtilities
const std::unordered_map<Kratos::VariableData::KeyType, Matrix::size_type>& rVarToRowMapping);

/**
* @brief Obtain the JPhi elemental matrix for a particular element.
* @brief Obtain the JPhi elemental matrix for a particular element.
* JPhi represents the projection of the Jacobian onto the ROM_BASIS.
* @param rJPhiElemental The matrix to store the result in. Must have the appropriate size already.
* @param rDofs The set of degrees of freedom (DoFs) of the element.
* @param rJPhi The JPhi matrix, from which rows are extracted according to the equation ID of each DoF.
*
* This function loops over all the DoFs for the given element. For each DoF, it uses its equation ID to extract a
*
* This function loops over all the DoFs for the given element. For each DoF, it uses its equation ID to extract a
* corresponding row from the rJPhi matrix, which is then stored in the corresponding row of rJPhiElemental.
*/
static void GetJPhiElemental(
Matrix &rJPhiElemental,
const Element::DofsVectorType& rDofs,
const Matrix &rJPhi);


/**
* @brief Finds the nearest neighbors for a set of nodes in a given model part.
*
* This function locates the closest node in the master structure to each node in the provided vector.
* It uses a spatial bin to efficiently search for the nearest neighbor. The IDs of the nearest nodes are
* stored in an unordered set to ensure uniqueness.
*
* @param rMasterStructureNodes Reference to the master structure nodes, which contain all the nodes in the model part.
* @param nodesVector A vector of pointers to the nodes for which the nearest neighbors are to be found.
* @return std::unordered_set<int> A set containing unique IDs of the nearest nodes.
*/
std::unordered_set<int> FindNearestNeighbors(
NodesPointerSetType& rMasterStructureNodes,
std::vector<Node::Pointer> nodesVector);

///@}

private:
///@}
///@name Member Variables
///@{

NodeBinsType::UniquePointer mpBins;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mpBins appears to be used only in the new FindNearestNeighbors function and its regenerated every time the function is called.

If this is the case please just remove the variable and declare it inside the function.

If you intend the utility to be reusable (calculate the bins once) then please move the initialization of the member variable to a separate function and then use the pointer.


///@}

///@name Private Static Operations
///@{
Expand Down
Loading