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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33 #include "FGFunction.h"
35 #include "FGPropertyValue.h"
36 #include "FGRealValue.h"
40 static const char *IdSrc = "$Id$";
41 static const char *IdHdr = ID_FUNCTION;
43 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47 FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, string prefix)
48 : PropertyManager(propMan), Prefix(prefix)
51 string operation, property_name;
53 cachedValue = -HUGE_VAL;
55 property_string = "property";
56 value_string = "value";
57 table_string = "table";
62 function_string = "function";
63 description_string = "description";
65 difference_string = "difference";
66 product_string = "product";
67 quotient_string = "quotient";
77 atan2_string = "atan2";
81 fraction_string = "fraction";
83 random_string = "random";
84 integer_string = "integer";
86 Name = el->GetAttributeValue("name");
87 operation = el->GetName();
89 if (operation == function_string) {
91 } else if (operation == product_string) {
93 } else if (operation == difference_string) {
95 } else if (operation == sum_string) {
97 } else if (operation == quotient_string) {
99 } else if (operation == pow_string) {
101 } else if (operation == abs_string) {
103 } else if (operation == sin_string) {
105 } else if (operation == exp_string) {
107 } else if (operation == cos_string) {
109 } else if (operation == tan_string) {
111 } else if (operation == asin_string) {
113 } else if (operation == acos_string) {
115 } else if (operation == atan_string) {
117 } else if (operation == atan2_string) {
119 } else if (operation == min_string) {
121 } else if (operation == max_string) {
123 } else if (operation == avg_string) {
125 } else if (operation == fraction_string) {
127 } else if (operation == integer_string) {
129 } else if (operation == mod_string) {
131 } else if (operation == random_string) {
133 } else if (operation != description_string) {
134 cerr << "Bad operation " << operation << " detected in configuration file" << endl;
137 element = el->GetElement();
139 cerr << fgred << highint << endl;
140 cerr << " No element was specified as an argument to the \"" << operation << "\" operation" << endl;
141 cerr << " This can happen when, for instance, a cos operation is specified and a " << endl;
142 cerr << " property name is given explicitly, but is not placed within a" << endl;
143 cerr << " <property></property> element tag pair." << endl;
149 operation = element->GetName();
152 if (operation == property_string || operation == p_string) {
153 property_name = element->GetDataLine();
154 FGPropertyManager* newNode = PropertyManager->GetNode(property_name);
156 cerr << "The property " << property_name << " is undefined." << endl;
159 Parameters.push_back(new FGPropertyValue( newNode ));
161 } else if (operation == value_string || operation == v_string) {
162 Parameters.push_back(new FGRealValue(element->GetDataAsNumber()));
163 } else if (operation == table_string || operation == t_string) {
164 Parameters.push_back(new FGTable(PropertyManager, element));
166 } else if (operation == product_string ||
167 operation == difference_string ||
168 operation == sum_string ||
169 operation == quotient_string ||
170 operation == pow_string ||
171 operation == exp_string ||
172 operation == abs_string ||
173 operation == sin_string ||
174 operation == cos_string ||
175 operation == tan_string ||
176 operation == asin_string ||
177 operation == acos_string ||
178 operation == atan_string ||
179 operation == atan2_string ||
180 operation == min_string ||
181 operation == max_string ||
182 operation == fraction_string ||
183 operation == integer_string ||
184 operation == mod_string ||
185 operation == random_string ||
186 operation == avg_string )
188 Parameters.push_back(new FGFunction(PropertyManager, element));
189 } else if (operation != description_string) {
190 cerr << "Bad operation " << operation << " detected in configuration file" << endl;
192 element = el->GetNextElement();
195 bind(); // Allow any function to save its value
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 FGFunction::~FGFunction(void)
204 for (unsigned int i=0; i<Parameters.size(); i++) delete Parameters[i];
207 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 void FGFunction::cacheValue(bool cache)
211 cached = false; // Must set cached to false prior to calling GetValue(), else
212 // it will _never_ calculate the value;
214 cachedValue = GetValue();
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 double FGFunction::GetValue(void) const
226 if (cached) return cachedValue;
228 double temp = Parameters[0]->GetValue();
234 for (i=1;i<Parameters.size();i++) {
235 temp *= Parameters[i]->GetValue();
239 for (i=1;i<Parameters.size();i++) {
240 temp -= Parameters[i]->GetValue();
244 for (i=1;i<Parameters.size();i++) {
245 temp += Parameters[i]->GetValue();
249 temp /= Parameters[1]->GetValue();
252 temp = pow(temp,Parameters[1]->GetValue());
279 temp = atan2(temp, Parameters[1]->GetValue());
282 temp = ((int)temp) % ((int) Parameters[1]->GetValue());
285 for (i=1;i<Parameters.size();i++) {
286 if (Parameters[i]->GetValue() < temp) temp = Parameters[i]->GetValue();
290 for (i=1;i<Parameters.size();i++) {
291 if (Parameters[i]->GetValue() > temp) temp = Parameters[i]->GetValue();
295 for (i=1;i<Parameters.size();i++) {
296 temp += Parameters[i]->GetValue();
298 temp /= Parameters.size();
301 temp = modf(temp, &scratch);
304 modf(temp, &scratch);
308 temp = GaussianRandomNumber();
311 cerr << "Unknown function operation type" << endl;
318 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 string FGFunction::GetValueAsString(void) const
325 sprintf(buffer,"%9.6f",GetValue());
326 value = string(buffer);
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 void FGFunction::bind(void)
334 if ( !Name.empty() ) {
335 string tmp = PropertyManager->mkPropertyName(Prefix + Name, false); // Allow upper case
336 PropertyManager->Tie( tmp, this, &FGFunction::GetValue);
340 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 // The bitmasked value choices are as follows:
342 // unset: In this case (the default) JSBSim would only print
343 // out the normally expected messages, essentially echoing
344 // the config files as they are read. If the environment
345 // variable is not set, debug_lvl is set to 1 internally
346 // 0: This requests JSBSim not to output any messages
348 // 1: This value explicity requests the normal JSBSim
350 // 2: This value asks for a message to be printed out when
351 // a class is instantiated
352 // 4: When this value is set, a message is displayed when a
353 // FGModel object executes its Run() method
354 // 8: When this value is set, various runtime state variables
355 // are printed out periodically
356 // 16: When set various parameters are sanity checked and
357 // a message is printed out when they go out of bounds
359 void FGFunction::Debug(int from)
361 if (debug_lvl <= 0) return;
363 if (debug_lvl & 1) { // Standard console startup message output
364 if (from == 0) { // Constructor
365 if (Type == eTopLevel)
366 cout << " Function: " << Name << endl;
369 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
370 if (from == 0) cout << "Instantiated: FGFunction" << endl;
371 if (from == 1) cout << "Destroyed: FGFunction" << endl;
373 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
375 if (debug_lvl & 8 ) { // Runtime state variables
377 if (debug_lvl & 16) { // Sanity checking
379 if (debug_lvl & 64) {
380 if (from == 0) { // Constructor
381 cout << IdSrc << endl;
382 cout << IdHdr << endl;