ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/rnemd/RNEMD.cpp
Revision: 1665
Committed: Tue Nov 22 20:38:56 2011 UTC (13 years, 5 months ago) by gezelter
Original Path: branches/development/src/integrators/RNEMD.cpp
File size: 36168 byte(s)
Log Message:
updated copyright notices

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

Properties

Name Value
svn:keywords Author Id Revision Date