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$";
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 ca.push_back( new FGFunction(PropertyManager, function_element) );
353 function_element = axis_element->FindNextElement("function");
355 Coeff[AxisIdx[axis]] = ca;
356 axis_element = document->FindNextElement("axis");
359 FGModel::PostLoad(document); // Perform base class Post-Load
364 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 // This private class function checks to verify consistency in the choice of
367 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or
368 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
369 // Note that if the "SIDE" axis specifier is entered first in a config file,
370 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
371 // This is OK, and the warning is due to the SIDE specifier used for both
372 // the Lift/Drag and Axial/Normal axis systems.
374 void FGAerodynamics::DetermineAxisSystem()
376 Element* axis_element = document->FindElement("axis");
378 while (axis_element) {
379 axis = axis_element->GetAttributeValue("name");
380 if (axis == "LIFT" || axis == "DRAG" || axis == "SIDE") {
381 if (axisType == atNone) axisType = atLiftDrag;
382 else if (axisType != atLiftDrag) {
383 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
384 << " aircraft config file." << endl;
386 } else if (axis == "AXIAL" || axis == "NORMAL") {
387 if (axisType == atNone) axisType = atAxialNormal;
388 else if (axisType != atAxialNormal) {
389 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
390 << " aircraft config file." << endl;
392 } else if (axis == "X" || axis == "Y" || axis == "Z") {
393 if (axisType == atNone) axisType = atBodyXYZ;
394 else if (axisType != atBodyXYZ) {
395 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
396 << " aircraft config file." << endl;
398 } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
399 cerr << endl << " An unknown axis type, " << axis << " has been specified"
400 << " in the aircraft configuration file." << endl;
403 axis_element = document->FindNextElement("axis");
405 if (axisType == atNone) {
406 axisType = atLiftDrag;
407 cerr << endl << " The aerodynamic axis system has been set by default"
408 << " to the Lift/Side/Drag system." << endl;
412 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414 string FGAerodynamics::GetCoefficientStrings(const string& delimeter) const
416 string CoeffStrings = "";
417 bool firstime = true;
418 unsigned int axis, sd;
420 for (axis = 0; axis < 6; axis++) {
421 for (sd = 0; sd < Coeff[axis].size(); sd++) {
425 CoeffStrings += delimeter;
427 CoeffStrings += Coeff[axis][sd]->GetName();
433 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 string FGAerodynamics::GetCoefficientValues(const string& delimeter) const
439 for (unsigned int axis = 0; axis < 6; axis++) {
440 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
441 if (buf.tellp() > 0) buf << delimeter;
442 buf << setw(9) << Coeff[axis][sd]->GetValue();
449 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 void FGAerodynamics::bind(void)
453 typedef double (FGAerodynamics::*PMF)(int) const;
455 PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
456 (PMF)&FGAerodynamics::GetForces);
457 PropertyManager->Tie("forces/fby-aero-lbs", this,2,
458 (PMF)&FGAerodynamics::GetForces);
459 PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
460 (PMF)&FGAerodynamics::GetForces);
461 PropertyManager->Tie("moments/l-aero-lbsft", this,1,
462 (PMF)&FGAerodynamics::GetMoments);
463 PropertyManager->Tie("moments/m-aero-lbsft", this,2,
464 (PMF)&FGAerodynamics::GetMoments);
465 PropertyManager->Tie("moments/n-aero-lbsft", this,3,
466 (PMF)&FGAerodynamics::GetMoments);
467 PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
468 (PMF)&FGAerodynamics::GetvFw);
469 PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
470 (PMF)&FGAerodynamics::GetvFw);
471 PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
472 (PMF)&FGAerodynamics::GetvFw);
473 PropertyManager->Tie("forces/lod-norm", this,
474 &FGAerodynamics::GetLoD);
475 PropertyManager->Tie("aero/cl-squared", this,
476 &FGAerodynamics::GetClSquared);
477 PropertyManager->Tie("aero/qbar-area", &qbar_area);
478 PropertyManager->Tie("aero/alpha-max-rad", this,
479 &FGAerodynamics::GetAlphaCLMax,
480 &FGAerodynamics::SetAlphaCLMax,
482 PropertyManager->Tie("aero/alpha-min-rad", this,
483 &FGAerodynamics::GetAlphaCLMin,
484 &FGAerodynamics::SetAlphaCLMin,
486 PropertyManager->Tie("aero/bi2vel", this,
487 &FGAerodynamics::GetBI2Vel);
488 PropertyManager->Tie("aero/ci2vel", this,
489 &FGAerodynamics::GetCI2Vel);
490 PropertyManager->Tie("aero/alpha-wing-rad", this,
491 &FGAerodynamics::GetAlphaW);
492 PropertyManager->Tie("systems/stall-warn-norm", this,
493 &FGAerodynamics::GetStallWarn);
494 PropertyManager->Tie("aero/stall-hyst-norm", this,
495 &FGAerodynamics::GetHysteresisParm);
498 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 // The bitmasked value choices are as follows:
500 // unset: In this case (the default) JSBSim would only print
501 // out the normally expected messages, essentially echoing
502 // the config files as they are read. If the environment
503 // variable is not set, debug_lvl is set to 1 internally
504 // 0: This requests JSBSim not to output any messages
506 // 1: This value explicity requests the normal JSBSim
508 // 2: This value asks for a message to be printed out when
509 // a class is instantiated
510 // 4: When this value is set, a message is displayed when a
511 // FGModel object executes its Run() method
512 // 8: When this value is set, various runtime state variables
513 // are printed out periodically
514 // 16: When set various parameters are sanity checked and
515 // a message is printed out when they go out of bounds
517 void FGAerodynamics::Debug(int from)
519 if (debug_lvl <= 0) return;
521 if (debug_lvl & 1) { // Standard console startup message output
522 if (from == 2) { // Loader
525 cout << endl << " Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
527 case (atAxialNormal):
528 cout << endl << " Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
531 cout << endl << " Aerodynamics (X|Y|Z axes):" << endl << endl;
534 cout << endl << " Aerodynamics (undefined axes):" << endl << endl;
539 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
540 if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
541 if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
543 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
545 if (debug_lvl & 8 ) { // Runtime state variables
547 if (debug_lvl & 16) { // Sanity checking
549 if (debug_lvl & 64) {
550 if (from == 0) { // Constructor
551 cout << IdSrc << endl;
552 cout << IdHdr << endl;
557 } // namespace JSBSim