NEESData.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.5 $
00022 // $Date: 2006/01/10 18:16:20 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/database/NEESData.cpp,v $
00024                                                                         
00025 // Written: fmk 
00026 // Created: 03/04
00027 //
00028 // Description: This file contains the class definition for NeesData.
00029 // NeesData is a concretes subclass of FE_Datastore. NEESData datastore
00030 // is used to store data from the recorders in a file format that can
00031 // be stored in the NEES data repository.
00032 
00033 // NEESData objects currently only implement the createTable() and 
00034 // insertDataIntoTable() methods of the abstract base FE_Datastore
00035 // class. At this time the other methods should never be called.
00036 //
00037 // What: "@(#) NEESData.C, revA"
00038 
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 //#include <bool.h>
00043 #include <iostream>
00044 #include <iomanip>
00045 using std::ios;
00046 using std::setiosflags;
00047 
00048 #include <FEM_ObjectBroker.h>
00049 #include <Domain.h>
00050 #include <ID.h>
00051 #include <Vector.h>
00052 #include <Matrix.h>
00053 #include <NEESData.h>
00054 
00055 NEESData::NEESData(const char *dataBaseName,
00056                    Domain &theDomain, 
00057                    FEM_ObjectBroker &theObjBroker) 
00058   :FE_Datastore(theDomain, theObjBroker), tables(0), numTables(0)
00059 {
00060   dataBase = new char [strlen(dataBaseName)+1];
00061   strcpy(dataBase, dataBaseName);
00062 }
00063 
00064 NEESData::~NEESData() 
00065 {
00066   // clean up all memory allocated
00067   for (int i=0; i<numTables; i++) {
00068     NEES_table* nextTable = tables->next;
00069     int numColumns = tables->numColumns;
00070     char **columns = tables->columns;
00071     for (int j=0; j<numColumns; j++)
00072       delete [] columns[j];
00073     delete [] columns;
00074     delete tables;
00075     tables = nextTable;
00076   }
00077 }
00078 
00079 int
00080 NEESData::getDbTag(void)
00081 {
00082   opserr << "NEESData::getDbTag(void) - should not be called\n";
00083   return -1;
00084 }
00085 
00086 int 
00087 NEESData::commitState(int commitTag)
00088 {
00089   opserr << "NEESData::commitState(int commitTag) - should not be called\n";
00090   return -1;
00091 }
00092 
00093 int 
00094 NEESData::sendMsg(int dataTag, int commitTag, 
00095                        const Message &, 
00096                        ChannelAddress *theAddress)
00097 {
00098   opserr << "NEESData::sendMsg(int dataTag, int commitTag,  - should not be called\n";
00099   return -1;
00100 }                      
00101 
00102 int 
00103 NEESData::recvMsg(int dataTag, int commitTag, 
00104                        Message &, 
00105                        ChannelAddress *theAddress)
00106 {
00107   opserr << "NEESData::recvMsg(int dataTag, int commitTag,  - should not be called\n";
00108   return -1;
00109 }                      
00110 
00111 
00112 int 
00113 NEESData::sendMatrix(int dataTag, int commitTag, 
00114                       const Matrix &theMatrix, 
00115                       ChannelAddress *theAddress)
00116 {
00117   opserr << "NEESData::sendMatrix(int dataTag, int commitTag,  - should not be called\n";
00118   return -1;
00119 }                      
00120 
00121 
00122 
00123 
00124 int 
00125 NEESData::recvMatrix(int dataTag, int commitTag, 
00126                       Matrix &theMatrix, 
00127                       ChannelAddress *theAddress)    
00128 {
00129   return -1;
00130 }                      
00131 
00132 
00133 
00134 int 
00135 NEESData::sendVector(int dataTag, int commitTag, 
00136                       const Vector &theVector, 
00137                       ChannelAddress *theAddress)
00138 {
00139   opserr << "NEESData::sendVector(int dataTag, int commitTag,  - should not be called\n";
00140   return -1;
00141 }                      
00142 
00143 int 
00144 NEESData::recvVector(int dataTag, int commitTag, 
00145                       Vector &theVector, 
00146                       ChannelAddress *theAddress)    
00147 {
00148   opserr << "NEESData::recvVector(int dataTag, int commitTag,  - should not be called\n";
00149   return -1;
00150 }                      
00151 
00152 
00153 
00154 int 
00155 NEESData::sendID(int dataTag, int commitTag, 
00156                       const ID &theID, 
00157                       ChannelAddress *theAddress)
00158 {
00159   opserr << "NEESData::sendID(int dataTag, int commitTag,  - should not be called\n";
00160   return -1;
00161 }
00162 
00163 int 
00164 NEESData::recvID(int dataTag, int commitTag, 
00165                       ID &theID, 
00166                       ChannelAddress *theAddress)    
00167 {
00168   opserr << "NEESData::recvID(int dataTag, int commitTag,  - should not be called\n";
00169   return -1;
00170 }                      
00171 
00172 
00173 #include <fstream>
00174 using std::ofstream;
00175 using std::cerr;
00176 
00177 
00178 int 
00179 NEESData::createTable(const char *tableName, int numColumns, char *columns[])
00180 {
00181   // create a copy of the data file name
00182   int res = 0;
00183   int tableNameLength = strlen(tableName);
00184   char *copyTableName = new char[tableNameLength + 5];
00185   char *fileName = new char[tableNameLength + 5];
00186 
00187   bool hasOutExtension = false;
00188 
00189   if (fileName == 0 || copyTableName == 0) {
00190     opserr << "NEESData::insertData - out of memory creating copy of string " << fileName << endln;
00191     return -1;
00192   }
00193 
00194   strcpy(copyTableName, tableName);
00195   if (tableNameLength > 4 && strcmp(".out", &tableName[tableNameLength-4]) == 0) {
00196     copyTableName[tableNameLength-4] = '\0';
00197     hasOutExtension = true;
00198   }
00199 
00200   // first ensure createTable has not already been called with this table name
00201   NEES_table *t = tables;
00202   for (int i=0; i<numTables; i++) {
00203     if (strcmp(t->name, tableName) == 0) {
00204       opserr << "WARNING: NEESData::createTable - table already exists: " << copyTableName << endln;
00205       return -1;
00206     } 
00207     t = t->next;
00208   }
00209 
00210   strcpy(fileName, copyTableName);
00211   strcat(fileName,".out");
00212 
00213   if (numColumns <= 0) {
00214     opserr << "WARNING: NEESData::createTable - number of data columns < 0 for table name: " << copyTableName << endln;
00215     delete [] fileName;
00216     delete [] copyTableName;
00217     return -1;
00218   }
00219 
00220   // 
00221   // test opening the data file
00222   // 
00223 
00224   strcpy(fileName, copyTableName);    
00225   strcat(fileName,".out");
00226 
00227   ofstream dataFile;
00228   dataFile.open(fileName, ios::out | ios::trunc); 
00229 
00230   if (!dataFile.is_open()) {
00231     opserr << "NEESData::createTable - failed to open file: " << fileName << endln;
00232     delete [] fileName;
00233     delete [] copyTableName;
00234     return -1;
00235   }
00236 
00237   // 
00238   // now open the xml file & write the data to it
00239   // 
00240 
00241   strcpy(fileName, copyTableName);    
00242   strcat(fileName,".xml");
00243 
00244   ofstream xmlFile;
00245   xmlFile.open(fileName, ios::out | ios::trunc); 
00246   
00247   if (xmlFile.is_open()) {
00248     // write the xml data
00249     xmlFile << "<?xml version=\"1.0\"?>\n";
00250     xmlFile << "<NumericalFileDataDescription>\n";
00251     xmlFile << "\t<DataFile>\n";
00252     xmlFile << "\t\t<DataFileName> " << copyTableName << ".out </DataFileName>\n";
00253     xmlFile << "\t\t<NumberDataColumns> " << numColumns << "</NumberDataColumns>\n";
00254     xmlFile << "\t</DataFile>\n";
00255     for (int i=0; i<numColumns; i++) {
00256       xmlFile << "\t<DataColumnDescription>\n";
00257       xmlFile << "\t\t<ColumnLocation> " << i+1 << "</ColumnLocation>\n";      
00258       xmlFile << "\t\t<Description> " << columns[i] << "</Description>\n";
00259       xmlFile << "\t</DataColumnDescription>\n";
00260     }
00261     xmlFile << "</NumericalFileDataDescription>\n";
00262     xmlFile.close();
00263     
00264   } else {
00265     opserr << "NEESData::createData - failed to open file: " << copyTableName << endln;
00266     delete [] fileName;
00267     delete [] copyTableName;
00268     res = -1;
00269   }
00270 
00271   // no longer need copyTableName
00272   delete [] copyTableName;
00273 
00274   // if we can get here we can open the data and have written to the xml file
00275   // now add a new struct to our list of tables
00276   NEES_table *nextTable = new NEES_table;
00277   if (nextTable == 0) {
00278     opserr << "NEESData::createData - out of memory creating Table structure for table: " << copyTableName << endln;
00279     res = -1;
00280   }    
00281   strcpy(fileName, tableName);
00282 
00283   nextTable->name = fileName;
00284   nextTable->numColumns = numColumns;
00285   nextTable->columns = new char *[numColumns];
00286   for (int ii=0; ii<numColumns; ii++)
00287     nextTable->columns[ii] = 0;
00288   nextTable->hasOutExtension = hasOutExtension;
00289 
00290   if (nextTable->columns == 0) {
00291     opserr << "NEESData::createData - out of memory creating Table structure for table: " << copyTableName << endln;
00292     delete nextTable;
00293     res = -1;
00294   }
00295 
00296   for (int k=0; k<numColumns; k++) {
00297 
00298     nextTable->columns[k] = new char [strlen(columns[k])+1];
00299 
00300     if (nextTable->columns[k] == 0) {
00301       opserr << "NEESData::createData - out of memory creating Table structure for table: " << copyTableName << endln;
00302       for (int l=0; l<k-1; l++)
00303         delete nextTable->columns[k];
00304       delete nextTable;
00305       res = -1;    
00306     }
00307 
00308     strcpy(nextTable->columns[k], columns[k]);
00309   }    
00310 
00311   nextTable->next = tables;
00312   tables = nextTable;
00313   numTables++;
00314 
00315   return 0;
00316 }
00317 
00318 int 
00319 NEESData::insertData(const char *tableName, char *columns[], 
00320                      int commitTag, const Vector &data)
00321 {
00322   // search the tables for valid table
00323   NEES_table *t = tables;
00324   for (int i=0; i<numTables; i++) {
00325     if (strcmp(t->name, tableName) == 0) 
00326       i = numTables;
00327     else 
00328       t = t->next;
00329   }
00330 
00331   if ( t == 0) {
00332     opserr << "NEESData::insertData - table: " << tableName << " has not been created\n";
00333     return -1;
00334   }      
00335 
00336   if (t->numColumns != data.Size()) {
00337     opserr << "NEESData::insertData - incorrect number of columns for table: " << tableName << "\n";
00338     return -2;
00339   }
00340 
00341   char *fileName = t->name;
00342   if (t->hasOutExtension == false)
00343     strcat(fileName,".out");
00344 
00345   ofstream table;
00346   table.open(fileName, ios::app); 
00347 
00348   table << setiosflags(ios::scientific);
00349   table << std::setprecision(16);
00350 
00351   if (table.is_open()) {
00352     // write the data
00353     for (int i=0; i<data.Size(); i++) {
00354       table << data(i) << "\t";
00355     }
00356     
00357     table << "\n";
00358     table.close();
00359 
00360   } else {
00361     opserr << "NEESData::insertData - failed to open file: " << fileName << endln;
00362     return -1;
00363   }
00364 
00365   strcpy(fileName, tableName);
00366   return 0;
00367 }
00368 
00369 
00370 int 
00371 NEESData::getData(const char *tableName, char *columns[], int commitTag, Vector &data)
00372 {
00373   return 0;
00374 }
00375 

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