ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/heatflux/src/applications/dynamicProps/StressCorrFunc.cpp
Revision: 1670
Committed: Fri Jan 27 19:07:19 2012 UTC (14 years, 3 months ago) by chuckv
File size: 8282 byte(s)
Log Message:
Adding experimental branch for calculation of the heatflux based on old version of OpenMD.
This will need to be ported to the development branch later.

File Contents

# User Rev Content
1 chuckv 1565 /*
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. Redistributions of source code must retain the above copyright
10     * notice, this list of conditions and the following disclaimer.
11     *
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * 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     *
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     * [4] Vardeman & Gezelter, in progress (2009).
40     */
41    
42     #include "applications/dynamicProps/StressCorrFunc.hpp"
43     #include "utils/PhysicalConstants.hpp"
44     #include "brains/ForceManager.hpp"
45     #include "brains/Thermo.hpp"
46    
47     namespace OpenMD {
48    
49     // We need all of the positions, velocities, etc. so that we can
50     // recalculate pressures and actions on the fly:
51     StressCorrFunc::StressCorrFunc(SimInfo* info, const std::string& filename,
52     const std::string& sele1,
53 gezelter 1596 const std::string& sele2,
54     long long int memSize)
55 chuckv 1565 : FrameTimeCorrFunc(info, filename, sele1, sele2,
56     DataStorage::dslPosition |
57     DataStorage::dslVelocity |
58 gezelter 1596 DataStorage::dslForce,
59     memSize){
60 chuckv 1565
61     setCorrFuncType("StressCorrFunc");
62     setOutputName(getPrefix(dumpFilename_) + ".action");
63     histogram_.resize(nTimeBins_);
64     count_.resize(nTimeBins_);
65     }
66    
67     void StressCorrFunc::correlateFrames(int frame1, int frame2) {
68     Snapshot* snapshot1 = bsMan_->getSnapshot(frame1);
69     Snapshot* snapshot2 = bsMan_->getSnapshot(frame2);
70     assert(snapshot1 && snapshot2);
71    
72     RealType time1 = snapshot1->getTime();
73     RealType time2 = snapshot2->getTime();
74     RealType vol1 = snapshot1->getVolume();
75     RealType vol2 = snapshot2->getVolume();
76    
77     int timeBin = int ((time2 - time1) /deltaTime_ + 0.5);
78    
79     //std::cerr << "times = " << time1 << " " << time2 << "\n";
80     //std::cerr << "vols = " << vol1 << " " << vol2 << "\n";
81    
82     int i;
83     int j;
84    
85     StuntDouble* sd1;
86    
87     Mat3x3d actionTensor1(0.0);
88     //std::cerr << "at1 = " << actionTensor1 << "\n";
89     Mat3x3d actionTensor2(0.0);
90     //std::cerr << "at2 = " << actionTensor2 << "\n";
91    
92     for (sd1 = seleMan1_.beginSelected(i); sd1 != NULL;
93     sd1 = seleMan1_.nextSelected(i)) {
94     //std::cerr << "found a SD\n";
95     Vector3d r1 = sd1->getPos(frame1);
96     //std::cerr << "r1 = " << r1 << "\n";
97     Vector3d v1 = sd1->getVel(frame1);
98     //std::cerr << "v1 = " << v1 << "\n";
99     Vector3d r2 = sd1->getPos(frame2);
100     //std::cerr << "r2 = " << r2 << "\n";
101     Vector3d v2 = sd1->getVel(frame2);
102     //std::cerr << "v2 = " << v2 << "\n";
103    
104     RealType m = sd1->getMass();
105    
106     //std::cerr << "m = " << m << "\n";
107    
108     actionTensor1 += m*outProduct(r1, v1);
109     actionTensor2 += m*outProduct(r2, v2);
110     }
111    
112     actionTensor1 /= vol1;
113     //std::cerr << "at1 = " << actionTensor1 << "\n";
114     actionTensor2 /= vol2;
115     //std::cerr << "at2 = " << actionTensor2 << "\n";
116    
117     Mat3x3d corrTensor(0.0);
118     //std::cerr << "ct = " << corrTensor << "\n";
119     RealType thisTerm;
120    
121     for (i = 0; i < 3; i++) {
122     for (j = 0; j < 3; j++) {
123    
124     //std::cerr << "i, j = " << i << " " << j << "\n";
125     if (i == j) {
126     thisTerm = (actionTensor2(i, j) - actionTensor1(i, j) - avePress_ *(time2-time1));
127     std::cerr << "at1, at2 = " << actionTensor1(i,j) << " " << actionTensor2(i,j) << " p = " << avePress_ << "\n";
128     } else {
129     thisTerm = (actionTensor2(i, j) - actionTensor1(i, j));
130     }
131    
132     //std::cerr << "thisTerm = " << thisTerm << "\n";
133     corrTensor(i, j) += thisTerm * thisTerm;
134     }
135     }
136    
137     //std::cerr << "ct = " << corrTensor << "\n";
138     //std::cerr << "hist = " << histogram_[timeBin] << "\n";
139     histogram_[timeBin] += corrTensor;
140     count_[timeBin]++;
141    
142     }
143    
144     void StressCorrFunc::postCorrelate() {
145     for (int i =0 ; i < nTimeBins_; ++i) {
146     if (count_[i] > 0) {
147     histogram_[i] /= count_[i];
148     }
149     }
150     }
151    
152    
153     void StressCorrFunc::preCorrelate() {
154     // Fill the histogram with empty 3x3 matrices:
155     std::fill(histogram_.begin(), histogram_.end(), Mat3x3d(0.0));
156     // count array set to zero
157     std::fill(count_.begin(), count_.end(), 0);
158    
159     SimInfo::MoleculeIterator mi;
160     Molecule* mol;
161     Molecule::AtomIterator ai;
162     Atom* atom;
163    
164     // We'll need the force manager to compute forces for the average pressure
165     ForceManager* forceMan = new ForceManager(info_);
166     forceMan->init();
167    
168     // We'll need thermo to compute the pressures from the virial
169     Thermo* thermo = new Thermo(info_);
170    
171     // prepare the averages
172     RealType pSum = 0.0;
173     RealType vSum = 0.0;
174     int nsamp = 0;
175    
176     // dump files can be enormous, so read them in block-by-block:
177     int nblocks = bsMan_->getNBlocks();
178     for (int i = 0; i < nblocks; ++i) {
179     std::cerr << "block = " << i << "\n";
180     bsMan_->loadBlock(i);
181     assert(bsMan_->isBlockActive(i));
182     SnapshotBlock block1 = bsMan_->getSnapshotBlock(i);
183     for (int j = block1.first; j < block1.second; ++j) {
184    
185     // go snapshot-by-snapshot through this block:
186     Snapshot* snap = bsMan_->getSnapshot(j);
187    
188     // update the positions and velocities of the atoms belonging
189     // to rigid bodies:
190    
191     updateFrame(j);
192    
193     // do the forces:
194 chuckv 1566 //forceMan->calcForces(true, true);
195 chuckv 1565 // call thermo to get the pressure and volume.
196     pSum += thermo->getPressure();
197     vSum += thermo->getVolume();
198     nsamp++;
199    
200     }
201     bsMan_->unloadBlock(i);
202     }
203    
204     avePress_ = pSum / ( PhysicalConstants::pressureConvert * (RealType)nsamp);
205     aveVol_ = vSum / (RealType)nsamp;
206     std::cout << "pAve = " << avePress_ << " vAve = " << aveVol_ << "\n";
207     }
208    
209    
210     void StressCorrFunc::writeCorrelate() {
211     std::ofstream ofs(getOutputFileName().c_str());
212    
213     if (ofs.is_open()) {
214    
215     ofs << "#" << getCorrFuncType() << "\n";
216     ofs << "#time\tcorrTensor\txx\txy\txz\tyx\tyy\tyz\tzx\tzy\tzz\n";
217    
218     for (int i = 0; i < nTimeBins_; ++i) {
219     ofs << time_[i] << "\t" <<
220     histogram_[i](0,0) << "\t" <<
221     histogram_[i](0,1) << "\t" <<
222     histogram_[i](0,2) << "\t" <<
223     histogram_[i](1,0) << "\t" <<
224     histogram_[i](1,1) << "\t" <<
225     histogram_[i](1,2) << "\t" <<
226     histogram_[i](2,0) << "\t" <<
227     histogram_[i](2,1) << "\t" <<
228     histogram_[i](2,2) << "\t" << "\n";
229     }
230    
231     } else {
232     sprintf(painCave.errMsg,
233     "StressCorrFunc::writeCorrelate Error: fail to open %s\n", getOutputFileName().c_str());
234     painCave.isFatal = 1;
235     simError();
236     }
237    
238     ofs.close();
239     }
240    
241     }