From 60fea14390cd85bd68cb154921d44e0213503ad3 Mon Sep 17 00:00:00 2001 From: Anne van de Graaf Date: Mon, 5 Aug 2024 14:03:52 +0200 Subject: [PATCH] [GeoMechanicsApplication] ApplyConstantPhreaticMultiLinePressureProcess checks size of coordinates (#12578) * Check that coordinates in gravity direction are not empty * Check that coordinates in horizontal direction are not empty * Check that at least two coordinates in gravity direction are given * Check that at least two coordinates in horizontal direction are given * No failure when at least two coordinates in gravity direction are given * No failure when at least two coordinates in horizontal direction are given * Replaced some tabs by spaces --- ...t_phreatic_multi_line_pressure_process.cpp | 7 + ...t_phreatic_multi_line_pressure_process.cpp | 143 ++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 applications/GeoMechanicsApplication/tests/cpp_tests/test_apply_constant_phreatic_multi_line_pressure_process.cpp diff --git a/applications/GeoMechanicsApplication/custom_processes/apply_constant_phreatic_multi_line_pressure_process.cpp b/applications/GeoMechanicsApplication/custom_processes/apply_constant_phreatic_multi_line_pressure_process.cpp index a27037a860ce..f113e2b554f8 100644 --- a/applications/GeoMechanicsApplication/custom_processes/apply_constant_phreatic_multi_line_pressure_process.cpp +++ b/applications/GeoMechanicsApplication/custom_processes/apply_constant_phreatic_multi_line_pressure_process.cpp @@ -81,6 +81,13 @@ void ApplyConstantPhreaticMultiLinePressureProcess::ValidateCoordinates(const Pa if (!std::is_sorted(HorizontalDirectionCoordinates().begin(), HorizontalDirectionCoordinates().end())) { KRATOS_ERROR << "The Horizontal Elements Coordinates are not ordered." << rParameters << std::endl; } + + KRATOS_ERROR_IF(GravityDirectionCoordinates().size() < 2) + << "At least two coordinates in gravity direction must be given, but got " + << GravityDirectionCoordinates().size() << std::endl; + KRATOS_ERROR_IF(HorizontalDirectionCoordinates().size() < 2) + << "At least two coordinates in horizontal direction must be given, but got " + << HorizontalDirectionCoordinates().size() << std::endl; } void ApplyConstantPhreaticMultiLinePressureProcess::InitializeCoordinates(const Parameters& rParameters) diff --git a/applications/GeoMechanicsApplication/tests/cpp_tests/test_apply_constant_phreatic_multi_line_pressure_process.cpp b/applications/GeoMechanicsApplication/tests/cpp_tests/test_apply_constant_phreatic_multi_line_pressure_process.cpp new file mode 100644 index 000000000000..53f2bcd1045e --- /dev/null +++ b/applications/GeoMechanicsApplication/tests/cpp_tests/test_apply_constant_phreatic_multi_line_pressure_process.cpp @@ -0,0 +1,143 @@ +// KRATOS___ +// // ) ) +// // ___ ___ +// // ____ //___) ) // ) ) +// // / / // // / / +// ((____/ / ((____ ((___/ / MECHANICS +// +// License: geo_mechanics_application/license.txt +// +// Main authors: Anne van de Graaf +// +#include "containers/model.h" +#include "custom_processes/apply_constant_phreatic_multi_line_pressure_process.h" +#include "geo_mechanics_fast_suite.h" +#include "includes/checks.h" + +using namespace Kratos; + +namespace +{ + +bool CanCreateInstanceOfApplyConstantPhreaticMultiLinePressureProcessWithoutFailure(ModelPart& rModelPart, + const Parameters& rProcessParameters) +{ + try { + ApplyConstantPhreaticMultiLinePressureProcess{rModelPart, rProcessParameters}; + } catch (const Exception&) { + return false; + } + + return true; +} + +} // namespace + +namespace Kratos::Testing +{ + +KRATOS_TEST_CASE_IN_SUITE(ApplyConstantPhreaticMultiLinePressureProcessThrowsWhenLessThanTwoCoordinatesInGravityDirectionAreGiven, + KratosGeoMechanicsFastSuiteWithoutKernel) +{ + auto model = Model{}; + auto& r_model_part = model.CreateModelPart("foo"); + auto test_parameters = Parameters{R"( + { + "model_part_name": "foo", + "variable_name": "WATER_PRESSURE", + "x_coordinates": [0.0, 1.0], + "y_coordinates": [], + "z_coordinates": [0.0, 0.0], + "gravity_direction": 1 + } )"}; + + KRATOS_EXPECT_EXCEPTION_IS_THROWN( + (ApplyConstantPhreaticMultiLinePressureProcess{r_model_part, test_parameters}), + "At least two coordinates in gravity direction must be given, but got 0") + + test_parameters.RemoveValue("y_coordinates"); + test_parameters.AddVector("y_coordinates", ScalarVector{1, 0.0}); + KRATOS_EXPECT_EXCEPTION_IS_THROWN( + (ApplyConstantPhreaticMultiLinePressureProcess{r_model_part, test_parameters}), + "At least two coordinates in gravity direction must be given, but got 1") +} + +KRATOS_TEST_CASE_IN_SUITE(ApplyConstantPhreaticMultiLinePressureProcessThrowsWhenLessThanTwoCoordinatesInHorizontalDirectionAreGiven, + KratosGeoMechanicsFastSuiteWithoutKernel) +{ + auto model = Model{}; + auto& r_model_part = model.CreateModelPart("foo"); + auto test_parameters = Parameters{R"( + { + "model_part_name": "foo", + "variable_name": "WATER_PRESSURE", + "x_coordinates": [], + "y_coordinates": [0.0, 1.0], + "z_coordinates": [0.0, 0.0], + "gravity_direction": 1, + "out_of_plane_direction": 2 + } )"}; + + KRATOS_EXPECT_EXCEPTION_IS_THROWN( + (ApplyConstantPhreaticMultiLinePressureProcess{r_model_part, test_parameters}), + "At least two coordinates in horizontal direction must be given, but got 0") + + test_parameters.RemoveValue("x_coordinates"); + test_parameters.AddVector("x_coordinates", ScalarVector{1, 0.0}); + KRATOS_EXPECT_EXCEPTION_IS_THROWN( + (ApplyConstantPhreaticMultiLinePressureProcess{r_model_part, test_parameters}), + "At least two coordinates in horizontal direction must be given, but got 1") +} + +KRATOS_TEST_CASE_IN_SUITE(ApplyConstantPhreaticMultiLinePressureProcessDoesNotThrowWhenAtLeastTwoCoordinatesInGravityDirectionAreGiven, + KratosGeoMechanicsFastSuiteWithoutKernel) +{ + auto model = Model{}; + auto& r_model_part = model.CreateModelPart("foo"); + auto test_parameters = Parameters{R"( + { + "model_part_name": "foo", + "variable_name": "WATER_PRESSURE", + "x_coordinates": [0.0, 1.0], + "y_coordinates": [1.0, 2.0], + "z_coordinates": [0.0, 0.0], + "gravity_direction": 1 + } )"}; + + KRATOS_EXPECT_TRUE(CanCreateInstanceOfApplyConstantPhreaticMultiLinePressureProcessWithoutFailure( + r_model_part, test_parameters)) + + test_parameters.RemoveValue("y_coordinates"); + test_parameters.AddVector("y_coordinates", ScalarVector{5, 1.0}); + + KRATOS_EXPECT_TRUE(CanCreateInstanceOfApplyConstantPhreaticMultiLinePressureProcessWithoutFailure( + r_model_part, test_parameters)) +} + +KRATOS_TEST_CASE_IN_SUITE(ApplyConstantPhreaticMultiLinePressureProcessDoesNotThrowWhenAtLeastTwoCoordinatesInHorizontalDirectionAreGiven, + KratosGeoMechanicsFastSuiteWithoutKernel) +{ + auto model = Model{}; + auto& r_model_part = model.CreateModelPart("foo"); + auto test_parameters = Parameters{R"( + { + "model_part_name": "foo", + "variable_name": "WATER_PRESSURE", + "x_coordinates": [1.0, 2.0], + "y_coordinates": [0.0, 1.0], + "z_coordinates": [0.0, 0.0], + "gravity_direction": 1, + "out_of_plane_direction": 2 + } )"}; + + KRATOS_EXPECT_TRUE(CanCreateInstanceOfApplyConstantPhreaticMultiLinePressureProcessWithoutFailure( + r_model_part, test_parameters)) + + test_parameters.RemoveValue("x_coordinates"); + test_parameters.AddVector("x_coordinates", ScalarVector{5, 1.0}); + + KRATOS_EXPECT_TRUE(CanCreateInstanceOfApplyConstantPhreaticMultiLinePressureProcessWithoutFailure( + r_model_part, test_parameters)) +} + +} // namespace Kratos::Testing \ No newline at end of file