]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAerodynamics.cpp
JSBSim updates, including external atmosphere fix
[flightgear.git] / src / FDM / JSBSim / FGAerodynamics.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGAerodynamics.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/13/00
6  Purpose:      Encapsulates the aerodynamic forces (gear and collision)
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jsb@hal-pc.org) -------------
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
30 HISTORY
31 --------------------------------------------------------------------------------
32 09/13/00   JSB   Created
33 04/22/01   JSB   Moved code into here from FGAircraft
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGAerodynamics.h"
40 #include "FGFactorGroup.h"
41 #include "FGCoefficient.h"
42 #include "FGPropertyManager.h"
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_AERODYNAMICS;
46
47 const unsigned NAxes=6;                           
48 const char* AxisNames[] = { "drag", "side-force", "lift", "rolling-moment",
49                             "pitching-moment","yawing-moment" }; 
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55
56 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
57 {
58   Name = "FGAerodynamics";
59
60   AxisIdx["DRAG"]  = 0;
61   AxisIdx["SIDE"]  = 1;
62   AxisIdx["LIFT"]  = 2;
63   AxisIdx["ROLL"]  = 3;
64   AxisIdx["PITCH"] = 4;
65   AxisIdx["YAW"]   = 5;
66
67   Coeff = new CoeffArray[6];
68   
69   impending_stall = stall_hyst = 0.0;
70   alphaclmin = alphaclmax = 0.0;
71   alphahystmin = alphahystmax = 0.0;
72   clsq = lod = 0.0;
73   alphaw = 0.0;  
74   bind();
75
76   Debug(0);
77 }
78
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
81 FGAerodynamics::~FGAerodynamics()
82 {
83   unsigned int i,j;
84   
85   unbind();
86   
87   for (i=0; i<6; i++) {
88     for (j=0; j<Coeff[i].size(); j++) {
89       delete Coeff[i][j];
90     }
91   }
92   delete[] Coeff;
93   
94   Debug(1);
95 }
96
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98
99 bool FGAerodynamics::Run(void)
100 {
101   unsigned int axis_ctr,ctr;
102   double alpha, twovel;
103
104   if (!FGModel::Run()) {
105
106     twovel = 2*Translation->GetVt();
107     if (twovel > 0) {
108       bi2vel = Aircraft->GetWingSpan() / twovel;
109       ci2vel = Aircraft->Getcbar() / twovel;
110     }  
111     
112     alphaw = Translation->Getalpha() + Aircraft->GetWingIncidence();
113     
114     alpha = Translation->Getalpha();
115     
116     if (alphaclmax != 0) {
117       if (alpha > 0.85*alphaclmax) {
118         impending_stall = 10*(alpha/alphaclmax - 0.85);
119       } else {
120         impending_stall = 0;
121       }
122     }
123    
124     if (alphahystmax != 0.0 && alphahystmin != 0.0) {
125       if (alpha > alphahystmax) {
126          stall_hyst = 1;
127       } else if (alpha < alphahystmin) {
128          stall_hyst = 0;
129       }    
130     }
131  
132     vLastFs = vFs;
133     vFs.InitMatrix();
134
135     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
136       for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
137         vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
138       }
139     }
140
141     //correct signs of drag and lift to wind axes convention
142     //positive forward, right, down
143     if ( Translation->Getqbar() > 0) {
144       clsq = vFs(eLift) / (Aircraft->GetWingArea()*Translation->Getqbar());
145       clsq *= clsq;
146     }
147     if ( vFs(eDrag)  > 0) {
148       lod = vFs(eLift) / vFs(eDrag);
149     }  
150         
151     //correct signs of drag and lift to wind axes convention
152     //positive forward, right, down
153     vFs(eDrag)*=-1; vFs(eLift)*=-1;
154
155     vForces = State->GetTs2b()*vFs;
156
157     vDXYZcg(eX) = -(Aircraft->GetXYZrp(eX) 
158                       - MassBalance->GetXYZcg(eX))*inchtoft;
159     vDXYZcg(eY) =  (Aircraft->GetXYZrp(eY) 
160                       - MassBalance->GetXYZcg(eY))*inchtoft;
161     vDXYZcg(eZ) = -(Aircraft->GetXYZrp(eZ) 
162                       - MassBalance->GetXYZcg(eZ))*inchtoft;
163
164     vMoments = vDXYZcg*vForces; // M = r X F
165
166     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
167       for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
168         vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
169       }
170     }
171
172     return false;
173   } else {
174     return true;
175   }
176 }
177
178 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179
180 bool FGAerodynamics::Load(FGConfigFile* AC_cfg)
181 {
182   string parameter, axis, scratch;
183
184   AC_cfg->GetNextConfigLine();
185
186   while ((parameter = AC_cfg->GetValue()) != string("/AERODYNAMICS")) {
187     if (parameter == "AXIS") {
188       CoeffArray ca;
189       axis = AC_cfg->GetValue("NAME");
190       AC_cfg->GetNextConfigLine();
191       while ((parameter = AC_cfg->GetValue()) != string("/AXIS")) {
192         if ( parameter == "COEFFICIENT" ) {
193           ca.push_back( new FGCoefficient(FDMExec) );
194           ca.back()->Load(AC_cfg);
195         } else if ( parameter == "GROUP" ) {
196           ca.push_back( new FGFactorGroup(FDMExec) );
197           ca.back()->Load(AC_cfg);
198         }
199       }
200       Coeff[AxisIdx[axis]] = ca;
201       AC_cfg->GetNextConfigLine();
202     } else if (parameter == "AC_ALPHALIMITS") {
203       *AC_cfg >> scratch >> alphaclmin >> alphaclmax;
204       if (debug_lvl > 0) cout << "    Maximum Alpha: " << alphaclmax
205                               << "    Minimum Alpha: " << alphaclmin
206                               << endl;
207     } else if (parameter == "AC_HYSTLIMITS") {
208       *AC_cfg >> scratch >> alphahystmin >> alphahystmax;
209       if (debug_lvl > 0) cout << "    Hysteresis Start: " << alphahystmax
210                               << "    Hysteresis End: " << alphahystmin
211                               << endl;
212     }
213   }
214
215   bindModel();
216   
217   return true;
218 }
219
220 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221
222 string FGAerodynamics::GetCoefficientStrings(void)
223 {
224   string CoeffStrings = "";
225   bool firstime = true;
226   unsigned int axis, sd;
227
228   for (axis = 0; axis < 6; axis++) {
229     for (sd = 0; sd < Coeff[axis].size(); sd++) {
230       if (firstime) {
231         firstime = false;
232       } else {
233         CoeffStrings += ", ";
234       }
235       CoeffStrings += Coeff[axis][sd]->GetCoefficientName();
236     }
237   }
238   return CoeffStrings;
239 }
240
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242
243 string FGAerodynamics::GetCoefficientValues(void)
244 {
245   string SDValues = "";
246   bool firstime = true;
247
248   for (unsigned int axis = 0; axis < 6; axis++) {
249     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
250       if (firstime) {
251         firstime = false;
252       } else {
253         SDValues += ", ";
254       }
255       SDValues += Coeff[axis][sd]->GetSDstring();
256     }
257   }
258
259   return SDValues;
260 }
261
262 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263
264 void FGAerodynamics::bind(void)
265 {
266   typedef double (FGAerodynamics::*PMF)(int) const;
267
268   PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
269                        (PMF)&FGAerodynamics::GetForces);
270   PropertyManager->Tie("forces/fby-aero-lbs", this,2,
271                        (PMF)&FGAerodynamics::GetForces);
272   PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
273                        (PMF)&FGAerodynamics::GetForces);
274   PropertyManager->Tie("moments/l-aero-lbsft", this,1,
275                        (PMF)&FGAerodynamics::GetMoments);
276   PropertyManager->Tie("moments/m-aero-lbsft", this,2,
277                        (PMF)&FGAerodynamics::GetMoments);
278   PropertyManager->Tie("moments/n-aero-lbsft", this,3,
279                        (PMF)&FGAerodynamics::GetMoments);
280   PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
281                        (PMF)&FGAerodynamics::GetvFs);
282   PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
283                        (PMF)&FGAerodynamics::GetvFs);
284   PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
285                        (PMF)&FGAerodynamics::GetvFs);
286   PropertyManager->Tie("forces/lod-norm", this,
287                        &FGAerodynamics::GetLoD);
288   PropertyManager->Tie("aero/cl-squared-norm", this,
289                        &FGAerodynamics::GetClSquared); 
290   PropertyManager->Tie("aero/alpha-max-deg", this,
291                        &FGAerodynamics::GetAlphaCLMax,
292                        &FGAerodynamics::SetAlphaCLMax,
293                        true);
294   PropertyManager->Tie("aero/alpha-min-deg", this,
295                        &FGAerodynamics::GetAlphaCLMin,
296                        &FGAerodynamics::SetAlphaCLMin,
297                        true);
298   PropertyManager->Tie("aero/bi2vel", this,
299                        &FGAerodynamics::GetBI2Vel);
300   PropertyManager->Tie("aero/ci2vel", this,
301                        &FGAerodynamics::GetCI2Vel);
302   PropertyManager->Tie("aero/alpha-wing-rad", this,
303                        &FGAerodynamics::GetAlphaW);
304   PropertyManager->Tie("systems/stall-warn-norm", this,
305                         &FGAerodynamics::GetStallWarn);
306   PropertyManager->Tie("aero/stall-hyst-norm", this,
307                         &FGAerodynamics::GetHysteresisParm);
308 }
309
310 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311
312 void FGAerodynamics::bindModel(void)
313
314   unsigned i,j;
315   FGPropertyManager* node;
316   string axis_node_name;
317   node = PropertyManager->GetNode("aero/buildup",true);
318   for (i=0;i<NAxes;i++) {
319      node = node->GetNode( string(AxisNames[i]),true );
320      for (j=0; j < Coeff[i].size(); j++) {
321        Coeff[i][j]->bind(node);
322      } 
323      node = (FGPropertyManager*)node->getParent();                                         
324   }
325 }
326
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328
329 void FGAerodynamics::unbind(void)
330 {
331   unsigned i,j;
332
333   PropertyManager->Untie("forces/fbx-aero-lbs");
334   PropertyManager->Untie("forces/fby-aero-lbs");
335   PropertyManager->Untie("forces/fbz-aero-lbs");
336   PropertyManager->Untie("moments/l-aero-lbsft");
337   PropertyManager->Untie("moments/m-aero-lbsft");
338   PropertyManager->Untie("moments/n-aero-lbsft");
339   PropertyManager->Untie("forces/fwx-aero-lbs");
340   PropertyManager->Untie("forces/fwy-aero-lbs");
341   PropertyManager->Untie("forces/fwz-aero-lbs");
342   PropertyManager->Untie("forces/lod-norm");
343   PropertyManager->Untie("aero/cl-squared-norm");  
344   PropertyManager->Untie("aero/alpha-max-deg");
345   PropertyManager->Untie("aero/alpha-min-deg");
346   PropertyManager->Untie("aero/bi2vel");
347   PropertyManager->Untie("aero/ci2vel");
348   PropertyManager->Untie("aero/alpha-wing-rad");
349   PropertyManager->Untie("systems/stall-warn-norm");
350
351   for ( i=0; i<NAxes; i++ ) {
352      for ( j=0; j < Coeff[i].size(); j++ ) {
353        Coeff[i][j]->unbind();
354        
355      }
356   }
357 }
358
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 //    The bitmasked value choices are as follows:
361 //    unset: In this case (the default) JSBSim would only print
362 //       out the normally expected messages, essentially echoing
363 //       the config files as they are read. If the environment
364 //       variable is not set, debug_lvl is set to 1 internally
365 //    0: This requests JSBSim not to output any messages
366 //       whatsoever.
367 //    1: This value explicity requests the normal JSBSim
368 //       startup messages
369 //    2: This value asks for a message to be printed out when
370 //       a class is instantiated
371 //    4: When this value is set, a message is displayed when a
372 //       FGModel object executes its Run() method
373 //    8: When this value is set, various runtime state variables
374 //       are printed out periodically
375 //    16: When set various parameters are sanity checked and
376 //       a message is printed out when they go out of bounds
377
378 void FGAerodynamics::Debug(int from)
379 {
380   if (debug_lvl <= 0) return;
381
382   if (debug_lvl & 1) { // Standard console startup message output
383     if (from == 0) { // Constructor
384
385     }
386   }
387   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
388     if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
389     if (from == 1) cout << "Destroyed:    FGAerodynamics" << endl;
390   }
391   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
392   }
393   if (debug_lvl & 8 ) { // Runtime state variables
394   }
395   if (debug_lvl & 16) { // Sanity checking
396   }
397   if (debug_lvl & 64) {
398     if (from == 0) { // Constructor
399       cout << IdSrc << endl;
400       cout << IdHdr << endl;
401     }
402   }
403 }
404