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