Skip to content

Commit

Permalink
(#115) add float limitRange function, update models
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlharvey committed Sep 27, 2024
1 parent 32b1d28 commit 78b1c42
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 21 deletions.
3 changes: 1 addition & 2 deletions aspects/electrical/Converter/GunnsElectConverterInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ GunnsBasicLink::SolutionResult GunnsElectConverterInput::confirmSolutionAcceptab
} else {
/// - Sensors are optional; if a sensor exists then the trip uses its sensed value of the
/// truth parameter, otherwise the trip looks directly at the truth parameter.
float sensedVin = static_cast<float>(
MsMath::limitRange(static_cast<double>(-FLT_MAX), mPotentialVector[0], static_cast<double>(FLT_MAX)));
float sensedVin = MsMath::limitRange(-FLT_MAX, static_cast<float>(mPotentialVector[0]), FLT_MAX);

/// - Note that since we step the sensors without a time-step, its drift malfunction isn't
/// integrated. This is because we don't have the time-step in this function, and we must
Expand Down
6 changes: 2 additions & 4 deletions aspects/electrical/Converter/GunnsElectConverterOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,8 @@ GunnsBasicLink::SolutionResult GunnsElectConverterOutput::confirmSolutionAccepta

/// - Sensors are optional; if a sensor exists then the trip uses its sensed value of
/// the truth parameter, otherwise the trip looks directly at the truth parameter.
float sensedVout = static_cast<float>(
MsMath::limitRange(static_cast<double>(-FLT_MAX), mPotentialVector[0], static_cast<double>(FLT_MAX)));
float sensedIout = static_cast<float>(
MsMath::limitRange(static_cast<double>(-FLT_MAX), mFlux, static_cast<double>(FLT_MAX)));
float sensedVout = MsMath::limitRange(-FLT_MAX, static_cast<float>(mPotentialVector[0]), FLT_MAX);
float sensedIout = MsMath::limitRange(-FLT_MAX, static_cast<float>(mFlux), FLT_MAX);

/// - Note that since we step the sensors without a time-step, its drift malfunction
/// isn't integrated. This is because we don't have the time-step in this function,
Expand Down
2 changes: 1 addition & 1 deletion aspects/electrical/resistive/GunnsElectricalResistor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void GunnsElectricalResistor::validate() const
}

