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.83 2010/11/07 13:30:54 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 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) : Root(root), FDMctr(fdmctr)
113 ExternalReactions = 0;
132 dT = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is
133 // run in standalone mode with no initialization file.
136 char* num = getenv("JSBSIM_DEBUG");
137 if (num) debug_lvl = atoi(num); // set debug level
138 } catch (...) { // if error set to 1
142 if (Root == 0) { // Then this is the root FDM
143 Root = new FGPropertyManager; // Create the property manager
148 FDMctr = new unsigned int; // Create and initialize the child FDM counter
152 // Store this FDM's ID
153 IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth"
155 // Prepare FDMctr for the next child FDM id
156 (*FDMctr)++; // instance. "child" instances are loaded last.
158 instance = Root->GetNode("/fdm/jsbsim",IdFDM,true);
160 // this is to catch errors in binding member functions to the property tree.
163 } catch ( string msg ) {
164 cout << "Caught error: " << msg << endl;
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 instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime);
178 instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel);
180 Constructing = false;
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 FGFDMExec::~FGFDMExec()
188 checkTied( instance );
191 if (IdFDM == 0) { // Meaning this is no child FDM
202 } catch ( string msg ) {
203 cout << "Caught error: " << msg << endl;
206 for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
207 ChildFDMList.clear();
209 PropertyCatalog.clear();
216 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 bool FGFDMExec::Allocate(void)
222 Atmosphere = new FGAtmosphere(this);
223 FCS = new FGFCS(this);
224 Propulsion = new FGPropulsion(this);
225 MassBalance = new FGMassBalance(this);
226 Aerodynamics = new FGAerodynamics (this);
227 Inertial = new FGInertial(this);
229 GroundCallback = new FGGroundCallback(Inertial->GetRefRadius());
231 GroundReactions = new FGGroundReactions(this);
232 ExternalReactions = new FGExternalReactions(this);
233 BuoyantForces = new FGBuoyantForces(this);
234 Aircraft = new FGAircraft(this);
235 Propagate = new FGPropagate(this);
236 Auxiliary = new FGAuxiliary(this);
237 Input = new FGInput(this);
239 // Schedule a model. The second arg (the integer) is the pass number. For
240 // instance, the atmosphere model could get executed every fifth pass it is called.
243 Schedule(Atmosphere, 1);
245 Schedule(Propulsion, 1);
246 Schedule(MassBalance, 1);
247 Schedule(Aerodynamics, 1);
248 Schedule(Inertial, 1);
249 Schedule(GroundReactions, 1);
250 Schedule(ExternalReactions, 1);
251 Schedule(BuoyantForces, 1);
252 Schedule(Aircraft, 1);
253 Schedule(Propagate, 1);
254 Schedule(Auxiliary, 1);
256 // Initialize models so they can communicate with each other
258 vector <FGModel*>::iterator it;
259 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
261 IC = new FGInitialCondition(this);
268 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 bool FGFDMExec::DeAllocate(void)
279 delete GroundReactions;
280 delete ExternalReactions;
281 delete BuoyantForces;
287 for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
293 delete GroundCallback;
305 ExternalReactions = 0;
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 void FGFDMExec::Schedule(FGModel* model, int rate)
322 model->SetRate(rate);
323 Models.push_back(model);
326 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 bool FGFDMExec::Run(void)
334 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
335 ChildFDMList[i]->AssignState(Propagate); // Transfer state to the child FDM
336 ChildFDMList[i]->Run();
339 // returns true if success, false if complete
340 if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript();
342 vector <FGModel*>::iterator it;
343 for (it = Models.begin(); it != Models.end(); ++it) (*it)->Run();
346 if (!Holding()) IncrTime();
347 if (Terminate) success = false;
352 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 // This call will cause the sim time to reset to 0.0
355 bool FGFDMExec::RunIC(void)
357 SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0.
360 ResumeIntegration(); // Restores the integration rate to what it was.
365 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 void FGFDMExec::Initialize(FGInitialCondition *FGIC)
369 Propagate->SetInitialState( FGIC );
372 Atmosphere->SetWindNED( FGIC->GetWindNFpsIC(),
373 FGIC->GetWindEFpsIC(),
374 FGIC->GetWindDFpsIC() );
376 FGColumnVector3 vAeroUVW;
377 vAeroUVW = Propagate->GetUVW() + Propagate->GetTl2b()*Atmosphere->GetTotalWindNED();
380 if (vAeroUVW(eW) != 0.0)
381 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
384 if (vAeroUVW(eV) != 0.0)
385 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;
389 Auxiliary->SetAB(alpha, beta);
391 double Vt = vAeroUVW.Magnitude();
392 Auxiliary->SetVt(Vt);
394 Auxiliary->SetMach(Vt/Atmosphere->GetSoundSpeed());
396 double qbar = 0.5*Vt*Vt*Atmosphere->GetDensity();
397 Auxiliary->Setqbar(qbar);
400 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 // A private, internal function call for Tie-ing to a property, so it needs an
405 void FGFDMExec::ResetToInitialConditions(int mode)
408 for (unsigned int i=0; i<Outputs.size(); i++) {
409 Outputs[i]->SetStartNewFile(true);
413 ResetToInitialConditions();
416 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 void FGFDMExec::ResetToInitialConditions(void)
420 if (Constructing) return;
422 vector <FGModel*>::iterator it;
423 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
426 if (Script) Script->ResetEvents();
429 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 void FGFDMExec::SetGroundCallback(FGGroundCallback* p)
433 delete GroundCallback;
437 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
439 vector <string> FGFDMExec::EnumerateFDMs(void)
441 vector <string> FDMList;
443 FDMList.push_back(Aircraft->GetAircraftName());
445 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
446 FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
452 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 bool FGFDMExec::LoadScript(const string& script, double deltaT)
458 Script = new FGScript(this);
459 result = Script->LoadScript(RootDir + script, deltaT);
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 bool FGFDMExec::LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
467 const string& model, bool addModelToPath)
469 FGFDMExec::AircraftPath = RootDir + AircraftPath;
470 FGFDMExec::EnginePath = RootDir + EnginePath;
471 FGFDMExec::SystemsPath = RootDir + SystemsPath;
473 return LoadModel(model, addModelToPath);
476 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
481 string aircraftCfgFileName;
482 Element* element = 0L;
483 bool result = false; // initialize result to false, indicating input file not yet read
485 modelName = model; // Set the class modelName attribute
487 if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
488 cerr << "Error: attempted to load aircraft with undefined ";
489 cerr << "aircraft, engine, and system paths" << endl;
493 FullAircraftPath = AircraftPath;
494 if (addModelToPath) FullAircraftPath += "/" + model;
495 aircraftCfgFileName = FullAircraftPath + "/" + model + ".xml";
502 int saved_debug_lvl = debug_lvl;
504 document = LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
506 if (IsChild) debug_lvl = 0;
508 ReadPrologue(document);
510 if (IsChild) debug_lvl = saved_debug_lvl;
512 // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
513 element = document->FindElement("fileheader");
515 result = ReadFileHeader(element);
517 cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
522 if (IsChild) debug_lvl = 0;
524 // Process the metrics element. This element is REQUIRED.
525 element = document->FindElement("metrics");
527 result = Aircraft->Load(element);
529 cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
533 cerr << endl << "No metrics element was found in the aircraft config file." << endl;
537 // Process the mass_balance element. This element is REQUIRED.
538 element = document->FindElement("mass_balance");
540 result = MassBalance->Load(element);
542 cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
546 cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
550 // Process the ground_reactions element. This element is REQUIRED.
551 element = document->FindElement("ground_reactions");
553 result = GroundReactions->Load(element);
555 cerr << endl << "Aircraft ground_reactions element has problems in file " << aircraftCfgFileName << endl;
559 cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
563 // Process the external_reactions element. This element is OPTIONAL.
564 element = document->FindElement("external_reactions");
566 result = ExternalReactions->Load(element);
568 cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
573 // Process the buoyant_forces element. This element is OPTIONAL.
574 element = document->FindElement("buoyant_forces");
576 result = BuoyantForces->Load(element);
578 cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
583 // Process the propulsion element. This element is OPTIONAL.
584 element = document->FindElement("propulsion");
586 result = Propulsion->Load(element);
588 cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
593 // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
594 element = document->FindElement("system");
596 result = FCS->Load(element, FGFCS::stSystem);
598 cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
601 element = document->FindNextElement("system");
604 // Process the autopilot element. This element is OPTIONAL.
605 element = document->FindElement("autopilot");
607 result = FCS->Load(element, FGFCS::stAutoPilot);
609 cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
614 // Process the flight_control element. This element is OPTIONAL.
615 element = document->FindElement("flight_control");
617 result = FCS->Load(element, FGFCS::stFCS);
619 cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
624 // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
625 element = document->FindElement("aerodynamics");
627 result = Aerodynamics->Load(element);
629 cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
633 cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
636 // Process the input element. This element is OPTIONAL.
637 element = document->FindElement("input");
639 result = Input->Load(element);
641 cerr << endl << "Aircraft input element has problems in file " << aircraftCfgFileName << endl;
646 // Process the output element[s]. This element is OPTIONAL, and there may be more than one.
648 typedef int (FGOutput::*iOPMF)(void) const;
649 element = document->FindElement("output");
651 if (debug_lvl > 0) cout << endl << " Output data set: " << idx << " ";
652 FGOutput* Output = new FGOutput(this);
655 result = Output->Load(element);
657 cerr << endl << "Aircraft output element has problems in file " << aircraftCfgFileName << endl;
660 Outputs.push_back(Output);
661 string outputProp = CreateIndexedPropertyName("simulation/output",idx);
662 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
665 element = document->FindNextElement("output");
668 // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
669 element = document->FindElement("child");
671 result = ReadChild(element);
673 cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
681 MassBalance->Run(); // Update all mass properties for the report.
682 MassBalance->GetMassPropertiesReport();
684 cout << endl << fgblue << highint
685 << "End of vehicle configuration loading." << endl
686 << "-------------------------------------------------------------------------------"
690 if (IsChild) debug_lvl = saved_debug_lvl;
694 << " JSBSim failed to open the configuration file: " << aircraftCfgFileName
698 // Late bind previously undefined FCS inputs.
701 } catch (string prop) {
702 cerr << endl << fgred << " Could not late bind property " << prop
703 << ". Aborting." << endl;
708 struct PropertyCatalogStructure masterPCS;
709 masterPCS.base_string = "";
710 masterPCS.node = (FGPropertyManager*)Root;
711 BuildPropertyCatalog(&masterPCS);
717 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
721 struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
724 for (int i=0; i<pcs->node->nChildren(); i++) {
725 pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
726 node_idx = pcs->node->getChild(i)->getIndex();
728 pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx);
730 if (pcs->node->getChild(i)->nChildren() == 0) {
731 if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
732 pcsNew->base_string = pcsNew->base_string.erase(0,12);
734 PropertyCatalog.push_back(pcsNew->base_string);
736 pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
737 BuildPropertyCatalog(pcsNew);
743 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
745 string FGFDMExec::QueryPropertyCatalog(const string& in)
748 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
749 if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
751 if (results.empty()) return "No matches found\n";
755 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 void FGFDMExec::PrintPropertyCatalog(void)
760 cout << " " << fgblue << highint << underon << "Property Catalog for "
761 << modelName << reset << endl << endl;
762 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
763 cout << " " << PropertyCatalog[i] << endl;
767 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 bool FGFDMExec::ReadFileHeader(Element* el)
771 bool result = true; // true for success
773 if (debug_lvl == 0) return result;
776 cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
779 if (el->FindElement("description"))
780 cout << " Description: " << el->FindElement("description")->GetDataLine() << endl;
781 if (el->FindElement("author"))
782 cout << " Model Author: " << el->FindElement("author")->GetDataLine() << endl;
783 if (el->FindElement("filecreationdate"))
784 cout << " Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
785 if (el->FindElement("version"))
786 cout << " Version: " << el->FindElement("version")->GetDataLine() << endl;
791 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
795 bool result = true; // true for success
797 if (!el) return false;
799 string AircraftName = el->GetAttributeValue("name");
800 Aircraft->SetAircraftName(AircraftName);
802 if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
803 << underoff << ": " << highint << AircraftName << normint << endl;
805 CFGVersion = el->GetAttributeValue("version");
806 Release = el->GetAttributeValue("release");
809 cout << " Version: " << highint << CFGVersion
811 if (CFGVersion != needed_cfg_version) {
812 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
813 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
814 cerr << "Current version needed is: " << needed_cfg_version << endl;
815 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
819 if (Release == "ALPHA" && (debug_lvl & 1)) {
821 << highint << "This aircraft model is an " << fgred << Release
822 << reset << highint << " release!!!" << endl << endl << reset
823 << "This aircraft model may not even properly load, and probably"
824 << " will not fly as expected." << endl << endl
825 << fgred << highint << "Use this model for development purposes ONLY!!!"
826 << normint << reset << endl << endl;
827 } else if (Release == "BETA" && (debug_lvl & 1)) {
829 << highint << "This aircraft model is a " << fgred << Release
830 << reset << highint << " release!!!" << endl << endl << reset
831 << "This aircraft model probably will not fly as expected." << endl << endl
832 << fgblue << highint << "Use this model for development purposes ONLY!!!"
833 << normint << reset << endl << endl;
834 } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
836 << highint << "This aircraft model is a " << fgblue << Release
837 << reset << highint << " release." << endl << endl << reset;
838 } else if (debug_lvl & 1) {
840 << highint << "This aircraft model is an " << fgred << Release
841 << reset << highint << " release!!!" << endl << endl << reset
842 << "This aircraft model may not even properly load, and probably"
843 << " will not fly as expected." << endl << endl
844 << fgred << highint << "Use this model for development purposes ONLY!!!"
845 << normint << reset << endl << endl;
851 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 bool FGFDMExec::ReadChild(Element* el)
855 // Add a new childData object to the child FDM list
856 // Populate that childData element with a new FDMExec object
857 // Set the IsChild flag for that FDMExec object
858 // Get the aircraft name
859 // set debug level to print out no additional data for child objects
860 // Load the model given the aircraft name
861 // reset debug level to prior setting
865 struct childData* child = new childData;
867 child->exec = new FGFDMExec(Root, FDMctr);
868 child->exec->SetChild(true);
870 string childAircraft = el->GetAttributeValue("name");
871 string sMated = el->GetAttributeValue("mated");
872 if (sMated == "false") child->mated = false; // child objects are mated by default.
873 string sInternal = el->GetAttributeValue("internal");
874 if (sInternal == "true") child->internal = true; // child objects are external by default.
876 child->exec->SetAircraftPath( AircraftPath );
877 child->exec->SetEnginePath( EnginePath );
878 child->exec->SetSystemsPath( SystemsPath );
879 child->exec->LoadModel(childAircraft);
881 Element* location = el->FindElement("location");
883 child->Loc = location->FindElementTripletConvertTo("IN");
885 cerr << endl << highint << fgred << " No location was found for this child object!" << reset << endl;
889 Element* orientation = el->FindElement("orient");
891 child->Orient = orientation->FindElementTripletConvertTo("RAD");
892 } else if (debug_lvl > 0) {
893 cerr << endl << highint << " No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
896 ChildFDMList.push_back(child);
901 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
908 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 FGTrim* FGFDMExec::GetTrim(void)
913 Trim = new FGTrim(this,tNone);
917 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 void FGFDMExec::DisableOutput(void)
921 for (unsigned i=0; i<Outputs.size(); i++) {
922 Outputs[i]->Disable();
926 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
928 void FGFDMExec::EnableOutput(void)
930 for (unsigned i=0; i<Outputs.size(); i++) {
931 Outputs[i]->Enable();
935 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937 bool FGFDMExec::SetOutputDirectives(const string& fname)
941 FGOutput* Output = new FGOutput(this);
942 Output->SetDirectivesFile(RootDir + fname);
945 result = Output->Load(0);
948 Outputs.push_back(Output);
949 typedef int (FGOutput::*iOPMF)(void) const;
950 string outputProp = CreateIndexedPropertyName("simulation/output",Outputs.size()-1);
951 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
957 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959 void FGFDMExec::DoTrim(int mode)
963 if (Constructing) return;
965 if (mode < 0 || mode > JSBSim::tNone) {
966 cerr << endl << "Illegal trimming mode!" << endl << endl;
969 saved_time = sim_time;
970 FGTrim trim(this, (JSBSim::TrimMode)mode);
971 if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
973 sim_time = saved_time;
976 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 void FGFDMExec::DoTrimAnalysis(int mode)
981 if (Constructing) return;
983 if (mode < 0 || mode > JSBSim::taNone) {
984 cerr << endl << "Illegal trimming mode!" << endl << endl;
987 saved_time = sim_time;
989 FGTrimAnalysis trimAnalysis(this, (JSBSim::TrimAnalysisMode)mode);
991 if ( !trimAnalysis.Load(IC->GetInitFile(), false) ) {
992 cerr << "A problem occurred with trim configuration file " << trimAnalysis.Load(IC->GetInitFile()) << endl;
996 bool result = trimAnalysis.DoTrim();
998 if ( !result ) cerr << endl << "Trim Failed" << endl << endl;
1000 trimAnalysis.Report();
1001 Setsim_time(saved_time);
1004 cout << "\nOutput: " << GetOutputFileName() << endl;
1008 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010 void FGFDMExec::UseAtmosphereMSIS(void)
1012 FGAtmosphere *oldAtmosphere = Atmosphere;
1013 Atmosphere = new MSIS(this);
1014 if (!Atmosphere->InitModel()) {
1015 cerr << fgred << "MSIS Atmosphere model init failed" << fgdef << endl;
1018 delete oldAtmosphere;
1021 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 void FGFDMExec::UseAtmosphereMars(void)
1026 FGAtmosphere *oldAtmosphere = Atmosphere;
1027 Atmosphere = new FGMars(this);
1028 if (!Atmosphere->InitModel()) {
1029 cerr << fgred << "Mars Atmosphere model init failed" << fgdef << endl;
1032 delete oldAtmosphere;
1036 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037 // The bitmasked value choices are as follows:
1038 // unset: In this case (the default) JSBSim would only print
1039 // out the normally expected messages, essentially echoing
1040 // the config files as they are read. If the environment
1041 // variable is not set, debug_lvl is set to 1 internally
1042 // 0: This requests JSBSim not to output any messages
1044 // 1: This value explicity requests the normal JSBSim
1046 // 2: This value asks for a message to be printed out when
1047 // a class is instantiated
1048 // 4: When this value is set, a message is displayed when a
1049 // FGModel object executes its Run() method
1050 // 8: When this value is set, various runtime state variables
1051 // are printed out periodically
1052 // 16: When set various parameters are sanity checked and
1053 // a message is printed out when they go out of bounds
1055 void FGFDMExec::Debug(int from)
1057 if (debug_lvl <= 0) return;
1059 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1060 if (from == 0) { // Constructor
1061 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
1062 << JSBSim_version << underoff << normint << endl;
1063 cout << halfint << " [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1064 cout << normint << "JSBSim startup beginning ...\n\n";
1065 } else if (from == 3) {
1066 cout << "\n\nJSBSim startup complete\n\n";
1069 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1070 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1071 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
1073 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1075 cout << "================== Frame: " << Frame << " Time: "
1076 << sim_time << " dt: " << dT << endl;
1079 if (debug_lvl & 8 ) { // Runtime state variables
1081 if (debug_lvl & 16) { // Sanity checking
1083 if (debug_lvl & 64) {
1084 if (from == 0) { // Constructor
1085 cout << IdSrc << endl;
1086 cout << IdHdr << endl;