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