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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 #include "FGFDMExec.h"
43 #include "FGAtmosphere.h"
45 #include "FGPropulsion.h"
46 #include "FGMassBalance.h"
47 #include "FGAerodynamics.h"
48 #include "FGInertial.h"
49 #include "FGGroundReactions.h"
50 #include "FGExternalReactions.h"
51 #include "FGAircraft.h"
52 #include "FGPropagate.h"
53 #include "FGAuxiliary.h"
60 static const char *IdSrc = "$Id: FGModel.cpp,v 1.14 2010/02/25 05:21:36 jberndt Exp $";
61 static const char *IdHdr = ID_MODEL;
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 FGModel::FGModel(FGFDMExec* fdmex)
83 ExternalReactions = 0;
88 //in order for FGModel derived classes to self-bind (that is, call
89 //their bind function in the constructor, the PropertyManager pointer
90 //must be brought up now.
91 PropertyManager = FDMExec->GetPropertyManager();
96 if (debug_lvl & 2) cout << " FGModel Base Class" << endl;
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 for (unsigned int i=0; i<interface_properties.size(); i++) delete interface_properties[i];
104 interface_properties.clear();
106 for (unsigned int i=0; i<PreFunctions.size(); i++) delete PreFunctions[i];
107 for (unsigned int i=0; i<PostFunctions.size(); i++) delete PostFunctions[i];
109 if (debug_lvl & 2) cout << "Destroyed: FGModel" << endl;
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 bool FGModel::InitModel(void)
116 Atmosphere = FDMExec->GetAtmosphere();
117 FCS = FDMExec->GetFCS();
118 Propulsion = FDMExec->GetPropulsion();
119 MassBalance = FDMExec->GetMassBalance();
120 Aerodynamics = FDMExec->GetAerodynamics();
121 Inertial = FDMExec->GetInertial();
122 GroundReactions = FDMExec->GetGroundReactions();
123 ExternalReactions = FDMExec->GetExternalReactions();
124 BuoyantForces = FDMExec->GetBuoyantForces();
125 Aircraft = FDMExec->GetAircraft();
126 Propagate = FDMExec->GetPropagate();
127 Auxiliary = FDMExec->GetAuxiliary();
136 !ExternalReactions ||
139 !Auxiliary) return(false);
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 bool FGModel::Load(Element* el)
147 // Interface properties are all stored in the interface properties array.
148 string interface_property_string = "";
150 Element *property_element = el->FindElement("property");
151 if (property_element && debug_lvl > 0) cout << endl << " Declared properties"
153 while (property_element) {
154 interface_property_string = property_element->GetDataLine();
155 if (PropertyManager->HasNode(interface_property_string)) {
156 cerr << " Property " << interface_property_string
157 << " is already defined." << endl;
160 if ( ! property_element->GetAttributeValue("value").empty())
161 value = property_element->GetAttributeValueAsNumber("value");
162 interface_properties.push_back(new double(value));
163 PropertyManager->Tie(interface_property_string, interface_properties.back());
165 cout << " " << interface_property_string << " (initial value: "
166 << value << ")" << endl << endl;
168 property_element = el->FindNextElement("property");
171 // End of interface property loading logic
173 // Load model pre-functions, if any
175 Element *function = el->FindElement("function");
177 if (function->GetAttributeValue("type") == "pre") {
178 PreFunctions.push_back(new FGFunction(PropertyManager, function));
179 } else if (function->GetAttributeValue("type").empty()) { // Assume pre-function
180 string funcname = function->GetAttributeValue("name");
181 PreFunctions.push_back(new FGFunction(PropertyManager, function));
183 function = el->FindNextElement("function");
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 void FGModel::PostLoad(Element* el)
193 // Load model post-functions, if any
195 Element *function = el->FindElement("function");
197 if (function->GetAttributeValue("type") == "post") {
198 PostFunctions.push_back(new FGFunction(PropertyManager, function));
200 function = el->FindNextElement("function");
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 // Tell the Functions to cache values, so when the function values
206 // are being used in the model, the functions do not get
207 // calculated each time, but instead use the values that have already
208 // been calculated for this frame.
210 void FGModel::RunPreFunctions(void)
212 vector <FGFunction*>::iterator it;
213 for (it = PreFunctions.begin(); it != PreFunctions.end(); it++)
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 // Tell the Functions to cache values, so when the function values
219 // are being used in the model, the functions do not get
220 // calculated each time, but instead use the values that have already
221 // been calculated for this frame.
223 void FGModel::RunPostFunctions(void)
225 vector <FGFunction*>::iterator it;
226 for (it = PostFunctions.begin(); it != PostFunctions.end(); it++)
230 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
236 if (rate == 1) return false; // Fast exit if nothing to do
238 if (exe_ctr >= rate) exe_ctr = 1;
240 if (exe_ctr++ == 1) return false;
244 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 // The bitmasked value choices are as follows:
246 // unset: In this case (the default) JSBSim would only print
247 // out the normally expected messages, essentially echoing
248 // the config files as they are read. If the environment
249 // variable is not set, debug_lvl is set to 1 internally
250 // 0: This requests JSBSim not to output any messages
252 // 1: This value explicity requests the normal JSBSim
254 // 2: This value asks for a message to be printed out when
255 // a class is instantiated
256 // 4: When this value is set, a message is displayed when a
257 // FGModel object executes its Run() method
258 // 8: When this value is set, various runtime state variables
259 // are printed out periodically
260 // 16: When set various parameters are sanity checked and
261 // a message is printed out when they go out of bounds
263 void FGModel::Debug(int from)
265 if (debug_lvl <= 0) return;
267 if (debug_lvl & 1) { // Standard console startup message output
268 if (from == 0) { // Constructor
272 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
273 if (from == 0) cout << "Instantiated: FGModel" << endl;
274 if (from == 1) cout << "Destroyed: FGModel" << endl;
276 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
278 if (debug_lvl & 8 ) { // Runtime state variables
280 if (debug_lvl & 16) { // Sanity checking
282 if (debug_lvl & 64) {
283 if (from == 0) { // Constructor
284 cout << IdSrc << endl;
285 cout << IdHdr << endl;