]> git.mxchange.org Git - flightgear.git/blob - src/FDM/TankProperties.cxx
cb4f82c9821fb18de1343320d77c2ab273ad52b4
[flightgear.git] / src / FDM / TankProperties.cxx
1 // TankProperties.cxx -- expose (fuel)-tank properties 
2 //
3 // Written by Torsten Dreyer, started January 2011.
4 //
5 // Copyright (C) 2011  Torsten Dreyer - Torsten (at) t3r _dot_ de
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include "TankProperties.hxx"
27
28 #include <simgear/math/SGMath.hxx>
29 #include <Main/fg_props.hxx>
30
31 static const double LBS_PER_KG = 2.20462262;
32 static const double KG_PER_LBS = 1.0/LBS_PER_KG;
33 static const double USGAL_PER_M3 = 1000.0/3.785411784;
34 static const double M3_PER_USGAL = 1.0/USGAL_PER_M3;
35 static const double IMPGAL_PER_M3 = 1000.0/4.54609;
36 static const double M3_PER_IMPGAL = 1.0/IMPGAL_PER_M3;
37
38 TankProperties::TankProperties(SGPropertyNode_ptr rootNode ) :
39   _content_kg(0.0),
40   _density_kgpm3(0.0),
41   _capacity_m3(0.0),
42   _unusable_m3(0.0)
43 {
44   _tiedProperties.setRoot( rootNode );
45   _tiedProperties.Tie("level-kg", this, &TankProperties::getContent_kg, &TankProperties::setContent_kg );
46   _tiedProperties.Tie("density-kgpm3", this, &TankProperties::getDensity_kgpm3, &TankProperties::setDensity_kgpm3 );
47   _tiedProperties.Tie("capacity-m3", this, &TankProperties::getCapacity_m3, &TankProperties::setCapacity_m3 );
48   _tiedProperties.Tie("unusable-m3", this, &TankProperties::getUnusable_m3, &TankProperties::setUnusable_m3 );
49   _tiedProperties.Tie("level-m3", this, &TankProperties::getContent_m3, &TankProperties::setContent_m3 );
50   _tiedProperties.Tie("level-norm", this, &TankProperties::getContent_norm, &TankProperties::setContent_norm );
51
52   _tiedProperties.Tie("density-ppg", this, &TankProperties::getDensity_ppg, &TankProperties::setDensity_ppg );
53   _tiedProperties.Tie("level-lbs", this, &TankProperties::getContent_lbs, &TankProperties::setContent_lbs );
54   _tiedProperties.Tie("level-gal_us", this, &TankProperties::getContent_gal_us, &TankProperties::setContent_gal_us );
55   _tiedProperties.Tie("level-gal_imp", this, &TankProperties::getContent_gal_imp, &TankProperties::setContent_gal_imp );
56
57   _tiedProperties.Tie("capacity-gal_us", this, &TankProperties::getCapacity_gal_us, &TankProperties::setCapacity_gal_us );
58   _tiedProperties.Tie("unusable-gal_us", this, &TankProperties::getUnusable_gal_us, &TankProperties::setUnusable_gal_us );
59
60   _tiedProperties.Tie("capacity-gal_imp", this, &TankProperties::getCapacity_gal_imp, &TankProperties::setCapacity_gal_imp );
61   _tiedProperties.Tie("unusable-gal_imp", this, &TankProperties::getUnusable_gal_imp, &TankProperties::setUnusable_gal_imp );
62
63   _tiedProperties.Tie("empty", this, &TankProperties::getEmpty );
64 }
65
66 TankProperties::~TankProperties()
67 {
68 }
69
70 double TankProperties::getContent_kg() const
71 {
72   return _content_kg;
73 }
74
75 void TankProperties::setContent_kg( double value )
76 {
77   _content_kg = value;
78 }
79
80 double TankProperties::getDensity_kgpm3() const
81 {
82   return _density_kgpm3;
83 }
84
85 void TankProperties::setDensity_kgpm3( double value )
86 {
87   _density_kgpm3 = value;
88 }
89
90 double TankProperties::getDensity_ppg() const
91 {
92   return _density_kgpm3 * LBS_PER_KG / USGAL_PER_M3;
93 }
94
95 void TankProperties::setDensity_ppg( double value )
96 {
97   _density_kgpm3 = value * KG_PER_LBS / M3_PER_USGAL;
98 }
99
100 double TankProperties::getContent_lbs() const
101 {
102   return _content_kg * LBS_PER_KG;
103 }
104
105 void TankProperties::setContent_lbs( double value )
106 {
107   _content_kg = value * KG_PER_LBS;
108 }
109
110 double TankProperties::getContent_m3() const
111 {
112   return _density_kgpm3 > SGLimitsd::min() ? _content_kg / _density_kgpm3 : 0.0;
113 }
114
115 void TankProperties::setContent_m3( double value )
116 {
117   // ugly hack to allow setting of a volumetric content without having the density
118   _content_kg = value * (_density_kgpm3>0.0?_density_kgpm3:755.0);
119 }
120
121 double TankProperties::getContent_gal_us() const
122 {
123   return getContent_m3() * USGAL_PER_M3;
124 }
125
126 void TankProperties::setContent_gal_us( double value )
127 {
128   setContent_m3( value * M3_PER_USGAL );
129 }
130
131 double TankProperties::getContent_gal_imp() const
132 {
133   return getContent_m3() * IMPGAL_PER_M3;
134 }
135
136 void TankProperties::setContent_gal_imp( double value )
137 {
138   setContent_m3( value * M3_PER_IMPGAL );
139 }
140
141 double TankProperties::getCapacity_m3() const
142 {
143   return _capacity_m3;
144 }
145
146 void TankProperties::setCapacity_m3( double value )
147 {
148   _capacity_m3 = value;
149 }
150
151 double TankProperties::getCapacity_gal_us() const
152 {
153   return _capacity_m3 * USGAL_PER_M3;
154 }
155
156 void TankProperties::setCapacity_gal_us( double value )
157 {
158   _capacity_m3 = value * M3_PER_USGAL;
159 }
160
161 double TankProperties::getCapacity_gal_imp() const
162 {
163   return _capacity_m3 * IMPGAL_PER_M3;
164 }
165
166 void TankProperties::setCapacity_gal_imp( double value )
167 {
168   _capacity_m3 = value * M3_PER_IMPGAL;
169 }
170
171 double TankProperties::getUnusable_m3() const
172 {
173   return _unusable_m3;
174 }
175
176 void TankProperties::setUnusable_m3( double value )
177 {
178   _unusable_m3 = value;
179 }
180
181 double TankProperties::getUnusable_gal_us() const
182 {
183   return _unusable_m3 * USGAL_PER_M3;
184 }
185
186 void TankProperties::setUnusable_gal_us( double value )
187 {
188   _unusable_m3 = value * M3_PER_USGAL;
189 }
190
191
192 double TankProperties::getUnusable_gal_imp() const
193 {
194   return _unusable_m3 * IMPGAL_PER_M3;
195 }
196
197 void TankProperties::setUnusable_gal_imp( double value )
198 {
199   _unusable_m3 = value * M3_PER_IMPGAL;
200 }
201
202 double TankProperties::getContent_norm() const
203 {
204   return  _capacity_m3 > SGLimitsd::min() ? getContent_m3() / _capacity_m3 : 0.0;
205 }
206
207 void TankProperties::setContent_norm( double value )
208 {
209   setContent_m3(_capacity_m3 * value);
210 }
211
212 bool TankProperties::getEmpty() const
213 {
214   return getContent_m3() <= _unusable_m3;
215 }
216
217 TankPropertiesList::TankPropertiesList( SGPropertyNode_ptr rootNode )
218 {
219   // we don't have a global rule how many tanks we support, so I assume eight.
220   // Because hard coded values suck, make it settable by a property.
221   // If tanks were configured, use that number
222   int n = rootNode->getChildren("tank").size();
223   if( n == 0 ) n = rootNode->getIntValue( "numtanks", 8 );
224   for( int i = 0; i < n; i++ ) {
225     push_back( new TankProperties( rootNode->getChild( "tank", i, true ) ) );
226   }
227
228   _tiedProperties.setRoot( rootNode );
229   _tiedProperties.Tie("total-fuel-kg", this, &TankPropertiesList::getTotalContent_kg );
230   _tiedProperties.Tie("total-fuel-lbs", this, &TankPropertiesList::getTotalContent_lbs );
231   _tiedProperties.Tie("total-fuel-gal_us", this, &TankPropertiesList::getTotalContent_gal_us );
232   _tiedProperties.Tie("total-fuel-gals", this, &TankPropertiesList::getTotalContent_gal_us );
233   _tiedProperties.Tie("total-fuel-gal_imp", this, &TankPropertiesList::getTotalContent_gal_imp );
234   _tiedProperties.Tie("total-fuel-norm", this, &TankPropertiesList::getTotalContent_norm );
235 }
236
237 double TankPropertiesList::getTotalContent_lbs() const
238 {
239   double value = 0.0;
240   for( const_iterator it = begin(); it != end(); ++it )
241     value += (*it)->getContent_lbs();
242   return value;
243 }
244
245 double TankPropertiesList::getTotalContent_kg() const
246 {
247   double value = 0.0;
248   for( const_iterator it = begin(); it != end(); ++it )
249     value += (*it)->getContent_kg();
250   return value;
251 }
252
253 double TankPropertiesList::getTotalContent_gal_us() const
254 {
255   double value = 0.0;
256   for( const_iterator it = begin(); it != end(); ++it )
257     value += (*it)->getContent_gal_us();
258   return value;
259 }
260
261 double TankPropertiesList::getTotalContent_gal_imp() const
262 {
263   double value = 0.0;
264   for( const_iterator it = begin(); it != end(); ++it )
265     value += (*it)->getContent_gal_imp();
266   return value;
267 }
268
269 double TankPropertiesList::getTotalContent_m3() const
270 {
271   double value = 0.0;
272   for( const_iterator it = begin(); it != end(); ++it )
273     value += (*it)->getContent_m3();
274   return value;
275 }
276
277 double TankPropertiesList::getTotalContent_norm() const
278 {
279   double content = 0.0;
280   double capacity = 0.0;
281   for( const_iterator it = begin(); it != end(); ++it ) {
282     content += (*it)->getContent_m3();
283     capacity += (*it)->getCapacity_m3();
284   }
285   return capacity > SGLimitsd::min() ? content / capacity : 0.0;
286 }
287
288