]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGTank.cpp
Merge branch 'next' of git://gitorious.org/fg/flightgear into next
[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 (jon@jsbsim.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 #include "FGFDMExec.h"
41 #include "models/FGAuxiliary.h"
42 #include "input_output/FGXMLElement.h"
43 #include "input_output/FGPropertyManager.h"
44 #include <iostream>
45 #include <cstdlib>
46
47 using namespace std;
48
49 namespace JSBSim {
50
51 static const char *IdSrc = "$Id: FGTank.cpp,v 1.29 2011/06/06 22:39:52 jentron Exp $";
52 static const char *IdHdr = ID_TANK;
53
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 CLASS IMPLEMENTATION
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57
58 FGTank::FGTank(FGFDMExec* exec, Element* el, int tank_number)
59                   : TankNumber(tank_number), Exec(exec)
60 {
61   string token, strFuelName;
62   Element* element;
63   Element* element_Grain;
64   Area = 1.0;
65   Density = 6.6;
66   InitialTemperature = Temperature = -9999.0;
67   Ixx = Iyy = Izz = 0.0;
68   Radius = Contents = Standpipe = Length = InnerRadius = 0.0;
69   ExternalFlow = 0.0;
70   InitialStandpipe = 0.0;
71   Capacity = 0.00001;
72   Priority = InitialPriority = 1;
73   PropertyManager = Exec->GetPropertyManager();
74   vXYZ.InitMatrix();
75   vXYZ_drain.InitMatrix();
76
77   type = el->GetAttributeValue("type");
78   if      (type == "FUEL")     Type = ttFUEL;
79   else if (type == "OXIDIZER") Type = ttOXIDIZER;
80   else                         Type = ttUNKNOWN;
81
82   element = el->FindElement("location");
83   if (element)  vXYZ = element->FindElementTripletConvertTo("IN");
84   else          cerr << "No location found for this tank." << endl;
85
86   vXYZ_drain = vXYZ; // Set initial drain location to initial tank CG
87
88   element = el->FindElement("drain_location");
89   if (element)  {
90     vXYZ_drain = element->FindElementTripletConvertTo("IN");
91   }
92
93   if (el->FindElement("radius"))
94     Radius = el->FindElementValueAsNumberConvertTo("radius", "IN");
95   if (el->FindElement("capacity"))
96     Capacity = el->FindElementValueAsNumberConvertTo("capacity", "LBS");
97   if (el->FindElement("contents"))
98     InitialContents = Contents = el->FindElementValueAsNumberConvertTo("contents", "LBS");
99   if (el->FindElement("temperature"))
100     InitialTemperature = Temperature = el->FindElementValueAsNumber("temperature");
101   if (el->FindElement("standpipe"))
102     InitialStandpipe = Standpipe = el->FindElementValueAsNumberConvertTo("standpipe", "LBS");
103   if (el->FindElement("priority"))
104     InitialPriority = Priority = el->FindElementValueAsNumber("priority");
105   if (el->FindElement("density"))
106     Density = el->FindElementValueAsNumberConvertTo("density", "LBS/GAL");
107   if (el->FindElement("type"))
108     strFuelName = el->FindElementValue("type");
109
110
111   SetPriority( InitialPriority );     // this will also set the Selected flag
112
113   if (Capacity == 0) {
114     cerr << "Tank capacity must not be zero. Reset to 0.00001 lbs!" << endl;
115     Capacity = 0.00001;
116     Contents = 0.0;
117   }
118   PctFull = 100.0*Contents/Capacity;            // percent full; 0 to 100.0
119
120   // Check whether this is a solid propellant "tank". Initialize it if true.
121
122   grainType = gtUNKNOWN; // This is the default
123   
124   element_Grain = el->FindElement("grain_config");
125   if (element_Grain) {
126
127     strGType = element_Grain->GetAttributeValue("type");
128     if (strGType == "CYLINDRICAL")     grainType = gtCYLINDRICAL;
129     else if (strGType == "ENDBURNING") grainType = gtENDBURNING;
130     else                               cerr << "Unknown propellant grain type specified" << endl;
131
132     if (element_Grain->FindElement("length"))
133       Length = element_Grain->FindElementValueAsNumberConvertTo("length", "IN");
134     if (element_Grain->FindElement("bore_diameter"))
135       InnerRadius = element_Grain->FindElementValueAsNumberConvertTo("bore_diameter", "IN")/2.0;
136
137     // Initialize solid propellant values for debug and runtime use.
138
139     switch (grainType) {
140       case gtCYLINDRICAL:
141         if (Radius <= InnerRadius) {
142           cerr << "The bore diameter should be smaller than the total grain diameter!" << endl;
143           exit(-1);
144         }
145         Volume = M_PI * Length * (Radius*Radius - InnerRadius*InnerRadius); // cubic inches
146         break;
147       case gtENDBURNING:
148         Volume = M_PI * Length * Radius * Radius; // cubic inches
149         break;
150       case gtUNKNOWN:
151         cerr << "Unknown grain type found in this rocket engine definition." << endl;
152         exit(-1);
153     }
154     Density = (Contents*lbtoslug)/Volume; // slugs/in^3
155     CalculateInertias();
156   }
157
158   string property_name, base_property_name;
159   base_property_name = CreateIndexedPropertyName("propulsion/tank", TankNumber);
160   property_name = base_property_name + "/contents-lbs";
161   PropertyManager->Tie( property_name.c_str(), (FGTank*)this, &FGTank::GetContents,
162                                        &FGTank::SetContents );
163   property_name = base_property_name + "/priority";
164   PropertyManager->Tie( property_name.c_str(), (FGTank*)this, &FGTank::GetPriority,
165                                        &FGTank::SetPriority );
166   property_name = base_property_name + "/external-flow-rate-pps";
167   PropertyManager->Tie( property_name.c_str(), (FGTank*)this, &FGTank::GetExternalFlow,
168                                        &FGTank::SetExternalFlow );
169
170   if (Temperature != -9999.0)  InitialTemperature = Temperature = FahrenheitToCelsius(Temperature);
171   Area = 40.0 * pow(Capacity/1975, 0.666666667);
172
173   // A named fuel type will override a previous density value
174   if (!strFuelName.empty()) Density = ProcessFuelName(strFuelName); 
175
176   Debug(0);
177 }
178
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180
181 FGTank::~FGTank()
182 {
183   Debug(1);
184 }
185
186 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187
188 void FGTank::ResetToIC(void)
189 {
190   SetTemperature( InitialTemperature );
191   SetStandpipe ( InitialStandpipe );
192   SetContents ( InitialContents );
193   PctFull = 100.0*Contents/Capacity;
194   SetPriority( InitialPriority );
195 }
196
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198
199 const FGColumnVector3 FGTank::GetXYZ(void)
200 {
201   return vXYZ_drain + (Contents/Capacity)*(vXYZ - vXYZ_drain);
202 }
203
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205
206 const double FGTank::GetXYZ(int idx)
207 {
208   return vXYZ_drain(idx) + (Contents/Capacity)*(vXYZ(idx)-vXYZ_drain(idx));
209 }
210
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212
213 double FGTank::Drain(double used)
214 {
215   double remaining = Contents - used;
216
217   if (remaining >= 0) { // Reduce contents by amount used.
218
219     Contents -= used;
220     PctFull = 100.0*Contents/Capacity;
221
222   } else { // This tank must be empty.
223
224     Contents = 0.0;
225     PctFull = 0.0;
226   }
227
228   if (grainType != gtUNKNOWN) CalculateInertias();
229
230   return remaining;
231 }
232
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235 double FGTank::Fill(double amount)
236 {
237   double overage = 0.0;
238
239   Contents += amount;
240
241   if (Contents > Capacity) {
242     overage = Contents - Capacity;
243     Contents = Capacity;
244     PctFull = 100.0;
245   } else {
246     PctFull = Contents/Capacity*100.0;
247   }
248   return overage;
249 }
250
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252
253 void FGTank::SetContents(double amount)
254 {
255   Contents = amount;
256   if (Contents > Capacity) {
257     Contents = Capacity;
258     PctFull = 100.0;
259   } else {
260     PctFull = Contents/Capacity*100.0;
261   }
262 }
263
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
265
266 void FGTank::SetContentsGallons(double gallons)
267 {
268   SetContents(gallons * Density);
269 }
270
271
272 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273
274 double FGTank::Calculate(double dt)
275 {
276   if(ExternalFlow < 0.) Drain( -ExternalFlow *dt);
277   else Fill(ExternalFlow * dt);
278
279   if (Temperature == -9999.0) return 0.0;
280   double HeatCapacity = 900.0;        // Joules/lbm/C
281   double TempFlowFactor = 1.115;      // Watts/sqft/C
282   double TAT = Exec->GetAuxiliary()->GetTAT_C();
283   double Tdiff = TAT - Temperature;
284   double dTemp = 0.0;                 // Temp change due to one surface
285   if (fabs(Tdiff) > 0.1) {
286     dTemp = (TempFlowFactor * Area * Tdiff * dt) / (Contents * HeatCapacity);
287   }
288
289   return Temperature += (dTemp + dTemp);    // For now, assume upper/lower the same
290 }
291
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 //  This function calculates the moments of inertia for a solid propellant
294 //  grain - either an end burning cylindrical grain or a bored cylindrical
295 //  grain.
296
297 void FGTank::CalculateInertias(void)
298 {
299   double Mass = Contents*lbtoslug;
300   double RadSumSqr;
301   double Rad2 = Radius*Radius;
302
303   if (Density > 0.0) {
304     Volume = (Contents*lbtoslug)/Density; // in^3
305   } else {
306     cerr << endl << "  Solid propellant grain density is zero!" << endl << endl;
307     exit(-1);
308   }
309
310   switch (grainType) {
311     case gtCYLINDRICAL:
312       InnerRadius = sqrt(Rad2 - Volume/(M_PI * Length));
313       RadSumSqr = (Rad2 + InnerRadius*InnerRadius)/144.0;
314       Ixx = 0.5*Mass*RadSumSqr;
315       Iyy = Mass*(3.0*RadSumSqr + Length*Length/144.0)/12.0;
316       break;
317     case gtENDBURNING:
318       Length = Volume/(M_PI*Rad2);
319       Ixx = 0.5*Mass*Rad2/144.0;
320       Iyy = Mass*(3.0*Rad2 + Length*Length)/(144.0*12.0);
321       break;
322     case gtUNKNOWN:
323       cerr << "Unknown grain type found." << endl;
324       exit(-1);
325       break;
326   }
327   Izz  = Iyy;
328
329 }
330
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332
333 double FGTank::ProcessFuelName(std::string const& name)
334 {
335    if      (name == "AVGAS")    return 6.02; 
336    else if (name == "JET-A")    return 6.74;
337    else if (name == "JET-A1")   return 6.74;
338    else if (name == "JET-B")    return 6.48;
339    else if (name == "JP-1")     return 6.76;
340    else if (name == "JP-2")     return 6.38;
341    else if (name == "JP-3")     return 6.34;
342    else if (name == "JP-4")     return 6.48;
343    else if (name == "JP-5")     return 6.81;
344    else if (name == "JP-6")     return 6.55;
345    else if (name == "JP-7")     return 6.61;
346    else if (name == "JP-8")     return 6.66;
347    else if (name == "JP-8+100") return 6.66;
348  //else if (name == "JP-9")     return 6.74;
349  //else if (name == "JPTS")     return 6.74;
350    else if (name == "RP-1")     return 6.73;
351    else if (name == "T-1")      return 6.88;
352    else if (name == "ETHANOL")  return 6.58;
353    else if (name == "HYDRAZINE")return 8.61;
354    else if (name == "F-34")     return 6.66;
355    else if (name == "F-35")     return 6.74;
356    else if (name == "F-40")     return 6.48;
357    else if (name == "F-44")     return 6.81;
358    else if (name == "AVTAG")    return 6.48;
359    else if (name == "AVCAT")    return 6.81;
360    else {
361      cerr << "Unknown fuel type specified: "<< name << endl;
362    } 
363
364    return 6.6;
365 }
366
367
368 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
369 //    The bitmasked value choices are as follows:
370 //    unset: In this case (the default) JSBSim would only print
371 //       out the normally expected messages, essentially echoing
372 //       the config files as they are read. If the environment
373 //       variable is not set, debug_lvl is set to 1 internally
374 //    0: This requests JSBSim not to output any messages
375 //       whatsoever.
376 //    1: This value explicity requests the normal JSBSim
377 //       startup messages
378 //    2: This value asks for a message to be printed out when
379 //       a class is instantiated
380 //    4: When this value is set, a message is displayed when a
381 //       FGModel object executes its Run() method
382 //    8: When this value is set, various runtime state variables
383 //       are printed out periodically
384 //    16: When set various parameters are sanity checked and
385 //       a message is printed out when they go out of bounds
386
387 void FGTank::Debug(int from)
388 {
389   if (debug_lvl <= 0) return;
390
391   if (debug_lvl & 1) { // Standard console startup message output
392     if (from == 0) { // Constructor
393       cout << "      " << type << " tank holds " << Capacity << " lbs. " << type << endl;
394       cout << "      currently at " << PctFull << "% of maximum capacity" << endl;
395       cout << "      Tank location (X, Y, Z): " << vXYZ(eX) << ", " << vXYZ(eY) << ", " << vXYZ(eZ) << endl;
396       cout << "      Effective radius: " << Radius << " inches" << endl;
397       cout << "      Initial temperature: " << Temperature << " Fahrenheit" << endl;
398       cout << "      Priority: " << Priority << endl;
399     }
400   }
401   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
402     if (from == 0) cout << "Instantiated: FGTank" << endl;
403     if (from == 1) cout << "Destroyed:    FGTank" << endl;
404   }
405   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
406   }
407   if (debug_lvl & 8 ) { // Runtime state variables
408   }
409   if (debug_lvl & 16) { // Sanity checking
410   }
411   if (debug_lvl & 64) {
412     if (from == 0) { // Constructor
413       cout << IdSrc << endl;
414       cout << IdHdr << endl;
415     }
416   }
417 }
418 }