Skip to content

[IO] Enhance IO class by adding support for reading master-slave constraints and geometries from sub-model parts (transition to extend Metis geometries/constraint support)) #13304

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

Merged
merged 14 commits into from
Jun 3, 2025
Merged
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
189 changes: 159 additions & 30 deletions kratos/includes/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Pooyan Dadvand
//

#pragma once

// System includes
#include <string>
#include <iostream>
#include <unordered_set>

// External includes
Expand Down Expand Up @@ -72,43 +70,66 @@ class KRATOS_API(KRATOS_CORE) IO
KRATOS_DEFINE_LOCAL_FLAG( MESH_ONLY );
KRATOS_DEFINE_LOCAL_FLAG( SCIENTIFIC_PRECISION );

typedef Node NodeType;
/// Node type definition
using NodeType = Node;

typedef Geometry<NodeType> GeometryType;
/// Geometry type definition
using GeometryType = Geometry<NodeType>;

typedef Mesh<NodeType, Properties, Element, Condition> MeshType;
/// Mesh type definition
using MeshType = Mesh<NodeType, Properties, Element, Condition>;

typedef MeshType::NodesContainerType NodesContainerType;
/// Nodes container type within MeshType
using NodesContainerType = typename MeshType::NodesContainerType;

typedef MeshType::PropertiesContainerType PropertiesContainerType;
/// Properties container type within MeshType
using PropertiesContainerType = typename MeshType::PropertiesContainerType;

typedef ModelPart::GeometryContainerType GeometryContainerType;
/// Geometry container type within ModelPart
using GeometryContainerType = typename ModelPart::GeometryContainerType;

typedef MeshType::ElementsContainerType ElementsContainerType;
/// The geometry map type within ModelPart
using GeometriesMapType = typename ModelPart::GeometriesMapType;

typedef MeshType::ConditionsContainerType ConditionsContainerType;
/// Elements container type within MeshType
using ElementsContainerType = typename MeshType::ElementsContainerType;

typedef std::vector<std::vector<std::size_t> > ConnectivitiesContainerType;
/// Conditions container type within MeshType
using ConditionsContainerType = typename MeshType::ConditionsContainerType;

typedef std::vector<std::vector<std::size_t> > PartitionIndicesContainerType;
/// MasterSlaveConstraint container type within MeshType
using MasterSlaveConstraintContainerType = typename MeshType::MasterSlaveConstraintContainerType;

typedef std::vector<std::size_t> PartitionIndicesType;
/// Connectivities container type
using ConnectivitiesContainerType = std::vector<std::vector<std::size_t>>;

typedef std::size_t SizeType;
/// Partition indices container type
using PartitionIndicesContainerType = std::vector<std::vector<std::size_t>>;

typedef DenseMatrix<int> GraphType;
/// Partition indices type
using PartitionIndicesType = std::vector<std::size_t>;

// auxiliary struct containg information about the partitioning of the entities in a ModelPart
/// Size type definition
using SizeType = std::size_t;

/// Graph type definition
using GraphType = DenseMatrix<int>;

// Auxiliary struct containing information about the partitioning of the entities in a ModelPart
struct PartitioningInfo
{
GraphType Graph;
PartitionIndicesType NodesPartitions; // partition where the Node is local
PartitionIndicesType ElementsPartitions; // partition where the Element is local
PartitionIndicesType ConditionsPartitions; // partition where the Condition is local
PartitionIndicesContainerType NodesAllPartitions; // partitions, in which the Node is present (local & ghost)
PartitionIndicesContainerType ElementsAllPartitions; // partitions, in which the Element is present (local & ghost)
PartitionIndicesContainerType ConditionsAllPartitions; // partitions, in which the Condition is present (local & ghost)
};
PartitionIndicesType NodesPartitions; // Partition where the Node is local
PartitionIndicesType ElementsPartitions; // Partition where the Element is local
PartitionIndicesType ConditionsPartitions; // Partition where the Condition is local
PartitionIndicesType ConstraintsPartitions; // Partition where the MasterSlaveConstraint is local
PartitionIndicesType GeometriesPartitions; // Partition where the Geometry is local
PartitionIndicesContainerType NodesAllPartitions; // Partitions, in which the Node is present (local & ghost)
PartitionIndicesContainerType ElementsAllPartitions; // Partitions, in which the Element is present (local & ghost)
PartitionIndicesContainerType ConditionsAllPartitions; // Partitions, in which the Condition is present (local & ghost)
PartitionIndicesContainerType ConstraintsAllPartitions; // Partitions, in which the MasterSlaveConstraint is present (local & ghost)
PartitionIndicesContainerType GeometriesAllPartitions; // Partitions, in which the Geometry is present (local & ghost)
};;

