JSBSim Flight Dynamics Model  1.1.11 (13 Feb 2022)
An Open Source Flight Dynamics and Control Software Library in C++
FGModelFunctions.cpp
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 
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
28 
29 HISTORY
30 --------------------------------------------------------------------------------
31 
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 
40 #include "FGFDMExec.h"
41 #include "FGModelFunctions.h"
42 #include "input_output/FGXMLElement.h"
43 
44 using namespace std;
45 
46 namespace JSBSim {
47 
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 CLASS IMPLEMENTATION
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 
52 FGModelFunctions::~FGModelFunctions()
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 
62 bool FGModelFunctions::InitModel(void)
63 {
64  LocalProperties.ResetToIC();
65 
66  return true;
67 }
68 
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 
71 bool 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 
81 void 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 
102 void 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 
121 void FGModelFunctions::RunPreFunctions(void)
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 
133 void FGModelFunctions::RunPostFunctions(void)
134 {
135  for (auto postfunc: PostFunctions)
136  postfunc->cacheValue(true);
137 }
138 
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 
141 FGFunction* FGModelFunctions::GetPreFunction(const std::string& name)
142 {
143  for (auto prefunc: PreFunctions) {
144  if (prefunc->GetName() == name)
145  return prefunc;
146  }
147 
148  return nullptr;
149 }
150 
151 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 
153 string 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 
176 string 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 }
JSBSim::FGFunction
Represents a mathematical function.
Definition: FGFunction.h:752
JSBSim::FGFunction::GetName
std::string GetName(void) const override
Retrieves the name of the function.
Definition: FGFunction.h:798