ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/dynamicProps/EnergyCorrFunc.cpp
Revision: 2071
Committed: Sat Mar 7 21:41:51 2015 UTC (10 years, 5 months ago) by gezelter
File size: 9568 byte(s)
Log Message:
Reducing the number of warnings when using g++ to compile.

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 gezelter 1879 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1782 * [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 1596 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 1596 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 1609 SimInfo::MoleculeIterator mi1;
84     SimInfo::MoleculeIterator mi2;
85     Molecule* mol1;
86     Molecule* mol2;
87     Molecule::AtomIterator ai1;
88     Molecule::AtomIterator ai2;
89     Atom* atom1;
90     Atom* atom2;
91     std::vector<RealType> particleEnergies1;
92     std::vector<RealType> particleEnergies2;
93     std::vector<Vector3d> atomPositions1;
94     std::vector<Vector3d> atomPositions2;
95     int thisAtom1, thisAtom2;
96    
97 chuckv 1246 Snapshot* snapshot1 = bsMan_->getSnapshot(frame1);
98     Snapshot* snapshot2 = bsMan_->getSnapshot(frame2);
99     assert(snapshot1 && snapshot2);
100    
101     RealType time1 = snapshot1->getTime();
102     RealType time2 = snapshot2->getTime();
103    
104     int timeBin = int ((time2 - time1) /deltaTime_ + 0.5);
105    
106 gezelter 1609 // now do the correlation
107    
108     particleEnergies1 = E_a_[frame1];
109     particleEnergies2 = E_a_[frame2];
110    
111     updateFrame(frame1);
112     atomPositions1.clear();
113     for (mol1 = info_->beginMolecule(mi1); mol1 != NULL;
114     mol1 = info_->nextMolecule(mi1)) {
115     for(atom1 = mol1->beginAtom(ai1); atom1 != NULL;
116     atom1 = mol1->nextAtom(ai1)) {
117     atomPositions1.push_back(atom1->getPos(frame1));
118     }
119     }
120     updateFrame(frame2);
121     atomPositions2.clear();
122     for (mol2 = info_->beginMolecule(mi2); mol2 != NULL;
123     mol2 = info_->nextMolecule(mi2)) {
124     for(atom2 = mol2->beginAtom(ai2); atom2 != NULL;
125     atom2 = mol2->nextAtom(ai2)) {
126     atomPositions2.push_back(atom2->getPos(frame2));
127     }
128     }
129    
130     thisAtom1 = 0;
131    
132     for (mol1 = info_->beginMolecule(mi1); mol1 != NULL;
133     mol1 = info_->nextMolecule(mi1)) {
134     for(atom1 = mol1->beginAtom(ai1); atom1 != NULL;
135     atom1 = mol1->nextAtom(ai1)) {
136    
137     Vector3d r1 = atomPositions1[thisAtom1];
138     RealType energy1 = particleEnergies1[thisAtom1] - AvgE_a_[thisAtom1];
139    
140     thisAtom2 = 0;
141    
142     for (mol2 = info_->beginMolecule(mi2); mol2 != NULL;
143     mol2 = info_->nextMolecule(mi2)) {
144     for(atom2 = mol2->beginAtom(ai2); atom2 != NULL;
145     atom2 = mol2->nextAtom(ai2)) {
146    
147     Vector3d r2 = atomPositions2[thisAtom2];
148     RealType energy2 = particleEnergies2[thisAtom2] - AvgE_a_[thisAtom2];
149    
150     Vector3d deltaPos = (r2-r1);
151     RealType Eprod = energy2*energy1;
152    
153     histogram_[timeBin][0] += deltaPos.x()*deltaPos.x() * Eprod;
154     histogram_[timeBin][1] += deltaPos.y()*deltaPos.y() * Eprod;
155     histogram_[timeBin][2] += deltaPos.z()*deltaPos.z() * Eprod;
156    
157     thisAtom2++;
158     }
159     }
160    
161     thisAtom1++;
162     }
163     }
164 gezelter 1313
165     count_[timeBin]++;
166    
167 chuckv 1246 }
168    
169     void EnergyCorrFunc::postCorrelate() {
170     for (int i =0 ; i < nTimeBins_; ++i) {
171     if (count_[i] > 0) {
172     histogram_[i] /= count_[i];
173     }
174     }
175     }
176    
177     void EnergyCorrFunc::preCorrelate() {
178     // Fill the histogram with empty 3x3 matrices:
179     std::fill(histogram_.begin(), histogram_.end(), 0.0);
180     // count array set to zero
181     std::fill(count_.begin(), count_.end(), 0);
182    
183     SimInfo::MoleculeIterator mi;
184     Molecule* mol;
185     Molecule::AtomIterator ai;
186     Atom* atom;
187     std::vector<RealType > particleEnergies;
188    
189     // We'll need the force manager to compute forces for the average pressure
190     ForceManager* forceMan = new ForceManager(info_);
191    
192     // dump files can be enormous, so read them in block-by-block:
193     int nblocks = bsMan_->getNBlocks();
194     bool firsttime = true;
195     for (int i = 0; i < nblocks; ++i) {
196     bsMan_->loadBlock(i);
197     assert(bsMan_->isBlockActive(i));
198     SnapshotBlock block1 = bsMan_->getSnapshotBlock(i);
199     for (int j = block1.first; j < block1.second; ++j) {
200    
201     // update the positions and velocities of the atoms belonging
202     // to rigid bodies:
203 gezelter 1249
204 chuckv 1246 updateFrame(j);
205 gezelter 1249
206 chuckv 1246 // do the forces:
207 gezelter 1249
208 gezelter 1464 forceMan->calcForces();
209 gezelter 1249
210 chuckv 1246 int index = 0;
211 gezelter 1249
212 chuckv 1246 for (mol = info_->beginMolecule(mi); mol != NULL;
213 gezelter 1249 mol = info_->nextMolecule(mi)) {
214 chuckv 1246 for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
215     RealType mass = atom->getMass();
216 gezelter 1609 Vector3d vel = atom->getVel(j);
217 gezelter 1249 RealType kinetic = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
218 gezelter 1390 vel[2]*vel[2]) / PhysicalConstants::energyConvert;
219 gezelter 1609 RealType potential = atom->getParticlePot(j);
220 gezelter 1313 RealType eatom = (kinetic + potential)/2.0;
221 chuckv 1246 particleEnergies.push_back(eatom);
222     if(firsttime)
223 gezelter 1249 {
224     AvgE_a_.push_back(eatom);
225     } else {
226 chuckv 1246 /* We assume the the number of atoms does not change.*/
227     AvgE_a_[index] += eatom;
228     }
229     index++;
230     }
231     }
232     firsttime = false;
233     E_a_.push_back(particleEnergies);
234     }
235 chuckv 1247
236 chuckv 1246 bsMan_->unloadBlock(i);
237     }
238    
239 gezelter 1249 int nframes = bsMan_->getNFrames();
240 gezelter 1782 for (unsigned int i = 0; i < AvgE_a_.size(); i++){
241 gezelter 1249 AvgE_a_[i] /= nframes;
242     }
243 chuckv 1246
244 gezelter 1609 }
245    
246 chuckv 1246
247 gezelter 1249
248 chuckv 1246 void EnergyCorrFunc::writeCorrelate() {
249     std::ofstream ofs(getOutputFileName().c_str());
250    
251     if (ofs.is_open()) {
252    
253     ofs << "#" << getCorrFuncType() << "\n";
254     ofs << "#time\tK_x\tK_y\tK_z\n";
255    
256     for (int i = 0; i < nTimeBins_; ++i) {
257     ofs << time_[i] << "\t" <<
258     histogram_[i].x() << "\t" <<
259     histogram_[i].y() << "\t" <<
260     histogram_[i].z() << "\t" << "\n";
261     }
262    
263     } else {
264     sprintf(painCave.errMsg,
265     "EnergyCorrFunc::writeCorrelate Error: fail to open %s\n", getOutputFileName().c_str());
266     painCave.isFatal = 1;
267     simError();
268     }
269    
270     ofs.close();
271    
272     }
273    
274     }

Properties

Name Value
svn:keywords Author Id Revision Date