1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGAerodynamics.cpp
6 Purpose: Encapsulates the aerodynamic forces
8 ------------- Copyright (C) 2000 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
31 --------------------------------------------------------------------------------
33 04/22/01 JSB Moved code into here from FGAircraft
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 #include "FGFDMExec.h"
44 #include "FGAerodynamics.h"
45 #include "input_output/FGPropertyManager.h"
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;
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
61 Name = "FGAerodynamics";
71 AxisIdx["NORMAL"] = 2;
79 AeroFunctions = new AeroFunctionArray[6];
81 impending_stall = stall_hyst = 0.0;
82 alphaclmin = alphaclmax = 0.0;
83 alphahystmin = alphahystmax = 0.0;
86 bi2vel = ci2vel = 0.0;
88 vDeltaRP.InitMatrix();
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 FGAerodynamics::~FGAerodynamics()
102 for (j=0; j<AeroFunctions[i].size(); j++)
103 delete AeroFunctions[i][j];
105 delete[] AeroFunctions;
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 bool FGAerodynamics::InitModel(void)
116 if (!FGModel::InitModel()) return false;
118 impending_stall = stall_hyst = 0.0;
119 alphaclmin = alphaclmax = 0.0;
120 alphahystmin = alphahystmax = 0.0;
123 bi2vel = ci2vel = 0.0;
125 vDeltaRP.InitMatrix();
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 bool FGAerodynamics::Run(bool Holding)
134 if (FGModel::Run(Holding)) return true;
135 if (Holding) return false; // if paused don't execute
137 unsigned int axis_ctr, ctr;
138 const double twovel=2*in.Vt;
142 // calculate some oft-used quantities for speed
145 bi2vel = in.Wingspan / twovel;
146 ci2vel = in.Wingchord / twovel;
148 alphaw = in.Alpha + in.Wingincidence;
149 qbar_area = in.Wingarea * in.Qbar;
151 if (alphaclmax != 0) {
152 if (in.Alpha > 0.85*alphaclmax) {
153 impending_stall = 10*(in.Alpha/alphaclmax - 0.85);
159 if (alphahystmax != 0.0 && alphahystmin != 0.0) {
160 if (in.Alpha > alphahystmax) {
162 } else if (in.Alpha < alphahystmin) {
168 vFnative.InitMatrix();
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();
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
181 case atBodyXYZ: // Forces already in body axes; no manipulation needed
182 vFw = in.Tb2w*vFnative;
185 case atLiftDrag: // Copy forces into wind axes
187 vFw(eDrag)*=-1; vFw(eLift)*=-1;
188 vForces = in.Tw2b*vFw;
190 case atAxialNormal: // Convert native forces into Axial|Normal|Side system
191 vFw = in.Tb2w*vFnative;
192 vFnative(eX)*=-1; vFnative(eZ)*=-1;
196 cerr << endl << " A proper axis type has NOT been selected. Check "
197 << "your aerodynamics definition." << endl;
201 // Calculate lift coefficient squared
203 clsq = vFw(eLift) / (in.Wingarea*in.Qbar);
207 // Calculate lift Lift over Drag
208 if ( fabs(vFw(eDrag)) > 0.0) lod = fabs( vFw(eLift) / vFw(eDrag) );
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;
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);
221 vMoments = vDXYZcg*vForces; // M = r X F
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();
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 bool FGAerodynamics::Load(Element *element)
238 string parameter, axis, scratch;
239 string scratch_unit="";
240 string fname="", file="";
241 Element *temp_element, *axis_element, *function_element;
243 string separator = "/";
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;
254 FGModel::Load(document); // Perform base class Pre-Load
256 DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
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");
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");
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);
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");
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;
293 function_element = axis_element->FindNextElement("function");
295 AeroFunctions[AxisIdx[axis]] = ca;
296 axis_element = document->FindNextElement("axis");
299 PostLoad(document, PropertyManager); // Perform base class Post-Load
304 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
314 void FGAerodynamics::DetermineAxisSystem()
316 Element* axis_element = document->FindElement("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;
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;
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;
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;
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;
348 axis_element = document->FindNextElement("axis");
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;
357 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 string FGAerodynamics::GetAeroFunctionStrings(const string& delimeter) const
361 string AeroFunctionStrings = "";
362 bool firstime = true;
363 unsigned int axis, sd;
365 for (axis = 0; axis < 6; axis++) {
366 for (sd = 0; sd < AeroFunctions[axis].size(); sd++) {
370 AeroFunctionStrings += delimeter;
372 AeroFunctionStrings += AeroFunctions[axis][sd]->GetName();
376 string FunctionStrings = FGModelFunctions::GetFunctionStrings(delimeter);
378 if (FunctionStrings.size() > 0) {
379 if (AeroFunctionStrings.size() > 0) {
380 AeroFunctionStrings += delimeter + FunctionStrings;
382 AeroFunctionStrings = FunctionStrings;
386 return AeroFunctionStrings;
389 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 string FGAerodynamics::GetAeroFunctionValues(const string& delimeter) const
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();
402 string FunctionValues = FGModelFunctions::GetFunctionValues(delimeter);
404 if (FunctionValues.size() > 0) {
405 if (buf.str().size() > 0) {
406 buf << delimeter << FunctionValues;
408 buf << FunctionValues;
415 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 void FGAerodynamics::bind(void)
419 typedef double (FGAerodynamics::*PMF)(int) const;
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,
448 PropertyManager->Tie("aero/alpha-min-rad", this,
449 &FGAerodynamics::GetAlphaCLMin,
450 &FGAerodynamics::SetAlphaCLMin,
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);
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
472 // 1: This value explicity requests the normal JSBSim
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
483 void FGAerodynamics::Debug(int from)
485 if (debug_lvl <= 0) return;
487 if (debug_lvl & 1) { // Standard console startup message output
488 if (from == 2) { // Loader
491 cout << endl << " Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
493 case (atAxialNormal):
494 cout << endl << " Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
497 cout << endl << " Aerodynamics (X|Y|Z axes):" << endl << endl;
500 cout << endl << " Aerodynamics (undefined axes):" << endl << endl;
505 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
506 if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
507 if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
509 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
511 if (debug_lvl & 8 ) { // Runtime state variables
513 if (debug_lvl & 16) { // Sanity checking
515 if (debug_lvl & 64) {
516 if (from == 0) { // Constructor
517 cout << IdSrc << endl;
518 cout << IdHdr << endl;
523 } // namespace JSBSim