ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/applications/dynamicProps/CorrelationFunction.cpp
Revision: 2016
Committed: Mon Feb 14 15:18:09 2005 UTC (19 years, 7 months ago) by gezelter
File size: 10031 byte(s)
Log Message:
bug fix and start of switch for type of correlation function

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 gezelter 2016 int jstart, jend;
143    
144     assert(bsMan_->isBlockActive(block1) && bsMan_->isBlockActive(block2));
145 tim 2002
146 gezelter 2016 SnapshotBlock snapshotBlock1 = bsMan_->getSnapshotBlock(block1);
147     SnapshotBlock snapshotBlock2 = bsMan_->getSnapshotBlock(block2);
148    
149     jend = snapshotBlock2.second;
150    
151     for (int i = snapshotBlock1.first; i < snapshotBlock1.second; ++i) {
152 tim 2002
153 gezelter 2016 //evaluate selection if it is dynamic
154     if (evaluator1_.isDynamic()) {
155     seleMan1_.setSelectionSet(evaluator1_.evaluate());
156     validateSelection(seleMan1_);
157     }
158    
159     //update the position or velocity of the atoms belong to rigid bodies
160     updateFrame(i);
161    
162     // if the two blocks are the same, we don't want to correlate
163     // backwards in time, so start j at the same frame as i
164 tim 2002
165 gezelter 2016 if (block1 == block2)
166     jstart = i;
167     else
168     jstart = snapshotBlock2.first;
169    
170     for(int j = jstart; j < jend; ++j) {
171     //evaluate selection
172     if (!evaluator2_.isDynamic()) {
173     seleMan2_.setSelectionSet(evaluator2_.evaluate());
174     validateSelection(seleMan2_);
175     }
176     //update the position or velocity of the atoms belong to rigid bodies
177     updateFrame(j);
178    
179     correlateFrames(i, j);
180 tim 2002 }
181 gezelter 2016 }
182 tim 2002 }
183    
184    
185     void CorrelationFunction::correlateFrames(int frame1, int frame2) {
186     int count = seleMan1_.getSelectionCount();
187     assert( count == seleMan2_.getSelectionCount());
188    
189     Snapshot* snapshot1 = bsMan_->getSnapshot(frame1);
190     Snapshot* snapshot2 = bsMan_->getSnapshot(frame2);
191     assert(snapshot1 && snapshot2);
192    
193     double time1 = snapshot1->getTime();
194     double time2 = snapshot2->getTime();
195 gezelter 2016
196     if (time2 < time1) { printf("BARF ON NON POSITIVE DT!\n"); stop;}
197    
198 tim 2002 int timeBin = int ((time2 - time1) /deltaTime_ + 0.5);
199     count_[timeBin] += count;
200    
201     int i;
202     int j;
203     StuntDouble* sd1;
204     StuntDouble* sd2;
205 tim 2008
206 gezelter 2016 switch (correlationFunctionType()) {
207    
208     case frame:
209     double corrVal = calcCorrVal(frame1, frame2);
210     histogram_[timeBin] += corrVal;
211     count_[timeBin] += 1;
212     break;
213    
214     case particle:
215     for (sd1 = seleMan1_.beginSelected(i), sd1 != NULL;
216     sd1 = seleMan1_.nextSelected(i)) {
217     double corrVal = calcCorrVal(frame1, frame2, sd);
218 tim 2002 histogram_[timeBin] += corrVal;
219 gezelter 2016 count_[timeBin] += 1;
220     }
221     break;
222 tim 2002
223 gezelter 2016 case cross:
224     for (sd1 = seleMan1_.beginSelected(i); sd1 != NULL; sd1 = seleMan1_.nextSelected(i)) {
225    
226     for (sd2 = seleMan2_.beginSelected(j); sd2 != NULL; sd2 = seleMan2_.nextSelected(j)) {
227     double corrVal = calcCorrVal(frame1, frame2, sd1, sd2);
228     histogram_[timeBin] += corrVal;
229     count_[timeBin] += 1;
230     }
231     }
232     break;
233 tim 2002
234 gezelter 2016 default:
235     return NULL;
236    
237     }
238 tim 2002 }
239    
240    
241     void CorrelationFunction::updateFrame(int frame){
242     Molecule* mol;
243     RigidBody* rb;
244     SimInfo::MoleculeIterator mi;
245     Molecule::RigidBodyIterator rbIter;
246    
247     /** @todo need improvement */
248     if (storageLayout_ & DataStorage::dslPosition) {
249     for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
250    
251     //change the positions of atoms which belong to the rigidbodies
252     for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
253     rb->updateAtoms(frame);
254     }
255     }
256     }
257    
258     if (storageLayout_ & DataStorage::dslVelocity) {
259     for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
260    
261     //change the positions of atoms which belong to the rigidbodies
262     for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
263     rb->updateAtomVel(frame);
264     }
265     }
266    
267     }
268    
269     }
270    
271    
272     void CorrelationFunction::preCorrelate() {
273     std::fill(histogram_.begin(), histogram_.end(), 0.0);
274     std::fill(count_.begin(), count_.end(), 0);
275     }
276    
277     void CorrelationFunction::postCorrelate() {
278     for (int i =0 ; i < nTimeBins_; ++i) {
279     if (count_[i] > 0) {
280 tim 2008 histogram_[i] /= count_[i];
281 tim 2002 }
282     }
283     }
284    
285    
286     void CorrelationFunction::writeCorrelate() {
287     std::ofstream ofs(outputFilename_.c_str());
288    
289     if (ofs.is_open()) {
290    
291     ofs << "#" << getCorrFuncType() << "\n";
292 tim 2008 ofs << "#selection script1: \"" << selectionScript1_ <<"\"\tselection script2: \"" << selectionScript2_ << "\"\n";
293 tim 2002 ofs << "#extra information: " << extra_ << "\n";
294     ofs << "#time\tcorrVal\n";
295    
296     for (int i = 0; i < nTimeBins_; ++i) {
297     ofs << time_[i] << "\t" << histogram_[i] << "\n";
298     }
299    
300     } else {
301     sprintf(painCave.errMsg,
302     "CorrelationFunction::writeCorrelate Error: fail to open %s\n", outputFilename_.c_str());
303     painCave.isFatal = 1;
304     simError();
305     }
306    
307     ofs.close();
308     }
309    
310     }