]> git.mxchange.org Git - flightgear.git/blob - src/FDM/LaRCsimIC.cxx
First steps in a weather reorganization. Note especially that
[flightgear.git] / src / FDM / LaRCsimIC.cxx
1 /*******************************************************************************
2  
3  Header:       LaRCsimIC.cxx
4  Author:       Tony Peden
5  Date started: 10/9/99
6  
7  ------------- Copyright (C) 1999  Anthony K. Peden (apeden@earthlink.net) -------------
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  
27
28 #include <simgear/compiler.h>
29
30 #include <math.h>
31 #include STL_IOSTREAM
32
33 #include "FDM/LaRCsimIC.hxx"
34 #include <FDM/LaRCsim/ls_cockpit.h>
35 #include <FDM/LaRCsim/ls_generic.h>
36 #include <FDM/LaRCsim/ls_interface.h>
37 #include <FDM/LaRCsim/atmos_62.h>
38 #include <FDM/LaRCsim/ls_constants.h>
39 #include <FDM/LaRCsim/ls_geodesy.h>
40
41 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
42 SG_USING_STD(cout);
43 SG_USING_STD(endl);
44 #endif
45
46
47 LaRCsimIC::LaRCsimIC(void) {
48   vt=vtg=vw=vc=ve=0;
49   mach=0;
50   alpha=beta=gamma=0;
51   theta=phi=psi=0;
52   altitude=runway_altitude=hdot=alt_agl=0;
53   latitude_gd=latitude_gc=longitude=0;
54   u=v=w=0;  
55   vnorth=veast=vdown=0;
56   vnorthwind=veastwind=vdownwind=0;
57   lastSpeedSet=lssetvt;
58   lastAltSet=lssetasl;
59   get_atmosphere();
60   ls_geod_to_geoc( latitude_gd,altitude,&sea_level_radius,&latitude_gc); 
61   
62 }
63
64
65 LaRCsimIC::~LaRCsimIC(void) {}
66
67 void LaRCsimIC::get_atmosphere(void) {
68   Altitude=altitude; //set LaRCsim variable
69   ls_atmos(Altitude, &density_ratio, &soundspeed, &T, &p);
70   rho=density_ratio*SEA_LEVEL_DENSITY;
71 }  
72
73 void LaRCsimIC::SetVcalibratedKtsIC( SCALAR tt) {
74     vc=tt*KTS_TO_FPS;
75     lastSpeedSet=lssetvc;
76     vt=sqrt(1/density_ratio*vc*vc);
77 }
78
79 void LaRCsimIC::SetVtrueFpsIC( SCALAR tt) {
80   vt=tt;
81   lastSpeedSet=lssetvt;
82 }
83
84 void LaRCsimIC::SetMachIC( SCALAR tt) {
85   mach=tt;
86   vt=mach*soundspeed;
87   lastSpeedSet=lssetmach;
88 }
89
90 void LaRCsimIC::SetVequivalentKtsIC(SCALAR tt) {
91   ve=tt*KTS_TO_FPS;
92   lastSpeedSet=lssetve;
93   vt=sqrt(SEA_LEVEL_DENSITY/rho)*ve;
94 }  
95
96 void LaRCsimIC::SetClimbRateFpmIC( SCALAR tt) {
97   SetClimbRateFpsIC(tt/60.0);
98 }
99
100 void LaRCsimIC::SetClimbRateFpsIC( SCALAR tt) {
101   vtg=vt+vw;
102   if(vtg > 0.1) {
103     hdot = tt - vdownwind;
104     gamma=asin(hdot/vtg);
105     getTheta();
106     vdown=-1*hdot;
107   }
108 }
109
110 void LaRCsimIC::SetFlightPathAngleRadIC( SCALAR tt) {
111   gamma=tt;
112   getTheta();
113   vtg=vt+vw;
114   hdot=vtg*sin(tt);
115   vdown=-1*hdot;
116 }
117
118 void LaRCsimIC::SetPitchAngleRadIC(SCALAR tt) { 
119   if(alt_agl < (DEFAULT_AGL_ALT + 0.1) || vt < 10 ) 
120     theta=DEFAULT_PITCH_ON_GROUND; 
121   else
122     theta=tt;  
123   getAlpha();
124 }
125
126 void LaRCsimIC::SetUVWFpsIC(SCALAR vu, SCALAR vv, SCALAR vw) {
127   u=vu; v=vv; w=vw;
128   vt=sqrt(u*u+v*v+w*w);
129   lastSpeedSet=lssetuvw;
130 }
131
132   
133 void LaRCsimIC::SetVNEDAirmassFpsIC(SCALAR north, SCALAR east, SCALAR down ) {
134   vnorthwind=north; veastwind=east; vdownwind=down;
135   vw=sqrt(vnorthwind*vnorthwind + veastwind*veastwind + vdownwind*vdownwind);
136   vtg=vt+vw;
137   SetClimbRateFpsIC(-1*(vdown+vdownwind));
138 }  
139
140 void LaRCsimIC::SetAltitudeFtIC( SCALAR tt) {
141   if(tt > (runway_altitude + DEFAULT_AGL_ALT)) {
142     altitude=tt;
143   } else {
144     altitude=runway_altitude + DEFAULT_AGL_ALT;
145     alt_agl=DEFAULT_AGL_ALT;
146     theta=DEFAULT_PITCH_ON_GROUND; 
147   }  
148   lastAltSet=lssetasl;
149   get_atmosphere();
150   //lets try to make sure the user gets what they intended
151
152   switch(lastSpeedSet) {
153   case lssetned:
154     calcVtfromNED();
155     break;  
156   case lssetuvw:
157   case lssetvt:
158     SetVtrueFpsIC(vt);
159     break;
160   case lssetvc:
161     SetVcalibratedKtsIC(vc*V_TO_KNOTS);
162     break;
163   case lssetve:
164     SetVequivalentKtsIC(ve*V_TO_KNOTS);
165     break;
166   case lssetmach:
167     SetMachIC(mach);
168     break;
169   }
170 }
171
172 void LaRCsimIC::SetAltitudeAGLFtIC( SCALAR tt) {
173   alt_agl=tt;
174   lastAltSet=lssetagl;
175   altitude=runway_altitude + alt_agl;
176 }
177
178 void LaRCsimIC::SetRunwayAltitudeFtIC( SCALAR tt) {
179   runway_altitude=tt;
180   if(lastAltSet == lssetagl) 
181       altitude = runway_altitude + alt_agl;
182 }  
183         
184 void LaRCsimIC::calcVtfromNED(void) {
185   //take the earth's rotation out of veast first
186   //float veastner = veast- OMEGA_EARTH*sea_level_radius*cos( latitude_gd );
187   float veastner=veast;
188   u = (vnorth - vnorthwind)*cos(theta)*cos(psi) + 
189       (veastner - veastwind)*cos(theta)*sin(psi) - 
190       (vdown - vdownwind)*sin(theta);
191   v = (vnorth - vnorthwind)*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
192       (veastner - veastwind)*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
193       (vdown - vdownwind)*sin(phi)*cos(theta);
194   w = (vnorth - vnorthwind)*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
195       (veastner - veastwind)*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
196       (vdown - vdownwind)*cos(phi)*cos(theta);
197   vt = sqrt(u*u + v*v + w*w);
198
199
200 void LaRCsimIC::calcNEDfromVt(void) {
201   float veastner;
202   
203   //get the body components of vt
204   u = GetUBodyFpsIC();
205   v = GetVBodyFpsIC();   
206   w = GetWBodyFpsIC();
207   
208   //transform them to local axes and add on the wind components
209   vnorth = u*cos(theta)*cos(psi) +
210            v*(sin(phi)*sin(theta)*cos(psi) - cos(phi)*sin(psi)) +
211            w*(cos(phi)*sin(theta)*cos(psi) + sin(phi)*sin(psi)) +
212            vnorthwind;
213   
214   //need to account for the earth's rotation here
215   veastner = u*cos(theta)*sin(psi) +
216              v*(sin(phi)*sin(theta)*sin(psi) + cos(phi)*cos(psi)) +
217              w*(cos(phi)*sin(theta)*sin(psi) - sin(phi)*cos(psi)) +
218              veastwind;
219   veast = veastner;
220   //veast = veastner + OMEGA_EARTH*sea_level_radius*cos( latitude_gd );  
221   
222   vdown =  u*sin(theta) +
223            v*sin(phi)*cos(theta) +
224            w*cos(phi)*cos(theta) +
225            vdownwind;
226 }           
227
228 void LaRCsimIC::SetVNEDFpsIC( SCALAR north, SCALAR east, SCALAR down) {
229   vnorth=north;
230   veast=east;
231   vdown=down;
232   SetClimbRateFpsIC(-1*vdown);
233   lastSpeedSet=lssetned;
234   calcVtfromNED();
235 }        
236   
237 void LaRCsimIC::SetLatitudeGDRadIC(SCALAR tt) {
238   latitude_gd=tt;
239   ls_geod_to_geoc(latitude_gd,altitude,&sea_level_radius, &latitude_gc);
240 }
241   
242 bool LaRCsimIC::getAlpha(void) {
243   bool result=false;
244   float guess=theta-gamma;
245   xlo=xhi=0;
246   xmin=-89;
247   xmax=89;
248   sfunc=&LaRCsimIC::GammaEqOfAlpha;
249   if(findInterval(0,guess)){
250     if(solve(&alpha,0)){
251       result=true;
252     }
253   }
254   return result;
255 }      
256
257       
258 bool LaRCsimIC::getTheta(void) {
259   bool result=false;
260   float guess=alpha+gamma;
261   xlo=xhi=0;
262   xmin=-89;xmax=89;
263   sfunc=&LaRCsimIC::GammaEqOfTheta;
264   if(findInterval(0,guess)){
265     if(solve(&theta,0)){
266       result=true;
267     }
268   }
269   return result;
270 }      
271   
272
273
274 SCALAR LaRCsimIC::GammaEqOfTheta( SCALAR theta_arg) {
275   SCALAR a,b,c;
276   
277   a=cos(alpha)*cos(beta)*sin(theta_arg);
278   b=sin(beta)*sin(phi);
279   c=sin(alpha)*cos(beta)*cos(phi);
280   return sin(gamma)-a+(b+c)*cos(theta_arg);
281 }
282
283 SCALAR LaRCsimIC::GammaEqOfAlpha( SCALAR alpha_arg) {
284   float a,b,c;
285   
286   a=cos(alpha_arg)*cos(beta)*sin(theta);
287   b=sin(beta)*sin(phi);
288   c=sin(alpha_arg)*cos(beta)*cos(phi);
289   return sin(gamma)-a+(b+c)*cos(theta);
290 }
291
292   
293
294
295
296
297 bool LaRCsimIC::findInterval( SCALAR x,SCALAR guess) {
298   //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
299
300   int i=0;
301   bool found=false;
302   float flo,fhi,fguess;
303   float lo,hi,step;
304   step=0.1;
305   fguess=(this->*sfunc)(guess)-x;
306   lo=hi=guess;
307   do {
308     step=2*step;
309     lo-=step;
310     hi+=step;
311     if(lo < xmin) lo=xmin;
312     if(hi > xmax) hi=xmax;
313     i++;
314     flo=(this->*sfunc)(lo)-x;
315     fhi=(this->*sfunc)(hi)-x;
316     if(flo*fhi <=0) {  //found interval with root
317       found=true;
318       if(flo*fguess <= 0) {  //narrow interval down a bit
319         hi=lo+step;    //to pass solver interval that is as
320         //small as possible
321       }
322       else if(fhi*fguess <= 0) {
323         lo=hi-step;
324       }
325     }
326     //cout << "findInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
327   }
328   while((found == 0) && (i <= 100));
329   xlo=lo;
330   xhi=hi;
331   return found;
332 }
333
334
335
336
337 bool LaRCsimIC::solve( SCALAR *y,SCALAR x) {  
338   float x1,x2,x3,f1,f2,f3,d,d0;
339   float eps=1E-5;
340   float const relax =0.9;
341   int i;
342   bool success=false;
343   
344    //initializations
345   d=1;
346   
347     x1=xlo;x3=xhi;
348     f1=(this->*sfunc)(x1)-x;
349     f3=(this->*sfunc)(x3)-x;
350     d0=fabs(x3-x1);
351   
352     //iterations
353     i=0;
354     while ((fabs(d) > eps) && (i < 100)) {
355       d=(x3-x1)/d0;
356       x2=x1-d*d0*f1/(f3-f1);
357       
358       f2=(this->*sfunc)(x2)-x;
359       //cout << "solve x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
360       //cout << "                " << f1 << "," << f2 << "," << f3 << endl;
361
362       if(fabs(f2) <= 0.001) {
363         x1=x3=x2;
364       } else if(f1*f2 <= 0.0) {
365         x3=x2;
366         f3=f2;
367         f1=relax*f1;
368       } else if(f2*f3 <= 0) {
369         x1=x2;
370         f1=f2;
371         f3=relax*f3;
372       }
373       //cout << i << endl;
374       i++;
375     }//end while
376     if(i < 100) {
377       success=true;
378       *y=x2;
379     }
380
381   //cout << "Success= " << success << " Vcas: " << vcas*V_TO_KNOTS << " Mach: " << x2 << endl;
382   return success;
383 }