1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Schedules and runs the model routines.
8 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
30 This class wraps up the simulation scheduling routines.
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 COMMENTS, REFERENCES, and NOTES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 #include "FGFDMExec.h"
45 #include "models/FGAtmosphere.h"
46 #include "models/atmosphere/FGMSIS.h"
47 #include "models/atmosphere/FGMars.h"
48 #include "models/FGFCS.h"
49 #include "models/FGPropulsion.h"
50 #include "models/FGMassBalance.h"
51 #include "models/FGGroundReactions.h"
52 #include "models/FGExternalReactions.h"
53 #include "models/FGBuoyantForces.h"
54 #include "models/FGAerodynamics.h"
55 #include "models/FGInertial.h"
56 #include "models/FGAircraft.h"
57 #include "models/FGPropagate.h"
58 #include "models/FGAuxiliary.h"
59 #include "models/FGInput.h"
60 #include "models/FGOutput.h"
61 #include "initialization/FGInitialCondition.h"
62 //#include "initialization/FGTrimAnalysis.h" // Remove until later
63 #include "input_output/FGPropertyManager.h"
64 #include "input_output/FGScript.h"
74 static const char *IdSrc = "$Id: FGFDMExec.cpp,v 1.82 2010/10/07 03:17:29 jberndt Exp $";
75 static const char *IdHdr = ID_FDMEXEC;
77 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
81 void checkTied ( FGPropertyManager *node )
83 int N = node->nChildren();
86 for (int i=0; i<N; i++) {
87 if (node->getChild(i)->nChildren() ) {
88 checkTied( (FGPropertyManager*)node->getChild(i) );
90 if ( node->getChild(i)->isTied() ) {
91 name = ((FGPropertyManager*)node->getChild(i))->GetFullyQualifiedName();
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 FGFDMExec::FGFDMExec(FGPropertyManager* root) : Root(root), delete_root(false)
101 FDMctr = new unsigned int;
104 root_overload = (root != NULL);
107 FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) : Root(root), delete_root(false), FDMctr(fdmctr)
110 root_overload = (root != NULL);
113 void FGFDMExec::Initialize()
125 ExternalReactions = 0;
143 dT = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is
144 // run in standalone mode with no initialization file.
147 char* num = getenv("JSBSIM_DEBUG");
148 if (num) debug_lvl = atoi(num); // set debug level
149 } catch (...) { // if error set to 1
153 if (Root == 0) { // Then this is the root FDM
154 Root = new FGPropertyManager; // Create the property manager
157 FDMctr = new unsigned int; // Create and initialize the child FDM counter
161 // Store this FDM's ID
162 IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth"
164 // Prepare FDMctr for the next child FDM id
165 (*FDMctr)++; // instance. "child" instances are loaded last.
167 instance = Root->GetNode("/fdm/jsbsim",IdFDM,true);
169 // this is to catch errors in binding member functions to the property tree.
172 } catch ( string msg ) {
173 cout << "Caught error: " << msg << endl;
181 typedef int (FGFDMExec::*iPMF)(void) const;
182 // instance->Tie("simulation/do_trim_analysis", this, (iPMF)0, &FGFDMExec::DoTrimAnalysis);
183 instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim);
184 instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions);
185 instance->Tie("simulation/terminate", (int *)&Terminate);
186 instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime);
187 instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel);
189 Constructing = false;
192 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 FGFDMExec::~FGFDMExec()
197 checkTied( instance );
200 if(FDMctr != 0 && !root_overload) {
206 if (IdFDM == 0) { // Meaning this is no child FDM
211 } catch ( string msg ) {
212 cout << "Caught error: " << msg << endl;
215 for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
216 ChildFDMList.clear();
218 PropertyCatalog.clear();
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 bool FGFDMExec::Allocate(void)
231 Atmosphere = new FGAtmosphere(this);
232 FCS = new FGFCS(this);
233 Propulsion = new FGPropulsion(this);
234 MassBalance = new FGMassBalance(this);
235 Aerodynamics = new FGAerodynamics (this);
236 Inertial = new FGInertial(this);
238 GroundCallback = new FGGroundCallback(Inertial->GetRefRadius());
240 GroundReactions = new FGGroundReactions(this);
241 ExternalReactions = new FGExternalReactions(this);
242 BuoyantForces = new FGBuoyantForces(this);
243 Aircraft = new FGAircraft(this);
244 Propagate = new FGPropagate(this);
245 Auxiliary = new FGAuxiliary(this);
246 Input = new FGInput(this);
248 // Schedule a model. The second arg (the integer) is the pass number. For
249 // instance, the atmosphere model could get executed every fifth pass it is called.
252 Schedule(Atmosphere, 1);
254 Schedule(Propulsion, 1);
255 Schedule(MassBalance, 1);
256 Schedule(Aerodynamics, 1);
257 Schedule(Inertial, 1);
258 Schedule(GroundReactions, 1);
259 Schedule(ExternalReactions, 1);
260 Schedule(BuoyantForces, 1);
261 Schedule(Aircraft, 1);
262 Schedule(Propagate, 1);
263 Schedule(Auxiliary, 1);
265 // Initialize models so they can communicate with each other
267 vector <FGModel*>::iterator it;
268 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
270 IC = new FGInitialCondition(this);
277 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 bool FGFDMExec::DeAllocate(void)
288 delete GroundReactions;
289 delete ExternalReactions;
290 delete BuoyantForces;
296 for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
302 delete GroundCallback;
314 ExternalReactions = 0;
327 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 void FGFDMExec::Schedule(FGModel* model, int rate)
331 model->SetRate(rate);
332 Models.push_back(model);
335 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 bool FGFDMExec::Run(void)
343 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
344 ChildFDMList[i]->AssignState(Propagate); // Transfer state to the child FDM
345 ChildFDMList[i]->Run();
348 // returns true if success, false if complete
349 if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript();
351 vector <FGModel*>::iterator it;
352 for (it = Models.begin(); it != Models.end(); ++it) (*it)->Run();
355 if (!Holding()) IncrTime();
356 if (Terminate) success = false;
361 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 // This call will cause the sim time to reset to 0.0
364 bool FGFDMExec::RunIC(void)
366 SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0.
369 ResumeIntegration(); // Restores the integration rate to what it was.
374 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 void FGFDMExec::Initialize(FGInitialCondition *FGIC)
378 Propagate->SetInitialState( FGIC );
381 Atmosphere->SetWindNED( FGIC->GetWindNFpsIC(),
382 FGIC->GetWindEFpsIC(),
383 FGIC->GetWindDFpsIC() );
385 FGColumnVector3 vAeroUVW;
386 vAeroUVW = Propagate->GetUVW() + Propagate->GetTl2b()*Atmosphere->GetTotalWindNED();
389 if (vAeroUVW(eW) != 0.0)
390 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
393 if (vAeroUVW(eV) != 0.0)
394 beta = vAeroUVW(eU)*vAeroUVW(eU)+vAeroUVW(eW)*vAeroUVW(eW) > 0.0 ? atan2(vAeroUVW(eV), (fabs(vAeroUVW(eU))/vAeroUVW(eU))*sqrt(vAeroUVW(eU)*vAeroUVW(eU) + vAeroUVW(eW)*vAeroUVW(eW))) : 0.0;
398 Auxiliary->SetAB(alpha, beta);
400 double Vt = vAeroUVW.Magnitude();
401 Auxiliary->SetVt(Vt);
403 Auxiliary->SetMach(Vt/Atmosphere->GetSoundSpeed());
405 double qbar = 0.5*Vt*Vt*Atmosphere->GetDensity();
406 Auxiliary->Setqbar(qbar);
409 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411 // A private, internal function call for Tie-ing to a property, so it needs an
414 void FGFDMExec::ResetToInitialConditions(int mode)
417 for (unsigned int i=0; i<Outputs.size(); i++) {
418 Outputs[i]->SetStartNewFile(true);
422 ResetToInitialConditions();
425 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 void FGFDMExec::ResetToInitialConditions(void)
429 if (Constructing) return;
431 vector <FGModel*>::iterator it;
432 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
435 if (Script) Script->ResetEvents();
438 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 void FGFDMExec::SetGroundCallback(FGGroundCallback* p)
442 delete GroundCallback;
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 vector <string> FGFDMExec::EnumerateFDMs(void)
450 vector <string> FDMList;
452 FDMList.push_back(Aircraft->GetAircraftName());
454 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
455 FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
461 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 bool FGFDMExec::LoadScript(const string& script, double deltaT)
467 Script = new FGScript(this);
468 result = Script->LoadScript(RootDir + script, deltaT);
473 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 bool FGFDMExec::LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
476 const string& model, bool addModelToPath)
478 FGFDMExec::AircraftPath = RootDir + AircraftPath;
479 FGFDMExec::EnginePath = RootDir + EnginePath;
480 FGFDMExec::SystemsPath = RootDir + SystemsPath;
482 return LoadModel(model, addModelToPath);
485 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
490 string aircraftCfgFileName;
491 Element* element = 0L;
492 bool result = false; // initialize result to false, indicating input file not yet read
494 modelName = model; // Set the class modelName attribute
496 if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
497 cerr << "Error: attempted to load aircraft with undefined ";
498 cerr << "aircraft, engine, and system paths" << endl;
502 FullAircraftPath = AircraftPath;
503 if (addModelToPath) FullAircraftPath += "/" + model;
504 aircraftCfgFileName = FullAircraftPath + "/" + model + ".xml";
511 int saved_debug_lvl = debug_lvl;
513 document = LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
515 if (IsChild) debug_lvl = 0;
517 ReadPrologue(document);
519 if (IsChild) debug_lvl = saved_debug_lvl;
521 // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
522 element = document->FindElement("fileheader");
524 result = ReadFileHeader(element);
526 cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
531 if (IsChild) debug_lvl = 0;
533 // Process the metrics element. This element is REQUIRED.
534 element = document->FindElement("metrics");
536 result = Aircraft->Load(element);
538 cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
542 cerr << endl << "No metrics element was found in the aircraft config file." << endl;
546 // Process the mass_balance element. This element is REQUIRED.
547 element = document->FindElement("mass_balance");
549 result = MassBalance->Load(element);
551 cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
555 cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
559 // Process the ground_reactions element. This element is REQUIRED.
560 element = document->FindElement("ground_reactions");
562 result = GroundReactions->Load(element);
564 cerr << endl << "Aircraft ground_reactions element has problems in file " << aircraftCfgFileName << endl;
568 cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
572 // Process the external_reactions element. This element is OPTIONAL.
573 element = document->FindElement("external_reactions");
575 result = ExternalReactions->Load(element);
577 cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
582 // Process the buoyant_forces element. This element is OPTIONAL.
583 element = document->FindElement("buoyant_forces");
585 result = BuoyantForces->Load(element);
587 cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
592 // Process the propulsion element. This element is OPTIONAL.
593 element = document->FindElement("propulsion");
595 result = Propulsion->Load(element);
597 cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
602 // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
603 element = document->FindElement("system");
605 result = FCS->Load(element, FGFCS::stSystem);
607 cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
610 element = document->FindNextElement("system");
613 // Process the autopilot element. This element is OPTIONAL.
614 element = document->FindElement("autopilot");
616 result = FCS->Load(element, FGFCS::stAutoPilot);
618 cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
623 // Process the flight_control element. This element is OPTIONAL.
624 element = document->FindElement("flight_control");
626 result = FCS->Load(element, FGFCS::stFCS);
628 cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
633 // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
634 element = document->FindElement("aerodynamics");
636 result = Aerodynamics->Load(element);
638 cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
642 cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
645 // Process the input element. This element is OPTIONAL.
646 element = document->FindElement("input");
648 result = Input->Load(element);
650 cerr << endl << "Aircraft input element has problems in file " << aircraftCfgFileName << endl;
655 // Process the output element[s]. This element is OPTIONAL, and there may be more than one.
657 typedef int (FGOutput::*iOPMF)(void) const;
658 element = document->FindElement("output");
660 if (debug_lvl > 0) cout << endl << " Output data set: " << idx << " ";
661 FGOutput* Output = new FGOutput(this);
664 result = Output->Load(element);
666 cerr << endl << "Aircraft output element has problems in file " << aircraftCfgFileName << endl;
669 Outputs.push_back(Output);
670 string outputProp = CreateIndexedPropertyName("simulation/output",idx);
671 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
674 element = document->FindNextElement("output");
677 // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
678 element = document->FindElement("child");
680 result = ReadChild(element);
682 cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
690 MassBalance->Run(); // Update all mass properties for the report.
691 MassBalance->GetMassPropertiesReport();
693 cout << endl << fgblue << highint
694 << "End of vehicle configuration loading." << endl
695 << "-------------------------------------------------------------------------------"
699 if (IsChild) debug_lvl = saved_debug_lvl;
703 << " JSBSim failed to open the configuration file: " << aircraftCfgFileName
707 // Late bind previously undefined FCS inputs.
710 } catch (string prop) {
711 cerr << endl << fgred << " Could not late bind property " << prop
712 << ". Aborting." << endl;
717 struct PropertyCatalogStructure masterPCS;
718 masterPCS.base_string = "";
719 masterPCS.node = (FGPropertyManager*)Root;
720 BuildPropertyCatalog(&masterPCS);
726 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
730 struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
733 for (int i=0; i<pcs->node->nChildren(); i++) {
734 pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
735 node_idx = pcs->node->getChild(i)->getIndex();
737 pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx);
739 if (pcs->node->getChild(i)->nChildren() == 0) {
740 if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
741 pcsNew->base_string = pcsNew->base_string.erase(0,12);
743 PropertyCatalog.push_back(pcsNew->base_string);
745 pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
746 BuildPropertyCatalog(pcsNew);
752 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 string FGFDMExec::QueryPropertyCatalog(const string& in)
757 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
758 if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
760 if (results.empty()) return "No matches found\n";
764 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 void FGFDMExec::PrintPropertyCatalog(void)
769 cout << " " << fgblue << highint << underon << "Property Catalog for "
770 << modelName << reset << endl << endl;
771 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
772 cout << " " << PropertyCatalog[i] << endl;
776 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778 bool FGFDMExec::ReadFileHeader(Element* el)
780 bool result = true; // true for success
782 if (debug_lvl == 0) return result;
785 cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
788 if (el->FindElement("description"))
789 cout << " Description: " << el->FindElement("description")->GetDataLine() << endl;
790 if (el->FindElement("author"))
791 cout << " Model Author: " << el->FindElement("author")->GetDataLine() << endl;
792 if (el->FindElement("filecreationdate"))
793 cout << " Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
794 if (el->FindElement("version"))
795 cout << " Version: " << el->FindElement("version")->GetDataLine() << endl;
800 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
804 bool result = true; // true for success
806 if (!el) return false;
808 string AircraftName = el->GetAttributeValue("name");
809 Aircraft->SetAircraftName(AircraftName);
811 if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
812 << underoff << ": " << highint << AircraftName << normint << endl;
814 CFGVersion = el->GetAttributeValue("version");
815 Release = el->GetAttributeValue("release");
818 cout << " Version: " << highint << CFGVersion
820 if (CFGVersion != needed_cfg_version) {
821 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
822 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
823 cerr << "Current version needed is: " << needed_cfg_version << endl;
824 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
828 if (Release == "ALPHA" && (debug_lvl & 1)) {
830 << highint << "This aircraft model is an " << fgred << Release
831 << reset << highint << " release!!!" << endl << endl << reset
832 << "This aircraft model may not even properly load, and probably"
833 << " will not fly as expected." << endl << endl
834 << fgred << highint << "Use this model for development purposes ONLY!!!"
835 << normint << reset << endl << endl;
836 } else if (Release == "BETA" && (debug_lvl & 1)) {
838 << highint << "This aircraft model is a " << fgred << Release
839 << reset << highint << " release!!!" << endl << endl << reset
840 << "This aircraft model probably will not fly as expected." << endl << endl
841 << fgblue << highint << "Use this model for development purposes ONLY!!!"
842 << normint << reset << endl << endl;
843 } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
845 << highint << "This aircraft model is a " << fgblue << Release
846 << reset << highint << " release." << endl << endl << reset;
847 } else if (debug_lvl & 1) {
849 << highint << "This aircraft model is an " << fgred << Release
850 << reset << highint << " release!!!" << endl << endl << reset
851 << "This aircraft model may not even properly load, and probably"
852 << " will not fly as expected." << endl << endl
853 << fgred << highint << "Use this model for development purposes ONLY!!!"
854 << normint << reset << endl << endl;
860 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 bool FGFDMExec::ReadChild(Element* el)
864 // Add a new childData object to the child FDM list
865 // Populate that childData element with a new FDMExec object
866 // Set the IsChild flag for that FDMExec object
867 // Get the aircraft name
868 // set debug level to print out no additional data for child objects
869 // Load the model given the aircraft name
870 // reset debug level to prior setting
874 struct childData* child = new childData;
876 child->exec = new FGFDMExec(Root, FDMctr);
877 child->exec->SetChild(true);
879 string childAircraft = el->GetAttributeValue("name");
880 string sMated = el->GetAttributeValue("mated");
881 if (sMated == "false") child->mated = false; // child objects are mated by default.
882 string sInternal = el->GetAttributeValue("internal");
883 if (sInternal == "true") child->internal = true; // child objects are external by default.
885 child->exec->SetAircraftPath( AircraftPath );
886 child->exec->SetEnginePath( EnginePath );
887 child->exec->SetSystemsPath( SystemsPath );
888 child->exec->LoadModel(childAircraft);
890 Element* location = el->FindElement("location");
892 child->Loc = location->FindElementTripletConvertTo("IN");
894 cerr << endl << highint << fgred << " No location was found for this child object!" << reset << endl;
898 Element* orientation = el->FindElement("orient");
900 child->Orient = orientation->FindElementTripletConvertTo("RAD");
901 } else if (debug_lvl > 0) {
902 cerr << endl << highint << " No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
905 ChildFDMList.push_back(child);
910 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
917 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 FGTrim* FGFDMExec::GetTrim(void)
922 Trim = new FGTrim(this,tNone);
926 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
928 void FGFDMExec::DisableOutput(void)
930 for (unsigned i=0; i<Outputs.size(); i++) {
931 Outputs[i]->Disable();
935 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937 void FGFDMExec::EnableOutput(void)
939 for (unsigned i=0; i<Outputs.size(); i++) {
940 Outputs[i]->Enable();
944 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946 bool FGFDMExec::SetOutputDirectives(const string& fname)
950 FGOutput* Output = new FGOutput(this);
951 Output->SetDirectivesFile(RootDir + fname);
954 result = Output->Load(0);
957 Outputs.push_back(Output);
958 typedef int (FGOutput::*iOPMF)(void) const;
959 string outputProp = CreateIndexedPropertyName("simulation/output",Outputs.size()-1);
960 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
966 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968 void FGFDMExec::DoTrim(int mode)
972 if (Constructing) return;
974 if (mode < 0 || mode > JSBSim::tNone) {
975 cerr << endl << "Illegal trimming mode!" << endl << endl;
978 saved_time = sim_time;
979 FGTrim trim(this, (JSBSim::TrimMode)mode);
980 if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
982 sim_time = saved_time;
985 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987 void FGFDMExec::DoTrimAnalysis(int mode)
990 if (Constructing) return;
992 if (mode < 0 || mode > JSBSim::taNone) {
993 cerr << endl << "Illegal trimming mode!" << endl << endl;
996 saved_time = sim_time;
998 FGTrimAnalysis trimAnalysis(this, (JSBSim::TrimAnalysisMode)mode);
1000 if ( !trimAnalysis.Load(IC->GetInitFile(), false) ) {
1001 cerr << "A problem occurred with trim configuration file " << trimAnalysis.Load(IC->GetInitFile()) << endl;
1005 bool result = trimAnalysis.DoTrim();
1007 if ( !result ) cerr << endl << "Trim Failed" << endl << endl;
1009 trimAnalysis.Report();
1010 Setsim_time(saved_time);
1013 cout << "\nOutput: " << GetOutputFileName() << endl;
1017 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1019 void FGFDMExec::UseAtmosphereMSIS(void)
1021 FGAtmosphere *oldAtmosphere = Atmosphere;
1022 Atmosphere = new MSIS(this);
1023 if (!Atmosphere->InitModel()) {
1024 cerr << fgred << "MSIS Atmosphere model init failed" << fgdef << endl;
1027 delete oldAtmosphere;
1030 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1032 void FGFDMExec::UseAtmosphereMars(void)
1035 FGAtmosphere *oldAtmosphere = Atmosphere;
1036 Atmosphere = new FGMars(this);
1037 if (!Atmosphere->InitModel()) {
1038 cerr << fgred << "Mars Atmosphere model init failed" << fgdef << endl;
1041 delete oldAtmosphere;
1045 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1046 // The bitmasked value choices are as follows:
1047 // unset: In this case (the default) JSBSim would only print
1048 // out the normally expected messages, essentially echoing
1049 // the config files as they are read. If the environment
1050 // variable is not set, debug_lvl is set to 1 internally
1051 // 0: This requests JSBSim not to output any messages
1053 // 1: This value explicity requests the normal JSBSim
1055 // 2: This value asks for a message to be printed out when
1056 // a class is instantiated
1057 // 4: When this value is set, a message is displayed when a
1058 // FGModel object executes its Run() method
1059 // 8: When this value is set, various runtime state variables
1060 // are printed out periodically
1061 // 16: When set various parameters are sanity checked and
1062 // a message is printed out when they go out of bounds
1064 void FGFDMExec::Debug(int from)
1066 if (debug_lvl <= 0) return;
1068 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1069 if (from == 0) { // Constructor
1070 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
1071 << JSBSim_version << underoff << normint << endl;
1072 cout << halfint << " [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1073 cout << normint << "JSBSim startup beginning ...\n\n";
1074 } else if (from == 3) {
1075 cout << "\n\nJSBSim startup complete\n\n";
1078 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1079 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1080 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
1082 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1084 cout << "================== Frame: " << Frame << " Time: "
1085 << sim_time << " dt: " << dT << endl;
1088 if (debug_lvl & 8 ) { // Runtime state variables
1090 if (debug_lvl & 16) { // Sanity checking
1092 if (debug_lvl & 64) {
1093 if (from == 0) { // Constructor
1094 cout << IdSrc << endl;
1095 cout << IdHdr << endl;