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