/// - Throw an exception if electrical efficiency <= 0 or electrical efficiency >= 1.
if (!MsMath::isInRange(static_cast<double>(FLT_EPSILON), mElectricalEfficiency, 1.0 - static_cast<double>(FLT_EPSILON))) {
if (!MsMath::isInRange(FLT_EPSILON, static_cast<float>(mElectricalEfficiency), 1.0 - FLT_EPSILON)) {
GUNNS_ERROR(TsInitializationException, "Invalid Configuration Data", "Electrical Efficiency outside valid range (0-1).");
}
}
Expand Down
24 changes: 24 additions & 0 deletions ms-utils/math/MsMath.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class MsMath
static double protectedDiv(const double num, const double den, const double threshold = DBL_EPSILON, const double retval = 0.0);
/// @brief Limits the value of a double argument to a specified range (lower <= x <= upper).
static double limitRange(const double lower, const double x, const double upper);
/// @brief Limits the value of a float argument to a specified range (lower <= x <= upper).
static float limitRange(const float lower, const float x, const float upper);
/// @brief Limits the value of an integer argument to a specified range (lower <= x <= upper).
static int limitRange(const int lower, const int x, const int upper);
/// @brief Is the double input within specified range (lower <= x <= upper)?
Expand Down Expand Up @@ -409,6 +411,28 @@ inline double MsMath::limitRange(const double lower, const double x, const doubl
return std::max(std::min(x, upper), lower);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// @param[in] lower (--) Lower limit of range.
/// @param[in] x (--) Input argument to be limited.
/// @param[in] upper (--) Upper limit of range.
///
/// @return float (--) The value limited to the specified range.
///
/// @warning If lower > upper, then lower is returned without complaint, so don't do that.
///
/// @details Limits the value of a float argument to the specified range (lower <= x <= upper).
/// \verbatim
/// _
/// | lower, x < lower
/// result = | x, lower <= x <= upper
/// |_ upper, x > upper
/// \endverbatim
////////////////////////////////////////////////////////////////////////////////////////////////////
inline float MsMath::limitRange(const float lower, const float x, const float upper)
{
return std::max(std::min(x, upper), lower);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// @param[in] lower (--) Lower limit of range.
/// @param[in] x (--) Input argument to be limited.
Expand Down
59 changes: 46 additions & 13 deletions ms-utils/math/test/UtMath.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/************************** TRICK HEADER ***********************************************************
/*
@copyright Copyright 2024 United States Government as represented by the Administrator of the
National Aeronautics and Space Administration. All Rights Reserved.
LIBRARY DEPENDENCY:
()
***************************************************************************************************/
*/

#include <iostream>

Expand Down Expand Up @@ -666,12 +663,48 @@ void UtMath::testLimitRangeDouble()
std::cout << "Pass";
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// @details Tests the method for range limiting floats.
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testLimitRangeFloat()
{
std::cout << "\n.Math Test 12: Limit Range Float Tests......................................";

/// @test for argument well within range
float expected = 1.0F;
float returned = MsMath::limitRange(0.0F, 1.0F, 2.0F);
CPPUNIT_ASSERT(expected == returned);

/// @test for argument at lower limit
returned = MsMath::limitRange(1.0F, 1.0F, 2.0F);
CPPUNIT_ASSERT(expected == returned);

/// @test for argument below lower limit
returned = MsMath::limitRange(1.0F, 0.0F, 2.0F);
CPPUNIT_ASSERT(expected == returned);

/// @test for argument at upper limit
returned = MsMath::limitRange(0.0F, 1.0F, 1.0F);
CPPUNIT_ASSERT(expected == returned);

/// @test for argument above upper limit
returned = MsMath::limitRange(0.0F, 2.0F, 1.0F);
CPPUNIT_ASSERT(expected == returned);

/// @test for upper < lower
expected = 2.0F;
returned = MsMath::limitRange(2.0F, 1.0F, 0.0F);
CPPUNIT_ASSERT(expected == returned);

std::cout << "Pass";
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// @details Tests the method for range limiting integers.
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testLimitRangeInt()
{
std::cout << "\n.Math Test 12: Limit Range Integer Tests....................................";
std::cout << "\n.Math Test 13: Limit Range Integer Tests....................................";

/// @test for argument well within range
double expected = 1.0;
Expand Down Expand Up @@ -711,7 +744,7 @@ void UtMath::testLimitRangeInt()
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testIsInRangeDouble()
{
std::cout << "\n.Math Test 13: Is In Range Double Tests.....................................";
std::cout << "\n.Math Test 14: Is In Range Double Tests.....................................";

/// @test for argument well within range
CPPUNIT_ASSERT(MsMath::isInRange(0.0, 1.0, 2.0));
Expand Down Expand Up @@ -740,7 +773,7 @@ void UtMath::testIsInRangeDouble()
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testIsInRangeInt()
{
std::cout << "\n.Math Test 14: Is In Range Integer Tests....................................";
std::cout << "\n.Math Test 15: Is In Range Integer Tests....................................";

/// @test for argument well within range
CPPUNIT_ASSERT(MsMath::isInRange(0, 1, 2));
Expand Down Expand Up @@ -769,7 +802,7 @@ void UtMath::testIsInRangeInt()
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testInnerLimitDouble()
{
std::cout << "\n.Math Test 15: Inner Limit Double Tests.....................................";
std::cout << "\n.Math Test 16: Inner Limit Double Tests.....................................";

/// @test for argument well below lower limit
double expected = -1.0;
Expand Down Expand Up @@ -824,7 +857,7 @@ void UtMath::testInnerLimitDouble()
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testInnerLimitInt()
{
std::cout << "\n.Math Test 16: Inner Limit Integer Tests....................................";
std::cout << "\n.Math Test 17: Inner Limit Integer Tests....................................";

/// @test for argument well below lower limit
int expected = -1;
Expand Down Expand Up @@ -869,7 +902,7 @@ void UtMath::testInnerLimitInt()
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testRounding()
{
std::cout << "\n.Math Test 17: Rounding Tests...............................................";
std::cout << "\n.Math Test 18: Rounding Tests...............................................";

/// @test for argument exactly equal to integer
int expected = 1;
Expand Down Expand Up @@ -911,7 +944,7 @@ void UtMath::testRounding()
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testQuantize()
{
std::cout << "\n.Math Test 18: Quantize Tests...............................................";
std::cout << "\n.Math Test 19: Quantize Tests...............................................";

double arg = 3.14159;
double expected = 3.0;
Expand Down Expand Up @@ -998,7 +1031,7 @@ void UtMath::testQuantize()
////////////////////////////////////////////////////////////////////////////////////////////////////
void UtMath::testFastPow()
{
std::cout << "\n.Math Test 19: fastPow Tests................................................";
std::cout << "\n.Math Test 20: fastPow Tests................................................";

double base = 0.0;
double exp = 1.25;
Expand Down
5 changes: 4 additions & 1 deletion ms-utils/math/test/UtMath.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// @defgroup UT_TSM_UTILITIES_MATH_MATH MsMath Unit Tests
/// @ingroup UT_TSM_UTILITIES_MATH
///
/// @copyright Copyright 2019 United States Government as represented by the Administrator of the
/// @copyright Copyright 2024 United States Government as represented by the Administrator of the
/// National Aeronautics and Space Administration. All Rights Reserved.
///
/// @details Unit Tests for the Math class.
Expand Down Expand Up @@ -54,6 +54,8 @@ class UtMath : public CppUnit::TestFixture
void testprotectedDiv();
/// @brief Test double limitRange method.
void testLimitRangeDouble();
/// @brief Test float limitRange method.
void testLimitRangeFloat();
/// @brief Test int limitRange method.
void testLimitRangeInt();
/// @brief Test double isInRange method.
Expand Down Expand Up @@ -83,6 +85,7 @@ class UtMath : public CppUnit::TestFixture
CPPUNIT_TEST(testProtectedLog);
CPPUNIT_TEST(testprotectedDiv);
CPPUNIT_TEST(testLimitRangeDouble);
CPPUNIT_TEST(testLimitRangeFloat);
CPPUNIT_TEST(testLimitRangeInt);
CPPUNIT_TEST(testIsInRangeDouble);
CPPUNIT_TEST(testIsInRangeInt);
Expand Down

0 comments on commit 78b1c42

Please sign in to comment.