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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 #include "FGCoefficient.h"
51 #include "FGFDMExec.h"
52 #include "FGPropertyManager.h"
55 # if defined(sgi) && !defined(__GNUC__)
64 static const char *IdSrc = "$Id$";
65 static const char *IdHdr = ID_COEFFICIENT;
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 FGCoefficient::FGCoefficient( FGFDMExec* fdex )
74 State = FDMExec->GetState();
77 PropertyManager = FDMExec->GetPropertyManager();
80 LookupR = LookupC = 0;
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 FGCoefficient::~FGCoefficient()
105 if (Table) delete Table;
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 bool FGCoefficient::Load(FGConfigFile *AC_cfg)
117 name = AC_cfg->GetValue("NAME");
118 method = AC_cfg->GetValue("TYPE");
119 AC_cfg->GetNextConfigLine();
120 *AC_cfg >> description;
121 if (method == "EQUATION") type = EQUATION;
122 else if (method == "TABLE") type = TABLE;
123 else if (method == "VECTOR") type = VECTOR;
124 else if (method == "VALUE") type = VALUE;
127 if (type == VECTOR || type == TABLE) {
131 Table = new FGTable(rows, columns);
133 Table = new FGTable(rows);
136 *AC_cfg >> multparmsRow;
137 LookupR = PropertyManager->GetNode( multparmsRow );
141 *AC_cfg >> multparmsCol;
143 LookupC = PropertyManager->GetNode( multparmsCol );
146 // Here, read in the line of the form (e.g.) FG_MACH|FG_QBAR|FG_ALPHA
147 // where each non-dimensionalizing parameter for this coefficient is
148 // separated by a | character
150 string line=AC_cfg->GetCurrentLine();
153 for(unsigned i=0;i<line.length(); i++ ) {
154 if( !isspace(line[i]) ) {
159 tmp[j]='\0'; multparms=tmp;
160 end = multparms.length();
162 n = multparms.find("|");
164 if (multparms != string("none")) {
165 while (n < end && n >= 0) {
167 mult = multparms.substr(start,n);
168 multipliers.push_back( resolveSymbol( mult ) );
170 n = multparms.find("|",start);
172 mult = multparms.substr(start,n);
173 multipliers.push_back( resolveSymbol( mult ) );
174 // End of non-dimensionalizing parameter read-in
176 AC_cfg->GetNextConfigLine();
178 *AC_cfg >> StaticValue;
179 } else if (type == VECTOR || type == TABLE) {
182 cerr << "Unimplemented coefficient type: " << type << endl;
185 AC_cfg->GetNextConfigLine();
186 FGCoefficient::Debug(2);
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 double FGCoefficient::Value(double rVal, double cVal)
203 SD = Value = gain*Table->GetValue(rVal, cVal) + bias;
205 for (midx=0; midx < multipliers.size(); midx++) {
206 Value *= multipliers[midx]->getDoubleValue();
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 double FGCoefficient::Value(double Val)
217 SD = Value = gain*Table->GetValue(Val) + bias;
219 for (unsigned int midx=0; midx < multipliers.size(); midx++)
220 Value *= multipliers[midx]->getDoubleValue();
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 double FGCoefficient::Value(void)
231 SD = Value = gain*StaticValue + bias;
233 for (unsigned int midx=0; midx < multipliers.size(); midx++)
234 Value *= multipliers[midx]->getDoubleValue();
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 double FGCoefficient::TotalValue(void)
250 totalValue = Value();
254 totalValue = Value( LookupR->getDoubleValue() );
258 totalValue = Value( LookupR->getDoubleValue(),
259 LookupC->getDoubleValue() );
269 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 void FGCoefficient::DisplayCoeffFactors(void)
275 cout << " Non-Dimensionalized by: ";
277 if (multipliers.size() == 0) {
278 cout << "none" << endl;
280 for (i=0; i<multipliers.size(); i++)
281 cout << multipliers[i]->getName() << " ";
286 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 string FGCoefficient::GetSDstring(void)
293 sprintf(buffer,"%9.6f",SD);
294 value = string(buffer);
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 void FGCoefficient::bind(FGPropertyManager *parent)
305 node=parent->GetNode(name,true);
307 node->SetString("description",description);
308 if (LookupR) node->SetString("row-parm",LookupR->getName() );
309 if (LookupC) node->SetString("column-parm",LookupC->getName() );
312 if (multipliers.size() == 0)
315 for (i=0; i<multipliers.size(); i++) {
316 mult += multipliers[i]->getName();
317 if ( i < multipliers.size()-1 ) mult += " ";
319 node->SetString("multipliers",mult);
321 node->Tie("SD-norm",this,&FGCoefficient::GetSD );
322 node->Tie("value-lbs",this,&FGCoefficient::GetValue );
324 node->Tie("bias", this, &FGCoefficient::getBias,
325 &FGCoefficient::setBias );
327 node->Tie("gain", this, &FGCoefficient::getGain,
328 &FGCoefficient::setGain );
332 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 void FGCoefficient::unbind(void)
336 node->Untie("SD-norm");
337 node->Untie("value-lbs");
342 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 FGPropertyManager* FGCoefficient::resolveSymbol(string name){
345 FGPropertyManager* tmpn;
346 tmpn = PropertyManager->GetNode(name,false);
348 cerr << "Coefficient multipliers cannot create properties, check spelling?" << endl;
354 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355 // The bitmasked value choices are as follows:
356 // unset: In this case (the default) JSBSim would only print
357 // out the normally expected messages, essentially echoing
358 // the config files as they are read. If the environment
359 // variable is not set, debug_lvl is set to 1 internally
360 // 0: This requests JSBSim not to output any messages
362 // 1: This value explicity requests the normal JSBSim
364 // 2: This value asks for a message to be printed out when
365 // a class is instantiated
366 // 4: When this value is set, a message is displayed when a
367 // FGModel object executes its Run() method
368 // 8: When this value is set, various runtime state variables
369 // are printed out periodically
370 // 16: When set various parameters are sanity checked and
371 // a message is printed out when they go out of bounds
373 void FGCoefficient::Debug(int from)
375 if (debug_lvl <= 0) return;
377 if (debug_lvl & 1) { // Standard console startup message output
379 if (from == 2) { // Loading
380 cout << "\n " << highint << underon << name << underoff << normint << endl;
381 cout << " " << description << endl;
382 cout << " " << method << endl;
384 if (type == VECTOR || type == TABLE) {
385 cout << " Rows: " << rows << " ";
387 cout << "Cols: " << columns;
389 cout << endl << " Row indexing parameter: " << LookupR->getName() << endl;
393 cout << " Column indexing parameter: " << LookupC->getName() << endl;
397 cout << " Value = " << StaticValue << endl;
398 } else if (type == VECTOR || type == TABLE) {
402 DisplayCoeffFactors();
405 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
406 if (from == 0) cout << "Instantiated: FGCoefficient" << endl;
407 if (from == 1) cout << "Destroyed: FGCoefficient" << endl;
409 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
411 if (debug_lvl & 8 ) { // Runtime state variables
413 if (debug_lvl & 16) { // Sanity checking
415 if (debug_lvl & 64) {
416 if (from == 0) { // Constructor
417 cout << IdSrc << endl;
418 cout << IdHdr << endl;