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