]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGBuoyantForces.cpp
latest changes for JSBSim (1.0 prerelease)
[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 - 2009  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   Debug(1);
76 }
77
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80 bool FGBuoyantForces::InitModel(void)
81 {
82   if (!FGModel::InitModel()) return false;
83
84   return true;
85 }
86
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
89 bool FGBuoyantForces::Run(void)
90 {
91   if (FGModel::Run()) return true;
92   if (FDMExec->Holding()) return false; // if paused don't execute
93   if (NoneDefined) return true;
94
95   vTotalForces.InitMatrix();
96   vTotalMoments.InitMatrix();
97
98   for (unsigned int i=0; i<Cells.size(); i++) {
99     Cells[i]->Calculate(FDMExec->GetDeltaT());
100     vTotalForces  += Cells[i]->GetBodyForces();
101     vTotalMoments += Cells[i]->GetMoments();
102   }
103
104   return false;
105 }
106
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
109 bool FGBuoyantForces::Load(Element *element)
110 {
111   string fname="", file="";
112   Element *gas_cell_element;
113
114   Debug(2);
115
116   string separator = "/";
117
118   fname = element->GetAttributeValue("file");
119   if (!fname.empty()) {
120     file = FDMExec->GetFullAircraftPath() + separator + fname;
121     document = LoadXMLDocument(file);
122   } else {
123     document = element;
124   }
125
126   FGModel::Load(element); // Perform base class Load
127
128   gas_cell_element = document->FindElement("gas_cell");
129   while (gas_cell_element) {
130     NoneDefined = false;
131     Cells.push_back(new FGGasCell(FDMExec, gas_cell_element, Cells.size()));
132     gas_cell_element = document->FindNextElement("gas_cell");
133   }
134   
135   return true;
136 }
137
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139
140 double FGBuoyantForces::GetGasMass(void)
141 {
142   double Gw = 0.0;
143
144   for (unsigned int i = 0; i < Cells.size(); i++) {
145     Gw += Cells[i]->GetMass();
146   }
147
148   return Gw;
149 }
150
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152
153 const FGColumnVector3& FGBuoyantForces::GetGasMassMoment(void)
154 {
155   vXYZgasCell_arm.InitMatrix();
156   for (unsigned int i = 0; i < Cells.size(); i++) {
157     vXYZgasCell_arm += Cells[i]->GetMassMoment();
158   }
159   return vXYZgasCell_arm;
160 }
161
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163
164 const FGMatrix33& FGBuoyantForces::GetGasMassInertia(void)
165 {
166   const unsigned int size = Cells.size();
167   
168   if (size == 0) return gasCellJ;
169
170   gasCellJ = FGMatrix33();
171
172   for (unsigned int i=0; i < size; i++) {
173     FGColumnVector3 v = MassBalance->StructuralToBody( Cells[i]->GetXYZ() );
174     // Body basis is in FT. 
175     const double mass = Cells[i]->GetMass();
176     
177     // FIXME: Verify that this is the correct way to change between the
178     //        coordinate frames.
179     gasCellJ += Cells[i]->GetInertia() + 
180       FGMatrix33( 0,                - mass*v(1)*v(2), - mass*v(1)*v(3),
181                   - mass*v(2)*v(1), 0,                - mass*v(2)*v(3),
182                   - mass*v(3)*v(1), - mass*v(3)*v(2), 0 );
183   }
184   
185   return gasCellJ;
186 }
187
188 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189
190 string FGBuoyantForces::GetBuoyancyStrings(string delimeter)
191 {
192   string CoeffStrings = "";
193 /*
194   bool firstime = true;
195   for (sd = 0; sd < variables.size(); sd++) {
196     if (firstime) {
197       firstime = false;
198     } else {
199       CoeffStrings += delimeter;
200     }
201     CoeffStrings += variables[sd]->GetName();
202   }
203
204   for (axis = 0; axis < 6; axis++) {
205     for (sd = 0; sd < Coeff[axis].size(); sd++) {
206       if (firstime) {
207         firstime = false;
208       } else {
209         CoeffStrings += delimeter;
210       }
211       CoeffStrings += Coeff[axis][sd]->GetName();
212     }
213   }
214 */
215   return CoeffStrings;
216 }
217
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219
220 string FGBuoyantForces::GetBuoyancyValues(string delimeter)
221 {
222   string SDValues = "";
223 /*
224   bool firstime = true;
225   for (sd = 0; sd < variables.size(); sd++) {
226     if (firstime) {
227       firstime = false;
228     } else {
229       SDValues += delimeter;
230     }
231     SDValues += variables[sd]->GetValueAsString();
232   }
233
234   for (unsigned int axis = 0; axis < 6; axis++) {
235     for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
236       if (firstime) {
237         firstime = false;
238       } else {
239         SDValues += delimeter;
240       }
241       SDValues += Coeff[axis][sd]->GetValueAsString();
242     }
243   }
244 */
245   return SDValues;
246 }
247
248 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249
250 void FGBuoyantForces::bind(void)
251 {
252 }
253
254 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 //    The bitmasked value choices are as follows:
256 //    unset: In this case (the default) JSBSim would only print
257 //       out the normally expected messages, essentially echoing
258 //       the config files as they are read. If the environment
259 //       variable is not set, debug_lvl is set to 1 internally
260 //    0: This requests JSBSim not to output any messages
261 //       whatsoever.
262 //    1: This value explicity requests the normal JSBSim
263 //       startup messages
264 //    2: This value asks for a message to be printed out when
265 //       a class is instantiated
266 //    4: When this value is set, a message is displayed when a
267 //       FGModel object executes its Run() method
268 //    8: When this value is set, various runtime state variables
269 //       are printed out periodically
270 //    16: When set various parameters are sanity checked and
271 //       a message is printed out when they go out of bounds
272
273 void FGBuoyantForces::Debug(int from)
274 {
275   if (debug_lvl <= 0) return;
276
277   if (debug_lvl & 1) { // Standard console startup message output
278     if (from == 2) { // Loader
279       cout << endl << "  Buoyant Forces: " << endl;
280     }
281   }
282   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
283     if (from == 0) cout << "Instantiated: FGBuoyantForces" << endl;
284     if (from == 1) cout << "Destroyed:    FGBuoyantForces" << endl;
285   }
286   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
287   }
288   if (debug_lvl & 8 ) { // Runtime state variables
289   }
290   if (debug_lvl & 16) { // Sanity checking
291   }
292   if (debug_lvl & 64) {
293     if (from == 0) { // Constructor
294       cout << IdSrc << endl;
295       cout << IdHdr << endl;
296     }
297   }
298 }
299
300 } // namespace JSBSim