Debian Codegraph @

src/FDM/JSBSim/models/atmosphere/FGStandardAtmosphere.cpp

raw symbols SCIP
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGStandardAtmosphere.cpp
4 Author: Jon Berndt, Tony Peden
5 Date started: 5/2011
6 Purpose: Models the 1976 U.S. Standard Atmosphere
7 Called by: FGFDMExec
8
9 ------------- Copyright (C) 2011 Jon S. Berndt ([email protected]) -------------
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 2 of the License, or (at your option) any
14 later version.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 details.
20
21 You should have received a copy of the GNU Lesser General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc., 59
23 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Further information about the GNU Lesser General Public License can also be
26 found on the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
31
32 HISTORY
33 --------------------------------------------------------------------------------
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 COMMENTS, REFERENCES, and NOTES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 [1] Anderson, John D. "Introduction to Flight, Third Edition", McGraw-Hill,
39 1989, ISBN 0-07-001641-0
40 [2] Sonntag, D. "Important New Values of the Physical Constants of 1986,
41 Vapour Pressure Formulations based on the IST-90 and Psychrometer
42 Formulae", Z. Meteorol., 70 (5), pp. 340-344, 1990
43
44 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 INCLUDES
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47
48 #include <iomanip>
49
50 #include "FGFDMExec.h"
51 #include "FGStandardAtmosphere.h"
52
53 using namespace std;
54
55 namespace JSBSim {
56
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 CLASS IMPLEMENTATION
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60
61 FGStandardAtmosphere::FGStandardAtmosphere(FGFDMExec* fdmex)
62 : FGAtmosphere(fdmex), StdSLpressure(StdDaySLpressure), TemperatureBias(0.0),
63 TemperatureDeltaGradient(0.0), VaporMassFraction(0.0),
64 SaturatedVaporPressure(StdDaySLpressure), StdAtmosTemperatureTable(9),
65 MaxVaporMassFraction(11)
66 {
67 Name = "FGStandardAtmosphere";
68
69 // This is the U.S. Standard Atmosphere table for temperature in degrees
70 // Rankine, based on geometric altitude. The table values are often given
71 // in literature relative to geopotential altitude.
72 //
73 // GeoMet Alt Temp GeoPot Alt GeoMet Alt
74 // (ft) (deg R) (km) (km)
75 // -------- -------- ---------- ----------
76 //StdAtmosTemperatureTable << 0.00 << 518.67 // 0.000 0.000
77 // << 36151.80 << 389.97 // 11.000 11.019
78 // << 65823.90 << 389.97 // 20.000 20.063
79 // << 105518.06 << 411.60 // 32.000 32.162
80 // << 155348.07 << 487.20 // 47.000 47.350
81 // << 168676.12 << 487.20 // 51.000 51.413
82 // << 235570.77 << 386.40 // 71.000 71.802
83 // << 282152.08 << 336.50 // 84.852 86.000
84 // << 298556.40 << 336.50; // 91.000 - First layer in high altitude regime
85
86 // GeoPot Alt Temp GeoPot Alt GeoMet Alt
87 // (ft) (deg R) (km) (km)
88 // ----------- -------- ---------- ----------
89 StdAtmosTemperatureTable << 0.0000 << 518.67 // 0.000 0.000
90 << 36089.2388 << 389.97 // 11.000 11.019
91 << 65616.7979 << 389.97 // 20.000 20.063
92 << 104986.8766 << 411.57 // 32.000 32.162
93 << 154199.4751 << 487.17 // 47.000 47.350
94 << 167322.8346 << 487.17 // 51.000 51.413
95 << 232939.6325 << 386.37 // 71.000 71.802
96 << 278385.8268 << 336.5028 // 84.852 86.000
97 << 298556.4304 << 336.5028; // 91.000 - First layer in high altitude regime
98
99
100 // This is the maximum water vapor mass fraction in ppm (parts per million) of
101 // dry air measured in the atmosphere according to the ISA 1976 document.
102 // Values at altitude below 8 km are record high. All other values are 1%
103 // high.
104
105 // Geopot Alt Water Geopot Alt
106 // (ft) (ppm) (km)
107 // ---------- ----- ----------
108 MaxVaporMassFraction << 0.0000 << 35000. // 0.0000 - Record high
109 << 3280.8399 << 31000. // 1.0000
110 << 6561.6798 << 28000. // 2.0000
111 << 13123.3596 << 22000. // 4.0000
112 << 19685.0394 << 8900. // 6.0000
113 << 26246.7192 << 4700. // 8.0000 - Record high
114 << 32808.3990 << 1300. // 10.0000 - 1% high
115 << 39370.0787 << 230. // 12.0000
116 << 45931.7585 << 48. // 14.0000
117 << 52493.4383 << 38. // 16.0000 - 1% high
118 << 157480.3149 << 0; // 48.0 km : force to zero (see https://gitlab.com/flightgear/flightgear/-/issues/3267 )
119
120 unsigned int numRows = StdAtmosTemperatureTable.GetNumRows();
121
122 // Initialize the standard atmosphere lapse rates.
123 CalculateLapseRates();
124 StdLapseRates = LapseRates;
125
126 // Assume the altitude to fade out the gradient at is at the highest
127 // altitude in the table. Above that, other functions are used to
128 // calculate temperature.
129 GradientFadeoutAltitude = StdAtmosTemperatureTable(numRows, 0);
130
131 // Initialize the standard atmosphere pressure break points.
132 PressureBreakpoints.resize(numRows);
133 CalculatePressureBreakpoints(StdSLpressure);
134 StdPressureBreakpoints = PressureBreakpoints;
135
136 StdSLtemperature = StdAtmosTemperatureTable(1, 1);
137 StdSLdensity = StdSLpressure / (Rdry * StdSLtemperature);
138
139 CalculateStdDensityBreakpoints();
140 StdSLsoundspeed = sqrt(SHRatio*Rdry*StdSLtemperature);
141
142 bind();
143 Debug(0);
144 }
145
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147
148 FGStandardAtmosphere::~FGStandardAtmosphere()
149 {
150 Debug(1);
151 }
152
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154
155 bool FGStandardAtmosphere::InitModel(void)
156 {
157
158 // Assume the altitude to fade out the gradient at is at the highest
159 // altitude in the table. Above that, other functions are used to
160 // calculate temperature.
161 GradientFadeoutAltitude = StdAtmosTemperatureTable(StdAtmosTemperatureTable.GetNumRows(), 0);
162
163 TemperatureDeltaGradient = 0.0;
164 TemperatureBias = 0.0;
165 LapseRates = StdLapseRates;
166
167 PressureBreakpoints = StdPressureBreakpoints;
168
169 SLpressure = StdSLpressure;
170 SLtemperature = StdSLtemperature;
171 SLdensity = StdSLdensity;
172 SLsoundspeed = StdSLsoundspeed;
173
174 Calculate(0.0);
175
176 // PrintStandardAtmosphereTable();
177
178 return true;
179 }
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181
182 void FGStandardAtmosphere::Calculate(double altitude)
183 {
184 FGAtmosphere::Calculate(altitude);
185 SaturatedVaporPressure = CalculateVaporPressure(Temperature);
186 ValidateVaporMassFraction(altitude);
187 }
188
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 // Get the actual pressure as modeled at a specified altitude
191 // These calculations are from equations 33a and 33b in the U.S. Standard
192 // Atmosphere document referenced in the documentation for this code.
193
194 double FGStandardAtmosphere::GetPressure(double altitude) const
195 {
196 double GeoPotAlt = GeopotentialAltitude(altitude);
197
198 // Iterate through the altitudes to find the current Base Altitude
199 // in the table. That is, if the current altitude (the argument passed in)
200 // is 20000 ft, then the base altitude from the table is 0.0. If the
201 // passed-in altitude is 40000 ft, the base altitude is 36089.2388 ft (and
202 // the index "b" is 2 - the second entry in the table).
203 double BaseAlt = StdAtmosTemperatureTable(1,0);
204 unsigned int numRows = StdAtmosTemperatureTable.GetNumRows();
205 unsigned int b;
206
207 for (b=0; b < numRows-2; ++b) {
208 double testAlt = StdAtmosTemperatureTable(b+2,0);
209 if (GeoPotAlt < testAlt)
210 break;
211 BaseAlt = testAlt;
212 }
213
214 double Tmb = GetTemperature(GeometricAltitude(BaseAlt));
215 double deltaH = GeoPotAlt - BaseAlt;
216 double Lmb = LapseRates[b];
217
218 if (Lmb != 0.0) {
219 double Exp = g0 / (Rdry*Lmb);
220 double factor = Tmb/(Tmb + Lmb*deltaH);
221 return PressureBreakpoints[b]*pow(factor, Exp);
222 } else
223 return PressureBreakpoints[b]*exp(-g0*deltaH/(Rdry*Tmb));
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 void FGStandardAtmosphere::SetPressureSL(ePressure unit, double pressure)
229 {
230 double p = ConvertToPSF(pressure, unit);
231 SLpressure = ValidatePressure(p, "Sea Level pressure");
232 CalculateSLDensity();
233 CalculatePressureBreakpoints(SLpressure);
234 }
235
236 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237
238 void FGStandardAtmosphere::CalculateSLSoundSpeedAndDensity(void)
239 {
240 SLsoundspeed = sqrt(SHRatio*Reng*SLtemperature);
241 CalculateSLDensity();
242 }
243
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 // Get the modeled temperature at a specified altitude, including any bias or gradient
246 // effects.
247
248 double FGStandardAtmosphere::GetTemperature(double altitude) const
249 {
250 double GeoPotAlt = GeopotentialAltitude(altitude);
251
252 double T;
253
254 if (GeoPotAlt >= 0.0) {
255 T = StdAtmosTemperatureTable.GetValue(GeoPotAlt);
256
257 if (GeoPotAlt <= GradientFadeoutAltitude)
258 T -= TemperatureDeltaGradient * GeoPotAlt;
259 }
260 else {
261 // We don't need to add TemperatureDeltaGradient*GeoPotAlt here because
262 // the lapse rate vector already accounts for the temperature gradient.
263 T = StdAtmosTemperatureTable.GetValue(0.0) + GeoPotAlt*LapseRates[0];
264 }
265
266 T += TemperatureBias;
267
268 if (GeoPotAlt <= GradientFadeoutAltitude)
269 T += TemperatureDeltaGradient * GradientFadeoutAltitude;
270
271 return T;
272 }
273
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 // Retrieves the standard temperature at a particular altitude.
276
277 double FGStandardAtmosphere::GetStdTemperature(double altitude) const
278 {
279 double GeoPotAlt = GeopotentialAltitude(altitude);
280
281 if (GeoPotAlt >= 0.0)
282 return StdAtmosTemperatureTable.GetValue(GeoPotAlt);
283 else
284 return StdAtmosTemperatureTable.GetValue(0.0) + GeoPotAlt*LapseRates[0];
285 }
286
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288
289 double FGStandardAtmosphere::GetStdPressure(double altitude) const
290 {
291 double GeoPotAlt = GeopotentialAltitude(altitude);
292
293 // Iterate through the altitudes to find the current Base Altitude
294 // in the table. That is, if the current altitude (the argument passed in)
295 // is 20000 ft, then the base altitude from the table is 0.0. If the
296 // passed-in altitude is 40000 ft, the base altitude is 36089.2388 ft (and
297 // the index "b" is 2 - the second entry in the table).
298 double BaseAlt = StdAtmosTemperatureTable(1,0);
299 unsigned int numRows = StdAtmosTemperatureTable.GetNumRows();
300 unsigned int b;
301
302 for (b=0; b < numRows-2; ++b) {
303 double testAlt = StdAtmosTemperatureTable(b+2,0);
304 if (GeoPotAlt < testAlt)
305 break;
306 BaseAlt = testAlt;
307 }
308
309 double Tmb = GetStdTemperature(GeometricAltitude(BaseAlt));
310 double deltaH = GeoPotAlt - BaseAlt;
311 double Lmb = LapseRates[b];
312
313 if (Lmb != 0.0) {
314 double Exp = g0 / (Rdry*Lmb);
315 double factor = Tmb/(Tmb + Lmb*deltaH);
316 return StdPressureBreakpoints[b]*pow(factor, Exp);
317 } else
318 return StdPressureBreakpoints[b]*exp(-g0*deltaH/(Rdry*Tmb));
319 }
320
321 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 // Get the standard density at a specified altitude
323
324 double FGStandardAtmosphere::GetStdDensity(double altitude) const
325 {
326 return GetStdPressure(altitude)/(Rdry * GetStdTemperature(altitude));
327 }
328
329 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330
331 void FGStandardAtmosphere::SetTemperature(double t, double h, eTemperature unit)
332 {
333 double targetTemp = ConvertToRankine(t, unit);
334 double GeoPotAlt = GeopotentialAltitude(h);
335 double bias = targetTemp - GetStdTemperature(h);
336
337 if (GeoPotAlt <= GradientFadeoutAltitude)
338 bias -= TemperatureDeltaGradient * (GradientFadeoutAltitude - GeoPotAlt);
339
340 SetTemperatureBias(eRankine, bias);
341 CalculatePressureBreakpoints(SLpressure);
342
343 SLtemperature = GetTemperature(0.0);
344 CalculateSLSoundSpeedAndDensity();
345 }
346
347 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348
349 void FGStandardAtmosphere::SetTemperatureBias(eTemperature unit, double t)
350 {
351 // Retrieve the minimum temperature in the standard atmosphere, may not be the
352 // last row in future if for example it's extended and maybe there is some
353 // temperature inversion layer etc. So run through and find the minimum.
354 const double minStdAtmosphereTemp = StdAtmosTemperatureTable.GetMinValue();
355
356 // Minimum known temperature in the universe currently
357 constexpr double minUniverseTemperature = KelvinToRankine(1.0);
358
359 if (unit == eCelsius || unit == eKelvin)
360 t *= 1.80; // If temp delta "t" is given in metric, scale up to English
361
362 TemperatureBias = t;
363 // Confirm the temperature bias isn't going to result in an atmosphere
364 // temperature lower than the lowest known temperature in the universe
365 if (minStdAtmosphereTemp + TemperatureBias < minUniverseTemperature) {
366 double minBias = minUniverseTemperature - minStdAtmosphereTemp;
367 cerr << "The temperature bias " << TemperatureBias << " R is too low. "
368 << "It could result in temperatures below the absolute zero." << endl
369 << "Temperature bias is therefore capped to " << minBias << endl;
370 TemperatureBias = minBias;
371 }
372
373 CalculatePressureBreakpoints(SLpressure);
374
375 SLtemperature = GetTemperature(0.0);
376 CalculateSLSoundSpeedAndDensity();
377 }
378
379 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 // This function calculates a bias based on the supplied temperature for sea
381 // level. The bias is applied to the entire temperature profile at all altitudes.
382 // Internally, the Rankine scale is used for calculations, so any temperature
383 // supplied must be converted to that unit.
384
385 void FGStandardAtmosphere::SetTemperatureSL(double t, eTemperature unit)
386 {
387 SetTemperature(t, 0.0, unit);
388 }
389
390 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 // Sets a Sea Level temperature delta that is ramped out by 86 km (282,152 ft).
392
393 void FGStandardAtmosphere::SetSLTemperatureGradedDelta(eTemperature unit, double deltemp)
394 {
395 SetTemperatureGradedDelta(deltemp, 0.0, unit);
396 }
397
398 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 // Sets a temperature delta at the supplied altitude that is ramped out by 86 km.
400 // After this calculation is performed, the lapse rates and pressure breakpoints
401 // must be recalculated. Since we are calculating a delta here and not an actual
402 // temperature, we only need to be concerned about a scale factor and not
403 // the actual temperature itself.
404
405 void FGStandardAtmosphere::SetTemperatureGradedDelta(double deltemp, double h, eTemperature unit)
406 {
407 // Retrieve the minimum temperature in the standard atmosphere, may not be the
408 // last row in future if for example it's extended and maybe there is some
409 // temperature inversion layer etc. So run through and find the minimum.
410 const double minStdAtmosphereTemp = StdAtmosTemperatureTable.GetMinValue();
411 const double minDeltaTemperature = minStdAtmosphereTemp - StdSLtemperature;
412
413 if (unit == eCelsius || unit == eKelvin)
414 deltemp *= 1.80; // If temp delta "t" is given in metric, scale up to English
415
416 if (deltemp <= minDeltaTemperature) {
417 cerr << "The temperature delta " << deltemp << " R is too low. "
418 << "It could result in temperatures below the absolute zero." << endl
419 << "Temperature delta is therefore capped to " << minDeltaTemperature << endl;
420 deltemp = minDeltaTemperature;
421 }
422
423 TemperatureDeltaGradient = deltemp/(GradientFadeoutAltitude - GeopotentialAltitude(h));
424 CalculateLapseRates();
425 CalculatePressureBreakpoints(SLpressure);
426
427 SLtemperature = GetTemperature(0.0);
428 CalculateSLSoundSpeedAndDensity();
429 }
430
431 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432
433 void FGStandardAtmosphere::PrintStandardAtmosphereTable()
434 {
435 std::cout << "Altitude (ft) Temp (F) Pressure (psf) Density (sl/ft3)" << std::endl;
436 std::cout << "------------- -------- -------------- ----------------" << std::endl;
437 for (int i=0; i<280000; i+=1000) {
438 Calculate(i);
439 std::cout << std::setw(12) << std::setprecision(2) << i
440 << " " << std::setw(9) << std::setprecision(2) << Temperature - 459.67
441 << " " << std::setw(13) << std::setprecision(4) << Pressure
442 << " " << std::setw(18) << std::setprecision(8) << Density
443 << std::endl;
444 }
445
446 // Re-execute the Run() method to reset the calculated values
447 Run(false);
448 }
449
450 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 // This function calculates (or recalculates) the lapse rate over an altitude range
452 // where the "bh" in this case refers to the index of the base height in the
453 // StdAtmosTemperatureTable table. This function should be called anytime the
454 // temperature table is altered, such as when a gradient is applied across the
455 // temperature table for a range of altitudes.
456
457 void FGStandardAtmosphere::CalculateLapseRates()
458 {
459 unsigned int numRows = StdAtmosTemperatureTable.GetNumRows();
460 LapseRates.clear();
461
462 for (unsigned int bh=0; bh < numRows-1; bh++)
463 {
464 double t0 = StdAtmosTemperatureTable(bh+1,1);
465 double t1 = StdAtmosTemperatureTable(bh+2,1);
466 double h0 = StdAtmosTemperatureTable(bh+1,0);
467 double h1 = StdAtmosTemperatureTable(bh+2,0);
468 LapseRates.push_back((t1 - t0) / (h1 - h0) - TemperatureDeltaGradient);
469 }
470 }
471
472 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473
474 void FGStandardAtmosphere::CalculatePressureBreakpoints(double SLpress)
475 {
476 PressureBreakpoints[0] = SLpress;
477
478 for (unsigned int b=0; b<PressureBreakpoints.size()-1; b++) {
479 double BaseTemp = StdAtmosTemperatureTable(b+1,1);
480 double BaseAlt = StdAtmosTemperatureTable(b+1,0);
481 double UpperAlt = StdAtmosTemperatureTable(b+2,0);
482 double deltaH = UpperAlt - BaseAlt;
483 double Tmb = BaseTemp
484 + TemperatureBias
485 + (GradientFadeoutAltitude - BaseAlt)*TemperatureDeltaGradient;
486 if (LapseRates[b] != 0.00) {
487 double Lmb = LapseRates[b];
488 double Exp = g0 / (Rdry*Lmb);
489 double factor = Tmb/(Tmb + Lmb*deltaH);
490 PressureBreakpoints[b+1] = PressureBreakpoints[b]*pow(factor, Exp);
491 } else {
492 PressureBreakpoints[b+1] = PressureBreakpoints[b]*exp(-g0*deltaH/(Rdry*Tmb));
493 }
494 }
495 }
496
497 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498
499 void FGStandardAtmosphere::ResetSLTemperature()
500 {
501 TemperatureBias = TemperatureDeltaGradient = 0.0;
502 CalculateLapseRates();
503 CalculatePressureBreakpoints(SLpressure);
504
505 SLtemperature = StdSLtemperature;
506 CalculateSLSoundSpeedAndDensity();
507 }
508
509 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
510
511 void FGStandardAtmosphere::ResetSLPressure()
512 {
513 SLpressure = StdSLpressure;
514 CalculateSLDensity();
515 CalculatePressureBreakpoints(StdSLpressure);
516 }
517
518 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519
520 void FGStandardAtmosphere::CalculateStdDensityBreakpoints()
521 {
522 StdDensityBreakpoints.clear();
523 for (unsigned int i = 0; i < StdPressureBreakpoints.size(); i++)
524 StdDensityBreakpoints.push_back(StdPressureBreakpoints[i] / (Rdry * StdAtmosTemperatureTable(i + 1, 1)));
525 }
526
527 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528
529 double FGStandardAtmosphere::CalculateDensityAltitude(double density, double geometricAlt)
530 {
531 // Work out which layer we're dealing with
532 unsigned int b = 0;
533 for (; b < StdDensityBreakpoints.size() - 2; b++) {
534 if (density >= StdDensityBreakpoints[b + 1])
535 break;
536 }
537
538 // Get layer properties
539 double Tmb = StdAtmosTemperatureTable(b + 1, 1);
540 double Hb = StdAtmosTemperatureTable(b + 1, 0);
541 double Lmb = StdLapseRates[b];
542 double pb = StdDensityBreakpoints[b];
543
544 double density_altitude = 0.0;
545
546 // https://en.wikipedia.org/wiki/Barometric_formula for density solved for H
547 if (Lmb != 0.0) {
548 double Exp = -1.0 / (1.0 + g0/(Rdry*Lmb));
549 density_altitude = Hb + (Tmb / Lmb) * (pow(density / pb, Exp) - 1);
550 } else {
551 double Factor = -Rdry*Tmb / g0;
552 density_altitude = Hb + Factor * log(density / pb);
553 }
554
555 return GeometricAltitude(density_altitude);
556 }
557
558 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559
560 double FGStandardAtmosphere::CalculatePressureAltitude(double pressure, double geometricAlt)
561 {
562 // Work out which layer we're dealing with
563 unsigned int b = 0;
564 for (; b < StdPressureBreakpoints.size() - 2; b++) {
565 if (pressure >= StdPressureBreakpoints[b + 1])
566 break;
567 }
568
569 // Get layer properties
570 double Tmb = StdAtmosTemperatureTable(b + 1, 1);
571 double Hb = StdAtmosTemperatureTable(b + 1, 0);
572 double Lmb = StdLapseRates[b];
573 double Pb = StdPressureBreakpoints[b];
574
575 double pressure_altitude = 0.0;
576
577 if (Lmb != 0.00) {
578 // Equation 33(a) from ISA document solved for H
579 double Exp = -Rdry*Lmb / g0;
580 pressure_altitude = Hb + (Tmb / Lmb) * (pow(pressure / Pb, Exp) - 1);
581 } else {
582 // Equation 33(b) from ISA document solved for H
583 double Factor = -Rdry*Tmb / g0;
584 pressure_altitude = Hb + Factor * log(pressure / Pb);
585 }
586
587 return GeometricAltitude(pressure_altitude);
588 }
589
590 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591
592 double FGStandardAtmosphere::CalculateVaporPressure(double temperature)
593 {
594 double temperature_degC = RankineToCelsius(temperature);
595 return a*exp(b*temperature_degC/(c+temperature_degC));
596 }
597
598 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599
600 void FGStandardAtmosphere::ValidateVaporMassFraction(double h)
601 {
602 if (SaturatedVaporPressure < Pressure) {
603 double VaporPressure = Pressure*VaporMassFraction / (VaporMassFraction+Rdry/Rwater);
604 if (VaporPressure > SaturatedVaporPressure)
605 VaporMassFraction = Rdry * SaturatedVaporPressure / (Rwater * (Pressure - SaturatedVaporPressure));
606 }
607
608 double GeoPotAlt = GeopotentialAltitude(h);
609 double maxFraction = 1E-6*MaxVaporMassFraction.GetValue(GeoPotAlt);
610
611 if ((VaporMassFraction > maxFraction) || (VaporMassFraction < 0.0))
612 VaporMassFraction = maxFraction;
613
614 // Update the gas constant factor
615 Reng = (VaporMassFraction*Rwater + Rdry)/(1.0 + VaporMassFraction);
616 }
617
618 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619
620 void FGStandardAtmosphere::SetDewPoint(eTemperature unit, double dewpoint)
621 {
622 double dewPoint_R = ConvertToRankine(dewpoint, unit);
623 constexpr double minDewPoint = CelsiusToRankine(-c) + 1.0;
624
625 if (dewPoint_R <= minDewPoint) {
626 cerr << "The dew point temperature " << dewPoint_R << " is lower than "
627 << minDewPoint << " R." << endl
628 << "Dew point is therefore capped to " << minDewPoint << endl;
629 dewPoint_R = minDewPoint;
630 }
631
632 double VaporPressure = CalculateVaporPressure(dewPoint_R);
633 SetVaporPressure(ePSF, VaporPressure);
634
635 // double finalizedDewPoint = GetDewPoint(eRankine);
636 // if (finalizedDewPoint < dewPoint_R) {
637 // cerr << "Dew point temperature has been capped to " << finalizedDewPoint
638 // << endl;
639 // }
640 }
641
642 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643
644 double FGStandardAtmosphere::GetDewPoint(eTemperature to) const
645 {
646 double dewpoint_degC;
647 double VaporPressure = Pressure*VaporMassFraction / (VaporMassFraction+Rdry/Rwater);
648
649 if (VaporPressure <= 0.0)
650 dewpoint_degC = -c;
651 else {
652 double x = log(VaporPressure/a);
653 dewpoint_degC = c*x / (b - x);
654 }
655
656 return ConvertFromRankine(CelsiusToRankine(dewpoint_degC), to);
657 }
658
659 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
660
661 void FGStandardAtmosphere::SetVaporPressure(ePressure unit, double Pa)
662 {
663 double altitude = CalculatePressureAltitude(Pressure, 0.0);
664 double VaporPressure = ConvertToPSF(Pa, unit);
665 if (VaporPressure < 0.0) {
666 cerr << "The vapor pressure cannot be negative." << endl
667 << "Vapor pressure is set to 0.0" << endl;
668 VaporPressure = 0.0;
669 } else if (VaporPressure >= Pressure) {
670 cerr << "The vapor pressure " << VaporPressure
671 << " PSF is higher than the ambient pressure." << endl
672 << "Vapor pressure is therefore capped to " << Pressure-1.0 << endl;
673 VaporPressure = Pressure - 1.0;
674 }
675 VaporMassFraction = Rdry * VaporPressure / (Rwater * (Pressure - VaporPressure));
676 ValidateVaporMassFraction(altitude);
677 }
678
679 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680
681 double FGStandardAtmosphere::GetVaporPressure(ePressure to) const
682 {
683 double VaporPressure = Pressure*VaporMassFraction / (VaporMassFraction+Rdry/Rwater);
684 return ConvertFromPSF(VaporPressure, to);
685 }
686
687 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688
689 double FGStandardAtmosphere::GetSaturatedVaporPressure(ePressure to) const
690 {
691 return ConvertFromPSF(SaturatedVaporPressure, to);
692 }
693
694 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695
696 double FGStandardAtmosphere::GetRelativeHumidity(void) const
697 {
698 double VaporPressure = Pressure*VaporMassFraction / (VaporMassFraction+Rdry/Rwater);
699 return 100.0*VaporPressure/SaturatedVaporPressure;
700 }
701
702 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703
704 void FGStandardAtmosphere::SetRelativeHumidity(double RH)
705 {
706 if (RH < 0.0) {
707 cerr << "The relative humidity cannot be negative." << endl
708 << "Relative humidity is set to 0%" << endl;
709 RH = 0.0;
710 } else if (RH > 100.0) {
711 cerr << "The relative humidity cannot be higher than 100%." << endl
712 << "Relative humidity is set to 100%" << endl;
713 RH = 100.0;
714 }
715
716 double VaporPressure = 0.01*RH*SaturatedVaporPressure;
717 SetVaporPressure(ePSF, VaporPressure);
718 }
719
720 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721
722 double FGStandardAtmosphere::GetVaporMassFractionPPM(void) const
723 {
724 return VaporMassFraction*1E6;
725 }
726
727 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728
729 void FGStandardAtmosphere::SetVaporMassFractionPPM(double frac)
730 {
731 double altitude = CalculatePressureAltitude(Pressure, 0.0);
732 VaporMassFraction = frac*1E-6;
733 ValidateVaporMassFraction(altitude);
734
735 if (fabs(VaporMassFraction*1E6-frac)>1E-2) {
736 cerr << "The vapor mass fraction " << frac << " has been capped to "
737 << VaporMassFraction*1E6 << "PPM." << endl;
738 }
739 }
740
741 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
742
743 void FGStandardAtmosphere::bind(void)
744 {
745 typedef double (FGStandardAtmosphere::*PMFi)(int) const;
746 typedef void (FGStandardAtmosphere::*PMF)(int, double);
747 PropertyManager->Tie("atmosphere/delta-T", this, eRankine,
748 (PMFi)&FGStandardAtmosphere::GetTemperatureBias,
749 (PMF)&FGStandardAtmosphere::SetTemperatureBias);
750 PropertyManager->Tie("atmosphere/SL-graded-delta-T", this, eRankine,
751 (PMFi)&FGStandardAtmosphere::GetTemperatureDeltaGradient,
752 (PMF)&FGStandardAtmosphere::SetSLTemperatureGradedDelta);
753 PropertyManager->Tie("atmosphere/P-sl-psf", this, ePSF,
754 (PMFi)&FGStandardAtmosphere::GetPressureSL,
755 (PMF)&FGStandardAtmosphere::SetPressureSL);
756 PropertyManager->Tie("atmosphere/dew-point-R", this, eRankine,
757 (PMFi)&FGStandardAtmosphere::GetDewPoint,
758 (PMF)&FGStandardAtmosphere::SetDewPoint);
759 PropertyManager->Tie("atmosphere/vapor-pressure-psf", this, ePSF,
760 (PMFi)&FGStandardAtmosphere::GetVaporPressure,
761 (PMF)&FGStandardAtmosphere::SetVaporPressure);
762 PropertyManager->Tie("atmosphere/saturated-vapor-pressure-psf", this, ePSF,
763 (PMFi)&FGStandardAtmosphere::GetSaturatedVaporPressure);
764 PropertyManager->Tie("atmosphere/RH", this,
765 &FGStandardAtmosphere::GetRelativeHumidity,
766 &FGStandardAtmosphere::SetRelativeHumidity);
767 PropertyManager->Tie("atmosphere/vapor-fraction-ppm", this,
768 &FGStandardAtmosphere::GetVaporMassFractionPPM,
769 &FGStandardAtmosphere::SetVaporMassFractionPPM);
770 }
771
772 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 // The bitmasked value choices are as follows:
774 // unset: In this case (the default) JSBSim would only print
775 // out the normally expected messages, essentially echoing
776 // the config files as they are read. If the environment
777 // variable is not set, debug_lvl is set to 1 internally
778 // 0: This requests JSBSim not to output any messages
779 // whatsoever.
780 // 1: This value explicity requests the normal JSBSim
781 // startup messages
782 // 2: This value asks for a message to be printed out when
783 // a class is instantiated
784 // 4: When this value is set, a message is displayed when a
785 // FGModel object executes its Run() method
786 // 8: When this value is set, various runtime state variables
787 // are printed out periodically
788 // 16: When set various parameters are sanity checked and
789 // a message is printed out when they go out of bounds
790
791 void FGStandardAtmosphere::Debug(int from)
792 {
793 if (debug_lvl <= 0) return;
794
795 if (debug_lvl & 1) { // Standard console startup message output
796 if (from == 0) { // Constructor
797 }
798 }
799 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
800 if (from == 0) std::cout << "Instantiated: FGStandardAtmosphere" << std::endl;
801 if (from == 1) std::cout << "Destroyed: FGStandardAtmosphere" << std::endl;
802 }
803 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
804 }
805 if (debug_lvl & 8 ) { // Runtime state variables
806 }
807 if (debug_lvl & 16) { // Sanity checking
808 }
809 if (debug_lvl & 128) { //
810 }
811 if (debug_lvl & 64) {
812 if (from == 0) { // Constructor
813 }
814 }
815 }
816
817 } // namespace JSBSim