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 |
|
|
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 |
|
|
int timeBin = int ((time2 - time1) /deltaTime_ + 0.5); |
88 |
|
|
|
89 |
gezelter |
1313 |
Vector3d G_t_frame1 = G_t_[frame1]; |
90 |
|
|
Vector3d G_t_frame2 = G_t_[frame2]; |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
RealType diff_x = G_t_frame1.x()-G_t_frame2.x(); |
94 |
|
|
RealType diff_x_sq = diff_x * diff_x; |
95 |
|
|
|
96 |
|
|
RealType diff_y = G_t_frame1.y()-G_t_frame2.y(); |
97 |
|
|
RealType diff_y_sq = diff_y * diff_y; |
98 |
|
|
|
99 |
|
|
RealType diff_z = G_t_frame1.z()-G_t_frame2.z(); |
100 |
|
|
RealType diff_z_sq = diff_z*diff_z; |
101 |
|
|
|
102 |
|
|
histogram_[timeBin][0] += diff_x_sq; |
103 |
|
|
histogram_[timeBin][1] += diff_y_sq; |
104 |
|
|
histogram_[timeBin][2] += diff_z_sq; |
105 |
|
|
|
106 |
|
|
count_[timeBin]++; |
107 |
|
|
|
108 |
chuckv |
1246 |
} |
109 |
|
|
|
110 |
|
|
void EnergyCorrFunc::postCorrelate() { |
111 |
|
|
for (int i =0 ; i < nTimeBins_; ++i) { |
112 |
|
|
if (count_[i] > 0) { |
113 |
|
|
histogram_[i] /= count_[i]; |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
void EnergyCorrFunc::preCorrelate() { |
119 |
|
|
// Fill the histogram with empty 3x3 matrices: |
120 |
|
|
std::fill(histogram_.begin(), histogram_.end(), 0.0); |
121 |
|
|
// count array set to zero |
122 |
|
|
std::fill(count_.begin(), count_.end(), 0); |
123 |
|
|
|
124 |
|
|
SimInfo::MoleculeIterator mi; |
125 |
|
|
Molecule::IntegrableObjectIterator mj; |
126 |
|
|
Molecule* mol; |
127 |
|
|
Molecule::AtomIterator ai; |
128 |
|
|
Atom* atom; |
129 |
|
|
std::vector<RealType > particleEnergies; |
130 |
|
|
|
131 |
|
|
// We'll need the force manager to compute forces for the average pressure |
132 |
|
|
ForceManager* forceMan = new ForceManager(info_); |
133 |
|
|
|
134 |
gezelter |
1576 |
forceMan->initialize(); |
135 |
gezelter |
1313 |
|
136 |
chuckv |
1246 |
// We'll need thermo to compute the pressures from the virial |
137 |
|
|
Thermo* thermo = new Thermo(info_); |
138 |
|
|
|
139 |
|
|
// prepare the averages |
140 |
|
|
RealType pSum = 0.0; |
141 |
|
|
RealType vSum = 0.0; |
142 |
|
|
int nsamp = 0; |
143 |
|
|
|
144 |
|
|
// dump files can be enormous, so read them in block-by-block: |
145 |
|
|
int nblocks = bsMan_->getNBlocks(); |
146 |
|
|
bool firsttime = true; |
147 |
chuckv |
1247 |
int junkframe = 0; |
148 |
chuckv |
1246 |
for (int i = 0; i < nblocks; ++i) { |
149 |
|
|
bsMan_->loadBlock(i); |
150 |
|
|
assert(bsMan_->isBlockActive(i)); |
151 |
|
|
SnapshotBlock block1 = bsMan_->getSnapshotBlock(i); |
152 |
|
|
for (int j = block1.first; j < block1.second; ++j) { |
153 |
|
|
|
154 |
|
|
// go snapshot-by-snapshot through this block: |
155 |
|
|
Snapshot* snap = bsMan_->getSnapshot(j); |
156 |
gezelter |
1249 |
|
157 |
chuckv |
1246 |
// update the positions and velocities of the atoms belonging |
158 |
|
|
// to rigid bodies: |
159 |
gezelter |
1249 |
|
160 |
chuckv |
1246 |
updateFrame(j); |
161 |
gezelter |
1249 |
|
162 |
chuckv |
1246 |
// do the forces: |
163 |
gezelter |
1249 |
|
164 |
gezelter |
1464 |
forceMan->calcForces(); |
165 |
gezelter |
1249 |
|
166 |
chuckv |
1246 |
int index = 0; |
167 |
gezelter |
1249 |
|
168 |
chuckv |
1246 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
169 |
gezelter |
1249 |
mol = info_->nextMolecule(mi)) { |
170 |
chuckv |
1246 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
171 |
|
|
RealType mass = atom->getMass(); |
172 |
|
|
Vector3d vel = atom->getVel(); |
173 |
gezelter |
1249 |
RealType kinetic = mass * (vel[0]*vel[0] + vel[1]*vel[1] + |
174 |
gezelter |
1390 |
vel[2]*vel[2]) / PhysicalConstants::energyConvert; |
175 |
gezelter |
1313 |
RealType potential = atom->getParticlePot(); |
176 |
|
|
RealType eatom = (kinetic + potential)/2.0; |
177 |
chuckv |
1246 |
particleEnergies.push_back(eatom); |
178 |
|
|
if(firsttime) |
179 |
gezelter |
1249 |
{ |
180 |
|
|
AvgE_a_.push_back(eatom); |
181 |
|
|
} else { |
182 |
chuckv |
1246 |
/* We assume the the number of atoms does not change.*/ |
183 |
|
|
AvgE_a_[index] += eatom; |
184 |
|
|
} |
185 |
|
|
index++; |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
firsttime = false; |
189 |
|
|
E_a_.push_back(particleEnergies); |
190 |
|
|
} |
191 |
chuckv |
1247 |
|
192 |
chuckv |
1246 |
bsMan_->unloadBlock(i); |
193 |
|
|
} |
194 |
|
|
|
195 |
gezelter |
1249 |
int nframes = bsMan_->getNFrames(); |
196 |
|
|
for (int i = 0; i < AvgE_a_.size(); i++){ |
197 |
|
|
AvgE_a_[i] /= nframes; |
198 |
|
|
} |
199 |
chuckv |
1246 |
|
200 |
gezelter |
1249 |
int frame = 0; |
201 |
chuckv |
1246 |
|
202 |
gezelter |
1249 |
// Do it again to compute G^(kappa)(t) for x,y,z |
203 |
|
|
for (int i = 0; i < nblocks; ++i) { |
204 |
|
|
bsMan_->loadBlock(i); |
205 |
|
|
assert(bsMan_->isBlockActive(i)); |
206 |
|
|
SnapshotBlock block1 = bsMan_->getSnapshotBlock(i); |
207 |
|
|
for (int j = block1.first; j < block1.second; ++j) { |
208 |
chuckv |
1246 |
|
209 |
gezelter |
1249 |
// go snapshot-by-snapshot through this block: |
210 |
|
|
Snapshot* snap = bsMan_->getSnapshot(j); |
211 |
|
|
|
212 |
|
|
// update the positions and velocities of the atoms belonging |
213 |
|
|
// to rigid bodies: |
214 |
|
|
|
215 |
|
|
updateFrame(j); |
216 |
|
|
|
217 |
|
|
// this needs to be updated to the frame value: |
218 |
|
|
particleEnergies = E_a_[j]; |
219 |
|
|
|
220 |
|
|
int thisAtom = 0; |
221 |
|
|
Vector3d G_t; |
222 |
|
|
|
223 |
|
|
for (mol = info_->beginMolecule(mi); mol != NULL; |
224 |
|
|
mol = info_->nextMolecule(mi)) { |
225 |
|
|
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
226 |
|
|
|
227 |
|
|
Vector3d pos = atom->getPos(); |
228 |
|
|
|
229 |
|
|
G_t[0] += pos.x()*(particleEnergies[thisAtom]-AvgE_a_[thisAtom]); |
230 |
|
|
G_t[1] += pos.y()*(particleEnergies[thisAtom]-AvgE_a_[thisAtom]); |
231 |
|
|
G_t[2] += pos.z()*(particleEnergies[thisAtom]-AvgE_a_[thisAtom]); |
232 |
|
|
|
233 |
|
|
thisAtom++; |
234 |
|
|
} |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
G_t_.push_back(G_t); |
238 |
gezelter |
1313 |
//std::cerr <<"Frame: " << j <<"\t" << G_t << std::endl; |
239 |
gezelter |
1249 |
} |
240 |
|
|
bsMan_->unloadBlock(i); |
241 |
|
|
} |
242 |
chuckv |
1246 |
} |
243 |
|
|
|
244 |
|
|
|
245 |
|
|
void EnergyCorrFunc::writeCorrelate() { |
246 |
|
|
std::ofstream ofs(getOutputFileName().c_str()); |
247 |
|
|
|
248 |
|
|
if (ofs.is_open()) { |
249 |
|
|
|
250 |
|
|
ofs << "#" << getCorrFuncType() << "\n"; |
251 |
|
|
ofs << "#time\tK_x\tK_y\tK_z\n"; |
252 |
|
|
|
253 |
|
|
for (int i = 0; i < nTimeBins_; ++i) { |
254 |
|
|
ofs << time_[i] << "\t" << |
255 |
|
|
histogram_[i].x() << "\t" << |
256 |
|
|
histogram_[i].y() << "\t" << |
257 |
|
|
histogram_[i].z() << "\t" << "\n"; |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
} else { |
261 |
|
|
sprintf(painCave.errMsg, |
262 |
|
|
"EnergyCorrFunc::writeCorrelate Error: fail to open %s\n", getOutputFileName().c_str()); |
263 |
|
|
painCave.isFatal = 1; |
264 |
|
|
simError(); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
ofs.close(); |
268 |
|
|
|
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
} |