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