]> git.mxchange.org Git - flightgear.git/blob - src/FDM/TankProperties.cxx
36f37be99ba1b7c3ffbdcc9ae5695de7e08e0a4c
[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 {
43   _tiedProperties.setRoot( rootNode );
44   _tiedProperties.Tie("level-kg", this, &TankProperties::getContent_kg, &TankProperties::setContent_kg );
45   _tiedProperties.Tie("density-kgpm3", this, &TankProperties::getDensity_kgpm3, &TankProperties::setDensity_kgpm3 );
46   _tiedProperties.Tie("capacity-m3", this, &TankProperties::getCapacity_m3, &TankProperties::setCapacity_m3 );
47   _tiedProperties.Tie("level-m3", this, &TankProperties::getContent_m3, &TankProperties::setContent_m3 );
48   _tiedProperties.Tie("level-norm", this, &TankProperties::getContent_norm, &TankProperties::setContent_norm );
49
50   _tiedProperties.Tie("density-ppg", this, &TankProperties::getDensity_ppg, &TankProperties::setDensity_ppg );
51   _tiedProperties.Tie("level-lbs", this, &TankProperties::getContent_lbs, &TankProperties::setContent_lbs );
52   _tiedProperties.Tie("level-gal_us", this, &TankProperties::getContent_gal_us, &TankProperties::setContent_gal_us );
53   _tiedProperties.Tie("level-gal_imp", this, &TankProperties::getContent_gal_imp, &TankProperties::setContent_gal_imp );
54   _tiedProperties.Tie("capacity-gal_us", this, &TankProperties::getCapacity_gal_us, &TankProperties::setCapacity_gal_us );
55   _tiedProperties.Tie("capacity-gal_imp", this, &TankProperties::getCapacity_gal_imp, &TankProperties::setCapacity_gal_imp );
56 }
57
58 TankProperties::~TankProperties()
59 {
60 }
61
62 double TankProperties::getContent_kg() const
63 {
64   return _content_kg;
65 }
66
67 void TankProperties::setContent_kg( double value )
68 {
69   _content_kg = value;
70 }
71
72 double TankProperties::getDensity_kgpm3() const
73 {
74   return _density_kgpm3;
75 }
76
77 void TankProperties::setDensity_kgpm3( double value )
78 {
79   _density_kgpm3 = value;
80 }
81
82 double TankProperties::getDensity_ppg() const
83 {
84   return _density_kgpm3 * LBS_PER_KG / USGAL_PER_M3;
85 }
86
87 void TankProperties::setDensity_ppg( double value )
88 {
89   _density_kgpm3 = value * KG_PER_LBS / M3_PER_USGAL;
90 }
91
92 double TankProperties::getContent_lbs() const
93 {
94   return _content_kg * LBS_PER_KG;
95 }
96
97 void TankProperties::setContent_lbs( double value )
98 {
99   _content_kg = value * KG_PER_LBS;
100 }
101
102 double TankProperties::getContent_m3() const
103 {
104   return _density_kgpm3 > SGLimitsd::min() ? _content_kg / _density_kgpm3 : 0.0;
105 }
106
107 void TankProperties::setContent_m3( double value )
108 {
109   // ugly hack to allow setting of a volumetric content without having the density
110   _content_kg = value * (_density_kgpm3>0.0?_density_kgpm3:755.0);
111 }
112
113 double TankProperties::getContent_gal_us() const
114 {
115   return getContent_m3() * USGAL_PER_M3;
116 }
117
118 void TankProperties::setContent_gal_us( double value )
119 {
120   setContent_m3( value * M3_PER_USGAL );
121 }
122
123 double TankProperties::getContent_gal_imp() const
124 {
125   return getContent_m3() * IMPGAL_PER_M3;
126 }
127
128 void TankProperties::setContent_gal_imp( double value )
129 {
130   setContent_m3( value * M3_PER_IMPGAL );
131 }
132
133 double TankProperties::getCapacity_m3() const
134 {
135   return _capacity_m3;
136 }
137
138 void TankProperties::setCapacity_m3( double value )
139 {
140   _capacity_m3 = value;
141 }
142
143 double TankProperties::getCapacity_gal_us() const
144 {
145   return _capacity_m3 * USGAL_PER_M3;
146 }
147
148 void TankProperties::setCapacity_gal_us( double value )
149 {
150   _capacity_m3 = value * M3_PER_USGAL;
151 }
152
153 double TankProperties::getCapacity_gal_imp() const
154 {
155   return _capacity_m3 * IMPGAL_PER_M3;
156 }
157
158 void TankProperties::setCapacity_gal_imp( double value )
159 {
160   _capacity_m3 = value * M3_PER_IMPGAL;
161 }
162
163 double TankProperties::getContent_norm() const
164 {
165   return  _capacity_m3 > SGLimitsd::min() ? getContent_m3() / _capacity_m3 : 0.0;
166 }
167
168 void TankProperties::setContent_norm( double value )
169 {
170   setContent_m3(_capacity_m3 * value);
171 }
172
173 TankPropertiesList::TankPropertiesList( SGPropertyNode_ptr rootNode )
174 {
175   // we don't have a global rule how many tanks we support, so I assume eight.
176   // Because hard coded values suck, make it settable by a property.
177   // If tanks were configured, use that number
178   int n = rootNode->nChildren();
179   if( n == 0 ) n = rootNode->getIntValue( "numtanks", 8 );
180   for( int i = 0; i < n; i++ ) {
181     push_back( new TankProperties( rootNode->getChild( "tank", i, true ) ) );
182   }
183
184   _tiedProperties.setRoot( rootNode );
185   _tiedProperties.Tie("total-fuel-kg", this, &TankPropertiesList::getTotalContent_kg );
186   _tiedProperties.Tie("total-fuel-lbs", this, &TankPropertiesList::getTotalContent_lbs );
187   _tiedProperties.Tie("total-fuel-gal_us", this, &TankPropertiesList::getTotalContent_gal_us );
188   _tiedProperties.Tie("total-fuel-gals", this, &TankPropertiesList::getTotalContent_gal_us );
189   _tiedProperties.Tie("total-fuel-gal_imp", this, &TankPropertiesList::getTotalContent_gal_imp );
190   _tiedProperties.Tie("total-fuel-norm", this, &TankPropertiesList::getTotalContent_norm );
191 }
192
193 double TankPropertiesList::getTotalContent_lbs() const
194 {
195   double value = 0.0;
196   for( const_iterator it = begin(); it != end(); ++it )
197     value += (*it)->getContent_lbs();
198   return value;
199 }
200
201 double TankPropertiesList::getTotalContent_kg() const
202 {
203   double value = 0.0;
204   for( const_iterator it = begin(); it != end(); ++it )
205     value += (*it)->getContent_kg();
206   return value;
207 }
208
209 double TankPropertiesList::getTotalContent_gal_us() const
210 {
211   double value = 0.0;
212   for( const_iterator it = begin(); it != end(); ++it )
213     value += (*it)->getContent_gal_us();
214   return value;
215 }
216
217 double TankPropertiesList::getTotalContent_gal_imp() const
218 {
219   double value = 0.0;
220   for( const_iterator it = begin(); it != end(); ++it )
221     value += (*it)->getContent_gal_imp();
222   return value;
223 }
224
225 double TankPropertiesList::getTotalContent_m3() const
226 {
227   double value = 0.0;
228   for( const_iterator it = begin(); it != end(); ++it )
229     value += (*it)->getContent_m3();
230   return value;
231 }
232
233 double TankPropertiesList::getTotalContent_norm() const
234 {
235   double content = 0.0;
236   double capacity = 0.0;
237   for( const_iterator it = begin(); it != end(); ++it ) {
238     content += (*it)->getContent_m3();
239     capacity += (*it)->getCapacity_m3();
240   }
241   return capacity > SGLimitsd::min() ? content / capacity : 0.0;
242 }
243
244