ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/dynamicProps/EnergyCorrFunc.cpp
Revision: 3404
Committed: Mon Jun 2 02:42:50 2008 UTC (16 years, 3 months ago) by gezelter
File size: 9420 byte(s)
Log Message:
Fixed a bug in BlockSnapshotManager.

File Contents

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