]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAerodynamics.cpp
sync with JSB JSBSim CVS
[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.45 2012/04/13 13:25:52 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 structual axis. That is, if the shift is positive,
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(eX) = in.RPBody(eX) - vDeltaRP(eX); // vDeltaRP is given in the structural frame
218   vDXYZcg(eY) = in.RPBody(eY) + vDeltaRP(eY);
219   vDXYZcg(eZ) = in.RPBody(eZ) - vDeltaRP(eZ);
220
221   vMoments = vDXYZcg*vForces; // M = r X F
222
223   for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
224     for (ctr = 0; ctr < AeroFunctions[axis_ctr+3].size(); ctr++) {
225       vMoments(axis_ctr+1) += AeroFunctions[axis_ctr+3][ctr]->GetValue();
226     }
227   }
228
229   RunPostFunctions();
230
231   return false;
232 }
233
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236 bool FGAerodynamics::Load(Element *element)
237 {
238   string parameter, axis, scratch;
239   string scratch_unit="";
240   string fname="", file="";
241   Element *temp_element, *axis_element, *function_element;
242
243   string separator = "/";
244
245   fname = element->GetAttributeValue("file");
246   if (!fname.empty()) {
247     file = FDMExec->GetFullAircraftPath() + separator + fname;
248     document = LoadXMLDocument(file);
249     if (document == 0L) return false;
250   } else {
251     document = element;
252   }
253
254   FGModel::Load(document); // Perform base class Pre-Load
255
256   DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
257
258   Debug(2);
259
260   if ((temp_element = document->FindElement("alphalimits"))) {
261     scratch_unit = temp_element->GetAttributeValue("unit");
262     if (scratch_unit.empty()) scratch_unit = "RAD";
263     alphaclmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
264     alphaclmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
265   }
266
267   if ((temp_element = document->FindElement("hysteresis_limits"))) {
268     scratch_unit = temp_element->GetAttributeValue("unit");
269     if (scratch_unit.empty()) scratch_unit = "RAD";
270     alphahystmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
271     alphahystmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
272   }
273
274   if ((temp_element = document->FindElement("aero_ref_pt_shift_x"))) {
275     function_element = temp_element->FindElement("function");
276     AeroRPShift = new FGFunction(PropertyManager, function_element);
277   }
278
279   axis_element = document->FindElement("axis");
280   while (axis_element) {
281     AeroFunctionArray ca;
282     axis = axis_element->GetAttributeValue("name");
283     function_element = axis_element->FindElement("function");
284     while (function_element) {
285       string current_func_name = function_element->GetAttributeValue("name");
286       try {
287         ca.push_back( new FGFunction(PropertyManager, function_element) );
288       } catch (string const str) {
289         cerr << endl << fgred << "Error loading aerodynamic function in " 
290              << current_func_name << ":" << str << " Aborting." << reset << endl;
291         return false;
292       }
293       function_element = axis_element->FindNextElement("function");
294     }
295     AeroFunctions[AxisIdx[axis]] = ca;
296     axis_element = document->FindNextElement("axis");
297   }
298
299   PostLoad(document, PropertyManager); // Perform base class Post-Load
300
301   return true;
302 }
303
304 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 //
306 // This private class function checks to verify consistency in the choice of
307 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or 
308 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
309 // Note that if the "SIDE" axis specifier is entered first in a config file, 
310 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
311 // This is OK, and the warning is due to the SIDE specifier used for both
312 // the Lift/Drag and Axial/Normal axis systems.
313
314 void FGAerodynamics::DetermineAxisSystem()
315 {
316   Element* axis_element = document->FindElement("axis");
317   string axis;
318   while (axis_element) {
319     axis = axis_element->GetAttributeValue("name");
320     if (axis == "LIFT" || axis == "DRAG") {
321       if (axisType == atNone) axisType = atLiftDrag;
322       else if (axisType != atLiftDrag) {
323         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
324                      << " aircraft config file. (LIFT DRAG)" << endl;
325       }
326     } else if (axis == "SIDE") {
327       if (axisType != atNone && axisType != atLiftDrag && axisType != atAxialNormal) {
328         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
329                      << " aircraft config file. (SIDE)" << endl;
330       }
331     } else if (axis == "AXIAL" || axis == "NORMAL") {
332       if (axisType == atNone) axisType = atAxialNormal;
333       else if (axisType != atAxialNormal) {
334         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
335                      << " aircraft config file. (NORMAL AXIAL)" << endl;
336       }
337     } else if (axis == "X" || axis == "Y" || axis == "Z") {
338       if (axisType == atNone) axisType = atBodyXYZ;
339       else if (axisType != atBodyXYZ) {
340         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
341                      << " aircraft config file. (XYZ)" << endl;
342       }
343     } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
344       cerr << endl << "  An unknown axis type, " << axis << " has been specified"
345                    << " in the aircraft configuration file." << endl;
346       exit(-1);
347     }
348     axis_element = document->FindNextElement("axis");
349   }
350   if (axisType == atNone) {
351     axisType = atLiftDrag;
352     cerr << endl << "  The aerodynamic axis system has been set by default"
353                  << " to the Lift/Side/Drag system." << endl;
354   }
355 }
356
357 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358
359 string FGAerodynamics::GetAeroFunctionStrings(const string& delimeter) const
360 {
361   string AeroFunctionStrings = "";
362   bool firstime = true;
363   unsigned int axis, sd;
364
365   for (axis = 0; axis < 6; axis++) {
366     for (sd = 0; sd < AeroFunctions[axis].size(); sd++) {
367       if (firstime) {
368         firstime = false;
369       } else {
370         AeroFunctionStrings += delimeter;
371       }
372       AeroFunctionStrings += AeroFunctions[axis][sd]->GetName();
373     }
374   }
375
376   string FunctionStrings = FGModelFunctions::GetFunctionStrings(delimeter);
377
378   if (FunctionStrings.size() > 0) {
379     if (AeroFunctionStrings.size() > 0) {
380       AeroFunctionStrings += delimeter + FunctionStrings;
381     } else {
382       AeroFunctionStrings = FunctionStrings;
383     }
384   }
385
386   return AeroFunctionStrings;
387 }
388
389 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390
391 string FGAerodynamics::GetAeroFunctionValues(const string& delimeter) const
392 {
393   ostringstream buf;
394
395   for (unsigned int axis = 0; axis < 6; axis++) {
396     for (unsigned int sd = 0; sd < AeroFunctions[axis].size(); sd++) {
397       if (buf.tellp() > 0) buf << delimeter;
398       buf << AeroFunctions[axis][sd]->GetValue();
399     }
400   }
401
402   string FunctionValues = FGModelFunctions::GetFunctionValues(delimeter);
403
404   if (FunctionValues.size() > 0) {
405     if (buf.str().size() > 0) {
406       buf << delimeter << FunctionValues;
407     } else {
408       buf << FunctionValues;
409     }
410   }
411
412   return buf.str();
413 }
414
415 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416
417 void FGAerodynamics::bind(void)
418 {
419   typedef double (FGAerodynamics::*PMF)(int) const;
420
421   PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
422                        (PMF)&FGAerodynamics::GetForces);
423   PropertyManager->Tie("forces/fby-aero-lbs", this,2,
424                        (PMF)&FGAerodynamics::GetForces);
425   PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
426                        (PMF)&FGAerodynamics::GetForces);
427   PropertyManager->Tie("moments/l-aero-lbsft", this,1,
428                        (PMF)&FGAerodynamics::GetMoments);
429   PropertyManager->Tie("moments/m-aero-lbsft", this,2,
430                        (PMF)&FGAerodynamics::GetMoments);
431   PropertyManager->Tie("moments/n-aero-lbsft", this,3,
432                        (PMF)&FGAerodynamics::GetMoments);
433   PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
434                        (PMF)&FGAerodynamics::GetvFw);
435   PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
436                        (PMF)&FGAerodynamics::GetvFw);
437   PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
438                        (PMF)&FGAerodynamics::GetvFw);
439   PropertyManager->Tie("forces/lod-norm", this,
440                        &FGAerodynamics::GetLoD);
441   PropertyManager->Tie("aero/cl-squared", this,
442                        &FGAerodynamics::GetClSquared);
443   PropertyManager->Tie("aero/qbar-area", &qbar_area);
444   PropertyManager->Tie("aero/alpha-max-rad", this,
445                        &FGAerodynamics::GetAlphaCLMax,
446                        &FGAerodynamics::SetAlphaCLMax,
447                        true);
448   PropertyManager->Tie("aero/alpha-min-rad", this,
449                        &FGAerodynamics::GetAlphaCLMin,
450                        &FGAerodynamics::SetAlphaCLMin,
451                        true);
452   PropertyManager->Tie("aero/bi2vel", this,
453                        &FGAerodynamics::GetBI2Vel);
454   PropertyManager->Tie("aero/ci2vel", this,
455                        &FGAerodynamics::GetCI2Vel);
456   PropertyManager->Tie("aero/alpha-wing-rad", this,
457                        &FGAerodynamics::GetAlphaW);
458   PropertyManager->Tie("systems/stall-warn-norm", this,
459                         &FGAerodynamics::GetStallWarn);
460   PropertyManager->Tie("aero/stall-hyst-norm", this,
461                         &FGAerodynamics::GetHysteresisParm);
462 }
463
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 //    The bitmasked value choices are as follows:
466 //    unset: In this case (the default) JSBSim would only print
467 //       out the normally expected messages, essentially echoing
468 //       the config files as they are read. If the environment
469 //       variable is not set, debug_lvl is set to 1 internally
470 //    0: This requests JSBSim not to output any messages
471 //       whatsoever.
472 //    1: This value explicity requests the normal JSBSim
473 //       startup messages
474 //    2: This value asks for a message to be printed out when
475 //       a class is instantiated
476 //    4: When this value is set, a message is displayed when a
477 //       FGModel object executes its Run() method
478 //    8: When this value is set, various runtime state variables
479 //       are printed out periodically
480 //    16: When set various parameters are sanity checked and
481 //       a message is printed out when they go out of bounds
482
483 void FGAerodynamics::Debug(int from)
484 {
485   if (debug_lvl <= 0) return;
486
487   if (debug_lvl & 1) { // Standard console startup message output
488     if (from == 2) { // Loader
489       switch (axisType) {
490         case (atLiftDrag):
491           cout << endl << "  Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
492           break;
493         case (atAxialNormal):
494           cout << endl << "  Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
495           break;
496         case (atBodyXYZ):
497           cout << endl << "  Aerodynamics (X|Y|Z axes):" << endl << endl;
498           break;
499       case (atNone):
500           cout << endl << "  Aerodynamics (undefined axes):" << endl << endl;
501           break;
502       }
503     }
504   }
505   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
506     if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
507     if (from == 1) cout << "Destroyed:    FGAerodynamics" << endl;
508   }
509   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
510   }
511   if (debug_lvl & 8 ) { // Runtime state variables
512   }
513   if (debug_lvl & 16) { // Sanity checking
514   }
515   if (debug_lvl & 64) {
516     if (from == 0) { // Constructor
517       cout << IdSrc << endl;
518       cout << IdHdr << endl;
519     }
520   }
521 }
522
523 } // namespace JSBSim