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