1 #include <boost/tuple/tuple.hpp>
3 #include <simgear/math/SGMath.hxx>
4 #include <simgear/debug/logstream.hxx>
6 #include "atmosphere.hxx"
11 const ISA_layer ISA_def[] = {
13 // id (m) (ft) (Pa) (inHg) (K) (C) (K/m) (K/ft)
14 ISA_layer(0, 0, 0, 101325, 29.92126, 288.15, 15.00, 0.0065, 0.0019812),
15 ISA_layer(1, 11000, 36089, 22632.1, 6.683246, 216.65, -56.50, 0, 0),
16 ISA_layer(2, 20000, 65616, 5474.89, 1.616734, 216.65, -56.50, -0.0010, -0.0003048),
17 ISA_layer(3, 32000, 104986, 868.019, 0.256326, 228.65, -44.50, -0.0028, -0.0008534),
18 ISA_layer(4, 47000, 154199, 110.906, 0.0327506, 270.65, -2.50, 0, 0),
19 ISA_layer(5, 51000, 167322, 66.9389, 0.0197670, 270.65, -2.50, 0.0028, 0.0008534),
20 ISA_layer(6, 71000, 232939, 3.95642, 0.00116833, 214.65, -58.50, 0.0020, 0.0006096),
21 ISA_layer(7, 80000, 262467, 0.88628, 0.000261718, 196.65, -76.50),
24 const int ISA_def_size(sizeof(ISA_def) / sizeof(ISA_layer));
26 // Pressure within a layer, as a function of height.
27 // Physics model: standard or nonstandard atmosphere,
28 // depending on what parameters you pass in.
29 // Height in meters, pressures in pascals.
30 // As always, lapse is positive in the troposphere,
31 // and zero in the first part of the stratosphere.
33 double P_layer(const double height, const double href,
34 const double Pref, const double Tref,
36 using namespace atmodel;
38 double N = lapse * Rgas / mm / g;
39 return Pref * pow( (Tref - lapse*(height - href)) / Tref , (1/N));
41 return Pref * exp(-g * mm / Rgas / Tref * (height - href));
46 // Temperature within a layer, as a function of height.
47 // Physics model: standard or nonstandard atmosphere
48 // depending on what parameters you pass in.
49 // $hh in meters, pressures in Pa.
50 // As always, $lambda is positive in the troposphere,
51 // and zero in the first part of the stratosphere.
57 const double lambda) {
58 return Tb - lambda*(hh - hb);
61 // Pressure and temperature as a function of height, Psl, and Tsl.
62 // heights in meters, pressures in Pa.
63 // Daisy chain version.
64 // We need "seed" values for sea-level pressure and temperature.
65 // In addition, for every layer, we need three things
66 // from the table: the reference height in that layer,
67 // the lapse in that layer, and the cap (if any) for that layer
68 // (which we take from the /next/ row of the table, if any).
69 pair<double,double> PT_vs_hpt(
76 double hgt = ISA_def[0].height;
80 cout << "PT_vs_hpt: " << hh << " " << p0 << " " << t0 << endl;
84 for (const ISA_layer* pp = ISA_def; pp->lapse != -1; pp++, ii++) {
86 cout << "PT_vs_hpt: " << ii
87 << " height: " << pp->height
88 << " temp: " << pp->temp
89 << " lapse: " << pp->lapse
93 double lapse = pp->lapse;
94 // Stratosphere starts at a definite temperature,
95 // not a definite height:
97 xhgt = hgt + (t0 - (pp+1)->temp) / lapse;
98 } else if ((pp+1)->lapse != -1) {
99 xhgt = (pp+1)->height;
102 return make_pair(P_layer(hh, hgt, p0, t0, lapse),
103 T_layer(hh, hgt, p0, t0, lapse));
105 p0 = P_layer(xhgt, hgt, p0, t0, lapse);
106 t0 = t0 - lapse * (xhgt - hgt);
110 // Should never get here.
111 SG_LOG(SG_GENERAL, SG_ALERT, "PT_vs_hpt: ran out of layers for h=" << hh );
112 return make_pair(d0, d0);
116 FGAtmoCache::FGAtmoCache() :
120 FGAtmoCache::~FGAtmoCache() {
126 // The following two routines are called "fake" because they
127 // bypass the exceedingly complicated layer model implied by
128 // the "weather conditioins" popup menu.
129 // For now we must bypass it for several reasons, including
130 // the fact that we don't have an "environment" object for
131 // the airport (only for the airplane).
132 // degrees C, height in feet
133 double FGAtmo::fake_T_vs_a_us(const double h_ft,
134 const double Tsl) const {
135 using namespace atmodel;
136 return Tsl - ISA::lam0 * h_ft * foot;
139 // Dewpoint. degrees C or K, height in feet
140 double FGAtmo::fake_dp_vs_a_us(const double dpsl, const double h_ft) {
141 const double dp_lapse(0.002); // [K/m] approximate
142 // Reference: http://en.wikipedia.org/wiki/Lapse_rate
143 return dpsl - dp_lapse * h_ft * atmodel::foot;
146 // Height as a function of pressure.
147 // Valid in the troposphere only.
148 double FGAtmo::a_vs_p(const double press, const double qnh) {
149 using namespace atmodel;
151 double nn = lam0 * Rgas / g / mm;
152 return T0 * ( pow(qnh/P0,nn) - pow(press/P0,nn) ) / lam0;
155 // force retabulation
156 void FGAtmoCache::tabulate() {
157 using namespace atmodel;
159 a_tvs_p = new SGInterpTable;
161 for (double hgt = -1000; hgt <= 32000;) {
163 boost::tie(press, temp) = PT_vs_hpt(hgt);
164 a_tvs_p->addEntry(press / inHg, hgt / foot);
166 #ifdef DEBUG_EXPORT_P_H
168 char* fmt = " { %9.2f , %5.0f },";
169 if (press < 10000) fmt = " { %9.3f , %5.0f },";
170 snprintf(buf, 100, fmt, press, hgt);
181 // make sure cache is valid
182 void FGAtmoCache::cache() {
187 // Check the basic function,
188 // then compare against the interpolator.
189 void FGAtmoCache::check_model() {
208 for (int i = 0; ; i++) {
209 double height = hgts[i];
212 using namespace atmodel;
215 boost::tie(press, temp) = PT_vs_hpt(height);
216 cout << "Height: " << height
217 << " \tpressure: " << press << endl;
219 << a_tvs_p->interpolate(press / inHg)*foot << endl;
223 //////////////////////////////////////////////////////////////////////
225 FGAltimeter::FGAltimeter() : kset(atmodel::ISA::P0), kft(0)
230 double FGAltimeter::reading_ft(const double p_inHg, const double set_inHg) {
231 using namespace atmodel;
232 double press_alt = a_tvs_p->interpolate(p_inHg);
233 double kollsman_shift = a_tvs_p->interpolate(set_inHg);
234 return (press_alt - kollsman_shift);
237 // Altimeter setting _in pascals_
238 // ... caller gets to convert to inHg or millibars
239 // Field elevation in m
240 // Field pressure in pascals
241 // Valid for fields within the troposphere only.
242 double FGAtmo::QNH(const double field_elev, const double field_press) {
243 using namespace atmodel;
245 // Equation derived in altimetry.htm
246 // exponent in QNH equation:
247 double nn = ISA::lam0 * Rgas / g / mm;
248 // pressure ratio factor:
249 double prat = pow(ISA::P0 / field_press, nn);
250 double rslt = field_press
251 * pow(1. + ISA::lam0 * field_elev / ISA::T0 * prat, 1./nn);
253 SG_LOG(SG_GENERAL, SG_ALERT, "QNH: elev: " << field_elev
254 << " press: " << field_press
258 << " rslt/inHG: " << rslt/inHg);
263 // Invert the QNH calculation to get the field pressure from a metar
265 // field pressure _in pascals_
266 // ... caller gets to convert to inHg or millibars
267 // Field elevation in m
268 // Altimeter setting (QNH) in pascals
269 // Valid for fields within the troposphere only.
270 double FGAtmo::fieldPressure(const double field_elev, const double qnh)
272 using namespace atmodel;
273 static const double nn = ISA::lam0 * Rgas / g / mm;
274 const double pratio = pow(qnh / ISA::P0, nn);
275 return ISA::P0 * pow(pratio - field_elev * ISA::lam0 / ISA::T0, 1.0 / nn);
278 void FGAltimeter::dump_stack1(const double Tref) {
279 using namespace atmodel;
282 double Psl = P_layer(0, 0, ISA::P0, Tref, ISA::lam0);
283 snprintf(buf, bs, "Tref: %6.2f Psl: %5.0f = %7.4f",
284 Tref, Psl, Psl / inHg);
288 " %6s %6s %6s %6s %6s %6s %6s",
289 "A", "Aind", "Apr", "Aprind", "P", "Psl", "Qnh");
292 double hgts[] = {0, 2500, 5000, 7500, 10000, -9e99};
293 for (int ii = 0; ; ii++) {
294 double hgt_ft = hgts[ii];
295 double hgt = hgt_ft * foot;
298 double press = P_layer(hgt, 0, ISA::P0, Tref, ISA::lam0);
299 double qnhx = QNH(hgt, press) / inHg;
300 double qnh2 = SGMiscd::round(qnhx*100)/100;
302 double p_inHg = press / inHg;
303 double Aprind = reading_ft(p_inHg);
304 double Apr = a_vs_p(p_inHg*inHg) / foot;
305 double hind = reading_ft(p_inHg, qnh2);
307 " %6.0f %6.0f %6.0f %6.0f %6.2f %6.2f %6.2f",
308 hgt_ft, hind, Apr, Aprind, p_inHg, Psl/inHg, qnh2);
314 void FGAltimeter::dump_stack() {
315 using namespace atmodel;
316 cout << "........." << endl;
317 cout << "Size: " << sizeof(FGAtmo) << endl;
318 dump_stack1(ISA::T0);
319 dump_stack1(ISA::T0 - 20);