00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <stdio.h>
00032
00033 #include "FGFunction.h"
00034 #include "FGTable.h"
00035 #include "FGPropertyValue.h"
00036 #include "FGRealValue.h"
00037
00038 namespace JSBSim {
00039
00040 static const char *IdSrc = "$Id: FGFunction.cpp,v 1.22 2009/03/04 13:13:36 jberndt Exp $";
00041 static const char *IdHdr = ID_FUNCTION;
00042
00043
00044
00045
00046
00047 FGFunction::FGFunction(FGPropertyManager* propMan, Element* el, string prefix)
00048 : PropertyManager(propMan), Prefix(prefix)
00049 {
00050 Element* element;
00051 string operation, property_name;
00052 int size = el->GetNumElements();
00053 cached = false;
00054 cachedValue = -HUGE_VAL;
00055
00056 property_string = "property";
00057 value_string = "value";
00058 table_string = "table";
00059 p_string = "p";
00060 v_string = "v";
00061 t_string = "t";
00062
00063 function_string = "function";
00064 description_string = "description";
00065 sum_string = "sum";
00066 difference_string = "difference";
00067 product_string = "product";
00068 quotient_string = "quotient";
00069 pow_string = "pow";
00070 exp_string = "exp";
00071 abs_string = "abs";
00072 sin_string = "sin";
00073 cos_string = "cos";
00074 tan_string = "tan";
00075 asin_string = "asin";
00076 acos_string = "acos";
00077 atan_string = "atan";
00078 atan2_string = "atan2";
00079 min_string = "min";
00080 max_string = "max";
00081 avg_string = "avg";
00082 fraction_string = "fraction";
00083 mod_string = "mod";
00084 random_string = "random";
00085 integer_string = "integer";
00086
00087 Name = el->GetAttributeValue("name");
00088 operation = el->GetName();
00089
00090 if (operation == function_string) {
00091 Type = eTopLevel;
00092 } else if (operation == product_string) {
00093 Type = eProduct;
00094 } else if (operation == difference_string) {
00095 Type = eDifference;
00096 } else if (operation == sum_string) {
00097 Type = eSum;
00098 } else if (operation == quotient_string) {
00099 Type = eQuotient;
00100 } else if (operation == pow_string) {
00101 Type = ePow;
00102 } else if (operation == abs_string) {
00103 Type = eAbs;
00104 } else if (operation == sin_string) {
00105 Type = eSin;
00106 } else if (operation == exp_string) {
00107 Type = eExp;
00108 } else if (operation == cos_string) {
00109 Type = eCos;
00110 } else if (operation == tan_string) {
00111 Type = eTan;
00112 } else if (operation == asin_string) {
00113 Type = eASin;
00114 } else if (operation == acos_string) {
00115 Type = eACos;
00116 } else if (operation == atan_string) {
00117 Type = eATan;
00118 } else if (operation == atan2_string) {
00119 Type = eATan2;
00120 } else if (operation == min_string) {
00121 Type = eMin;
00122 } else if (operation == max_string) {
00123 Type = eMax;
00124 } else if (operation == avg_string) {
00125 Type = eAvg;
00126 } else if (operation == fraction_string) {
00127 Type = eFrac;
00128 } else if (operation == integer_string) {
00129 Type = eInteger;
00130 } else if (operation == mod_string) {
00131 Type = eMod;
00132 } else if (operation == random_string) {
00133 Type = eRandom;
00134 } else if (operation != description_string) {
00135 cerr << "Bad operation " << operation << " detected in configuration file" << endl;
00136 }
00137
00138 element = el->GetElement();
00139 if (!element) {
00140 cerr << fgred << highint << endl;
00141 cerr << " No element was specified as an argument to the \"" << operation << "\" operation" << endl;
00142 cerr << " This can happen when, for instance, a cos operation is specified and a " << endl;
00143 cerr << " property name is given explicitly, but is not placed within a" << endl;
00144 cerr << " <property></property> element tag pair." << endl;
00145 cerr << reset;
00146 exit(-2);
00147 }
00148
00149 while (element) {
00150 operation = element->GetName();
00151
00152
00153 if (operation == property_string || operation == p_string) {
00154 property_name = element->GetDataLine();
00155 FGPropertyManager* newNode = PropertyManager->GetNode(property_name);
00156 if (newNode == 0) {
00157 cerr << "The property " << property_name << " is undefined." << endl;
00158 abort();
00159 } else {
00160 Parameters.push_back(new FGPropertyValue( newNode ));
00161 }
00162 } else if (operation == value_string || operation == v_string) {
00163 Parameters.push_back(new FGRealValue(element->GetDataAsNumber()));
00164 } else if (operation == table_string || operation == t_string) {
00165 Parameters.push_back(new FGTable(PropertyManager, element));
00166
00167 } else if (operation == product_string ||
00168 operation == difference_string ||
00169 operation == sum_string ||
00170 operation == quotient_string ||
00171 operation == pow_string ||
00172 operation == exp_string ||
00173 operation == abs_string ||
00174 operation == sin_string ||
00175 operation == cos_string ||
00176 operation == tan_string ||
00177 operation == asin_string ||
00178 operation == acos_string ||
00179 operation == atan_string ||
00180 operation == atan2_string ||
00181 operation == min_string ||
00182 operation == max_string ||
00183 operation == fraction_string ||
00184 operation == integer_string ||
00185 operation == mod_string ||
00186 operation == random_string ||
00187 operation == avg_string )
00188 {
00189 Parameters.push_back(new FGFunction(PropertyManager, element));
00190 } else if (operation != description_string) {
00191 cerr << "Bad operation " << operation << " detected in configuration file" << endl;
00192 }
00193 element = el->GetNextElement();
00194 }
00195
00196 bind();
00197
00198 Debug(0);
00199 }
00200
00201
00202
00203 FGFunction::~FGFunction(void)
00204 {
00205 for (unsigned int i=0; i<Parameters.size(); i++) delete Parameters[i];
00206 }
00207
00208
00209
00210 void FGFunction::cacheValue(bool cache)
00211 {
00212 cached = false;
00213
00214 if (cache) {
00215 cachedValue = GetValue();
00216 cached = true;
00217 }
00218 }
00219
00220
00221
00222 double FGFunction::GetValue(void) const
00223 {
00224 unsigned int i;
00225 double scratch;
00226
00227 if (cached) return cachedValue;
00228
00229 double temp = Parameters[0]->GetValue();
00230
00231 switch (Type) {
00232 case eTopLevel:
00233 break;
00234 case eProduct:
00235 for (i=1;i<Parameters.size();i++) {
00236 temp *= Parameters[i]->GetValue();
00237 }
00238 break;
00239 case eDifference:
00240 for (i=1;i<Parameters.size();i++) {
00241 temp -= Parameters[i]->GetValue();
00242 }
00243 break;
00244 case eSum:
00245 for (i=1;i<Parameters.size();i++) {
00246 temp += Parameters[i]->GetValue();
00247 }
00248 break;
00249 case eQuotient:
00250 temp /= Parameters[1]->GetValue();
00251 break;
00252 case ePow:
00253 temp = pow(temp,Parameters[1]->GetValue());
00254 break;
00255 case eExp:
00256 temp = exp(temp);
00257 break;
00258 case eAbs:
00259 temp = fabs(temp);
00260 break;
00261 case eSin:
00262 temp = sin(temp);
00263 break;
00264 case eCos:
00265 temp = cos(temp);
00266 break;
00267 case eTan:
00268 temp = tan(temp);
00269 break;
00270 case eACos:
00271 temp = acos(temp);
00272 break;
00273 case eASin:
00274 temp = asin(temp);
00275 break;
00276 case eATan:
00277 temp = atan(temp);
00278 break;
00279 case eATan2:
00280 temp = atan2(temp, Parameters[1]->GetValue());
00281 break;
00282 case eMod:
00283 temp = ((int)temp) % ((int) Parameters[1]->GetValue());
00284 break;
00285 case eMin:
00286 for (i=1;i<Parameters.size();i++) {
00287 if (Parameters[i]->GetValue() < temp) temp = Parameters[i]->GetValue();
00288 }
00289 break;
00290 case eMax:
00291 for (i=1;i<Parameters.size();i++) {
00292 if (Parameters[i]->GetValue() > temp) temp = Parameters[i]->GetValue();
00293 }
00294 break;
00295 case eAvg:
00296 for (i=1;i<Parameters.size();i++) {
00297 temp += Parameters[i]->GetValue();
00298 }
00299 temp /= Parameters.size();
00300 break;
00301 case eFrac:
00302 temp = modf(temp, &scratch);
00303 break;
00304 case eInteger:
00305 modf(temp, &scratch);
00306 temp = scratch;
00307 break;
00308 case eRandom:
00309 temp = GaussianRandomNumber();
00310 break;
00311 default:
00312 cerr << "Unknown function operation type" << endl;
00313 break;
00314 }
00315
00316 return temp;
00317 }
00318
00319
00320
00321 string FGFunction::GetValueAsString(void) const
00322 {
00323 char buffer[20];
00324 string value;
00325
00326 sprintf(buffer,"%9.6f",GetValue());
00327 value = string(buffer);
00328 return value;
00329 }
00330
00331
00332
00333 void FGFunction::bind(void)
00334 {
00335 if ( !Name.empty() ) {
00336 string tmp = PropertyManager->mkPropertyName(Prefix + Name, false);
00337 PropertyManager->Tie( tmp, this, &FGFunction::GetValue);
00338 }
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 void FGFunction::Debug(int from)
00361 {
00362 if (debug_lvl <= 0) return;
00363
00364 if (debug_lvl & 1) {
00365 if (from == 0) {
00366 if (Type == eTopLevel)
00367 cout << " Function: " << Name << endl;
00368 }
00369 }
00370 if (debug_lvl & 2 ) {
00371 if (from == 0) cout << "Instantiated: FGFunction" << endl;
00372 if (from == 1) cout << "Destroyed: FGFunction" << endl;
00373 }
00374 if (debug_lvl & 4 ) {
00375 }
00376 if (debug_lvl & 8 ) {
00377 }
00378 if (debug_lvl & 16) {
00379 }
00380 if (debug_lvl & 64) {
00381 if (from == 0) {
00382 cout << IdSrc << endl;
00383 cout << IdHdr << endl;
00384 }
00385 }
00386 }
00387
00388 }