Skip to content

Commit

Permalink
Moved math regression test to be true unit tests.
Browse files Browse the repository at this point in the history
Moved matrix operations out of the quaternion operations file for better organization
  • Loading branch information
ddement committed Oct 22, 2024
1 parent 8c3d1e5 commit 8defc03
Show file tree
Hide file tree
Showing 13 changed files with 569 additions and 360 deletions.
1 change: 1 addition & 0 deletions src/constraints/calculate_fixed_bc_constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "constraints.hpp"

#include "src/math/matrix_operations.hpp"
#include "src/math/quaternion_operations.hpp"
#include "src/math/vector_operations.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/constraints/calculate_prescribed_bc_constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "constraints.hpp"

#include "src/math/matrix_operations.hpp"
#include "src/math/quaternion_operations.hpp"
#include "src/math/vector_operations.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/constraints/calculate_rigid_joint_constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "constraints.hpp"

#include "src/math/matrix_operations.hpp"
#include "src/math/quaternion_operations.hpp"
#include "src/math/vector_operations.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/constraints/calculate_rotation_control_constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "constraints.hpp"

#include "src/math/matrix_operations.hpp"
#include "src/math/quaternion_operations.hpp"
#include "src/math/vector_operations.hpp"

Expand Down
27 changes: 27 additions & 0 deletions src/math/matrix_operations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <Kokkos_Core.hpp>

/// Computes AX(A) of a square matrix
template <typename Matrix>
KOKKOS_INLINE_FUNCTION void AX_Matrix(const Matrix& A, const Matrix& AX_A) {
double trace = 0.;
for (int i = 0; i < A.extent_int(0); ++i) {
trace += A(i, i);
}
trace /= 2.;
for (int i = 0; i < A.extent_int(0); ++i) {
for (int j = 0; j < A.extent_int(1); ++j) {
AX_A(i, j) = -A(i, j) / 2.;
}
AX_A(i, i) += trace;
}
}

/// Computes the axial vector of a 3x3 rotation matrix
template <typename Matrix, typename Vector>
KOKKOS_INLINE_FUNCTION void AxialVectorOfMatrix(const Matrix& m, const Vector& v) {
v(0) = (m(2, 1) - m(1, 2)) / 2.;
v(1) = (m(0, 2) - m(2, 0)) / 2.;
v(2) = (m(1, 0) - m(0, 1)) / 2.;
}
24 changes: 0 additions & 24 deletions src/math/quaternion_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,6 @@ inline Array_4 QuaternionCompose(const Array_4& q1, const Array_4& q2) {
return qn;
}

/// Computes the axial vector of a 3x3 rotation matrix
template <typename Matrix, typename Vector>
KOKKOS_INLINE_FUNCTION void AxialVectorOfMatrix(const Matrix& m, const Vector& v) {
v(0) = (m(2, 1) - m(1, 2)) / 2.;
v(1) = (m(0, 2) - m(2, 0)) / 2.;
v(2) = (m(1, 0) - m(0, 1)) / 2.;
}

/// Computes AX(A) of a square matrix
template <typename Matrix>
KOKKOS_INLINE_FUNCTION void AX_Matrix(const Matrix& A, const Matrix& AX_A) {
double trace = 0.;
for (int i = 0; i < A.extent_int(0); ++i) {
trace += A(i, i);
}
trace /= 2.;
for (int i = 0; i < A.extent_int(0); ++i) {
for (int j = 0; j < A.extent_int(1); ++j) {
AX_A(i, j) = -A(i, j) / 2.;
}
AX_A(i, i) += trace;
}
}

/// Returns a 4-D quaternion from provided 3-D rotation vector, i.e. the exponential map
template <typename Vector, typename Quaternion>
KOKKOS_INLINE_FUNCTION void RotationVectorToQuaternion(
Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_executable(openturbine_unit_tests)
# Add subdirectories for additional components
add_subdirectory(beams)
add_subdirectory(external)
add_subdirectory(math)
add_subdirectory(model)
add_subdirectory(regression)
add_subdirectory(solver)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit_tests/math/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Specify the source files for the unit test executable
target_sources(
openturbine_unit_tests
PRIVATE
test_matrix_operations.cpp
test_quaternion_operations.cpp
test_vector_operations.cpp
)
63 changes: 63 additions & 0 deletions tests/unit_tests/math/test_matrix_operations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>

#include "src/math/matrix_operations.hpp"

namespace openturbine::tests {

template <unsigned rows, unsigned cols>
Kokkos::View<double[rows][cols]> Create2DView(const std::array<double, rows*cols>& input) {
auto view = Kokkos::View<double[rows][cols]>("view");
auto view_host = Kokkos::View<const double[rows][cols], Kokkos::HostSpace>(input.data());
auto view_mirror = Kokkos::create_mirror(view);
Kokkos::deep_copy(view_mirror, view_host);
Kokkos::deep_copy(view, view_mirror);
return view;
}

inline void test_AX_Matrix() {
const auto A = Create2DView<3, 3>({1., 2., 3., 4., 5., 6., 7., 8., 9.});
const auto out = Kokkos::View<double[3][3]>("out");
Kokkos::parallel_for(1, KOKKOS_LAMBDA(int) { AX_Matrix(A, out); });
const auto out_mirror = Kokkos::create_mirror(out);
Kokkos::deep_copy(out_mirror, out);

constexpr auto expected_data = std::array{7., -1., -1.5, -2., 5., -3., -3.5, -4., 3.};
const auto expected = Kokkos::View<double[3][3], Kokkos::HostSpace>::const_type(expected_data.data());

for(auto i = 0U; i < 3U; ++i) {
for(auto j = 0U; j < 3U; ++j) {
EXPECT_NEAR(out_mirror(i, j), expected(i, j), 1.e-15);
}
}
}

TEST(MatrixTest, AX_Matrix) {
test_AX_Matrix();
}


Kokkos::View<double[3]> TestAxialVectorOfMatrix(const Kokkos::View<const double[3][3]>& m) {
auto v = Kokkos::View<double[3]>("v");
Kokkos::parallel_for(
"AxialVectorOfMatrix", 1, KOKKOS_LAMBDA(int) { AxialVectorOfMatrix(m, v); }
);
return v;
}

TEST(MatrixTest, AxialVectorOfMatrix) {
const auto m = Create2DView<3, 3>({0., -1., 0., 1., 0., 0., 0., 0., 0.});
const auto v = TestAxialVectorOfMatrix(m);

const auto v_mirror = Kokkos::create_mirror(v);
Kokkos::deep_copy(v_mirror, v);

constexpr auto expected_data = std::array{0., 0., 1.};
const auto expected = Kokkos::View<double[3], Kokkos::HostSpace>::const_type(expected_data.data());

for(auto i = 0U; i < 3U; ++i) {
EXPECT_NEAR(v_mirror(i), expected(i), 1.e-15);
}
}

}
Loading

0 comments on commit 8defc03

Please sign in to comment.