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