1 |
tim |
333 |
/* |
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 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
tim |
333 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
tim |
333 |
* 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 |
gezelter |
1390 |
* |
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, 24107 (2008). |
39 |
gezelter |
1665 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
tim |
333 |
*/ |
42 |
|
|
|
43 |
|
|
#include "applications/dynamicProps/TimeCorrFunc.hpp" |
44 |
|
|
#include "utils/simError.h" |
45 |
|
|
#include "primitives/Molecule.hpp" |
46 |
gezelter |
1535 |
using namespace std; |
47 |
gezelter |
1390 |
namespace OpenMD { |
48 |
tim |
333 |
|
49 |
gezelter |
1535 |
TimeCorrFunc::TimeCorrFunc(SimInfo* info, const string& filename, |
50 |
|
|
const string& sele1, const string& sele2, |
51 |
gezelter |
1629 |
int storageLayout, long long int memSize) |
52 |
|
|
: info_(info), storageLayout_(storageLayout), memSize_(memSize), |
53 |
|
|
dumpFilename_(filename), selectionScript1_(sele1), |
54 |
|
|
selectionScript2_(sele2), evaluator1_(info), evaluator2_(info), |
55 |
|
|
seleMan1_(info), seleMan2_(info) { |
56 |
tim |
333 |
|
57 |
gezelter |
1535 |
int nAtoms = info->getNGlobalAtoms(); |
58 |
|
|
int nRigidBodies = info->getNGlobalRigidBodies(); |
59 |
|
|
|
60 |
|
|
set<AtomType*> atomTypes = info->getSimulatedAtomTypes(); |
61 |
|
|
set<AtomType*>::iterator i; |
62 |
|
|
bool hasDirectionalAtom = false; |
63 |
gezelter |
1787 |
bool hasDipole = false; |
64 |
|
|
bool hasQuadrupole = false; |
65 |
gezelter |
1535 |
for (i = atomTypes.begin(); i != atomTypes.end(); ++i) { |
66 |
|
|
if ((*i)->isDirectional()){ |
67 |
|
|
hasDirectionalAtom = true; |
68 |
gezelter |
507 |
} |
69 |
gezelter |
1787 |
if ((*i)->isDipole()){ |
70 |
|
|
hasDipole = true; |
71 |
gezelter |
507 |
} |
72 |
gezelter |
1787 |
if ((*i)->isQuadrupole()){ |
73 |
|
|
hasQuadrupole = true; |
74 |
|
|
} |
75 |
gezelter |
1535 |
} |
76 |
tim |
334 |
|
77 |
gezelter |
1535 |
if (nRigidBodies > 0 || hasDirectionalAtom) { |
78 |
|
|
storageLayout_ |= DataStorage::dslAmat; |
79 |
|
|
if(storageLayout_ & DataStorage::dslVelocity) { |
80 |
|
|
storageLayout_ |= DataStorage::dslAngularMomentum; |
81 |
gezelter |
507 |
} |
82 |
gezelter |
1535 |
if (storageLayout_ & DataStorage::dslForce) { |
83 |
|
|
storageLayout_ |= DataStorage::dslTorque; |
84 |
gezelter |
507 |
} |
85 |
gezelter |
1787 |
if (hasDipole) { |
86 |
|
|
storageLayout |= DataStorage::dslDipole; |
87 |
|
|
} |
88 |
|
|
if (hasQuadrupole) { |
89 |
|
|
storageLayout |= DataStorage::dslQuadrupole; |
90 |
|
|
} |
91 |
gezelter |
1535 |
} |
92 |
gezelter |
1787 |
|
93 |
gezelter |
1629 |
bsMan_ = new BlockSnapshotManager(info, dumpFilename_, storageLayout_, memSize_); |
94 |
gezelter |
1535 |
info_->setSnapshotManager(bsMan_); |
95 |
|
|
|
96 |
|
|
evaluator1_.loadScriptString(selectionScript1_); |
97 |
|
|
evaluator2_.loadScriptString(selectionScript2_); |
98 |
|
|
|
99 |
|
|
//if selection is static, we only need to evaluate it once |
100 |
|
|
if (!evaluator1_.isDynamic()) { |
101 |
|
|
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
102 |
|
|
validateSelection(seleMan1_); |
103 |
|
|
} else { |
104 |
|
|
sprintf(painCave.errMsg, |
105 |
|
|
"TimeCorrFunc Error: dynamic selection is not supported\n"); |
106 |
|
|
painCave.isFatal = 1; |
107 |
|
|
simError(); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
if (!evaluator2_.isDynamic()) { |
111 |
|
|
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
112 |
|
|
validateSelection(seleMan2_); |
113 |
|
|
} else { |
114 |
|
|
sprintf(painCave.errMsg, |
115 |
|
|
"TimeCorrFunc Error: dynamic selection is not supported\n"); |
116 |
|
|
painCave.isFatal = 1; |
117 |
|
|
simError(); |
118 |
|
|
} |
119 |
|
|
|
120 |
tim |
333 |
|
121 |
|
|
|
122 |
gezelter |
1535 |
/**@todo Fix Me */ |
123 |
|
|
Globals* simParams = info_->getSimParams(); |
124 |
|
|
if (simParams->haveSampleTime()){ |
125 |
|
|
deltaTime_ = simParams->getSampleTime(); |
126 |
|
|
} else { |
127 |
|
|
sprintf(painCave.errMsg, |
128 |
|
|
"TimeCorrFunc::writeCorrelate Error: can not figure out deltaTime\n"); |
129 |
|
|
painCave.isFatal = 1; |
130 |
|
|
simError(); |
131 |
|
|
} |
132 |
tim |
333 |
|
133 |
gezelter |
1535 |
int nframes = bsMan_->getNFrames(); |
134 |
|
|
nTimeBins_ = nframes; |
135 |
|
|
histogram_.resize(nTimeBins_); |
136 |
|
|
count_.resize(nTimeBins_); |
137 |
|
|
time_.resize(nTimeBins_); |
138 |
tim |
333 |
|
139 |
gezelter |
1535 |
for (int i = 0; i < nTimeBins_; ++i) { |
140 |
|
|
time_[i] = i * deltaTime_; |
141 |
|
|
} |
142 |
gezelter |
507 |
|
143 |
gezelter |
1535 |
} |
144 |
tim |
333 |
|
145 |
|
|
|
146 |
gezelter |
507 |
void TimeCorrFunc::doCorrelate() { |
147 |
tim |
333 |
preCorrelate(); |
148 |
|
|
|
149 |
|
|
int nblocks = bsMan_->getNBlocks(); |
150 |
|
|
|
151 |
|
|
for (int i = 0; i < nblocks; ++i) { |
152 |
gezelter |
507 |
bsMan_->loadBlock(i); |
153 |
tim |
351 |
|
154 |
gezelter |
507 |
for (int j = i; j < nblocks; ++j) { |
155 |
|
|
bsMan_->loadBlock(j); |
156 |
|
|
correlateBlocks(i, j); |
157 |
|
|
bsMan_->unloadBlock(j); |
158 |
|
|
} |
159 |
tim |
351 |
|
160 |
gezelter |
507 |
bsMan_->unloadBlock(i); |
161 |
tim |
333 |
} |
162 |
|
|
|
163 |
|
|
postCorrelate(); |
164 |
|
|
|
165 |
|
|
writeCorrelate(); |
166 |
gezelter |
507 |
} |
167 |
tim |
333 |
|
168 |
gezelter |
507 |
void TimeCorrFunc::correlateBlocks(int block1, int block2) { |
169 |
tim |
333 |
|
170 |
tim |
334 |
int jstart, jend; |
171 |
tim |
333 |
|
172 |
tim |
334 |
assert(bsMan_->isBlockActive(block1) && bsMan_->isBlockActive(block2)); |
173 |
tim |
333 |
|
174 |
tim |
334 |
SnapshotBlock snapshotBlock1 = bsMan_->getSnapshotBlock(block1); |
175 |
|
|
SnapshotBlock snapshotBlock2 = bsMan_->getSnapshotBlock(block2); |
176 |
tim |
333 |
|
177 |
tim |
334 |
jend = snapshotBlock2.second; |
178 |
|
|
|
179 |
|
|
for (int i = snapshotBlock1.first; i < snapshotBlock1.second; ++i) { |
180 |
|
|
|
181 |
gezelter |
507 |
//update the position or velocity of the atoms belong to rigid bodies |
182 |
|
|
updateFrame(i); |
183 |
tim |
334 |
|
184 |
gezelter |
507 |
// if the two blocks are the same, we don't want to correlate |
185 |
|
|
// backwards in time, so start j at the same frame as i |
186 |
|
|
if (block1 == block2) { |
187 |
tim |
334 |
jstart = i; |
188 |
gezelter |
507 |
} else { |
189 |
|
|
jstart = snapshotBlock2.first; |
190 |
|
|
} |
191 |
tim |
334 |
|
192 |
gezelter |
507 |
for(int j = jstart; j < jend; ++j) { |
193 |
|
|
//update the position or velocity of the atoms belong to rigid bodies |
194 |
|
|
updateFrame(j); |
195 |
tim |
334 |
|
196 |
gezelter |
507 |
correlateFrames(i, j); |
197 |
|
|
} |
198 |
tim |
333 |
} |
199 |
gezelter |
507 |
} |
200 |
tim |
333 |
|
201 |
gezelter |
507 |
void TimeCorrFunc::updateFrame(int frame){ |
202 |
tim |
333 |
Molecule* mol; |
203 |
|
|
RigidBody* rb; |
204 |
|
|
SimInfo::MoleculeIterator mi; |
205 |
|
|
Molecule::RigidBodyIterator rbIter; |
206 |
|
|
|
207 |
|
|
/** @todo need improvement */ |
208 |
|
|
if (storageLayout_ & DataStorage::dslPosition) { |
209 |
xsun |
1183 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
210 |
|
|
mol = info_->nextMolecule(mi)) { |
211 |
tim |
333 |
|
212 |
gezelter |
507 |
//change the positions of atoms which belong to the rigidbodies |
213 |
xsun |
1183 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
214 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
215 |
gezelter |
507 |
rb->updateAtoms(frame); |
216 |
|
|
} |
217 |
|
|
} |
218 |
tim |
333 |
} |
219 |
|
|
|
220 |
|
|
if (storageLayout_ & DataStorage::dslVelocity) { |
221 |
xsun |
1183 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
222 |
|
|
mol = info_->nextMolecule(mi)) { |
223 |
|
|
|
224 |
gezelter |
507 |
//change the positions of atoms which belong to the rigidbodies |
225 |
xsun |
1183 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
226 |
|
|
rb = mol->nextRigidBody(rbIter)) { |
227 |
gezelter |
507 |
rb->updateAtomVel(frame); |
228 |
|
|
} |
229 |
gezelter |
1535 |
} |
230 |
|
|
} |
231 |
gezelter |
507 |
} |
232 |
tim |
333 |
|
233 |
|
|
|
234 |
gezelter |
507 |
void TimeCorrFunc::preCorrelate() { |
235 |
gezelter |
1535 |
fill(histogram_.begin(), histogram_.end(), 0.0); |
236 |
|
|
fill(count_.begin(), count_.end(), 0); |
237 |
gezelter |
507 |
} |
238 |
tim |
333 |
|
239 |
gezelter |
507 |
void TimeCorrFunc::postCorrelate() { |
240 |
tim |
333 |
for (int i =0 ; i < nTimeBins_; ++i) { |
241 |
gezelter |
507 |
if (count_[i] > 0) { |
242 |
|
|
histogram_[i] /= count_[i]; |
243 |
|
|
} |
244 |
tim |
333 |
} |
245 |
gezelter |
507 |
} |
246 |
tim |
333 |
|
247 |
|
|
|
248 |
gezelter |
507 |
void TimeCorrFunc::writeCorrelate() { |
249 |
gezelter |
1535 |
ofstream ofs(outputFilename_.c_str()); |
250 |
tim |
333 |
|
251 |
|
|
if (ofs.is_open()) { |
252 |
|
|
|
253 |
gezelter |
507 |
ofs << "#" << getCorrFuncType() << "\n"; |
254 |
gezelter |
1535 |
ofs << "#selection script1: \"" << selectionScript1_ ; |
255 |
|
|
ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"\n"; |
256 |
gezelter |
507 |
ofs << "#extra information: " << extra_ << "\n"; |
257 |
|
|
ofs << "#time\tcorrVal\n"; |
258 |
tim |
333 |
|
259 |
gezelter |
507 |
for (int i = 0; i < nTimeBins_; ++i) { |
260 |
|
|
ofs << time_[i] << "\t" << histogram_[i] << "\n"; |
261 |
|
|
} |
262 |
tim |
333 |
|
263 |
|
|
} else { |
264 |
gezelter |
507 |
sprintf(painCave.errMsg, |
265 |
gezelter |
1535 |
"TimeCorrFunc::writeCorrelate Error: fail to open %s\n", |
266 |
|
|
outputFilename_.c_str()); |
267 |
gezelter |
507 |
painCave.isFatal = 1; |
268 |
|
|
simError(); |
269 |
tim |
333 |
} |
270 |
|
|
|
271 |
|
|
ofs.close(); |
272 |
gezelter |
507 |
} |
273 |
tim |
333 |
|
274 |
|
|
} |