]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGBuoyantForces.cpp
sync with JSBSim CVS
[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 - 2010  Anders Gidenstam        -------------
9  ------------- Copyright (C) 2008  Jon S. Berndt (jon@jsbsim.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"
42 #include <iostream>
43
44 using namespace std;
45
46 namespace JSBSim {
47
48 static const char *IdSrc = "$Id: FGBuoyantForces.cpp,v 1.17 2011/05/20 03:18:36 jberndt Exp $";
49 static const char *IdHdr = ID_BUOYANTFORCES;
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55 FGBuoyantForces::FGBuoyantForces(FGFDMExec* FDMExec) : FGModel(FDMExec)
56 {
57   Name = "FGBuoyantForces";
58
59   NoneDefined = true;
60
61   vTotalForces.InitMatrix();
62   vTotalMoments.InitMatrix();
63
64   gasCellJ.InitMatrix();
65
66   Debug(0);
67 }
68
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70
71 FGBuoyantForces::~FGBuoyantForces()
72 {
73   for (unsigned int i=0; i<Cells.size(); i++) delete Cells[i];
74   Cells.clear();
75
76   Debug(1);
77 }
78
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
81 bool FGBuoyantForces::InitModel(void)
82 {
83   return true;
84 }
85
86 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87
88 bool FGBuoyantForces::Run(bool Holding)
89 {
90   if (FGModel::Run(Holding)) return true;
91   if (Holding) return false; // if paused don't execute
92   if (NoneDefined) return true;
93
94   RunPreFunctions();
95
96   vTotalForces.InitMatrix();
97   vTotalMoments.InitMatrix();
98
99   for (unsigned int i=0; i<Cells.size(); i++) {
100     Cells[i]->Calculate(FDMExec->GetDeltaT());
101     vTotalForces  += Cells[i]->GetBodyForces();
102     vTotalMoments += Cells[i]->GetMoments();
103   }
104
105   RunPostFunctions();
106
107   return false;
108 }
109
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111
112 bool FGBuoyantForces::Load(Element *element)
113 {
114   string fname="", file="";
115   Element *gas_cell_element;
116
117   Debug(2);
118
119   string separator = "/";
120
121   fname = element->GetAttributeValue("file");
122   if (!fname.empty()) {
123     file = FDMExec->GetFullAircraftPath() + separator + fname;
124     document = LoadXMLDocument(file);
125   } else {
126     document = element;
127   }
128
129   FGModel::Load(element); // Perform base class Load
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   PostLoad(element, PropertyManager);
139
140   if (!NoneDefined) {
141     bind();
142   }
143
144   return true;
145 }
146
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148
149 double FGBuoyantForces::GetGasMass(void)
150 {
151   double Gw = 0.0;
152
153   for (unsigned int i = 0; i < Cells.size(); i++) {
154     Gw += Cells[i]->GetMass();
155   }
156
157   return Gw;
158 }
159
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162 const FGColumnVector3& FGBuoyantForces::GetGasMassMoment(void)
163 {
164   vXYZgasCell_arm.InitMatrix();
165   for (unsigned int i = 0; i < Cells.size(); i++) {
166     vXYZgasCell_arm += Cells[i]->GetMassMoment();
167   }
168   return vXYZgasCell_arm;
169 }
170
171 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172
173 const FGMatrix33& FGBuoyantForces::GetGasMassInertia(void)
174 {
175   const unsigned int size = Cells.size();
176   
177   if (size == 0) return gasCellJ;
178
179   gasCellJ = FGMatrix33();
180
181   for (unsigned int i=0; i < size; i++) {
182     FGColumnVector3 v = FDMExec->GetMassBalance()->StructuralToBody( Cells[i]->GetXYZ() );
183     // Body basis is in FT. 
184     const double mass = Cells[i]->GetMass();
185     
186     // FIXME: Verify that this is the correct way to change between the
187     //        coordinate frames.
188     gasCellJ += Cells[i]->GetInertia() + 
189       FGMatrix33( 0,                - mass*v(1)*v(2), - mass*v(1)*v(3),
190                   - mass*v(2)*v(1), 0,                - mass*v(2)*v(3),
191                   - mass*v(3)*v(1), - mass*v(3)*v(2), 0 );
192   }
193   
194   return gasCellJ;
195 }
196
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198
199 string FGBuoyantForces::GetBuoyancyStrings(string delimeter)
200 {
201   string CoeffStrings = "";
202 /*
203   bool firstime = true;
204   for (sd = 0; sd < variables.size(); sd++) {
205     if (firstime) {
206       firstime = false;
207     } else {
208       CoeffStrings += delimeter;
209     }
210     CoeffStrings += variables[sd]->GetName();
211   }
212
213   for (axis = 0; axis < 6; axis++) {
214     for (sd = 0; sd < AeroFunctions[axis].size(); sd++) {
215       if (firstime) {
216         firstime = false;
217       } else {
218         CoeffStrings += delimeter;
219       }
220       CoeffStrings += AeroFunctions[axis][sd]->GetName();
221     }
222   }
223 */
224   return CoeffStrings;
225 }
226
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228
229 string FGBuoyantForces::GetBuoyancyValues(string delimeter)
230 {
231   string SDValues = "";
232 /*
233   bool firstime = true;
234   for (sd = 0; sd < variables.size(); sd++) {
235     if (firstime) {
236       firstime = false;
237     } else {
238       SDValues += delimeter;
239     }
240     SDValues += variables[sd]->GetValueAsString();
241   }
242
243   for (unsigned int axis = 0; axis < 6; axis++) {
244     for (unsigned int sd = 0; sd < AeroFunctions[axis].size(); sd++) {
245       if (firstime) {
246         firstime = false;
247       } else {
248         SDValues += delimeter;
249       }
250       SDValues += AeroFunctions[axis][sd]->GetValueAsString();
251     }
252   }
253 */
254   return SDValues;
255 }
256
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
259 void FGBuoyantForces::bind(void)
260 {
261   typedef double (FGBuoyantForces::*PGF)(int) const;
262   typedef void   (FGBuoyantForces::*PSF)(int, double);
263   PropertyManager->Tie("moments/l-buoyancy-lbsft", this, eL,
264                        (PGF)&FGBuoyantForces::GetMoments, (PSF)0, false);
265   PropertyManager->Tie("moments/m-buoyancy-lbsft", this, eM,
266                        (PGF)&FGBuoyantForces::GetMoments, (PSF)0, false);
267   PropertyManager->Tie("moments/n-buoyancy-lbsft", this, eN,
268                        (PGF)&FGBuoyantForces::GetMoments, (PSF)0, false);
269   PropertyManager->Tie("forces/fbx-buoyancy-lbs", this, eX,
270                        (PGF)&FGBuoyantForces::GetForces, (PSF)0, false);
271   PropertyManager->Tie("forces/fby-buoyancy-lbs", this, eY,
272                        (PGF)&FGBuoyantForces::GetForces, (PSF)0, false);
273   PropertyManager->Tie("forces/fbz-buoyancy-lbs", this, eZ,
274                        (PGF)&FGBuoyantForces::GetForces, (PSF)0, false);
275 }
276
277 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 //    The bitmasked value choices are as follows:
279 //    unset: In this case (the default) JSBSim would only print
280 //       out the normally expected messages, essentially echoing
281 //       the config files as they are read. If the environment
282 //       variable is not set, debug_lvl is set to 1 internally
283 //    0: This requests JSBSim not to output any messages
284 //       whatsoever.
285 //    1: This value explicity requests the normal JSBSim
286 //       startup messages
287 //    2: This value asks for a message to be printed out when
288 //       a class is instantiated
289 //    4: When this value is set, a message is displayed when a
290 //       FGModel object executes its Run() method
291 //    8: When this value is set, various runtime state variables
292 //       are printed out periodically
293 //    16: When set various parameters are sanity checked and
294 //       a message is printed out when they go out of bounds
295
296 void FGBuoyantForces::Debug(int from)
297 {
298   if (debug_lvl <= 0) return;
299
300   if (debug_lvl & 1) { // Standard console startup message output
301     if (from == 2) { // Loader
302       cout << endl << "  Buoyant Forces: " << endl;
303     }
304   }
305   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
306     if (from == 0) cout << "Instantiated: FGBuoyantForces" << endl;
307     if (from == 1) cout << "Destroyed:    FGBuoyantForces" << endl;
308   }
309   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
310   }
311   if (debug_lvl & 8 ) { // Runtime state variables
312   }
313   if (debug_lvl & 16) { // Sanity checking
314   }
315   if (debug_lvl & 64) {
316     if (from == 0) { // Constructor
317       cout << IdSrc << endl;
318       cout << IdHdr << endl;
319     }
320   }
321 }
322
323 } // namespace JSBSim