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