Skip to content

Commit

Permalink
Clarify the code intent (#839)
Browse files Browse the repository at this point in the history

Co-authored-by: Sean McLeod <sean@seanmcleod.com>
  • Loading branch information
bcoconni and seanmcleod authored Feb 18, 2023
1 parent 0c519f8 commit ebb770e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 14 deletions.
18 changes: 18 additions & 0 deletions src/math/FGTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ double FGTable::GetValue(void) const

double FGTable::GetValue(double key) const
{
assert(nCols == 1);
assert(Data.size() == 2*nRows+2);
// If the key is off the end (or before the beginning) of the table, just
// return the boundary-table value, do not extrapolate.
Expand Down Expand Up @@ -520,6 +521,7 @@ double FGTable::GetValue(double rowKey, double colKey) const
{
if (nCols == 1) return GetValue(rowKey);

assert(Type == tt2D);
assert(Data.size() == (nCols+1)*(nRows+1));

unsigned int c = 2;
Expand Down Expand Up @@ -550,6 +552,7 @@ double FGTable::GetValue(double rowKey, double colKey) const

double FGTable::GetValue(double rowKey, double colKey, double tableKey) const
{
assert(Type == tt3D);
assert(Data.size() == nRows+1);
// If the key is off the end (or before the beginning) of the table, just
// return the boundary-table value, do not extrapolate.
Expand All @@ -575,6 +578,21 @@ double FGTable::GetValue(double rowKey, double colKey, double tableKey) const

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

double FGTable::GetMinValue(void) const
{
assert(Type == tt1D);
assert(Data.size() == 2*nRows+2);

double minValue = HUGE_VAL;

for(unsigned int i=1; i<=nRows; ++i)
minValue = std::min(minValue, Data[2*i+1]);

return minValue;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGTable::operator<<(istream& in_stream)
{
double x;
Expand Down
21 changes: 20 additions & 1 deletion src/math/FGTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,31 @@ class JSBSIM_API FGTable : public FGParameter, public FGJSBBase
/// The constructor for a table
FGTable (std::shared_ptr<FGPropertyManager> propMan, Element* el,
const std::string& prefix="");
FGTable (int );
FGTable (int);
FGTable (int, int);

/// Get the current table value
double GetValue(void) const;
/// @brief Get a value from a 1D internal table
/// @param key Row coordinate at which the value must be interpolated
/// @return The interpolated value
double GetValue(double key) const;
/// @brief Get a value from a 2D internal table
/// @param rowKey Row coordinate at which the value must be interpolated
/// @param colKey Column coordinate at which the value must be interpolated
/// @return The interpolated value
double GetValue(double rowKey, double colKey) const;
/// @brief Get a value from a 3D internal table
/// @param rowKey Row coordinate at which the value must be interpolated
/// @param colKey Column coordinate at which the value must be interpolated
/// @param TableKey Table coordinate at which the value must be interpolated
/// @return The interpolated value
double GetValue(double rowKey, double colKey, double TableKey) const;

double GetMinValue(void) const;
double GetMinValue(double colKey) const;
double GetMinValue(double colKey, double TableKey) const;

/** Read the table in.
Data in the config file should be in matrix format with the row
independents as the first column and the column independents in
Expand Down
11 changes: 6 additions & 5 deletions src/models/FGAtmosphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ double FGAtmosphere::ValidatePressure(double p, const string& msg, bool quiet) c

double FGAtmosphere::ValidateTemperature(double t, const string& msg, bool quiet) const
{
// Using pressure in Outer Space between stars in the Milky Way.
const double MinTemperature = ConvertToRankine(1.0, eKelvin);
if (t < MinTemperature) {
// Minimum known temperature in the universe currently
constexpr double minUniverseTemperature = KelvinToRankine(1.0);

if (t < minUniverseTemperature) {
if (!quiet) {
cerr << msg << " " << t << " is too low." << endl
<< msg << " is capped to " << MinTemperature << endl;
<< msg << " is capped to " << minUniverseTemperature << endl;
}
return MinTemperature;
return minUniverseTemperature;
}
return t;
}
Expand Down
26 changes: 18 additions & 8 deletions src/models/atmosphere/FGStandardAtmosphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,26 @@ void FGStandardAtmosphere::SetTemperature(double t, double h, eTemperature unit)

void FGStandardAtmosphere::SetTemperatureBias(eTemperature unit, double t)
{
unsigned int numRows = StdAtmosTemperatureTable.GetNumRows();
const double minTemperature = StdAtmosTemperatureTable(numRows, 1) - 1.8;
// Retrieve the minimum temperature in the standard atmosphere, may not be the
// last row in future if for example it's extended and maybe there is some
// temperature inversion layer etc. So run through and find the minimum.
const double minStdAtmosphereTemp = StdAtmosTemperatureTable.GetMinValue();

// Minimum known temperature in the universe currently
constexpr double minUniverseTemperature = KelvinToRankine(1.0);

if (unit == eCelsius || unit == eKelvin)
t *= 1.80; // If temp delta "t" is given in metric, scale up to English

TemperatureBias = t;
if (TemperatureBias <= -minTemperature) {
// Confirm the temperature bias isn't going to result in an atmosphere
// temperature lower than the lowest known temperature in the universe
if (minStdAtmosphereTemp + TemperatureBias < minUniverseTemperature) {
double minBias = minUniverseTemperature - minStdAtmosphereTemp;
cerr << "The temperature bias " << TemperatureBias << " R is too low. "
<< "It could result in temperatures below the absolute zero." << endl
<< "Temperature bias is therefore capped to " << -minTemperature << endl;
TemperatureBias = -minTemperature;
<< "Temperature bias is therefore capped to " << minBias << endl;
TemperatureBias = minBias;
}

CalculatePressureBreakpoints(SLpressure);
Expand Down Expand Up @@ -393,9 +401,11 @@ void FGStandardAtmosphere::SetSLTemperatureGradedDelta(eTemperature unit, double

void FGStandardAtmosphere::SetTemperatureGradedDelta(double deltemp, double h, eTemperature unit)
{
unsigned int numRows = StdAtmosTemperatureTable.GetNumRows();
const double minTemperature = StdAtmosTemperatureTable(numRows, 1);
const double minDeltaTemperature = minTemperature - StdSLtemperature;
// Retrieve the minimum temperature in the standard atmosphere, may not be the
// last row in future if for example it's extended and maybe there is some
// temperature inversion layer etc. So run through and find the minimum.
const double minStdAtmosphereTemp = StdAtmosTemperatureTable.GetMinValue();
const double minDeltaTemperature = minStdAtmosphereTemp - StdSLtemperature;

if (unit == eCelsius || unit == eKelvin)
deltemp *= 1.80; // If temp delta "t" is given in metric, scale up to English
Expand Down
40 changes: 40 additions & 0 deletions tests/unit_tests/FGTableTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,46 @@ class FGTable1DTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(t.GetValue(), 1.5);
}

void testMinValue() {
FGTable t1(1);
t1 << 0.0 << 1.0;

TS_ASSERT_EQUALS(t1.GetMinValue(), 1.0);

FGTable t21(2);
t21 << 0.0 << -1.0
<< 1.0 << 5.0;

TS_ASSERT_EQUALS(t21.GetMinValue(), -1.0);

FGTable t22(2);
t22 << 0.0 << 1.0
<< 1.0 << -5.0;

TS_ASSERT_EQUALS(t22.GetMinValue(), -5.0);

FGTable t31(3);
t31 << 0.0 << -1.0
<< 1.0 << 5.0
<< 3.0 << 3.0;

TS_ASSERT_EQUALS(t31.GetMinValue(), -1.0);

FGTable t32(3);
t32 << 0.0 << 1.0
<< 1.0 << -5.0
<< 3.0 << 3.0;

TS_ASSERT_EQUALS(t32.GetMinValue(), -5.0);

FGTable t33(3);
t33 << 0.0 << 1.0
<< 1.0 << 5.0
<< 3.0 << -3.0;

TS_ASSERT_EQUALS(t33.GetMinValue(), -3.0);
}

void testLoadInternalFromXML() {
auto pm = make_shared<FGPropertyManager>();
// FGTable expects <table> to be the child of another XML element, hence the
Expand Down

0 comments on commit ebb770e

Please sign in to comment.