ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/integrators/RNEMD.cpp
Revision: 1341
Committed: Fri May 8 19:47:05 2009 UTC (15 years, 11 months ago) by skuang
Original Path: trunk/src/integrators/RNEMD.cpp
File size: 12122 byte(s)
Log Message:
Bug fixes and sanity checks for RNEMD (nBins should be even), check to make
sure we aren't selecting inappropriate numbers of integrable Objects

File Contents

# User Rev Content
1 gezelter 1329 /*
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 gezelter 1332 #include "math/Vector3.hpp"
44 gezelter 1329 #include "math/SquareMatrix3.hpp"
45     #include "primitives/Molecule.hpp"
46     #include "primitives/StuntDouble.hpp"
47 gezelter 1332 #include "utils/OOPSEConstant.hpp"
48     #include "utils/Tuple.hpp"
49 gezelter 1329
50     #ifndef IS_MPI
51     #include "math/SeqRandNumGen.hpp"
52     #else
53     #include "math/ParallelRandNumGen.hpp"
54     #endif
55    
56    
57     namespace oopse {
58    
59 gezelter 1334 RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info), usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
60 gezelter 1329
61     int seedValue;
62     Globals * simParams = info->getSimParams();
63 skuang 1330
64     stringToEnumMap_["Kinetic"] = rnemdKinetic;
65     stringToEnumMap_["Px"] = rnemdPx;
66     stringToEnumMap_["Py"] = rnemdPy;
67     stringToEnumMap_["Pz"] = rnemdPz;
68     stringToEnumMap_["Unknown"] = rnemdUnknown;
69    
70 gezelter 1331 rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
71 skuang 1341 evaluator_.loadScriptString(rnemdObjectSelection_);
72     seleMan_.setSelectionSet(evaluator_.evaluate());
73 gezelter 1331
74 skuang 1341
75     // do some sanity checking
76    
77     int selectionCount = seleMan_.getSelectionCount();
78     int nIntegrable = info->getNGlobalIntegrableObjects();
79    
80     if (selectionCount > nIntegrable) {
81     sprintf(painCave.errMsg,
82     "RNEMD warning: The current RNEMD_objectSelection,\n"
83     "\t\t%s\n"
84     "\thas resulted in %d selected objects. However,\n"
85     "\tthe total number of integrable objects in the system\n"
86     "\tis only %d. This is almost certainly not what you want\n"
87     "\tto do. A likely cause of this is forgetting the _RB_0\n"
88     "\tselector in the selection script!\n",
89     rnemdObjectSelection_.c_str(),
90     selectionCount, nIntegrable);
91     painCave.isFatal = 0;
92     simError();
93    
94     }
95 gezelter 1331
96 skuang 1330 const std::string st = simParams->getRNEMD_swapType();
97    
98     std::map<std::string, RNEMDTypeEnum>::iterator i;
99     i = stringToEnumMap_.find(st);
100     rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
101    
102     set_RNEMD_swapTime(simParams->getRNEMD_swapTime());
103     set_RNEMD_nBins(simParams->getRNEMD_nBins());
104     exchangeSum_ = 0.0;
105 skuang 1338 counter_ = 0; //added by shenyu
106    
107 gezelter 1329 #ifndef IS_MPI
108     if (simParams->haveSeed()) {
109     seedValue = simParams->getSeed();
110     randNumGen_ = new SeqRandNumGen(seedValue);
111     }else {
112     randNumGen_ = new SeqRandNumGen();
113     }
114     #else
115     if (simParams->haveSeed()) {
116     seedValue = simParams->getSeed();
117     randNumGen_ = new ParallelRandNumGen(seedValue);
118     }else {
119     randNumGen_ = new ParallelRandNumGen();
120     }
121     #endif
122     }
123    
124     RNEMD::~RNEMD() {
125     delete randNumGen_;
126     }
127 skuang 1330
128 gezelter 1329 void RNEMD::doSwap() {
129 gezelter 1332 int midBin = nBins_ / 2;
130 gezelter 1331
131 gezelter 1332 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
132     Mat3x3d hmat = currentSnap_->getHmat();
133    
134 gezelter 1331 seleMan_.setSelectionSet(evaluator_.evaluate());
135    
136 gezelter 1333 int selei;
137 gezelter 1331 StuntDouble* sd;
138 gezelter 1333 int idx;
139 gezelter 1331
140 skuang 1338 RealType min_val;
141     bool min_found = false;
142     StuntDouble* min_sd;
143    
144     RealType max_val;
145     bool max_found = false;
146     StuntDouble* max_sd;
147    
148 gezelter 1333 for (sd = seleMan_.beginSelected(selei); sd != NULL;
149     sd = seleMan_.nextSelected(selei)) {
150 gezelter 1332
151 gezelter 1333 idx = sd->getLocalIndex();
152    
153 gezelter 1331 Vector3d pos = sd->getPos();
154 gezelter 1332
155     // wrap the stuntdouble's position back into the box:
156    
157 gezelter 1331 if (usePeriodicBoundaryConditions_)
158 gezelter 1332 currentSnap_->wrapVector(pos);
159    
160     // which bin is this stuntdouble in?
161 gezelter 1334 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
162 gezelter 1332
163 skuang 1341 int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
164 gezelter 1332
165 gezelter 1333
166 gezelter 1332 // if we're in bin 0 or the middleBin
167     if (binNo == 0 || binNo == midBin) {
168    
169     RealType mass = sd->getMass();
170     Vector3d vel = sd->getVel();
171     RealType value;
172    
173     switch(rnemdType_) {
174     case rnemdKinetic :
175    
176     value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
177     vel[2]*vel[2]);
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 skuang 1338 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 gezelter 1332 }
234 gezelter 1331 }
235 skuang 1341
236 skuang 1338 // missing: swap information in parallel
237    
238     if (max_found && min_found) {
239     if (min_val< max_val) {
240 skuang 1341
241 skuang 1338 Vector3d min_vel = min_sd->getVel();
242     Vector3d max_vel = max_sd->getVel();
243     RealType temp_vel;
244 skuang 1341
245 skuang 1338 switch(rnemdType_) {
246     case rnemdKinetic :
247     min_sd->setVel(max_vel);
248 skuang 1341 max_sd->setVel(min_vel);
249    
250 skuang 1338 if (min_sd->isDirectional() && max_sd->isDirectional()) {
251     Vector3d min_angMom = min_sd->getJ();
252     Vector3d max_angMom = max_sd->getJ();
253     min_sd->setJ(max_angMom);
254     max_sd->setJ(min_angMom);
255 skuang 1341 }
256 skuang 1338 break;
257     case rnemdPx :
258     temp_vel = min_vel.x();
259     min_vel.x() = max_vel.x();
260     max_vel.x() = temp_vel;
261     min_sd->setVel(min_vel);
262     max_sd->setVel(max_vel);
263     break;
264     case rnemdPy :
265     temp_vel = min_vel.y();
266     min_vel.y() = max_vel.y();
267     max_vel.y() = temp_vel;
268     min_sd->setVel(min_vel);
269     max_sd->setVel(max_vel);
270     break;
271     case rnemdPz :
272     temp_vel = min_vel.z();
273     min_vel.z() = max_vel.z();
274     max_vel.z() = temp_vel;
275     min_sd->setVel(min_vel);
276     max_sd->setVel(max_vel);
277     break;
278     case rnemdUnknown :
279     default :
280     break;
281     }
282     exchangeSum_ += max_val - min_val;
283     } else {
284     std::cerr << "exchange NOT performed.\nmin_val > max_val.\n";
285     }
286     } else {
287     std::cerr << "exchange NOT performed.\none of the two slabs empty.\n";
288     }
289     }
290    
291     void RNEMD::getStatus() {
292    
293     Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
294     Mat3x3d hmat = currentSnap_->getHmat();
295 skuang 1341 Stats& stat = currentSnap_->statData;
296 skuang 1338
297 skuang 1341 stat[Stats::RNEMD_SWAP_TOTAL] = exchangeSum_;
298 skuang 1338
299     seleMan_.setSelectionSet(evaluator_.evaluate());
300    
301     int selei;
302     StuntDouble* sd;
303     int idx;
304    
305 gezelter 1339 std::vector<RealType> valueHist; // keeps track of what's being averaged
306     std::vector<int> valueCount; // keeps track of the number of degrees of
307     // freedom being averaged
308 skuang 1338 valueHist.resize(nBins_);
309     valueCount.resize(nBins_);
310     //do they initialize themselves to zero automatically?
311     for (sd = seleMan_.beginSelected(selei); sd != NULL;
312     sd = seleMan_.nextSelected(selei)) {
313    
314     idx = sd->getLocalIndex();
315    
316     Vector3d pos = sd->getPos();
317    
318     // wrap the stuntdouble's position back into the box:
319    
320     if (usePeriodicBoundaryConditions_)
321     currentSnap_->wrapVector(pos);
322    
323     // which bin is this stuntdouble in?
324     // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
325    
326 skuang 1341 int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
327 skuang 1338
328     //std::cerr << "pos.z() = " << pos.z() << " bin = " << binNo << "\n";
329    
330     RealType mass = sd->getMass();
331     Vector3d vel = sd->getVel();
332     //std::cerr << "mass = " << mass << " vel = " << vel << "\n";
333     RealType value;
334    
335     switch(rnemdType_) {
336     case rnemdKinetic :
337    
338     value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
339     vel[2]*vel[2]);
340    
341 gezelter 1339 valueCount[binNo] += 3;
342 skuang 1341 //std::cerr <<"starting value = " << value << "\n";
343 skuang 1338 if (sd->isDirectional()) {
344 skuang 1341 //std::cerr << "angMom calculated.\n";
345 skuang 1338 Vector3d angMom = sd->getJ();
346 skuang 1341 //std::cerr << "current angMom: " << angMom << "\n";
347 skuang 1338 Mat3x3d I = sd->getI();
348    
349     if (sd->isLinear()) {
350     int i = sd->linearAxis();
351     int j = (i + 1) % 3;
352     int k = (i + 2) % 3;
353     value += angMom[j] * angMom[j] / I(j, j) +
354     angMom[k] * angMom[k] / I(k, k);
355 gezelter 1339
356     valueCount[binNo] +=2;
357    
358 skuang 1341 } else {
359     //std::cerr << "non-linear molecule.\n";
360 skuang 1338 value += angMom[0]*angMom[0]/I(0, 0)
361     + angMom[1]*angMom[1]/I(1, 1)
362     + angMom[2]*angMom[2]/I(2, 2);
363 gezelter 1339 valueCount[binNo] +=3;
364    
365 skuang 1338 }
366     }
367 skuang 1341 //std::cerr <<"total value = " << value << "\n";
368     //value *= 0.5 / OOPSEConstant::energyConvert; // get it in kcal / mol
369     //value *= 2.0 / OOPSEConstant::kb; // convert to temperature
370     value = value / OOPSEConstant::energyConvert / OOPSEConstant::kb;
371     //std::cerr <<"value = " << value << "\n";
372 skuang 1338 break;
373     case rnemdPx :
374     value = mass * vel[0];
375 gezelter 1339 valueCount[binNo]++;
376 skuang 1338 break;
377     case rnemdPy :
378     value = mass * vel[1];
379 gezelter 1339 valueCount[binNo]++;
380 skuang 1338 break;
381     case rnemdPz :
382     value = mass * vel[2];
383 gezelter 1339 valueCount[binNo]++;
384 skuang 1338 break;
385     case rnemdUnknown :
386     default :
387     break;
388     }
389     //std::cerr << "bin = " << binNo << " value = " << value ;
390     valueHist[binNo] += value;
391     //std::cerr << " hist = " << valueHist[binNo] << " count = " << valueCount[binNo] << "\n";
392     }
393 gezelter 1334
394 skuang 1338 std::cout << counter_++;
395     for (int j = 0; j < nBins_; j++)
396     std::cout << "\t" << valueHist[j] / (RealType)valueCount[j];
397     std::cout << "\n";
398 gezelter 1334 }
399 skuang 1338 }