1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 Date started: 8/25/2004
6 Purpose: Stores various parameter types for functions
8 ------------- Copyright (C) 2004 Jon S. Berndt (jon@jsbsim.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 You should have received a copy of the GNU Lesser General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU Lesser General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
35 #include "FGFunction.h"
37 #include "FGPropertyValue.h"
38 #include "FGRealValue.h"
39 #include "input_output/FGXMLElement.h"
40 #include "input_output/FGPropertyManager.h"
46 static const char *IdSrc = "$Id$";
47 static const char *IdHdr = ID_FUNCTION;
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53 FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, const string& prefix)
54 : PropertyManager(propMan), Prefix(prefix)
57 string operation, property_name;
59 cachedValue = -HUGE_VAL;
60 invlog2val = 1.0/log10(2.0);
62 property_string = "property";
63 value_string = "value";
64 table_string = "table";
69 function_string = "function";
70 description_string = "description";
72 difference_string = "difference";
73 product_string = "product";
74 quotient_string = "quotient";
79 log10_string = "log10";
87 atan2_string = "atan2";
91 fraction_string = "fraction";
93 random_string = "random";
94 integer_string = "integer";
96 Name = el->GetAttributeValue("name");
97 operation = el->GetName();
99 if (operation == function_string) {
101 } else if (operation == product_string) {
103 } else if (operation == difference_string) {
105 } else if (operation == sum_string) {
107 } else if (operation == quotient_string) {
109 } else if (operation == pow_string) {
111 } else if (operation == log2_string) {
113 } else if (operation == ln_string) {
115 } else if (operation == log10_string) {
117 } else if (operation == abs_string) {
119 } else if (operation == sin_string) {
121 } else if (operation == exp_string) {
123 } else if (operation == cos_string) {
125 } else if (operation == tan_string) {
127 } else if (operation == asin_string) {
129 } else if (operation == acos_string) {
131 } else if (operation == atan_string) {
133 } else if (operation == atan2_string) {
135 } else if (operation == min_string) {
137 } else if (operation == max_string) {
139 } else if (operation == avg_string) {
141 } else if (operation == fraction_string) {
143 } else if (operation == integer_string) {
145 } else if (operation == mod_string) {
147 } else if (operation == random_string) {
149 } else if (operation != description_string) {
150 cerr << "Bad operation " << operation << " detected in configuration file" << endl;
153 element = el->GetElement();
155 cerr << fgred << highint << endl;
156 cerr << " No element was specified as an argument to the \"" << operation << "\" operation" << endl;
157 cerr << " This can happen when, for instance, a cos operation is specified and a " << endl;
158 cerr << " property name is given explicitly, but is not placed within a" << endl;
159 cerr << " <property></property> element tag pair." << endl;
165 operation = element->GetName();
168 if (operation == property_string || operation == p_string) {
169 property_name = element->GetDataLine();
170 FGPropertyManager* newNode = PropertyManager->GetNode(property_name);
172 cerr << "The property " << property_name << " is undefined." << endl;
175 Parameters.push_back(new FGPropertyValue( newNode ));
177 } else if (operation == value_string || operation == v_string) {
178 Parameters.push_back(new FGRealValue(element->GetDataAsNumber()));
179 } else if (operation == table_string || operation == t_string) {
180 Parameters.push_back(new FGTable(PropertyManager, element));
182 } else if (operation == product_string ||
183 operation == difference_string ||
184 operation == sum_string ||
185 operation == quotient_string ||
186 operation == pow_string ||
187 operation == exp_string ||
188 operation == log2_string ||
189 operation == ln_string ||
190 operation == log10_string ||
191 operation == abs_string ||
192 operation == sin_string ||
193 operation == cos_string ||
194 operation == tan_string ||
195 operation == asin_string ||
196 operation == acos_string ||
197 operation == atan_string ||
198 operation == atan2_string ||
199 operation == min_string ||
200 operation == max_string ||
201 operation == fraction_string ||
202 operation == integer_string ||
203 operation == mod_string ||
204 operation == random_string ||
205 operation == avg_string )
207 Parameters.push_back(new FGFunction(PropertyManager, element));
208 } else if (operation != description_string) {
209 cerr << "Bad operation " << operation << " detected in configuration file" << endl;
211 element = el->GetNextElement();
214 bind(); // Allow any function to save its value
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 FGFunction::~FGFunction(void)
223 for (unsigned int i=0; i<Parameters.size(); i++) delete Parameters[i];
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 void FGFunction::cacheValue(bool cache)
230 cached = false; // Must set cached to false prior to calling GetValue(), else
231 // it will _never_ calculate the value;
233 cachedValue = GetValue();
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 double FGFunction::GetValue(void) const
245 if (cached) return cachedValue;
247 double temp = Parameters[0]->GetValue();
253 for (i=1;i<Parameters.size();i++) {
254 temp *= Parameters[i]->GetValue();
258 for (i=1;i<Parameters.size();i++) {
259 temp -= Parameters[i]->GetValue();
263 for (i=1;i<Parameters.size();i++) {
264 temp += Parameters[i]->GetValue();
268 temp /= Parameters[1]->GetValue();
271 temp = pow(temp,Parameters[1]->GetValue());
277 if (temp > 0.00) temp = log10(temp)*invlog2val;
278 else temp = -HUGE_VAL;
281 if (temp > 0.00) temp = log(temp);
282 else temp = -HUGE_VAL;
285 if (temp > 0.00) temp = log10(temp);
286 else temp = -HUGE_VAL;
310 temp = atan2(temp, Parameters[1]->GetValue());
313 temp = ((int)temp) % ((int) Parameters[1]->GetValue());
316 for (i=1;i<Parameters.size();i++) {
317 if (Parameters[i]->GetValue() < temp) temp = Parameters[i]->GetValue();
321 for (i=1;i<Parameters.size();i++) {
322 if (Parameters[i]->GetValue() > temp) temp = Parameters[i]->GetValue();
326 for (i=1;i<Parameters.size();i++) {
327 temp += Parameters[i]->GetValue();
329 temp /= Parameters.size();
332 temp = modf(temp, &scratch);
335 modf(temp, &scratch);
339 temp = GaussianRandomNumber();
342 cerr << "Unknown function operation type" << endl;
349 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 string FGFunction::GetValueAsString(void) const
353 ostringstream buffer;
355 buffer << setw(9) << setprecision(6) << GetValue();
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 void FGFunction::bind(void)
363 if ( !Name.empty() ) {
366 tmp = PropertyManager->mkPropertyName(Name, false); // Allow upper case
368 tmp = PropertyManager->mkPropertyName(Prefix + "/" + Name, false); // Allow upper case
370 PropertyManager->Tie( tmp, this, &FGFunction::GetValue);
374 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375 // The bitmasked value choices are as follows:
376 // unset: In this case (the default) JSBSim would only print
377 // out the normally expected messages, essentially echoing
378 // the config files as they are read. If the environment
379 // variable is not set, debug_lvl is set to 1 internally
380 // 0: This requests JSBSim not to output any messages
382 // 1: This value explicity requests the normal JSBSim
384 // 2: This value asks for a message to be printed out when
385 // a class is instantiated
386 // 4: When this value is set, a message is displayed when a
387 // FGModel object executes its Run() method
388 // 8: When this value is set, various runtime state variables
389 // are printed out periodically
390 // 16: When set various parameters are sanity checked and
391 // a message is printed out when they go out of bounds
393 void FGFunction::Debug(int from)
395 if (debug_lvl <= 0) return;
397 if (debug_lvl & 1) { // Standard console startup message output
398 if (from == 0) { // Constructor
399 if (Type == eTopLevel)
400 cout << " Function: " << Name << endl;
403 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
404 if (from == 0) cout << "Instantiated: FGFunction" << endl;
405 if (from == 1) cout << "Destroyed: FGFunction" << endl;
407 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
409 if (debug_lvl & 8 ) { // Runtime state variables
411 if (debug_lvl & 16) { // Sanity checking
413 if (debug_lvl & 64) {
414 if (from == 0) { // Constructor
415 cout << IdSrc << endl;
416 cout << IdHdr << endl;