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