1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGModelFunctions.cpp
5 Date started: August 2010
7 ------- Copyright (C) 2010 Jon S. Berndt (jon@jsbsim.org) ------------------
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
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
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.
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.
26 FUNCTIONAL DESCRIPTION
27 --------------------------------------------------------------------------------
30 --------------------------------------------------------------------------------
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 COMMENTS, REFERENCES, and NOTES
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGModelFunctions.h"
47 static const char *IdSrc = "$Id: FGModelFunctions.cpp,v 1.4 2010/09/07 00:40:03 jberndt Exp $";
48 static const char *IdHdr = ID_MODELFUNCTIONS;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 FGModelFunctions::~FGModelFunctions()
56 for (unsigned int i=0; i<interface_properties.size(); i++) delete interface_properties[i];
57 interface_properties.clear();
59 for (unsigned int i=0; i<PreFunctions.size(); i++) delete PreFunctions[i];
60 for (unsigned int i=0; i<PostFunctions.size(); i++) delete PostFunctions[i];
62 if (debug_lvl & 2) cout << "Destroyed: FGModelFunctions" << endl;
65 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 bool FGModelFunctions::Load(Element* el, FGPropertyManager* PM, string prefix)
69 // Interface properties are all stored in the interface properties array.
70 string interface_property_string = "";
72 Element *property_element = el->FindElement("property");
73 if (property_element && debug_lvl > 0) cout << endl << " Declared properties"
75 while (property_element) {
76 interface_property_string = property_element->GetDataLine();
77 if (PM->HasNode(interface_property_string)) {
78 cerr << " Property " << interface_property_string
79 << " is already defined." << endl;
82 if ( ! property_element->GetAttributeValue("value").empty())
83 value = property_element->GetAttributeValueAsNumber("value");
84 interface_properties.push_back(new double(value));
85 PM->Tie(interface_property_string, interface_properties.back());
87 cout << " " << interface_property_string << " (initial value: "
88 << value << ")" << endl << endl;
90 property_element = el->FindNextElement("property");
93 // End of interface property loading logic
95 PreLoad(el, PM, prefix);
97 return true; // TODO: Need to make this value mean something.
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 void FGModelFunctions::PreLoad(Element* el, FGPropertyManager* PM, string prefix)
104 // Load model post-functions, if any
106 Element *function = el->FindElement("function");
109 if (function->GetAttributeValue("type") == "pre") {
110 PreFunctions.push_back(new FGFunction(PM, function, prefix));
111 } else if (function->GetAttributeValue("type").empty()) { // Assume pre-function
112 string funcname = function->GetAttributeValue("name");
113 if (funcname.find("IdleThrust") == string::npos && // Do not process functions that are
114 funcname.find("MilThrust") == string::npos && // already pre-defined turbine engine
115 funcname.find("AugThrust") == string::npos && // functions. These are loaded within
116 funcname.find("Injection") == string::npos ) // the Turbine::Load() method.
118 PreFunctions.push_back(new FGFunction(PM, function, prefix));
121 function = el->FindNextElement("function");
125 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 void FGModelFunctions::PostLoad(Element* el, FGPropertyManager* PM, string prefix)
129 // Load model post-functions, if any
131 Element *function = el->FindElement("function");
133 if (function->GetAttributeValue("type") == "post") {
134 PostFunctions.push_back(new FGFunction(PM, function, prefix));
136 function = el->FindNextElement("function");
140 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 // Tell the Functions to cache values, so when the function values
142 // are being used in the model, the functions do not get
143 // calculated each time, but instead use the values that have already
144 // been calculated for this frame.
146 void FGModelFunctions::RunPreFunctions(void)
148 vector <FGFunction*>::iterator it;
149 for (it = PreFunctions.begin(); it != PreFunctions.end(); it++)
150 (*it)->cacheValue(true);
153 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 // Tell the Functions to cache values, so when the function values
155 // are being used in the model, the functions do not get
156 // calculated each time, but instead use the values that have already
157 // been calculated for this frame.
159 void FGModelFunctions::RunPostFunctions(void)
161 vector <FGFunction*>::iterator it;
162 for (it = PostFunctions.begin(); it != PostFunctions.end(); it++)