/*flapdata.cpp Implements the flapping data class Written by Theresa Robinson robinst@ecf.toronto.edu */ //#ifndef flapdata_cpp //#define flapdata_cpp #include "uiuc_flapdata.h" //#include #include /////////////////////////////////////////////////////////// //Implementation of FlapStruct public methods ////////////////////////////////////////////////////////// flapStruct::flapStruct(){ Lift=0; Thrust=0; Inertia=0; Moment=0; } flapStruct::flapStruct(const flapStruct &rhs){ Lift=rhs.getLift(); Thrust=rhs.getThrust(); Inertia=rhs.getInertia(); Moment=rhs.getMoment(); } flapStruct::flapStruct(double newLift, double newThrust, double newMoment, double newInertia){ Lift=newLift; Thrust=newThrust; Inertia=newInertia; Moment=newMoment; } double flapStruct::getLift() const{ return Lift; } double flapStruct::getThrust() const{ return Thrust; } double flapStruct::getInertia() const{ return Inertia; } double flapStruct::getMoment() const{ return Moment; } ///////////////////////////////////////////////////////////////// //Implementation of FlapData public methods //////////////////////////////////////////////////////////////// FlapData::FlapData(){ liftTable=NULL; thrustTable=NULL; momentTable=NULL; inertiaTable=NULL; alphaArray=NULL; speedArray=NULL; freqArray=NULL; phiArray=NULL; lastAlphaIndex=0; lastSpeedIndex=0; lastPhiIndex=0; lastFreqIndex=0; } //A constructor that takes a file name: //Opens that file and fills all the arrays from it //sets the guesses to zero for the speed and halfway //along the array for the alpha and frequency //All it does is call init FlapData::FlapData(const char* filename){ // printf("init flapdata\n"); init(filename); lastAlphaIndex=0; lastSpeedIndex=0; lastPhiIndex=0; lastFreqIndex=0; } //The destructor: //Frees all memory associated with this object FlapData::~FlapData(){ // printf("deleting flapdata\n"); delete liftTable; delete thrustTable; delete momentTable; delete inertiaTable; delete alphaArray; delete speedArray; delete freqArray; delete phiArray; } //An initialization function that does the same thing //as the second constructor //returns zero if it was successful int FlapData::init(const char* filename){ ifstream* f=new ifstream(filename); //open file for reading in text (ascii) mode if (f==NULL) { //file open error return(1); } if(readIn(f)){ //read the file, if there's a problem return(2); } delete f; return 0; //close the file, return the success of the file close } //A function that returns the interpolated values //for all four associated numbers //given the angle of attack, speed, and flapping frequency flapStruct FlapData::flapper(double alpha, double speed, double freq, double phi){ flapStruct results; int i,j,k,l; double lift,thrust,moment,inertia; if(speedspeedArray[speedLength-1]){ speed=speedArray[speedLength-1]; } if(alphaalphaArray[alphaLength-1]){ alpha=alphaArray[alphaLength-1]; } i=findIndex(alphaArray,alphaLength,alpha,lastAlphaIndex); j=findIndex(speedArray,speedLength,speed,lastSpeedIndex); k=findIndex(freqArray,freqLength,freq,lastFreqIndex); l=findIndex(phiArray,phiLength,phi,lastPhiIndex); lift=interpolate(liftTable, i, j, k, l, alpha, speed, freq, phi); thrust=interpolate(thrustTable, i, j, k, l, alpha, speed, freq, phi); moment=interpolate(momentTable, i, j, k, l, alpha, speed, freq, phi); inertia=interpolate(inertiaTable, i, j, k, l, alpha, speed, freq, phi); results=flapStruct(lift,thrust,moment,inertia); return results; } ////////////////////////////////////////////////////////////////// //Implementation of private FlapData methods ////////////////////////////////////////////////////////////////// //A function that returns an index i such that // array[i] < value < array[i+1] //The function returns -1 if // (value < array[0]) OR (value > array[n-1]) //(i.e. the value is not within the bounds of the array) //It performs a linear search starting at guess int FlapData::findIndex(double array[], double n, double value, int i){ while(valuearray[i+1]){ //more than the higher end of interval i if(i==n-1){ //if we're at the end of the array return(-1); //there's a problem } i++; //otherwise move to the next higher interval } // errmsg("In findIndex: array[" << i << "]= " << array[i] << "<=" << value << "<= array[" << (i+1) << "]=" << array[i+1]); return(i); } //A function that performs a linear interpolation based on the //eight points surrounding the value required double FlapData::interpolate(double**** table, int i, int j, int k, int l, double alpha, double speed, double freq, double phi){ // errmsg("\t\t\t\t\t\t\t\tGetting Values"); double f0000=table[i][j][k][l]; double f0001=table[i][j][k][l+1]; double f0010=table[i][j][k+1][l]; double f0011=table[i][j][k+1][l+1]; double f0100=table[i][j+1][k][l]; double f0101=table[i][j+1][k][l+1]; double f0110=table[i][j+1][k+1][l]; double f0111=table[i][j+1][k+1][l+1]; double f1000=table[i+1][j][k][l]; double f1001=table[i+1][j][k][l+1]; double f1010=table[i+1][j][k+1][l]; double f1011=table[i+1][j][k+1][l+1]; double f1100=table[i+1][j+1][k][l]; double f1101=table[i+1][j+1][k][l+1]; double f1110=table[i+1][j+1][k+1][l]; double f1111=table[i+1][j+1][k+1][l+1]; // errmsg("\t\t\t\t\t\t\t\t1st pass (3)"); // errmsg("phi[" << l << "]=" << phiArray[l] << "; phi[" << (l+1) <<"]=" << phiArray[l+1]); // errmsg("Finding " << phi <getline(numstr,200); sscanf(numstr,"%d,%d,%d,%d",&alphaLength,&speedLength,&freqLength,&phiLength); //Check to see if the first line is 0 0 0 0 //If so, tell user to download data file //Quits FlightGear if (alphaLength==0 && speedLength==0 && freqLength==0 && phiLength==0) uiuc_warnings_errors(7,""); alphaArray=new double[alphaLength]; speedArray=new double[speedLength]; freqArray=new double[freqLength]; phiArray=new double[phiLength]; for(i=0;iget(numstr,20,','); sscanf(numstr,"%lf",&alphaArray[i]); f->get(); } f->get(); for(i=0;iget(numstr,20,','); sscanf(numstr,"%lf",&speedArray[i]); f->get(); } f->get(); for(i=0;iget(numstr,20,','); sscanf(numstr,"%lf",&freqArray[i]); f->get(); } f->get(); for(i=0;iget(numstr,20,','); sscanf(numstr,"%lf",&phiArray[i]); f->get(); } f->get(); liftTable=new double***[alphaLength]; thrustTable=new double***[alphaLength]; momentTable=new double***[alphaLength]; inertiaTable=new double***[alphaLength]; for(i=0;igetline(numstr,200); sscanf(numstr,"%lf %lf %lf %lf",&liftTable[i][j][k][l],&thrustTable[i][j][k][l],&momentTable[i][j][k][l],&inertiaTable[i][j][k][l]); } } } } return 0; }; //#endif