JSBSim Flight Dynamics Model  1.2.0 (05 Nov 2023)
An Open Source Flight Dynamics and Control Software Library in C++
FGFDMExec.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  Header: FGFDMExec.h
3  Author: Jon Berndt
4  Date started: 11/17/98
5  file The header file for the JSBSim executive.
6 
7  ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
8 
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free
11  Software Foundation; either version 2 of the License, or (at your option) any
12  later version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17  details.
18 
19  You should have received a copy of the GNU Lesser General Public License along
20  with this program; if not, write to the Free Software Foundation, Inc., 59
21  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23  Further information about the GNU Lesser General Public License can also be
24  found on the world wide web at http://www.gnu.org.
25 
26 HISTORY
27 --------------------------------------------------------------------------------
28 11/17/98 JSB Created
29 7/31/99 TP Added RunIC function that runs the sim so that every frame
30  begins with the IC values from the given FGInitialCondition
31  object and dt=0.
32 
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 SENTRY
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36 
37 #ifndef FGFDMEXEC_HEADER_H
38 #define FGFDMEXEC_HEADER_H
39 
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 INCLUDES
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 
44 #include <memory>
45 
46 #include "models/FGPropagate.h"
47 #include "models/FGOutput.h"
48 #include "math/FGTemplateFunc.h"
49 
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 FORWARD DECLARATIONS
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53 
54 namespace JSBSim {
55 
56 class FGScript;
57 class FGTrim;
58 class FGAerodynamics;
59 class FGAircraft;
60 class FGAtmosphere;
61 class FGAccelerations;
62 class FGWinds;
63 class FGAuxiliary;
64 class FGBuoyantForces;
65 class FGExternalReactions;
66 class FGGroundReactions;
67 class FGFCS;
68 class FGInertial;
69 class FGInput;
70 class FGPropulsion;
71 class FGMassBalance;
72 
74  public:
75  TrimFailureException(const std::string& msg) : BaseException(msg) {}
76 };
77 
78 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 CLASS DOCUMENTATION
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
81 
179 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 CLASS DECLARATION
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
182 
183 class JSBSIM_API FGFDMExec : public FGJSBBase
184 {
185  struct childData {
186  std::unique_ptr<FGFDMExec> exec;
187  std::string info;
188  FGColumnVector3 Loc;
189  FGColumnVector3 Orient;
190  bool mated;
191  bool internal;
192 
193  childData(void) {
194  info = "";
195  Loc = FGColumnVector3(0,0,0);
196  Orient = FGColumnVector3(0,0,0);
197  mated = true;
198  internal = false;
199  }
200 
201  void Run(void) {exec->Run();}
202  void AssignState(FGPropagate* source_prop) {
203  exec->GetPropagate()->SetVState(source_prop->GetVState());
204  }
205  };
206 
207 public:
208 
210  FGFDMExec(FGPropertyManager* root = nullptr, std::shared_ptr<unsigned int> fdmctr = nullptr);
211 
213  ~FGFDMExec();
214 
215  // This list of enums is very important! The order in which models are listed
216  // here determines the order of execution of the models.
217  //
218  // There are some conditions that need to be met :
219  // 1. FCS can request mass geometry changes via the inertia/pointmass-*
220  // properties so it must be executed before MassBalance
221  // 2. MassBalance must be executed before Propulsion, Aerodynamics,
222  // GroundReactions, ExternalReactions and BuoyantForces to ensure that
223  // their moments are computed with the updated CG position.
224  enum eModels { ePropagate=0,
225  eInput,
226  eInertial,
227  eAtmosphere,
228  eWinds,
229  eSystems,
230  eMassBalance,
231  eAuxiliary,
232  ePropulsion,
233  eAerodynamics,
234  eGroundReactions,
235  eExternalReactions,
236  eBuoyantForces,
237  eAircraft,
238  eAccelerations,
239  eOutput,
240  eNumStandardModels };
241 
243  void Unbind(void) {instance->Unbind();}
244 
247  bool Run(void);
248 
252  bool RunIC(void);
253 
260  bool LoadPlanet(const SGPath& PlanetPath, bool useAircraftPath = true);
261 
277  bool LoadModel(const SGPath& AircraftPath, const SGPath& EnginePath,
278  const SGPath& SystemsPath, const std::string& model,
279  bool addModelToPath = true);
280 
291  bool LoadModel(const std::string& model, bool addModelToPath = true);
292 
304  bool LoadScript(const SGPath& Script, double deltaT=0.0,
305  const SGPath& initfile=SGPath());
306 
313  bool SetEnginePath(const SGPath& path) {
314  EnginePath = GetFullPath(path);
315  return true;
316  }
317 
325  bool SetAircraftPath(const SGPath& path) {
326  AircraftPath = GetFullPath(path);
327  return true;
328  }
329 
336  bool SetSystemsPath(const SGPath& path) {
337  SystemsPath = GetFullPath(path);
338  return true;
339  }
340 
347  bool SetOutputPath(const SGPath& path) {
348  OutputPath = GetFullPath(path);
349  return true;
350  }
351 
355  std::shared_ptr<FGAtmosphere> GetAtmosphere(void) const;
357  std::shared_ptr<FGAccelerations> GetAccelerations(void) const;
359  std::shared_ptr<FGWinds> GetWinds(void) const;
361  std::shared_ptr<FGFCS> GetFCS(void) const;
363  std::shared_ptr<FGPropulsion> GetPropulsion(void) const;
365  std::shared_ptr<FGMassBalance> GetMassBalance(void) const;
367  std::shared_ptr<FGAerodynamics> GetAerodynamics(void) const;
369  std::shared_ptr<FGInertial> GetInertial(void) const;
371  std::shared_ptr<FGGroundReactions> GetGroundReactions(void) const;
373  std::shared_ptr<FGExternalReactions> GetExternalReactions(void) const;
375  std::shared_ptr<FGBuoyantForces> GetBuoyantForces(void) const;
377  std::shared_ptr<FGAircraft> GetAircraft(void) const;
379  std::shared_ptr<FGPropagate> GetPropagate(void) const;
381  std::shared_ptr<FGAuxiliary> GetAuxiliary(void) const;
383  std::shared_ptr<FGInput> GetInput(void) const;
385  std::shared_ptr<FGOutput> GetOutput(void) const;
387  std::shared_ptr<FGScript> GetScript(void) const {return Script;}
389  std::shared_ptr<FGInitialCondition> GetIC(void) const {return IC;}
391  std::shared_ptr<FGTrim> GetTrim(void);
393 
395  const SGPath& GetEnginePath(void) { return EnginePath; }
397  const SGPath& GetAircraftPath(void) { return AircraftPath; }
399  const SGPath& GetSystemsPath(void) { return SystemsPath; }
401  const SGPath& GetFullAircraftPath(void) { return FullAircraftPath; }
403  const SGPath& GetOutputPath(void) { return OutputPath; }
404 
408  double GetPropertyValue(const std::string& property)
409  { return instance->GetNode()->GetDouble(property); }
410 
414  void SetPropertyValue(const std::string& property, double value)
415  { instance->GetNode()->SetDouble(property, value); }
416 
418  const std::string& GetModelName(void) const { return modelName; }
419 
421  std::shared_ptr<FGPropertyManager> GetPropertyManager(void) const { return instance; }
423  std::vector <std::string> EnumerateFDMs(void);
425  size_t GetFDMCount(void) const {return ChildFDMList.size();}
427  auto GetChildFDM(int i) const {return ChildFDMList[i];}
429  void SetChild(bool ch) {IsChild = ch;}
430 
446  bool SetOutputDirectives(const SGPath& fname)
447  { return Output->SetDirectivesFile(GetFullPath(fname)); }
448 
450  void ForceOutput(int idx=0) { Output->ForceOutput(idx); }
451 
453  void SetLoggingRate(double rate) { Output->SetRateHz(rate); }
454 
459  bool SetOutputFileName(const int n, const std::string& fname) { return Output->SetOutputName(n, fname); }
460 
465  std::string GetOutputFileName(int n) const { return Output->GetOutputName(n); }
466 
476  void DoTrim(int mode);
477 
479  void DisableOutput(void) { Output->Disable(); }
481  void EnableOutput(void) { Output->Enable(); }
483  void Hold(void) {holding = true;}
485  void EnableIncrementThenHold(int Timesteps) {TimeStepsUntilHold = Timesteps; IncrementThenHolding = true;}
487  void CheckIncrementalHold(void);
489  void Resume(void) {holding = false;}
491  bool Holding(void) {return holding;}
493  static const int START_NEW_OUTPUT = 0x1;
494  static const int DONT_EXECUTE_RUN_IC = 0x2;
502  void ResetToInitialConditions(int mode);
504  void SetDebugLevel(int level) {debug_lvl = level;}
505 
508  std::string base_string;
510  FGPropertyNode_ptr node;
511  };
512 
517  void BuildPropertyCatalog(struct PropertyCatalogStructure* pcs);
518 
526  std::string QueryPropertyCatalog(const std::string& check, const std::string& end_of_line="\n");
527 
528  // Print the contents of the property catalog for the loaded aircraft.
529  void PrintPropertyCatalog(void);
530 
531  // Print the simulation configuration
532  void PrintSimulationConfiguration(void) const;
533 
534  std::vector<std::string>& GetPropertyCatalog(void) {return PropertyCatalog;}
535 
536  void SetTrimStatus(bool status){ trim_status = status; }
537  bool GetTrimStatus(void) const { return trim_status; }
538  void SetTrimMode(int mode){ ta_mode = mode; }
539  int GetTrimMode(void) const { return ta_mode; }
540 
541  std::string GetPropulsionTankReport() const;
542 
544  double GetSimTime(void) const { return sim_time; }
545 
547  double GetDeltaT(void) const {return dT;}
548 
550  void SuspendIntegration(void) {saved_dT = dT; dT = 0.0;}
551 
553  void ResumeIntegration(void) {dT = saved_dT;}
554 
557  bool IntegrationSuspended(void) const {return dT == 0.0;}
558 
562  double Setsim_time(double cur_time);
563 
566  void Setdt(double delta_t) { dT = delta_t; }
567 
580  void SetRootDir(const SGPath& rootDir) {RootDir = rootDir;}
581 
585  const SGPath& GetRootDir(void) const {return RootDir;}
586 
590  double IncrTime(void);
591 
593  unsigned int GetFrame(void) const {return Frame;}
594 
596  int GetDebugLevel(void) const {return debug_lvl;};
597 
600  void Initialize(const FGInitialCondition* FGIC);
601 
606  void SetHoldDown(bool hd);
607 
611  bool GetHoldDown(void) const {return HoldDown;}
612 
613  FGTemplateFunc_ptr GetTemplateFunc(const std::string& name) {
614  return TemplateFunctions.count(name) ? TemplateFunctions[name] : nullptr;
615  }
616 
617  void AddTemplateFunc(const std::string& name, Element* el) {
618  TemplateFunctions[name] = std::make_shared<FGTemplateFunc>(this, el);
619  }
620 
621  auto GetRandomGenerator(void) const { return RandomGenerator; }
622 
623 private:
624  unsigned int Frame;
625  unsigned int IdFDM;
626  int disperse;
627  unsigned short Terminate;
628  double dT;
629  double saved_dT;
630  double sim_time;
631  bool holding;
632  bool IncrementThenHolding;
633  int TimeStepsUntilHold;
634  bool Constructing;
635  bool modelLoaded;
636  bool IsChild;
637  std::string modelName;
638  SGPath AircraftPath;
639  SGPath FullAircraftPath;
640  SGPath EnginePath;
641  SGPath SystemsPath;
642  SGPath OutputPath;
643  std::string CFGVersion;
644  std::string Release;
645  SGPath RootDir;
646 
647  // Standard Model pointers - shortcuts for internal executive use only.
648  // DO NOT TRY TO DELETE THEM !!!
649  FGPropagate* Propagate;
650  FGInertial* Inertial;
651  FGAtmosphere* Atmosphere;
652  FGWinds* Winds;
653  FGAuxiliary* Auxiliary;
654  FGFCS* FCS;
655  FGPropulsion* Propulsion;
656  FGAerodynamics* Aerodynamics;
657  FGGroundReactions* GroundReactions;
658  FGExternalReactions* ExternalReactions;
659  FGBuoyantForces* BuoyantForces;
660  FGMassBalance* MassBalance;
661  FGAircraft* Aircraft;
662  FGAccelerations* Accelerations;
663  FGOutput* Output;
664  FGInput* Input;
665 
666  bool trim_status;
667  int ta_mode;
668  int trim_completed;
669 
670  std::shared_ptr<FGInitialCondition> IC;
671  std::shared_ptr<FGScript> Script;
672  std::shared_ptr<FGTrim> Trim;
673 
674  FGPropertyNode_ptr Root;
675  std::shared_ptr<FGPropertyManager> instance;
676 
677  bool HoldDown;
678 
679  unsigned int RandomSeed;
680  std::shared_ptr<RandomNumberGenerator> RandomGenerator;
681 
682  // The FDM counter is used to give each child FDM an unique ID. The root FDM
683  // has the ID 0
684  std::shared_ptr<unsigned int> FDMctr;
685 
686  std::vector <std::string> PropertyCatalog;
687  std::vector <std::shared_ptr<childData>> ChildFDMList;
688  std::vector <std::shared_ptr<FGModel>> Models;
689  std::map<std::string, FGTemplateFunc_ptr> TemplateFunctions;
690 
691  bool ReadFileHeader(Element*);
692  bool ReadChild(Element*);
693  bool ReadPrologue(Element*);
694  void SRand(int sr);
695  int SRand(void) const {return RandomSeed;}
696  void LoadInputs(unsigned int idx);
697  void LoadPlanetConstants(void);
698  bool LoadPlanet(Element* el);
699  void LoadModelConstants(void);
700  bool Allocate(void);
701  bool DeAllocate(void);
702  void InitializeModels(void);
703  int GetDisperse(void) const {return disperse;}
704  SGPath GetFullPath(const SGPath& name) {
705  if (name.isRelative())
706  return RootDir/name.utf8Str();
707  else
708  return name;
709  }
710 
711  void Debug(int from);
712 };
713 }
714 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715 #endif
This class implements a 3 element column vector.
Encapsulates the JSBSim simulation executive.
Definition: FGFDMExec.h:184
std::shared_ptr< FGInitialCondition > GetIC(void) const
Returns a pointer to the FGInitialCondition object.
Definition: FGFDMExec.h:389
bool GetHoldDown(void) const
Gets the value of the property forces/hold-down.
Definition: FGFDMExec.h:611
void EnableOutput(void)
Enables data logging to all outputs.
Definition: FGFDMExec.h:481
void SetPropertyValue(const std::string &property, double value)
Sets a property value.
Definition: FGFDMExec.h:414
const SGPath & GetEnginePath(void)
Retrieves the engine path.
Definition: FGFDMExec.h:395
const SGPath & GetAircraftPath(void)
Retrieves the aircraft path.
Definition: FGFDMExec.h:397
void ForceOutput(int idx=0)
Forces the specified output object to print its items once.
Definition: FGFDMExec.h:450
bool SetSystemsPath(const SGPath &path)
Set the path to the systems config file directories.
Definition: FGFDMExec.h:336
bool SetOutputFileName(const int n, const std::string &fname)
Sets (or overrides) the output filename.
Definition: FGFDMExec.h:459
const SGPath & GetFullAircraftPath(void)
Retrieves the full aircraft path name.
Definition: FGFDMExec.h:401
void SetDebugLevel(int level)
Sets the debug level.
Definition: FGFDMExec.h:504
double GetPropertyValue(const std::string &property)
Retrieves the value of a property.
Definition: FGFDMExec.h:408
bool SetEnginePath(const SGPath &path)
Set the path to the engine config file directories.
Definition: FGFDMExec.h:313
const SGPath & GetSystemsPath(void)
Retrieves the systems path.
Definition: FGFDMExec.h:399
int GetDebugLevel(void) const
Retrieves the current debug level setting.
Definition: FGFDMExec.h:596
void SetLoggingRate(double rate)
Sets the logging rate in Hz for all output objects (if any).
Definition: FGFDMExec.h:453
const SGPath & GetRootDir(void) const
Retrieve the Root Directory.
Definition: FGFDMExec.h:585
bool SetOutputPath(const SGPath &path)
Set the directory where the output files will be written.
Definition: FGFDMExec.h:347
void EnableIncrementThenHold(int Timesteps)
Turn on hold after increment.
Definition: FGFDMExec.h:485
void SetRootDir(const SGPath &rootDir)
Set the root directory that is used to obtain absolute paths from relative paths.
Definition: FGFDMExec.h:580
std::string GetOutputFileName(int n) const
Retrieves the current output filename.
Definition: FGFDMExec.h:465
double GetDeltaT(void) const
Returns the simulation delta T.
Definition: FGFDMExec.h:547
size_t GetFDMCount(void) const
Gets the number of child FDMs.
Definition: FGFDMExec.h:425
unsigned int GetFrame(void) const
Retrieves the current frame count.
Definition: FGFDMExec.h:593
void Resume(void)
Resumes execution from a "Hold".
Definition: FGFDMExec.h:489
void DisableOutput(void)
Disables data logging to all outputs.
Definition: FGFDMExec.h:479
bool IntegrationSuspended(void) const
Returns the simulation suspension state.
Definition: FGFDMExec.h:557
bool SetOutputDirectives(const SGPath &fname)
Sets the output (logging) mechanism for this run.
Definition: FGFDMExec.h:446
void Unbind(void)
Unbind all tied JSBSim properties.
Definition: FGFDMExec.h:243
void SetChild(bool ch)
Marks this instance of the Exec object as a "child" object.
Definition: FGFDMExec.h:429
double GetSimTime(void) const
Returns the cumulative simulation time in seconds.
Definition: FGFDMExec.h:544
const std::string & GetModelName(void) const
Returns the model name.
Definition: FGFDMExec.h:418
void Setdt(double delta_t)
Sets the integration time step for the simulation executive.
Definition: FGFDMExec.h:566
void SuspendIntegration(void)
Suspends the simulation and sets the delta T to zero.
Definition: FGFDMExec.h:550
const SGPath & GetOutputPath(void)
Retrieves the path to the output files.
Definition: FGFDMExec.h:403
void Hold(void)
Pauses execution by preventing time from incrementing.
Definition: FGFDMExec.h:483
bool SetAircraftPath(const SGPath &path)
Set the path to the aircraft config file directories.
Definition: FGFDMExec.h:325
auto GetChildFDM(int i) const
Gets a particular child FDM.
Definition: FGFDMExec.h:427
std::shared_ptr< FGPropertyManager > GetPropertyManager(void) const
Returns a pointer to the property manager object.
Definition: FGFDMExec.h:421
std::shared_ptr< FGScript > GetScript(void) const
Retrieves the script object.
Definition: FGFDMExec.h:387
void ResumeIntegration(void)
Resumes the simulation by resetting delta T to the correct value.
Definition: FGFDMExec.h:553
bool Holding(void)
Returns true if the simulation is Holding (i.e. simulation time is not moving).
Definition: FGFDMExec.h:491
Initializes the simulation run.
JSBSim Base class.
Definition: FGJSBBase.h:118
Models the EOM and integration/propagation of state.
Definition: FGPropagate.h:95
FGPropertyNode_ptr node
The node for the property.
Definition: FGFDMExec.h:510
std::string base_string
Name of the property.
Definition: FGFDMExec.h:508