]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGModelFunctions.cpp
Merge commit 'refs/merge-requests/14' of git://gitorious.org/fg/flightgear into next
[flightgear.git] / src / FDM / JSBSim / math / 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 "FGModelFunctions.h"
41 #include <string>
42
43 using namespace std;
44
45 namespace JSBSim {
46
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;
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 CLASS IMPLEMENTATION
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 FGModelFunctions::~FGModelFunctions()
55 {
56   for (unsigned int i=0; i<interface_properties.size(); i++) delete interface_properties[i];
57   interface_properties.clear();
58
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];
61
62   if (debug_lvl & 2) cout << "Destroyed:    FGModelFunctions" << endl;
63 }
64
65 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66
67 bool FGModelFunctions::Load(Element* el, FGPropertyManager* PM, string prefix)
68 {
69   // Interface properties are all stored in the interface properties array.
70   string interface_property_string = "";
71
72   Element *property_element = el->FindElement("property");
73   if (property_element && debug_lvl > 0) cout << endl << "    Declared properties" 
74                                               << endl << endl;
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;
80     } else {
81       double value=0.0;
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());
86       if (debug_lvl > 0)
87         cout << "      " << interface_property_string << " (initial value: " 
88              << value << ")" << endl << endl;
89     }
90     property_element = el->FindNextElement("property");
91   }
92   
93   // End of interface property loading logic
94
95   PreLoad(el, PM, prefix);
96
97   return true; // TODO: Need to make this value mean something.
98 }
99
100 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101
102 void FGModelFunctions::PreLoad(Element* el, FGPropertyManager* PM, string prefix)
103 {
104   // Load model post-functions, if any
105
106   Element *function = el->FindElement("function");
107
108   while (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.
117       {
118         PreFunctions.push_back(new FGFunction(PM, function, prefix));
119       }
120     }
121     function = el->FindNextElement("function");
122   }
123 }
124
125 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126
127 void FGModelFunctions::PostLoad(Element* el, FGPropertyManager* PM, string prefix)
128 {
129   // Load model post-functions, if any
130
131   Element *function = el->FindElement("function");
132   while (function) {
133     if (function->GetAttributeValue("type") == "post") {
134       PostFunctions.push_back(new FGFunction(PM, function, prefix));
135     }
136     function = el->FindNextElement("function");
137   }
138 }
139
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.
145
146 void FGModelFunctions::RunPreFunctions(void)
147 {
148   vector <FGFunction*>::iterator it;
149   for (it = PreFunctions.begin(); it != PreFunctions.end(); it++)
150     (*it)->cacheValue(true);
151 }
152
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.
158
159 void FGModelFunctions::RunPostFunctions(void)
160 {
161   vector <FGFunction*>::iterator it;
162   for (it = PostFunctions.begin(); it != PostFunctions.end(); it++)
163     (*it)->GetValue();
164 }
165
166 }