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