1 |
/* |
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. Acknowledgement of the program authors must be made in any |
10 |
* publication of scientific results based in part on use of the |
11 |
* program. An acceptable form of acknowledgement is citation of |
12 |
* the article in which the program was described (Matthew |
13 |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
* |
18 |
* 2. Redistributions of source code must retain the above copyright |
19 |
* notice, this list of conditions and the following disclaimer. |
20 |
* |
21 |
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
* notice, this list of conditions and the following disclaimer in the |
23 |
* documentation and/or other materials provided with the |
24 |
* distribution. |
25 |
* |
26 |
* This software is provided "AS IS," without a warranty of any |
27 |
* kind. All express or implied conditions, representations and |
28 |
* warranties, including any implied warranty of merchantability, |
29 |
* fitness for a particular purpose or non-infringement, are hereby |
30 |
* excluded. The University of Notre Dame and its licensors shall not |
31 |
* be liable for any damages suffered by licensee as a result of |
32 |
* using, modifying or distributing the software or its |
33 |
* derivatives. In no event will the University of Notre Dame or its |
34 |
* licensors be liable for any lost revenue, profit or data, or for |
35 |
* direct, indirect, special, consequential, incidental or punitive |
36 |
* damages, however caused and regardless of the theory of liability, |
37 |
* arising out of the use of or inability to use software, even if the |
38 |
* University of Notre Dame has been advised of the possibility of |
39 |
* such damages. |
40 |
*/ |
41 |
|
42 |
#include "applications/dynamicProps/CorrelationFunction.hpp" |
43 |
#include "utils/simError.h" |
44 |
#include "primitives/Molecule.hpp" |
45 |
namespace oopse { |
46 |
|
47 |
CorrelationFunction::CorrelationFunction(SimInfo* info, const std::string& filename, |
48 |
const std::string& sele1, const std::string& sele2, int storageLayout) |
49 |
: info_(info), storageLayout_(storageLayout), dumpFilename_(filename), selectionScript1_(sele1), |
50 |
selectionScript2_(sele2), evaluator1_(info), evaluator2_(info), seleMan1_(info), seleMan2_(info){ |
51 |
|
52 |
int nAtoms = info->getNGlobalAtoms(); |
53 |
int nRigidBodies = info->getNGlobalRigidBodies(); |
54 |
int nStuntDoubles = nAtoms + nRigidBodies; |
55 |
|
56 |
std::set<AtomType*> atomTypes = info->getUniqueAtomTypes(); |
57 |
std::set<AtomType*>::iterator i; |
58 |
bool hasDirectionalAtom = false; |
59 |
bool hasMultipole = false; |
60 |
for (i = atomTypes.begin(); i != atomTypes.end(); ++i) { |
61 |
if ((*i)->isDirectional()){ |
62 |
hasDirectionalAtom = true; |
63 |
} |
64 |
if ((*i)->isMultipole()){ |
65 |
hasDirectionalAtom = true; |
66 |
} |
67 |
} |
68 |
|
69 |
if (nRigidBodies > 0 || hasDirectionalAtom) { |
70 |
storageLayout_ |= DataStorage::dslAmat; |
71 |
} |
72 |
if (hasMultipole) { |
73 |
storageLayout_ |= DataStorage::dslElectroFrame; |
74 |
} |
75 |
|
76 |
bsMan_ = new BlockSnapshotManager(info, dumpFilename_, storageLayout_); |
77 |
info_->setSnapshotManager(bsMan_); |
78 |
|
79 |
//if selection is static, we only need to evaluate it once |
80 |
if (!evaluator1_.isDynamic()) { |
81 |
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
82 |
validateSelection(seleMan1_); |
83 |
} |
84 |
if (!evaluator2_.isDynamic()) { |
85 |
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
86 |
validateSelection(seleMan2_); |
87 |
} |
88 |
|
89 |
int nframes = bsMan_->getNFrames(); |
90 |
nTimeBins_ = nframes; /**@todo Fixed Me */ |
91 |
histogram_.resize(nTimeBins_); |
92 |
count_.resize(nTimeBins_); |
93 |
time_.resize(nTimeBins_); |
94 |
//deltaTime_ = info_->; |
95 |
} |
96 |
|
97 |
|
98 |
void CorrelationFunction::doCorrelate() { |
99 |
preCorrelate(); |
100 |
|
101 |
int nblocks = bsMan_->getNBlocks(); |
102 |
|
103 |
for (int i = 0; i < nblocks; ++i) { |
104 |
bsMan_->loadBlock(i); |
105 |
for (int j = i; j < nblocks; ++j) { |
106 |
bsMan_->loadBlock(j); |
107 |
correlateBlocks(i, j); |
108 |
|
109 |
if (i != j) { |
110 |
//if i equals to j, the block is still used, should not unload it |
111 |
bsMan_->unloadBlock(j); |
112 |
} |
113 |
} |
114 |
bsMan_->unloadBlock(i); |
115 |
} |
116 |
|
117 |
postCorrelate(); |
118 |
|
119 |
writeCorrelate(); |
120 |
} |
121 |
|
122 |
void CorrelationFunction::correlateBlocks(int block1, int block2) { |
123 |
|
124 |
assert(bsMan_->isBlockActive(block1) && bsMan_->isBlockActive(block2)); |
125 |
|
126 |
SnapshotBlock snapshotBlock1 = bsMan_->getSnapshotBlock(block1); |
127 |
SnapshotBlock snapshotBlock2 = bsMan_->getSnapshotBlock(block2); |
128 |
|
129 |
for (int i = snapshotBlock1.first; i < snapshotBlock1.second; ++i) { |
130 |
|
131 |
//evaluate selection if it is dynamic |
132 |
if (evaluator1_.isDynamic()) { |
133 |
seleMan1_.setSelectionSet(evaluator1_.evaluate()); |
134 |
validateSelection(seleMan1_); |
135 |
} |
136 |
|
137 |
//update the position or velocity of the atoms belong to rigid bodies |
138 |
updateFrame(i); |
139 |
for(int j = snapshotBlock1.first; j < snapshotBlock1.second; ++j) { |
140 |
//evaluate selection |
141 |
if (!evaluator2_.isDynamic()) { |
142 |
seleMan2_.setSelectionSet(evaluator2_.evaluate()); |
143 |
validateSelection(seleMan2_); |
144 |
} |
145 |
//update the position or velocity of the atoms belong to rigid bodies |
146 |
updateFrame(j); |
147 |
|
148 |
correlateFrames(i, j); |
149 |
} |
150 |
} |
151 |
} |
152 |
|
153 |
|
154 |
void CorrelationFunction::correlateFrames(int frame1, int frame2) { |
155 |
int count = seleMan1_.getSelectionCount(); |
156 |
assert( count == seleMan2_.getSelectionCount()); |
157 |
|
158 |
Snapshot* snapshot1 = bsMan_->getSnapshot(frame1); |
159 |
Snapshot* snapshot2 = bsMan_->getSnapshot(frame2); |
160 |
assert(snapshot1 && snapshot2); |
161 |
|
162 |
double time1 = snapshot1->getTime(); |
163 |
double time2 = snapshot2->getTime(); |
164 |
int timeBin = int ((time2 - time1) /deltaTime_ + 0.5); |
165 |
count_[timeBin] += count; |
166 |
|
167 |
int i; |
168 |
int j; |
169 |
StuntDouble* sd1; |
170 |
StuntDouble* sd2; |
171 |
for (sd1 = seleMan1_.beginSelected(i), sd2 = seleMan2_.beginSelected(j); sd1 != NULL && sd2 != NULL; |
172 |
sd1 = seleMan1_.nextSelected(i), sd2 = seleMan2_.nextSelected(j)) { |
173 |
|
174 |
double corrVal = calcCorrVal(sd1, frame1, sd2, frame2); |
175 |
histogram_[timeBin] += corrVal; |
176 |
} |
177 |
|
178 |
//for (sd1 = seleMan1_.beginSelected(i); sd1 != NULL; sd1 = seleMan1_.nextSelected(i)) { |
179 |
// |
180 |
// for (sd2 = seleMan2_.beginSelected(j); sd2 != NULL; sd2 = seleMan2_.nextSelected(j)) { |
181 |
// double corrVal = calcCorrVal(sd1, frame1, sd2, frame2); |
182 |
// } |
183 |
//} |
184 |
|
185 |
} |
186 |
|
187 |
|
188 |
void CorrelationFunction::updateFrame(int frame){ |
189 |
Molecule* mol; |
190 |
RigidBody* rb; |
191 |
SimInfo::MoleculeIterator mi; |
192 |
Molecule::RigidBodyIterator rbIter; |
193 |
|
194 |
/** @todo need improvement */ |
195 |
if (storageLayout_ & DataStorage::dslPosition) { |
196 |
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
197 |
|
198 |
//change the positions of atoms which belong to the rigidbodies |
199 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
200 |
rb->updateAtoms(frame); |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
if (storageLayout_ & DataStorage::dslVelocity) { |
206 |
for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { |
207 |
|
208 |
//change the positions of atoms which belong to the rigidbodies |
209 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
210 |
rb->updateAtomVel(frame); |
211 |
} |
212 |
} |
213 |
|
214 |
} |
215 |
|
216 |
} |
217 |
|
218 |
|
219 |
void CorrelationFunction::preCorrelate() { |
220 |
std::fill(histogram_.begin(), histogram_.end(), 0.0); |
221 |
std::fill(count_.begin(), count_.end(), 0); |
222 |
} |
223 |
|
224 |
void CorrelationFunction::postCorrelate() { |
225 |
for (int i =0 ; i < nTimeBins_; ++i) { |
226 |
if (count_[i] > 0) { |
227 |
histogram_[i] /= count_[i]; |
228 |
} |
229 |
} |
230 |
} |
231 |
|
232 |
|
233 |
void CorrelationFunction::writeCorrelate() { |
234 |
std::ofstream ofs(outputFilename_.c_str()); |
235 |
|
236 |
if (ofs.is_open()) { |
237 |
|
238 |
ofs << "#" << getCorrFuncType() << "\n"; |
239 |
ofs << "#selection script1: \"" << selectionScript1_ <<"\"\tselection script2: \"" << selectionScript2_ << "\"\n"; |
240 |
ofs << "#extra information: " << extra_ << "\n"; |
241 |
ofs << "#time\tcorrVal\n"; |
242 |
|
243 |
for (int i = 0; i < nTimeBins_; ++i) { |
244 |
ofs << time_[i] << "\t" << histogram_[i] << "\n"; |
245 |
} |
246 |
|
247 |
} else { |
248 |
sprintf(painCave.errMsg, |
249 |
"CorrelationFunction::writeCorrelate Error: fail to open %s\n", outputFilename_.c_str()); |
250 |
painCave.isFatal = 1; |
251 |
simError(); |
252 |
} |
253 |
|
254 |
ofs.close(); |
255 |
} |
256 |
|
257 |
} |