1 |
gezelter |
2056 |
/* |
2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
|
|
* 1. Redistributions of source code must retain the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
|
|
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
|
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
|
|
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
|
|
*/ |
42 |
|
|
|
43 |
|
|
#include "applications/dynamicProps/MultipassCorrFunc.hpp" |
44 |
|
|
#include "utils/simError.h" |
45 |
|
|
#include "primitives/Molecule.hpp" |
46 |
|
|
using namespace std; |
47 |
|
|
namespace OpenMD { |
48 |
|
|
|
49 |
|
|
MultipassCorrFunc::MultipassCorrFunc(SimInfo* info, const string& filename, |
50 |
|
|
const string& sele1, const string& sele2, |
51 |
|
|
int storageLayout) |
52 |
gezelter |
2071 |
: storageLayout_(storageLayout), info_(info), dumpFilename_(filename), |
53 |
|
|
seleMan1_(info_), seleMan2_(info_), |
54 |
|
|
selectionScript1_(sele1), selectionScript2_(sele2), |
55 |
|
|
evaluator1_(info_), evaluator2_(info_) { |
56 |
gezelter |
2056 |
|
57 |
|
|
// Request maximum needed storage for the simulation (including of |
58 |
|
|
// whatever was passed down by the individual correlation |
59 |
|
|
// function). |
60 |
|
|
|
61 |
|
|
storageLayout_ = info->getStorageLayout() | storageLayout; |
62 |
|
|
|
63 |
|
|
DumpReader reader(info_, dumpFilename_); |
64 |
|
|
|
65 |
|
|
evaluator1_.loadScriptString(selectionScript1_); |
66 |
|
|
evaluator2_.loadScriptString(selectionScript2_); |
67 |
|
|
|
68 |
|
|
//if selection is static, we only need to evaluate it once |
69 |
|
|
if (!evaluator1_.isDynamic()) { |
70 |
|
|
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
71 |
|
|
validateSelection(seleMan1_); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
if (!evaluator2_.isDynamic()) { |
75 |
|
|
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
76 |
|
|
validateSelection(seleMan2_); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/**@todo Fix Me */ |
80 |
|
|
Globals* simParams = info_->getSimParams(); |
81 |
|
|
if (simParams->haveSampleTime()){ |
82 |
|
|
deltaTime_ = simParams->getSampleTime(); |
83 |
|
|
} else { |
84 |
|
|
sprintf(painCave.errMsg, |
85 |
|
|
"MultipassCorrFunc::writeCorrelate Error: can not figure out deltaTime\n"); |
86 |
|
|
painCave.isFatal = 1; |
87 |
|
|
simError(); |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
void MultipassCorrFunc::preCorrelate() { |
92 |
|
|
Molecule* mol; |
93 |
|
|
RigidBody* rb; |
94 |
|
|
SimInfo::MoleculeIterator mi; |
95 |
|
|
Molecule::RigidBodyIterator rbIter; |
96 |
|
|
StuntDouble* sd; |
97 |
|
|
|
98 |
|
|
int index, isd1, isd2; |
99 |
|
|
|
100 |
|
|
fill(histogram_.begin(), histogram_.end(), 0.0); |
101 |
|
|
fill(count_.begin(), count_.end(), 0); |
102 |
|
|
|
103 |
|
|
DumpReader reader(info_, dumpFilename_); |
104 |
|
|
|
105 |
|
|
nFrames_ = reader.getNFrames(); |
106 |
|
|
times_.resize(nFrames_); |
107 |
|
|
|
108 |
|
|
for (int istep = 0; istep < nFrames_; istep++) { |
109 |
|
|
reader.readFrame(istep); |
110 |
|
|
currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); |
111 |
|
|
times_[istep] = currentSnapshot_->getTime(); |
112 |
|
|
|
113 |
|
|
// update the positions of atoms which belong to the rigidbodies |
114 |
|
|
for (mol = info_->beginMolecule(mi); mol != NULL; |
115 |
|
|
mol = info_->nextMolecule(mi)) { |
116 |
|
|
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
117 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
118 |
|
|
rb->updateAtoms(); |
119 |
|
|
} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
if (evaluator1_.isDynamic()) { |
123 |
|
|
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
if (evaluator2_.isDynamic()) { |
127 |
|
|
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
for (sd = seleMan1_.beginSelected(isd1); sd != NULL; |
131 |
|
|
sd = seleMan1_.nextSelected(isd1)) { |
132 |
|
|
|
133 |
|
|
sele1ToIndex_[istep].push_back(sd->getGlobalIndex()); |
134 |
|
|
index = sele1ToIndex_[istep].size(); |
135 |
|
|
computeProperty1(istep, sd, index); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
for (sd = seleMan2_.beginSelected(isd2); sd != NULL; |
139 |
|
|
sd = seleMan2_.nextSelected(isd2)) { |
140 |
|
|
|
141 |
|
|
sele2ToIndex_[istep].push_back(sd->getGlobalIndex()); |
142 |
|
|
index = sele2ToIndex_[istep].size(); |
143 |
|
|
|
144 |
|
|
computeProperty2(istep, sd, index); |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
void MultipassCorrFunc::doCorrelate() { |
151 |
|
|
preCorrelate(); |
152 |
|
|
correlation(); |
153 |
|
|
postCorrelate(); |
154 |
|
|
writeCorrelate(); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
void MultipassCorrFunc::correlation() { |
158 |
|
|
std::vector<int> s1; |
159 |
|
|
std::vector<int> s2; |
160 |
|
|
|
161 |
|
|
std::vector<int>::iterator i1; |
162 |
|
|
std::vector<int>::iterator i2; |
163 |
|
|
|
164 |
|
|
|
165 |
|
|
for (int i = 0; i < nFrames_; ++i) { |
166 |
|
|
|
167 |
|
|
RealType time1 = times_[i]; |
168 |
|
|
s1 = sele1ToIndex_[i]; |
169 |
|
|
|
170 |
|
|
for(int j = i; j < nFrames_; ++j) { |
171 |
|
|
|
172 |
|
|
// Perform a sanity check on the actual configuration times to |
173 |
|
|
// make sure the configurations are spaced the same amount the |
174 |
|
|
// sample time said they were spaced: |
175 |
|
|
|
176 |
|
|
RealType time2 = times_[j]; |
177 |
|
|
|
178 |
|
|
if ( fabs( (time2 - time1) - (j-i)*deltaTime_ ) > 1.0e-4 ) { |
179 |
|
|
sprintf(painCave.errMsg, |
180 |
|
|
"MultipassCorrFunc::correlateBlocks Error: sampleTime (%f)\n" |
181 |
|
|
"\tin %s does not match actual time-spacing between\n" |
182 |
|
|
"\tconfigurations %d (t = %f) and %d (t = %f).\n", |
183 |
|
|
deltaTime_, dumpFilename_.c_str(), i, time1, j, time2); |
184 |
|
|
painCave.isFatal = 1; |
185 |
|
|
simError(); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
int timeBin = int ((time2 - time1) / deltaTime_ + 0.5); |
189 |
|
|
s2 = sele2ToIndex_[j]; |
190 |
|
|
|
191 |
|
|
for (i1 = s1.begin(), i2 = s2.begin(); |
192 |
|
|
i1 != s1.end() && i2 != s2.end(); ++i1, ++i2){ |
193 |
|
|
|
194 |
|
|
// If the selections are dynamic, they might not have the |
195 |
|
|
// same objects in both frames, so we need to roll either of |
196 |
|
|
// the selections until we have the same object to |
197 |
|
|
// correlate. |
198 |
|
|
|
199 |
|
|
while ( *i1 < *i2 && i1 != s1.end()) { |
200 |
|
|
++i1; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
while ( *i2 < *i1 && i2 != s2.end() ) { |
204 |
|
|
++i2; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
if ( i1 == s1.end() || i2 == s2.end() ) break; |
208 |
|
|
|
209 |
|
|
RealType corrVal = calcCorrVal(i, j, *i1, *i2); |
210 |
|
|
histogram_[timeBin] += corrVal; |
211 |
|
|
count_[timeBin]++; |
212 |
|
|
|
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
void MultipassCorrFunc::postCorrelate() { |
221 |
|
|
for (int i =0 ; i < nTimeBins_; ++i) { |
222 |
|
|
if (count_[i] > 0) { |
223 |
|
|
histogram_[i] /= count_[i]; |
224 |
|
|
} else { |
225 |
|
|
histogram_[i] = 0; |
226 |
|
|
} |
227 |
|
|
} |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
|
231 |
|
|
void MultipassCorrFunc::updateFrame(int frame){ |
232 |
|
|
Molecule* mol; |
233 |
|
|
RigidBody* rb; |
234 |
|
|
SimInfo::MoleculeIterator mi; |
235 |
|
|
Molecule::RigidBodyIterator rbIter; |
236 |
|
|
/** @todo need improvement */ |
237 |
|
|
if (storageLayout_ & DataStorage::dslPosition) { |
238 |
|
|
for (mol = info_->beginMolecule(mi); mol != NULL; |
239 |
|
|
mol = info_->nextMolecule(mi)) { |
240 |
|
|
|
241 |
|
|
//change the positions of atoms which belong to the rigidbodies |
242 |
|
|
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
243 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
244 |
|
|
rb->updateAtoms(frame); |
245 |
|
|
} |
246 |
|
|
} |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
if (storageLayout_ & DataStorage::dslVelocity) { |
250 |
|
|
for (mol = info_->beginMolecule(mi); mol != NULL; |
251 |
|
|
mol = info_->nextMolecule(mi)) { |
252 |
|
|
|
253 |
|
|
//change the positions of atoms which belong to the rigidbodies |
254 |
|
|
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
255 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
256 |
|
|
rb->updateAtomVel(frame); |
257 |
|
|
} |
258 |
|
|
} |
259 |
|
|
} |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
void MultipassCorrFunc::writeCorrelate() { |
265 |
|
|
ofstream ofs(outputFilename_.c_str()); |
266 |
|
|
|
267 |
|
|
if (ofs.is_open()) { |
268 |
|
|
|
269 |
|
|
ofs << "#" << getCorrFuncType() << "\n"; |
270 |
|
|
ofs << "#selection script1: \"" << selectionScript1_ ; |
271 |
|
|
ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"\n"; |
272 |
|
|
ofs << "#extra information: " << extra_ << "\n"; |
273 |
|
|
ofs << "#time\tcorrVal\n"; |
274 |
|
|
|
275 |
|
|
for (int i = 0; i < nTimeBins_; ++i) { |
276 |
|
|
ofs << times_[i]-times_[0] << "\t" << histogram_[i] << "\n"; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
} else { |
280 |
|
|
sprintf(painCave.errMsg, |
281 |
|
|
"MultipassCorrFunc::writeCorrelate Error: fail to open %s\n", |
282 |
|
|
outputFilename_.c_str()); |
283 |
|
|
painCave.isFatal = 1; |
284 |
|
|
simError(); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
ofs.close(); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
} |