]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGModel.cpp
Merge branch 'curt/navradio'
[flightgear.git] / src / FDM / JSBSim / models / 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 "FGState.h"
43 #include "FGFDMExec.h"
44 #include "FGAtmosphere.h"
45 #include "FGFCS.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"
55 #include <iostream>
56
57 using namespace std;
58
59 namespace JSBSim {
60
61 static const char *IdSrc = "$Id$";
62 static const char *IdHdr = ID_MODEL;
63
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 GLOBAL DECLARATIONS
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 CLASS IMPLEMENTATION
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71
72 FGModel::FGModel(FGFDMExec* fdmex)
73 {
74   FDMExec     = fdmex;
75   NextModel   = 0L;
76
77   State           = 0;
78   Atmosphere      = 0;
79   FCS             = 0;
80   Propulsion      = 0;
81   MassBalance     = 0;
82   Aerodynamics    = 0;
83   Inertial        = 0;
84   GroundReactions = 0;
85   ExternalReactions = 0;
86   Aircraft        = 0;
87   Propagate       = 0;
88   Auxiliary       = 0;
89
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();
94
95   exe_ctr     = 1;
96   rate        = 1;
97
98   if (debug_lvl & 2) cout << "              FGModel Base Class" << endl;
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103 FGModel::~FGModel()
104 {
105   for (unsigned int i=0; i<interface_properties.size(); i++) delete interface_properties[i];
106   interface_properties.clear();
107
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];
110
111   if (debug_lvl & 2) cout << "Destroyed:    FGModel" << endl;
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 bool FGModel::InitModel(void)
117 {
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();
131
132   if (!State ||
133       !Atmosphere ||
134       !FCS ||
135       !Propulsion ||
136       !MassBalance ||
137       !Aerodynamics ||
138       !Inertial ||
139       !GroundReactions ||
140       !ExternalReactions ||
141       !Aircraft ||
142       !Propagate ||
143       !Auxiliary) return(false);
144   else return(true);
145 }
146
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148
149 bool FGModel::Load(Element* el)
150 {
151   // Interface properties are all stored in the interface properties array.
152   string interface_property_string = "";
153
154   Element *property_element = el->FindElement("property");
155   if (property_element && debug_lvl > 0) cout << endl << "    Declared properties" 
156                                               << endl << endl;
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;
162     } else {
163       double value=0.0;
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());
168       if (debug_lvl > 0)
169         cout << "      " << interface_property_string << " (initial value: " 
170              << value << ")" << endl << endl;
171     }
172     property_element = el->FindNextElement("property");
173   }
174   
175   // End of interface property loading logic
176
177   // Load model pre-functions, if any
178
179   Element *function = el->FindElement("function");
180   while (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));
186     }
187     function = el->FindNextElement("function");
188   }
189
190   return true;
191 }
192
193 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194
195 void FGModel::PostLoad(Element* el)
196 {
197   // Load model post-functions, if any
198
199   Element *function = el->FindElement("function");
200   while (function) {
201     if (function->GetAttributeValue("type") == "post") {
202       PostFunctions.push_back(new FGFunction(PropertyManager, function));
203     }
204     function = el->FindNextElement("function");
205   }
206 }
207
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.
213
214 void FGModel::RunPreFunctions(void)
215 {
216   vector <FGFunction*>::iterator it;
217   for (it = PreFunctions.begin(); it != PreFunctions.end(); it++)
218     (*it)->GetValue();
219 }
220
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.
226
227 void FGModel::RunPostFunctions(void)
228 {
229   vector <FGFunction*>::iterator it;
230   for (it = PostFunctions.begin(); it != PostFunctions.end(); it++)
231     (*it)->GetValue();
232 }
233
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235
236 bool FGModel::Run()
237 {
238   if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
239
240   if (rate == 1) return false; // Fast exit if nothing to do
241
242   if (exe_ctr >= rate) exe_ctr = 1;
243
244   if (exe_ctr++ == 1) return false;
245   else              return true;
246 }
247
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
255 //       whatsoever.
256 //    1: This value explicity requests the normal JSBSim
257 //       startup messages
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
266
267 void FGModel::Debug(int from)
268 {
269   if (debug_lvl <= 0) return;
270
271   if (debug_lvl & 1) { // Standard console startup message output
272     if (from == 0) { // Constructor
273
274     }
275   }
276   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
277     if (from == 0) cout << "Instantiated: FGModel" << endl;
278     if (from == 1) cout << "Destroyed:    FGModel" << endl;
279   }
280   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
281   }
282   if (debug_lvl & 8 ) { // Runtime state variables
283   }
284   if (debug_lvl & 16) { // Sanity checking
285   }
286   if (debug_lvl & 64) {
287     if (from == 0) { // Constructor
288       cout << IdSrc << endl;
289       cout << IdHdr << endl;
290     }
291   }
292 }
293 }