///@}
///@name Life Cycle
Expand Down Expand Up @@ -349,6 +370,54 @@ class KRATOS_API(KRATOS_CORE) IO
KRATOS_ERROR << "Calling base class method (WriteConditions). Please check the definition of derived class" << std::endl;
}

/**
* @brief Reads the master-slave constraints from an input source.
* @details This method is intended to be overridden by derived classes to implement
* the specific logic for reading master-slave constraints into the provided
* container. The base class implementation throws an error, indicating that
* the method must be implemented in the derived class.
* @param rThisNodes The nodes to be used for associating the master-slave constraints.
* @param rConstraintContainer The container where the master-slave
* constraints will be stored. This container is expected to be populated
* by the derived class implementation.
* @throws Exception If the base class method is called directly, an error is
* thrown to indicate that the method must be implemented in a derived class.
*/
virtual void ReadConstraints(
NodesContainerType& rThisNodes,
MasterSlaveConstraintContainerType& rConstraintContainer
)
{
KRATOS_ERROR << "Calling base class method (ReadConstraints). Please check the definition of derived class" << std::endl;
}

/**
* @brief This method reads the constraints connectivities
* @param rConditionsConnectivities The constraints connectivities
* @return The number of constraints
*/
virtual std::size_t ReadConstraintsConnectivities(ConnectivitiesContainerType& rConstraintsConnectivities)
{
KRATOS_ERROR << "Calling base class method (ReadConstraintsConnectivities). Please check the definition of derived class" << std::endl;
}

/**
* @brief Writes the master-slave constraints to the output.
* @details This method is intended to be overridden by derived classes to provide
* specific functionality for writing master-slave constraints. The base
* class implementation throws an error, indicating that the method must
* be implemented in the derived class.
* @param rConstraintContainer The container holding the master-slave
* constraints to be written.
* @throws Exception Always throws an error if called on the base class.
* Derived classes must override this method to provide the actual
* implementation.
*/
virtual void WriteConstraints(MasterSlaveConstraintContainerType const& rConstraintContainer)
{
KRATOS_ERROR << "Calling base class method (WriteNewConstraint). Please check the definition of derived class" << std::endl;
}

/**
* @brief This method reads the initial values of the model part
* @param rThisModelPart The model part with the initial values to be read
Expand Down Expand Up @@ -564,22 +633,82 @@ class KRATOS_API(KRATOS_CORE) IO
KRATOS_ERROR << "Calling base class method (DivideInputToPartitions). Please check the definition of derived class" << std::endl;
}

/**
* @brief Virtual method to read element and condition IDs from a sub-model part.
* @details This method is intended to be overridden by derived classes.
* @param rModelPartName The name of the sub-model part to read from.
* @param rElementsIds Set to store element IDs.
* @param rConditionsIds Set to store condition IDs.
*/
virtual void ReadSubModelPartElementsAndConditionsIds(
std::string const& rModelPartName,
std::unordered_set<SizeType> &rElementsIds,
std::unordered_set<SizeType> &rConditionsIds)
std::unordered_set<SizeType>& rElementsIds,
std::unordered_set<SizeType>& rConditionsIds
)
{
KRATOS_ERROR << "Calling base class method (ReadSubModelPartElementsAndConditionsIds). Please check the definition of derived class" << std::endl;
}

