FlightGear next
FGModelFunctions.cpp
Go to the documentation of this file.
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGModelFunctions.cpp
4 Author: Jon S. Berndt
5 Date started: August 2010
6
7 ------- Copyright (C) 2010 Jon S. Berndt (jon@jsbsim.org) ------------------
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 details.
18
19 You should have received a copy of the GNU Lesser General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc., 59
21 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on the world wide web at http://www.gnu.org.
25
26FUNCTIONAL DESCRIPTION
27--------------------------------------------------------------------------------
28
29HISTORY
30--------------------------------------------------------------------------------
31
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33COMMENTS, REFERENCES, and NOTES
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37INCLUDES
38%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40#include "FGFDMExec.h"
41#include "FGModelFunctions.h"
43
44using namespace std;
45
46namespace JSBSim {
47
48/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49CLASS IMPLEMENTATION
50%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
53{
54 for (auto prefunc: PreFunctions) delete prefunc;
55 for (auto postfunc: PostFunctions) delete postfunc;
56
57 if (debug_lvl & 2) cout << "Destroyed: FGModelFunctions" << endl;
58}
59
60//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61
63{
64 LocalProperties.ResetToIC();
65
66 return true;
67}
68
69//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70
71bool FGModelFunctions::Load(Element* el, FGFDMExec* fdmex, string prefix)
72{
73 LocalProperties.Load(el, fdmex->GetPropertyManager(), false);
74 PreLoad(el, fdmex, prefix);
75
76 return true; // TODO: Need to make this value mean something.
77}
78
79//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
81void FGModelFunctions::PreLoad(Element* el, FGFDMExec* fdmex, string prefix)
82{
83 // Load model post-functions, if any
84
85 Element *function = el->FindElement("function");
86
87 while (function) {
88 string fType = function->GetAttributeValue("type");
89 if (fType.empty() || fType == "pre")
90 PreFunctions.push_back(new FGFunction(fdmex, function, prefix));
91 else if (fType == "template") {
92 string name = function->GetAttributeValue("name");
93 fdmex->AddTemplateFunc(name, function);
94 }
95
96 function = el->FindNextElement("function");
97 }
98}
99
100//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101
102void FGModelFunctions::PostLoad(Element* el, FGFDMExec* fdmex, string prefix)
103{
104 // Load model post-functions, if any
105
106 Element *function = el->FindElement("function");
107 while (function) {
108 if (function->GetAttributeValue("type") == "post") {
109 PostFunctions.push_back(new FGFunction(fdmex, function, prefix));
110 }
111 function = el->FindNextElement("function");
112 }
113}
114
115//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116// Tell the Functions to cache values, so when the function values
117// are being used in the model, the functions do not get
118// calculated each time, but instead use the values that have already
119// been calculated for this frame.
120
122{
123 for (auto prefunc: PreFunctions)
124 prefunc->cacheValue(true);
125}
126
127//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128// Tell the Functions to cache values, so when the function values
129// are being used in the model, the functions do not get
130// calculated each time, but instead use the values that have already
131// been calculated for this frame.
132
134{
135 for (auto postfunc: PostFunctions)
136 postfunc->cacheValue(true);
137}
138
139//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
142{
143 for (auto prefunc: PreFunctions) {
144 if (prefunc->GetName() == name)
145 return prefunc;
146 }
147
148 return nullptr;
149}
150
151//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152
153string FGModelFunctions::GetFunctionStrings(const string& delimeter) const
154{
155 string FunctionStrings;
156
157 for (auto prefunc: PreFunctions) {
158 if (!FunctionStrings.empty())
159 FunctionStrings += delimeter;
160
161 FunctionStrings += prefunc->GetName();
162 }
163
164 for (auto postfunc: PostFunctions) {
165 if (!FunctionStrings.empty())
166 FunctionStrings += delimeter;
167
168 FunctionStrings += postfunc->GetName();
169 }
170
171 return FunctionStrings;
172}
173
174//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175
176string FGModelFunctions::GetFunctionValues(const string& delimeter) const
177{
178 ostringstream buf;
179
180 for (auto prefunc: PreFunctions) {
181 if (buf.tellp() > 0) buf << delimeter;
182 buf << prefunc->GetValue();
183 }
184
185 for (auto postfunc: PostFunctions) {
186 if (buf.tellp() > 0) buf << delimeter;
187 buf << postfunc->GetValue();
188 }
189
190 return buf.str();
191}
192
193//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194
195}
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
Element * FindElement(const std::string &el="")
Searches for a specified element.
Element * FindNextElement(const std::string &el="")
Searches for the next element as specified.
void AddTemplateFunc(const std::string &name, Element *el)
Definition FGFDMExec.h:615
FGPropertyManager * GetPropertyManager(void)
Returns a pointer to the property manager object.
Represents a mathematical function.
Definition FGFunction.h:753
static short debug_lvl
Definition FGJSBBase.h:190
std::vector< FGFunction * > PreFunctions
std::string GetFunctionValues(const std::string &delimeter) const
Gets the function values.
std::vector< FGFunction * > PostFunctions
std::string GetFunctionStrings(const std::string &delimeter) const
Gets the strings for the current set of functions.
FGPropertyReader LocalProperties
void PostLoad(Element *el, FGFDMExec *fdmex, std::string prefix="")
bool Load(Element *el, FGFDMExec *fdmex, std::string prefix="")
virtual bool InitModel(void)
FGFunction * GetPreFunction(const std::string &name)
Get one of the "pre" function.
void PreLoad(Element *el, FGFDMExec *fdmex, std::string prefix="")
const char * name