ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/integrators/RNEMD.cpp
Revision: 1561
Committed: Wed May 11 19:04:40 2011 UTC (14 years ago) by gezelter
File size: 36085 byte(s)
Log Message:
replaced debugging statements with calls to simError()
code reformatting for readability

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. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * 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 *
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 */
41
42 #include <cmath>
43 #include "integrators/RNEMD.hpp"
44 #include "math/Vector3.hpp"
45 #include "math/SquareMatrix3.hpp"
46 #include "math/Polynomial.hpp"
47 #include "primitives/Molecule.hpp"
48 #include "primitives/StuntDouble.hpp"
49 #include "utils/PhysicalConstants.hpp"
50 #include "utils/Tuple.hpp"
51
52 #ifndef IS_MPI
53 #include "math/SeqRandNumGen.hpp"
54 #else
55 #include "math/ParallelRandNumGen.hpp"
56 #endif
57
58 #define HONKING_LARGE_VALUE 1.0e10
59
60 using namespace std;
61 namespace OpenMD {
62
63 RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info),
64 usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
65
66 failTrialCount_ = 0;
67 failRootCount_ = 0;
68
69 int seedValue;
70 Globals * simParams = info->getSimParams();
71
72 stringToEnumMap_["KineticSwap"] = rnemdKineticSwap;
73 stringToEnumMap_["KineticScale"] = rnemdKineticScale;
74 stringToEnumMap_["PxScale"] = rnemdPxScale;
75 stringToEnumMap_["PyScale"] = rnemdPyScale;
76 stringToEnumMap_["PzScale"] = rnemdPzScale;
77 stringToEnumMap_["Px"] = rnemdPx;
78 stringToEnumMap_["Py"] = rnemdPy;
79 stringToEnumMap_["Pz"] = rnemdPz;
80 stringToEnumMap_["Unknown"] = rnemdUnknown;
81
82 rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
83 evaluator_.loadScriptString(rnemdObjectSelection_);
84 seleMan_.setSelectionSet(evaluator_.evaluate());
85
86 // do some sanity checking
87
88 int selectionCount = seleMan_.getSelectionCount();
89 int nIntegrable = info->getNGlobalIntegrableObjects();
90
91 if (selectionCount > nIntegrable) {
92 sprintf(painCave.errMsg,
93 "RNEMD: The current RNEMD_objectSelection,\n"
94 "\t\t%s\n"
95 "\thas resulted in %d selected objects. However,\n"
96 "\tthe total number of integrable objects in the system\n"
97 "\tis only %d. This is almost certainly not what you want\n"
98 "\tto do. A likely cause of this is forgetting the _RB_0\n"
99 "\tselector in the selection script!\n",
100 rnemdObjectSelection_.c_str(),
101 selectionCount, nIntegrable);
102 painCave.isFatal = 0;
103 painCave.severity = OPENMD_WARNING;
104 simError();
105 }
106
107 const string st = simParams->getRNEMD_exchangeType();
108
109 map<string, RNEMDTypeEnum>::iterator i;
110 i = stringToEnumMap_.find(st);
111 rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
112 if (rnemdType_ == rnemdUnknown) {
113 sprintf(painCave.errMsg,
114 "RNEMD: The current RNEMD_exchangeType,\n"
115 "\t\t%s\n"
116 "\tis not one of the recognized exchange types.\n",
117 st.c_str());
118 painCave.isFatal = 1;
119 painCave.severity = OPENMD_ERROR;
120 simError();
121 }
122
123 output3DTemp_ = false;
124 if (simParams->haveRNEMD_outputDimensionalTemperature()) {
125 output3DTemp_ = simParams->getRNEMD_outputDimensionalTemperature();
126 }
127
128 #ifdef IS_MPI
129 if (worldRank == 0) {
130 #endif
131
132 string rnemdFileName;
133 switch(rnemdType_) {
134 case rnemdKineticSwap :
135 case rnemdKineticScale :
136 rnemdFileName = "temperature.log";
137 break;
138 case rnemdPx :
139 case rnemdPxScale :
140 case rnemdPy :
141 case rnemdPyScale :
142 rnemdFileName = "momemtum.log";
143 break;
144 case rnemdPz :
145 case rnemdPzScale :
146 case rnemdUnknown :
147 default :
148 rnemdFileName = "rnemd.log";
149 break;
150 }
151 rnemdLog_.open(rnemdFileName.c_str());
152
153 string xTempFileName;
154 string yTempFileName;
155 string zTempFileName;
156 if (output3DTemp_) {
157 xTempFileName = "temperatureX.log";
158 yTempFileName = "temperatureY.log";
159 zTempFileName = "temperatureZ.log";
160 xTempLog_.open(xTempFileName.c_str());
161 yTempLog_.open(yTempFileName.c_str());
162 zTempLog_.open(zTempFileName.c_str());
163 }
164
165 #ifdef IS_MPI
166 }
167 #endif
168
169 set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
170 set_RNEMD_nBins(simParams->getRNEMD_nBins());
171 midBin_ = nBins_ / 2;
172 if (simParams->haveRNEMD_binShift()) {
173 if (simParams->getRNEMD_binShift()) {
174 zShift_ = 0.5 / (RealType)(nBins_);
175 } else {
176 zShift_ = 0.0;
177 }
178 } else {
179 zShift_ = 0.0;
180 }
181 //cerr << "we have zShift_ = " << zShift_ << "\n";
182 //shift slabs by half slab width, might be useful in heterogeneous systems
183 //set to 0.0 if not using it; can NOT be used in status output yet
184 if (simParams->haveRNEMD_logWidth()) {
185 set_RNEMD_logWidth(simParams->getRNEMD_logWidth());
186 /*arbitary rnemdLogWidth_ no checking
187 if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
188 cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
189 cerr << "Automaically set back to default.\n";
190 rnemdLogWidth_ = nBins_;
191 }*/
192 } else {
193 set_RNEMD_logWidth(nBins_);
194 }
195 valueHist_.resize(rnemdLogWidth_, 0.0);
196 valueCount_.resize(rnemdLogWidth_, 0);
197 xTempHist_.resize(rnemdLogWidth_, 0.0);
198 yTempHist_.resize(rnemdLogWidth_, 0.0);
199 zTempHist_.resize(rnemdLogWidth_, 0.0);
200 xyzTempCount_.resize(rnemdLogWidth_, 0);
201
202 set_RNEMD_exchange_total(0.0);
203 if (simParams->haveRNEMD_targetFlux()) {
204 set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
205 } else {
206 set_RNEMD_target_flux(0.0);
207 }
208
209 #ifndef IS_MPI
210 if (simParams->haveSeed()) {
211 seedValue = simParams->getSeed();
212 randNumGen_ = new SeqRandNumGen(seedValue);
213 }else {
214 randNumGen_ = new SeqRandNumGen();
215 }
216 #else
217 if (simParams->haveSeed()) {
218 seedValue = simParams->getSeed();
219 randNumGen_ = new ParallelRandNumGen(seedValue);
220 }else {
221 randNumGen_ = new ParallelRandNumGen();
222 }
223 #endif
224 }
225
226 RNEMD::~RNEMD() {
227 delete randNumGen_;
228
229 #ifdef IS_MPI
230 if (worldRank == 0) {
231 #endif
232
233 sprintf(painCave.errMsg,
234 "RNEMD: total failed trials: %d\n",
235 failTrialCount_);
236 painCave.isFatal = 0;
237 painCave.severity = OPENMD_INFO;
238 simError();
239
240 rnemdLog_.close();
241 if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale || rnemdType_ == rnemdPyScale) {
242 sprintf(painCave.errMsg,
243 "RNEMD: total root-checking warnings: %d\n",
244 failRootCount_);
245 painCave.isFatal = 0;
246 painCave.severity = OPENMD_INFO;
247 simError();
248 }
249 if (output3DTemp_) {
250 xTempLog_.close();
251 yTempLog_.close();
252 zTempLog_.close();
253 }
254 #ifdef IS_MPI
255 }
256 #endif
257 }
258
259 void RNEMD::doSwap() {
260
261 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
262 Mat3x3d hmat = currentSnap_->getHmat();
263
264 seleMan_.setSelectionSet(evaluator_.evaluate());
265
266 int selei;
267 StuntDouble* sd;
268 int idx;
269
270 RealType min_val;
271 bool min_found = false;
272 StuntDouble* min_sd;
273
274 RealType max_val;
275 bool max_found = false;
276 StuntDouble* max_sd;
277
278 for (sd = seleMan_.beginSelected(selei); sd != NULL;
279 sd = seleMan_.nextSelected(selei)) {
280
281 idx = sd->getLocalIndex();
282
283 Vector3d pos = sd->getPos();
284
285 // wrap the stuntdouble's position back into the box:
286
287 if (usePeriodicBoundaryConditions_)
288 currentSnap_->wrapVector(pos);
289
290 // which bin is this stuntdouble in?
291 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
292
293 int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
294
295
296 // if we're in bin 0 or the middleBin
297 if (binNo == 0 || binNo == midBin_) {
298
299 RealType mass = sd->getMass();
300 Vector3d vel = sd->getVel();
301 RealType value;
302
303 switch(rnemdType_) {
304 case rnemdKineticSwap :
305
306 value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
307 vel[2]*vel[2]);
308 /*
309 if (sd->isDirectional()) {
310 Vector3d angMom = sd->getJ();
311 Mat3x3d I = sd->getI();
312
313 if (sd->isLinear()) {
314 int i = sd->linearAxis();
315 int j = (i + 1) % 3;
316 int k = (i + 2) % 3;
317 value += angMom[j] * angMom[j] / I(j, j) +
318 angMom[k] * angMom[k] / I(k, k);
319 } else {
320 value += angMom[0]*angMom[0]/I(0, 0)
321 + angMom[1]*angMom[1]/I(1, 1)
322 + angMom[2]*angMom[2]/I(2, 2);
323 }
324 } no exchange of angular momenta
325 */
326 //make exchangeSum_ comparable between swap & scale
327 //temporarily without using energyConvert
328 //value = value * 0.5 / PhysicalConstants::energyConvert;
329 value *= 0.5;
330 break;
331 case rnemdPx :
332 value = mass * vel[0];
333 break;
334 case rnemdPy :
335 value = mass * vel[1];
336 break;
337 case rnemdPz :
338 value = mass * vel[2];
339 break;
340 default :
341 break;
342 }
343
344 if (binNo == 0) {
345 if (!min_found) {
346 min_val = value;
347 min_sd = sd;
348 min_found = true;
349 } else {
350 if (min_val > value) {
351 min_val = value;
352 min_sd = sd;
353 }
354 }
355 } else { //midBin_
356 if (!max_found) {
357 max_val = value;
358 max_sd = sd;
359 max_found = true;
360 } else {
361 if (max_val < value) {
362 max_val = value;
363 max_sd = sd;
364 }
365 }
366 }
367 }
368 }
369
370 #ifdef IS_MPI
371 int nProc, worldRank;
372
373 nProc = MPI::COMM_WORLD.Get_size();
374 worldRank = MPI::COMM_WORLD.Get_rank();
375
376 bool my_min_found = min_found;
377 bool my_max_found = max_found;
378
379 // Even if we didn't find a minimum, did someone else?
380 MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found, 1, MPI::BOOL, MPI::LOR);
381 // Even if we didn't find a maximum, did someone else?
382 MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found, 1, MPI::BOOL, MPI::LOR);
383 struct {
384 RealType val;
385 int rank;
386 } max_vals, min_vals;
387
388 if (min_found) {
389 if (my_min_found)
390 min_vals.val = min_val;
391 else
392 min_vals.val = HONKING_LARGE_VALUE;
393
394 min_vals.rank = worldRank;
395
396 // Who had the minimum?
397 MPI::COMM_WORLD.Allreduce(&min_vals, &min_vals,
398 1, MPI::REALTYPE_INT, MPI::MINLOC);
399 min_val = min_vals.val;
400 }
401
402 if (max_found) {
403 if (my_max_found)
404 max_vals.val = max_val;
405 else
406 max_vals.val = -HONKING_LARGE_VALUE;
407
408 max_vals.rank = worldRank;
409
410 // Who had the maximum?
411 MPI::COMM_WORLD.Allreduce(&max_vals, &max_vals,
412 1, MPI::REALTYPE_INT, MPI::MAXLOC);
413 max_val = max_vals.val;
414 }
415 #endif
416
417 if (max_found && min_found) {
418 if (min_val < max_val) {
419
420 #ifdef IS_MPI
421 if (max_vals.rank == worldRank && min_vals.rank == worldRank) {
422 // I have both maximum and minimum, so proceed like a single
423 // processor version:
424 #endif
425 // objects to be swapped: velocity ONLY
426 Vector3d min_vel = min_sd->getVel();
427 Vector3d max_vel = max_sd->getVel();
428 RealType temp_vel;
429
430 switch(rnemdType_) {
431 case rnemdKineticSwap :
432 min_sd->setVel(max_vel);
433 max_sd->setVel(min_vel);
434 /*
435 if (min_sd->isDirectional() && max_sd->isDirectional()) {
436 Vector3d min_angMom = min_sd->getJ();
437 Vector3d max_angMom = max_sd->getJ();
438 min_sd->setJ(max_angMom);
439 max_sd->setJ(min_angMom);
440 } no angular momentum exchange
441 */
442 break;
443 case rnemdPx :
444 temp_vel = min_vel.x();
445 min_vel.x() = max_vel.x();
446 max_vel.x() = temp_vel;
447 min_sd->setVel(min_vel);
448 max_sd->setVel(max_vel);
449 break;
450 case rnemdPy :
451 temp_vel = min_vel.y();
452 min_vel.y() = max_vel.y();
453 max_vel.y() = temp_vel;
454 min_sd->setVel(min_vel);
455 max_sd->setVel(max_vel);
456 break;
457 case rnemdPz :
458 temp_vel = min_vel.z();
459 min_vel.z() = max_vel.z();
460 max_vel.z() = temp_vel;
461 min_sd->setVel(min_vel);
462 max_sd->setVel(max_vel);
463 break;
464 default :
465 break;
466 }
467 #ifdef IS_MPI
468 // the rest of the cases only apply in parallel simulations:
469 } else if (max_vals.rank == worldRank) {
470 // I had the max, but not the minimum
471
472 Vector3d min_vel;
473 Vector3d max_vel = max_sd->getVel();
474 MPI::Status status;
475
476 // point-to-point swap of the velocity vector
477 MPI::COMM_WORLD.Sendrecv(max_vel.getArrayPointer(), 3, MPI::REALTYPE,
478 min_vals.rank, 0,
479 min_vel.getArrayPointer(), 3, MPI::REALTYPE,
480 min_vals.rank, 0, status);
481
482 switch(rnemdType_) {
483 case rnemdKineticSwap :
484 max_sd->setVel(min_vel);
485 //no angular momentum exchange for now
486 /*
487 if (max_sd->isDirectional()) {
488 Vector3d min_angMom;
489 Vector3d max_angMom = max_sd->getJ();
490
491 // point-to-point swap of the angular momentum vector
492 MPI::COMM_WORLD.Sendrecv(max_angMom.getArrayPointer(), 3,
493 MPI::REALTYPE, min_vals.rank, 1,
494 min_angMom.getArrayPointer(), 3,
495 MPI::REALTYPE, min_vals.rank, 1,
496 status);
497
498 max_sd->setJ(min_angMom);
499 }
500 */
501 break;
502 case rnemdPx :
503 max_vel.x() = min_vel.x();
504 max_sd->setVel(max_vel);
505 break;
506 case rnemdPy :
507 max_vel.y() = min_vel.y();
508 max_sd->setVel(max_vel);
509 break;
510 case rnemdPz :
511 max_vel.z() = min_vel.z();
512 max_sd->setVel(max_vel);
513 break;
514 default :
515 break;
516 }
517 } else if (min_vals.rank == worldRank) {
518 // I had the minimum but not the maximum:
519
520 Vector3d max_vel;
521 Vector3d min_vel = min_sd->getVel();
522 MPI::Status status;
523
524 // point-to-point swap of the velocity vector
525 MPI::COMM_WORLD.Sendrecv(min_vel.getArrayPointer(), 3, MPI::REALTYPE,
526 max_vals.rank, 0,
527 max_vel.getArrayPointer(), 3, MPI::REALTYPE,
528 max_vals.rank, 0, status);
529
530 switch(rnemdType_) {
531 case rnemdKineticSwap :
532 min_sd->setVel(max_vel);
533 // no angular momentum exchange for now
534 /*
535 if (min_sd->isDirectional()) {
536 Vector3d min_angMom = min_sd->getJ();
537 Vector3d max_angMom;
538
539 // point-to-point swap of the angular momentum vector
540 MPI::COMM_WORLD.Sendrecv(min_angMom.getArrayPointer(), 3,
541 MPI::REALTYPE, max_vals.rank, 1,
542 max_angMom.getArrayPointer(), 3,
543 MPI::REALTYPE, max_vals.rank, 1,
544 status);
545
546 min_sd->setJ(max_angMom);
547 }
548 */
549 break;
550 case rnemdPx :
551 min_vel.x() = max_vel.x();
552 min_sd->setVel(min_vel);
553 break;
554 case rnemdPy :
555 min_vel.y() = max_vel.y();
556 min_sd->setVel(min_vel);
557 break;
558 case rnemdPz :
559 min_vel.z() = max_vel.z();
560 min_sd->setVel(min_vel);
561 break;
562 default :
563 break;
564 }
565 }
566 #endif
567 exchangeSum_ += max_val - min_val;
568 } else {
569 sprintf(painCave.errMsg,
570 "RNEMD: exchange NOT performed because min_val > max_val\n");
571 painCave.isFatal = 0;
572 painCave.severity = OPENMD_INFO;
573 simError();
574 failTrialCount_++;
575 }
576 } else {
577 sprintf(painCave.errMsg,
578 "RNEMD: exchange NOT performed because at least one\n"
579 "\tof the two slabs is empty\n");
580 painCave.isFatal = 0;
581 painCave.severity = OPENMD_INFO;
582 simError();
583 failTrialCount_++;
584 }
585
586 }
587
588 void RNEMD::doScale() {
589
590 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
591 Mat3x3d hmat = currentSnap_->getHmat();
592
593 seleMan_.setSelectionSet(evaluator_.evaluate());
594
595 int selei;
596 StuntDouble* sd;
597 int idx;
598
599 vector<StuntDouble*> hotBin, coldBin;
600
601 RealType Phx = 0.0;
602 RealType Phy = 0.0;
603 RealType Phz = 0.0;
604 RealType Khx = 0.0;
605 RealType Khy = 0.0;
606 RealType Khz = 0.0;
607 RealType Pcx = 0.0;
608 RealType Pcy = 0.0;
609 RealType Pcz = 0.0;
610 RealType Kcx = 0.0;
611 RealType Kcy = 0.0;
612 RealType Kcz = 0.0;
613
614 for (sd = seleMan_.beginSelected(selei); sd != NULL;
615 sd = seleMan_.nextSelected(selei)) {
616
617 idx = sd->getLocalIndex();
618
619 Vector3d pos = sd->getPos();
620
621 // wrap the stuntdouble's position back into the box:
622
623 if (usePeriodicBoundaryConditions_)
624 currentSnap_->wrapVector(pos);
625
626 // which bin is this stuntdouble in?
627 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
628
629 int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
630
631 // if we're in bin 0 or the middleBin
632 if (binNo == 0 || binNo == midBin_) {
633
634 RealType mass = sd->getMass();
635 Vector3d vel = sd->getVel();
636
637 if (binNo == 0) {
638 hotBin.push_back(sd);
639 Phx += mass * vel.x();
640 Phy += mass * vel.y();
641 Phz += mass * vel.z();
642 Khx += mass * vel.x() * vel.x();
643 Khy += mass * vel.y() * vel.y();
644 Khz += mass * vel.z() * vel.z();
645 } else { //midBin_
646 coldBin.push_back(sd);
647 Pcx += mass * vel.x();
648 Pcy += mass * vel.y();
649 Pcz += mass * vel.z();
650 Kcx += mass * vel.x() * vel.x();
651 Kcy += mass * vel.y() * vel.y();
652 Kcz += mass * vel.z() * vel.z();
653 }
654 }
655 }
656
657 Khx *= 0.5;
658 Khy *= 0.5;
659 Khz *= 0.5;
660 Kcx *= 0.5;
661 Kcy *= 0.5;
662 Kcz *= 0.5;
663
664 #ifdef IS_MPI
665 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
666 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
667 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phz, 1, MPI::REALTYPE, MPI::SUM);
668 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcx, 1, MPI::REALTYPE, MPI::SUM);
669 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcy, 1, MPI::REALTYPE, MPI::SUM);
670 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcz, 1, MPI::REALTYPE, MPI::SUM);
671
672 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
673 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
674 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
675 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
676 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
677 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
678 #endif
679
680 //use coldBin coeff's
681 RealType px = Pcx / Phx;
682 RealType py = Pcy / Phy;
683 RealType pz = Pcz / Phz;
684
685 RealType a000, a110, c0, a001, a111, b01, b11, c1, c;
686 switch(rnemdType_) {
687 case rnemdKineticScale :
688 // used hotBin coeff's & only scale x & y dimensions
689 /*
690 RealType px = Phx / Pcx;
691 RealType py = Phy / Pcy;
692 a110 = Khy;
693 c0 = - Khx - Khy - targetFlux_;
694 a000 = Khx;
695 a111 = Kcy * py * py;
696 b11 = -2.0 * Kcy * py * (1.0 + py);
697 c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
698 b01 = -2.0 * Kcx * px * (1.0 + px);
699 a001 = Kcx * px * px;
700 */
701 //scale all three dimensions, let c_x = c_y
702 a000 = Kcx + Kcy;
703 a110 = Kcz;
704 c0 = targetFlux_ - Kcx - Kcy - Kcz;
705 a001 = Khx * px * px + Khy * py * py;
706 a111 = Khz * pz * pz;
707 b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
708 b11 = -2.0 * Khz * pz * (1.0 + pz);
709 c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
710 + Khz * pz * (2.0 + pz) - targetFlux_;
711 break;
712 case rnemdPxScale :
713 c = 1 - targetFlux_ / Pcx;
714 a000 = Kcy;
715 a110 = Kcz;
716 c0 = Kcx * c * c - Kcx - Kcy - Kcz;
717 a001 = py * py * Khy;
718 a111 = pz * pz * Khz;
719 b01 = -2.0 * Khy * py * (1.0 + py);
720 b11 = -2.0 * Khz * pz * (1.0 + pz);
721 c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
722 + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
723 break;
724 case rnemdPyScale :
725 c = 1 - targetFlux_ / Pcy;
726 a000 = Kcx;
727 a110 = Kcz;
728 c0 = Kcy * c * c - Kcx - Kcy - Kcz;
729 a001 = px * px * Khx;
730 a111 = pz * pz * Khz;
731 b01 = -2.0 * Khx * px * (1.0 + px);
732 b11 = -2.0 * Khz * pz * (1.0 + pz);
733 c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
734 + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
735 break;
736 case rnemdPzScale ://we don't really do this, do we?
737 c = 1 - targetFlux_ / Pcz;
738 a000 = Kcx;
739 a110 = Kcy;
740 c0 = Kcz * c * c - Kcx - Kcy - Kcz;
741 a001 = px * px * Khx;
742 a111 = py * py * Khy;
743 b01 = -2.0 * Khx * px * (1.0 + px);
744 b11 = -2.0 * Khy * py * (1.0 + py);
745 c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
746 + Khz * (fastpow(c * pz - pz - 1.0, 2) - 1.0);
747 break;
748 default :
749 break;
750 }
751
752 RealType v1 = a000 * a111 - a001 * a110;
753 RealType v2 = a000 * b01;
754 RealType v3 = a000 * b11;
755 RealType v4 = a000 * c1 - a001 * c0;
756 RealType v8 = a110 * b01;
757 RealType v10 = - b01 * c0;
758
759 RealType u0 = v2 * v10 - v4 * v4;
760 RealType u1 = -2.0 * v3 * v4;
761 RealType u2 = -v2 * v8 - v3 * v3 - 2.0 * v1 * v4;
762 RealType u3 = -2.0 * v1 * v3;
763 RealType u4 = - v1 * v1;
764 //rescale coefficients
765 RealType maxAbs = fabs(u0);
766 if (maxAbs < fabs(u1)) maxAbs = fabs(u1);
767 if (maxAbs < fabs(u2)) maxAbs = fabs(u2);
768 if (maxAbs < fabs(u3)) maxAbs = fabs(u3);
769 if (maxAbs < fabs(u4)) maxAbs = fabs(u4);
770 u0 /= maxAbs;
771 u1 /= maxAbs;
772 u2 /= maxAbs;
773 u3 /= maxAbs;
774 u4 /= maxAbs;
775 //max_element(start, end) is also available.
776 Polynomial<RealType> poly; //same as DoublePolynomial poly;
777 poly.setCoefficient(4, u4);
778 poly.setCoefficient(3, u3);
779 poly.setCoefficient(2, u2);
780 poly.setCoefficient(1, u1);
781 poly.setCoefficient(0, u0);
782 vector<RealType> realRoots = poly.FindRealRoots();
783
784 vector<RealType>::iterator ri;
785 RealType r1, r2, alpha0;
786 vector<pair<RealType,RealType> > rps;
787 for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
788 r2 = *ri;
789 //check if FindRealRoots() give the right answer
790 if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
791 sprintf(painCave.errMsg,
792 "RNEMD Warning: polynomial solve seems to have an error!");
793 painCave.isFatal = 0;
794 simError();
795 failRootCount_++;
796 }
797 //might not be useful w/o rescaling coefficients
798 alpha0 = -c0 - a110 * r2 * r2;
799 if (alpha0 >= 0.0) {
800 r1 = sqrt(alpha0 / a000);
801 if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111)) < 1e-6)
802 { rps.push_back(make_pair(r1, r2)); }
803 if (r1 > 1e-6) { //r1 non-negative
804 r1 = -r1;
805 if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111)) <1e-6)
806 { rps.push_back(make_pair(r1, r2)); }
807 }
808 }
809 }
810 // Consider combining together the solving pair part w/ the searching
811 // best solution part so that we don't need the pairs vector
812 if (!rps.empty()) {
813 RealType smallestDiff = HONKING_LARGE_VALUE;
814 RealType diff;
815 pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
816 vector<pair<RealType,RealType> >::iterator rpi;
817 for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
818 r1 = (*rpi).first;
819 r2 = (*rpi).second;
820 switch(rnemdType_) {
821 case rnemdKineticScale :
822 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
823 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
824 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
825 break;
826 case rnemdPxScale :
827 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
828 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
829 break;
830 case rnemdPyScale :
831 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
832 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
833 break;
834 case rnemdPzScale :
835 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
836 + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
837 default :
838 break;
839 }
840 if (diff < smallestDiff) {
841 smallestDiff = diff;
842 bestPair = *rpi;
843 }
844 }
845 #ifdef IS_MPI
846 if (worldRank == 0) {
847 #endif
848 sprintf(painCave.errMsg,
849 "RNEMD: roots r1= %lf\tr2 = %lf\n",
850 bestPair.first, bestPair.second);
851 painCave.isFatal = 0;
852 painCave.severity = OPENMD_INFO;
853 simError();
854 #ifdef IS_MPI
855 }
856 #endif
857
858 RealType x, y, z;
859 switch(rnemdType_) {
860 case rnemdKineticScale :
861 x = bestPair.first;
862 y = bestPair.first;
863 z = bestPair.second;
864 break;
865 case rnemdPxScale :
866 x = c;
867 y = bestPair.first;
868 z = bestPair.second;
869 break;
870 case rnemdPyScale :
871 x = bestPair.first;
872 y = c;
873 z = bestPair.second;
874 break;
875 case rnemdPzScale :
876 x = bestPair.first;
877 y = bestPair.second;
878 z = c;
879 break;
880 default :
881 break;
882 }
883 vector<StuntDouble*>::iterator sdi;
884 Vector3d vel;
885 for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
886 vel = (*sdi)->getVel();
887 vel.x() *= x;
888 vel.y() *= y;
889 vel.z() *= z;
890 (*sdi)->setVel(vel);
891 }
892 //convert to hotBin coefficient
893 x = 1.0 + px * (1.0 - x);
894 y = 1.0 + py * (1.0 - y);
895 z = 1.0 + pz * (1.0 - z);
896 for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
897 vel = (*sdi)->getVel();
898 vel.x() *= x;
899 vel.y() *= y;
900 vel.z() *= z;
901 (*sdi)->setVel(vel);
902 }
903 exchangeSum_ += targetFlux_;
904 //we may want to check whether the exchange has been successful
905 } else {
906 sprintf(painCave.errMsg,
907 "RNEMD: exchange NOT performed!\n");
908 painCave.isFatal = 0;
909 painCave.severity = OPENMD_INFO;
910 simError();
911 failTrialCount_++;
912 }
913
914 }
915
916 void RNEMD::doRNEMD() {
917
918 switch(rnemdType_) {
919 case rnemdKineticScale :
920 case rnemdPxScale :
921 case rnemdPyScale :
922 case rnemdPzScale :
923 doScale();
924 break;
925 case rnemdKineticSwap :
926 case rnemdPx :
927 case rnemdPy :
928 case rnemdPz :
929 doSwap();
930 break;
931 case rnemdUnknown :
932 default :
933 break;
934 }
935 }
936
937 void RNEMD::collectData() {
938
939 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
940 Mat3x3d hmat = currentSnap_->getHmat();
941
942 seleMan_.setSelectionSet(evaluator_.evaluate());
943
944 int selei;
945 StuntDouble* sd;
946 int idx;
947
948 // alternative approach, track all molecules instead of only those
949 // selected for scaling/swapping:
950 /*
951 SimInfo::MoleculeIterator miter;
952 vector<StuntDouble*>::iterator iiter;
953 Molecule* mol;
954 StuntDouble* integrableObject;
955 for (mol = info_->beginMolecule(miter); mol != NULL;
956 mol = info_->nextMolecule(miter))
957 integrableObject is essentially sd
958 for (integrableObject = mol->beginIntegrableObject(iiter);
959 integrableObject != NULL;
960 integrableObject = mol->nextIntegrableObject(iiter))
961 */
962 for (sd = seleMan_.beginSelected(selei); sd != NULL;
963 sd = seleMan_.nextSelected(selei)) {
964
965 idx = sd->getLocalIndex();
966
967 Vector3d pos = sd->getPos();
968
969 // wrap the stuntdouble's position back into the box:
970
971 if (usePeriodicBoundaryConditions_)
972 currentSnap_->wrapVector(pos);
973
974 // which bin is this stuntdouble in?
975 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
976
977 int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
978 rnemdLogWidth_;
979 // no symmetrization allowed due to arbitary rnemdLogWidth_ value
980 /*
981 if (rnemdLogWidth_ == midBin_ + 1)
982 if (binNo > midBin_)
983 binNo = nBins_ - binNo;
984 */
985 RealType mass = sd->getMass();
986 Vector3d vel = sd->getVel();
987 RealType value;
988 RealType xVal, yVal, zVal;
989
990 switch(rnemdType_) {
991 case rnemdKineticSwap :
992 case rnemdKineticScale :
993
994 value = mass * (vel[0]*vel[0] + vel[1]*vel[1] + vel[2]*vel[2]);
995
996 valueCount_[binNo] += 3;
997 if (sd->isDirectional()) {
998 Vector3d angMom = sd->getJ();
999 Mat3x3d I = sd->getI();
1000
1001 if (sd->isLinear()) {
1002 int i = sd->linearAxis();
1003 int j = (i + 1) % 3;
1004 int k = (i + 2) % 3;
1005 value += angMom[j] * angMom[j] / I(j, j) +
1006 angMom[k] * angMom[k] / I(k, k);
1007
1008 valueCount_[binNo] +=2;
1009
1010 } else {
1011 value += angMom[0]*angMom[0]/I(0, 0)
1012 + angMom[1]*angMom[1]/I(1, 1)
1013 + angMom[2]*angMom[2]/I(2, 2);
1014 valueCount_[binNo] +=3;
1015 }
1016 }
1017 value = value / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1018
1019 break;
1020 case rnemdPx :
1021 case rnemdPxScale :
1022 value = mass * vel[0];
1023 valueCount_[binNo]++;
1024 break;
1025 case rnemdPy :
1026 case rnemdPyScale :
1027 value = mass * vel[1];
1028 valueCount_[binNo]++;
1029 break;
1030 case rnemdPz :
1031 case rnemdPzScale :
1032 value = pos.z(); //temporarily for homogeneous systems ONLY
1033 valueCount_[binNo]++;
1034 break;
1035 case rnemdUnknown :
1036 default :
1037 value = 1.0;
1038 valueCount_[binNo]++;
1039 break;
1040 }
1041 valueHist_[binNo] += value;
1042
1043 if (output3DTemp_) {
1044 xVal = mass * vel.x() * vel.x() / PhysicalConstants::energyConvert
1045 / PhysicalConstants::kb;
1046 yVal = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1047 / PhysicalConstants::kb;
1048 zVal = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1049 / PhysicalConstants::kb;
1050 xTempHist_[binNo] += xVal;
1051 yTempHist_[binNo] += yVal;
1052 zTempHist_[binNo] += zVal;
1053 xyzTempCount_[binNo]++;
1054 }
1055 }
1056 }
1057
1058 void RNEMD::getStarted() {
1059 collectData();
1060 /* now should be able to output profile in step 0, but might not be useful
1061 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1062 Stats& stat = currentSnap_->statData;
1063 stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1064 */
1065 getStatus();
1066 }
1067
1068 void RNEMD::getStatus() {
1069
1070 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1071 Stats& stat = currentSnap_->statData;
1072 RealType time = currentSnap_->getTime();
1073
1074 stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1075 //or to be more meaningful, define another item as exchangeSum_ / time
1076 int j;
1077
1078 #ifdef IS_MPI
1079
1080 // all processors have the same number of bins, and STL vectors pack their
1081 // arrays, so in theory, this should be safe:
1082
1083 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueHist_[0],
1084 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1085 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueCount_[0],
1086 rnemdLogWidth_, MPI::INT, MPI::SUM);
1087 if (output3DTemp_) {
1088 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xTempHist_[0],
1089 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1090 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &yTempHist_[0],
1091 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1092 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &zTempHist_[0],
1093 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1094 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xyzTempCount_[0],
1095 rnemdLogWidth_, MPI::INT, MPI::SUM);
1096 }
1097 // If we're the root node, should we print out the results
1098 int worldRank = MPI::COMM_WORLD.Get_rank();
1099 if (worldRank == 0) {
1100 #endif
1101 rnemdLog_ << time;
1102 for (j = 0; j < rnemdLogWidth_; j++) {
1103 rnemdLog_ << "\t" << valueHist_[j] / (RealType)valueCount_[j];
1104 }
1105 rnemdLog_ << "\n";
1106 if (output3DTemp_) {
1107 xTempLog_ << time;
1108 for (j = 0; j < rnemdLogWidth_; j++) {
1109 xTempLog_ << "\t" << xTempHist_[j] / (RealType)xyzTempCount_[j];
1110 }
1111 xTempLog_ << "\n";
1112 yTempLog_ << time;
1113 for (j = 0; j < rnemdLogWidth_; j++) {
1114 yTempLog_ << "\t" << yTempHist_[j] / (RealType)xyzTempCount_[j];
1115 }
1116 yTempLog_ << "\n";
1117 zTempLog_ << time;
1118 for (j = 0; j < rnemdLogWidth_; j++) {
1119 zTempLog_ << "\t" << zTempHist_[j] / (RealType)xyzTempCount_[j];
1120 }
1121 zTempLog_ << "\n";
1122 }
1123 #ifdef IS_MPI
1124 }
1125 #endif
1126 for (j = 0; j < rnemdLogWidth_; j++) {
1127 valueCount_[j] = 0;
1128 valueHist_[j] = 0.0;
1129 }
1130 if (output3DTemp_)
1131 for (j = 0; j < rnemdLogWidth_; j++) {
1132 xTempHist_[j] = 0.0;
1133 yTempHist_[j] = 0.0;
1134 zTempHist_[j] = 0.0;
1135 xyzTempCount_[j] = 0;
1136 }
1137 }
1138 }

Properties

Name Value
svn:keywords Author Id Revision Date