1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Base class for all models
7 Called by: FGSimExec, et. al.
9 ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This base class for the FGAerodynamics, FGPropagate, etc. classes defines methods
34 --------------------------------------------------------------------------------
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 #include "FGFDMExec.h"
44 #include "FGAtmosphere.h"
46 #include "FGPropulsion.h"
47 #include "FGMassBalance.h"
48 #include "FGAerodynamics.h"
49 #include "FGInertial.h"
50 #include "FGGroundReactions.h"
51 #include "FGExternalReactions.h"
52 #include "FGAircraft.h"
53 #include "FGPropagate.h"
54 #include "FGAuxiliary.h"
61 static const char *IdSrc = "$Id$";
62 static const char *IdHdr = ID_MODEL;
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 FGModel::FGModel(FGFDMExec* fdmex)
85 ExternalReactions = 0;
90 //in order for FGModel derived classes to self-bind (that is, call
91 //their bind function in the constructor, the PropertyManager pointer
92 //must be brought up now.
93 PropertyManager = FDMExec->GetPropertyManager();
98 if (debug_lvl & 2) cout << " FGModel Base Class" << endl;
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 for (unsigned int i=0; i<interface_properties.size(); i++) delete interface_properties[i];
106 interface_properties.clear();
108 for (unsigned int i=0; i<PreFunctions.size(); i++) delete PreFunctions[i];
109 for (unsigned int i=0; i<PostFunctions.size(); i++) delete PostFunctions[i];
111 if (debug_lvl & 2) cout << "Destroyed: FGModel" << endl;
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 bool FGModel::InitModel(void)
118 State = FDMExec->GetState();
119 Atmosphere = FDMExec->GetAtmosphere();
120 FCS = FDMExec->GetFCS();
121 Propulsion = FDMExec->GetPropulsion();
122 MassBalance = FDMExec->GetMassBalance();
123 Aerodynamics = FDMExec->GetAerodynamics();
124 Inertial = FDMExec->GetInertial();
125 GroundReactions = FDMExec->GetGroundReactions();
126 ExternalReactions = FDMExec->GetExternalReactions();
127 BuoyantForces = FDMExec->GetBuoyantForces();
128 Aircraft = FDMExec->GetAircraft();
129 Propagate = FDMExec->GetPropagate();
130 Auxiliary = FDMExec->GetAuxiliary();
140 !ExternalReactions ||
143 !Auxiliary) return(false);
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 bool FGModel::Load(Element* el)
151 // Interface properties are all stored in the interface properties array.
152 string interface_property_string = "";
154 Element *property_element = el->FindElement("property");
155 if (property_element && debug_lvl > 0) cout << endl << " Declared properties"
157 while (property_element) {
158 interface_property_string = property_element->GetDataLine();
159 if (PropertyManager->HasNode(interface_property_string)) {
160 cerr << " Property " << interface_property_string
161 << " is already defined." << endl;
164 if ( ! property_element->GetAttributeValue("value").empty())
165 value = property_element->GetAttributeValueAsNumber("value");
166 interface_properties.push_back(new double(value));
167 PropertyManager->Tie(interface_property_string, interface_properties.back());
169 cout << " " << interface_property_string << " (initial value: "
170 << value << ")" << endl << endl;
172 property_element = el->FindNextElement("property");
175 // End of interface property loading logic
177 // Load model pre-functions, if any
179 Element *function = el->FindElement("function");
181 if (function->GetAttributeValue("type") == "pre") {
182 PreFunctions.push_back(new FGFunction(PropertyManager, function));
183 } else if (function->GetAttributeValue("type").empty()) { // Assume pre-function
184 string funcname = function->GetAttributeValue("name");
185 PreFunctions.push_back(new FGFunction(PropertyManager, function));
187 function = el->FindNextElement("function");
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195 void FGModel::PostLoad(Element* el)
197 // Load model post-functions, if any
199 Element *function = el->FindElement("function");
201 if (function->GetAttributeValue("type") == "post") {
202 PostFunctions.push_back(new FGFunction(PropertyManager, function));
204 function = el->FindNextElement("function");
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 // Tell the Functions to cache values, so when the function values
210 // are being used in the model, the functions do not get
211 // calculated each time, but instead use the values that have already
212 // been calculated for this frame.
214 void FGModel::RunPreFunctions(void)
216 vector <FGFunction*>::iterator it;
217 for (it = PreFunctions.begin(); it != PreFunctions.end(); it++)
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 // Tell the Functions to cache values, so when the function values
223 // are being used in the model, the functions do not get
224 // calculated each time, but instead use the values that have already
225 // been calculated for this frame.
227 void FGModel::RunPostFunctions(void)
229 vector <FGFunction*>::iterator it;
230 for (it = PostFunctions.begin(); it != PostFunctions.end(); it++)
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
240 if (rate == 1) return false; // Fast exit if nothing to do
242 if (exe_ctr >= rate) exe_ctr = 1;
244 if (exe_ctr++ == 1) return false;
248 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 // The bitmasked value choices are as follows:
250 // unset: In this case (the default) JSBSim would only print
251 // out the normally expected messages, essentially echoing
252 // the config files as they are read. If the environment
253 // variable is not set, debug_lvl is set to 1 internally
254 // 0: This requests JSBSim not to output any messages
256 // 1: This value explicity requests the normal JSBSim
258 // 2: This value asks for a message to be printed out when
259 // a class is instantiated
260 // 4: When this value is set, a message is displayed when a
261 // FGModel object executes its Run() method
262 // 8: When this value is set, various runtime state variables
263 // are printed out periodically
264 // 16: When set various parameters are sanity checked and
265 // a message is printed out when they go out of bounds
267 void FGModel::Debug(int from)
269 if (debug_lvl <= 0) return;
271 if (debug_lvl & 1) { // Standard console startup message output
272 if (from == 0) { // Constructor
276 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
277 if (from == 0) cout << "Instantiated: FGModel" << endl;
278 if (from == 1) cout << "Destroyed: FGModel" << endl;
280 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
282 if (debug_lvl & 8 ) { // Runtime state variables
284 if (debug_lvl & 16) { // Sanity checking
286 if (debug_lvl & 64) {
287 if (from == 0) { // Constructor
288 cout << IdSrc << endl;
289 cout << IdHdr << endl;