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