]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAerodynamics.cpp
Patch from Melchior Franz:
[flightgear.git] / src / FDM / JSBSim / 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 (gear and collision)
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 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 General Public License for more
18  details.
19
20  You should have received a copy of the GNU 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 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 "FGFactorGroup.h"
41 #include "FGCoefficient.h"
42 #include "FGPropertyManager.h"
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_AERODYNAMICS;
46
47 const unsigned NAxes=6;                           
48 const char* AxisNames[] = { "drag", "side-force", "lift", "rolling-moment",
49                             "pitching-moment","yawing-moment" }; 
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   Coeff = new CoeffArray[6];
68   
69   clsq=lod=0;
70   
71   bind();
72
73   Debug(0);
74 }
75
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
78 FGAerodynamics::~FGAerodynamics()
79 {
80   unsigned int i,j;
81   
82   unbind();
83   
84   for (i=0; i<6; i++) {
85     for (j=0; j<Coeff[i].size(); j++) {
86       delete Coeff[i][j];
87     }
88   }
89   delete[] Coeff;
90   
91   Debug(1);
92 }
93
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 bool FGAerodynamics::Run(void)
97 {
98   unsigned int axis_ctr,ctr;
99
100   if (!FGModel::Run()) {
101
102     vLastFs = vFs;
103     vFs.InitMatrix();
104
105     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
106       for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
107         //Coeff[axis_ctr][ctr]->Run();
108         vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
109         //cout << Coeff[axis_ctr][ctr]->Getname() << "= " 
110         //     << Coeff[axis_ctr][ctr]->TotalValue() << endl;
111       }
112     }
113     //correct signs of drag and lift to wind axes convention
114     //positive forward, right, down
115     if ( Translation->Getqbar() > 0) {
116       clsq = vFs(eLift) / (Aircraft->GetWingArea()*Translation->Getqbar());
117       clsq *= clsq;
118     }
119     if ( vFs(eDrag)  > 0) {
120       lod = vFs(eLift) / vFs(eDrag);
121     }  
122         
123     //correct signs of drag and lift to wind axes convention
124     //positive forward, right, down
125     vFs(eDrag)*=-1; vFs(eLift)*=-1;
126     //cout << "Aircraft::vFs: " << vFs << endl;
127     vForces = State->GetTs2b()*vFs;
128
129     vDXYZcg(eX) = -(Aircraft->GetXYZrp(eX) 
130                       - MassBalance->GetXYZcg(eX))*inchtoft;
131     vDXYZcg(eY) =  (Aircraft->GetXYZrp(eY) 
132                       - MassBalance->GetXYZcg(eY))*inchtoft;
133     vDXYZcg(eZ) = -(Aircraft->GetXYZrp(eZ) 
134                       - MassBalance->GetXYZcg(eZ))*inchtoft;
135
136     vMoments = vDXYZcg*vForces; // M = r X F
137
138     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
139       for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
140         //Coeff[axis_ctr+3][ctr]->Run();
141         vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
142       }
143     }
144     return false;
145   } else {
146     return true;
147   }
148 }
149
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151
152 bool FGAerodynamics::Load(FGConfigFile* AC_cfg)
153 {
154   string token, axis;
155
156   AC_cfg->GetNextConfigLine();
157
158   while ((token = AC_cfg->GetValue()) != string("/AERODYNAMICS")) {
159     if (token == "AXIS") {
160       CoeffArray ca;
161       axis = AC_cfg->GetValue("NAME");
162       AC_cfg->GetNextConfigLine();
163       while ((token = AC_cfg->GetValue()) != string("/AXIS")) {
164         if ( token == "COEFFICIENT" ) {
165           ca.push_back( new FGCoefficient(FDMExec) );
166           ca.back()->Load(AC_cfg);
167         } else if ( token == "GROUP" ) {
168           ca.push_back( new FGFactorGroup(FDMExec) );
169           ca.back()->Load(AC_cfg);
170         }
171       }
172       Coeff[AxisIdx[axis]] = ca;
173       AC_cfg->GetNextConfigLine();
174     }
175   }
176
177   bindModel();
178   
179   return true;
180 }
181
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183
184 string FGAerodynamics::GetCoefficientStrings(void)
185 {
186   string CoeffStrings = "";
187   bool firstime = true;
188   unsigned int axis, sd;
189
190   for (axis = 0; axis < 6; axis++) {
191     for (sd = 0; sd < Coeff[axis].size(); sd++) {
192       if (firstime) {
193         firstime = false;
194       } else {
195         CoeffStrings += ", ";
196       }
197       CoeffStrings += Coeff[axis][sd]->GetCoefficientStrings();
198     }
199   }
200   return CoeffStrings;
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 string FGAerodynamics::GetCoefficientValues(void)
206 {
207   string SDValues = "";
208   bool firstime = true;
209
210   for (unsigned int axis = 0; axis < 6; axis++) {
211     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
212       if (firstime) {
213         firstime = false;
214       } else {
215         SDValues += ", ";
216       }
217       SDValues += Coeff[axis][sd]->GetCoefficientValues();
218     }
219   }
220
221   return SDValues;
222 }
223
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225
226 void FGAerodynamics::bind(void)
227 {
228   typedef double (FGAerodynamics::*PMF)(int) const;
229
230   PropertyManager->Tie("forces/fbx-aero-lbs", this,1,
231                        (PMF)&FGAerodynamics::GetForces);
232   PropertyManager->Tie("forces/fby-aero-lbs", this,2,
233                        (PMF)&FGAerodynamics::GetForces);
234   PropertyManager->Tie("forces/fbz-aero-lbs", this,3,
235                        (PMF)&FGAerodynamics::GetForces);
236   PropertyManager->Tie("moments/l-aero-lbsft", this,1,
237                        (PMF)&FGAerodynamics::GetMoments);
238   PropertyManager->Tie("moments/m-aero-lbsft", this,2,
239                        (PMF)&FGAerodynamics::GetMoments);
240   PropertyManager->Tie("moments/n-aero-lbsft", this,3,
241                        (PMF)&FGAerodynamics::GetMoments);
242   PropertyManager->Tie("forces/fwx-aero-lbs", this,1,
243                        (PMF)&FGAerodynamics::GetvFs);
244   PropertyManager->Tie("forces/fwy-aero-lbs", this,2,
245                        (PMF)&FGAerodynamics::GetvFs);
246   PropertyManager->Tie("forces/fwz-aero-lbs", this,3,
247                        (PMF)&FGAerodynamics::GetvFs);
248   PropertyManager->Tie("forces/lod-norm", this,
249                        &FGAerodynamics::GetLoD);
250   PropertyManager->Tie("aero/cl-squared-norm", this,
251                        &FGAerodynamics::GetClSquared); 
252 }
253
254 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255
256 void FGAerodynamics::bindModel(void)
257
258   unsigned i,j;
259   FGPropertyManager* node;
260   string axis_node_name;
261   node = PropertyManager->GetNode("aero/buildup",true);
262   for (i=0;i<NAxes;i++) {
263      node = node->GetNode( string(AxisNames[i]),true );
264      for (j=0; j < Coeff[i].size(); j++) {
265        Coeff[i][j]->bind(node);
266      } 
267      node = (FGPropertyManager*)node->getParent();                                         
268   }
269 }
270
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272
273 void FGAerodynamics::unbind(void)
274 {
275   unsigned i,j;
276
277   PropertyManager->Untie("forces/fbx-aero-lbs");
278   PropertyManager->Untie("forces/fby-aero-lbs");
279   PropertyManager->Untie("forces/fbz-aero-lbs");
280   PropertyManager->Untie("moments/l-aero-lbsft");
281   PropertyManager->Untie("moments/m-aero-lbsft");
282   PropertyManager->Untie("moments/n-aero-lbsft");
283   PropertyManager->Untie("forces/fwx-aero-lbs");
284   PropertyManager->Untie("forces/fwy-aero-lbs");
285   PropertyManager->Untie("forces/fwz-aero-lbs");
286   PropertyManager->Untie("forces/lod-norm");
287   PropertyManager->Untie("aero/cl-squared-norm");  
288   
289   for ( i=0; i<NAxes; i++ ) {
290      for ( j=0; j < Coeff[i].size(); j++ ) {
291        Coeff[i][j]->unbind();
292        
293      }
294   }
295 }
296
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 //    The bitmasked value choices are as follows:
299 //    unset: In this case (the default) JSBSim would only print
300 //       out the normally expected messages, essentially echoing
301 //       the config files as they are read. If the environment
302 //       variable is not set, debug_lvl is set to 1 internally
303 //    0: This requests JSBSim not to output any messages
304 //       whatsoever.
305 //    1: This value explicity requests the normal JSBSim
306 //       startup messages
307 //    2: This value asks for a message to be printed out when
308 //       a class is instantiated
309 //    4: When this value is set, a message is displayed when a
310 //       FGModel object executes its Run() method
311 //    8: When this value is set, various runtime state variables
312 //       are printed out periodically
313 //    16: When set various parameters are sanity checked and
314 //       a message is printed out when they go out of bounds
315
316 void FGAerodynamics::Debug(int from)
317 {
318   if (debug_lvl <= 0) return;
319
320   if (debug_lvl & 1) { // Standard console startup message output
321     if (from == 0) { // Constructor
322
323     }
324   }
325   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
326     if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
327     if (from == 1) cout << "Destroyed:    FGAerodynamics" << endl;
328   }
329   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
330   }
331   if (debug_lvl & 8 ) { // Runtime state variables
332   }
333   if (debug_lvl & 16) { // Sanity checking
334   }
335   if (debug_lvl & 64) {
336     if (from == 0) { // Constructor
337       cout << IdSrc << endl;
338       cout << IdHdr << endl;
339     }
340   }
341 }
342