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 "FGPropagate.h"
46 #include "FGAircraft.h"
47 #include "FGAuxiliary.h"
48 #include "FGMassBalance.h"
49 #include "input_output/FGPropertyManager.h"
55 static const char *IdSrc = "$Id: FGAerodynamics.cpp,v 1.34 2010/10/15 11:32:41 jberndt Exp $";
56 static const char *IdHdr = ID_AERODYNAMICS;
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
63 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
65 Name = "FGAerodynamics";
75 AxisIdx["NORMAL"] = 2;
83 Coeff = new CoeffArray[6];
85 impending_stall = stall_hyst = 0.0;
86 alphaclmin = alphaclmax = 0.0;
87 alphahystmin = alphahystmax = 0.0;
90 bi2vel = ci2vel = 0.0;
92 vDeltaRP.InitMatrix();
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 FGAerodynamics::~FGAerodynamics()
106 for (j=0; j<Coeff[i].size(); j++)
116 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 bool FGAerodynamics::InitModel(void)
120 if (!FGModel::InitModel()) return false;
122 impending_stall = stall_hyst = 0.0;
123 alphaclmin = alphaclmax = 0.0;
124 alphahystmin = alphahystmax = 0.0;
127 bi2vel = ci2vel = 0.0;
129 vDeltaRP.InitMatrix();
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 bool FGAerodynamics::Run(void)
137 unsigned int axis_ctr, ctr;
138 double alpha, twovel;
140 if (FGModel::Run()) return true;
141 if (FDMExec->Holding()) return false; // if paused don't execute
145 // calculate some oft-used quantities for speed
147 twovel = 2*Auxiliary->GetVt();
149 bi2vel = Aircraft->GetWingSpan() / twovel;
150 ci2vel = Aircraft->Getcbar() / twovel;
152 alphaw = Auxiliary->Getalpha() + Aircraft->GetWingIncidence();
153 alpha = Auxiliary->Getalpha();
154 qbar_area = Aircraft->GetWingArea() * Auxiliary->Getqbar();
156 if (alphaclmax != 0) {
157 if (alpha > 0.85*alphaclmax) {
158 impending_stall = 10*(alpha/alphaclmax - 0.85);
164 if (alphahystmax != 0.0 && alphahystmin != 0.0) {
165 if (alpha > alphahystmax) {
167 } else if (alpha < alphahystmin) {
173 vFnative.InitMatrix();
175 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
176 for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
177 vFnative(axis_ctr+1) += Coeff[axis_ctr][ctr]->GetValue();
181 // Note that we still need to convert to wind axes here, because it is
182 // used in the L/D calculation, and we still may want to look at Lift
186 case atBodyXYZ: // Forces already in body axes; no manipulation needed
187 vFw = GetTb2w()*vFnative;
190 case atLiftDrag: // Copy forces into wind axes
192 vFw(eDrag)*=-1; vFw(eLift)*=-1;
193 vForces = GetTw2b()*vFw;
195 case atAxialNormal: // Convert native forces into Axial|Normal|Side system
196 vFw = GetTb2w()*vFnative;
197 vFnative(eX)*=-1; vFnative(eZ)*=-1;
201 cerr << endl << " A proper axis type has NOT been selected. Check "
202 << "your aerodynamics definition." << endl;
206 // Calculate aerodynamic reference point shift, if any
207 if (AeroRPShift) vDeltaRP(eX) = AeroRPShift->GetValue()*Aircraft->Getcbar()*12.0;
209 // Calculate lift coefficient squared
210 if ( Auxiliary->Getqbar() > 0) {
211 clsq = vFw(eLift) / (Aircraft->GetWingArea()*Auxiliary->Getqbar());
215 // Calculate lift Lift over Drag
216 if ( fabs(vFw(eDrag)) > 0.0) lod = fabs( vFw(eLift) / vFw(eDrag) );
218 vDXYZcg = MassBalance->StructuralToBody(Aircraft->GetXYZrp() + vDeltaRP);
220 vMoments = vDXYZcg*vForces; // M = r X F
222 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
223 for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
224 vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->GetValue();
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 // From Stevens and Lewis, "Aircraft Control and Simulation", 3rd Ed., the
236 // transformation from body to wind axes is defined (where "a" is alpha and "B"
239 // cos(a)*cos(B) sin(B) sin(a)*cos(B)
240 // -cos(a)*sin(B) cos(B) -sin(a)*sin(B)
243 // The transform from wind to body axes is then,
245 // cos(a)*cos(B) -cos(a)*sin(B) -sin(a)
247 // sin(a)*cos(B) -sin(a)*sin(B) cos(a)
249 FGMatrix33& FGAerodynamics::GetTw2b(void)
251 double ca, cb, sa, sb;
253 double alpha = Auxiliary->Getalpha();
254 double beta = Auxiliary->Getbeta();
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 FGMatrix33& FGAerodynamics::GetTb2w(void)
279 double ca, cb, sa, sb;
281 alpha = Auxiliary->Getalpha();
282 beta = Auxiliary->Getbeta();
302 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 bool FGAerodynamics::Load(Element *element)
306 string parameter, axis, scratch;
307 string scratch_unit="";
308 string fname="", file="";
309 Element *temp_element, *axis_element, *function_element;
311 string separator = "/";
313 fname = element->GetAttributeValue("file");
314 if (!fname.empty()) {
315 file = FDMExec->GetFullAircraftPath() + separator + fname;
316 document = LoadXMLDocument(file);
321 FGModel::Load(document); // Perform base class Pre-Load
323 DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
327 if ((temp_element = document->FindElement("alphalimits"))) {
328 scratch_unit = temp_element->GetAttributeValue("unit");
329 if (scratch_unit.empty()) scratch_unit = "RAD";
330 alphaclmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
331 alphaclmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
334 if ((temp_element = document->FindElement("hysteresis_limits"))) {
335 scratch_unit = temp_element->GetAttributeValue("unit");
336 if (scratch_unit.empty()) scratch_unit = "RAD";
337 alphahystmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
338 alphahystmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
341 if ((temp_element = document->FindElement("aero_ref_pt_shift_x"))) {
342 function_element = temp_element->FindElement("function");
343 AeroRPShift = new FGFunction(PropertyManager, function_element);
346 axis_element = document->FindElement("axis");
347 while (axis_element) {
349 axis = axis_element->GetAttributeValue("name");
350 function_element = axis_element->FindElement("function");
351 while (function_element) {
352 string current_func_name = function_element->GetAttributeValue("name");
354 ca.push_back( new FGFunction(PropertyManager, function_element) );
355 } catch (string const str) {
356 cerr << endl << fgred << "Error loading aerodynamic function in "
357 << current_func_name << ":" << str << " Aborting." << reset << endl;
360 function_element = axis_element->FindNextElement("function");
362 Coeff[AxisIdx[axis]] = ca;
363 axis_element = document->FindNextElement("axis");
366 PostLoad(document, PropertyManager); // Perform base class Post-Load
371 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 // This private class function checks to verify consistency in the choice of
374 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or
375 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
376 // Note that if the "SIDE" axis specifier is entered first in a config file,
377 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
378 // This is OK, and the warning is due to the SIDE specifier used for both
379 // the Lift/Drag and Axial/Normal axis systems.
381 void FGAerodynamics::DetermineAxisSystem()
383 Element* axis_element = document->FindElement("axis");
385 while (axis_element) {
386 axis = axis_element->GetAttributeValue("name");
387 if (axis == "LIFT" || axis == "DRAG" || axis == "SIDE") {
388 if (axisType == atNone) axisType = atLiftDrag;
389 else if (axisType != atLiftDrag) {
390 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
391 << " aircraft config file." << endl;
393 } else if (axis == "AXIAL" || axis == "NORMAL") {
394 if (axisType == atNone) axisType = atAxialNormal;
395 else if (axisType != atAxialNormal) {
396 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
397 << " aircraft config file." << endl;
399 } else if (axis == "X" || axis == "Y" || axis == "Z") {
400 if (axisType == atNone) axisType = atBodyXYZ;
401 else if (axisType != atBodyXYZ) {
402 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
403 << " aircraft config file." << endl;
405 } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
406 cerr << endl << " An unknown axis type, " << axis << " has been specified"
407 << " in the aircraft configuration file." << endl;
410 axis_element = document->FindNextElement("axis");
412 if (axisType == atNone) {
413 axisType = atLiftDrag;
414 cerr << endl << " The aerodynamic axis system has been set by default"
415 << " to the Lift/Side/Drag system." << endl;
419 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 string FGAerodynamics::GetCoefficientStrings(const string& delimeter) const
423 string CoeffStrings = "";
424 bool firstime = true;
425 unsigned int axis, sd;
427 for (axis = 0; axis < 6; axis++) {
428 for (sd = 0; sd < Coeff[axis].size(); sd++) {
432 CoeffStrings += delimeter;
434 CoeffStrings += Coeff[axis][sd]->GetName();
440 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 string FGAerodynamics::GetCoefficientValues(const string& delimeter) const
446 for (unsigned int axis = 0; axis < 6; axis++) {
447 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
448 if (buf.tellp() > 0) buf << delimeter;
449 buf << setw(9) << Coeff[axis][sd]->GetValue();
456 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 void FGAerodynamics::bind(void)
460 typedef double (FGAerodynamics::*PMF)(int) const;
462 PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
463 (PMF)&FGAerodynamics::GetForces);
464 PropertyManager->Tie("forces/fby-aero-lbs", this,2,
465 (PMF)&FGAerodynamics::GetForces);
466 PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
467 (PMF)&FGAerodynamics::GetForces);
468 PropertyManager->Tie("moments/l-aero-lbsft", this,1,
469 (PMF)&FGAerodynamics::GetMoments);
470 PropertyManager->Tie("moments/m-aero-lbsft", this,2,
471 (PMF)&FGAerodynamics::GetMoments);
472 PropertyManager->Tie("moments/n-aero-lbsft", this,3,
473 (PMF)&FGAerodynamics::GetMoments);
474 PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
475 (PMF)&FGAerodynamics::GetvFw);
476 PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
477 (PMF)&FGAerodynamics::GetvFw);
478 PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
479 (PMF)&FGAerodynamics::GetvFw);
480 PropertyManager->Tie("forces/lod-norm", this,
481 &FGAerodynamics::GetLoD);
482 PropertyManager->Tie("aero/cl-squared", this,
483 &FGAerodynamics::GetClSquared);
484 PropertyManager->Tie("aero/qbar-area", &qbar_area);
485 PropertyManager->Tie("aero/alpha-max-rad", this,
486 &FGAerodynamics::GetAlphaCLMax,
487 &FGAerodynamics::SetAlphaCLMax,
489 PropertyManager->Tie("aero/alpha-min-rad", this,
490 &FGAerodynamics::GetAlphaCLMin,
491 &FGAerodynamics::SetAlphaCLMin,
493 PropertyManager->Tie("aero/bi2vel", this,
494 &FGAerodynamics::GetBI2Vel);
495 PropertyManager->Tie("aero/ci2vel", this,
496 &FGAerodynamics::GetCI2Vel);
497 PropertyManager->Tie("aero/alpha-wing-rad", this,
498 &FGAerodynamics::GetAlphaW);
499 PropertyManager->Tie("systems/stall-warn-norm", this,
500 &FGAerodynamics::GetStallWarn);
501 PropertyManager->Tie("aero/stall-hyst-norm", this,
502 &FGAerodynamics::GetHysteresisParm);
505 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 // The bitmasked value choices are as follows:
507 // unset: In this case (the default) JSBSim would only print
508 // out the normally expected messages, essentially echoing
509 // the config files as they are read. If the environment
510 // variable is not set, debug_lvl is set to 1 internally
511 // 0: This requests JSBSim not to output any messages
513 // 1: This value explicity requests the normal JSBSim
515 // 2: This value asks for a message to be printed out when
516 // a class is instantiated
517 // 4: When this value is set, a message is displayed when a
518 // FGModel object executes its Run() method
519 // 8: When this value is set, various runtime state variables
520 // are printed out periodically
521 // 16: When set various parameters are sanity checked and
522 // a message is printed out when they go out of bounds
524 void FGAerodynamics::Debug(int from)
526 if (debug_lvl <= 0) return;
528 if (debug_lvl & 1) { // Standard console startup message output
529 if (from == 2) { // Loader
532 cout << endl << " Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
534 case (atAxialNormal):
535 cout << endl << " Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
538 cout << endl << " Aerodynamics (X|Y|Z axes):" << endl << endl;
541 cout << endl << " Aerodynamics (undefined axes):" << endl << endl;
546 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
547 if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
548 if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
550 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
552 if (debug_lvl & 8 ) { // Runtime state variables
554 if (debug_lvl & 16) { // Sanity checking
556 if (debug_lvl & 64) {
557 if (from == 0) { // Constructor
558 cout << IdSrc << endl;
559 cout << IdHdr << endl;
564 } // namespace JSBSim