1 |
#include "ZconsVisitor.hpp" |
2 |
#include <cmath> |
3 |
|
4 |
ZConsVisitor::ZConsVisitor(SimInfo* info) : BaseVisitor(), zconsReader(NULL){ |
5 |
GenericData* data; |
6 |
DoubleData* tolerance; |
7 |
ZConsParaData* zConsParaData; |
8 |
StringData* filename; |
9 |
DoubleData* sampleTime; |
10 |
vector<ZConsParaItem>* parameters; |
11 |
|
12 |
this->info = info; |
13 |
visitorName = "ZConsVisitor"; |
14 |
|
15 |
//retrieve tolerance for z-constraint molecuels |
16 |
data = info->getProperty(ZCONSTOL_ID); |
17 |
|
18 |
if (!data){ |
19 |
cerr << "Can not get zconstraint tolerance from SimInfo" << endl; |
20 |
haveZcons = false; |
21 |
return; |
22 |
} |
23 |
else{ |
24 |
tolerance = dynamic_cast<DoubleData*>(data); |
25 |
|
26 |
if (!tolerance){ |
27 |
cerr << "Can not get zconstraint tolerance from SimInfo" << endl; |
28 |
haveZcons = false; |
29 |
return; |
30 |
} |
31 |
else{ |
32 |
zconsTol = tolerance->getData(); |
33 |
} |
34 |
} |
35 |
|
36 |
//retrieve sample time of z-contraint |
37 |
data = info->getProperty(ZCONSTIME_ID); |
38 |
|
39 |
if (!data){ |
40 |
cerr << "Can not get zcons time from SimInfo" << endl; |
41 |
haveZcons = false; |
42 |
return; |
43 |
} |
44 |
else{ |
45 |
sampleTime = dynamic_cast<DoubleData*>(data); |
46 |
|
47 |
if (!sampleTime){ |
48 |
cerr << "Can not get zcons time from SimInfo" << endl; |
49 |
haveZcons = false; |
50 |
return; |
51 |
} |
52 |
else{ |
53 |
zconsTime = sampleTime->getData(); |
54 |
} |
55 |
} |
56 |
|
57 |
//retrieve index of z-constraint molecules |
58 |
data = info->getProperty(ZCONSPARADATA_ID); |
59 |
if (!data){ |
60 |
cerr << "Can not get index of z-constraint molecules from SimInfo" << endl; |
61 |
haveZcons = false; |
62 |
return; |
63 |
} |
64 |
else{ |
65 |
zConsParaData = dynamic_cast<ZConsParaData*>(data); |
66 |
|
67 |
if (!zConsParaData){ |
68 |
cerr << "Can not get index of z-constraint molecules from SimInfo" << endl; |
69 |
haveZcons = false; |
70 |
return; |
71 |
} |
72 |
else{ |
73 |
vector<ZConsParaItem>::iterator i; |
74 |
Molecule* mol; |
75 |
|
76 |
parameters = zConsParaData->getData(); |
77 |
for(i = parameters->begin(); i != parameters->end(); ++i){ |
78 |
|
79 |
mol = findZconsMol(i->zconsIndex); |
80 |
if(mol != NULL) |
81 |
zconsMol.push_back(mol); |
82 |
|
83 |
} |
84 |
|
85 |
if(zconsMol.size() < 1){ |
86 |
cerr << "number of zconstraint molecules is less than one" << endl; |
87 |
haveZcons = false; |
88 |
return; |
89 |
} |
90 |
|
91 |
}//end if (!zConsParaData) |
92 |
|
93 |
}//end if (!data |
94 |
|
95 |
//retrieve output filename of z force |
96 |
data = info->getProperty(ZCONSFILENAME_ID); |
97 |
if (!data){ |
98 |
cerr << "Can not get filename of z-constraint output from SimInfo" << endl; |
99 |
haveZcons = false; |
100 |
return; |
101 |
} |
102 |
else{ |
103 |
filename = dynamic_cast<StringData*>(data); |
104 |
|
105 |
if (!filename){ |
106 |
cerr << "Can not get filename of z-constraint output from SimInfo" << endl; |
107 |
haveZcons = false; |
108 |
return; |
109 |
} |
110 |
else{ |
111 |
zconsFilename = filename->getData(); |
112 |
} |
113 |
} |
114 |
|
115 |
zconsReader = new ZConsReader(zconsFilename); |
116 |
|
117 |
if (zconsReader->hasNextFrame()) |
118 |
zconsReader->readNextFrame(); |
119 |
|
120 |
haveZcons = true; |
121 |
} |
122 |
|
123 |
ZConsVisitor::~ZConsVisitor(){ |
124 |
if(!zconsReader) |
125 |
delete zconsReader; |
126 |
|
127 |
} |
128 |
|
129 |
void ZConsVisitor::visit(Atom* atom){ |
130 |
string prefix; |
131 |
if(isZconstraint(atom->getIndex(), prefix)) |
132 |
internalVisit(atom, prefix); |
133 |
} |
134 |
|
135 |
void ZConsVisitor::visit(DirectionalAtom* datom){ |
136 |
string prefix; |
137 |
|
138 |
if(isZconstraint(datom->getIndex(), prefix)) |
139 |
internalVisit(datom, prefix); |
140 |
} |
141 |
|
142 |
void ZConsVisitor::visit(RigidBody* rb){ |
143 |
string prefix; |
144 |
vector<Atom*> atoms; |
145 |
|
146 |
atoms = rb->getAtoms(); |
147 |
|
148 |
if(isZconstraint(atoms[0]->getIndex(), prefix)) |
149 |
internalVisit(rb, prefix); |
150 |
} |
151 |
|
152 |
Molecule* ZConsVisitor::findZconsMol(int index){ |
153 |
Molecule* mols; |
154 |
mols = info->molecules; |
155 |
for(int i =0; i < info->n_mol; i++) |
156 |
if(index = mols[i].getMyIndex()) |
157 |
return &mols[i]; |
158 |
|
159 |
return NULL; |
160 |
} |
161 |
|
162 |
void ZConsVisitor::update(){ |
163 |
Molecule* mol; |
164 |
double com[3]; |
165 |
ZConsState state; |
166 |
Atom** atoms; |
167 |
int natoms; |
168 |
|
169 |
getZconsPos(info->currentTime); |
170 |
|
171 |
for(size_t i = 0; i < zconsMol.size(); i++){ |
172 |
zconsMol[i]->getCOM(com); |
173 |
|
174 |
if(fabs(com[2] - zconsPos[i]) < zconsTol) |
175 |
state = zsFixed; |
176 |
else |
177 |
state = zsMoving; |
178 |
|
179 |
//update the state of zconstraint atom |
180 |
natoms = zconsMol[i]->getNAtoms(); |
181 |
atoms = zconsMol[i]->getMyAtoms(); |
182 |
for(int j =0; j < natoms; j++) |
183 |
zconsState[atoms[i]->getIndex()] = state; |
184 |
} |
185 |
|
186 |
} |
187 |
|
188 |
void ZConsVisitor::getZconsPos(double time){ |
189 |
|
190 |
vector<double> tempPos; |
191 |
vector<double> prevPos; |
192 |
double tempTime; |
193 |
|
194 |
while(true){ |
195 |
tempTime = zconsReader->getCurTime(); |
196 |
|
197 |
if(tempTime >= time) |
198 |
return; |
199 |
|
200 |
zconsReader->readNextFrame(); |
201 |
zconsPos = zconsReader->getZConsPos(); |
202 |
} |
203 |
|
204 |
} |
205 |
|
206 |
void ZConsVisitor::internalVisit(StuntDouble* sd, const string& prefix){ |
207 |
GenericData* data; |
208 |
AtomData* atomData; |
209 |
AtomInfo* atomInfo; |
210 |
vector<AtomInfo*>::iterator iter; |
211 |
|
212 |
//if there is not atom data, just skip it |
213 |
data = sd->getProperty("ATOMDATA"); |
214 |
if(data != NULL) |
215 |
atomData = dynamic_cast<AtomData*>(data); |
216 |
if(atomData == NULL) |
217 |
return; |
218 |
else |
219 |
return; |
220 |
|
221 |
for(atomInfo = atomData->beginAtomInfo(iter); atomInfo; atomInfo = atomData->nextAtomInfo(iter)) |
222 |
atomInfo->AtomType = prefix + atomInfo->AtomType; |
223 |
} |
224 |
|
225 |
|
226 |
bool ZConsVisitor::isZconstraint(int index, string& prefix){ |
227 |
map<int, ZConsState>::iterator i; |
228 |
|
229 |
i = zconsState.find(index); |
230 |
if(i !=zconsState.end()){ |
231 |
prefix = (*i).second; |
232 |
return true; |
233 |
} |
234 |
else{ |
235 |
prefix = ""; |
236 |
return false; |
237 |
} |
238 |
} |
239 |
|
240 |
const string ZConsVisitor::toString(){ |
241 |
char buffer[65535]; |
242 |
string result; |
243 |
|
244 |
sprintf(buffer ,"------------------------------------------------------------------\n"); |
245 |
result += buffer; |
246 |
|
247 |
sprintf(buffer ,"Visitor name: %s\n", visitorName.c_str()); |
248 |
result += buffer; |
249 |
|
250 |
sprintf(buffer , "number of zconstraint molecule :%d\n", zconsMol.size()); |
251 |
result += buffer; |
252 |
|
253 |
sprintf(buffer , "zconstraint tolerance = %lf\n", zconsTol); |
254 |
result += buffer; |
255 |
|
256 |
sprintf(buffer , "zconstraint sample time = %lf\n", zconsTime); |
257 |
result += buffer; |
258 |
|
259 |
sprintf(buffer , "zconstraint output filename = %s\n", zconsFilename.c_str()); |
260 |
result += buffer; |
261 |
|
262 |
for(size_t i = 0; i < zconsMol.size(); ++i){ |
263 |
sprintf(buffer ,"zconstraint molecule[%d] = \n", zconsMol[i]->getMyIndex()); |
264 |
result += buffer; |
265 |
|
266 |
} |
267 |
|
268 |
|
269 |
sprintf(buffer ,"------------------------------------------------------------------\n"); |
270 |
result += buffer; |
271 |
|
272 |
return result; |
273 |
} |