1 /*******************************************************************************
3 Header: FGInitialCondition.cpp
7 ------------- Copyright (C) 1999 Anthony K. Peden (apeden@earthlink.net) -------------
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
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
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.
23 Further information about the GNU General Public License can also be found on
24 the world wide web at http://www.gnu.org.
28 --------------------------------------------------------------------------------
32 FUNCTIONAL DESCRIPTION
33 --------------------------------------------------------------------------------
35 The purpose of this class is to take a set of initial conditions and provide
36 a kinematically consistent set of body axis velocity components, euler
37 angles, and altitude. This class does not attempt to trim the model i.e.
38 the sim will most likely start in a very dynamic state (unless, of course,
39 you have chosen your IC's wisely) even after setting it up with this class.
41 CAVEAT: This class makes use of alpha=theta-gamma. This means that setting
42 any of the three with this class is only valid for steady state
43 (all accels zero) and zero pitch rate. One example where this
44 would produce invalid results is setting up for a trim in a pull-up
45 or pushover (both have nonzero pitch rate). Maybe someday...
47 ********************************************************************************
49 *******************************************************************************/
51 #include "FGInitialCondition.h"
52 #include "FGFDMExec.h"
54 #include "FGAtmosphere.h"
56 #include "FGAircraft.h"
57 #include "FGTranslation.h"
58 #include "FGRotation.h"
59 #include "FGPosition.h"
60 #include "FGAuxiliary.h"
64 FGInitialCondition::FGInitialCondition(FGFDMExec *FDMExec) {
73 if(FDMExec != NULL ) {
75 fdmex->GetPosition()->Seth(altitude);
76 fdmex->GetAtmosphere()->Run();
78 cout << "FGInitialCondition: This class requires a pointer to an valid FGFDMExec object" << endl;
84 FGInitialCondition::~FGInitialCondition(void) {}
87 void FGInitialCondition::SetVcalibratedKtsIC(float tt) {
89 if(getMachFromVcas(&mach,tt*KTSTOFPS)) {
90 //cout << "Mach: " << mach << endl;
93 vt=mach*fdmex->GetAtmosphere()->GetSoundSpeed();
94 ve=vt*sqrt(fdmex->GetAtmosphere()->GetDensityRatio());
95 //cout << "Vt: " << vt*FPSTOKTS << " Vc: " << vc*FPSTOKTS << endl;
98 cout << "Failed to get Mach number for given Vc and altitude, Vc unchanged." << endl;
99 cout << "Please mail the set of initial conditions used to apeden@earthlink.net" << endl;
103 void FGInitialCondition::SetVequivalentKtsIC(float tt) {
106 vt=ve*1/sqrt(fdmex->GetAtmosphere()->GetDensityRatio());
107 mach=vt/fdmex->GetAtmosphere()->GetSoundSpeed();
111 void FGInitialCondition::SetVtrueKtsIC(float tt) {
114 mach=vt/fdmex->GetAtmosphere()->GetSoundSpeed();
116 ve=vt*sqrt(fdmex->GetAtmosphere()->GetDensityRatio());
119 void FGInitialCondition::SetMachIC(float tt) {
121 lastSpeedSet=setmach;
122 vt=mach*fdmex->GetAtmosphere()->GetSoundSpeed();
124 ve=vt*sqrt(fdmex->GetAtmosphere()->GetDensityRatio());
125 //cout << "Vt: " << vt*FPSTOKTS << " Vc: " << vc*FPSTOKTS << endl;
130 void FGInitialCondition::SetClimbRateFpmIC(float tt) {
138 void FGInitialCondition::SetUBodyFpsIC(float tt) {
140 vt=sqrt(u*u+v*v+w*w);
144 void FGInitialCondition::SetVBodyFpsIC(float tt) {
146 vt=sqrt(u*u+v*v+w*w);
150 void FGInitialCondition::SetWBodyFpsIC(float tt) {
152 vt=sqrt(u*u+v*v+w*w);
157 void FGInitialCondition::SetAltitudeFtIC(float tt) {
159 fdmex->GetPosition()->Seth(altitude);
160 fdmex->GetAtmosphere()->Run();
162 //lets try to make sure the user gets what they intended
164 switch(lastSpeedSet) {
166 SetVtrueKtsIC(vt*FPSTOKTS);
169 SetVcalibratedKtsIC(vc*FPSTOKTS);
172 SetVequivalentKtsIC(ve*FPSTOKTS);
180 float FGInitialCondition::calcVcas(float Mach) {
182 float p=fdmex->GetAtmosphere()->GetPressure();
183 float psl=fdmex->GetAtmosphere()->GetPressureSL();
184 float rhosl=fdmex->GetAtmosphere()->GetDensitySL();
187 if(Mach < 1) //calculate total pressure assuming isentropic flow
188 pt=p*pow((1 + 0.2*Mach*Mach),3.5);
190 // shock in front of pitot tube, we'll assume its normal and use
191 // the Rayleigh Pitot Tube Formula, i.e. the ratio of total
192 // pressure behind the shock to the static pressure in front
195 //the normal shock assumption should not be a bad one -- most supersonic
196 //aircraft place the pitot probe out front so that it is the forward
197 //most point on the aircraft. The real shock would, of course, take
198 //on something like the shape of a rounded-off cone but, here again,
199 //the assumption should be good since the opening of the pitot probe
200 //is very small and, therefore, the effects of the shock curvature
201 //should be small as well. AFAIK, this approach is fairly well accepted
202 //within the aerospace community
204 B = 5.76*Mach*Mach/(5.6*Mach*Mach - 0.8);
206 // The denominator above is zero for Mach ~ 0.38, for which
207 // we'll never be here, so we're safe
209 D = (2.8*Mach*Mach-0.4)*0.4167;
213 A = pow(((pt-p)/psl+1),0.28571);
214 vcas = sqrt(7*psl/rhosl*(A-1));
215 //cout << "calcVcas: vcas= " << vcas*FPSTOKTS << " mach= " << Mach << " pressure: " << pt << endl;
219 bool FGInitialCondition::findMachInterval(float *mlo, float *mhi, float vcas) {
220 //void find_interval(inter_params &ip,eqfunc f,float y,float constant, int &flag){
224 float flo,fhi,fguess;
225 float lo,hi,guess,step;
228 fguess=calcVcas(guess)-vcas;
237 flo=calcVcas(lo)-vcas;
238 fhi=calcVcas(hi)-vcas;
239 if(flo*fhi <=0) { //found interval with root
241 if(flo*fguess <= 0) { //narrow interval down a bit
242 hi=lo+step; //to pass solver interval that is as
245 else if(fhi*fguess <= 0) {
249 //cout << "findMachInterval: i=" << i << " Lo= " << lo << " Hi= " << hi << endl;
251 while((found == 0) && (i <= 100));
259 bool FGInitialCondition::getMachFromVcas(float *Mach,float vcas) {
262 float x1,x2,x3,f1,f2,f3,d,d0;
264 float const relax =0.9;
275 if(findMachInterval(&x1,&x3,vcas)) {
278 f1=calcVcas(x1)-vcas;
279 f3=calcVcas(x3)-vcas;
284 while ((fabs(d) > eps) && (i < 100)) {
285 //cout << "getMachFromVcas x1,x2,x3: " << x1 << "," << x2 << "," << x3 << endl;
287 x2=x1-d*d0*f1/(f3-f1);
289 f2=calcVcas(x2)-vcas;
294 } else if(f2*f3 <= 0) {
308 //cout << "Success= " << success << " Vcas: " << vcas*FPSTOKTS << " Mach: " << x2 << endl;