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

[Core] Geometries to entities modeler #11370

Merged
merged 36 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a3c9e13
Merge branch 'core/model-part-io-geometries-fix' into core/geometries…
rubenzorrilla Jul 6, 2023
22bf135
Preliminary implementation (working on it)
rubenzorrilla Jul 6, 2023
f4239a5
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Jul 7, 2023
1ffea05
Working version with test
rubenzorrilla Jul 7, 2023
c1bb3f1
Merge branch 'master' into core/geometries-to-entities-modeler
loumalouomega Jul 10, 2023
b8d8192
Applying intermediate comments
loumalouomega Jul 10, 2023
3ec860a
Moving to source file
loumalouomega Jul 10, 2023
936d8c8
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Jul 24, 2023
02783b8
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Jul 31, 2023
11e0232
Geometries PrintData minor improvement
rubenzorrilla Aug 1, 2023
0b7e75e
Minors in entities utilities
rubenzorrilla Aug 1, 2023
344b056
Minor advances (mixed not working)
rubenzorrilla Aug 1, 2023
5b7fe76
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Aug 9, 2023
2046af2
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Aug 30, 2023
f533030
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Aug 31, 2023
38f1950
Adding HasPrototypeEntity
rubenzorrilla Sep 1, 2023
36bc13d
Working version
rubenzorrilla Sep 1, 2023
38717be
Merge branch 'core/entities-utilities-has-entity' into core/geometrie…
rubenzorrilla Sep 1, 2023
ba08fec
Remove undesired changes
rubenzorrilla Sep 1, 2023
2cc9c6d
Merge branch 'core/entities-utilities-has-entity' into core/geometrie…
rubenzorrilla Sep 1, 2023
42e5f2f
Merge branch 'core/geometries-to-entities-modeler' of https://github.…
rubenzorrilla Sep 5, 2023
54c2347
Merge branch 'core/geometries-print-data-minor-improvements' into cor…
rubenzorrilla Sep 5, 2023
f5a3ea2
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Sep 5, 2023
251c865
More robust type check
rubenzorrilla Sep 5, 2023
21ede17
Add to registry
rubenzorrilla Sep 6, 2023
604a4d9
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Sep 21, 2023
097ce9a
Update registry macro
rubenzorrilla Sep 21, 2023
0f08fab
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Oct 2, 2023
2d43044
Merge branch 'core/geometries-to-entities-modeler' of https://github.…
rubenzorrilla Oct 2, 2023
bc2c578
KratosCore minors
rubenzorrilla Oct 2, 2023
d30143c
Deprecating old modelers factory
rubenzorrilla Oct 2, 2023
70234b2
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Oct 2, 2023
aa35d1d
Backwards compatible modelers creation
rubenzorrilla Oct 2, 2023
54b6bfb
Update kratos/python_scripts/analysis_stage.py
rubenzorrilla Oct 3, 2023
a26755f
Merge branch 'master' into core/geometries-to-entities-modeler
rubenzorrilla Oct 3, 2023
4aa9245
Avoid template specialization
rubenzorrilla Oct 3, 2023
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
168 changes: 168 additions & 0 deletions kratos/modeler/create_entities_from_geometries_modeler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Ruben Zorrilla
//

// System includes

// External includes

// Project includes
#include "includes/parallel_environment.h"
#include "utilities/entities_utilities.h"
#include "utilities/variable_utils.h"
#include "create_entities_from_geometries_modeler.h"

