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