JSBSim Flight Dynamics Model  1.0 (02 March 2017)
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 Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  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 with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA 02111-1307, USA.
24 
25  Further information about the GNU Lesser General Public License can also be found on
26  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 methods
31 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 IDENT(IdSrc,"$Id: FGModel.cpp,v 1.27 2017/02/25 14:23:19 bcoconni Exp $");
50 IDENT(IdHdr,ID_MODEL);
51 
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 GLOBAL DECLARATIONS
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 CLASS IMPLEMENTATION
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 
60 FGModel::FGModel(FGFDMExec* fdmex)
61 {
62  FDMExec = fdmex;
63 
64  //in order for FGModel derived classes to self-bind (that is, call
65  //their bind function in the constructor, the PropertyManager pointer
66  //must be brought up now.
67  PropertyManager = FDMExec->GetPropertyManager();
68 
69  exe_ctr = 1;
70  rate = 1;
71 
72  if (debug_lvl & 2) cout << " FGModel Base Class" << endl;
73 }
74 
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 
77 FGModel::~FGModel()
78 {
79  if (debug_lvl & 2) cout << "Destroyed: FGModel" << endl;
80 }
81 
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 
84 bool FGModel::InitModel(void)
85 {
86  exe_ctr = 1;
87  return FGModelFunctions::InitModel();
88 }
89 
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 
92 bool FGModel::Run(bool Holding)
93 {
94  if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
95 
96  if (rate == 1) return false; // Fast exit if nothing to do
97 
98  if (exe_ctr >= rate) exe_ctr = 0;
99 
100  if (exe_ctr++ == 1) return false;
101  else return true;
102 }
103 
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 
106 SGPath FGModel::FindFullPathName(const SGPath& path) const
107 {
108  return CheckPathName(FDMExec->GetFullAircraftPath(), path);
109 }
110 
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 
113 bool FGModel::Load(Element* el)
114 {
115  FGModelLoader ModelLoader(this);
116  Element* document = ModelLoader.Open(el);
117 
118  if (!document) return false;
119 
120  if (document->GetName() != el->GetName()) {
121  cerr << el->ReadFrom()
122  << " Read model '" << document->GetName()
123  << "' while expecting model '" << el->GetName() << "'" << endl;
124  return false;
125  }
126 
127  bool result = FGModelFunctions::Load(document, PropertyManager);
128 
129  if (document != el) {
130  el->MergeAttributes(document);
131 
132  // After reading interface properties in a file, read properties in the
133  // local model element. This allows general-purpose models to be defined in
134  // a file, with overrides or initial loaded constants supplied in the
135  // relevant element of the aircraft configuration file.
136 
137  LocalProperties.Load(el, PropertyManager, true);
138 
139  Element* element = document->FindElement();
140  while (element) {
141  el->AddChildElement(element);
142  element->SetParent(el);
143  element = document->FindNextElement();
144  }
145  }
146 
147  return result;
148 }
149 
150 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 // The bitmasked value choices are as follows:
152 // unset: In this case (the default) JSBSim would only print
153 // out the normally expected messages, essentially echoing
154 // the config files as they are read. If the environment
155 // variable is not set, debug_lvl is set to 1 internally
156 // 0: This requests JSBSim not to output any messages
157 // whatsoever.
158 // 1: This value explicity requests the normal JSBSim
159 // startup messages
160 // 2: This value asks for a message to be printed out when
161 // a class is instantiated
162 // 4: When this value is set, a message is displayed when a
163 // FGModel object executes its Run() method
164 // 8: When this value is set, various runtime state variables
165 // are printed out periodically
166 // 16: When set various parameters are sanity checked and
167 // a message is printed out when they go out of bounds
168 
169 void FGModel::Debug(int from)
170 {
171  if (debug_lvl <= 0) return;
172 
173  if (debug_lvl & 1) { // Standard console startup message output
174  if (from == 0) { // Constructor
175 
176  }
177  }
178  if (debug_lvl & 2 ) { // Instantiation/Destruction notification
179  if (from == 0) cout << "Instantiated: FGModel" << endl;
180  if (from == 1) cout << "Destroyed: FGModel" << endl;
181  }
182  if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
183  }
184  if (debug_lvl & 8 ) { // Runtime state variables
185  }
186  if (debug_lvl & 16) { // Sanity checking
187  }
188  if (debug_lvl & 64) {
189  if (from == 0) { // Constructor
190  cout << IdSrc << endl;
191  cout << IdHdr << endl;
192  }
193  }
194 }
195 }
void AddChildElement(Element *el)
Adds a child element to the list of children stored for this element.
Definition: FGXMLElement.h:341
const SGPath & GetFullAircraftPath(void)
Retrieves the full aircraft path name.
Definition: FGFDMExec.h:397
void SetParent(Element *p)
This function sets the value of the parent class attribute to the supplied Element pointer...
Definition: FGXMLElement.h:337
STL namespace.
Element * FindElement(const std::string &el="")
Searches for a specified element.
FGPropertyManager * GetPropertyManager(void)
Returns a pointer to the property manager object.
Definition: FGFDMExec.cpp:1099
void MergeAttributes(Element *el)
Merges the attributes of the current element with another element.
const std::string & GetName(void) const
Retrieves the element name.
Definition: FGXMLElement.h:186
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.
Encapsulates the JSBSim simulation executive.
Definition: FGFDMExec.h:189