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)
101 FDMctr = new unsigned int;
106 FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) : Root(root), FDMctr(fdmctr)
111 void FGFDMExec::Initialize()
123 ExternalReactions = 0;
141 dT = 1.0/120.0; // a default timestep size. This is needed for when JSBSim is
142 // run in standalone mode with no initialization file.
145 char* num = getenv("JSBSIM_DEBUG");
146 if (num) debug_lvl = atoi(num); // set debug level
147 } catch (...) { // if error set to 1
151 if (Root == 0) { // Then this is the root FDM
152 Root = new FGPropertyManager; // Create the property manager
154 FDMctr = new unsigned int; // Create and initialize the child FDM counter
158 // Store this FDM's ID
159 IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth"
161 // Prepare FDMctr for the next child FDM id
162 (*FDMctr)++; // instance. "child" instances are loaded last.
164 instance = Root->GetNode("/fdm/jsbsim",IdFDM,true);
166 // this is to catch errors in binding member functions to the property tree.
169 } catch ( string msg ) {
170 cout << "Caught error: " << msg << endl;
178 typedef int (FGFDMExec::*iPMF)(void) const;
179 // instance->Tie("simulation/do_trim_analysis", this, (iPMF)0, &FGFDMExec::DoTrimAnalysis);
180 instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim);
181 instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions);
182 instance->Tie("simulation/terminate", (int *)&Terminate);
183 instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime);
184 instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel);
186 Constructing = false;
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 FGFDMExec::~FGFDMExec()
194 checkTied( instance );
197 if (IdFDM == 0) { // Meaning this is no child FDM
207 } catch ( string msg ) {
208 cout << "Caught error: " << msg << endl;
211 for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
212 ChildFDMList.clear();
214 PropertyCatalog.clear();
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 bool FGFDMExec::Allocate(void)
227 Atmosphere = new FGAtmosphere(this);
228 FCS = new FGFCS(this);
229 Propulsion = new FGPropulsion(this);
230 MassBalance = new FGMassBalance(this);
231 Aerodynamics = new FGAerodynamics (this);
232 Inertial = new FGInertial(this);
234 GroundCallback = new FGGroundCallback(Inertial->GetRefRadius());
236 GroundReactions = new FGGroundReactions(this);
237 ExternalReactions = new FGExternalReactions(this);
238 BuoyantForces = new FGBuoyantForces(this);
239 Aircraft = new FGAircraft(this);
240 Propagate = new FGPropagate(this);
241 Auxiliary = new FGAuxiliary(this);
242 Input = new FGInput(this);
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.
248 Schedule(Atmosphere, 1);
250 Schedule(Propulsion, 1);
251 Schedule(MassBalance, 1);
252 Schedule(Aerodynamics, 1);
253 Schedule(Inertial, 1);
254 Schedule(GroundReactions, 1);
255 Schedule(ExternalReactions, 1);
256 Schedule(BuoyantForces, 1);
257 Schedule(Aircraft, 1);
258 Schedule(Propagate, 1);
259 Schedule(Auxiliary, 1);
261 // Initialize models so they can communicate with each other
263 vector <FGModel*>::iterator it;
264 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
266 IC = new FGInitialCondition(this);
273 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 bool FGFDMExec::DeAllocate(void)
284 delete GroundReactions;
285 delete ExternalReactions;
286 delete BuoyantForces;
292 for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
298 delete GroundCallback;
310 ExternalReactions = 0;
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 void FGFDMExec::Schedule(FGModel* model, int rate)
327 model->SetRate(rate);
328 Models.push_back(model);
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 bool FGFDMExec::Run(void)
339 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
340 ChildFDMList[i]->AssignState(Propagate); // Transfer state to the child FDM
341 ChildFDMList[i]->Run();
344 // returns true if success, false if complete
345 if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript();
347 vector <FGModel*>::iterator it;
348 for (it = Models.begin(); it != Models.end(); ++it) (*it)->Run();
351 if (!Holding()) IncrTime();
352 if (Terminate) success = false;
357 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 // This call will cause the sim time to reset to 0.0
360 bool FGFDMExec::RunIC(void)
362 SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0.
365 ResumeIntegration(); // Restores the integration rate to what it was.
370 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 void FGFDMExec::Initialize(FGInitialCondition *FGIC)
374 Propagate->SetInitialState( FGIC );
377 Atmosphere->SetWindNED( FGIC->GetWindNFpsIC(),
378 FGIC->GetWindEFpsIC(),
379 FGIC->GetWindDFpsIC() );
381 FGColumnVector3 vAeroUVW;
382 vAeroUVW = Propagate->GetUVW() + Propagate->GetTl2b()*Atmosphere->GetTotalWindNED();
385 if (vAeroUVW(eW) != 0.0)
386 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
389 if (vAeroUVW(eV) != 0.0)
390 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;
394 Auxiliary->SetAB(alpha, beta);
396 double Vt = vAeroUVW.Magnitude();
397 Auxiliary->SetVt(Vt);
399 Auxiliary->SetMach(Vt/Atmosphere->GetSoundSpeed());
401 double qbar = 0.5*Vt*Vt*Atmosphere->GetDensity();
402 Auxiliary->Setqbar(qbar);
405 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 // A private, internal function call for Tie-ing to a property, so it needs an
410 void FGFDMExec::ResetToInitialConditions(int mode)
413 for (unsigned int i=0; i<Outputs.size(); i++) {
414 Outputs[i]->SetStartNewFile(true);
418 ResetToInitialConditions();
421 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 void FGFDMExec::ResetToInitialConditions(void)
425 if (Constructing) return;
427 vector <FGModel*>::iterator it;
428 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
431 if (Script) Script->ResetEvents();
434 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 void FGFDMExec::SetGroundCallback(FGGroundCallback* p)
438 delete GroundCallback;
442 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 vector <string> FGFDMExec::EnumerateFDMs(void)
446 vector <string> FDMList;
448 FDMList.push_back(Aircraft->GetAircraftName());
450 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
451 FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 bool FGFDMExec::LoadScript(const string& script, double deltaT)
463 Script = new FGScript(this);
464 result = Script->LoadScript(RootDir + script, deltaT);
469 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471 bool FGFDMExec::LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
472 const string& model, bool addModelToPath)
474 FGFDMExec::AircraftPath = RootDir + AircraftPath;
475 FGFDMExec::EnginePath = RootDir + EnginePath;
476 FGFDMExec::SystemsPath = RootDir + SystemsPath;
478 return LoadModel(model, addModelToPath);
481 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483 bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
486 string aircraftCfgFileName;
487 Element* element = 0L;
488 bool result = false; // initialize result to false, indicating input file not yet read
490 modelName = model; // Set the class modelName attribute
492 if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
493 cerr << "Error: attempted to load aircraft with undefined ";
494 cerr << "aircraft, engine, and system paths" << endl;
498 FullAircraftPath = AircraftPath;
499 if (addModelToPath) FullAircraftPath += "/" + model;
500 aircraftCfgFileName = FullAircraftPath + "/" + model + ".xml";
507 int saved_debug_lvl = debug_lvl;
509 document = LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
511 if (IsChild) debug_lvl = 0;
513 ReadPrologue(document);
515 if (IsChild) debug_lvl = saved_debug_lvl;
517 // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
518 element = document->FindElement("fileheader");
520 result = ReadFileHeader(element);
522 cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
527 if (IsChild) debug_lvl = 0;
529 // Process the metrics element. This element is REQUIRED.
530 element = document->FindElement("metrics");
532 result = Aircraft->Load(element);
534 cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
538 cerr << endl << "No metrics element was found in the aircraft config file." << endl;
542 // Process the mass_balance element. This element is REQUIRED.
543 element = document->FindElement("mass_balance");
545 result = MassBalance->Load(element);
547 cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
551 cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
555 // Process the ground_reactions element. This element is REQUIRED.
556 element = document->FindElement("ground_reactions");
558 result = GroundReactions->Load(element);
560 cerr << endl << "Aircraft ground_reactions element has problems in file " << aircraftCfgFileName << endl;
564 cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
568 // Process the external_reactions element. This element is OPTIONAL.
569 element = document->FindElement("external_reactions");
571 result = ExternalReactions->Load(element);
573 cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
578 // Process the buoyant_forces element. This element is OPTIONAL.
579 element = document->FindElement("buoyant_forces");
581 result = BuoyantForces->Load(element);
583 cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
588 // Process the propulsion element. This element is OPTIONAL.
589 element = document->FindElement("propulsion");
591 result = Propulsion->Load(element);
593 cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
598 // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
599 element = document->FindElement("system");
601 result = FCS->Load(element, FGFCS::stSystem);
603 cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
606 element = document->FindNextElement("system");
609 // Process the autopilot element. This element is OPTIONAL.
610 element = document->FindElement("autopilot");
612 result = FCS->Load(element, FGFCS::stAutoPilot);
614 cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
619 // Process the flight_control element. This element is OPTIONAL.
620 element = document->FindElement("flight_control");
622 result = FCS->Load(element, FGFCS::stFCS);
624 cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
629 // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
630 element = document->FindElement("aerodynamics");
632 result = Aerodynamics->Load(element);
634 cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
638 cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
641 // Process the input element. This element is OPTIONAL.
642 element = document->FindElement("input");
644 result = Input->Load(element);
646 cerr << endl << "Aircraft input element has problems in file " << aircraftCfgFileName << endl;
651 // Process the output element[s]. This element is OPTIONAL, and there may be more than one.
653 typedef int (FGOutput::*iOPMF)(void) const;
654 element = document->FindElement("output");
656 if (debug_lvl > 0) cout << endl << " Output data set: " << idx << " ";
657 FGOutput* Output = new FGOutput(this);
660 result = Output->Load(element);
662 cerr << endl << "Aircraft output element has problems in file " << aircraftCfgFileName << endl;
665 Outputs.push_back(Output);
666 string outputProp = CreateIndexedPropertyName("simulation/output",idx);
667 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
670 element = document->FindNextElement("output");
673 // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
674 element = document->FindElement("child");
676 result = ReadChild(element);
678 cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
686 MassBalance->Run(); // Update all mass properties for the report.
687 MassBalance->GetMassPropertiesReport();
689 cout << endl << fgblue << highint
690 << "End of vehicle configuration loading." << endl
691 << "-------------------------------------------------------------------------------"
695 if (IsChild) debug_lvl = saved_debug_lvl;
699 << " JSBSim failed to open the configuration file: " << aircraftCfgFileName
703 // Late bind previously undefined FCS inputs.
706 } catch (string prop) {
707 cerr << endl << fgred << " Could not late bind property " << prop
708 << ". Aborting." << endl;
713 struct PropertyCatalogStructure masterPCS;
714 masterPCS.base_string = "";
715 masterPCS.node = (FGPropertyManager*)Root;
716 BuildPropertyCatalog(&masterPCS);
722 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
726 struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
729 for (int i=0; i<pcs->node->nChildren(); i++) {
730 pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
731 node_idx = pcs->node->getChild(i)->getIndex();
733 pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx);
735 if (pcs->node->getChild(i)->nChildren() == 0) {
736 if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
737 pcsNew->base_string = pcsNew->base_string.erase(0,12);
739 PropertyCatalog.push_back(pcsNew->base_string);
741 pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
742 BuildPropertyCatalog(pcsNew);
748 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750 string FGFDMExec::QueryPropertyCatalog(const string& in)
753 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
754 if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
756 if (results.empty()) return "No matches found\n";
760 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 void FGFDMExec::PrintPropertyCatalog(void)
765 cout << " " << fgblue << highint << underon << "Property Catalog for "
766 << modelName << reset << endl << endl;
767 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
768 cout << " " << PropertyCatalog[i] << endl;
772 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 bool FGFDMExec::ReadFileHeader(Element* el)
776 bool result = true; // true for success
778 if (debug_lvl == 0) return result;
781 cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
784 if (el->FindElement("description"))
785 cout << " Description: " << el->FindElement("description")->GetDataLine() << endl;
786 if (el->FindElement("author"))
787 cout << " Model Author: " << el->FindElement("author")->GetDataLine() << endl;
788 if (el->FindElement("filecreationdate"))
789 cout << " Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
790 if (el->FindElement("version"))
791 cout << " Version: " << el->FindElement("version")->GetDataLine() << endl;
796 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
800 bool result = true; // true for success
802 if (!el) return false;
804 string AircraftName = el->GetAttributeValue("name");
805 Aircraft->SetAircraftName(AircraftName);
807 if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
808 << underoff << ": " << highint << AircraftName << normint << endl;
810 CFGVersion = el->GetAttributeValue("version");
811 Release = el->GetAttributeValue("release");
814 cout << " Version: " << highint << CFGVersion
816 if (CFGVersion != needed_cfg_version) {
817 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
818 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
819 cerr << "Current version needed is: " << needed_cfg_version << endl;
820 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
824 if (Release == "ALPHA" && (debug_lvl & 1)) {
826 << highint << "This aircraft model is an " << fgred << Release
827 << reset << highint << " release!!!" << endl << endl << reset
828 << "This aircraft model may not even properly load, and probably"
829 << " will not fly as expected." << endl << endl
830 << fgred << highint << "Use this model for development purposes ONLY!!!"
831 << normint << reset << endl << endl;
832 } else if (Release == "BETA" && (debug_lvl & 1)) {
834 << highint << "This aircraft model is a " << fgred << Release
835 << reset << highint << " release!!!" << endl << endl << reset
836 << "This aircraft model probably will not fly as expected." << endl << endl
837 << fgblue << highint << "Use this model for development purposes ONLY!!!"
838 << normint << reset << endl << endl;
839 } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
841 << highint << "This aircraft model is a " << fgblue << Release
842 << reset << highint << " release." << endl << endl << reset;
843 } else if (debug_lvl & 1) {
845 << highint << "This aircraft model is an " << fgred << Release
846 << reset << highint << " release!!!" << endl << endl << reset
847 << "This aircraft model may not even properly load, and probably"
848 << " will not fly as expected." << endl << endl
849 << fgred << highint << "Use this model for development purposes ONLY!!!"
850 << normint << reset << endl << endl;
856 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 bool FGFDMExec::ReadChild(Element* el)
860 // Add a new childData object to the child FDM list
861 // Populate that childData element with a new FDMExec object
862 // Set the IsChild flag for that FDMExec object
863 // Get the aircraft name
864 // set debug level to print out no additional data for child objects
865 // Load the model given the aircraft name
866 // reset debug level to prior setting
870 struct childData* child = new childData;
872 child->exec = new FGFDMExec(Root, FDMctr);
873 child->exec->SetChild(true);
875 string childAircraft = el->GetAttributeValue("name");
876 string sMated = el->GetAttributeValue("mated");
877 if (sMated == "false") child->mated = false; // child objects are mated by default.
878 string sInternal = el->GetAttributeValue("internal");
879 if (sInternal == "true") child->internal = true; // child objects are external by default.
881 child->exec->SetAircraftPath( AircraftPath );
882 child->exec->SetEnginePath( EnginePath );
883 child->exec->SetSystemsPath( SystemsPath );
884 child->exec->LoadModel(childAircraft);
886 Element* location = el->FindElement("location");
888 child->Loc = location->FindElementTripletConvertTo("IN");
890 cerr << endl << highint << fgred << " No location was found for this child object!" << reset << endl;
894 Element* orientation = el->FindElement("orient");
896 child->Orient = orientation->FindElementTripletConvertTo("RAD");
897 } else if (debug_lvl > 0) {
898 cerr << endl << highint << " No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
901 ChildFDMList.push_back(child);
906 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
913 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 FGTrim* FGFDMExec::GetTrim(void)
918 Trim = new FGTrim(this,tNone);
922 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 void FGFDMExec::DisableOutput(void)
926 for (unsigned i=0; i<Outputs.size(); i++) {
927 Outputs[i]->Disable();
931 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
933 void FGFDMExec::EnableOutput(void)
935 for (unsigned i=0; i<Outputs.size(); i++) {
936 Outputs[i]->Enable();
940 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942 bool FGFDMExec::SetOutputDirectives(const string& fname)
946 FGOutput* Output = new FGOutput(this);
947 Output->SetDirectivesFile(RootDir + fname);
950 result = Output->Load(0);
953 Outputs.push_back(Output);
954 typedef int (FGOutput::*iOPMF)(void) const;
955 string outputProp = CreateIndexedPropertyName("simulation/output",Outputs.size()-1);
956 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
962 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 void FGFDMExec::DoTrim(int mode)
968 if (Constructing) return;
970 if (mode < 0 || mode > JSBSim::tNone) {
971 cerr << endl << "Illegal trimming mode!" << endl << endl;
974 saved_time = sim_time;
975 FGTrim trim(this, (JSBSim::TrimMode)mode);
976 if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
978 sim_time = saved_time;
981 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
983 void FGFDMExec::DoTrimAnalysis(int mode)
986 if (Constructing) return;
988 if (mode < 0 || mode > JSBSim::taNone) {
989 cerr << endl << "Illegal trimming mode!" << endl << endl;
992 saved_time = sim_time;
994 FGTrimAnalysis trimAnalysis(this, (JSBSim::TrimAnalysisMode)mode);
996 if ( !trimAnalysis.Load(IC->GetInitFile(), false) ) {
997 cerr << "A problem occurred with trim configuration file " << trimAnalysis.Load(IC->GetInitFile()) << endl;
1001 bool result = trimAnalysis.DoTrim();
1003 if ( !result ) cerr << endl << "Trim Failed" << endl << endl;
1005 trimAnalysis.Report();
1006 Setsim_time(saved_time);
1009 cout << "\nOutput: " << GetOutputFileName() << endl;
1013 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 void FGFDMExec::UseAtmosphereMSIS(void)
1017 FGAtmosphere *oldAtmosphere = Atmosphere;
1018 Atmosphere = new MSIS(this);
1019 if (!Atmosphere->InitModel()) {
1020 cerr << fgred << "MSIS Atmosphere model init failed" << fgdef << endl;
1023 delete oldAtmosphere;
1026 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1028 void FGFDMExec::UseAtmosphereMars(void)
1031 FGAtmosphere *oldAtmosphere = Atmosphere;
1032 Atmosphere = new FGMars(this);
1033 if (!Atmosphere->InitModel()) {
1034 cerr << fgred << "Mars Atmosphere model init failed" << fgdef << endl;
1037 delete oldAtmosphere;
1041 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1042 // The bitmasked value choices are as follows:
1043 // unset: In this case (the default) JSBSim would only print
1044 // out the normally expected messages, essentially echoing
1045 // the config files as they are read. If the environment
1046 // variable is not set, debug_lvl is set to 1 internally
1047 // 0: This requests JSBSim not to output any messages
1049 // 1: This value explicity requests the normal JSBSim
1051 // 2: This value asks for a message to be printed out when
1052 // a class is instantiated
1053 // 4: When this value is set, a message is displayed when a
1054 // FGModel object executes its Run() method
1055 // 8: When this value is set, various runtime state variables
1056 // are printed out periodically
1057 // 16: When set various parameters are sanity checked and
1058 // a message is printed out when they go out of bounds
1060 void FGFDMExec::Debug(int from)
1062 if (debug_lvl <= 0) return;
1064 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1065 if (from == 0) { // Constructor
1066 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
1067 << JSBSim_version << underoff << normint << endl;
1068 cout << halfint << " [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1069 cout << normint << "JSBSim startup beginning ...\n\n";
1070 } else if (from == 3) {
1071 cout << "\n\nJSBSim startup complete\n\n";
1074 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1075 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1076 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
1078 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1080 cout << "================== Frame: " << Frame << " Time: "
1081 << sim_time << " dt: " << dT << endl;
1084 if (debug_lvl & 8 ) { // Runtime state variables
1086 if (debug_lvl & 16) { // Sanity checking
1088 if (debug_lvl & 64) {
1089 if (from == 0) { // Constructor
1090 cout << IdSrc << endl;
1091 cout << IdHdr << endl;