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