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