ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/applications/dynamicProps/CorrelationFunction.cpp
Revision: 2014
Committed: Sun Feb 13 21:03:07 2005 UTC (19 years, 7 months ago) by tim
File size: 9480 byte(s)
Log Message:
more fix

File Contents

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