2 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 Purpose: Schedules and runs the model routines.
9 ------------- Copyright (C) 1999 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 --------------------------------------------------------------------------------
31 This class wraps up the simulation scheduling routines.
34 --------------------------------------------------------------------------------
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 COMMENTS, REFERENCES, and NOTES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50 #include "FGFDMExec.h"
51 #include "models/atmosphere/FGStandardAtmosphere.h"
52 #include "models/atmosphere/FGWinds.h"
53 #include "models/FGFCS.h"
54 #include "models/FGPropulsion.h"
55 #include "models/FGMassBalance.h"
56 #include "models/FGGroundReactions.h"
57 #include "models/FGExternalReactions.h"
58 #include "models/FGBuoyantForces.h"
59 #include "models/FGAerodynamics.h"
60 #include "models/FGInertial.h"
61 #include "models/FGAircraft.h"
62 #include "models/FGAccelerations.h"
63 #include "models/FGPropagate.h"
64 #include "models/FGAuxiliary.h"
65 #include "models/FGInput.h"
66 #include "models/FGOutput.h"
67 #include "initialization/FGTrim.h"
68 #include "input_output/FGScript.h"
69 #include "input_output/FGXMLFileRead.h"
75 IDENT(IdSrc,"$Id: FGFDMExec.cpp,v 1.185 2015/12/13 08:01:50 bcoconni Exp $");
76 IDENT(IdHdr,ID_FDMEXEC);
78 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) : Root(root), FDMctr(fdmctr)
104 IncrementThenHolding = false; // increment then hold is off by default
105 TimeStepsUntilHold = -1;
108 dT = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is
109 // run in standalone mode with no initialization file.
111 AircraftPath = "aircraft";
112 EnginePath = "engine";
113 SystemsPath = "systems";
116 char* num = getenv("JSBSIM_DEBUG");
117 if (num) debug_lvl = atoi(num); // set debug level
118 } catch (...) { // if error set to 1
122 if (Root == 0) { // Then this is the root FDM
123 Root = new FGPropertyManager; // Create the property manager
128 FDMctr = new unsigned int; // Create and initialize the child FDM counter
132 // Store this FDM's ID
133 IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth"
135 // Prepare FDMctr for the next child FDM id
136 (*FDMctr)++; // instance. "child" instances are loaded last.
138 FGPropertyNode* instanceRoot = Root->GetNode("/fdm/jsbsim",IdFDM,true);
139 instance = new FGPropertyManager(instanceRoot);
142 char* num = getenv("JSBSIM_DISPERSE");
144 if (atoi(num) != 0) disperse = 1; // set dispersions on
146 } catch (...) { // if error set to false
148 std::cerr << "Could not process JSBSIM_DISPERSIONS environment variable: Assumed NO dispersions." << endl;
152 // this is to catch errors in binding member functions to the property tree.
155 } catch (const string& msg ) {
156 cout << "Caught error: " << msg << endl;
165 typedef int (FGFDMExec::*iPMF)(void) const;
166 instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim, false);
167 instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions, false);
168 instance->Tie("simulation/disperse", this, &FGFDMExec::GetDisperse);
169 instance->Tie("simulation/randomseed", this, (iPMF)&FGFDMExec::SRand, &FGFDMExec::SRand, false);
170 instance->Tie("simulation/terminate", (int *)&Terminate);
171 instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime);
172 instance->Tie("simulation/dt", this, &FGFDMExec::GetDeltaT);
173 instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel);
174 instance->Tie("simulation/frame", (int *)&Frame, false);
175 instance->Tie("simulation/trim-completed", (int *)&trim_completed, false);
177 Constructing = false;
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 FGFDMExec::~FGFDMExec()
190 if (IdFDM == 0) { // Meaning this is no child FDM
201 } catch (const string& msg ) {
202 cout << "Caught error: " << msg << endl;
205 for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
206 ChildFDMList.clear();
208 PropertyCatalog.clear();
210 SetGroundCallback(0);
212 if (FDMctr > 0) (*FDMctr)--;
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 bool FGFDMExec::Allocate(void)
223 Models.resize(eNumStandardModels);
225 // First build the inertial model since some other models are relying on
226 // the inertial model and the ground callback to build themselves.
227 // Note that this does not affect the order in which the models will be
229 Models[eInertial] = new FGInertial(this);
230 SetGroundCallback(new FGDefaultGroundCallback(static_cast<FGInertial*>(Models[eInertial])->GetRefRadius()));
232 // See the eModels enum specification in the header file. The order of the
233 // enums specifies the order of execution. The Models[] vector is the primary
234 // storage array for the list of models.
235 Models[ePropagate] = new FGPropagate(this);
236 Models[eInput] = new FGInput(this);
237 Models[eAtmosphere] = new FGStandardAtmosphere(this);
238 Models[eWinds] = new FGWinds(this);
239 Models[eSystems] = new FGFCS(this);
240 Models[eMassBalance] = new FGMassBalance(this);
241 Models[eAuxiliary] = new FGAuxiliary(this);
242 Models[ePropulsion] = new FGPropulsion(this);
243 Models[eAerodynamics] = new FGAerodynamics (this);
244 Models[eGroundReactions] = new FGGroundReactions(this);
245 Models[eExternalReactions] = new FGExternalReactions(this);
246 Models[eBuoyantForces] = new FGBuoyantForces(this);
247 Models[eAircraft] = new FGAircraft(this);
248 Models[eAccelerations] = new FGAccelerations(this);
249 Models[eOutput] = new FGOutput(this);
251 // Assign the Model shortcuts for internal executive use only.
252 Propagate = (FGPropagate*)Models[ePropagate];
253 Inertial = (FGInertial*)Models[eInertial];
254 Atmosphere = (FGAtmosphere*)Models[eAtmosphere];
255 Winds = (FGWinds*)Models[eWinds];
256 FCS = (FGFCS*)Models[eSystems];
257 MassBalance = (FGMassBalance*)Models[eMassBalance];
258 Auxiliary = (FGAuxiliary*)Models[eAuxiliary];
259 Propulsion = (FGPropulsion*)Models[ePropulsion];
260 Aerodynamics = (FGAerodynamics*)Models[eAerodynamics];
261 GroundReactions = (FGGroundReactions*)Models[eGroundReactions];
262 ExternalReactions = (FGExternalReactions*)Models[eExternalReactions];
263 BuoyantForces = (FGBuoyantForces*)Models[eBuoyantForces];
264 Aircraft = (FGAircraft*)Models[eAircraft];
265 Accelerations = (FGAccelerations*)Models[eAccelerations];
266 Output = (FGOutput*)Models[eOutput];
268 // Initialize planet (environment) constants
269 LoadPlanetConstants();
272 for (unsigned int i = 0; i < Models.size(); i++) {
273 // The Input/Output models must not be initialized prior to IC loading
274 if (i == eInput || i == eOutput) continue;
277 Models[i]->InitModel();
280 IC = new FGInitialCondition(this);
288 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 bool FGFDMExec::DeAllocate(void)
293 for (unsigned int i=0; i<eNumStandardModels; i++) delete Models[i];
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 bool FGFDMExec::Run(void)
314 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
315 ChildFDMList[i]->AssignState( (FGPropagate*)Models[ePropagate] ); // Transfer state to the child FDM
316 ChildFDMList[i]->Run();
321 // returns true if success, false if complete
322 if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript();
324 for (unsigned int i = 0; i < Models.size(); i++) {
326 Models[i]->Run(holding);
330 unsigned int mode = ResetMode;
333 ResetToInitialConditions(mode);
336 if (Terminate) success = false;
341 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 void FGFDMExec::LoadInputs(unsigned int idx)
347 Propagate->in.vPQRidot = Accelerations->GetPQRidot();
348 Propagate->in.vQtrndot = Accelerations->GetQuaterniondot();
349 Propagate->in.vUVWidot = Accelerations->GetUVWidot();
350 Propagate->in.DeltaT = dT;
355 Inertial->in.Radius = Propagate->GetRadius();
356 Inertial->in.Latitude = Propagate->GetLatitude();
359 Atmosphere->in.altitudeASL = Propagate->GetAltitudeASL();
362 Winds->in.AltitudeASL = Propagate->GetAltitudeASL();
363 Winds->in.DistanceAGL = Propagate->GetDistanceAGL();
364 Winds->in.Tl2b = Propagate->GetTl2b();
365 Winds->in.Tw2b = Auxiliary->GetTw2b();
366 Winds->in.V = Auxiliary->GetVt();
367 Winds->in.totalDeltaT = dT * Winds->GetRate();
370 Auxiliary->in.Pressure = Atmosphere->GetPressure();
371 Auxiliary->in.Density = Atmosphere->GetDensity();
372 Auxiliary->in.DensitySL = Atmosphere->GetDensitySL();
373 Auxiliary->in.PressureSL = Atmosphere->GetPressureSL();
374 Auxiliary->in.Temperature = Atmosphere->GetTemperature();
375 Auxiliary->in.SoundSpeed = Atmosphere->GetSoundSpeed();
376 Auxiliary->in.KinematicViscosity = Atmosphere->GetKinematicViscosity();
377 Auxiliary->in.DistanceAGL = Propagate->GetDistanceAGL();
378 Auxiliary->in.Mass = MassBalance->GetMass();
379 Auxiliary->in.Tl2b = Propagate->GetTl2b();
380 Auxiliary->in.Tb2l = Propagate->GetTb2l();
381 Auxiliary->in.vPQR = Propagate->GetPQR();
382 Auxiliary->in.vPQRi = Propagate->GetPQRi();
383 Auxiliary->in.vPQRidot = Accelerations->GetPQRidot();
384 Auxiliary->in.vUVW = Propagate->GetUVW();
385 Auxiliary->in.vUVWdot = Accelerations->GetUVWdot();
386 Auxiliary->in.vVel = Propagate->GetVel();
387 Auxiliary->in.vBodyAccel = Accelerations->GetBodyAccel();
388 Auxiliary->in.ToEyePt = MassBalance->StructuralToBody(Aircraft->GetXYZep());
389 Auxiliary->in.VRPBody = MassBalance->StructuralToBody(Aircraft->GetXYZvrp());
390 Auxiliary->in.RPBody = MassBalance->StructuralToBody(Aircraft->GetXYZrp());
391 Auxiliary->in.vFw = Aerodynamics->GetvFw();
392 Auxiliary->in.vLocation = Propagate->GetLocation();
393 Auxiliary->in.CosTht = Propagate->GetCosEuler(eTht);
394 Auxiliary->in.SinTht = Propagate->GetSinEuler(eTht);
395 Auxiliary->in.CosPhi = Propagate->GetCosEuler(ePhi);
396 Auxiliary->in.SinPhi = Propagate->GetSinEuler(ePhi);
397 Auxiliary->in.Psi = Propagate->GetEuler(ePsi);
398 Auxiliary->in.TotalWindNED = Winds->GetTotalWindNED();
399 Auxiliary->in.TurbPQR = Winds->GetTurbPQR();
400 Auxiliary->in.WindPsi = Winds->GetWindPsi();
401 Auxiliary->in.Vwind = Winds->GetTotalWindNED().Magnitude();
402 Auxiliary->in.PitotAngle = Aircraft->GetPitotAngle();
405 // Dynamic inputs come into the components that FCS manages through properties
408 Propulsion->in.Pressure = Atmosphere->GetPressure();
409 Propulsion->in.PressureRatio = Atmosphere->GetPressureRatio();
410 Propulsion->in.Temperature = Atmosphere->GetTemperature();
411 Propulsion->in.DensityRatio = Atmosphere->GetDensityRatio();
412 Propulsion->in.Density = Atmosphere->GetDensity();
413 Propulsion->in.Soundspeed = Atmosphere->GetSoundSpeed();
414 Propulsion->in.TotalPressure = Auxiliary->GetTotalPressure();
415 Propulsion->in.Vc = Auxiliary->GetVcalibratedKTS();
416 Propulsion->in.Vt = Auxiliary->GetVt();
417 Propulsion->in.qbar = Auxiliary->Getqbar();
418 Propulsion->in.TAT_c = Auxiliary->GetTAT_C();
419 Propulsion->in.AeroUVW = Auxiliary->GetAeroUVW();
420 Propulsion->in.AeroPQR = Auxiliary->GetAeroPQR();
421 Propulsion->in.alpha = Auxiliary->Getalpha();
422 Propulsion->in.beta = Auxiliary->Getbeta();
423 Propulsion->in.TotalDeltaT = dT * Propulsion->GetRate();
424 Propulsion->in.ThrottlePos = FCS->GetThrottlePos();
425 Propulsion->in.MixturePos = FCS->GetMixturePos();
426 Propulsion->in.ThrottleCmd = FCS->GetThrottleCmd();
427 Propulsion->in.MixtureCmd = FCS->GetMixtureCmd();
428 Propulsion->in.PropAdvance = FCS->GetPropAdvance();
429 Propulsion->in.PropFeather = FCS->GetPropFeather();
430 Propulsion->in.H_agl = Propagate->GetDistanceAGL();
431 Propulsion->in.PQR = Propagate->GetPQR();
435 Aerodynamics->in.Alpha = Auxiliary->Getalpha();
436 Aerodynamics->in.Beta = Auxiliary->Getbeta();
437 Aerodynamics->in.Qbar = Auxiliary->Getqbar();
438 Aerodynamics->in.Vt = Auxiliary->GetVt();
439 Aerodynamics->in.Tb2w = Auxiliary->GetTb2w();
440 Aerodynamics->in.Tw2b = Auxiliary->GetTw2b();
441 Aerodynamics->in.RPBody = MassBalance->StructuralToBody(Aircraft->GetXYZrp());
443 case eGroundReactions:
444 // There are no external inputs to this model.
445 GroundReactions->in.Vground = Auxiliary->GetVground();
446 GroundReactions->in.VcalibratedKts = Auxiliary->GetVcalibratedKTS();
447 GroundReactions->in.Temperature = Atmosphere->GetTemperature();
448 GroundReactions->in.TakeoffThrottle = (FCS->GetThrottlePos().size() > 0) ? (FCS->GetThrottlePos(0) > 0.90) : false;
449 GroundReactions->in.SteerPosDeg = FCS->GetSteerPosDeg();
450 GroundReactions->in.BrakePos = FCS->GetBrakePos();
451 GroundReactions->in.FCSGearPos = FCS->GetGearPos();
452 GroundReactions->in.EmptyWeight = MassBalance->GetEmptyWeight();
453 GroundReactions->in.Tb2l = Propagate->GetTb2l();
454 GroundReactions->in.Tec2l = Propagate->GetTec2l();
455 GroundReactions->in.Tec2b = Propagate->GetTec2b();
456 GroundReactions->in.PQR = Propagate->GetPQR();
457 GroundReactions->in.UVW = Propagate->GetUVW();
458 GroundReactions->in.DistanceAGL = Propagate->GetDistanceAGL();
459 GroundReactions->in.DistanceASL = Propagate->GetAltitudeASL();
460 GroundReactions->in.TotalDeltaT = dT * GroundReactions->GetRate();
461 GroundReactions->in.WOW = GroundReactions->GetWOW();
462 GroundReactions->in.Location = Propagate->GetLocation();
463 GroundReactions->in.vXYZcg = MassBalance->GetXYZcg();
465 case eExternalReactions:
466 // There are no external inputs to this model.
469 BuoyantForces->in.Density = Atmosphere->GetDensity();
470 BuoyantForces->in.Pressure = Atmosphere->GetPressure();
471 BuoyantForces->in.Temperature = Atmosphere->GetTemperature();
472 BuoyantForces->in.gravity = Inertial->gravity();
475 MassBalance->in.GasInertia = BuoyantForces->GetGasMassInertia();
476 MassBalance->in.GasMass = BuoyantForces->GetGasMass();
477 MassBalance->in.GasMoment = BuoyantForces->GetGasMassMoment();
478 MassBalance->in.TanksWeight = Propulsion->GetTanksWeight();
479 MassBalance->in.TanksMoment = Propulsion->GetTanksMoment();
480 MassBalance->in.TankInertia = Propulsion->CalculateTankInertias();
483 Aircraft->in.AeroForce = Aerodynamics->GetForces();
484 Aircraft->in.PropForce = Propulsion->GetForces();
485 Aircraft->in.GroundForce = GroundReactions->GetForces();
486 Aircraft->in.ExternalForce = ExternalReactions->GetForces();
487 Aircraft->in.BuoyantForce = BuoyantForces->GetForces();
488 Aircraft->in.AeroMoment = Aerodynamics->GetMoments();
489 Aircraft->in.PropMoment = Propulsion->GetMoments();
490 Aircraft->in.GroundMoment = GroundReactions->GetMoments();
491 Aircraft->in.ExternalMoment = ExternalReactions->GetMoments();
492 Aircraft->in.BuoyantMoment = BuoyantForces->GetMoments();
495 Accelerations->in.J = MassBalance->GetJ();
496 Accelerations->in.Jinv = MassBalance->GetJinv();
497 Accelerations->in.Ti2b = Propagate->GetTi2b();
498 Accelerations->in.Tb2i = Propagate->GetTb2i();
499 Accelerations->in.Tec2b = Propagate->GetTec2b();
500 Accelerations->in.Tec2i = Propagate->GetTec2i();
501 Accelerations->in.qAttitudeECI = Propagate->GetQuaternionECI();
502 Accelerations->in.Moment = Aircraft->GetMoments();
503 Accelerations->in.GroundMoment = GroundReactions->GetMoments();
504 Accelerations->in.Force = Aircraft->GetForces();
505 Accelerations->in.GroundForce = GroundReactions->GetForces();
506 Accelerations->in.GAccel = Inertial->GetGAccel(Propagate->GetRadius());
507 Accelerations->in.J2Grav = Inertial->GetGravityJ2(Propagate->GetLocation());
508 Accelerations->in.vPQRi = Propagate->GetPQRi();
509 Accelerations->in.vPQR = Propagate->GetPQR();
510 Accelerations->in.vUVW = Propagate->GetUVW();
511 Accelerations->in.vInertialPosition = Propagate->GetInertialPosition();
512 Accelerations->in.DeltaT = dT;
513 Accelerations->in.Mass = MassBalance->GetMass();
514 Accelerations->in.MultipliersList = GroundReactions->GetMultipliersList();
515 Accelerations->in.TerrainVelocity = Propagate->GetTerrainVelocity();
516 Accelerations->in.TerrainAngularVel = Propagate->GetTerrainAngularVelocity();
523 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 void FGFDMExec::LoadPlanetConstants(void)
527 Propagate->in.vOmegaPlanet = Inertial->GetOmegaPlanet();
528 Accelerations->in.vOmegaPlanet = Inertial->GetOmegaPlanet();
529 Propagate->in.SemiMajor = Inertial->GetSemimajor();
530 Propagate->in.SemiMinor = Inertial->GetSemiminor();
531 Auxiliary->in.SLGravity = Inertial->SLgravity();
534 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 void FGFDMExec::LoadModelConstants(void)
538 Winds->in.wingspan = Aircraft->GetWingSpan();
539 Aerodynamics->in.Wingarea = Aircraft->GetWingArea();
540 Aerodynamics->in.Wingchord = Aircraft->Getcbar();
541 Aerodynamics->in.Wingincidence = Aircraft->GetWingIncidence();
542 Aerodynamics->in.Wingspan = Aircraft->GetWingSpan();
543 Auxiliary->in.Wingspan = Aircraft->GetWingSpan();
544 Auxiliary->in.Wingchord = Aircraft->Getcbar();
545 GroundReactions->in.vXYZcg = MassBalance->GetXYZcg();
547 LoadPlanetConstants();
550 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 // This call will cause the sim time to reset to 0.0
553 bool FGFDMExec::RunIC(void)
555 FGPropulsion* propulsion = (FGPropulsion*)Models[ePropulsion];
557 SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0.
560 Models[eInput]->InitModel();
561 Models[eOutput]->InitModel();
564 ResumeIntegration(); // Restores the integration rate to what it was.
567 MassBalance->GetMassPropertiesReport(0);
569 cout << endl << fgblue << highint
570 << "End of vehicle configuration loading." << endl
571 << "-------------------------------------------------------------------------------"
572 << reset << std::setprecision(6) << endl;
575 for (unsigned int n=0; n < propulsion->GetNumEngines(); ++n) {
576 if (IC->IsEngineRunning(n)) {
578 propulsion->InitRunning(n);
579 } catch (const string& str) {
589 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591 void FGFDMExec::Initialize(FGInitialCondition* FGIC)
593 Propagate->SetInitialState(FGIC);
594 Winds->SetWindNED(FGIC->GetWindNEDFpsIC());
596 Propagate->InitializeDerivatives();
599 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601 void FGFDMExec::ResetToInitialConditions(int mode)
603 if (Constructing) return;
605 if (mode == 1) Output->SetStartNewOutput();
607 for (unsigned int i = 0; i < Models.size(); i++) {
608 // The Input/Output models will be initialized during the RunIC() execution
609 if (i == eInput || i == eOutput) continue;
612 Models[i]->InitModel();
618 Script->ResetEvents();
623 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625 vector <string> FGFDMExec::EnumerateFDMs(void)
627 vector <string> FDMList;
628 FGAircraft* Aircraft = (FGAircraft*)Models[eAircraft];
630 FDMList.push_back(Aircraft->GetAircraftName());
632 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
633 FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
639 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 bool FGFDMExec::LoadScript(const string& script, double deltaT, const string& initfile)
645 Script = new FGScript(this);
646 result = Script->LoadScript(RootDir + script, deltaT, initfile);
651 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653 bool FGFDMExec::LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
654 const string& model, bool addModelToPath)
656 FGFDMExec::AircraftPath = RootDir + AircraftPath;
657 FGFDMExec::EnginePath = RootDir + EnginePath;
658 FGFDMExec::SystemsPath = RootDir + SystemsPath;
660 return LoadModel(model, addModelToPath);
663 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665 bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
667 string aircraftCfgFileName;
668 bool result = false; // initialize result to false, indicating input file not yet read
670 modelName = model; // Set the class modelName attribute
672 if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
673 cerr << "Error: attempted to load aircraft with undefined ";
674 cerr << "aircraft, engine, and system paths" << endl;
678 FullAircraftPath = AircraftPath;
679 if (addModelToPath) FullAircraftPath += "/" + model;
680 aircraftCfgFileName = FullAircraftPath + "/" + model + ".xml";
687 int saved_debug_lvl = debug_lvl;
688 FGXMLFileRead XMLFileRead;
689 Element *document = XMLFileRead.LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
692 if (IsChild) debug_lvl = 0;
694 ReadPrologue(document);
696 if (IsChild) debug_lvl = saved_debug_lvl;
698 // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
699 Element* element = document->FindElement("fileheader");
701 result = ReadFileHeader(element);
703 cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
708 if (IsChild) debug_lvl = 0;
710 // Process the metrics element. This element is REQUIRED.
711 element = document->FindElement("metrics");
713 result = ((FGAircraft*)Models[eAircraft])->Load(element);
715 cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
719 cerr << endl << "No metrics element was found in the aircraft config file." << endl;
723 // Process the mass_balance element. This element is REQUIRED.
724 element = document->FindElement("mass_balance");
726 result = ((FGMassBalance*)Models[eMassBalance])->Load(element);
728 cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
732 cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
736 // Process the ground_reactions element. This element is REQUIRED.
737 element = document->FindElement("ground_reactions");
739 result = ((FGGroundReactions*)Models[eGroundReactions])->Load(element);
741 cerr << endl << "Aircraft ground_reactions element has problems in file " << aircraftCfgFileName << endl;
744 ((FGFCS*)Models[eSystems])->AddGear(((FGGroundReactions*)Models[eGroundReactions])->GetNumGearUnits());
746 cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
750 // Process the external_reactions element. This element is OPTIONAL.
751 element = document->FindElement("external_reactions");
753 result = ((FGExternalReactions*)Models[eExternalReactions])->Load(element);
755 cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
760 // Process the buoyant_forces element. This element is OPTIONAL.
761 element = document->FindElement("buoyant_forces");
763 result = ((FGBuoyantForces*)Models[eBuoyantForces])->Load(element);
765 cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
770 // Process the propulsion element. This element is OPTIONAL.
771 element = document->FindElement("propulsion");
773 result = ((FGPropulsion*)Models[ePropulsion])->Load(element);
775 cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
778 for (unsigned int i=0; i<((FGPropulsion*)Models[ePropulsion])->GetNumEngines(); i++)
779 ((FGFCS*)Models[eSystems])->AddThrottle();
782 // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
783 element = document->FindElement("system");
785 result = ((FGFCS*)Models[eSystems])->Load(element);
787 cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
790 element = document->FindNextElement("system");
793 // Process the autopilot element. This element is OPTIONAL.
794 element = document->FindElement("autopilot");
796 result = ((FGFCS*)Models[eSystems])->Load(element);
798 cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
803 // Process the flight_control element. This element is OPTIONAL.
804 element = document->FindElement("flight_control");
806 result = ((FGFCS*)Models[eSystems])->Load(element);
808 cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
813 // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
814 element = document->FindElement("aerodynamics");
816 result = ((FGAerodynamics*)Models[eAerodynamics])->Load(element);
818 cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
822 cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
825 // Process the input element. This element is OPTIONAL, and there may be more than one.
826 element = document->FindElement("input");
828 if (!static_cast<FGInput*>(Models[eInput])->Load(element))
831 element = document->FindNextElement("input");
834 // Process the output element[s]. This element is OPTIONAL, and there may be
836 element = document->FindElement("output");
838 if (!static_cast<FGOutput*>(Models[eOutput])->Load(element))
841 element = document->FindNextElement("output");
844 // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
845 element = document->FindElement("child");
847 result = ReadChild(element);
849 cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
854 // Since all vehicle characteristics have been loaded, place the values in the Inputs
855 // structure for the FGModel-derived classes.
856 LoadModelConstants();
860 if (IsChild) debug_lvl = saved_debug_lvl;
864 << " JSBSim failed to open the configuration file: " << aircraftCfgFileName
868 for (unsigned int i=0; i< Models.size(); i++) LoadInputs(i);
871 struct PropertyCatalogStructure masterPCS;
872 masterPCS.base_string = "";
873 masterPCS.node = Root->GetNode();
874 BuildPropertyCatalog(&masterPCS);
880 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
882 string FGFDMExec::GetPropulsionTankReport()
884 return ((FGPropulsion*)Models[ePropulsion])->GetPropulsionTankReport();
887 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
891 struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
893 for (int i=0; i<pcs->node->nChildren(); i++) {
895 pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
896 int node_idx = pcs->node->getChild(i)->getIndex();
898 pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx);
900 if (pcs->node->getChild(i)->nChildren() == 0) {
901 if (pcsNew->base_string.substr(0,12) == string("/fdm/jsbsim/")) {
902 pcsNew->base_string = pcsNew->base_string.erase(0,12);
904 if (pcs->node->getChild(i)->getAttribute(SGPropertyNode::READ)) access="R";
905 if (pcs->node->getChild(i)->getAttribute(SGPropertyNode::WRITE)) access+="W";
906 PropertyCatalog.push_back(pcsNew->base_string+" ("+access+")");
908 pcsNew->node = (FGPropertyNode*)pcs->node->getChild(i);
909 BuildPropertyCatalog(pcsNew);
915 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917 string FGFDMExec::QueryPropertyCatalog(const string& in)
920 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
921 if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
923 if (results.empty()) return "No matches found\n";
927 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
929 void FGFDMExec::PrintPropertyCatalog(void)
932 cout << " " << fgblue << highint << underon << "Property Catalog for "
933 << modelName << reset << endl << endl;
934 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
935 cout << " " << PropertyCatalog[i] << endl;
939 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941 void FGFDMExec::PrintSimulationConfiguration(void) const
943 cout << endl << "Simulation Configuration" << endl << "------------------------" << endl;
944 cout << MassBalance->Name << endl;
945 cout << GroundReactions->Name << endl;
946 cout << Aerodynamics->Name << endl;
947 cout << Propulsion->Name << endl;
950 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
952 bool FGFDMExec::ReadFileHeader(Element* el)
954 bool result = true; // true for success
956 if (debug_lvl == 0) return result;
959 cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
962 if (el->FindElement("description"))
963 cout << " Description: " << el->FindElement("description")->GetDataLine() << endl;
964 if (el->FindElement("author"))
965 cout << " Model Author: " << el->FindElement("author")->GetDataLine() << endl;
966 if (el->FindElement("filecreationdate"))
967 cout << " Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
968 if (el->FindElement("version"))
969 cout << " Version: " << el->FindElement("version")->GetDataLine() << endl;
974 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
978 bool result = true; // true for success
980 if (!el) return false;
982 string AircraftName = el->GetAttributeValue("name");
983 ((FGAircraft*)Models[eAircraft])->SetAircraftName(AircraftName);
985 if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
986 << underoff << ": " << highint << AircraftName << normint << endl;
988 CFGVersion = el->GetAttributeValue("version");
989 Release = el->GetAttributeValue("release");
992 cout << " Version: " << highint << CFGVersion
994 if (CFGVersion != needed_cfg_version) {
995 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
996 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
997 cerr << "Current version needed is: " << needed_cfg_version << endl;
998 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
1002 if (Release == "ALPHA" && (debug_lvl & 1)) {
1003 cout << endl << endl
1004 << highint << "This aircraft model is an " << fgred << Release
1005 << reset << highint << " release!!!" << endl << endl << reset
1006 << "This aircraft model may not even properly load, and probably"
1007 << " will not fly as expected." << endl << endl
1008 << fgred << highint << "Use this model for development purposes ONLY!!!"
1009 << normint << reset << endl << endl;
1010 } else if (Release == "BETA" && (debug_lvl & 1)) {
1011 cout << endl << endl
1012 << highint << "This aircraft model is a " << fgred << Release
1013 << reset << highint << " release!!!" << endl << endl << reset
1014 << "This aircraft model probably will not fly as expected." << endl << endl
1015 << fgblue << highint << "Use this model for development purposes ONLY!!!"
1016 << normint << reset << endl << endl;
1017 } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
1018 cout << endl << endl
1019 << highint << "This aircraft model is a " << fgblue << Release
1020 << reset << highint << " release." << endl << endl << reset;
1021 } else if (debug_lvl & 1) {
1022 cout << endl << endl
1023 << highint << "This aircraft model is an " << fgred << Release
1024 << reset << highint << " release!!!" << endl << endl << reset
1025 << "This aircraft model may not even properly load, and probably"
1026 << " will not fly as expected." << endl << endl
1027 << fgred << highint << "Use this model for development purposes ONLY!!!"
1028 << normint << reset << endl << endl;
1034 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1036 bool FGFDMExec::ReadChild(Element* el)
1038 // Add a new childData object to the child FDM list
1039 // Populate that childData element with a new FDMExec object
1040 // Set the IsChild flag for that FDMExec object
1041 // Get the aircraft name
1042 // set debug level to print out no additional data for child objects
1043 // Load the model given the aircraft name
1044 // reset debug level to prior setting
1046 struct childData* child = new childData;
1048 child->exec = new FGFDMExec(Root, FDMctr);
1049 child->exec->SetChild(true);
1051 string childAircraft = el->GetAttributeValue("name");
1052 string sMated = el->GetAttributeValue("mated");
1053 if (sMated == "false") child->mated = false; // child objects are mated by default.
1054 string sInternal = el->GetAttributeValue("internal");
1055 if (sInternal == "true") child->internal = true; // child objects are external by default.
1057 child->exec->SetAircraftPath( AircraftPath );
1058 child->exec->SetEnginePath( EnginePath );
1059 child->exec->SetSystemsPath( SystemsPath );
1060 child->exec->LoadModel(childAircraft);
1062 Element* location = el->FindElement("location");
1064 child->Loc = location->FindElementTripletConvertTo("IN");
1066 cerr << endl << highint << fgred << " No location was found for this child object!" << reset << endl;
1070 Element* orientation = el->FindElement("orient");
1072 child->Orient = orientation->FindElementTripletConvertTo("RAD");
1073 } else if (debug_lvl > 0) {
1074 cerr << endl << highint << " No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
1077 ChildFDMList.push_back(child);
1082 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
1089 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091 FGTrim* FGFDMExec::GetTrim(void)
1094 Trim = new FGTrim(this,tNone);
1098 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1100 void FGFDMExec::CheckIncrementalHold(void)
1102 // Only check if increment then hold is on
1103 if( IncrementThenHolding ) {
1105 if (TimeStepsUntilHold == 0) {
1107 // Should hold simulation if TimeStepsUntilHold has reached zero
1110 // Still need to decrement TimeStepsUntilHold as value of -1
1111 // indicates that incremental then hold is turned off
1112 IncrementThenHolding = false;
1113 TimeStepsUntilHold--;
1115 } else if ( TimeStepsUntilHold > 0 ) {
1116 // Keep decrementing until 0 is reached
1117 TimeStepsUntilHold--;
1122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1124 void FGFDMExec::DoTrim(int mode)
1126 if (Constructing) return;
1128 if (mode < 0 || mode > JSBSim::tNone)
1129 throw("Illegal trimming mode!");
1131 FGTrim trim(this, (JSBSim::TrimMode)mode);
1132 bool success = trim.DoTrim();
1136 throw("Trim Failed");
1141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1143 void FGFDMExec::SRand(int sr)
1146 gaussian_random_number_phase = 0;
1150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1151 // The bitmasked value choices are as follows:
1152 // unset: In this case (the default) JSBSim would only print
1153 // out the normally expected messages, essentially echoing
1154 // the config files as they are read. If the environment
1155 // variable is not set, debug_lvl is set to 1 internally
1156 // 0: This requests JSBSim not to output any messages
1158 // 1: This value explicity requests the normal JSBSim
1160 // 2: This value asks for a message to be printed out when
1161 // a class is instantiated
1162 // 4: When this value is set, a message is displayed when a
1163 // FGModel object executes its Run() method
1164 // 8: When this value is set, various runtime state variables
1165 // are printed out periodically
1166 // 16: When set various parameters are sanity checked and
1167 // a message is printed out when they go out of bounds
1169 void FGFDMExec::Debug(int from)
1171 if (debug_lvl <= 0) return;
1173 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1174 if (from == 0) { // Constructor
1176 << "JSBSim Flight Dynamics Model v" << JSBSim_version << endl;
1177 cout << " [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1178 cout << "JSBSim startup beginning ...\n\n";
1179 if (disperse == 1) cout << "Dispersions are ON." << endl << endl;
1180 } else if (from == 3) {
1181 cout << "\n\nJSBSim startup complete\n\n";
1184 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1185 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1186 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
1188 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1190 cout << "================== Frame: " << Frame << " Time: "
1191 << sim_time << " dt: " << dT << endl;
1194 if (debug_lvl & 8 ) { // Runtime state variables
1196 if (debug_lvl & 16) { // Sanity checking
1198 if (debug_lvl & 64) {
1199 if (from == 0) { // Constructor
1200 cout << IdSrc << endl;
1201 cout << IdHdr << endl;