1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Schedules and runs the model routines.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 You should have received a copy of the GNU Lesser General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU Lesser General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
30 This class wraps up the simulation scheduling routines.
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES, and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48 #include "FGFDMExec.h"
49 #include "models/atmosphere/FGStandardAtmosphere.h"
50 #include "models/atmosphere/FGWinds.h"
51 #include "models/FGFCS.h"
52 #include "models/FGPropulsion.h"
53 #include "models/FGMassBalance.h"
54 #include "models/FGGroundReactions.h"
55 #include "models/FGExternalReactions.h"
56 #include "models/FGBuoyantForces.h"
57 #include "models/FGAerodynamics.h"
58 #include "models/FGInertial.h"
59 #include "models/FGAircraft.h"
60 #include "models/FGAccelerations.h"
61 #include "models/FGPropagate.h"
62 #include "models/FGAuxiliary.h"
63 #include "models/FGInput.h"
64 #include "models/FGOutput.h"
65 #include "initialization/FGInitialCondition.h"
66 #include "input_output/FGPropertyManager.h"
67 #include "input_output/FGScript.h"
73 static const char *IdSrc = "$Id: FGFDMExec.cpp,v 1.120 2011/11/10 12:06:13 jberndt Exp $";
74 static const char *IdHdr = ID_FDMEXEC;
76 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
80 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) : Root(root), FDMctr(fdmctr)
87 SetGroundCallback(new FGDefaultGroundCallback());
102 dT = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is
103 // run in standalone mode with no initialization file.
105 AircraftPath = "aircraft";
106 EnginePath = "engine";
107 SystemsPath = "systems";
110 char* num = getenv("JSBSIM_DEBUG");
111 if (num) debug_lvl = atoi(num); // set debug level
112 } catch (...) { // if error set to 1
116 if (Root == 0) { // Then this is the root FDM
117 Root = new FGPropertyManager; // Create the property manager
122 FDMctr = new unsigned int; // Create and initialize the child FDM counter
126 // Store this FDM's ID
127 IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth"
129 // Prepare FDMctr for the next child FDM id
130 (*FDMctr)++; // instance. "child" instances are loaded last.
132 instance = Root->GetNode("/fdm/jsbsim",IdFDM,true);
134 // this is to catch errors in binding member functions to the property tree.
137 } catch ( string msg ) {
138 cout << "Caught error: " << msg << endl;
146 typedef int (FGFDMExec::*iPMF)(void) const;
147 // instance->Tie("simulation/do_trim_analysis", this, (iPMF)0, &FGFDMExec::DoTrimAnalysis, false);
148 instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim, false);
149 instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions, false);
150 instance->Tie("simulation/terminate", (int *)&Terminate);
151 instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime);
152 instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel);
153 instance->Tie("simulation/frame", (int *)&Frame, false);
155 Constructing = false;
158 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 FGFDMExec::~FGFDMExec()
166 if (IdFDM == 0) { // Meaning this is no child FDM
177 } catch ( string msg ) {
178 cout << "Caught error: " << msg << endl;
181 for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
182 ChildFDMList.clear();
184 PropertyCatalog.clear();
186 if (FDMctr > 0) (*FDMctr)--;
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 bool FGFDMExec::Allocate(void)
197 Models.resize(eNumStandardModels);
199 // See the eModels enum specification in the header file. The order of the enums
200 // specifies the order of execution. The Models[] vector is the primary
201 // storage array for the list of models.
202 Models[ePropagate] = new FGPropagate(this);
203 Models[eInput] = new FGInput(this);
204 Models[eInertial] = new FGInertial(this);
205 Models[eAtmosphere] = new FGStandardAtmosphere(this);
206 Models[eWinds] = new FGWinds(this);
207 Models[eAuxiliary] = new FGAuxiliary(this);
208 Models[eSystems] = new FGFCS(this);
209 Models[ePropulsion] = new FGPropulsion(this);
210 Models[eAerodynamics] = new FGAerodynamics (this);
212 GetGroundCallback()->SetSeaLevelRadius(((FGInertial*)Models[eInertial])->GetRefRadius());
214 Models[eGroundReactions] = new FGGroundReactions(this);
215 Models[eExternalReactions] = new FGExternalReactions(this);
216 Models[eBuoyantForces] = new FGBuoyantForces(this);
217 Models[eMassBalance] = new FGMassBalance(this);
218 Models[eAircraft] = new FGAircraft(this);
219 Models[eAccelerations] = new FGAccelerations(this);
221 // Assign the Model shortcuts for internal executive use only.
222 Propagate = (FGPropagate*)Models[ePropagate];
223 Inertial = (FGInertial*)Models[eInertial];
224 Atmosphere = (FGAtmosphere*)Models[eAtmosphere];
225 Winds = (FGWinds*)Models[eWinds];
226 Auxiliary = (FGAuxiliary*)Models[eAuxiliary];
227 FCS = (FGFCS*)Models[eSystems];
228 Propulsion = (FGPropulsion*)Models[ePropulsion];
229 Aerodynamics = (FGAerodynamics*)Models[eAerodynamics];
230 GroundReactions = (FGGroundReactions*)Models[eGroundReactions];
231 ExternalReactions = (FGExternalReactions*)Models[eExternalReactions];
232 BuoyantForces = (FGBuoyantForces*)Models[eBuoyantForces];
233 MassBalance = (FGMassBalance*)Models[eMassBalance];
234 Aircraft = (FGAircraft*)Models[eAircraft];
235 Accelerations = (FGAccelerations*)Models[eAccelerations];
237 // Initialize planet (environment) constants
238 LoadPlanetConstants();
241 for (unsigned int i = 0; i < Models.size(); i++) {
243 Models[i]->InitModel();
246 IC = new FGInitialCondition(this);
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 bool FGFDMExec::DeAllocate(void)
258 for (unsigned int i=0; i<eNumStandardModels; i++) delete Models[i];
261 for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 void FGFDMExec::Schedule(FGModel* model, int rate)
278 model->SetRate(rate);
279 Models.push_back(model);
282 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 bool FGFDMExec::Run(void)
290 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
291 ChildFDMList[i]->AssignState( (FGPropagate*)Models[ePropagate] ); // Transfer state to the child FDM
292 ChildFDMList[i]->Run();
295 if (firstPass && !IntegrationSuspended()) {
296 // Outputs the initial conditions
297 for (unsigned int i = 0; i < Outputs.size(); i++)
298 Outputs[i]->Run(holding);
305 // returns true if success, false if complete
306 if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript();
308 for (unsigned int i = 0; i < Models.size(); i++) {
310 Models[i]->Run(holding);
313 if (Terminate) success = false;
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 void FGFDMExec::LoadInputs(unsigned int idx)
324 Propagate->in.vPQRidot = Accelerations->GetPQRidot();
325 Propagate->in.vQtrndot = Accelerations->GetQuaterniondot();
326 Propagate->in.vUVWidot = Accelerations->GetUVWidot();
327 Propagate->in.DeltaT = dT;
332 Inertial->in.Radius = Propagate->GetRadius();
333 Inertial->in.Latitude = Propagate->GetLatitude();
336 Atmosphere->in.altitudeASL = Propagate->GetAltitudeASL();
339 Winds->in.AltitudeASL = Propagate->GetAltitudeASL();
340 Winds->in.DistanceAGL = Propagate->GetDistanceAGL();
341 Winds->in.Tl2b = Propagate->GetTl2b();
342 Winds->in.Tw2b = Auxiliary->GetTw2b();
343 Winds->in.V = Auxiliary->GetVt();
344 Winds->in.totalDeltaT = dT * Winds->GetRate();
347 Auxiliary->in.Pressure = Atmosphere->GetPressure();
348 Auxiliary->in.Density = Atmosphere->GetDensity();
349 Auxiliary->in.DensitySL = Atmosphere->GetDensitySL();
350 Auxiliary->in.PressureSL = Atmosphere->GetPressureSL();
351 Auxiliary->in.Temperature = Atmosphere->GetTemperature();
352 Auxiliary->in.SoundSpeed = Atmosphere->GetSoundSpeed();
353 Auxiliary->in.KinematicViscosity = Atmosphere->GetKinematicViscosity();
354 Auxiliary->in.DistanceAGL = Propagate->GetDistanceAGL();
355 Auxiliary->in.Mass = MassBalance->GetMass();
356 Auxiliary->in.Tl2b = Propagate->GetTl2b();
357 Auxiliary->in.Tb2l = Propagate->GetTb2l();
358 Auxiliary->in.vPQR = Propagate->GetPQR();
359 Auxiliary->in.vPQRdot = Accelerations->GetPQRdot();
360 Auxiliary->in.vUVW = Propagate->GetUVW();
361 Auxiliary->in.vUVWdot = Accelerations->GetUVWdot();
362 Auxiliary->in.vVel = Propagate->GetVel();
363 Auxiliary->in.vBodyAccel = Accelerations->GetBodyAccel();
364 Auxiliary->in.ToEyePt = MassBalance->StructuralToBody(Aircraft->GetXYZep());
365 Auxiliary->in.VRPBody = MassBalance->StructuralToBody(Aircraft->GetXYZvrp());
366 Auxiliary->in.RPBody = MassBalance->StructuralToBody(Aircraft->GetXYZrp());
367 Auxiliary->in.vFw = Aerodynamics->GetvFw();
368 Auxiliary->in.vLocation = Propagate->GetLocation();
369 Auxiliary->in.Latitude = Propagate->GetLatitude();
370 Auxiliary->in.Longitude = Propagate->GetLongitude();
371 Auxiliary->in.CosTht = Propagate->GetCosEuler(eTht);
372 Auxiliary->in.SinTht = Propagate->GetSinEuler(eTht);
373 Auxiliary->in.CosPhi = Propagate->GetCosEuler(ePhi);
374 Auxiliary->in.SinPhi = Propagate->GetSinEuler(ePhi);
375 Auxiliary->in.Psi = Propagate->GetEuler(ePsi);
376 Auxiliary->in.TotalWindNED = Winds->GetTotalWindNED();
377 Auxiliary->in.TurbPQR = Winds->GetTurbPQR();
378 Auxiliary->in.WindPsi = Winds->GetWindPsi();
379 Auxiliary->in.Vwind = Winds->GetTotalWindNED().Magnitude();
382 // Dynamic inputs come into the components that FCS manages through properties
385 Propulsion->in.SLPressure = Atmosphere->GetPressureSL();
386 Propulsion->in.Pressure = Atmosphere->GetPressure();
387 Propulsion->in.PressureRatio = Atmosphere->GetPressureRatio();
388 Propulsion->in.Temperature = Atmosphere->GetTemperature();
389 Propulsion->in.DensityRatio = Atmosphere->GetDensityRatio();
390 Propulsion->in.Density = Atmosphere->GetDensity();
391 Propulsion->in.Soundspeed = Atmosphere->GetSoundSpeed();
392 Propulsion->in.TotalPressure = Auxiliary->GetTotalPressure();
393 Propulsion->in.TotalTempearture = Auxiliary->GetTotalTemperature();
394 Propulsion->in.Vc = Auxiliary->GetVcalibratedKTS();
395 Propulsion->in.Vt = Auxiliary->GetVt();
396 Propulsion->in.qbar = Auxiliary->Getqbar();
397 Propulsion->in.TAT_c = Auxiliary->GetTAT_C();
398 Propulsion->in.AeroUVW = Auxiliary->GetAeroUVW();
399 Propulsion->in.AeroPQR = Auxiliary->GetAeroPQR();
400 Propulsion->in.alpha = Auxiliary->Getalpha();
401 Propulsion->in.beta = Auxiliary->Getbeta();
402 Propulsion->in.TotalDeltaT = dT * Propulsion->GetRate();
403 Propulsion->in.ThrottlePos = FCS->GetThrottlePos();
404 Propulsion->in.MixturePos = FCS->GetMixturePos();
405 Propulsion->in.ThrottleCmd = FCS->GetThrottleCmd();
406 Propulsion->in.MixtureCmd = FCS->GetMixtureCmd();
407 Propulsion->in.PropAdvance = FCS->GetPropAdvance();
408 Propulsion->in.PropFeather = FCS->GetPropFeather();
409 Propulsion->in.H_agl = Propagate->GetDistanceAGL();
410 Propulsion->in.PQR = Propagate->GetPQR();
413 Aerodynamics->in.Alpha = Auxiliary->Getalpha();
414 Aerodynamics->in.Beta = Auxiliary->Getbeta();
415 Aerodynamics->in.Qbar = Auxiliary->Getqbar();
416 Aerodynamics->in.Vt = Auxiliary->GetVt();
417 Aerodynamics->in.Tb2w = Auxiliary->GetTb2w();
418 Aerodynamics->in.Tw2b = Auxiliary->GetTw2b();
419 Aerodynamics->in.RPBody = MassBalance->StructuralToBody(Aircraft->GetXYZrp());
421 case eGroundReactions:
422 // There are no external inputs to this model.
423 GroundReactions->in.Vground = Auxiliary->GetVground();
424 GroundReactions->in.VcalibratedKts = Auxiliary->GetVcalibratedKTS();
425 GroundReactions->in.Temperature = Atmosphere->GetTemperature();
426 GroundReactions->in.TakeoffThrottle = (FCS->GetThrottlePos().size() > 0) ? (FCS->GetThrottlePos(0) > 0.90) : false;
427 GroundReactions->in.SteerPosDeg = FCS->GetSteerPosDeg();
428 GroundReactions->in.BrakePos = FCS->GetBrakePos();
429 GroundReactions->in.FCSGearPos = FCS->GetGearPos();
430 GroundReactions->in.EmptyWeight = MassBalance->GetEmptyWeight();
431 GroundReactions->in.Tb2l = Propagate->GetTb2l();
432 GroundReactions->in.Tec2l = Propagate->GetTec2l();
433 GroundReactions->in.Tec2b = Propagate->GetTec2b();
434 GroundReactions->in.PQR = Propagate->GetPQR();
435 GroundReactions->in.UVW = Propagate->GetUVW();
436 GroundReactions->in.DistanceAGL = Propagate->GetDistanceAGL();
437 GroundReactions->in.DistanceASL = Propagate->GetAltitudeASL();
438 GroundReactions->in.TotalDeltaT = dT * GroundReactions->GetRate();
439 GroundReactions->in.WOW = GroundReactions->GetWOW();
440 GroundReactions->in.Location = Propagate->GetLocation();
441 for (int i=0; i<GroundReactions->GetNumGearUnits(); i++) {
442 GroundReactions->in.vWhlBodyVec[i] = MassBalance->StructuralToBody(GroundReactions->GetGearUnit(i)->GetLocation());
445 case eExternalReactions:
446 // There are no external inputs to this model.
449 BuoyantForces->in.Density = Atmosphere->GetDensity();
450 BuoyantForces->in.Pressure = Atmosphere->GetPressure();
451 BuoyantForces->in.Temperature = Atmosphere->GetTemperature();
452 BuoyantForces->in.gravity = Inertial->gravity();
455 MassBalance->in.GasInertia = BuoyantForces->GetGasMassInertia();
456 MassBalance->in.GasMass = BuoyantForces->GetGasMass();
457 MassBalance->in.GasMoment = BuoyantForces->GetGasMassMoment();
458 MassBalance->in.TanksWeight = Propulsion->GetTanksWeight();
459 MassBalance->in.TanksMoment = Propulsion->GetTanksMoment();
460 MassBalance->in.TankInertia = Propulsion->CalculateTankInertias();
463 Aircraft->in.AeroForce = Aerodynamics->GetForces();
464 Aircraft->in.PropForce = Propulsion->GetForces();
465 Aircraft->in.GroundForce = GroundReactions->GetForces();
466 Aircraft->in.ExternalForce = ExternalReactions->GetForces();
467 Aircraft->in.BuoyantForce = BuoyantForces->GetForces();
468 Aircraft->in.AeroMoment = Aerodynamics->GetMoments();
469 Aircraft->in.PropMoment = Propulsion->GetMoments();
470 Aircraft->in.GroundMoment = GroundReactions->GetMoments();
471 Aircraft->in.ExternalMoment = ExternalReactions->GetMoments();
472 Aircraft->in.BuoyantMoment = BuoyantForces->GetMoments();
473 Aircraft->in.Weight = MassBalance->GetWeight();
474 Aircraft->in.Tl2b = Propagate->GetTl2b();
477 Accelerations->in.J = MassBalance->GetJ();
478 Accelerations->in.Jinv = MassBalance->GetJinv();
479 Accelerations->in.Ti2b = Propagate->GetTi2b();
480 Accelerations->in.Tb2i = Propagate->GetTb2i();
481 Accelerations->in.Tec2b = Propagate->GetTec2b();
482 Accelerations->in.Tl2b = Propagate->GetTl2b();
483 Accelerations->in.qAttitudeECI = Propagate->GetQuaternionECI();
484 Accelerations->in.Moment = Aircraft->GetMoments();
485 Accelerations->in.GroundMoment = GroundReactions->GetMoments();
486 Accelerations->in.Force = Aircraft->GetForces();
487 Accelerations->in.GroundForce = GroundReactions->GetForces();
488 Accelerations->in.GAccel = Inertial->GetGAccel(Propagate->GetRadius());
489 Accelerations->in.J2Grav = Inertial->GetGravityJ2(Propagate->GetLocation());
490 Accelerations->in.vPQRi = Propagate->GetPQRi();
491 Accelerations->in.vPQR = Propagate->GetPQR();
492 Accelerations->in.vUVW = Propagate->GetUVW();
493 Accelerations->in.vInertialPosition = Propagate->GetInertialPosition();
494 Accelerations->in.DeltaT = dT;
495 Accelerations->in.Mass = MassBalance->GetMass();
496 Accelerations->in.MultipliersList = GroundReactions->GetMultipliersList();
497 Accelerations->in.TerrainVelocity = Propagate->GetTerrainVelocity();
498 Accelerations->in.TerrainAngularVel = Propagate->GetTerrainAngularVelocity();
505 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507 void FGFDMExec::LoadPlanetConstants(void)
509 Propagate->in.vOmegaPlanet = Inertial->GetOmegaPlanet();
510 Accelerations->in.vOmegaPlanet = Inertial->GetOmegaPlanet();
511 Propagate->in.RefRadius = Inertial->GetRefRadius();
512 Propagate->in.SemiMajor = Inertial->GetSemimajor();
513 Propagate->in.SemiMinor = Inertial->GetSemiminor();
514 Auxiliary->in.SLGravity = Inertial->SLgravity();
515 Auxiliary->in.ReferenceRadius = Inertial->GetRefRadius();
518 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520 void FGFDMExec::LoadModelConstants(void)
522 Winds->in.wingspan = Aircraft->GetWingSpan();
523 FCS->in.NumGear = GroundReactions->GetNumGearUnits();
524 Aerodynamics->in.Wingarea = Aircraft->GetWingArea();
525 Aerodynamics->in.Wingchord = Aircraft->Getcbar();
526 Aerodynamics->in.Wingincidence = Aircraft->GetWingIncidence();
527 Aerodynamics->in.Wingspan = Aircraft->GetWingSpan();
528 Auxiliary->in.Wingspan = Aircraft->GetWingSpan();
529 Auxiliary->in.Wingchord = Aircraft->Getcbar();
530 for (int i=0; i<GroundReactions->GetNumGearUnits(); i++) {
531 GroundReactions->in.vWhlBodyVec[i] = MassBalance->StructuralToBody(GroundReactions->GetGearUnit(i)->GetLocation());
534 LoadPlanetConstants();
537 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538 // This call will cause the sim time to reset to 0.0
540 bool FGFDMExec::RunIC(void)
542 SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0.
545 ResumeIntegration(); // Restores the integration rate to what it was.
550 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552 void FGFDMExec::Initialize(FGInitialCondition *FGIC)
556 Propagate->SetInitialState( FGIC );
557 LoadInputs(eAccelerations);
558 Accelerations->Run(false);
559 LoadInputs(ePropagate);
560 Propagate->InitializeDerivatives();
561 LoadInputs(eAtmosphere);
562 Atmosphere->Run(false);
563 Winds->SetWindNED(FGIC->GetWindNEDFpsIC());
564 Auxiliary->Run(false);
567 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569 // A private, internal function call for Tie-ing to a property, so it needs an
572 void FGFDMExec::ResetToInitialConditions(int mode)
575 for (unsigned int i=0; i<Outputs.size(); i++) {
576 Outputs[i]->SetStartNewFile(true);
580 ResetToInitialConditions();
583 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 void FGFDMExec::ResetToInitialConditions(void)
587 if (Constructing) return;
589 vector <FGModel*>::iterator it;
590 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
593 if (Script) Script->ResetEvents();
596 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 bool FGFDMExec::SetOutputFileName(const string& fname)
600 if (Outputs.size() > 0) Outputs[0]->SetOutputFileName(fname);
605 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 string FGFDMExec::GetOutputFileName(void)
609 if (Outputs.size() > 0) return Outputs[0]->GetOutputFileName();
610 else return string("");
613 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615 vector <string> FGFDMExec::EnumerateFDMs(void)
617 vector <string> FDMList;
618 FGAircraft* Aircraft = (FGAircraft*)Models[eAircraft];
620 FDMList.push_back(Aircraft->GetAircraftName());
622 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
623 FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
629 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 bool FGFDMExec::LoadScript(const string& script, double deltaT)
635 Script = new FGScript(this);
636 result = Script->LoadScript(RootDir + script, deltaT);
641 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 bool FGFDMExec::LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
644 const string& model, bool addModelToPath)
646 FGFDMExec::AircraftPath = RootDir + AircraftPath;
647 FGFDMExec::EnginePath = RootDir + EnginePath;
648 FGFDMExec::SystemsPath = RootDir + SystemsPath;
650 return LoadModel(model, addModelToPath);
653 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
655 bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
658 string aircraftCfgFileName;
659 Element* element = 0L;
660 bool result = false; // initialize result to false, indicating input file not yet read
662 modelName = model; // Set the class modelName attribute
664 if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
665 cerr << "Error: attempted to load aircraft with undefined ";
666 cerr << "aircraft, engine, and system paths" << endl;
670 FullAircraftPath = AircraftPath;
671 if (addModelToPath) FullAircraftPath += "/" + model;
672 aircraftCfgFileName = FullAircraftPath + "/" + model + ".xml";
679 int saved_debug_lvl = debug_lvl;
681 document = LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
683 if (IsChild) debug_lvl = 0;
685 ReadPrologue(document);
687 if (IsChild) debug_lvl = saved_debug_lvl;
689 // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
690 element = document->FindElement("fileheader");
692 result = ReadFileHeader(element);
694 cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
699 if (IsChild) debug_lvl = 0;
701 // Process the metrics element. This element is REQUIRED.
702 element = document->FindElement("metrics");
704 result = ((FGAircraft*)Models[eAircraft])->Load(element);
706 cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
710 cerr << endl << "No metrics element was found in the aircraft config file." << endl;
714 // Process the mass_balance element. This element is REQUIRED.
715 element = document->FindElement("mass_balance");
717 result = ((FGMassBalance*)Models[eMassBalance])->Load(element);
719 cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
723 cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
727 // Process the ground_reactions element. This element is REQUIRED.
728 element = document->FindElement("ground_reactions");
730 result = ((FGGroundReactions*)Models[eGroundReactions])->Load(element);
732 cerr << endl << "Aircraft ground_reactions element has problems in file " << aircraftCfgFileName << endl;
735 ((FGFCS*)Models[eSystems])->AddGear(((FGGroundReactions*)Models[eGroundReactions])->GetNumGearUnits());
737 cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
741 // Process the external_reactions element. This element is OPTIONAL.
742 element = document->FindElement("external_reactions");
744 result = ((FGExternalReactions*)Models[eExternalReactions])->Load(element);
746 cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
751 // Process the buoyant_forces element. This element is OPTIONAL.
752 element = document->FindElement("buoyant_forces");
754 result = ((FGBuoyantForces*)Models[eBuoyantForces])->Load(element);
756 cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
761 // Process the propulsion element. This element is OPTIONAL.
762 element = document->FindElement("propulsion");
764 result = ((FGPropulsion*)Models[ePropulsion])->Load(element);
766 cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
769 for (unsigned int i=0; i<((FGPropulsion*)Models[ePropulsion])->GetNumEngines(); i++)
770 ((FGFCS*)Models[eSystems])->AddThrottle();
773 // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
774 element = document->FindElement("system");
776 result = ((FGFCS*)Models[eSystems])->Load(element, FGFCS::stSystem);
778 cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
781 element = document->FindNextElement("system");
784 // Process the autopilot element. This element is OPTIONAL.
785 element = document->FindElement("autopilot");
787 result = ((FGFCS*)Models[eSystems])->Load(element, FGFCS::stAutoPilot);
789 cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
794 // Process the flight_control element. This element is OPTIONAL.
795 element = document->FindElement("flight_control");
797 result = ((FGFCS*)Models[eSystems])->Load(element, FGFCS::stFCS);
799 cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
804 // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
805 element = document->FindElement("aerodynamics");
807 result = ((FGAerodynamics*)Models[eAerodynamics])->Load(element);
809 cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
813 cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
816 // Process the input element. This element is OPTIONAL.
817 element = document->FindElement("input");
819 result = ((FGInput*)Models[eInput])->Load(element);
821 cerr << endl << "Aircraft input element has problems in file " << aircraftCfgFileName << endl;
826 // Process the output element[s]. This element is OPTIONAL, and there may be more than one.
828 typedef double (FGOutput::*iOPMF)(void) const;
829 typedef int (FGFDMExec::*iOPV)(void) const;
830 element = document->FindElement("output");
832 if (debug_lvl > 0) cout << endl << " Output data set: " << idx << " ";
833 FGOutput* Output = new FGOutput(this);
836 result = Output->Load(element);
838 cerr << endl << "Aircraft output element has problems in file " << aircraftCfgFileName << endl;
841 Outputs.push_back(Output);
842 string outputProp = CreateIndexedPropertyName("simulation/output",idx);
843 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate, false);
844 instance->Tie("simulation/force-output", this, (iOPV)0, &FGFDMExec::ForceOutput, false);
847 element = document->FindNextElement("output");
850 // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
851 element = document->FindElement("child");
853 result = ReadChild(element);
855 cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
860 // Since all vehicle characteristics have been loaded, place the values in the Inputs
861 // structure for the FGModel-derived classes.
862 LoadModelConstants();
867 LoadInputs(eMassBalance); // Update all input mass properties for the report.
868 Models[eMassBalance]->Run(false); // Update all mass properties for the report.
869 ((FGMassBalance*)Models[eMassBalance])->GetMassPropertiesReport();
871 cout << endl << fgblue << highint
872 << "End of vehicle configuration loading." << endl
873 << "-------------------------------------------------------------------------------"
877 if (IsChild) debug_lvl = saved_debug_lvl;
881 << " JSBSim failed to open the configuration file: " << aircraftCfgFileName
885 for (unsigned int i=0; i< Models.size(); i++) LoadInputs(i);
888 struct PropertyCatalogStructure masterPCS;
889 masterPCS.base_string = "";
890 masterPCS.node = (FGPropertyManager*)Root;
891 BuildPropertyCatalog(&masterPCS);
897 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 string FGFDMExec::GetPropulsionTankReport()
901 return ((FGPropulsion*)Models[ePropulsion])->GetPropulsionTankReport();
904 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
906 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
908 struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
911 for (int i=0; i<pcs->node->nChildren(); i++) {
912 pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
913 node_idx = pcs->node->getChild(i)->getIndex();
915 pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx);
917 if (pcs->node->getChild(i)->nChildren() == 0) {
918 if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
919 pcsNew->base_string = pcsNew->base_string.erase(0,12);
921 PropertyCatalog.push_back(pcsNew->base_string);
923 pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
924 BuildPropertyCatalog(pcsNew);
930 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 string FGFDMExec::QueryPropertyCatalog(const string& in)
935 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
936 if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
938 if (results.empty()) return "No matches found\n";
942 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 void FGFDMExec::PrintPropertyCatalog(void)
947 cout << " " << fgblue << highint << underon << "Property Catalog for "
948 << modelName << reset << endl << endl;
949 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
950 cout << " " << PropertyCatalog[i] << endl;
954 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 bool FGFDMExec::ReadFileHeader(Element* el)
958 bool result = true; // true for success
960 if (debug_lvl == 0) return result;
963 cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
966 if (el->FindElement("description"))
967 cout << " Description: " << el->FindElement("description")->GetDataLine() << endl;
968 if (el->FindElement("author"))
969 cout << " Model Author: " << el->FindElement("author")->GetDataLine() << endl;
970 if (el->FindElement("filecreationdate"))
971 cout << " Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
972 if (el->FindElement("version"))
973 cout << " Version: " << el->FindElement("version")->GetDataLine() << endl;
978 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
982 bool result = true; // true for success
984 if (!el) return false;
986 string AircraftName = el->GetAttributeValue("name");
987 ((FGAircraft*)Models[eAircraft])->SetAircraftName(AircraftName);
989 if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
990 << underoff << ": " << highint << AircraftName << normint << endl;
992 CFGVersion = el->GetAttributeValue("version");
993 Release = el->GetAttributeValue("release");
996 cout << " Version: " << highint << CFGVersion
998 if (CFGVersion != needed_cfg_version) {
999 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
1000 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
1001 cerr << "Current version needed is: " << needed_cfg_version << endl;
1002 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
1006 if (Release == "ALPHA" && (debug_lvl & 1)) {
1007 cout << endl << endl
1008 << highint << "This aircraft model is an " << fgred << Release
1009 << reset << highint << " release!!!" << endl << endl << reset
1010 << "This aircraft model may not even properly load, and probably"
1011 << " will not fly as expected." << endl << endl
1012 << fgred << highint << "Use this model for development purposes ONLY!!!"
1013 << normint << reset << endl << endl;
1014 } else if (Release == "BETA" && (debug_lvl & 1)) {
1015 cout << endl << endl
1016 << highint << "This aircraft model is a " << fgred << Release
1017 << reset << highint << " release!!!" << endl << endl << reset
1018 << "This aircraft model probably will not fly as expected." << endl << endl
1019 << fgblue << highint << "Use this model for development purposes ONLY!!!"
1020 << normint << reset << endl << endl;
1021 } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
1022 cout << endl << endl
1023 << highint << "This aircraft model is a " << fgblue << Release
1024 << reset << highint << " release." << endl << endl << reset;
1025 } else if (debug_lvl & 1) {
1026 cout << endl << endl
1027 << highint << "This aircraft model is an " << fgred << Release
1028 << reset << highint << " release!!!" << endl << endl << reset
1029 << "This aircraft model may not even properly load, and probably"
1030 << " will not fly as expected." << endl << endl
1031 << fgred << highint << "Use this model for development purposes ONLY!!!"
1032 << normint << reset << endl << endl;
1038 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040 bool FGFDMExec::ReadChild(Element* el)
1042 // Add a new childData object to the child FDM list
1043 // Populate that childData element with a new FDMExec object
1044 // Set the IsChild flag for that FDMExec object
1045 // Get the aircraft name
1046 // set debug level to print out no additional data for child objects
1047 // Load the model given the aircraft name
1048 // reset debug level to prior setting
1052 struct childData* child = new childData;
1054 child->exec = new FGFDMExec(Root, FDMctr);
1055 child->exec->SetChild(true);
1057 string childAircraft = el->GetAttributeValue("name");
1058 string sMated = el->GetAttributeValue("mated");
1059 if (sMated == "false") child->mated = false; // child objects are mated by default.
1060 string sInternal = el->GetAttributeValue("internal");
1061 if (sInternal == "true") child->internal = true; // child objects are external by default.
1063 child->exec->SetAircraftPath( AircraftPath );
1064 child->exec->SetEnginePath( EnginePath );
1065 child->exec->SetSystemsPath( SystemsPath );
1066 child->exec->LoadModel(childAircraft);
1068 Element* location = el->FindElement("location");
1070 child->Loc = location->FindElementTripletConvertTo("IN");
1072 cerr << endl << highint << fgred << " No location was found for this child object!" << reset << endl;
1076 Element* orientation = el->FindElement("orient");
1078 child->Orient = orientation->FindElementTripletConvertTo("RAD");
1079 } else if (debug_lvl > 0) {
1080 cerr << endl << highint << " No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
1083 ChildFDMList.push_back(child);
1088 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
1095 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1097 FGTrim* FGFDMExec::GetTrim(void)
1100 Trim = new FGTrim(this,tNone);
1104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1106 void FGFDMExec::DisableOutput(void)
1108 for (unsigned i=0; i<Outputs.size(); i++) {
1109 Outputs[i]->Disable();
1113 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1115 void FGFDMExec::EnableOutput(void)
1117 for (unsigned i=0; i<Outputs.size(); i++) {
1118 Outputs[i]->Enable();
1122 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1124 void FGFDMExec::ForceOutput(int idx)
1126 if (idx >= (int)0 && idx < (int)Outputs.size()) Outputs[idx]->Print();
1129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1131 bool FGFDMExec::SetOutputDirectives(const string& fname)
1135 FGOutput* Output = new FGOutput(this);
1136 Output->SetDirectivesFile(RootDir + fname);
1137 Output->InitModel();
1139 result = Output->Load(0);
1142 Output->Run(holding);
1143 Outputs.push_back(Output);
1144 typedef double (FGOutput::*iOPMF)(void) const;
1145 string outputProp = CreateIndexedPropertyName("simulation/output",Outputs.size()-1);
1146 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate, false);
1154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1156 void FGFDMExec::DoTrim(int mode)
1160 if (Constructing) return;
1162 if (mode < 0 || mode > JSBSim::tNone) {
1163 cerr << endl << "Illegal trimming mode!" << endl << endl;
1166 saved_time = sim_time;
1167 FGTrim trim(this, (JSBSim::TrimMode)mode);
1168 if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
1170 sim_time = saved_time;
1173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1174 // The bitmasked value choices are as follows:
1175 // unset: In this case (the default) JSBSim would only print
1176 // out the normally expected messages, essentially echoing
1177 // the config files as they are read. If the environment
1178 // variable is not set, debug_lvl is set to 1 internally
1179 // 0: This requests JSBSim not to output any messages
1181 // 1: This value explicity requests the normal JSBSim
1183 // 2: This value asks for a message to be printed out when
1184 // a class is instantiated
1185 // 4: When this value is set, a message is displayed when a
1186 // FGModel object executes its Run() method
1187 // 8: When this value is set, various runtime state variables
1188 // are printed out periodically
1189 // 16: When set various parameters are sanity checked and
1190 // a message is printed out when they go out of bounds
1192 void FGFDMExec::Debug(int from)
1194 if (debug_lvl <= 0) return;
1196 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1197 if (from == 0) { // Constructor
1199 << "JSBSim Flight Dynamics Model v" << JSBSim_version << endl;
1200 cout << " [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1201 cout << "JSBSim startup beginning ...\n\n";
1202 } else if (from == 3) {
1203 cout << "\n\nJSBSim startup complete\n\n";
1206 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1207 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1208 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
1210 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1212 cout << "================== Frame: " << Frame << " Time: "
1213 << sim_time << " dt: " << dT << endl;
1216 if (debug_lvl & 8 ) { // Runtime state variables
1218 if (debug_lvl & 16) { // Sanity checking
1220 if (debug_lvl & 64) {
1221 if (from == 0) { // Constructor
1222 cout << IdSrc << endl;
1223 cout << IdHdr << endl;