1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGFDMExec.h"
41 #include "models/FGAuxiliary.h"
42 #include "input_output/FGXMLElement.h"
43 #include "input_output/FGPropertyManager.h"
51 static const char *IdSrc = "$Id: FGTank.cpp,v 1.28 2010/01/24 19:26:04 jberndt Exp $";
52 static const char *IdHdr = ID_TANK;
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 FGTank::FGTank(FGFDMExec* exec, Element* el, int tank_number)
59 : TankNumber(tank_number), Exec(exec)
61 string token, strFuelName;
63 Element* element_Grain;
66 InitialTemperature = Temperature = -9999.0;
67 Ixx = Iyy = Izz = 0.0;
68 Radius = Contents = Standpipe = Length = InnerRadius = 0.0;
69 InitialStandpipe = 0.0;
71 Priority = InitialPriority = 1;
72 PropertyManager = Exec->GetPropertyManager();
74 vXYZ_drain.InitMatrix();
76 type = el->GetAttributeValue("type");
77 if (type == "FUEL") Type = ttFUEL;
78 else if (type == "OXIDIZER") Type = ttOXIDIZER;
79 else Type = ttUNKNOWN;
81 element = el->FindElement("location");
82 if (element) vXYZ = element->FindElementTripletConvertTo("IN");
83 else cerr << "No location found for this tank." << endl;
85 vXYZ_drain = vXYZ; // Set initial drain location to initial tank CG
87 element = el->FindElement("drain_location");
89 vXYZ_drain = element->FindElementTripletConvertTo("IN");
92 if (el->FindElement("radius"))
93 Radius = el->FindElementValueAsNumberConvertTo("radius", "IN");
94 if (el->FindElement("capacity"))
95 Capacity = el->FindElementValueAsNumberConvertTo("capacity", "LBS");
96 if (el->FindElement("contents"))
97 InitialContents = Contents = el->FindElementValueAsNumberConvertTo("contents", "LBS");
98 if (el->FindElement("temperature"))
99 InitialTemperature = Temperature = el->FindElementValueAsNumber("temperature");
100 if (el->FindElement("standpipe"))
101 InitialStandpipe = Standpipe = el->FindElementValueAsNumberConvertTo("standpipe", "LBS");
102 if (el->FindElement("priority"))
103 InitialPriority = Priority = el->FindElementValueAsNumber("priority");
104 if (el->FindElement("density"))
105 Density = el->FindElementValueAsNumberConvertTo("density", "LBS/GAL");
106 if (el->FindElement("type"))
107 strFuelName = el->FindElementValue("type");
110 SetPriority( InitialPriority ); // this will also set the Selected flag
113 cerr << "Tank capacity must not be zero. Reset to 0.00001 lbs!" << endl;
117 PctFull = 100.0*Contents/Capacity; // percent full; 0 to 100.0
119 // Check whether this is a solid propellant "tank". Initialize it if true.
121 grainType = gtUNKNOWN; // This is the default
123 element_Grain = el->FindElement("grain_config");
126 strGType = element_Grain->GetAttributeValue("type");
127 if (strGType == "CYLINDRICAL") grainType = gtCYLINDRICAL;
128 else if (strGType == "ENDBURNING") grainType = gtENDBURNING;
129 else cerr << "Unknown propellant grain type specified" << endl;
131 if (element_Grain->FindElement("length"))
132 Length = element_Grain->FindElementValueAsNumberConvertTo("length", "IN");
133 if (element_Grain->FindElement("bore_diameter"))
134 InnerRadius = element_Grain->FindElementValueAsNumberConvertTo("bore_diameter", "IN")/2.0;
136 // Initialize solid propellant values for debug and runtime use.
140 if (Radius <= InnerRadius) {
141 cerr << "The bore diameter should be smaller than the total grain diameter!" << endl;
144 Volume = M_PI * Length * (Radius*Radius - InnerRadius*InnerRadius); // cubic inches
147 Volume = M_PI * Length * Radius * Radius; // cubic inches
150 cerr << "Unknown grain type found in this rocket engine definition." << endl;
153 Density = (Contents*lbtoslug)/Volume; // slugs/in^3
157 string property_name, base_property_name;
158 base_property_name = CreateIndexedPropertyName("propulsion/tank", TankNumber);
159 property_name = base_property_name + "/contents-lbs";
160 PropertyManager->Tie( property_name.c_str(), (FGTank*)this, &FGTank::GetContents,
161 &FGTank::SetContents );
162 property_name = base_property_name + "/priority";
163 PropertyManager->Tie( property_name.c_str(), (FGTank*)this, &FGTank::GetPriority,
164 &FGTank::SetPriority );
166 if (Temperature != -9999.0) InitialTemperature = Temperature = FahrenheitToCelsius(Temperature);
167 Area = 40.0 * pow(Capacity/1975, 0.666666667);
169 // A named fuel type will override a previous density value
170 if (!strFuelName.empty()) Density = ProcessFuelName(strFuelName);
175 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 void FGTank::ResetToIC(void)
186 SetTemperature( InitialTemperature );
187 SetStandpipe ( InitialStandpipe );
188 SetContents ( InitialContents );
189 PctFull = 100.0*Contents/Capacity;
190 SetPriority( InitialPriority );
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 const FGColumnVector3 FGTank::GetXYZ(void)
197 return vXYZ_drain + (Contents/Capacity)*(vXYZ - vXYZ_drain);
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 const double FGTank::GetXYZ(int idx)
204 return vXYZ_drain(idx) + (Contents/Capacity)*(vXYZ(idx)-vXYZ_drain(idx));
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 double FGTank::Drain(double used)
211 double remaining = Contents - used;
213 if (remaining >= 0) { // Reduce contents by amount used.
216 PctFull = 100.0*Contents/Capacity;
218 } else { // This tank must be empty.
224 if (grainType != gtUNKNOWN) CalculateInertias();
229 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 double FGTank::Fill(double amount)
233 double overage = 0.0;
237 if (Contents > Capacity) {
238 overage = Contents - Capacity;
242 PctFull = Contents/Capacity*100.0;
247 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 void FGTank::SetContents(double amount)
252 if (Contents > Capacity) {
256 PctFull = Contents/Capacity*100.0;
260 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 void FGTank::SetContentsGallons(double gallons)
264 SetContents(gallons * Density);
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 double FGTank::Calculate(double dt)
272 if (Temperature == -9999.0) return 0.0;
273 double HeatCapacity = 900.0; // Joules/lbm/C
274 double TempFlowFactor = 1.115; // Watts/sqft/C
275 double TAT = Exec->GetAuxiliary()->GetTAT_C();
276 double Tdiff = TAT - Temperature;
277 double dTemp = 0.0; // Temp change due to one surface
278 if (fabs(Tdiff) > 0.1) {
279 dTemp = (TempFlowFactor * Area * Tdiff * dt) / (Contents * HeatCapacity);
281 return Temperature += (dTemp + dTemp); // For now, assume upper/lower the same
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 // This function calculates the moments of inertia for a solid propellant
286 // grain - either an end burning cylindrical grain or a bored cylindrical
289 void FGTank::CalculateInertias(void)
291 double Mass = Contents*lbtoslug;
293 double Rad2 = Radius*Radius;
296 Volume = (Contents*lbtoslug)/Density; // in^3
298 cerr << endl << " Solid propellant grain density is zero!" << endl << endl;
304 InnerRadius = sqrt(Rad2 - Volume/(M_PI * Length));
305 RadSumSqr = (Rad2 + InnerRadius*InnerRadius)/144.0;
306 Ixx = 0.5*Mass*RadSumSqr;
307 Iyy = Mass*(3.0*RadSumSqr + Length*Length/144.0)/12.0;
310 Length = Volume/(M_PI*Rad2);
311 Ixx = 0.5*Mass*Rad2/144.0;
312 Iyy = Mass*(3.0*Rad2 + Length*Length)/(144.0*12.0);
315 cerr << "Unknown grain type found." << endl;
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 double FGTank::ProcessFuelName(std::string const& name)
327 if (name == "AVGAS") return 6.02;
328 else if (name == "JET-A") return 6.74;
329 else if (name == "JET-A1") return 6.74;
330 else if (name == "JET-B") return 6.48;
331 else if (name == "JP-1") return 6.76;
332 else if (name == "JP-2") return 6.38;
333 else if (name == "JP-3") return 6.34;
334 else if (name == "JP-4") return 6.48;
335 else if (name == "JP-5") return 6.81;
336 else if (name == "JP-6") return 6.55;
337 else if (name == "JP-7") return 6.61;
338 else if (name == "JP-8") return 6.66;
339 else if (name == "JP-8+100") return 6.66;
340 //else if (name == "JP-9") return 6.74;
341 //else if (name == "JPTS") return 6.74;
342 else if (name == "RP-1") return 6.73;
343 else if (name == "T-1") return 6.88;
344 else if (name == "ETHANOL") return 6.58;
345 else if (name == "HYDRAZINE")return 8.61;
346 else if (name == "F-34") return 6.66;
347 else if (name == "F-35") return 6.74;
348 else if (name == "F-40") return 6.48;
349 else if (name == "F-44") return 6.81;
350 else if (name == "AVTAG") return 6.48;
351 else if (name == "AVCAT") return 6.81;
353 cerr << "Unknown fuel type specified: "<< name << endl;
360 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 // The bitmasked value choices are as follows:
362 // unset: In this case (the default) JSBSim would only print
363 // out the normally expected messages, essentially echoing
364 // the config files as they are read. If the environment
365 // variable is not set, debug_lvl is set to 1 internally
366 // 0: This requests JSBSim not to output any messages
368 // 1: This value explicity requests the normal JSBSim
370 // 2: This value asks for a message to be printed out when
371 // a class is instantiated
372 // 4: When this value is set, a message is displayed when a
373 // FGModel object executes its Run() method
374 // 8: When this value is set, various runtime state variables
375 // are printed out periodically
376 // 16: When set various parameters are sanity checked and
377 // a message is printed out when they go out of bounds
379 void FGTank::Debug(int from)
381 if (debug_lvl <= 0) return;
383 if (debug_lvl & 1) { // Standard console startup message output
384 if (from == 0) { // Constructor
385 cout << " " << type << " tank holds " << Capacity << " lbs. " << type << endl;
386 cout << " currently at " << PctFull << "% of maximum capacity" << endl;
387 cout << " Tank location (X, Y, Z): " << vXYZ(eX) << ", " << vXYZ(eY) << ", " << vXYZ(eZ) << endl;
388 cout << " Effective radius: " << Radius << " inches" << endl;
389 cout << " Initial temperature: " << Temperature << " Fahrenheit" << endl;
390 cout << " Priority: " << Priority << endl;
393 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
394 if (from == 0) cout << "Instantiated: FGTank" << endl;
395 if (from == 1) cout << "Destroyed: FGTank" << endl;
397 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
399 if (debug_lvl & 8 ) { // Runtime state variables
401 if (debug_lvl & 16) { // Sanity checking
403 if (debug_lvl & 64) {
404 if (from == 0) { // Constructor
405 cout << IdSrc << endl;
406 cout << IdHdr << endl;