]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGFDMExec.cpp
Sync. w. JSBSim cvs
[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/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions);
176   instance->Tie("simulation/terminate", (int *)&Terminate);
177   Constructing = false;
178 }
179
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181
182 FGFDMExec::~FGFDMExec()
183 {
184   try {
185     checkTied( instance );
186     DeAllocate();
187     if (Root == 0)  delete master;
188   } catch ( string msg ) {
189     cout << "Caught error: " << msg << endl;
190   }
191
192   for (unsigned int i=1; i<SlaveFDMList.size(); i++) delete SlaveFDMList[i]->exec;
193   SlaveFDMList.clear();
194
195   //ToDo remove property catalog.
196
197   Debug(1);
198 }
199
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201
202 bool FGFDMExec::Allocate(void)
203 {
204   bool result=true;
205
206   Atmosphere      = new FGAtmosphere(this);
207   FCS             = new FGFCS(this);
208   Propulsion      = new FGPropulsion(this);
209   MassBalance     = new FGMassBalance(this);
210   Aerodynamics    = new FGAerodynamics (this);
211   Inertial        = new FGInertial(this);
212
213   GroundCallback  = new FGGroundCallback(Inertial->GetRefRadius());
214
215   GroundReactions = new FGGroundReactions(this);
216   ExternalReactions = new FGExternalReactions(this);
217   BuoyantForces   = new FGBuoyantForces(this);
218   Aircraft        = new FGAircraft(this);
219   Propagate       = new FGPropagate(this);
220   Auxiliary       = new FGAuxiliary(this);
221   Input           = new FGInput(this);
222
223   State           = new FGState(this); // This must be done here, as the FGState
224                                        // class needs valid pointers to the above
225                                        // model classes
226
227   // Initialize models so they can communicate with each other
228
229   Atmosphere->InitModel();
230   FCS->InitModel();
231   Propulsion->InitModel();
232   MassBalance->InitModel();
233   Aerodynamics->InitModel();
234   Inertial->InitModel();
235   GroundReactions->InitModel();
236   ExternalReactions->InitModel();
237   BuoyantForces->InitModel();
238   Aircraft->InitModel();
239   Propagate->InitModel();
240   Auxiliary->InitModel();
241   Input->InitModel();
242
243   IC = new FGInitialCondition(this);
244
245   // Schedule a model. The second arg (the integer) is the pass number. For
246   // instance, the atmosphere model could get executed every fifth pass it is called
247   // by the executive. IC and Trim objects are NOT scheduled.
248
249   Schedule(Input,           1);
250   Schedule(Atmosphere,      1);
251   Schedule(FCS,             1);
252   Schedule(Propulsion,      1);
253   Schedule(MassBalance,     1);
254   Schedule(Aerodynamics,    1);
255   Schedule(Inertial,        1);
256   Schedule(GroundReactions, 1);
257   Schedule(ExternalReactions, 1);
258   Schedule(BuoyantForces,   1);
259   Schedule(Aircraft,        1);
260   Schedule(Propagate,       1);
261   Schedule(Auxiliary,       1);
262
263   modelLoaded = false;
264
265   return result;
266 }
267
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269
270 bool FGFDMExec::DeAllocate(void)
271 {
272   delete Input;
273   delete Atmosphere;
274   delete FCS;
275   delete Propulsion;
276   delete MassBalance;
277   delete Aerodynamics;
278   delete Inertial;
279   delete GroundReactions;
280   delete ExternalReactions;
281   delete BuoyantForces;
282   delete Aircraft;
283   delete Propagate;
284   delete Auxiliary;
285   delete State;
286   delete Script;
287
288   for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
289   Outputs.clear();
290
291   delete IC;
292   delete Trim;
293
294   delete GroundCallback;
295
296   FirstModel  = 0L;
297   Error       = 0;
298
299   State           = 0;
300   Input           = 0;
301   Atmosphere      = 0;
302   FCS             = 0;
303   Propulsion      = 0;
304   MassBalance     = 0;
305   Aerodynamics    = 0;
306   Inertial        = 0;
307   GroundReactions = 0;
308   ExternalReactions = 0;
309   BuoyantForces   = 0;
310   Aircraft        = 0;
311   Propagate       = 0;
312   Auxiliary       = 0;
313   Script          = 0;
314
315   modelLoaded = false;
316   return modelLoaded;
317 }
318
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320
321 int FGFDMExec::Schedule(FGModel* model, int rate)
322 {
323   FGModel* model_iterator;
324
325   model_iterator = FirstModel;
326
327   if (model_iterator == 0L) {                  // this is the first model
328
329     FirstModel = model;
330     FirstModel->NextModel = 0L;
331     FirstModel->SetRate(rate);
332
333   } else {                                     // subsequent model
334
335     while (model_iterator->NextModel != 0L) {
336       model_iterator = model_iterator->NextModel;
337     }
338     model_iterator->NextModel = model;
339     model_iterator->NextModel->SetRate(rate);
340
341   }
342
343   return 0;
344 }
345
346 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347
348 bool FGFDMExec::Run(void)
349 {
350   bool success=true;
351   FGModel* model_iterator;
352
353   model_iterator = FirstModel;
354   if (model_iterator == 0L) return false;
355
356   Debug(2);
357
358   for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
359 //    SlaveFDMList[i]->exec->State->Initialize(); // Transfer state to the slave FDM
360 //    SlaveFDMList[i]->exec->Run();
361   }
362
363   // returns true if success
364   // false if complete
365   if (Script != 0 && !State->IntegrationSuspended()) success = Script->RunScript();
366
367   while (model_iterator != 0L) {
368     model_iterator->Run();
369     model_iterator = model_iterator->NextModel;
370   }
371
372   Frame++;
373   if (!Holding()) State->IncrTime();
374   if (Terminate) success = false;
375
376   return (success);
377 }
378
379 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 // This call will cause the sim time to reset to 0.0
381
382 bool FGFDMExec::RunIC(void)
383 {
384   State->SuspendIntegration();
385   State->Initialize(IC);
386   Run();
387   State->ResumeIntegration();
388
389   return true;
390 }
391
392 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 //
394 // A private, internal function call for Tie-ing to a property, so it needs an
395 // argument. Nothing is done with the argument, yet.
396
397 void FGFDMExec::ResetToInitialConditions(int mode)
398 {
399   ResetToInitialConditions();
400 }
401
402 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403
404 void FGFDMExec::ResetToInitialConditions(void)
405 {
406   FGModel* model_iterator;
407
408   model_iterator = FirstModel;
409   if (model_iterator == 0L) return;
410
411   while (model_iterator != 0L) {
412     model_iterator->InitModel();
413     model_iterator = model_iterator->NextModel;
414   }
415
416   RunIC();
417   if (Script) Script->ResetEvents();
418 }
419
420 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421
422 void FGFDMExec::SetGroundCallback(FGGroundCallback* p)
423 {
424   delete GroundCallback;
425   GroundCallback = p;
426 }
427
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429
430 double FGFDMExec::GetSimTime(void)
431 {
432   return (State->Getsim_time());
433 }
434
435 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436
437 double FGFDMExec::GetDeltaT(void)
438 {
439   return (State->Getdt());
440 }
441
442 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443
444 vector <string> FGFDMExec::EnumerateFDMs(void)
445 {
446   vector <string> FDMList;
447
448   FDMList.push_back(Aircraft->GetAircraftName());
449
450   for (unsigned int i=1; i<SlaveFDMList.size(); i++) {
451     FDMList.push_back(SlaveFDMList[i]->exec->GetAircraft()->GetAircraftName());
452   }
453
454   return FDMList;
455 }
456
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458
459 bool FGFDMExec::LoadScript(string script)
460 {
461   bool result;
462
463   Script = new FGScript(this);
464   result = Script->LoadScript(script);
465
466   return result;
467 }
468
469 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470
471 bool FGFDMExec::LoadModel(string AircraftPath, string EnginePath, string SystemsPath,
472                 string model, bool addModelToPath)
473 {
474   FGFDMExec::AircraftPath = AircraftPath;
475   FGFDMExec::EnginePath = EnginePath;
476   FGFDMExec::SystemsPath = SystemsPath;
477
478   return LoadModel(model, addModelToPath);
479 }
480
481 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482
483 bool FGFDMExec::LoadModel(string model, bool addModelToPath)
484 {
485   string token;
486   string aircraftCfgFileName;
487   string separator = "/";
488   Element* element = 0L;
489   bool result = false; // initialize result to false, indicating input file not yet read
490
491   modelName = model; // Set the class modelName attribute
492
493   if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
494     cerr << "Error: attempted to load aircraft with undefined ";
495     cerr << "aircraft, engine, and system paths" << endl;
496     return false;
497   }
498
499   FullAircraftPath = AircraftPath;
500   if (addModelToPath) FullAircraftPath += separator + model;
501   aircraftCfgFileName = FullAircraftPath + separator + model + ".xml";
502
503   if (modelLoaded) {
504     DeAllocate();
505     Allocate();
506   }
507
508   document = LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
509   if (document) {
510     ReadPrologue(document);
511     element = document->GetElement();
512
513     result = true;
514     while (element && result) {
515       string element_name = element->GetName();
516       if (element_name == "fileheader" )           result = ReadFileHeader(element);
517       else if (element_name == "slave")            result = ReadSlave(element);
518       else if (element_name == "metrics")          result = Aircraft->Load(element);
519       else if (element_name == "mass_balance")     result = MassBalance->Load(element);
520       else if (element_name == "ground_reactions") result = GroundReactions->Load(element);
521       else if (element_name == "external_reactions") result = ExternalReactions->Load(element);
522       else if (element_name == "buoyant_forces")   result = BuoyantForces->Load(element);
523       else if (element_name == "propulsion")       result = Propulsion->Load(element);
524       else if (element_name == "system")           result = FCS->Load(element,
525                                                             FGFCS::stSystem);
526       else if (element_name == "autopilot")        result = FCS->Load(element,
527                                                             FGFCS::stAutoPilot);
528       else if (element_name == "flight_control")   result = FCS->Load(element,
529                                                             FGFCS::stFCS);
530       else if (element_name == "aerodynamics")     result = Aerodynamics->Load(element);
531       else if (element_name == "input")            result = Input->Load(element);
532       else if (element_name == "output")           {
533           FGOutput* Output = new FGOutput(this);
534           Output->InitModel();
535           Schedule(Output,       1);
536           result = Output->Load(element);
537           Outputs.push_back(Output);
538       }
539       else {
540         cerr << "Found unexpected subsystem: " << element_name << ", exiting." << endl;
541         result = false;
542         break;
543       }
544       element = document->GetNextElement();
545     }
546   } else {
547     cerr << fgred
548          << "  JSBSim failed to load aircraft model."
549          << fgdef << endl;
550     return false;
551   }
552
553   if (result) {
554     modelLoaded = true;
555     Debug(3);
556   } else {
557     cerr << fgred
558          << "  JSBSim failed to load properly."
559          << fgdef << endl;
560     return false;
561   }
562
563   struct PropertyCatalogStructure masterPCS;
564   masterPCS.base_string = "";
565   masterPCS.node = (FGPropertyManager*)Root;
566
567   BuildPropertyCatalog(&masterPCS);
568
569   return result;
570 }
571
572 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573
574 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
575 {
576   struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
577   int node_idx = 0;
578   char int_buf[10];
579
580   for (unsigned int i=0; i<pcs->node->nChildren(); i++) {
581     pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
582     node_idx = pcs->node->getChild(i)->getIndex();
583     sprintf(int_buf, "[%d]", node_idx);
584     if (node_idx != 0) pcsNew->base_string += string(int_buf);
585     if (pcs->node->getChild(i)->nChildren() == 0) {
586       if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
587         pcsNew->base_string = pcsNew->base_string.erase(0,12);
588       }
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