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;
106 FGFDMExec::FGFDMExec(FGPropertyManager* root, unsigned int* fdmctr) : Root(root), delete_root(false), 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
155 FDMctr = new unsigned int; // Create and initialize the child FDM counter
159 // Store this FDM's ID
160 IdFDM = (*FDMctr); // The main (parent) JSBSim instance is always the "zeroth"
162 // Prepare FDMctr for the next child FDM id
163 (*FDMctr)++; // instance. "child" instances are loaded last.
165 instance = Root->GetNode("/fdm/jsbsim",IdFDM,true);
167 // this is to catch errors in binding member functions to the property tree.
170 } catch ( string msg ) {
171 cout << "Caught error: " << msg << endl;
179 typedef int (FGFDMExec::*iPMF)(void) const;
180 // instance->Tie("simulation/do_trim_analysis", this, (iPMF)0, &FGFDMExec::DoTrimAnalysis);
181 instance->Tie("simulation/do_simple_trim", this, (iPMF)0, &FGFDMExec::DoTrim);
182 instance->Tie("simulation/reset", this, (iPMF)0, &FGFDMExec::ResetToInitialConditions);
183 instance->Tie("simulation/terminate", (int *)&Terminate);
184 instance->Tie("simulation/sim-time-sec", this, &FGFDMExec::GetSimTime);
185 instance->Tie("simulation/jsbsim-debug", this, &FGFDMExec::GetDebugLevel, &FGFDMExec::SetDebugLevel);
187 Constructing = false;
190 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 FGFDMExec::~FGFDMExec()
195 checkTied( instance );
198 if (IdFDM == 0) { // Meaning this is no child FDM
209 } catch ( string msg ) {
210 cout << "Caught error: " << msg << endl;
213 for (unsigned int i=1; i<ChildFDMList.size(); i++) delete ChildFDMList[i]->exec;
214 ChildFDMList.clear();
216 PropertyCatalog.clear();
223 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 bool FGFDMExec::Allocate(void)
229 Atmosphere = new FGAtmosphere(this);
230 FCS = new FGFCS(this);
231 Propulsion = new FGPropulsion(this);
232 MassBalance = new FGMassBalance(this);
233 Aerodynamics = new FGAerodynamics (this);
234 Inertial = new FGInertial(this);
236 GroundCallback = new FGGroundCallback(Inertial->GetRefRadius());
238 GroundReactions = new FGGroundReactions(this);
239 ExternalReactions = new FGExternalReactions(this);
240 BuoyantForces = new FGBuoyantForces(this);
241 Aircraft = new FGAircraft(this);
242 Propagate = new FGPropagate(this);
243 Auxiliary = new FGAuxiliary(this);
244 Input = new FGInput(this);
246 // Schedule a model. The second arg (the integer) is the pass number. For
247 // instance, the atmosphere model could get executed every fifth pass it is called.
250 Schedule(Atmosphere, 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);
263 // Initialize models so they can communicate with each other
265 vector <FGModel*>::iterator it;
266 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
268 IC = new FGInitialCondition(this);
275 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 bool FGFDMExec::DeAllocate(void)
286 delete GroundReactions;
287 delete ExternalReactions;
288 delete BuoyantForces;
294 for (unsigned i=0; i<Outputs.size(); i++) delete Outputs[i];
300 delete GroundCallback;
312 ExternalReactions = 0;
325 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 void FGFDMExec::Schedule(FGModel* model, int rate)
329 model->SetRate(rate);
330 Models.push_back(model);
333 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 bool FGFDMExec::Run(void)
341 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
342 ChildFDMList[i]->AssignState(Propagate); // Transfer state to the child FDM
343 ChildFDMList[i]->Run();
346 // returns true if success, false if complete
347 if (Script != 0 && !IntegrationSuspended()) success = Script->RunScript();
349 vector <FGModel*>::iterator it;
350 for (it = Models.begin(); it != Models.end(); ++it) (*it)->Run();
353 if (!Holding()) IncrTime();
354 if (Terminate) success = false;
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 // This call will cause the sim time to reset to 0.0
362 bool FGFDMExec::RunIC(void)
364 SuspendIntegration(); // saves the integration rate, dt, then sets it to 0.0.
367 ResumeIntegration(); // Restores the integration rate to what it was.
372 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 void FGFDMExec::Initialize(FGInitialCondition *FGIC)
376 Propagate->SetInitialState( FGIC );
379 Atmosphere->SetWindNED( FGIC->GetWindNFpsIC(),
380 FGIC->GetWindEFpsIC(),
381 FGIC->GetWindDFpsIC() );
383 FGColumnVector3 vAeroUVW;
384 vAeroUVW = Propagate->GetUVW() + Propagate->GetTl2b()*Atmosphere->GetTotalWindNED();
387 if (vAeroUVW(eW) != 0.0)
388 alpha = vAeroUVW(eU)*vAeroUVW(eU) > 0.0 ? atan2(vAeroUVW(eW), vAeroUVW(eU)) : 0.0;
391 if (vAeroUVW(eV) != 0.0)
392 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;
396 Auxiliary->SetAB(alpha, beta);
398 double Vt = vAeroUVW.Magnitude();
399 Auxiliary->SetVt(Vt);
401 Auxiliary->SetMach(Vt/Atmosphere->GetSoundSpeed());
403 double qbar = 0.5*Vt*Vt*Atmosphere->GetDensity();
404 Auxiliary->Setqbar(qbar);
407 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 // A private, internal function call for Tie-ing to a property, so it needs an
412 void FGFDMExec::ResetToInitialConditions(int mode)
415 for (unsigned int i=0; i<Outputs.size(); i++) {
416 Outputs[i]->SetStartNewFile(true);
420 ResetToInitialConditions();
423 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 void FGFDMExec::ResetToInitialConditions(void)
427 if (Constructing) return;
429 vector <FGModel*>::iterator it;
430 for (it = Models.begin(); it != Models.end(); ++it) (*it)->InitModel();
433 if (Script) Script->ResetEvents();
436 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 void FGFDMExec::SetGroundCallback(FGGroundCallback* p)
440 delete GroundCallback;
444 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446 vector <string> FGFDMExec::EnumerateFDMs(void)
448 vector <string> FDMList;
450 FDMList.push_back(Aircraft->GetAircraftName());
452 for (unsigned int i=1; i<ChildFDMList.size(); i++) {
453 FDMList.push_back(ChildFDMList[i]->exec->GetAircraft()->GetAircraftName());
459 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
461 bool FGFDMExec::LoadScript(const string& script, double deltaT)
465 Script = new FGScript(this);
466 result = Script->LoadScript(RootDir + script, deltaT);
471 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 bool FGFDMExec::LoadModel(const string& AircraftPath, const string& EnginePath, const string& SystemsPath,
474 const string& model, bool addModelToPath)
476 FGFDMExec::AircraftPath = RootDir + AircraftPath;
477 FGFDMExec::EnginePath = RootDir + EnginePath;
478 FGFDMExec::SystemsPath = RootDir + SystemsPath;
480 return LoadModel(model, addModelToPath);
483 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 bool FGFDMExec::LoadModel(const string& model, bool addModelToPath)
488 string aircraftCfgFileName;
489 Element* element = 0L;
490 bool result = false; // initialize result to false, indicating input file not yet read
492 modelName = model; // Set the class modelName attribute
494 if( AircraftPath.empty() || EnginePath.empty() || SystemsPath.empty()) {
495 cerr << "Error: attempted to load aircraft with undefined ";
496 cerr << "aircraft, engine, and system paths" << endl;
500 FullAircraftPath = AircraftPath;
501 if (addModelToPath) FullAircraftPath += "/" + model;
502 aircraftCfgFileName = FullAircraftPath + "/" + model + ".xml";
509 int saved_debug_lvl = debug_lvl;
511 document = LoadXMLDocument(aircraftCfgFileName); // "document" is a class member
513 if (IsChild) debug_lvl = 0;
515 ReadPrologue(document);
517 if (IsChild) debug_lvl = saved_debug_lvl;
519 // Process the fileheader element in the aircraft config file. This element is OPTIONAL.
520 element = document->FindElement("fileheader");
522 result = ReadFileHeader(element);
524 cerr << endl << "Aircraft fileheader element has problems in file " << aircraftCfgFileName << endl;
529 if (IsChild) debug_lvl = 0;
531 // Process the metrics element. This element is REQUIRED.
532 element = document->FindElement("metrics");
534 result = Aircraft->Load(element);
536 cerr << endl << "Aircraft metrics element has problems in file " << aircraftCfgFileName << endl;
540 cerr << endl << "No metrics element was found in the aircraft config file." << endl;
544 // Process the mass_balance element. This element is REQUIRED.
545 element = document->FindElement("mass_balance");
547 result = MassBalance->Load(element);
549 cerr << endl << "Aircraft mass_balance element has problems in file " << aircraftCfgFileName << endl;
553 cerr << endl << "No mass_balance element was found in the aircraft config file." << endl;
557 // Process the ground_reactions element. This element is REQUIRED.
558 element = document->FindElement("ground_reactions");
560 result = GroundReactions->Load(element);
562 cerr << endl << "Aircraft ground_reactions element has problems in file " << aircraftCfgFileName << endl;
566 cerr << endl << "No ground_reactions element was found in the aircraft config file." << endl;
570 // Process the external_reactions element. This element is OPTIONAL.
571 element = document->FindElement("external_reactions");
573 result = ExternalReactions->Load(element);
575 cerr << endl << "Aircraft external_reactions element has problems in file " << aircraftCfgFileName << endl;
580 // Process the buoyant_forces element. This element is OPTIONAL.
581 element = document->FindElement("buoyant_forces");
583 result = BuoyantForces->Load(element);
585 cerr << endl << "Aircraft buoyant_forces element has problems in file " << aircraftCfgFileName << endl;
590 // Process the propulsion element. This element is OPTIONAL.
591 element = document->FindElement("propulsion");
593 result = Propulsion->Load(element);
595 cerr << endl << "Aircraft propulsion element has problems in file " << aircraftCfgFileName << endl;
600 // Process the system element[s]. This element is OPTIONAL, and there may be more than one.
601 element = document->FindElement("system");
603 result = FCS->Load(element, FGFCS::stSystem);
605 cerr << endl << "Aircraft system element has problems in file " << aircraftCfgFileName << endl;
608 element = document->FindNextElement("system");
611 // Process the autopilot element. This element is OPTIONAL.
612 element = document->FindElement("autopilot");
614 result = FCS->Load(element, FGFCS::stAutoPilot);
616 cerr << endl << "Aircraft autopilot element has problems in file " << aircraftCfgFileName << endl;
621 // Process the flight_control element. This element is OPTIONAL.
622 element = document->FindElement("flight_control");
624 result = FCS->Load(element, FGFCS::stFCS);
626 cerr << endl << "Aircraft flight_control element has problems in file " << aircraftCfgFileName << endl;
631 // Process the aerodynamics element. This element is OPTIONAL, but almost always expected.
632 element = document->FindElement("aerodynamics");
634 result = Aerodynamics->Load(element);
636 cerr << endl << "Aircraft aerodynamics element has problems in file " << aircraftCfgFileName << endl;
640 cerr << endl << "No expected aerodynamics element was found in the aircraft config file." << endl;
643 // Process the input element. This element is OPTIONAL.
644 element = document->FindElement("input");
646 result = Input->Load(element);
648 cerr << endl << "Aircraft input element has problems in file " << aircraftCfgFileName << endl;
653 // Process the output element[s]. This element is OPTIONAL, and there may be more than one.
655 typedef int (FGOutput::*iOPMF)(void) const;
656 element = document->FindElement("output");
658 if (debug_lvl > 0) cout << endl << " Output data set: " << idx << " ";
659 FGOutput* Output = new FGOutput(this);
662 result = Output->Load(element);
664 cerr << endl << "Aircraft output element has problems in file " << aircraftCfgFileName << endl;
667 Outputs.push_back(Output);
668 string outputProp = CreateIndexedPropertyName("simulation/output",idx);
669 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
672 element = document->FindNextElement("output");
675 // Lastly, process the child element. This element is OPTIONAL - and NOT YET SUPPORTED.
676 element = document->FindElement("child");
678 result = ReadChild(element);
680 cerr << endl << "Aircraft child element has problems in file " << aircraftCfgFileName << endl;
688 MassBalance->Run(); // Update all mass properties for the report.
689 MassBalance->GetMassPropertiesReport();
691 cout << endl << fgblue << highint
692 << "End of vehicle configuration loading." << endl
693 << "-------------------------------------------------------------------------------"
697 if (IsChild) debug_lvl = saved_debug_lvl;
701 << " JSBSim failed to open the configuration file: " << aircraftCfgFileName
705 // Late bind previously undefined FCS inputs.
708 } catch (string prop) {
709 cerr << endl << fgred << " Could not late bind property " << prop
710 << ". Aborting." << endl;
715 struct PropertyCatalogStructure masterPCS;
716 masterPCS.base_string = "";
717 masterPCS.node = (FGPropertyManager*)Root;
718 BuildPropertyCatalog(&masterPCS);
724 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 void FGFDMExec::BuildPropertyCatalog(struct PropertyCatalogStructure* pcs)
728 struct PropertyCatalogStructure* pcsNew = new struct PropertyCatalogStructure;
731 for (int i=0; i<pcs->node->nChildren(); i++) {
732 pcsNew->base_string = pcs->base_string + "/" + pcs->node->getChild(i)->getName();
733 node_idx = pcs->node->getChild(i)->getIndex();
735 pcsNew->base_string = CreateIndexedPropertyName(pcsNew->base_string, node_idx);
737 if (pcs->node->getChild(i)->nChildren() == 0) {
738 if (pcsNew->base_string.substr(0,11) == string("/fdm/jsbsim")) {
739 pcsNew->base_string = pcsNew->base_string.erase(0,12);
741 PropertyCatalog.push_back(pcsNew->base_string);
743 pcsNew->node = (FGPropertyManager*)pcs->node->getChild(i);
744 BuildPropertyCatalog(pcsNew);
750 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 string FGFDMExec::QueryPropertyCatalog(const string& in)
755 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
756 if (PropertyCatalog[i].find(in) != string::npos) results += PropertyCatalog[i] + "\n";
758 if (results.empty()) return "No matches found\n";
762 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
764 void FGFDMExec::PrintPropertyCatalog(void)
767 cout << " " << fgblue << highint << underon << "Property Catalog for "
768 << modelName << reset << endl << endl;
769 for (unsigned i=0; i<PropertyCatalog.size(); i++) {
770 cout << " " << PropertyCatalog[i] << endl;
774 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 bool FGFDMExec::ReadFileHeader(Element* el)
778 bool result = true; // true for success
780 if (debug_lvl == 0) return result;
783 cout << endl <<highint << fgblue << "Reading child model: " << IdFDM << reset << endl << endl;
786 if (el->FindElement("description"))
787 cout << " Description: " << el->FindElement("description")->GetDataLine() << endl;
788 if (el->FindElement("author"))
789 cout << " Model Author: " << el->FindElement("author")->GetDataLine() << endl;
790 if (el->FindElement("filecreationdate"))
791 cout << " Creation Date: " << el->FindElement("filecreationdate")->GetDataLine() << endl;
792 if (el->FindElement("version"))
793 cout << " Version: " << el->FindElement("version")->GetDataLine() << endl;
798 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 bool FGFDMExec::ReadPrologue(Element* el) // el for ReadPrologue is the document element
802 bool result = true; // true for success
804 if (!el) return false;
806 string AircraftName = el->GetAttributeValue("name");
807 Aircraft->SetAircraftName(AircraftName);
809 if (debug_lvl & 1) cout << underon << "Reading Aircraft Configuration File"
810 << underoff << ": " << highint << AircraftName << normint << endl;
812 CFGVersion = el->GetAttributeValue("version");
813 Release = el->GetAttributeValue("release");
816 cout << " Version: " << highint << CFGVersion
818 if (CFGVersion != needed_cfg_version) {
819 cerr << endl << fgred << "YOU HAVE AN INCOMPATIBLE CFG FILE FOR THIS AIRCRAFT."
820 " RESULTS WILL BE UNPREDICTABLE !!" << endl;
821 cerr << "Current version needed is: " << needed_cfg_version << endl;
822 cerr << " You have version: " << CFGVersion << endl << fgdef << endl;
826 if (Release == "ALPHA" && (debug_lvl & 1)) {
828 << highint << "This aircraft model is an " << fgred << Release
829 << reset << highint << " release!!!" << endl << endl << reset
830 << "This aircraft model may not even properly load, and probably"
831 << " will not fly as expected." << endl << endl
832 << fgred << highint << "Use this model for development purposes ONLY!!!"
833 << normint << reset << endl << endl;
834 } else if (Release == "BETA" && (debug_lvl & 1)) {
836 << highint << "This aircraft model is a " << fgred << Release
837 << reset << highint << " release!!!" << endl << endl << reset
838 << "This aircraft model probably will not fly as expected." << endl << endl
839 << fgblue << highint << "Use this model for development purposes ONLY!!!"
840 << normint << reset << endl << endl;
841 } else if (Release == "PRODUCTION" && (debug_lvl & 1)) {
843 << highint << "This aircraft model is a " << fgblue << Release
844 << reset << highint << " release." << endl << endl << reset;
845 } else if (debug_lvl & 1) {
847 << highint << "This aircraft model is an " << fgred << Release
848 << reset << highint << " release!!!" << endl << endl << reset
849 << "This aircraft model may not even properly load, and probably"
850 << " will not fly as expected." << endl << endl
851 << fgred << highint << "Use this model for development purposes ONLY!!!"
852 << normint << reset << endl << endl;
858 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 bool FGFDMExec::ReadChild(Element* el)
862 // Add a new childData object to the child FDM list
863 // Populate that childData element with a new FDMExec object
864 // Set the IsChild flag for that FDMExec object
865 // Get the aircraft name
866 // set debug level to print out no additional data for child objects
867 // Load the model given the aircraft name
868 // reset debug level to prior setting
872 struct childData* child = new childData;
874 child->exec = new FGFDMExec(Root, FDMctr);
875 child->exec->SetChild(true);
877 string childAircraft = el->GetAttributeValue("name");
878 string sMated = el->GetAttributeValue("mated");
879 if (sMated == "false") child->mated = false; // child objects are mated by default.
880 string sInternal = el->GetAttributeValue("internal");
881 if (sInternal == "true") child->internal = true; // child objects are external by default.
883 child->exec->SetAircraftPath( AircraftPath );
884 child->exec->SetEnginePath( EnginePath );
885 child->exec->SetSystemsPath( SystemsPath );
886 child->exec->LoadModel(childAircraft);
888 Element* location = el->FindElement("location");
890 child->Loc = location->FindElementTripletConvertTo("IN");
892 cerr << endl << highint << fgred << " No location was found for this child object!" << reset << endl;
896 Element* orientation = el->FindElement("orient");
898 child->Orient = orientation->FindElementTripletConvertTo("RAD");
899 } else if (debug_lvl > 0) {
900 cerr << endl << highint << " No orientation was found for this child object! Assuming 0,0,0." << reset << endl;
903 ChildFDMList.push_back(child);
908 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 FGPropertyManager* FGFDMExec::GetPropertyManager(void)
915 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917 FGTrim* FGFDMExec::GetTrim(void)
920 Trim = new FGTrim(this,tNone);
924 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
926 void FGFDMExec::DisableOutput(void)
928 for (unsigned i=0; i<Outputs.size(); i++) {
929 Outputs[i]->Disable();
933 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 void FGFDMExec::EnableOutput(void)
937 for (unsigned i=0; i<Outputs.size(); i++) {
938 Outputs[i]->Enable();
942 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 bool FGFDMExec::SetOutputDirectives(const string& fname)
948 FGOutput* Output = new FGOutput(this);
949 Output->SetDirectivesFile(RootDir + fname);
952 result = Output->Load(0);
955 Outputs.push_back(Output);
956 typedef int (FGOutput::*iOPMF)(void) const;
957 string outputProp = CreateIndexedPropertyName("simulation/output",Outputs.size()-1);
958 instance->Tie(outputProp+"/log_rate_hz", Output, (iOPMF)0, &FGOutput::SetRate);
964 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966 void FGFDMExec::DoTrim(int mode)
970 if (Constructing) return;
972 if (mode < 0 || mode > JSBSim::tNone) {
973 cerr << endl << "Illegal trimming mode!" << endl << endl;
976 saved_time = sim_time;
977 FGTrim trim(this, (JSBSim::TrimMode)mode);
978 if ( !trim.DoTrim() ) cerr << endl << "Trim Failed" << endl << endl;
980 sim_time = saved_time;
983 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 void FGFDMExec::DoTrimAnalysis(int mode)
988 if (Constructing) return;
990 if (mode < 0 || mode > JSBSim::taNone) {
991 cerr << endl << "Illegal trimming mode!" << endl << endl;
994 saved_time = sim_time;
996 FGTrimAnalysis trimAnalysis(this, (JSBSim::TrimAnalysisMode)mode);
998 if ( !trimAnalysis.Load(IC->GetInitFile(), false) ) {
999 cerr << "A problem occurred with trim configuration file " << trimAnalysis.Load(IC->GetInitFile()) << endl;
1003 bool result = trimAnalysis.DoTrim();
1005 if ( !result ) cerr << endl << "Trim Failed" << endl << endl;
1007 trimAnalysis.Report();
1008 Setsim_time(saved_time);
1011 cout << "\nOutput: " << GetOutputFileName() << endl;
1015 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 void FGFDMExec::UseAtmosphereMSIS(void)
1019 FGAtmosphere *oldAtmosphere = Atmosphere;
1020 Atmosphere = new MSIS(this);
1021 if (!Atmosphere->InitModel()) {
1022 cerr << fgred << "MSIS Atmosphere model init failed" << fgdef << endl;
1025 delete oldAtmosphere;
1028 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1030 void FGFDMExec::UseAtmosphereMars(void)
1033 FGAtmosphere *oldAtmosphere = Atmosphere;
1034 Atmosphere = new FGMars(this);
1035 if (!Atmosphere->InitModel()) {
1036 cerr << fgred << "Mars Atmosphere model init failed" << fgdef << endl;
1039 delete oldAtmosphere;
1043 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1044 // The bitmasked value choices are as follows:
1045 // unset: In this case (the default) JSBSim would only print
1046 // out the normally expected messages, essentially echoing
1047 // the config files as they are read. If the environment
1048 // variable is not set, debug_lvl is set to 1 internally
1049 // 0: This requests JSBSim not to output any messages
1051 // 1: This value explicity requests the normal JSBSim
1053 // 2: This value asks for a message to be printed out when
1054 // a class is instantiated
1055 // 4: When this value is set, a message is displayed when a
1056 // FGModel object executes its Run() method
1057 // 8: When this value is set, various runtime state variables
1058 // are printed out periodically
1059 // 16: When set various parameters are sanity checked and
1060 // a message is printed out when they go out of bounds
1062 void FGFDMExec::Debug(int from)
1064 if (debug_lvl <= 0) return;
1066 if (debug_lvl & 1 && IdFDM == 0) { // Standard console startup message output
1067 if (from == 0) { // Constructor
1068 cout << "\n\n " << highint << underon << "JSBSim Flight Dynamics Model v"
1069 << JSBSim_version << underoff << normint << endl;
1070 cout << halfint << " [JSBSim-ML v" << needed_cfg_version << "]\n\n";
1071 cout << normint << "JSBSim startup beginning ...\n\n";
1072 } else if (from == 3) {
1073 cout << "\n\nJSBSim startup complete\n\n";
1076 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
1077 if (from == 0) cout << "Instantiated: FGFDMExec" << endl;
1078 if (from == 1) cout << "Destroyed: FGFDMExec" << endl;
1080 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
1082 cout << "================== Frame: " << Frame << " Time: "
1083 << sim_time << " dt: " << dT << endl;
1086 if (debug_lvl & 8 ) { // Runtime state variables
1088 if (debug_lvl & 16) { // Sanity checking
1090 if (debug_lvl & 64) {
1091 if (from == 0) { // Constructor
1092 cout << IdSrc << endl;
1093 cout << IdHdr << endl;