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