ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/integrators/RNEMD.cpp
Revision: 1560
Committed: Wed May 11 17:55:32 2011 UTC (13 years, 11 months ago) by skuang
File size: 35177 byte(s)
Log Message:
option binShift enables bin division shifts Lz/2N
option output3DTemp enables x,y,z dimensional temperature recording
disables angular momentum exchange for swapping algorithm
fixed a bug for swapping MPI code

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

Properties

Name Value
svn:keywords Author Id Revision Date