]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGBuoyantForces.cpp
Update to the latest version of JSBSim which supports Lighter Than Air craft
[flightgear.git] / src / FDM / JSBSim / models / FGBuoyantForces.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGBuoyantForces.cpp
4  Authors:      Anders Gidenstam, Jon S. Berndt
5  Date started: 01/21/08
6  Purpose:      Encapsulates the buoyant forces
7
8  ------------- Copyright (C) 2008  Anders Gidenstam               -------------
9  ------------- Copyright (C) 2008  Jon S. Berndt (jsb@hal-pc.org) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
19  details.
20
21  You should have received a copy of the GNU Lesser General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 01/21/08   JSB   Created
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGBuoyantForces.h"
40 #include "FGMassBalance.h"
41 #include <input_output/FGPropertyManager.h>  // Need?
42
43 namespace JSBSim {
44
45 static const char *IdSrc = "$Id$";
46 static const char *IdHdr = ID_BUOYANTFORCES;
47
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 CLASS IMPLEMENTATION
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
52 FGBuoyantForces::FGBuoyantForces(FGFDMExec* FDMExec) : FGModel(FDMExec)
53 {
54   Name = "FGBuoyantForces";
55
56   NoneDefined = true;
57
58   vTotalForces.InitMatrix();
59   vTotalMoments.InitMatrix();
60
61   gasCellJ.InitMatrix();
62
63   bind();
64
65   Debug(0);
66 }
67
68 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69
70 FGBuoyantForces::~FGBuoyantForces()
71 {
72   for (unsigned int i=0; i<Cells.size(); i++) delete Cells[i];
73   Cells.clear();
74
75   unbind();
76
77   Debug(1);
78 }
79
80 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81
82 bool FGBuoyantForces::InitModel(void)
83 {
84   if (!FGModel::InitModel()) return false;
85
86   return true;
87 }
88
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
91 bool FGBuoyantForces::Run(void)
92 {
93   if (FGModel::Run()) return true;
94   if (FDMExec->Holding()) return false; // if paused don't execute
95   if (NoneDefined) return true;
96
97   vTotalForces.InitMatrix();
98   vTotalMoments.InitMatrix();
99
100   for (unsigned int i=0; i<Cells.size(); i++) {
101     Cells[i]->Calculate(FDMExec->GetDeltaT());
102     vTotalForces  += Cells[i]->GetBodyForces();
103     vTotalMoments += Cells[i]->GetMoments();
104   }
105
106   return false;
107 }
108
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110
111 bool FGBuoyantForces::Load(Element *element)
112 {
113   string fname="", file="";
114   Element *gas_cell_element;
115
116   Debug(2);
117
118   string separator = "/";
119 #ifdef macintosh
120   separator = ";";
121 #endif
122
123   fname = element->GetAttributeValue("file");
124   if (!fname.empty()) {
125     file = FDMExec->GetFullAircraftPath() + separator + fname;
126     document = LoadXMLDocument(file);
127   } else {
128     document = element;
129   }
130
131   gas_cell_element = document->FindElement("gas_cell");
132   while (gas_cell_element) {
133     NoneDefined = false;
134     Cells.push_back(new FGGasCell(FDMExec, gas_cell_element, Cells.size()));
135     gas_cell_element = document->FindNextElement("gas_cell");
136   }
137   
138   return true;
139 }
140
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142
143 double FGBuoyantForces::GetGasMass(void)
144 {
145   double Gw = 0.0;
146
147   for (unsigned int i = 0; i < Cells.size(); i++) {
148     Gw += Cells[i]->GetMass();
149   }
150
151   return Gw;
152 }
153
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156 FGColumnVector3& FGBuoyantForces::GetGasMassMoment(void)
157 {
158   vXYZgasCell_arm.InitMatrix();
159   for (unsigned int i = 0; i < Cells.size(); i++) {
160     vXYZgasCell_arm += Cells[i]->GetMassMoment();
161   }
162   return vXYZgasCell_arm;
163 }
164
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166
167 FGMatrix33& FGBuoyantForces::GetGasMassInertia(void)
168 {
169   const unsigned int size = Cells.size();
170   
171   if (size == 0) return gasCellJ;
172
173   gasCellJ = FGMatrix33();
174
175   for (unsigned int i=0; i < size; i++) {
176     FGColumnVector3 v = MassBalance->StructuralToBody( Cells[i]->GetXYZ() );
177     // Body basis is in FT. 
178     const double mass = Cells[i]->GetMass();
179     
180     // FIXME: Verify that this is the correct way to change between the
181     //        coordinate frames.
182     gasCellJ += Cells[i]->GetInertia() + 
183       FGMatrix33( 0,                - mass*v(1)*v(2), - mass*v(1)*v(3),
184                   - mass*v(2)*v(1), 0,                - mass*v(2)*v(3),
185                   - mass*v(3)*v(1), - mass*v(3)*v(2), 0 );
186   }
187   
188   return gasCellJ;
189 }
190
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192
193 string FGBuoyantForces::GetBuoyancyStrings(string delimeter)
194 {
195   string CoeffStrings = "";
196   bool firstime = true;
197 /*
198   for (sd = 0; sd < variables.size(); sd++) {
199     if (firstime) {
200       firstime = false;
201     } else {
202       CoeffStrings += delimeter;
203     }
204     CoeffStrings += variables[sd]->GetName();
205   }
206
207   for (axis = 0; axis < 6; axis++) {
208     for (sd = 0; sd < Coeff[axis].size(); sd++) {
209       if (firstime) {
210         firstime = false;
211       } else {
212         CoeffStrings += delimeter;
213       }
214       CoeffStrings += Coeff[axis][sd]->GetName();
215     }
216   }
217 */
218   return CoeffStrings;
219 }
220
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222
223 string FGBuoyantForces::GetBuoyancyValues(string delimeter)
224 {
225   string SDValues = "";
226   bool firstime = true;
227 /*
228   for (sd = 0; sd < variables.size(); sd++) {
229     if (firstime) {
230       firstime = false;
231     } else {
232       SDValues += delimeter;
233     }
234     SDValues += variables[sd]->GetValueAsString();
235   }
236
237   for (unsigned int axis = 0; axis < 6; axis++) {
238     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
239       if (firstime) {
240         firstime = false;
241       } else {
242         SDValues += delimeter;
243       }
244       SDValues += Coeff[axis][sd]->GetValueAsString();
245     }
246   }
247 */
248   return SDValues;
249 }
250
251 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252
253 void FGBuoyantForces::bind(void)
254 {
255 }
256
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
259 void FGBuoyantForces::unbind(void)
260 {
261 }
262
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 //    The bitmasked value choices are as follows:
265 //    unset: In this case (the default) JSBSim would only print
266 //       out the normally expected messages, essentially echoing
267 //       the config files as they are read. If the environment
268 //       variable is not set, debug_lvl is set to 1 internally
269 //    0: This requests JSBSim not to output any messages
270 //       whatsoever.
271 //    1: This value explicity requests the normal JSBSim
272 //       startup messages
273 //    2: This value asks for a message to be printed out when
274 //       a class is instantiated
275 //    4: When this value is set, a message is displayed when a
276 //       FGModel object executes its Run() method
277 //    8: When this value is set, various runtime state variables
278 //       are printed out periodically
279 //    16: When set various parameters are sanity checked and
280 //       a message is printed out when they go out of bounds
281
282 void FGBuoyantForces::Debug(int from)
283 {
284   if (debug_lvl <= 0) return;
285
286   if (debug_lvl & 1) { // Standard console startup message output
287     if (from == 2) { // Loader
288       cout << endl << "  Buoyant Forces: " << endl;
289     }
290   }
291   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
292     if (from == 0) cout << "Instantiated: FGBuoyantForces" << endl;
293     if (from == 1) cout << "Destroyed:    FGBuoyantForces" << endl;
294   }
295   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
296   }
297   if (debug_lvl & 8 ) { // Runtime state variables
298   }
299   if (debug_lvl & 16) { // Sanity checking
300   }
301   if (debug_lvl & 64) {
302     if (from == 0) { // Constructor
303       cout << IdSrc << endl;
304       cout << IdHdr << endl;
305     }
306   }
307 }
308
309 } // namespace JSBSim