]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
Remove unused code
[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     element = document->GetElement();
518
519     result = true;
520     while (element && result) {
521       string element_name = element->GetName();
522       if (element_name == "fileheader" )           result = ReadFileHeader(element);
523       else if (element_name == "slave")            result = ReadSlave(element);
524       else if (element_name == "metrics")          result = Aircraft->Load(element);
525       else if (element_name == "mass_balance")     result = MassBalance->Load(element);
526       else if (element_name == "ground_reactions") result = GroundReactions->Load(element);
527       else if (element_name == "external_reactions") result = ExternalReactions->Load(element);
528       else if (element_name == "buoyant_forces")   result = BuoyantForces->Load(element);
529       else if (element_name == "propulsion")       result = Propulsion->Load(element);
530       else if (element_name == "system")           result = FCS->Load(element,
531                                                             FGFCS::stSystem);
532       else if (element_name == "autopilot")        result = FCS->Load(element,
533                                                             FGFCS::stAutoPilot);
534       else if (element_name == "flight_control")   result = FCS->Load(element,
535                                                             FGFCS::stFCS);
536       else if (element_name == "aerodynamics")     result = Aerodynamics->Load(element);
537       else if (element_name == "input")            result = Input->Load(element);
538       else if (element_name == "output")           {
539           FGOutput* Output = new FGOutput(this);
540           Output->InitModel();
541           Schedule(Output,       1);
542           result = Output->Load(element);
543           Outputs.push_back(Output);
544       }
545       else {
546         cerr << "Found unexpected subsystem: " << element_name << ", exiting." << endl;
547         result = false;
548         break;
549       }
550       element = document->GetNextElement();
551     }
552   } else {
553     cerr << fgred
554          << "  JSBSim failed to load aircraft model."
555          << fgdef << endl;
556     return false;
557   }
558
559   if (result) {
560     modelLoaded = true;
561     Debug(3);
562   } else {
563     cerr << fgred
564          << "  JSBSim failed to load properly."
565          << fgdef << endl;
566     return false;
567   }
568
569   struct PropertyCatalogStructure masterPCS;
570   masterPCS.base_string = "";
571   masterPCS.node = (FGPropertyManager*)Root;
572
573   BuildPropertyCatalog(&masterPCS);
574
575   return result;
576 }
577
578 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579
580 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
581 {
582   struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
583   int node_idx = 0;
584   char int_buf[10];
585
586   for (unsigned int i=0; i<pcs->node->nChildren(); i++) {
587     pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
588     node_idx = pcs->node->getChild(i)->getIndex();
589     sprintf(int_buf, "[%d]", node_idx);
590     if (node_idx != 0) pcsNew->base_string += string(int_buf);
591     if (pcs->node->getChild(i)->nChildren() == 0) {
592       if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
593         pcsNew->base_string = pcsNew->base_string.erase(0,12);
594       }
595       PropertyCatalog.push_back(pcsNew->base_string);
596     } else {
597       pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
598       BuildPropertyCatalog(pcsNew);
599     }
600   }
601   delete pcsNew;
602 }
603
604 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
605
606 string FGFDMExec::QueryPropertyCatalog(string in)
607 {
608   string results="";
609   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
610     if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
611   }
612   if (results.empty()) return "No matches found\n";
613   return results;
614 }
615
616 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617
618 void FGFDMExec::PrintPropertyCatalog(void)
619 {
620   cout << endl;
621   cout << "  " << fgblue << highint << underon << "Property Catalog for "
622        << modelName << reset << endl << endl;
623   for (unsigned i=0; i<PropertyCatalog.size(); i++) {
624     cout << "    " << PropertyCatalog[i] << endl;
625   }
626 }
627
628 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629
630 bool FGFDMExec::ReadFileHeader(Element* el)
631 {
632   bool result = true; // true for success
633
634   if (debug_lvl & ~1) return result;
635
636   if (el->FindElement("author"))
637     cout << "  Model Author:  " << el->FindElement("author")->GetDataLine() << endl;
638   if (el->FindElement("filecreationdate"))
639     cout << "  Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
640   if (el->FindElement("version"))
641     cout << "  Version:       " << el->FindElement("version")->GetDataLine() << endl;
642   if (el->FindElement("description"))
643     cout << "  Description:   " << el->FindElement("description")->GetDataLine() << endl;
644
645   return result;
646 }
647
648 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649
650 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
651 {
652   bool result = true; // true for success
653
654   if (!el) return false;
655
656   string AircraftName = el->GetAttributeValue("name");
657   Aircraft->SetAircraftName(AircraftName);
658
659   if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
660             << underoff << ": " << highint << AircraftName << normint << endl;
661
662   CFGVersion = el->GetAttributeValue("version");
663   Release    = el->GetAttributeValue("release");
664
665   if (debug_lvl & 1)
666     cout << "                            Version: " << highint << CFGVersion
667                                                     << normint << endl;
668   if (CFGVersion != needed_cfg_version) {
669     cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
670             " RESULTS WILL BE UNPREDICTABLE !!" << endl;
671     cerr << "Current version needed is: " << needed_cfg_version << endl;
672     cerr << "         You have version: " << CFGVersion << endl << fgdef << endl;
673     return false;
674   }
675
676   if (Release == "ALPHA" && (debug_lvl & 1)) {
677     cout << endl << endl
678          << highint << "This aircraft model is an " << fgred << Release
679          << reset << highint << " release!!!" << endl << endl << reset
680          << "This aircraft model may not even properly load, and probably"
681          << " will not fly as expected." << endl << endl
682          << fgred << highint << "Use this model for development purposes ONLY!!!"
683          << normint << reset << endl << endl;
684   } else if (Release == "BETA" && (debug_lvl & 1)) {
685     cout << endl << endl
686          << highint << "This aircraft model is a " << fgred << Release
687          << reset << highint << " release!!!" << endl << endl << reset
688          << "This aircraft model probably will not fly as expected." << endl << endl
689          << fgblue << highint << "Use this model for development purposes ONLY!!!"
690          << normint << reset << endl << endl;
691   } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
692     cout << endl << endl
693          << highint << "This aircraft model is a " << fgblue << Release
694          << reset << highint << " release." << endl << endl << reset;
695   } else if (debug_lvl & 1) {
696     cout << endl << endl
697          << highint << "This aircraft model is an " << fgred << Release
698          << reset << highint << " release!!!" << endl << endl << reset
699          << "This aircraft model may not even properly load, and probably"
700          << " will not fly as expected." << endl << endl
701          << fgred << highint << "Use this model for development purposes ONLY!!!"
702          << normint << reset << endl << endl;
703   }
704
705   return result;
706 }
707
708 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709
710 bool FGFDMExec::ReadSlave(Element* el)
711 {
712   // Add a new slaveData object to the slave FDM list
713   // Populate that slaveData element with a new FDMExec object
714   // Set the IsSlave flag for that FDMExec object
715   // Get the aircraft name
716   // set debug level to print out no additional data for slave objects
717   // Load the model given the aircraft name
718   // reset debug level to prior setting
719
720   int saved_debug_lvl = debug_lvl;
721   string token;
722
723   SlaveFDMList.push_back(new slaveData);
724   SlaveFDMList.back()->exec = new FGFDMExec();
725   SlaveFDMList.back()->exec->SetSlave(true);
726 /*
727   string AircraftName = AC_cfg->GetValue("file");
728
729   debug_lvl = 0;                 // turn off debug output for slave vehicle
730
731   SlaveFDMList.back()->exec->SetAircraftPath( AircraftPath );
732   SlaveFDMList.back()->exec->SetEnginePath( EnginePath );
733   SlaveFDMList.back()->exec->SetSystemsPath( SystemsPath );
734   SlaveFDMList.back()->exec->LoadModel(AircraftName);
735   debug_lvl = saved_debug_lvl;   // turn debug output back on for master vehicle
736
737   AC_cfg->GetNextConfigLine();
738   while ((token = AC_cfg->GetValue()) != string("/SLAVE")) {
739     *AC_cfg >> token;
740     if      (token == "xloc")  { *AC_cfg >> SlaveFDMList.back()->x;    }
741     else if (token == "yloc")  { *AC_cfg >> SlaveFDMList.back()->y;    }
742     else if (token == "zloc")  { *AC_cfg >> SlaveFDMList.back()->z;    }
743     else if (token == "pitch") { *AC_cfg >> SlaveFDMList.back()->pitch;}
744     else if (token == "yaw")   { *AC_cfg >> SlaveFDMList.back()->yaw;  }
745     else if (token == "roll")  { *AC_cfg >> SlaveFDMList.back()->roll;  }
746     else cerr << "Unknown identifier: " << token << " in slave vehicle definition" << endl;
747   }
748 */
749   if (debug_lvl > 0)  {
750     cout << "      X = " << SlaveFDMList.back()->x << endl;
751     cout << "      Y = " << SlaveFDMList.back()->y << endl;
752     cout << "      Z = " << SlaveFDMList.back()->z << endl;
753     cout << "      Pitch = " << SlaveFDMList.back()->pitch << endl;
754     cout << "      Yaw = " << SlaveFDMList.back()->yaw << endl;
755     cout << "      Roll = " << SlaveFDMList.back()->roll << endl;
756   }
757
758   return true;
759 }
760
761 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762
763 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
764 {
765   return instance;
766 }
767
768 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769
770 FGTrim* FGFDMExec::GetTrim(void)
771 {
772   delete Trim;
773   Trim = new FGTrim(this,tNone);
774   return Trim;
775 }
776
777 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778
779 void FGFDMExec::DisableOutput(void)
780 {
781   for (unsigned i=0; i<Outputs.size(); i++) {
782     Outputs[i]->Disable();
783   }
784 }
785
786 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787
788 void FGFDMExec::EnableOutput(void)
789 {
790   for (unsigned i=0; i<Outputs.size(); i++) {
791     Outputs[i]->Enable();
792   }
793 }
794
795 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796
797 bool FGFDMExec::SetOutputDirectives(string fname)
798 {
799   bool result;
800
801   FGOutput* Output = new FGOutput(this);
802   Output->SetDirectivesFile(fname);
803   Output->InitModel();
804   Schedule(Output,       1);
805   result = Output->Load(0);
806   Outputs.push_back(Output);
807
808   return result;
809 }
810
811 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812
813 void FGFDMExec::DoTrim(int mode)
814 {
815   double saved_time;
816
817   if (Constructing) return;
818
819   if (mode < 0 || mode > JSBSim::tNone) {
820     cerr << endl << "Illegal trimming mode!" << endl << endl;
821     return;
822   }
823   saved_time = State->Getsim_time();
824   FGTrim trim(this, (JSBSim::TrimMode)mode);
825   if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
826   trim.Report();
827   State->Setsim_time(saved_time);
828 }
829
830 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831 /*
832 void FGFDMExec::DoTrimAnalysis(int mode)
833 {
834   double saved_time;
835   if (Constructing) return;
836
837   if (mode < 0 || mode > JSBSim::taNone) {
838     cerr << endl << "Illegal trimming mode!" << endl << endl;
839     return;
840   }
841   saved_time = State->Getsim_time();
842
843   FGTrimAnalysis trimAnalysis(this, (JSBSim::TrimAnalysisMode)mode);
844
845   if ( !trimAnalysis.Load(IC->GetInitFile(), false) ) {
846     cerr << "A problem occurred with trim configuration file " << trimAnalysis.Load(IC->GetInitFile()) << endl;
847     exit(-1);
848   }
849
850   bool result = trimAnalysis.DoTrim();
851
852   if ( !result ) cerr << endl << "Trim Failed" << endl << endl;
853
854   trimAnalysis.Report();
855   State->Setsim_time(saved_time);
856
857   EnableOutput();
858   cout << "\nOutput: " << GetOutputFileName() << endl;
859
860 }
861 */
862 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863
864 void FGFDMExec::UseAtmosphereMSIS(void)
865 {
866   FGAtmosphere *oldAtmosphere = Atmosphere;
867   Atmosphere = new MSIS(this);
868   if (!Atmosphere->InitModel()) {
869     cerr << fgred << "MSIS Atmosphere model init failed" << fgdef << endl;
870     Error+=1;
871   }
872   delete oldAtmosphere;
873 }
874
875 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876
877 void FGFDMExec::UseAtmosphereMars(void)
878 {
879 /*
880   FGAtmosphere *oldAtmosphere = Atmosphere;
881   Atmosphere = new FGMars(this);
882   if (!Atmosphere->InitModel()) {
883     cerr << fgred << "Mars Atmosphere model init failed" << fgdef << endl;
884     Error+=1;
885   }
886   delete oldAtmosphere;
887 */
888 }
889
890 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 //    The bitmasked value choices are as follows:
892 //    unset: In this case (the default) JSBSim would only print
893 //       out the normally expected messages, essentially echoing
894 //       the config files as they are read. If the environment
895 //       variable is not set, debug_lvl is set to 1 internally
896 //    0: This requests JSBSim not to output any messages
897 //       whatsoever.
898 //    1: This value explicity requests the normal JSBSim
899 //       startup messages
900 //    2: This value asks for a message to be printed out when
901 //       a class is instantiated
902 //    4: When this value is set, a message is displayed when a
903 //       FGModel object executes its Run() method
904 //    8: When this value is set, various runtime state variables
905 //       are printed out periodically
906 //    16: When set various parameters are sanity checked and
907 //       a message is printed out when they go out of bounds
908
909 void FGFDMExec::Debug(int from)
910 {
911   if (debug_lvl <= 0) return;
912
913   if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
914     if (from == 0) { // Constructor
915       cout << "\n\n     " << highint << underon << "JSBSim Flight Dynamics Model v"
916                                      << JSBSim_version << underoff << normint << endl;
917       cout << halfint << "            [JSBSim-ML v" << needed_cfg_version << "]\n\n";
918       cout << normint << "JSBSim startup beginning ...\n\n";
919     } else if (from == 3) {
920       cout << "\n\nJSBSim startup complete\n\n";
921     }
922   }
923   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
924     if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
925     if (from == 1) cout << "Destroyed:    FGFDMExec" << endl;
926   }
927   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
928     if (from == 2) {
929       cout << "================== Frame: " << Frame << "  Time: "
930            << State->Getsim_time() << " dt: " << State->Getdt() << endl;
931     }
932   }
933   if (debug_lvl & 8 ) { // Runtime state variables
934   }
935   if (debug_lvl & 16) { // Sanity checking
936   }
937   if (debug_lvl & 64) {
938     if (from == 0) { // Constructor
939       cout << IdSrc << endl;
940       cout << IdHdr << endl;
941     }
942   }
943 }
944 }
945
946