00001
00002
00004
00005 #include "ForcingData.h"
00006
00008
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
00026
00027
00028
00029
00030
00032
00033
00034 void CForcingData::AttachNewForcingData(ReactionNetwork *reactionNetwork, std::string fileName)
00035 {
00036 int whereAmI;
00037 int nChem = reactionNetwork->GetNumberOfChemicals();
00038
00039
00040 char lineBuf[1000];
00041 double time;
00042 double value;
00043 char name[100];
00044
00045
00046 ifstream dataFile(fileName.c_str());
00047 assert(dataFile);
00048
00049
00050 while(dataFile.getline(lineBuf,sizeof(lineBuf)))
00051 {
00052
00053 if(lineBuf[0] == '%') {continue;}
00054 std::istringstream dataStream(lineBuf);
00055 dataStream >> time >> name >> value;
00056
00057
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
00065 m_vpForcingVector.push_back(new CForceVectorPoint(time,value,whereAmI));
00066 }
00067
00068 SortForcingVector();
00069
00070 return;
00071 }
00072
00073 void CForcingData::SortForcingVector()
00074 {
00075
00076 CForceVectorPoint::CForceVectorPointComparator comparator;
00077 std::sort(m_vpForcingVector.begin(),m_vpForcingVector.end(),comparator);
00078 }
00079
00081
00082
00084
00085 double CForcingData::GetNextForcingTime(double time)
00086 {
00087
00088
00089 std::vector<CForceVectorPoint *>::iterator it;
00090
00091
00092
00093 CForceVectorPoint *fp = new CForceVectorPoint(time,0.0,0);
00094
00095 CForceVectorPoint::CForceVectorPointComparator comparator;
00096
00097 it = std::lower_bound(m_vpForcingVector.begin(),m_vpForcingVector.end(),fp,comparator);
00098
00099
00100 if( it != m_vpForcingVector.end() )
00101 {
00102 return (*it)->GetTime();
00103 }
00104 else
00105 {
00106
00107 throw std::runtime_error("There are no more forcings recorded.");
00108 }
00109
00110
00111 delete fp;
00112
00113 }
00114
00116
00117
00118
00120
00121 double CForcingData::GetLatestForcingTime()
00122 {
00123 double latestTime = (m_vpForcingVector[m_vpForcingVector.size() - 1])->GetTime();
00124 return latestTime;
00125 }