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