]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGWeatherUtils.h
- FGBinding now extends FGConditional
[flightgear.git] / src / WeatherCM / FGWeatherUtils.h
1 /*****************************************************************************
2
3  Header:       FGWeatherUtils.h 
4  Author:       Christian Mayer
5  Date started: 28.05.99
6
7  -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  details.
18
19  You should have received a copy of the GNU General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 FUNCTIONAL DESCRIPTION
27 ------------------------------------------------------------------------------
28 Utilities for the weather calculation like converting formulas
29
30 HISTORY
31 ------------------------------------------------------------------------------
32 02.06.1999 Christian Mayer      Created
33 08.06.1999 Christian Mayer      Changed sat_vp
34 16.06.1999 Durk Talsma          Portability for Linux
35 20.06.1999 Christian Mayer      added lots of consts
36 11.10.1999 Christian Mayer      changed set<> to map<> on Bernie Bright's 
37                                 suggestion
38 19.10.1999 Christian Mayer      change to use PLIB's sg instead of Point[2/3]D
39                                 and lots of wee code cleaning
40 17.01.2000 Christian Mayer      Added conversion routines make it easier for
41                                 JSBsim to use the weather database.
42 18.05.2000 Christian Mayer      Added function for geting the density when
43                                 temperature and pressure are given
44 *****************************************************************************/
45
46 /****************************************************************************/
47 /* SENTRY                                                                   */
48 /****************************************************************************/
49 #ifndef FGWeatherUtils_H
50 #define FGWeatherUtils_H
51
52 /****************************************************************************/
53 /* INCLUDES                                                                 */
54 /****************************************************************************/
55 #include <math.h>
56                 
57 /****************************************************************************/
58 /* DEFINES                                                                  */
59 /****************************************************************************/
60
61 /****************************************************************************/
62 /*assuming as given:                                                        */
63 /*                                                                          */
64 /* t:       temperature in °C                                               */
65 /* p:       preasure in mbar                                                */
66 /* //abs_hum: absoloute humidity in g/m^3,                                  */
67 /* act_vp:  actual vapor pressure pascal                                    */
68 /*                                                                          */
69 /* Calculated vaues:                                                        */
70 /* //max_hum: maximum of humidity in g/m^3,                                 */
71 /* sat_vp:  saturated vapor pressure in pascal                              */
72 /* rel_hum: relative humidity in %                                          */
73 /* dp:      dew point in °C                                                 */
74 /* wb:      approximate wetbulp in °C                                       */
75 /*                                                                          */
76 /* NOTE: Pascal is the SI unit for pressure and is defined as Pa = N/m^2    */
77 /*       1 mbar = 1 hPa = 100 Pa                                            */
78 /* NOTE: °C isn't a SI unit, so I should use °K instead. But as all         */
79 /*       formulas are given in °C and the weather database only uses        */
80 /*       'normal' temperatures, I've kept it in °C                          */
81 /****************************************************************************/
82
83 #define SAT_VP_CONST1 610.483125
84 #define SAT_VP_CONST2 7.444072452
85 #define SAT_VP_CONST3 235.3120919
86
87 inline WeatherPrecision sat_vp(const WeatherPrecision temp)
88 {
89     //old:
90     //return 6.112 * pow( 10, (7.5*dp)/(237.7+dp) );    //in mbar
91
92     //new:
93     //advantages: return the result as SI unit pascal and the constants
94     //are choosen that the correct results are returned for 0°C, 20°C and
95     //100°C. By 100°C I'm now returning a preasure of 1013.25 hPa
96     return SAT_VP_CONST1 * pow( 10, (SAT_VP_CONST2*temp)/(SAT_VP_CONST3+temp) );    //in pascal
97 }
98
99 inline WeatherPrecision rel_hum(const WeatherPrecision act_vp, const WeatherPrecision sat_vp)
100 {
101     return (act_vp / sat_vp) * 100;     //in %
102 }
103
104 inline WeatherPrecision dp(const WeatherPrecision sat_vp)
105 {
106     return (SAT_VP_CONST3*log10(sat_vp/SAT_VP_CONST1))/(SAT_VP_CONST2-log10(sat_vp/SAT_VP_CONST1)); //in °C 
107 }
108
109 inline WeatherPrecision wb(const WeatherPrecision t, const WeatherPrecision p, const WeatherPrecision dp)
110 {
111     WeatherPrecision e = sat_vp(dp); 
112     WeatherPrecision tcur, tcvp, peq, diff;
113     WeatherPrecision tmin, tmax;
114
115     if (t > dp)
116     {
117         tmax = t;
118         tmin = dp;
119     }
120     else
121     {
122         tmax = dp;
123         tmin = t;
124     }
125
126     while (true) 
127     { 
128         tcur=(tmax+tmin)/2;
129         tcvp=sat_vp(tcur);
130         
131         peq = 0.000660*(1+0.00155*tcur)*p*(t-tcur);
132         
133         diff = peq-tcvp+e;
134         
135         if (fabs(diff) < 0.01) 
136             return tcur;        //in °C
137         
138         if (diff < 0) 
139             tmax=tcur; 
140         else 
141             tmin=tcur; 
142     }; 
143
144 }
145
146 // Assume that we've got an ideal gas in normal altitudes
147 inline WeatherPrecision Density(const WeatherPrecision AirPressure, const WeatherPrecision Temperature )
148 {
149     const float rho0 = 1.293; /*density for air in normal altitudes at 0°C and 1013 mbar*/
150
151     return rho0 * 273.15 * AirPressure / (101300.0 * Temperature);
152 }
153
154 inline WeatherPrecision Celsius             (const WeatherPrecision celsius)
155 {
156     return celsius + 273.15;                            //Kelvin
157 }
158
159 inline WeatherPrecision Fahrenheit          (const WeatherPrecision fahrenheit)
160 {
161     return (fahrenheit * 9.0 / 5.0) + 32.0 + 273.15;    //Kelvin
162 }
163
164 inline WeatherPrecision Kelvin2Celsius      (const WeatherPrecision kelvin)
165 {
166     return kelvin - 273.15;                             //Celsius
167 }
168
169 inline WeatherPrecision Kelvin2Fahrenheit   (const WeatherPrecision kelvin)
170 {
171     return ((kelvin - 273.15) * 9.0 / 5.0) + 32.0;      //Fahrenheit
172 }
173
174 inline WeatherPrecision Celsius2Fahrenheit  (const WeatherPrecision celsius)
175 {
176     return (celsius * 9.0 / 5.0) + 32.0;                //Fahrenheit
177 }
178
179 inline WeatherPrecision Fahrenheit2Celsius  (const WeatherPrecision fahrenheit)
180 {
181     return (fahrenheit - 32.0) * 5.0 / 9.0;             //Celsius
182 }
183
184 inline WeatherPrecision Torr2Pascal         (const WeatherPrecision torr)
185 {
186     return (101325.0/760.0)*torr;                       //Pascal
187 }
188
189 inline WeatherPrecision Rankine2Kelvin      (const WeatherPrecision Rankine)
190 {
191     return (5.0 / 9.0) * Rankine;                       //Kelvin
192 }
193
194 inline WeatherPrecision JSBsim2SIdensity    (const WeatherPrecision JSBsim)
195 {
196     return JSBsim / 0.0019403203;                       //kg / cubic metres
197 }
198
199 inline WeatherPrecision psf2Pascal          (const WeatherPrecision psf)
200 {
201     return psf / 0.0020885434;                          //lbs / square foot (used in JSBsim)
202 }
203
204 inline WeatherPrecision Kelvin2Rankine      (const WeatherPrecision kelvin)
205 {
206     return (9.0 / 5.0) * kelvin;                        //Rankine (used in JSBsim)
207 }
208
209 inline WeatherPrecision SIdensity2JSBsim    (const WeatherPrecision SIdensity)
210 {
211     return 0.0019403203 * SIdensity;                    //slug / cubic feet (used in JSBsim)
212 }
213
214 inline WeatherPrecision Pascal2psf          (const WeatherPrecision Pascal)
215 {
216     return 0.0020885434 * Pascal;                       //lbs / square feet (used in JSBsim)
217 }
218
219 /****************************************************************************/
220 /* CLASS DECLARATION                                                        */
221 /****************************************************************************/
222
223 /****************************************************************************/
224 #endif /*FGWeatherUtils_H*/