Skip to content

Commit

Permalink
Merge pull request #12087 from KratosMultiphysics/core/pointer_vector…
Browse files Browse the repository at this point in the history
…_set/introduce_insert

[Core] Introducing proper insert method to PointerVectorSet
  • Loading branch information
sunethwarna authored Jun 10, 2024
2 parents 25654a9 + 359f87e commit 384b3cd
Show file tree
Hide file tree
Showing 7 changed files with 737 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/* The mappers includes */
#include "spaces/ublas_space.h"
#include "mappers/mapper_flags.h"
#include "factories/mapper_factory.h"
#include "factories/mapper_factory.h"

// NOTE: The following contains the license of the MMG library
/* =============================================================================
Expand Down Expand Up @@ -366,6 +366,10 @@ void MmgProcess<TMMGLibrary>::InitializeMeshData()
for (auto it_dof = mDofs.begin(); it_dof != mDofs.end(); ++it_dof)
(**it_dof).FreeDof();

mrThisModelPart.Nodes().Unique();
mrThisModelPart.Conditions().Unique();
mrThisModelPart.Elements().Unique();

// Generate the maps of reference
mMmgUtilities.GenerateReferenceMaps(mrThisModelPart, aux_ref_cond, aux_ref_elem, mpRefCondition, mpRefElement);

Expand Down Expand Up @@ -590,7 +594,7 @@ void MmgProcess<TMMGLibrary>::ExecuteRemeshing()
ModelPart& r_old_auxiliar_model_part = r_old_model_part.GetSubModelPart("AUXILIAR_COLLAPSED_PRISMS");
ModelPart& r_auxiliary_model_part = mrThisModelPart.GetSubModelPart("AUXILIAR_COLLAPSED_PRISMS");

// Define mapper factory
// Define mapper factory
DEFINE_MAPPER_FACTORY_SERIAL
if (MapperFactoryType::HasMapper("nearest_element") && mThisParameters["use_mapper_if_available"].GetBool()) {
KRATOS_INFO_IF("MmgProcess", mEchoLevel > 0) << "Using MappingApplication to interpolate values" << std::endl;
Expand Down Expand Up @@ -1504,7 +1508,7 @@ const Parameters MmgProcess<TMMGLibrary>::GetDefaultParameters() const
"max_num_search_iterations" : 8,
"echo_level" : 0
}
},
},
"extrapolate_contour_values" : true,
"surface_elements" : false,
"search_parameters" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2854,7 +2854,7 @@ void MmgUtilities<MMGLibrary::MMG2D>::SetLocalParameter(
double HMin,
double HMax,
double HausdorffValue
)
)
{
if ( MMG2D_Set_localParameter(mMmgMesh, mMmgMet, MMG5_Edg, rColor, HMin, HMax, HausdorffValue) != 1)
KRATOS_ERROR << "Unable to set local parameter" << std::endl;
Expand All @@ -2869,7 +2869,7 @@ void MmgUtilities<MMGLibrary::MMG3D>::SetLocalParameter(
double HMin,
double HMax,
double HausdorffValue
)
)
{
if ( MMG3D_Set_localParameter(mMmgMesh, mMmgMet, MMG5_Triangle, rColor, HMin, HMax, HausdorffValue) != 1)
KRATOS_ERROR << "Unable to set local parameter" << std::endl;
Expand All @@ -2884,7 +2884,7 @@ void MmgUtilities<MMGLibrary::MMGS>::SetLocalParameter(
double HMin,
double HMax,
double HausdorffValue
)
)
{
if ( MMGS_Set_localParameter(mMmgMesh, mMmgMet, MMG5_Triangle, rColor, HMin, HMax, HausdorffValue) != 1)
KRATOS_ERROR << "Unable to set local parameter" << std::endl;
Expand Down Expand Up @@ -3578,6 +3578,10 @@ void MmgUtilities<TMMGLibrary>::ReorderAllIds(ModelPart& rModelPart)
for(IndexType i = 0; i < r_elements_array.size(); ++i)
(it_elem_begin + i)->SetId(i + 1);

r_nodes_array.Unique();
r_conditions_array.Unique();
r_elements_array.Unique();

KRATOS_CATCH("");
}

Expand Down Expand Up @@ -3801,6 +3805,8 @@ void MmgUtilities<TMMGLibrary>::GenerateMeshDataFromModelPart(
}
}

r_nodes_array.Unique();

/* Conditions */
counter_to_remesh = block_for_each<SumReduction<IndexType>>(
r_conditions_array,
Expand Down Expand Up @@ -3829,6 +3835,8 @@ void MmgUtilities<TMMGLibrary>::GenerateMeshDataFromModelPart(
}
}

