]> git.mxchange.org Git - flightgear.git/blob - src/FDM/Balloon/BalloonSim.cpp
Tweaks to go along with changes in SGSocket.
[flightgear.git] / src / FDM / Balloon / BalloonSim.cpp
1 /*****************************************************************************
2
3  Module:       BalloonSim.cpp
4  Author:       Christian Mayer
5  Date started: 01.09.99
6  Called by:    
7
8  -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 ------------------------------------------------------------------------------
29 A hot air balloon simulator
30
31 HISTORY
32 ------------------------------------------------------------------------------
33 01.09.1999 Christian Mayer      Created
34 03.10.1999 Christian Mayer      cleaned  the code  by moveing  WeatherDatabase 
35                                 calls inside the update()
36 *****************************************************************************/
37
38 /****************************************************************************/
39 /* INCLUDES                                                                 */
40 /****************************************************************************/
41 #include <stdio.h>
42 // #include <conio.h>
43 #include <math.h>
44
45 #include <simgear/constants.h>
46
47 #include <Aircraft/aircraft.hxx>
48 #include <Main/options.hxx>
49 #include <WeatherCM/FGLocalWeatherDatabase.h>
50
51 #include "BalloonSim.h"
52
53 #include <plib/sg.h>
54
55 /****************************************************************************/
56 /********************************** CODE ************************************/
57 /****************************************************************************/
58
59 /****************************************************************************/
60 /*                                                                          */
61 /*  Constructor:                                                            */
62 /*  Set the balloon model to some values that seem reasonable as I haven't  */
63 /*  got much original values                                                */
64 /*                                                                          */
65 /****************************************************************************/
66 balloon::balloon()
67 {
68     dt = 0.1;
69     ground_level = 3400.0;
70
71     sgSetVec3(gravity_vector, 0.0, 0.0, -9.81);
72     sgSetVec3(velocity, 0.0, 0.0, 0.0);
73     sgSetVec3(position, 0.0, 0.0, 0.0);
74     sgSetVec3(hpr,      0.0, 0.0, 0.0);
75
76     /************************************************************************/
77     /* My balloon  has a  radius of  8.8 metres  as that  gives a  envelope */
78     /* volume of about 2854 m^3  which is  about 77000 ft^3,  a very common */
79     /* size for hot air balloons                                            */
80     /************************************************************************/
81
82     balloon_envelope_area = 4.0 * (8.8 * 8.8) * FG_PI; 
83     balloon_envelope_volume = (4.0/3.0) * (8.8 * 8.8 * 8.8) * FG_PI;
84
85     wind_facing_area_of_balloon = FG_PI * (8.8 * 8.8);
86     wind_facing_area_of_basket = 2.0;   //guessed: 2 m^2
87     
88     cw_envelope=0.45;                   //a sphere in this case
89     cw_basket=0.8;
90
91     weight_of_total_fuel = 40.0;        //big guess
92     weight_of_envelope = 200.0;         //big guess
93     weight_of_basket = 40.0;            //big guess
94     weight_of_cargo = 750.0;            //big guess
95
96     fuel_left=1.0;
97     max_flow_of_fuel_per_second=10.0*1.0/3600.0; //assuming 10% of one hour of total burn time
98     current_burner_strength = 0.0;      //the throttle
99
100     lambda = 0.15;                      //for plasic
101     l_of_the_envelope = 1.0/1000.0;     //the thickness of the envelope (in m): 1mm
102
103     T = 273.16 + 130.6;                 //Temperature in the envelope => still at ground level
104 }
105
106 void balloon::update()
107 {
108     /************************************************************************/
109     /* I'm  simplifying  the  balloon by  reducing the  simulation  to  two */
110     /* points:                                                              */  
111     /* the center of the basket (CB) and the center of the envelope (CE)    */
112     /*                                                                      */
113     /*                                 ce                                   */
114     /*                                 I                                    */
115     /*                                 I                                    */
116     /*                                 cg (=center of gravity)              */
117     /*                                 I                                    */
118     /*                                 cb                                   */
119     /*                                                                      */
120     /* On each center  are forces acting:  gravity and  wind resitance.  CE */
121     /* additionally got the lift  (=> I need to calculate the weight of the */
122     /* air inside, too)                                                     */
123     /*                                                                      */
124     /* The weight of the air  in the envelope is  dependant of the tempera- */
125     /* ture. This temperature is decreasing over the time that is dependant */
126     /* of the insulation  of the envelope  material (lambda),  the gas used */
127     /* (air) and the wind speed. For a plane surface it's for air:          */
128     /*                                                                      */
129     /*    alpha = 4.8 + 3.4*v   with v < 5.0 m/s                            */
130     /*                                                                      */
131     /* The value k that takes all of that into account is defined as:       */
132     /*                                                                      */
133     /*    1 / k = 1 / alpha1 + 1 / alpha2 + l / lambda                      */
134     /*                                                                      */
135     /* with 'l' beeing the 'length' i.e. thickness of the insulator, alpha1 */
136     /* the air inside and alpha2 the air outside of the envelope.  So our k */
137     /* is:                                                                  */
138     /*                                                                      */
139     /*    k = 1 / [1/4.8 + 1/(4.8+3.4v) + l/lambda]                         */
140     /*                                                                      */
141     /* The energy lost by this process is defined as:                       */
142     /*                                                                      */
143     /*    dQ = k * A * t * dT                                               */
144     /*                                                                      */
145     /* with Q being  the energy,  k that value  defined above,  A the total */
146     /* area of the envelope,  t the time (in hours)  and dT the temperature */
147     /* difference between the inside and the outside.                       */
148     /* To get the temperature of the air in the inside I need the formula:  */
149     /*                                                                      */
150     /*    dQ = cAir * m * dT                                                */
151     /*                                                                      */
152     /* with cAir being the specific heat capacity(?)  of air (= 1.00 kcal / */
153     /* kg * degree),  m the mass of the air and  dT the temperature change. */
154     /* As the envelope is open I'm  assuming that the  same air pressure is */
155     /* inside and  outside of  it  (practical there  should  be  a slightly */
156     /* higher air pressure  in the inside or the envelope  would collapse). */
157     /* So it's easy to calculate the density of the air inside:             */
158     /*                                                                      */
159     /*    rho = p / R * T                                                   */
160     /*                                                                      */
161     /* with p being  the pressure,  R the gas constant(?)  which is for air */
162     /* 287.14 N * m / kg * K and T the absolute temperature.                */
163     /*                                                                      */
164     /* The value returned by this function is the displacement of the CB    */
165     /************************************************************************/
166
167     /************************************************************************/
168     /* NOTE:  This is the simplified version:  I'm assuming that  the whole */
169     /* balloon consists only of the envelope.I will improove the simulation */
170     /* later, but currently was my main concern to get it going...          */
171     /************************************************************************/
172
173     sgVec3 v;
174
175     //get the current wind velocity and store it in v
176     //Point3D temp = WeatherDatabase->get(position).Wind;
177     //sgSetVec3(v, temp.x(), temp.y(), temp.z());
178     sgCopyVec3(v, WeatherDatabase->get(position).Wind );
179
180     sgSubVec3(v, velocity);
181     float speed = sgLengthVec3(v);
182     
183     // calculate the density of the gas inside
184     double rho = WeatherDatabase->get(position).AirPressure / (287.14 * T);
185
186     // calculate the mass of the air
187     double mAir = rho * balloon_envelope_volume;
188
189     // loss of energy by cooling down:
190     float k = 1.0 / (1.0/4.8 + 1.0/(4.8+3.4*speed) + l_of_the_envelope/lambda);
191     float Q = k * balloon_envelope_area * (dt/3600.0) * (WeatherDatabase->get(position).Temperature - T);   //(dt/3600.0) = time since last call in hours
192
193     // gain of energy by heating:
194     if (fuel_left > 0.0)        //but only with some fuel left ;-)
195     {
196         float fuel_burning = current_burner_strength * max_flow_of_fuel_per_second * dt * weight_of_total_fuel; //in kg
197     
198         //convert to cubemetres (I'm wrongly assuming 'normal' conditions; but that's correct for my special case)
199         float cube_metres_burned = fuel_burning / 2.2;  //2.2 is the density for propane
200
201         fuel_left -= fuel_burning / weight_of_total_fuel;
202
203         // get energy through burning. 
204         Q += 22250.0 * cube_metres_burned;  //22250 for propan, 29500 would be butane and if you dare: 2580 would be hydrogen...
205     }
206
207     // calculate the new temperature in the inside:
208     T += Q / (1.00 * mAir);
209
210     //calculate the masses of the envelope and the basket
211     float mEnvelope = mAir + weight_of_envelope;
212     float mBasket   = weight_of_total_fuel*fuel_left + weight_of_basket + weight_of_cargo;
213
214     float mTotal = mEnvelope + mBasket;
215
216     //calulate the forces
217     sgVec3 fTotal, fFriction, fLift;
218
219     sgScaleVec3(fTotal, gravity_vector, mTotal);
220     sgScaleVec3(fFriction, v, cw_envelope * wind_facing_area_of_balloon * WeatherDatabase->getAirDensity(position) * speed / 2.0);  //wind resistance
221     sgScaleVec3(fLift, gravity_vector, -balloon_envelope_volume * WeatherDatabase->get(position).AirPressure / (287.14 * WeatherDatabase->get(position).Temperature));
222    
223     sgAddVec3(fTotal, fLift);
224     sgAddVec3(fTotal, fFriction);
225     
226     //claculate acceleration: a = F / m
227     sgVec3 aTotal, vTotal, dTotal;
228
229     sgScaleVec3(aTotal, fTotal, 1.0 / mTotal);
230
231     //integrate the displacement: d = 0.5 * a * dt**2 + v * dt + d
232     sgScaleVec3(vTotal, velocity, dt); 
233     sgScaleVec3(dTotal, aTotal, 0.5*dt*dt); sgAddVec3(dTotal, vTotal);
234
235     //integrate the velocity to 'velocity': v = a * dt + v
236     sgScaleVec3(vTotal, aTotal, dt); sgAddVec3(velocity, vTotal);
237
238     /************************************************************************/
239     /* VERY WRONG STUFF: it's just here to get some results to start with   */  
240     /************************************************************************/
241
242     // care for the ground
243     if (position[2] < (ground_level+0.001) )
244         position[2] = ground_level;
245
246     //return results
247     sgAddVec3(position, dTotal);
248
249     //cout << "BallonSim: T: " << (T-273.16) << " alt: " << position[2] << " ground: " << ground_level << " throttle: " << current_burner_strength << "\n";
250 }
251
252 void balloon::set_burner_strength(const float bs)
253 {
254     if ((bs>=0.0) && (bs<=1.0))
255     current_burner_strength = bs;
256 }
257
258 void balloon::getVelocity(sgVec3 v) const
259 {
260     sgCopyVec3(v, velocity);
261 }
262
263 void balloon::setVelocity(const sgVec3 v)
264 {
265     sgCopyVec3(velocity, v);
266 }
267
268 void balloon::getPosition(sgVec3 v) const
269 {
270     sgCopyVec3(v, position);
271 }
272
273 void balloon::setPosition(const sgVec3 v)
274 {
275     sgCopyVec3(position, v);
276 }
277
278 void balloon::getHPR(sgVec3 angles) const //the balloon isn't allways exactly vertical
279 {
280     sgCopyVec3(angles, hpr);
281 }
282
283 void balloon::setHPR(const sgVec3 angles)  //the balloon isn't allways exactly vertical
284 {
285     sgCopyVec3(hpr, angles);
286 }
287
288 void balloon::setGroundLevel(const float altitude)
289 {
290     ground_level = altitude;
291 }
292
293 float balloon::getTemperature(void) const
294 {
295     return T;
296 }
297
298 float balloon::getFuelLeft(void) const
299 {
300     return fuel_left;
301 }
302
303 /*
304 void main(void)
305 {
306     bool burner = true;
307     balloon bal;
308     bool exit = false;
309     sgVec3 pos={0.0, 0.0, 0.0};
310     char c;
311     float acc_dt = 0.0;
312     float alt;
313
314 bool hysteresis = false; // moving up
315     for (;!exit;)
316     {
317         for (int i=0; i<100; i++)
318         {
319             bal.update(0.1); acc_dt += 0.1;
320             bal.getPosition(pos);
321             alt = pos[2];
322
323             if (alt > 3010) 
324             {
325                 hysteresis = true;
326                 burner = false;
327             }
328             if ((alt < 2990) && (hysteresis == true))
329             {
330                 hysteresis = false;
331                 burner = true;
332             }
333             if ((bal.getTemperature()-273.16)>250.0)
334                 burner = false; //emergency
335         }
336
337         // toogle burner
338         c = getch();
339         if (c==' ')
340             burner=!burner;
341         //if (c=='s')
342         //    show=!show;
343
344         //printf("Position: (%f/%f/%f), dP: (%f/%f/%f), burner: ", pos[0], pos[1], pos[2], dp[0], dp[1], dp[2]);
345         printf("%f         \t%f         \t%f         \t%f\n", acc_dt/60.0, bal.getTemperature()-273.16, pos[2], bal.getFuelLeft());
346         if (burner==true)
347         {
348             //printf("on\n");
349             bal.set_burner_strength(1.0);
350         }
351         else
352         {
353             //printf("off\n");
354             bal.set_burner_strength(0.0);
355         }
356
357     }
358 }
359 */