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