Skip to content

Commit

Permalink
Merge pull request #322 from Exawind/add_masses_unit_tests
Browse files Browse the repository at this point in the history
Added unit tests for the kernels computing the system variables for masses
  • Loading branch information
ddement authored Dec 19, 2024
2 parents 5b03a22 + 47c5be4 commit 14b21d5
Show file tree
Hide file tree
Showing 12 changed files with 694 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/unit_tests/system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target_sources(

add_subdirectory(springs)
add_subdirectory(beams)
add_subdirectory(masses)
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/unit_tests/system/masses/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Specify the source files for the unit test executable
target_sources(
openturbine_unit_tests
PRIVATE
test_update_node_state.cpp
test_calculate_RR0.cpp
test_rotate_section_matrix.cpp
test_calculate_mass_matrix_components.cpp
test_calculate_inertial_forces.cpp
test_calculate_gravity_force.cpp
test_calculate_gyroscopic_matrix.cpp
test_calculate_inertia_stiffness_matrix.cpp
)
63 changes: 63 additions & 0 deletions tests/unit_tests/system/masses/test_calculate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>

namespace openturbine::tests {

inline void CompareWithExpected(
const Kokkos::View<const double**>::host_mirror_type& result,
const Kokkos::View<const double**, Kokkos::HostSpace>& expected
) {
for (auto i = 0U; i < result.extent(0); ++i) {
for (auto j = 0U; j < result.extent(1); ++j) {
EXPECT_DOUBLE_EQ(result(i, j), expected(i, j));
}
}
}

inline void CompareWithExpected(
const Kokkos::View<const double***>::host_mirror_type& result,
const Kokkos::View<const double***, Kokkos::HostSpace>& expected
) {
for (auto i = 0U; i < result.extent(0); ++i) {
for (auto j = 0U; j < result.extent(1); ++j) {
for (auto k = 0U; k < result.extent(2); ++k) {
EXPECT_DOUBLE_EQ(result(i, j, k), expected(i, j, k));
}
}
}
}

inline void CompareWithExpected(
const Kokkos::View<const double****>::host_mirror_type& result,
const Kokkos::View<const double****, Kokkos::HostSpace>& expected
) {
for (auto i = 0U; i < result.extent(0); ++i) {
for (auto j = 0U; j < result.extent(1); ++j) {
for (auto k = 0U; k < result.extent(2); ++k) {
for (auto l = 0U; l < result.extent(3); ++l) {
EXPECT_DOUBLE_EQ(result(i, j, k, l), expected(i, j, k, l));
}
}
}
}
}

inline void CompareWithExpected(
const Kokkos::View<const double*****>::host_mirror_type& result,
const Kokkos::View<const double*****, Kokkos::HostSpace>& expected
) {
for (auto i = 0U; i < result.extent(0); ++i) {
for (auto j = 0U; j < result.extent(1); ++j) {
for (auto k = 0U; k < result.extent(2); ++k) {
for (auto l = 0U; l < result.extent(3); ++l) {
for (auto m = 0U; m < result.extent(4); ++m) {
EXPECT_DOUBLE_EQ(result(i, j, k, l, m), expected(i, j, k, l, m));
}
}
}
}
}
}
} // namespace openturbine::tests
44 changes: 44 additions & 0 deletions tests/unit_tests/system/masses/test_calculate_RR0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>

#include "test_calculate.hpp"

#include "src/system/masses/calculate_RR0.hpp"

namespace openturbine::tests {

struct ExecuteCalculateRR0 {
size_t i_elem;
Kokkos::View<double[1][7]>::const_type x;
Kokkos::View<double[1][6][6]> rr0;

KOKKOS_FUNCTION
void operator()(size_t) const { masses::CalculateRR0{i_elem, x, rr0}(); }
};

TEST(CalculateRR0MassesTests, OneNode) {
const auto x = Kokkos::View<double[1][7]>("x");
constexpr auto x_host_data = std::array{1., 2., 3., 4., 5., 6., 7.};
const auto x_host = Kokkos::View<const double[1][7], Kokkos::HostSpace>(x_host_data.data());
const auto x_mirror = Kokkos::create_mirror(x);
Kokkos::deep_copy(x_mirror, x_host);
Kokkos::deep_copy(x, x_mirror);

const auto rr0 = Kokkos::View<double[1][6][6]>("rr0");

Kokkos::parallel_for("CalculateRR0", 1, ExecuteCalculateRR0{0, x, rr0});

constexpr auto expected_rr0_data = std::array{-44., 4., 118., 0., 0., 0., //
116., -22., 44., 0., 0., 0., //
22., 124., 4., 0., 0., 0., //
0., 0., 0., -44., 4., 118., //
0., 0., 0., 116., -22., 44., //
0., 0., 0., 22., 124., 4.};
const auto expected_rr0 =
Kokkos::View<const double[1][6][6], Kokkos::HostSpace>(expected_rr0_data.data());

const auto rr0_mirror = Kokkos::create_mirror(rr0);
Kokkos::deep_copy(rr0_mirror, rr0);
CompareWithExpected(rr0_mirror, expected_rr0);
}
} // namespace openturbine::tests
62 changes: 62 additions & 0 deletions tests/unit_tests/system/masses/test_calculate_gravity_force.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>

#include "test_calculate.hpp"

#include "src/system/masses/calculate_gravity_force.hpp"

namespace openturbine::tests {

struct ExecuteCalculateGravityForce {
size_t i_elem;
Kokkos::View<double[3]>::const_type gravity;
Kokkos::View<double* [6][6]>::const_type Muu;
Kokkos::View<double* [3][3]>::const_type eta_tilde;
Kokkos::View<double* [6]> FG;

KOKKOS_FUNCTION
void operator()(size_t) const {
masses::CalculateGravityForce{i_elem, gravity, Muu, eta_tilde, FG}();
}
};

TEST(CalculateGravityForceTestsMasses, OneNode) {
const auto Muu = Kokkos::View<double[1][6][6]>("Muu");
constexpr auto Muu_data = std::array{1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.,
25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36.};
const auto Muu_host = Kokkos::View<const double[1][6][6], Kokkos::HostSpace>(Muu_data.data());
const auto Muu_mirror = Kokkos::create_mirror(Muu);
Kokkos::deep_copy(Muu_mirror, Muu_host);
Kokkos::deep_copy(Muu, Muu_mirror);

const auto eta_tilde = Kokkos::View<double[1][3][3]>("eta_tilde");
constexpr auto eta_tilde_data = std::array{37., 38., 39., 40., 41., 42., 43., 44., 45.};
const auto eta_tilde_host =
Kokkos::View<const double[1][3][3], Kokkos::HostSpace>(eta_tilde_data.data());
const auto eta_tilde_mirror = Kokkos::create_mirror(eta_tilde);
Kokkos::deep_copy(eta_tilde_mirror, eta_tilde_host);
Kokkos::deep_copy(eta_tilde, eta_tilde_mirror);

const auto gravity = Kokkos::View<double[3]>("gravity");
constexpr auto gravity_data = std::array{46., 47., 48.};
const auto gravity_host = Kokkos::View<const double[3], Kokkos::HostSpace>(gravity_data.data());
const auto gravity_mirror = Kokkos::create_mirror(gravity);
Kokkos::deep_copy(gravity_mirror, gravity_host);
Kokkos::deep_copy(gravity, gravity_mirror);

const auto FG = Kokkos::View<double[1][6]>("FG");

Kokkos::parallel_for(
"CalculateGravityForce", 1, ExecuteCalculateGravityForce{0, gravity, Muu, eta_tilde, FG}
);

constexpr auto FG_exact_data = std::array{46., 47., 48., 5360., 5783., 6206.};
const auto FG_exact = Kokkos::View<const double[1][6], Kokkos::HostSpace>(FG_exact_data.data());

const auto FG_mirror = Kokkos::create_mirror(FG);
Kokkos::deep_copy(FG_mirror, FG);
CompareWithExpected(FG_mirror, FG_exact);
}

} // namespace openturbine::tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>

#include "test_calculate.hpp"

#include "src/system/masses/calculate_gyroscopic_matrix.hpp"

namespace openturbine::tests {

struct ExecuteCalculateGyroscopicMatrix {
size_t i_elem;
Kokkos::View<double[1][6][6]>::const_type Muu;
Kokkos::View<double[1][3]>::const_type omega;
Kokkos::View<double[1][3][3]>::const_type omega_tilde;
Kokkos::View<double[1][3][3]>::const_type rho;
Kokkos::View<double[1][3]>::const_type eta;
Kokkos::View<double[1][6][6]> Guu;

KOKKOS_FUNCTION
void operator()(size_t) const {
masses::CalculateGyroscopicMatrix{i_elem, Muu, omega, omega_tilde, rho, eta, Guu}();
}
};

TEST(CalculateGyroscopicMatrixMassesTests, OneNode) {
const auto Muu = Kokkos::View<double[1][6][6]>("Muu");
constexpr auto Muu_data = std::array{1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.,
25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36.};
const auto Muu_host = Kokkos::View<const double[1][6][6], Kokkos::HostSpace>(Muu_data.data());
const auto Muu_mirror = Kokkos::create_mirror(Muu);
Kokkos::deep_copy(Muu_mirror, Muu_host);
Kokkos::deep_copy(Muu, Muu_mirror);

const auto omega = Kokkos::View<double[1][3]>("omega");
constexpr auto omega_data = std::array{40., 41., 42.};
const auto omega_host = Kokkos::View<const double[1][3], Kokkos::HostSpace>(omega_data.data());
const auto omega_mirror = Kokkos::create_mirror(omega);
Kokkos::deep_copy(omega_mirror, omega_host);
Kokkos::deep_copy(omega, omega_mirror);

const auto omega_tilde = Kokkos::View<double[1][3][3]>("omega_tilde");
constexpr auto omega_tilde_data = std::array{46., 47., 48., 49., 50., 51., 52., 53., 54.};
const auto omega_tilde_host =
Kokkos::View<const double[1][3][3], Kokkos::HostSpace>(omega_tilde_data.data());
const auto omega_tilde_mirror = Kokkos::create_mirror(omega_tilde);
Kokkos::deep_copy(omega_tilde_mirror, omega_tilde_host);
Kokkos::deep_copy(omega_tilde, omega_tilde_mirror);

const auto rho = Kokkos::View<double[1][3][3]>("rho");
constexpr auto rho_data = std::array{55., 56., 57., 58., 59., 60., 61., 62., 63.};
const auto rho_host = Kokkos::View<const double[1][3][3], Kokkos::HostSpace>(rho_data.data());
const auto rho_mirror = Kokkos::create_mirror(rho);
Kokkos::deep_copy(rho_mirror, rho_host);
Kokkos::deep_copy(rho, rho_mirror);

const auto eta = Kokkos::View<double[1][3]>("eta");
constexpr auto eta_data = std::array{64., 65., 66.};
const auto eta_host = Kokkos::View<const double[1][3], Kokkos::HostSpace>(eta_data.data());
const auto eta_mirror = Kokkos::create_mirror(eta);
Kokkos::deep_copy(eta_mirror, eta_host);
Kokkos::deep_copy(eta, eta_mirror);

const auto Guu = Kokkos::View<double[1][6][6]>("Guu");

Kokkos::parallel_for(
"CalculateGyroscopicMatrix", 1,
ExecuteCalculateGyroscopicMatrix{0, Muu, omega, omega_tilde, rho, eta, Guu}
);

constexpr auto Guu_exact_data =
std::array{0., 0., 0., 18., 10301., -9734., 0., 0., 0., -10322., -30., 9182.,
0., 0., 0., 9764., -9191., 12., 0., 0., 0., 8184., 15953., 1207.,
0., 0., 0., 1078., 8856., 15896., 0., 0., 0., 16487., 2497., 9546.};
const auto Guu_exact =
Kokkos::View<const double[1][6][6], Kokkos::HostSpace>(Guu_exact_data.data());

const auto Guu_mirror = Kokkos::create_mirror(Guu);
Kokkos::deep_copy(Guu_mirror, Guu);
CompareWithExpected(Guu_mirror, Guu_exact);
}

} // namespace openturbine::tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>

#include "test_calculate.hpp"

#include "src/system/masses/calculate_inertia_stiffness_matrix.hpp"

namespace openturbine::tests {

struct ExecuteCalculateInertiaStiffnessMatrix {
size_t i_elem;
Kokkos::View<double[1][6][6]>::const_type Muu;
Kokkos::View<double[1][3]>::const_type u_ddot;
Kokkos::View<double[1][3]>::const_type omega;
Kokkos::View<double[1][3]>::const_type omega_dot;
Kokkos::View<double[1][3][3]>::const_type omega_tilde;
Kokkos::View<double[1][3][3]>::const_type omega_dot_tilde;
Kokkos::View<double[1][3][3]>::const_type rho;
Kokkos::View<double[1][3]>::const_type eta;
Kokkos::View<double[1][6][6]> Kuu;

KOKKOS_FUNCTION
void operator()(size_t) const {
masses::CalculateInertiaStiffnessMatrix{
i_elem, Muu, u_ddot, omega, omega_dot, omega_tilde, omega_dot_tilde, rho, eta, Kuu
}();
}
};

TEST(CalculateInertiaStiffnessMatrixMassesTests, OneNode) {
const auto Muu = Kokkos::View<double[1][6][6]>("Muu");
constexpr auto Muu_data = std::array{1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.,
25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36.};
const auto Muu_host = Kokkos::View<const double[1][6][6], Kokkos::HostSpace>(Muu_data.data());
const auto Muu_mirror = Kokkos::create_mirror(Muu);
Kokkos::deep_copy(Muu_mirror, Muu_host);
Kokkos::deep_copy(Muu, Muu_mirror);

const auto u_ddot = Kokkos::View<double[1][3]>("u_ddot");
constexpr auto u_ddot_data = std::array{37., 38., 39.};
const auto u_ddot_host = Kokkos::View<const double[1][3], Kokkos::HostSpace>(u_ddot_data.data());
const auto u_ddot_mirror = Kokkos::create_mirror(u_ddot);
Kokkos::deep_copy(u_ddot_mirror, u_ddot_host);
Kokkos::deep_copy(u_ddot, u_ddot_mirror);

const auto omega = Kokkos::View<double[1][3]>("omega");
constexpr auto omega_data = std::array{40., 41., 42.};
const auto omega_host = Kokkos::View<const double[1][3], Kokkos::HostSpace>(omega_data.data());
const auto omega_mirror = Kokkos::create_mirror(omega);
Kokkos::deep_copy(omega_mirror, omega_host);
Kokkos::deep_copy(omega, omega_mirror);

const auto omega_dot = Kokkos::View<double[1][3]>("omega_dot");
constexpr auto omega_dot_data = std::array{43., 44., 45.};
const auto omega_dot_host =
Kokkos::View<const double[1][3], Kokkos::HostSpace>(omega_dot_data.data());
const auto omega_dot_mirror = Kokkos::create_mirror(omega_dot);
Kokkos::deep_copy(omega_dot_mirror, omega_dot_host);
Kokkos::deep_copy(omega_dot, omega_dot_mirror);

const auto eta_tilde = Kokkos::View<double[1][3][3]>("eta_tilde");
constexpr auto eta_tilde_data = std::array{46., 47., 48., 49., 50., 51., 52., 53., 54.};
const auto eta_tilde_host =
Kokkos::View<const double[1][3][3], Kokkos::HostSpace>(eta_tilde_data.data());
const auto eta_tilde_mirror = Kokkos::create_mirror(eta_tilde);
Kokkos::deep_copy(eta_tilde_mirror, eta_tilde_host);
Kokkos::deep_copy(eta_tilde, eta_tilde_mirror);

const auto omega_tilde = Kokkos::View<double[1][3][3]>("omega_tilde");
constexpr auto omega_tilde_data = std::array{46., 47., 48., 49., 50., 51., 52., 53., 54.};
const auto omega_tilde_host =
Kokkos::View<const double[1][3][3], Kokkos::HostSpace>(omega_tilde_data.data());
const auto omega_tilde_mirror = Kokkos::create_mirror(omega_tilde);
Kokkos::deep_copy(omega_tilde_mirror, omega_tilde_host);
Kokkos::deep_copy(omega_tilde, omega_tilde_mirror);

const auto omega_dot_tilde = Kokkos::View<double[1][3][3]>("omega_dot_tilde");
constexpr auto omega_dot_tilde_data = std::array{55., 56., 57., 58., 59., 60., 61., 62., 63.};
const auto omega_dot_tilde_host =
Kokkos::View<const double[1][3][3], Kokkos::HostSpace>(omega_dot_tilde_data.data());
const auto omega_dot_tilde_mirror = Kokkos::create_mirror(omega_dot_tilde);
Kokkos::deep_copy(omega_dot_tilde_mirror, omega_dot_tilde_host);
Kokkos::deep_copy(omega_dot_tilde, omega_dot_tilde_mirror);

const auto rho = Kokkos::View<double[1][3][3]>("rho");
constexpr auto rho_data = std::array{64., 65., 66., 67., 68., 69., 70., 71., 72.};
const auto rho_host = Kokkos::View<const double[1][3][3], Kokkos::HostSpace>(rho_data.data());
const auto rho_mirror = Kokkos::create_mirror(rho);
Kokkos::deep_copy(rho_mirror, rho_host);
Kokkos::deep_copy(rho, rho_mirror);

const auto eta = Kokkos::View<double[1][3]>("eta");
constexpr auto eta_data = std::array{73., 74., 75.};
const auto eta_host = Kokkos::View<const double[1][3], Kokkos::HostSpace>(eta_data.data());
const auto eta_mirror = Kokkos::create_mirror(eta);
Kokkos::deep_copy(eta_mirror, eta_host);
Kokkos::deep_copy(eta, eta_mirror);

const auto Kuu = Kokkos::View<double[1][6][6]>("Kuu");

Kokkos::parallel_for(
"CalculateInertiaStiffnessMatrix", 1,
ExecuteCalculateInertiaStiffnessMatrix{
0, Muu, u_ddot, omega, omega_dot, omega_tilde, omega_dot_tilde, rho, eta, Kuu
}
);

constexpr auto Kuu_exact_data = std::array<double, 36>{
0., 0., 0., 3396., -6792., 3396., 0., 0., 0., 3609., -7218., 3609.,
0., 0., 0., 3822., -7644., 3822., 0., 0., 0., 1407766., 1481559., 1465326.,
0., 0., 0., 1496300., 1558384., 1576048., 0., 0., 0., 1604122., 1652877., 1652190.
};
const auto Kuu_exact =
Kokkos::View<const double[1][6][6], Kokkos::HostSpace>(Kuu_exact_data.data());

const auto Kuu_mirror = Kokkos::create_mirror(Kuu);
Kokkos::deep_copy(Kuu_mirror, Kuu);
CompareWithExpected(Kuu_mirror, Kuu_exact);
}

} // namespace openturbine::tests
Loading

0 comments on commit 14b21d5

Please sign in to comment.