]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGTank.cpp
Sync. with JSBSim CVS
[flightgear.git] / src / FDM / JSBSim / models / propulsion / FGTank.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGTank.cpp
4  Author:       Jon Berndt
5  Date started: 01/21/99
6  Called by:    FGAircraft
7
8  ------------- Copyright (C) 1999  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 See header file.
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 01/21/99   JSB   Created
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGTank.h"
40
41 using std::cerr;
42 using std::endl;
43 using std::cout;
44
45 namespace JSBSim {
46
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_TANK;
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 CLASS IMPLEMENTATION
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 FGTank::FGTank(FGFDMExec* exec, Element* el, int tank_number)
55                   : TankNumber(tank_number)
56 {
57   string token;
58   Element* element;
59   Element* element_Grain;
60   Area = 1.0;
61   Temperature = -9999.0;
62   Auxiliary = exec->GetAuxiliary();
63   Radius = Capacity = Contents = Standpipe = Length = InnerRadius = 0.0;
64   PropertyManager = exec->GetPropertyManager();
65   vXYZ.InitMatrix();
66   vXYZ_drain.InitMatrix();
67
68   type = el->GetAttributeValue("type");
69   if      (type == "FUEL")     Type = ttFUEL;
70   else if (type == "OXIDIZER") Type = ttOXIDIZER;
71   else                         Type = ttUNKNOWN;
72
73   element = el->FindElement("location");
74   if (element)  vXYZ = element->FindElementTripletConvertTo("IN");
75   else          cerr << "No location found for this tank." << endl;
76
77   vXYZ_drain = vXYZ; // Set initial drain location to initial tank CG
78
79   element = el->FindElement("drain_location");
80   if (element)  {
81     vXYZ_drain = element->FindElementTripletConvertTo("IN");
82   }
83
84   if (el->FindElement("radius"))
85     Radius = el->FindElementValueAsNumberConvertTo("radius", "IN");
86   if (el->FindElement("capacity"))
87     Capacity = el->FindElementValueAsNumberConvertTo("capacity", "LBS");
88   if (el->FindElement("contents"))
89     InitialContents = Contents = el->FindElementValueAsNumberConvertTo("contents", "LBS");
90   if (el->FindElement("temperature"))
91     InitialTemperature = Temperature = el->FindElementValueAsNumber("temperature");
92   if (el->FindElement("standpipe"))
93     InitialStandpipe = Standpipe = el->FindElementValueAsNumberConvertTo("standpipe", "LBS");
94
95   Selected = true;
96
97   if (Capacity != 0) {
98     PctFull = 100.0*Contents/Capacity;            // percent full; 0 to 100.0
99   } else {
100     Contents = 0;
101     PctFull  = 0;
102   }
103
104   // Check whether this is a solid propellant "tank". Initialize it if true.
105
106   grainType = gtUNKNOWN; // This is the default
107   
108   element_Grain = el->FindElement("grain_config");
109   if (element_Grain) {
110
111     strGType = element_Grain->GetAttributeValue("type");
112     if (strGType == "CYLINDRICAL")     grainType = gtCYLINDRICAL;
113     else if (strGType == "ENDBURNING") grainType = gtENDBURNING;
114     else                               cerr << "Unknown propellant grain type specified" << endl;
115
116     if (element_Grain->FindElement("length"))
117       Length = element_Grain->FindElementValueAsNumberConvertTo("length", "IN");
118     if (element_Grain->FindElement("bore_diameter"))
119       InnerRadius = element_Grain->FindElementValueAsNumberConvertTo("bore_diameter", "IN")/2.0;
120
121     // Initialize solid propellant values for debug and runtime use.
122
123     switch (grainType) {
124       case gtCYLINDRICAL:
125         if (Radius <= InnerRadius) {
126           cerr << "The bore diameter should be smaller than the total grain diameter!" << endl;
127           exit(-1);
128         }
129         Volume = M_PI * Length * (Radius*Radius - InnerRadius*InnerRadius); // cubic inches
130         break;
131       case gtENDBURNING:
132         Volume = M_PI * Length * Radius * Radius; // cubic inches
133         break;
134       case gtUNKNOWN:
135         cerr << "Unknown grain type found in this rocket engine definition." << endl;
136         exit(-1);
137     }
138     Density = (Contents*lbtoslug)/Volume; // slugs/in^3
139
140   }
141
142   char property_name[80];
143   snprintf(property_name, 80, "propulsion/tank[%d]/contents-lbs", TankNumber);
144   PropertyManager->Tie( property_name, (FGTank*)this, &FGTank::GetContents,
145                                        &FGTank::SetContents );
146
147   if (Temperature != -9999.0)  InitialTemperature = Temperature = FahrenheitToCelsius(Temperature);
148   Area = 40.0 * pow(Capacity/1975, 0.666666667);
149
150   Debug(0);
151 }
152
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154
155 FGTank::~FGTank()
156 {
157   Debug(1);
158 }
159
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162 void FGTank::ResetToIC(void)
163 {
164   Temperature = InitialTemperature;
165   Standpipe = InitialStandpipe;
166   Contents = InitialContents;
167   PctFull = 100.0*Contents/Capacity;
168   Selected = true;
169 }
170
171 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172
173 const FGColumnVector3 FGTank::GetXYZ(void)
174 {
175   return vXYZ_drain + (Contents/Capacity)*(vXYZ - vXYZ_drain);
176 }
177
178 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179
180 const double FGTank::GetXYZ(int idx)
181 {
182   return vXYZ_drain(idx) + (Contents/Capacity)*(vXYZ(idx)-vXYZ_drain(idx));
183 }
184
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186
187 double FGTank::Drain(double used)
188 {
189   double remaining = Contents - used;
190
191   if (remaining >= 0) { // Reduce contents by amount used.
192
193     Contents -= used;
194     PctFull = 100.0*Contents/Capacity;
195
196   } else { // This tank must be empty.
197
198     Contents = 0.0;
199     PctFull = 0.0;
200     Selected = false;
201   }
202
203   if (grainType != gtUNKNOWN) CalculateInertias();
204
205   return remaining;
206 }
207
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209
210 double FGTank::Fill(double amount)
211 {
212   double overage = 0.0;
213
214   Contents += amount;
215
216   if (Contents > Capacity) {
217     overage = Contents - Capacity;
218     Contents = Capacity;
219     PctFull = 100.0;
220   } else {
221     PctFull = Contents/Capacity*100.0;
222   }
223   return overage;
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 void FGTank::SetContents(double amount)
229 {
230   Contents = amount;
231   if (Contents > Capacity) {
232     Contents = Capacity;
233     PctFull = 100.0;
234   } else {
235     PctFull = Contents/Capacity*100.0;
236   }
237 }
238
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240
241 double FGTank::Calculate(double dt)
242 {
243   if (Temperature == -9999.0) return 0.0;
244   double HeatCapacity = 900.0;        // Joules/lbm/C
245   double TempFlowFactor = 1.115;      // Watts/sqft/C
246   double TAT = Auxiliary->GetTAT_C();
247   double Tdiff = TAT - Temperature;
248   double dTemp = 0.0;                 // Temp change due to one surface
249   if (fabs(Tdiff) > 0.1) {
250     dTemp = (TempFlowFactor * Area * Tdiff * dt) / (Contents * HeatCapacity);
251   }
252   return Temperature += (dTemp + dTemp);    // For now, assume upper/lower the same
253 }
254
255 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 //  This function calculates the moments of inertia for a solid propellant
257 //  grain - either an end burning cylindrical grain or a bored cylindrical
258 //  grain.
259
260 void FGTank::CalculateInertias(void)
261 {
262   double Mass = Contents*lbtoslug;
263   double RadSumSqr;
264   double Rad2 = Radius*Radius;
265   Volume = (Contents*lbtoslug)/Density; // in^3
266
267   switch (grainType) {
268     case gtCYLINDRICAL:
269       InnerRadius = sqrt(Rad2 - Volume/(M_PI * Length));
270       RadSumSqr = (Rad2 + InnerRadius*InnerRadius)/144.0;
271       Ixx = 0.5*Mass*RadSumSqr;
272       Iyy = Mass*(3.0*RadSumSqr + Length*Length/144.0)/12.0;
273       break;
274     case gtENDBURNING:
275       Length = Volume/(M_PI*Rad2);
276       Ixx = 0.5*Mass*Rad2/144.0;
277       Iyy = Mass*(3.0*Rad2 + Length*Length)/(144.0*12.0);
278       break;
279   }
280   Izz  = Iyy;
281
282 }
283
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 //    The bitmasked value choices are as follows:
286 //    unset: In this case (the default) JSBSim would only print
287 //       out the normally expected messages, essentially echoing
288 //       the config files as they are read. If the environment
289 //       variable is not set, debug_lvl is set to 1 internally
290 //    0: This requests JSBSim not to output any messages
291 //       whatsoever.
292 //    1: This value explicity requests the normal JSBSim
293 //       startup messages
294 //    2: This value asks for a message to be printed out when
295 //       a class is instantiated
296 //    4: When this value is set, a message is displayed when a
297 //       FGModel object executes its Run() method
298 //    8: When this value is set, various runtime state variables
299 //       are printed out periodically
300 //    16: When set various parameters are sanity checked and
301 //       a message is printed out when they go out of bounds
302
303 void FGTank::Debug(int from)
304 {
305   if (debug_lvl <= 0) return;
306
307   if (debug_lvl & 1) { // Standard console startup message output
308     if (from == 0) { // Constructor
309       cout << "      " << type << " tank holds " << Capacity << " lbs. " << type << endl;
310       cout << "      currently at " << PctFull << "% of maximum capacity" << endl;
311       cout << "      Tank location (X, Y, Z): " << vXYZ(eX) << ", " << vXYZ(eY) << ", " << vXYZ(eZ) << endl;
312       cout << "      Effective radius: " << Radius << " inches" << endl;
313       cout << "      Initial temperature: " << Temperature << " Fahrenheit" << endl;
314     }
315   }
316   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
317     if (from == 0) cout << "Instantiated: FGTank" << endl;
318     if (from == 1) cout << "Destroyed:    FGTank" << endl;
319   }
320   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
321   }
322   if (debug_lvl & 8 ) { // Runtime state variables
323   }
324   if (debug_lvl & 16) { // Sanity checking
325   }
326   if (debug_lvl & 64) {
327     if (from == 0) { // Constructor
328       cout << IdSrc << endl;
329       cout << IdHdr << endl;
330     }
331   }
332 }
333 }