ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/integrators/RNEMD.cpp
Revision: 1629
Committed: Wed Sep 14 21:15:17 2011 UTC (13 years, 7 months ago) by gezelter
File size: 36102 byte(s)
Log Message:
Merging changes from old branch into development branch

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

Properties

Name Value
svn:keywords Author Id Revision Date