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
13 Software Foundation; either version 2 of the License, or (at your option) any
14 later 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
22 with this program; if not, write to the Free Software Foundation, Inc., 59
23 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Further information about the GNU Lesser General Public License can also be
26 found on 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
31 methods 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 "input_output/FGModelLoader.h"
44
45 using namespace std;
46
47 namespace JSBSim {
48
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 GLOBAL DECLARATIONS
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 CLASS IMPLEMENTATION
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
FGModel(FGFDMExec * fdmex)57 FGModel::FGModel(FGFDMExec* fdmex)
58 {
59 FDMExec = fdmex;
60
61 //in order for FGModel derived classes to self-bind (that is, call
62 //their bind function in the constructor, the PropertyManager pointer
63 //must be brought up now.
64 PropertyManager = FDMExec->GetPropertyManager();
65
66 exe_ctr = 1;
67 rate = 1;
68
69 if (debug_lvl & 2) cout << " FGModel Base Class" << endl;
70 }
71
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73
~FGModel()74 FGModel::~FGModel()
75 {
76 if (debug_lvl & 2) cout << "Destroyed: FGModel" << endl;
77 }
78
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
InitModel(void)81 bool FGModel::InitModel(void)
82 {
83 exe_ctr = 1;
84 return FGModelFunctions::InitModel();
85 }
86
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
Run(bool Holding)89 bool FGModel::Run(bool Holding)
90 {
91 if (debug_lvl & 4) cout << "Entering Run() for model " << Name << endl;
92
93 if (rate == 1) return false; // Fast exit if nothing to do
94
95 if (exe_ctr >= rate) exe_ctr = 0;
96
97 if (exe_ctr++ == 1) return false;
98 else return true;
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
FindFullPathName(const SGPath & path) const103 SGPath FGModel::FindFullPathName(const SGPath& path) const
104 {
105 return CheckPathName(FDMExec->GetFullAircraftPath(), path);
106 }
107
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
Load(Element * el,bool preLoad)110 bool FGModel::Load(Element* el, bool preLoad)
111 {
112 FGModelLoader ModelLoader(this);
113 Element* document = ModelLoader.Open(el);
114
115 if (!document) return false;
116
117 if (document->GetName() != el->GetName()) {
118 cerr << el->ReadFrom()
119 << " Read model '" << document->GetName()
120 << "' while expecting model '" << el->GetName() << "'" << endl;
121 return false;
122 }
123
124 bool result = true;
125
126 if (preLoad)
127 result = FGModelFunctions::Load(document, FDMExec);
128
129 if (document != el) {
130 el->MergeAttributes(document);
131
132 if (preLoad) {
133 // After reading interface properties in a file, read properties in the
134 // local model element. This allows general-purpose models to be defined
135 // in a file, with overrides or initial loaded constants supplied in the
136 // relevant element of the aircraft configuration file.
137 LocalProperties.Load(el, PropertyManager, true);
138 }
139
140 Element* element = document->FindElement();
141 while (element) {
142 el->AddChildElement(element);
143 element->SetParent(el);
144 element = document->FindNextElement();
145 }
146 }
147
148 return result;
149 }
150
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 // The bitmasked value choices are as follows:
153 // unset: In this case (the default) JSBSim would only print
154 // out the normally expected messages, essentially echoing
155 // the config files as they are read. If the environment
156 // variable is not set, debug_lvl is set to 1 internally
157 // 0: This requests JSBSim not to output any messages
158 // whatsoever.
159 // 1: This value explicity requests the normal JSBSim
160 // startup messages
161 // 2: This value asks for a message to be printed out when
162 // a class is instantiated
163 // 4: When this value is set, a message is displayed when a
164 // FGModel object executes its Run() method
165 // 8: When this value is set, various runtime state variables
166 // are printed out periodically
167 // 16: When set various parameters are sanity checked and
168 // a message is printed out when they go out of bounds
169
Debug(int from)170 void FGModel::Debug(int from)
171 {
172 if (debug_lvl <= 0) return;
173
174 if (debug_lvl & 1) { // Standard console startup message output
175 if (from == 0) { // Constructor
176
177 }
178 }
179 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
180 if (from == 0) cout << "Instantiated: FGModel" << endl;
181 if (from == 1) cout << "Destroyed: FGModel" << endl;
182 }
183 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
184 }
185 if (debug_lvl & 8 ) { // Runtime state variables
186 }
187 if (debug_lvl & 16) { // Sanity checking
188 }
189 if (debug_lvl & 64) {
190 if (from == 0) { // Constructor
191 }
192 }
193 }
194 }
195