Skip to content

Commit 9de5fe4

Browse files
Add FGNullAtmosphere and extend LoadPlanet atmosphere factory (JSBSim-Team#1462)
Co-authored-by: Bertrand Coconnier <bcoconni@users.noreply.github.com>
1 parent e5446e1 commit 9de5fe4

6 files changed

Lines changed: 76 additions & 9 deletions

File tree

src/FGFDMExec.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ INCLUDES
4747
#include "FGFDMExec.h"
4848
#include "models/atmosphere/FGStandardAtmosphere.h"
4949
#include "models/atmosphere/FGMSIS.h"
50+
#include "models/atmosphere/FGMars.h"
5051
#include "models/atmosphere/FGWinds.h"
5152
#include "models/FGFCS.h"
5253
#include "models/FGPropulsion.h"
@@ -63,6 +64,7 @@ INCLUDES
6364
#include "initialization/FGLinearization.h"
6465
#include "input_output/FGScript.h"
6566
#include "input_output/FGXMLFileRead.h"
67+
#include "input_output/string_utilities.h"
6668
#include "initialization/FGInitialCondition.h"
6769
#include "input_output/FGLog.h"
6870

@@ -788,10 +790,20 @@ bool FGFDMExec::LoadPlanet(Element* element)
788790
Element* atm_element = element->FindElement("atmosphere");
789791
if (atm_element && atm_element->HasAttribute("model")) {
790792
string model = atm_element->GetAttributeValue("model");
791-
if (model == "MSIS") {
792-
// Replace the existing atmosphere model
793+
to_lower(model);
794+
795+
if (model == "msis" || model == "mars") {
793796
instance->Unbind(Models[eAtmosphere]);
794-
Models[eAtmosphere] = std::make_shared<FGMSIS>(this);
797+
}
798+
799+
std::shared_ptr<FGAtmosphere> newAtmosphere;
800+
if (model == "msis")
801+
newAtmosphere = std::make_shared<FGMSIS>(this);
802+
else if (model == "mars")
803+
newAtmosphere = std::make_shared<FGMars>(this);
804+
805+
if (newAtmosphere) {
806+
Models[eAtmosphere] = newAtmosphere;
795807
Atmosphere = static_cast<FGAtmosphere*>(Models[eAtmosphere].get());
796808

797809
// Model initialization sequence

src/models/atmosphere/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
set(SOURCES FGMSIS.cpp
2+
FGMars.cpp
23
FGStandardAtmosphere.cpp
34
FGWinds.cpp
45
MSIS/nrlmsise-00.c
56
MSIS/nrlmsise-00_data.c)
67

78
set(HEADERS FGMSIS.h
9+
FGMars.h
810
FGStandardAtmosphere.h
911
FGWinds.h
1012
MSIS/nrlmsise-00.h)

src/models/atmosphere/FGMars.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ FGMars::FGMars(FGFDMExec* fdmex) : FGAtmosphere(fdmex)
6161
Name = "FGMars";
6262
Reng = 53.5 * 44.01;
6363

64-
bind();
6564
Debug(0);
6665
}
6766

@@ -74,9 +73,9 @@ void FGMars::Calculate(double altitude)
7473
// LIMIT the temperatures so they do not descend below absolute zero.
7574

7675
if (altitude < 22960.0) {
77-
Temperature = -25.68 - 0.000548*altitude; // Deg Fahrenheit
76+
Temperature = -25.68 - 0.000548*altitude + 459.67; // Convert F to Rankine
7877
} else {
79-
Temperature = -10.34 - 0.001217*altitude; // Deg Fahrenheit
78+
Temperature = -10.34 - 0.001217*altitude + 459.67; // Convert F to Rankine
8079
}
8180
Pressure = 14.62*exp(-0.00003*altitude); // psf - 14.62 psf =~ 7 millibars
8281
Density = Pressure/(Reng*Temperature); // slugs/ft^3 (needs deg R. as input

src/models/atmosphere/FGMars.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ class FGMars : public FGAtmosphere {
6363
/// Constructor
6464
FGMars(FGFDMExec*);
6565

66-
private:
67-
void Calculate(double altitude);
66+
double GetTemperature(double altitude) const override { return Temperature; }
67+
double GetPressure(double altitude) const override { return Pressure; }
68+
void SetTemperature(double t, double h, eTemperature unit) override {}
6869

69-
void Debug(int from);
70+
private:
71+
void Calculate(double altitude) override;
72+
void Debug(int from) override;
7073
};
7174

7275
} // namespace JSBSim

tests/TestPlanet.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,49 @@ def test_load_MSIS_atmosphere(self):
7575
self.assertAlmostEqual(self.fdm['atmosphere/rho-slugs_ft3']/0.001940318, 1.263428, delta=1E-6)
7676
self.assertAlmostEqual(self.fdm['atmosphere/P-psf'], 2132.294, delta=1E-3)
7777

78+
def test_mars_atmosphere(self):
79+
# Mars atmosphere via <planet><atmosphere model="Mars"/></planet>.
80+
# Reference values are the closed-form output of FGMars::Calculate at
81+
# altitude = 0:
82+
# T = -25.68 + 459.67 = 433.99 R (~241.1 K)
83+
# P = 14.62 psf (~7 mbar)
84+
# rho = P / (Reng * T), Reng = 53.5*44.01 (CO2)
85+
tripod = FlightModel(self, 'tripod')
86+
mars_file = self.sandbox.path_to_jsbsim_file('tests/mars.xml')
87+
tripod.include_planet_test_file(mars_file)
88+
self.fdm = tripod.start()
89+
self.fdm['ic/h-agl-ft'] = 0.0
90+
self.fdm['ic/long-gc-deg'] = 0.0
91+
self.fdm['ic/lat-geod-deg'] = 0.0
92+
self.fdm.run_ic()
93+
94+
# Mars equatorial radius = 3396.2 km
95+
self.assertAlmostEqual(self.fdm['metrics/terrain-radius']*0.3048/3396200, 1.0)
96+
# Surface gravity ~3.71-3.72 m/s^2 (JSBSim uses oblate-spheroid gravity,
97+
# not point-mass GM/R^2, so the value is a few mGal above textbook).
98+
self.assertAlmostEqual(self.fdm['accelerations/gravity-ft_sec2']*0.3048, 3.72, delta=5E-2)
99+
100+
self.assertAlmostEqual(self.fdm['atmosphere/T-R'], 433.99, delta=1E-2)
101+
self.assertAlmostEqual(self.fdm['atmosphere/P-psf'], 14.62, delta=1E-2)
102+
# rho = 14.62 / (53.5*44.01 * 433.99) ~ 1.4308e-5 slugs/ft^3
103+
self.assertAlmostEqual(self.fdm['atmosphere/rho-slugs_ft3'], 1.4308e-5, delta=1E-8)
104+
105+
def test_load_Mars_atmosphere(self):
106+
# Same as above but using FGFDMExec::LoadPlanet at runtime instead of
107+
# including the planet file at FDM construction time.
108+
tripod = FlightModel(self, 'tripod')
109+
mars_file = self.sandbox.path_to_jsbsim_file('tests/mars.xml')
110+
self.fdm = tripod.start()
111+
self.fdm.load_planet(mars_file, False)
112+
self.fdm['ic/h-agl-ft'] = 0.0
113+
self.fdm['ic/long-gc-deg'] = 0.0
114+
self.fdm['ic/lat-geod-deg'] = 0.0
115+
self.fdm.run_ic()
116+
117+
self.assertAlmostEqual(self.fdm['atmosphere/T-R'], 433.99, delta=1E-2)
118+
self.assertAlmostEqual(self.fdm['atmosphere/P-psf'], 14.62, delta=1E-2)
119+
self.assertAlmostEqual(self.fdm['atmosphere/rho-slugs_ft3'], 1.4308e-5, delta=1E-8)
120+
78121
def test_planet_geographic_error1(self):
79122
# Check that a negative equatorial radius raises an exception
80123
tripod = FlightModel(self, 'tripod')

tests/mars.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<planet name="Mars">
2+
<equatorial_radius unit="KM">3396.2</equatorial_radius>
3+
<polar_radius unit="KM">3376.2</polar_radius>
4+
<rotation_rate unit="RAD/SEC">7.0882E-5</rotation_rate>
5+
<GM unit="M3/SEC2">4.2828372E13</GM>
6+
<J2>1.9605E-3</J2>
7+
<atmosphere model="Mars"/>
8+
</planet>

0 commit comments

Comments
 (0)