/* * This utility reads an output file produced by SimDE, * determines the biggest and smallest time differences * between successive steps, as well as the total time, * and rewrites the file with these values at the start. * Usage: * simdeoutproc * simdeoutproc file1.ext file2.ext ... */ #include #include #include #include int process(std::string fname) { // Open the input file std::ifstream infile(fname.c_str()); if (!infile) { cout << "Error opening " << fname << " for reading." << endl; return -1; } std::string stringbuff; // Contains the contents of each line read std::string subbuff; // Contains part of each line read const int LineSize = 1024; std::vector contents; // Eventually will hold the entire file's contents std::string::size_type pos1 = 0, pos2 = 0; double tcurr = 0, tmin = 1.0e9, tmax = 0, tlast = -1.0e9, tdiff = 0; std::vector reactants; std::vector reactvals, reactvalsmin; double value = 0; int count = 0; int nlines = 0; // Read the contents of the input file // First deal with commented lines while ('#' == infile.peek()) { getline(infile, stringbuff); if (stringbuff.find("ReactantName") != std::string::npos) { // This line gives a reactant's name. // Extract it and push it onto the reactant name vector. pos1 = stringbuff.find_last_of(" "); reactants.push_back(stringbuff.substr(pos1)); } contents.push_back(stringbuff); cout << "Read a comment\n"; } if (reactants.size()==0) { cout << "Error: no reactants found in input file." << endl; return -1; } // Populate the vector of reactant numbers with zeros std::vector::iterator react_it = reactants.begin(); while (react_it != reactants.end()) { reactvals.push_back(0.0); reactvalsmin.push_back(1e15); react_it++; } // Read the first data line cout << "Reading data \n"; if (getline(infile, stringbuff)) { cout << "Read " << stringbuff << endl; contents.push_back(stringbuff); pos1 = stringbuff.find_first_of(" "); tcurr = atof(stringbuff.substr(0, pos1).c_str()); tlast = tcurr; nlines++; } // Read the subsequent lines while (getline(infile, stringbuff)) { contents.push_back(stringbuff); pos1 = stringbuff.find_first_of(" "); tcurr = atof(stringbuff.substr(0, pos1).c_str()); if ((tdiff = tcurr - tlast) < tmin) { tmin = tdiff; } if (tdiff > tmax) { tmax = tdiff; } tlast = tcurr; subbuff = stringbuff.substr(pos1); pos1 = subbuff.find_first_not_of(" "); subbuff = subbuff.substr(pos1); pos1 = subbuff.find_first_of(" "); subbuff = subbuff.substr(pos1); pos1 = subbuff.find_first_not_of(" "); subbuff = subbuff.substr(pos1); count = 0; while (pos1 != std::string::npos) { pos1 = subbuff.find_first_of(" "); if (pos2 == std::string::npos) { value = atof(subbuff.c_str()); } else { value = atof(subbuff.substr(0, pos1).c_str() ); } if (reactvals[count] < value) { reactvals[count] = value; cout << "New reactvals[" << count <<"] = " << value << endl; } if (reactvalsmin[count] > value) { reactvalsmin[count] = value; cout << "New reactvalsmin[" << count <<"] = " << value << endl; } count++; pos1 = subbuff.find_first_of(" "); if (pos1 != std::string::npos) { subbuff = subbuff.substr(pos1); pos1 = subbuff.find_first_not_of(" "); if (pos1 != std::string::npos) { subbuff = subbuff.substr(pos1); } } } nlines++; } infile.close(); cout << "Done reading data\n"; // Now open the same filename for output std::ofstream outfile(fname.c_str()); if (!outfile) { cout << "Error opening " << fname << " for writing." << std::endl; return -1; } // Write the information we've just garnered outfile << "#TotalTime " << tlast << std::endl; outfile << "#MinimumTimeDifference " << tmin << std::endl; outfile << "#MaximumTimeDifference " << tmax << std::endl; std::vector::iterator val_it = reactvals.begin(); react_it = reactants.begin(); double max = 0; while (react_it != reactants.end()) { outfile << "#MaximumValueOf " << *react_it << " " << *val_it << endl; if (*val_it > max) { max = *val_it; } react_it++; val_it++; } outfile <<"#MaximumOfAllReactants " << max << endl; //minima val_it = reactvalsmin.begin(); react_it = reactants.begin(); max = 1e15; while (react_it != reactants.end()) { //outfile << "#MinimumValueOf " << *react_it << " " << *val_it << endl; if (*val_it < max) { max = *val_it; } react_it++; val_it++; } //outfile <<"#MinimumOfAllReactants " << max << endl; outfile <<"#NumberOfSteps " << nlines << endl; // Now append the rest of the file contents std::vector::iterator it = contents.begin(); while (it != contents.end()) { outfile << *it << endl; it++; } outfile.close(); return 0; } int main(int argc, char **argv) { std::string fname; if (argc < 2) { // Get the file to process from the user. cout << "Enter the file to process: "; cin >> fname; if (process(fname) < 0) { cout << "Error processing " << fname << endl; return -1; } } else { // Assume that the command-line arguments are filenames int count = 0; while (argc - count > 1) { fname = argv[1 + count++]; if (process(fname) < 0) { cout << "Error processing " << fname << endl; return -1; } cout << "Processing " << fname << endl; } } return 0; }