r_conditions_array.Unique();

/* Elements */
counter_to_remesh = block_for_each<SumReduction<IndexType>>(
r_elements_array,
Expand Down Expand Up @@ -3857,6 +3865,8 @@ void MmgUtilities<TMMGLibrary>::GenerateMeshDataFromModelPart(
}
}

r_elements_array.Unique();

// Now we compute the colors
rColors.clear();
ColorsMapType nodes_colors, cond_colors, elem_colors;
Expand Down
115 changes: 115 additions & 0 deletions kratos/containers/key_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Suneth Warnakulasuriya
//

#pragma once

// System includes
#include <functional>
#include <type_traits>
#include <utility>

// External includes

// Project includes

namespace Kratos
{
///@name Kratos Classes
///@{

/**
* @class TestOperators
* @brief Operator class to test whether operators "<" and "==" is defined for given TDataType.
*/
struct TestOperators
{
template<class TDataType>
static auto TestEqual(TDataType*) -> decltype(std::declval<TDataType>() == std::declval<TDataType>());
template<typename>
static auto TestEqual(...) -> std::false_type;

template<class TDataType>
static auto TestLess(TDataType*) -> decltype(std::declval<TDataType>() < std::declval<TDataType>());
template<typename>
static auto TestLess(...) -> std::false_type;
};

/**
* @class KeyGenerator
* @brief A functor that serves as the identity function if "<" and "==" operators are defined, otherwise, returns memory location.
* @details This class provides two overloaded operator() functions, one for non-const objects
* and another for const objects. The operator() returns the input objects memory location as it is,
* effectively acting as an identity function for address if the operators "<" and "==" is not defined. Otherwise,
* this returns the exact input object.
*
* The purpose of this functor is to allow objects' memory locations or exact object to be used as keys in sets or other
* containers that require comparison. By using this functor, you can avoid defining
* custom comparison functions or operators when the object itself can be considered
* for comparison.
* @tparam TDataType The data type of the object that the functor operates on.
* @author Suneth Warnakulasuriya
*/
template<class TDataType>
class KeyGenerator
{
private:
///@name Private static variables
///@{

static constexpr bool HasOperatorEqual = std::is_same_v<bool, decltype(TestOperators::TestEqual<TDataType>(0))>;

static constexpr bool HasOperatorLess = std::is_same_v<bool, decltype(TestOperators::TestLess<TDataType>(0))>;

static constexpr bool HasOperatorsDefined = HasOperatorEqual && HasOperatorLess;

///@}

public:
///@name Public operators
///@{

/**
* @brief Operator that returns a non-const reference to the input object if "<" and "==" operators are defined.
* Otherwise this returns a pointer to the input object.
* @param data The input object of type TDataType.
* @return A non-const reference or pointer to the same object as provided in the parameter.
*/
std::conditional_t<HasOperatorsDefined, TDataType&, TDataType*> operator()(TDataType& rData)
{
if constexpr(HasOperatorsDefined) {
return rData;
} else {
return &rData;
}
}

/**
* @brief Operator that returns a const reference to the input object if "<" and "==" operators are defined.
* Otherwise this returns a pointer to the const input object.
* @param data The input object of type TDataType.
* @return A const reference or pointer to the same object as provided in the parameter.
*/
std::conditional_t<HasOperatorsDefined, TDataType const&, TDataType const*> operator()(const TDataType& rData) const
{
if constexpr(HasOperatorsDefined) {
return rData;
} else {
return &rData;
}
}

///@}
};

///@}

} // namespace Kratos.
Loading

0 comments on commit 384b3cd

Please sign in to comment.