ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/dynamicProps/EnergyCorrFunc.cpp
Revision: 1246
Committed: Tue May 27 21:24:40 2008 UTC (17 years ago) by chuckv
Original Path: trunk/src/applications/dynamicProps/EnergyCorrFunc.cpp
File size: 9188 byte(s)
Log Message:
Files for Hefland moment.

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     * 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     //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    
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    
157     for (int i = 0; i < nblocks; ++i) {
158     //std::cerr << "block = " << i << "\n";
159     bsMan_->loadBlock(i);
160     assert(bsMan_->isBlockActive(i));
161     SnapshotBlock block1 = bsMan_->getSnapshotBlock(i);
162     for (int j = block1.first; j < block1.second; ++j) {
163    
164     // go snapshot-by-snapshot through this block:
165     Snapshot* snap = bsMan_->getSnapshot(j);
166    
167     // update the positions and velocities of the atoms belonging
168     // to rigid bodies:
169    
170     updateFrame(j);
171    
172    
173     // do the forces:
174    
175     forceMan->calcForces(true, true);
176    
177     int index = 0;
178    
179     for (mol = info_->beginMolecule(mi); mol != NULL;
180     mol = info_->nextMolecule(mi)) {
181     for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
182     RealType mass = atom->getMass();
183     Vector3d vel = atom->getVel();
184     RealType kinetic = mass * (vel[0]*vel[0] + vel[1]*vel[1] + vel[2]*vel[2]);
185     RealType eatom = (kinetic + atom->getParticlePot())/2.0;
186     particleEnergies.push_back(eatom);
187    
188     if(firsttime)
189     {
190     AvgE_a_.push_back(eatom);
191     }else{
192     /* We assume the the number of atoms does not change.*/
193     AvgE_a_[index] += eatom;
194     }
195     index++;
196     }
197     }
198     firsttime = false;
199     E_a_.push_back(particleEnergies);
200    
201     }
202     bsMan_->unloadBlock(i);
203     }
204    
205     int nframes = bsMan_->getNFrames();
206     for (int i = 0; i < AvgE_a_.size(); i++){
207     AvgE_a_[i] /= nframes;
208     }
209    
210    
211    
212     int frame = 0;
213    
214     // Do it again to compute G^(kappa)(t) for x,y,z
215     for (int i = 0; i < nblocks; ++i) {
216     //std::cerr << "block = " << i << "\n";
217     bsMan_->loadBlock(i);
218    
219     assert(bsMan_->isBlockActive(i));
220     SnapshotBlock block1 = bsMan_->getSnapshotBlock(i);
221     for (int j = block1.first; j < block1.second; ++j) {
222    
223     // go snapshot-by-snapshot through this block:
224     Snapshot* snap = bsMan_->getSnapshot(j);
225     particleEnergies = E_a_[frame];
226    
227     int thisAtom = 0;
228     Vector3d G_t;
229    
230     for (mol = info_->beginMolecule(mi); mol != NULL;
231     mol = info_->nextMolecule(mi)) {
232     for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
233    
234    
235     Vector3d pos = atom->getPos();
236    
237    
238    
239     /* G_t[0] += pos.x()*(particleEnergies[thisAtom]-AvgE_a_[thisAtom]);
240     G_t[1] += pos.y()*(particleEnergies[thisAtom]-AvgE_a_[thisAtom]);
241     G_t[2] += pos.z()*(particleEnergies[thisAtom]-AvgE_a_[thisAtom]);
242     */
243     thisAtom++;
244     }
245     }
246    
247    
248     G_t_.push_back(G_t);
249     frame++;
250     }
251    
252    
253    
254     bsMan_->unloadBlock(i);
255     }
256    
257    
258    
259    
260     }
261    
262    
263     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     }