Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

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