]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGAerodynamics.cpp
e946a5b479defc2fb9315aacc1387fec30eca466
[flightgear.git] / src / FDM / JSBSim / models / FGAerodynamics.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGAerodynamics.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/13/00
6  Purpose:      Encapsulates the aerodynamic forces
7
8  ------------- Copyright (C) 2000  Jon S. Berndt (jsb@hal-pc.org) -------------
9
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
13  version.
14
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
18  details.
19
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.
23
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.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29
30 HISTORY
31 --------------------------------------------------------------------------------
32 09/13/00   JSB   Created
33 04/22/01   JSB   Moved code into here from FGAircraft
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
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>
45
46 namespace JSBSim {
47
48 static const char *IdSrc = "$Id$";
49 static const char *IdHdr = ID_AERODYNAMICS;
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55
56 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec)
57 {
58   Name = "FGAerodynamics";
59
60   AxisIdx["DRAG"]   = 0;
61   AxisIdx["SIDE"]   = 1;
62   AxisIdx["LIFT"]   = 2;
63   AxisIdx["ROLL"]   = 3;
64   AxisIdx["PITCH"]  = 4;
65   AxisIdx["YAW"]    = 5;
66
67   AxisIdx["AXIAL"]  = 0;
68   AxisIdx["NORMAL"] = 2;
69
70   AxisIdx["X"] = 0;
71   AxisIdx["Y"] = 1;
72   AxisIdx["Z"] = 2;
73
74   axisType = atNone;
75
76   Coeff = new CoeffArray[6];
77
78   impending_stall = stall_hyst = 0.0;
79   alphaclmin = alphaclmax = 0.0;
80   alphahystmin = alphahystmax = 0.0;
81   clsq = lod = 0.0;
82   alphaw = 0.0;
83   bi2vel = ci2vel = 0.0;
84   AeroRPShift = 0;
85   vDeltaRP.InitMatrix();
86
87   bind();
88
89   Debug(0);
90 }
91
92 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93
94 FGAerodynamics::~FGAerodynamics()
95 {
96   unsigned int i,j;
97
98   for (i=0; i<6; i++)
99     for (j=0; j<Coeff[i].size(); j++)
100       delete Coeff[i][j];
101
102   delete[] Coeff;
103
104   for (i=0; i<variables.size(); i++)
105     delete variables[i];
106
107   delete AeroRPShift;
108
109   Debug(1);
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 bool FGAerodynamics::InitModel(void)
115 {
116   if (!FGModel::InitModel()) return false;
117
118   impending_stall = stall_hyst = 0.0;
119   alphaclmin = alphaclmax = 0.0;
120   alphahystmin = alphahystmax = 0.0;
121   clsq = lod = 0.0;
122   alphaw = 0.0;
123   bi2vel = ci2vel = 0.0;
124   AeroRPShift = 0;
125   vDeltaRP.InitMatrix();
126
127   return true;
128 }
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
131 bool FGAerodynamics::Run(void)
132 {
133   unsigned int axis_ctr, ctr, i;
134   double alpha, twovel;
135
136   if (FGModel::Run()) return true;
137   if (FDMExec->Holding()) return false; // if paused don't execute
138
139   // calculate some oft-used quantities for speed
140
141   twovel = 2*Auxiliary->GetVt();
142   if (twovel != 0) {
143     bi2vel = Aircraft->GetWingSpan() / twovel;
144     ci2vel = Aircraft->Getcbar() / twovel;
145   }
146   alphaw = Auxiliary->Getalpha() + Aircraft->GetWingIncidence();
147   alpha = Auxiliary->Getalpha();
148   qbar_area = Aircraft->GetWingArea() * Auxiliary->Getqbar();
149
150   if (alphaclmax != 0) {
151     if (alpha > 0.85*alphaclmax) {
152       impending_stall = 10*(alpha/alphaclmax - 0.85);
153     } else {
154       impending_stall = 0;
155     }
156   }
157
158   if (alphahystmax != 0.0 && alphahystmin != 0.0) {
159     if (alpha > alphahystmax) {
160        stall_hyst = 1;
161     } else if (alpha < alphahystmin) {
162        stall_hyst = 0;
163     }
164   }
165
166   vFw.InitMatrix();
167   vFnative.InitMatrix();
168
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.
173
174   for (i=0; i<variables.size(); i++) variables[i]->cacheValue(true);
175
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();
179     }
180   }
181
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
184   // and Drag.
185
186   switch (axisType) {
187     case atBodyXYZ:       // Forces already in body axes; no manipulation needed
188       vFw = GetTb2w()*vFnative;
189       vForces = vFnative;
190       break;
191     case atLiftDrag:      // Copy forces into wind axes
192       vFw = vFnative;
193       vFw(eDrag)*=-1; vFw(eLift)*=-1;
194       vForces = GetTw2b()*vFw;
195       break;
196     case atAxialNormal:   // Convert native forces into Axial|Normal|Side system
197       vFw = GetTb2w()*vFnative;
198       vFnative(eX)*=-1; vFnative(eZ)*=-1;
199       vForces = vFnative;
200       break;
201     default:
202       cerr << endl << "  A proper axis type has NOT been selected. Check "
203                    << "your aerodynamics definition." << endl;
204       exit(-1);
205   }
206
207   // Calculate aerodynamic reference point shift, if any
208   if (AeroRPShift) vDeltaRP(eX) = AeroRPShift->GetValue()*Aircraft->Getcbar()*12.0;
209
210   // Calculate lift coefficient squared
211   if ( Auxiliary->Getqbar() > 0) {
212     clsq = vFw(eLift) / (Aircraft->GetWingArea()*Auxiliary->Getqbar());
213     clsq *= clsq;
214   }
215
216   // Calculate lift Lift over Drag
217   if ( fabs(vFw(eDrag)) > 0.0) lod = fabs( vFw(eLift) / vFw(eDrag) );
218
219   vDXYZcg = MassBalance->StructuralToBody(Aircraft->GetXYZrp() + vDeltaRP);
220
221   vMoments = vDXYZcg*vForces; // M = r X F
222
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();
226     }
227   }
228
229   return false;
230 }
231
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 //
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"
236 // is beta):
237 //
238 //   cos(a)*cos(B)     sin(B)    sin(a)*cos(B)
239 //  -cos(a)*sin(B)     cos(B)   -sin(a)*sin(B)
240 //  -sin(a)              0       cos(a)
241 //
242 // The transform from wind to body axes is then,
243 //
244 //   cos(a)*cos(B)  -cos(a)*sin(B)  -sin(a)
245 //          sin(B)          cos(B)     0
246 //   sin(a)*cos(B)  -sin(a)*sin(B)   cos(a)
247
248 FGMatrix33& FGAerodynamics::GetTw2b(void)
249 {
250   double ca, cb, sa, sb;
251
252   double alpha = Auxiliary->Getalpha();
253   double beta  = Auxiliary->Getbeta();
254
255   ca = cos(alpha);
256   sa = sin(alpha);
257   cb = cos(beta);
258   sb = sin(beta);
259
260   mTw2b(1,1) = ca*cb;
261   mTw2b(1,2) = -ca*sb;
262   mTw2b(1,3) = -sa;
263   mTw2b(2,1) = sb;
264   mTw2b(2,2) = cb;
265   mTw2b(2,3) = 0.0;
266   mTw2b(3,1) = sa*cb;
267   mTw2b(3,2) = -sa*sb;
268   mTw2b(3,3) = ca;
269
270   return mTw2b;
271 }
272
273 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274
275 FGMatrix33& FGAerodynamics::GetTb2w(void)
276 {
277   double alpha,beta;
278   double ca, cb, sa, sb;
279
280   alpha = Auxiliary->Getalpha();
281   beta  = Auxiliary->Getbeta();
282
283   ca = cos(alpha);
284   sa = sin(alpha);
285   cb = cos(beta);
286   sb = sin(beta);
287
288   mTb2w(1,1) = ca*cb;
289   mTb2w(1,2) = sb;
290   mTb2w(1,3) = sa*cb;
291   mTb2w(2,1) = -ca*sb;
292   mTb2w(2,2) = cb;
293   mTb2w(2,3) = -sa*sb;
294   mTb2w(3,1) = -sa;
295   mTb2w(3,2) = 0.0;
296   mTb2w(3,3) = ca;
297
298   return mTb2w;
299 }
300
301 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302
303 bool FGAerodynamics::Load(Element *element)
304 {
305   string parameter, axis, scratch;
306   string scratch_unit="";
307   string fname="", file="";
308   Element *temp_element, *axis_element, *function_element;
309
310   string separator = "/";
311 #ifdef macintosh
312   separator = ";";
313 #endif
314
315   fname = element->GetAttributeValue("file");
316   if (!fname.empty()) {
317     file = FDMExec->GetFullAircraftPath() + separator + fname;
318     document = LoadXMLDocument(file);
319   } else {
320     document = element;
321   }
322
323   DetermineAxisSystem(); // Detemine if Lift/Side/Drag, etc. is used.
324
325   Debug(2);
326
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");
332   }
333
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");
339   }
340
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);
344   }
345
346   function_element = document->FindElement("function");
347   while (function_element) {
348     variables.push_back( new FGFunction(PropertyManager, function_element) );
349     function_element = document->FindNextElement("function");
350   }
351
352   axis_element = document->FindElement("axis");
353   while (axis_element) {
354     CoeffArray ca;
355     axis = axis_element->GetAttributeValue("name");
356     function_element = axis_element->FindElement("function");
357     while (function_element) {
358       ca.push_back( new FGFunction(PropertyManager, function_element) );
359       function_element = axis_element->FindNextElement("function");
360     }
361     Coeff[AxisIdx[axis]] = ca;
362     axis_element = document->FindNextElement("axis");
363   }
364
365   return true;
366 }
367
368 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 //
370 // This private class function checks to verify consistency in the choice of
371 // aerodynamic axes used in the config file. One set of LIFT|DRAG|SIDE, or 
372 // X|Y|Z, or AXIAL|NORMAL|SIDE must be chosen; mixed system axes are not allowed.
373 // Note that if the "SIDE" axis specifier is entered first in a config file, 
374 // a warning message will be given IF the AXIAL|NORMAL specifiers are also given.
375 // This is OK, and the warning is due to the SIDE specifier used for both
376 // the Lift/Drag and Axial/Normal axis systems.
377
378 void FGAerodynamics::DetermineAxisSystem()
379 {
380   Element* axis_element = document->FindElement("axis");
381   string axis;
382   while (axis_element) {
383     axis = axis_element->GetAttributeValue("name");
384     if (axis == "LIFT" || axis == "DRAG" || axis == "SIDE") {
385       if (axisType == atNone) axisType = atLiftDrag;
386       else if (axisType != atLiftDrag) {
387         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
388                      << " aircraft config file." << endl;
389       }
390     } else if (axis == "AXIAL" || axis == "NORMAL") {
391       if (axisType == atNone) axisType = atAxialNormal;
392       else if (axisType != atAxialNormal) {
393         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
394                      << " aircraft config file." << endl;
395       }
396     } else if (axis == "X" || axis == "Y" || axis == "Z") {
397       if (axisType == atNone) axisType = atBodyXYZ;
398       else if (axisType != atBodyXYZ) {
399         cerr << endl << "  Mixed aerodynamic axis systems have been used in the"
400                      << " aircraft config file." << endl;
401       }
402     } else if (axis != "ROLL" && axis != "PITCH" && axis != "YAW") { // error
403       cerr << endl << "  An unknown axis type, " << axis << " has been specified"
404                    << " in the aircraft configuration file." << endl;
405       exit(-1);
406     }
407     axis_element = document->FindNextElement("axis");
408   }
409   if (axisType == atNone) {
410     axisType = atLiftDrag;
411     cerr << endl << "  The aerodynamic axis system has been set by default"
412                  << " to the Lift/Side/Drag system." << endl;
413   }
414 }
415
416 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417
418 string FGAerodynamics::GetCoefficientStrings(string delimeter)
419 {
420   string CoeffStrings = "";
421   bool firstime = true;
422   unsigned int axis, sd;
423
424   for (sd = 0; sd < variables.size(); sd++) {
425     if (firstime) {
426       firstime = false;
427     } else {
428       CoeffStrings += delimeter;
429     }
430     CoeffStrings += variables[sd]->GetName();
431   }
432
433   for (axis = 0; axis < 6; axis++) {
434     for (sd = 0; sd < Coeff[axis].size(); sd++) {
435       if (firstime) {
436         firstime = false;
437       } else {
438         CoeffStrings += delimeter;
439       }
440       CoeffStrings += Coeff[axis][sd]->GetName();
441     }
442   }
443   return CoeffStrings;
444 }
445
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447
448 string FGAerodynamics::GetCoefficientValues(string delimeter)
449 {
450   string SDValues = "";
451   bool firstime = true;
452   unsigned int sd;
453
454   for (sd = 0; sd < variables.size(); sd++) {
455     if (firstime) {
456       firstime = false;
457     } else {
458       SDValues += delimeter;
459     }
460     SDValues += variables[sd]->GetValueAsString();
461   }
462
463   for (unsigned int axis = 0; axis < 6; axis++) {
464     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
465       if (firstime) {
466         firstime = false;
467       } else {
468         SDValues += delimeter;
469       }
470       SDValues += Coeff[axis][sd]->GetValueAsString();
471     }
472   }
473
474   return SDValues;
475 }
476
477 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478
479 void FGAerodynamics::bind(void)
480 {
481   typedef double (FGAerodynamics::*PMF)(int) const;
482
483   PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
484                        (PMF)&FGAerodynamics::GetForces);
485   PropertyManager->Tie("forces/fby-aero-lbs", this,2,
486                        (PMF)&FGAerodynamics::GetForces);
487   PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
488                        (PMF)&FGAerodynamics::GetForces);
489   PropertyManager->Tie("moments/l-aero-lbsft", this,1,
490                        (PMF)&FGAerodynamics::GetMoments);
491   PropertyManager->Tie("moments/m-aero-lbsft", this,2,
492                        (PMF)&FGAerodynamics::GetMoments);
493   PropertyManager->Tie("moments/n-aero-lbsft", this,3,
494                        (PMF)&FGAerodynamics::GetMoments);
495   PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
496                        (PMF)&FGAerodynamics::GetvFw);
497   PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
498                        (PMF)&FGAerodynamics::GetvFw);
499   PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
500                        (PMF)&FGAerodynamics::GetvFw);
501   PropertyManager->Tie("forces/lod-norm", this,
502                        &FGAerodynamics::GetLoD);
503   PropertyManager->Tie("aero/cl-squared", this,
504                        &FGAerodynamics::GetClSquared);
505   PropertyManager->Tie("aero/qbar-area", &qbar_area);
506   PropertyManager->Tie("aero/alpha-max-rad", this,
507                        &FGAerodynamics::GetAlphaCLMax,
508                        &FGAerodynamics::SetAlphaCLMax,
509                        true);
510   PropertyManager->Tie("aero/alpha-min-rad", this,
511                        &FGAerodynamics::GetAlphaCLMin,
512                        &FGAerodynamics::SetAlphaCLMin,
513                        true);
514   PropertyManager->Tie("aero/bi2vel", this,
515                        &FGAerodynamics::GetBI2Vel);
516   PropertyManager->Tie("aero/ci2vel", this,
517                        &FGAerodynamics::GetCI2Vel);
518   PropertyManager->Tie("aero/alpha-wing-rad", this,
519                        &FGAerodynamics::GetAlphaW);
520   PropertyManager->Tie("systems/stall-warn-norm", this,
521                         &FGAerodynamics::GetStallWarn);
522   PropertyManager->Tie("aero/stall-hyst-norm", this,
523                         &FGAerodynamics::GetHysteresisParm);
524 }
525
526 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
527 //    The bitmasked value choices are as follows:
528 //    unset: In this case (the default) JSBSim would only print
529 //       out the normally expected messages, essentially echoing
530 //       the config files as they are read. If the environment
531 //       variable is not set, debug_lvl is set to 1 internally
532 //    0: This requests JSBSim not to output any messages
533 //       whatsoever.
534 //    1: This value explicity requests the normal JSBSim
535 //       startup messages
536 //    2: This value asks for a message to be printed out when
537 //       a class is instantiated
538 //    4: When this value is set, a message is displayed when a
539 //       FGModel object executes its Run() method
540 //    8: When this value is set, various runtime state variables
541 //       are printed out periodically
542 //    16: When set various parameters are sanity checked and
543 //       a message is printed out when they go out of bounds
544
545 void FGAerodynamics::Debug(int from)
546 {
547   if (debug_lvl <= 0) return;
548
549   if (debug_lvl & 1) { // Standard console startup message output
550     if (from == 2) { // Loader
551       switch (axisType) {
552         case (atLiftDrag):
553           cout << endl << "  Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
554           break;
555         case (atAxialNormal):
556           cout << endl << "  Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
557           break;
558         case (atBodyXYZ):
559           cout << endl << "  Aerodynamics (X|Y|Z axes):" << endl << endl;
560           break;
561       }
562     }
563   }
564   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
565     if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
566     if (from == 1) cout << "Destroyed:    FGAerodynamics" << endl;
567   }
568   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
569   }
570   if (debug_lvl & 8 ) { // Runtime state variables
571   }
572   if (debug_lvl & 16) { // Sanity checking
573   }
574   if (debug_lvl & 64) {
575     if (from == 0) { // Constructor
576       cout << IdSrc << endl;
577       cout << IdHdr << endl;
578     }
579   }
580 }
581
582 } // namespace JSBSim