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 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC vs.
branches/development/src/rnemd/RNEMD.cpp (file contents), Revision 1777 by gezelter, Thu Aug 9 18:35:09 2012 UTC

# Line 36 | Line 36
36   * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).            
37   * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).          
38   * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).          
39 < * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 < * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 > * [4]  Vardeman & Gezelter, in progress (2009).                        
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"
49   #include "primitives/StuntDouble.hpp"
50   #include "utils/PhysicalConstants.hpp"
51   #include "utils/Tuple.hpp"
52 <
53 < #ifndef IS_MPI
54 < #include "math/SeqRandNumGen.hpp"
55 < #else
52 > #ifdef IS_MPI
53   #include <mpi.h>
57 #include "math/ParallelRandNumGen.hpp"
54   #endif
55  
56   #define HONKING_LARGE_VALUE 1.0e10
# Line 65 | Line 61 | namespace OpenMD {
61    RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info),
62                                  usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
63  
64 +    trialCount_ = 0;
65      failTrialCount_ = 0;
66      failRootCount_ = 0;
67  
68      int seedValue;
69      Globals * simParams = info->getSimParams();
70 +    RNEMDParameters* rnemdParams = simParams->getRNEMDParameters();
71  
72 <    stringToEnumMap_["KineticSwap"] = rnemdKineticSwap;
73 <    stringToEnumMap_["KineticScale"] = rnemdKineticScale;
76 <    stringToEnumMap_["PxScale"] = rnemdPxScale;
77 <    stringToEnumMap_["PyScale"] = rnemdPyScale;
78 <    stringToEnumMap_["PzScale"] = rnemdPzScale;
79 <    stringToEnumMap_["Px"] = rnemdPx;
80 <    stringToEnumMap_["Py"] = rnemdPy;
81 <    stringToEnumMap_["Pz"] = rnemdPz;
82 <    stringToEnumMap_["Unknown"] = rnemdUnknown;
72 >    doRNEMD_ = rnemdParams->getUseRNEMD();
73 >    if (!doRNEMD_) return;
74  
75 <    rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
75 >    stringToMethod_["Swap"]  = rnemdSwap;
76 >    stringToMethod_["NIVS"]  = rnemdNIVS;
77 >    stringToMethod_["VSS"]   = rnemdVSS;
78 >
79 >    stringToFluxType_["KE"]  = rnemdKE;
80 >    stringToFluxType_["Px"]  = rnemdPx;
81 >    stringToFluxType_["Py"]  = rnemdPy;
82 >    stringToFluxType_["Pz"]  = rnemdPz;
83 >    stringToFluxType_["Pvector"]  = rnemdPvector;
84 >    stringToFluxType_["KE+Px"]  = rnemdKePx;
85 >    stringToFluxType_["KE+Py"]  = rnemdKePy;
86 >    stringToFluxType_["KE+Pvector"]  = rnemdKePvector;
87 >
88 >    runTime_ = simParams->getRunTime();
89 >    statusTime_ = simParams->getStatusTime();
90 >
91 >    rnemdObjectSelection_ = rnemdParams->getObjectSelection();
92      evaluator_.loadScriptString(rnemdObjectSelection_);
93      seleMan_.setSelectionSet(evaluator_.evaluate());
94  
95 <    // do some sanity checking
95 >    const string methStr = rnemdParams->getMethod();
96 >    bool hasFluxType = rnemdParams->haveFluxType();
97  
98 <    int selectionCount = seleMan_.getSelectionCount();
99 <    int nIntegrable = info->getNGlobalIntegrableObjects();
100 <
101 <    if (selectionCount > nIntegrable) {
98 >    string fluxStr;
99 >    if (hasFluxType) {
100 >      fluxStr = rnemdParams->getFluxType();
101 >    } else {
102        sprintf(painCave.errMsg,
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"
107 <              "\tis only %d.  This is almost certainly not what you want\n"
108 <              "\tto do.  A likely cause of this is forgetting the _RB_0\n"
101 <              "\tselector in the selection script!\n",
102 <              rnemdObjectSelection_.c_str(),
103 <              selectionCount, nIntegrable);
104 <      painCave.isFatal = 0;
105 <      painCave.severity = OPENMD_WARNING;
103 >              "RNEMD: No fluxType was set in the md file.  This parameter,\n"
104 >              "\twhich must be one of the following values:\n"
105 >              "\tKE, Px, Py, Pz, Pvector, KE+Px, KE+Py, KE+Pvector\n"
106 >              "\tmust be set to use RNEMD\n");
107 >      painCave.isFatal = 1;
108 >      painCave.severity = OPENMD_ERROR;
109        simError();
110      }
111 +
112 +    bool hasKineticFlux = rnemdParams->haveKineticFlux();
113 +    bool hasMomentumFlux = rnemdParams->haveMomentumFlux();
114 +    bool hasMomentumFluxVector = rnemdParams->haveMomentumFluxVector();
115 +    bool hasSlabWidth = rnemdParams->haveSlabWidth();
116 +    bool hasSlabACenter = rnemdParams->haveSlabACenter();
117 +    bool hasSlabBCenter = rnemdParams->haveSlabBCenter();
118 +    bool hasOutputFileName = rnemdParams->haveOutputFileName();
119 +    bool hasOutputFields = rnemdParams->haveOutputFields();
120      
121 <    const string st = simParams->getRNEMD_exchangeType();
121 >    map<string, RNEMDMethod>::iterator i;
122 >    i = stringToMethod_.find(methStr);
123 >    if (i != stringToMethod_.end())
124 >      rnemdMethod_ = i->second;
125 >    else {
126 >      sprintf(painCave.errMsg,
127 >              "RNEMD: The current method,\n"
128 >              "\t\t%s is not one of the recognized\n"
129 >              "\texchange methods: Swap, NIVS, or VSS\n",
130 >              methStr.c_str());
131 >      painCave.isFatal = 1;
132 >      painCave.severity = OPENMD_ERROR;
133 >      simError();
134 >    }
135  
136 <    map<string, RNEMDTypeEnum>::iterator i;
137 <    i = stringToEnumMap_.find(st);
138 <    rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
139 <    if (rnemdType_ == rnemdUnknown) {
136 >    map<string, RNEMDFluxType>::iterator j;
137 >    j = stringToFluxType_.find(fluxStr);
138 >    if (j != stringToFluxType_.end())
139 >      rnemdFluxType_ = j->second;
140 >    else {
141        sprintf(painCave.errMsg,
142 <              "RNEMD: The current RNEMD_exchangeType,\n"
142 >              "RNEMD: The current fluxType,\n"
143                "\t\t%s\n"
144 <              "\tis not one of the recognized exchange types.\n",
145 <              st.c_str());
144 >              "\tis not one of the recognized flux types.\n",
145 >              fluxStr.c_str());
146        painCave.isFatal = 1;
147        painCave.severity = OPENMD_ERROR;
148        simError();
149      }
124    
125    output3DTemp_ = false;
126    if (simParams->haveRNEMD_outputDimensionalTemperature()) {
127      output3DTemp_ = simParams->getRNEMD_outputDimensionalTemperature();
128    }
150  
151 < #ifdef IS_MPI
152 <    if (worldRank == 0) {
153 < #endif
154 <
155 <      string rnemdFileName;
156 <      switch(rnemdType_) {
157 <      case rnemdKineticSwap :
137 <      case rnemdKineticScale :
138 <        rnemdFileName = "temperature.log";
151 >    bool methodFluxMismatch = false;
152 >    bool hasCorrectFlux = false;
153 >    switch(rnemdMethod_) {
154 >    case rnemdSwap:
155 >      switch (rnemdFluxType_) {
156 >      case rnemdKE:
157 >        hasCorrectFlux = hasKineticFlux;
158          break;
159 <      case rnemdPx :
160 <      case rnemdPxScale :
161 <      case rnemdPy :
162 <      case rnemdPyScale :
144 <        rnemdFileName = "momemtum.log";
159 >      case rnemdPx:
160 >      case rnemdPy:
161 >      case rnemdPz:
162 >        hasCorrectFlux = hasMomentumFlux;
163          break;
146      case rnemdPz :
147      case rnemdPzScale :
148      case rnemdUnknown :
164        default :
165 <        rnemdFileName = "rnemd.log";
165 >        methodFluxMismatch = true;
166          break;
167        }
168 <      rnemdLog_.open(rnemdFileName.c_str());
169 <
170 <      string xTempFileName;
171 <      string yTempFileName;
172 <      string zTempFileName;
173 <      if (output3DTemp_) {
174 <        xTempFileName = "temperatureX.log";
175 <        yTempFileName = "temperatureY.log";
176 <        zTempFileName = "temperatureZ.log";
177 <        xTempLog_.open(xTempFileName.c_str());
178 <        yTempLog_.open(yTempFileName.c_str());
179 <        zTempLog_.open(zTempFileName.c_str());
168 >      break;
169 >    case rnemdNIVS:
170 >      switch (rnemdFluxType_) {
171 >      case rnemdKE:
172 >      case rnemdRotKE:
173 >      case rnemdFullKE:
174 >        hasCorrectFlux = hasKineticFlux;
175 >        break;
176 >      case rnemdPx:
177 >      case rnemdPy:
178 >      case rnemdPz:
179 >        hasCorrectFlux = hasMomentumFlux;
180 >        break;
181 >      case rnemdKePx:
182 >      case rnemdKePy:
183 >        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
184 >        break;
185 >      default:
186 >        methodFluxMismatch = true;
187 >        break;
188        }
189 <
190 < #ifdef IS_MPI
191 <    }
192 < #endif
193 <
194 <    set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
195 <    set_RNEMD_nBins(simParams->getRNEMD_nBins());
196 <    midBin_ = nBins_ / 2;
197 <    if (simParams->haveRNEMD_binShift()) {
198 <      if (simParams->getRNEMD_binShift()) {
199 <        zShift_ = 0.5 / (RealType)(nBins_);
200 <      } else {
201 <        zShift_ = 0.0;
189 >      break;
190 >    case rnemdVSS:
191 >      switch (rnemdFluxType_) {
192 >      case rnemdKE:
193 >      case rnemdRotKE:
194 >      case rnemdFullKE:
195 >        hasCorrectFlux = hasKineticFlux;
196 >        break;
197 >      case rnemdPx:
198 >      case rnemdPy:
199 >      case rnemdPz:
200 >        hasCorrectFlux = hasMomentumFlux;
201 >        break;
202 >      case rnemdPvector:
203 >        hasCorrectFlux = hasMomentumFluxVector;
204 >        break;
205 >      case rnemdKePx:
206 >      case rnemdKePy:
207 >        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
208 >        break;
209 >      case rnemdKePvector:
210 >        hasCorrectFlux = hasMomentumFluxVector && hasKineticFlux;
211 >        break;
212 >      default:
213 >        methodFluxMismatch = true;
214 >        break;
215        }
216 <    } else {
217 <      zShift_ = 0.0;
216 >    default:
217 >      break;
218      }
183    //cerr << "we have zShift_ = " << zShift_ << "\n";
184    //shift slabs by half slab width, might be useful in heterogeneous systems
185    //set to 0.0 if not using it; can NOT be used in status output yet
186    if (simParams->haveRNEMD_logWidth()) {
187      set_RNEMD_logWidth(simParams->getRNEMD_logWidth());
188      /*arbitary rnemdLogWidth_ no checking
189        if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
190        cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
191        cerr << "Automaically set back to default.\n";
192        rnemdLogWidth_ = nBins_;
193        }*/
194    } else {
195      set_RNEMD_logWidth(nBins_);
196    }
197    valueHist_.resize(rnemdLogWidth_, 0.0);
198    valueCount_.resize(rnemdLogWidth_, 0);
199    xTempHist_.resize(rnemdLogWidth_, 0.0);
200    yTempHist_.resize(rnemdLogWidth_, 0.0);
201    zTempHist_.resize(rnemdLogWidth_, 0.0);
202    xyzTempCount_.resize(rnemdLogWidth_, 0);
219  
220 <    set_RNEMD_exchange_total(0.0);
221 <    if (simParams->haveRNEMD_targetFlux()) {
222 <      set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
220 >    if (methodFluxMismatch) {
221 >      sprintf(painCave.errMsg,
222 >              "RNEMD: The current method,\n"
223 >              "\t\t%s\n"
224 >              "\tcannot be used with the current flux type, %s\n",
225 >              methStr.c_str(), fluxStr.c_str());
226 >      painCave.isFatal = 1;
227 >      painCave.severity = OPENMD_ERROR;
228 >      simError();        
229 >    }
230 >    if (!hasCorrectFlux) {
231 >      sprintf(painCave.errMsg,
232 >              "RNEMD: The current method, %s, and flux type, %s,\n"
233 >              "\tdid not have the correct flux value specified. Options\n"
234 >              "\tinclude: kineticFlux, momentumFlux, and momentumFluxVector\n",
235 >              methStr.c_str(), fluxStr.c_str());
236 >      painCave.isFatal = 1;
237 >      painCave.severity = OPENMD_ERROR;
238 >      simError();        
239 >    }
240 >
241 >    if (hasKineticFlux) {
242 >      // convert the kcal / mol / Angstroms^2 / fs values in the md file
243 >      // into  amu / fs^3:
244 >      kineticFlux_ = rnemdParams->getKineticFlux()
245 >        * PhysicalConstants::energyConvert;
246      } else {
247 <      set_RNEMD_target_flux(0.0);
247 >      kineticFlux_ = 0.0;
248      }
249 +    if (hasMomentumFluxVector) {
250 +      momentumFluxVector_ = rnemdParams->getMomentumFluxVector();
251 +    } else {
252 +      momentumFluxVector_ = V3Zero;
253 +      if (hasMomentumFlux) {
254 +        RealType momentumFlux = rnemdParams->getMomentumFlux();
255 +        switch (rnemdFluxType_) {
256 +        case rnemdPx:
257 +          momentumFluxVector_.x() = momentumFlux;
258 +          break;
259 +        case rnemdPy:
260 +          momentumFluxVector_.y() = momentumFlux;
261 +          break;
262 +        case rnemdPz:
263 +          momentumFluxVector_.z() = momentumFlux;
264 +          break;
265 +        case rnemdKePx:
266 +          momentumFluxVector_.x() = momentumFlux;
267 +          break;
268 +        case rnemdKePy:
269 +          momentumFluxVector_.y() = momentumFlux;
270 +          break;
271 +        default:
272 +          break;
273 +        }
274 +      }    
275 +    }
276  
277 < #ifndef IS_MPI
278 <    if (simParams->haveSeed()) {
279 <      seedValue = simParams->getSeed();
280 <      randNumGen_ = new SeqRandNumGen(seedValue);
281 <    }else {
282 <      randNumGen_ = new SeqRandNumGen();
283 <    }    
284 < #else
285 <    if (simParams->haveSeed()) {
286 <      seedValue = simParams->getSeed();
287 <      randNumGen_ = new ParallelRandNumGen(seedValue);
288 <    }else {
289 <      randNumGen_ = new ParallelRandNumGen();
290 <    }    
291 < #endif
292 <  }
277 >    // do some sanity checking
278 >
279 >    int selectionCount = seleMan_.getSelectionCount();
280 >    int nIntegrable = info->getNGlobalIntegrableObjects();
281 >
282 >    if (selectionCount > nIntegrable) {
283 >      sprintf(painCave.errMsg,
284 >              "RNEMD: The current objectSelection,\n"
285 >              "\t\t%s\n"
286 >              "\thas resulted in %d selected objects.  However,\n"
287 >              "\tthe total number of integrable objects in the system\n"
288 >              "\tis only %d.  This is almost certainly not what you want\n"
289 >              "\tto do.  A likely cause of this is forgetting the _RB_0\n"
290 >              "\tselector in the selection script!\n",
291 >              rnemdObjectSelection_.c_str(),
292 >              selectionCount, nIntegrable);
293 >      painCave.isFatal = 0;
294 >      painCave.severity = OPENMD_WARNING;
295 >      simError();
296 >    }
297 >
298 >    areaAccumulator_ = new Accumulator();
299 >
300 >    nBins_ = rnemdParams->getOutputBins();
301 >
302 >    data_.resize(RNEMD::ENDINDEX);
303 >    OutputData z;
304 >    z.units =  "Angstroms";
305 >    z.title =  "Z";
306 >    z.dataType = "RealType";
307 >    z.accumulator.reserve(nBins_);
308 >    for (unsigned int i = 0; i < nBins_; i++)
309 >      z.accumulator.push_back( new Accumulator() );
310 >    data_[Z] = z;
311 >    outputMap_["Z"] =  Z;
312 >
313 >    OutputData temperature;
314 >    temperature.units =  "K";
315 >    temperature.title =  "Temperature";
316 >    temperature.dataType = "RealType";
317 >    temperature.accumulator.reserve(nBins_);
318 >    for (unsigned int i = 0; i < nBins_; i++)
319 >      temperature.accumulator.push_back( new Accumulator() );
320 >    data_[TEMPERATURE] = temperature;
321 >    outputMap_["TEMPERATURE"] =  TEMPERATURE;
322 >
323 >    OutputData velocity;
324 >    velocity.units = "angstroms/fs";
325 >    velocity.title =  "Velocity";  
326 >    velocity.dataType = "Vector3d";
327 >    velocity.accumulator.reserve(nBins_);
328 >    for (unsigned int i = 0; i < nBins_; i++)
329 >      velocity.accumulator.push_back( new VectorAccumulator() );
330 >    data_[VELOCITY] = velocity;
331 >    outputMap_["VELOCITY"] = VELOCITY;
332 >
333 >    OutputData density;
334 >    density.units =  "g cm^-3";
335 >    density.title =  "Density";
336 >    density.dataType = "RealType";
337 >    density.accumulator.reserve(nBins_);
338 >    for (unsigned int i = 0; i < nBins_; i++)
339 >      density.accumulator.push_back( new Accumulator() );
340 >    data_[DENSITY] = density;
341 >    outputMap_["DENSITY"] =  DENSITY;
342 >
343 >    if (hasOutputFields) {
344 >      parseOutputFileFormat(rnemdParams->getOutputFields());
345 >    } else {
346 >      outputMask_.set(Z);
347 >      switch (rnemdFluxType_) {
348 >      case rnemdKE:
349 >      case rnemdRotKE:
350 >      case rnemdFullKE:
351 >        outputMask_.set(TEMPERATURE);
352 >        break;
353 >      case rnemdPx:
354 >      case rnemdPy:
355 >        outputMask_.set(VELOCITY);
356 >        break;
357 >      case rnemdPz:        
358 >      case rnemdPvector:
359 >        outputMask_.set(VELOCITY);
360 >        outputMask_.set(DENSITY);
361 >        break;
362 >      case rnemdKePx:
363 >      case rnemdKePy:
364 >        outputMask_.set(TEMPERATURE);
365 >        outputMask_.set(VELOCITY);
366 >        break;
367 >      case rnemdKePvector:
368 >        outputMask_.set(TEMPERATURE);
369 >        outputMask_.set(VELOCITY);
370 >        outputMask_.set(DENSITY);        
371 >        break;
372 >      default:
373 >        break;
374 >      }
375 >    }
376 >      
377 >    if (hasOutputFileName) {
378 >      rnemdFileName_ = rnemdParams->getOutputFileName();
379 >    } else {
380 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
381 >    }          
382 >
383 >    exchangeTime_ = rnemdParams->getExchangeTime();
384 >
385 >    Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
386 >    Mat3x3d hmat = currentSnap_->getHmat();
387    
388 <  RNEMD::~RNEMD() {
389 <    delete randNumGen_;
388 >    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
389 >    // Lx, Ly = box dimensions in x & y
390 >    // dt = exchange time interval
391 >    // flux = target flux
392 >
393 >    RealType area = currentSnap_->getXYarea();
394 >    kineticTarget_ = 2.0 * kineticFlux_ * exchangeTime_ * area;
395 >    momentumTarget_ = 2.0 * momentumFluxVector_ * exchangeTime_ * area;
396 >
397 >    // total exchange sums are zeroed out at the beginning:
398 >
399 >    kineticExchange_ = 0.0;
400 >    momentumExchange_ = V3Zero;
401 >
402 >    if (hasSlabWidth)
403 >      slabWidth_ = rnemdParams->getSlabWidth();
404 >    else
405 >      slabWidth_ = hmat(2,2) / 10.0;
406 >  
407 >    if (hasSlabACenter)
408 >      slabACenter_ = rnemdParams->getSlabACenter();
409 >    else
410 >      slabACenter_ = 0.0;
411      
412 +    if (hasSlabBCenter)
413 +      slabBCenter_ = rnemdParams->getSlabBCenter();
414 +    else
415 +      slabBCenter_ = hmat(2,2) / 2.0;
416 +    
417 +  }
418 +  
419 +  RNEMD::~RNEMD() {
420 +    if (!doRNEMD_) return;
421   #ifdef IS_MPI
422      if (worldRank == 0) {
423   #endif
234      
235      sprintf(painCave.errMsg,
236              "RNEMD: total failed trials: %d\n",
237              failTrialCount_);
238      painCave.isFatal = 0;
239      painCave.severity = OPENMD_INFO;
240      simError();
424  
425 <      rnemdLog_.close();
426 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale || rnemdType_ == rnemdPyScale) {
427 <        sprintf(painCave.errMsg,
428 <                "RNEMD: total root-checking warnings: %d\n",
246 <                failRootCount_);
247 <        painCave.isFatal = 0;
248 <        painCave.severity = OPENMD_INFO;
249 <        simError();
250 <      }
251 <      if (output3DTemp_) {
252 <        xTempLog_.close();
253 <        yTempLog_.close();
254 <        zTempLog_.close();
255 <      }
425 >      writeOutputFile();
426 >
427 >      rnemdFile_.close();
428 >      
429   #ifdef IS_MPI
430      }
431   #endif
432    }
433 +  
434 +  bool RNEMD::inSlabA(Vector3d pos) {
435 +    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
436 +  }
437 +  bool RNEMD::inSlabB(Vector3d pos) {
438 +    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
439 +  }
440  
441    void RNEMD::doSwap() {
442 <
442 >    if (!doRNEMD_) return;
443      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
444      Mat3x3d hmat = currentSnap_->getHmat();
445  
# Line 288 | Line 468 | namespace OpenMD {
468  
469        if (usePeriodicBoundaryConditions_)
470          currentSnap_->wrapVector(pos);
471 +      bool inA = inSlabA(pos);
472 +      bool inB = inSlabB(pos);
473  
474 <      // which bin is this stuntdouble in?
293 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
294 <
295 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
296 <
297 <
298 <      // if we're in bin 0 or the middleBin
299 <      if (binNo == 0 || binNo == midBin_) {
474 >      if (inA || inB) {
475          
476          RealType mass = sd->getMass();
477          Vector3d vel = sd->getVel();
478          RealType value;
479 <
480 <        switch(rnemdType_) {
481 <        case rnemdKineticSwap :
479 >        
480 >        switch(rnemdFluxType_) {
481 >        case rnemdKE :
482            
483 <          value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
484 <                          vel[2]*vel[2]);
485 <          /*
311 <            if (sd->isDirectional()) {
483 >          value = mass * vel.lengthSquare();
484 >          
485 >          if (sd->isDirectional()) {
486              Vector3d angMom = sd->getJ();
487              Mat3x3d I = sd->getI();
488              
489              if (sd->isLinear()) {
490 <            int i = sd->linearAxis();
491 <            int j = (i + 1) % 3;
492 <            int k = (i + 2) % 3;
493 <            value += angMom[j] * angMom[j] / I(j, j) +
494 <            angMom[k] * angMom[k] / I(k, k);
490 >              int i = sd->linearAxis();
491 >              int j = (i + 1) % 3;
492 >              int k = (i + 2) % 3;
493 >              value += angMom[j] * angMom[j] / I(j, j) +
494 >                angMom[k] * angMom[k] / I(k, k);
495              } else {                        
496 <            value += angMom[0]*angMom[0]/I(0, 0)
497 <            + angMom[1]*angMom[1]/I(1, 1)
498 <            + angMom[2]*angMom[2]/I(2, 2);
496 >              value += angMom[0]*angMom[0]/I(0, 0)
497 >                + angMom[1]*angMom[1]/I(1, 1)
498 >                + angMom[2]*angMom[2]/I(2, 2);
499              }
500 <            } no exchange of angular momenta
327 <          */
328 <          //make exchangeSum_ comparable between swap & scale
329 <          //temporarily without using energyConvert
330 <          //value = value * 0.5 / PhysicalConstants::energyConvert;
500 >          } //angular momenta exchange enabled
501            value *= 0.5;
502            break;
503          case rnemdPx :
# Line 343 | Line 513 | namespace OpenMD {
513            break;
514          }
515          
516 <        if (binNo == 0) {
516 >        if (inA == 0) {
517            if (!min_found) {
518              min_val = value;
519              min_sd = sd;
# Line 354 | Line 524 | namespace OpenMD {
524                min_sd = sd;
525              }
526            }
527 <        } else { //midBin_
527 >        } else {
528            if (!max_found) {
529              max_val = value;
530              max_sd = sd;
# Line 368 | Line 538 | namespace OpenMD {
538          }
539        }
540      }
541 <
541 >    
542   #ifdef IS_MPI
543      int nProc, worldRank;
544 <
544 >    
545      nProc = MPI::COMM_WORLD.Get_size();
546      worldRank = MPI::COMM_WORLD.Get_rank();
547  
# Line 382 | Line 552 | namespace OpenMD {
552      MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found, 1, MPI::BOOL, MPI::LOR);
553      // Even if we didn't find a maximum, did someone else?
554      MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found, 1, MPI::BOOL, MPI::LOR);
555 <    struct {
556 <      RealType val;
557 <      int rank;
558 <    } max_vals, min_vals;
559 <    
560 <    if (min_found) {
561 <      if (my_min_found)
555 > #endif
556 >
557 >    if (max_found && min_found) {
558 >
559 > #ifdef IS_MPI
560 >      struct {
561 >        RealType val;
562 >        int rank;
563 >      } max_vals, min_vals;
564 >      
565 >      if (my_min_found) {
566          min_vals.val = min_val;
567 <      else
567 >      } else {
568          min_vals.val = HONKING_LARGE_VALUE;
569 <      
569 >      }
570        min_vals.rank = worldRank;    
571        
572        // Who had the minimum?
573        MPI::COMM_WORLD.Allreduce(&min_vals, &min_vals,
574                                  1, MPI::REALTYPE_INT, MPI::MINLOC);
575        min_val = min_vals.val;
402    }
576        
577 <    if (max_found) {
578 <      if (my_max_found)
579 <        max_vals.val = max_val;
407 <      else
577 >      if (my_max_found) {
578 >        max_vals.val = max_val;
579 >      } else {
580          max_vals.val = -HONKING_LARGE_VALUE;
581 <      
581 >      }
582        max_vals.rank = worldRank;    
583        
584        // Who had the maximum?
585        MPI::COMM_WORLD.Allreduce(&max_vals, &max_vals,
586                                  1, MPI::REALTYPE_INT, MPI::MAXLOC);
587        max_val = max_vals.val;
416    }
588   #endif
589 <
419 <    if (max_found && min_found) {
589 >      
590        if (min_val < max_val) {
591 <
591 >        
592   #ifdef IS_MPI      
593          if (max_vals.rank == worldRank && min_vals.rank == worldRank) {
594            // I have both maximum and minimum, so proceed like a single
595            // processor version:
596   #endif
597 <          // objects to be swapped: velocity ONLY
597 >
598            Vector3d min_vel = min_sd->getVel();
599            Vector3d max_vel = max_sd->getVel();
600            RealType temp_vel;
601            
602 <          switch(rnemdType_) {
603 <          case rnemdKineticSwap :
602 >          switch(rnemdFluxType_) {
603 >          case rnemdKE :
604              min_sd->setVel(max_vel);
605              max_sd->setVel(min_vel);
606 <            /*
437 <              if (min_sd->isDirectional() && max_sd->isDirectional()) {
606 >            if (min_sd->isDirectional() && max_sd->isDirectional()) {
607                Vector3d min_angMom = min_sd->getJ();
608                Vector3d max_angMom = max_sd->getJ();
609                min_sd->setJ(max_angMom);
610                max_sd->setJ(min_angMom);
611 <              } no angular momentum exchange
612 <            */
611 >            }//angular momenta exchange enabled
612 >            //assumes same rigid body identity
613              break;
614            case rnemdPx :
615              temp_vel = min_vel.x();
# Line 466 | Line 635 | namespace OpenMD {
635            default :
636              break;
637            }
638 +
639   #ifdef IS_MPI
640            // the rest of the cases only apply in parallel simulations:
641          } else if (max_vals.rank == worldRank) {
# Line 481 | Line 651 | namespace OpenMD {
651                                     min_vel.getArrayPointer(), 3, MPI::REALTYPE,
652                                     min_vals.rank, 0, status);
653            
654 <          switch(rnemdType_) {
655 <          case rnemdKineticSwap :
654 >          switch(rnemdFluxType_) {
655 >          case rnemdKE :
656              max_sd->setVel(min_vel);
657 <            //no angular momentum exchange for now
488 <            /*
657 >            //angular momenta exchange enabled
658              if (max_sd->isDirectional()) {
659                Vector3d min_angMom;
660                Vector3d max_angMom = max_sd->getJ();
# Line 498 | Line 667 | namespace OpenMD {
667                                         status);
668                
669                max_sd->setJ(min_angMom);
670 <             }
502 <             */            
670 >            }
671              break;
672            case rnemdPx :
673              max_vel.x() = min_vel.x();
# Line 529 | Line 697 | namespace OpenMD {
697                                     max_vel.getArrayPointer(), 3, MPI::REALTYPE,
698                                     max_vals.rank, 0, status);
699            
700 <          switch(rnemdType_) {
701 <          case rnemdKineticSwap :
700 >          switch(rnemdFluxType_) {
701 >          case rnemdKE :
702              min_sd->setVel(max_vel);
703 <            // no angular momentum exchange for now
536 <            /*
703 >            //angular momenta exchange enabled
704              if (min_sd->isDirectional()) {
705                Vector3d min_angMom = min_sd->getJ();
706                Vector3d max_angMom;
# Line 547 | Line 714 | namespace OpenMD {
714                
715                min_sd->setJ(max_angMom);
716              }
550            */
717              break;
718            case rnemdPx :
719              min_vel.x() = max_vel.x();
# Line 566 | Line 732 | namespace OpenMD {
732            }
733          }
734   #endif
735 <        exchangeSum_ += max_val - min_val;
735 >        
736 >        switch(rnemdFluxType_) {
737 >        case rnemdKE:
738 >          kineticExchange_ += max_val - min_val;
739 >          break;
740 >        case rnemdPx:
741 >          momentumExchange_.x() += max_val - min_val;
742 >          break;
743 >        case rnemdPy:
744 >          momentumExchange_.y() += max_val - min_val;
745 >          break;
746 >        case rnemdPz:
747 >          momentumExchange_.z() += max_val - min_val;
748 >          break;
749 >        default:
750 >          break;
751 >        }
752        } else {        
753          sprintf(painCave.errMsg,
754 <                "RNEMD: exchange NOT performed because min_val > max_val\n");
754 >                "RNEMD::doSwap exchange NOT performed because min_val > max_val\n");
755          painCave.isFatal = 0;
756          painCave.severity = OPENMD_INFO;
757          simError();        
# Line 577 | Line 759 | namespace OpenMD {
759        }
760      } else {
761        sprintf(painCave.errMsg,
762 <              "RNEMD: exchange NOT performed because at least one\n"
763 <              "\tof the two slabs is empty\n");
762 >              "RNEMD::doSwap exchange NOT performed because selected object\n"
763 >              "\twas not present in at least one of the two slabs.\n");
764        painCave.isFatal = 0;
765        painCave.severity = OPENMD_INFO;
766        simError();        
767        failTrialCount_++;
768 <    }
587 <    
768 >    }    
769    }
770    
771 <  void RNEMD::doScale() {
772 <
771 >  void RNEMD::doNIVS() {
772 >    if (!doRNEMD_) return;
773      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
774      Mat3x3d hmat = currentSnap_->getHmat();
775  
# Line 606 | Line 787 | namespace OpenMD {
787      RealType Khx = 0.0;
788      RealType Khy = 0.0;
789      RealType Khz = 0.0;
790 +    RealType Khw = 0.0;
791      RealType Pcx = 0.0;
792      RealType Pcy = 0.0;
793      RealType Pcz = 0.0;
794      RealType Kcx = 0.0;
795      RealType Kcy = 0.0;
796      RealType Kcz = 0.0;
797 +    RealType Kcw = 0.0;
798  
799      for (sd = seleMan_.beginSelected(selei); sd != NULL;
800           sd = seleMan_.nextSelected(selei)) {
# Line 626 | Line 809 | namespace OpenMD {
809          currentSnap_->wrapVector(pos);
810  
811        // which bin is this stuntdouble in?
812 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
812 >      bool inA = inSlabA(pos);
813 >      bool inB = inSlabB(pos);
814  
815 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
816 <
633 <      // if we're in bin 0 or the middleBin
634 <      if (binNo == 0 || binNo == midBin_) {
635 <        
815 >      if (inA || inB) {
816 >              
817          RealType mass = sd->getMass();
818          Vector3d vel = sd->getVel();
819        
820 <        if (binNo == 0) {
820 >        if (inA) {
821            hotBin.push_back(sd);
822            Phx += mass * vel.x();
823            Phy += mass * vel.y();
# Line 644 | Line 825 | namespace OpenMD {
825            Khx += mass * vel.x() * vel.x();
826            Khy += mass * vel.y() * vel.y();
827            Khz += mass * vel.z() * vel.z();
828 <        } else { //midBin_
828 >          if (sd->isDirectional()) {
829 >            Vector3d angMom = sd->getJ();
830 >            Mat3x3d I = sd->getI();
831 >            if (sd->isLinear()) {
832 >              int i = sd->linearAxis();
833 >              int j = (i + 1) % 3;
834 >              int k = (i + 2) % 3;
835 >              Khw += angMom[j] * angMom[j] / I(j, j) +
836 >                angMom[k] * angMom[k] / I(k, k);
837 >            } else {
838 >              Khw += angMom[0]*angMom[0]/I(0, 0)
839 >                + angMom[1]*angMom[1]/I(1, 1)
840 >                + angMom[2]*angMom[2]/I(2, 2);
841 >            }
842 >          }
843 >        } else {
844            coldBin.push_back(sd);
845            Pcx += mass * vel.x();
846            Pcy += mass * vel.y();
# Line 652 | Line 848 | namespace OpenMD {
848            Kcx += mass * vel.x() * vel.x();
849            Kcy += mass * vel.y() * vel.y();
850            Kcz += mass * vel.z() * vel.z();
851 +          if (sd->isDirectional()) {
852 +            Vector3d angMom = sd->getJ();
853 +            Mat3x3d I = sd->getI();
854 +            if (sd->isLinear()) {
855 +              int i = sd->linearAxis();
856 +              int j = (i + 1) % 3;
857 +              int k = (i + 2) % 3;
858 +              Kcw += angMom[j] * angMom[j] / I(j, j) +
859 +                angMom[k] * angMom[k] / I(k, k);
860 +            } else {
861 +              Kcw += angMom[0]*angMom[0]/I(0, 0)
862 +                + angMom[1]*angMom[1]/I(1, 1)
863 +                + angMom[2]*angMom[2]/I(2, 2);
864 +            }
865 +          }
866          }
867        }
868      }
869 <
869 >    
870      Khx *= 0.5;
871      Khy *= 0.5;
872      Khz *= 0.5;
873 +    Khw *= 0.5;
874      Kcx *= 0.5;
875      Kcy *= 0.5;
876      Kcz *= 0.5;
877 +    Kcw *= 0.5;
878  
879   #ifdef IS_MPI
880      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
# Line 674 | Line 887 | namespace OpenMD {
887      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
888      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
889      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
890 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khw, 1, MPI::REALTYPE, MPI::SUM);
891 +
892      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
893      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
894      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
895 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcw, 1, MPI::REALTYPE, MPI::SUM);
896   #endif
897  
898 <    //use coldBin coeff's
898 >    //solve coldBin coeff's first
899      RealType px = Pcx / Phx;
900      RealType py = Pcy / Phy;
901      RealType pz = Pcz / Phz;
902 +    RealType c, x, y, z;
903 +    bool successfulScale = false;
904 +    if ((rnemdFluxType_ == rnemdFullKE) ||
905 +        (rnemdFluxType_ == rnemdRotKE)) {
906 +      //may need sanity check Khw & Kcw > 0
907  
908 <    RealType a000, a110, c0, a001, a111, b01, b11, c1, c;
909 <    switch(rnemdType_) {
910 <    case rnemdKineticScale :
911 <      // used hotBin coeff's & only scale x & y dimensions
912 <      /*
692 <      RealType px = Phx / Pcx;
693 <      RealType py = Phy / Pcy;
694 <      a110 = Khy;
695 <      c0 = - Khx - Khy - targetFlux_;
696 <      a000 = Khx;
697 <      a111 = Kcy * py * py;
698 <      b11 = -2.0 * Kcy * py * (1.0 + py);
699 <      c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
700 <      b01 = -2.0 * Kcx * px * (1.0 + px);
701 <      a001 = Kcx * px * px;
702 <      */
703 <      //scale all three dimensions, let c_x = c_y
704 <      a000 = Kcx + Kcy;
705 <      a110 = Kcz;
706 <      c0 = targetFlux_ - Kcx - Kcy - Kcz;
707 <      a001 = Khx * px * px + Khy * py * py;
708 <      a111 = Khz * pz * pz;
709 <      b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
710 <      b11 = -2.0 * Khz * pz * (1.0 + pz);
711 <      c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
712 <        + Khz * pz * (2.0 + pz) - targetFlux_;
713 <      break;
714 <    case rnemdPxScale :
715 <      c = 1 - targetFlux_ / Pcx;
716 <      a000 = Kcy;
717 <      a110 = Kcz;
718 <      c0 = Kcx * c * c - Kcx - Kcy - Kcz;
719 <      a001 = py * py * Khy;
720 <      a111 = pz * pz * Khz;
721 <      b01 = -2.0 * Khy * py * (1.0 + py);
722 <      b11 = -2.0 * Khz * pz * (1.0 + pz);
723 <      c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
724 <        + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
725 <      break;
726 <    case rnemdPyScale :
727 <      c = 1 - targetFlux_ / Pcy;
728 <      a000 = Kcx;
729 <      a110 = Kcz;
730 <      c0 = Kcy * c * c - Kcx - Kcy - Kcz;
731 <      a001 = px * px * Khx;
732 <      a111 = pz * pz * Khz;
733 <      b01 = -2.0 * Khx * px * (1.0 + px);
734 <      b11 = -2.0 * Khz * pz * (1.0 + pz);
735 <      c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
736 <        + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
737 <      break;
738 <    case rnemdPzScale ://we don't really do this, do we?
739 <      c = 1 - targetFlux_ / Pcz;
740 <      a000 = Kcx;
741 <      a110 = Kcy;
742 <      c0 = Kcz * c * c - Kcx - Kcy - Kcz;
743 <      a001 = px * px * Khx;
744 <      a111 = py * py * Khy;
745 <      b01 = -2.0 * Khx * px * (1.0 + px);
746 <      b11 = -2.0 * Khy * py * (1.0 + py);
747 <      c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
748 <        + Khz * (fastpow(c * pz - pz - 1.0, 2) - 1.0);
749 <      break;      
750 <    default :
751 <      break;
752 <    }
908 >      if (rnemdFluxType_ == rnemdFullKE) {
909 >        c = 1.0 - kineticTarget_ / (Kcx + Kcy + Kcz + Kcw);
910 >      } else {
911 >        c = 1.0 - kineticTarget_ / Kcw;
912 >      }
913  
914 <    RealType v1 = a000 * a111 - a001 * a110;
915 <    RealType v2 = a000 * b01;
916 <    RealType v3 = a000 * b11;
917 <    RealType v4 = a000 * c1 - a001 * c0;
918 <    RealType v8 = a110 * b01;
919 <    RealType v10 = - b01 * c0;
920 <
921 <    RealType u0 = v2 * v10 - v4 * v4;
922 <    RealType u1 = -2.0 * v3 * v4;
923 <    RealType u2 = -v2 * v8 - v3 * v3 - 2.0 * v1 * v4;
924 <    RealType u3 = -2.0 * v1 * v3;
925 <    RealType u4 = - v1 * v1;
926 <    //rescale coefficients
927 <    RealType maxAbs = fabs(u0);
928 <    if (maxAbs < fabs(u1)) maxAbs = fabs(u1);
929 <    if (maxAbs < fabs(u2)) maxAbs = fabs(u2);
930 <    if (maxAbs < fabs(u3)) maxAbs = fabs(u3);
931 <    if (maxAbs < fabs(u4)) maxAbs = fabs(u4);
932 <    u0 /= maxAbs;
933 <    u1 /= maxAbs;
934 <    u2 /= maxAbs;
935 <    u3 /= maxAbs;
936 <    u4 /= maxAbs;
937 <    //max_element(start, end) is also available.
938 <    Polynomial<RealType> poly; //same as DoublePolynomial poly;
939 <    poly.setCoefficient(4, u4);
940 <    poly.setCoefficient(3, u3);
941 <    poly.setCoefficient(2, u2);
942 <    poly.setCoefficient(1, u1);
943 <    poly.setCoefficient(0, u0);
944 <    vector<RealType> realRoots = poly.FindRealRoots();
945 <
946 <    vector<RealType>::iterator ri;
947 <    RealType r1, r2, alpha0;
948 <    vector<pair<RealType,RealType> > rps;
949 <    for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
950 <      r2 = *ri;
951 <      //check if FindRealRoots() give the right answer
952 <      if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
953 <        sprintf(painCave.errMsg,
954 <                "RNEMD Warning: polynomial solve seems to have an error!");
955 <        painCave.isFatal = 0;
956 <        simError();
957 <        failRootCount_++;
914 >      if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
915 >        c = sqrt(c);
916 >        //std::cerr << "cold slab scaling coefficient: " << c << endl;
917 >        //now convert to hotBin coefficient
918 >        RealType w = 0.0;
919 >        if (rnemdFluxType_ ==  rnemdFullKE) {
920 >          x = 1.0 + px * (1.0 - c);
921 >          y = 1.0 + py * (1.0 - c);
922 >          z = 1.0 + pz * (1.0 - c);
923 >          /* more complicated way
924 >             w = 1.0 + (Kcw - Kcw * c * c - (c * c * (Kcx + Kcy + Kcz
925 >             + Khx * px * px + Khy * py * py + Khz * pz * pz)
926 >             - 2.0 * c * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py)
927 >             + Khz * pz * (1.0 + pz)) + Khx * px * (2.0 + px)
928 >             + Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
929 >             - Kcx - Kcy - Kcz)) / Khw; the following is simpler
930 >          */
931 >          if ((fabs(x - 1.0) < 0.1) && (fabs(y - 1.0) < 0.1) &&
932 >              (fabs(z - 1.0) < 0.1)) {
933 >            w = 1.0 + (kineticTarget_
934 >                       + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
935 >                       + Khz * (1.0 - z * z)) / Khw;
936 >          }//no need to calculate w if x, y or z is out of range
937 >        } else {
938 >          w = 1.0 + kineticTarget_ / Khw;
939 >        }
940 >        if ((w > 0.81) && (w < 1.21)) {//restrict scaling coefficients
941 >          //if w is in the right range, so should be x, y, z.
942 >          vector<StuntDouble*>::iterator sdi;
943 >          Vector3d vel;
944 >          for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
945 >            if (rnemdFluxType_ == rnemdFullKE) {
946 >              vel = (*sdi)->getVel() * c;
947 >              (*sdi)->setVel(vel);
948 >            }
949 >            if ((*sdi)->isDirectional()) {
950 >              Vector3d angMom = (*sdi)->getJ() * c;
951 >              (*sdi)->setJ(angMom);
952 >            }
953 >          }
954 >          w = sqrt(w);
955 >          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
956 >          //           << "\twh= " << w << endl;
957 >          for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
958 >            if (rnemdFluxType_ == rnemdFullKE) {
959 >              vel = (*sdi)->getVel();
960 >              vel.x() *= x;
961 >              vel.y() *= y;
962 >              vel.z() *= z;
963 >              (*sdi)->setVel(vel);
964 >            }
965 >            if ((*sdi)->isDirectional()) {
966 >              Vector3d angMom = (*sdi)->getJ() * w;
967 >              (*sdi)->setJ(angMom);
968 >            }
969 >          }
970 >          successfulScale = true;
971 >          kineticExchange_ += kineticTarget_;
972 >        }
973        }
974 <      //might not be useful w/o rescaling coefficients
975 <      alpha0 = -c0 - a110 * r2 * r2;
976 <      if (alpha0 >= 0.0) {
977 <        r1 = sqrt(alpha0 / a000);
978 <        if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111)) < 1e-6)
979 <          { rps.push_back(make_pair(r1, r2)); }
980 <        if (r1 > 1e-6) { //r1 non-negative
981 <          r1 = -r1;
982 <          if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111)) <1e-6)
983 <            { rps.push_back(make_pair(r1, r2)); }
984 <        }
985 <      }
986 <    }
987 <    // Consider combining together the solving pair part w/ the searching
988 <    // best solution part so that we don't need the pairs vector
989 <    if (!rps.empty()) {
990 <      RealType smallestDiff = HONKING_LARGE_VALUE;
991 <      RealType diff;
992 <      pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
993 <      vector<pair<RealType,RealType> >::iterator rpi;
994 <      for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
995 <        r1 = (*rpi).first;
996 <        r2 = (*rpi).second;
997 <        switch(rnemdType_) {
998 <        case rnemdKineticScale :
999 <          diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1000 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1001 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1002 <          break;
1003 <        case rnemdPxScale :
1004 <          diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1005 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1006 <          break;
1007 <        case rnemdPyScale :
1008 <          diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1009 <            + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1010 <          break;
1011 <        case rnemdPzScale :
1012 <          diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1013 <            + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1014 <        default :
1015 <          break;
1016 <        }
1017 <        if (diff < smallestDiff) {
1018 <          smallestDiff = diff;
1019 <          bestPair = *rpi;
1020 <        }
1021 <      }
1022 < #ifdef IS_MPI
1023 <      if (worldRank == 0) {
1024 < #endif
1025 <        sprintf(painCave.errMsg,
1026 <                "RNEMD: roots r1= %lf\tr2 = %lf\n",
1027 <                bestPair.first, bestPair.second);
1028 <        painCave.isFatal = 0;
1029 <        painCave.severity = OPENMD_INFO;
1030 <        simError();
1031 < #ifdef IS_MPI
974 >    } else {
975 >      RealType a000, a110, c0, a001, a111, b01, b11, c1;
976 >      switch(rnemdFluxType_) {
977 >      case rnemdKE :
978 >        /* used hotBin coeff's & only scale x & y dimensions
979 >           RealType px = Phx / Pcx;
980 >           RealType py = Phy / Pcy;
981 >           a110 = Khy;
982 >           c0 = - Khx - Khy - kineticTarget_;
983 >           a000 = Khx;
984 >           a111 = Kcy * py * py;
985 >           b11 = -2.0 * Kcy * py * (1.0 + py);
986 >           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + kineticTarget_;
987 >           b01 = -2.0 * Kcx * px * (1.0 + px);
988 >           a001 = Kcx * px * px;
989 >        */
990 >        //scale all three dimensions, let c_x = c_y
991 >        a000 = Kcx + Kcy;
992 >        a110 = Kcz;
993 >        c0 = kineticTarget_ - Kcx - Kcy - Kcz;
994 >        a001 = Khx * px * px + Khy * py * py;
995 >        a111 = Khz * pz * pz;
996 >        b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
997 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
998 >        c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
999 >          + Khz * pz * (2.0 + pz) - kineticTarget_;
1000 >        break;
1001 >      case rnemdPx :
1002 >        c = 1 - momentumTarget_.x() / Pcx;
1003 >        a000 = Kcy;
1004 >        a110 = Kcz;
1005 >        c0 = Kcx * c * c - Kcx - Kcy - Kcz;
1006 >        a001 = py * py * Khy;
1007 >        a111 = pz * pz * Khz;
1008 >        b01 = -2.0 * Khy * py * (1.0 + py);
1009 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
1010 >        c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
1011 >          + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
1012 >        break;
1013 >      case rnemdPy :
1014 >        c = 1 - momentumTarget_.y() / Pcy;
1015 >        a000 = Kcx;
1016 >        a110 = Kcz;
1017 >        c0 = Kcy * c * c - Kcx - Kcy - Kcz;
1018 >        a001 = px * px * Khx;
1019 >        a111 = pz * pz * Khz;
1020 >        b01 = -2.0 * Khx * px * (1.0 + px);
1021 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
1022 >        c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
1023 >          + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
1024 >        break;
1025 >      case rnemdPz ://we don't really do this, do we?
1026 >        c = 1 - momentumTarget_.z() / Pcz;
1027 >        a000 = Kcx;
1028 >        a110 = Kcy;
1029 >        c0 = Kcz * c * c - Kcx - Kcy - Kcz;
1030 >        a001 = px * px * Khx;
1031 >        a111 = py * py * Khy;
1032 >        b01 = -2.0 * Khx * px * (1.0 + px);
1033 >        b11 = -2.0 * Khy * py * (1.0 + py);
1034 >        c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
1035 >          + Khz * (fastpow(c * pz - pz - 1.0, 2) - 1.0);
1036 >        break;
1037 >      default :
1038 >        break;
1039        }
858 #endif
1040        
1041 <      RealType x, y, z;
1042 <      switch(rnemdType_) {
1043 <      case rnemdKineticScale :
1044 <        x = bestPair.first;
1045 <        y = bestPair.first;
1046 <        z = bestPair.second;
1047 <        break;
1048 <      case rnemdPxScale :
1049 <        x = c;
1050 <        y = bestPair.first;
1051 <        z = bestPair.second;
1052 <        break;
1053 <      case rnemdPyScale :
1054 <        x = bestPair.first;
1055 <        y = c;
1056 <        z = bestPair.second;
1057 <        break;
1058 <      case rnemdPzScale :
1059 <        x = bestPair.first;
1060 <        y = bestPair.second;
1061 <        z = c;
1062 <        break;          
1063 <      default :
1064 <        break;
1041 >      RealType v1 = a000 * a111 - a001 * a110;
1042 >      RealType v2 = a000 * b01;
1043 >      RealType v3 = a000 * b11;
1044 >      RealType v4 = a000 * c1 - a001 * c0;
1045 >      RealType v8 = a110 * b01;
1046 >      RealType v10 = - b01 * c0;
1047 >      
1048 >      RealType u0 = v2 * v10 - v4 * v4;
1049 >      RealType u1 = -2.0 * v3 * v4;
1050 >      RealType u2 = -v2 * v8 - v3 * v3 - 2.0 * v1 * v4;
1051 >      RealType u3 = -2.0 * v1 * v3;
1052 >      RealType u4 = - v1 * v1;
1053 >      //rescale coefficients
1054 >      RealType maxAbs = fabs(u0);
1055 >      if (maxAbs < fabs(u1)) maxAbs = fabs(u1);
1056 >      if (maxAbs < fabs(u2)) maxAbs = fabs(u2);
1057 >      if (maxAbs < fabs(u3)) maxAbs = fabs(u3);
1058 >      if (maxAbs < fabs(u4)) maxAbs = fabs(u4);
1059 >      u0 /= maxAbs;
1060 >      u1 /= maxAbs;
1061 >      u2 /= maxAbs;
1062 >      u3 /= maxAbs;
1063 >      u4 /= maxAbs;
1064 >      //max_element(start, end) is also available.
1065 >      Polynomial<RealType> poly; //same as DoublePolynomial poly;
1066 >      poly.setCoefficient(4, u4);
1067 >      poly.setCoefficient(3, u3);
1068 >      poly.setCoefficient(2, u2);
1069 >      poly.setCoefficient(1, u1);
1070 >      poly.setCoefficient(0, u0);
1071 >      vector<RealType> realRoots = poly.FindRealRoots();
1072 >      
1073 >      vector<RealType>::iterator ri;
1074 >      RealType r1, r2, alpha0;
1075 >      vector<pair<RealType,RealType> > rps;
1076 >      for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
1077 >        r2 = *ri;
1078 >        //check if FindRealRoots() give the right answer
1079 >        if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
1080 >          sprintf(painCave.errMsg,
1081 >                  "RNEMD Warning: polynomial solve seems to have an error!");
1082 >          painCave.isFatal = 0;
1083 >          simError();
1084 >          failRootCount_++;
1085 >        }
1086 >        //might not be useful w/o rescaling coefficients
1087 >        alpha0 = -c0 - a110 * r2 * r2;
1088 >        if (alpha0 >= 0.0) {
1089 >          r1 = sqrt(alpha0 / a000);
1090 >          if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
1091 >              < 1e-6)
1092 >            { rps.push_back(make_pair(r1, r2)); }
1093 >          if (r1 > 1e-6) { //r1 non-negative
1094 >            r1 = -r1;
1095 >            if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
1096 >                < 1e-6)
1097 >              { rps.push_back(make_pair(r1, r2)); }
1098 >          }
1099 >        }
1100        }
1101 <      vector<StuntDouble*>::iterator sdi;
1102 <      Vector3d vel;
1103 <      for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1104 <        vel = (*sdi)->getVel();
1105 <        vel.x() *= x;
1106 <        vel.y() *= y;
1107 <        vel.z() *= z;
1108 <        (*sdi)->setVel(vel);
1101 >      // Consider combining together the solving pair part w/ the searching
1102 >      // best solution part so that we don't need the pairs vector
1103 >      if (!rps.empty()) {
1104 >        RealType smallestDiff = HONKING_LARGE_VALUE;
1105 >        RealType diff;
1106 >        pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
1107 >        vector<pair<RealType,RealType> >::iterator rpi;
1108 >        for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1109 >          r1 = (*rpi).first;
1110 >          r2 = (*rpi).second;
1111 >          switch(rnemdFluxType_) {
1112 >          case rnemdKE :
1113 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1114 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1115 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1116 >            break;
1117 >          case rnemdPx :
1118 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1119 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1120 >            break;
1121 >          case rnemdPy :
1122 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1123 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1124 >            break;
1125 >          case rnemdPz :
1126 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1127 >              + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1128 >          default :
1129 >            break;
1130 >          }
1131 >          if (diff < smallestDiff) {
1132 >            smallestDiff = diff;
1133 >            bestPair = *rpi;
1134 >          }
1135 >        }
1136 > #ifdef IS_MPI
1137 >        if (worldRank == 0) {
1138 > #endif
1139 >          // sprintf(painCave.errMsg,
1140 >          //         "RNEMD: roots r1= %lf\tr2 = %lf\n",
1141 >          //         bestPair.first, bestPair.second);
1142 >          // painCave.isFatal = 0;
1143 >          // painCave.severity = OPENMD_INFO;
1144 >          // simError();
1145 > #ifdef IS_MPI
1146 >        }
1147 > #endif
1148 >        
1149 >        switch(rnemdFluxType_) {
1150 >        case rnemdKE :
1151 >          x = bestPair.first;
1152 >          y = bestPair.first;
1153 >          z = bestPair.second;
1154 >          break;
1155 >        case rnemdPx :
1156 >          x = c;
1157 >          y = bestPair.first;
1158 >          z = bestPair.second;
1159 >          break;
1160 >        case rnemdPy :
1161 >          x = bestPair.first;
1162 >          y = c;
1163 >          z = bestPair.second;
1164 >          break;
1165 >        case rnemdPz :
1166 >          x = bestPair.first;
1167 >          y = bestPair.second;
1168 >          z = c;
1169 >          break;          
1170 >        default :
1171 >          break;
1172 >        }
1173 >        vector<StuntDouble*>::iterator sdi;
1174 >        Vector3d vel;
1175 >        for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1176 >          vel = (*sdi)->getVel();
1177 >          vel.x() *= x;
1178 >          vel.y() *= y;
1179 >          vel.z() *= z;
1180 >          (*sdi)->setVel(vel);
1181 >        }
1182 >        //convert to hotBin coefficient
1183 >        x = 1.0 + px * (1.0 - x);
1184 >        y = 1.0 + py * (1.0 - y);
1185 >        z = 1.0 + pz * (1.0 - z);
1186 >        for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1187 >          vel = (*sdi)->getVel();
1188 >          vel.x() *= x;
1189 >          vel.y() *= y;
1190 >          vel.z() *= z;
1191 >          (*sdi)->setVel(vel);
1192 >        }
1193 >        successfulScale = true;
1194 >        switch(rnemdFluxType_) {
1195 >        case rnemdKE :
1196 >          kineticExchange_ += kineticTarget_;
1197 >          break;
1198 >        case rnemdPx :
1199 >        case rnemdPy :
1200 >        case rnemdPz :
1201 >          momentumExchange_ += momentumTarget_;
1202 >          break;          
1203 >        default :
1204 >          break;
1205 >        }      
1206        }
1207 <      //convert to hotBin coefficient
1208 <      x = 1.0 + px * (1.0 - x);
896 <      y = 1.0 + py * (1.0 - y);
897 <      z = 1.0 + pz * (1.0 - z);
898 <      for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
899 <        vel = (*sdi)->getVel();
900 <        vel.x() *= x;
901 <        vel.y() *= y;
902 <        vel.z() *= z;
903 <        (*sdi)->setVel(vel);
904 <      }
905 <      exchangeSum_ += targetFlux_;
906 <      //we may want to check whether the exchange has been successful
907 <    } else {
1207 >    }
1208 >    if (successfulScale != true) {
1209        sprintf(painCave.errMsg,
1210 <              "RNEMD: exchange NOT performed!\n");
1210 >              "RNEMD::doNIVS exchange NOT performed - roots that solve\n"
1211 >              "\tthe constraint equations may not exist or there may be\n"
1212 >              "\tno selected objects in one or both slabs.\n");
1213        painCave.isFatal = 0;
1214        painCave.severity = OPENMD_INFO;
1215        simError();        
1216        failTrialCount_++;
1217      }
1218 +  }
1219  
1220 +  void RNEMD::doVSS() {
1221 +    if (!doRNEMD_) return;
1222 +    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1223 +    RealType time = currentSnap_->getTime();    
1224 +    Mat3x3d hmat = currentSnap_->getHmat();
1225 +
1226 +    seleMan_.setSelectionSet(evaluator_.evaluate());
1227 +
1228 +    int selei;
1229 +    StuntDouble* sd;
1230 +    int idx;
1231 +
1232 +    vector<StuntDouble*> hotBin, coldBin;
1233 +
1234 +    Vector3d Ph(V3Zero);
1235 +    RealType Mh = 0.0;
1236 +    RealType Kh = 0.0;
1237 +    Vector3d Pc(V3Zero);
1238 +    RealType Mc = 0.0;
1239 +    RealType Kc = 0.0;
1240 +    
1241 +
1242 +    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1243 +         sd = seleMan_.nextSelected(selei)) {
1244 +
1245 +      idx = sd->getLocalIndex();
1246 +
1247 +      Vector3d pos = sd->getPos();
1248 +
1249 +      // wrap the stuntdouble's position back into the box:
1250 +
1251 +      if (usePeriodicBoundaryConditions_)
1252 +        currentSnap_->wrapVector(pos);
1253 +
1254 +      // which bin is this stuntdouble in?
1255 +      bool inA = inSlabA(pos);
1256 +      bool inB = inSlabB(pos);
1257 +      
1258 +      if (inA || inB) {
1259 +        
1260 +        RealType mass = sd->getMass();
1261 +        Vector3d vel = sd->getVel();
1262 +      
1263 +        if (inA) {
1264 +          hotBin.push_back(sd);
1265 +          //std::cerr << "before, velocity = " << vel << endl;
1266 +          Ph += mass * vel;
1267 +          //std::cerr << "after, velocity = " << vel << endl;
1268 +          Mh += mass;
1269 +          Kh += mass * vel.lengthSquare();
1270 +          if (rnemdFluxType_ == rnemdFullKE) {
1271 +            if (sd->isDirectional()) {
1272 +              Vector3d angMom = sd->getJ();
1273 +              Mat3x3d I = sd->getI();
1274 +              if (sd->isLinear()) {
1275 +                int i = sd->linearAxis();
1276 +                int j = (i + 1) % 3;
1277 +                int k = (i + 2) % 3;
1278 +                Kh += angMom[j] * angMom[j] / I(j, j) +
1279 +                  angMom[k] * angMom[k] / I(k, k);
1280 +              } else {
1281 +                Kh += angMom[0] * angMom[0] / I(0, 0) +
1282 +                  angMom[1] * angMom[1] / I(1, 1) +
1283 +                  angMom[2] * angMom[2] / I(2, 2);
1284 +              }
1285 +            }
1286 +          }
1287 +        } else { //midBin_
1288 +          coldBin.push_back(sd);
1289 +          Pc += mass * vel;
1290 +          Mc += mass;
1291 +          Kc += mass * vel.lengthSquare();
1292 +          if (rnemdFluxType_ == rnemdFullKE) {
1293 +            if (sd->isDirectional()) {
1294 +              Vector3d angMom = sd->getJ();
1295 +              Mat3x3d I = sd->getI();
1296 +              if (sd->isLinear()) {
1297 +                int i = sd->linearAxis();
1298 +                int j = (i + 1) % 3;
1299 +                int k = (i + 2) % 3;
1300 +                Kc += angMom[j] * angMom[j] / I(j, j) +
1301 +                  angMom[k] * angMom[k] / I(k, k);
1302 +              } else {
1303 +                Kc += angMom[0] * angMom[0] / I(0, 0) +
1304 +                  angMom[1] * angMom[1] / I(1, 1) +
1305 +                  angMom[2] * angMom[2] / I(2, 2);
1306 +              }
1307 +            }
1308 +          }
1309 +        }
1310 +      }
1311 +    }
1312 +    
1313 +    Kh *= 0.5;
1314 +    Kc *= 0.5;
1315 +
1316 +    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1317 +    //        << "\tKc= " << Kc << endl;
1318 +    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1319 +    
1320 + #ifdef IS_MPI
1321 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1322 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
1323 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mh, 1, MPI::REALTYPE, MPI::SUM);
1324 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kh, 1, MPI::REALTYPE, MPI::SUM);
1325 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mc, 1, MPI::REALTYPE, MPI::SUM);
1326 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kc, 1, MPI::REALTYPE, MPI::SUM);
1327 + #endif
1328 +
1329 +    bool successfulExchange = false;
1330 +    if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1331 +      Vector3d vc = Pc / Mc;
1332 +      Vector3d ac = -momentumTarget_ / Mc + vc;
1333 +      Vector3d acrec = -momentumTarget_ / Mc;
1334 +      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1335 +      if (cNumerator > 0.0) {
1336 +        RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1337 +        if (cDenominator > 0.0) {
1338 +          RealType c = sqrt(cNumerator / cDenominator);
1339 +          if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1340 +            Vector3d vh = Ph / Mh;
1341 +            Vector3d ah = momentumTarget_ / Mh + vh;
1342 +            Vector3d ahrec = momentumTarget_ / Mh;
1343 +            RealType hNumerator = Kh + kineticTarget_
1344 +              - 0.5 * Mh * ah.lengthSquare();
1345 +            if (hNumerator > 0.0) {
1346 +              RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1347 +              if (hDenominator > 0.0) {
1348 +                RealType h = sqrt(hNumerator / hDenominator);
1349 +                if ((h > 0.9) && (h < 1.1)) {
1350 +                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1351 +                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1352 +                  vector<StuntDouble*>::iterator sdi;
1353 +                  Vector3d vel;
1354 +                  for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1355 +                    //vel = (*sdi)->getVel();
1356 +                    vel = ((*sdi)->getVel() - vc) * c + ac;
1357 +                    (*sdi)->setVel(vel);
1358 +                    if (rnemdFluxType_ == rnemdFullKE) {
1359 +                      if ((*sdi)->isDirectional()) {
1360 +                        Vector3d angMom = (*sdi)->getJ() * c;
1361 +                        (*sdi)->setJ(angMom);
1362 +                      }
1363 +                    }
1364 +                  }
1365 +                  for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1366 +                    //vel = (*sdi)->getVel();
1367 +                    vel = ((*sdi)->getVel() - vh) * h + ah;
1368 +                    (*sdi)->setVel(vel);
1369 +                    if (rnemdFluxType_ == rnemdFullKE) {
1370 +                      if ((*sdi)->isDirectional()) {
1371 +                        Vector3d angMom = (*sdi)->getJ() * h;
1372 +                        (*sdi)->setJ(angMom);
1373 +                      }
1374 +                    }
1375 +                  }
1376 +                  successfulExchange = true;
1377 +                  kineticExchange_ += kineticTarget_;
1378 +                  momentumExchange_ += momentumTarget_;
1379 +                }
1380 +              }
1381 +            }
1382 +          }
1383 +        }
1384 +      }
1385 +    }
1386 +    if (successfulExchange != true) {
1387 +      sprintf(painCave.errMsg,
1388 +              "RNEMD::doVSS exchange NOT performed - roots that solve\n"
1389 +              "\tthe constraint equations may not exist or there may be\n"
1390 +              "\tno selected objects in one or both slabs.\n");
1391 +      painCave.isFatal = 0;
1392 +      painCave.severity = OPENMD_INFO;
1393 +      simError();        
1394 +      failTrialCount_++;
1395 +    }
1396    }
1397  
1398    void RNEMD::doRNEMD() {
1399 <
1400 <    switch(rnemdType_) {
1401 <    case rnemdKineticScale :
1402 <    case rnemdPxScale :
923 <    case rnemdPyScale :
924 <    case rnemdPzScale :
925 <      doScale();
926 <      break;
927 <    case rnemdKineticSwap :
928 <    case rnemdPx :
929 <    case rnemdPy :
930 <    case rnemdPz :
1399 >    if (!doRNEMD_) return;
1400 >    trialCount_++;
1401 >    switch(rnemdMethod_) {
1402 >    case rnemdSwap:
1403        doSwap();
1404        break;
1405 <    case rnemdUnknown :
1405 >    case rnemdNIVS:
1406 >      doNIVS();
1407 >      break;
1408 >    case rnemdVSS:
1409 >      doVSS();
1410 >      break;
1411 >    case rnemdUnkownMethod:
1412      default :
1413        break;
1414      }
1415    }
1416  
1417    void RNEMD::collectData() {
1418 <
1418 >    if (!doRNEMD_) return;
1419      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1420      Mat3x3d hmat = currentSnap_->getHmat();
1421  
1422 +    areaAccumulator_->add(currentSnap_->getXYarea());
1423 +
1424      seleMan_.setSelectionSet(evaluator_.evaluate());
1425  
1426      int selei;
1427      StuntDouble* sd;
1428      int idx;
1429  
1430 +    vector<RealType> binMass(nBins_, 0.0);
1431 +    vector<RealType> binPx(nBins_, 0.0);
1432 +    vector<RealType> binPy(nBins_, 0.0);
1433 +    vector<RealType> binPz(nBins_, 0.0);
1434 +    vector<RealType> binKE(nBins_, 0.0);
1435 +    vector<int> binDOF(nBins_, 0);
1436 +    vector<int> binCount(nBins_, 0);
1437 +
1438      // alternative approach, track all molecules instead of only those
1439      // selected for scaling/swapping:
1440      /*
1441      SimInfo::MoleculeIterator miter;
1442      vector<StuntDouble*>::iterator iiter;
1443      Molecule* mol;
1444 <    StuntDouble* integrableObject;
1444 >    StuntDouble* sd;
1445      for (mol = info_->beginMolecule(miter); mol != NULL;
1446 <         mol = info_->nextMolecule(miter))
1447 <      integrableObject is essentially sd
1448 <        for (integrableObject = mol->beginIntegrableObject(iiter);
1449 <             integrableObject != NULL;
1450 <             integrableObject = mol->nextIntegrableObject(iiter))
1446 >      mol = info_->nextMolecule(miter))
1447 >      sd is essentially sd
1448 >        for (sd = mol->beginIntegrableObject(iiter);
1449 >             sd != NULL;
1450 >             sd = mol->nextIntegrableObject(iiter))
1451      */
1452      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1453           sd = seleMan_.nextSelected(selei)) {
# Line 972 | Line 1460 | namespace OpenMD {
1460        
1461        if (usePeriodicBoundaryConditions_)
1462          currentSnap_->wrapVector(pos);
1463 <      
1463 >
1464 >
1465        // which bin is this stuntdouble in?
1466        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1467 <      
1468 <      int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
1469 <        rnemdLogWidth_;
1470 <      // no symmetrization allowed due to arbitary rnemdLogWidth_ value
1471 <      /*
983 <      if (rnemdLogWidth_ == midBin_ + 1)
984 <        if (binNo > midBin_)
985 <          binNo = nBins_ - binNo;
986 <      */
1467 >      // Shift molecules by half a box to have bins start at 0
1468 >      // The modulo operator is used to wrap the case when we are
1469 >      // beyond the end of the bins back to the beginning.
1470 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1471 >    
1472        RealType mass = sd->getMass();
1473        Vector3d vel = sd->getVel();
989      RealType value;
990      RealType xVal, yVal, zVal;
1474  
1475 <      switch(rnemdType_) {
1476 <      case rnemdKineticSwap :
1477 <      case rnemdKineticScale :
1478 <        
1479 <        value = mass * (vel[0]*vel[0] + vel[1]*vel[1] + vel[2]*vel[2]);
1480 <        
1481 <        valueCount_[binNo] += 3;
1482 <        if (sd->isDirectional()) {
1483 <          Vector3d angMom = sd->getJ();
1484 <          Mat3x3d I = sd->getI();
1485 <          
1486 <          if (sd->isLinear()) {
1487 <            int i = sd->linearAxis();
1488 <            int j = (i + 1) % 3;
1489 <            int k = (i + 2) % 3;
1490 <            value += angMom[j] * angMom[j] / I(j, j) +
1491 <              angMom[k] * angMom[k] / I(k, k);
1492 <            
1493 <            valueCount_[binNo] +=2;
1494 <            
1495 <          } else {
1496 <            value += angMom[0]*angMom[0]/I(0, 0)
1497 <              + angMom[1]*angMom[1]/I(1, 1)
1498 <              + angMom[2]*angMom[2]/I(2, 2);
1016 <            valueCount_[binNo] +=3;
1017 <          }
1018 <        }
1019 <        value = value / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1020 <        
1021 <        break;
1022 <      case rnemdPx :
1023 <      case rnemdPxScale :
1024 <        value = mass * vel[0];
1025 <        valueCount_[binNo]++;
1026 <        break;
1027 <      case rnemdPy :
1028 <      case rnemdPyScale :
1029 <        value = mass * vel[1];
1030 <        valueCount_[binNo]++;
1031 <        break;
1032 <      case rnemdPz :
1033 <      case rnemdPzScale :
1034 <        value = pos.z(); //temporarily for homogeneous systems ONLY
1035 <        valueCount_[binNo]++;
1036 <        break;
1037 <      case rnemdUnknown :
1038 <      default :
1039 <        value = 1.0;
1040 <        valueCount_[binNo]++;
1041 <        break;
1475 >      binCount[binNo]++;
1476 >      binMass[binNo] += mass;
1477 >      binPx[binNo] += mass*vel.x();
1478 >      binPy[binNo] += mass*vel.y();
1479 >      binPz[binNo] += mass*vel.z();
1480 >      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1481 >      binDOF[binNo] += 3;
1482 >
1483 >      if (sd->isDirectional()) {
1484 >        Vector3d angMom = sd->getJ();
1485 >        Mat3x3d I = sd->getI();
1486 >        if (sd->isLinear()) {
1487 >          int i = sd->linearAxis();
1488 >          int j = (i + 1) % 3;
1489 >          int k = (i + 2) % 3;
1490 >          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
1491 >                                 angMom[k] * angMom[k] / I(k, k));
1492 >          binDOF[binNo] += 2;
1493 >        } else {
1494 >          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
1495 >                                 angMom[1] * angMom[1] / I(1, 1) +
1496 >                                 angMom[2] * angMom[2] / I(2, 2));
1497 >          binDOF[binNo] += 3;
1498 >        }
1499        }
1500 <      valueHist_[binNo] += value;
1500 >    }
1501 >    
1502  
1503 <      if (output3DTemp_) {
1504 <        xVal = mass * vel.x() * vel.x() / PhysicalConstants::energyConvert
1505 <          / PhysicalConstants::kb;
1506 <        yVal = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1507 <          / PhysicalConstants::kb;
1508 <        zVal = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1509 <          / PhysicalConstants::kb;
1510 <        xTempHist_[binNo] += xVal;
1511 <        yTempHist_[binNo] += yVal;
1512 <        zTempHist_[binNo] += zVal;
1513 <        xyzTempCount_[binNo]++;
1503 > #ifdef IS_MPI
1504 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
1505 >                              nBins_, MPI::INT, MPI::SUM);
1506 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
1507 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1508 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
1509 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1510 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
1511 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1512 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
1513 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1514 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
1515 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1516 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
1517 >                              nBins_, MPI::INT, MPI::SUM);
1518 > #endif
1519 >
1520 >    Vector3d vel;
1521 >    RealType den;
1522 >    RealType temp;
1523 >    RealType z;
1524 >    for (int i = 0; i < nBins_; i++) {
1525 >      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
1526 >      vel.x() = binPx[i] / binMass[i];
1527 >      vel.y() = binPy[i] / binMass[i];
1528 >      vel.z() = binPz[i] / binMass[i];
1529 >
1530 >      den = binMass[i] * nBins_ * PhysicalConstants::densityConvert
1531 >        / currentSnap_->getVolume() ;
1532 >
1533 >      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
1534 >                               PhysicalConstants::energyConvert);
1535 >
1536 >      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
1537 >        if(outputMask_[j]) {
1538 >          switch(j) {
1539 >          case Z:
1540 >            (data_[j].accumulator[i])->add(z);
1541 >            break;
1542 >          case TEMPERATURE:
1543 >            data_[j].accumulator[i]->add(temp);
1544 >            break;
1545 >          case VELOCITY:
1546 >            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
1547 >            break;
1548 >          case DENSITY:
1549 >            data_[j].accumulator[i]->add(den);
1550 >            break;
1551 >          }
1552 >        }
1553        }
1554      }
1555    }
1556  
1557    void RNEMD::getStarted() {
1558 +    if (!doRNEMD_) return;
1559      collectData();
1560 <    /* now should be able to output profile in step 0, but might not be useful
1063 <       Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1064 <       Stats& stat = currentSnap_->statData;
1065 <       stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1066 <    */
1067 <    getStatus();
1560 >    writeOutputFile();
1561    }
1562  
1563 <  void RNEMD::getStatus() {
1564 <
1565 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1566 <    Stats& stat = currentSnap_->statData;
1567 <    RealType time = currentSnap_->getTime();
1568 <
1569 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1570 <    //or to be more meaningful, define another item as exchangeSum_ / time
1571 <    int j;
1572 <
1563 >  void RNEMD::parseOutputFileFormat(const std::string& format) {
1564 >    if (!doRNEMD_) return;
1565 >    StringTokenizer tokenizer(format, " ,;|\t\n\r");
1566 >    
1567 >    while(tokenizer.hasMoreTokens()) {
1568 >      std::string token(tokenizer.nextToken());
1569 >      toUpper(token);
1570 >      OutputMapType::iterator i = outputMap_.find(token);
1571 >      if (i != outputMap_.end()) {
1572 >        outputMask_.set(i->second);
1573 >      } else {
1574 >        sprintf( painCave.errMsg,
1575 >                 "RNEMD::parseOutputFileFormat: %s is not a recognized\n"
1576 >                 "\toutputFileFormat keyword.\n", token.c_str() );
1577 >        painCave.isFatal = 0;
1578 >        painCave.severity = OPENMD_ERROR;
1579 >        simError();            
1580 >      }
1581 >    }  
1582 >  }
1583 >  
1584 >  void RNEMD::writeOutputFile() {
1585 >    if (!doRNEMD_) return;
1586 >    
1587   #ifdef IS_MPI
1081
1082    // all processors have the same number of bins, and STL vectors pack their
1083    // arrays, so in theory, this should be safe:
1084
1085    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueHist_[0],
1086                              rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1087    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueCount_[0],
1088                              rnemdLogWidth_, MPI::INT, MPI::SUM);
1089    if (output3DTemp_) {
1090      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xTempHist_[0],
1091                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1092      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &yTempHist_[0],
1093                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1094      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &zTempHist_[0],
1095                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1096      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xyzTempCount_[0],
1097                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1098    }
1588      // If we're the root node, should we print out the results
1589      int worldRank = MPI::COMM_WORLD.Get_rank();
1590      if (worldRank == 0) {
1591   #endif
1592 <      rnemdLog_ << time;
1593 <      for (j = 0; j < rnemdLogWidth_; j++) {
1594 <        rnemdLog_ << "\t" << valueHist_[j] / (RealType)valueCount_[j];
1592 >      rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
1593 >      
1594 >      if( !rnemdFile_ ){        
1595 >        sprintf( painCave.errMsg,
1596 >                 "Could not open \"%s\" for RNEMD output.\n",
1597 >                 rnemdFileName_.c_str());
1598 >        painCave.isFatal = 1;
1599 >        simError();
1600        }
1601 <      rnemdLog_ << "\n";
1602 <      if (output3DTemp_) {
1603 <        xTempLog_ << time;      
1604 <        for (j = 0; j < rnemdLogWidth_; j++) {
1605 <          xTempLog_ << "\t" << xTempHist_[j] / (RealType)xyzTempCount_[j];
1601 >
1602 >      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1603 >
1604 >      RealType time = currentSnap_->getTime();
1605 >      RealType avgArea;
1606 >      areaAccumulator_->getAverage(avgArea);
1607 >      RealType Jz = kineticExchange_ / (2.0 * time * avgArea)
1608 >        / PhysicalConstants::energyConvert;
1609 >      Vector3d JzP = momentumExchange_ / (2.0 * time * avgArea);      
1610 >
1611 >      rnemdFile_ << "#######################################################\n";
1612 >      rnemdFile_ << "# RNEMD {\n";
1613 >
1614 >      map<string, RNEMDMethod>::iterator mi;
1615 >      for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
1616 >        if ( (*mi).second == rnemdMethod_)
1617 >          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
1618 >      }
1619 >      map<string, RNEMDFluxType>::iterator fi;
1620 >      for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
1621 >        if ( (*fi).second == rnemdFluxType_)
1622 >          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
1623 >      }
1624 >      
1625 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
1626 >
1627 >      rnemdFile_ << "#    objectSelection = \""
1628 >                 << rnemdObjectSelection_ << "\";\n";
1629 >      rnemdFile_ << "#    slabWidth = " << slabWidth_ << ";\n";
1630 >      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << ";\n";
1631 >      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << ";\n";
1632 >      rnemdFile_ << "# }\n";
1633 >      rnemdFile_ << "#######################################################\n";
1634 >      rnemdFile_ << "# RNEMD report:\n";      
1635 >      rnemdFile_ << "#     running time = " << time << " fs\n";
1636 >      rnemdFile_ << "#     target flux:\n";
1637 >      rnemdFile_ << "#         kinetic = "
1638 >                 << kineticFlux_ / PhysicalConstants::energyConvert
1639 >                 << " (kcal/mol/A^2/fs)\n";
1640 >      rnemdFile_ << "#         momentum = " << momentumFluxVector_
1641 >                 << " (amu/A/fs^2)\n";
1642 >      rnemdFile_ << "#     target one-time exchanges:\n";
1643 >      rnemdFile_ << "#         kinetic = "
1644 >                 << kineticTarget_ / PhysicalConstants::energyConvert
1645 >                 << " (kcal/mol)\n";
1646 >      rnemdFile_ << "#         momentum = " << momentumTarget_
1647 >                 << " (amu*A/fs)\n";
1648 >      rnemdFile_ << "#     actual exchange totals:\n";
1649 >      rnemdFile_ << "#         kinetic = "
1650 >                 << kineticExchange_ / PhysicalConstants::energyConvert
1651 >                 << " (kcal/mol)\n";
1652 >      rnemdFile_ << "#         momentum = " << momentumExchange_
1653 >                 << " (amu*A/fs)\n";      
1654 >      rnemdFile_ << "#     actual flux:\n";
1655 >      rnemdFile_ << "#         kinetic = " << Jz
1656 >                 << " (kcal/mol/A^2/fs)\n";
1657 >      rnemdFile_ << "#         momentum = " << JzP
1658 >                 << " (amu/A/fs^2)\n";
1659 >      rnemdFile_ << "#     exchange statistics:\n";
1660 >      rnemdFile_ << "#         attempted = " << trialCount_ << "\n";
1661 >      rnemdFile_ << "#         failed = " << failTrialCount_ << "\n";    
1662 >      if (rnemdMethod_ == rnemdNIVS) {
1663 >        rnemdFile_ << "#         NIVS root-check errors = "
1664 >                   << failRootCount_ << "\n";
1665 >      }
1666 >      rnemdFile_ << "#######################################################\n";
1667 >      
1668 >      
1669 >      
1670 >      //write title
1671 >      rnemdFile_ << "#";
1672 >      for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1673 >        if (outputMask_[i]) {
1674 >          rnemdFile_ << "\t" << data_[i].title <<
1675 >            "(" << data_[i].units << ")";
1676 >          // add some extra tabs for column alignment
1677 >          if (data_[i].dataType == "Vector3d") rnemdFile_ << "\t\t";
1678          }
1679 <        xTempLog_ << "\n";
1680 <        yTempLog_ << time;
1681 <        for (j = 0; j < rnemdLogWidth_; j++) {
1682 <          yTempLog_ << "\t" << yTempHist_[j] / (RealType)xyzTempCount_[j];
1679 >      }
1680 >      rnemdFile_ << std::endl;
1681 >      
1682 >      rnemdFile_.precision(8);
1683 >      
1684 >      for (unsigned int j = 0; j < nBins_; j++) {        
1685 >        
1686 >        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1687 >          if (outputMask_[i]) {
1688 >            if (data_[i].dataType == "RealType")
1689 >              writeReal(i,j);
1690 >            else if (data_[i].dataType == "Vector3d")
1691 >              writeVector(i,j);
1692 >            else {
1693 >              sprintf( painCave.errMsg,
1694 >                       "RNEMD found an unknown data type for: %s ",
1695 >                       data_[i].title.c_str());
1696 >              painCave.isFatal = 1;
1697 >              simError();
1698 >            }
1699 >          }
1700          }
1701 <        yTempLog_ << "\n";
1702 <        zTempLog_ << time;
1703 <        for (j = 0; j < rnemdLogWidth_; j++) {
1704 <          zTempLog_ << "\t" << zTempHist_[j] / (RealType)xyzTempCount_[j];
1701 >        rnemdFile_ << std::endl;
1702 >        
1703 >      }        
1704 >
1705 >      rnemdFile_ << "#######################################################\n";
1706 >      rnemdFile_ << "# Standard Deviations in those quantities follow:\n";
1707 >      rnemdFile_ << "#######################################################\n";
1708 >
1709 >
1710 >      for (unsigned int j = 0; j < nBins_; j++) {        
1711 >        rnemdFile_ << "#";
1712 >        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1713 >          if (outputMask_[i]) {
1714 >            if (data_[i].dataType == "RealType")
1715 >              writeRealStdDev(i,j);
1716 >            else if (data_[i].dataType == "Vector3d")
1717 >              writeVectorStdDev(i,j);
1718 >            else {
1719 >              sprintf( painCave.errMsg,
1720 >                       "RNEMD found an unknown data type for: %s ",
1721 >                       data_[i].title.c_str());
1722 >              painCave.isFatal = 1;
1723 >              simError();
1724 >            }
1725 >          }
1726          }
1727 <        zTempLog_ << "\n";
1728 <      }
1727 >        rnemdFile_ << std::endl;
1728 >        
1729 >      }        
1730 >      
1731 >      rnemdFile_.flush();
1732 >      rnemdFile_.close();
1733 >      
1734   #ifdef IS_MPI
1735      }
1736   #endif
1737 <    for (j = 0; j < rnemdLogWidth_; j++) {
1738 <      valueCount_[j] = 0;
1739 <      valueHist_[j] = 0.0;
1737 >    
1738 >  }
1739 >  
1740 >  void RNEMD::writeReal(int index, unsigned int bin) {
1741 >    if (!doRNEMD_) return;
1742 >    assert(index >=0 && index < ENDINDEX);
1743 >    assert(bin < nBins_);
1744 >    RealType s;
1745 >    
1746 >    data_[index].accumulator[bin]->getAverage(s);
1747 >    
1748 >    if (! isinf(s) && ! isnan(s)) {
1749 >      rnemdFile_ << "\t" << s;
1750 >    } else{
1751 >      sprintf( painCave.errMsg,
1752 >               "RNEMD detected a numerical error writing: %s for bin %d",
1753 >               data_[index].title.c_str(), bin);
1754 >      painCave.isFatal = 1;
1755 >      simError();
1756 >    }    
1757 >  }
1758 >  
1759 >  void RNEMD::writeVector(int index, unsigned int bin) {
1760 >    if (!doRNEMD_) return;
1761 >    assert(index >=0 && index < ENDINDEX);
1762 >    assert(bin < nBins_);
1763 >    Vector3d s;
1764 >    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
1765 >    if (isinf(s[0]) || isnan(s[0]) ||
1766 >        isinf(s[1]) || isnan(s[1]) ||
1767 >        isinf(s[2]) || isnan(s[2]) ) {      
1768 >      sprintf( painCave.errMsg,
1769 >               "RNEMD detected a numerical error writing: %s for bin %d",
1770 >               data_[index].title.c_str(), bin);
1771 >      painCave.isFatal = 1;
1772 >      simError();
1773 >    } else {
1774 >      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1775      }
1776 <    if (output3DTemp_)
1777 <      for (j = 0; j < rnemdLogWidth_; j++) {
1778 <        xTempHist_[j] = 0.0;
1779 <        yTempHist_[j] = 0.0;
1780 <        zTempHist_[j] = 0.0;
1781 <        xyzTempCount_[j] = 0;
1782 <      }
1776 >  }  
1777 >
1778 >  void RNEMD::writeRealStdDev(int index, unsigned int bin) {
1779 >    if (!doRNEMD_) return;
1780 >    assert(index >=0 && index < ENDINDEX);
1781 >    assert(bin < nBins_);
1782 >    RealType s;
1783 >    
1784 >    data_[index].accumulator[bin]->getStdDev(s);
1785 >    
1786 >    if (! isinf(s) && ! isnan(s)) {
1787 >      rnemdFile_ << "\t" << s;
1788 >    } else{
1789 >      sprintf( painCave.errMsg,
1790 >               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1791 >               data_[index].title.c_str(), bin);
1792 >      painCave.isFatal = 1;
1793 >      simError();
1794 >    }    
1795    }
1796 +  
1797 +  void RNEMD::writeVectorStdDev(int index, unsigned int bin) {
1798 +    if (!doRNEMD_) return;
1799 +    assert(index >=0 && index < ENDINDEX);
1800 +    assert(bin < nBins_);
1801 +    Vector3d s;
1802 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getStdDev(s);
1803 +    if (isinf(s[0]) || isnan(s[0]) ||
1804 +        isinf(s[1]) || isnan(s[1]) ||
1805 +        isinf(s[2]) || isnan(s[2]) ) {      
1806 +      sprintf( painCave.errMsg,
1807 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1808 +               data_[index].title.c_str(), bin);
1809 +      painCave.isFatal = 1;
1810 +      simError();
1811 +    } else {
1812 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1813 +    }
1814 +  }  
1815   }
1816 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines