]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGTank.cpp
Sync. with JSBSim CVS (header cleanups).
[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   Area = 1.0;
60   Temperature = -9999.0;
61   Auxiliary = exec->GetAuxiliary();
62   Radius = Capacity = Contents = Standpipe = 0.0;
63   PropertyManager = exec->GetPropertyManager();
64   vXYZ.InitMatrix();
65   vXYZ_drain.InitMatrix();
66
67   type = el->GetAttributeValue("type");
68   if      (type == "FUEL")     Type = ttFUEL;
69   else if (type == "OXIDIZER") Type = ttOXIDIZER;
70   else                         Type = ttUNKNOWN;
71
72   element = el->FindElement("location");
73   if (element)  vXYZ = element->FindElementTripletConvertTo("IN");
74   else          cerr << "No location found for this tank." << endl;
75
76   vXYZ_drain = vXYZ; // Set initial drain location to initial tank CG
77
78   element = el->FindElement("drain_location");
79   if (element)  {
80     vXYZ_drain = element->FindElementTripletConvertTo("IN");
81   }
82
83   if (el->FindElement("radius"))
84     Radius = el->FindElementValueAsNumberConvertTo("radius", "IN");
85   if (el->FindElement("capacity"))
86     Capacity = el->FindElementValueAsNumberConvertTo("capacity", "LBS");
87   if (el->FindElement("contents"))
88     InitialContents = Contents = el->FindElementValueAsNumberConvertTo("contents", "LBS");
89   if (el->FindElement("temperature"))
90     InitialTemperature = Temperature = el->FindElementValueAsNumber("temperature");
91   if (el->FindElement("standpipe"))
92     InitialStandpipe = Standpipe = el->FindElementValueAsNumberConvertTo("standpipe", "LBS");
93
94   Selected = true;
95
96   if (Capacity != 0) {
97     PctFull = 100.0*Contents/Capacity;            // percent full; 0 to 100.0
98   } else {
99     Contents = 0;
100     PctFull  = 0;
101   }
102
103   char property_name[80];
104   snprintf(property_name, 80, "propulsion/tank[%d]/contents-lbs", TankNumber);
105   PropertyManager->Tie( property_name, (FGTank*)this, &FGTank::GetContents,
106                                        &FGTank::SetContents );
107
108   if (Temperature != -9999.0)  InitialTemperature = Temperature = FahrenheitToCelsius(Temperature);
109   Area = 40.0 * pow(Capacity/1975, 0.666666667);
110
111   Debug(0);
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 FGTank::~FGTank()
117 {
118   Debug(1);
119 }
120
121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
123 void FGTank::ResetToIC(void)
124 {
125   Temperature = InitialTemperature;
126   Standpipe = InitialStandpipe;
127   Contents = InitialContents;
128   PctFull = 100.0*Contents/Capacity;
129   Selected = true;
130 }
131
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133
134 const FGColumnVector3 FGTank::GetXYZ(void)
135 {
136   return vXYZ_drain + (Contents/Capacity)*(vXYZ - vXYZ_drain);
137 }
138
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141 const double FGTank::GetXYZ(int idx)
142 {
143   return vXYZ_drain(idx) + (Contents/Capacity)*(vXYZ(idx)-vXYZ_drain(idx));
144 }
145
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147
148 double FGTank::Drain(double used)
149 {
150   double remaining = Contents - used;
151
152   if (remaining >= 0) { // Reduce contents by amount used.
153
154     Contents -= used;
155     PctFull = 100.0*Contents/Capacity;
156
157   } else { // This tank must be empty.
158
159     Contents = 0.0;
160     PctFull = 0.0;
161     Selected = false;
162   }
163   return remaining;
164 }
165
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167
168 double FGTank::Fill(double amount)
169 {
170   double overage = 0.0;
171
172   Contents += amount;
173
174   if (Contents > Capacity) {
175     overage = Contents - Capacity;
176     Contents = Capacity;
177     PctFull = 100.0;
178   } else {
179     PctFull = Contents/Capacity*100.0;
180   }
181   return overage;
182 }
183
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185
186 void FGTank::SetContents(double amount)
187 {
188   Contents = amount;
189   if (Contents > Capacity) {
190     Contents = Capacity;
191     PctFull = 100.0;
192   } else {
193     PctFull = Contents/Capacity*100.0;
194   }
195 }
196
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198
199 double FGTank::Calculate(double dt)
200 {
201   if (Temperature == -9999.0) return 0.0;
202   double HeatCapacity = 900.0;        // Joules/lbm/C
203   double TempFlowFactor = 1.115;      // Watts/sqft/C
204   double TAT = Auxiliary->GetTAT_C();
205   double Tdiff = TAT - Temperature;
206   double dTemp = 0.0;                 // Temp change due to one surface
207   if (fabs(Tdiff) > 0.1) {
208     dTemp = (TempFlowFactor * Area * Tdiff * dt) / (Contents * HeatCapacity);
209   }
210   return Temperature += (dTemp + dTemp);    // For now, assume upper/lower the same
211 }
212
213 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 //    The bitmasked value choices are as follows:
215 //    unset: In this case (the default) JSBSim would only print
216 //       out the normally expected messages, essentially echoing
217 //       the config files as they are read. If the environment
218 //       variable is not set, debug_lvl is set to 1 internally
219 //    0: This requests JSBSim not to output any messages
220 //       whatsoever.
221 //    1: This value explicity requests the normal JSBSim
222 //       startup messages
223 //    2: This value asks for a message to be printed out when
224 //       a class is instantiated
225 //    4: When this value is set, a message is displayed when a
226 //       FGModel object executes its Run() method
227 //    8: When this value is set, various runtime state variables
228 //       are printed out periodically
229 //    16: When set various parameters are sanity checked and
230 //       a message is printed out when they go out of bounds
231
232 void FGTank::Debug(int from)
233 {
234   if (debug_lvl <= 0) return;
235
236   if (debug_lvl & 1) { // Standard console startup message output
237     if (from == 0) { // Constructor
238       cout << "      " << type << " tank holds " << Capacity << " lbs. " << type << endl;
239       cout << "      currently at " << PctFull << "% of maximum capacity" << endl;
240       cout << "      Tank location (X, Y, Z): " << vXYZ(eX) << ", " << vXYZ(eY) << ", " << vXYZ(eZ) << endl;
241       cout << "      Effective radius: " << Radius << " inches" << endl;
242       cout << "      Initial temperature: " << Temperature << " Fahrenheit" << endl;
243     }
244   }
245   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
246     if (from == 0) cout << "Instantiated: FGTank" << endl;
247     if (from == 1) cout << "Destroyed:    FGTank" << endl;
248   }
249   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
250   }
251   if (debug_lvl & 8 ) { // Runtime state variables
252   }
253   if (debug_lvl & 16) { // Sanity checking
254   }
255   if (debug_lvl & 64) {
256     if (from == 0) { // Constructor
257       cout << IdSrc << endl;
258       cout << IdHdr << endl;
259     }
260   }
261 }
262 }