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

Comparing:
branches/development/src/integrators/RNEMD.cpp (file contents), Revision 1465 by chuckv, Fri Jul 9 23:08:25 2010 UTC vs.
branches/development/src/rnemd/RNEMD.cpp (file contents), Revision 1764 by gezelter, Tue Jul 3 18:32:27 2012 UTC

# Line 40 | Line 40
40   */
41  
42   #include <cmath>
43 < #include "integrators/RNEMD.hpp"
43 > #include "rnemd/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"
# Line 53 | Line 54
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), usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
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 +    RNEMDParameters* rnemdParams = simParams->getRNEMDParameters();
74  
75      stringToEnumMap_["KineticSwap"] = rnemdKineticSwap;
76      stringToEnumMap_["KineticScale"] = rnemdKineticScale;
77 +    stringToEnumMap_["KineticScaleVAM"] = rnemdKineticScaleVAM;
78 +    stringToEnumMap_["KineticScaleAM"] = rnemdKineticScaleAM;
79      stringToEnumMap_["PxScale"] = rnemdPxScale;
80      stringToEnumMap_["PyScale"] = rnemdPyScale;
81      stringToEnumMap_["PzScale"] = rnemdPzScale;
82      stringToEnumMap_["Px"] = rnemdPx;
83      stringToEnumMap_["Py"] = rnemdPy;
84      stringToEnumMap_["Pz"] = rnemdPz;
85 +    stringToEnumMap_["ShiftScaleV"] = rnemdShiftScaleV;
86 +    stringToEnumMap_["ShiftScaleVAM"] = rnemdShiftScaleVAM;
87      stringToEnumMap_["Unknown"] = rnemdUnknown;
88  
89 <    rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
89 >    runTime_ = simParams->getRunTime();
90 >    statusTime_ = simParams->getStatusTime();
91 >
92 >    rnemdObjectSelection_ = rnemdParams->getObjectSelection();
93      evaluator_.loadScriptString(rnemdObjectSelection_);
94      seleMan_.setSelectionSet(evaluator_.evaluate());
95  
# Line 88 | Line 100 | namespace OpenMD {
100  
101      if (selectionCount > nIntegrable) {
102        sprintf(painCave.errMsg,
103 <              "RNEMD warning: The current RNEMD_objectSelection,\n"
103 >              "RNEMD: The current RNEMD_objectSelection,\n"
104                "\t\t%s\n"
105                "\thas resulted in %d selected objects.  However,\n"
106                "\tthe total number of integrable objects in the system\n"
# Line 98 | Line 110 | namespace OpenMD {
110                rnemdObjectSelection_.c_str(),
111                selectionCount, nIntegrable);
112        painCave.isFatal = 0;
113 +      painCave.severity = OPENMD_WARNING;
114        simError();
102
115      }
116      
117 <    const std::string st = simParams->getRNEMD_exchangeType();
117 >    const string st = rnemdParams->getExchangeType();
118  
119 <    std::map<std::string, RNEMDTypeEnum>::iterator i;
119 >    map<string, RNEMDTypeEnum>::iterator i;
120      i = stringToEnumMap_.find(st);
121      rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
122      if (rnemdType_ == rnemdUnknown) {
123 <      std::cerr << "WARNING! RNEMD Type Unknown!\n";
123 >      sprintf(painCave.errMsg,
124 >              "RNEMD: The current RNEMD_exchangeType,\n"
125 >              "\t\t%s\n"
126 >              "\tis not one of the recognized exchange types.\n",
127 >              st.c_str());
128 >      painCave.isFatal = 1;
129 >      painCave.severity = OPENMD_ERROR;
130 >      simError();
131      }
132 +    
133 +    outputTemp_ = false;
134 +    if (rnemdParams->haveOutputTemperature()) {
135 +      outputTemp_ = rnemdParams->getOutputTemperature();
136 +    } else if ((rnemdType_ == rnemdKineticSwap) ||
137 +               (rnemdType_ == rnemdKineticScale) ||
138 +               (rnemdType_ == rnemdKineticScaleVAM) ||
139 +               (rnemdType_ == rnemdKineticScaleAM)) {
140 +      outputTemp_ = true;
141 +    }
142 +    outputVx_ = false;
143 +    if (rnemdParams->haveOutputVx()) {
144 +      outputVx_ = rnemdParams->getOutputVx();
145 +    } else if ((rnemdType_ == rnemdPx) || (rnemdType_ == rnemdPxScale)) {
146 +      outputVx_ = true;
147 +    }
148 +    outputVy_ = false;
149 +    if (rnemdParams->haveOutputVy()) {
150 +      outputVy_ = rnemdParams->getOutputVy();
151 +    } else if ((rnemdType_ == rnemdPy) || (rnemdType_ == rnemdPyScale)) {
152 +      outputVy_ = true;
153 +    }
154 +    output3DTemp_ = false;
155 +    if (rnemdParams->haveOutputXyzTemperature()) {
156 +      output3DTemp_ = rnemdParams->getOutputXyzTemperature();
157 +    }
158 +    outputRotTemp_ = false;
159 +    if (rnemdParams->haveOutputRotTemperature()) {
160 +      outputRotTemp_ = rnemdParams->getOutputRotTemperature();
161 +    }
162 +    // James put this in.
163 +    outputDen_ = false;
164 +    if (rnemdParams->haveOutputDen()) {
165 +      outputDen_ = rnemdParams->getOutputDen();
166 +    }
167 +    outputAh_ = false;
168 +    if (rnemdParams->haveOutputAh()) {
169 +      outputAh_ = rnemdParams->getOutputAh();
170 +    }    
171 +    outputVz_ = false;
172 +    if (rnemdParams->haveOutputVz()) {
173 +      outputVz_ = rnemdParams->getOutputVz();
174 +    } else if ((rnemdType_ == rnemdPz) || (rnemdType_ == rnemdPzScale)) {
175 +      outputVz_ = true;
176 +    }
177 +    
178  
179   #ifdef IS_MPI
180      if (worldRank == 0) {
181   #endif
182  
183 <      std::string rnemdFileName;
184 <      std::string xTempFileName;
185 <      std::string yTempFileName;
186 <      std::string zTempFileName;
122 <      switch(rnemdType_) {
123 <      case rnemdKineticSwap :
124 <      case rnemdKineticScale :
183 >      //may have rnemdWriter separately
184 >      string rnemdFileName;
185 >
186 >      if (outputTemp_) {
187          rnemdFileName = "temperature.log";
188 <        break;
127 <      case rnemdPx :
128 <      case rnemdPxScale :
129 <      case rnemdPy :
130 <      case rnemdPyScale :
131 <        rnemdFileName = "momemtum.log";
132 <        xTempFileName = "temperatureX.log";
133 <        yTempFileName = "temperatureY.log";
134 <        zTempFileName = "temperatureZ.log";
135 <        xTempLog_.open(xTempFileName.c_str());
136 <        yTempLog_.open(yTempFileName.c_str());
137 <        zTempLog_.open(zTempFileName.c_str());
138 <        break;
139 <      case rnemdPz :
140 <      case rnemdPzScale :
141 <      case rnemdUnknown :
142 <      default :
143 <        rnemdFileName = "rnemd.log";
144 <        break;
188 >        tempLog_.open(rnemdFileName.c_str());
189        }
190 <      rnemdLog_.open(rnemdFileName.c_str());
190 >      if (outputVx_) {
191 >        rnemdFileName = "velocityX.log";
192 >        vxzLog_.open(rnemdFileName.c_str());
193 >      }
194 >      if (outputVy_) {
195 >        rnemdFileName = "velocityY.log";
196 >        vyzLog_.open(rnemdFileName.c_str());
197 >      }
198  
199 +      if (output3DTemp_) {
200 +        rnemdFileName = "temperatureX.log";
201 +        xTempLog_.open(rnemdFileName.c_str());
202 +        rnemdFileName = "temperatureY.log";
203 +        yTempLog_.open(rnemdFileName.c_str());
204 +        rnemdFileName = "temperatureZ.log";
205 +        zTempLog_.open(rnemdFileName.c_str());
206 +      }
207 +      if (outputRotTemp_) {
208 +        rnemdFileName = "temperatureR.log";
209 +        rotTempLog_.open(rnemdFileName.c_str());
210 +      }
211 +      
212 +      //James put this in
213 +      if (outputDen_) {
214 +        rnemdFileName = "Density.log";
215 +        denLog_.open(rnemdFileName.c_str());
216 +      }
217 +      if (outputAh_) {
218 +        rnemdFileName = "Ah.log";
219 +        AhLog_.open(rnemdFileName.c_str());
220 +      }
221 +      if (outputVz_) {
222 +        rnemdFileName = "velocityZ.log";
223 +        vzzLog_.open(rnemdFileName.c_str());
224 +      }
225 +      logFrameCount_ = 0;
226   #ifdef IS_MPI
227      }
228   #endif
229  
230 <    set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
231 <    set_RNEMD_nBins(simParams->getRNEMD_nBins());
230 >    set_RNEMD_exchange_time(rnemdParams->getExchangeTime());
231 >    set_RNEMD_nBins(rnemdParams->getNbins());
232      midBin_ = nBins_ / 2;
233 <    if (simParams->haveRNEMD_logWidth()) {
234 <      rnemdLogWidth_ = simParams->getRNEMD_logWidth();
233 >    if (rnemdParams->haveBinShift()) {
234 >      if (rnemdParams->getBinShift()) {
235 >        zShift_ = 0.5 / (RealType)(nBins_);
236 >      } else {
237 >        zShift_ = 0.0;
238 >      }
239 >    } else {
240 >      zShift_ = 0.0;
241 >    }
242 >    //cerr << "I shift slabs by " << zShift_ << " Lz\n";
243 >    //shift slabs by half slab width, maybe useful in heterogeneous systems
244 >    //set to 0.0 if not using it; N/A in status output yet
245 >    if (rnemdParams->haveLogWidth()) {
246 >      set_RNEMD_logWidth(rnemdParams->getLogWidth());
247 >      /*arbitary rnemdLogWidth_, no checking;
248        if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
249 <        std::cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
250 <        std::cerr << "Automaically set back to default.\n";
249 >        cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
250 >        cerr << "Automaically set back to default.\n";
251          rnemdLogWidth_ = nBins_;
252 <      }
252 >      }*/
253      } else {
254 <      rnemdLogWidth_ = nBins_;
254 >      set_RNEMD_logWidth(nBins_);
255      }
256 <    valueHist_.resize(rnemdLogWidth_, 0.0);
257 <    valueCount_.resize(rnemdLogWidth_, 0);
256 >    tempHist_.resize(rnemdLogWidth_, 0.0);
257 >    tempCount_.resize(rnemdLogWidth_, 0);
258 >    pxzHist_.resize(rnemdLogWidth_, 0.0);
259 >    //vxzCount_.resize(rnemdLogWidth_, 0);
260 >    pyzHist_.resize(rnemdLogWidth_, 0.0);
261 >    //vyzCount_.resize(rnemdLogWidth_, 0);
262 >
263 >    mHist_.resize(rnemdLogWidth_, 0.0);
264      xTempHist_.resize(rnemdLogWidth_, 0.0);
265      yTempHist_.resize(rnemdLogWidth_, 0.0);
266      zTempHist_.resize(rnemdLogWidth_, 0.0);
267 +    xyzTempCount_.resize(rnemdLogWidth_, 0);
268 +    rotTempHist_.resize(rnemdLogWidth_, 0.0);
269 +    rotTempCount_.resize(rnemdLogWidth_, 0);
270 +    // James put this in
271 +    DenHist_.resize(rnemdLogWidth_, 0.0);
272 +    pzzHist_.resize(rnemdLogWidth_, 0.0);
273  
274      set_RNEMD_exchange_total(0.0);
275 <    if (simParams->haveRNEMD_targetFlux()) {
276 <      set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
275 >    if (rnemdParams->haveTargetFlux()) {
276 >      set_RNEMD_target_flux(rnemdParams->getTargetFlux());
277      } else {
278        set_RNEMD_target_flux(0.0);
279      }
280 +    if (rnemdParams->haveTargetJzKE()) {
281 +      set_RNEMD_target_JzKE(rnemdParams->getTargetJzKE());
282 +    } else {
283 +      set_RNEMD_target_JzKE(0.0);
284 +    }
285 +    if (rnemdParams->haveTargetJzpx()) {
286 +      set_RNEMD_target_jzpx(rnemdParams->getTargetJzpx());
287 +    } else {
288 +      set_RNEMD_target_jzpx(0.0);
289 +    }
290 +    jzp_.x() = targetJzpx_;
291 +    njzp_.x() = -targetJzpx_;
292 +    if (rnemdParams->haveTargetJzpy()) {
293 +      set_RNEMD_target_jzpy(rnemdParams->getTargetJzpy());
294 +    } else {
295 +      set_RNEMD_target_jzpy(0.0);
296 +    }
297 +    jzp_.y() = targetJzpy_;
298 +    njzp_.y() = -targetJzpy_;
299 +    if (rnemdParams->haveTargetJzpz()) {
300 +      set_RNEMD_target_jzpz(rnemdParams->getTargetJzpz());
301 +    } else {
302 +      set_RNEMD_target_jzpz(0.0);
303 +    }
304 +    jzp_.z() = targetJzpz_;
305 +    njzp_.z() = -targetJzpz_;
306  
307   #ifndef IS_MPI
308      if (simParams->haveSeed()) {
# Line 198 | Line 327 | namespace OpenMD {
327   #ifdef IS_MPI
328      if (worldRank == 0) {
329   #endif
330 <      std::cerr << "total fail trials: " << failTrialCount_ << "\n";
331 <      rnemdLog_.close();
332 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale || rnemdType_ == rnemdPyScale)
333 <        std::cerr<< "total root-checking warnings: " << failRootCount_ << "\n";
334 <      if (rnemdType_ == rnemdPx || rnemdType_ == rnemdPxScale || rnemdType_ == rnemdPy || rnemdType_ == rnemdPyScale) {
330 >      
331 >      sprintf(painCave.errMsg,
332 >              "RNEMD: total failed trials: %d\n",
333 >              failTrialCount_);
334 >      painCave.isFatal = 0;
335 >      painCave.severity = OPENMD_INFO;
336 >      simError();
337 >      
338 >      if (outputTemp_) tempLog_.close();
339 >      if (outputVx_)   vxzLog_.close();
340 >      if (outputVy_)   vyzLog_.close();
341 >
342 >      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
343 >          rnemdType_ == rnemdPyScale) {
344 >        sprintf(painCave.errMsg,
345 >                "RNEMD: total root-checking warnings: %d\n",
346 >                failRootCount_);
347 >        painCave.isFatal = 0;
348 >        painCave.severity = OPENMD_INFO;
349 >        simError();
350 >      }
351 >      if (output3DTemp_) {
352          xTempLog_.close();
353          yTempLog_.close();
354          zTempLog_.close();
355        }
356 +      if (outputRotTemp_) rotTempLog_.close();
357 +      // James put this in
358 +      if (outputDen_) denLog_.close();
359 +      if (outputAh_)  AhLog_.close();
360 +      if (outputVz_)  vzzLog_.close();
361 +      
362   #ifdef IS_MPI
363      }
364   #endif
# Line 246 | Line 398 | namespace OpenMD {
398        // which bin is this stuntdouble in?
399        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
400  
401 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
401 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
402  
403  
404        // if we're in bin 0 or the middleBin
# Line 259 | Line 411 | namespace OpenMD {
411          switch(rnemdType_) {
412          case rnemdKineticSwap :
413            
414 <          value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
415 <                          vel[2]*vel[2]);
416 <          if (sd->isDirectional()) {
414 >          value = mass * vel.lengthSquare();
415 >          
416 >          if (sd->isDirectional()) {
417              Vector3d angMom = sd->getJ();
418              Mat3x3d I = sd->getI();
419              
420              if (sd->isLinear()) {
421 <              int i = sd->linearAxis();
422 <              int j = (i + 1) % 3;
423 <              int k = (i + 2) % 3;
424 <              value += angMom[j] * angMom[j] / I(j, j) +
425 <                angMom[k] * angMom[k] / I(k, k);
421 >              int i = sd->linearAxis();
422 >              int j = (i + 1) % 3;
423 >              int k = (i + 2) % 3;
424 >              value += angMom[j] * angMom[j] / I(j, j) +
425 >                angMom[k] * angMom[k] / I(k, k);
426              } else {                        
427 <              value += angMom[0]*angMom[0]/I(0, 0)
428 <                + angMom[1]*angMom[1]/I(1, 1)
429 <                + angMom[2]*angMom[2]/I(2, 2);
427 >              value += angMom[0]*angMom[0]/I(0, 0)
428 >                + angMom[1]*angMom[1]/I(1, 1)
429 >                + angMom[2]*angMom[2]/I(2, 2);
430              }
431 <          }
431 >          } //angular momenta exchange enabled
432 >          //energyConvert temporarily disabled
433            //make exchangeSum_ comparable between swap & scale
281          //temporarily without using energyConvert
434            //value = value * 0.5 / PhysicalConstants::energyConvert;
435            value *= 0.5;
436            break;
# Line 331 | Line 483 | namespace OpenMD {
483      bool my_max_found = max_found;
484  
485      // Even if we didn't find a minimum, did someone else?
486 <    MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found,
335 <                              1, MPI::BOOL, MPI::LAND);
336 <    
486 >    MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found, 1, MPI::BOOL, MPI::LOR);
487      // Even if we didn't find a maximum, did someone else?
488 <    MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found,
489 <                              1, MPI::BOOL, MPI::LAND);
490 <    
491 <    struct {
492 <      RealType val;
493 <      int rank;
494 <    } max_vals, min_vals;
495 <    
496 <    if (min_found) {
497 <      if (my_min_found)
488 >    MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found, 1, MPI::BOOL, MPI::LOR);
489 > #endif
490 >
491 >    if (max_found && min_found) {
492 >
493 > #ifdef IS_MPI
494 >      struct {
495 >        RealType val;
496 >        int rank;
497 >      } max_vals, min_vals;
498 >      
499 >      if (my_min_found) {
500          min_vals.val = min_val;
501 <      else
501 >      } else {
502          min_vals.val = HONKING_LARGE_VALUE;
503 <      
503 >      }
504        min_vals.rank = worldRank;    
505        
506        // Who had the minimum?
507        MPI::COMM_WORLD.Allreduce(&min_vals, &min_vals,
508                                  1, MPI::REALTYPE_INT, MPI::MINLOC);
509        min_val = min_vals.val;
358    }
510        
511 <    if (max_found) {
361 <      if (my_max_found)
511 >      if (my_max_found) {
512          max_vals.val = max_val;
513 <      else
513 >      } else {
514          max_vals.val = -HONKING_LARGE_VALUE;
515 <      
515 >      }
516        max_vals.rank = worldRank;    
517        
518        // Who had the maximum?
519        MPI::COMM_WORLD.Allreduce(&max_vals, &max_vals,
520                                  1, MPI::REALTYPE_INT, MPI::MAXLOC);
521        max_val = max_vals.val;
372    }
522   #endif
523 <
524 <    if (max_found && min_found) {
525 <      if (min_val< max_val) {
377 <
523 >      
524 >      if (min_val < max_val) {
525 >        
526   #ifdef IS_MPI      
527          if (max_vals.rank == worldRank && min_vals.rank == worldRank) {
528            // I have both maximum and minimum, so proceed like a single
529            // processor version:
530   #endif
531 <          // objects to be swapped: velocity & angular velocity
531 >
532            Vector3d min_vel = min_sd->getVel();
533            Vector3d max_vel = max_sd->getVel();
534            RealType temp_vel;
# Line 389 | Line 537 | namespace OpenMD {
537            case rnemdKineticSwap :
538              min_sd->setVel(max_vel);
539              max_sd->setVel(min_vel);
540 <            if (min_sd->isDirectional() && max_sd->isDirectional()) {
540 >            if (min_sd->isDirectional() && max_sd->isDirectional()) {
541                Vector3d min_angMom = min_sd->getJ();
542                Vector3d max_angMom = max_sd->getJ();
543                min_sd->setJ(max_angMom);
544                max_sd->setJ(min_angMom);
545 <            }
545 >            }//angular momenta exchange enabled
546 >            //assumes same rigid body identity
547              break;
548            case rnemdPx :
549              temp_vel = min_vel.x();
# Line 420 | Line 569 | namespace OpenMD {
569            default :
570              break;
571            }
572 +
573   #ifdef IS_MPI
574            // the rest of the cases only apply in parallel simulations:
575          } else if (max_vals.rank == worldRank) {
# Line 438 | Line 588 | namespace OpenMD {
588            switch(rnemdType_) {
589            case rnemdKineticSwap :
590              max_sd->setVel(min_vel);
591 <            
591 >            //angular momenta exchange enabled
592              if (max_sd->isDirectional()) {
593                Vector3d min_angMom;
594                Vector3d max_angMom = max_sd->getJ();
595 <
595 >              
596                // point-to-point swap of the angular momentum vector
597                MPI::COMM_WORLD.Sendrecv(max_angMom.getArrayPointer(), 3,
598                                         MPI::REALTYPE, min_vals.rank, 1,
599                                         min_angMom.getArrayPointer(), 3,
600                                         MPI::REALTYPE, min_vals.rank, 1,
601                                         status);
602 <
602 >              
603                max_sd->setJ(min_angMom);
604 <            }
604 >            }
605              break;
606            case rnemdPx :
607              max_vel.x() = min_vel.x();
# Line 484 | Line 634 | namespace OpenMD {
634            switch(rnemdType_) {
635            case rnemdKineticSwap :
636              min_sd->setVel(max_vel);
637 <            
637 >            //angular momenta exchange enabled
638              if (min_sd->isDirectional()) {
639                Vector3d min_angMom = min_sd->getJ();
640                Vector3d max_angMom;
641 <
641 >              
642                // point-to-point swap of the angular momentum vector
643                MPI::COMM_WORLD.Sendrecv(min_angMom.getArrayPointer(), 3,
644                                         MPI::REALTYPE, max_vals.rank, 1,
645                                         max_angMom.getArrayPointer(), 3,
646                                         MPI::REALTYPE, max_vals.rank, 1,
647                                         status);
648 <
648 >              
649                min_sd->setJ(max_angMom);
650              }
651              break;
# Line 517 | Line 667 | namespace OpenMD {
667          }
668   #endif
669          exchangeSum_ += max_val - min_val;
670 <      } else {
671 <        std::cerr << "exchange NOT performed!\nmin_val > max_val.\n";
670 >      } else {        
671 >        sprintf(painCave.errMsg,
672 >                "RNEMD: exchange NOT performed because min_val > max_val\n");
673 >        painCave.isFatal = 0;
674 >        painCave.severity = OPENMD_INFO;
675 >        simError();        
676          failTrialCount_++;
677        }
678      } else {
679 <      std::cerr << "exchange NOT performed!\n";
680 <      std::cerr << "at least one of the two slabs empty.\n";
679 >      sprintf(painCave.errMsg,
680 >              "RNEMD: exchange NOT performed because selected object\n"
681 >              "\tnot present in at least one of the two slabs.\n");
682 >      painCave.isFatal = 0;
683 >      painCave.severity = OPENMD_INFO;
684 >      simError();        
685        failTrialCount_++;
686      }
687      
# Line 540 | Line 698 | namespace OpenMD {
698      StuntDouble* sd;
699      int idx;
700  
701 <    std::vector<StuntDouble*> hotBin, coldBin;
701 >    vector<StuntDouble*> hotBin, coldBin;
702  
703      RealType Phx = 0.0;
704      RealType Phy = 0.0;
# Line 548 | Line 706 | namespace OpenMD {
706      RealType Khx = 0.0;
707      RealType Khy = 0.0;
708      RealType Khz = 0.0;
709 +    RealType Khw = 0.0;
710      RealType Pcx = 0.0;
711      RealType Pcy = 0.0;
712      RealType Pcz = 0.0;
713      RealType Kcx = 0.0;
714      RealType Kcy = 0.0;
715      RealType Kcz = 0.0;
716 +    RealType Kcw = 0.0;
717  
718      for (sd = seleMan_.beginSelected(selei); sd != NULL;
719           sd = seleMan_.nextSelected(selei)) {
# Line 570 | Line 730 | namespace OpenMD {
730        // which bin is this stuntdouble in?
731        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
732  
733 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
733 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
734  
735        // if we're in bin 0 or the middleBin
736        if (binNo == 0 || binNo == midBin_) {
# Line 586 | Line 746 | namespace OpenMD {
746            Khx += mass * vel.x() * vel.x();
747            Khy += mass * vel.y() * vel.y();
748            Khz += mass * vel.z() * vel.z();
749 +          //if (rnemdType_ == rnemdKineticScaleVAM) {
750 +          if (sd->isDirectional()) {
751 +            Vector3d angMom = sd->getJ();
752 +            Mat3x3d I = sd->getI();
753 +            if (sd->isLinear()) {
754 +              int i = sd->linearAxis();
755 +              int j = (i + 1) % 3;
756 +              int k = (i + 2) % 3;
757 +              Khw += angMom[j] * angMom[j] / I(j, j) +
758 +                angMom[k] * angMom[k] / I(k, k);
759 +            } else {
760 +              Khw += angMom[0]*angMom[0]/I(0, 0)
761 +                + angMom[1]*angMom[1]/I(1, 1)
762 +                + angMom[2]*angMom[2]/I(2, 2);
763 +            }
764 +          }
765 +          //}
766          } else { //midBin_
767            coldBin.push_back(sd);
768            Pcx += mass * vel.x();
# Line 594 | Line 771 | namespace OpenMD {
771            Kcx += mass * vel.x() * vel.x();
772            Kcy += mass * vel.y() * vel.y();
773            Kcz += mass * vel.z() * vel.z();
774 +          //if (rnemdType_ == rnemdKineticScaleVAM) {
775 +          if (sd->isDirectional()) {
776 +            Vector3d angMom = sd->getJ();
777 +            Mat3x3d I = sd->getI();
778 +            if (sd->isLinear()) {
779 +              int i = sd->linearAxis();
780 +              int j = (i + 1) % 3;
781 +              int k = (i + 2) % 3;
782 +              Kcw += angMom[j] * angMom[j] / I(j, j) +
783 +                angMom[k] * angMom[k] / I(k, k);
784 +            } else {
785 +              Kcw += angMom[0]*angMom[0]/I(0, 0)
786 +                + angMom[1]*angMom[1]/I(1, 1)
787 +                + angMom[2]*angMom[2]/I(2, 2);
788 +            }
789 +          }
790 +          //}
791          }
792        }
793      }
794 <
794 >    
795      Khx *= 0.5;
796      Khy *= 0.5;
797      Khz *= 0.5;
798 +    Khw *= 0.5;
799      Kcx *= 0.5;
800      Kcy *= 0.5;
801      Kcz *= 0.5;
802 +    Kcw *= 0.5;
803  
804 +    // std::cerr << "Khx= " << Khx << "\tKhy= " << Khy << "\tKhz= " << Khz
805 +    //        << "\tKhw= " << Khw << "\tKcx= " << Kcx << "\tKcy= " << Kcy
806 +    //        << "\tKcz= " << Kcz << "\tKcw= " << Kcw << "\n";
807 +    // std::cerr << "Phx= " << Phx << "\tPhy= " << Phy << "\tPhz= " << Phz
808 +    //        << "\tPcx= " << Pcx << "\tPcy= " << Pcy << "\tPcz= " <<Pcz<<"\n";
809 +
810   #ifdef IS_MPI
811      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
812      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
# Line 616 | Line 818 | namespace OpenMD {
818      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
819      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
820      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
821 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khw, 1, MPI::REALTYPE, MPI::SUM);
822 +
823      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
824      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
825      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
826 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcw, 1, MPI::REALTYPE, MPI::SUM);
827   #endif
828  
829 <    //use coldBin coeff's
829 >    //solve coldBin coeff's first
830      RealType px = Pcx / Phx;
831      RealType py = Pcy / Phy;
832      RealType pz = Pcz / Phz;
833 +    RealType c, x, y, z;
834 +    bool successfulScale = false;
835 +    if ((rnemdType_ == rnemdKineticScaleVAM) ||
836 +        (rnemdType_ == rnemdKineticScaleAM)) {
837 +      //may need sanity check Khw & Kcw > 0
838  
839 <    RealType a000, a110, c0, a001, a111, b01, b11, c1, c;
840 <    switch(rnemdType_) {
841 <    case rnemdKineticScale :
842 <    /*used hotBin coeff's & only scale x & y dimensions
843 <      RealType px = Phx / Pcx;
634 <      RealType py = Phy / Pcy;
635 <      a110 = Khy;
636 <      c0 = - Khx - Khy - targetFlux_;
637 <      a000 = Khx;
638 <      a111 = Kcy * py * py
639 <      b11 = -2.0 * Kcy * py * (1.0 + py);
640 <      c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
641 <      b01 = -2.0 * Kcx * px * (1.0 + px);
642 <      a001 = Kcx * px * px;
643 <    */
839 >      if (rnemdType_ == rnemdKineticScaleVAM) {
840 >        c = 1.0 - targetFlux_ / (Kcx + Kcy + Kcz + Kcw);
841 >      } else {
842 >        c = 1.0 - targetFlux_ / Kcw;
843 >      }
844  
845 <      //scale all three dimensions, let c_x = c_y
846 <      a000 = Kcx + Kcy;
847 <      a110 = Kcz;
848 <      c0 = targetFlux_ - Kcx - Kcy - Kcz;
849 <      a001 = Khx * px * px + Khy * py * py;
850 <      a111 = Khz * pz * pz;
851 <      b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
852 <      b11 = -2.0 * Khz * pz * (1.0 + pz);
853 <      c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
854 <         + Khz * pz * (2.0 + pz) - targetFlux_;
855 <      break;
856 <    case rnemdPxScale :
857 <      c = 1 - targetFlux_ / Pcx;
858 <      a000 = Kcy;
859 <      a110 = Kcz;
860 <      c0 = Kcx * c * c - Kcx - Kcy - Kcz;
861 <      a001 = py * py * Khy;
862 <      a111 = pz * pz * Khz;
863 <      b01 = -2.0 * Khy * py * (1.0 + py);
864 <      b11 = -2.0 * Khz * pz * (1.0 + pz);
865 <      c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
866 <         + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
867 <      break;
868 <    case rnemdPyScale :
869 <      c = 1 - targetFlux_ / Pcy;
870 <      a000 = Kcx;
871 <      a110 = Kcz;
872 <      c0 = Kcy * c * c - Kcx - Kcy - Kcz;
873 <      a001 = px * px * Khx;
874 <      a111 = pz * pz * Khz;
875 <      b01 = -2.0 * Khx * px * (1.0 + px);
876 <      b11 = -2.0 * Khz * pz * (1.0 + pz);
877 <      c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
878 <         + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
879 <      break;
880 <    case rnemdPzScale ://we don't really do this, do we?
881 <      c = 1 - targetFlux_ / Pcz;
882 <      a000 = Kcx;
883 <      a110 = Kcy;
884 <      c0 = Kcz * c * c - Kcx - Kcy - Kcz;
885 <      a001 = px * px * Khx;
886 <      a111 = py * py * Khy;
887 <      b01 = -2.0 * Khx * px * (1.0 + px);
888 <      b11 = -2.0 * Khy * py * (1.0 + py);
889 <      c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
890 <        + Khz * (fastpow(c * pz - pz - 1.0, 2) - 1.0);
891 <      break;      
892 <    default :
893 <      break;
894 <    }
895 <
896 <    RealType v1 = a000 * a111 - a001 * a110;
897 <    RealType v2 = a000 * b01;
898 <    RealType v3 = a000 * b11;
899 <    RealType v4 = a000 * c1 - a001 * c0;
900 <    RealType v8 = a110 * b01;
901 <    RealType v10 = - b01 * c0;
902 <
903 <    RealType u0 = v2 * v10 - v4 * v4;
904 <    RealType u1 = -2.0 * v3 * v4;
905 <    RealType u2 = -v2 * v8 - v3 * v3 - 2.0 * v1 * v4;
906 <    RealType u3 = -2.0 * v1 * v3;
907 <    RealType u4 = - v1 * v1;
908 <    //rescale coefficients
909 <    RealType maxAbs = fabs(u0);
910 <    if (maxAbs < fabs(u1)) maxAbs = fabs(u1);
911 <    if (maxAbs < fabs(u2)) maxAbs = fabs(u2);
712 <    if (maxAbs < fabs(u3)) maxAbs = fabs(u3);
713 <    if (maxAbs < fabs(u4)) maxAbs = fabs(u4);
714 <    u0 /= maxAbs;
715 <    u1 /= maxAbs;
716 <    u2 /= maxAbs;
717 <    u3 /= maxAbs;
718 <    u4 /= maxAbs;
719 <    //max_element(start, end) is also available.
720 <    Polynomial<RealType> poly; //same as DoublePolynomial poly;
721 <    poly.setCoefficient(4, u4);
722 <    poly.setCoefficient(3, u3);
723 <    poly.setCoefficient(2, u2);
724 <    poly.setCoefficient(1, u1);
725 <    poly.setCoefficient(0, u0);
726 <    std::vector<RealType> realRoots = poly.FindRealRoots();
727 <
728 <    std::vector<RealType>::iterator ri;
729 <    RealType r1, r2, alpha0;
730 <    std::vector<std::pair<RealType,RealType> > rps;
731 <    for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
732 <      r2 = *ri;
733 <      //check if FindRealRoots() give the right answer
734 <      if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
735 <        sprintf(painCave.errMsg,
736 <                "RNEMD Warning: polynomial solve seems to have an error!");
737 <        painCave.isFatal = 0;
738 <        simError();
739 <        failRootCount_++;
845 >      if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
846 >        c = sqrt(c);
847 >        std::cerr << "cold slab scaling coefficient: " << c << endl;
848 >        //now convert to hotBin coefficient
849 >        RealType w = 0.0;
850 >        if (rnemdType_ ==  rnemdKineticScaleVAM) {
851 >          x = 1.0 + px * (1.0 - c);
852 >          y = 1.0 + py * (1.0 - c);
853 >          z = 1.0 + pz * (1.0 - c);
854 >          /* more complicated way
855 >             w = 1.0 + (Kcw - Kcw * c * c - (c * c * (Kcx + Kcy + Kcz
856 >             + Khx * px * px + Khy * py * py + Khz * pz * pz)
857 >             - 2.0 * c * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py)
858 >             + Khz * pz * (1.0 + pz)) + Khx * px * (2.0 + px)
859 >             + Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
860 >             - Kcx - Kcy - Kcz)) / Khw; the following is simpler
861 >          */
862 >          if ((fabs(x - 1.0) < 0.1) && (fabs(y - 1.0) < 0.1) &&
863 >              (fabs(z - 1.0) < 0.1)) {
864 >            w = 1.0 + (targetFlux_ + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
865 >                       + Khz * (1.0 - z * z)) / Khw;
866 >          }//no need to calculate w if x, y or z is out of range
867 >        } else {
868 >          w = 1.0 + targetFlux_ / Khw;
869 >        }
870 >        if ((w > 0.81) && (w < 1.21)) {//restrict scaling coefficients
871 >          //if w is in the right range, so should be x, y, z.
872 >          vector<StuntDouble*>::iterator sdi;
873 >          Vector3d vel;
874 >          for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
875 >            if (rnemdType_ == rnemdKineticScaleVAM) {
876 >              vel = (*sdi)->getVel() * c;
877 >              //vel.x() *= c;
878 >              //vel.y() *= c;
879 >              //vel.z() *= c;
880 >              (*sdi)->setVel(vel);
881 >            }
882 >            if ((*sdi)->isDirectional()) {
883 >              Vector3d angMom = (*sdi)->getJ() * c;
884 >              //angMom[0] *= c;
885 >              //angMom[1] *= c;
886 >              //angMom[2] *= c;
887 >              (*sdi)->setJ(angMom);
888 >            }
889 >          }
890 >          w = sqrt(w);
891 >          std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
892 >                    << "\twh= " << w << endl;
893 >          for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
894 >            if (rnemdType_ == rnemdKineticScaleVAM) {
895 >              vel = (*sdi)->getVel();
896 >              vel.x() *= x;
897 >              vel.y() *= y;
898 >              vel.z() *= z;
899 >              (*sdi)->setVel(vel);
900 >            }
901 >            if ((*sdi)->isDirectional()) {
902 >              Vector3d angMom = (*sdi)->getJ() * w;
903 >              //angMom[0] *= w;
904 >              //angMom[1] *= w;
905 >              //angMom[2] *= w;
906 >              (*sdi)->setJ(angMom);
907 >            }
908 >          }
909 >          successfulScale = true;
910 >          exchangeSum_ += targetFlux_;
911 >        }
912        }
913 <      //might not be useful w/o rescaling coefficients
914 <      alpha0 = -c0 - a110 * r2 * r2;
915 <      if (alpha0 >= 0.0) {
916 <        r1 = sqrt(alpha0 / a000);
917 <        if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111)) < 1e-6)
918 <          { rps.push_back(std::make_pair(r1, r2)); }
919 <        if (r1 > 1e-6) { //r1 non-negative
920 <          r1 = -r1;
921 <          if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111)) <1e-6)
922 <            { rps.push_back(std::make_pair(r1, r2)); }
923 <        }
913 >    } else {
914 >      RealType a000, a110, c0, a001, a111, b01, b11, c1;
915 >      switch(rnemdType_) {
916 >      case rnemdKineticScale :
917 >        /* used hotBin coeff's & only scale x & y dimensions
918 >           RealType px = Phx / Pcx;
919 >           RealType py = Phy / Pcy;
920 >           a110 = Khy;
921 >           c0 = - Khx - Khy - targetFlux_;
922 >           a000 = Khx;
923 >           a111 = Kcy * py * py;
924 >           b11 = -2.0 * Kcy * py * (1.0 + py);
925 >           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
926 >           b01 = -2.0 * Kcx * px * (1.0 + px);
927 >           a001 = Kcx * px * px;
928 >        */
929 >        //scale all three dimensions, let c_x = c_y
930 >        a000 = Kcx + Kcy;
931 >        a110 = Kcz;
932 >        c0 = targetFlux_ - Kcx - Kcy - Kcz;
933 >        a001 = Khx * px * px + Khy * py * py;
934 >        a111 = Khz * pz * pz;
935 >        b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
936 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
937 >        c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
938 >          + Khz * pz * (2.0 + pz) - targetFlux_;
939 >        break;
940 >      case rnemdPxScale :
941 >        c = 1 - targetFlux_ / Pcx;
942 >        a000 = Kcy;
943 >        a110 = Kcz;
944 >        c0 = Kcx * c * c - Kcx - Kcy - Kcz;
945 >        a001 = py * py * Khy;
946 >        a111 = pz * pz * Khz;
947 >        b01 = -2.0 * Khy * py * (1.0 + py);
948 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
949 >        c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
950 >          + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
951 >        break;
952 >      case rnemdPyScale :
953 >        c = 1 - targetFlux_ / Pcy;
954 >        a000 = Kcx;
955 >        a110 = Kcz;
956 >        c0 = Kcy * c * c - Kcx - Kcy - Kcz;
957 >        a001 = px * px * Khx;
958 >        a111 = pz * pz * Khz;
959 >        b01 = -2.0 * Khx * px * (1.0 + px);
960 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
961 >        c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
962 >          + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
963 >        break;
964 >      case rnemdPzScale ://we don't really do this, do we?
965 >        c = 1 - targetFlux_ / Pcz;
966 >        a000 = Kcx;
967 >        a110 = Kcy;
968 >        c0 = Kcz * c * c - Kcx - Kcy - Kcz;
969 >        a001 = px * px * Khx;
970 >        a111 = py * py * Khy;
971 >        b01 = -2.0 * Khx * px * (1.0 + px);
972 >        b11 = -2.0 * Khy * py * (1.0 + py);
973 >        c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
974 >          + Khz * (fastpow(c * pz - pz - 1.0, 2) - 1.0);
975 >        break;
976 >      default :
977 >        break;
978        }
979 <    }
980 <    // Consider combininig together the solving pair part w/ the searching
981 <    // best solution part so that we don't need the pairs vector
982 <    if (!rps.empty()) {
983 <      RealType smallestDiff = HONKING_LARGE_VALUE;
984 <      RealType diff;
985 <      std::pair<RealType,RealType> bestPair = std::make_pair(1.0, 1.0);
986 <      std::vector<std::pair<RealType,RealType> >::iterator rpi;
987 <      for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
988 <        r1 = (*rpi).first;
989 <        r2 = (*rpi).second;
990 <        switch(rnemdType_) {
991 <        case rnemdKineticScale :
992 <          diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
993 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
994 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
995 <          break;
996 <        case rnemdPxScale :
997 <          diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
998 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
999 <          break;
1000 <        case rnemdPyScale :
1001 <          diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1002 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1003 <          break;
1004 <        case rnemdPzScale :
1005 <        default :
1006 <          break;
1007 <        }
1008 <        if (diff < smallestDiff) {
1009 <          smallestDiff = diff;
1010 <          bestPair = *rpi;
1011 <        }
979 >      
980 >      RealType v1 = a000 * a111 - a001 * a110;
981 >      RealType v2 = a000 * b01;
982 >      RealType v3 = a000 * b11;
983 >      RealType v4 = a000 * c1 - a001 * c0;
984 >      RealType v8 = a110 * b01;
985 >      RealType v10 = - b01 * c0;
986 >      
987 >      RealType u0 = v2 * v10 - v4 * v4;
988 >      RealType u1 = -2.0 * v3 * v4;
989 >      RealType u2 = -v2 * v8 - v3 * v3 - 2.0 * v1 * v4;
990 >      RealType u3 = -2.0 * v1 * v3;
991 >      RealType u4 = - v1 * v1;
992 >      //rescale coefficients
993 >      RealType maxAbs = fabs(u0);
994 >      if (maxAbs < fabs(u1)) maxAbs = fabs(u1);
995 >      if (maxAbs < fabs(u2)) maxAbs = fabs(u2);
996 >      if (maxAbs < fabs(u3)) maxAbs = fabs(u3);
997 >      if (maxAbs < fabs(u4)) maxAbs = fabs(u4);
998 >      u0 /= maxAbs;
999 >      u1 /= maxAbs;
1000 >      u2 /= maxAbs;
1001 >      u3 /= maxAbs;
1002 >      u4 /= maxAbs;
1003 >      //max_element(start, end) is also available.
1004 >      Polynomial<RealType> poly; //same as DoublePolynomial poly;
1005 >      poly.setCoefficient(4, u4);
1006 >      poly.setCoefficient(3, u3);
1007 >      poly.setCoefficient(2, u2);
1008 >      poly.setCoefficient(1, u1);
1009 >      poly.setCoefficient(0, u0);
1010 >      vector<RealType> realRoots = poly.FindRealRoots();
1011 >      
1012 >      vector<RealType>::iterator ri;
1013 >      RealType r1, r2, alpha0;
1014 >      vector<pair<RealType,RealType> > rps;
1015 >      for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
1016 >        r2 = *ri;
1017 >        //check if FindRealRoots() give the right answer
1018 >        if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
1019 >          sprintf(painCave.errMsg,
1020 >                  "RNEMD Warning: polynomial solve seems to have an error!");
1021 >          painCave.isFatal = 0;
1022 >          simError();
1023 >          failRootCount_++;
1024 >        }
1025 >        //might not be useful w/o rescaling coefficients
1026 >        alpha0 = -c0 - a110 * r2 * r2;
1027 >        if (alpha0 >= 0.0) {
1028 >          r1 = sqrt(alpha0 / a000);
1029 >          if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
1030 >              < 1e-6)
1031 >            { rps.push_back(make_pair(r1, r2)); }
1032 >          if (r1 > 1e-6) { //r1 non-negative
1033 >            r1 = -r1;
1034 >            if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
1035 >                < 1e-6)
1036 >              { rps.push_back(make_pair(r1, r2)); }
1037 >          }
1038 >        }
1039        }
1040 +      // Consider combining together the solving pair part w/ the searching
1041 +      // best solution part so that we don't need the pairs vector
1042 +      if (!rps.empty()) {
1043 +        RealType smallestDiff = HONKING_LARGE_VALUE;
1044 +        RealType diff;
1045 +        pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
1046 +        vector<pair<RealType,RealType> >::iterator rpi;
1047 +        for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1048 +          r1 = (*rpi).first;
1049 +          r2 = (*rpi).second;
1050 +          switch(rnemdType_) {
1051 +          case rnemdKineticScale :
1052 +            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1053 +              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1054 +              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1055 +            break;
1056 +          case rnemdPxScale :
1057 +            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1058 +              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1059 +            break;
1060 +          case rnemdPyScale :
1061 +            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1062 +              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1063 +            break;
1064 +          case rnemdPzScale :
1065 +            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1066 +              + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1067 +          default :
1068 +            break;
1069 +          }
1070 +          if (diff < smallestDiff) {
1071 +            smallestDiff = diff;
1072 +            bestPair = *rpi;
1073 +          }
1074 +        }
1075   #ifdef IS_MPI
1076 <      if (worldRank == 0) {
1076 >        if (worldRank == 0) {
1077   #endif
1078 <        std::cerr << "we choose r1 = " << bestPair.first
1079 <                  << " and r2 = " << bestPair.second << "\n";
1078 >          sprintf(painCave.errMsg,
1079 >                  "RNEMD: roots r1= %lf\tr2 = %lf\n",
1080 >                  bestPair.first, bestPair.second);
1081 >          painCave.isFatal = 0;
1082 >          painCave.severity = OPENMD_INFO;
1083 >          simError();
1084   #ifdef IS_MPI
1085 <      }
1085 >        }
1086   #endif
1087 +        
1088 +        switch(rnemdType_) {
1089 +        case rnemdKineticScale :
1090 +          x = bestPair.first;
1091 +          y = bestPair.first;
1092 +          z = bestPair.second;
1093 +          break;
1094 +        case rnemdPxScale :
1095 +          x = c;
1096 +          y = bestPair.first;
1097 +          z = bestPair.second;
1098 +          break;
1099 +        case rnemdPyScale :
1100 +          x = bestPair.first;
1101 +          y = c;
1102 +          z = bestPair.second;
1103 +          break;
1104 +        case rnemdPzScale :
1105 +          x = bestPair.first;
1106 +          y = bestPair.second;
1107 +          z = c;
1108 +          break;          
1109 +        default :
1110 +          break;
1111 +        }
1112 +        vector<StuntDouble*>::iterator sdi;
1113 +        Vector3d vel;
1114 +        for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1115 +          vel = (*sdi)->getVel();
1116 +          vel.x() *= x;
1117 +          vel.y() *= y;
1118 +          vel.z() *= z;
1119 +          (*sdi)->setVel(vel);
1120 +        }
1121 +        //convert to hotBin coefficient
1122 +        x = 1.0 + px * (1.0 - x);
1123 +        y = 1.0 + py * (1.0 - y);
1124 +        z = 1.0 + pz * (1.0 - z);
1125 +        for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1126 +          vel = (*sdi)->getVel();
1127 +          vel.x() *= x;
1128 +          vel.y() *= y;
1129 +          vel.z() *= z;
1130 +          (*sdi)->setVel(vel);
1131 +        }
1132 +        successfulScale = true;
1133 +        exchangeSum_ += targetFlux_;
1134 +      }
1135 +    }
1136 +    if (successfulScale != true) {
1137 +      sprintf(painCave.errMsg,
1138 +              "RNEMD: exchange NOT performed!\n");
1139 +      painCave.isFatal = 0;
1140 +      painCave.severity = OPENMD_INFO;
1141 +      simError();        
1142 +      failTrialCount_++;
1143 +    }
1144 +  }
1145  
1146 <      RealType x, y, z;
1147 <        switch(rnemdType_) {
1148 <        case rnemdKineticScale :
1149 <          x = bestPair.first;
1150 <          y = bestPair.first;
1151 <          z = bestPair.second;
1152 <          break;
1153 <        case rnemdPxScale :
1154 <          x = c;
1155 <          y = bestPair.first;
1156 <          z = bestPair.second;
1157 <          break;
1158 <        case rnemdPyScale :
1159 <          x = bestPair.first;
1160 <          y = c;
1161 <          z = bestPair.second;
1162 <          break;
1163 <        case rnemdPzScale :
1164 <          x = bestPair.first;
1165 <          y = bestPair.second;
1166 <          z = c;
1167 <          break;          
1168 <        default :
1169 <          break;
1170 <        }
1171 <      std::vector<StuntDouble*>::iterator sdi;
1172 <      Vector3d vel;
1173 <      for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1174 <        vel = (*sdi)->getVel();
1175 <        vel.x() *= x;
1176 <        vel.y() *= y;
1177 <        vel.z() *= z;
1178 <        (*sdi)->setVel(vel);
1146 >  void RNEMD::doShiftScale() {
1147 >
1148 >    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1149 >    RealType time = currentSnap_->getTime();    
1150 >    Mat3x3d hmat = currentSnap_->getHmat();
1151 >
1152 >    seleMan_.setSelectionSet(evaluator_.evaluate());
1153 >
1154 >    int selei;
1155 >    StuntDouble* sd;
1156 >    int idx;
1157 >
1158 >    vector<StuntDouble*> hotBin, coldBin;
1159 >
1160 >    Vector3d Ph(V3Zero);
1161 >    RealType Mh = 0.0;
1162 >    RealType Kh = 0.0;
1163 >    Vector3d Pc(V3Zero);
1164 >    RealType Mc = 0.0;
1165 >    RealType Kc = 0.0;
1166 >    
1167 >
1168 >    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1169 >         sd = seleMan_.nextSelected(selei)) {
1170 >
1171 >      idx = sd->getLocalIndex();
1172 >
1173 >      Vector3d pos = sd->getPos();
1174 >
1175 >      // wrap the stuntdouble's position back into the box:
1176 >
1177 >      if (usePeriodicBoundaryConditions_)
1178 >        currentSnap_->wrapVector(pos);
1179 >
1180 >      // which bin is this stuntdouble in?
1181 >      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1182 >
1183 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
1184 >
1185 >      // if we're in bin 0 or the middleBin
1186 >      if (binNo == 0 || binNo == midBin_) {
1187 >        
1188 >        RealType mass = sd->getMass();
1189 >        Vector3d vel = sd->getVel();
1190 >      
1191 >        if (binNo == 0) {
1192 >          hotBin.push_back(sd);
1193 >          //std::cerr << "before, velocity = " << vel << endl;
1194 >          Ph += mass * vel;
1195 >          //std::cerr << "after, velocity = " << vel << endl;
1196 >          Mh += mass;
1197 >          Kh += mass * vel.lengthSquare();
1198 >          if (rnemdType_ == rnemdShiftScaleVAM) {
1199 >            if (sd->isDirectional()) {
1200 >              Vector3d angMom = sd->getJ();
1201 >              Mat3x3d I = sd->getI();
1202 >              if (sd->isLinear()) {
1203 >                int i = sd->linearAxis();
1204 >                int j = (i + 1) % 3;
1205 >                int k = (i + 2) % 3;
1206 >                Kh += angMom[j] * angMom[j] / I(j, j) +
1207 >                  angMom[k] * angMom[k] / I(k, k);
1208 >              } else {
1209 >                Kh += angMom[0] * angMom[0] / I(0, 0) +
1210 >                  angMom[1] * angMom[1] / I(1, 1) +
1211 >                  angMom[2] * angMom[2] / I(2, 2);
1212 >              }
1213 >            }
1214 >          }
1215 >        } else { //midBin_
1216 >          coldBin.push_back(sd);
1217 >          Pc += mass * vel;
1218 >          Mc += mass;
1219 >          Kc += mass * vel.lengthSquare();
1220 >          if (rnemdType_ == rnemdShiftScaleVAM) {
1221 >            if (sd->isDirectional()) {
1222 >              Vector3d angMom = sd->getJ();
1223 >              Mat3x3d I = sd->getI();
1224 >              if (sd->isLinear()) {
1225 >                int i = sd->linearAxis();
1226 >                int j = (i + 1) % 3;
1227 >                int k = (i + 2) % 3;
1228 >                Kc += angMom[j] * angMom[j] / I(j, j) +
1229 >                  angMom[k] * angMom[k] / I(k, k);
1230 >              } else {
1231 >                Kc += angMom[0] * angMom[0] / I(0, 0) +
1232 >                  angMom[1] * angMom[1] / I(1, 1) +
1233 >                  angMom[2] * angMom[2] / I(2, 2);
1234 >              }
1235 >            }
1236 >          }
1237 >        }
1238        }
1239 <      //convert to hotBin coefficient
1240 <      x = 1.0 + px * (1.0 - x);
1241 <      y = 1.0 + py * (1.0 - y);
1242 <      z = 1.0 + pz * (1.0 - z);
1243 <      for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1244 <        vel = (*sdi)->getVel();
1245 <        vel.x() *= x;
1246 <        vel.y() *= y;
1247 <        vel.z() *= z;
1248 <        (*sdi)->setVel(vel);
1239 >    }
1240 >    
1241 >    Kh *= 0.5;
1242 >    Kc *= 0.5;
1243 >
1244 >    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1245 >    //        << "\tKc= " << Kc << endl;
1246 >    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1247 >    
1248 > #ifdef IS_MPI
1249 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1250 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
1251 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mh, 1, MPI::REALTYPE, MPI::SUM);
1252 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kh, 1, MPI::REALTYPE, MPI::SUM);
1253 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mc, 1, MPI::REALTYPE, MPI::SUM);
1254 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kc, 1, MPI::REALTYPE, MPI::SUM);
1255 > #endif
1256 >
1257 >    bool successfulExchange = false;
1258 >    if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1259 >      Vector3d vc = Pc / Mc;
1260 >      Vector3d ac = njzp_ / Mc + vc;
1261 >      Vector3d acrec = njzp_ / Mc;
1262 >      RealType cNumerator = Kc - targetJzKE_ - 0.5 * Mc * ac.lengthSquare();
1263 >      if (cNumerator > 0.0) {
1264 >        RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1265 >        if (cDenominator > 0.0) {
1266 >          RealType c = sqrt(cNumerator / cDenominator);
1267 >          if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1268 >            Vector3d vh = Ph / Mh;
1269 >            Vector3d ah = jzp_ / Mh + vh;
1270 >            Vector3d ahrec = jzp_ / Mh;
1271 >            RealType hNumerator = Kh + targetJzKE_
1272 >              - 0.5 * Mh * ah.lengthSquare();
1273 >            if (hNumerator > 0.0) {
1274 >              RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1275 >              if (hDenominator > 0.0) {
1276 >                RealType h = sqrt(hNumerator / hDenominator);
1277 >                if ((h > 0.9) && (h < 1.1)) {
1278 >                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1279 >                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1280 >                  vector<StuntDouble*>::iterator sdi;
1281 >                  Vector3d vel;
1282 >                  for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1283 >                    //vel = (*sdi)->getVel();
1284 >                    vel = ((*sdi)->getVel() - vc) * c + ac;
1285 >                    (*sdi)->setVel(vel);
1286 >                    if (rnemdType_ == rnemdShiftScaleVAM) {
1287 >                      if ((*sdi)->isDirectional()) {
1288 >                        Vector3d angMom = (*sdi)->getJ() * c;
1289 >                        (*sdi)->setJ(angMom);
1290 >                      }
1291 >                    }
1292 >                  }
1293 >                  for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1294 >                    //vel = (*sdi)->getVel();
1295 >                    vel = ((*sdi)->getVel() - vh) * h + ah;
1296 >                    (*sdi)->setVel(vel);
1297 >                    if (rnemdType_ == rnemdShiftScaleVAM) {
1298 >                      if ((*sdi)->isDirectional()) {
1299 >                        Vector3d angMom = (*sdi)->getJ() * h;
1300 >                        (*sdi)->setJ(angMom);
1301 >                      }
1302 >                    }
1303 >                  }
1304 >                  successfulExchange = true;
1305 >                  exchangeSum_ += targetFlux_;
1306 >                  // this is a redundant variable for doShiftScale() so that
1307 >                  // RNEMD can output one exchange quantity needed in a job.
1308 >                  // need a better way to do this.
1309 >                  //cerr << "acx =" << ac.x() << "ahx =" << ah.x() << '\n';
1310 >                  //cerr << "acy =" << ac.y() << "ahy =" << ah.y() << '\n';
1311 >                  //cerr << "acz =" << ac.z() << "ahz =" << ah.z() << '\n';
1312 >                  Asum_ += (ahrec.z() - acrec.z());
1313 >                  Jsum_ += (jzp_.z()*((1/Mh)+(1/Mc)));
1314 >                  AhCount_ = ahrec.z();
1315 >                  if (outputAh_) {
1316 >                    AhLog_ << time << "   ";
1317 >                    AhLog_ << AhCount_;
1318 >                    AhLog_ << endl;
1319 >                  }              
1320 >                }
1321 >              }
1322 >            }
1323 >          }
1324 >        }
1325        }
1326 <      exchangeSum_ += targetFlux_;
1327 <      //we may want to check whether the exchange has been successful
1328 <    } else {
1329 <      std::cerr << "exchange NOT performed!\n";//MPI incompatible
1326 >    }
1327 >    if (successfulExchange != true) {
1328 >      //   sprintf(painCave.errMsg,
1329 >      //              "RNEMD: exchange NOT performed!\n");
1330 >      //   painCave.isFatal = 0;
1331 >      //   painCave.severity = OPENMD_INFO;
1332 >      //   simError();        
1333        failTrialCount_++;
1334      }
847
1335    }
1336  
1337    void RNEMD::doRNEMD() {
1338  
1339      switch(rnemdType_) {
1340      case rnemdKineticScale :
1341 +    case rnemdKineticScaleVAM :
1342 +    case rnemdKineticScaleAM :
1343      case rnemdPxScale :
1344      case rnemdPyScale :
1345      case rnemdPzScale :
# Line 862 | Line 1351 | namespace OpenMD {
1351      case rnemdPz :
1352        doSwap();
1353        break;
1354 +    case rnemdShiftScaleV :
1355 +    case rnemdShiftScaleVAM :
1356 +      doShiftScale();
1357 +      break;
1358      case rnemdUnknown :
1359      default :
1360        break;
# Line 879 | Line 1372 | namespace OpenMD {
1372      StuntDouble* sd;
1373      int idx;
1374  
1375 +    logFrameCount_++;
1376 +
1377 +    // alternative approach, track all molecules instead of only those
1378 +    // selected for scaling/swapping:
1379 +    /*
1380 +    SimInfo::MoleculeIterator miter;
1381 +    vector<StuntDouble*>::iterator iiter;
1382 +    Molecule* mol;
1383 +    StuntDouble* integrableObject;
1384 +    for (mol = info_->beginMolecule(miter); mol != NULL;
1385 +      mol = info_->nextMolecule(miter))
1386 +      integrableObject is essentially sd
1387 +        for (integrableObject = mol->beginIntegrableObject(iiter);
1388 +             integrableObject != NULL;
1389 +             integrableObject = mol->nextIntegrableObject(iiter))
1390 +    */
1391      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1392           sd = seleMan_.nextSelected(selei)) {
1393        
# Line 894 | Line 1403 | namespace OpenMD {
1403        // which bin is this stuntdouble in?
1404        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1405        
1406 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1407 <
1406 >      int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
1407 >        rnemdLogWidth_;
1408 >      // no symmetrization allowed due to arbitary rnemdLogWidth_
1409 >      /*
1410        if (rnemdLogWidth_ == midBin_ + 1)
1411          if (binNo > midBin_)
1412            binNo = nBins_ - binNo;
1413 <
1413 >      */
1414        RealType mass = sd->getMass();
1415 +      mHist_[binNo] += mass;
1416        Vector3d vel = sd->getVel();
1417        RealType value;
1418 <      RealType xVal, yVal, zVal;
1418 >      //RealType xVal, yVal, zVal;
1419  
1420 <      switch(rnemdType_) {
1421 <      case rnemdKineticSwap :
1422 <      case rnemdKineticScale :
911 <        
912 <        value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
913 <                        vel[2]*vel[2]);
914 <        
915 <        valueCount_[binNo] += 3;
1420 >      if (outputTemp_) {
1421 >        value = mass * vel.lengthSquare();
1422 >        tempCount_[binNo] += 3;
1423          if (sd->isDirectional()) {
1424            Vector3d angMom = sd->getJ();
1425            Mat3x3d I = sd->getI();
919          
1426            if (sd->isLinear()) {
1427              int i = sd->linearAxis();
1428              int j = (i + 1) % 3;
1429              int k = (i + 2) % 3;
1430              value += angMom[j] * angMom[j] / I(j, j) +
1431                angMom[k] * angMom[k] / I(k, k);
1432 <
927 <            valueCount_[binNo] +=2;
928 <
1432 >            tempCount_[binNo] +=2;
1433            } else {
1434 <            value += angMom[0]*angMom[0]/I(0, 0)
1435 <              + angMom[1]*angMom[1]/I(1, 1)
1436 <              + angMom[2]*angMom[2]/I(2, 2);
1437 <            valueCount_[binNo] +=3;
1434 >            value += angMom[0] * angMom[0] / I(0, 0) +
1435 >              angMom[1]*angMom[1]/I(1, 1) +
1436 >              angMom[2]*angMom[2]/I(2, 2);
1437 >            tempCount_[binNo] +=3;
1438            }
1439          }
1440 <        value = value / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1441 <
1442 <        break;
1443 <      case rnemdPx :
1444 <      case rnemdPxScale :
1440 >        value = value / PhysicalConstants::energyConvert
1441 >          / PhysicalConstants::kb;//may move to getStatus()
1442 >        tempHist_[binNo] += value;
1443 >      }
1444 >      if (outputVx_) {
1445          value = mass * vel[0];
1446 <        valueCount_[binNo]++;
1447 <        xVal = mass * vel.x() * vel.x() / PhysicalConstants::energyConvert
1448 <          / PhysicalConstants::kb;
1449 <        yVal = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
946 <          / PhysicalConstants::kb;
947 <        zVal = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
948 <          / PhysicalConstants::kb;
949 <        xTempHist_[binNo] += xVal;
950 <        yTempHist_[binNo] += yVal;
951 <        zTempHist_[binNo] += zVal;
952 <        break;
953 <      case rnemdPy :
954 <      case rnemdPyScale :
1446 >        //vxzCount_[binNo]++;
1447 >        pxzHist_[binNo] += value;
1448 >      }
1449 >      if (outputVy_) {
1450          value = mass * vel[1];
1451 <        valueCount_[binNo]++;
1452 <        break;
958 <      case rnemdPz :
959 <      case rnemdPzScale :
960 <        value = mass * vel[2];
961 <        valueCount_[binNo]++;
962 <        break;
963 <      case rnemdUnknown :
964 <      default :
965 <        break;
1451 >        //vyzCount_[binNo]++;
1452 >        pyzHist_[binNo] += value;
1453        }
967      valueHist_[binNo] += value;
968    }
1454  
1455 +      if (output3DTemp_) {
1456 +        value = mass * vel.x() * vel.x();
1457 +        xTempHist_[binNo] += value;
1458 +        value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1459 +          / PhysicalConstants::kb;
1460 +        yTempHist_[binNo] += value;
1461 +        value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1462 +          / PhysicalConstants::kb;
1463 +        zTempHist_[binNo] += value;
1464 +        xyzTempCount_[binNo]++;
1465 +      }
1466 +      if (outputRotTemp_) {
1467 +        if (sd->isDirectional()) {
1468 +          Vector3d angMom = sd->getJ();
1469 +          Mat3x3d I = sd->getI();
1470 +          if (sd->isLinear()) {
1471 +            int i = sd->linearAxis();
1472 +            int j = (i + 1) % 3;
1473 +            int k = (i + 2) % 3;
1474 +            value = angMom[j] * angMom[j] / I(j, j) +
1475 +              angMom[k] * angMom[k] / I(k, k);
1476 +            rotTempCount_[binNo] +=2;
1477 +          } else {
1478 +            value = angMom[0] * angMom[0] / I(0, 0) +
1479 +              angMom[1] * angMom[1] / I(1, 1) +
1480 +              angMom[2] * angMom[2] / I(2, 2);
1481 +            rotTempCount_[binNo] +=3;
1482 +          }
1483 +        }
1484 +        value = value / PhysicalConstants::energyConvert
1485 +          / PhysicalConstants::kb;//may move to getStatus()
1486 +        rotTempHist_[binNo] += value;
1487 +      }
1488 +      // James put this in.
1489 +      if (outputDen_) {
1490 +        //value = 1.0;
1491 +        DenHist_[binNo] += 1;
1492 +      }
1493 +      if (outputVz_) {
1494 +        value = mass * vel[2];
1495 +        //vyzCount_[binNo]++;
1496 +        pzzHist_[binNo] += value;
1497 +      }    
1498 +    }
1499    }
1500  
1501    void RNEMD::getStarted() {
1502 +    collectData();
1503 +    /*now can output profile in step 0, but might not be useful;
1504      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1505      Stats& stat = currentSnap_->statData;
1506      stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1507 +    */
1508 +    //may output a header for the log file here
1509 +    getStatus();
1510    }
1511  
1512    void RNEMD::getStatus() {
1513  
1514      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
981    Stats& stat = currentSnap_->statData;
1515      RealType time = currentSnap_->getTime();
983
984    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1516      //or to be more meaningful, define another item as exchangeSum_ / time
1517      int j;
1518  
# Line 990 | Line 1521 | namespace OpenMD {
1521      // all processors have the same number of bins, and STL vectors pack their
1522      // arrays, so in theory, this should be safe:
1523  
1524 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueHist_[0],
1525 <                              rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1526 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueCount_[0],
1527 <                              rnemdLogWidth_, MPI::INT, MPI::SUM);
1528 <    if (rnemdType_ == rnemdPx || rnemdType_ == rnemdPxScale) {
1524 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &mHist_[0],
1525 >                              rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1526 >    if (outputTemp_) {
1527 >      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempHist_[0],
1528 >                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1529 >      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempCount_[0],
1530 >                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1531 >    }
1532 >    if (outputVx_) {
1533 >      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pxzHist_[0],
1534 >                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1535 >      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vxzCount_[0],
1536 >      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1537 >    }
1538 >    if (outputVy_) {
1539 >      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pyzHist_[0],
1540 >                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1541 >      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vyzCount_[0],
1542 >      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1543 >    }
1544 >    if (output3DTemp_) {
1545        MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xTempHist_[0],
1546                                  rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1547        MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &yTempHist_[0],
1548                                  rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1549        MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &zTempHist_[0],
1550                                  rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1551 +      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xyzTempCount_[0],
1552 +                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1553      }
1554 +    if (outputRotTemp_) {
1555 +      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempHist_[0],
1556 +                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1557 +      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempCount_[0],
1558 +                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1559 +    }
1560 +    // James put this in
1561 +    if (outputDen_) {
1562 +      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &DenHist_[0],
1563 +                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1564 +    }
1565 +    if (outputAh_) {
1566 +      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &AhCount_,
1567 +                                1, MPI::REALTYPE, MPI::SUM);
1568 +    }
1569 +    if (outputVz_) {
1570 +      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pzzHist_[0],
1571 +                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1572 +    }
1573 +    
1574      // If we're the root node, should we print out the results
1575      int worldRank = MPI::COMM_WORLD.Get_rank();
1576      if (worldRank == 0) {
1577   #endif
1578 <      rnemdLog_ << time;
1579 <      for (j = 0; j < rnemdLogWidth_; j++) {
1580 <        rnemdLog_ << "\t" << valueHist_[j] / (RealType)valueCount_[j];
1578 >
1579 >      if (outputTemp_) {
1580 >        tempLog_ << time;
1581 >        for (j = 0; j < rnemdLogWidth_; j++) {
1582 >          tempLog_ << "\t" << tempHist_[j] / (RealType)tempCount_[j];
1583 >        }
1584 >        tempLog_ << endl;
1585        }
1586 <      rnemdLog_ << "\n";
1587 <      if (rnemdType_ == rnemdPx || rnemdType_ == rnemdPxScale ) {
1588 <        xTempLog_ << time;      
1586 >      if (outputVx_) {
1587 >        vxzLog_ << time;
1588 >        for (j = 0; j < rnemdLogWidth_; j++) {
1589 >          vxzLog_ << "\t" << pxzHist_[j] / mHist_[j];
1590 >        }
1591 >        vxzLog_ << endl;
1592 >      }
1593 >      if (outputVy_) {
1594 >        vyzLog_ << time;
1595 >        for (j = 0; j < rnemdLogWidth_; j++) {
1596 >          vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1597 >        }
1598 >        vyzLog_ << endl;
1599 >      }
1600 >
1601 >      if (output3DTemp_) {
1602 >        RealType temp;
1603 >        xTempLog_ << time;
1604          for (j = 0; j < rnemdLogWidth_; j++) {
1605 <          xTempLog_ << "\t" << xTempHist_[j] / (RealType)valueCount_[j];
1605 >          if (outputVx_)
1606 >            xTempHist_[j] -= pxzHist_[j] * pxzHist_[j] / mHist_[j];
1607 >          temp = xTempHist_[j] / (RealType)xyzTempCount_[j]
1608 >            / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1609 >          xTempLog_ << "\t" << temp;
1610          }
1611 <        xTempLog_ << "\n";
1611 >        xTempLog_ << endl;
1612          yTempLog_ << time;
1613          for (j = 0; j < rnemdLogWidth_; j++) {
1614 <          yTempLog_ << "\t" << yTempHist_[j] / (RealType)valueCount_[j];
1614 >          yTempLog_ << "\t" << yTempHist_[j] / (RealType)xyzTempCount_[j];
1615          }
1616 <        yTempLog_ << "\n";
1616 >        yTempLog_ << endl;
1617          zTempLog_ << time;
1618          for (j = 0; j < rnemdLogWidth_; j++) {
1619 <          zTempLog_ << "\t" << zTempHist_[j] / (RealType)valueCount_[j];
1619 >          zTempLog_ << "\t" << zTempHist_[j] / (RealType)xyzTempCount_[j];
1620          }
1621 <        zTempLog_ << "\n";
1621 >        zTempLog_ << endl;
1622        }
1623 +      if (outputRotTemp_) {
1624 +        rotTempLog_ << time;
1625 +        for (j = 0; j < rnemdLogWidth_; j++) {
1626 +          rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1627 +        }
1628 +        rotTempLog_ << endl;
1629 +      }
1630 +      // James put this in.
1631 +      Mat3x3d hmat = currentSnap_->getHmat();
1632 +      if (outputDen_) {
1633 +        denLog_ << time;
1634 +        for (j = 0; j < rnemdLogWidth_; j++) {
1635 +          
1636 +          RealType binVol = hmat(0,0) * hmat(1,1) * (hmat(2,2) / float(nBins_));
1637 +          denLog_ << "\t" << DenHist_[j] / (float(logFrameCount_) * binVol);
1638 +        }
1639 +        denLog_ << endl;
1640 +      }
1641 +      if (outputVz_) {
1642 +        vzzLog_ << time;
1643 +        for (j = 0; j < rnemdLogWidth_; j++) {
1644 +          vzzLog_ << "\t" << pzzHist_[j] / mHist_[j];
1645 +        }
1646 +        vzzLog_ << endl;
1647 +      }      
1648   #ifdef IS_MPI
1649      }
1650   #endif
1651 +
1652      for (j = 0; j < rnemdLogWidth_; j++) {
1653 <      valueCount_[j] = 0;
1036 <      valueHist_[j] = 0.0;
1653 >      mHist_[j] = 0.0;
1654      }
1655 <    if (rnemdType_ == rnemdPx || rnemdType_ == rnemdPxScale)
1655 >    if (outputTemp_)
1656        for (j = 0; j < rnemdLogWidth_; j++) {
1657 +        tempCount_[j] = 0;
1658 +        tempHist_[j] = 0.0;
1659 +      }
1660 +    if (outputVx_)
1661 +      for (j = 0; j < rnemdLogWidth_; j++) {
1662 +        //pxzCount_[j] = 0;
1663 +        pxzHist_[j] = 0.0;
1664 +      }
1665 +    if (outputVy_)
1666 +      for (j = 0; j < rnemdLogWidth_; j++) {
1667 +        //pyzCount_[j] = 0;
1668 +        pyzHist_[j] = 0.0;
1669 +      }
1670 +
1671 +    if (output3DTemp_)
1672 +      for (j = 0; j < rnemdLogWidth_; j++) {
1673          xTempHist_[j] = 0.0;
1674          yTempHist_[j] = 0.0;
1675          zTempHist_[j] = 0.0;
1676 +        xyzTempCount_[j] = 0;
1677        }
1678 +    if (outputRotTemp_)
1679 +      for (j = 0; j < rnemdLogWidth_; j++) {
1680 +        rotTempCount_[j] = 0;
1681 +        rotTempHist_[j] = 0.0;
1682 +      }
1683 +    // James put this in
1684 +    if (outputDen_)
1685 +      for (j = 0; j < rnemdLogWidth_; j++) {
1686 +        //pyzCount_[j] = 0;
1687 +        DenHist_[j] = 0.0;
1688 +      }
1689 +    if (outputVz_)
1690 +      for (j = 0; j < rnemdLogWidth_; j++) {
1691 +        //pyzCount_[j] = 0;
1692 +        pzzHist_[j] = 0.0;
1693 +      }    
1694 +     // reset the counter
1695 +    
1696 +    Numcount_++;
1697 +    if (Numcount_ > int(runTime_/statusTime_))
1698 +      cerr << "time =" << time << "  Asum =" << Asum_ << '\n';
1699 +    if (Numcount_ > int(runTime_/statusTime_))
1700 +      cerr << "time =" << time << "  Jsum =" << Jsum_ << '\n';
1701 +    
1702 +    logFrameCount_ = 0;
1703    }
1704   }
1705 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines