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++)
111 for (i=0; i<variables.size(); i++)
119 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 bool FGAerodynamics::InitModel(void)
123 if (!FGModel::InitModel()) return false;
125 impending_stall = stall_hyst = 0.0;
126 alphaclmin = alphaclmax = 0.0;
127 alphahystmin = alphahystmax = 0.0;
130 bi2vel = ci2vel = 0.0;
132 vDeltaRP.InitMatrix();
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 bool FGAerodynamics::Run(void)
140 unsigned int axis_ctr, ctr, i;
141 double alpha, twovel;
143 if (FGModel::Run()) return true;
144 if (FDMExec->Holding()) return false; // if paused don't execute
146 // calculate some oft-used quantities for speed
148 twovel = 2*Auxiliary->GetVt();
150 bi2vel = Aircraft->GetWingSpan() / twovel;
151 ci2vel = Aircraft->Getcbar() / twovel;
153 alphaw = Auxiliary->Getalpha() + Aircraft->GetWingIncidence();
154 alpha = Auxiliary->Getalpha();
155 qbar_area = Aircraft->GetWingArea() * Auxiliary->Getqbar();
157 if (alphaclmax != 0) {
158 if (alpha > 0.85*alphaclmax) {
159 impending_stall = 10*(alpha/alphaclmax - 0.85);
165 if (alphahystmax != 0.0 && alphahystmin != 0.0) {
166 if (alpha > alphahystmax) {
168 } else if (alpha < alphahystmin) {
174 vFnative.InitMatrix();
176 // Tell the variable functions to cache their values, so while the aerodynamic
177 // functions are being calculated for each axis, these functions do not get
178 // calculated each time, but instead use the values that have already
179 // been calculated for this frame.
181 for (i=0; i<variables.size(); i++) variables[i]->cacheValue(true);
183 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
184 for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
185 vFnative(axis_ctr+1) += Coeff[axis_ctr][ctr]->GetValue();
189 // Note that we still need to convert to wind axes here, because it is
190 // used in the L/D calculation, and we still may want to look at Lift
194 case atBodyXYZ: // Forces already in body axes; no manipulation needed
195 vFw = GetTb2w()*vFnative;
198 case atLiftDrag: // Copy forces into wind axes
200 vFw(eDrag)*=-1; vFw(eLift)*=-1;
201 vForces = GetTw2b()*vFw;
203 case atAxialNormal: // Convert native forces into Axial|Normal|Side system
204 vFw = GetTb2w()*vFnative;
205 vFnative(eX)*=-1; vFnative(eZ)*=-1;
209 cerr << endl << " A proper axis type has NOT been selected. Check "
210 << "your aerodynamics definition." << endl;
214 // Calculate aerodynamic reference point shift, if any
215 if (AeroRPShift) vDeltaRP(eX) = AeroRPShift->GetValue()*Aircraft->Getcbar()*12.0;
217 // Calculate lift coefficient squared
218 if ( Auxiliary->Getqbar() > 0) {
219 clsq = vFw(eLift) / (Aircraft->GetWingArea()*Auxiliary->Getqbar());
223 // Calculate lift Lift over Drag
224 if ( fabs(vFw(eDrag)) > 0.0) lod = fabs( vFw(eLift) / vFw(eDrag) );
226 vDXYZcg = MassBalance->StructuralToBody(Aircraft->GetXYZrp() + vDeltaRP);
228 vMoments = vDXYZcg*vForces; // M = r X F
230 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
231 for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
232 vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->GetValue();
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 // From Stevens and Lewis, "Aircraft Control and Simulation", 3rd Ed., the
242 // transformation from body to wind axes is defined (where "a" is alpha and "B"
245 // cos(a)*cos(B) sin(B) sin(a)*cos(B)
246 // -cos(a)*sin(B) cos(B) -sin(a)*sin(B)
249 // The transform from wind to body axes is then,
251 // cos(a)*cos(B) -cos(a)*sin(B) -sin(a)
253 // sin(a)*cos(B) -sin(a)*sin(B) cos(a)
255 FGMatrix33& FGAerodynamics::GetTw2b(void)
257 double ca, cb, sa, sb;
259 double alpha = Auxiliary->Getalpha();
260 double beta = Auxiliary->Getbeta();
280 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 FGMatrix33& FGAerodynamics::GetTb2w(void)
285 double ca, cb, sa, sb;
287 alpha = Auxiliary->Getalpha();
288 beta = Auxiliary->Getbeta();
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 bool FGAerodynamics::Load(Element *element)
312 string parameter, axis, scratch;
313 string scratch_unit="";
314 string fname="", file="";
315 Element *temp_element, *axis_element, *function_element;
317 string separator = "/";
319 fname = element->GetAttributeValue("file");
320 if (!fname.empty()) {
321 file = FDMExec->GetFullAircraftPath() + separator + fname;
322 document = LoadXMLDocument(file);
327 FGModel::Load(element); // Perform base class Load
329 DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
333 if ((temp_element = document->FindElement("alphalimits"))) {
334 scratch_unit = temp_element->GetAttributeValue("unit");
335 if (scratch_unit.empty()) scratch_unit = "RAD";
336 alphaclmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
337 alphaclmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
340 if ((temp_element = document->FindElement("hysteresis_limits"))) {
341 scratch_unit = temp_element->GetAttributeValue("unit");
342 if (scratch_unit.empty()) scratch_unit = "RAD";
343 alphahystmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
344 alphahystmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
347 if ((temp_element = document->FindElement("aero_ref_pt_shift_x"))) {
348 function_element = temp_element->FindElement("function");
349 AeroRPShift = new FGFunction(PropertyManager, function_element);
352 function_element = document->FindElement("function");
353 while (function_element) {
354 variables.push_back( new FGFunction(PropertyManager, function_element) );
355 function_element = document->FindNextElement("function");
358 axis_element = document->FindElement("axis");
359 while (axis_element) {
361 axis = axis_element->GetAttributeValue("name");
362 function_element = axis_element->FindElement("function");
363 while (function_element) {
364 ca.push_back( new FGFunction(PropertyManager, function_element) );
365 function_element = axis_element->FindNextElement("function");
367 Coeff[AxisIdx[axis]] = ca;
368 axis_element = document->FindNextElement("axis");
374 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 // This private class function checks to verify consistency in the choice of
377 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or
378 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
379 // Note that if the "SIDE" axis specifier is entered first in a config file,
380 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
381 // This is OK, and the warning is due to the SIDE specifier used for both
382 // the Lift/Drag and Axial/Normal axis systems.
384 void FGAerodynamics::DetermineAxisSystem()
386 Element* axis_element = document->FindElement("axis");
388 while (axis_element) {
389 axis = axis_element->GetAttributeValue("name");
390 if (axis == "LIFT" || axis == "DRAG" || axis == "SIDE") {
391 if (axisType == atNone) axisType = atLiftDrag;
392 else if (axisType != atLiftDrag) {
393 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
394 << " aircraft config file." << endl;
396 } else if (axis == "AXIAL" || axis == "NORMAL") {
397 if (axisType == atNone) axisType = atAxialNormal;
398 else if (axisType != atAxialNormal) {
399 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
400 << " aircraft config file." << endl;
402 } else if (axis == "X" || axis == "Y" || axis == "Z") {
403 if (axisType == atNone) axisType = atBodyXYZ;
404 else if (axisType != atBodyXYZ) {
405 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
406 << " aircraft config file." << endl;
408 } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
409 cerr << endl << " An unknown axis type, " << axis << " has been specified"
410 << " in the aircraft configuration file." << endl;
413 axis_element = document->FindNextElement("axis");
415 if (axisType == atNone) {
416 axisType = atLiftDrag;
417 cerr << endl << " The aerodynamic axis system has been set by default"
418 << " to the Lift/Side/Drag system." << endl;
422 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 string FGAerodynamics::GetCoefficientStrings(const string& delimeter) const
426 string CoeffStrings = "";
427 bool firstime = true;
428 unsigned int axis, sd;
430 for (sd = 0; sd < variables.size(); sd++) {
434 CoeffStrings += delimeter;
436 CoeffStrings += variables[sd]->GetName();
439 for (axis = 0; axis < 6; axis++) {
440 for (sd = 0; sd < Coeff[axis].size(); sd++) {
444 CoeffStrings += delimeter;
446 CoeffStrings += Coeff[axis][sd]->GetName();
452 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 string FGAerodynamics::GetCoefficientValues(const string& delimeter) const
459 for (unsigned int sd = 0; sd < variables.size(); sd++) {
460 if (buf.tellp() > 0) buf << delimeter;
461 buf << setw(9) << variables[sd]->GetValue();
464 for (unsigned int axis = 0; axis < 6; axis++) {
465 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
466 if (buf.tellp() > 0) buf << delimeter;
467 buf << setw(9) << Coeff[axis][sd]->GetValue();
474 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 void FGAerodynamics::bind(void)
478 typedef double (FGAerodynamics::*PMF)(int) const;
480 PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
481 (PMF)&FGAerodynamics::GetForces);
482 PropertyManager->Tie("forces/fby-aero-lbs", this,2,
483 (PMF)&FGAerodynamics::GetForces);
484 PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
485 (PMF)&FGAerodynamics::GetForces);
486 PropertyManager->Tie("moments/l-aero-lbsft", this,1,
487 (PMF)&FGAerodynamics::GetMoments);
488 PropertyManager->Tie("moments/m-aero-lbsft", this,2,
489 (PMF)&FGAerodynamics::GetMoments);
490 PropertyManager->Tie("moments/n-aero-lbsft", this,3,
491 (PMF)&FGAerodynamics::GetMoments);
492 PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
493 (PMF)&FGAerodynamics::GetvFw);
494 PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
495 (PMF)&FGAerodynamics::GetvFw);
496 PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
497 (PMF)&FGAerodynamics::GetvFw);
498 PropertyManager->Tie("forces/lod-norm", this,
499 &FGAerodynamics::GetLoD);
500 PropertyManager->Tie("aero/cl-squared", this,
501 &FGAerodynamics::GetClSquared);
502 PropertyManager->Tie("aero/qbar-area", &qbar_area);
503 PropertyManager->Tie("aero/alpha-max-rad", this,
504 &FGAerodynamics::GetAlphaCLMax,
505 &FGAerodynamics::SetAlphaCLMax,
507 PropertyManager->Tie("aero/alpha-min-rad", this,
508 &FGAerodynamics::GetAlphaCLMin,
509 &FGAerodynamics::SetAlphaCLMin,
511 PropertyManager->Tie("aero/bi2vel", this,
512 &FGAerodynamics::GetBI2Vel);
513 PropertyManager->Tie("aero/ci2vel", this,
514 &FGAerodynamics::GetCI2Vel);
515 PropertyManager->Tie("aero/alpha-wing-rad", this,
516 &FGAerodynamics::GetAlphaW);
517 PropertyManager->Tie("systems/stall-warn-norm", this,
518 &FGAerodynamics::GetStallWarn);
519 PropertyManager->Tie("aero/stall-hyst-norm", this,
520 &FGAerodynamics::GetHysteresisParm);
523 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 // The bitmasked value choices are as follows:
525 // unset: In this case (the default) JSBSim would only print
526 // out the normally expected messages, essentially echoing
527 // the config files as they are read. If the environment
528 // variable is not set, debug_lvl is set to 1 internally
529 // 0: This requests JSBSim not to output any messages
531 // 1: This value explicity requests the normal JSBSim
533 // 2: This value asks for a message to be printed out when
534 // a class is instantiated
535 // 4: When this value is set, a message is displayed when a
536 // FGModel object executes its Run() method
537 // 8: When this value is set, various runtime state variables
538 // are printed out periodically
539 // 16: When set various parameters are sanity checked and
540 // a message is printed out when they go out of bounds
542 void FGAerodynamics::Debug(int from)
544 if (debug_lvl <= 0) return;
546 if (debug_lvl & 1) { // Standard console startup message output
547 if (from == 2) { // Loader
550 cout << endl << " Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
552 case (atAxialNormal):
553 cout << endl << " Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
556 cout << endl << " Aerodynamics (X|Y|Z axes):" << endl << endl;
559 cout << endl << " Aerodynamics (undefined axes):" << endl << endl;
564 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
565 if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
566 if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
568 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
570 if (debug_lvl & 8 ) { // Runtime state variables
572 if (debug_lvl & 16) { // Sanity checking
574 if (debug_lvl & 64) {
575 if (from == 0) { // Constructor
576 cout << IdSrc << endl;
577 cout << IdHdr << endl;
582 } // namespace JSBSim