diff --git a/kratos/utilities/cpp_tests_utilities.cpp b/kratos/utilities/cpp_tests_utilities.cpp index b1f6a9772035..9146210c9ffb 100644 --- a/kratos/utilities/cpp_tests_utilities.cpp +++ b/kratos/utilities/cpp_tests_utilities.cpp @@ -403,32 +403,106 @@ 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; } @@ -436,30 +510,92 @@ ModelPart& CreateCubeSkinModelPart( /***********************************************************************************/ /***********************************************************************************/ -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; } diff --git a/kratos/utilities/cpp_tests_utilities.h b/kratos/utilities/cpp_tests_utilities.h index 62672c30addc..0a71336592ea 100644 --- a/kratos/utilities/cpp_tests_utilities.h +++ b/kratos/utilities/cpp_tests_utilities.h @@ -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