| 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 - 2011 Anders Gidenstam ------------- |
| 9 | ------------- Copyright (C) 2008 Jon S. Berndt ([email protected]) ------------- |
| 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 |
| 13 | Software Foundation; either version 2 of the License, or (at your option) any |
| 14 | later 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 |
| 22 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 23 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | Further information about the GNU Lesser General Public License can also be |
| 26 | found on 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 "FGFDMExec.h" |
| 40 | #include "FGBuoyantForces.h" |
| 41 | #include "input_output/FGXMLElement.h" |
| 42 | |
| 43 | using namespace std; |
| 44 | |
| 45 | namespace JSBSim { |
| 46 | |
| 47 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 48 | CLASS IMPLEMENTATION |
| 49 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 50 | |
| 51 | FGBuoyantForces::FGBuoyantForces(FGFDMExec* FDMExec) : FGModel(FDMExec) |
| 52 | { |
| 53 | Name = "FGBuoyantForces"; |
| 54 | |
| 55 | NoneDefined = true; |
| 56 | |
| 57 | vTotalForces.InitMatrix(); |
| 58 | vTotalMoments.InitMatrix(); |
| 59 | |
| 60 | gasCellJ.InitMatrix(); |
| 61 | |
| 62 | Debug(0); |
| 63 | } |
| 64 | |
| 65 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 66 | |
| 67 | FGBuoyantForces::~FGBuoyantForces() |
| 68 | { |
| 69 | for (unsigned int i=0; i<Cells.size(); i++) delete Cells[i]; |
| 70 | Cells.clear(); |
| 71 | |
| 72 | Debug(1); |
| 73 | } |
| 74 | |
| 75 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 76 | |
| 77 | bool FGBuoyantForces::InitModel(void) |
| 78 | { |
| 79 | if (!FGModel::InitModel()) return false; |
| 80 | |
| 81 | vTotalForces.InitMatrix(); |
| 82 | vTotalMoments.InitMatrix(); |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 88 | |
| 89 | bool FGBuoyantForces::Run(bool Holding) |
| 90 | { |
| 91 | if (FGModel::Run(Holding)) return true; |
| 92 | if (Holding) return false; // if paused don't execute |
| 93 | if (NoneDefined) return true; |
| 94 | |
| 95 | RunPreFunctions(); |
| 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 | RunPostFunctions(); |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 112 | |
| 113 | bool FGBuoyantForces::Load(Element *document) |
| 114 | { |
| 115 | Element *gas_cell_element; |
| 116 | |
| 117 | Debug(2); |
| 118 | |
| 119 | // Perform base class Pre-Load |
| 120 | if (!FGModel::Upload(document, true)) |
| 121 | return false; |
| 122 | |
| 123 | gas_cell_element = document->FindElement("gas_cell"); |
| 124 | while (gas_cell_element) { |
| 125 | NoneDefined = false; |
| 126 | Cells.push_back(new FGGasCell(FDMExec, gas_cell_element, Cells.size(), in)); |
| 127 | gas_cell_element = document->FindNextElement("gas_cell"); |
| 128 | } |
| 129 | |
| 130 | PostLoad(document, FDMExec); |
| 131 | |
| 132 | if (!NoneDefined) { |
| 133 | bind(); |
| 134 | } |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 140 | |
| 141 | double FGBuoyantForces::GetGasMass(void) const |
| 142 | { |
| 143 | double Gw = 0.0; |
| 144 | |
| 145 | for (unsigned int i = 0; i < Cells.size(); i++) { |
| 146 | Gw += Cells[i]->GetMass(); |
| 147 | } |
| 148 | |
| 149 | return Gw; |
| 150 | } |
| 151 | |
| 152 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 153 | |
| 154 | const FGColumnVector3& FGBuoyantForces::GetGasMassMoment(void) |
| 155 | { |
| 156 | vXYZgasCell_arm.InitMatrix(); |
| 157 | for (unsigned int i = 0; i < Cells.size(); i++) { |
| 158 | vXYZgasCell_arm += Cells[i]->GetMassMoment(); |
| 159 | } |
| 160 | return vXYZgasCell_arm; |
| 161 | } |
| 162 | |
| 163 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 164 | |
| 165 | const FGMatrix33& FGBuoyantForces::GetGasMassInertia(void) |
| 166 | { |
| 167 | size_t size = Cells.size(); |
| 168 | |
| 169 | if (size == 0) return gasCellJ; |
| 170 | |
| 171 | gasCellJ.InitMatrix(); |
| 172 | |
| 173 | for (unsigned int i=0; i < size; i++) { |
| 174 | gasCellJ += Cells[i]->GetInertia(); |
| 175 | } |
| 176 | |
| 177 | return gasCellJ; |
| 178 | } |
| 179 | |
| 180 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 181 | |
| 182 | string FGBuoyantForces::GetBuoyancyStrings(const string& delimeter) |
| 183 | { |
| 184 | string CoeffStrings = ""; |
| 185 | /* |
| 186 | bool firstime = true; |
| 187 | for (sd = 0; sd < variables.size(); sd++) { |
| 188 | if (firstime) { |
| 189 | firstime = false; |
| 190 | } else { |
| 191 | CoeffStrings += delimeter; |
| 192 | } |
| 193 | CoeffStrings += variables[sd]->GetName(); |
| 194 | } |
| 195 | |
| 196 | for (axis = 0; axis < 6; axis++) { |
| 197 | for (sd = 0; sd < AeroFunctions[axis].size(); sd++) { |
| 198 | if (firstime) { |
| 199 | firstime = false; |
| 200 | } else { |
| 201 | CoeffStrings += delimeter; |
| 202 | } |
| 203 | CoeffStrings += AeroFunctions[axis][sd]->GetName(); |
| 204 | } |
| 205 | } |
| 206 | */ |
| 207 | return CoeffStrings; |
| 208 | } |
| 209 | |
| 210 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 211 | |
| 212 | string FGBuoyantForces::GetBuoyancyValues(const string& delimeter) |
| 213 | { |
| 214 | string SDValues = ""; |
| 215 | /* |
| 216 | bool firstime = true; |
| 217 | for (sd = 0; sd < variables.size(); sd++) { |
| 218 | if (firstime) { |
| 219 | firstime = false; |
| 220 | } else { |
| 221 | SDValues += delimeter; |
| 222 | } |
| 223 | SDValues += variables[sd]->GetValueAsString(); |
| 224 | } |
| 225 | |
| 226 | for (unsigned int axis = 0; axis < 6; axis++) { |
| 227 | for (unsigned int sd = 0; sd < AeroFunctions[axis].size(); sd++) { |
| 228 | if (firstime) { |
| 229 | firstime = false; |
| 230 | } else { |
| 231 | SDValues += delimeter; |
| 232 | } |
| 233 | SDValues += AeroFunctions[axis][sd]->GetValueAsString(); |
| 234 | } |
| 235 | } |
| 236 | */ |
| 237 | return SDValues; |
| 238 | } |
| 239 | |
| 240 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 241 | |
| 242 | void FGBuoyantForces::bind(void) |
| 243 | { |
| 244 | using PSF = void (FGBuoyantForces::*)(int, double); |
| 245 | PropertyManager->Tie("moments/l-buoyancy-lbsft", this, eL, |
| 246 | &FGBuoyantForces::GetMoments, (PSF)nullptr); |
| 247 | PropertyManager->Tie("moments/m-buoyancy-lbsft", this, eM, |
| 248 | &FGBuoyantForces::GetMoments, (PSF)nullptr); |
| 249 | PropertyManager->Tie("moments/n-buoyancy-lbsft", this, eN, |
| 250 | &FGBuoyantForces::GetMoments, (PSF)nullptr); |
| 251 | PropertyManager->Tie("forces/fbx-buoyancy-lbs", this, eX, |
| 252 | &FGBuoyantForces::GetForces, (PSF)nullptr); |
| 253 | PropertyManager->Tie("forces/fby-buoyancy-lbs", this, eY, |
| 254 | &FGBuoyantForces::GetForces, (PSF)nullptr); |
| 255 | PropertyManager->Tie("forces/fbz-buoyancy-lbs", this, eZ, |
| 256 | &FGBuoyantForces::GetForces, (PSF)nullptr); |
| 257 | } |
| 258 | |
| 259 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 260 | // The bitmasked value choices are as follows: |
| 261 | // unset: In this case (the default) JSBSim would only print |
| 262 | // out the normally expected messages, essentially echoing |
| 263 | // the config files as they are read. If the environment |
| 264 | // variable is not set, debug_lvl is set to 1 internally |
| 265 | // 0: This requests JSBSim not to output any messages |
| 266 | // whatsoever. |
| 267 | // 1: This value explicity requests the normal JSBSim |
| 268 | // startup messages |
| 269 | // 2: This value asks for a message to be printed out when |
| 270 | // a class is instantiated |
| 271 | // 4: When this value is set, a message is displayed when a |
| 272 | // FGModel object executes its Run() method |
| 273 | // 8: When this value is set, various runtime state variables |
| 274 | // are printed out periodically |
| 275 | // 16: When set various parameters are sanity checked and |
| 276 | // a message is printed out when they go out of bounds |
| 277 | |
| 278 | void FGBuoyantForces::Debug(int from) |
| 279 | { |
| 280 | if (debug_lvl <= 0) return; |
| 281 | |
| 282 | if (debug_lvl & 1) { // Standard console startup message output |
| 283 | if (from == 2) { // Loader |
| 284 | cout << endl << " Buoyant Forces: " << endl; |
| 285 | } |
| 286 | } |
| 287 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 288 | if (from == 0) cout << "Instantiated: FGBuoyantForces" << endl; |
| 289 | if (from == 1) cout << "Destroyed: FGBuoyantForces" << endl; |
| 290 | } |
| 291 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 292 | } |
| 293 | if (debug_lvl & 8 ) { // Runtime state variables |
| 294 | } |
| 295 | if (debug_lvl & 16) { // Sanity checking |
| 296 | } |
| 297 | if (debug_lvl & 64) { |
| 298 | if (from == 0) { // Constructor |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | } // namespace JSBSim |