]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGAerodynamics.cpp
Fix a segfault on exit. Introduces a small memory leak if the instrument
[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
43 static const char *IdSrc = "$Id$";
44 static const char *IdHdr = ID_AERODYNAMICS;
45
46 extern short debug_lvl;
47
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 CLASS IMPLEMENTATION
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
52
53 FGAerodynamics::FGAerodynamics(FGFDMExec* FDMExec) : FGModel(FDMExec),
54     vMoments(3),
55     vForces(3),
56     vFs(3),
57     vLastFs(3),
58     vDXYZcg(3)
59 {
60   Name = "FGAerodynamics";
61
62   AxisIdx["DRAG"]  = 0;
63   AxisIdx["SIDE"]  = 1;
64   AxisIdx["LIFT"]  = 2;
65   AxisIdx["ROLL"]  = 3;
66   AxisIdx["PITCH"] = 4;
67   AxisIdx["YAW"]   = 5;
68
69   Coeff = new CoeffArray[6];
70
71   if (debug_lvl & 2) cout << "Instantiated: FGAerodynamics" << endl;
72 }
73
74 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75
76 FGAerodynamics::~FGAerodynamics()
77 {
78   unsigned int i,j;
79   for (i=0; i<6; i++) {
80     for (j=0; j<Coeff[i].size(); j++) {
81       delete Coeff[i][j];
82     }
83   }
84   delete[] Coeff;
85
86   if (debug_lvl & 2) cout << "Destroyed:    FGAerodynamics" << endl;
87 }
88
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
91 bool FGAerodynamics::Run(void)
92 {
93   float alpha, beta;
94   unsigned int axis_ctr,ctr;
95
96   if (!FGModel::Run()) {
97
98     alpha = Translation->Getalpha();
99     beta = Translation->Getbeta();
100
101     vLastFs = vFs;
102     vFs.InitMatrix();
103
104     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
105       for (ctr=0; ctr < Coeff[axis_ctr].size(); ctr++) {
106         vFs(axis_ctr+1) += Coeff[axis_ctr][ctr]->TotalValue();
107       }
108     }
109
110     vForces = State->GetTs2b(alpha, beta)*vFs;
111
112     // see http://home.earthlink.net/~apeden/jsbsim_moments_due_to_forces.txt
113     // for details on this
114
115     vDXYZcg(eX) = -(Aircraft->GetXYZrp(eX) - MassBalance->GetXYZcg(eX))/12.0;
116     vDXYZcg(eY) =  (Aircraft->GetXYZrp(eY) - MassBalance->GetXYZcg(eY))/12.0;
117     vDXYZcg(eZ) = -(Aircraft->GetXYZrp(eZ) - MassBalance->GetXYZcg(eZ))/12.0;
118
119     vMoments(eL) = vForces(eZ)*vDXYZcg(eY) - vForces(eY)*vDXYZcg(eZ);
120     vMoments(eM) = vForces(eX)*vDXYZcg(eZ) - vForces(eZ)*vDXYZcg(eX);
121     vMoments(eN) = vForces(eY)*vDXYZcg(eX) - vForces(eX)*vDXYZcg(eY);
122
123     for (axis_ctr = 0; axis_ctr < 3; axis_ctr++) {
124       for (ctr = 0; ctr < Coeff[axis_ctr+3].size(); ctr++) {
125         vMoments(axis_ctr+1) += Coeff[axis_ctr+3][ctr]->TotalValue();
126       }
127     }
128     return false;
129   } else {
130     return true;
131   }
132 }
133
134 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135
136 bool FGAerodynamics::Load(FGConfigFile* AC_cfg)
137 {
138   string token, axis;
139
140   AC_cfg->GetNextConfigLine();
141
142   while ((token = AC_cfg->GetValue()) != "/AERODYNAMICS") {
143     if (token == "AXIS") {
144       CoeffArray ca;
145       axis = AC_cfg->GetValue("NAME");
146       AC_cfg->GetNextConfigLine();
147       while ((token = AC_cfg->GetValue()) != "/AXIS") {
148         if( token == "COEFFICIENT" ) {
149           ca.push_back( new FGCoefficient(FDMExec) );
150           ca.back()->Load(AC_cfg);
151         } else if ( token == "GROUP" ) {
152           ca.push_back( new FGFactorGroup(FDMExec) );
153           ca.back()->Load(AC_cfg);
154         }
155       }
156       Coeff[AxisIdx[axis]] = ca;
157       AC_cfg->GetNextConfigLine();
158     }
159   }
160
161   return true;
162 }
163
164 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165
166 string FGAerodynamics::GetCoefficientStrings(void)
167 {
168   string CoeffStrings = "";
169   bool firstime = true;
170   unsigned int axis, sd;
171
172   for (axis = 0; axis < 6; axis++) {
173     for (sd = 0; sd < Coeff[axis].size(); sd++) {
174       if (firstime) {
175         firstime = false;
176       } else {
177         CoeffStrings += ", ";
178       }
179       CoeffStrings += Coeff[axis][sd]->GetCoefficientStrings();
180     }
181   }
182   return CoeffStrings;
183 }
184
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186
187 string FGAerodynamics::GetCoefficientValues(void)
188 {
189   string SDValues = "";
190   char buffer[10];
191   bool firstime = true;
192
193   for (unsigned int axis = 0; axis < 6; axis++) {
194     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
195       if (firstime) {
196         firstime = false;
197       } else {
198         SDValues += ", ";
199       }
200       SDValues += Coeff[axis][sd]->GetCoefficientValues();
201     }
202   }
203
204   return SDValues;
205 }
206
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208
209 float FGAerodynamics::GetNlf(void)
210 {
211   if (fabs(Position->GetGamma()) < 1.57) {
212     return (vFs(eZ)/(MassBalance->GetWeight()*cos(Position->GetGamma())));
213   } else {
214     return 0.0;
215   }
216 }
217
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219
220 float FGAerodynamics::GetLoD(void)
221 {
222   float LoD;
223
224   if (vFs(1) != 0.00) return vFs(3)/vFs(1);
225   else                return 0.00;
226 }
227
228 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229
230 void FGAerodynamics::Debug(void)
231 {
232     //TODO: Add your source code here
233 }
234