namespace Kratos
{

namespace {

/**
* @brief Create entities from geometries.
* @param EntityName the name of the entity
rubenzorrilla marked this conversation as resolved.
Show resolved Hide resolved
* @param rModelPart the model part
*/
template <class TEntitiesContainerType, class TEntityIdentifier>
void CreateEntitiesFromGeometries(
const TEntityIdentifier& rEntityIdentifier,
ModelPart& rModelPart
)
{
// Create the entities container and allocate space
TEntitiesContainerType entities_to_add;
entities_to_add.reserve(rModelPart.NumberOfGeometries());

// Get current max element id
using SizeType = std::size_t;
rubenzorrilla marked this conversation as resolved.
Show resolved Hide resolved
SizeType max_id;
const auto& r_root_model_part = rModelPart.GetRootModelPart();
if constexpr (std::is_same<typename TEntitiesContainerType::value_type, Element>::value) {
max_id = block_for_each<MaxReduction<SizeType>>(r_root_model_part.Elements(), [](auto& rElement){
return rElement.Id();
});
} else if constexpr (std::is_same<typename TEntitiesContainerType::value_type, Condition>::value) {
max_id = block_for_each<MaxReduction<SizeType>>(r_root_model_part.Conditions(), [](auto& rCondition){
return rCondition.Id();
});
}

// Loop geometries to create the corresponding entities from them
// Note that we retrieve the corresponding prototype entity from the entities idenfifier
// This makes possible to loop and create entities from different type geometries
for (auto& r_geom : rModelPart.Geometries()) {
if (rEntityIdentifier.HasPrototypeEntity(r_geom)) {
const auto& r_ref_entity = rEntityIdentifier.GetPrototypeEntity(r_geom);
auto p_entity = r_ref_entity.Create(++max_id, r_geom, nullptr);
entities_to_add.push_back(p_entity);
}
}

// Add the created entities to current submodelpart
if constexpr (std::is_same<typename TEntitiesContainerType::value_type, Element>::value) {
rModelPart.AddElements(entities_to_add.begin(), entities_to_add.end());
} else if constexpr (std::is_same<typename TEntitiesContainerType::value_type, Condition>::value) {
rModelPart.AddConditions(entities_to_add.begin(), entities_to_add.end());
}
}

}

template <>
void CreateEntitiesFromGeometriesModeler::RemoveModelPartEntities<Element>(ModelPart &rModelPart)
{
const SizeType n_elements = rModelPart.NumberOfElements();
KRATOS_WARNING_IF("CreateEntitiesFromGeometriesModeler", n_elements != 0)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this a warning? Isn't this why the function was made for? (Maybe should be KRATOS_INFO_IF).
Imho warning should be if n_elements == 0.

Also you could merge both functions (see LoopEntitiesList)

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea is to inform the user that there are elements (entities) to be removed.

<< "There are " << n_elements << " elements in '" << rModelPart.FullName() << "' model part. These are to be removed." << std::endl;
VariableUtils().SetFlag(TO_ERASE, true, rModelPart.Elements());
rModelPart.RemoveElementsFromAllLevels(TO_ERASE);
}

template <>
void CreateEntitiesFromGeometriesModeler::RemoveModelPartEntities<Condition>(ModelPart &rModelPart)
{
const SizeType n_conditions = rModelPart.NumberOfConditions();
KRATOS_WARNING_IF("CreateEntitiesFromGeometriesModeler", n_conditions != 0)
Copy link
Member

Choose a reason for hiding this comment

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

same

Copy link
Member Author

Choose a reason for hiding this comment

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

Same answer.

<< "There are " << n_conditions << " conditions in '" << rModelPart.FullName() << "' model part. These are to be removed." << std::endl;
VariableUtils().SetFlag(TO_ERASE, true, rModelPart.Conditions());
rModelPart.RemoveConditionsFromAllLevels(TO_ERASE);
}

template <>
void CreateEntitiesFromGeometriesModeler::LoopEntitiesList<Element>(Parameters EntitiesList)
{
for (auto& r_data : EntitiesList) {
// Get model part in which the entities are to be created
const std::string& r_model_part_name = r_data["model_part_name"].GetString();
auto& r_model_part = mpModel->GetModelPart(r_model_part_name);

// Get entities substitution list
// Note that the entities identifier allow to substitue different geometry types
const std::string& r_element_name = r_data["element_name"].GetString();
auto entity_identifier = EntitiesUtilities::EntitityIdentifier<Element>(r_element_name);

// Wipe current model part entities
RemoveModelPartEntities<Element>(r_model_part);

// Create submodelpart elements from geometries
CreateEntitiesFromGeometries<ModelPart::ElementsContainerType, EntitiesUtilities::EntitityIdentifier<Element>>(entity_identifier, r_model_part);
}
}

template <>
void CreateEntitiesFromGeometriesModeler::LoopEntitiesList<Condition>(Parameters EntitiesList)
{
for (auto& r_data : EntitiesList) {
// Get model part in which the entities are to be created
const std::string& r_model_part_name = r_data["model_part_name"].GetString();
auto& r_model_part = mpModel->GetModelPart(r_model_part_name);

// Get entities substitution list
// Note that the entities identifier allow to substitue different geometry types
const std::string& r_element_name = r_data["condition_name"].GetString();
auto entity_identifier = EntitiesUtilities::EntitityIdentifier<Condition>(r_element_name);

// Wipe current model part entities
RemoveModelPartEntities<Condition>(r_model_part);

// Create submodelpart conditions from geometries
CreateEntitiesFromGeometries<ModelPart::ConditionsContainerType, EntitiesUtilities::EntitityIdentifier<Condition>>(entity_identifier, r_model_part);
}
loumalouomega marked this conversation as resolved.
Show resolved Hide resolved
rubenzorrilla marked this conversation as resolved.
Show resolved Hide resolved
}

void CreateEntitiesFromGeometriesModeler::SetupModelPart()
{
// Check parallelism
//TODO: Make this MPI compatible by renumbering the local ids
KRATOS_ERROR_IF(ParallelEnvironment::GetDefaultDataCommunicator().IsDistributed())
<< "CreateEntitiesFromGeometriesModeler is not MPI compatible yet." << std::endl;

// Get the elements list from input settings
const auto& r_elements_list = mParameters.GetValue("elements_list");
const SizeType n_element_pairs = r_elements_list.size();
KRATOS_INFO_IF("CreateEntitiesFromGeometriesModeler", mEchoLevel != 0 && n_element_pairs == 0)
<< "No elements found in element list." << std::endl;

// Loop the element list to create the correspoding entities
LoopEntitiesList<Element>(r_elements_list);

// Get the conditions list from input settings
const auto& r_conditions_list = mParameters.GetValue("conditions_list");
const SizeType n_condition_pairs = r_conditions_list.size();
KRATOS_INFO_IF("CreateEntitiesFromGeometriesModeler", mEchoLevel != 0 && n_condition_pairs == 0)
<< "No conditions found in condition list." << std::endl;

// Loop the condition list to create the correspoding entities
LoopEntitiesList<Condition>(r_conditions_list);
}

} // namespace Kratos.


