]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAerodynamics.cpp
New version of JSBSim, a big rewrite.
[flightgear.git] / src / FDM / JSBSim / models / 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
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jon@jsbsim.org) -------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser 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 Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser 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 Lesser 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 <iostream>
40 #include <sstream>
41 #include <iomanip>
42 #include <cstdlib>
43 #include "FGFDMExec.h"
44 #include "FGAerodynamics.h"
45 #include "input_output/FGPropertyManager.h"
46
47 using namespace std;
48
49 namespace JSBSim {
50
51 static const char *IdSrc = "$Id: FGAerodynamics.cpp,v 1.41 2011/08/04 12:46:32 jberndt Exp $";
52 static const char *IdHdr = ID_AERODYNAMICS;
53
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 CLASS IMPLEMENTATION
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57
58
59 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
60 {
61   Name = "FGAerodynamics";
62
63   AxisIdx["DRAG"]   = 0;
64   AxisIdx["SIDE"]   = 1;
65   AxisIdx["LIFT"]   = 2;
66   AxisIdx["ROLL"]   = 3;
67   AxisIdx["PITCH"]  = 4;
68   AxisIdx["YAW"]    = 5;
69
70   AxisIdx["AXIAL"]  = 0;
71   AxisIdx["NORMAL"] = 2;
72
73   AxisIdx["X"] = 0;
74   AxisIdx["Y"] = 1;
75   AxisIdx["Z"] = 2;
76
77   axisType = atNone;
78
79   AeroFunctions = new AeroFunctionArray[6];
80
81   impending_stall = stall_hyst = 0.0;
82   alphaclmin = alphaclmax = 0.0;
83   alphahystmin = alphahystmax = 0.0;
84   clsq = lod = 0.0;
85   alphaw = 0.0;
86   bi2vel = ci2vel = 0.0;
87   AeroRPShift = 0;
88   vDeltaRP.InitMatrix();
89
90   bind();
91
92   Debug(0);
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 FGAerodynamics::~FGAerodynamics()
98 {
99   unsigned int i,j;
100
101   for (i=0; i<6; i++)
102     for (j=0; j<AeroFunctions[i].size(); j++)
103       delete AeroFunctions[i][j];
104
105   delete[] AeroFunctions;
106
107   delete AeroRPShift;
108
109   Debug(1);
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 bool FGAerodynamics::InitModel(void)
115 {
116   if (!FGModel::InitModel()) return false;
117
118   impending_stall = stall_hyst = 0.0;
119   alphaclmin = alphaclmax = 0.0;
120   alphahystmin = alphahystmax = 0.0;
121   clsq = lod = 0.0;
122   alphaw = 0.0;
123   bi2vel = ci2vel = 0.0;
124   AeroRPShift = 0;
125   vDeltaRP.InitMatrix();
126
127   return true;
128 }
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131 bool FGAerodynamics::Run(bool Holding)
132 {
133
134   if (FGModel::Run(Holding)) return true;
135   if (Holding) return false; // if paused don't execute
136
137   unsigned int axis_ctr, ctr;
138   const double twovel=2*in.Vt;
139
140   RunPreFunctions();
141
142   // calculate some oft-used quantities for speed
143
144   if (twovel != 0) {
145     bi2vel = in.Wingspan / twovel;
146     ci2vel = in.Wingchord / twovel;
147   }
148   alphaw = in.Alpha + in.Wingincidence;
149   qbar_area = in.Wingarea * in.Qbar;
150
151   if (alphaclmax != 0) {
152     if (in.Alpha > 0.85*alphaclmax) {
153       impending_stall = 10*(in.Alpha/alphaclmax - 0.85);
154     } else {
155       impending_stall = 0;
156     }
157   }
158
159   if (alphahystmax != 0.0 && alphahystmin != 0.0) {
160     if (in.Alpha > alphahystmax) {
161        stall_hyst = 1;
162     } else if (in.Alpha < alphahystmin) {
163        stall_hyst = 0;
164     }
165   }
166
167   vFw.InitMatrix();
168   vFnative.InitMatrix();
169
170   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
171     for (ctr=0; ctr < AeroFunctions[axis_ctr].size(); ctr++) {
172       vFnative(axis_ctr+1) += AeroFunctions[axis_ctr][ctr]->GetValue();
173     }
174   }
175
176   // Note that we still need to convert to wind axes here, because it is
177   // used in the L/D calculation, and we still may want to look at Lift
178   // and Drag.
179
180   switch (axisType) {
181     case atBodyXYZ:       // Forces already in body axes; no manipulation needed
182       vFw = in.Tb2w*vFnative;
183       vForces = vFnative;
184       break;
185     case atLiftDrag:      // Copy forces into wind axes
186       vFw = vFnative;
187       vFw(eDrag)*=-1; vFw(eLift)*=-1;
188       vForces = in.Tw2b*vFw;
189       break;
190     case atAxialNormal:   // Convert native forces into Axial|Normal|Side system
191       vFw = in.Tb2w*vFnative;
192       vFnative(eX)*=-1; vFnative(eZ)*=-1;
193       vForces = vFnative;
194       break;
195     default:
196       cerr << endl << "  A proper axis type has NOT been selected. Check "
197                    << "your aerodynamics definition." << endl;
198       exit(-1);
199   }
200
201   // Calculate lift coefficient squared
202   if ( in.Qbar > 0) {
203     clsq = vFw(eLift) / (in.Wingarea*in.Qbar);
204     clsq *= clsq;
205   }
206
207   // Calculate lift Lift over Drag
208   if ( fabs(vFw(eDrag)) > 0.0) lod = fabs( vFw(eLift) / vFw(eDrag) );
209
210   // Calculate aerodynamic reference point shift, if any. The shift
211   // takes place in the body axis. That is, if the shift is negative,
212   // it is towards the back (tail) of the vehicle. The AeroRPShift
213   // function should be non-dimensionalized by the wing chord. The
214   // calculated vDeltaRP will be in feet.
215   if (AeroRPShift) vDeltaRP(eX) = AeroRPShift->GetValue()*in.Wingchord;
216
217   vDXYZcg = in.RPBody + vDeltaRP;
218
219   vMoments = vDXYZcg*vForces; // M = r X F
220
221   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
222     for (ctr = 0; ctr < AeroFunctions[axis_ctr+3].size(); ctr++) {
223       vMoments(axis_ctr+1) += AeroFunctions[axis_ctr+3][ctr]->GetValue();
224     }
225   }
226
227   RunPostFunctions();
228
229   return false;
230 }
231
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233
234 bool FGAerodynamics::Load(Element *element)
235 {
236   string parameter, axis, scratch;
237   string scratch_unit="";
238   string fname="", file="";
239   Element *temp_element, *axis_element, *function_element;
240
241   string separator = "/";
242
243   fname = element->GetAttributeValue("file");
244   if (!fname.empty()) {
245     file = FDMExec->GetFullAircraftPath() + separator + fname;
246     document = LoadXMLDocument(file);
247     if (document == 0L) return false;
248   } else {
249     document = element;
250   }
251
252   FGModel::Load(document); // Perform base class Pre-Load
253
254   DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
255
256   Debug(2);
257
258   if ((temp_element = document->FindElement("alphalimits"))) {
259     scratch_unit = temp_element->GetAttributeValue("unit");
260     if (scratch_unit.empty()) scratch_unit = "RAD";
261     alphaclmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
262     alphaclmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
263   }
264
265   if ((temp_element = document->FindElement("hysteresis_limits"))) {
266     scratch_unit = temp_element->GetAttributeValue("unit");
267     if (scratch_unit.empty()) scratch_unit = "RAD";
268     alphahystmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
269     alphahystmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
270   }
271
272   if ((temp_element = document->FindElement("aero_ref_pt_shift_x"))) {
273     function_element = temp_element->FindElement("function");
274     AeroRPShift = new FGFunction(PropertyManager, function_element);
275   }
276
277   axis_element = document->FindElement("axis");
278   while (axis_element) {
279     AeroFunctionArray ca;
280     axis = axis_element->GetAttributeValue("name");
281     function_element = axis_element->FindElement("function");
282     while (function_element) {
283       string current_func_name = function_element->GetAttributeValue("name");
284       try {
285         ca.push_back( new FGFunction(PropertyManager, function_element) );
286       } catch (string const str) {
287         cerr << endl << fgred << "Error loading aerodynamic function in " 
288              << current_func_name << ":" << str << " Aborting." << reset << endl;
289         return false;
290       }
291       function_element = axis_element->FindNextElement("function");
292     }
293     AeroFunctions[AxisIdx[axis]] = ca;
294     axis_element = document->FindNextElement("axis");
295   }
296
297   PostLoad(document, PropertyManager); // Perform base class Post-Load
298
299   return true;
300 }
301
302 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 //
304 // This private class function checks to verify consistency in the choice of
305 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or 
306 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
307 // Note that if the "SIDE" axis specifier is entered first in a config file, 
308 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
309 // This is OK, and the warning is due to the SIDE specifier used for both
310 // the Lift/Drag and Axial/Normal axis systems.
311
312 void FGAerodynamics::DetermineAxisSystem()
313 {
314   Element* axis_element = document->FindElement("axis");
315   string axis;
316   while (axis_element) {
317     axis = axis_element->GetAttributeValue("name");
318     if (axis == "LIFT" || axis == "DRAG") {
319       if (axisType == atNone) axisType = atLiftDrag;
320       else if (axisType != atLiftDrag) {
321         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
322                      << " aircraft config file. (LIFT DRAG)" << endl;
323       }
324     } else if (axis == "SIDE") {
325       if (axisType != atNone && axisType != atLiftDrag && axisType != atAxialNormal) {
326         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
327                      << " aircraft config file. (SIDE)" << endl;
328       }
329     } else if (axis == "AXIAL" || axis == "NORMAL") {
330       if (axisType == atNone) axisType = atAxialNormal;
331       else if (axisType != atAxialNormal) {
332         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
333                      << " aircraft config file. (NORMAL AXIAL)" << endl;
334       }
335     } else if (axis == "X" || axis == "Y" || axis == "Z") {
336       if (axisType == atNone) axisType = atBodyXYZ;
337       else if (axisType != atBodyXYZ) {
338         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
339                      << " aircraft config file. (XYZ)" << endl;
340       }
341     } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
342       cerr << endl << "  An unknown axis type, " << axis << " has been specified"
343                    << " in the aircraft configuration file." << endl;
344       exit(-1);
345     }
346     axis_element = document->FindNextElement("axis");
347   }
348   if (axisType == atNone) {
349     axisType = atLiftDrag;
350     cerr << endl << "  The aerodynamic axis system has been set by default"
351                  << " to the Lift/Side/Drag system." << endl;
352   }
353 }
354
355 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356
357 string FGAerodynamics::GetAeroFunctionStrings(const string& delimeter) const
358 {
359   string AeroFunctionStrings = "";
360   bool firstime = true;
361   unsigned int axis, sd;
362
363   for (axis = 0; axis < 6; axis++) {
364     for (sd = 0; sd < AeroFunctions[axis].size(); sd++) {
365       if (firstime) {
366         firstime = false;
367       } else {
368         AeroFunctionStrings += delimeter;
369       }
370       AeroFunctionStrings += AeroFunctions[axis][sd]->GetName();
371     }
372   }
373   return AeroFunctionStrings;
374 }
375
376 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377
378 string FGAerodynamics::GetAeroFunctionValues(const string& delimeter) const
379 {
380   ostringstream buf;
381
382   for (unsigned int axis = 0; axis < 6; axis++) {
383     for (unsigned int sd = 0; sd < AeroFunctions[axis].size(); sd++) {
384       if (buf.tellp() > 0) buf << delimeter;
385       buf << setw(9) << AeroFunctions[axis][sd]->GetValue();
386     }
387   }
388
389   return buf.str();
390 }
391
392 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393
394 void FGAerodynamics::bind(void)
395 {
396   typedef double (FGAerodynamics::*PMF)(int) const;
397
398   PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
399                        (PMF)&FGAerodynamics::GetForces);
400   PropertyManager->Tie("forces/fby-aero-lbs", this,2,
401                        (PMF)&FGAerodynamics::GetForces);
402   PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
403                        (PMF)&FGAerodynamics::GetForces);
404   PropertyManager->Tie("moments/l-aero-lbsft", this,1,
405                        (PMF)&FGAerodynamics::GetMoments);
406   PropertyManager->Tie("moments/m-aero-lbsft", this,2,
407                        (PMF)&FGAerodynamics::GetMoments);
408   PropertyManager->Tie("moments/n-aero-lbsft", this,3,
409                        (PMF)&FGAerodynamics::GetMoments);
410   PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
411                        (PMF)&FGAerodynamics::GetvFw);
412   PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
413                        (PMF)&FGAerodynamics::GetvFw);
414   PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
415                        (PMF)&FGAerodynamics::GetvFw);
416   PropertyManager->Tie("forces/lod-norm", this,
417                        &FGAerodynamics::GetLoD);
418   PropertyManager->Tie("aero/cl-squared", this,
419                        &FGAerodynamics::GetClSquared);
420   PropertyManager->Tie("aero/qbar-area", &qbar_area);
421   PropertyManager->Tie("aero/alpha-max-rad", this,
422                        &FGAerodynamics::GetAlphaCLMax,
423                        &FGAerodynamics::SetAlphaCLMax,
424                        true);
425   PropertyManager->Tie("aero/alpha-min-rad", this,
426                        &FGAerodynamics::GetAlphaCLMin,
427                        &FGAerodynamics::SetAlphaCLMin,
428                        true);
429   PropertyManager->Tie("aero/bi2vel", this,
430                        &FGAerodynamics::GetBI2Vel);
431   PropertyManager->Tie("aero/ci2vel", this,
432                        &FGAerodynamics::GetCI2Vel);
433   PropertyManager->Tie("aero/alpha-wing-rad", this,
434                        &FGAerodynamics::GetAlphaW);
435   PropertyManager->Tie("systems/stall-warn-norm", this,
436                         &FGAerodynamics::GetStallWarn);
437   PropertyManager->Tie("aero/stall-hyst-norm", this,
438                         &FGAerodynamics::GetHysteresisParm);
439 }
440
441 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 //    The bitmasked value choices are as follows:
443 //    unset: In this case (the default) JSBSim would only print
444 //       out the normally expected messages, essentially echoing
445 //       the config files as they are read. If the environment
446 //       variable is not set, debug_lvl is set to 1 internally
447 //    0: This requests JSBSim not to output any messages
448 //       whatsoever.
449 //    1: This value explicity requests the normal JSBSim
450 //       startup messages
451 //    2: This value asks for a message to be printed out when
452 //       a class is instantiated
453 //    4: When this value is set, a message is displayed when a
454 //       FGModel object executes its Run() method
455 //    8: When this value is set, various runtime state variables
456 //       are printed out periodically
457 //    16: When set various parameters are sanity checked and
458 //       a message is printed out when they go out of bounds
459
460 void FGAerodynamics::Debug(int from)
461 {
462   if (debug_lvl <= 0) return;
463
464   if (debug_lvl & 1) { // Standard console startup message output
465     if (from == 2) { // Loader
466       switch (axisType) {
467         case (atLiftDrag):
468           cout << endl << "  Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
469           break;
470         case (atAxialNormal):
471           cout << endl << "  Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
472           break;
473         case (atBodyXYZ):
474           cout << endl << "  Aerodynamics (X|Y|Z axes):" << endl << endl;
475           break;
476       case (atNone):
477           cout << endl << "  Aerodynamics (undefined axes):" << endl << endl;
478           break;
479       }
480     }
481   }
482   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
483     if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
484     if (from == 1) cout << "Destroyed:    FGAerodynamics" << endl;
485   }
486   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
487   }
488   if (debug_lvl & 8 ) { // Runtime state variables
489   }
490   if (debug_lvl & 16) { // Sanity checking
491   }
492   if (debug_lvl & 64) {
493     if (from == 0) { // Constructor
494       cout << IdSrc << endl;
495       cout << IdHdr << endl;
496     }
497   }
498 }
499
500 } // namespace JSBSim