JSBSim Flight Dynamics Model  1.0 (02 March 2017)
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 Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  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 with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23  Further information about the GNU Lesser General Public License can also be found on
24  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 <iostream>
41 #include <sstream>
42 #include <string>
43 
44 #include "FGModelFunctions.h"
45 #include "FGFunction.h"
46 #include "input_output/FGXMLElement.h"
47 
48 using namespace std;
49 
50 namespace JSBSim {
51 
52 IDENT(IdSrc,"$Id: FGModelFunctions.cpp,v 1.15 2015/03/28 14:49:01 bcoconni Exp $");
53 IDENT(IdHdr,ID_MODELFUNCTIONS);
54 
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 CLASS IMPLEMENTATION
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 
59 FGModelFunctions::~FGModelFunctions()
60 {
61  for (unsigned int i=0; i<PreFunctions.size(); i++) delete PreFunctions[i];
62  for (unsigned int i=0; i<PostFunctions.size(); i++) delete PostFunctions[i];
63 
64  if (debug_lvl & 2) cout << "Destroyed: FGModelFunctions" << endl;
65 }
66 
67 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 
69 bool FGModelFunctions::InitModel(void)
70 {
71  LocalProperties.ResetToIC();
72 
73  return true;
74 }
75 
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 
78 bool FGModelFunctions::Load(Element* el, FGPropertyManager* PM, string prefix)
79 {
80  LocalProperties.Load(el, PM, false);
81  PreLoad(el, PM, prefix);
82 
83  return true; // TODO: Need to make this value mean something.
84 }
85 
86 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 
88 void FGModelFunctions::PreLoad(Element* el, FGPropertyManager* PM, string prefix)
89 {
90  // Load model post-functions, if any
91 
92  Element *function = el->FindElement("function");
93 
94  while (function) {
95  string fType = function->GetAttributeValue("type");
96  if (fType.empty() || fType == "pre")
97  PreFunctions.push_back(new FGFunction(PM, function, prefix));
98 
99  function = el->FindNextElement("function");
100  }
101 }
102 
103 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 
105 void FGModelFunctions::PostLoad(Element* el, FGPropertyManager* PM, string prefix)
106 {
107  // Load model post-functions, if any
108 
109  Element *function = el->FindElement("function");
110  while (function) {
111  if (function->GetAttributeValue("type") == "post") {
112  PostFunctions.push_back(new FGFunction(PM, function, prefix));
113  }
114  function = el->FindNextElement("function");
115  }
116 }
117 
118 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 // Tell the Functions to cache values, so when the function values
120 // are being used in the model, the functions do not get
121 // calculated each time, but instead use the values that have already
122 // been calculated for this frame.
123 
124 void FGModelFunctions::RunPreFunctions(void)
125 {
126  size_t sz = PreFunctions.size();
127  for (unsigned int i=0; i<sz; i++) {
128  PreFunctions[i]->cacheValue(true);
129  }
130 }
131 
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 // Tell the Functions to cache values, so when the function values
134 // are being used in the model, the functions do not get
135 // calculated each time, but instead use the values that have already
136 // been calculated for this frame.
137 
138 void FGModelFunctions::RunPostFunctions(void)
139 {
140  size_t sz = PostFunctions.size();
141  for (unsigned int i=0; i<sz; i++) {
142  PostFunctions[i]->cacheValue(true);
143  }
144 }
145 
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 
148 FGFunction* FGModelFunctions::GetPreFunction(const std::string& name)
149 {
150  FGFunction* result;
151  vector<FGFunction*>::iterator it = PreFunctions.begin();
152 
153  for (; it != PreFunctions.end(); ++it) {
154  result = *it;
155  if (result->GetName() == name)
156  return result;
157  }
158 
159  return 0;
160 }
161 
162 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 
164 string FGModelFunctions::GetFunctionStrings(const string& delimeter) const
165 {
166  string FunctionStrings = "";
167  bool firstime = true;
168  unsigned int sd;
169 
170  for (sd = 0; sd < PreFunctions.size(); sd++) {
171  if (firstime) {
172  firstime = false;
173  } else {
174  FunctionStrings += delimeter;
175  }
176  FunctionStrings += PreFunctions[sd]->GetName();
177  }
178 
179  for (sd = 0; sd < PostFunctions.size(); sd++) {
180  if (firstime) {
181  firstime = false;
182  } else {
183  FunctionStrings += delimeter;
184  }
185  FunctionStrings += PostFunctions[sd]->GetName();
186  }
187 
188  return FunctionStrings;
189 }
190 
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 
193 string FGModelFunctions::GetFunctionValues(const string& delimeter) const
194 {
195  ostringstream buf;
196 
197  for (unsigned int sd = 0; sd < PreFunctions.size(); sd++) {
198  if (buf.tellp() > 0) buf << delimeter;
199  buf << PreFunctions[sd]->GetValue();
200  }
201 
202  for (unsigned int sd = 0; sd < PostFunctions.size(); sd++) {
203  if (buf.tellp() > 0) buf << delimeter;
204  buf << PostFunctions[sd]->GetValue();
205  }
206 
207  return buf.str();
208 }
209 
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 
212 }
STL namespace.
std::string GetName(void) const
Retrieves the name of the function.
Definition: FGFunction.h:731
Represents a mathematical function.
Definition: FGFunction.h:699