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