]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGFunction.cpp
Merge branch 'work4' into next
[flightgear.git] / src / FDM / JSBSim / math / FGFunction.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGFunction.cpp
4 Author: Jon Berndt
5 Date started: 8/25/2004
6 Purpose: Stores various parameter types for functions
7
8  ------------- Copyright (C) 2004  Jon S. Berndt (jon@jsbsim.org) -------------
9
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
13  version.
14
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
18  details.
19
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.
23
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.
26
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28 INCLUDES
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
30
31 #include <sstream>
32 #include <iomanip>
33 #include <cstdlib>
34 #include <cmath>
35 #include "FGFunction.h"
36 #include "FGTable.h"
37 #include "FGPropertyValue.h"
38 #include "FGRealValue.h"
39 #include "input_output/FGXMLElement.h"
40 #include "input_output/FGPropertyManager.h"
41
42 using namespace std;
43
44 namespace JSBSim {
45
46 static const char *IdSrc = "$Id: FGFunction.cpp,v 1.32 2010/03/18 13:21:24 jberndt Exp $";
47 static const char *IdHdr = ID_FUNCTION;
48
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 CLASS IMPLEMENTATION
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52
53 FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, const string& prefix)
54                                       : PropertyManager(propMan), Prefix(prefix)
55 {
56   Element* element;
57   string operation, property_name;
58   cached = false;
59   cachedValue = -HUGE_VAL;
60   invlog2val = 1.0/log10(2.0);
61
62   property_string = "property";
63   value_string = "value";
64   table_string = "table";
65   p_string = "p";
66   v_string = "v";
67   t_string = "t";
68
69   function_string = "function";
70   description_string = "description";
71   sum_string = "sum";
72   difference_string = "difference";
73   product_string = "product";
74   quotient_string = "quotient";
75   pow_string = "pow";
76   exp_string = "exp";
77   log2_string = "log2";
78   ln_string = "ln";
79   log10_string = "log10";
80   abs_string = "abs";
81   sin_string = "sin";
82   cos_string = "cos";
83   tan_string = "tan";
84   asin_string = "asin";
85   acos_string = "acos";
86   atan_string = "atan";
87   atan2_string = "atan2";
88   min_string = "min";
89   max_string = "max";
90   avg_string = "avg";
91   fraction_string = "fraction";
92   mod_string = "mod";
93   random_string = "random";
94   integer_string = "integer";
95
96   Name = el->GetAttributeValue("name");
97   operation = el->GetName();
98
99   if (operation == function_string) {
100     Type = eTopLevel;
101   } else if (operation == product_string) {
102     Type = eProduct;
103   } else if (operation == difference_string) {
104     Type = eDifference;
105   } else if (operation == sum_string) {
106     Type = eSum;
107   } else if (operation == quotient_string) {
108     Type = eQuotient;
109   } else if (operation == pow_string) {
110     Type = ePow;
111   } else if (operation == log2_string) {
112     Type = eLog2;
113   } else if (operation == ln_string) {
114     Type = eLn;
115   } else if (operation == log10_string) {
116     Type = eLog10;
117   } else if (operation == abs_string) {
118     Type = eAbs;
119   } else if (operation == sin_string) {
120     Type = eSin;
121   } else if (operation == exp_string) {
122     Type = eExp;
123   } else if (operation == cos_string) {
124     Type = eCos;
125   } else if (operation == tan_string) {
126     Type = eTan;
127   } else if (operation == asin_string) {
128     Type = eASin;
129   } else if (operation == acos_string) {
130     Type = eACos;
131   } else if (operation == atan_string) {
132     Type = eATan;
133   } else if (operation == atan2_string) {
134     Type = eATan2;
135   } else if (operation == min_string) {
136     Type = eMin;
137   } else if (operation == max_string) {
138     Type = eMax;
139   } else if (operation == avg_string) {
140     Type = eAvg;
141   } else if (operation == fraction_string) {
142     Type = eFrac;
143   } else if (operation == integer_string) {
144     Type = eInteger;
145   } else if (operation == mod_string) {
146     Type = eMod;
147   } else if (operation == random_string) {
148     Type = eRandom;
149   } else if (operation != description_string) {
150     cerr << "Bad operation " << operation << " detected in configuration file" << endl;
151   }
152
153   element = el->GetElement();
154   if (!element) {
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;
160     cerr << reset;
161     exit(-2);
162   }
163   
164   while (element) {
165     operation = element->GetName();
166
167     // data types
168     if (operation == property_string || operation == p_string) {
169       property_name = element->GetDataLine();
170       FGPropertyManager* newNode = PropertyManager->GetNode(property_name);
171       if (newNode == 0) {
172         cerr << "The property " << property_name << " is undefined." << endl;
173         abort();
174       } else {
175         Parameters.push_back(new FGPropertyValue( newNode ));
176       }
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));
181     // operations
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 )
206     {
207       Parameters.push_back(new FGFunction(PropertyManager, element));
208     } else if (operation != description_string) {
209       cerr << "Bad operation " << operation << " detected in configuration file" << endl;
210     }
211     element = el->GetNextElement();
212   }
213
214   bind(); // Allow any function to save its value
215
216   Debug(0);
217 }
218
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
221 FGFunction::~FGFunction(void)
222 {
223   for (unsigned int i=0; i<Parameters.size(); i++) delete Parameters[i];
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 void FGFunction::cacheValue(bool cache)
229 {
230   cached = false; // Must set cached to false prior to calling GetValue(), else
231                   // it will _never_ calculate the value;
232   if (cache) {
233     cachedValue = GetValue();
234     cached = true;
235   }
236 }
237
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239
240 double FGFunction::GetValue(void) const
241 {
242   unsigned int i;
243   double scratch;
244
245   if (cached) return cachedValue;
246
247   double temp = Parameters[0]->GetValue();
248
249   switch (Type) {
250   case eTopLevel:
251     break;
252   case eProduct:
253     for (i=1;i<Parameters.size();i++) {
254       temp *= Parameters[i]->GetValue();
255     }
256     break;
257   case eDifference:
258     for (i=1;i<Parameters.size();i++) {
259       temp -= Parameters[i]->GetValue();
260     }
261     break;
262   case eSum:
263     for (i=1;i<Parameters.size();i++) {
264       temp += Parameters[i]->GetValue();
265     }
266     break;
267   case eQuotient:
268     if (Parameters[1]->GetValue() != 0.0)
269       temp /= Parameters[1]->GetValue();
270     else
271       temp = HUGE_VAL;
272     break;
273   case ePow:
274     temp = pow(temp,Parameters[1]->GetValue());
275     break;
276   case eExp:
277     temp = exp(temp);
278     break;
279   case eLog2:
280     if (temp > 0.00) temp = log10(temp)*invlog2val;
281     else temp = -HUGE_VAL;
282     break;
283   case eLn:
284     if (temp > 0.00) temp = log(temp);
285     else temp = -HUGE_VAL;
286     break;
287   case eLog10:
288     if (temp > 0.00) temp = log10(temp);
289     else temp = -HUGE_VAL;
290     break;
291   case eAbs:
292     temp = fabs(temp);
293     break;
294   case eSin:
295     temp = sin(temp);
296     break;
297   case eCos:
298     temp = cos(temp);
299     break;
300   case eTan:
301     temp = tan(temp);
302     break;
303   case eACos:
304     temp = acos(temp);
305     break;
306   case eASin:
307     temp = asin(temp);
308     break;
309   case eATan:
310     temp = atan(temp);
311     break;
312   case eATan2:
313     temp = atan2(temp, Parameters[1]->GetValue());
314     break;
315   case eMod:
316     temp = ((int)temp) % ((int) Parameters[1]->GetValue());
317     break;
318   case eMin:
319     for (i=1;i<Parameters.size();i++) {
320       if (Parameters[i]->GetValue() < temp) temp = Parameters[i]->GetValue();
321     }    
322     break;
323   case eMax:
324     for (i=1;i<Parameters.size();i++) {
325       if (Parameters[i]->GetValue() > temp) temp = Parameters[i]->GetValue();
326     }    
327     break;
328   case eAvg:
329     for (i=1;i<Parameters.size();i++) {
330       temp += Parameters[i]->GetValue();
331     }
332     temp /= Parameters.size();
333     break;
334   case eFrac:
335     temp = modf(temp, &scratch);
336     break;
337   case eInteger:
338     modf(temp, &scratch);
339     temp = scratch;
340     break;
341   case eRandom:
342     temp = GaussianRandomNumber();
343     break;
344   default:
345     cerr << "Unknown function operation type" << endl;
346     break;
347   }
348
349   return temp;
350 }
351
352 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353
354 string FGFunction::GetValueAsString(void) const
355 {
356   ostringstream buffer;
357
358   buffer << setw(9) << setprecision(6) << GetValue();
359   return buffer.str();
360 }
361
362 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363
364 void FGFunction::bind(void)
365 {
366   if ( !Name.empty() ) {
367     string tmp;
368     if (Prefix.empty())
369       tmp  = PropertyManager->mkPropertyName(Name, false); // Allow upper case
370     else
371       tmp  = PropertyManager->mkPropertyName(Prefix + "/" + Name, false); // Allow upper case
372
373     PropertyManager->Tie( tmp, this, &FGFunction::GetValue);
374   }
375 }
376
377 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 //    The bitmasked value choices are as follows:
379 //    unset: In this case (the default) JSBSim would only print
380 //       out the normally expected messages, essentially echoing
381 //       the config files as they are read. If the environment
382 //       variable is not set, debug_lvl is set to 1 internally
383 //    0: This requests JSBSim not to output any messages
384 //       whatsoever.
385 //    1: This value explicity requests the normal JSBSim
386 //       startup messages
387 //    2: This value asks for a message to be printed out when
388 //       a class is instantiated
389 //    4: When this value is set, a message is displayed when a
390 //       FGModel object executes its Run() method
391 //    8: When this value is set, various runtime state variables
392 //       are printed out periodically
393 //    16: When set various parameters are sanity checked and
394 //       a message is printed out when they go out of bounds
395
396 void FGFunction::Debug(int from)
397 {
398   if (debug_lvl <= 0) return;
399
400   if (debug_lvl & 1) { // Standard console startup message output
401     if (from == 0) { // Constructor
402       if (Type == eTopLevel)
403         cout << "    Function: " << Name << endl;
404     }
405   }
406   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
407     if (from == 0) cout << "Instantiated: FGFunction" << endl;
408     if (from == 1) cout << "Destroyed:    FGFunction" << endl;
409   }
410   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
411   }
412   if (debug_lvl & 8 ) { // Runtime state variables
413   }
414   if (debug_lvl & 16) { // Sanity checking
415   }
416   if (debug_lvl & 64) {
417     if (from == 0) { // Constructor
418       cout << IdSrc << endl;
419       cout << IdHdr << endl;
420     }
421   }
422 }
423
424 }