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