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

ForcingData.cpp

Go to the documentation of this file.
00001 // ForcingData.cpp: implementation of the CForcingData class.
00002 //
00004 
00005 #include "ForcingData.h"
00006 
00008 // Construction/Destruction
00010 
00011 CForcingData::CForcingData()
00012 {
00013 
00014 }
00015 
00016 CForcingData::~CForcingData()
00017 {
00018         for(int i = 0; i < m_vpForcingVector.size(); i++)
00019         {
00020                 delete m_vpForcingVector[i];
00021         }
00022 }
00023 
00025 // Forcing data is read in from a file, with the format
00026 //              time            Chemical Name           value           
00027 // Data does not have to be ordered in any way; entries
00028 // in the above format should be input with no separating whitespace
00029 // ForcingData is very similar to ChemicalTimeSeriesData, except with 
00030 // less functionality since it doesn't need to do as many things.
00032 
00033 
00034 void CForcingData::AttachNewForcingData(ReactionNetwork *reactionNetwork, std::string fileName)
00035 {
00036         int whereAmI;
00037         int nChem = reactionNetwork->GetNumberOfChemicals();
00038 
00039         // used for reading in the data
00040         char lineBuf[1000];
00041         double time;
00042         double value;
00043         char name[100];
00044 
00045         // open the file for reading
00046         ifstream dataFile(fileName.c_str());
00047         assert(dataFile);
00048         
00049         // put the force data line by line into the linebuffer and then parse it
00050         while(dataFile.getline(lineBuf,sizeof(lineBuf)))
00051         {
00052                 // '%' is a comment character
00053                 if(lineBuf[0] == '%') {continue;}
00054                 std::istringstream dataStream(lineBuf);
00055                 dataStream >> time >> name >> value;
00056                 
00057                 // find the number the network has assigned to this chemical
00058                 std::string nameString(name);
00059                 for(whereAmI = 0; whereAmI < nChem; whereAmI++)
00060                 {       
00061                         bool isEqual = ( nameString == (reactionNetwork->GetChemical(whereAmI)->GetName()) );
00062                         if(isEqual) {break;}    
00063                 }
00064                 // create a force point and put it on the vector
00065                 m_vpForcingVector.push_back(new CForceVectorPoint(time,value,whereAmI));
00066         }
00067         // sort the vector
00068         SortForcingVector();
00069                 
00070         return;
00071 }
00072 
00073 void CForcingData::SortForcingVector()
00074 {
00075         // use STL sort with the correct comparator
00076         CForceVectorPoint::CForceVectorPointComparator comparator;
00077         std::sort(m_vpForcingVector.begin(),m_vpForcingVector.end(),comparator);
00078 }
00079 
00081 // GetNextForcingTime(t) returns the next time after t at which there
00082 // is a forcing point recorded. 
00084 
00085 double CForcingData::GetNextForcingTime(double time)
00086 {
00087         // STL algorithm
00088         // define an iterator for the timeVector
00089         std::vector<CForceVectorPoint *>::iterator it;
00090         // the comparator is defined in terms of CForceVectorPoint *, so
00091         // that's the value we need (it's a dummy in terms of everything
00092         // except time)
00093         CForceVectorPoint *fp = new CForceVectorPoint(time,0.0,0);
00094         // comparator
00095         CForceVectorPoint::CForceVectorPointComparator comparator;
00096         // get the iterator
00097         it = std::lower_bound(m_vpForcingVector.begin(),m_vpForcingVector.end(),fp,comparator); 
00098         // iterator points to a data point; if it's at the end the comparison
00099         // time is off-the-end of the vector
00100         if( it != m_vpForcingVector.end() )
00101         {
00102                 return (*it)->GetTime();
00103         }
00104         else
00105         {
00106                 // throwing an exception is just temporary
00107                 throw std::runtime_error("There are no more forcings recorded.");
00108         }
00109         
00110         // cleanup
00111         delete fp;
00112         //delete it; commented out for linux
00113 }
00114 
00116 // The function GetLatestForcingTime() gets the time at which the last
00117 // forcing point is recorded.  This function assumes the vector of 
00118 // forcings is already sorted according to time.
00120 
00121 double CForcingData::GetLatestForcingTime()
00122 {
00123         double latestTime = (m_vpForcingVector[m_vpForcingVector.size() - 1])->GetTime();
00124         return latestTime;
00125 }

Generated on Mon Nov 3 09:37:50 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002