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][MPI] Partially adapt CppTestsUtilities for MPI #11716

Merged
merged 1 commit into from
Oct 24, 2023
Merged
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
214 changes: 175 additions & 39 deletions kratos/utilities/cpp_tests_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,63 +403,199 @@ ModelPart& CreateCubeSkinModelPart(
Model& rCurrentModel,
const double HalfX,
const double HalfY,
const double HalfZ
const double HalfZ,
const DataCommunicator& rDataCommunicator
)
{
// Generate the cube skin
ModelPart& r_skin_part = rCurrentModel.CreateModelPart("Skin");
r_skin_part.CreateNewNode(1, -HalfX, -HalfY, -HalfZ);
r_skin_part.CreateNewNode(2, HalfX, -HalfY, -HalfZ);
r_skin_part.CreateNewNode(3, HalfX, HalfY, -HalfZ);
r_skin_part.CreateNewNode(4, -HalfX, HalfY, -HalfZ);
r_skin_part.CreateNewNode(5, -HalfX, -HalfY, HalfZ);
r_skin_part.CreateNewNode(6, HalfX, -HalfY, HalfZ);
r_skin_part.CreateNewNode(7, HalfX, HalfY, HalfZ);
r_skin_part.CreateNewNode(8, -HalfX, HalfY, HalfZ);
Properties::Pointer p_properties(new Properties(0));
r_skin_part.CreateNewElement("Element3D3N", 1, { 1,2,3 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 2, { 1,3,4 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 3, { 5,6,7 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 4, { 5,7,8 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 5, { 3,6,2 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 6, { 3,7,6 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 7, { 4,5,1 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 8, { 4,8,5 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 9, { 3,4,8 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 10, { 3,8,7 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 11, { 2,1,5 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 12, { 2,5,6 }, p_properties);

// Distributed related variables
if (rDataCommunicator.IsDistributed()) {
r_skin_part.AddNodalSolutionStepVariable(PARTITION_INDEX);
}
const int rank = rDataCommunicator.Rank();
const int world_size = rDataCommunicator.Size();

// Create properties
auto p_properties = r_skin_part.CreateNewProperties(1, 0);

// If only one partition
if (world_size == 1) {
// Create nodes
r_skin_part.CreateNewNode(1, -HalfX, -HalfY, -HalfZ);
r_skin_part.CreateNewNode(2, HalfX, -HalfY, -HalfZ);
r_skin_part.CreateNewNode(3, HalfX, HalfY, -HalfZ);
r_skin_part.CreateNewNode(4, -HalfX, HalfY, -HalfZ);
r_skin_part.CreateNewNode(5, -HalfX, -HalfY, HalfZ);
r_skin_part.CreateNewNode(6, HalfX, -HalfY, HalfZ);
r_skin_part.CreateNewNode(7, HalfX, HalfY, HalfZ);
r_skin_part.CreateNewNode(8, -HalfX, HalfY, HalfZ);

// Set the partition index
if (rDataCommunicator.IsDistributed()) {
for (auto& r_node : r_skin_part.Nodes()) {
r_node.FastGetSolutionStepValue(PARTITION_INDEX) = 0;
}
}

// Create elements
r_skin_part.CreateNewElement("Element3D3N", 1, { 1,2,3 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 2, { 1,3,4 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 3, { 5,6,7 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 4, { 5,7,8 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 5, { 3,6,2 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 6, { 3,7,6 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 7, { 4,5,1 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 8, { 4,8,5 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 9, { 3,4,8 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 10, { 3,8,7 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 11, { 2,1,5 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 12, { 2,5,6 }, p_properties);
} else {
if (rank == 0) {
// Create nodes
auto p_node1 = r_skin_part.CreateNewNode(1, -HalfX, -HalfY, -HalfZ);
auto p_node2 = r_skin_part.CreateNewNode(2, HalfX, -HalfY, -HalfZ);
auto p_node3 = r_skin_part.CreateNewNode(3, HalfX, HalfY, -HalfZ);
auto p_node4 = r_skin_part.CreateNewNode(4, -HalfX, HalfY, -HalfZ);

// Set partitions
p_node1->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node2->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node3->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node4->FastGetSolutionStepValue(PARTITION_INDEX) = 0;

// Create elements
r_skin_part.CreateNewElement("Element3D3N", 1, { 1,2,3 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 2, { 1,3,4 }, p_properties);
} else if (rank == 1) {
// Create nodes
auto p_node1 = r_skin_part.CreateNewNode(1, -HalfX, -HalfY, -HalfZ);
auto p_node2 = r_skin_part.CreateNewNode(2, HalfX, -HalfY, -HalfZ);
auto p_node3 = r_skin_part.CreateNewNode(3, HalfX, HalfY, -HalfZ);
auto p_node4 = r_skin_part.CreateNewNode(4, -HalfX, HalfY, -HalfZ);
auto p_node5 = r_skin_part.CreateNewNode(5, -HalfX, -HalfY, HalfZ);
auto p_node6 = r_skin_part.CreateNewNode(6, HalfX, -HalfY, HalfZ);
auto p_node7 = r_skin_part.CreateNewNode(7, HalfX, HalfY, HalfZ);
auto p_node8 = r_skin_part.CreateNewNode(8, -HalfX, HalfY, HalfZ);

// Set partitions
p_node1->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node2->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node3->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node4->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node5->FastGetSolutionStepValue(PARTITION_INDEX) = 1;
p_node6->FastGetSolutionStepValue(PARTITION_INDEX) = 1;
p_node7->FastGetSolutionStepValue(PARTITION_INDEX) = 1;
p_node8->FastGetSolutionStepValue(PARTITION_INDEX) = 1;

// Create elements
r_skin_part.CreateNewElement("Element3D3N", 3, { 5,6,7 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 4, { 5,7,8 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 5, { 3,6,2 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 6, { 3,7,6 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 7, { 4,5,1 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 8, { 4,8,5 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 9, { 3,4,8 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 10, { 3,8,7 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 11, { 2,1,5 }, p_properties);
r_skin_part.CreateNewElement("Element3D3N", 12, { 2,5,6 }, p_properties);
}
}

return r_skin_part;
}

/***********************************************************************************/
/***********************************************************************************/

ModelPart& CreateCubeModelPart(Model& rCurrentModel)
ModelPart& CreateCubeModelPart(
Model& rCurrentModel,
const DataCommunicator& rDataCommunicator
)
{
// Generate the cube skin
ModelPart& r_model_part = rCurrentModel.CreateModelPart("Cube");

// Distributed related variables
if (rDataCommunicator.IsDistributed()) {
r_model_part.AddNodalSolutionStepVariable(PARTITION_INDEX);
}
const int rank = rDataCommunicator.Rank();
const int world_size = rDataCommunicator.Size();

// Create properties
auto p_properties = r_model_part.CreateNewProperties(1, 0);

r_model_part.CreateNewNode(1 , 0.0 , 1.0 , 1.0);
r_model_part.CreateNewNode(2 , 0.0 , 1.0 , 0.0);
r_model_part.CreateNewNode(3 , 0.0 , 0.0 , 1.0);
r_model_part.CreateNewNode(4 , 0.5 , 1.0 , 1.0);
r_model_part.CreateNewNode(5 , 0.0 , 0.0 , 0.0);
r_model_part.CreateNewNode(6 , 0.5 , 1.0 , 0.0);
r_model_part.CreateNewNode(7 , 0.5 , 0.0 , 1.0);
r_model_part.CreateNewNode(8 , 0.5 , 0.0 , 0.0);
r_model_part.CreateNewNode(9 , 1.0 , 1.0 , 1.0);
r_model_part.CreateNewNode(10 , 1.0 , 1.0 , 0.0);
r_model_part.CreateNewNode(11 , 1.0 , 0.0 , 1.0);
r_model_part.CreateNewNode(12 , 1.0 , 0.0 , 0.0);

// Create elements
r_model_part.CreateNewElement("Element3D8N", 1, {{5,8,6,2,3,7,4,1}}, p_properties);
r_model_part.CreateNewElement("Element3D8N", 2, {{8,12,10,6,7,11,9,4}}, p_properties);
if (world_size == 1) {
// Create nodes
r_model_part.CreateNewNode(1 , 0.0 , 1.0 , 1.0);
r_model_part.CreateNewNode(2 , 0.0 , 1.0 , 0.0);
r_model_part.CreateNewNode(3 , 0.0 , 0.0 , 1.0);
r_model_part.CreateNewNode(4 , 0.5 , 1.0 , 1.0);
r_model_part.CreateNewNode(5 , 0.0 , 0.0 , 0.0);
r_model_part.CreateNewNode(6 , 0.5 , 1.0 , 0.0);
r_model_part.CreateNewNode(7 , 0.5 , 0.0 , 1.0);
r_model_part.CreateNewNode(8 , 0.5 , 0.0 , 0.0);
r_model_part.CreateNewNode(9 , 1.0 , 1.0 , 1.0);
r_model_part.CreateNewNode(10 , 1.0 , 1.0 , 0.0);
r_model_part.CreateNewNode(11 , 1.0 , 0.0 , 1.0);
r_model_part.CreateNewNode(12 , 1.0 , 0.0 , 0.0);

// Set the partition index
if (rDataCommunicator.IsDistributed()) {
for (auto& r_node : r_model_part.Nodes()) {
r_node.FastGetSolutionStepValue(PARTITION_INDEX) = 0;
}
}

// Create elements
r_model_part.CreateNewElement("Element3D8N", 1, {{5,8,6,2,3,7,4,1}}, p_properties);
r_model_part.CreateNewElement("Element3D8N", 2, {{8,12,10,6,7,11,9,4}}, p_properties);
} else { // Assuming always two partitions
if (rank == 0) {
// Create nodes
r_model_part.CreateNewNode(1 , 0.0 , 1.0 , 1.0);
r_model_part.CreateNewNode(2 , 0.0 , 1.0 , 0.0);
r_model_part.CreateNewNode(3 , 0.0 , 0.0 , 1.0);
r_model_part.CreateNewNode(4 , 0.5 , 1.0 , 1.0);
r_model_part.CreateNewNode(5 , 0.0 , 0.0 , 0.0);
r_model_part.CreateNewNode(6 , 0.5 , 1.0 , 0.0);
r_model_part.CreateNewNode(7 , 0.5 , 0.0 , 1.0);
r_model_part.CreateNewNode(8 , 0.5 , 0.0 , 0.0);

// Set partitions
for (auto& r_node : r_model_part.Nodes()) {
r_node.FastGetSolutionStepValue(PARTITION_INDEX) = 0;
}

// Create elements
r_model_part.CreateNewElement("Element3D8N", 1, {{5,8,6,2,3,7,4,1}}, p_properties);
} else if (rank == 1) {
// Create nodes
auto p_node4 = r_model_part.CreateNewNode(4 , 0.5 , 1.0 , 1.0);
auto p_node6 = r_model_part.CreateNewNode(6 , 0.5 , 1.0 , 0.0);
auto p_node7 = r_model_part.CreateNewNode(7 , 0.5 , 0.0 , 1.0);
auto p_node8 = r_model_part.CreateNewNode(8 , 0.5 , 0.0 , 0.0);
auto p_node9 = r_model_part.CreateNewNode(9 , 1.0 , 1.0 , 1.0);
auto p_node10 = r_model_part.CreateNewNode(10 , 1.0 , 1.0 , 0.0);
auto p_node11 = r_model_part.CreateNewNode(11 , 1.0 , 0.0 , 1.0);
auto p_node12 = r_model_part.CreateNewNode(12 , 1.0 , 0.0 , 0.0);

// Set partitions
for (auto& r_node : r_model_part.Nodes()) {
r_node.FastGetSolutionStepValue(PARTITION_INDEX) = 1;
}
p_node4->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node6->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node7->FastGetSolutionStepValue(PARTITION_INDEX) = 0;
p_node8->FastGetSolutionStepValue(PARTITION_INDEX) = 0;

// Create elements
r_model_part.CreateNewElement("Element3D8N", 2, {{8,12,10,6,7,11,9,4}}, p_properties);
}
}

return r_model_part;
}
Expand Down
10 changes: 8 additions & 2 deletions kratos/utilities/cpp_tests_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,27 @@ namespace CppTestsUtilities
* @param HalfX The half-length of the cube in the X-direction.
* @param HalfY The half-length of the cube in the Y-direction.
* @param HalfZ The half-length of the cube in the Z-direction.
* @param rDataCommunicator The data communicator.
* @return ModelPart& The created cube skin model part.
*/
KRATOS_API(KRATOS_CORE) ModelPart& CreateCubeSkinModelPart(
Model& rCurrentModel,
const double HalfX = 0.6,
const double HalfY = 0.9,
const double HalfZ = 0.3
const double HalfZ = 0.3,
const DataCommunicator& rDataCommunicator = DataCommunicator::GetDefault()
);

/**
* @brief Create a cube model part.
* @param rCurrentModel The current model.
* @param rDataCommunicator The data communicator.
* @return The created cube model part.
*/
KRATOS_API(KRATOS_CORE) ModelPart& CreateCubeModelPart(Model& rCurrentModel);
KRATOS_API(KRATOS_CORE) ModelPart& CreateCubeModelPart(
Model& rCurrentModel,
const DataCommunicator& rDataCommunicator = DataCommunicator::GetDefault()
);

}; // namespace CppTestsUtilities
} // namespace Kratos
Loading