From cba12071e94a62fe66be44d32fde399b320278c2 Mon Sep 17 00:00:00 2001 From: faisal-bhuiyan Date: Sun, 22 Dec 2024 18:43:14 -0700 Subject: [PATCH] Add a r-test for Springs to test out the springs forces under axial displacement --- .../springs/calculate_force_coefficients.hpp | 2 +- .../regression/CMakeLists.txt | 1 + .../regression/test_precession.cpp | 2 +- .../regression/test_springs_force.cpp | 97 +++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tests/regression_tests/regression/test_springs_force.cpp diff --git a/src/system/springs/calculate_force_coefficients.hpp b/src/system/springs/calculate_force_coefficients.hpp index c54aa044..f9bf1edf 100644 --- a/src/system/springs/calculate_force_coefficients.hpp +++ b/src/system/springs/calculate_force_coefficients.hpp @@ -20,7 +20,7 @@ struct CalculateForceCoefficients { KOKKOS_FUNCTION void operator()(int i_elem) const { // c1 = k * (l_ref/l - 1) - c1_(i_elem) = k_(i_elem) * (l_ref_(i_elem) / l_(i_elem) - 1.0); + c1_(i_elem) = k_(i_elem) * (l_ref_(i_elem) / l_(i_elem) - 1.); // c2 = k * l_ref/(l^3) c2_(i_elem) = k_(i_elem) * l_ref_(i_elem) / (l_(i_elem) * l_(i_elem) * l_(i_elem)); } diff --git a/tests/regression_tests/regression/CMakeLists.txt b/tests/regression_tests/regression/CMakeLists.txt index 90f6f4ab..74e16627 100644 --- a/tests/regression_tests/regression/CMakeLists.txt +++ b/tests/regression_tests/regression/CMakeLists.txt @@ -11,5 +11,6 @@ target_sources( test_rotor.cpp test_solver.cpp test_springs.cpp + test_springs_force.cpp test_utilities.cpp ) diff --git a/tests/regression_tests/regression/test_precession.cpp b/tests/regression_tests/regression/test_precession.cpp index 459e0df5..711056af 100644 --- a/tests/regression_tests/regression/test_precession.cpp +++ b/tests/regression_tests/regression/test_precession.cpp @@ -72,7 +72,7 @@ inline auto SetUpPrecessionTest() { create_element_freedom_table(elements, state); create_constraint_freedom_table(constraints, state); - [[maybe_unused]] auto solver = Solver( + auto solver = Solver( state.ID, state.node_freedom_allocation_table, state.node_freedom_map_table, elements.NumberOfNodesPerElement(), elements.NodeStateIndices(), constraints.num_dofs, constraints.type, constraints.base_node_freedom_table, constraints.target_node_freedom_table, diff --git a/tests/regression_tests/regression/test_springs_force.cpp b/tests/regression_tests/regression/test_springs_force.cpp new file mode 100644 index 00000000..dcf346e9 --- /dev/null +++ b/tests/regression_tests/regression/test_springs_force.cpp @@ -0,0 +1,97 @@ +#include + +#include "test_utilities.hpp" + +#include "src/elements/springs/create_springs.hpp" +#include "src/elements/springs/springs.hpp" +#include "src/model/model.hpp" +#include "src/step/update_system_variables_springs.hpp" + +namespace openturbine::tests { + +inline auto SetUpSpringsForceTest() { + auto model = Model(); + + // Add two nodes for the spring element + model.AddNode( + {0, 0, 0, 1, 0, 0, 0}, // First node at origin + {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0} + ); + model.AddNode( + {1, 0, 0, 1, 0, 0, 0}, // Second node at (1,0,0) + {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0} + ); + + const auto springs_input = SpringsInput({SpringElement( + std::array{model.GetNode(0), model.GetNode(1)}, + 10., // Spring stiffness coeff. + 1. // Undeformed length + )}); + + auto springs = CreateSprings(springs_input); + auto state = model.CreateState(); + + return std::make_tuple(springs, state); +} + +TEST(SpringsForceTest, ZeroDisplacement) { + auto [springs, state] = SetUpSpringsForceTest(); + + UpdateSystemVariablesSprings(springs, state); + + auto l_ref_host = Kokkos::create_mirror(springs.l_ref); + auto l_host = Kokkos::create_mirror(springs.l); + auto c1_host = Kokkos::create_mirror(springs.c1); + auto f_host = Kokkos::create_mirror(springs.f); + auto a_host = Kokkos::create_mirror(springs.a); + + Kokkos::deep_copy(l_ref_host, springs.l_ref); + Kokkos::deep_copy(l_host, springs.l); + Kokkos::deep_copy(c1_host, springs.c1); + Kokkos::deep_copy(f_host, springs.f); + Kokkos::deep_copy(a_host, springs.a); + + EXPECT_DOUBLE_EQ(l_ref_host(0), 1.); // Undeformed length = 1. + EXPECT_DOUBLE_EQ(l_host(0), 1.); // Current length = 1. + EXPECT_DOUBLE_EQ(c1_host(0), 0.); // c1 = 0. + expect_kokkos_view_1D_equal(Kokkos::subview(f_host, 0, Kokkos::ALL), {0., 0., 0.}); // No force + expect_kokkos_view_2D_equal( + Kokkos::subview(a_host, 0, Kokkos::ALL, Kokkos::ALL), + {{-10., 0., 0.}, {0., 0., 0.}, {0., 0., 0.}} + ); +} + +TEST(SpringsForceTest, UnitDisplacement) { + auto [springs, state] = SetUpSpringsForceTest(); + + state.q(1, 0) = 1.; // Displace second node in x direction + UpdateSystemVariablesSprings(springs, state); + + auto l_ref_host = Kokkos::create_mirror(springs.l_ref); + auto l_host = Kokkos::create_mirror(springs.l); + auto c1_host = Kokkos::create_mirror(springs.c1); + auto c2_host = Kokkos::create_mirror(springs.c2); + auto f_host = Kokkos::create_mirror(springs.f); + auto a_host = Kokkos::create_mirror(springs.a); + + Kokkos::deep_copy(l_ref_host, springs.l_ref); + Kokkos::deep_copy(l_host, springs.l); + Kokkos::deep_copy(c1_host, springs.c1); + Kokkos::deep_copy(c2_host, springs.c2); + Kokkos::deep_copy(f_host, springs.f); + Kokkos::deep_copy(a_host, springs.a); + + EXPECT_DOUBLE_EQ(l_ref_host(0), 1.); // Undeformed length = 1. + EXPECT_DOUBLE_EQ(l_host(0), 2.); // Current length = 2. + EXPECT_DOUBLE_EQ(c1_host(0), -5.); // c1 = -5. + EXPECT_DOUBLE_EQ(c2_host(0), 10. * 1. / std::pow(2., 3)); // c2 = 10. / 8. + expect_kokkos_view_1D_equal( + Kokkos::subview(f_host, 0, Kokkos::ALL), {-10., 0., 0.} + ); // Force = c1 * {r} + expect_kokkos_view_2D_equal( + Kokkos::subview(a_host, 0, Kokkos::ALL, Kokkos::ALL), + {{-10., 0., 0.}, {0., -5., 0.}, {0., 0., -5.}} + ); +} + +} // namespace openturbine::tests