1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGCoefficient.cpp
6 Purpose: Encapsulates the stability derivative class FGCoefficient;
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class models the stability derivative coefficient lookup tables or
31 equations. Note that the coefficients need not be calculated each delta-t.
33 Note that the values in a row which index into the table must be the same value
34 for each column of data, so the first column of numbers for each altitude are
35 seen to be equal, and there are the same number of values for each altitude.
37 See the header file FGCoefficient.h for the values of the identifiers.
40 --------------------------------------------------------------------------------
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53 #include "FGCoefficient.h"
55 #include "FGFDMExec.h"
56 #include "FGPropertyManager.h"
59 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
70 static const char *IdSrc = "$Id$";
71 static const char *IdHdr = ID_COEFFICIENT;
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
77 FGCoefficient::FGCoefficient( FGFDMExec* fdex )
80 State = FDMExec->GetState();
84 PropertyManager = FDMExec->GetPropertyManager();
87 LookupR = LookupC = 0;
89 rows = columns = tables = 0;
102 multparmsRow.erase();
103 multparmsCol.erase();
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 FGCoefficient::~FGCoefficient()
112 if (Table) delete Table;
116 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 bool FGCoefficient::Load(FGConfigFile *AC_cfg)
124 name = AC_cfg->GetValue("NAME");
125 method = AC_cfg->GetValue("TYPE");
126 AC_cfg->GetNextConfigLine();
127 *AC_cfg >> description;
128 if (method == "EQUATION") type = EQUATION;
129 else if (method == "TABLE") type = TABLE;
130 else if (method == "TABLE3D") type = TABLE3D;
131 else if (method == "VECTOR") type = VECTOR;
132 else if (method == "VALUE") type = VALUE;
135 if (type == VECTOR || type == TABLE || type == TABLE3D) {
137 if (type == TABLE3D) {
138 *AC_cfg >> rows >> columns >> tables;
139 Table = new FGTable(rows, columns, tables);
140 *AC_cfg >> multparmsRow >> multparmsCol >> multparmsTable;
141 LookupR = PropertyManager->GetNode( multparmsRow );
142 LookupC = PropertyManager->GetNode( multparmsCol );
143 LookupT = PropertyManager->GetNode( multparmsTable );
144 } else if (type == TABLE) {
145 *AC_cfg >> rows >> columns;
146 Table = new FGTable(rows, columns);
147 *AC_cfg >> multparmsRow >> multparmsCol;
148 LookupR = PropertyManager->GetNode( multparmsRow );
149 LookupC = PropertyManager->GetNode( multparmsCol );
152 Table = new FGTable(rows);
153 *AC_cfg >> multparmsRow;
154 LookupR = PropertyManager->GetNode( multparmsRow );
158 // Here, read in the line of the form:
159 // {property1} | {property2} | {property3}
160 // where each non-dimensionalizing property for this coefficient is
161 // separated by a | character
163 string line=AC_cfg->GetCurrentLine();
166 for(unsigned i=0;i<line.length(); i++ ) {
167 if( !isspace(line[i]) ) {
172 tmp[j]='\0'; multparms=tmp;
173 end = multparms.length();
175 n = multparms.find("|");
176 if (n == string::npos) n = end;
178 if (multparms != string("none")) {
179 while (n < end && n != string::npos) {
181 mult = multparms.substr(start,n);
182 multipliers.push_back( resolveSymbol( mult ) );
184 n = multparms.find("|",start);
186 mult = multparms.substr(start,n);
187 multipliers.push_back( resolveSymbol( mult ) );
188 // End of non-dimensionalizing parameter read-in
190 AC_cfg->GetNextConfigLine();
193 *AC_cfg >> StaticValue;
194 } else if (type == VECTOR || type == TABLE || type == TABLE3D) {
197 cerr << "Unimplemented coefficient type: " << type << endl;
200 AC_cfg->GetNextConfigLine();
201 FGCoefficient::Debug(2);
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 double FGCoefficient::Value(double rVal, double cVal, double tVal)
218 SD = Value = gain*Table->GetValue(rVal, cVal, tVal) + bias;
220 for (midx=0; midx < multipliers.size(); midx++) {
221 Value *= multipliers[midx]->getDoubleValue();
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 double FGCoefficient::Value(double rVal, double cVal)
233 SD = Value = gain*Table->GetValue(rVal, cVal) + bias;
235 for (midx=0; midx < multipliers.size(); midx++) {
236 Value *= multipliers[midx]->getDoubleValue();
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 double FGCoefficient::Value(double Val)
247 SD = Value = gain*Table->GetValue(Val) + bias;
249 for (unsigned int midx=0; midx < multipliers.size(); midx++)
250 Value *= multipliers[midx]->getDoubleValue();
255 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 double FGCoefficient::Value(void)
261 SD = Value = gain*StaticValue + bias;
263 for (unsigned int midx=0; midx < multipliers.size(); midx++)
264 Value *= multipliers[midx]->getDoubleValue();
269 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 double FGCoefficient::TotalValue(void)
280 totalValue = Value();
284 totalValue = Value( LookupR->getDoubleValue() );
288 totalValue = Value( LookupR->getDoubleValue(),
289 LookupC->getDoubleValue() );
293 totalValue = Value( LookupR->getDoubleValue(),
294 LookupC->getDoubleValue(),
295 LookupT->getDoubleValue() );
305 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 void FGCoefficient::DisplayCoeffFactors(void)
311 cout << " Non-Dimensionalized by: ";
313 if (multipliers.size() == 0) {
314 cout << "none" << endl;
316 for (i=0; i<multipliers.size(); i++)
317 cout << multipliers[i]->getName() << " ";
322 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 string FGCoefficient::GetSDstring(void)
329 sprintf(buffer,"%9.6f",SD);
330 value = string(buffer);
334 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 void FGCoefficient::bind(FGPropertyManager *parent)
341 node = parent->GetNode(name,true);
343 node->SetString("description",description);
344 if (LookupR) node->SetString("row-parm",LookupR->getName() );
345 if (LookupC) node->SetString("column-parm",LookupC->getName() );
348 if (multipliers.size() == 0)
351 for (i=0; i<multipliers.size(); i++) {
352 mult += multipliers[i]->getName();
353 if ( i < multipliers.size()-1 ) mult += " ";
355 node->SetString("multipliers",mult);
357 node->Tie("SD-norm",this,&FGCoefficient::GetSD );
358 node->Tie("value-lbs",this,&FGCoefficient::GetValue );
360 node->Tie("bias", this, &FGCoefficient::getBias,
361 &FGCoefficient::setBias );
363 node->Tie("gain", this, &FGCoefficient::getGain,
364 &FGCoefficient::setGain );
368 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
370 void FGCoefficient::unbind(void)
372 node->Untie("SD-norm");
373 node->Untie("value-lbs");
378 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 FGPropertyManager* FGCoefficient::resolveSymbol(string name)
382 FGPropertyManager* tmpn;
384 tmpn = PropertyManager->GetNode(name,false);
386 cerr << "Coefficient multipliers cannot create properties, check spelling?" << endl;
392 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 void FGCoefficient::convert(string prop)
397 cout << " <function name=\"aero/function/" << name << "\">" << endl;
399 cout << " <function name=\"aero/coefficient/" << name << "\">" << endl;
401 cout << " <description>" << description << "</description>" << endl;
402 cout << " <product>" << endl;
404 for (int i=0; i<multipliers.size(); i++)
405 cout << " <property>" << (multipliers[i]->GetFullyQualifiedName()).substr(12) << "</property>" << endl;
408 cout << " <property>aero/function/" << prop << "</property>" << endl;
412 cout << " <value>" << StaticValue << "</value>" << endl;
416 cout << " <table>" << endl;
417 cout << " <independentVar>" << (LookupR->GetFullyQualifiedName()).substr(12) << "</independentVar>" << endl;
418 cout << " <tableData>" << endl;
420 cout << " </tableData>" << endl;
421 cout << " </table>" << endl;
425 cout << " <table>" << endl;
426 cout << " <independentVar lookup=\"row\">" << (LookupR->GetFullyQualifiedName()).substr(12) << "</independentVar>" << endl;
427 cout << " <independentVar lookup=\"column\">" << (LookupC->GetFullyQualifiedName()).substr(12) << "</independentVar>" << endl;
428 cout << " <tableData>" << endl;
430 cout << " </tableData>" << endl;
431 cout << " </table>" << endl;
435 cout << " <table>" << endl;
436 cout << " <independentVar lookup=\"row\">" << (LookupR->GetFullyQualifiedName()).substr(12) << "</independentVar>" << endl;
437 cout << " <independentVar lookup=\"column\">" << (LookupC->GetFullyQualifiedName()).substr(12) << "</independentVar>" << endl;
438 cout << " <independentVar lookup=\"table\">" << (LookupT->GetFullyQualifiedName()).substr(12) << "</independentVar>" << endl;
439 cout << " <tableData>" << endl;
441 cout << " </tableData>" << endl;
442 cout << " </table>" << endl;
447 cout << " </product>" << endl;
448 cout << " </function>" << endl;
451 cout << " === MOVE THE ABOVE FACTOR " << name << " OUTSIDE OF AND BEFORE ANY <AXIS> DEFINITION ===" << endl;
452 for (int i=0; i<sum.size(); i++) {
453 sum[i]->convert(name);
458 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 // The bitmasked value choices are as follows:
460 // unset: In this case (the default) JSBSim would only print
461 // out the normally expected messages, essentially echoing
462 // the config files as they are read. If the environment
463 // variable is not set, debug_lvl is set to 1 internally
464 // 0: This requests JSBSim not to output any messages
466 // 1: This value explicity requests the normal JSBSim
468 // 2: This value asks for a message to be printed out when
469 // a class is instantiated
470 // 4: When this value is set, a message is displayed when a
471 // FGModel object executes its Run() method
472 // 8: When this value is set, various runtime state variables
473 // are printed out periodically
474 // 16: When set various parameters are sanity checked and
475 // a message is printed out when they go out of bounds
477 void FGCoefficient::Debug(int from)
479 if (debug_lvl <= 0) return;
481 if (debug_lvl & 1) { // Standard console startup message output
483 if (from == 2) { // Loading
484 cout << "\n " << highint << underon << name << underoff << normint << endl;
485 cout << " " << description << endl;
486 cout << " " << method << endl;
488 if (type == VECTOR || type == TABLE || type == TABLE3D) {
489 cout << " Rows: " << rows << " indexed by: " << LookupR->getName() << endl;
490 if (type == TABLE || type == TABLE3D) {
491 cout << " Cols: " << columns << " indexed by: " << LookupC->getName() << endl;
492 if (type == TABLE3D) {
493 cout << " Tables: " << tables << " indexed by: " << LookupT->getName() << endl;
497 } else if (type == VALUE) {
498 cout << " Value = " << StaticValue << endl;
501 DisplayCoeffFactors();
504 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
505 if (from == 0) cout << "Instantiated: FGCoefficient" << endl;
506 if (from == 1) cout << "Destroyed: FGCoefficient" << endl;
508 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
510 if (debug_lvl & 8 ) { // Runtime state variables
512 if (debug_lvl & 16) { // Sanity checking
514 if (debug_lvl & 64) {
515 if (from == 0) { // Constructor
516 cout << IdSrc << endl;
517 cout << IdHdr << endl;
522 } // namespace JSBSim