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