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 all commits
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 @@ -67,6 +67,8 @@ void AddCustomUtilitiesToPython(pybind11::module& m)
.def_static("ProjectRomSolutionIncrementToNodes", &RomAuxiliaryUtilities::ProjectRomSolutionIncrementToNodes)
.def_static("GetElementIdsInModelPart", &RomAuxiliaryUtilities::GetElementIdsInModelPart)
.def_static("GetConditionIdsInModelPart", &RomAuxiliaryUtilities::GetConditionIdsInModelPart)
.def_static("InitializeBins", &RomAuxiliaryUtilities::InitializeBins)
.def_static("FindNearestNeighbors", &RomAuxiliaryUtilities::FindNearestNeighbors)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
SizeType GetNumberOfROMModes() const noexcept
{
return mNumberOfRomModes;
}
}

void ProjectToFineBasis(
const TSystemVectorType& rRomUnkowns,
Expand Down Expand Up @@ -280,7 +280,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
r_root_mp.GetValue(ROM_SOLUTION_INCREMENT) = ZeroVector(GetNumberOfROMModes());
}


void BuildAndSolve(
typename TSchemeType::Pointer pScheme,
ModelPart &rModelPart,
Expand All @@ -294,6 +294,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
RomSystemVectorType brom = ZeroVector(GetNumberOfROMModes());

BuildROM(pScheme, rModelPart, Arom, brom);
CalculateReactions(pScheme, rModelPart, A, Dx, b);
SolveROM(rModelPart, Arom, brom, Dx);

KRATOS_CATCH("")
Expand Down Expand Up @@ -414,7 +415,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
///@}
///@name Protected operators
///@{

void BuildRHSNoDirichlet(
ModelPart& rModelPart,
TSystemVectorType& rb)
Expand Down Expand Up @@ -453,7 +454,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
AtomicAdd(r_bi, r_rhs_cond[i]); // Building RHS.
}
});
}
}

KRATOS_CATCH("")

Expand Down Expand Up @@ -536,7 +537,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T

KRATOS_CATCH("")
}

static DofQueue ExtractDofSet(
typename TSchemeType::Pointer pScheme,
ModelPart& rModelPart)
Expand Down Expand Up @@ -626,7 +627,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
RomSystemMatrixType aux = {}; // Auxiliary: romA = phi.t * (LHS * phi) := phi.t * aux
};


/**
* Class to sum-reduce matrices and vectors.
*/
Expand Down Expand Up @@ -681,7 +682,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
}

/**
* Builds the reduced system of equations on rank 0
* Builds the reduced system of equations on rank 0
*/
virtual void BuildROM(
typename TSchemeType::Pointer pScheme,
Expand Down Expand Up @@ -712,7 +713,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
if(!elements.empty())
{
std::tie(rA, rb) =
block_for_each<SystemSumReducer>(elements, assembly_tls_container,
block_for_each<SystemSumReducer>(elements, assembly_tls_container,
[&](Element& r_element, AssemblyTLS& r_thread_prealloc)
{
return CalculateLocalContribution(r_element, r_thread_prealloc, *pScheme, r_current_process_info);
Expand All @@ -726,7 +727,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
RomSystemVectorType bconditions;

std::tie(Aconditions, bconditions) =
block_for_each<SystemSumReducer>(conditions, assembly_tls_container,
block_for_each<SystemSumReducer>(conditions, assembly_tls_container,
[&](Condition& r_condition, AssemblyTLS& r_thread_prealloc)
{
return CalculateLocalContribution(r_condition, r_thread_prealloc, *pScheme, r_current_process_info);
Expand Down Expand Up @@ -754,7 +755,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
KRATOS_TRY

RomSystemVectorType dxrom(GetNumberOfROMModes());

const auto solving_timer = BuiltinTimer();
MathUtils<double>::Solve(rA, dxrom, rb);
// KRATOS_WATCH(dxrom)
Expand Down Expand Up @@ -791,7 +792,7 @@ class ROMBuilderAndSolver : public BuilderAndSolver<TSparseSpace, TDenseSpace, T
SizeType mNumberOfRomModes;

///@}
///@name Private operations
///@name Private operations
///@{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1088,4 +1088,35 @@ void RomAuxiliaryUtilities::GetJPhiElemental(
}
}

// Define the static member
RomAuxiliaryUtilities::NodeBinsType::UniquePointer RomAuxiliaryUtilities::mpBins = nullptr;


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

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

if (!mpBins) {
KRATOS_ERROR << "Bins have not been initialized. Call InitializeBins first." << std::endl;
}

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 @@ -366,9 +372,37 @@ class KRATOS_API(ROM_APPLICATION) RomAuxiliaryUtilities
const Element::DofsVectorType& rDofs,
const Matrix &rJPhi);

/**
* @brief Initializes the bins for the nodes in the master structure.
*
* This function sets up the spatial bins for efficient nearest neighbor search.
*
* @param rMasterStructureNodes Reference to the master structure nodes.
*/
static void InitializeBins(NodesPointerSetType& rMasterStructureNodes);

/**
* @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 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.
*/
static std::unordered_set<int> FindNearestNeighbors(std::vector<Node::Pointer> nodesVector);

///@}

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

static NodeBinsType::UniquePointer mpBins;

///@}

///@name Private Static Operations
///@{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ def AppendHRomWeightsToRomParameters(self):

# Call the GetConditionIdsNotInHRomModelPart function
new_conditions = KratosROM.RomAuxiliaryUtilities.GetConditionIdsNotInHRomModelPart(
root_model_part, # The complete model part
conditions_to_include_model_part, # The model part containing the conditions to be included
hrom_weights)

Expand Down Expand Up @@ -384,6 +383,7 @@ def AppendHRomWeightsToRomParameters(self):

# Call the GetNodalNeighbouringElementIdsNotInHRom function
new_nodal_neighbours = KratosROM.RomAuxiliaryUtilities.GetNodalNeighbouringElementIdsNotInHRom(
root_model_part,
nodal_neighbours_model_part, # The model part containing the nodal neighbouring elements to be included
hrom_weights)

Expand Down
Loading