]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGModel.cpp
Sync with JSBSim CVS again
[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
56 namespace JSBSim {
57
58 static const char *IdSrc = "$Id$";
59 static const char *IdHdr = ID_MODEL;
60
61 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 GLOBAL DECLARATIONS
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 CLASS IMPLEMENTATION
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68
69 FGModel::FGModel(FGFDMExec* fdmex)
70 {
71   FDMExec     = fdmex;
72   NextModel   = 0L;
73
74   State           = 0;
75   Atmosphere      = 0;
76   FCS             = 0;
77   Propulsion      = 0;
78   MassBalance     = 0;
79   Aerodynamics    = 0;
80   Inertial        = 0;
81   GroundReactions = 0;
82   ExternalReactions = 0;
83   Aircraft        = 0;
84   Propagate       = 0;
85   Auxiliary       = 0;
86
87   //in order for FGModel derived classes to self-bind (that is, call
88   //their bind function in the constructor, the PropertyManager pointer
89   //must be brought up now.
90   PropertyManager = FDMExec->GetPropertyManager();
91
92   exe_ctr     = 1;
93   rate        = 1;
94
95   if (debug_lvl & 2) cout << "              FGModel Base Class" << endl;
96 }
97
98 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99
100 FGModel::~FGModel()
101 {
102   for (unsigned int i=0; i<interface_properties.size(); i++) delete interface_properties[i];
103   interface_properties.clear();
104
105   if (debug_lvl & 2) cout << "Destroyed:    FGModel" << endl;
106 }
107
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110 bool FGModel::InitModel(void)
111 {
112   State           = FDMExec->GetState();
113   Atmosphere      = FDMExec->GetAtmosphere();
114   FCS             = FDMExec->GetFCS();
115   Propulsion      = FDMExec->GetPropulsion();
116   MassBalance     = FDMExec->GetMassBalance();
117   Aerodynamics    = FDMExec->GetAerodynamics();
118   Inertial        = FDMExec->GetInertial();
119   GroundReactions = FDMExec->GetGroundReactions();
120   ExternalReactions = FDMExec->GetExternalReactions();
121   BuoyantForces   = FDMExec->GetBuoyantForces();
122   Aircraft        = FDMExec->GetAircraft();
123   Propagate       = FDMExec->GetPropagate();
124   Auxiliary       = FDMExec->GetAuxiliary();
125
126   if (!State ||
127       !Atmosphere ||
128       !FCS ||
129       !Propulsion ||
130       !MassBalance ||
131       !Aerodynamics ||
132       !Inertial ||
133       !GroundReactions ||
134       !ExternalReactions ||
135       !Aircraft ||
136       !Propagate ||
137       !Auxiliary) return(false);
138   else return(true);
139 }
140
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142
143 bool FGModel::Load(Element* el)
144 {
145   // Interface properties are all stored in the interface properties array.
146
147   string interface_property_string = "";
148
149   Element *property_element = el->FindElement("property");
150   if (property_element && debug_lvl > 0) cout << endl << "    Declared properties" << endl << endl;
151   while (property_element) {
152     interface_property_string = property_element->GetDataLine();
153     if (PropertyManager->HasNode(interface_property_string)) {
154       cerr << "      Property " << interface_property_string << " is already defined." << endl;
155     } else {
156       double value=0.0;
157       if ( ! property_element->GetAttributeValue("value").empty())
158         value = property_element->GetAttributeValueAsNumber("value");
159       interface_properties.push_back(new double(value));
160       PropertyManager->Tie(interface_property_string, interface_properties.back());
161       if (debug_lvl > 0)
162         cout << "      " << interface_property_string << " (initial value: " << value << ")" << endl;
163     }
164     property_element = el->FindNextElement("property");
165   }
166   
167   return true;
168 }
169
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171
172 bool FGModel::Run()
173 {
174   if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
175
176   if (exe_ctr++ >= rate) exe_ctr = 1;
177
178   if (exe_ctr == 1) return false;
179   else              return true;
180 }
181
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 //    The bitmasked value choices are as follows:
184 //    unset: In this case (the default) JSBSim would only print
185 //       out the normally expected messages, essentially echoing
186 //       the config files as they are read. If the environment
187 //       variable is not set, debug_lvl is set to 1 internally
188 //    0: This requests JSBSim not to output any messages
189 //       whatsoever.
190 //    1: This value explicity requests the normal JSBSim
191 //       startup messages
192 //    2: This value asks for a message to be printed out when
193 //       a class is instantiated
194 //    4: When this value is set, a message is displayed when a
195 //       FGModel object executes its Run() method
196 //    8: When this value is set, various runtime state variables
197 //       are printed out periodically
198 //    16: When set various parameters are sanity checked and
199 //       a message is printed out when they go out of bounds
200
201 void FGModel::Debug(int from)
202 {
203   if (debug_lvl <= 0) return;
204
205   if (debug_lvl & 1) { // Standard console startup message output
206     if (from == 0) { // Constructor
207
208     }
209   }
210   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
211     if (from == 0) cout << "Instantiated: FGModel" << endl;
212     if (from == 1) cout << "Destroyed:    FGModel" << endl;
213   }
214   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
215   }
216   if (debug_lvl & 8 ) { // Runtime state variables
217   }
218   if (debug_lvl & 16) { // Sanity checking
219   }
220   if (debug_lvl & 64) {
221     if (from == 0) { // Constructor
222       cout << IdSrc << endl;
223       cout << IdHdr << endl;
224     }
225   }
226 }
227 }