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.41 2011/08/04 12:46:32 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 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;
217 vDXYZcg = in.RPBody + vDeltaRP;
219 vMoments = vDXYZcg*vForces; // M = r X F
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();
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 bool FGAerodynamics::Load(Element *element)
236 string parameter, axis, scratch;
237 string scratch_unit="";
238 string fname="", file="";
239 Element *temp_element, *axis_element, *function_element;
241 string separator = "/";
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;
252 FGModel::Load(document); // Perform base class Pre-Load
254 DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
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");
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");
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);
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");
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;
291 function_element = axis_element->FindNextElement("function");
293 AeroFunctions[AxisIdx[axis]] = ca;
294 axis_element = document->FindNextElement("axis");
297 PostLoad(document, PropertyManager); // Perform base class Post-Load
302 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
312 void FGAerodynamics::DetermineAxisSystem()
314 Element* axis_element = document->FindElement("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;
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;
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;
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;
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;
346 axis_element = document->FindNextElement("axis");
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;
355 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 string FGAerodynamics::GetAeroFunctionStrings(const string& delimeter) const
359 string AeroFunctionStrings = "";
360 bool firstime = true;
361 unsigned int axis, sd;
363 for (axis = 0; axis < 6; axis++) {
364 for (sd = 0; sd < AeroFunctions[axis].size(); sd++) {
368 AeroFunctionStrings += delimeter;
370 AeroFunctionStrings += AeroFunctions[axis][sd]->GetName();
373 return AeroFunctionStrings;
376 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 string FGAerodynamics::GetAeroFunctionValues(const string& delimeter) const
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();
392 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 void FGAerodynamics::bind(void)
396 typedef double (FGAerodynamics::*PMF)(int) const;
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,
425 PropertyManager->Tie("aero/alpha-min-rad", this,
426 &FGAerodynamics::GetAlphaCLMin,
427 &FGAerodynamics::SetAlphaCLMin,
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);
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
449 // 1: This value explicity requests the normal JSBSim
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
460 void FGAerodynamics::Debug(int from)
462 if (debug_lvl <= 0) return;
464 if (debug_lvl & 1) { // Standard console startup message output
465 if (from == 2) { // Loader
468 cout << endl << " Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
470 case (atAxialNormal):
471 cout << endl << " Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
474 cout << endl << " Aerodynamics (X|Y|Z axes):" << endl << endl;
477 cout << endl << " Aerodynamics (undefined axes):" << endl << endl;
482 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
483 if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
484 if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
486 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
488 if (debug_lvl & 8 ) { // Runtime state variables
490 if (debug_lvl & 16) { // Sanity checking
492 if (debug_lvl & 64) {
493 if (from == 0) { // Constructor
494 cout << IdSrc << endl;
495 cout << IdHdr << endl;
500 } // namespace JSBSim