/**
* @brief Virtual method to read element, condition, master-slave constraint, and geometry IDs from a sub-model part.
* @details This method is intended to be overridden by derived classes.
* @param rModelPartName The name of the sub-model part to read from.
* @param rElementsIds Set to store element IDs.
* @param rConditionsIds Set to store condition IDs.
* @param rConstraintIds Set to store master-slave constraint IDs.
* @param rGeometriesIds Set to store geometry IDs.
*/
virtual void ReadSubModelPartEntitiesIds(
Copy link
Member

Choose a reason for hiding this comment

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

I understand that this is the substitution of the above method. Then we shall change the above one.....

Copy link
Member Author

Choose a reason for hiding this comment

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

I keep it the old one because we are just changing this file in this PR, in other case should be removed in the final one

std::string const& rModelPartName,
std::unordered_set<SizeType>& rElementsIds,
std::unordered_set<SizeType>& rConditionsIds,
std::unordered_set<SizeType>& rConstraintIds,
std::unordered_set<SizeType>& rGeometriesIds
)
{
KRATOS_WARNING("IO") << " The method ReadSubModelPartEntitiesIds with Constraint and Geometries is not implemented. Only the elements and conditions are read." << std::endl;
return ReadSubModelPartElementsAndConditionsIds(rModelPartName, rElementsIds, rConditionsIds);
}

/**
* @brief Virtual method to read nodal graph from entities list.
* @details This method is intended to be overridden by derived classes.
* @param rAuxConnectivities Container of connectivities.
* @param rElementsIds Set of element IDs.
* @param rConditionsIds Set of condition IDs.
* @return The size of the nodal graph.
*/
virtual std::size_t ReadNodalGraphFromEntitiesList(
ConnectivitiesContainerType& rAuxConnectivities,
std::unordered_set<SizeType> &rElementsIds,
std::unordered_set<SizeType> &rConditionsIds)
std::unordered_set<SizeType>& rElementsIds,
std::unordered_set<SizeType>& rConditionsIds
)
{
KRATOS_ERROR << "Calling base class method (ReadNodalGraphFromEntitiesList). Please check the definition of derived class" << std::endl;
}

/**
* @brief Virtual method to read nodal graph from entities list including master-slave constraints and geometries.
* @details This method is intended to be overridden by derived classes.
* @param rAuxConnectivities Container of connectivities.
* @param rElementsIds Set of element IDs.
* @param rConditionsIds Set of condition IDs.
* @param rConstraintIds Set of master-slave constraint IDs.
* @param rGeometriesIds Set of geometry IDs.
* @return The size of the nodal graph.
*/
virtual std::size_t ReadNodalGraphFromEntitiesList(
Copy link
Member

Choose a reason for hiding this comment

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

This one again. I definitely suggest you to provide a list of usage in Kratos to see how changing the interface would affect us.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is used in the final PR:#13435
See applications/MetisApplication/custom_processes/metis_divide_submodelparts_heterogeneous_input_process.cpp

ConnectivitiesContainerType& rAuxConnectivities,
std::unordered_set<SizeType>& rElementsIds,
std::unordered_set<SizeType>& rConditionsIds,
std::unordered_set<SizeType>& rConstraintIds,
std::unordered_set<SizeType>& rGeometriesIds
)
{
KRATOS_WARNING("IO") << " The method ReadNodalGraphFromEntitiesList with Constraint and Geometries is not implemented. Only the elements and conditions are read." << std::endl;
return ReadNodalGraphFromEntitiesList(rAuxConnectivities, rElementsIds, rConditionsIds);
}

///@}
///@name Access
///@{
Expand Down Expand Up @@ -719,4 +848,4 @@ inline std::ostream& operator << (std::ostream& rOStream,
///@}


} // namespace Kratos.
} // namespace Kratos.
Loading