1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_TANK;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 FGTank::FGTank(FGFDMExec* exec, Element* el, int tank_number)
55 : TankNumber(tank_number)
59 Element* element_Grain;
61 Temperature = -9999.0;
62 Auxiliary = exec->GetAuxiliary();
63 Radius = Capacity = Contents = Standpipe = Length = InnerRadius = 0.0;
64 PropertyManager = exec->GetPropertyManager();
66 vXYZ_drain.InitMatrix();
68 type = el->GetAttributeValue("type");
69 if (type == "FUEL") Type = ttFUEL;
70 else if (type == "OXIDIZER") Type = ttOXIDIZER;
71 else Type = ttUNKNOWN;
73 element = el->FindElement("location");
74 if (element) vXYZ = element->FindElementTripletConvertTo("IN");
75 else cerr << "No location found for this tank." << endl;
77 vXYZ_drain = vXYZ; // Set initial drain location to initial tank CG
79 element = el->FindElement("drain_location");
81 vXYZ_drain = element->FindElementTripletConvertTo("IN");
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");
98 PctFull = 100.0*Contents/Capacity; // percent full; 0 to 100.0
104 // Check whether this is a solid propellant "tank". Initialize it if true.
106 grainType = gtUNKNOWN; // This is the default
108 element_Grain = el->FindElement("grain_config");
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;
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;
121 // Initialize solid propellant values for debug and runtime use.
125 if (Radius <= InnerRadius) {
126 cerr << "The bore diameter should be smaller than the total grain diameter!" << endl;
129 Volume = M_PI * Length * (Radius*Radius - InnerRadius*InnerRadius); // cubic inches
132 Volume = M_PI * Length * Radius * Radius; // cubic inches
135 cerr << "Unknown grain type found in this rocket engine definition." << endl;
138 Density = (Contents*lbtoslug)/Volume; // slugs/in^3
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 );
147 if (Temperature != -9999.0) InitialTemperature = Temperature = FahrenheitToCelsius(Temperature);
148 Area = 40.0 * pow(Capacity/1975, 0.666666667);
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 void FGTank::ResetToIC(void)
164 Temperature = InitialTemperature;
165 Standpipe = InitialStandpipe;
166 Contents = InitialContents;
167 PctFull = 100.0*Contents/Capacity;
171 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 const FGColumnVector3 FGTank::GetXYZ(void)
175 return vXYZ_drain + (Contents/Capacity)*(vXYZ - vXYZ_drain);
178 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 const double FGTank::GetXYZ(int idx)
182 return vXYZ_drain(idx) + (Contents/Capacity)*(vXYZ(idx)-vXYZ_drain(idx));
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 double FGTank::Drain(double used)
189 double remaining = Contents - used;
191 if (remaining >= 0) { // Reduce contents by amount used.
194 PctFull = 100.0*Contents/Capacity;
196 } else { // This tank must be empty.
203 if (grainType != gtUNKNOWN) CalculateInertias();
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 double FGTank::Fill(double amount)
212 double overage = 0.0;
216 if (Contents > Capacity) {
217 overage = Contents - Capacity;
221 PctFull = Contents/Capacity*100.0;
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 void FGTank::SetContents(double amount)
231 if (Contents > Capacity) {
235 PctFull = Contents/Capacity*100.0;
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 double FGTank::Calculate(double dt)
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);
252 return Temperature += (dTemp + dTemp); // For now, assume upper/lower the same
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
260 void FGTank::CalculateInertias(void)
262 double Mass = Contents*lbtoslug;
264 double Rad2 = Radius*Radius;
265 Volume = (Contents*lbtoslug)/Density; // in^3
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;
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);
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
292 // 1: This value explicity requests the normal JSBSim
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
303 void FGTank::Debug(int from)
305 if (debug_lvl <= 0) return;
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;
316 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
317 if (from == 0) cout << "Instantiated: FGTank" << endl;
318 if (from == 1) cout << "Destroyed: FGTank" << endl;
320 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
322 if (debug_lvl & 8 ) { // Runtime state variables
324 if (debug_lvl & 16) { // Sanity checking
326 if (debug_lvl & 64) {
327 if (from == 0) { // Constructor
328 cout << IdSrc << endl;
329 cout << IdHdr << endl;