1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGAerodynamics.cpp
6 Purpose: Encapsulates the aerodynamic forces
8 ------------- Copyright (C) 2000 Jon S. Berndt (jsb@hal-pc.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 #include "FGAerodynamics.h"
40 #include "FGPropagate.h"
41 #include "FGAircraft.h"
42 #include "FGAuxiliary.h"
43 #include "FGMassBalance.h"
44 #include <input_output/FGPropertyManager.h>
48 static const char *IdSrc = "$Id$";
49 static const char *IdHdr = ID_AERODYNAMICS;
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
58 Name = "FGAerodynamics";
68 AxisIdx["NORMAL"] = 2;
76 Coeff = new CoeffArray[6];
78 impending_stall = stall_hyst = 0.0;
79 alphaclmin = alphaclmax = 0.0;
80 alphahystmin = alphahystmax = 0.0;
83 bi2vel = ci2vel = 0.0;
85 vDeltaRP.InitMatrix();
92 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 FGAerodynamics::~FGAerodynamics()
99 for (j=0; j<Coeff[i].size(); j++)
104 for (i=0; i<variables.size(); i++)
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(void)
133 unsigned int axis_ctr, ctr, i;
134 double alpha, twovel;
136 if (FGModel::Run()) return true;
137 if (FDMExec->Holding()) return false; // if paused don't execute
139 // calculate some oft-used quantities for speed
141 twovel = 2*Auxiliary->GetVt();
143 bi2vel = Aircraft->GetWingSpan() / twovel;
144 ci2vel = Aircraft->Getcbar() / twovel;
146 alphaw = Auxiliary->Getalpha() + Aircraft->GetWingIncidence();
147 alpha = Auxiliary->Getalpha();
148 qbar_area = Aircraft->GetWingArea() * Auxiliary->Getqbar();
150 if (alphaclmax != 0) {
151 if (alpha > 0.85*alphaclmax) {
152 impending_stall = 10*(alpha/alphaclmax - 0.85);
158 if (alphahystmax != 0.0 && alphahystmin != 0.0) {
159 if (alpha > alphahystmax) {
161 } else if (alpha < alphahystmin) {
167 vFnative.InitMatrix();
169 // Tell the variable functions to cache their values, so while the aerodynamic
170 // functions are being calculated for each axis, these functions do not get
171 // calculated each time, but instead use the values that have already
172 // been calculated for this frame.
174 for (i=0; i<variables.size(); i++) variables[i]->cacheValue(true);
176 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
177 for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
178 vFnative(axis_ctr+1) += Coeff[axis_ctr][ctr]->GetValue();
182 // Note that we still need to convert to wind axes here, because it is
183 // used in the L/D calculation, and we still may want to look at Lift
187 case atBodyXYZ: // Forces already in body axes; no manipulation needed
188 vFw = GetTb2w()*vFnative;
191 case atLiftDrag: // Copy forces into wind axes
193 vFw(eDrag)*=-1; vFw(eLift)*=-1;
194 vForces = GetTw2b()*vFw;
196 case atAxialNormal: // Convert native forces into Axial|Normal|Side system
197 vFw = GetTb2w()*vFnative;
198 vFnative(eX)*=-1; vFnative(eZ)*=-1;
202 cerr << endl << " A proper axis type has NOT been selected. Check "
203 << "your aerodynamics definition." << endl;
207 // Calculate aerodynamic reference point shift, if any
208 if (AeroRPShift) vDeltaRP(eX) = AeroRPShift->GetValue()*Aircraft->Getcbar()*12.0;
210 // Calculate lift coefficient squared
211 if ( Auxiliary->Getqbar() > 0) {
212 clsq = vFw(eLift) / (Aircraft->GetWingArea()*Auxiliary->Getqbar());
216 // Calculate lift Lift over Drag
217 if ( fabs(vFw(eDrag)) > 0.0) lod = fabs( vFw(eLift) / vFw(eDrag) );
219 vDXYZcg = MassBalance->StructuralToBody(Aircraft->GetXYZrp() + vDeltaRP);
221 vMoments = vDXYZcg*vForces; // M = r X F
223 for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
224 for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
225 vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->GetValue();
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 // From Stevens and Lewis, "Aircraft Control and Simulation", 3rd Ed., the
235 // transformation from body to wind axes is defined (where "a" is alpha and "B"
238 // cos(a)*cos(B) sin(B) sin(a)*cos(B)
239 // -cos(a)*sin(B) cos(B) -sin(a)*sin(B)
242 // The transform from wind to body axes is then,
244 // cos(a)*cos(B) -cos(a)*sin(B) -sin(a)
246 // sin(a)*cos(B) -sin(a)*sin(B) cos(a)
248 FGMatrix33& FGAerodynamics::GetTw2b(void)
250 double ca, cb, sa, sb;
252 double alpha = Auxiliary->Getalpha();
253 double beta = Auxiliary->Getbeta();
273 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 FGMatrix33& FGAerodynamics::GetTb2w(void)
278 double ca, cb, sa, sb;
280 alpha = Auxiliary->Getalpha();
281 beta = Auxiliary->Getbeta();
301 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 bool FGAerodynamics::Load(Element *element)
305 string parameter, axis, scratch;
306 string scratch_unit="";
307 string fname="", file="";
308 Element *temp_element, *axis_element, *function_element;
310 string separator = "/";
312 fname = element->GetAttributeValue("file");
313 if (!fname.empty()) {
314 file = FDMExec->GetFullAircraftPath() + separator + fname;
315 document = LoadXMLDocument(file);
320 DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
324 if (temp_element = document->FindElement("alphalimits")) {
325 scratch_unit = temp_element->GetAttributeValue("unit");
326 if (scratch_unit.empty()) scratch_unit = "RAD";
327 alphaclmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
328 alphaclmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
331 if (temp_element = document->FindElement("hysteresis_limits")) {
332 scratch_unit = temp_element->GetAttributeValue("unit");
333 if (scratch_unit.empty()) scratch_unit = "RAD";
334 alphahystmin = temp_element->FindElementValueAsNumberConvertFromTo("min", scratch_unit, "RAD");
335 alphahystmax = temp_element->FindElementValueAsNumberConvertFromTo("max", scratch_unit, "RAD");
338 if (temp_element = document->FindElement("aero_ref_pt_shift_x")) {
339 function_element = temp_element->FindElement("function");
340 AeroRPShift = new FGFunction(PropertyManager, function_element);
343 function_element = document->FindElement("function");
344 while (function_element) {
345 variables.push_back( new FGFunction(PropertyManager, function_element) );
346 function_element = document->FindNextElement("function");
349 axis_element = document->FindElement("axis");
350 while (axis_element) {
352 axis = axis_element->GetAttributeValue("name");
353 function_element = axis_element->FindElement("function");
354 while (function_element) {
355 ca.push_back( new FGFunction(PropertyManager, function_element) );
356 function_element = axis_element->FindNextElement("function");
358 Coeff[AxisIdx[axis]] = ca;
359 axis_element = document->FindNextElement("axis");
365 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 // This private class function checks to verify consistency in the choice of
368 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or
369 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
370 // Note that if the "SIDE" axis specifier is entered first in a config file,
371 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
372 // This is OK, and the warning is due to the SIDE specifier used for both
373 // the Lift/Drag and Axial/Normal axis systems.
375 void FGAerodynamics::DetermineAxisSystem()
377 Element* axis_element = document->FindElement("axis");
379 while (axis_element) {
380 axis = axis_element->GetAttributeValue("name");
381 if (axis == "LIFT" || axis == "DRAG" || axis == "SIDE") {
382 if (axisType == atNone) axisType = atLiftDrag;
383 else if (axisType != atLiftDrag) {
384 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
385 << " aircraft config file." << endl;
387 } else if (axis == "AXIAL" || axis == "NORMAL") {
388 if (axisType == atNone) axisType = atAxialNormal;
389 else if (axisType != atAxialNormal) {
390 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
391 << " aircraft config file." << endl;
393 } else if (axis == "X" || axis == "Y" || axis == "Z") {
394 if (axisType == atNone) axisType = atBodyXYZ;
395 else if (axisType != atBodyXYZ) {
396 cerr << endl << " Mixed aerodynamic axis systems have been used in the"
397 << " aircraft config file." << endl;
399 } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
400 cerr << endl << " An unknown axis type, " << axis << " has been specified"
401 << " in the aircraft configuration file." << endl;
404 axis_element = document->FindNextElement("axis");
406 if (axisType == atNone) {
407 axisType = atLiftDrag;
408 cerr << endl << " The aerodynamic axis system has been set by default"
409 << " to the Lift/Side/Drag system." << endl;
413 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415 string FGAerodynamics::GetCoefficientStrings(string delimeter)
417 string CoeffStrings = "";
418 bool firstime = true;
419 unsigned int axis, sd;
421 for (sd = 0; sd < variables.size(); sd++) {
425 CoeffStrings += delimeter;
427 CoeffStrings += variables[sd]->GetName();
430 for (axis = 0; axis < 6; axis++) {
431 for (sd = 0; sd < Coeff[axis].size(); sd++) {
435 CoeffStrings += delimeter;
437 CoeffStrings += Coeff[axis][sd]->GetName();
443 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445 string FGAerodynamics::GetCoefficientValues(string delimeter)
447 string SDValues = "";
448 bool firstime = true;
451 for (sd = 0; sd < variables.size(); sd++) {
455 SDValues += delimeter;
457 SDValues += variables[sd]->GetValueAsString();
460 for (unsigned int axis = 0; axis < 6; axis++) {
461 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
465 SDValues += delimeter;
467 SDValues += Coeff[axis][sd]->GetValueAsString();
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;
561 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
562 if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
563 if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
565 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
567 if (debug_lvl & 8 ) { // Runtime state variables
569 if (debug_lvl & 16) { // Sanity checking
571 if (debug_lvl & 64) {
572 if (from == 0) { // Constructor
573 cout << IdSrc << endl;
574 cout << IdHdr << endl;
579 } // namespace JSBSim