183 changes: 183 additions & 0 deletions kratos/modeler/create_entities_from_geometries_modeler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Ruben Zorrilla
//
//

#pragma once

// System includes

// External includes

// Project includes
#include "containers/model.h"
#include "includes/define.h"
#include "includes/define_registry.h"
#include "includes/kratos_components.h"
#include "modeler/modeler.h"
#include "utilities/parallel_utilities.h"
#include "utilities/reduction_utilities.h"

namespace Kratos
{

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

/**
* @class CreateEntitiesFromGeometriesModeler
* @ingroup KratosCore
* @brief Modeler to create element/condition entities from geometries
* @details Given a list of pairs of the type submodelpart name and entity (element/condition) name,
* this modeler creates the corresponding entities from the submodelpart geometries.
* @author Ruben Zorrilla
*/
class KRATOS_API(KRATOS_CORE) CreateEntitiesFromGeometriesModeler
: public Modeler
{
public:
///@name Type Definitions
///@{

/// Pointer definition of Modeler
KRATOS_CLASS_POINTER_DEFINITION(CreateEntitiesFromGeometriesModeler);

///@}
///@name Life Cycle
///@{

/// Default constructor
CreateEntitiesFromGeometriesModeler() : Modeler() {};

/// Constructor with Model
CreateEntitiesFromGeometriesModeler(
Model &rModel,
Parameters Settings)
: Modeler(rModel, Settings)
{
mpModel = &rModel;
mParameters.ValidateAndAssignDefaults(GetDefaultParameters());
}

/// Destructor.
~CreateEntitiesFromGeometriesModeler() = default;

/// Creates the CreateEntitiesFromGeometriesModeler Pointer
Modeler::Pointer Create(
Model& rModel,
const Parameters Settings) const override
{
return Kratos::make_shared<CreateEntitiesFromGeometriesModeler>(rModel, Settings);
}

///@}
///@name Operations
///@{

/// Convert the geometry model or import analysis suitable models.
void SetupModelPart() override;

/// This method provides the defaults parameters to avoid conflicts between the different constructors
const Parameters GetDefaultParameters() const override
{
const Parameters default_parameters = Parameters(R"({
"echo_level" : 0,
"elements_list" : [],
"conditions_list" : []
})");
return default_parameters;
}

///@}
///@name Operators
///@{


///@}
///@name Input and output
///@{

/// Turn back information as a string.
std::string Info() const override
{
return "CreateEntitiesFromGeometriesModeler";
}

/// Print information about this object.
void PrintInfo(std::ostream& rOStream) const override
{
rOStream << Info();
}

/// Print object's data.
void PrintData(std::ostream& rOStream) const override
{
}

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

KRATOS_REGISTRY_ADD_PROTOTYPE("Modelers.KratosMultiphysics", Modeler, CreateEntitiesFromGeometriesModeler)
KRATOS_REGISTRY_ADD_PROTOTYPE("Modelers.All", Modeler, CreateEntitiesFromGeometriesModeler)

///@name Private members
///@{

Model* mpModel = nullptr; /// The model considered in the geometry replacement

///@}
///@name Private Operations
///@{

/**
* @brief Loops through a list of entities.
* @tparam TEntityType The type of entity.
* @param EntitiesList The list of entities.
*/
template <class TEntityType>
loumalouomega marked this conversation as resolved.
Show resolved Hide resolved
rubenzorrilla marked this conversation as resolved.
Show resolved Hide resolved
void LoopEntitiesList(Parameters EntitiesList);

/**
* @brief Removes entities from a model part.
* @tparam TEntityType The type of entity.
* @param rModelPart The model part from which entities will be removed.
*/
template <class TEntityType>
void RemoveModelPartEntities(ModelPart& rModelPart);

///@}
}; // Class CreateEntitiesFromGeometriesModeler

