PathSeries.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.6 $
00022 // $Date: 2005/03/11 22:08:00 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/domain/pattern/PathSeries.cpp,v $
00024                                                                         
00025                                                                         
00026 // File: ~/domain/pattern/PathSeries.C
00027 //
00028 // Written: fmk 
00029 // Created: 07/99
00030 // Revision: A
00031 //
00032 // Purpose: This file contains the class definition for PathSeries.
00033 // PathSeries is a concrete class. A PathSeries 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: "@(#) PathSeries.C, revA"
00038 
00039 
00040 #include <PathSeries.h>
00041 #include <Vector.h>
00042 #include <Channel.h>
00043 #include <math.h>
00044 
00045 #include <fstream>
00046 using std::ifstream;
00047 
00048 #include <iomanip>
00049 using std::ios;
00050 
00051 PathSeries::PathSeries()        
00052   :TimeSeries(TSERIES_TAG_PathSeries),
00053    thePath(0), pathTimeIncr(0.0), cFactor(0.0), otherDbTag(0), lastSendCommitTag(-1)
00054 {
00055   // does nothing
00056 }
00057 
00058                    
00059 PathSeries::PathSeries(const Vector &theLoadPath, 
00060                        double theTimeIncr, 
00061                        double theFactor)
00062   :TimeSeries(TSERIES_TAG_PathSeries),
00063    thePath(0), pathTimeIncr(theTimeIncr), cFactor(theFactor), otherDbTag(0), lastSendCommitTag(-1)
00064 {
00065   // create a copy of the vector containg path points
00066   thePath = new Vector(theLoadPath);
00067 
00068   // ensure we did not run out of memory
00069   if (thePath == 0 || thePath->Size() == 0) {
00070     opserr << "PathSeries::PathSeries() - ran out of memory constructing";
00071     opserr << " a Vector of size: " <<  theLoadPath.Size() << endln;
00072     if (thePath != 0)
00073       delete thePath; 
00074     thePath = 0;
00075   }
00076 }
00077 
00078 
00079 PathSeries::PathSeries(const char *fileName, 
00080                        double theTimeIncr, 
00081                        double theFactor)
00082   :TimeSeries(TSERIES_TAG_PathSeries),
00083    thePath(0), pathTimeIncr(theTimeIncr), cFactor(theFactor), otherDbTag(0), lastSendCommitTag(-1)
00084 {
00085   // determine the number of data points .. open file and count num entries
00086   int numDataPoints =0;
00087   double dataPoint;
00088 
00089   ifstream theFile;
00090   theFile.open(fileName);
00091 
00092   if (theFile.bad() || !theFile.is_open()) {
00093     opserr << "WARNING - PathSeries::PathSeries()";
00094     opserr << " - could not open file " << fileName << endln;
00095   } else {
00096     while (theFile >> dataPoint)
00097       numDataPoints++;
00098   }   
00099   theFile.close();
00100 
00101 
00102   // create a vector and read in the data
00103   if (numDataPoints != 0) {
00104 
00105     // first open the file
00106     ifstream theFile1;
00107     theFile1.open(fileName, ios::in);
00108     if (theFile1.bad() || !theFile1.is_open()) {
00109       opserr << "WARNING - PathSeries::PathSeries()";
00110       opserr << " - could not open file " << fileName << endln;
00111     } else {
00112 
00113       // now create the vector
00114       thePath = new Vector(numDataPoints);
00115 
00116       // ensure we did not run out of memory
00117       if (thePath == 0 || thePath->Size() == 0) {
00118         opserr << "PathSeries::PathSeries() - ran out of memory constructing";
00119         opserr << " a Vector of size: " << numDataPoints << endln;
00120 
00121         if (thePath != 0)
00122           delete thePath;
00123         thePath = 0;
00124       }
00125 
00126       // read the data from the file
00127       else {
00128         int count = 0;
00129         while (theFile1 >> dataPoint) {
00130           (*thePath)(count) = dataPoint;
00131           count++;
00132         }
00133       }
00134 
00135       // finally close the file
00136       theFile1.close();
00137     }
00138   }
00139 }
00140 
00141 
00142 PathSeries::~PathSeries()
00143 {
00144   if (thePath != 0)
00145     delete thePath;
00146 }
00147 
00148 double
00149 PathSeries::getFactor(double pseudoTime)
00150 {
00151   // check for a quick return
00152   if (pseudoTime < 0.0 || thePath == 0)
00153     return 0.0;
00154 
00155   // determine indexes into the data array whose boundary holds the time
00156   double incr = pseudoTime/pathTimeIncr; 
00157   int incr1 = floor(incr);
00158   int incr2 = incr1+1;
00159 
00160   if (incr2 >= thePath->Size())
00161     return 0.0;
00162   else {
00163     double value1 = (*thePath)[incr1];
00164     double value2 = (*thePath)[incr2];
00165     return cFactor*(value1 + (value2-value1)*(pseudoTime/pathTimeIncr - incr1));
00166   }
00167 }
00168 
00169 
00170 double
00171 PathSeries::getDuration()
00172 {
00173   if (thePath == 0)
00174   {
00175     opserr << "WARNING -- PathSeries::getDuration() on empty Vector" << endln;
00176         return 0.0;
00177   }
00178   return (thePath->Size() * pathTimeIncr);
00179 }
00180 
00181 double
00182 PathSeries::getPeakFactor()
00183 {
00184   if (thePath == 0)
00185   {
00186     opserr << "WARNING -- PathSeries::getPeakFactor() on empty Vector" << endln;
00187         return 0.0;
00188   }
00189 
00190   double peak = fabs((*thePath)[0]);
00191   int num = thePath->Size();
00192   double temp;
00193 
00194   for (int i = 1; i < num; i++)
00195   {
00196         temp = fabs((*thePath)[i]);
00197         if (temp > peak)
00198           peak = temp;
00199   }
00200   
00201   return (peak*cFactor);
00202 }
00203 
00204 int
00205 PathSeries::sendSelf(int commitTag, Channel &theChannel)
00206 {
00207   int dbTag = this->getDbTag();
00208 
00209   Vector data(5);
00210   data(0) = cFactor;
00211   data(1) = pathTimeIncr;
00212   data(2) = -1;
00213   
00214   if (thePath != 0) {
00215     int size = thePath->Size();
00216     data(2) = size;
00217     if (otherDbTag == 0)
00218       otherDbTag = theChannel.getDbTag();
00219     data(3) = otherDbTag;
00220   }
00221 
00222   if ((lastSendCommitTag == -1) && (theChannel.isDatastore() == 1)) {
00223     lastSendCommitTag = commitTag;
00224   }
00225 
00226   data(4) = lastSendCommitTag;
00227 
00228   int result = theChannel.sendVector(dbTag,commitTag, data);
00229   if (result < 0) {
00230     opserr << "PathSeries::sendSelf() - channel failed to send data\n";
00231     return result;
00232   }
00233 
00234   // we only send the vector data if this is the first time it is sent to the database
00235   // or the channel is for sending the data to a remote process
00236 
00237   if ((lastSendCommitTag == commitTag) || (theChannel.isDatastore() == 0)) {
00238     if (thePath != 0) {
00239       result = theChannel.sendVector(otherDbTag, commitTag, *thePath);
00240       if (result < 0) {
00241         opserr << "PathSeries::sendSelf() - ";
00242         opserr << "channel failed to send tha Path Vector\n";
00243         return result;  
00244       }
00245     }
00246   }
00247 
00248   return 0;
00249 }
00250 
00251 
00252 int 
00253 PathSeries::recvSelf(int commitTag, Channel &theChannel, 
00254                        FEM_ObjectBroker &theBroker)
00255 {
00256   int dbTag = this->getDbTag();
00257 
00258   Vector data(5);
00259   int result = theChannel.recvVector(dbTag,commitTag, data);
00260   if (result < 0) {
00261     opserr << "PathSeries::sendSelf() - channel failed to receive data\n";
00262     cFactor = 1.0;
00263     return result;
00264   }
00265 
00266   cFactor = data(0);
00267   pathTimeIncr = data(1);
00268   int size = data(2);
00269   otherDbTag = data(3);
00270   lastSendCommitTag = data(4);
00271   
00272   // get the path vector, only receive it once as it can't change
00273   if (thePath == 0 && size > 0) {
00274     thePath = new Vector(size);
00275     if (thePath == 0 || thePath->Size() == 0) {
00276       opserr << "PathSeries::recvSelf() - ran out of memory";
00277       opserr << " a Vector of size: " <<  size << endln;  
00278       if (thePath != 0)
00279         delete thePath;
00280       thePath = 0;
00281       return -1;
00282     }
00283 
00284     result = theChannel.recvVector(otherDbTag, lastSendCommitTag, *thePath);    
00285     if (result < 0) {
00286       opserr << "PathSeries::recvSelf() - ";
00287       opserr << "channel failed to receive tha Path Vector\n";
00288       return result;  
00289     }
00290   }
00291 
00292   return 0;    
00293 }
00294 
00295 
00296 
00297 void
00298 PathSeries::Print(OPS_Stream &s, int flag)
00299 {
00300     //s << "Path Time Series: constant factor: " << cFactor;
00301     //s << "  time Incr: " << pathTimeIncr << endln;
00302     if (flag == 1 && thePath != 0)
00303       //s << " specified path: " << *thePath;
00304           s << *thePath;
00305 }

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