ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/applications/dynamicProps/CorrelationFunction.cpp
Revision: 2010
Committed: Sun Feb 13 20:05:42 2005 UTC (19 years, 5 months ago) by tim
File size: 9372 byte(s)
Log Message:
maximum length defaults to the cutoff radius

File Contents

# Content
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 /**@todo Fixed Me */
90 Globals* simParams = info_->getSimParams();
91 if (simParams->haveSampleTime()){
92 deltaTime_ = simParams->getSampleTime();
93 } else {
94 sprintf(painCave.errMsg,
95 "CorrelationFunction::writeCorrelate Error: can not figure out deltaTime\n");
96 painCave.isFatal = 1;
97 simError();
98 }
99
100 int nframes = bsMan_->getNFrames();
101 nTimeBins_ = nframes;
102 histogram_.resize(nTimeBins_);
103 count_.resize(nTimeBins_);
104 time_.resize(nTimeBins_);
105
106 for (int i = 0; i < nTimeBins_; ++i) {
107 time_[i] = i * deltaTime_;
108 }
109
110 }
111
112
113 void CorrelationFunction::doCorrelate() {
114 preCorrelate();
115
116 int nblocks = bsMan_->getNBlocks();
117
118 for (int i = 0; i < nblocks; ++i) {
119 bsMan_->loadBlock(i);
120 for (int j = i; j < nblocks; ++j) {
121 bsMan_->loadBlock(j);
122 correlateBlocks(i, j);
123
124 if (i != j) {
125 //if i equals to j, the block is still used, should not unload it
126 bsMan_->unloadBlock(j);
127 }
128 }
129 bsMan_->unloadBlock(i);
130 }
131
132 postCorrelate();
133
134 writeCorrelate();
135 }
136
137 void CorrelationFunction::correlateBlocks(int block1, int block2) {
138
139 assert(bsMan_->isBlockActive(block1) && bsMan_->isBlockActive(block2));
140
141 SnapshotBlock snapshotBlock1 = bsMan_->getSnapshotBlock(block1);
142 SnapshotBlock snapshotBlock2 = bsMan_->getSnapshotBlock(block2);
143
144 for (int i = snapshotBlock1.first; i < snapshotBlock1.second; ++i) {
145
146 //evaluate selection if it is dynamic
147 if (evaluator1_.isDynamic()) {
148 seleMan1_.setSelectionSet(evaluator1_.evaluate());
149 validateSelection(seleMan1_);
150 }
151
152 //update the position or velocity of the atoms belong to rigid bodies
153 updateFrame(i);
154 for(int j = snapshotBlock1.first; j < snapshotBlock1.second; ++j) {
155 //evaluate selection
156 if (!evaluator2_.isDynamic()) {
157 seleMan2_.setSelectionSet(evaluator2_.evaluate());
158 validateSelection(seleMan2_);
159 }
160 //update the position or velocity of the atoms belong to rigid bodies
161 updateFrame(j);
162
163 correlateFrames(i, j);
164 }
165 }
166 }
167
168
169 void CorrelationFunction::correlateFrames(int frame1, int frame2) {
170 int count = seleMan1_.getSelectionCount();
171 assert( count == seleMan2_.getSelectionCount());
172
173 Snapshot* snapshot1 = bsMan_->getSnapshot(frame1);
174 Snapshot* snapshot2 = bsMan_->getSnapshot(frame2);
175 assert(snapshot1 && snapshot2);
176
177 double time1 = snapshot1->getTime();
178 double time2 = snapshot2->getTime();
179 int timeBin = int ((time2 - time1) /deltaTime_ + 0.5);
180 count_[timeBin] += count;
181
182 int i;
183 int j;
184 StuntDouble* sd1;
185 StuntDouble* sd2;
186 for (sd1 = seleMan1_.beginSelected(i), sd2 = seleMan2_.beginSelected(j); sd1 != NULL && sd2 != NULL;
187 sd1 = seleMan1_.nextSelected(i), sd2 = seleMan2_.nextSelected(j)) {
188
189 double corrVal = calcCorrVal(sd1, frame1, sd2, frame2);
190 histogram_[timeBin] += corrVal;
191 }
192
193 //for (sd1 = seleMan1_.beginSelected(i); sd1 != NULL; sd1 = seleMan1_.nextSelected(i)) {
194 //
195 // for (sd2 = seleMan2_.beginSelected(j); sd2 != NULL; sd2 = seleMan2_.nextSelected(j)) {
196 // double corrVal = calcCorrVal(sd1, frame1, sd2, frame2);
197 // }
198 //}
199
200 }
201
202
203 void CorrelationFunction::updateFrame(int frame){
204 Molecule* mol;
205 RigidBody* rb;
206 SimInfo::MoleculeIterator mi;
207 Molecule::RigidBodyIterator rbIter;
208
209 /** @todo need improvement */
210 if (storageLayout_ & DataStorage::dslPosition) {
211 for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
212
213 //change the positions of atoms which belong to the rigidbodies
214 for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
215 rb->updateAtoms(frame);
216 }
217 }
218 }
219
220 if (storageLayout_ & DataStorage::dslVelocity) {
221 for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
222
223 //change the positions of atoms which belong to the rigidbodies
224 for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
225 rb->updateAtomVel(frame);
226 }
227 }
228
229 }
230
231 }
232
233
234 void CorrelationFunction::preCorrelate() {
235 std::fill(histogram_.begin(), histogram_.end(), 0.0);
236 std::fill(count_.begin(), count_.end(), 0);
237 }
238
239 void CorrelationFunction::postCorrelate() {
240 for (int i =0 ; i < nTimeBins_; ++i) {
241 if (count_[i] > 0) {
242 histogram_[i] /= count_[i];
243 }
244 }
245 }
246
247
248 void CorrelationFunction::writeCorrelate() {
249 std::ofstream ofs(outputFilename_.c_str());
250
251 if (ofs.is_open()) {
252
253 ofs << "#" << getCorrFuncType() << "\n";
254 ofs << "#selection script1: \"" << selectionScript1_ <<"\"\tselection script2: \"" << selectionScript2_ << "\"\n";
255 ofs << "#extra information: " << extra_ << "\n";
256 ofs << "#time\tcorrVal\n";
257
258 for (int i = 0; i < nTimeBins_; ++i) {
259 ofs << time_[i] << "\t" << histogram_[i] << "\n";
260 }
261
262 } else {
263 sprintf(painCave.errMsg,
264 "CorrelationFunction::writeCorrelate Error: fail to open %s\n", outputFilename_.c_str());
265 painCave.isFatal = 1;
266 simError();
267 }
268
269 ofs.close();
270 }
271
272 }