PathTimeSeries.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.8 $
00022 // $Date: 2005/10/20 21:58:54 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/domain/pattern/PathTimeSeries.cpp,v $
00024                                                                         
00025                                                                         
00026 // Written: fmk 
00027 // Created: 07/99
00028 // Revision: A
00029 //
00030 // Purpose: This file contains the class definition for PathTimeSeries.
00031 // PathTimeSeries is a concrete class. A PathTimeSeries object provides
00032 // a linear time series. the factor is given by the pseudoTime and 
00033 // a constant factor provided in the constructor.
00034 //
00035 // What: "@(#) PathTimeSeries.C, revA"
00036 
00037 
00038 #include <PathTimeSeries.h>
00039 #include <Vector.h>
00040 #include <Channel.h>
00041 #include <math.h>
00042 
00043 #include <fstream>
00044 using std::ifstream;
00045 
00046 #include <iomanip>
00047 using std::ios;
00048 
00049 PathTimeSeries::PathTimeSeries()        
00050   :TimeSeries(TSERIES_TAG_PathTimeSeries),
00051    thePath(0), time(0), currentTimeLoc(0), 
00052    cFactor(0.0), dbTag1(0), dbTag2(0), lastSendCommitTag(-1)
00053 {
00054   // does nothing
00055 }
00056 
00057                    
00058 PathTimeSeries::PathTimeSeries(const Vector &theLoadPath, 
00059                                const Vector &theTimePath, 
00060                                double theFactor)
00061   :TimeSeries(TSERIES_TAG_PathTimeSeries),
00062    thePath(0), time(0), currentTimeLoc(0), 
00063    cFactor(theFactor), dbTag1(0), dbTag2(0), lastSendCommitTag(-1), lastChannel(0)
00064 {
00065   // check vectors are of same size
00066   if (theLoadPath.Size() != theTimePath.Size()) {
00067     opserr << "WARNING PathTimeSeries::PathTimeSeries() - vector containing data ";
00068     opserr << "points for path and time are not of the same size\n";
00069   } else {
00070 
00071     // create copies of the vectors
00072     thePath = new Vector(theLoadPath);
00073     time = new Vector(theTimePath);
00074 
00075     // ensure did not run out of memory creating copies
00076     if (thePath == 0 || thePath->Size() == 0 ||
00077         time == 0 || time->Size() == 0) {
00078       
00079       opserr << "WARNING PathTimeSeries::PathTimeSeries() - out of memory\n ";
00080       if (thePath != 0)
00081         delete thePath;
00082       if (time != 0)
00083         delete time;
00084       thePath = 0;
00085       time = 0;
00086     }
00087   }
00088 }
00089 
00090 
00091 PathTimeSeries::PathTimeSeries(const char *filePathName, 
00092                                const char *fileTimeName, 
00093                                double theFactor)
00094   :TimeSeries(TSERIES_TAG_PathTimeSeries),
00095    thePath(0), time(0), currentTimeLoc(0), 
00096    cFactor(theFactor), dbTag1(0), dbTag2(0), lastSendCommitTag(-1), lastChannel(0)
00097 {
00098 
00099   // determine the number of data points
00100   int numDataPoints1 =0;
00101   int numDataPoints2 =0;
00102   double dataPoint;
00103   ifstream theFile;
00104   
00105   // first open and go through file containg path
00106   theFile.open(filePathName, ios::in);
00107   if (theFile.bad() || !theFile.is_open()) {
00108     opserr << "WARNING - PathTimeSeries::PathTimeSeries()";
00109     opserr << " - could not open file " << filePathName << endln;
00110   } else {
00111     while (theFile >> dataPoint)
00112       numDataPoints1++;
00113   }   
00114   theFile.close();
00115 
00116   // now open and go through file containg time
00117   ifstream theFile1;
00118   theFile1.open(fileTimeName, ios::in);
00119   if (theFile1.bad() || !theFile1.is_open()) {
00120     opserr << "WARNING - PathTimeSeries::PathTimeSeries()";
00121     opserr << " - could not open file " << fileTimeName << endln;
00122   } else {
00123     while (theFile1 >> dataPoint)
00124       numDataPoints2++;
00125   }   
00126   theFile1.close();
00127 
00128   // check number of data entries in both are the same
00129   if (numDataPoints1 != numDataPoints2) {
00130     opserr << "WARNING PathTimeSeries::PathTimeSeries() - files containing data ";
00131     opserr << "points for path and time do not contain same number of points\n";
00132   } else {
00133 
00134 
00135     // create a vector and read in the data
00136     if (numDataPoints1 != 0) {
00137 
00138       // now create the two vector
00139       thePath = new Vector(numDataPoints1);
00140       time = new Vector(numDataPoints1);
00141 
00142       // ensure did not run out of memory creating copies
00143       if (thePath == 0 || thePath->Size() == 0 ||
00144           time == 0 || time->Size() == 0) {
00145           
00146         opserr << "WARNING PathTimeSeries::PathTimeSeries() - out of memory\n ";
00147         if (thePath != 0)
00148           delete thePath;
00149         if (time != 0)
00150           delete time;
00151         thePath = 0;
00152         time = 0;
00153       }
00154       
00155       // first open the path file and read in the data
00156       ifstream theFile2;
00157       theFile2.open(filePathName, ios::in);
00158       if (theFile2.bad() || !theFile2.is_open()) {
00159         opserr << "WARNING - PathTimeSeries::PathTimeSeries()";
00160         opserr << " - could not open file " << filePathName << endln;
00161         delete thePath;
00162         delete time;
00163         thePath = 0;
00164         time =0;
00165       } else { // read in the path data and then do the time
00166         int count = 0;
00167         while (theFile2 >> dataPoint) {
00168           (*thePath)(count) = dataPoint;
00169           count++;
00170         }
00171 
00172         // finally close the file
00173         theFile2.close();
00174 
00175         // now open the time file and read in the data
00176         ifstream theFile3;
00177         theFile3.open(fileTimeName, ios::in);
00178         if (theFile3.bad() || !theFile3.is_open()) {
00179           opserr << "WARNING - PathTimeSeries::PathTimeSeries()";
00180           opserr << " - could not open file " << fileTimeName << endln;
00181           delete thePath;
00182           delete time;
00183           thePath = 0;
00184           time =0;
00185         } else { // read in the data
00186           int count = 0;
00187           while (theFile3 >> dataPoint) {
00188             (*time)(count) = dataPoint;
00189             count++;
00190           }
00191           theFile3.close();
00192         } // read in the data 
00193       }   // read in the path data and then do the time
00194     }
00195   }
00196 }
00197 
00198 
00199 PathTimeSeries::PathTimeSeries(const char *fileName, 
00200                                double theFactor)
00201   :TimeSeries(TSERIES_TAG_PathTimeSeries),
00202    thePath(0), time(0), currentTimeLoc(0), 
00203    cFactor(theFactor), dbTag1(0), dbTag2(0), lastChannel(0)
00204 {
00205 
00206 
00207         // determine the number of data points
00208         int numDataPoints = 0;
00209         double dataPoint;
00210         ifstream theFile;
00211   
00212         // first open and go through file counting entries
00213         theFile.open(fileName, ios::in);
00214         if (theFile.bad() || !theFile.is_open()) {
00215           opserr << "WARNING - PathTimeSeries::PathTimeSeries()";
00216           opserr << " - could not open file " << fileName << endln;
00217         }
00218         else {
00219           while (theFile >> dataPoint) {
00220             numDataPoints++;
00221             theFile >> dataPoint;       // Read in second value of pair
00222           }
00223         }
00224         theFile.close();
00225 
00226 
00227 
00228         // create a vector and read in the data
00229         if (numDataPoints != 0) {
00230 
00231                 // now create the two vector
00232                 thePath = new Vector(numDataPoints);
00233                 time = new Vector(numDataPoints);
00234 
00235                 // ensure did not run out of memory creating copies
00236                 if (thePath == 0 || thePath->Size() == 0 || time == 0 || time->Size() == 0) {
00237           
00238                         opserr << "WARNING PathTimeSeries::PathTimeSeries() - out of memory\n ";
00239                         if (thePath != 0)
00240                                 delete thePath;
00241                         if (time != 0)
00242                                 delete time;
00243                         thePath = 0;
00244                         time = 0;
00245                 }
00246       
00247                 // first open the file and read in the data
00248                 ifstream theFile1;
00249                 theFile1.open(fileName, ios::in);
00250                 if (theFile1.bad() || !theFile1.is_open()) {
00251 
00252                         opserr << "WARNING - PathTimeSeries::PathTimeSeries()";
00253                         opserr << " - could not open file " << fileName << endln;
00254                         delete thePath;
00255                         delete time;
00256                         thePath = 0;
00257                         time =0;
00258                 }
00259                 else { // read in the time and then read the value
00260                         int count = 0;
00261                         while (theFile1 >> dataPoint) {
00262                                 (*time)(count) = dataPoint;
00263                                 theFile1 >> dataPoint;
00264                                 (*thePath)(count) = dataPoint;
00265                                 count++;
00266                         }
00267 
00268                         // finally close the file
00269                         theFile1.close();
00270                 } 
00271         
00272         }
00273     
00274 }
00275 
00276 PathTimeSeries::~PathTimeSeries()
00277 {
00278   if (thePath != 0)
00279     delete thePath;
00280   if (time != 0)
00281     delete time;
00282 }
00283 
00284 double
00285 PathTimeSeries::getTimeIncr (double pseudoTime)
00286 {
00287   // NEED TO FILL IN, FOR NOW return 1.0
00288   return 1.0;
00289 }
00290 
00291 double
00292 PathTimeSeries::getFactor(double pseudoTime)
00293 {
00294   // check for a quick return
00295   if (thePath == 0)
00296     return 0.0;
00297 
00298   // determine indexes into the data array whose boundary holds the time
00299   double time1 = (*time)(currentTimeLoc);
00300 
00301   // check for another quick return
00302   if (pseudoTime == time1)
00303     return cFactor * (*thePath)[currentTimeLoc];
00304 
00305   int size = time->Size();
00306   int sizem1 = size - 1;
00307   int sizem2 = size - 2;
00308   
00309   // check we are not at the end
00310   if (pseudoTime > time1 && currentTimeLoc == sizem1)
00311     return 0.0;
00312 
00313   if (pseudoTime < time1 && currentTimeLoc == 0)
00314     return 0.0;
00315 
00316   // otherwise go find the current interval
00317   double time2 = (*time)(currentTimeLoc+1);
00318   if (pseudoTime > time2) {
00319     while ((pseudoTime > time2) && (currentTimeLoc < sizem2)) {
00320       currentTimeLoc++;
00321       time1 = time2;
00322       time2 = (*time)(currentTimeLoc+1);
00323     }
00324     // if pseudo time greater than ending time reurn 0
00325     if (pseudoTime > time2)
00326       return 0.0;
00327 
00328   } else if (pseudoTime < time1) {
00329     while ((pseudoTime < time1) && (currentTimeLoc > 0)) {
00330       currentTimeLoc--;
00331       time2 = time1;    
00332       time1 = (*time)(currentTimeLoc);
00333     }
00334     // if starting time less than initial starting time return 0
00335     if (pseudoTime < time1)
00336       return 0.0;
00337   }
00338 
00339   double value1 = (*thePath)[currentTimeLoc];
00340   double value2 = (*thePath)[currentTimeLoc+1];
00341   return cFactor*(value1 + (value2-value1)*(pseudoTime-time1)/(time2 - time1));
00342 }
00343 
00344 double
00345 PathTimeSeries::getDuration()
00346 {
00347   if (thePath == 0)
00348   {
00349     opserr << "WARNING -- PathTimeSeries::getDuration() on empty Vector" << endln;
00350         return 0.0;
00351   }
00352 
00353   int lastIndex = time->Size(); // index to last entry in time vector
00354   return ((*time)[lastIndex-1]);
00355 }
00356 
00357 double
00358 PathTimeSeries::getPeakFactor()
00359 {
00360   if (thePath == 0)
00361   {
00362     opserr << "WARNING -- PathTimeSeries::getPeakFactor() on empty Vector" << endln;
00363         return 0.0;
00364   }
00365 
00366   double peak = fabs((*thePath)[0]);
00367   int num = thePath->Size();
00368   double temp;
00369 
00370   for (int i = 1; i < num; i++)
00371   {
00372         temp = fabs((*thePath)[i]);
00373         if (temp > peak)
00374           peak = temp;
00375   }
00376   
00377   return (peak*cFactor);
00378 }
00379 
00380 int
00381 PathTimeSeries::sendSelf(int commitTag, Channel &theChannel)
00382 {
00383   int dbTag = this->getDbTag();
00384   Vector data(5);
00385   data(0) = cFactor;
00386   data(1) = -1;
00387   
00388   if (thePath != 0) {
00389     int size = thePath->Size();
00390     data(1) = size;
00391     if (dbTag1 == 0) {
00392       dbTag1 = theChannel.getDbTag();
00393       dbTag2 = theChannel.getDbTag();
00394     }
00395     data(2) = dbTag1;
00396     data(3) = dbTag2;
00397   }
00398 
00399   if ((lastSendCommitTag == -1) && (theChannel.isDatastore() == 1)) {
00400     lastSendCommitTag = commitTag;
00401   }
00402 
00403   data(4) = lastSendCommitTag;
00404   
00405   int result = theChannel.sendVector(dbTag,commitTag, data);
00406   if (result < 0) {
00407     opserr << "PathTimeSeries::sendSelf() - channel failed to send data\n";
00408     return result;
00409   }
00410 
00411   // we only send the vector data if this is the first time it is sent to the database
00412   // or the channel is for sending the data to a remote process
00413 
00414   if (lastChannel != &theChannel || (lastSendCommitTag == commitTag) || (theChannel.isDatastore() == 0)) {  
00415 
00416     lastChannel = &theChannel;
00417 
00418     if (thePath != 0) {
00419       result = theChannel.sendVector(dbTag1, commitTag, *thePath);
00420       if (result < 0) {
00421         opserr << "PathTimeSeries::sendSelf() - ";
00422         opserr << "channel failed to send tha Path Vector\n";
00423         return result;  
00424       }
00425     }
00426     if (time != 0) {
00427       result = theChannel.sendVector(dbTag2, commitTag, *time);
00428       if (result < 0) {
00429         opserr << "PathTimeSeries::sendSelf() - ";
00430         opserr << "channel failed to send tha Path Vector\n";
00431         return result;  
00432       }
00433     }
00434     return 0;
00435   }
00436   return 0;
00437 }
00438 
00439 
00440 int 
00441 PathTimeSeries::recvSelf(int commitTag, Channel &theChannel, 
00442                        FEM_ObjectBroker &theBroker)
00443 {
00444   int dbTag = this->getDbTag();
00445   Vector data(5);
00446   int result = theChannel.recvVector(dbTag,commitTag, data);
00447   if (result < 0) {
00448     opserr << "PathTimeSeries::sendSelf() - channel failed to receive data\n";
00449     cFactor = 1.0;
00450     return result;
00451   }
00452   cFactor = data(0);
00453   int size = data(1);
00454   lastSendCommitTag = data(4);
00455 
00456   // get the data cvector, only receive them once as they cannot change
00457   if (thePath == 0 && size > 0) {
00458     dbTag1 = data(2);
00459     dbTag2 = data(3);
00460     thePath = new Vector(size);
00461     time = new Vector(size);
00462     if (thePath == 0 || time == 0 ||
00463         thePath->Size() == 0 || time->Size() == 0) {
00464 
00465       opserr << "PathTimeSeries::recvSelf() - ran out of memory";
00466       opserr << " a Vector of size: " <<  size << endln;  
00467       if (thePath != 0)
00468         delete thePath;
00469       if (time != 0)
00470         delete time;
00471       thePath = 0;
00472       time = 0;
00473       return -1;
00474     }
00475     result = theChannel.recvVector(dbTag1, lastSendCommitTag, *thePath);    
00476     if (result < 0) {
00477       opserr << "PathTimeSeries::recvSelf() - ";
00478       opserr << "channel failed to receive tha Path Vector\n";
00479       return result;  
00480     }
00481     result = theChannel.recvVector(dbTag2, lastSendCommitTag, *time);    
00482     if (result < 0) {
00483       opserr << "PathTimeSeries::recvSelf() - ";
00484       opserr << "channel failed to receive tha time Vector\n";
00485       return result;  
00486     }
00487   }
00488   return 0;    
00489 }
00490 
00491 
00492 
00493 void
00494 PathTimeSeries::Print(OPS_Stream &s, int flag)
00495 {
00496     s << "Path Time Series: constant factor: " << cFactor;
00497     if (flag == 1 && thePath != 0) {
00498       s << " specified path: " << *thePath;
00499       s << " specified time: " << *time;
00500     }
00501 }

Generated on Mon Oct 23 15:05:02 2006 for OpenSees by doxygen 1.5.0