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.37 2011/03/11 13:02:26 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 AeroFunctions = new AeroFunctionArray[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<AeroFunctions[i].size(); j++)
107 delete AeroFunctions[i][j];
109 delete[] AeroFunctions;
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)
138 if (FGModel::Run()) return true;
139 if (FDMExec->Holding()) return false; // if paused don't execute
141 unsigned int axis_ctr, ctr;
142 const double alpha=FDMExec->GetAuxiliary()->Getalpha();
143 const double twovel=2*FDMExec->GetAuxiliary()->GetVt();
144 const double qbar = FDMExec->GetAuxiliary()->Getqbar();
145 const double wingarea = FDMExec->GetAircraft()->GetWingArea(); // TODO: Make these constants constant!
146 const double wingspan = FDMExec->GetAircraft()->GetWingSpan();
147 const double wingchord = FDMExec->GetAircraft()->Getcbar();
148 const double wingincidence = FDMExec->GetAircraft()->GetWingIncidence();
151 // calculate some oft-used quantities for speed
154 bi2vel = wingspan / twovel;
155 ci2vel = wingchord / twovel;
157 alphaw = alpha + wingincidence;
158 qbar_area = wingarea * qbar;
160 if (alphaclmax != 0) {
161 if (alpha > 0.85*alphaclmax) {
162 impending_stall = 10*(alpha/alphaclmax - 0.85);
168 if (alphahystmax != 0.0 && alphahystmin != 0.0) {
169 if (alpha > alphahystmax) {
171 } else if (alpha < alphahystmin) {
177 vFnative.InitMatrix();
179 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
180 for (ctr=0; ctr < AeroFunctions[axis_ctr].size(); ctr++) {
181 vFnative(axis_ctr+1) += AeroFunctions[axis_ctr][ctr]->GetValue();
185 // Note that we still need to convert to wind axes here, because it is
186 // used in the L/D calculation, and we still may want to look at Lift
190 case atBodyXYZ: // Forces already in body axes; no manipulation needed
191 vFw = GetTb2w()*vFnative;
194 case atLiftDrag: // Copy forces into wind axes
196 vFw(eDrag)*=-1; vFw(eLift)*=-1;
197 vForces = GetTw2b()*vFw;
199 case atAxialNormal: // Convert native forces into Axial|Normal|Side system
200 vFw = GetTb2w()*vFnative;
201 vFnative(eX)*=-1; vFnative(eZ)*=-1;
205 cerr << endl << " A proper axis type has NOT been selected. Check "
206 << "your aerodynamics definition." << endl;
210 // Calculate aerodynamic reference point shift, if any
211 if (AeroRPShift) vDeltaRP(eX) = AeroRPShift->GetValue()*wingchord*12.0;
213 // Calculate lift coefficient squared
215 clsq = vFw(eLift) / (wingarea*qbar);
219 // Calculate lift Lift over Drag
220 if ( fabs(vFw(eDrag)) > 0.0) lod = fabs( vFw(eLift) / vFw(eDrag) );
222 vDXYZcg = FDMExec->GetMassBalance()->StructuralToBody(FDMExec->GetAircraft()->GetXYZrp() + vDeltaRP);
224 vMoments = vDXYZcg*vForces; // M = r X F
226 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
227 for (ctr = 0; ctr < AeroFunctions[axis_ctr+3].size(); ctr++) {
228 vMoments(axis_ctr+1) += AeroFunctions[axis_ctr+3][ctr]->GetValue();
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 // From Stevens and Lewis, "Aircraft Control and Simulation", 3rd Ed., the
240 // transformation from body to wind axes is defined (where "a" is alpha and "B"
243 // cos(a)*cos(B) sin(B) sin(a)*cos(B)
244 // -cos(a)*sin(B) cos(B) -sin(a)*sin(B)
247 // The transform from wind to body axes is then,
249 // cos(a)*cos(B) -cos(a)*sin(B) -sin(a)
251 // sin(a)*cos(B) -sin(a)*sin(B) cos(a)
253 FGMatrix33& FGAerodynamics::GetTw2b(void)
255 double ca, cb, sa, sb;
257 double alpha = FDMExec->GetAuxiliary()->Getalpha();
258 double beta = FDMExec->GetAuxiliary()->Getbeta();
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 FGMatrix33& FGAerodynamics::GetTb2w(void)
283 double ca, cb, sa, sb;
285 alpha = FDMExec->GetAuxiliary()->Getalpha();
286 beta = FDMExec->GetAuxiliary()->Getbeta();
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 bool FGAerodynamics::Load(Element *element)
310 string parameter, axis, scratch;
311 string scratch_unit="";
312 string fname="", file="";
313 Element *temp_element, *axis_element, *function_element;
315 string separator = "/";
317 fname = element->GetAttributeValue("file");
318 if (!fname.empty()) {
319 file = FDMExec->GetFullAircraftPath() + separator + fname;
320 document = LoadXMLDocument(file);
325 FGModel::Load(document); // Perform base class Pre-Load
327 DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
331 if ((temp_element = document->FindElement("alphalimits"))) {
332 scratch_unit = temp_element->GetAttributeValue("unit");
333 if (scratch_unit.empty()) scratch_unit = "RAD";
334 alphaclmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
335 alphaclmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
338 if ((temp_element = document->FindElement("hysteresis_limits"))) {
339 scratch_unit = temp_element->GetAttributeValue("unit");
340 if (scratch_unit.empty()) scratch_unit = "RAD";
341 alphahystmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
342 alphahystmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
345 if ((temp_element = document->FindElement("aero_ref_pt_shift_x"))) {
346 function_element = temp_element->FindElement("function");
347 AeroRPShift = new FGFunction(PropertyManager, function_element);
350 axis_element = document->FindElement("axis");
351 while (axis_element) {
352 AeroFunctionArray ca;
353 axis = axis_element->GetAttributeValue("name");
354 function_element = axis_element->FindElement("function");
355 while (function_element) {
356 string current_func_name = function_element->GetAttributeValue("name");
358 ca.push_back( new FGFunction(PropertyManager, function_element) );
359 } catch (string const str) {
360 cerr << endl << fgred << "Error loading aerodynamic function in "
361 << current_func_name << ":" << str << " Aborting." << reset << endl;
364 function_element = axis_element->FindNextElement("function");
366 AeroFunctions[AxisIdx[axis]] = ca;
367 axis_element = document->FindNextElement("axis");
370 PostLoad(document, PropertyManager); // Perform base class Post-Load
375 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377 // This private class function checks to verify consistency in the choice of
378 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or
379 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
380 // Note that if the "SIDE" axis specifier is entered first in a config file,
381 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
382 // This is OK, and the warning is due to the SIDE specifier used for both
383 // the Lift/Drag and Axial/Normal axis systems.
385 void FGAerodynamics::DetermineAxisSystem()
387 Element* axis_element = document->FindElement("axis");
389 while (axis_element) {
390 axis = axis_element->GetAttributeValue("name");
391 if (axis == "LIFT" || axis == "DRAG") {
392 if (axisType == atNone) axisType = atLiftDrag;
393 else if (axisType != atLiftDrag) {
394 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
395 << " aircraft config file. (LIFT DRAG)" << endl;
397 } else if (axis == "SIDE") {
398 if (axisType != atNone && axisType != atLiftDrag && axisType != atAxialNormal) {
399 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
400 << " aircraft config file. (SIDE)" << endl;
402 } else if (axis == "AXIAL" || axis == "NORMAL") {
403 if (axisType == atNone) axisType = atAxialNormal;
404 else if (axisType != atAxialNormal) {
405 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
406 << " aircraft config file. (NORMAL AXIAL)" << endl;
408 } else if (axis == "X" || axis == "Y" || axis == "Z") {
409 if (axisType == atNone) axisType = atBodyXYZ;
410 else if (axisType != atBodyXYZ) {
411 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
412 << " aircraft config file. (XYZ)" << endl;
414 } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
415 cerr << endl << " An unknown axis type, " << axis << " has been specified"
416 << " in the aircraft configuration file." << endl;
419 axis_element = document->FindNextElement("axis");
421 if (axisType == atNone) {
422 axisType = atLiftDrag;
423 cerr << endl << " The aerodynamic axis system has been set by default"
424 << " to the Lift/Side/Drag system." << endl;
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 string FGAerodynamics::GetAeroFunctionStrings(const string& delimeter) const
432 string AeroFunctionStrings = "";
433 bool firstime = true;
434 unsigned int axis, sd;
436 for (axis = 0; axis < 6; axis++) {
437 for (sd = 0; sd < AeroFunctions[axis].size(); sd++) {
441 AeroFunctionStrings += delimeter;
443 AeroFunctionStrings += AeroFunctions[axis][sd]->GetName();
446 return AeroFunctionStrings;
449 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 string FGAerodynamics::GetAeroFunctionValues(const string& delimeter) const
455 for (unsigned int axis = 0; axis < 6; axis++) {
456 for (unsigned int sd = 0; sd < AeroFunctions[axis].size(); sd++) {
457 if (buf.tellp() > 0) buf << delimeter;
458 buf << setw(9) << AeroFunctions[axis][sd]->GetValue();
465 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 void FGAerodynamics::bind(void)
469 typedef double (FGAerodynamics::*PMF)(int) const;
471 PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
472 (PMF)&FGAerodynamics::GetForces);
473 PropertyManager->Tie("forces/fby-aero-lbs", this,2,
474 (PMF)&FGAerodynamics::GetForces);
475 PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
476 (PMF)&FGAerodynamics::GetForces);
477 PropertyManager->Tie("moments/l-aero-lbsft", this,1,
478 (PMF)&FGAerodynamics::GetMoments);
479 PropertyManager->Tie("moments/m-aero-lbsft", this,2,
480 (PMF)&FGAerodynamics::GetMoments);
481 PropertyManager->Tie("moments/n-aero-lbsft", this,3,
482 (PMF)&FGAerodynamics::GetMoments);
483 PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
484 (PMF)&FGAerodynamics::GetvFw);
485 PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
486 (PMF)&FGAerodynamics::GetvFw);
487 PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
488 (PMF)&FGAerodynamics::GetvFw);
489 PropertyManager->Tie("forces/lod-norm", this,
490 &FGAerodynamics::GetLoD);
491 PropertyManager->Tie("aero/cl-squared", this,
492 &FGAerodynamics::GetClSquared);
493 PropertyManager->Tie("aero/qbar-area", &qbar_area);
494 PropertyManager->Tie("aero/alpha-max-rad", this,
495 &FGAerodynamics::GetAlphaCLMax,
496 &FGAerodynamics::SetAlphaCLMax,
498 PropertyManager->Tie("aero/alpha-min-rad", this,
499 &FGAerodynamics::GetAlphaCLMin,
500 &FGAerodynamics::SetAlphaCLMin,
502 PropertyManager->Tie("aero/bi2vel", this,
503 &FGAerodynamics::GetBI2Vel);
504 PropertyManager->Tie("aero/ci2vel", this,
505 &FGAerodynamics::GetCI2Vel);
506 PropertyManager->Tie("aero/alpha-wing-rad", this,
507 &FGAerodynamics::GetAlphaW);
508 PropertyManager->Tie("systems/stall-warn-norm", this,
509 &FGAerodynamics::GetStallWarn);
510 PropertyManager->Tie("aero/stall-hyst-norm", this,
511 &FGAerodynamics::GetHysteresisParm);
514 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 // The bitmasked value choices are as follows:
516 // unset: In this case (the default) JSBSim would only print
517 // out the normally expected messages, essentially echoing
518 // the config files as they are read. If the environment
519 // variable is not set, debug_lvl is set to 1 internally
520 // 0: This requests JSBSim not to output any messages
522 // 1: This value explicity requests the normal JSBSim
524 // 2: This value asks for a message to be printed out when
525 // a class is instantiated
526 // 4: When this value is set, a message is displayed when a
527 // FGModel object executes its Run() method
528 // 8: When this value is set, various runtime state variables
529 // are printed out periodically
530 // 16: When set various parameters are sanity checked and
531 // a message is printed out when they go out of bounds
533 void FGAerodynamics::Debug(int from)
535 if (debug_lvl <= 0) return;
537 if (debug_lvl & 1) { // Standard console startup message output
538 if (from == 2) { // Loader
541 cout << endl << " Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
543 case (atAxialNormal):
544 cout << endl << " Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
547 cout << endl << " Aerodynamics (X|Y|Z axes):" << endl << endl;
550 cout << endl << " Aerodynamics (undefined axes):" << endl << endl;
555 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
556 if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
557 if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
559 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
561 if (debug_lvl & 8 ) { // Runtime state variables
563 if (debug_lvl & 16) { // Sanity checking
565 if (debug_lvl & 64) {
566 if (from == 0) { // Constructor
567 cout << IdSrc << endl;
568 cout << IdHdr << endl;
573 } // namespace JSBSim