ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/integrators/RNEMD.cpp
(Generate patch)

Comparing trunk/src/integrators/RNEMD.cpp (file contents):
Revision 1350 by gezelter, Thu May 21 18:56:45 2009 UTC vs.
Revision 1561 by gezelter, Wed May 11 19:04:40 2011 UTC

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

Comparing trunk/src/integrators/RNEMD.cpp (property svn:keywords):
Revision 1350 by gezelter, Thu May 21 18:56:45 2009 UTC vs.
Revision 1561 by gezelter, Wed May 11 19:04:40 2011 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines