]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
sync. with JSBSim again
[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 (jsb@hal-pc.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 "FGFDMExec.h"
45 #include "FGState.h"
46 #include <models/FGAtmosphere.h>
47 #include <models/atmosphere/FGMSIS.h>
48 #include <models/atmosphere/FGMars.h>
49 #include <models/FGFCS.h>
50 #include <models/FGPropulsion.h>
51 #include <models/FGMassBalance.h>
52 #include <models/FGGroundReactions.h>
53 #include <models/FGExternalReactions.h>
54 #include <models/FGBuoyantForces.h>
55 #include <models/FGAerodynamics.h>
56 #include <models/FGInertial.h>
57 #include <models/FGAircraft.h>
58 #include <models/FGPropagate.h>
59 #include <models/FGAuxiliary.h>
60 #include <models/FGInput.h>
61 #include <models/FGOutput.h>
62 #include <initialization/FGInitialCondition.h>
63 //#include <initialization/FGTrimAnalysis.h> // Remove until later
64 #include <input_output/FGPropertyManager.h>
65 #include <input_output/FGScript.h>
66
67 #include <iostream>
68 #include <iterator>
69
70 namespace JSBSim {
71
72 static const char *IdSrc = "$Id$";
73 static const char *IdHdr = ID_FDMEXEC;
74
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 GLOBAL DECLARATIONS
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
78
79 unsigned int FGFDMExec::FDMctr = 0;
80 FGPropertyManager* FGFDMExec::master=0;
81
82 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 CLASS IMPLEMENTATION
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85
86 void checkTied ( FGPropertyManager *node )
87 {
88   int N = node->nChildren();
89   string name;
90
91   for (int i=0; i<N; i++) {
92     if (node->getChild(i)->nChildren() ) {
93       checkTied( (FGPropertyManager*)node->getChild(i) );
94     } else if ( node->getChild(i)->isTied() ) {
95       name = ((FGPropertyManager*)node->getChild(i))->GetFullyQualifiedName();
96       node->Untie(name);
97     }
98   }
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 // Constructor
103
104 FGFDMExec::FGFDMExec(FGPropertyManager* root) : Root(root)
105 {
106
107   Frame           = 0;
108   FirstModel      = 0;
109   Error           = 0;
110   GroundCallback  = 0;
111   State           = 0;
112   Atmosphere      = 0;
113   FCS             = 0;
114   Propulsion      = 0;
115   MassBalance     = 0;
116   Aerodynamics    = 0;
117   Inertial        = 0;
118   GroundReactions = 0;
119   ExternalReactions = 0;
120   BuoyantForces   = 0;
121   Aircraft        = 0;
122   Propagate       = 0;
123   Auxiliary       = 0;
124   Input           = 0;
125   IC              = 0;
126   Trim            = 0;
127   Script          = 0;
128
129   modelLoaded = false;
130   IsChild = false;
131   holding = false;
132   Terminate = false;
133
134   IdFDM = FDMctr; // The main (parent) JSBSim instance is always the "zeroth"
135   FDMctr++;       // instance. "child" instances are loaded last.
136
137   try {
138     char* num = getenv("JSBSIM_DEBUG");
139     if (num) debug_lvl = atoi(num); // set debug level
140   } catch (...) {               // if error set to 1
141     debug_lvl = 1;
142   }
143
144   if (Root == 0) {
145     if (master == 0)
146       master = new FGPropertyManager;
147     Root = master;
148   }
149
150   instance = Root->GetNode("/fdm/jsbsim",IdFDM,true);
151   Debug(0);
152   // this is to catch errors in binding member functions to the property tree.
153   try {
154     Allocate();
155   } catch ( string msg ) {
156     cout << "Caught error: " << msg << endl;
157     exit(1);
158   }
159
160   trim_status = false;
161   ta_mode     = 99;
162
163   Constructing = true;
164   typedef int (FGFDMExec::*iPMF)(void) const;
165 //  instance->Tie("simulation/do_trim_analysis", this, (iPMF)0, &FGFDMExec::DoTrimAnalysis);
166   instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim);
167   instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions);
168   instance->Tie("simulation/terminate", (int *)&Terminate);
169   Constructing = false;
170 }
171
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173
174 FGFDMExec::~FGFDMExec()
175 {
176   try {
177     checkTied( instance );
178     DeAllocate();
179     if (Root == 0)  delete master;
180   } catch ( string msg ) {
181     cout << "Caught error: " << msg << endl;
182   }
183
184   for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
185   ChildFDMList.clear();
186
187   PropertyCatalog.clear();
188
189   FDMctr--;
190
191   Debug(1);
192 }
193
194 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195
196 bool FGFDMExec::Allocate(void)
197 {
198   bool result=true;
199
200   Atmosphere      = new FGAtmosphere(this);
201   FCS             = new FGFCS(this);
202   Propulsion      = new FGPropulsion(this);
203   MassBalance     = new FGMassBalance(this);
204   Aerodynamics    = new FGAerodynamics (this);
205   Inertial        = new FGInertial(this);
206
207   GroundCallback  = new FGGroundCallback(Inertial->GetRefRadius());
208
209   GroundReactions = new FGGroundReactions(this);
210   ExternalReactions = new FGExternalReactions(this);
211   BuoyantForces   = new FGBuoyantForces(this);
212   Aircraft        = new FGAircraft(this);
213   Propagate       = new FGPropagate(this);
214   Auxiliary       = new FGAuxiliary(this);
215   Input           = new FGInput(this);
216
217   State           = new FGState(this); // This must be done here, as the FGState
218                                        // class needs valid pointers to the above
219                                        // model classes
220
221   // Initialize models so they can communicate with each other
222
223   Atmosphere->InitModel();
224   FCS->InitModel();
225   Propulsion->InitModel();
226   MassBalance->InitModel();
227   Aerodynamics->InitModel();
228   Inertial->InitModel();
229   GroundReactions->InitModel();
230   ExternalReactions->InitModel();
231   BuoyantForces->InitModel();
232   Aircraft->InitModel();
233   Propagate->InitModel();
234   Auxiliary->InitModel();
235   Input->InitModel();
236
237   IC = new FGInitialCondition(this);
238
239   // Schedule a model. The second arg (the integer) is the pass number. For
240   // instance, the atmosphere model could get executed every fifth pass it is called
241   // by the executive. IC and Trim objects are NOT scheduled.
242
243   Schedule(Input,           1);
244   Schedule(Atmosphere,      1);
245   Schedule(FCS,             1);
246   Schedule(Propulsion,      1);
247   Schedule(MassBalance,     1);
248   Schedule(Aerodynamics,    1);
249   Schedule(Inertial,        1);
250   Schedule(GroundReactions, 1);
251   Schedule(ExternalReactions, 1);
252   Schedule(BuoyantForces,   1);
253   Schedule(Aircraft,        1);
254   Schedule(Propagate,       1);
255   Schedule(Auxiliary,       1);
256
257   modelLoaded = false;
258
259   return result;
260 }
261
262 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263
264 bool FGFDMExec::DeAllocate(void)
265 {
266   delete Input;
267   delete Atmosphere;
268   delete FCS;
269   delete Propulsion;
270   delete MassBalance;
271   delete Aerodynamics;
272   delete Inertial;
273   delete GroundReactions;
274   delete ExternalReactions;
275   delete BuoyantForces;
276   delete Aircraft;
277   delete Propagate;
278   delete Auxiliary;
279   delete State;
280   delete Script;
281
282   for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
283   Outputs.clear();
284
285   delete IC;
286   delete Trim;
287
288   delete GroundCallback;
289
290   FirstModel  = 0L;
291   Error       = 0;
292
293   State           = 0;
294   Input           = 0;
295   Atmosphere      = 0;
296   FCS             = 0;
297   Propulsion      = 0;
298   MassBalance     = 0;
299   Aerodynamics    = 0;
300   Inertial        = 0;
301   GroundReactions = 0;
302   ExternalReactions = 0;
303   BuoyantForces   = 0;
304   Aircraft        = 0;
305   Propagate       = 0;
306   Auxiliary       = 0;
307   Script          = 0;
308
309   modelLoaded = false;
310   return modelLoaded;
311 }
312
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314
315 int FGFDMExec::Schedule(FGModel* model, int rate)
316 {
317   FGModel* model_iterator;
318
319   model_iterator = FirstModel;
320
321   if (model_iterator == 0L) {                  // this is the first model
322
323     FirstModel = model;
324     FirstModel->NextModel = 0L;
325     FirstModel->SetRate(rate);
326
327   } else {                                     // subsequent model
328
329     while (model_iterator->NextModel != 0L) {
330       model_iterator = model_iterator->NextModel;
331     }
332     model_iterator->NextModel = model;
333     model_iterator->NextModel->SetRate(rate);
334
335   }
336
337   return 0;
338 }
339
340 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341
342 bool FGFDMExec::Run(void)
343 {
344   bool success=true;
345   FGModel* model_iterator;
346
347   model_iterator = FirstModel;
348   if (model_iterator == 0L) return false;
349
350   Debug(2);
351
352   for (unsigned int i=1; i<ChildFDMList.size(); i++) {
353     ChildFDMList[i]->AssignState(Propagate); // Transfer state to the child FDM
354     ChildFDMList[i]->Run();
355   }
356
357   // returns true if success
358   // false if complete
359   if (Script != 0 && !State->IntegrationSuspended()) success = Script->RunScript();
360
361   while (model_iterator != 0L) {
362     model_iterator->Run();
363     model_iterator = model_iterator->NextModel;
364   }
365
366   Frame++;
367   if (!Holding()) State->IncrTime();
368   if (Terminate) success = false;
369
370   return (success);
371 }
372
373 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 // This call will cause the sim time to reset to 0.0
375
376 bool FGFDMExec::RunIC(void)
377 {
378   State->SuspendIntegration();
379   State->Initialize(IC);
380   Run();
381   State->ResumeIntegration();
382
383   return true;
384 }
385
386 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 //
388 // A private, internal function call for Tie-ing to a property, so it needs an
389 // argument.
390
391 void FGFDMExec::ResetToInitialConditions(int mode)
392 {
393   if (mode == 1) {
394     for (unsigned int i=0; i<Outputs.size(); i++) {
395       Outputs[i]->SetStartNewFile(true); 
396     }
397   }
398   
399   ResetToInitialConditions();
400 }
401
402 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403
404 void FGFDMExec::ResetToInitialConditions(void)
405 {
406   FGModel* model_iterator;
407
408   model_iterator = FirstModel;
409   if (model_iterator == 0L || Constructing) return;
410
411   while (model_iterator != 0L) {
412     model_iterator->InitModel();
413     model_iterator = model_iterator->NextModel;
414   }
415
416   RunIC();
417   if (Script) Script->ResetEvents();
418 }
419
420 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421
422 void FGFDMExec::SetGroundCallback(FGGroundCallback* p)
423 {
424   delete GroundCallback;
425   GroundCallback = p;
426 }
427
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429
430 double FGFDMExec::GetSimTime(void)
431 {
432   return (State->Getsim_time());
433 }
434
435 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436
437 double FGFDMExec::GetDeltaT(void)
438 {
439   return (State->Getdt());
440 }
441
442 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443
444 vector <string> FGFDMExec::EnumerateFDMs(void)
445 {
446   vector <string> FDMList;
447
448   FDMList.push_back(Aircraft->GetAircraftName());
449
450   for (unsigned int i=1; i<ChildFDMList.size(); i++) {
451     FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
452   }
453
454   return FDMList;
455 }
456
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458
459 bool FGFDMExec::LoadScript(string script)
460 {
461   bool result;
462
463   Script = new FGScript(this);
464   result = Script->LoadScript(script);
465
466   return result;
467 }
468
469 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470
471 bool FGFDMExec::LoadModel(string AircraftPath, string EnginePath, string SystemsPath,
472                 string model, bool addModelToPath)
473 {
474   FGFDMExec::AircraftPath = AircraftPath;
475   FGFDMExec::EnginePath = EnginePath;
476   FGFDMExec::SystemsPath = SystemsPath;
477
478   return LoadModel(model, addModelToPath);
479 }
480
481 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482
483 bool FGFDMExec::LoadModel(string model, bool addModelToPath)
484 {
485   string token;
486   string aircraftCfgFileName;
487   string separator = "/";
488   Element* element = 0L;
489   bool result = false; // initialize result to false, indicating input file not yet read
490
491   modelName = model; // Set the class modelName attribute
492
493   if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
494     cerr << "Error: attempted to load aircraft with undefined ";
495     cerr << "aircraft, engine, and system paths" << endl;
496     return false;
497   }
498
499   FullAircraftPath = AircraftPath;
500   if (addModelToPath) FullAircraftPath += separator + model;
501   aircraftCfgFileName = FullAircraftPath + separator + model + ".xml";
502
503   if (modelLoaded) {
504     DeAllocate();
505     Allocate();
506   }
507
508   int saved_debug_lvl = debug_lvl;
509
510   document = LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
511   if (document) {
512     if (IsChild) debug_lvl = 0;
513
514     ReadPrologue(document);
515
516     if (IsChild) debug_lvl = saved_debug_lvl;
517
518     // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
519     element = document->FindElement("fileheader");
520     if (element) {
521       result = ReadFileHeader(element);
522       if (!result) {
523         cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
524         return result;
525       }
526     }
527
528     if (IsChild) debug_lvl = 0;
529
530     // Process the metrics element. This element is REQUIRED.
531     element = document->FindElement("metrics");
532     if (element) {
533       result = Aircraft->Load(element);
534       if (!result) {
535         cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
536         return result;
537       }
538     } else {
539       cerr << endl << "No metrics element was found in the aircraft config file." << endl;
540       return false;
541     }
542
543     // Process the mass_balance element. This element is REQUIRED.
544     element = document->FindElement("mass_balance");
545     if (element) {
546       result = MassBalance->Load(element);
547       if (!result) {
548         cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
549         return result;
550       }
551     } else {
552       cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
553       return false;
554     }
555
556     // Process the ground_reactions element. This element is REQUIRED.
557     element = document->FindElement("ground_reactions");
558     if (element) {
559       result = GroundReactions->Load(element);
560       if (!result) {
561         cerr << endl << "Aircraft ground_reactions element has problems in file " << aircraftCfgFileName << endl;
562         return result;
563       }
564     } else {
565       cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
566       return false;
567     }
568
569     // Process the external_reactions element. This element is OPTIONAL.
570     element = document->FindElement("external_reactions");
571     if (element) {
572       result = ExternalReactions->Load(element);
573       if (!result) {
574         cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
575         return result;
576       }
577     }
578
579     // Process the buoyant_forces element. This element is OPTIONAL.
580     element = document->FindElement("buoyant_forces");
581     if (element) {
582       result = BuoyantForces->Load(element);
583       if (!result) {
584         cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
585         return result;
586       }
587     }
588
589     // Process the propulsion element. This element is OPTIONAL.
590     element = document->FindElement("propulsion");
591     if (element) {
592       result = Propulsion->Load(element);
593       if (!result) {
594         cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
595         return result;
596       }
597     }
598
599     // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
600     element = document->FindElement("system");
601     while (element) {
602       result = FCS->Load(element, FGFCS::stSystem);
603       if (!result) {
604         cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
605         return result;
606       }
607       element = document->FindNextElement("system");
608     }
609
610     // Process the autopilot element. This element is OPTIONAL.
611     element = document->FindElement("autopilot");
612     if (element) {
613       result = FCS->Load(element, FGFCS::stAutoPilot);
614       if (!result) {
615         cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
616         return result;
617       }
618     }
619
620     // Process the flight_control element. This element is OPTIONAL.
621     element = document->FindElement("flight_control");
622     if (element) {
623       result = FCS->Load(element, FGFCS::stFCS);
624       if (!result) {
625         cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
626         return result;
627       }
628     }
629
630     // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
631     element = document->FindElement("aerodynamics");
632     if (element) {
633       result = Aerodynamics->Load(element);
634       if (!result) {
635         cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
636         return result;
637       }
638     } else {
639       cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
640     }
641
642     // Process the input element. This element is OPTIONAL.
643     element = document->FindElement("input");
644     if (element) {
645       result = Input->Load(element);
646       if (!result) {
647         cerr << endl << "Aircraft input element has problems in file " << aircraftCfgFileName << endl;
648         return result;
649       }
650     }
651
652     // Process the output element[s]. This element is OPTIONAL, and there may be more than one.
653     element = document->FindElement("output");
654     while (element) {
655       FGOutput* Output = new FGOutput(this);
656       Output->InitModel();
657       Schedule(Output, 1);
658       result = Output->Load(element);
659       Outputs.push_back(Output);
660       if (!result) {
661         cerr << endl << "Aircraft output element has problems in file " << aircraftCfgFileName << endl;
662         return result;
663       }
664       element = document->FindNextElement("output");
665     }
666
667     // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
668     element = document->FindElement("child");
669     if (element) {
670       result = ReadChild(element);
671       if (!result) {
672         cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
673         return result;
674       }
675     }
676
677     modelLoaded = true;
678
679     if (debug_lvl > 0) {
680       cout << endl << fgblue << highint
681            << "End of vehicle configuration loading." << endl
682            << "-------------------------------------------------------------------------------"
683            << reset << endl;
684     }
685     
686     if (IsChild) debug_lvl = saved_debug_lvl;
687
688   } else {
689     cerr << fgred
690          << "  JSBSim failed to open the configuration file: " << aircraftCfgFileName
691          << fgdef << endl;
692   }
693
694   struct PropertyCatalogStructure masterPCS;
695   masterPCS.base_string = "";
696   masterPCS.node = (FGPropertyManager*)Root;
697
698   BuildPropertyCatalog(&masterPCS);
699
700   return result;
701 }
702
703 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704
705 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
706 {
707   struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
708   int node_idx = 0;
709   char int_buf[10];
710
711   for (unsigned int i=0; i<pcs->node->nChildren(); i++) {
712     pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
713     node_idx = pcs->node->getChild(i)->getIndex();
714     sprintf(int_buf, "[%d]", node_idx);
715     if (node_idx != 0) pcsNew->base_string += string(int_buf);
716     if (pcs->node->getChild(i)->nChildren() == 0) {
717       if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
718         pcsNew->base_string = pcsNew->base_string.erase(0,12);
719       }
720       PropertyCatalog.push_back(pcsNew->base_string);
721     } else {
722       pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
723       BuildPropertyCatalog(pcsNew);
724     }
725   }
726   delete pcsNew;
727 }
728
729 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730
731 string FGFDMExec::QueryPropertyCatalog(string in)
732 {
733   string results="";
734   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
735     if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
736   }
737   if (results.empty()) return "No matches found\n";
738   return results;
739 }
740
741 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
742
743 void FGFDMExec::PrintPropertyCatalog(void)
744 {
745   cout << endl;
746   cout << "  " << fgblue << highint << underon << "Property Catalog for "
747        << modelName << reset << endl << endl;
748   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
749     cout << "    " << PropertyCatalog[i] << endl;
750   }
751 }
752
753 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754
755 bool FGFDMExec::ReadFileHeader(Element* el)
756 {
757   bool result = true; // true for success
758
759   if (debug_lvl == 0) return result;
760
761   if (IsChild) {
762     cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
763   }
764
765   if (el->FindElement("description"))
766     cout << "  Description:   " << el->FindElement("description")->GetDataLine() << endl;
767   if (el->FindElement("author"))
768     cout << "  Model Author:  " << el->FindElement("author")->GetDataLine() << endl;
769   if (el->FindElement("filecreationdate"))
770     cout << "  Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
771   if (el->FindElement("version"))
772     cout << "  Version:       " << el->FindElement("version")->GetDataLine() << endl;
773
774   return result;
775 }
776
777 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778
779 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
780 {
781   bool result = true; // true for success
782
783   if (!el) return false;
784
785   string AircraftName = el->GetAttributeValue("name");
786   Aircraft->SetAircraftName(AircraftName);
787
788   if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
789             << underoff << ": " << highint << AircraftName << normint << endl;
790
791   CFGVersion = el->GetAttributeValue("version");
792   Release    = el->GetAttributeValue("release");
793
794   if (debug_lvl & 1)
795     cout << "                            Version: " << highint << CFGVersion
796                                                     << normint << endl;
797   if (CFGVersion != needed_cfg_version) {
798     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
799             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
800     cerr << "Current version needed is: " << needed_cfg_version << endl;
801     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
802     return false;
803   }
804
805   if (Release == "ALPHA" && (debug_lvl & 1)) {
806     cout << endl << endl
807          << highint << "This aircraft model is an " << fgred << Release
808          << reset << highint << " release!!!" << endl << endl << reset
809          << "This aircraft model may not even properly load, and probably"
810          << " will not fly as expected." << endl << endl
811          << fgred << highint << "Use this model for development purposes ONLY!!!"
812          << normint << reset << endl << endl;
813   } else if (Release == "BETA" && (debug_lvl & 1)) {
814     cout << endl << endl
815          << highint << "This aircraft model is a " << fgred << Release
816          << reset << highint << " release!!!" << endl << endl << reset
817          << "This aircraft model probably will not fly as expected." << endl << endl
818          << fgblue << highint << "Use this model for development purposes ONLY!!!"
819          << normint << reset << endl << endl;
820   } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
821     cout << endl << endl
822          << highint << "This aircraft model is a " << fgblue << Release
823          << reset << highint << " release." << endl << endl << reset;
824   } else if (debug_lvl & 1) {
825     cout << endl << endl
826          << highint << "This aircraft model is an " << fgred << Release
827          << reset << highint << " release!!!" << endl << endl << reset
828          << "This aircraft model may not even properly load, and probably"
829          << " will not fly as expected." << endl << endl
830          << fgred << highint << "Use this model for development purposes ONLY!!!"
831          << normint << reset << endl << endl;
832   }
833
834   return result;
835 }
836
837 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838
839 bool FGFDMExec::ReadChild(Element* el)
840 {
841   // Add a new childData object to the child FDM list
842   // Populate that childData element with a new FDMExec object
843   // Set the IsChild flag for that FDMExec object
844   // Get the aircraft name
845   // set debug level to print out no additional data for child objects
846   // Load the model given the aircraft name
847   // reset debug level to prior setting
848
849   string token;
850
851   struct childData* child = new childData;
852
853   child->exec = new FGFDMExec();
854   child->exec->SetChild(true);
855
856   string childAircraft = el->GetAttributeValue("name");
857   string sMated = el->GetAttributeValue("mated");
858   if (sMated == "false") child->mated = false; // child objects are mated by default.
859   string sInternal = el->GetAttributeValue("internal");
860   if (sInternal == "true") child->internal = true; // child objects are external by default.
861
862   child->exec->SetAircraftPath( AircraftPath );
863   child->exec->SetEnginePath( EnginePath );
864   child->exec->SetSystemsPath( SystemsPath );
865   child->exec->LoadModel(childAircraft);
866
867   Element* location = el->FindElement("location");
868   if (location) {
869     child->Loc = location->FindElementTripletConvertTo("IN");
870   } else {
871     cerr << endl << highint << fgred << "  No location was found for this child object!" << reset << endl;
872     exit(-1);
873   }
874   
875   Element* orientation = el->FindElement("orient");
876   if (orientation) {
877     child->Orient = orientation->FindElementTripletConvertTo("RAD");
878   } else if (debug_lvl > 0) {
879     cerr << endl << highint << "  No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
880   }
881
882   ChildFDMList.push_back(child);
883
884   return true;
885 }
886
887 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888
889 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
890 {
891   return instance;
892 }
893
894 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895
896 FGTrim* FGFDMExec::GetTrim(void)
897 {
898   delete Trim;
899   Trim = new FGTrim(this,tNone);
900   return Trim;
901 }
902
903 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904
905 void FGFDMExec::DisableOutput(void)
906 {
907   for (unsigned i=0; i<Outputs.size(); i++) {
908     Outputs[i]->Disable();
909   }
910 }
911
912 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913
914 void FGFDMExec::EnableOutput(void)
915 {
916   for (unsigned i=0; i<Outputs.size(); i++) {
917     Outputs[i]->Enable();
918   }
919 }
920
921 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
922
923 bool FGFDMExec::SetOutputDirectives(string fname)
924 {
925   bool result;
926
927   FGOutput* Output = new FGOutput(this);
928   Output->SetDirectivesFile(fname);
929   Output->InitModel();
930   Schedule(Output,       1);
931   result = Output->Load(0);
932   Outputs.push_back(Output);
933
934   return result;
935 }
936
937 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938
939 void FGFDMExec::DoTrim(int mode)
940 {
941   double saved_time;
942
943   if (Constructing) return;
944
945   if (mode < 0 || mode > JSBSim::tNone) {
946     cerr << endl << "Illegal trimming mode!" << endl << endl;
947     return;
948   }
949   saved_time = State->Getsim_time();
950   FGTrim trim(this, (JSBSim::TrimMode)mode);
951   if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
952   trim.Report();
953   State->Setsim_time(saved_time);
954 }
955
956 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 /*
958 void FGFDMExec::DoTrimAnalysis(int mode)
959 {
960   double saved_time;
961   if (Constructing) return;
962
963   if (mode < 0 || mode > JSBSim::taNone) {
964     cerr << endl << "Illegal trimming mode!" << endl << endl;
965     return;
966   }
967   saved_time = State->Getsim_time();
968
969   FGTrimAnalysis trimAnalysis(this, (JSBSim::TrimAnalysisMode)mode);
970
971   if ( !trimAnalysis.Load(IC->GetInitFile(), false) ) {
972     cerr << "A problem occurred with trim configuration file " << trimAnalysis.Load(IC->GetInitFile()) << endl;
973     exit(-1);
974   }
975
976   bool result = trimAnalysis.DoTrim();
977
978   if ( !result ) cerr << endl << "Trim Failed" << endl << endl;
979
980   trimAnalysis.Report();
981   State->Setsim_time(saved_time);
982
983   EnableOutput();
984   cout << "\nOutput: " << GetOutputFileName() << endl;
985
986 }
987 */
988 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989
990 void FGFDMExec::UseAtmosphereMSIS(void)
991 {
992   FGAtmosphere *oldAtmosphere = Atmosphere;
993   Atmosphere = new MSIS(this);
994   if (!Atmosphere->InitModel()) {
995     cerr << fgred << "MSIS Atmosphere model init failed" << fgdef << endl;
996     Error+=1;
997   }
998   delete oldAtmosphere;
999 }
1000
1001 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1002
1003 void FGFDMExec::UseAtmosphereMars(void)
1004 {
1005 /*
1006   FGAtmosphere *oldAtmosphere = Atmosphere;
1007   Atmosphere = new FGMars(this);
1008   if (!Atmosphere->InitModel()) {
1009     cerr << fgred << "Mars Atmosphere model init failed" << fgdef << endl;
1010     Error+=1;
1011   }
1012   delete oldAtmosphere;
1013 */
1014 }
1015
1016 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 //    The bitmasked value choices are as follows:
1018 //    unset: In this case (the default) JSBSim would only print
1019 //       out the normally expected messages, essentially echoing
1020 //       the config files as they are read. If the environment
1021 //       variable is not set, debug_lvl is set to 1 internally
1022 //    0: This requests JSBSim not to output any messages
1023 //       whatsoever.
1024 //    1: This value explicity requests the normal JSBSim
1025 //       startup messages
1026 //    2: This value asks for a message to be printed out when
1027 //       a class is instantiated
1028 //    4: When this value is set, a message is displayed when a
1029 //       FGModel object executes its Run() method
1030 //    8: When this value is set, various runtime state variables
1031 //       are printed out periodically
1032 //    16: When set various parameters are sanity checked and
1033 //       a message is printed out when they go out of bounds
1034
1035 void FGFDMExec::Debug(int from)
1036 {
1037   if (debug_lvl <= 0) return;
1038
1039   if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1040     if (from == 0) { // Constructor
1041       cout << "\n\n     " << highint << underon << "JSBSim Flight Dynamics Model v"
1042                                      << JSBSim_version << underoff << normint << endl;
1043       cout << halfint << "            [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1044       cout << normint << "JSBSim startup beginning ...\n\n";
1045     } else if (from == 3) {
1046       cout << "\n\nJSBSim startup complete\n\n";
1047     }
1048   }
1049   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1050     if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1051     if (from == 1) cout << "Destroyed:    FGFDMExec" << endl;
1052   }
1053   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1054     if (from == 2) {
1055       cout << "================== Frame: " << Frame << "  Time: "
1056            << State->Getsim_time() << " dt: " << State->Getdt() << endl;
1057     }
1058   }
1059   if (debug_lvl & 8 ) { // Runtime state variables
1060   }
1061   if (debug_lvl & 16) { // Sanity checking
1062   }
1063   if (debug_lvl & 64) {
1064     if (from == 0) { // Constructor
1065       cout << IdSrc << endl;
1066       cout << IdHdr << endl;
1067     }
1068   }
1069 }
1070 }
1071
1072