1 
2 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 
4  Module:       FGFDMExec.cpp
5  Author:       Jon S. Berndt
6  Date started: 11/17/98
7  Purpose:      Schedules and runs the model routines.
8 
9  ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
10 
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser General Public License as published by the Free
13  Software Foundation; either version 2 of the License, or (at your option) any
14  later version.
15 
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
19  details.
20 
21  You should have received a copy of the GNU Lesser General Public License along
22  with this program; if not, write to the Free Software Foundation, Inc., 59
23  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 
25  Further information about the GNU Lesser General Public License can also be
26  found on the world wide web at http://www.gnu.org.
27 
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 
31 This class wraps up the simulation scheduling routines.
32 
33 HISTORY
34 --------------------------------------------------------------------------------
35 11/17/98   JSB   Created
36 
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 COMMENTS, REFERENCES,  and NOTES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 INCLUDES
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 
45 #include <iomanip>
46 
47 #include "FGFDMExec.h"
48 #include "models/atmosphere/FGStandardAtmosphere.h"
49 #include "models/atmosphere/FGWinds.h"
50 #include "models/FGFCS.h"
51 #include "models/FGPropulsion.h"
52 #include "models/FGMassBalance.h"
53 #include "models/FGExternalReactions.h"
54 #include "models/FGBuoyantForces.h"
55 #include "models/FGAerodynamics.h"
56 #include "models/FGInertial.h"
57 #include "models/FGAircraft.h"
58 #include "models/FGAccelerations.h"
59 #include "models/FGAuxiliary.h"
60 #include "models/FGInput.h"
61 #include "initialization/FGTrim.h"
62 #include "input_output/FGScript.h"
63 #include "input_output/FGXMLFileRead.h"
64 
65 using namespace std;
66 
67 namespace JSBSim {
68 
69 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 CLASS IMPLEMENTATION
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 
73 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 // Constructor
75 
FGFDMExec(FGPropertyManager * root,unsigned int * fdmctr)76 FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr)
77   : Root(root), FDMctr(fdmctr)
78 {
79   Frame           = 0;
80   IC              = nullptr;
81   Trim            = nullptr;
82   Script          = nullptr;
83   disperse        = 0;
84 
85   RootDir = "";
86 
87   modelLoaded = false;
88   IsChild = false;
89   holding = false;
90   Terminate = false;
91   StandAlone = false;
92   ResetMode = 0;
93   RandomSeed = 0;
94   HoldDown = false;
95 
96   IncrementThenHolding = false;  // increment then hold is off by default
97   TimeStepsUntilHold = -1;
98 
99   sim_time = 0.0;
100   dT = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is
101                   // run in standalone mode with no initialization file.
102 
103   AircraftPath = "aircraft";
104   EnginePath = "engine";
105   SystemsPath = "systems";
106 
107   try {
108     char* num = getenv("JSBSIM_DEBUG");
109     if (num) debug_lvl = atoi(num); // set debug level
110   } catch (...) {                   // if error set to 1
111     debug_lvl = 1;
112   }
113 
114   if (Root == 0) {                 // Then this is the root FDM
115     Root = new FGPropertyManager;  // Create the property manager
116     StandAlone = true;
117   }
118 
119   if (FDMctr == 0) {
120     FDMctr = new unsigned int;     // Create and initialize the child FDM counter
121     (*FDMctr) = 0;
122   }
123 
124   // Store this FDM's ID
125   IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth"
126 
127   // Prepare FDMctr for the next child FDM id
128   (*FDMctr)++;       // instance. "child" instances are loaded last.
129 
130   FGPropertyNode* instanceRoot = Root->GetNode("/fdm/jsbsim",IdFDM,true);
131   instance = new FGPropertyManager(instanceRoot);
132 
133   try {
134     char* num = getenv("JSBSIM_DISPERSE");
135     if (num) {
136       if (atoi(num) != 0) disperse = 1;  // set dispersions on
137     }
138   } catch (...) {                        // if error set to false
139     disperse = 0;
140     std::cerr << "Could not process JSBSIM_DISPERSIONS environment variable: Assumed NO dispersions." << endl;
141   }
142 
143   Debug(0);
144   // this is to catch errors in binding member functions to the property tree.
145   try {
146     Allocate();
147   } catch (const string& msg ) {
148     cout << "Caught error: " << msg << endl;
149     exit(1);
150   }
151 
152   trim_status = false;
153   ta_mode     = 99;
154   trim_completed = 0;
155 
156   Constructing = true;
157   typedef int (FGFDMExec::*iPMF)(void) const;
158   instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim, false);
159   instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions, false);
160   instance->Tie("simulation/disperse", this, &FGFDMExec::GetDisperse);
161   instance->Tie("simulation/randomseed", this, (iPMF)&FGFDMExec::SRand, &FGFDMExec::SRand, false);
162   instance->Tie("simulation/terminate", (int *)&Terminate);
163   instance->Tie("simulation/pause", (int *)&holding);
164   instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime);
165   instance->Tie("simulation/dt", this, &FGFDMExec::GetDeltaT);
166   instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel);
167   instance->Tie("simulation/frame", (int *)&Frame, false);
168   instance->Tie("simulation/trim-completed", (int *)&trim_completed, false);
169   instance->Tie("forces/hold-down", this, &FGFDMExec::GetHoldDown, &FGFDMExec::SetHoldDown);
170 
171   Constructing = false;
172 }
173 
174 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 
~FGFDMExec()176 FGFDMExec::~FGFDMExec()
177 {
178   try {
179     Unbind();
180     DeAllocate();
181 
182     delete instance;
183 
184     if (IdFDM == 0) { // Meaning this is no child FDM
185       if(Root != 0) {
186          if(StandAlone)
187             delete Root;
188          Root = 0;
189       }
190       if(FDMctr != 0) {
191          delete FDMctr;
192          FDMctr = 0;
193       }
194     }
195   } catch (const string& msg ) {
196     cout << "Caught error: " << msg << endl;
197   }
198 
199   for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
200   ChildFDMList.clear();
201 
202   PropertyCatalog.clear();
203 
204   SetGroundCallback(0);
205 
206   if (FDMctr != 0) (*FDMctr)--;
207 
208   Debug(1);
209 }
210 
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 
Allocate(void)213 bool FGFDMExec::Allocate(void)
214 {
215   bool result=true;
216 
217   Models.resize(eNumStandardModels);
218 
219   // First build the inertial model since some other models are relying on
220   // the inertial model and the ground callback to build themselves.
221   // Note that this does not affect the order in which the models will be
222   // executed later.
223   Models[eInertial]          = new FGInertial(this);
224   SetGroundCallback(new FGDefaultGroundCallback(static_cast<FGInertial*>(Models[eInertial])->GetRefRadius()));
225 
226   // See the eModels enum specification in the header file. The order of the
227   // enums specifies the order of execution. The Models[] vector is the primary
228   // storage array for the list of models.
229   Models[ePropagate]         = new FGPropagate(this);
230   Models[eInput]             = new FGInput(this);
231   Models[eAtmosphere]        = new FGStandardAtmosphere(this);
232   Models[eWinds]             = new FGWinds(this);
233   Models[eSystems]           = new FGFCS(this);
234   Models[eMassBalance]       = new FGMassBalance(this);
235   Models[eAuxiliary]         = new FGAuxiliary(this);
236   Models[ePropulsion]        = new FGPropulsion(this);
237   Models[eAerodynamics]      = new FGAerodynamics (this);
238   Models[eGroundReactions]   = new FGGroundReactions(this);
239   Models[eExternalReactions] = new FGExternalReactions(this);
240   Models[eBuoyantForces]     = new FGBuoyantForces(this);
241   Models[eAircraft]          = new FGAircraft(this);
242   Models[eAccelerations]     = new FGAccelerations(this);
243   Models[eOutput]            = new FGOutput(this);
244 
245   // Assign the Model shortcuts for internal executive use only.
246   Propagate = (FGPropagate*)Models[ePropagate];
247   Inertial = (FGInertial*)Models[eInertial];
248   Atmosphere = (FGAtmosphere*)Models[eAtmosphere];
249   Winds = (FGWinds*)Models[eWinds];
250   FCS = (FGFCS*)Models[eSystems];
251   MassBalance = (FGMassBalance*)Models[eMassBalance];
252   Auxiliary = (FGAuxiliary*)Models[eAuxiliary];
253   Propulsion = (FGPropulsion*)Models[ePropulsion];
254   Aerodynamics = (FGAerodynamics*)Models[eAerodynamics];
255   GroundReactions = (FGGroundReactions*)Models[eGroundReactions];
256   ExternalReactions = (FGExternalReactions*)Models[eExternalReactions];
257   BuoyantForces = (FGBuoyantForces*)Models[eBuoyantForces];
258   Aircraft = (FGAircraft*)Models[eAircraft];
259   Accelerations = (FGAccelerations*)Models[eAccelerations];
260   Output = (FGOutput*)Models[eOutput];
261 
262   // Initialize planet (environment) constants
263   LoadPlanetConstants();
264 
265   // Initialize models
266   for (unsigned int i = 0; i < Models.size(); i++) {
267     // The Input/Output models must not be initialized prior to IC loading
268     if (i == eInput || i == eOutput) continue;
269 
270     LoadInputs(i);
271     Models[i]->InitModel();
272   }
273 
274   IC = new FGInitialCondition(this);
275   IC->bind(instance);
276 
277   modelLoaded = false;
278 
279   return result;
280 }
281 
282 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 
DeAllocate(void)284 bool FGFDMExec::DeAllocate(void)
285 {
286 
287   for (unsigned int i=0; i<eNumStandardModels; i++) delete Models[i];
288   Models.clear();
289 
290   delete Script;
291   delete IC;
292   delete Trim;
293 
294   modelLoaded = false;
295   return modelLoaded;
296 }
297 
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 
Run(void)300 bool FGFDMExec::Run(void)
301 {
302   bool success=true;
303 
304   Debug(2);
305 
306   for (unsigned int i=1; i<ChildFDMList.size(); i++) {
307     ChildFDMList[i]->AssignState( (FGPropagate*)Models[ePropagate] ); // Transfer state to the child FDM
308     ChildFDMList[i]->Run();
309   }
310 
311   IncrTime();
312 
313   // returns true if success, false if complete
314   if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript();
315 
316   for (unsigned int i = 0; i < Models.size(); i++) {
317     LoadInputs(i);
318     Models[i]->Run(holding);
319   }
320 
321   if (ResetMode) {
322     unsigned int mode = ResetMode;
323 
324     ResetMode = 0;
325     ResetToInitialConditions(mode);
326   }
327 
328   if (Terminate) success = false;
329 
330   return success;
331 }
332 
333 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 
LoadInputs(unsigned int idx)335 void FGFDMExec::LoadInputs(unsigned int idx)
336 {
337   switch(idx) {
338   case ePropagate:
339     Propagate->in.vPQRidot     = Accelerations->GetPQRidot();
340     Propagate->in.vUVWidot     = Accelerations->GetUVWidot();
341     Propagate->in.DeltaT       = dT;
342     break;
343   case eInput:
344     break;
345   case eInertial:
346     Inertial->in.Position      = Propagate->GetLocation();
347     break;
348   case eAtmosphere:
349     Atmosphere->in.altitudeASL = Propagate->GetAltitudeASL();
350     break;
351   case eWinds:
352     Winds->in.AltitudeASL      = Propagate->GetAltitudeASL();
353     Winds->in.DistanceAGL      = Propagate->GetDistanceAGL();
354     Winds->in.Tl2b             = Propagate->GetTl2b();
355     Winds->in.Tw2b             = Auxiliary->GetTw2b();
356     Winds->in.V                = Auxiliary->GetVt();
357     Winds->in.totalDeltaT      = dT * Winds->GetRate();
358     break;
359   case eAuxiliary:
360     Auxiliary->in.Pressure     = Atmosphere->GetPressure();
361     Auxiliary->in.Density      = Atmosphere->GetDensity();
362     Auxiliary->in.DensitySL    = Atmosphere->GetDensitySL();
363     Auxiliary->in.PressureSL   = Atmosphere->GetPressureSL();
364     Auxiliary->in.Temperature  = Atmosphere->GetTemperature();
365     Auxiliary->in.SoundSpeed   = Atmosphere->GetSoundSpeed();
366     Auxiliary->in.KinematicViscosity = Atmosphere->GetKinematicViscosity();
367     Auxiliary->in.DistanceAGL  = Propagate->GetDistanceAGL();
368     Auxiliary->in.Mass         = MassBalance->GetMass();
369     Auxiliary->in.Tl2b         = Propagate->GetTl2b();
370     Auxiliary->in.Tb2l         = Propagate->GetTb2l();
371     Auxiliary->in.vPQR         = Propagate->GetPQR();
372     Auxiliary->in.vPQRi        = Propagate->GetPQRi();
373     Auxiliary->in.vPQRidot     = Accelerations->GetPQRidot();
374     Auxiliary->in.vUVW         = Propagate->GetUVW();
375     Auxiliary->in.vUVWdot      = Accelerations->GetUVWdot();
376     Auxiliary->in.vVel         = Propagate->GetVel();
377     Auxiliary->in.vBodyAccel   = Accelerations->GetBodyAccel();
378     Auxiliary->in.ToEyePt      = MassBalance->StructuralToBody(Aircraft->GetXYZep());
379     Auxiliary->in.VRPBody      = MassBalance->StructuralToBody(Aircraft->GetXYZvrp());
380     Auxiliary->in.RPBody       = MassBalance->StructuralToBody(Aircraft->GetXYZrp());
381     Auxiliary->in.vFw          = Aerodynamics->GetvFw();
382     Auxiliary->in.vLocation    = Propagate->GetLocation();
383     Auxiliary->in.CosTht       = Propagate->GetCosEuler(eTht);
384     Auxiliary->in.SinTht       = Propagate->GetSinEuler(eTht);
385     Auxiliary->in.CosPhi       = Propagate->GetCosEuler(ePhi);
386     Auxiliary->in.SinPhi       = Propagate->GetSinEuler(ePhi);
387     Auxiliary->in.TotalWindNED = Winds->GetTotalWindNED();
388     Auxiliary->in.TurbPQR      = Winds->GetTurbPQR();
389     break;
390   case eSystems:
391     // Dynamic inputs come into the components that FCS manages through properties
392     break;
393   case ePropulsion:
394     Propulsion->in.Pressure         = Atmosphere->GetPressure();
395     Propulsion->in.PressureRatio    = Atmosphere->GetPressureRatio();
396     Propulsion->in.Temperature      = Atmosphere->GetTemperature();
397     Propulsion->in.DensityRatio     = Atmosphere->GetDensityRatio();
398     Propulsion->in.Density          = Atmosphere->GetDensity();
399     Propulsion->in.Soundspeed       = Atmosphere->GetSoundSpeed();
400     Propulsion->in.TotalPressure    = Auxiliary->GetTotalPressure();
401     Propulsion->in.Vc               = Auxiliary->GetVcalibratedKTS();
402     Propulsion->in.Vt               = Auxiliary->GetVt();
403     Propulsion->in.qbar             = Auxiliary->Getqbar();
404     Propulsion->in.TAT_c            = Auxiliary->GetTAT_C();
405     Propulsion->in.AeroUVW          = Auxiliary->GetAeroUVW();
406     Propulsion->in.AeroPQR          = Auxiliary->GetAeroPQR();
407     Propulsion->in.alpha            = Auxiliary->Getalpha();
408     Propulsion->in.beta             = Auxiliary->Getbeta();
409     Propulsion->in.TotalDeltaT      = dT * Propulsion->GetRate();
410     Propulsion->in.ThrottlePos      = FCS->GetThrottlePos();
411     Propulsion->in.MixturePos       = FCS->GetMixturePos();
412     Propulsion->in.ThrottleCmd      = FCS->GetThrottleCmd();
413     Propulsion->in.MixtureCmd       = FCS->GetMixtureCmd();
414     Propulsion->in.PropAdvance      = FCS->GetPropAdvance();
415     Propulsion->in.PropFeather      = FCS->GetPropFeather();
416     Propulsion->in.H_agl            = Propagate->GetDistanceAGL();
417     Propulsion->in.PQRi             = Propagate->GetPQRi();
418 
419     break;
420   case eAerodynamics:
421     Aerodynamics->in.Alpha     = Auxiliary->Getalpha();
422     Aerodynamics->in.Beta      = Auxiliary->Getbeta();
423     Aerodynamics->in.Qbar      = Auxiliary->Getqbar();
424     Aerodynamics->in.Vt        = Auxiliary->GetVt();
425     Aerodynamics->in.Tb2w      = Auxiliary->GetTb2w();
426     Aerodynamics->in.Tw2b      = Auxiliary->GetTw2b();
427     Aerodynamics->in.RPBody    = MassBalance->StructuralToBody(Aircraft->GetXYZrp());
428     break;
429   case eGroundReactions:
430     // There are no external inputs to this model.
431     GroundReactions->in.Vground         = Auxiliary->GetVground();
432     GroundReactions->in.VcalibratedKts  = Auxiliary->GetVcalibratedKTS();
433     GroundReactions->in.Temperature     = Atmosphere->GetTemperature();
434     GroundReactions->in.TakeoffThrottle = (FCS->GetThrottlePos().size() > 0) ? (FCS->GetThrottlePos(0) > 0.90) : false;
435     GroundReactions->in.BrakePos        = FCS->GetBrakePos();
436     GroundReactions->in.FCSGearPos      = FCS->GetGearPos();
437     GroundReactions->in.EmptyWeight     = MassBalance->GetEmptyWeight();
438     GroundReactions->in.Tb2l            = Propagate->GetTb2l();
439     GroundReactions->in.Tec2l           = Propagate->GetTec2l();
440     GroundReactions->in.Tec2b           = Propagate->GetTec2b();
441     GroundReactions->in.PQR             = Propagate->GetPQR();
442     GroundReactions->in.UVW             = Propagate->GetUVW();
443     GroundReactions->in.DistanceAGL     = Propagate->GetDistanceAGL();
444     GroundReactions->in.DistanceASL     = Propagate->GetAltitudeASL();
445     GroundReactions->in.TotalDeltaT     = dT * GroundReactions->GetRate();
446     GroundReactions->in.WOW             = GroundReactions->GetWOW();
447     GroundReactions->in.Location        = Propagate->GetLocation();
448     GroundReactions->in.vXYZcg          = MassBalance->GetXYZcg();
449     break;
450   case eExternalReactions:
451     // There are no external inputs to this model.
452     break;
453   case eBuoyantForces:
454     BuoyantForces->in.Density     = Atmosphere->GetDensity();
455     BuoyantForces->in.Pressure    = Atmosphere->GetPressure();
456     BuoyantForces->in.Temperature = Atmosphere->GetTemperature();
457     BuoyantForces->in.gravity     = Inertial->GetGravity().Magnitude();
458     break;
459   case eMassBalance:
460     MassBalance->in.GasInertia  = BuoyantForces->GetGasMassInertia();
461     MassBalance->in.GasMass     = BuoyantForces->GetGasMass();
462     MassBalance->in.GasMoment   = BuoyantForces->GetGasMassMoment();
463     MassBalance->in.TanksWeight = Propulsion->GetTanksWeight();
464     MassBalance->in.TanksMoment = Propulsion->GetTanksMoment();
465     MassBalance->in.TankInertia = Propulsion->CalculateTankInertias();
466     break;
467   case eAircraft:
468     Aircraft->in.AeroForce     = Aerodynamics->GetForces();
469     Aircraft->in.PropForce     = Propulsion->GetForces();
470     Aircraft->in.GroundForce   = GroundReactions->GetForces();
471     Aircraft->in.ExternalForce = ExternalReactions->GetForces();
472     Aircraft->in.BuoyantForce  = BuoyantForces->GetForces();
473     Aircraft->in.AeroMoment    = Aerodynamics->GetMoments();
474     Aircraft->in.PropMoment    = Propulsion->GetMoments();
475     Aircraft->in.GroundMoment  = GroundReactions->GetMoments();
476     Aircraft->in.ExternalMoment = ExternalReactions->GetMoments();
477     Aircraft->in.BuoyantMoment = BuoyantForces->GetMoments();
478     break;
479   case eAccelerations:
480     Accelerations->in.J        = MassBalance->GetJ();
481     Accelerations->in.Jinv     = MassBalance->GetJinv();
482     Accelerations->in.Ti2b     = Propagate->GetTi2b();
483     Accelerations->in.Tb2i     = Propagate->GetTb2i();
484     Accelerations->in.Tec2b    = Propagate->GetTec2b();
485     Accelerations->in.Tec2i    = Propagate->GetTec2i();
486     Accelerations->in.Moment   = Aircraft->GetMoments();
487     Accelerations->in.GroundMoment  = GroundReactions->GetMoments();
488     Accelerations->in.Force    = Aircraft->GetForces();
489     Accelerations->in.GroundForce   = GroundReactions->GetForces();
490     Accelerations->in.vGravAccel = Inertial->GetGravity();
491     Accelerations->in.vPQRi    = Propagate->GetPQRi();
492     Accelerations->in.vPQR     = Propagate->GetPQR();
493     Accelerations->in.vUVW     = Propagate->GetUVW();
494     Accelerations->in.vInertialPosition = Propagate->GetInertialPosition();
495     Accelerations->in.DeltaT   = dT;
496     Accelerations->in.Mass     = MassBalance->GetMass();
497     Accelerations->in.MultipliersList = GroundReactions->GetMultipliersList();
498     Accelerations->in.TerrainVelocity = Propagate->GetTerrainVelocity();
499     Accelerations->in.TerrainAngularVel = Propagate->GetTerrainAngularVelocity();
500     break;
501   default:
502     break;
503   }
504 }
505 
506 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507 
LoadPlanetConstants(void)508 void FGFDMExec::LoadPlanetConstants(void)
509 {
510   Propagate->in.vOmegaPlanet     = Inertial->GetOmegaPlanet();
511   Accelerations->in.vOmegaPlanet = Inertial->GetOmegaPlanet();
512   Propagate->in.SemiMajor        = Inertial->GetSemimajor();
513   Propagate->in.SemiMinor        = Inertial->GetSemiminor();
514   Auxiliary->in.SLGravity        = Inertial->SLgravity();
515 }
516 
517 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 
LoadModelConstants(void)519 void FGFDMExec::LoadModelConstants(void)
520 {
521   Winds->in.wingspan             = Aircraft->GetWingSpan();
522   Aerodynamics->in.Wingarea      = Aircraft->GetWingArea();
523   Aerodynamics->in.Wingchord     = Aircraft->Getcbar();
524   Aerodynamics->in.Wingincidence = Aircraft->GetWingIncidence();
525   Aerodynamics->in.Wingspan      = Aircraft->GetWingSpan();
526   Auxiliary->in.Wingspan         = Aircraft->GetWingSpan();
527   Auxiliary->in.Wingchord        = Aircraft->Getcbar();
528   GroundReactions->in.vXYZcg     = MassBalance->GetXYZcg();
529 
530   LoadPlanetConstants();
531 }
532 
533 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 // This call will cause the sim time to reset to 0.0
535 
RunIC(void)536 bool FGFDMExec::RunIC(void)
537 {
538   FGPropulsion* propulsion = (FGPropulsion*)Models[ePropulsion];
539 
540   SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0.
541   Initialize(IC);
542 
543   Models[eInput]->InitModel();
544   Models[eOutput]->InitModel();
545 
546   Run();
547   Propagate->InitializeDerivatives();
548   ResumeIntegration(); // Restores the integration rate to what it was.
549 
550   if (debug_lvl > 0) {
551     MassBalance->GetMassPropertiesReport(0);
552 
553     cout << endl << fgblue << highint
554          << "End of vehicle configuration loading." << endl
555          << "-------------------------------------------------------------------------------"
556          << reset << std::setprecision(6) << endl;
557   }
558 
559   for (unsigned int n=0; n < propulsion->GetNumEngines(); ++n) {
560     if (IC->IsEngineRunning(n)) {
561       try {
562         propulsion->InitRunning(n);
563       } catch (const string& str) {
564         cerr << str << endl;
565         return false;
566       }
567     }
568   }
569 
570   return true;
571 }
572 
573 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 
Initialize(FGInitialCondition * FGIC)575 void FGFDMExec::Initialize(FGInitialCondition* FGIC)
576 {
577   Propagate->SetInitialState(FGIC);
578   Winds->SetWindNED(FGIC->GetWindNEDFpsIC());
579   Run();
580 }
581 
582 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583 
ResetToInitialConditions(int mode)584 void FGFDMExec::ResetToInitialConditions(int mode)
585 {
586   if (Constructing) return;
587 
588   if (mode == 1) Output->SetStartNewOutput();
589 
590   for (unsigned int i = 0; i < Models.size(); i++) {
591     // The Input/Output models will be initialized during the RunIC() execution
592     if (i == eInput || i == eOutput) continue;
593 
594     LoadInputs(i);
595     Models[i]->InitModel();
596   }
597 
598   if (Script)
599     Script->ResetEvents();
600   else
601     Setsim_time(0.0);
602 
603   RunIC();
604 }
605 
606 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 
SetHoldDown(bool hd)608 void FGFDMExec::SetHoldDown(bool hd)
609 {
610   HoldDown = hd;
611   Accelerations->SetHoldDown(hd);
612   if (hd) {
613     Propagate->in.vPQRidot = Accelerations->GetPQRidot();
614     Propagate->in.vUVWidot = Accelerations->GetUVWidot();
615   }
616   Propagate->SetHoldDown(hd);
617 }
618 
619 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 
EnumerateFDMs(void)621 vector <string> FGFDMExec::EnumerateFDMs(void)
622 {
623   vector <string> FDMList;
624   FGAircraft* Aircraft = (FGAircraft*)Models[eAircraft];
625 
626   FDMList.push_back(Aircraft->GetAircraftName());
627 
628   for (unsigned int i=1; i<ChildFDMList.size(); i++) {
629     FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
630   }
631 
632   return FDMList;
633 }
634 
635 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 
LoadScript(const SGPath & script,double deltaT,const SGPath & initfile)637 bool FGFDMExec::LoadScript(const SGPath& script, double deltaT,
638                            const SGPath& initfile)
639 {
640   bool result;
641 
642   Script = new FGScript(this);
643   result = Script->LoadScript(GetFullPath(script), deltaT, initfile);
644 
645   return result;
646 }
647 
648 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 
LoadModel(const SGPath & AircraftPath,const SGPath & EnginePath,const SGPath & SystemsPath,const string & model,bool addModelToPath)650 bool FGFDMExec::LoadModel(const SGPath& AircraftPath, const SGPath& EnginePath,
651                           const SGPath& SystemsPath, const string& model,
652                           bool addModelToPath)
653 {
654   FGFDMExec::AircraftPath = GetFullPath(AircraftPath);
655   FGFDMExec::EnginePath = GetFullPath(EnginePath);
656   FGFDMExec::SystemsPath = GetFullPath(SystemsPath);
657 
658   return LoadModel(model, addModelToPath);
659 }
660 
661 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662 
LoadModel(const string & model,bool addModelToPath)663 bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
664 {
665   SGPath aircraftCfgFileName;
666   bool result = false; // initialize result to false, indicating input file not yet read
667 
668   modelName = model; // Set the class modelName attribute
669 
670   if( AircraftPath.isNull() || EnginePath.isNull() || SystemsPath.isNull()) {
671     cerr << "Error: attempted to load aircraft with undefined "
672          << "aircraft, engine, and system paths" << endl;
673     return false;
674   }
675 
676   FullAircraftPath = AircraftPath;
677   if (addModelToPath) FullAircraftPath.append(model);
678   aircraftCfgFileName = FullAircraftPath/(model + ".xml");
679 
680   if (modelLoaded) {
681     DeAllocate();
682     Allocate();
683   }
684 
685   int saved_debug_lvl = debug_lvl;
686   FGXMLFileRead XMLFileRead;
687   Element *document = XMLFileRead.LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
688 
689   if (document) {
690     if (IsChild) debug_lvl = 0;
691 
692     ReadPrologue(document);
693 
694     if (IsChild) debug_lvl = saved_debug_lvl;
695 
696     // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
697     Element* element = document->FindElement("fileheader");
698     if (element) {
699       result = ReadFileHeader(element);
700       if (!result) {
701         cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
702         return result;
703       }
704     }
705 
706     if (IsChild) debug_lvl = 0;
707 
708     // Process the metrics element. This element is REQUIRED.
709     element = document->FindElement("metrics");
710     if (element) {
711       result = ((FGAircraft*)Models[eAircraft])->Load(element);
712       if (!result) {
713         cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
714         return result;
715       }
716     } else {
717       cerr << endl << "No metrics element was found in the aircraft config file." << endl;
718       return false;
719     }
720 
721     // Process the mass_balance element. This element is REQUIRED.
722     element = document->FindElement("mass_balance");
723     if (element) {
724       result = ((FGMassBalance*)Models[eMassBalance])->Load(element);
725       if (!result) {
726         cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
727         return result;
728       }
729     } else {
730       cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
731       return false;
732     }
733 
734     // Process the ground_reactions element. This element is REQUIRED.
735     element = document->FindElement("ground_reactions");
736     if (element) {
737       result = ((FGGroundReactions*)Models[eGroundReactions])->Load(element);
738       if (!result) {
739         cerr << endl << element->ReadFrom()
740              << "Aircraft ground_reactions element has problems in file "
741              << aircraftCfgFileName << endl;
742         return result;
743       }
744     } else {
745       cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
746       return false;
747     }
748 
749     // Process the external_reactions element. This element is OPTIONAL.
750     element = document->FindElement("external_reactions");
751     if (element) {
752       result = ((FGExternalReactions*)Models[eExternalReactions])->Load(element);
753       if (!result) {
754         cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
755         return result;
756       }
757     }
758 
759     // Process the buoyant_forces element. This element is OPTIONAL.
760     element = document->FindElement("buoyant_forces");
761     if (element) {
762       result = ((FGBuoyantForces*)Models[eBuoyantForces])->Load(element);
763       if (!result) {
764         cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
765         return result;
766       }
767     }
768 
769     // Process the propulsion element. This element is OPTIONAL.
770     element = document->FindElement("propulsion");
771     if (element) {
772       result = ((FGPropulsion*)Models[ePropulsion])->Load(element);
773       if (!result) {
774         cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
775         return result;
776       }
777       for (unsigned int i=0; i<((FGPropulsion*)Models[ePropulsion])->GetNumEngines(); i++)
778         ((FGFCS*)Models[eSystems])->AddThrottle();
779     }
780 
781     // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
782     element = document->FindElement("system");
783     while (element) {
784       result = ((FGFCS*)Models[eSystems])->Load(element);
785       if (!result) {
786         cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
787         return result;
788       }
789       element = document->FindNextElement("system");
790     }
791 
792     // Process the autopilot element. This element is OPTIONAL.
793     element = document->FindElement("autopilot");
794     if (element) {
795       result = ((FGFCS*)Models[eSystems])->Load(element);
796       if (!result) {
797         cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
798         return result;
799       }
800     }
801 
802     // Process the flight_control element. This element is OPTIONAL.
803     element = document->FindElement("flight_control");
804     if (element) {
805       result = ((FGFCS*)Models[eSystems])->Load(element);
806       if (!result) {
807         cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
808         return result;
809       }
810     }
811 
812     // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
813     element = document->FindElement("aerodynamics");
814     if (element) {
815       result = ((FGAerodynamics*)Models[eAerodynamics])->Load(element);
816       if (!result) {
817         cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
818         return result;
819       }
820     } else {
821       cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
822     }
823 
824     // Process the input element. This element is OPTIONAL, and there may be more than one.
825     element = document->FindElement("input");
826     while (element) {
827       if (!static_cast<FGInput*>(Models[eInput])->Load(element))
828         return false;
829 
830       element = document->FindNextElement("input");
831     }
832 
833     // Process the output element[s]. This element is OPTIONAL, and there may be
834     // more than one.
835     element = document->FindElement("output");
836     while (element) {
837       if (!static_cast<FGOutput*>(Models[eOutput])->Load(element))
838         return false;
839 
840       element = document->FindNextElement("output");
841     }
842 
843     // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
844     element = document->FindElement("child");
845     if (element) {
846       result = ReadChild(element);
847       if (!result) {
848         cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
849         return result;
850       }
851     }
852 
853     // Since all vehicle characteristics have been loaded, place the values in the Inputs
854     // structure for the FGModel-derived classes.
855     LoadModelConstants();
856 
857     modelLoaded = true;
858 
859     if (IsChild) debug_lvl = saved_debug_lvl;
860 
861   } else {
862     cerr << fgred
863          << "  JSBSim failed to open the configuration file: " << aircraftCfgFileName
864          << fgdef << endl;
865   }
866 
867   for (unsigned int i=0; i< Models.size(); i++) LoadInputs(i);
868 
869   if (result) {
870     struct PropertyCatalogStructure masterPCS;
871     masterPCS.base_string = "";
872     masterPCS.node = Root->GetNode();
873     BuildPropertyCatalog(&masterPCS);
874   }
875 
876   return result;
877 }
878 
879 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 
GetPropulsionTankReport()881 string FGFDMExec::GetPropulsionTankReport()
882 {
883   return ((FGPropulsion*)Models[ePropulsion])->GetPropulsionTankReport();
884 }
885 
886 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887 
BuildPropertyCatalog(struct PropertyCatalogStructure * pcs)888 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
889 {
890   struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
891 
892   for (int i=0; i<pcs->node->nChildren(); i++) {
893     string access="";
894     pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
895     int node_idx = pcs->node->getChild(i)->getIndex();
896     if (node_idx != 0) {
897       pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx);
898     }
899     if (pcs->node->getChild(i)->nChildren() == 0) {
900       if (pcsNew->base_string.substr(0,12) == string("/fdm/jsbsim/")) {
901         pcsNew->base_string = pcsNew->base_string.erase(0,12);
902       }
903       if (pcs->node->getChild(i)->getAttribute(SGPropertyNode::READ)) access="R";
904       if (pcs->node->getChild(i)->getAttribute(SGPropertyNode::WRITE)) access+="W";
905       PropertyCatalog.push_back(pcsNew->base_string+" ("+access+")");
906     } else {
907       pcsNew->node = (FGPropertyNode*)pcs->node->getChild(i);
908       BuildPropertyCatalog(pcsNew);
909     }
910   }
911   delete pcsNew;
912 }
913 
914 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 
QueryPropertyCatalog(const string & in)916 string FGFDMExec::QueryPropertyCatalog(const string& in)
917 {
918   string results="";
919   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
920     if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
921   }
922   if (results.empty()) return "No matches found\n";
923   return results;
924 }
925 
926 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 
PrintPropertyCatalog(void)928 void FGFDMExec::PrintPropertyCatalog(void)
929 {
930   cout << endl;
931   cout << "  " << fgblue << highint << underon << "Property Catalog for "
932        << modelName << reset << endl << endl;
933   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
934     cout << "    " << PropertyCatalog[i] << endl;
935   }
936 }
937 
938 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939 
PrintSimulationConfiguration(void) const940 void FGFDMExec::PrintSimulationConfiguration(void) const
941 {
942   cout << endl << "Simulation Configuration" << endl << "------------------------" << endl;
943   cout << MassBalance->GetName() << endl;
944   cout << GroundReactions->GetName() << endl;
945   cout << Aerodynamics->GetName() << endl;
946   cout << Propulsion->GetName() << endl;
947 }
948 
949 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 
ReadFileHeader(Element * el)951 bool FGFDMExec::ReadFileHeader(Element* el)
952 {
953   bool result = true; // true for success
954 
955   if (debug_lvl == 0) return result;
956 
957   if (IsChild) {
958     cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
959   }
960 
961   if (el->FindElement("description"))
962     cout << "  Description:   " << el->FindElement("description")->GetDataLine() << endl;
963   if (el->FindElement("author"))
964     cout << "  Model Author:  " << el->FindElement("author")->GetDataLine() << endl;
965   if (el->FindElement("filecreationdate"))
966     cout << "  Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
967   if (el->FindElement("version"))
968     cout << "  Version:       " << el->FindElement("version")->GetDataLine() << endl;
969 
970   return result;
971 }
972 
973 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974 
ReadPrologue(Element * el)975 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
976 {
977   bool result = true; // true for success
978 
979   if (!el) return false;
980 
981   string AircraftName = el->GetAttributeValue("name");
982   ((FGAircraft*)Models[eAircraft])->SetAircraftName(AircraftName);
983 
984   if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
985             << underoff << ": " << highint << AircraftName << normint << endl;
986 
987   CFGVersion = el->GetAttributeValue("version");
988   Release    = el->GetAttributeValue("release");
989 
990   if (debug_lvl & 1)
991     cout << "                            Version: " << highint << CFGVersion
992                                                     << normint << endl;
993   if (CFGVersion != needed_cfg_version) {
994     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
995             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
996     cerr << "Current version needed is: " << needed_cfg_version << endl;
997     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
998     return false;
999   }
1000 
1001   if (Release == "ALPHA" && (debug_lvl & 1)) {
1002     cout << endl << endl
1003          << highint << "This aircraft model is an " << fgred << Release
1004          << reset << highint << " release!!!" << endl << endl << reset
1005          << "This aircraft model may not even properly load, and probably"
1006          << " will not fly as expected." << endl << endl
1007          << fgred << highint << "Use this model for development purposes ONLY!!!"
1008          << normint << reset << endl << endl;
1009   } else if (Release == "BETA" && (debug_lvl & 1)) {
1010     cout << endl << endl
1011          << highint << "This aircraft model is a " << fgred << Release
1012          << reset << highint << " release!!!" << endl << endl << reset
1013          << "This aircraft model probably will not fly as expected." << endl << endl
1014          << fgblue << highint << "Use this model for development purposes ONLY!!!"
1015          << normint << reset << endl << endl;
1016   } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
1017     cout << endl << endl
1018          << highint << "This aircraft model is a " << fgblue << Release
1019          << reset << highint << " release." << endl << endl << reset;
1020   } else if (debug_lvl & 1) {
1021     cout << endl << endl
1022          << highint << "This aircraft model is an " << fgred << Release
1023          << reset << highint << " release!!!" << endl << endl << reset
1024          << "This aircraft model may not even properly load, and probably"
1025          << " will not fly as expected." << endl << endl
1026          << fgred << highint << "Use this model for development purposes ONLY!!!"
1027          << normint << reset << endl << endl;
1028   }
1029 
1030   return result;
1031 }
1032 
1033 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1034 
ReadChild(Element * el)1035 bool FGFDMExec::ReadChild(Element* el)
1036 {
1037   // Add a new childData object to the child FDM list
1038   // Populate that childData element with a new FDMExec object
1039   // Set the IsChild flag for that FDMExec object
1040   // Get the aircraft name
1041   // set debug level to print out no additional data for child objects
1042   // Load the model given the aircraft name
1043   // reset debug level to prior setting
1044 
1045   struct childData* child = new childData;
1046 
1047   child->exec = new FGFDMExec(Root, FDMctr);
1048   child->exec->SetChild(true);
1049 
1050   string childAircraft = el->GetAttributeValue("name");
1051   string sMated = el->GetAttributeValue("mated");
1052   if (sMated == "false") child->mated = false; // child objects are mated by default.
1053   string sInternal = el->GetAttributeValue("internal");
1054   if (sInternal == "true") child->internal = true; // child objects are external by default.
1055 
1056   child->exec->SetAircraftPath( AircraftPath );
1057   child->exec->SetEnginePath( EnginePath );
1058   child->exec->SetSystemsPath( SystemsPath );
1059   child->exec->LoadModel(childAircraft);
1060 
1061   Element* location = el->FindElement("location");
1062   if (location) {
1063     child->Loc = location->FindElementTripletConvertTo("IN");
1064   } else {
1065     cerr << endl << highint << fgred << "  No location was found for this child object!" << reset << endl;
1066     exit(-1);
1067   }
1068 
1069   Element* orientation = el->FindElement("orient");
1070   if (orientation) {
1071     child->Orient = orientation->FindElementTripletConvertTo("RAD");
1072   } else if (debug_lvl > 0) {
1073     cerr << endl << highint << "  No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
1074   }
1075 
1076   ChildFDMList.push_back(child);
1077 
1078   return true;
1079 }
1080 
1081 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 
GetPropertyManager(void)1083 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
1084 {
1085   return instance;
1086 }
1087 
1088 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089 
GetTrim(void)1090 FGTrim* FGFDMExec::GetTrim(void)
1091 {
1092   delete Trim;
1093   Trim = new FGTrim(this,tNone);
1094   return Trim;
1095 }
1096 
1097 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1098 
CheckIncrementalHold(void)1099 void FGFDMExec::CheckIncrementalHold(void)
1100 {
1101   // Only check if increment then hold is on
1102   if( IncrementThenHolding ) {
1103 
1104     if (TimeStepsUntilHold == 0) {
1105 
1106       // Should hold simulation if TimeStepsUntilHold has reached zero
1107       holding = true;
1108 
1109       // Still need to decrement TimeStepsUntilHold as value of -1
1110       // indicates that incremental then hold is turned off
1111       IncrementThenHolding = false;
1112       TimeStepsUntilHold--;
1113 
1114     } else if ( TimeStepsUntilHold > 0 ) {
1115       // Keep decrementing until 0 is reached
1116       TimeStepsUntilHold--;
1117     }
1118   }
1119 }
1120 
1121 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1122 
DoTrim(int mode)1123 void FGFDMExec::DoTrim(int mode)
1124 {
1125   if (Constructing) return;
1126 
1127   if (mode < 0 || mode > JSBSim::tNone)
1128     throw("Illegal trimming mode!");
1129 
1130   FGTrim trim(this, (JSBSim::TrimMode)mode);
1131   bool success = trim.DoTrim();
1132 
1133   if (debug_lvl > 0)
1134     trim.Report();
1135 
1136   if (!success)
1137     throw("Trim Failed");
1138 
1139   trim_completed = 1;
1140 }
1141 
1142 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1143 
SRand(int sr)1144 void FGFDMExec::SRand(int sr)
1145 {
1146   RandomSeed = sr;
1147   gaussian_random_number_phase = 0;
1148   srand(RandomSeed);
1149 }
1150 
1151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152 //    The bitmasked value choices are as follows:
1153 //    unset: In this case (the default) JSBSim would only print
1154 //       out the normally expected messages, essentially echoing
1155 //       the config files as they are read. If the environment
1156 //       variable is not set, debug_lvl is set to 1 internally
1157 //    0: This requests JSBSim not to output any messages
1158 //       whatsoever.
1159 //    1: This value explicity requests the normal JSBSim
1160 //       startup messages
1161 //    2: This value asks for a message to be printed out when
1162 //       a class is instantiated
1163 //    4: When this value is set, a message is displayed when a
1164 //       FGModel object executes its Run() method
1165 //    8: When this value is set, various runtime state variables
1166 //       are printed out periodically
1167 //    16: When set various parameters are sanity checked and
1168 //       a message is printed out when they go out of bounds
1169 
Debug(int from)1170 void FGFDMExec::Debug(int from)
1171 {
1172   if (debug_lvl <= 0) return;
1173 
1174   if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1175     if (from == 0) { // Constructor
1176       cout << "\n\n     "
1177            << "JSBSim Flight Dynamics Model v" << JSBSim_version << endl;
1178       cout << "            [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1179       cout << "JSBSim startup beginning ...\n\n";
1180       if (disperse == 1) cout << "Dispersions are ON." << endl << endl;
1181     } else if (from == 3) {
1182       cout << "\n\nJSBSim startup complete\n\n";
1183     }
1184   }
1185   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1186     if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1187     if (from == 1) cout << "Destroyed:    FGFDMExec" << endl;
1188   }
1189   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1190     if (from == 2) {
1191       cout << "================== Frame: " << Frame << "  Time: "
1192            << sim_time << " dt: " << dT << endl;
1193     }
1194   }
1195   if (debug_lvl & 8 ) { // Runtime state variables
1196   }
1197   if (debug_lvl & 16) { // Sanity checking
1198   }
1199   if (debug_lvl & 64) {
1200     if (from == 0) { // Constructor
1201     }
1202   }
1203 }
1204 }
1205