]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Atmosphere.cpp
Patch from Melchior Franz:
[flightgear.git] / src / FDM / YASim / Atmosphere.cpp
1 #include "Math.hpp"
2 #include "Atmosphere.hpp"
3 namespace yasim {
4
5 // Copied from McCormick, who got it from "The ARDC Model Atmosphere"
6 // Note that there's an error in the text in the first entry,
7 // McCormick lists 299.16/101325/1.22500, but those don't agree with
8 // R=287.  I chose to correct the temperature to 288.20, since 79F is
9 // pretty hot for a "standard" atmosphere.
10 //                             meters   kelvin      Pa   kg/m^3
11 float Atmosphere::data[][4] = {{ 0.0f,     288.20f, 101325.0f, 1.22500f },
12                                {   900.0f, 282.31f,  90971.0f, 1.12260f },
13                                {  1800.0f, 276.46f,  81494.0f, 1.02690f },
14                                {  2700.0f, 270.62f,  72835.0f, 0.93765f },
15                                {  3600.0f, 264.77f,  64939.0f, 0.85445f },
16                                {  4500.0f, 258.93f,  57752.0f, 0.77704f },
17                                {  5400.0f, 253.09f,  51226.0f, 0.70513f },
18                                {  6300.0f, 247.25f,  45311.0f, 0.63845f },
19                                {  7200.0f, 241.41f,  39963.0f, 0.57671f },
20                                {  8100.0f, 235.58f,  35140.0f, 0.51967f },
21                                {  9000.0f, 229.74f,  30800.0f, 0.46706f },
22                                {  9900.0f, 223.91f,  26906.0f, 0.41864f },
23                                { 10800.0f, 218.08f,  23422.0f, 0.37417f },
24                                { 11700.0f, 216.66f,  20335.0f, 0.32699f },
25                                { 12600.0f, 216.66f,  17654.0f, 0.28388f },
26                                { 13500.0f, 216.66f,  15327.0f, 0.24646f },
27                                { 14400.0f, 216.66f,  13308.0f, 0.21399f },
28                                { 15300.0f, 216.66f,  11555.0f, 0.18580f },
29                                { 16200.0f, 216.66f,  10033.0f, 0.16133f },
30                                { 17100.0f, 216.66f,   8712.0f, 0.14009f },
31                                { 18000.0f, 216.66f,   7565.0f, 0.12165f },
32                                { 18900.0f, 216.66f,   6570.0f, 0.10564f }};
33
34 // Universal gas constant for air, in SI units.  P = R * rho * T.
35 // P in pascals (N/m^2), rho is kg/m^3, T in kelvin.
36 const float R = 287.1f;
37
38 // Specific heat ratio for air, at "low" temperatures.  
39 const float GAMMA = 1.4f;
40
41 float Atmosphere::getStdTemperature(float alt)
42 {
43     return getRecord(alt, 1);
44 }
45
46 float Atmosphere::getStdPressure(float alt)
47 {
48     return getRecord(alt, 2);
49 }
50
51 float Atmosphere::getStdDensity(float alt)
52 {
53     return getRecord(alt, 3);
54 }
55
56 float Atmosphere::calcVEAS(float spd, float pressure, float temp)
57 {
58     static float rho0 = getStdDensity(0);
59     float densityRatio = calcDensity(pressure, temp) / rho0;
60     return spd * Math::sqrt(densityRatio);
61 }
62
63 float Atmosphere::calcVCAS(float spd, float pressure, float temp)
64 {
65     // Stolen shamelessly from JSBSim.  Constants that appear:
66     //   2/5  == gamma-1
67     //   5/12 == 1/(gamma+1)
68     //   4/5  == 2*(gamma-1)
69     //  14/5  == 2*gamma
70     //  28/5  == 4*gamma
71     // 144/25 == (gamma+1)^2
72
73     float m2 = calcMach(spd, temp);
74     m2 = m2*m2; // mach^2
75
76     float cp; // pressure coefficient
77     if(m2 < 1) {
78         // (1+(mach^2)/5)^(gamma/(gamma-1))
79         cp = Math::pow(1+0.2*m2, 3.5);
80     } else {
81         float tmp0 = ((144.0f/25.0f) * m2) / (28.0f/5.0f*m2 - 4.0f/5.0f);
82         float tmp1 = ((14.0f/5.0f) * m2 - (2.0f/5.0f)) * (5.0f/12.0f);
83         cp = Math::pow(tmp0, 3.5) * tmp1;
84     }
85
86     // Conditions at sea level
87     float p0 = getStdPressure(0);
88     float rho0 = getStdDensity(0);
89
90     float tmp = Math::pow((pressure/p0)*(cp-1) + 1, (2/7.));
91     return Math::sqrt((7*p0/rho0)*(tmp-1));
92 }
93
94 float Atmosphere::calcDensity(float pressure, float temp)
95 {
96     return pressure / (R * temp);
97 }
98
99 float Atmosphere::calcMach(float spd, float temp)
100 {
101     return spd / Math::sqrt(GAMMA * R * temp);
102 }
103
104 void Atmosphere::calcStaticAir(float p0, float t0, float d0, float v,
105                                float* pOut, float* tOut, float* dOut)
106 {
107     const static float C0 = ((GAMMA-1)/(2*R*GAMMA));
108     const static float C1 = 1/(GAMMA-1);
109
110     *tOut = t0 + (v*v) * C0;
111     *dOut = d0 * Math::pow(*tOut / t0, C1);
112     *pOut = (*dOut) * R * (*tOut);
113 }
114
115 float Atmosphere::getRecord(float alt, int recNum)
116 {
117     int hi = (sizeof(data) / (4*sizeof(float))) - 1;
118     int lo = 0;
119
120     // safety valve, clamp to the edges of the table
121     if(alt < data[0][0])       hi=1;
122     else if(alt > data[hi][0]) lo = hi-1;
123
124     // binary search
125     while(1) {
126         if(hi-lo == 1) break;
127         int mid = (hi+lo)>>1;
128         if(alt < data[mid][0]) hi = mid;
129         else lo = mid;
130     }
131
132     // interpolate
133     float frac = (alt - data[lo][0])/(data[hi][0] - data[lo][0]);
134     float a = data[lo][recNum];
135     float b = data[hi][recNum];
136     return a + frac * (b-a);
137 }
138
139 }; // namespace yasim