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