JSBSim Flight Dynamics Model  1.2.0 (05 Nov 2023)
An Open Source Flight Dynamics and Control Software Library in C++
FGModel.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Module: FGModel.cpp
4  Author: Jon Berndt
5  Date started: 11/11/98
6  Purpose: Base class for all models
7  Called by: FGSimExec, et. al.
8 
9  ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
10 
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser General Public License as published by the Free
13  Software Foundation; either version 2 of the License, or (at your option) any
14  later version.
15 
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19  details.
20 
21  You should have received a copy of the GNU Lesser General Public License along
22  with this program; if not, write to the Free Software Foundation, Inc., 59
23  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 
25  Further information about the GNU Lesser General Public License can also be
26  found on the world wide web at http://www.gnu.org.
27 
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This base class for the FGAerodynamics, FGPropagate, etc. classes defines
31 methods common to all models.
32 
33 HISTORY
34 --------------------------------------------------------------------------------
35 11/11/98 JSB Created
36 
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 
41 #include "FGModel.h"
42 #include "FGFDMExec.h"
43 #include "input_output/FGModelLoader.h"
44 
45 using namespace std;
46 
47 namespace JSBSim {
48 
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 GLOBAL DECLARATIONS
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 CLASS IMPLEMENTATION
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 
57 FGModel::FGModel(FGFDMExec* fdmex)
58 {
59  FDMExec = fdmex;
60 
61  //in order for FGModel derived classes to self-bind (that is, call
62  //their bind function in the constructor, the PropertyManager pointer
63  //must be brought up now.
64  PropertyManager = FDMExec->GetPropertyManager();
65 
66  exe_ctr = 1;
67  rate = 1;
68 
69  if (debug_lvl & 2) cout << " FGModel Base Class" << endl;
70 }
71 
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 
74 FGModel::~FGModel()
75 {
76  if (debug_lvl & 2) cout << "Destroyed: FGModel" << endl;
77 }
78 
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 
81 bool FGModel::InitModel(void)
82 {
83  exe_ctr = 1;
84  return FGModelFunctions::InitModel();
85 }
86 
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 
89 bool FGModel::Run(bool Holding)
90 {
91  if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
92 
93  if (rate == 1) return false; // Fast exit if nothing to do
94 
95  if (exe_ctr >= rate) exe_ctr = 0;
96 
97  if (exe_ctr++ == 1) return false;
98  else return true;
99 }
100 
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 
103 SGPath FGModel::FindFullPathName(const SGPath& path) const
104 {
105  return CheckPathName(FDMExec->GetFullAircraftPath(), path);
106 }
107 
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 
110 bool FGModel::Upload(Element* el, bool preLoad)
111 {
112  FGModelLoader ModelLoader(this);
113  Element* document = ModelLoader.Open(el);
114 
115  if (!document) return false;
116 
117  if (document->GetName() != el->GetName()) {
118  cerr << el->ReadFrom()
119  << " Read model '" << document->GetName()
120  << "' while expecting model '" << el->GetName() << "'" << endl;
121  return false;
122  }
123 
124  bool result = true;
125 
126  if (preLoad)
127  result = FGModelFunctions::Load(document, FDMExec);
128 
129  if (document != el) {
130  el->MergeAttributes(document);
131 
132  if (preLoad) {
133  // After reading interface properties in a file, read properties in the
134  // local model element. This allows general-purpose models to be defined
135  // in a file, with overrides or initial loaded constants supplied in the
136  // relevant element of the aircraft configuration file.
137  LocalProperties.Load(el, PropertyManager.get(), true);
138  }
139 
140  Element* element = document->FindElement();
141  while (element) {
142  el->AddChildElement(element);
143  element->SetParent(el);
144  element = document->FindNextElement();
145  }
146  }
147 
148  return result;
149 }
150 
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 // The bitmasked value choices are as follows:
153 // unset: In this case (the default) JSBSim would only print
154 // out the normally expected messages, essentially echoing
155 // the config files as they are read. If the environment
156 // variable is not set, debug_lvl is set to 1 internally
157 // 0: This requests JSBSim not to output any messages
158 // whatsoever.
159 // 1: This value explicity requests the normal JSBSim
160 // startup messages
161 // 2: This value asks for a message to be printed out when
162 // a class is instantiated
163 // 4: When this value is set, a message is displayed when a
164 // FGModel object executes its Run() method
165 // 8: When this value is set, various runtime state variables
166 // are printed out periodically
167 // 16: When set various parameters are sanity checked and
168 // a message is printed out when they go out of bounds
169 
170 void FGModel::Debug(int from)
171 {
172  if (debug_lvl <= 0) return;
173 
174  if (debug_lvl & 1) { // Standard console startup message output
175  if (from == 0) { // Constructor
176 
177  }
178  }
179  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
180  if (from == 0) cout << "Instantiated: FGModel" << endl;
181  if (from == 1) cout << "Destroyed: FGModel" << endl;
182  }
183  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
184  }
185  if (debug_lvl & 8 ) { // Runtime state variables
186  }
187  if (debug_lvl & 16) { // Sanity checking
188  }
189  if (debug_lvl & 64) {
190  if (from == 0) { // Constructor
191  }
192  }
193 }
194 }
Element * FindElement(const std::string &el="")
Searches for a specified element.
void MergeAttributes(Element *el)
Merges the attributes of the current element with another element.
std::string ReadFrom(void) const
Return a string that contains a description of the location where the current XML element was read fr...
Element * FindNextElement(const std::string &el="")
Searches for the next element as specified.
void AddChildElement(Element *el)
Adds a child element to the list of children stored for this element.
Definition: FGXMLElement.h:345
void SetParent(Element *p)
This function sets the value of the parent class attribute to the supplied Element pointer.
Definition: FGXMLElement.h:341
const std::string & GetName(void) const
Retrieves the element name.
Definition: FGXMLElement.h:179
Encapsulates the JSBSim simulation executive.
Definition: FGFDMExec.h:184
const SGPath & GetFullAircraftPath(void)
Retrieves the full aircraft path name.
Definition: FGFDMExec.h:401
std::shared_ptr< FGPropertyManager > GetPropertyManager(void) const
Returns a pointer to the property manager object.
Definition: FGFDMExec.h:421