ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/dynamicProps/EnergyCorrFunc.cpp
Revision: 1247
Committed: Fri May 30 14:15:28 2008 UTC (16 years, 11 months ago) by chuckv
Original Path: trunk/src/applications/dynamicProps/EnergyCorrFunc.cpp
File size: 9538 byte(s)
Log Message:
More changes, still broken.

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 chuckv 1247 // //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 1246
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 1247 int junkframe = 0;
157 chuckv 1246 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 chuckv 1247
173 chuckv 1246 // 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     if(firsttime)
188     {
189     AvgE_a_.push_back(eatom);
190     }else{
191     /* We assume the the number of atoms does not change.*/
192     AvgE_a_[index] += eatom;
193     }
194     index++;
195     }
196     }
197     firsttime = false;
198     E_a_.push_back(particleEnergies);
199     }
200 chuckv 1247
201 chuckv 1246 bsMan_->unloadBlock(i);
202     }
203    
204 chuckv 1247 int nframes = bsMan_->getNFrames();
205     for (int i = 0; i < AvgE_a_.size(); i++){
206     AvgE_a_[i] /= nframes;
207     }
208    
209    
210    
211     int frame = 0;
212    
213     // Do it again to compute G^(kappa)(t) for x,y,z
214     for (int i = 0; i < nblocks; ++i) {
215     //std::cerr << "block = " << i << "\n";
216     bsMan_->loadBlock(i);
217    
218     assert(bsMan_->isBlockActive(i));
219     SnapshotBlock block1 = bsMan_->getSnapshotBlock(i);
220     for (int j = block1.first; j < block1.second; ++j) {
221    
222     // go snapshot-by-snapshot through this block:
223     Snapshot* snap = bsMan_->getSnapshot(j);
224    
225     updateFrame(j);
226     particleEnergies = E_a_[frame];
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    
236     Vector3d pos = atom->getPos();
237    
238    
239    
240     G_t[0] += (particleEnergies[thisAtom]-AvgE_a_[thisAtom]);
241     G_t[1] += (particleEnergies[thisAtom]-AvgE_a_[thisAtom]);
242     G_t[2] += (particleEnergies[thisAtom]-AvgE_a_[thisAtom]);
243    
244     thisAtom++;
245     }
246     }
247    
248    
249     G_t_.push_back(G_t);
250     frame++;
251     std::cerr <<"Frame: " << frame <<"\t" << G_t << std::endl;
252     }
253    
254    
255    
256     bsMan_->unloadBlock(i);
257     }
258 chuckv 1246
259    
260    
261    
262     }
263    
264    
265     void EnergyCorrFunc::writeCorrelate() {
266     std::ofstream ofs(getOutputFileName().c_str());
267    
268     if (ofs.is_open()) {
269    
270     ofs << "#" << getCorrFuncType() << "\n";
271     ofs << "#time\tK_x\tK_y\tK_z\n";
272    
273     for (int i = 0; i < nTimeBins_; ++i) {
274     ofs << time_[i] << "\t" <<
275     histogram_[i].x() << "\t" <<
276     histogram_[i].y() << "\t" <<
277     histogram_[i].z() << "\t" << "\n";
278     }
279    
280     } else {
281     sprintf(painCave.errMsg,
282     "EnergyCorrFunc::writeCorrelate Error: fail to open %s\n", getOutputFileName().c_str());
283     painCave.isFatal = 1;
284     simError();
285     }
286    
287     ofs.close();
288    
289     }
290    
291     }