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