1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGBuoyantForces.cpp
4 Authors: Anders Gidenstam, Jon S. Berndt
6 Purpose: Encapsulates the buoyant forces
8 ------------- Copyright (C) 2008 - 2009 Anders Gidenstam -------------
9 ------------- Copyright (C) 2008 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 #include "FGBuoyantForces.h"
40 #include "FGMassBalance.h"
41 #include "input_output/FGPropertyManager.h" // Need?
48 static const char *IdSrc = "$Id$";
49 static const char *IdHdr = ID_BUOYANTFORCES;
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 FGBuoyantForces::FGBuoyantForces(FGFDMExec* FDMExec) : FGModel(FDMExec)
57 Name = "FGBuoyantForces";
61 vTotalForces.InitMatrix();
62 vTotalMoments.InitMatrix();
64 gasCellJ.InitMatrix();
71 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 FGBuoyantForces::~FGBuoyantForces()
75 for (unsigned int i=0; i<Cells.size(); i++) delete Cells[i];
81 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 bool FGBuoyantForces::InitModel(void)
85 if (!FGModel::InitModel()) return false;
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 bool FGBuoyantForces::Run(void)
94 if (FGModel::Run()) return true;
95 if (FDMExec->Holding()) return false; // if paused don't execute
96 if (NoneDefined) return true;
100 vTotalForces.InitMatrix();
101 vTotalMoments.InitMatrix();
103 for (unsigned int i=0; i<Cells.size(); i++) {
104 Cells[i]->Calculate(FDMExec->GetDeltaT());
105 vTotalForces += Cells[i]->GetBodyForces();
106 vTotalMoments += Cells[i]->GetMoments();
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 bool FGBuoyantForces::Load(Element *element)
118 string fname="", file="";
119 Element *gas_cell_element;
123 string separator = "/";
125 fname = element->GetAttributeValue("file");
126 if (!fname.empty()) {
127 file = FDMExec->GetFullAircraftPath() + separator + fname;
128 document = LoadXMLDocument(file);
133 FGModel::Load(element); // Perform base class Load
135 gas_cell_element = document->FindElement("gas_cell");
136 while (gas_cell_element) {
138 Cells.push_back(new FGGasCell(FDMExec, gas_cell_element, Cells.size()));
139 gas_cell_element = document->FindNextElement("gas_cell");
142 FGModel::PostLoad(element);
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 double FGBuoyantForces::GetGasMass(void)
153 for (unsigned int i = 0; i < Cells.size(); i++) {
154 Gw += Cells[i]->GetMass();
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 const FGColumnVector3& FGBuoyantForces::GetGasMassMoment(void)
164 vXYZgasCell_arm.InitMatrix();
165 for (unsigned int i = 0; i < Cells.size(); i++) {
166 vXYZgasCell_arm += Cells[i]->GetMassMoment();
168 return vXYZgasCell_arm;
171 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 const FGMatrix33& FGBuoyantForces::GetGasMassInertia(void)
175 const unsigned int size = Cells.size();
177 if (size == 0) return gasCellJ;
179 gasCellJ = FGMatrix33();
181 for (unsigned int i=0; i < size; i++) {
182 FGColumnVector3 v = MassBalance->StructuralToBody( Cells[i]->GetXYZ() );
183 // Body basis is in FT.
184 const double mass = Cells[i]->GetMass();
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 );
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 string FGBuoyantForces::GetBuoyancyStrings(string delimeter)
201 string CoeffStrings = "";
203 bool firstime = true;
204 for (sd = 0; sd < variables.size(); sd++) {
208 CoeffStrings += delimeter;
210 CoeffStrings += variables[sd]->GetName();
213 for (axis = 0; axis < 6; axis++) {
214 for (sd = 0; sd < Coeff[axis].size(); sd++) {
218 CoeffStrings += delimeter;
220 CoeffStrings += Coeff[axis][sd]->GetName();
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 string FGBuoyantForces::GetBuoyancyValues(string delimeter)
231 string SDValues = "";
233 bool firstime = true;
234 for (sd = 0; sd < variables.size(); sd++) {
238 SDValues += delimeter;
240 SDValues += variables[sd]->GetValueAsString();
243 for (unsigned int axis = 0; axis < 6; axis++) {
244 for (unsigned int sd = 0; sd < Coeff[axis].size(); sd++) {
248 SDValues += delimeter;
250 SDValues += Coeff[axis][sd]->GetValueAsString();
257 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259 void FGBuoyantForces::bind(void)
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
271 // 1: This value explicity requests the normal JSBSim
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
282 void FGBuoyantForces::Debug(int from)
284 if (debug_lvl <= 0) return;
286 if (debug_lvl & 1) { // Standard console startup message output
287 if (from == 2) { // Loader
288 cout << endl << " Buoyant Forces: " << endl;
291 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
292 if (from == 0) cout << "Instantiated: FGBuoyantForces" << endl;
293 if (from == 1) cout << "Destroyed: FGBuoyantForces" << endl;
295 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
297 if (debug_lvl & 8 ) { // Runtime state variables
299 if (debug_lvl & 16) { // Sanity checking
301 if (debug_lvl & 64) {
302 if (from == 0) { // Constructor
303 cout << IdSrc << endl;
304 cout << IdHdr << endl;
309 } // namespace JSBSim