1 |
|
2 |
#include <cmath> |
3 |
#include "visitors/ZconsVisitor.hpp" |
4 |
#include "primitives/Molecule.hpp" |
5 |
#include "utils/StringUtils.hpp" |
6 |
namespace oopse { |
7 |
|
8 |
ZConsVisitor::ZConsVisitor(SimInfo* info) : BaseVisitor(), info_(info), zconsReader_(NULL){ |
9 |
|
10 |
visitorName = "ZConsVisitor"; |
11 |
currSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); |
12 |
Globals* simParam = info_->getSimParams(); |
13 |
|
14 |
if (simParam->haveZconstraintTime()){ |
15 |
zconsTime_ = simParam->getZconsTime(); |
16 |
} |
17 |
else{ |
18 |
sprintf(painCave.errMsg, |
19 |
"ZConstraint error: If you use a ZConstraint,\n" |
20 |
"\tyou must set zconsTime.\n"); |
21 |
painCave.isFatal = 1; |
22 |
simError(); |
23 |
} |
24 |
|
25 |
if (simParam->haveZconsTol()){ |
26 |
zconsTol_ = simParam->getZconsTol(); |
27 |
} |
28 |
else{ |
29 |
zconsTol_ = 0.01; |
30 |
sprintf(painCave.errMsg, |
31 |
"ZConstraint Warning: Tolerance for z-constraint method is not specified.\n" |
32 |
"\tOOPSE will use a default value of %f.\n" |
33 |
"\tTo set the tolerance, use the zconsTol variable.\n", |
34 |
zconsTol_); |
35 |
painCave.isFatal = 0; |
36 |
simError(); |
37 |
} |
38 |
|
39 |
int nZconstraints = simParam->getNzConstraints(); |
40 |
ZconStamp** stamp = simParam->getZconStamp(); |
41 |
for (int i = 0; i < nZconstraints; i++){ |
42 |
int zmolIndex = stamp[i]->getMolIndex(); |
43 |
zmolStates_.insert(std::make_pair(zmolIndex, zsMoving)); |
44 |
} |
45 |
|
46 |
|
47 |
//fill zatomToZmol_ array |
48 |
/** @todo only works for single version now*/ |
49 |
std::map<int, ZConsState>::iterator j; |
50 |
for (j = zmolStates_.begin(); j != zmolStates_.end(); ++j) { |
51 |
Molecule* mol = info_->getMoleculeByGlobalIndex(j->first); |
52 |
assert(mol != NULL); |
53 |
Molecule::AtomIterator ai; |
54 |
Atom* at; |
55 |
for (at = mol->beginAtom(ai); at != NULL; at = mol->nextAtom(ai)) { |
56 |
zatomToZmol_.insert(std::make_pair(at->getGlobalIndex(), mol->getGlobalIndex())); |
57 |
} |
58 |
} |
59 |
|
60 |
zconsFilename_ = getPrefix(info_->getFinalConfigFileName()) + ".fz"; |
61 |
|
62 |
zconsReader_ = new ZConsReader(info); |
63 |
|
64 |
if (zconsReader_->hasNextFrame()) |
65 |
zconsReader_->readNextFrame(); |
66 |
|
67 |
} |
68 |
|
69 |
ZConsVisitor::~ZConsVisitor(){ |
70 |
if(!zconsReader_) |
71 |
delete zconsReader_; |
72 |
|
73 |
} |
74 |
|
75 |
void ZConsVisitor::visit(Atom* atom){ |
76 |
std::string prefix; |
77 |
if(isZconstraint(atom->getGlobalIndex(), prefix)) |
78 |
internalVisit(atom, prefix); |
79 |
} |
80 |
|
81 |
void ZConsVisitor::visit(DirectionalAtom* datom){ |
82 |
std::string prefix; |
83 |
|
84 |
if(isZconstraint(datom->getGlobalIndex(), prefix)) |
85 |
internalVisit(datom, prefix); |
86 |
} |
87 |
|
88 |
void ZConsVisitor::visit(RigidBody* rb){ |
89 |
std::string prefix; |
90 |
std::vector<Atom*> atoms; |
91 |
|
92 |
atoms = rb->getAtoms(); |
93 |
|
94 |
if(isZconstraint(atoms[0]->getGlobalIndex(), prefix)) |
95 |
internalVisit(rb, prefix); |
96 |
} |
97 |
|
98 |
void ZConsVisitor::update(){ |
99 |
Molecule* mol; |
100 |
Vector3d com; |
101 |
ZConsState state; |
102 |
std::map<int, ZConsState>::iterator i; |
103 |
for ( i = zmolStates_.begin(); i != zmolStates_.end(); ++i) { |
104 |
i->second = zsMoving; |
105 |
} |
106 |
|
107 |
readZconsFile(currSnapshot_->getTime()); |
108 |
|
109 |
const std::vector<ZconsData>& fixedZmolData = zconsReader_->getFixedZMolData(); |
110 |
std::vector<ZconsData>::const_iterator j; |
111 |
for (j = fixedZmolData.begin(); j != fixedZmolData.end(); ++j) { |
112 |
std::map<int, ZConsState>::iterator k = zmolStates_.find(j->zmolIndex); |
113 |
assert(k != zmolStates_.end()); |
114 |
k->second = zsFixed; |
115 |
} |
116 |
|
117 |
} |
118 |
|
119 |
void ZConsVisitor::readZconsFile(double time) { |
120 |
double tempTime; |
121 |
while(zconsReader_->hasNextFrame()){ |
122 |
tempTime = zconsReader_->getCurTime(); |
123 |
if(tempTime >= time) { |
124 |
return; |
125 |
} |
126 |
|
127 |
zconsReader_->readNextFrame(); |
128 |
} |
129 |
} |
130 |
|
131 |
void ZConsVisitor::internalVisit(StuntDouble* sd, const std::string& prefix){ |
132 |
GenericData* data; |
133 |
AtomData* atomData; |
134 |
AtomInfo* atomInfo; |
135 |
std::vector<AtomInfo*>::iterator iter; |
136 |
|
137 |
//if there is not atom data, just skip it |
138 |
data = sd->getPropertyByName("ATOMDATA"); |
139 |
if(data != NULL){ |
140 |
atomData = dynamic_cast<AtomData*>(data); |
141 |
if(atomData == NULL) |
142 |
return; |
143 |
} |
144 |
else |
145 |
return; |
146 |
|
147 |
for(atomInfo = atomData->beginAtomInfo(iter); atomInfo; atomInfo = atomData->nextAtomInfo(iter)) |
148 |
(atomInfo->AtomType).insert(0, prefix); |
149 |
} |
150 |
|
151 |
|
152 |
bool ZConsVisitor::isZconstraint(int atomIndex, std::string& prefix){ |
153 |
std::string prefixString[] = {"ZF", "ZM"}; |
154 |
std::map<int, int>::iterator i = zatomToZmol_.find(atomIndex); |
155 |
if (i == zatomToZmol_.end() ){ |
156 |
prefix = ""; |
157 |
return false; |
158 |
} else { |
159 |
|
160 |
std::map<int, ZConsState>::iterator j = zmolStates_.find(i->second); |
161 |
assert(j !=zmolStates_.end()); |
162 |
prefix = prefixString[j->second]; |
163 |
return true; |
164 |
} |
165 |
} |
166 |
|
167 |
const std::string ZConsVisitor::toString(){ |
168 |
char buffer[65535]; |
169 |
std::string result; |
170 |
|
171 |
sprintf(buffer ,"------------------------------------------------------------------\n"); |
172 |
result += buffer; |
173 |
|
174 |
sprintf(buffer ,"Visitor name: %s\n", visitorName.c_str()); |
175 |
result += buffer; |
176 |
|
177 |
sprintf(buffer , "number of zconstraint molecule: %d\n", zmolStates_.size()); |
178 |
result += buffer; |
179 |
|
180 |
sprintf(buffer , "zconstraint tolerance = %lf\n", zconsTol_); |
181 |
result += buffer; |
182 |
|
183 |
sprintf(buffer , "zconstraint sample time = %lf\n", zconsTime_); |
184 |
result += buffer; |
185 |
|
186 |
sprintf(buffer , "zconstraint output filename = %s\n", zconsFilename_.c_str()); |
187 |
result += buffer; |
188 |
|
189 |
std::map<int, ZConsState>::iterator i; |
190 |
int j = 0; |
191 |
for ( i = zmolStates_.begin(); i != zmolStates_.end(); ++i) { |
192 |
sprintf(buffer ,"zconstraint molecule[%d] = %d\n", j++, i->first); |
193 |
result += buffer; |
194 |
} |
195 |
|
196 |
sprintf(buffer ,"------------------------------------------------------------------\n"); |
197 |
result += buffer; |
198 |
|
199 |
return result; |
200 |
} |
201 |
|
202 |
|
203 |
}//namespace oopse |