| 1 |
#include "ZConsReader.hpp"
|
| 2 |
#include "simError.h"
|
| 3 |
|
| 4 |
ZConsReader::ZConsReader(SimInfo* info)
|
| 5 |
:istream(NULL){
|
| 6 |
|
| 7 |
GenericData* data;
|
| 8 |
StringData* filename;
|
| 9 |
this->info = info;
|
| 10 |
|
| 11 |
//retrieve output filename of z force
|
| 12 |
data = info->getProperty(ZCONSFILENAME_ID); |
| 13 |
if(!data) { |
| 14 |
|
| 15 |
|
| 16 |
sprintf( painCave.errMsg, |
| 17 |
"ZConsReader error: If you use an ZConstraint\n"
|
| 18 |
" , you must set output filename of z-force.\n"); |
| 19 |
painCave.isFatal = 1; |
| 20 |
simError(); |
| 21 |
|
| 22 |
} |
| 23 |
else{ |
| 24 |
|
| 25 |
filename = dynamic_cast<StringData*>(data); |
| 26 |
|
| 27 |
if(!filename){ |
| 28 |
|
| 29 |
sprintf( painCave.errMsg, |
| 30 |
"ZConsReader error: Can not get property from SimInfo\n");
|
| 31 |
painCave.isFatal = 1; |
| 32 |
simError(); |
| 33 |
|
| 34 |
} |
| 35 |
else{ |
| 36 |
zconsFileName = filename->getData();
|
| 37 |
} |
| 38 |
|
| 39 |
}
|
| 40 |
|
| 41 |
istream = new ifstream(zconsFileName.c_str());
|
| 42 |
|
| 43 |
if (!istream){
|
| 44 |
cerr << "open " << filename << "error" << endl;
|
| 45 |
exit(1);
|
| 46 |
}
|
| 47 |
|
| 48 |
readHeader();
|
| 49 |
}
|
| 50 |
|
| 51 |
ZConsReader::ZConsReader(const string& filename){
|
| 52 |
istream = new ifstream(zconsFileName.c_str());
|
| 53 |
|
| 54 |
if (!istream){
|
| 55 |
cerr << "open " << filename << "error" << endl;
|
| 56 |
exit(1);
|
| 57 |
}
|
| 58 |
|
| 59 |
readHeader();
|
| 60 |
}
|
| 61 |
|
| 62 |
ZConsReader::~ZConsReader(){
|
| 63 |
istream->close();
|
| 64 |
}
|
| 65 |
|
| 66 |
int ZConsReader::getNumZMol(){
|
| 67 |
return nZMol;
|
| 68 |
}
|
| 69 |
|
| 70 |
vector<int> ZConsReader::getZConsIndex(){
|
| 71 |
return index;
|
| 72 |
}
|
| 73 |
|
| 74 |
vector<double> ZConsReader::getZConsPos(){
|
| 75 |
return zconsPos;
|
| 76 |
}
|
| 77 |
|
| 78 |
//double ZConsReader::getKRatio(){
|
| 79 |
// return kRatio;
|
| 80 |
//}
|
| 81 |
|
| 82 |
double ZConsReader::getCurTime(){
|
| 83 |
return curTime;
|
| 84 |
}
|
| 85 |
|
| 86 |
vector<double> ZConsReader::getCurZPos(){
|
| 87 |
return curZPos;
|
| 88 |
}
|
| 89 |
|
| 90 |
vector<double> ZConsReader::getCurFZ(){
|
| 91 |
return curFZ;
|
| 92 |
}
|
| 93 |
|
| 94 |
void ZConsReader::readHeader(){
|
| 95 |
const int MAXBUFFERSIZE = 2000;
|
| 96 |
char line[MAXBUFFERSIZE];
|
| 97 |
int zmolIndex;
|
| 98 |
float zmolPos;
|
| 99 |
int sscanfCount;
|
| 100 |
|
| 101 |
istream->getline(line, MAXBUFFERSIZE);
|
| 102 |
|
| 103 |
cout << line << endl;
|
| 104 |
//skip the comment lines
|
| 105 |
while(line[0] == '#')
|
| 106 |
istream->getline(line, MAXBUFFERSIZE);
|
| 107 |
|
| 108 |
sscanfCount = sscanf(line, "%d", &nZMol);
|
| 109 |
|
| 110 |
if (sscanfCount != 1){
|
| 111 |
cerr << "ZConsReader Error : reading file error" << endl;
|
| 112 |
exit(1);
|
| 113 |
}
|
| 114 |
|
| 115 |
for(int i = 0 ; i < nZMol; i++){
|
| 116 |
|
| 117 |
istream->getline(line, MAXBUFFERSIZE);
|
| 118 |
|
| 119 |
sscanfCount = sscanf(line, "%d\t%f", &zmolIndex, &zmolPos);
|
| 120 |
if (sscanfCount != 2){
|
| 121 |
cerr << "ZConsReader Error : reading file error" << endl;
|
| 122 |
exit(1);
|
| 123 |
}
|
| 124 |
|
| 125 |
index.push_back(zmolIndex);
|
| 126 |
zconsPos.push_back(zmolPos);
|
| 127 |
}
|
| 128 |
|
| 129 |
curZPos.resize(nZMol);
|
| 130 |
curFZ.resize(nZMol);
|
| 131 |
}
|
| 132 |
|
| 133 |
void ZConsReader::readNextFrame(){
|
| 134 |
const int MAXBUFFERSIZE = 2000;
|
| 135 |
char line[MAXBUFFERSIZE];
|
| 136 |
int tempNZMol;
|
| 137 |
int sscanfCount;
|
| 138 |
int tempIndex;
|
| 139 |
float tempCurTime;
|
| 140 |
float tempFZ;
|
| 141 |
float tempCurZPos;
|
| 142 |
float tempZconsPos;
|
| 143 |
|
| 144 |
istream->getline(line, MAXBUFFERSIZE);
|
| 145 |
sscanfCount = sscanf(line, "%f", &tempCurTime);
|
| 146 |
if (sscanfCount != 1){
|
| 147 |
cerr << "ZConsReader Error : reading file error" << endl;
|
| 148 |
exit(1);
|
| 149 |
}
|
| 150 |
curTime = tempCurTime;
|
| 151 |
|
| 152 |
istream->getline(line, MAXBUFFERSIZE);
|
| 153 |
sscanfCount = sscanf(line, "%d", &tempNZMol);
|
| 154 |
if (sscanfCount != 1){
|
| 155 |
cerr << "ZConsReader Error : reading file error" << endl;
|
| 156 |
exit(1);
|
| 157 |
}
|
| 158 |
|
| 159 |
if (tempNZMol != nZMol){
|
| 160 |
cerr << "ZConsReader Error: reading file error" << endl;
|
| 161 |
exit(1);
|
| 162 |
}
|
| 163 |
|
| 164 |
for(int i = 0; i < nZMol; i++){
|
| 165 |
istream->getline(line, MAXBUFFERSIZE);
|
| 166 |
sscanfCount = sscanf(line, "%d\t%f\t%f\t%f", &tempIndex, &tempFZ, &tempCurZPos,&tempZconsPos);
|
| 167 |
if (sscanfCount != 4){
|
| 168 |
cerr << "ZConsReader Error : reading file error" << endl;
|
| 169 |
exit(1);
|
| 170 |
}
|
| 171 |
|
| 172 |
index[i] = tempIndex;
|
| 173 |
curFZ[i] = tempFZ;
|
| 174 |
curZPos[i]= tempCurZPos;
|
| 175 |
zconsPos[i] = tempZconsPos;
|
| 176 |
}
|
| 177 |
|
| 178 |
}
|
| 179 |
|
| 180 |
bool ZConsReader::hasNextFrame(){
|
| 181 |
return istream->peek() != EOF ? true : false;
|
| 182 |
}
|
| 183 |
|