ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/integrators/RNEMD.cpp
Revision: 1339
Committed: Thu Apr 23 18:31:05 2009 UTC (16 years ago) by gezelter
File size: 12603 byte(s)
Log Message:
bug fix in DLM, added temperature profiles and uniform bins to RNEMD

File Contents

# Content
1 /*
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 #include "integrators/RNEMD.hpp"
43 #include "math/Vector3.hpp"
44 #include "math/SquareMatrix3.hpp"
45 #include "primitives/Molecule.hpp"
46 #include "primitives/StuntDouble.hpp"
47 #include "utils/OOPSEConstant.hpp"
48 #include "utils/Tuple.hpp"
49
50 #ifndef IS_MPI
51 #include "math/SeqRandNumGen.hpp"
52 #else
53 #include "math/ParallelRandNumGen.hpp"
54 #endif
55
56 /* Remove me after testing*/
57 /*
58 #include <cstdio>
59 #include <iostream>
60 */
61 /*End remove me*/
62
63 namespace oopse {
64
65 RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info), usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
66
67 int seedValue;
68 Globals * simParams = info->getSimParams();
69
70 stringToEnumMap_["Kinetic"] = rnemdKinetic;
71 stringToEnumMap_["Px"] = rnemdPx;
72 stringToEnumMap_["Py"] = rnemdPy;
73 stringToEnumMap_["Pz"] = rnemdPz;
74 stringToEnumMap_["Unknown"] = rnemdUnknown;
75
76 rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
77
78 std::cerr << "calling evaluator with " << rnemdObjectSelection_ << "\n";
79 evaluator_.loadScriptString(rnemdObjectSelection_);
80 std::cerr << "done\n";
81
82 const std::string st = simParams->getRNEMD_swapType();
83
84 std::map<std::string, RNEMDTypeEnum>::iterator i;
85 i = stringToEnumMap_.find(st);
86 rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
87
88 set_RNEMD_swapTime(simParams->getRNEMD_swapTime());
89 set_RNEMD_nBins(simParams->getRNEMD_nBins());
90 exchangeSum_ = 0.0;
91 counter_ = 0; //added by shenyu
92 //profile_.open("profile", std::ios::out);
93
94 #ifndef IS_MPI
95 if (simParams->haveSeed()) {
96 seedValue = simParams->getSeed();
97 randNumGen_ = new SeqRandNumGen(seedValue);
98 }else {
99 randNumGen_ = new SeqRandNumGen();
100 }
101 #else
102 if (simParams->haveSeed()) {
103 seedValue = simParams->getSeed();
104 randNumGen_ = new ParallelRandNumGen(seedValue);
105 }else {
106 randNumGen_ = new ParallelRandNumGen();
107 }
108 #endif
109 }
110
111 RNEMD::~RNEMD() {
112 delete randNumGen_;
113 //profile_.close();
114 }
115
116 void RNEMD::doSwap() {
117 //std::cerr << "in RNEMD!\n";
118 //std::cerr << "nBins = " << nBins_ << "\n";
119 int midBin = nBins_ / 2;
120 //std::cerr << "midBin = " << midBin << "\n";
121 //std::cerr << "swapTime = " << swapTime_ << "\n";
122 //std::cerr << "swapType = " << rnemdType_ << "\n";
123 //std::cerr << "selection = " << rnemdObjectSelection_ << "\n";
124
125 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
126 Mat3x3d hmat = currentSnap_->getHmat();
127
128 //std::cerr << "hmat = " << hmat << "\n";
129
130 seleMan_.setSelectionSet(evaluator_.evaluate());
131
132 //std::cerr << "selectionCount = " << seleMan_.getSelectionCount() << "\n\n";
133
134 int selei;
135 StuntDouble* sd;
136 int idx;
137
138 RealType min_val;
139 bool min_found = false;
140 StuntDouble* min_sd;
141
142 RealType max_val;
143 bool max_found = false;
144 StuntDouble* max_sd;
145
146 for (sd = seleMan_.beginSelected(selei); sd != NULL;
147 sd = seleMan_.nextSelected(selei)) {
148
149 idx = sd->getLocalIndex();
150
151 Vector3d pos = sd->getPos();
152
153 // wrap the stuntdouble's position back into the box:
154
155 if (usePeriodicBoundaryConditions_)
156 currentSnap_->wrapVector(pos);
157
158 // which bin is this stuntdouble in?
159 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
160
161 int binNo = int((nBins_-1) * (pos.z() + 0.5*hmat(2,2)) / hmat(2,2));
162
163 //std::cerr << "pos.z() = " << pos.z() << " bin = " << binNo << "\n";
164
165 // if we're in bin 0 or the middleBin
166 if (binNo == 0 || binNo == midBin) {
167
168 RealType mass = sd->getMass();
169 Vector3d vel = sd->getVel();
170 RealType value;
171
172 switch(rnemdType_) {
173 case rnemdKinetic :
174
175 value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
176 vel[2]*vel[2]);
177
178 if (sd->isDirectional()) {
179 Vector3d angMom = sd->getJ();
180 Mat3x3d I = sd->getI();
181
182 if (sd->isLinear()) {
183 int i = sd->linearAxis();
184 int j = (i + 1) % 3;
185 int k = (i + 2) % 3;
186 value += angMom[j] * angMom[j] / I(j, j) +
187 angMom[k] * angMom[k] / I(k, k);
188 } else {
189 value += angMom[0]*angMom[0]/I(0, 0)
190 + angMom[1]*angMom[1]/I(1, 1)
191 + angMom[2]*angMom[2]/I(2, 2);
192 }
193 }
194 value = value * 0.5 / OOPSEConstant::energyConvert;
195 break;
196 case rnemdPx :
197 value = mass * vel[0];
198 break;
199 case rnemdPy :
200 value = mass * vel[1];
201 break;
202 case rnemdPz :
203 value = mass * vel[2];
204 break;
205 case rnemdUnknown :
206 default :
207 break;
208 }
209
210 if (binNo == 0) {
211 if (!min_found) {
212 min_val = value;
213 min_sd = sd;
214 min_found = true;
215 } else {
216 if (min_val > value) {
217 min_val = value;
218 min_sd = sd;
219 }
220 }
221 } else {
222 if (!max_found) {
223 max_val = value;
224 max_sd = sd;
225 max_found = true;
226 } else {
227 if (max_val < value) {
228 max_val = value;
229 max_sd = sd;
230 }
231 }
232 }
233 }
234 }
235 //std::cerr << "smallest value = " << min_val << "\n";
236 //std::cerr << "largest value = " << max_val << "\n";
237
238 // missing: swap information in parallel
239
240 if (max_found && min_found) {
241 if (min_val< max_val) {
242 Vector3d min_vel = min_sd->getVel();
243 Vector3d max_vel = max_sd->getVel();
244 RealType temp_vel;
245 switch(rnemdType_) {
246 case rnemdKinetic :
247 min_sd->setVel(max_vel);
248 max_sd->setVel(min_vel);
249 if (min_sd->isDirectional() && max_sd->isDirectional()) {
250 Vector3d min_angMom = min_sd->getJ();
251 Vector3d max_angMom = max_sd->getJ();
252 min_sd->setJ(max_angMom);
253 max_sd->setJ(min_angMom);
254 }
255 break;
256 case rnemdPx :
257 temp_vel = min_vel.x();
258 min_vel.x() = max_vel.x();
259 max_vel.x() = temp_vel;
260 min_sd->setVel(min_vel);
261 max_sd->setVel(max_vel);
262 break;
263 case rnemdPy :
264 temp_vel = min_vel.y();
265 min_vel.y() = max_vel.y();
266 max_vel.y() = temp_vel;
267 min_sd->setVel(min_vel);
268 max_sd->setVel(max_vel);
269 break;
270 case rnemdPz :
271 temp_vel = min_vel.z();
272 min_vel.z() = max_vel.z();
273 max_vel.z() = temp_vel;
274 min_sd->setVel(min_vel);
275 max_sd->setVel(max_vel);
276 break;
277 case rnemdUnknown :
278 default :
279 break;
280 }
281 exchangeSum_ += max_val - min_val;
282 } else {
283 std::cerr << "exchange NOT performed.\nmin_val > max_val.\n";
284 }
285 } else {
286 std::cerr << "exchange NOT performed.\none of the two slabs empty.\n";
287 }
288 std::cerr << "exchangeSum = " << exchangeSum_ << "\n";
289 }
290
291 void RNEMD::getStatus() {
292 //std::cerr << "in RNEMD!\n";
293 //std::cerr << "nBins = " << nBins_ << "\n";
294 int midBin = nBins_ / 2;
295 //std::cerr << "midBin = " << midBin << "\n";
296 //std::cerr << "swapTime = " << swapTime_ << "\n";
297 //std::cerr << "exchangeSum = " << exchangeSum_ << "\n";
298 //std::cerr << "swapType = " << rnemdType_ << "\n";
299 //std::cerr << "selection = " << rnemdObjectSelection_ << "\n";
300
301 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
302 Mat3x3d hmat = currentSnap_->getHmat();
303
304 //std::cerr << "hmat = " << hmat << "\n";
305
306 seleMan_.setSelectionSet(evaluator_.evaluate());
307
308 //std::cerr << "selectionCount = " << seleMan_.getSelectionCount() << "\n\n";
309
310 int selei;
311 StuntDouble* sd;
312 int idx;
313 /*
314 RealType min_val;
315 bool min_found = false;
316 StuntDouble* min_sd;
317
318 RealType max_val;
319 bool max_found = false;
320 StuntDouble* max_sd;
321 */
322 std::vector<RealType> valueHist; // keeps track of what's being averaged
323 std::vector<int> valueCount; // keeps track of the number of degrees of
324 // freedom being averaged
325 valueHist.resize(nBins_);
326 valueCount.resize(nBins_);
327 //do they initialize themselves to zero automatically?
328 for (sd = seleMan_.beginSelected(selei); sd != NULL;
329 sd = seleMan_.nextSelected(selei)) {
330
331 idx = sd->getLocalIndex();
332
333 Vector3d pos = sd->getPos();
334
335 // wrap the stuntdouble's position back into the box:
336
337 if (usePeriodicBoundaryConditions_)
338 currentSnap_->wrapVector(pos);
339
340 // which bin is this stuntdouble in?
341 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
342
343 int binNo = int((nBins_-1) * (pos.z()+0.5*hmat(2,2)) / hmat(2,2));
344
345 //std::cerr << "pos.z() = " << pos.z() << " bin = " << binNo << "\n";
346
347 RealType mass = sd->getMass();
348 Vector3d vel = sd->getVel();
349 //std::cerr << "mass = " << mass << " vel = " << vel << "\n";
350 RealType value;
351
352 switch(rnemdType_) {
353 case rnemdKinetic :
354
355 value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
356 vel[2]*vel[2]);
357
358 valueCount[binNo] += 3;
359
360 if (sd->isDirectional()) {
361 Vector3d angMom = sd->getJ();
362 Mat3x3d I = sd->getI();
363
364 if (sd->isLinear()) {
365 int i = sd->linearAxis();
366 int j = (i + 1) % 3;
367 int k = (i + 2) % 3;
368 value += angMom[j] * angMom[j] / I(j, j) +
369 angMom[k] * angMom[k] / I(k, k);
370
371 valueCount[binNo] +=2;
372
373 } else {
374 value += angMom[0]*angMom[0]/I(0, 0)
375 + angMom[1]*angMom[1]/I(1, 1)
376 + angMom[2]*angMom[2]/I(2, 2);
377 valueCount[binNo] +=3;
378
379 }
380 }
381 //std::cerr <<"this value = " << value << "\n";
382 value *= 0.5 / OOPSEConstant::energyConvert; // get it in kcal / mol
383 value *= 2.0 / OOPSEConstant::kb; // convert to temperature
384 //std::cerr <<"this value = " << value << "\n";
385 break;
386 case rnemdPx :
387 value = mass * vel[0];
388 valueCount[binNo]++;
389 break;
390 case rnemdPy :
391 value = mass * vel[1];
392 valueCount[binNo]++;
393 break;
394 case rnemdPz :
395 value = mass * vel[2];
396 valueCount[binNo]++;
397 break;
398 case rnemdUnknown :
399 default :
400 break;
401 }
402 //std::cerr << "bin = " << binNo << " value = " << value ;
403 valueHist[binNo] += value;
404 //std::cerr << " hist = " << valueHist[binNo] << " count = " << valueCount[binNo] << "\n";
405 }
406
407 std::cout << counter_++;
408 for (int j = 0; j < nBins_; j++)
409 std::cout << "\t" << valueHist[j] / (RealType)valueCount[j];
410 std::cout << "\n";
411 }
412 }