Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
* Fix `SetTemperatureSL()` and `SetPressureSL()` which did not update the sea level density and speed of sound.
* Make `Reng` a non `static` member (see JSBSim-Team#666). This was useless and prevented `Reng` from being accessed when JSBSim was compiled as a DLL.
  • Loading branch information
bcoconni committed Mar 12, 2023
1 parent 5d0c9fb commit 6c1cce8
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/models/FGAtmosphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ CLASS IMPLEMENTATION

// Atmosphere constants in British units converted from the SI values specified in the
// ISA document - https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770009539.pdf
double FGAtmosphere::Reng = Rstar / Mair;

const double FGAtmosphere::StdDaySLsoundspeed = sqrt(SHRatio*Reng*StdDaySLtemperature);
const double FGAtmosphere::StdDaySLsoundspeed = sqrt(SHRatio*Reng0*StdDaySLtemperature);

FGAtmosphere::FGAtmosphere(FGFDMExec* fdmex) : FGModel(fdmex)
{
Expand Down Expand Up @@ -175,6 +173,7 @@ void FGAtmosphere::SetPressureSL(ePressure unit, double pressure)
double press = ConvertToPSF(pressure, unit);

SLpressure = ValidatePressure(press, "Sea Level pressure");
SLdensity = GetDensity(0.0);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -202,6 +201,8 @@ void FGAtmosphere::SetTemperatureSL(double t, eTemperature unit)
double temp = ConvertToRankine(t, unit);

SLtemperature = ValidateTemperature(temp, "Sea Level temperature");
SLdensity = GetDensity(0.0);
SLsoundspeed = GetSoundSpeed(0.0);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
4 changes: 3 additions & 1 deletion src/models/FGAtmosphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,13 @@ class FGAtmosphere : public FGModel {
*/
static constexpr double g0 = 9.80665 / fttom;
/// Specific gas constant for air - ft*lbf/slug/R
static double Reng;
static constexpr double Reng0 = Rstar / Mair;
//@}

static constexpr double SHRatio = 1.4;

double Reng = Reng0;

virtual void bind(void);
void Debug(int from) override;
};
Expand Down
145 changes: 138 additions & 7 deletions tests/unit_tests/FGAtmosphereTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class DummyAtmosphere : public FGAtmosphere
class FGAtmosphereTest : public CxxTest::TestSuite
{
public:
void testDefaultValuesBeforeInit() {
void testDefaultValuesBeforeInit()
{
FGFDMExec fdmex;
auto atm = DummyAtmosphere(&fdmex, 1.0, 1.0);

Expand Down Expand Up @@ -79,7 +80,8 @@ class FGAtmosphereTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(atm.GetKinematicViscosity(), 0.0);
}

void testDefaultValuesAfterInit() {
void testDefaultValuesAfterInit()
{
FGFDMExec fdmex;
auto atm = DummyAtmosphere(&fdmex, 1.0, 1.0);

Expand Down Expand Up @@ -122,7 +124,8 @@ class FGAtmosphereTest : public CxxTest::TestSuite
TS_ASSERT_DELTA(atm.GetKinematicViscosity(), nu, epsilon);
}

void testGetAltitudeParameters() {
void testGetAltitudeParameters()
{
FGFDMExec fdmex;
auto atm = DummyAtmosphere(&fdmex, 0.1, 1.0);
TS_ASSERT(atm.InitModel());
Expand Down Expand Up @@ -177,7 +180,8 @@ class FGAtmosphereTest : public CxxTest::TestSuite
}
}

void testRun() {
void testRun()
{
FGFDMExec fdmex;
auto atm = DummyAtmosphere(&fdmex, 0.1, 1.0);
TS_ASSERT(atm.InitModel());
Expand Down Expand Up @@ -235,7 +239,8 @@ class FGAtmosphereTest : public CxxTest::TestSuite
}
}

void testTemperatureOverride() {
void testTemperatureOverride()
{
FGFDMExec fdmex;
auto pm = fdmex.GetPropertyManager();
auto atm = DummyAtmosphere(&fdmex, 0.1, 1.0);
Expand Down Expand Up @@ -298,7 +303,8 @@ class FGAtmosphereTest : public CxxTest::TestSuite
}
}

void testPressureOverride() {
void testPressureOverride()
{
FGFDMExec fdmex;
auto pm = fdmex.GetPropertyManager();
auto atm = DummyAtmosphere(&fdmex, 0.1, 1.0);
Expand Down Expand Up @@ -360,7 +366,8 @@ class FGAtmosphereTest : public CxxTest::TestSuite
}
}

void testDensityOverride() {
void testDensityOverride()
{
FGFDMExec fdmex;
auto pm = fdmex.GetPropertyManager();
auto atm = DummyAtmosphere(&fdmex, 0.1, 1.0);
Expand Down Expand Up @@ -421,4 +428,128 @@ class FGAtmosphereTest : public CxxTest::TestSuite
TS_ASSERT_DELTA(atm.GetKinematicViscosity(), nu, epsilon);
}
}

void testTemperatureSL()
{
FGFDMExec fdmex;
auto pm = fdmex.GetPropertyManager();
auto atm = DummyAtmosphere(&fdmex, 0.1, 1.0);
TS_ASSERT(atm.InitModel());

const double R = atm.GetR();
const double gamma = atm.GetGamma();
const double T0 = 300.0;
const double P0 = FGAtmosphere::StdDaySLpressure;
const double rho0 = P0/(R*T0);
const double a0 = sqrt(gamma*R*T0);
const double beta = atm.GetBeta();
const double k = atm.GetSutherlandConstant();

atm.SetTemperatureSL(T0, FGAtmosphere::eRankine);

for(double h=-1000.0; h<10000; h+= 1000) {
atm.in.altitudeASL = h;
TS_ASSERT(atm.Run(false) == false);

double T = T0+0.1*h;
TS_ASSERT_EQUALS(atm.GetTemperatureSL(), T0);
TS_ASSERT_DELTA(atm.GetTemperature(), T, epsilon);
TS_ASSERT_EQUALS(atm.GetTemperature(0.0), T0);
TS_ASSERT_DELTA(atm.GetTemperature(h), T, epsilon);
TS_ASSERT_DELTA(atm.GetTemperatureRatio(), T/T0, epsilon);
TS_ASSERT_EQUALS(atm.GetTemperatureRatio(0.0), 1.0);
TS_ASSERT_DELTA(atm.GetTemperatureRatio(h), T/T0, epsilon);

double P = P0 + 1.0*h;
TS_ASSERT_EQUALS(atm.GetPressureSL(), P0);
TS_ASSERT_DELTA(atm.GetPressure(), P, epsilon);
TS_ASSERT_EQUALS(atm.GetPressure(0.0), P0);
TS_ASSERT_DELTA(atm.GetPressure(h), P, epsilon);
TS_ASSERT_DELTA(atm.GetPressureRatio(), P/P0, epsilon);

double rho = P/(R*T);
TS_ASSERT_DELTA(atm.GetDensity(), rho, epsilon);
TS_ASSERT_DELTA(atm.GetDensity(0.0), rho0, epsilon);
TS_ASSERT_DELTA(atm.GetDensity(h), rho, epsilon);
TS_ASSERT_DELTA(atm.GetDensitySL(), rho0, epsilon);
TS_ASSERT_EQUALS(atm.GetDensityRatio(), rho/rho0);

double a = sqrt(gamma*R*T);
TS_ASSERT_DELTA(atm.GetSoundSpeed(), a, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeed(0.0), a0, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeed(h), a, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeedSL(), a0, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeedRatio(), a/a0, epsilon);

TS_ASSERT_EQUALS(atm.GetDensityAltitude(), h);
TS_ASSERT_EQUALS(atm.GetPressureAltitude(), h);

double mu = beta*T*sqrt(T)/(k+T);
double nu = mu/rho;
TS_ASSERT_DELTA(atm.GetAbsoluteViscosity(), mu, epsilon);
TS_ASSERT_DELTA(atm.GetKinematicViscosity(), nu, epsilon);
}
}

void testPressureSL()
{
FGFDMExec fdmex;
auto pm = fdmex.GetPropertyManager();
auto atm = DummyAtmosphere(&fdmex, 0.1, 1.0);
TS_ASSERT(atm.InitModel());

const double R = atm.GetR();
const double gamma = atm.GetGamma();
const double T0 = FGAtmosphere::StdDaySLtemperature;
const double P0 = 3000.0;
const double rho0 = P0/(R*T0);
const double a0 = sqrt(gamma*R*T0);
const double beta = atm.GetBeta();
const double k = atm.GetSutherlandConstant();

atm.SetPressureSL(FGAtmosphere::ePSF, P0);

for(double h=-1000.0; h<10000; h+= 1000) {
atm.in.altitudeASL = h;
TS_ASSERT(atm.Run(false) == false);

double T = T0+0.1*h;
TS_ASSERT_EQUALS(atm.GetTemperatureSL(), T0);
TS_ASSERT_DELTA(atm.GetTemperature(), T, epsilon);
TS_ASSERT_EQUALS(atm.GetTemperature(0.0), T0);
TS_ASSERT_DELTA(atm.GetTemperature(h), T, epsilon);
TS_ASSERT_DELTA(atm.GetTemperatureRatio(), T/T0, epsilon);
TS_ASSERT_EQUALS(atm.GetTemperatureRatio(0.0), 1.0);
TS_ASSERT_DELTA(atm.GetTemperatureRatio(h), T/T0, epsilon);

double P = P0 + 1.0*h;
TS_ASSERT_EQUALS(atm.GetPressureSL(), P0);
TS_ASSERT_DELTA(atm.GetPressure(), P, epsilon);
TS_ASSERT_EQUALS(atm.GetPressure(0.0), P0);
TS_ASSERT_DELTA(atm.GetPressure(h), P, epsilon);
TS_ASSERT_DELTA(atm.GetPressureRatio(), P/P0, epsilon);

double rho = P/(R*T);
TS_ASSERT_DELTA(atm.GetDensity(), rho, epsilon);
TS_ASSERT_DELTA(atm.GetDensity(0.0), rho0, epsilon);
TS_ASSERT_DELTA(atm.GetDensity(h), rho, epsilon);
TS_ASSERT_DELTA(atm.GetDensitySL(), rho0, epsilon);
TS_ASSERT_EQUALS(atm.GetDensityRatio(), rho/rho0);

double a = sqrt(gamma*R*T);
TS_ASSERT_DELTA(atm.GetSoundSpeed(), a, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeed(0.0), a0, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeed(h), a, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeedSL(), a0, epsilon);
TS_ASSERT_DELTA(atm.GetSoundSpeedRatio(), a/a0, epsilon);

TS_ASSERT_EQUALS(atm.GetDensityAltitude(), h);
TS_ASSERT_EQUALS(atm.GetPressureAltitude(), h);

double mu = beta*T*sqrt(T)/(k+T);
double nu = mu/rho;
TS_ASSERT_DELTA(atm.GetAbsoluteViscosity(), mu, epsilon);
TS_ASSERT_DELTA(atm.GetKinematicViscosity(), nu, epsilon);
}
}
};

0 comments on commit 6c1cce8

Please sign in to comment.