]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIThermal.cxx
Vivian Meazza: AI escorts
[flightgear.git] / src / AIModel / AIThermal.cxx
1 // FGAIThermal - FGAIBase-derived class creates an AI thermal
2 //
3 // Copyright (C) 2004  David P. Culp - davidculp2@comcast.net
4 //
5 // An attempt to refine the thermal shape and behaviour by WooT 2009
6 //
7 // Copyright (C) 2009 Patrice Poly ( WooT )
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
29 #include <Scenery/scenery.hxx>
30 #include <string>
31 #include <math.h>
32
33 using std::string;
34
35 #include "AIThermal.hxx"
36
37
38 FGAIThermal::FGAIThermal() : FGAIBase(otThermal) {
39    max_strength = 6.0;
40    diameter = 0.5;
41    strength = factor = 0.0;
42    cycle_timer = 60*(rand()%31); // some random in the birth time
43    ground_elev_ft = 0.0;
44    dt_count=0.9;
45    alt=0.0;
46 }
47
48 FGAIThermal::~FGAIThermal() {
49 }
50
51 void FGAIThermal::readFromScenario(SGPropertyNode* scFileNode) {
52   if (!scFileNode)
53     return;
54
55   FGAIBase::readFromScenario(scFileNode);
56
57   setMaxStrength(scFileNode->getDoubleValue("strength-fps", 8.0)); 
58   setDiameter(scFileNode->getDoubleValue("diameter-ft", 0.0)/6076.11549); 
59   setHeight(scFileNode->getDoubleValue("height-msl", 5000.0));  
60 }
61
62 bool FGAIThermal::init(bool search_in_AI_path) {
63    factor = 8.0 * max_strength / (diameter * diameter * diameter);
64    setAltitude( height );
65    _surface_wind_from_deg_node =
66             fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg", true);
67    _surface_wind_speed_node =
68             fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt", true);
69    _aloft_wind_from_deg_node =
70             fgGetNode("/environment/config/aloft/entry[2]/wind-from-heading-deg", true);
71    _aloft_wind_speed_node =
72             fgGetNode("/environment/config/aloft/entry[2]/wind-speed-kt", true);
73     do_agl_calc = 1;
74    return FGAIBase::init(search_in_AI_path);
75 }
76
77 void FGAIThermal::bind() {
78         props->tie("position/altitude-agl-ft", // for debug and tweak
79                 SGRawValuePointer<double>(&altitude_agl_ft));
80         props->tie("alt-rel", // for debug and tweak
81                 SGRawValuePointer<double>(&alt_rel));
82         props->tie("time", // for debug and tweak
83                 SGRawValuePointer<double>(&time));
84         props->tie("xx", // for debug and tweak
85                 SGRawValuePointer<double>(&xx));
86         props->tie("is-forming", // for debug abd tweak
87                 SGRawValuePointer<bool>(&is_forming));
88         props->tie("is-formed", // for debug abd tweak
89                 SGRawValuePointer<bool>(&is_formed));
90         props->tie("is-dying", // for debug abd tweak
91                 SGRawValuePointer<bool>(&is_dying));
92         props->tie("is-dead", // for debug abd tweak
93                 SGRawValuePointer<bool>(&is_dead));
94     FGAIBase::bind();
95 }
96
97 void FGAIThermal::unbind() {
98         props->untie("position/altitude-agl-ft");
99         props->untie("alt-rel");
100         props->untie("time");   
101         props->untie("is-forming");
102         props->untie("is-formed");
103         props->untie("is-dying");
104         props->untie("is-dead");
105         props->untie("xx");
106     FGAIBase::unbind();
107 }
108
109
110 void FGAIThermal::update(double dt) {
111    FGAIBase::update(dt);
112    Run(dt);
113    Transform();
114 }
115
116
117
118 //the formula to get the available portion of VUpMax depending on altitude
119 //returns a double between 0 and 1
120 double FGAIThermal::get_strength_fac(double alt_frac) {
121
122 double PI = 4.0 * atan(1.0);
123 double fac = 0.0;
124 if ( alt_frac <=0.0 ) { // do submarines get thermals ?
125         fac = 0.0;
126         }
127 else if ( ( alt_frac>0.0 ) && (alt_frac<=0.1) ) { // ground layer
128         fac = ( 0.1*( pow( (10.0*alt_frac),10.0) ) );
129         }
130 else if ( ( alt_frac>0.1 ) && (alt_frac<=1.0) ) {   // main body of the thermal
131         fac = 0.4175 - 0.5825* ( cos ( PI*  (1.0-sqrt(alt_frac) ) +PI) ) ;
132         }
133 else if ( ( alt_frac >1.0 ) && (alt_frac < 1.1 ) ) {  //above the ceiling, but not above the cloud
134         fac = (0.5 * ( 1.0 + cos ( PI*( (-2.0*alt_frac)*5.0 ) ) ) );
135         }
136 else if ( alt_frac >= 1.1 ) {  //above the cloud
137         fac = 0.0;
138         }
139 return fac;
140 }
141
142
143 void FGAIThermal::Run(double dt) {
144
145 // *****************************************
146 // the thermal characteristics and variables
147 // *****************************************
148
149 cycle_timer += dt ;
150
151 // time
152
153 // the time needed for the thermal to be completely formed
154 double tmin1 = 5.0 ;
155 // the time when the thermal begins to die
156 double tmin2 = 20.0 ;
157 // the time when the thermal is completely dead
158 double tmin3 = 25.0;
159 double alive_cycle_time = tmin3*60.0;
160 //the time of the complete cycle, including a period of inactivity
161 double tmin4 = 30.0;
162 // some times expressed in a fraction of tmin3;
163 double t1 = tmin1/tmin3 ;
164 double t2 = tmin2/tmin3 ;
165 double t3 = 1.0 ;
166 double t4 = tmin4/tmin3;
167 // the time elapsed since the thermal was born, in a 0-1 fraction of tmin3
168
169 time = cycle_timer/alive_cycle_time;
170 //comment above and
171 //uncomment below to freeze the time cycle
172  time=0.5;
173
174 if ( time >= t4) { 
175         cycle_timer = 60*(rand()%31);
176         }
177
178
179 //the position of the thermal 'top'
180 double thermal_foot_lat = (pos.getLatitudeDeg());
181 double thermal_foot_lon = (pos.getLongitudeDeg());
182
183 //the max updraft one can expect in this thermal
184 double MaxUpdraft=max_strength;
185 //the max sink one can expect in this thermal, this is a negative number
186 double MinUpdraft=-max_strength*0.25;
187 //the fraction of MaxUpdraft one can expect at our height and time
188 double maxstrengthavail;
189 //max updraft at the user altitude and time
190 double v_up_max;
191 //min updraft at the user altitude and time, this is a negative number
192 double v_up_min;
193 double wind_speed;
194
195
196 //max radius of the the thermal, including the sink area
197 double Rmax = diameter/2.0;
198 // 'shaping' of the thermal, the higher, the more conical the thermal- between 0 and 1
199 double shaping=0.8;
200 //the radius of the thermal at our altitude in FT, including sink
201 double Rsink;
202 //the relative radius of the thermal where we have updraft, between 0 an 1
203 double r_up_frac=0.9;
204 //radius of the thermal where we have updraft, in FT
205 double Rup;
206 //how far are we from the thermal center at our altitude in FEET
207 double dist_center;
208
209 //the position of the center of the thermal slice at our altitude
210 double slice_center_lon;
211 double slice_center_lat;
212
213
214
215 // **************************************
216 // various variables relative to the user
217 // **************************************
218
219 double user_latitude  = manager->get_user_latitude();
220 double user_longitude = manager->get_user_longitude();
221 double user_altitude  = manager->get_user_altitude(); // MSL
222
223 //we need to know the thermal foot AGL altitude
224
225
226 //we could do this only once, as thermal don't move
227 //but then agl info is lost on user reset
228 //so we only do this every 10 seconds to save cpu
229 dt_count += dt;
230 if (dt_count >= 10.0 ) {
231         //double alt;
232         if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0)) {
233         ground_elev_ft =  alt * SG_METER_TO_FEET;
234         do_agl_calc = 0;
235         altitude_agl_ft = height - ground_elev_ft ;
236         dt_count = 0.0;
237         }
238 }
239
240 //user altitude relative to the thermal height, seen AGL from the thermal foot
241 if ( user_altitude < 1.0 ) { user_altitude = 1.0 ;}; // an ugly way to avoid NaNs for users at alt 0
242 double user_altitude_agl= ( user_altitude - ground_elev_ft ) ;
243 alt_rel = user_altitude_agl / altitude_agl_ft;
244
245
246
247 //the updraft user feels !
248 double Vup;
249
250 // *********************
251 // environment variables
252 // *********************
253
254 // the  wind heading at the user alt
255 double wind_heading_rad;
256
257 // the "ambient" sink outside thermals
258 double global_sink = -1.0;
259
260 // **************
261 // some constants
262 // **************
263
264 double PI = 4.0 * atan(1.0);
265
266
267 // ******************
268 // thermal main cycle
269 // ******************
270
271 //we get the max strenght proportion we can expect at the time and altitude, formuled between 0 and 1
272 //double xx;
273 if (time <= t1) {
274         xx= ( time / t1 );
275         maxstrengthavail = xx* get_strength_fac ( alt_rel / xx );
276
277         is_forming=1;is_formed=0;is_dying=0;is_dead=0;
278
279         }
280 else if ( (time > t1) && (time <= t2) ) {
281         maxstrengthavail = get_strength_fac ( (alt_rel) );
282
283         is_forming=0;is_formed=1;is_dying=0;is_dead=0;
284
285         }
286 else if ( (time > t2) && (time <= t3) ) {
287         xx= ( ( time - t2) / (1.0 - t2) ) ;
288         maxstrengthavail = get_strength_fac ( alt_rel - xx );
289
290         is_forming=0;is_formed=0;is_dying=1;is_dead=0;
291
292         }
293 else {
294         maxstrengthavail = 0.0;
295         is_forming=0;is_formed=0;is_dying=0;is_dead=1;
296
297         }
298
299 //we get the diameter of the thermal slice at the user altitude
300 //the thermal has a slight conic shape
301
302 if ( (alt_rel >= 0.0) && (alt_rel < 1.0 ) ) {
303         Rsink = ( shaping*Rmax ) + ( (  (1.0-shaping)*Rmax*alt_rel ) / altitude_agl_ft );  // in the main thermal body
304         }
305 else if ( (alt_rel >=1.0) && (alt_rel < 1.1) ) {
306         Rsink = (Rmax/2.0) * ( 1.0+ cos ( (10.0*PI*alt_rel)-(2.0*PI) ) ); // above the ceiling
307         }
308 else {
309         Rsink = 0.0; // above the cloud
310         }
311
312 //we get the portion of the diameter that produces lift
313 Rup = r_up_frac * Rsink ;
314
315 //we now determine v_up_max and VupMin depending on our altitude
316
317 v_up_max = maxstrengthavail * MaxUpdraft;
318 v_up_min = maxstrengthavail * MinUpdraft;
319
320 // Now we know, for current t and alt, v_up_max, VupMin, Rup, Rsink.
321
322 // We still need to know how far we are from the thermal center
323
324 // To determine the thermal inclinaison in the wind, we use a ugly approximation,
325 // in which we say the thermal bends 20° (0.34906 rad ) for 10 kts wind.
326 // We move the thermal foot upwind, to keep the cloud model over the "center" at ceiling level.
327 // the displacement distance of the center of the thermal slice, at user altitude,
328 // and relative to a hipothetical vertical thermal,  would be:
329
330 // get surface and 9000 ft wind
331
332 double ground_wind_from_deg = _surface_wind_from_deg_node->getDoubleValue();
333 double ground_wind_speed_kts  = _surface_wind_speed_node->getDoubleValue();
334 double aloft_wind_from_deg = _aloft_wind_from_deg_node->getDoubleValue();
335 double aloft_wind_speed_kts  = _aloft_wind_speed_node->getDoubleValue();
336
337 double ground_wind_from_rad = (PI/2.0) - PI*( ground_wind_from_deg/180.0);
338 double aloft_wind_from_rad = (PI/2.0) - PI*( aloft_wind_from_deg/180.0);
339
340 wind_heading_rad= PI+ 0.5*( ground_wind_from_rad + aloft_wind_from_rad );
341
342 wind_speed = ground_wind_speed_kts + user_altitude * ( (aloft_wind_speed_kts -ground_wind_speed_kts ) / 9000.0 );
343
344 double dt_center_alt = -(tan (0.034906*wind_speed)) * ( altitude_agl_ft-user_altitude_agl );
345
346 // now, lets find how far we are from this shifted slice
347
348 double dt_slice_lon_FT = ( dt_center_alt * cos ( wind_heading_rad ));
349 double dt_slice_lat_FT = ( dt_center_alt * sin ( wind_heading_rad ));
350
351 double dt_slice_lon = dt_slice_lon_FT / ft_per_deg_lon;
352 double dt_slice_lat = dt_slice_lat_FT / ft_per_deg_lat;
353
354 slice_center_lon = thermal_foot_lon + dt_slice_lon;
355 slice_center_lat = thermal_foot_lat + dt_slice_lat;
356
357 double dist_center_lon = fabs(slice_center_lon - user_longitude)* ft_per_deg_lon;
358 double dist_center_lat = fabs(slice_center_lat - user_latitude)* ft_per_deg_lat;
359
360 double dist_center_FT = sqrt ( dist_center_lon*dist_center_lon + dist_center_lat*dist_center_lat ); // feet
361
362 dist_center = dist_center_FT/ 6076.11549; //nautic miles
363
364
365 // Now we can calculate Vup
366
367 if ( max_strength >=0.0 ) { // this is a thermal
368
369         if ( ( dist_center >= 0.0 ) && ( dist_center < Rup ) ) {  //user is in the updraft area
370                 Vup = v_up_max * cos ( dist_center* PI/(2.0*Rup) );
371                 }
372         else if ( ( dist_center > Rup ) && ( dist_center <= ((Rup+Rsink)/2.0) ) ) { //user in the 1st half of the sink area
373                 Vup = v_up_min * cos (( dist_center - ( Rup+Rsink)/2.0 ) * PI / ( 2.0* (  ( Rup+Rsink)/2.0 -Rup )));
374                 }
375         else if ( ( dist_center > ((Rup+Rsink)/2.0) ) && dist_center <= Rsink ) {   // user in the 2nd half of the sink area
376                 Vup = ( global_sink + v_up_min )/2.0 + ( global_sink - v_up_min )/2.0 *cos ( (dist_center-Rsink) *PI/ ( (Rsink-Rup )/2.0) );
377                 }
378         else {  // outside the thermal
379                 Vup = global_sink;
380                 } 
381         }
382
383 else { // this is a sink, we don't want updraft on the sides, nor do we want to feel sink near or above ceiling and ground
384         if ( alt_rel <=1.1 ) {
385                 double fac =  ( 1.0 - ( 1.0 - 1.815*alt_rel)*( 1.0 - 1.815*alt_rel) );
386                 Vup = fac * (global_sink + ( ( v_up_max - global_sink )/2.0 ) * ( 1.0+cos ( dist_center* PI / Rmax ) )) ;
387                 }
388         else { Vup = global_sink; }
389 }
390
391 //correct for no global sink above clouds and outside thermals
392 if ( ( (alt_rel > 1.0) && (alt_rel <1.1)) && ( dist_center > Rsink ) ) {
393         Vup = global_sink * ( 11.0 -10.0 * alt_rel );
394         }
395 if ( alt_rel >= 1.1 ) { 
396         Vup = 0.0;
397         }
398
399 strength = Vup;
400 range = dist_center;
401
402 }
403
404
405