]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGMassBalance.cpp
Added FGTurbine.[ch]*
[flightgear.git] / src / FDM / JSBSim / FGMassBalance.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGMassBalance.cpp
4  Author:       Jon S. Berndt
5  Date started: 09/12/2000
6  Purpose:      This module models weight and balance
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 This class models the change in weight and balance of the aircraft due to fuel
31 burnoff, etc.
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 09/12/2000  JSB  Created
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include "FGMassBalance.h"
42 #include "FGPropertyManager.h"
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_MASSBALANCE;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51
52 FGMassBalance::FGMassBalance(FGFDMExec* fdmex) : FGModel(fdmex)
53 {
54   Name = "FGMassBalance";
55   Weight = EmptyWeight = Mass = 0.0;
56   Ixx = Iyy = Izz = Ixy = Ixz = 0.0;
57   baseIxx = baseIyy = baseIzz = baseIxy = baseIxz = 0.0;
58   vbaseXYZcg(eX) = 0.0;
59   vbaseXYZcg(eY) = 0.0;
60   vbaseXYZcg(eZ) = 0.0;
61   bind();
62
63   Debug(0);
64 }
65
66 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67
68 FGMassBalance::~FGMassBalance()
69 {
70   unbind();
71   Debug(1);
72 }
73
74 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75
76 bool FGMassBalance::Run(void)
77 {
78   if (!FGModel::Run()) {
79
80     Weight = EmptyWeight + Propulsion->GetTanksWeight() + GetPointMassWeight();
81
82     Mass = Weight / Inertial->gravity();
83
84 // Calculate new CG here.
85
86     vXYZcg = (Propulsion->GetTanksMoment() + EmptyWeight*vbaseXYZcg
87                                        + GetPointMassMoment() ) / Weight;
88
89 // Calculate new moments of inertia here
90
91     Ixx = baseIxx + Propulsion->GetTanksIxx(vXYZcg) + GetPMIxx();
92     Iyy = baseIyy + Propulsion->GetTanksIyy(vXYZcg) + GetPMIyy();
93     Izz = baseIzz + Propulsion->GetTanksIzz(vXYZcg) + GetPMIzz();
94     Ixy = baseIxy + Propulsion->GetTanksIxy(vXYZcg) + GetPMIxy();
95     Ixz = baseIxz + Propulsion->GetTanksIxz(vXYZcg) + GetPMIxz();
96
97     Debug(2);
98
99     return false;
100   } else {
101     return true;
102   }
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 void FGMassBalance::AddPointMass(double weight, double X, double Y, double Z)
108 {
109   PointMassLoc.push_back(*(new FGColumnVector3(X, Y, Z)));
110   PointMassWeight.push_back(weight);
111 }
112
113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114
115 double FGMassBalance::GetPointMassWeight(void)
116 {
117   double PM_total_weight = 0.0;
118
119   for (unsigned int i=0; i<PointMassWeight.size(); i++) {
120     PM_total_weight += PointMassWeight[i];
121   }
122   return PM_total_weight;
123 }
124
125 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126
127 FGColumnVector3& FGMassBalance::GetPointMassMoment(void)
128 {
129   PointMassCG.InitMatrix();
130
131   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
132     PointMassCG += PointMassWeight[i]*PointMassLoc[i];
133   }
134   return PointMassCG;
135 }
136
137 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138
139 double FGMassBalance::GetPMIxx(void)
140 {
141   double I = 0.0;
142   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
143     I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eX)-vXYZcg(eX))*PointMassWeight[i];
144   }
145   I /= (144.0*Inertial->gravity());
146   return I;
147 }
148
149 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150
151 double FGMassBalance::GetPMIyy(void)
152 {
153   double I = 0.0;
154   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
155     I += (PointMassLoc[i](eY)-vXYZcg(eY))*(PointMassLoc[i](eY)-vXYZcg(eY))*PointMassWeight[i];
156   }
157   I /= (144.0*Inertial->gravity());
158   return I;
159 }
160
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162
163 double FGMassBalance::GetPMIzz(void)
164 {
165   double I = 0.0;
166   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
167     I += (PointMassLoc[i](eZ)-vXYZcg(eZ))*(PointMassLoc[i](eZ)-vXYZcg(eZ))*PointMassWeight[i];
168   }
169   I /= (144.0*Inertial->gravity());
170   return I;
171 }
172
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 double FGMassBalance::GetPMIxy(void)
176 {
177   double I = 0.0;
178   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
179     I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eY)-vXYZcg(eY))*PointMassWeight[i];
180   }
181   I /= (144.0*Inertial->gravity());
182   return I;
183 }
184
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186
187 double FGMassBalance::GetPMIxz(void)
188 {
189   double I = 0.0;
190   for (unsigned int i=0; i<PointMassLoc.size(); i++) {
191     I += (PointMassLoc[i](eX)-vXYZcg(eX))*(PointMassLoc[i](eZ)-vXYZcg(eZ))*PointMassWeight[i];
192   }
193   I /= (144.0*Inertial->gravity());
194   return I;
195 }
196
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198
199 void FGMassBalance::bind(void)
200
201   typedef double (FGMassBalance::*PMF)(int) const;
202   PropertyManager->Tie("inertia/mass-slugs", this,
203                        &FGMassBalance::GetMass);
204   PropertyManager->Tie("inertia/weight-lbs", this,
205                        &FGMassBalance::GetWeight);
206   PropertyManager->Tie("inertia/ixx-lbsft2", this,
207                        &FGMassBalance::GetIxx);
208   PropertyManager->Tie("inertia/iyy-lbsft2", this,
209                        &FGMassBalance::GetIyy);
210   PropertyManager->Tie("inertia/izz-lbsft2", this,
211                        &FGMassBalance::GetIzz);
212   PropertyManager->Tie("inertia/ixy-lbsft2", this,
213                        &FGMassBalance::GetIxy);
214   PropertyManager->Tie("inertia/ixz-lbsft2", this,
215                        &FGMassBalance::GetIxz);
216   PropertyManager->Tie("inertia/cg-x-ft", this,1,
217                        (PMF)&FGMassBalance::GetXYZcg);
218   PropertyManager->Tie("inertia/cg-y-ft", this,2,
219                        (PMF)&FGMassBalance::GetXYZcg);
220   PropertyManager->Tie("inertia/cg-z-ft", this,3,
221                        (PMF)&FGMassBalance::GetXYZcg);
222 }
223
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225
226 void FGMassBalance::unbind(void)
227 {
228   PropertyManager->Untie("inertia/mass-slugs");
229   PropertyManager->Untie("inertia/weight-lbs");
230   PropertyManager->Untie("inertia/ixx-lbsft2");
231   PropertyManager->Untie("inertia/iyy-lbsft2");
232   PropertyManager->Untie("inertia/izz-lbsft2");
233   PropertyManager->Untie("inertia/ixy-lbsft2");
234   PropertyManager->Untie("inertia/ixz-lbsft2");
235   PropertyManager->Untie("inertia/cg-x-ft");
236   PropertyManager->Untie("inertia/cg-y-ft");
237   PropertyManager->Untie("inertia/cg-z-ft");
238 }
239
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 //    The bitmasked value choices are as follows:
242 //    unset: In this case (the default) JSBSim would only print
243 //       out the normally expected messages, essentially echoing
244 //       the config files as they are read. If the environment
245 //       variable is not set, debug_lvl is set to 1 internally
246 //    0: This requests JSBSim not to output any messages
247 //       whatsoever.
248 //    1: This value explicity requests the normal JSBSim
249 //       startup messages
250 //    2: This value asks for a message to be printed out when
251 //       a class is instantiated
252 //    4: When this value is set, a message is displayed when a
253 //       FGModel object executes its Run() method
254 //    8: When this value is set, various runtime state variables
255 //       are printed out periodically
256 //    16: When set various parameters are sanity checked and
257 //       a message is printed out when they go out of bounds
258
259 void FGMassBalance::Debug(int from)
260 {
261   if (debug_lvl <= 0) return;
262
263   if (debug_lvl & 1) { // Standard console startup message output
264     if (from == 0) { // Constructor
265
266     }
267   }
268   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
269     if (from == 0) cout << "Instantiated: FGPiston" << endl;
270     if (from == 1) cout << "Destroyed:    FGPiston" << endl;
271   }
272   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
273   }
274   if (debug_lvl & 8 ) { // Runtime state variables
275   }
276   if (debug_lvl & 16) { // Sanity checking
277     if (from == 2) {
278       if (EmptyWeight <= 0.0 || EmptyWeight > 1e9)
279         cout << "MassBalance::EmptyWeight out of bounds: " << EmptyWeight << endl;
280       if (Weight <= 0.0 || Weight > 1e9)
281         cout << "MassBalance::Weight out of bounds: " << Weight << endl;
282       if (Mass <= 0.0 || Mass > 1e9)
283         cout << "MassBalance::Mass out of bounds: " << Mass << endl;
284     }
285   }
286   if (debug_lvl & 64) {
287     if (from == 0) { // Constructor
288       cout << IdSrc << endl;
289       cout << IdHdr << endl;
290     }
291   }
292 }
293