1 // simulates ridge lift
3 // Written by Patrice Poly
4 // Copyright (C) 2009 Patrice Poly - p.polypa@gmail.com
7 // Entirely based on the paper :
8 // http://carrier.csi.cam.ac.uk/forsterlewis/soaring/sim/fsx/dev/sim_probe/sim_probe_paper.html
9 // by Ian Forster-Lewis, University of Cambridge, 26th December 2007
12 // This program is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of the
15 // License, or (at your option) any later version.
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // General Public License for more details.
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/util.hxx>
36 #include <Scenery/scenery.hxx>
43 #include "ridge_lift.hxx"
45 static string CreateIndexedPropertyName(string Property, int index)
47 std::stringstream str;
51 return Property + "[" + tmp + "]";
54 static inline double sign(double x) {
55 return x == 0 ? 0 : x > 0 ? 1.0 : -1.0;
58 static const double BOUNDARY1_m = 40.0;
61 FGRidgeLift::FGRidgeLift ()
63 dist_probe_m[0] = 0.0; // in meters
64 dist_probe_m[1] = 250.0;
65 dist_probe_m[2] = 750.0;
66 dist_probe_m[3] = 2000.0;
67 dist_probe_m[4] = -100.0;
74 FGRidgeLift::~FGRidgeLift()
79 void FGRidgeLift::init(void)
81 _ridge_lift_fps_node = fgGetNode("/environment/ridge-lift-fps", true);
82 _surface_wind_from_deg_node =
83 fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg"
85 _surface_wind_speed_node =
86 fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt"
88 _earth_radius_node = fgGetNode("/position/sea-level-radius-ft", true);
89 _user_longitude_node = fgGetNode("/position/longitude-deg", true);
90 _user_latitude_node = fgGetNode("/position/latitude-deg", true);
91 _user_altitude_ft_node = fgGetNode("/position/altitude-ft", true);
92 _user_altitude_agl_ft_node = fgGetNode("/position/altitude-agl-ft", true);
95 void FGRidgeLift::bind() {
98 for( int i = 0; i < 5; i++ ) {
99 prop = CreateIndexedPropertyName("/environment/ridge-lift/probe-elev-m", i );
100 fgTie( prop.c_str(), this, i, &FGRidgeLift::get_probe_elev_m); // read-only
102 prop = CreateIndexedPropertyName("/environment/ridge-lift/probe-lat-deg", i );
103 fgTie( prop.c_str(), this, i, &FGRidgeLift::get_probe_lat_deg); // read-only
105 prop = CreateIndexedPropertyName("/environment/ridge-lift/probe-lon-deg", i );
106 fgTie( prop.c_str(), this, i, &FGRidgeLift::get_probe_lon_deg); // read-only
109 for( int i = 0; i < 4; i++ ) {
110 prop = CreateIndexedPropertyName("/environment/ridge-lift/slope", i );
111 fgTie( prop.c_str(), this, i, &FGRidgeLift::get_slope); // read-only
115 void FGRidgeLift::unbind() {
118 for( int i = 0; i < 5; i++ ) {
120 prop = CreateIndexedPropertyName("/environment/ridge-lift/probe-elev-m", i );
121 fgUntie( prop.c_str() );
123 prop = CreateIndexedPropertyName("/environment/ridge-lift/probe-lat-deg", i );
124 fgUntie( prop.c_str() );
126 prop = CreateIndexedPropertyName("/environment/ridge-lift/probe-lon-deg", i );
127 fgUntie( prop.c_str() );
130 for( int i = 0; i < 4; i++ ) {
131 prop = CreateIndexedPropertyName("/environment/ridge-lift/slope", i );
132 fgUntie( prop.c_str() );
136 void FGRidgeLift::update(double dt) {
138 //get the windspeed at ground level
140 double ground_wind_from_rad = _surface_wind_from_deg_node->getDoubleValue() * SG_DEGREES_TO_RADIANS;
141 double ground_wind_speed_mps = _surface_wind_speed_node->getDoubleValue() * SG_NM_TO_METER / 3600;
147 double user_latitude_rad = _user_latitude_node->getDoubleValue() * SG_DEGREES_TO_RADIANS;
148 double user_longitude_rad = _user_longitude_node->getDoubleValue() * SG_DEGREES_TO_RADIANS;
150 double earth_rad_m = _earth_radius_node->getDoubleValue() * SG_FEET_TO_METER;
151 if( earth_rad_m < SG_EPSILON )
152 earth_rad_m = SG_EARTH_RAD * 1000;
154 // Placing the probes
156 for (int i = 0; i < sizeof(probe_lat_rad)/sizeof(probe_lat_rad[0]); i++) {
157 double probe_radius_ratio = dist_probe_m[i]/earth_rad_m;
159 probe_lat_rad[i] = asin(sin(user_latitude_rad)*cos(probe_radius_ratio)
160 +cos(user_latitude_rad)*sin(probe_radius_ratio)*cos(ground_wind_from_rad));
161 if (probe_lat_rad[i] < SG_EPSILON ) {
162 probe_lon_rad[i] = user_latitude_rad; // probe on a pole
164 probe_lon_rad[i] = fmod((user_longitude_rad+asin(sin(ground_wind_from_rad)
165 *sin(probe_radius_ratio)/cos(probe_lat_rad[i]))+SG_PI)
168 probe_lat_deg[i]= probe_lat_rad[i] * SG_RADIANS_TO_DEGREES;
169 probe_lon_deg[i]= probe_lon_rad[i] * SG_RADIANS_TO_DEGREES;
172 for (int i = 0; i < sizeof(probe_elev_m)/sizeof(probe_elev_m[0]); i++) {
173 double elevation = 0;
174 if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(
175 SGGeod::fromRad(probe_lon_rad[i],probe_lat_rad[i]), 20000), elevation, 0)) {
176 if ( elevation > 0.1 ) {
177 probe_elev_m[i] = elevation;
179 probe_elev_m[i] = 0.1 ;
182 probe_elev_m[i] = 0.1;
188 slope[0] = (probe_elev_m[0] - probe_elev_m[1]) / dist_probe_m[1];
189 slope[1] = (probe_elev_m[1] - probe_elev_m[2]) / dist_probe_m[2];
190 slope[2] = (probe_elev_m[2] - probe_elev_m[3]) / dist_probe_m[3];
191 slope[3] = (probe_elev_m[4] - probe_elev_m[0]) / -dist_probe_m[4];
193 for (int i = 0; i < sizeof(slope)/sizeof(slope[0]); i++)
194 adj_slope[i] = sin(atan(5.0 * pow ( (fabs(slope[i])),1.7) ) ) *sign(slope[i]);
197 adj_slope[0] = 0.2 * adj_slope[0];
198 adj_slope[1] = 0.2 * adj_slope[1];
199 if ( adj_slope [2] < 0.0 ) {
200 adj_slope[2] = 0.5 * adj_slope[2];
205 if ( ( adj_slope [0] >= 0.0 ) && ( adj_slope [3] < 0.0 ) ) {
208 adj_slope[3] = 0.2 * adj_slope[3];
210 lift_factor = adj_slope[0]+adj_slope[1]+adj_slope[2]+adj_slope[3];
216 //user altitude above ground
217 double user_altitude_agl_m = _user_altitude_agl_ft_node->getDoubleValue() * SG_FEET_TO_METER;
220 double boundary2_m = 130.0; // in the lift
221 if (lift_factor < 0.0) { // in the sink
222 double highest_probe_temp= max ( probe_elev_m[1], probe_elev_m[2] );
223 double highest_probe_downwind_m= max ( highest_probe_temp, probe_elev_m[3] );
224 boundary2_m = highest_probe_downwind_m - probe_elev_m[0];
228 if ( user_altitude_agl_m < BOUNDARY1_m ) {
229 agl_factor = 0.5+0.5*user_altitude_agl_m /BOUNDARY1_m ;
230 } else if ( user_altitude_agl_m < boundary2_m ) {
233 agl_factor = exp(-(2 + probe_elev_m[0] / 2000) *
234 (user_altitude_agl_m - boundary2_m) / max(probe_elev_m[0],200.0));
237 double lift_mps = lift_factor* ground_wind_speed_mps * agl_factor;
239 //the updraft, finally, in ft per second
240 strength = fgGetLowPass( strength, lift_mps * SG_METER_TO_FEET, dt );
241 // if(isnan(strength)) strength=0;
242 _ridge_lift_fps_node->setDoubleValue( strength );