///@}
///@name Input and output
///@{

/// input stream function
inline std::istream& operator >> (
std::istream& rIStream,
CreateEntitiesFromGeometriesModeler& rThis);

/// output stream function
inline std::ostream& operator << (
std::ostream& rOStream,
const CreateEntitiesFromGeometriesModeler& rThis)
{
rThis.PrintInfo(rOStream);
rOStream << std::endl;
rThis.PrintData(rOStream);

return rOStream;
}
///@}

} // namespace Kratos.
5 changes: 5 additions & 0 deletions kratos/python/add_modeler_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "modeler/modeler_factory.h"
#include "modeler/edge_swapping_2d_modeler.h"
#include "modeler/connectivity_preserve_modeler.h"
#include "modeler/create_entities_from_geometries_modeler.h"
#include "modeler/serial_model_part_combinator_modeler.h"
#include "modeler/duplicate_mesh_modeler.h"
#include "modeler/copy_properties_modeler.h"
Expand Down Expand Up @@ -60,6 +61,7 @@ void AddModelerToPython(pybind11::module& m)
py::class_<Modeler, Modeler::Pointer>(m,"Modeler")
.def(py::init<>())
.def(py::init<Model&, Parameters>())
.def("Create", &Modeler::Create)
// Modeler Stages Initialize
.def("SetupGeometryModel", &Modeler::SetupGeometryModel)
.def("PrepareGeometryModel", &Modeler::PrepareGeometryModel)
Expand Down Expand Up @@ -106,6 +108,9 @@ void AddModelerToPython(pybind11::module& m)
.def(py::init<Model&, Parameters>())
;

py::class_< CreateEntitiesFromGeometriesModeler, CreateEntitiesFromGeometriesModeler::Pointer, Modeler >(m, "CreateEntitiesFromGeometriesModeler")
.def(py::init<Model&, Parameters>())
;
}

} // namespace Kratos::Python.
2 changes: 2 additions & 0 deletions kratos/python/add_registry_to_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "includes/define_python.h"
#include "includes/registry.h"
#include "add_registry_to_python.h"
#include "modeler/modeler.h"
#include "operations/operation.h"
#include "processes/process.h"

Expand Down Expand Up @@ -67,6 +68,7 @@ void AddRegistryToPython(pybind11::module& m)
.def_static("HasItems", &Registry::HasItems)
.def_static("HasValue", &Registry::HasValue)
.def_static("GetItem", &Registry::GetItem, py::return_value_policy::reference)
.def_static("GetModeler", &Registry::GetValue<Modeler>, py::return_value_policy::reference)
.def_static("GetOperation", &Registry::GetValue<Operation>, py::return_value_policy::reference)
.def_static("GetProcess", &Registry::GetValue<Process>, py::return_value_policy::reference)
.def_static("RemoveItem", &Registry::RemoveItem)
Expand Down
Loading
Loading