| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Module: FGPropulsion.cpp |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 08/20/00 |
| 6 | Purpose: Encapsulates the set of engines and tanks associated |
| 7 | with this aircraft |
| 8 | |
| 9 | ------------- Copyright (C) 2000 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 | The Propulsion class is the container for the entire propulsion system, which is |
| 31 | comprised of engines and tanks. Once the Propulsion class gets the config file, |
| 32 | it reads in information which is specific to a type of engine. Then: |
| 33 | |
| 34 | 1) The appropriate engine type instance is created |
| 35 | 2) At least one tank object is created, and is linked to an engine. |
| 36 | |
| 37 | At Run time each engines Calculate() method is called. |
| 38 | |
| 39 | HISTORY |
| 40 | -------------------------------------------------------------------------------- |
| 41 | 08/20/00 JSB Created |
| 42 | |
| 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 44 | INCLUDES |
| 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 46 | |
| 47 | #include <iomanip> |
| 48 | #include <array> |
| 49 | |
| 50 | #include "FGFDMExec.h" |
| 51 | #include "FGPropulsion.h" |
| 52 | #include "models/FGMassBalance.h" |
| 53 | #include "models/propulsion/FGRocket.h" |
| 54 | #include "models/propulsion/FGTurbine.h" |
| 55 | #include "models/propulsion/FGPiston.h" |
| 56 | #include "models/propulsion/FGElectric.h" |
| 57 | #include "models/propulsion/FGTurboProp.h" |
| 58 | #include "models/propulsion/FGTank.h" |
| 59 | #include "input_output/FGModelLoader.h" |
| 60 | |
| 61 | using namespace std; |
| 62 | |
| 63 | namespace JSBSim { |
| 64 | |
| 65 | extern short debug_lvl; |
| 66 | |
| 67 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 68 | CLASS IMPLEMENTATION |
| 69 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 70 | |
| 71 | FGPropulsion::FGPropulsion(FGFDMExec* exec) : FGModel(exec) |
| 72 | { |
| 73 | Name = "FGPropulsion"; |
| 74 | |
| 75 | numSelectedFuelTanks = numSelectedOxiTanks = 0; |
| 76 | numTanks = numEngines = 0; |
| 77 | numOxiTanks = numFuelTanks = 0; |
| 78 | ActiveEngine = -1; // -1: ALL, 0: Engine 1, 1: Engine 2 ... |
| 79 | tankJ.InitMatrix(); |
| 80 | DumpRate = 0.0; |
| 81 | RefuelRate = 6000.0; |
| 82 | FuelFreeze = false; |
| 83 | IsBound = |
| 84 | HavePistonEngine = |
| 85 | HaveTurbineEngine = |
| 86 | HaveRocketEngine = |
| 87 | HaveTurboPropEngine = |
| 88 | HaveElectricEngine = false; |
| 89 | |
| 90 | Debug(0); |
| 91 | } |
| 92 | |
| 93 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | |
| 95 | FGPropulsion::~FGPropulsion() |
| 96 | { |
| 97 | for (unsigned int i=0; i<Engines.size(); i++) delete Engines[i]; |
| 98 | Engines.clear(); |
| 99 | for (unsigned int i=0; i<Tanks.size(); i++) delete Tanks[i]; |
| 100 | Tanks.clear(); |
| 101 | Debug(1); |
| 102 | } |
| 103 | |
| 104 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 105 | |
| 106 | bool FGPropulsion::InitModel(void) |
| 107 | { |
| 108 | bool result = true; |
| 109 | |
| 110 | if (!FGModel::InitModel()) return false; |
| 111 | |
| 112 | vForces.InitMatrix(); |
| 113 | vMoments.InitMatrix(); |
| 114 | |
| 115 | for (unsigned int i=0; i<numTanks; i++) Tanks[i]->ResetToIC(); |
| 116 | TotalFuelQuantity = 0.0; |
| 117 | TotalOxidizerQuantity = 0.0; |
| 118 | refuel = dump = false; |
| 119 | |
| 120 | for (unsigned int i=0; i<numEngines; i++) |
| 121 | Engines[i]->ResetToIC(); |
| 122 | |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 127 | |
| 128 | bool FGPropulsion::Run(bool Holding) |
| 129 | { |
| 130 | unsigned int i; |
| 131 | |
| 132 | if (FGModel::Run(Holding)) return true; |
| 133 | if (Holding) return false; |
| 134 | |
| 135 | RunPreFunctions(); |
| 136 | |
| 137 | vForces.InitMatrix(); |
| 138 | vMoments.InitMatrix(); |
| 139 | |
| 140 | for (i=0; i<numEngines; i++) { |
| 141 | Engines[i]->Calculate(); |
| 142 | ConsumeFuel(Engines[i]); |
| 143 | vForces += Engines[i]->GetBodyForces(); // sum body frame forces |
| 144 | vMoments += Engines[i]->GetMoments(); // sum body frame moments |
| 145 | } |
| 146 | |
| 147 | TotalFuelQuantity = 0.0; |
| 148 | TotalOxidizerQuantity = 0.0; |
| 149 | for (i=0; i<numTanks; i++) { |
| 150 | Tanks[i]->Calculate( in.TotalDeltaT, in.TAT_c); |
| 151 | switch (Tanks[i]->GetType()) { |
| 152 | case FGTank::ttFUEL: |
| 153 | TotalFuelQuantity += Tanks[i]->GetContents(); |
| 154 | break; |
| 155 | case FGTank::ttOXIDIZER: |
| 156 | TotalOxidizerQuantity += Tanks[i]->GetContents(); |
| 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (refuel.node() && refuel) DoRefuel( in.TotalDeltaT ); |
| 164 | if (dump.node() && dump) DumpFuel( in.TotalDeltaT ); |
| 165 | |
| 166 | RunPostFunctions(); |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 172 | // |
| 173 | // The engine can tell us how much fuel it needs, but it is up to the propulsion |
| 174 | // subsystem manager class FGPropulsion to manage fuel flow amongst tanks. Engines |
| 175 | // May burn fuel from more than one tank at a time, and may burn from one tank |
| 176 | // before another - that is, may burn from one tank until the tank is depleted, |
| 177 | // then burn from the next highest priority tank. This can be accompished |
| 178 | // by defining a fuel management system, but this way of specifying priorities |
| 179 | // is more automatic from a user perspective. |
| 180 | |
| 181 | void FGPropulsion::ConsumeFuel(FGEngine* engine) |
| 182 | { |
| 183 | if (FuelFreeze) return; |
| 184 | if (FDMExec->GetTrimStatus()) return; |
| 185 | |
| 186 | unsigned int TanksWithFuel=0, CurrentFuelTankPriority=1; |
| 187 | unsigned int TanksWithOxidizer=0, CurrentOxidizerTankPriority=1; |
| 188 | vector <int> FeedListFuel, FeedListOxi; |
| 189 | bool Starved = true; // Initially set Starved to true. Set to false in code below. |
| 190 | bool hasOxTanks = false; |
| 191 | |
| 192 | // For this engine, |
| 193 | // 1) Count how many fuel tanks with the current priority level have fuel |
| 194 | // 2) If there none, then try next lower priority (higher number) - that is, |
| 195 | // increment CurrentPriority. |
| 196 | // 3) Build the feed list. |
| 197 | // 4) Do the same for oxidizer tanks, if needed. |
| 198 | |
| 199 | // Process fuel tanks, if any |
| 200 | while ((TanksWithFuel == 0) && (CurrentFuelTankPriority <= numTanks)) { |
| 201 | for (unsigned int i=0; i<engine->GetNumSourceTanks(); i++) { |
| 202 | unsigned int TankId = engine->GetSourceTank(i); |
| 203 | FGTank* Tank = Tanks[TankId]; |
| 204 | unsigned int TankPriority = Tank->GetPriority(); |
| 205 | if (TankPriority != 0) { |
| 206 | switch(Tank->GetType()) { |
| 207 | case FGTank::ttFUEL: |
| 208 | if ((Tank->GetContents() > Tank->GetUnusable()) && Tank->GetSelected() && (TankPriority == CurrentFuelTankPriority)) { |
| 209 | TanksWithFuel++; |
| 210 | Starved = false; |
| 211 | FeedListFuel.push_back(TankId); |
| 212 | } |
| 213 | break; |
| 214 | case FGTank::ttOXIDIZER: |
| 215 | // Skip this here (done below) |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | if (TanksWithFuel == 0) CurrentFuelTankPriority++; // No tanks at this priority, try next priority |
| 221 | } |
| 222 | |
| 223 | bool FuelStarved = Starved; |
| 224 | Starved = true; |
| 225 | |
| 226 | // Process Oxidizer tanks, if any |
| 227 | if (engine->GetType() == FGEngine::etRocket) { |
| 228 | while ((TanksWithOxidizer == 0) && (CurrentOxidizerTankPriority <= numTanks)) { |
| 229 | for (unsigned int i=0; i<engine->GetNumSourceTanks(); i++) { |
| 230 | unsigned int TankId = engine->GetSourceTank(i); |
| 231 | FGTank* Tank = Tanks[TankId]; |
| 232 | unsigned int TankPriority = Tank->GetPriority(); |
| 233 | if (TankPriority != 0) { |
| 234 | switch(Tank->GetType()) { |
| 235 | case FGTank::ttFUEL: |
| 236 | // Skip this here (done above) |
| 237 | break; |
| 238 | case FGTank::ttOXIDIZER: |
| 239 | hasOxTanks = true; |
| 240 | if (Tank->GetContents() > Tank->GetUnusable() && Tank->GetSelected() && TankPriority == CurrentOxidizerTankPriority) { |
| 241 | TanksWithOxidizer++; |
| 242 | if (TanksWithFuel > 0) Starved = false; |
| 243 | FeedListOxi.push_back(TankId); |
| 244 | } |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | if (TanksWithOxidizer == 0) CurrentOxidizerTankPriority++; // No tanks at this priority, try next priority |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | bool OxiStarved = Starved; |
| 254 | |
| 255 | engine->SetStarved(FuelStarved || (hasOxTanks && OxiStarved)); // Tanks can be refilled, so be sure to reset engine Starved flag here. |
| 256 | |
| 257 | // No fuel or fuel/oxidizer found at any priority! |
| 258 | // if (Starved) return; |
| 259 | if (FuelStarved || (hasOxTanks && OxiStarved)) return; |
| 260 | |
| 261 | double FuelToBurn = engine->CalcFuelNeed(); // How much fuel does this engine need? |
| 262 | double FuelNeededPerTank = FuelToBurn / TanksWithFuel; // Determine fuel needed per tank. |
| 263 | for (unsigned int i=0; i<FeedListFuel.size(); i++) { |
| 264 | Tanks[FeedListFuel[i]]->Drain(FuelNeededPerTank); |
| 265 | } |
| 266 | |
| 267 | if (engine->GetType() == FGEngine::etRocket) { |
| 268 | double OxidizerToBurn = engine->CalcOxidizerNeed(); // How much fuel does this engine need? |
| 269 | double OxidizerNeededPerTank = 0; |
| 270 | if (TanksWithOxidizer > 0) OxidizerNeededPerTank = OxidizerToBurn / TanksWithOxidizer; // Determine fuel needed per tank. |
| 271 | for (unsigned int i=0; i<FeedListOxi.size(); i++) { |
| 272 | Tanks[FeedListOxi[i]]->Drain(OxidizerNeededPerTank); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | |
| 278 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 279 | |
| 280 | bool FGPropulsion::GetSteadyState(void) |
| 281 | { |
| 282 | double currentThrust = 0, lastThrust = -1; |
| 283 | int steady_count = 0, j = 0; |
| 284 | bool steady = false; |
| 285 | bool TrimMode = FDMExec->GetTrimStatus(); |
| 286 | double TimeStep = FDMExec->GetDeltaT(); |
| 287 | |
| 288 | vForces.InitMatrix(); |
| 289 | vMoments.InitMatrix(); |
| 290 | |
| 291 | if (!FGModel::Run(false)) { |
| 292 | FDMExec->SetTrimStatus(true); |
| 293 | // This is a time marching algorithm so it needs a non-zero time step to |
| 294 | // reach a steady state. |
| 295 | in.TotalDeltaT = 0.5; |
| 296 | |
| 297 | for (unsigned int i=0; i<numEngines; i++) { |
| 298 | steady=false; |
| 299 | steady_count=0; |
| 300 | j=0; |
| 301 | while (!steady && j < 6000) { |
| 302 | Engines[i]->Calculate(); |
| 303 | lastThrust = currentThrust; |
| 304 | currentThrust = Engines[i]->GetThrust(); |
| 305 | if (fabs(lastThrust-currentThrust) < 0.0001) { |
| 306 | steady_count++; |
| 307 | if (steady_count > 120) { |
| 308 | steady=true; |
| 309 | } |
| 310 | } else { |
| 311 | steady_count=0; |
| 312 | } |
| 313 | j++; |
| 314 | } |
| 315 | vForces += Engines[i]->GetBodyForces(); // sum body frame forces |
| 316 | vMoments += Engines[i]->GetMoments(); // sum body frame moments |
| 317 | } |
| 318 | |
| 319 | FDMExec->SetTrimStatus(TrimMode); |
| 320 | in.TotalDeltaT = TimeStep; |
| 321 | |
| 322 | return false; |
| 323 | } else { |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 329 | |
| 330 | void FGPropulsion::InitRunning(int n) |
| 331 | { |
| 332 | if (n >= 0) { // A specific engine is supposed to be initialized |
| 333 | |
| 334 | if (n >= (int)GetNumEngines() ) { |
| 335 | throw(string("Tried to initialize a non-existent engine!")); |
| 336 | } |
| 337 | |
| 338 | in.ThrottleCmd[n] = in.ThrottlePos[n] = 1; // Set the throttle command and position |
| 339 | in.MixtureCmd[n] = in.MixturePos[n] = 1; // Set the mixture command and position |
| 340 | |
| 341 | GetEngine(n)->InitRunning(); |
| 342 | GetSteadyState(); |
| 343 | |
| 344 | } else if (n < 0) { // -1 refers to "All Engines" |
| 345 | |
| 346 | for (unsigned int i=0; i<GetNumEngines(); i++) { |
| 347 | in.ThrottleCmd[i] = in.ThrottlePos[i] = 1; // Set the throttle command and position |
| 348 | in.MixtureCmd[i] = in.MixturePos[i] = 1; // Set the mixture command and position |
| 349 | GetEngine(i)->InitRunning(); |
| 350 | } |
| 351 | |
| 352 | GetSteadyState(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 357 | |
| 358 | bool FGPropulsion::Load(Element* el) |
| 359 | { |
| 360 | FGModelLoader ModelLoader(this); |
| 361 | |
| 362 | Debug(2); |
| 363 | ReadingEngine = false; |
| 364 | double FuelDensity = 6.0; |
| 365 | |
| 366 | Name = "Propulsion Model: " + el->GetAttributeValue("name"); |
| 367 | |
| 368 | // Perform base class Pre-Load |
| 369 | if (!FGModel::Upload(el, true)) |
| 370 | return false; |
| 371 | |
| 372 | // Process tank definitions first to establish the number of fuel tanks |
| 373 | |
| 374 | Element* tank_element = el->FindElement("tank"); |
| 375 | while (tank_element) { |
| 376 | Tanks.push_back(new FGTank(FDMExec, tank_element, numTanks)); |
| 377 | if (Tanks.back()->GetType() == FGTank::ttFUEL) { |
| 378 | FuelDensity = Tanks[numFuelTanks]->GetDensity(); |
| 379 | numFuelTanks++; |
| 380 | } |
| 381 | else if (Tanks.back()->GetType() == FGTank::ttOXIDIZER) numOxiTanks++; |
| 382 | else {cerr << "Unknown tank type specified." << endl; return false;} |
| 383 | numTanks++; |
| 384 | tank_element = el->FindNextElement("tank"); |
| 385 | } |
| 386 | numSelectedFuelTanks = numFuelTanks; |
| 387 | numSelectedOxiTanks = numOxiTanks; |
| 388 | |
| 389 | ReadingEngine = true; |
| 390 | Element* engine_element = el->FindElement("engine"); |
| 391 | while (engine_element) { |
| 392 | if (!ModelLoader.Open(engine_element)) return false; |
| 393 | |
| 394 | try { |
| 395 | // Locate the thruster definition |
| 396 | Element* thruster_element = engine_element->FindElement("thruster"); |
| 397 | if (!thruster_element || !ModelLoader.Open(thruster_element)) |
| 398 | throw("No thruster definition supplied with engine definition."); |
| 399 | |
| 400 | if (engine_element->FindElement("piston_engine")) { |
| 401 | HavePistonEngine = true; |
| 402 | if (!IsBound) bind(); |
| 403 | Element *element = engine_element->FindElement("piston_engine"); |
| 404 | Engines.push_back(new FGPiston(FDMExec, element, numEngines, in)); |
| 405 | } else if (engine_element->FindElement("turbine_engine")) { |
| 406 | HaveTurbineEngine = true; |
| 407 | if (!IsBound) bind(); |
| 408 | Element *element = engine_element->FindElement("turbine_engine"); |
| 409 | Engines.push_back(new FGTurbine(FDMExec, element, numEngines, in)); |
| 410 | } else if (engine_element->FindElement("turboprop_engine")) { |
| 411 | HaveTurboPropEngine = true; |
| 412 | if (!IsBound) bind(); |
| 413 | Element *element = engine_element->FindElement("turboprop_engine"); |
| 414 | Engines.push_back(new FGTurboProp(FDMExec, element, numEngines, in)); |
| 415 | } else if (engine_element->FindElement("rocket_engine")) { |
| 416 | HaveRocketEngine = true; |
| 417 | if (!IsBound) bind(); |
| 418 | Element *element = engine_element->FindElement("rocket_engine"); |
| 419 | Engines.push_back(new FGRocket(FDMExec, element, numEngines, in)); |
| 420 | } else if (engine_element->FindElement("electric_engine")) { |
| 421 | HaveElectricEngine = true; |
| 422 | if (!IsBound) bind(); |
| 423 | Element *element = engine_element->FindElement("electric_engine"); |
| 424 | Engines.push_back(new FGElectric(FDMExec, element, numEngines, in)); |
| 425 | } else { |
| 426 | cerr << engine_element->ReadFrom() << " Unknown engine type" << endl; |
| 427 | return false; |
| 428 | } |
| 429 | } catch (std::string& str) { |
| 430 | cerr << endl << fgred << str << reset << endl; |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | numEngines++; |
| 435 | |
| 436 | engine_element = el->FindNextElement("engine"); |
| 437 | } |
| 438 | |
| 439 | CalculateTankInertias(); |
| 440 | |
| 441 | if (el->FindElement("dump-rate")) |
| 442 | DumpRate = el->FindElementValueAsNumberConvertTo("dump-rate", "LBS/MIN"); |
| 443 | if (el->FindElement("refuel-rate")) |
| 444 | RefuelRate = el->FindElementValueAsNumberConvertTo("refuel-rate", "LBS/MIN"); |
| 445 | |
| 446 | unsigned int i; |
| 447 | for (i=0; i<Engines.size(); i++) { |
| 448 | Engines[i]->SetFuelDensity(FuelDensity); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | PostLoad(el, FDMExec); |
| 453 | |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 458 | |
| 459 | SGPath FGPropulsion::FindFullPathName(const SGPath& path) const |
| 460 | { |
| 461 | SGPath name = FGModel::FindFullPathName(path); |
| 462 | if (!ReadingEngine && !name.isNull()) return name; |
| 463 | |
| 464 | #ifdef _WIN32 |
| 465 | // Singular and plural are allowed for the folder names for consistency with |
| 466 | // the default engine folder name "engine" and for backward compatibility |
| 467 | // regarding the folder name "Engines". |
| 468 | const array<string, 2> dir_names = {"Engines", "engine"}; |
| 469 | #else |
| 470 | // Allow alternative capitalization for case sensitive OSes. |
| 471 | const array<string, 4> dir_names = {"Engines", "engines", "Engine", "engine"}; |
| 472 | #endif |
| 473 | |
| 474 | for(const string& dir_name: dir_names) { |
| 475 | name = CheckPathName(FDMExec->GetFullAircraftPath()/dir_name, path); |
| 476 | if (!name.isNull()) return name; |
| 477 | } |
| 478 | |
| 479 | return CheckPathName(FDMExec->GetEnginePath(), path); |
| 480 | } |
| 481 | |
| 482 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 483 | |
| 484 | string FGPropulsion::GetPropulsionStrings(const string& delimiter) const |
| 485 | { |
| 486 | unsigned int i; |
| 487 | |
| 488 | string PropulsionStrings = ""; |
| 489 | bool firstime = true; |
| 490 | stringstream buf; |
| 491 | |
| 492 | for (i=0; i<Engines.size(); i++) { |
| 493 | if (firstime) firstime = false; |
| 494 | else PropulsionStrings += delimiter; |
| 495 | |
| 496 | PropulsionStrings += Engines[i]->GetEngineLabels(delimiter); |
| 497 | } |
| 498 | for (i=0; i<Tanks.size(); i++) { |
| 499 | if (Tanks[i]->GetType() == FGTank::ttFUEL) buf << delimiter << "Fuel Tank " << i; |
| 500 | else if (Tanks[i]->GetType() == FGTank::ttOXIDIZER) buf << delimiter << "Oxidizer Tank " << i; |
| 501 | } |
| 502 | |
| 503 | PropulsionStrings += buf.str(); |
| 504 | buf.str(""); |
| 505 | |
| 506 | return PropulsionStrings; |
| 507 | } |
| 508 | |
| 509 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 510 | |
| 511 | string FGPropulsion::GetPropulsionValues(const string& delimiter) const |
| 512 | { |
| 513 | unsigned int i; |
| 514 | |
| 515 | string PropulsionValues = ""; |
| 516 | bool firstime = true; |
| 517 | stringstream buf; |
| 518 | |
| 519 | for (i=0; i<Engines.size(); i++) { |
| 520 | if (firstime) firstime = false; |
| 521 | else PropulsionValues += delimiter; |
| 522 | |
| 523 | PropulsionValues += Engines[i]->GetEngineValues(delimiter); |
| 524 | } |
| 525 | for (i=0; i<Tanks.size(); i++) { |
| 526 | buf << delimiter; |
| 527 | buf << Tanks[i]->GetContents(); |
| 528 | } |
| 529 | |
| 530 | PropulsionValues += buf.str(); |
| 531 | buf.str(""); |
| 532 | |
| 533 | return PropulsionValues; |
| 534 | } |
| 535 | |
| 536 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 537 | |
| 538 | string FGPropulsion::GetPropulsionTankReport() |
| 539 | { |
| 540 | string out=""; |
| 541 | stringstream outstream; |
| 542 | |
| 543 | /*const FGMatrix33& mTkI =*/ CalculateTankInertias(); |
| 544 | |
| 545 | for (unsigned int i=0; i<numTanks; i++) |
| 546 | { |
| 547 | FGTank* tank = Tanks[i]; |
| 548 | string tankname=""; |
| 549 | if (tank->GetType() == FGTank::ttFUEL && tank->GetGrainType() != FGTank::gtUNKNOWN) { |
| 550 | tankname = "Solid Fuel"; |
| 551 | } else if (tank->GetType() == FGTank::ttFUEL) { |
| 552 | tankname = "Fuel"; |
| 553 | } else if (tank->GetType() == FGTank::ttOXIDIZER) { |
| 554 | tankname = "Oxidizer"; |
| 555 | } else { |
| 556 | tankname = "(Unknown tank type)"; |
| 557 | } |
| 558 | outstream << highint << left << setw(4) << i << setw(30) << tankname << normint |
| 559 | << right << setw(10) << tank->GetContents() << setw(8) << tank->GetXYZ(eX) |
| 560 | << setw(8) << tank->GetXYZ(eY) << setw(8) << tank->GetXYZ(eZ) |
| 561 | << setw(12) << tank->GetIxx() << setw(12) << tank->GetIyy() |
| 562 | << setw(12) << tank->GetIzz() << endl; |
| 563 | } |
| 564 | return outstream.str(); |
| 565 | } |
| 566 | |
| 567 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 568 | |
| 569 | const FGColumnVector3& FGPropulsion::GetTanksMoment(void) |
| 570 | { |
| 571 | vXYZtank_arm.InitMatrix(); |
| 572 | for (unsigned int i=0; i<Tanks.size(); i++) { |
| 573 | vXYZtank_arm += Tanks[i]->GetXYZ() * Tanks[i]->GetContents(); |
| 574 | } |
| 575 | return vXYZtank_arm; |
| 576 | } |
| 577 | |
| 578 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 579 | |
| 580 | double FGPropulsion::GetTanksWeight(void) const |
| 581 | { |
| 582 | double Tw = 0.0; |
| 583 | |
| 584 | for (unsigned int i=0; i<Tanks.size(); i++) Tw += Tanks[i]->GetContents(); |
| 585 | |
| 586 | return Tw; |
| 587 | } |
| 588 | |
| 589 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 590 | |
| 591 | const FGMatrix33& FGPropulsion::CalculateTankInertias(void) |
| 592 | { |
| 593 | size_t size = Tanks.size(); |
| 594 | |
| 595 | if (size == 0) return tankJ; |
| 596 | |
| 597 | tankJ.InitMatrix(); |
| 598 | |
| 599 | for (unsigned int i=0; i<size; i++) { |
| 600 | tankJ += FDMExec->GetMassBalance()->GetPointmassInertia( lbtoslug * Tanks[i]->GetContents(), |
| 601 | Tanks[i]->GetXYZ()); |
| 602 | tankJ(1,1) += Tanks[i]->GetIxx(); |
| 603 | tankJ(2,2) += Tanks[i]->GetIyy(); |
| 604 | tankJ(3,3) += Tanks[i]->GetIzz(); |
| 605 | } |
| 606 | |
| 607 | return tankJ; |
| 608 | } |
| 609 | |
| 610 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 611 | |
| 612 | void FGPropulsion::SetMagnetos(int setting) |
| 613 | { |
| 614 | if (ActiveEngine < 0) { |
| 615 | for (unsigned i=0; i<Engines.size(); i++) { |
| 616 | // ToDo: first need to make sure the engine Type is really appropriate: |
| 617 | // do a check to see if it is of type Piston. This should be done for |
| 618 | // all of this kind of possibly across-the-board settings. |
| 619 | if (Engines[i]->GetType() == FGEngine::etPiston) |
| 620 | ((FGPiston*)Engines[i])->SetMagnetos(setting); |
| 621 | } |
| 622 | } else { |
| 623 | ((FGPiston*)Engines[ActiveEngine])->SetMagnetos(setting); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 628 | |
| 629 | void FGPropulsion::SetStarter(int setting) |
| 630 | { |
| 631 | if (ActiveEngine < 0) { |
| 632 | for (unsigned i=0; i<Engines.size(); i++) { |
| 633 | if (setting == 0) |
| 634 | Engines[i]->SetStarter(false); |
| 635 | else |
| 636 | Engines[i]->SetStarter(true); |
| 637 | } |
| 638 | } else { |
| 639 | if (setting == 0) |
| 640 | Engines[ActiveEngine]->SetStarter(false); |
| 641 | else |
| 642 | Engines[ActiveEngine]->SetStarter(true); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 647 | |
| 648 | int FGPropulsion::GetStarter(void) const |
| 649 | { |
| 650 | if (ActiveEngine < 0) { |
| 651 | bool starter = true; |
| 652 | |
| 653 | for (unsigned i=0; i<Engines.size(); i++) |
| 654 | starter &= Engines[i]->GetStarter(); |
| 655 | |
| 656 | return starter ? 1 : 0; |
| 657 | } else |
| 658 | return Engines[ActiveEngine]->GetStarter() ? 1: 0; |
| 659 | } |
| 660 | |
| 661 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 662 | |
| 663 | void FGPropulsion::SetCutoff(int setting) |
| 664 | { |
| 665 | bool bsetting = setting == 0 ? false : true; |
| 666 | |
| 667 | if (ActiveEngine < 0) { |
| 668 | for (unsigned i=0; i<Engines.size(); i++) { |
| 669 | switch (Engines[i]->GetType()) { |
| 670 | case FGEngine::etTurbine: |
| 671 | ((FGTurbine*)Engines[i])->SetCutoff(bsetting); |
| 672 | break; |
| 673 | case FGEngine::etTurboprop: |
| 674 | ((FGTurboProp*)Engines[i])->SetCutoff(bsetting); |
| 675 | break; |
| 676 | default: |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | } else { |
| 681 | switch (Engines[ActiveEngine]->GetType()) { |
| 682 | case FGEngine::etTurbine: |
| 683 | ((FGTurbine*)Engines[ActiveEngine])->SetCutoff(bsetting); |
| 684 | break; |
| 685 | case FGEngine::etTurboprop: |
| 686 | ((FGTurboProp*)Engines[ActiveEngine])->SetCutoff(bsetting); |
| 687 | break; |
| 688 | default: |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 695 | |
| 696 | int FGPropulsion::GetCutoff(void) const |
| 697 | { |
| 698 | if (ActiveEngine < 0) { |
| 699 | bool cutoff = true; |
| 700 | |
| 701 | for (unsigned i=0; i<Engines.size(); i++) { |
| 702 | switch (Engines[i]->GetType()) { |
| 703 | case FGEngine::etTurbine: |
| 704 | cutoff &= ((FGTurbine*)Engines[i])->GetCutoff(); |
| 705 | break; |
| 706 | case FGEngine::etTurboprop: |
| 707 | cutoff &= ((FGTurboProp*)Engines[i])->GetCutoff(); |
| 708 | break; |
| 709 | default: |
| 710 | return -1; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | return cutoff ? 1 : 0; |
| 715 | } else { |
| 716 | switch (Engines[ActiveEngine]->GetType()) { |
| 717 | case FGEngine::etTurbine: |
| 718 | return ((FGTurbine*)Engines[ActiveEngine])->GetCutoff() ? 1 : 0; |
| 719 | case FGEngine::etTurboprop: |
| 720 | return ((FGTurboProp*)Engines[ActiveEngine])->GetCutoff() ? 1 : 0; |
| 721 | default: |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | return -1; |
| 727 | } |
| 728 | |
| 729 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 730 | |
| 731 | void FGPropulsion::SetActiveEngine(int engine) |
| 732 | { |
| 733 | if (engine >= (int)Engines.size() || engine < 0) |
| 734 | ActiveEngine = -1; |
| 735 | else |
| 736 | ActiveEngine = engine; |
| 737 | } |
| 738 | |
| 739 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 740 | |
| 741 | double FGPropulsion::Transfer(int source, int target, double amount) |
| 742 | { |
| 743 | double shortage, overage; |
| 744 | |
| 745 | if (source == -1) { |
| 746 | shortage = 0.0; |
| 747 | } else { |
| 748 | shortage = Tanks[source]->Drain(amount); |
| 749 | } |
| 750 | if (target == -1) { |
| 751 | overage = 0.0; |
| 752 | } else { |
| 753 | overage = Tanks[target]->Fill(amount - shortage); |
| 754 | } |
| 755 | return overage; |
| 756 | } |
| 757 | |
| 758 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 759 | |
| 760 | void FGPropulsion::DoRefuel(double time_slice) |
| 761 | { |
| 762 | unsigned int i; |
| 763 | |
| 764 | double fillrate = RefuelRate / 60.0 * time_slice; |
| 765 | int TanksNotFull = 0; |
| 766 | |
| 767 | for (i=0; i<numTanks; i++) { |
| 768 | if (Tanks[i]->GetPctFull() < 99.99) ++TanksNotFull; |
| 769 | } |
| 770 | |
| 771 | // adds fuel equally to all tanks that are not full |
| 772 | if (TanksNotFull) { |
| 773 | for (i=0; i<numTanks; i++) { |
| 774 | if (Tanks[i]->GetPctFull() < 99.99) |
| 775 | Transfer(-1, i, fillrate/TanksNotFull); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 781 | |
| 782 | void FGPropulsion::DumpFuel(double time_slice) |
| 783 | { |
| 784 | unsigned int i; |
| 785 | int TanksDumping = 0; |
| 786 | |
| 787 | for (i=0; i<numTanks; i++) { |
| 788 | if (Tanks[i]->GetContents() > Tanks[i]->GetStandpipe()) ++TanksDumping; |
| 789 | } |
| 790 | |
| 791 | if (TanksDumping == 0) return; |
| 792 | |
| 793 | double dump_rate_per_tank = DumpRate / 60.0 * time_slice / TanksDumping; |
| 794 | |
| 795 | for (i=0; i<numTanks; i++) { |
| 796 | if (Tanks[i]->GetContents() > Tanks[i]->GetStandpipe()) { |
| 797 | Transfer(i, -1, dump_rate_per_tank); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 803 | |
| 804 | void FGPropulsion::SetFuelFreeze(bool f) |
| 805 | { |
| 806 | FuelFreeze = f; |
| 807 | for (unsigned int i=0; i<numEngines; i++) { |
| 808 | Engines[i]->SetFuelFreeze(f); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 813 | |
| 814 | void FGPropulsion::bind(void) |
| 815 | { |
| 816 | typedef double (FGPropulsion::*PMF)(int) const; |
| 817 | typedef int (FGPropulsion::*iPMF)(void) const; |
| 818 | typedef bool (FGPropulsion::*bPMF)(void) const; |
| 819 | |
| 820 | IsBound = true; |
| 821 | PropertyManager->Tie("propulsion/set-running", this, (iPMF)0, &FGPropulsion::InitRunning); |
| 822 | if (HaveTurbineEngine || HaveTurboPropEngine) { |
| 823 | PropertyManager->Tie("propulsion/starter_cmd", this, &FGPropulsion::GetStarter, &FGPropulsion::SetStarter); |
| 824 | PropertyManager->Tie("propulsion/cutoff_cmd", this, &FGPropulsion::GetCutoff, &FGPropulsion::SetCutoff); |
| 825 | } |
| 826 | |
| 827 | if (HavePistonEngine) { |
| 828 | PropertyManager->Tie("propulsion/starter_cmd", this, &FGPropulsion::GetStarter, &FGPropulsion::SetStarter); |
| 829 | PropertyManager->Tie("propulsion/magneto_cmd", this, (iPMF)0, &FGPropulsion::SetMagnetos); |
| 830 | } |
| 831 | |
| 832 | PropertyManager->Tie("propulsion/active_engine", this, (iPMF)&FGPropulsion::GetActiveEngine, |
| 833 | &FGPropulsion::SetActiveEngine); |
| 834 | PropertyManager->Tie("forces/fbx-prop-lbs", this, eX, (PMF)&FGPropulsion::GetForces); |
| 835 | PropertyManager->Tie("forces/fby-prop-lbs", this, eY, (PMF)&FGPropulsion::GetForces); |
| 836 | PropertyManager->Tie("forces/fbz-prop-lbs", this, eZ, (PMF)&FGPropulsion::GetForces); |
| 837 | PropertyManager->Tie("moments/l-prop-lbsft", this, eX, (PMF)&FGPropulsion::GetMoments); |
| 838 | PropertyManager->Tie("moments/m-prop-lbsft", this, eY, (PMF)&FGPropulsion::GetMoments); |
| 839 | PropertyManager->Tie("moments/n-prop-lbsft", this, eZ, (PMF)&FGPropulsion::GetMoments); |
| 840 | TotalFuelQuantity = PropertyManager->CreatePropertyObject<double>("propulsion/total-fuel-lbs"); |
| 841 | TotalOxidizerQuantity = PropertyManager->CreatePropertyObject<double>("propulsion/total-oxidizer-lbs"); |
| 842 | refuel = PropertyManager->CreatePropertyObject<bool>("propulsion/refuel"); |
| 843 | dump = PropertyManager->CreatePropertyObject<bool>("propulsion/fuel_dump"); |
| 844 | PropertyManager->Tie("propulsion/fuel_freeze", this, (bPMF)nullptr, &FGPropulsion::SetFuelFreeze); |
| 845 | } |
| 846 | |
| 847 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 848 | // The bitmasked value choices are as follows: |
| 849 | // unset: In this case (the default) JSBSim would only print |
| 850 | // out the normally expected messages, essentially echoing |
| 851 | // the config files as they are read. If the environment |
| 852 | // variable is not set, debug_lvl is set to 1 internally |
| 853 | // 0: This requests JSBSim not to output any messages |
| 854 | // whatsoever. |
| 855 | // 1: This value explicity requests the normal JSBSim |
| 856 | // startup messages |
| 857 | // 2: This value asks for a message to be printed out when |
| 858 | // a class is instantiated |
| 859 | // 4: When this value is set, a message is displayed when a |
| 860 | // FGModel object executes its Run() method |
| 861 | // 8: When this value is set, various runtime state variables |
| 862 | // are printed out periodically |
| 863 | // 16: When set various parameters are sanity checked and |
| 864 | // a message is printed out when they go out of bounds |
| 865 | |
| 866 | void FGPropulsion::Debug(int from) |
| 867 | { |
| 868 | if (debug_lvl <= 0) return; |
| 869 | |
| 870 | if (debug_lvl & 1) { // Standard console startup message output |
| 871 | if (from == 2) { // Loader |
| 872 | cout << endl << " Propulsion:" << endl; |
| 873 | } |
| 874 | } |
| 875 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 876 | if (from == 0) cout << "Instantiated: FGPropulsion" << endl; |
| 877 | if (from == 1) cout << "Destroyed: FGPropulsion" << endl; |
| 878 | } |
| 879 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 880 | } |
| 881 | if (debug_lvl & 8 ) { // Runtime state variables |
| 882 | } |
| 883 | if (debug_lvl & 16) { // Sanity checking |
| 884 | } |
| 885 | if (debug_lvl & 64) { |
| 886 | if (from == 0) { // Constructor |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | } |