ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/dynamicProps/EnergyCorrFunc.cpp
Revision: 1665
Committed: Tue Nov 22 20:38:56 2011 UTC (13 years, 6 months ago) by gezelter
File size: 10024 byte(s)
Log Message:
updated copyright notices

File Contents

# User Rev Content
1 chuckv 1246 /*
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 chuckv 1246 * notice, this list of conditions and the following disclaimer.
11     *
12 gezelter 1390 * 2. Redistributions in binary form must reproduce the above copyright
13 chuckv 1246 * 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 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 chuckv 1246 */
42    
43     /* Uses the Helfand-moment method for calculating thermal
44     * conductivity using the relation kappa = (N,V)lim(t)->inf 1/(2*k_B*T^2*V*t) <[G_K(t)-G_K(0)]^2>
45     * where G_K is the Helfand moment for thermal conductivity definded as
46     * G_K(t) = sum_{a=1}{^N} x_a(E_a-<E_a>) and E_a is defined to be
47     * E_a = p_2^2/(2*m)+1/2 sum_{b.ne.a} u(r_ab) where p is momentum and u is pot energy for the
48     * particle pair a-b. This routine calculates E_a, <E_a> and does the correlation
49     * <[G_K(t)-G_K(0)]^2>.
50     * See Viscardy et al. JCP 126, 184513 (2007)
51     */
52    
53    
54    
55     #include "applications/dynamicProps/EnergyCorrFunc.hpp"
56 gezelter 1390 #include "utils/PhysicalConstants.hpp"
57 chuckv 1246 #include "brains/ForceManager.hpp"
58     #include "brains/Thermo.hpp"
59    
60 gezelter 1390 namespace OpenMD {
61 chuckv 1246
62     // We need all of the positions, velocities, etc. so that we can
63     // recalculate pressures and actions on the fly:
64     EnergyCorrFunc::EnergyCorrFunc(SimInfo* info, const std::string& filename,
65     const std::string& sele1,
66 gezelter 1629 const std::string& sele2,
67     long long int memSize)
68 chuckv 1246 : FrameTimeCorrFunc(info, filename, sele1, sele2,
69     DataStorage::dslPosition |
70     DataStorage::dslVelocity |
71     DataStorage::dslForce |
72     DataStorage::dslTorque |
73 gezelter 1629 DataStorage::dslParticlePot,
74     memSize){
75 chuckv 1246
76     setCorrFuncType("EnergyCorrFunc");
77     setOutputName(getPrefix(dumpFilename_) + ".moment");
78     histogram_.resize(nTimeBins_);
79     count_.resize(nTimeBins_);
80     }
81    
82     void EnergyCorrFunc::correlateFrames(int frame1, int frame2) {
83 gezelter 1629 SimInfo::MoleculeIterator mi1;
84     SimInfo::MoleculeIterator mi2;
85     Molecule::IntegrableObjectIterator mj1;
86     Molecule::IntegrableObjectIterator mj2;
87     Molecule* mol1;
88     Molecule* mol2;
89     Molecule::AtomIterator ai1;
90     Molecule::AtomIterator ai2;
91     Atom* atom1;
92     Atom* atom2;
93     std::vector<RealType> particleEnergies1;
94     std::vector<RealType> particleEnergies2;
95     std::vector<Vector3d> atomPositions1;
96     std::vector<Vector3d> atomPositions2;
97     int thisAtom1, thisAtom2;
98    
99 chuckv 1246 Snapshot* snapshot1 = bsMan_->getSnapshot(frame1);
100     Snapshot* snapshot2 = bsMan_->getSnapshot(frame2);
101     assert(snapshot1 && snapshot2);
102    
103     RealType time1 = snapshot1->getTime();
104     RealType time2 = snapshot2->getTime();
105    
106     int timeBin = int ((time2 - time1) /deltaTime_ + 0.5);
107    
108 gezelter 1629 // now do the correlation
109    
110     particleEnergies1 = E_a_[frame1];
111     particleEnergies2 = E_a_[frame2];
112    
113     updateFrame(frame1);
114     atomPositions1.clear();
115     for (mol1 = info_->beginMolecule(mi1); mol1 != NULL;
116     mol1 = info_->nextMolecule(mi1)) {
117     for(atom1 = mol1->beginAtom(ai1); atom1 != NULL;
118     atom1 = mol1->nextAtom(ai1)) {
119     atomPositions1.push_back(atom1->getPos(frame1));
120     }
121     }
122     updateFrame(frame2);
123     atomPositions2.clear();
124     for (mol2 = info_->beginMolecule(mi2); mol2 != NULL;
125     mol2 = info_->nextMolecule(mi2)) {
126     for(atom2 = mol2->beginAtom(ai2); atom2 != NULL;
127     atom2 = mol2->nextAtom(ai2)) {
128     atomPositions2.push_back(atom2->getPos(frame2));
129     }
130     }
131    
132     thisAtom1 = 0;
133    
134     for (mol1 = info_->beginMolecule(mi1); mol1 != NULL;
135     mol1 = info_->nextMolecule(mi1)) {
136     for(atom1 = mol1->beginAtom(ai1); atom1 != NULL;
137     atom1 = mol1->nextAtom(ai1)) {
138    
139     Vector3d r1 = atomPositions1[thisAtom1];
140     RealType energy1 = particleEnergies1[thisAtom1] - AvgE_a_[thisAtom1];
141    
142     thisAtom2 = 0;
143    
144     for (mol2 = info_->beginMolecule(mi2); mol2 != NULL;
145     mol2 = info_->nextMolecule(mi2)) {
146     for(atom2 = mol2->beginAtom(ai2); atom2 != NULL;
147     atom2 = mol2->nextAtom(ai2)) {
148    
149     Vector3d r2 = atomPositions2[thisAtom2];
150     RealType energy2 = particleEnergies2[thisAtom2] - AvgE_a_[thisAtom2];
151    
152     Vector3d deltaPos = (r2-r1);
153     RealType Eprod = energy2*energy1;
154    
155     histogram_[timeBin][0] += deltaPos.x()*deltaPos.x() * Eprod;
156     histogram_[timeBin][1] += deltaPos.y()*deltaPos.y() * Eprod;
157     histogram_[timeBin][2] += deltaPos.z()*deltaPos.z() * Eprod;
158    
159     thisAtom2++;
160     }
161     }
162    
163     thisAtom1++;
164     }
165     }
166 gezelter 1313
167     count_[timeBin]++;
168    
169 chuckv 1246 }
170    
171     void EnergyCorrFunc::postCorrelate() {
172     for (int i =0 ; i < nTimeBins_; ++i) {
173     if (count_[i] > 0) {
174     histogram_[i] /= count_[i];
175     }
176     }
177     }
178    
179     void EnergyCorrFunc::preCorrelate() {
180     // Fill the histogram with empty 3x3 matrices:
181     std::fill(histogram_.begin(), histogram_.end(), 0.0);
182     // count array set to zero
183     std::fill(count_.begin(), count_.end(), 0);
184    
185     SimInfo::MoleculeIterator mi;
186     Molecule::IntegrableObjectIterator mj;
187     Molecule* mol;
188     Molecule::AtomIterator ai;
189     Atom* atom;
190     std::vector<RealType > particleEnergies;
191    
192     // We'll need the force manager to compute forces for the average pressure
193     ForceManager* forceMan = new ForceManager(info_);
194    
195     // We'll need thermo to compute the pressures from the virial
196     Thermo* thermo = new Thermo(info_);
197    
198     // prepare the averages
199     RealType pSum = 0.0;
200     RealType vSum = 0.0;
201     int nsamp = 0;
202    
203     // dump files can be enormous, so read them in block-by-block:
204     int nblocks = bsMan_->getNBlocks();
205     bool firsttime = true;
206 chuckv 1247 int junkframe = 0;
207 chuckv 1246 for (int i = 0; i < nblocks; ++i) {
208     bsMan_->loadBlock(i);
209     assert(bsMan_->isBlockActive(i));
210     SnapshotBlock block1 = bsMan_->getSnapshotBlock(i);
211     for (int j = block1.first; j < block1.second; ++j) {
212    
213     // go snapshot-by-snapshot through this block:
214     Snapshot* snap = bsMan_->getSnapshot(j);
215 gezelter 1249
216 chuckv 1246 // update the positions and velocities of the atoms belonging
217     // to rigid bodies:
218 gezelter 1249
219 chuckv 1246 updateFrame(j);
220 gezelter 1249
221 chuckv 1246 // do the forces:
222 gezelter 1249
223 gezelter 1464 forceMan->calcForces();
224 gezelter 1249
225 chuckv 1246 int index = 0;
226 gezelter 1249
227 chuckv 1246 for (mol = info_->beginMolecule(mi); mol != NULL;
228 gezelter 1249 mol = info_->nextMolecule(mi)) {
229 chuckv 1246 for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
230     RealType mass = atom->getMass();
231 gezelter 1629 Vector3d vel = atom->getVel(j);
232 gezelter 1249 RealType kinetic = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
233 gezelter 1390 vel[2]*vel[2]) / PhysicalConstants::energyConvert;
234 gezelter 1629 RealType potential = atom->getParticlePot(j);
235 gezelter 1313 RealType eatom = (kinetic + potential)/2.0;
236 chuckv 1246 particleEnergies.push_back(eatom);
237     if(firsttime)
238 gezelter 1249 {
239     AvgE_a_.push_back(eatom);
240     } else {
241 chuckv 1246 /* We assume the the number of atoms does not change.*/
242     AvgE_a_[index] += eatom;
243     }
244     index++;
245     }
246     }
247     firsttime = false;
248     E_a_.push_back(particleEnergies);
249     }
250 chuckv 1247
251 chuckv 1246 bsMan_->unloadBlock(i);
252     }
253    
254 gezelter 1249 int nframes = bsMan_->getNFrames();
255     for (int i = 0; i < AvgE_a_.size(); i++){
256     AvgE_a_[i] /= nframes;
257     }
258 chuckv 1246
259 gezelter 1629 }
260    
261 chuckv 1246
262 gezelter 1249
263 chuckv 1246 void EnergyCorrFunc::writeCorrelate() {
264     std::ofstream ofs(getOutputFileName().c_str());
265    
266     if (ofs.is_open()) {
267    
268     ofs << "#" << getCorrFuncType() << "\n";
269     ofs << "#time\tK_x\tK_y\tK_z\n";
270    
271     for (int i = 0; i < nTimeBins_; ++i) {
272     ofs << time_[i] << "\t" <<
273     histogram_[i].x() << "\t" <<
274     histogram_[i].y() << "\t" <<
275     histogram_[i].z() << "\t" << "\n";
276     }
277    
278     } else {
279     sprintf(painCave.errMsg,
280     "EnergyCorrFunc::writeCorrelate Error: fail to open %s\n", getOutputFileName().c_str());
281     painCave.isFatal = 1;
282     simError();
283     }
284    
285     ofs.close();
286    
287     }
288    
289     }

Properties

Name Value
svn:keywords Author Id Revision Date