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