]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/propulsion/FGTank.cpp
Sync. w. JSB CVS as of 15/01/2007
[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 <FGFDMExec.h>
40 #include <models/FGAuxiliary.h>
41 #include "FGTank.h"
42
43 #if !defined ( sgi ) || defined( __GNUC__ ) && (_COMPILER_VERSION < 740)
44 using std::cerr;
45 using std::endl;
46 using std::cout;
47 #endif
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)
59 {
60   string token;
61   Element* element;
62   Area = 1.0;
63   Temperature = -9999.0;
64   Auxiliary = exec->GetAuxiliary();
65   Radius = Capacity = Contents = 0.0;
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   if (el->FindElement("radius"))
77     Radius = el->FindElementValueAsNumberConvertTo("radius", "IN");
78   if (el->FindElement("capacity"))
79     Capacity = el->FindElementValueAsNumberConvertTo("capacity", "LBS");
80   if (el->FindElement("contents"))
81     Contents = el->FindElementValueAsNumberConvertTo("contents", "LBS");
82   if (el->FindElement("temperature"))
83     Temperature = el->FindElementValueAsNumber("temperature");
84
85   Selected = true;
86
87   if (Capacity != 0) {
88     PctFull = 100.0*Contents/Capacity;            // percent full; 0 to 100.0
89   } else {
90     Contents = 0;
91     PctFull  = 0;
92   }
93
94   if (Temperature != -9999.0)  Temperature = FahrenheitToCelsius(Temperature);
95   Area = 40.0 * pow(Capacity/1975, 0.666666667);
96
97   Debug(0);
98 }
99
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101
102 FGTank::~FGTank()
103 {
104   Debug(1);
105 }
106
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109 double FGTank::Drain(double used)
110 {
111   double shortage = Contents - used;
112
113   if (shortage >= 0) {
114     Contents -= used;
115     PctFull = 100.0*Contents/Capacity;
116   } else {
117     Contents = 0.0;
118     PctFull = 0.0;
119     Selected = false;
120   }
121   return shortage;
122 }
123
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126 double FGTank::Fill(double amount)
127 {
128   double overage = 0.0;
129
130   Contents += amount;
131
132   if (Contents > Capacity) {
133     overage = Contents - Capacity;
134     Contents = Capacity;
135     PctFull = 100.0;
136   } else {
137     PctFull = Contents/Capacity*100.0;
138   }
139   return overage;
140 }
141
142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143
144 void FGTank::SetContents(double amount)
145 {
146   Contents = amount;
147   if (Contents > Capacity) {
148     Contents = Capacity;
149     PctFull = 100.0;
150   } else {
151     PctFull = Contents/Capacity*100.0;
152   }
153 }
154
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156
157 double FGTank::Calculate(double dt)
158 {
159   if (Temperature == -9999.0) return 0.0;
160   double HeatCapacity = 900.0;        // Joules/lbm/C
161   double TempFlowFactor = 1.115;      // Watts/sqft/C
162   double TAT = Auxiliary->GetTAT_C();
163   double Tdiff = TAT - Temperature;
164   double dT = 0.0;                    // Temp change due to one surface
165   if (fabs(Tdiff) > 0.1) {
166     dT = (TempFlowFactor * Area * Tdiff * dt) / (Contents * HeatCapacity);
167   }
168   return Temperature += (dT + dT);    // For now, assume upper/lower the same
169 }
170
171 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 //    The bitmasked value choices are as follows:
173 //    unset: In this case (the default) JSBSim would only print
174 //       out the normally expected messages, essentially echoing
175 //       the config files as they are read. If the environment
176 //       variable is not set, debug_lvl is set to 1 internally
177 //    0: This requests JSBSim not to output any messages
178 //       whatsoever.
179 //    1: This value explicity requests the normal JSBSim
180 //       startup messages
181 //    2: This value asks for a message to be printed out when
182 //       a class is instantiated
183 //    4: When this value is set, a message is displayed when a
184 //       FGModel object executes its Run() method
185 //    8: When this value is set, various runtime state variables
186 //       are printed out periodically
187 //    16: When set various parameters are sanity checked and
188 //       a message is printed out when they go out of bounds
189
190 void FGTank::Debug(int from)
191 {
192   if (debug_lvl <= 0) return;
193
194   if (debug_lvl & 1) { // Standard console startup message output
195     if (from == 0) { // Constructor
196       cout << "      " << type << " tank holds " << Capacity << " lbs. " << type << endl;
197       cout << "      currently at " << PctFull << "% of maximum capacity" << endl;
198       cout << "      Tank location (X, Y, Z): " << vXYZ(eX) << ", " << vXYZ(eY) << ", " << vXYZ(eZ) << endl;
199       cout << "      Effective radius: " << Radius << " inches" << endl;
200       cout << "      Initial temperature: " << Temperature << " Fahrenheit" << endl;
201     }
202   }
203   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
204     if (from == 0) cout << "Instantiated: FGTank" << endl;
205     if (from == 1) cout << "Destroyed:    FGTank" << endl;
206   }
207   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
208   }
209   if (debug_lvl & 8 ) { // Runtime state variables
210   }
211   if (debug_lvl & 16) { // Sanity checking
212   }
213   if (debug_lvl & 64) {
214     if (from == 0) { // Constructor
215       cout << IdSrc << endl;
216       cout << IdHdr << endl;
217     }
218   }
219 }
220 }