Skip to content

Commit

Permalink
Refactor ConstraintType and helper functions to enhance clarity + uni…
Browse files Browse the repository at this point in the history
…t test coverage
  • Loading branch information
faisal-bhuiyan committed Dec 30, 2024
1 parent fc9eb02 commit f3c2359
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 264 deletions.
38 changes: 25 additions & 13 deletions src/constraints/constraint_type.hpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
#pragma once

#include <stdexcept>

namespace openturbine {

enum class ConstraintType : std::uint8_t {
kNone = 0, //< No constraint -- default
kFixedBC = 1, //< Fixed boundary condition/clamped constraint -- all DOFs fixed
kPrescribedBC = 2, //< Prescribed boundary condition -- displacement and rotation are
// specified i.e. all DOFs are defined
kNone = 0, //< No constraint -- default type
kFixedBC = 1, //< Fixed boundary condition/clamped constraint -- all DOFs fixed at
//< the node where the constraint is applied
kPrescribedBC = 2, //< Prescribed boundary condition -- displacement and orientation values
//< are specified => all DOFs are defined at the node
kRigidJoint = 3, //< Rigid constraint between two nodes -- no relative motion permitted
// between the nodes i.e. all DOFs of target node are constrained
//< between the nodes => all DOFs of target node are constrained
kRevoluteJoint = 4, //< Target node rotates freely around a specified axis --
// all but one DOFs are constrained
kRotationControl = 5, //< A rotation is specified about a given axis -- 1 DOF allowed about
// the axis specified by the user, the other 5 DOFs are constrained
//< all but one DOFs are constrained
kRotationControl = 5, //< A rotation is specified about a given axis and other DOFs
//< are constrained => all DOFs are constrained/specified
};

/// Returns the number of nodes used/required by the constraint type
KOKKOS_INLINE_FUNCTION
constexpr size_t GetNumberOfNodes(ConstraintType t) {
// Rigid joint, revolute joint, and rotation control have two nodes
// Rigid joint/revolute joint/rotation control constraints require two nodes
const auto has_two_nodes = t == ConstraintType::kRigidJoint ||
t == ConstraintType::kRevoluteJoint ||
t == ConstraintType::kRotationControl;
// Default is one node (fixed and prescribed BCs)
return 1U + static_cast<size_t>(has_two_nodes);
}

/// Returns the number of degrees of freedom used/fixed by the constraint type
/// Returns the number of degrees of freedom prescribed/fixed by the constraint type
KOKKOS_INLINE_FUNCTION
constexpr size_t NumDOFsForConstraint(ConstraintType type) {
switch (type) {
case ConstraintType::kRevoluteJoint: {
case ConstraintType::kRevoluteJoint:
return 5U; // A revolute joint constraint fixes 5 DOFs
} break;
case ConstraintType::kNone:
[[fallthrough]];
case ConstraintType::kFixedBC:
[[fallthrough]];
case ConstraintType::kPrescribedBC:
[[fallthrough]];
case ConstraintType::kRigidJoint:
[[fallthrough]];
case ConstraintType::kRotationControl:
return 6U; // All other constraints fix 6 DOFs
default:
return static_cast<size_t>(6U); // Default: Fixes 6 DOFs
throw std::runtime_error("Invalid constraint type");
}
}

Expand Down
251 changes: 0 additions & 251 deletions src/constraints/constraints.rs

This file was deleted.

1 change: 1 addition & 0 deletions tests/unit_tests/constraints/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ target_sources(
test_calculate_revolute_joint_output.cpp
test_calculate_rigid_joint_constraint.cpp
test_calculate_rotation_control_constraint.cpp
test_constraint_type.cpp
)
35 changes: 35 additions & 0 deletions tests/unit_tests/constraints/test_constraint_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <Kokkos_Core.hpp>
#include <gtest/gtest.h>

#include "src/constraints/constraint_type.hpp"

namespace openturbine::tests {

TEST(ConstraintTypeTest, GetNumberOfNodesReturnsCorrectCount) {
// Single node constraints
EXPECT_EQ(GetNumberOfNodes(ConstraintType::kNone), 1U);
EXPECT_EQ(GetNumberOfNodes(ConstraintType::kFixedBC), 1U);
EXPECT_EQ(GetNumberOfNodes(ConstraintType::kPrescribedBC), 1U);

// Two node constraints
EXPECT_EQ(GetNumberOfNodes(ConstraintType::kRigidJoint), 2U);
EXPECT_EQ(GetNumberOfNodes(ConstraintType::kRevoluteJoint), 2U);
EXPECT_EQ(GetNumberOfNodes(ConstraintType::kRotationControl), 2U);
}

TEST(ConstraintTypeTest, NumDOFsForConstraintReturnsCorrectCount) {
// Special case: Revolute joint has 5 DOFs
EXPECT_EQ(NumDOFsForConstraint(ConstraintType::kRevoluteJoint), 5U);

// All other constraints have 6 DOFs
EXPECT_EQ(NumDOFsForConstraint(ConstraintType::kNone), 6U);
EXPECT_EQ(NumDOFsForConstraint(ConstraintType::kFixedBC), 6U);
EXPECT_EQ(NumDOFsForConstraint(ConstraintType::kPrescribedBC), 6U);
EXPECT_EQ(NumDOFsForConstraint(ConstraintType::kRigidJoint), 6U);
EXPECT_EQ(NumDOFsForConstraint(ConstraintType::kRotationControl), 6U);

// Expect a runtime error for an invalid constraint type
EXPECT_THROW(NumDOFsForConstraint(static_cast<ConstraintType>(100)), std::runtime_error);
}

} // namespace openturbine::tests

0 comments on commit f3c2359

Please sign in to comment.