ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/rnemd/RNEMD.cpp
Revision: 1723
Committed: Thu May 24 20:59:54 2012 UTC (12 years, 11 months ago) by gezelter
Original Path: branches/development/src/integrators/RNEMD.cpp
File size: 49500 byte(s)
Log Message:
Bug fixes for heat flux import

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/Vector.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 "math/ParallelRandNumGen.hpp"
57 #include <mpi.h>
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_["KineticScaleVAM"] = rnemdKineticScaleVAM;
77 stringToEnumMap_["KineticScaleAM"] = rnemdKineticScaleAM;
78 stringToEnumMap_["PxScale"] = rnemdPxScale;
79 stringToEnumMap_["PyScale"] = rnemdPyScale;
80 stringToEnumMap_["PzScale"] = rnemdPzScale;
81 stringToEnumMap_["Px"] = rnemdPx;
82 stringToEnumMap_["Py"] = rnemdPy;
83 stringToEnumMap_["Pz"] = rnemdPz;
84 stringToEnumMap_["ShiftScaleV"] = rnemdShiftScaleV;
85 stringToEnumMap_["ShiftScaleVAM"] = rnemdShiftScaleVAM;
86 stringToEnumMap_["Unknown"] = rnemdUnknown;
87
88 rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
89 evaluator_.loadScriptString(rnemdObjectSelection_);
90 seleMan_.setSelectionSet(evaluator_.evaluate());
91
92 // do some sanity checking
93
94 int selectionCount = seleMan_.getSelectionCount();
95 int nIntegrable = info->getNGlobalIntegrableObjects();
96
97 if (selectionCount > nIntegrable) {
98 sprintf(painCave.errMsg,
99 "RNEMD: The current RNEMD_objectSelection,\n"
100 "\t\t%s\n"
101 "\thas resulted in %d selected objects. However,\n"
102 "\tthe total number of integrable objects in the system\n"
103 "\tis only %d. This is almost certainly not what you want\n"
104 "\tto do. A likely cause of this is forgetting the _RB_0\n"
105 "\tselector in the selection script!\n",
106 rnemdObjectSelection_.c_str(),
107 selectionCount, nIntegrable);
108 painCave.isFatal = 0;
109 painCave.severity = OPENMD_WARNING;
110 simError();
111 }
112
113 const string st = simParams->getRNEMD_exchangeType();
114
115 map<string, RNEMDTypeEnum>::iterator i;
116 i = stringToEnumMap_.find(st);
117 rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
118 if (rnemdType_ == rnemdUnknown) {
119 sprintf(painCave.errMsg,
120 "RNEMD: The current RNEMD_exchangeType,\n"
121 "\t\t%s\n"
122 "\tis not one of the recognized exchange types.\n",
123 st.c_str());
124 painCave.isFatal = 1;
125 painCave.severity = OPENMD_ERROR;
126 simError();
127 }
128
129 outputTemp_ = false;
130 if (simParams->haveRNEMD_outputTemperature()) {
131 outputTemp_ = simParams->getRNEMD_outputTemperature();
132 } else if ((rnemdType_ == rnemdKineticSwap) ||
133 (rnemdType_ == rnemdKineticScale) ||
134 (rnemdType_ == rnemdKineticScaleVAM) ||
135 (rnemdType_ == rnemdKineticScaleAM)) {
136 outputTemp_ = true;
137 }
138 outputVx_ = false;
139 if (simParams->haveRNEMD_outputVx()) {
140 outputVx_ = simParams->getRNEMD_outputVx();
141 } else if ((rnemdType_ == rnemdPx) || (rnemdType_ == rnemdPxScale)) {
142 outputVx_ = true;
143 }
144 outputVy_ = false;
145 if (simParams->haveRNEMD_outputVy()) {
146 outputVy_ = simParams->getRNEMD_outputVy();
147 } else if ((rnemdType_ == rnemdPy) || (rnemdType_ == rnemdPyScale)) {
148 outputVy_ = true;
149 }
150 output3DTemp_ = false;
151 if (simParams->haveRNEMD_outputXyzTemperature()) {
152 output3DTemp_ = simParams->getRNEMD_outputXyzTemperature();
153 }
154 outputRotTemp_ = false;
155 if (simParams->haveRNEMD_outputRotTemperature()) {
156 outputRotTemp_ = simParams->getRNEMD_outputRotTemperature();
157 }
158
159 #ifdef IS_MPI
160 if (worldRank == 0) {
161 #endif
162
163 //may have rnemdWriter separately
164 string rnemdFileName;
165
166 if (outputTemp_) {
167 rnemdFileName = "temperature.log";
168 tempLog_.open(rnemdFileName.c_str());
169 }
170 if (outputVx_) {
171 rnemdFileName = "velocityX.log";
172 vxzLog_.open(rnemdFileName.c_str());
173 }
174 if (outputVy_) {
175 rnemdFileName = "velocityY.log";
176 vyzLog_.open(rnemdFileName.c_str());
177 }
178
179 if (output3DTemp_) {
180 rnemdFileName = "temperatureX.log";
181 xTempLog_.open(rnemdFileName.c_str());
182 rnemdFileName = "temperatureY.log";
183 yTempLog_.open(rnemdFileName.c_str());
184 rnemdFileName = "temperatureZ.log";
185 zTempLog_.open(rnemdFileName.c_str());
186 }
187 if (outputRotTemp_) {
188 rnemdFileName = "temperatureR.log";
189 rotTempLog_.open(rnemdFileName.c_str());
190 }
191
192 #ifdef IS_MPI
193 }
194 #endif
195
196 set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
197 set_RNEMD_nBins(simParams->getRNEMD_nBins());
198 midBin_ = nBins_ / 2;
199 if (simParams->haveRNEMD_binShift()) {
200 if (simParams->getRNEMD_binShift()) {
201 zShift_ = 0.5 / (RealType)(nBins_);
202 } else {
203 zShift_ = 0.0;
204 }
205 } else {
206 zShift_ = 0.0;
207 }
208 //cerr << "I shift slabs by " << zShift_ << " Lz\n";
209 //shift slabs by half slab width, maybe useful in heterogeneous systems
210 //set to 0.0 if not using it; N/A in status output yet
211 if (simParams->haveRNEMD_logWidth()) {
212 set_RNEMD_logWidth(simParams->getRNEMD_logWidth());
213 /*arbitary rnemdLogWidth_, no checking;
214 if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
215 cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
216 cerr << "Automaically set back to default.\n";
217 rnemdLogWidth_ = nBins_;
218 }*/
219 } else {
220 set_RNEMD_logWidth(nBins_);
221 }
222 tempHist_.resize(rnemdLogWidth_, 0.0);
223 tempCount_.resize(rnemdLogWidth_, 0);
224 pxzHist_.resize(rnemdLogWidth_, 0.0);
225 //vxzCount_.resize(rnemdLogWidth_, 0);
226 pyzHist_.resize(rnemdLogWidth_, 0.0);
227 //vyzCount_.resize(rnemdLogWidth_, 0);
228
229 mHist_.resize(rnemdLogWidth_, 0.0);
230 xTempHist_.resize(rnemdLogWidth_, 0.0);
231 yTempHist_.resize(rnemdLogWidth_, 0.0);
232 zTempHist_.resize(rnemdLogWidth_, 0.0);
233 xyzTempCount_.resize(rnemdLogWidth_, 0);
234 rotTempHist_.resize(rnemdLogWidth_, 0.0);
235 rotTempCount_.resize(rnemdLogWidth_, 0);
236
237 set_RNEMD_exchange_total(0.0);
238 if (simParams->haveRNEMD_targetFlux()) {
239 set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
240 } else {
241 set_RNEMD_target_flux(0.0);
242 }
243 if (simParams->haveRNEMD_targetJzKE()) {
244 set_RNEMD_target_JzKE(simParams->getRNEMD_targetJzKE());
245 } else {
246 set_RNEMD_target_JzKE(0.0);
247 }
248 if (simParams->haveRNEMD_targetJzpx()) {
249 set_RNEMD_target_jzpx(simParams->getRNEMD_targetJzpx());
250 } else {
251 set_RNEMD_target_jzpx(0.0);
252 }
253 jzp_.x() = targetJzpx_;
254 njzp_.x() = -targetJzpx_;
255 if (simParams->haveRNEMD_targetJzpy()) {
256 set_RNEMD_target_jzpy(simParams->getRNEMD_targetJzpy());
257 } else {
258 set_RNEMD_target_jzpy(0.0);
259 }
260 jzp_.y() = targetJzpy_;
261 njzp_.y() = -targetJzpy_;
262 if (simParams->haveRNEMD_targetJzpz()) {
263 set_RNEMD_target_jzpz(simParams->getRNEMD_targetJzpz());
264 } else {
265 set_RNEMD_target_jzpz(0.0);
266 }
267 jzp_.z() = targetJzpz_;
268 njzp_.z() = -targetJzpz_;
269
270 #ifndef IS_MPI
271 if (simParams->haveSeed()) {
272 seedValue = simParams->getSeed();
273 randNumGen_ = new SeqRandNumGen(seedValue);
274 }else {
275 randNumGen_ = new SeqRandNumGen();
276 }
277 #else
278 if (simParams->haveSeed()) {
279 seedValue = simParams->getSeed();
280 randNumGen_ = new ParallelRandNumGen(seedValue);
281 }else {
282 randNumGen_ = new ParallelRandNumGen();
283 }
284 #endif
285 }
286
287 RNEMD::~RNEMD() {
288 delete randNumGen_;
289
290 #ifdef IS_MPI
291 if (worldRank == 0) {
292 #endif
293
294 sprintf(painCave.errMsg,
295 "RNEMD: total failed trials: %d\n",
296 failTrialCount_);
297 painCave.isFatal = 0;
298 painCave.severity = OPENMD_INFO;
299 simError();
300
301 if (outputTemp_) tempLog_.close();
302 if (outputVx_) vxzLog_.close();
303 if (outputVy_) vyzLog_.close();
304
305 if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
306 rnemdType_ == rnemdPyScale) {
307 sprintf(painCave.errMsg,
308 "RNEMD: total root-checking warnings: %d\n",
309 failRootCount_);
310 painCave.isFatal = 0;
311 painCave.severity = OPENMD_INFO;
312 simError();
313 }
314 if (output3DTemp_) {
315 xTempLog_.close();
316 yTempLog_.close();
317 zTempLog_.close();
318 }
319 if (outputRotTemp_) rotTempLog_.close();
320
321 #ifdef IS_MPI
322 }
323 #endif
324 }
325
326 void RNEMD::doSwap() {
327
328 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
329 Mat3x3d hmat = currentSnap_->getHmat();
330
331 seleMan_.setSelectionSet(evaluator_.evaluate());
332
333 int selei;
334 StuntDouble* sd;
335 int idx;
336
337 RealType min_val;
338 bool min_found = false;
339 StuntDouble* min_sd;
340
341 RealType max_val;
342 bool max_found = false;
343 StuntDouble* max_sd;
344
345 for (sd = seleMan_.beginSelected(selei); sd != NULL;
346 sd = seleMan_.nextSelected(selei)) {
347
348 idx = sd->getLocalIndex();
349
350 Vector3d pos = sd->getPos();
351
352 // wrap the stuntdouble's position back into the box:
353
354 if (usePeriodicBoundaryConditions_)
355 currentSnap_->wrapVector(pos);
356
357 // which bin is this stuntdouble in?
358 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
359
360 int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
361
362
363 // if we're in bin 0 or the middleBin
364 if (binNo == 0 || binNo == midBin_) {
365
366 RealType mass = sd->getMass();
367 Vector3d vel = sd->getVel();
368 RealType value;
369
370 switch(rnemdType_) {
371 case rnemdKineticSwap :
372
373 value = mass * vel.lengthSquare();
374
375 if (sd->isDirectional()) {
376 Vector3d angMom = sd->getJ();
377 Mat3x3d I = sd->getI();
378
379 if (sd->isLinear()) {
380 int i = sd->linearAxis();
381 int j = (i + 1) % 3;
382 int k = (i + 2) % 3;
383 value += angMom[j] * angMom[j] / I(j, j) +
384 angMom[k] * angMom[k] / I(k, k);
385 } else {
386 value += angMom[0]*angMom[0]/I(0, 0)
387 + angMom[1]*angMom[1]/I(1, 1)
388 + angMom[2]*angMom[2]/I(2, 2);
389 }
390 } //angular momenta exchange enabled
391 //energyConvert temporarily disabled
392 //make exchangeSum_ comparable between swap & scale
393 //value = value * 0.5 / PhysicalConstants::energyConvert;
394 value *= 0.5;
395 break;
396 case rnemdPx :
397 value = mass * vel[0];
398 break;
399 case rnemdPy :
400 value = mass * vel[1];
401 break;
402 case rnemdPz :
403 value = mass * vel[2];
404 break;
405 default :
406 break;
407 }
408
409 if (binNo == 0) {
410 if (!min_found) {
411 min_val = value;
412 min_sd = sd;
413 min_found = true;
414 } else {
415 if (min_val > value) {
416 min_val = value;
417 min_sd = sd;
418 }
419 }
420 } else { //midBin_
421 if (!max_found) {
422 max_val = value;
423 max_sd = sd;
424 max_found = true;
425 } else {
426 if (max_val < value) {
427 max_val = value;
428 max_sd = sd;
429 }
430 }
431 }
432 }
433 }
434
435 #ifdef IS_MPI
436 int nProc, worldRank;
437
438 nProc = MPI::COMM_WORLD.Get_size();
439 worldRank = MPI::COMM_WORLD.Get_rank();
440
441 bool my_min_found = min_found;
442 bool my_max_found = max_found;
443
444 // Even if we didn't find a minimum, did someone else?
445 MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found, 1, MPI::BOOL, MPI::LOR);
446 // Even if we didn't find a maximum, did someone else?
447 MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found, 1, MPI::BOOL, MPI::LOR);
448 #endif
449
450 if (max_found && min_found) {
451
452 #ifdef IS_MPI
453 struct {
454 RealType val;
455 int rank;
456 } max_vals, min_vals;
457
458 if (my_min_found) {
459 min_vals.val = min_val;
460 } else {
461 min_vals.val = HONKING_LARGE_VALUE;
462 }
463 min_vals.rank = worldRank;
464
465 // Who had the minimum?
466 MPI::COMM_WORLD.Allreduce(&min_vals, &min_vals,
467 1, MPI::REALTYPE_INT, MPI::MINLOC);
468 min_val = min_vals.val;
469
470 if (my_max_found) {
471 max_vals.val = max_val;
472 } else {
473 max_vals.val = -HONKING_LARGE_VALUE;
474 }
475 max_vals.rank = worldRank;
476
477 // Who had the maximum?
478 MPI::COMM_WORLD.Allreduce(&max_vals, &max_vals,
479 1, MPI::REALTYPE_INT, MPI::MAXLOC);
480 max_val = max_vals.val;
481 #endif
482
483 if (min_val < max_val) {
484
485 #ifdef IS_MPI
486 if (max_vals.rank == worldRank && min_vals.rank == worldRank) {
487 // I have both maximum and minimum, so proceed like a single
488 // processor version:
489 #endif
490
491 Vector3d min_vel = min_sd->getVel();
492 Vector3d max_vel = max_sd->getVel();
493 RealType temp_vel;
494
495 switch(rnemdType_) {
496 case rnemdKineticSwap :
497 min_sd->setVel(max_vel);
498 max_sd->setVel(min_vel);
499 if (min_sd->isDirectional() && max_sd->isDirectional()) {
500 Vector3d min_angMom = min_sd->getJ();
501 Vector3d max_angMom = max_sd->getJ();
502 min_sd->setJ(max_angMom);
503 max_sd->setJ(min_angMom);
504 }//angular momenta exchange enabled
505 //assumes same rigid body identity
506 break;
507 case rnemdPx :
508 temp_vel = min_vel.x();
509 min_vel.x() = max_vel.x();
510 max_vel.x() = temp_vel;
511 min_sd->setVel(min_vel);
512 max_sd->setVel(max_vel);
513 break;
514 case rnemdPy :
515 temp_vel = min_vel.y();
516 min_vel.y() = max_vel.y();
517 max_vel.y() = temp_vel;
518 min_sd->setVel(min_vel);
519 max_sd->setVel(max_vel);
520 break;
521 case rnemdPz :
522 temp_vel = min_vel.z();
523 min_vel.z() = max_vel.z();
524 max_vel.z() = temp_vel;
525 min_sd->setVel(min_vel);
526 max_sd->setVel(max_vel);
527 break;
528 default :
529 break;
530 }
531
532 #ifdef IS_MPI
533 // the rest of the cases only apply in parallel simulations:
534 } else if (max_vals.rank == worldRank) {
535 // I had the max, but not the minimum
536
537 Vector3d min_vel;
538 Vector3d max_vel = max_sd->getVel();
539 MPI::Status status;
540
541 // point-to-point swap of the velocity vector
542 MPI::COMM_WORLD.Sendrecv(max_vel.getArrayPointer(), 3, MPI::REALTYPE,
543 min_vals.rank, 0,
544 min_vel.getArrayPointer(), 3, MPI::REALTYPE,
545 min_vals.rank, 0, status);
546
547 switch(rnemdType_) {
548 case rnemdKineticSwap :
549 max_sd->setVel(min_vel);
550 //angular momenta exchange enabled
551 if (max_sd->isDirectional()) {
552 Vector3d min_angMom;
553 Vector3d max_angMom = max_sd->getJ();
554
555 // point-to-point swap of the angular momentum vector
556 MPI::COMM_WORLD.Sendrecv(max_angMom.getArrayPointer(), 3,
557 MPI::REALTYPE, min_vals.rank, 1,
558 min_angMom.getArrayPointer(), 3,
559 MPI::REALTYPE, min_vals.rank, 1,
560 status);
561
562 max_sd->setJ(min_angMom);
563 }
564 break;
565 case rnemdPx :
566 max_vel.x() = min_vel.x();
567 max_sd->setVel(max_vel);
568 break;
569 case rnemdPy :
570 max_vel.y() = min_vel.y();
571 max_sd->setVel(max_vel);
572 break;
573 case rnemdPz :
574 max_vel.z() = min_vel.z();
575 max_sd->setVel(max_vel);
576 break;
577 default :
578 break;
579 }
580 } else if (min_vals.rank == worldRank) {
581 // I had the minimum but not the maximum:
582
583 Vector3d max_vel;
584 Vector3d min_vel = min_sd->getVel();
585 MPI::Status status;
586
587 // point-to-point swap of the velocity vector
588 MPI::COMM_WORLD.Sendrecv(min_vel.getArrayPointer(), 3, MPI::REALTYPE,
589 max_vals.rank, 0,
590 max_vel.getArrayPointer(), 3, MPI::REALTYPE,
591 max_vals.rank, 0, status);
592
593 switch(rnemdType_) {
594 case rnemdKineticSwap :
595 min_sd->setVel(max_vel);
596 //angular momenta exchange enabled
597 if (min_sd->isDirectional()) {
598 Vector3d min_angMom = min_sd->getJ();
599 Vector3d max_angMom;
600
601 // point-to-point swap of the angular momentum vector
602 MPI::COMM_WORLD.Sendrecv(min_angMom.getArrayPointer(), 3,
603 MPI::REALTYPE, max_vals.rank, 1,
604 max_angMom.getArrayPointer(), 3,
605 MPI::REALTYPE, max_vals.rank, 1,
606 status);
607
608 min_sd->setJ(max_angMom);
609 }
610 break;
611 case rnemdPx :
612 min_vel.x() = max_vel.x();
613 min_sd->setVel(min_vel);
614 break;
615 case rnemdPy :
616 min_vel.y() = max_vel.y();
617 min_sd->setVel(min_vel);
618 break;
619 case rnemdPz :
620 min_vel.z() = max_vel.z();
621 min_sd->setVel(min_vel);
622 break;
623 default :
624 break;
625 }
626 }
627 #endif
628 exchangeSum_ += max_val - min_val;
629 } else {
630 sprintf(painCave.errMsg,
631 "RNEMD: exchange NOT performed because min_val > max_val\n");
632 painCave.isFatal = 0;
633 painCave.severity = OPENMD_INFO;
634 simError();
635 failTrialCount_++;
636 }
637 } else {
638 sprintf(painCave.errMsg,
639 "RNEMD: exchange NOT performed because selected object\n"
640 "\tnot present in at least one of the two slabs.\n");
641 painCave.isFatal = 0;
642 painCave.severity = OPENMD_INFO;
643 simError();
644 failTrialCount_++;
645 }
646
647 }
648
649 void RNEMD::doScale() {
650
651 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
652 Mat3x3d hmat = currentSnap_->getHmat();
653
654 seleMan_.setSelectionSet(evaluator_.evaluate());
655
656 int selei;
657 StuntDouble* sd;
658 int idx;
659
660 vector<StuntDouble*> hotBin, coldBin;
661
662 RealType Phx = 0.0;
663 RealType Phy = 0.0;
664 RealType Phz = 0.0;
665 RealType Khx = 0.0;
666 RealType Khy = 0.0;
667 RealType Khz = 0.0;
668 RealType Khw = 0.0;
669 RealType Pcx = 0.0;
670 RealType Pcy = 0.0;
671 RealType Pcz = 0.0;
672 RealType Kcx = 0.0;
673 RealType Kcy = 0.0;
674 RealType Kcz = 0.0;
675 RealType Kcw = 0.0;
676
677 for (sd = seleMan_.beginSelected(selei); sd != NULL;
678 sd = seleMan_.nextSelected(selei)) {
679
680 idx = sd->getLocalIndex();
681
682 Vector3d pos = sd->getPos();
683
684 // wrap the stuntdouble's position back into the box:
685
686 if (usePeriodicBoundaryConditions_)
687 currentSnap_->wrapVector(pos);
688
689 // which bin is this stuntdouble in?
690 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
691
692 int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
693
694 // if we're in bin 0 or the middleBin
695 if (binNo == 0 || binNo == midBin_) {
696
697 RealType mass = sd->getMass();
698 Vector3d vel = sd->getVel();
699
700 if (binNo == 0) {
701 hotBin.push_back(sd);
702 Phx += mass * vel.x();
703 Phy += mass * vel.y();
704 Phz += mass * vel.z();
705 Khx += mass * vel.x() * vel.x();
706 Khy += mass * vel.y() * vel.y();
707 Khz += mass * vel.z() * vel.z();
708 //if (rnemdType_ == rnemdKineticScaleVAM) {
709 if (sd->isDirectional()) {
710 Vector3d angMom = sd->getJ();
711 Mat3x3d I = sd->getI();
712 if (sd->isLinear()) {
713 int i = sd->linearAxis();
714 int j = (i + 1) % 3;
715 int k = (i + 2) % 3;
716 Khw += angMom[j] * angMom[j] / I(j, j) +
717 angMom[k] * angMom[k] / I(k, k);
718 } else {
719 Khw += angMom[0]*angMom[0]/I(0, 0)
720 + angMom[1]*angMom[1]/I(1, 1)
721 + angMom[2]*angMom[2]/I(2, 2);
722 }
723 }
724 //}
725 } else { //midBin_
726 coldBin.push_back(sd);
727 Pcx += mass * vel.x();
728 Pcy += mass * vel.y();
729 Pcz += mass * vel.z();
730 Kcx += mass * vel.x() * vel.x();
731 Kcy += mass * vel.y() * vel.y();
732 Kcz += mass * vel.z() * vel.z();
733 //if (rnemdType_ == rnemdKineticScaleVAM) {
734 if (sd->isDirectional()) {
735 Vector3d angMom = sd->getJ();
736 Mat3x3d I = sd->getI();
737 if (sd->isLinear()) {
738 int i = sd->linearAxis();
739 int j = (i + 1) % 3;
740 int k = (i + 2) % 3;
741 Kcw += angMom[j] * angMom[j] / I(j, j) +
742 angMom[k] * angMom[k] / I(k, k);
743 } else {
744 Kcw += angMom[0]*angMom[0]/I(0, 0)
745 + angMom[1]*angMom[1]/I(1, 1)
746 + angMom[2]*angMom[2]/I(2, 2);
747 }
748 }
749 //}
750 }
751 }
752 }
753
754 Khx *= 0.5;
755 Khy *= 0.5;
756 Khz *= 0.5;
757 Khw *= 0.5;
758 Kcx *= 0.5;
759 Kcy *= 0.5;
760 Kcz *= 0.5;
761 Kcw *= 0.5;
762
763 std::cerr << "Khx= " << Khx << "\tKhy= " << Khy << "\tKhz= " << Khz
764 << "\tKhw= " << Khw << "\tKcx= " << Kcx << "\tKcy= " << Kcy
765 << "\tKcz= " << Kcz << "\tKcw= " << Kcw << "\n";
766 std::cerr << "Phx= " << Phx << "\tPhy= " << Phy << "\tPhz= " << Phz
767 << "\tPcx= " << Pcx << "\tPcy= " << Pcy << "\tPcz= " <<Pcz<<"\n";
768
769 #ifdef IS_MPI
770 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
771 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
772 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phz, 1, MPI::REALTYPE, MPI::SUM);
773 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcx, 1, MPI::REALTYPE, MPI::SUM);
774 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcy, 1, MPI::REALTYPE, MPI::SUM);
775 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcz, 1, MPI::REALTYPE, MPI::SUM);
776
777 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
778 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
779 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
780 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khw, 1, MPI::REALTYPE, MPI::SUM);
781
782 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
783 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
784 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
785 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcw, 1, MPI::REALTYPE, MPI::SUM);
786 #endif
787
788 //solve coldBin coeff's first
789 RealType px = Pcx / Phx;
790 RealType py = Pcy / Phy;
791 RealType pz = Pcz / Phz;
792 RealType c, x, y, z;
793 bool successfulScale = false;
794 if ((rnemdType_ == rnemdKineticScaleVAM) ||
795 (rnemdType_ == rnemdKineticScaleAM)) {
796 //may need sanity check Khw & Kcw > 0
797
798 if (rnemdType_ == rnemdKineticScaleVAM) {
799 c = 1.0 - targetFlux_ / (Kcx + Kcy + Kcz + Kcw);
800 } else {
801 c = 1.0 - targetFlux_ / Kcw;
802 }
803
804 if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
805 c = sqrt(c);
806 std::cerr << "cold slab scaling coefficient: " << c << endl;
807 //now convert to hotBin coefficient
808 RealType w = 0.0;
809 if (rnemdType_ == rnemdKineticScaleVAM) {
810 x = 1.0 + px * (1.0 - c);
811 y = 1.0 + py * (1.0 - c);
812 z = 1.0 + pz * (1.0 - c);
813 /* more complicated way
814 w = 1.0 + (Kcw - Kcw * c * c - (c * c * (Kcx + Kcy + Kcz
815 + Khx * px * px + Khy * py * py + Khz * pz * pz)
816 - 2.0 * c * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py)
817 + Khz * pz * (1.0 + pz)) + Khx * px * (2.0 + px)
818 + Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
819 - Kcx - Kcy - Kcz)) / Khw; the following is simpler
820 */
821 if ((fabs(x - 1.0) < 0.1) && (fabs(y - 1.0) < 0.1) &&
822 (fabs(z - 1.0) < 0.1)) {
823 w = 1.0 + (targetFlux_ + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
824 + Khz * (1.0 - z * z)) / Khw;
825 }//no need to calculate w if x, y or z is out of range
826 } else {
827 w = 1.0 + targetFlux_ / Khw;
828 }
829 if ((w > 0.81) && (w < 1.21)) {//restrict scaling coefficients
830 //if w is in the right range, so should be x, y, z.
831 vector<StuntDouble*>::iterator sdi;
832 Vector3d vel;
833 for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
834 if (rnemdType_ == rnemdKineticScaleVAM) {
835 vel = (*sdi)->getVel() * c;
836 //vel.x() *= c;
837 //vel.y() *= c;
838 //vel.z() *= c;
839 (*sdi)->setVel(vel);
840 }
841 if ((*sdi)->isDirectional()) {
842 Vector3d angMom = (*sdi)->getJ() * c;
843 //angMom[0] *= c;
844 //angMom[1] *= c;
845 //angMom[2] *= c;
846 (*sdi)->setJ(angMom);
847 }
848 }
849 w = sqrt(w);
850 std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
851 << "\twh= " << w << endl;
852 for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
853 if (rnemdType_ == rnemdKineticScaleVAM) {
854 vel = (*sdi)->getVel();
855 vel.x() *= x;
856 vel.y() *= y;
857 vel.z() *= z;
858 (*sdi)->setVel(vel);
859 }
860 if ((*sdi)->isDirectional()) {
861 Vector3d angMom = (*sdi)->getJ() * w;
862 //angMom[0] *= w;
863 //angMom[1] *= w;
864 //angMom[2] *= w;
865 (*sdi)->setJ(angMom);
866 }
867 }
868 successfulScale = true;
869 exchangeSum_ += targetFlux_;
870 }
871 }
872 } else {
873 RealType a000, a110, c0, a001, a111, b01, b11, c1;
874 switch(rnemdType_) {
875 case rnemdKineticScale :
876 /* used hotBin coeff's & only scale x & y dimensions
877 RealType px = Phx / Pcx;
878 RealType py = Phy / Pcy;
879 a110 = Khy;
880 c0 = - Khx - Khy - targetFlux_;
881 a000 = Khx;
882 a111 = Kcy * py * py;
883 b11 = -2.0 * Kcy * py * (1.0 + py);
884 c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
885 b01 = -2.0 * Kcx * px * (1.0 + px);
886 a001 = Kcx * px * px;
887 */
888 //scale all three dimensions, let c_x = c_y
889 a000 = Kcx + Kcy;
890 a110 = Kcz;
891 c0 = targetFlux_ - Kcx - Kcy - Kcz;
892 a001 = Khx * px * px + Khy * py * py;
893 a111 = Khz * pz * pz;
894 b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
895 b11 = -2.0 * Khz * pz * (1.0 + pz);
896 c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
897 + Khz * pz * (2.0 + pz) - targetFlux_;
898 break;
899 case rnemdPxScale :
900 c = 1 - targetFlux_ / Pcx;
901 a000 = Kcy;
902 a110 = Kcz;
903 c0 = Kcx * c * c - Kcx - Kcy - Kcz;
904 a001 = py * py * Khy;
905 a111 = pz * pz * Khz;
906 b01 = -2.0 * Khy * py * (1.0 + py);
907 b11 = -2.0 * Khz * pz * (1.0 + pz);
908 c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
909 + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
910 break;
911 case rnemdPyScale :
912 c = 1 - targetFlux_ / Pcy;
913 a000 = Kcx;
914 a110 = Kcz;
915 c0 = Kcy * c * c - Kcx - Kcy - Kcz;
916 a001 = px * px * Khx;
917 a111 = pz * pz * Khz;
918 b01 = -2.0 * Khx * px * (1.0 + px);
919 b11 = -2.0 * Khz * pz * (1.0 + pz);
920 c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
921 + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
922 break;
923 case rnemdPzScale ://we don't really do this, do we?
924 c = 1 - targetFlux_ / Pcz;
925 a000 = Kcx;
926 a110 = Kcy;
927 c0 = Kcz * c * c - Kcx - Kcy - Kcz;
928 a001 = px * px * Khx;
929 a111 = py * py * Khy;
930 b01 = -2.0 * Khx * px * (1.0 + px);
931 b11 = -2.0 * Khy * py * (1.0 + py);
932 c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
933 + Khz * (fastpow(c * pz - pz - 1.0, 2) - 1.0);
934 break;
935 default :
936 break;
937 }
938
939 RealType v1 = a000 * a111 - a001 * a110;
940 RealType v2 = a000 * b01;
941 RealType v3 = a000 * b11;
942 RealType v4 = a000 * c1 - a001 * c0;
943 RealType v8 = a110 * b01;
944 RealType v10 = - b01 * c0;
945
946 RealType u0 = v2 * v10 - v4 * v4;
947 RealType u1 = -2.0 * v3 * v4;
948 RealType u2 = -v2 * v8 - v3 * v3 - 2.0 * v1 * v4;
949 RealType u3 = -2.0 * v1 * v3;
950 RealType u4 = - v1 * v1;
951 //rescale coefficients
952 RealType maxAbs = fabs(u0);
953 if (maxAbs < fabs(u1)) maxAbs = fabs(u1);
954 if (maxAbs < fabs(u2)) maxAbs = fabs(u2);
955 if (maxAbs < fabs(u3)) maxAbs = fabs(u3);
956 if (maxAbs < fabs(u4)) maxAbs = fabs(u4);
957 u0 /= maxAbs;
958 u1 /= maxAbs;
959 u2 /= maxAbs;
960 u3 /= maxAbs;
961 u4 /= maxAbs;
962 //max_element(start, end) is also available.
963 Polynomial<RealType> poly; //same as DoublePolynomial poly;
964 poly.setCoefficient(4, u4);
965 poly.setCoefficient(3, u3);
966 poly.setCoefficient(2, u2);
967 poly.setCoefficient(1, u1);
968 poly.setCoefficient(0, u0);
969 vector<RealType> realRoots = poly.FindRealRoots();
970
971 vector<RealType>::iterator ri;
972 RealType r1, r2, alpha0;
973 vector<pair<RealType,RealType> > rps;
974 for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
975 r2 = *ri;
976 //check if FindRealRoots() give the right answer
977 if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
978 sprintf(painCave.errMsg,
979 "RNEMD Warning: polynomial solve seems to have an error!");
980 painCave.isFatal = 0;
981 simError();
982 failRootCount_++;
983 }
984 //might not be useful w/o rescaling coefficients
985 alpha0 = -c0 - a110 * r2 * r2;
986 if (alpha0 >= 0.0) {
987 r1 = sqrt(alpha0 / a000);
988 if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
989 < 1e-6)
990 { rps.push_back(make_pair(r1, r2)); }
991 if (r1 > 1e-6) { //r1 non-negative
992 r1 = -r1;
993 if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
994 < 1e-6)
995 { rps.push_back(make_pair(r1, r2)); }
996 }
997 }
998 }
999 // Consider combining together the solving pair part w/ the searching
1000 // best solution part so that we don't need the pairs vector
1001 if (!rps.empty()) {
1002 RealType smallestDiff = HONKING_LARGE_VALUE;
1003 RealType diff;
1004 pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
1005 vector<pair<RealType,RealType> >::iterator rpi;
1006 for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1007 r1 = (*rpi).first;
1008 r2 = (*rpi).second;
1009 switch(rnemdType_) {
1010 case rnemdKineticScale :
1011 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1012 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1013 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1014 break;
1015 case rnemdPxScale :
1016 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1017 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1018 break;
1019 case rnemdPyScale :
1020 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1021 + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1022 break;
1023 case rnemdPzScale :
1024 diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1025 + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1026 default :
1027 break;
1028 }
1029 if (diff < smallestDiff) {
1030 smallestDiff = diff;
1031 bestPair = *rpi;
1032 }
1033 }
1034 #ifdef IS_MPI
1035 if (worldRank == 0) {
1036 #endif
1037 sprintf(painCave.errMsg,
1038 "RNEMD: roots r1= %lf\tr2 = %lf\n",
1039 bestPair.first, bestPair.second);
1040 painCave.isFatal = 0;
1041 painCave.severity = OPENMD_INFO;
1042 simError();
1043 #ifdef IS_MPI
1044 }
1045 #endif
1046
1047 switch(rnemdType_) {
1048 case rnemdKineticScale :
1049 x = bestPair.first;
1050 y = bestPair.first;
1051 z = bestPair.second;
1052 break;
1053 case rnemdPxScale :
1054 x = c;
1055 y = bestPair.first;
1056 z = bestPair.second;
1057 break;
1058 case rnemdPyScale :
1059 x = bestPair.first;
1060 y = c;
1061 z = bestPair.second;
1062 break;
1063 case rnemdPzScale :
1064 x = bestPair.first;
1065 y = bestPair.second;
1066 z = c;
1067 break;
1068 default :
1069 break;
1070 }
1071 vector<StuntDouble*>::iterator sdi;
1072 Vector3d vel;
1073 for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1074 vel = (*sdi)->getVel();
1075 vel.x() *= x;
1076 vel.y() *= y;
1077 vel.z() *= z;
1078 (*sdi)->setVel(vel);
1079 }
1080 //convert to hotBin coefficient
1081 x = 1.0 + px * (1.0 - x);
1082 y = 1.0 + py * (1.0 - y);
1083 z = 1.0 + pz * (1.0 - z);
1084 for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1085 vel = (*sdi)->getVel();
1086 vel.x() *= x;
1087 vel.y() *= y;
1088 vel.z() *= z;
1089 (*sdi)->setVel(vel);
1090 }
1091 successfulScale = true;
1092 exchangeSum_ += targetFlux_;
1093 }
1094 }
1095 if (successfulScale != true) {
1096 sprintf(painCave.errMsg,
1097 "RNEMD: exchange NOT performed!\n");
1098 painCave.isFatal = 0;
1099 painCave.severity = OPENMD_INFO;
1100 simError();
1101 failTrialCount_++;
1102 }
1103 }
1104
1105 void RNEMD::doShiftScale() {
1106
1107 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1108 Mat3x3d hmat = currentSnap_->getHmat();
1109
1110 seleMan_.setSelectionSet(evaluator_.evaluate());
1111
1112 int selei;
1113 StuntDouble* sd;
1114 int idx;
1115
1116 vector<StuntDouble*> hotBin, coldBin;
1117
1118 Vector3d Ph(V3Zero);
1119 RealType Mh = 0.0;
1120 RealType Kh = 0.0;
1121 Vector3d Pc(V3Zero);
1122 RealType Mc = 0.0;
1123 RealType Kc = 0.0;
1124
1125 for (sd = seleMan_.beginSelected(selei); sd != NULL;
1126 sd = seleMan_.nextSelected(selei)) {
1127
1128 idx = sd->getLocalIndex();
1129
1130 Vector3d pos = sd->getPos();
1131
1132 // wrap the stuntdouble's position back into the box:
1133
1134 if (usePeriodicBoundaryConditions_)
1135 currentSnap_->wrapVector(pos);
1136
1137 // which bin is this stuntdouble in?
1138 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1139
1140 int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
1141
1142 // if we're in bin 0 or the middleBin
1143 if (binNo == 0 || binNo == midBin_) {
1144
1145 RealType mass = sd->getMass();
1146 Vector3d vel = sd->getVel();
1147
1148 if (binNo == 0) {
1149 hotBin.push_back(sd);
1150 //std::cerr << "before, velocity = " << vel << endl;
1151 Ph += mass * vel;
1152 //std::cerr << "after, velocity = " << vel << endl;
1153 Mh += mass;
1154 Kh += mass * vel.lengthSquare();
1155 if (rnemdType_ == rnemdShiftScaleVAM) {
1156 if (sd->isDirectional()) {
1157 Vector3d angMom = sd->getJ();
1158 Mat3x3d I = sd->getI();
1159 if (sd->isLinear()) {
1160 int i = sd->linearAxis();
1161 int j = (i + 1) % 3;
1162 int k = (i + 2) % 3;
1163 Kh += angMom[j] * angMom[j] / I(j, j) +
1164 angMom[k] * angMom[k] / I(k, k);
1165 } else {
1166 Kh += angMom[0] * angMom[0] / I(0, 0) +
1167 angMom[1] * angMom[1] / I(1, 1) +
1168 angMom[2] * angMom[2] / I(2, 2);
1169 }
1170 }
1171 }
1172 } else { //midBin_
1173 coldBin.push_back(sd);
1174 Pc += mass * vel;
1175 Mc += mass;
1176 Kc += mass * vel.lengthSquare();
1177 if (rnemdType_ == rnemdShiftScaleVAM) {
1178 if (sd->isDirectional()) {
1179 Vector3d angMom = sd->getJ();
1180 Mat3x3d I = sd->getI();
1181 if (sd->isLinear()) {
1182 int i = sd->linearAxis();
1183 int j = (i + 1) % 3;
1184 int k = (i + 2) % 3;
1185 Kc += angMom[j] * angMom[j] / I(j, j) +
1186 angMom[k] * angMom[k] / I(k, k);
1187 } else {
1188 Kc += angMom[0] * angMom[0] / I(0, 0) +
1189 angMom[1] * angMom[1] / I(1, 1) +
1190 angMom[2] * angMom[2] / I(2, 2);
1191 }
1192 }
1193 }
1194 }
1195 }
1196 }
1197
1198 Kh *= 0.5;
1199 Kc *= 0.5;
1200
1201 std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1202 << "\tKc= " << Kc << endl;
1203 std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1204
1205 #ifdef IS_MPI
1206 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1207 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
1208 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mh, 1, MPI::REALTYPE, MPI::SUM);
1209 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kh, 1, MPI::REALTYPE, MPI::SUM);
1210 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mc, 1, MPI::REALTYPE, MPI::SUM);
1211 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kc, 1, MPI::REALTYPE, MPI::SUM);
1212 #endif
1213
1214 bool successfulExchange = false;
1215 if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1216 Vector3d vc = Pc / Mc;
1217 Vector3d ac = njzp_ / Mc + vc;
1218 RealType cNumerator = Kc - targetJzKE_ - 0.5 * Mc * ac.lengthSquare();
1219 if (cNumerator > 0.0) {
1220 RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1221 if (cDenominator > 0.0) {
1222 RealType c = sqrt(cNumerator / cDenominator);
1223 if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1224 Vector3d vh = Ph / Mh;
1225 Vector3d ah = jzp_ / Mh + vh;
1226 RealType hNumerator = Kh + targetJzKE_
1227 - 0.5 * Mh * ah.lengthSquare();
1228 if (hNumerator > 0.0) {
1229 RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1230 if (hDenominator > 0.0) {
1231 RealType h = sqrt(hNumerator / hDenominator);
1232 if ((h > 0.9) && (h < 1.1)) {
1233 std::cerr << "cold slab scaling coefficient: " << c << "\n";
1234 std::cerr << "hot slab scaling coefficient: " << h << "\n";
1235 vector<StuntDouble*>::iterator sdi;
1236 Vector3d vel;
1237 for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1238 //vel = (*sdi)->getVel();
1239 vel = ((*sdi)->getVel() - vc) * c + ac;
1240 (*sdi)->setVel(vel);
1241 if (rnemdType_ == rnemdShiftScaleVAM) {
1242 if ((*sdi)->isDirectional()) {
1243 Vector3d angMom = (*sdi)->getJ() * c;
1244 (*sdi)->setJ(angMom);
1245 }
1246 }
1247 }
1248 for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1249 //vel = (*sdi)->getVel();
1250 vel = ((*sdi)->getVel() - vh) * h + ah;
1251 (*sdi)->setVel(vel);
1252 if (rnemdType_ == rnemdShiftScaleVAM) {
1253 if ((*sdi)->isDirectional()) {
1254 Vector3d angMom = (*sdi)->getJ() * h;
1255 (*sdi)->setJ(angMom);
1256 }
1257 }
1258 }
1259 successfulExchange = true;
1260 exchangeSum_ += targetFlux_;
1261 // this is a redundant variable for doShiftScale() so that
1262 // RNEMD can output one exchange quantity needed in a job.
1263 // need a better way to do this.
1264 }
1265 }
1266 }
1267 }
1268 }
1269 }
1270 }
1271 if (successfulExchange != true) {
1272 sprintf(painCave.errMsg,
1273 "RNEMD: exchange NOT performed!\n");
1274 painCave.isFatal = 0;
1275 painCave.severity = OPENMD_INFO;
1276 simError();
1277 failTrialCount_++;
1278 }
1279 }
1280
1281 void RNEMD::doRNEMD() {
1282
1283 switch(rnemdType_) {
1284 case rnemdKineticScale :
1285 case rnemdKineticScaleVAM :
1286 case rnemdKineticScaleAM :
1287 case rnemdPxScale :
1288 case rnemdPyScale :
1289 case rnemdPzScale :
1290 doScale();
1291 break;
1292 case rnemdKineticSwap :
1293 case rnemdPx :
1294 case rnemdPy :
1295 case rnemdPz :
1296 doSwap();
1297 break;
1298 case rnemdShiftScaleV :
1299 case rnemdShiftScaleVAM :
1300 doShiftScale();
1301 break;
1302 case rnemdUnknown :
1303 default :
1304 break;
1305 }
1306 }
1307
1308 void RNEMD::collectData() {
1309
1310 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1311 Mat3x3d hmat = currentSnap_->getHmat();
1312
1313 seleMan_.setSelectionSet(evaluator_.evaluate());
1314
1315 int selei;
1316 StuntDouble* sd;
1317 int idx;
1318
1319 // alternative approach, track all molecules instead of only those
1320 // selected for scaling/swapping:
1321 /*
1322 SimInfo::MoleculeIterator miter;
1323 vector<StuntDouble*>::iterator iiter;
1324 Molecule* mol;
1325 StuntDouble* integrableObject;
1326 for (mol = info_->beginMolecule(miter); mol != NULL;
1327 mol = info_->nextMolecule(miter))
1328 integrableObject is essentially sd
1329 for (integrableObject = mol->beginIntegrableObject(iiter);
1330 integrableObject != NULL;
1331 integrableObject = mol->nextIntegrableObject(iiter))
1332 */
1333 for (sd = seleMan_.beginSelected(selei); sd != NULL;
1334 sd = seleMan_.nextSelected(selei)) {
1335
1336 idx = sd->getLocalIndex();
1337
1338 Vector3d pos = sd->getPos();
1339
1340 // wrap the stuntdouble's position back into the box:
1341
1342 if (usePeriodicBoundaryConditions_)
1343 currentSnap_->wrapVector(pos);
1344
1345 // which bin is this stuntdouble in?
1346 // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1347
1348 int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
1349 rnemdLogWidth_;
1350 // no symmetrization allowed due to arbitary rnemdLogWidth_
1351 /*
1352 if (rnemdLogWidth_ == midBin_ + 1)
1353 if (binNo > midBin_)
1354 binNo = nBins_ - binNo;
1355 */
1356 RealType mass = sd->getMass();
1357 mHist_[binNo] += mass;
1358 Vector3d vel = sd->getVel();
1359 RealType value;
1360 //RealType xVal, yVal, zVal;
1361
1362 if (outputTemp_) {
1363 value = mass * vel.lengthSquare();
1364 tempCount_[binNo] += 3;
1365 if (sd->isDirectional()) {
1366 Vector3d angMom = sd->getJ();
1367 Mat3x3d I = sd->getI();
1368 if (sd->isLinear()) {
1369 int i = sd->linearAxis();
1370 int j = (i + 1) % 3;
1371 int k = (i + 2) % 3;
1372 value += angMom[j] * angMom[j] / I(j, j) +
1373 angMom[k] * angMom[k] / I(k, k);
1374 tempCount_[binNo] +=2;
1375 } else {
1376 value += angMom[0] * angMom[0] / I(0, 0) +
1377 angMom[1]*angMom[1]/I(1, 1) +
1378 angMom[2]*angMom[2]/I(2, 2);
1379 tempCount_[binNo] +=3;
1380 }
1381 }
1382 value = value / PhysicalConstants::energyConvert
1383 / PhysicalConstants::kb;//may move to getStatus()
1384 tempHist_[binNo] += value;
1385 }
1386 if (outputVx_) {
1387 value = mass * vel[0];
1388 //vxzCount_[binNo]++;
1389 pxzHist_[binNo] += value;
1390 }
1391 if (outputVy_) {
1392 value = mass * vel[1];
1393 //vyzCount_[binNo]++;
1394 pyzHist_[binNo] += value;
1395 }
1396
1397 if (output3DTemp_) {
1398 value = mass * vel.x() * vel.x();
1399 xTempHist_[binNo] += value;
1400 value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1401 / PhysicalConstants::kb;
1402 yTempHist_[binNo] += value;
1403 value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1404 / PhysicalConstants::kb;
1405 zTempHist_[binNo] += value;
1406 xyzTempCount_[binNo]++;
1407 }
1408 if (outputRotTemp_) {
1409 if (sd->isDirectional()) {
1410 Vector3d angMom = sd->getJ();
1411 Mat3x3d I = sd->getI();
1412 if (sd->isLinear()) {
1413 int i = sd->linearAxis();
1414 int j = (i + 1) % 3;
1415 int k = (i + 2) % 3;
1416 value = angMom[j] * angMom[j] / I(j, j) +
1417 angMom[k] * angMom[k] / I(k, k);
1418 rotTempCount_[binNo] +=2;
1419 } else {
1420 value = angMom[0] * angMom[0] / I(0, 0) +
1421 angMom[1] * angMom[1] / I(1, 1) +
1422 angMom[2] * angMom[2] / I(2, 2);
1423 rotTempCount_[binNo] +=3;
1424 }
1425 }
1426 value = value / PhysicalConstants::energyConvert
1427 / PhysicalConstants::kb;//may move to getStatus()
1428 rotTempHist_[binNo] += value;
1429 }
1430
1431 }
1432 }
1433
1434 void RNEMD::getStarted() {
1435 collectData();
1436 /*now can output profile in step 0, but might not be useful;
1437 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1438 Stats& stat = currentSnap_->statData;
1439 stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1440 */
1441 //may output a header for the log file here
1442 getStatus();
1443 }
1444
1445 void RNEMD::getStatus() {
1446
1447 Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1448 Stats& stat = currentSnap_->statData;
1449 RealType time = currentSnap_->getTime();
1450
1451 stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1452 //or to be more meaningful, define another item as exchangeSum_ / time
1453 int j;
1454
1455 #ifdef IS_MPI
1456
1457 // all processors have the same number of bins, and STL vectors pack their
1458 // arrays, so in theory, this should be safe:
1459
1460 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &mHist_[0],
1461 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1462 if (outputTemp_) {
1463 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempHist_[0],
1464 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1465 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempCount_[0],
1466 rnemdLogWidth_, MPI::INT, MPI::SUM);
1467 }
1468 if (outputVx_) {
1469 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pxzHist_[0],
1470 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1471 //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vxzCount_[0],
1472 // rnemdLogWidth_, MPI::INT, MPI::SUM);
1473 }
1474 if (outputVy_) {
1475 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pyzHist_[0],
1476 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1477 //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vyzCount_[0],
1478 // rnemdLogWidth_, MPI::INT, MPI::SUM);
1479 }
1480 if (output3DTemp_) {
1481 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xTempHist_[0],
1482 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1483 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &yTempHist_[0],
1484 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1485 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &zTempHist_[0],
1486 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1487 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xyzTempCount_[0],
1488 rnemdLogWidth_, MPI::INT, MPI::SUM);
1489 }
1490 if (outputRotTemp_) {
1491 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempHist_[0],
1492 rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1493 MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempCount_[0],
1494 rnemdLogWidth_, MPI::INT, MPI::SUM);
1495 }
1496
1497 // If we're the root node, should we print out the results
1498 int worldRank = MPI::COMM_WORLD.Get_rank();
1499 if (worldRank == 0) {
1500 #endif
1501
1502 if (outputTemp_) {
1503 tempLog_ << time;
1504 for (j = 0; j < rnemdLogWidth_; j++) {
1505 tempLog_ << "\t" << tempHist_[j] / (RealType)tempCount_[j];
1506 }
1507 tempLog_ << endl;
1508 }
1509 if (outputVx_) {
1510 vxzLog_ << time;
1511 for (j = 0; j < rnemdLogWidth_; j++) {
1512 vxzLog_ << "\t" << pxzHist_[j] / mHist_[j];
1513 }
1514 vxzLog_ << endl;
1515 }
1516 if (outputVy_) {
1517 vyzLog_ << time;
1518 for (j = 0; j < rnemdLogWidth_; j++) {
1519 vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1520 }
1521 vyzLog_ << endl;
1522 }
1523
1524 if (output3DTemp_) {
1525 RealType temp;
1526 xTempLog_ << time;
1527 for (j = 0; j < rnemdLogWidth_; j++) {
1528 if (outputVx_)
1529 xTempHist_[j] -= pxzHist_[j] * pxzHist_[j] / mHist_[j];
1530 temp = xTempHist_[j] / (RealType)xyzTempCount_[j]
1531 / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1532 xTempLog_ << "\t" << temp;
1533 }
1534 xTempLog_ << endl;
1535 yTempLog_ << time;
1536 for (j = 0; j < rnemdLogWidth_; j++) {
1537 yTempLog_ << "\t" << yTempHist_[j] / (RealType)xyzTempCount_[j];
1538 }
1539 yTempLog_ << endl;
1540 zTempLog_ << time;
1541 for (j = 0; j < rnemdLogWidth_; j++) {
1542 zTempLog_ << "\t" << zTempHist_[j] / (RealType)xyzTempCount_[j];
1543 }
1544 zTempLog_ << endl;
1545 }
1546 if (outputRotTemp_) {
1547 rotTempLog_ << time;
1548 for (j = 0; j < rnemdLogWidth_; j++) {
1549 rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1550 }
1551 rotTempLog_ << endl;
1552 }
1553
1554 #ifdef IS_MPI
1555 }
1556 #endif
1557
1558 for (j = 0; j < rnemdLogWidth_; j++) {
1559 mHist_[j] = 0.0;
1560 }
1561 if (outputTemp_)
1562 for (j = 0; j < rnemdLogWidth_; j++) {
1563 tempCount_[j] = 0;
1564 tempHist_[j] = 0.0;
1565 }
1566 if (outputVx_)
1567 for (j = 0; j < rnemdLogWidth_; j++) {
1568 //pxzCount_[j] = 0;
1569 pxzHist_[j] = 0.0;
1570 }
1571 if (outputVy_)
1572 for (j = 0; j < rnemdLogWidth_; j++) {
1573 //pyzCount_[j] = 0;
1574 pyzHist_[j] = 0.0;
1575 }
1576
1577 if (output3DTemp_)
1578 for (j = 0; j < rnemdLogWidth_; j++) {
1579 xTempHist_[j] = 0.0;
1580 yTempHist_[j] = 0.0;
1581 zTempHist_[j] = 0.0;
1582 xyzTempCount_[j] = 0;
1583 }
1584 if (outputRotTemp_)
1585 for (j = 0; j < rnemdLogWidth_; j++) {
1586 rotTempCount_[j] = 0;
1587 rotTempHist_[j] = 0.0;
1588 }
1589 }
1590 }
1591

Properties

Name Value
svn:keywords Author Id Revision Date