]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGModel.cpp
Merge branch 'ehofman/sound'
[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   if (debug_lvl & 2) cout << "Destroyed:    FGModel" << endl;
109 }
110
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113 bool FGModel::InitModel(void)
114 {
115   State           = FDMExec->GetState();
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 (!State ||
130       !Atmosphere ||
131       !FCS ||
132       !Propulsion ||
133       !MassBalance ||
134       !Aerodynamics ||
135       !Inertial ||
136       !GroundReactions ||
137       !ExternalReactions ||
138       !Aircraft ||
139       !Propagate ||
140       !Auxiliary) return(false);
141   else return(true);
142 }
143
144 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145
146 bool FGModel::Load(Element* el)
147 {
148   // Interface properties are all stored in the interface properties array.
149
150   string interface_property_string = "";
151
152   Element *property_element = el->FindElement("property");
153   if (property_element && debug_lvl > 0) cout << endl << "    Declared properties" << endl << endl;
154   while (property_element) {
155     interface_property_string = property_element->GetDataLine();
156     if (PropertyManager->HasNode(interface_property_string)) {
157       cerr << "      Property " << interface_property_string << " 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: " << value << ")" << endl;
166     }
167     property_element = el->FindNextElement("property");
168   }
169   
170   return true;
171 }
172
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 bool FGModel::Run()
176 {
177   if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
178
179   if (exe_ctr++ >= rate) exe_ctr = 1;
180
181   if (exe_ctr == 1) return false;
182   else              return true;
183 }
184
185 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 //    The bitmasked value choices are as follows:
187 //    unset: In this case (the default) JSBSim would only print
188 //       out the normally expected messages, essentially echoing
189 //       the config files as they are read. If the environment
190 //       variable is not set, debug_lvl is set to 1 internally
191 //    0: This requests JSBSim not to output any messages
192 //       whatsoever.
193 //    1: This value explicity requests the normal JSBSim
194 //       startup messages
195 //    2: This value asks for a message to be printed out when
196 //       a class is instantiated
197 //    4: When this value is set, a message is displayed when a
198 //       FGModel object executes its Run() method
199 //    8: When this value is set, various runtime state variables
200 //       are printed out periodically
201 //    16: When set various parameters are sanity checked and
202 //       a message is printed out when they go out of bounds
203
204 void FGModel::Debug(int from)
205 {
206   if (debug_lvl <= 0) return;
207
208   if (debug_lvl & 1) { // Standard console startup message output
209     if (from == 0) { // Constructor
210
211     }
212   }
213   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
214     if (from == 0) cout << "Instantiated: FGModel" << endl;
215     if (from == 1) cout << "Destroyed:    FGModel" << endl;
216   }
217   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
218   }
219   if (debug_lvl & 8 ) { // Runtime state variables
220   }
221   if (debug_lvl & 16) { // Sanity checking
222   }
223   if (debug_lvl & 64) {
224     if (from == 0) { // Constructor
225       cout << IdSrc << endl;
226       cout << IdHdr << endl;
227     }
228   }
229 }
230 }