00001
00002
00004
00005 #include "ChemicalTimeSeriesData.h"
00006
00008
00010
00011
00012
00013
00014
00015 ChemicalTimeSeriesData::ChemicalTimeSeriesData(ReactionNetwork *reactionNetwork)
00016 {
00017 this->nChem = reactionNetwork->GetNumberOfChemicals();
00018 chemData.resize(0);
00019 for(int i = 0; i < nChem; i++)
00020 {
00021 std::vector<DataPoint *> localVector;
00022 chemData.push_back(localVector);
00023 }
00024
00025 }
00026
00028
00029
00030
00031
00032
00033
00034
00035
00037
00038 ChemicalTimeSeriesData::ChemicalTimeSeriesData(ReactionNetwork *reactionNetwork, std::string fileName)
00039 {
00040 this->nChem = reactionNetwork->GetNumberOfChemicals();
00041
00042
00043 int whereAmI;
00044 int i;
00045 char lineBuf[1000];
00046 double time;
00047 char name[100];
00048 double value;
00049 double error;
00050
00051 this->nChem = reactionNetwork->GetNumberOfChemicals();
00052 chemData.resize(0);
00053 for(i = 0; i < nChem; i++)
00054 {
00055 std::vector<DataPoint *> localVector;
00056 chemData.push_back(localVector);
00057 }
00058
00059
00060 ifstream dataFile(fileName.c_str());
00061 assert(dataFile);
00062
00063
00064
00065
00066
00067
00068 while(dataFile.getline(lineBuf,sizeof(lineBuf)))
00069 {
00070 if(lineBuf[0] == '%') {continue;}
00071 std::istringstream dataStream(lineBuf);
00072 dataStream >> time >> name >> value >> error;
00073
00074
00075 std::string nameString(name);
00076 for(whereAmI = 0; whereAmI < nChem; whereAmI++)
00077 {
00078 bool isEqual = ( nameString == (reactionNetwork->GetChemical(whereAmI)->GetName()) );
00079 if(isEqual) {break;}
00080 }
00081
00082 DataPoint *dp = new DataPoint(time,value,error);
00083 chemData[whereAmI].push_back(dp);
00084 }
00085
00086 SortChemTimeSeriesData();
00087
00088 TimeVectorSynchronize();
00089
00090
00091 int counter = 0;
00092 for(i = 0; i < nChem; i++)
00093 {
00094 if(chemData[i].size() > 0) {counter++;}
00095 }
00096 nTimeSeries = counter;
00097
00098 return;
00099 }
00100
00102
00103
00104
00105
00106
00108
00109 void ChemicalTimeSeriesData::AttachNewTimeSeries(ReactionNetwork *reactionNetwork, std::string fileName)
00110 {
00111 this->nChem = reactionNetwork->GetNumberOfChemicals();
00112
00113
00114 int whereAmI;
00115 char lineBuf[1000];
00116 double time;
00117 char name[100];
00118 double value;
00119 double error;
00120
00121
00122
00123 ifstream dataFile(fileName.c_str());
00124 assert(dataFile);
00125
00126
00127 while(dataFile.getline(lineBuf,sizeof(lineBuf)))
00128 {
00129 if(lineBuf[0] == '%') {continue;}
00130 std::istringstream dataStream(lineBuf);
00131 dataStream >> time >> name >> value >> error;
00132
00133 std::string nameString(name);
00134 for(whereAmI = 0; whereAmI < nChem; whereAmI++)
00135 {
00136 bool isEqual = ( nameString == (reactionNetwork->GetChemical(whereAmI)->GetName()) );
00137 if(isEqual) {break;}
00138 }
00139
00140
00141 chemData[whereAmI].push_back(new DataPoint(time,value,error));
00142 }
00143
00144 SortChemTimeSeriesData();
00145
00146 TimeVectorSynchronize();
00147
00148
00149
00150
00151 int counter = 0;
00152 for(int i = 0; i < nChem; i++)
00153 {
00154
00155
00156 if(chemData[i].size() > 0) {counter++;}
00157 }
00158 nTimeSeries = counter;
00159
00160
00161 return;
00162 }
00163
00164 void ChemicalTimeSeriesData::TimeVectorSynchronize()
00165 {
00166
00167 timeVector.erase(timeVector.begin(),timeVector.end());
00168
00169 for(int i = 0; i < nChem; i++)
00170 {
00171 if(chemData[i].size() > 0)
00172 {
00173 for(int pos = 0; pos < chemData[i].size(); pos++)
00174 {
00175 double time = (chemData[i])[pos]->GetTime();
00176 TimeVectorPoint *tp = new TimeVectorPoint(time,pos,i);
00177 timeVector.push_back(tp);
00178 }
00179 }
00180 }
00181
00182 SortTimeVector();
00183 }
00184
00185
00186 ChemicalTimeSeriesData::~ChemicalTimeSeriesData()
00187 {
00188 int i,j;
00189
00190 for(i = 0; i < nChem; i++)
00191 {
00192 for(j = 0; j < chemData[i].size(); j++)
00193 {
00194 delete chemData[i][j];
00195 }
00196 }
00197 for(i = 0; i < timeVector.size(); i++)
00198 {
00199 delete timeVector[i];
00200 }
00201 }
00202
00203 std::vector<DataPoint *> ChemicalTimeSeriesData::GetTimeSeries(int chemIndex)
00204 {
00205 return chemData[chemIndex];
00206 }
00207
00208 void ChemicalTimeSeriesData::SortChemTimeSeriesData()
00209 {
00210
00211 DataPoint::DataPointComparator comparator;
00212 for(int i = 0; i < nChem; i++)
00213 {
00214 if( chemData[i].size() > 0 )
00215 {
00216 std::sort(chemData[i].begin(),chemData[i].end(),comparator);
00217 }
00218 }
00219 }
00220
00221 void ChemicalTimeSeriesData::SortTimeVector()
00222 {
00223
00224 TimeVectorPoint::TimeVectorPointComparator comparator;
00225 std::sort(timeVector.begin(),timeVector.end(),comparator);
00226 }
00227
00228 int ChemicalTimeSeriesData::GetNDataPoints()
00229 {
00230 return timeVector.size();
00231 }
00232
00233 int ChemicalTimeSeriesData::GetNTimeSeries()
00234 {
00235 return nTimeSeries;
00236 }
00237
00238 int ChemicalTimeSeriesData::GetNChemicals()
00239 {
00240 return nChem;
00241 }
00242
00243 double ChemicalTimeSeriesData::GetNextDataTime(double time)
00244 {
00245
00246
00247 std::vector<TimeVectorPoint *>::iterator it;
00248
00249
00250
00251 TimeVectorPoint *tp = new TimeVectorPoint(time,0,0);
00252
00253 TimeVectorPoint::TimeVectorPointComparator comparator;
00254
00255 it = std::lower_bound(timeVector.begin(),timeVector.end(),tp,comparator);
00256
00257
00258 if( it != timeVector.end() )
00259 {
00260 return (*it)->GetTime();
00261 }
00262 else
00263 {
00264
00265 throw std::runtime_error("Trial value is past final data point");
00266 }
00267
00268
00269 delete tp;
00270
00271 }
00272
00274
00275
00276
00277
00279
00280 double ChemicalTimeSeriesData::GetLatestDataTime()
00281 {
00282 SortTimeVector();
00283 double latestTime = (timeVector[timeVector.size() - 1])->GetTime();
00284 return latestTime;
00285 }