ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/dynamicProps/TimeCorrFunc.cpp
Revision: 1629
Committed: Wed Sep 14 21:15:17 2011 UTC (13 years, 8 months ago) by gezelter
File size: 8463 byte(s)
Log Message:
Merging changes from old branch into development branch

File Contents

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

Properties

Name Value
svn:executable *
svn:keywords Author Id Revision Date