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

Comparing:
branches/development/src/integrators/RNEMD.cpp (file contents), Revision 1723 by gezelter, Thu May 24 20:59:54 2012 UTC vs.
trunk/src/rnemd/RNEMD.cpp (file contents), Revision 1789 by gezelter, Wed Aug 29 20:52:19 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"
# Line 49 | Line 49
49   #include "primitives/StuntDouble.hpp"
50   #include "utils/PhysicalConstants.hpp"
51   #include "utils/Tuple.hpp"
52 <
53 < #ifndef IS_MPI
54 < #include "math/SeqRandNumGen.hpp"
55 < #else
56 < #include "math/ParallelRandNumGen.hpp"
52 > #ifdef IS_MPI
53   #include <mpi.h>
54   #endif
55  
56 + #ifdef _MSC_VER
57 + #define isnan(x) _isnan((x))
58 + #define isinf(x) (!_finite(x) && !_isnan(x))
59 + #endif
60 +
61   #define HONKING_LARGE_VALUE 1.0e10
62  
63   using namespace std;
# Line 65 | Line 66 | namespace OpenMD {
66    RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info),
67                                  usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
68  
69 +    trialCount_ = 0;
70      failTrialCount_ = 0;
71      failRootCount_ = 0;
72  
71    int seedValue;
73      Globals * simParams = info->getSimParams();
74 +    RNEMDParameters* rnemdParams = simParams->getRNEMDParameters();
75  
76 <    stringToEnumMap_["KineticSwap"] = rnemdKineticSwap;
77 <    stringToEnumMap_["KineticScale"] = rnemdKineticScale;
76 <    stringToEnumMap_["KineticScaleVAM"] = rnemdKineticScaleVAM;
77 <    stringToEnumMap_["KineticScaleAM"] = rnemdKineticScaleAM;
78 <    stringToEnumMap_["PxScale"] = rnemdPxScale;
79 <    stringToEnumMap_["PyScale"] = rnemdPyScale;
80 <    stringToEnumMap_["PzScale"] = rnemdPzScale;
81 <    stringToEnumMap_["Px"] = rnemdPx;
82 <    stringToEnumMap_["Py"] = rnemdPy;
83 <    stringToEnumMap_["Pz"] = rnemdPz;
84 <    stringToEnumMap_["ShiftScaleV"] = rnemdShiftScaleV;
85 <    stringToEnumMap_["ShiftScaleVAM"] = rnemdShiftScaleVAM;
86 <    stringToEnumMap_["Unknown"] = rnemdUnknown;
76 >    doRNEMD_ = rnemdParams->getUseRNEMD();
77 >    if (!doRNEMD_) return;
78  
79 <    rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
79 >    stringToMethod_["Swap"]  = rnemdSwap;
80 >    stringToMethod_["NIVS"]  = rnemdNIVS;
81 >    stringToMethod_["VSS"]   = rnemdVSS;
82 >
83 >    stringToFluxType_["KE"]  = rnemdKE;
84 >    stringToFluxType_["Px"]  = rnemdPx;
85 >    stringToFluxType_["Py"]  = rnemdPy;
86 >    stringToFluxType_["Pz"]  = rnemdPz;
87 >    stringToFluxType_["Pvector"]  = rnemdPvector;
88 >    stringToFluxType_["KE+Px"]  = rnemdKePx;
89 >    stringToFluxType_["KE+Py"]  = rnemdKePy;
90 >    stringToFluxType_["KE+Pvector"]  = rnemdKePvector;
91 >
92 >    runTime_ = simParams->getRunTime();
93 >    statusTime_ = simParams->getStatusTime();
94 >
95 >    rnemdObjectSelection_ = rnemdParams->getObjectSelection();
96      evaluator_.loadScriptString(rnemdObjectSelection_);
97      seleMan_.setSelectionSet(evaluator_.evaluate());
98  
99 +    const string methStr = rnemdParams->getMethod();
100 +    bool hasFluxType = rnemdParams->haveFluxType();
101 +
102 +    string fluxStr;
103 +    if (hasFluxType) {
104 +      fluxStr = rnemdParams->getFluxType();
105 +    } else {
106 +      sprintf(painCave.errMsg,
107 +              "RNEMD: No fluxType was set in the md file.  This parameter,\n"
108 +              "\twhich must be one of the following values:\n"
109 +              "\tKE, Px, Py, Pz, Pvector, KE+Px, KE+Py, KE+Pvector\n"
110 +              "\tmust be set to use RNEMD\n");
111 +      painCave.isFatal = 1;
112 +      painCave.severity = OPENMD_ERROR;
113 +      simError();
114 +    }
115 +
116 +    bool hasKineticFlux = rnemdParams->haveKineticFlux();
117 +    bool hasMomentumFlux = rnemdParams->haveMomentumFlux();
118 +    bool hasMomentumFluxVector = rnemdParams->haveMomentumFluxVector();
119 +    bool hasSlabWidth = rnemdParams->haveSlabWidth();
120 +    bool hasSlabACenter = rnemdParams->haveSlabACenter();
121 +    bool hasSlabBCenter = rnemdParams->haveSlabBCenter();
122 +    bool hasOutputFileName = rnemdParams->haveOutputFileName();
123 +    bool hasOutputFields = rnemdParams->haveOutputFields();
124 +    
125 +    map<string, RNEMDMethod>::iterator i;
126 +    i = stringToMethod_.find(methStr);
127 +    if (i != stringToMethod_.end())
128 +      rnemdMethod_ = i->second;
129 +    else {
130 +      sprintf(painCave.errMsg,
131 +              "RNEMD: The current method,\n"
132 +              "\t\t%s is not one of the recognized\n"
133 +              "\texchange methods: Swap, NIVS, or VSS\n",
134 +              methStr.c_str());
135 +      painCave.isFatal = 1;
136 +      painCave.severity = OPENMD_ERROR;
137 +      simError();
138 +    }
139 +
140 +    map<string, RNEMDFluxType>::iterator j;
141 +    j = stringToFluxType_.find(fluxStr);
142 +    if (j != stringToFluxType_.end())
143 +      rnemdFluxType_ = j->second;
144 +    else {
145 +      sprintf(painCave.errMsg,
146 +              "RNEMD: The current fluxType,\n"
147 +              "\t\t%s\n"
148 +              "\tis not one of the recognized flux types.\n",
149 +              fluxStr.c_str());
150 +      painCave.isFatal = 1;
151 +      painCave.severity = OPENMD_ERROR;
152 +      simError();
153 +    }
154 +
155 +    bool methodFluxMismatch = false;
156 +    bool hasCorrectFlux = false;
157 +    switch(rnemdMethod_) {
158 +    case rnemdSwap:
159 +      switch (rnemdFluxType_) {
160 +      case rnemdKE:
161 +        hasCorrectFlux = hasKineticFlux;
162 +        break;
163 +      case rnemdPx:
164 +      case rnemdPy:
165 +      case rnemdPz:
166 +        hasCorrectFlux = hasMomentumFlux;
167 +        break;
168 +      default :
169 +        methodFluxMismatch = true;
170 +        break;
171 +      }
172 +      break;
173 +    case rnemdNIVS:
174 +      switch (rnemdFluxType_) {
175 +      case rnemdKE:
176 +      case rnemdRotKE:
177 +      case rnemdFullKE:
178 +        hasCorrectFlux = hasKineticFlux;
179 +        break;
180 +      case rnemdPx:
181 +      case rnemdPy:
182 +      case rnemdPz:
183 +        hasCorrectFlux = hasMomentumFlux;
184 +        break;
185 +      case rnemdKePx:
186 +      case rnemdKePy:
187 +        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
188 +        break;
189 +      default:
190 +        methodFluxMismatch = true;
191 +        break;
192 +      }
193 +      break;
194 +    case rnemdVSS:
195 +      switch (rnemdFluxType_) {
196 +      case rnemdKE:
197 +      case rnemdRotKE:
198 +      case rnemdFullKE:
199 +        hasCorrectFlux = hasKineticFlux;
200 +        break;
201 +      case rnemdPx:
202 +      case rnemdPy:
203 +      case rnemdPz:
204 +        hasCorrectFlux = hasMomentumFlux;
205 +        break;
206 +      case rnemdPvector:
207 +        hasCorrectFlux = hasMomentumFluxVector;
208 +        break;
209 +      case rnemdKePx:
210 +      case rnemdKePy:
211 +        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
212 +        break;
213 +      case rnemdKePvector:
214 +        hasCorrectFlux = hasMomentumFluxVector && hasKineticFlux;
215 +        break;
216 +      default:
217 +        methodFluxMismatch = true;
218 +        break;
219 +      }
220 +    default:
221 +      break;
222 +    }
223 +
224 +    if (methodFluxMismatch) {
225 +      sprintf(painCave.errMsg,
226 +              "RNEMD: The current method,\n"
227 +              "\t\t%s\n"
228 +              "\tcannot be used with the current flux type, %s\n",
229 +              methStr.c_str(), fluxStr.c_str());
230 +      painCave.isFatal = 1;
231 +      painCave.severity = OPENMD_ERROR;
232 +      simError();        
233 +    }
234 +    if (!hasCorrectFlux) {
235 +      sprintf(painCave.errMsg,
236 +              "RNEMD: The current method, %s, and flux type, %s,\n"
237 +              "\tdid not have the correct flux value specified. Options\n"
238 +              "\tinclude: kineticFlux, momentumFlux, and momentumFluxVector\n",
239 +              methStr.c_str(), fluxStr.c_str());
240 +      painCave.isFatal = 1;
241 +      painCave.severity = OPENMD_ERROR;
242 +      simError();        
243 +    }
244 +
245 +    if (hasKineticFlux) {
246 +      // convert the kcal / mol / Angstroms^2 / fs values in the md file
247 +      // into  amu / fs^3:
248 +      kineticFlux_ = rnemdParams->getKineticFlux()
249 +        * PhysicalConstants::energyConvert;
250 +    } else {
251 +      kineticFlux_ = 0.0;
252 +    }
253 +    if (hasMomentumFluxVector) {
254 +      momentumFluxVector_ = rnemdParams->getMomentumFluxVector();
255 +    } else {
256 +      momentumFluxVector_ = V3Zero;
257 +      if (hasMomentumFlux) {
258 +        RealType momentumFlux = rnemdParams->getMomentumFlux();
259 +        switch (rnemdFluxType_) {
260 +        case rnemdPx:
261 +          momentumFluxVector_.x() = momentumFlux;
262 +          break;
263 +        case rnemdPy:
264 +          momentumFluxVector_.y() = momentumFlux;
265 +          break;
266 +        case rnemdPz:
267 +          momentumFluxVector_.z() = momentumFlux;
268 +          break;
269 +        case rnemdKePx:
270 +          momentumFluxVector_.x() = momentumFlux;
271 +          break;
272 +        case rnemdKePy:
273 +          momentumFluxVector_.y() = momentumFlux;
274 +          break;
275 +        default:
276 +          break;
277 +        }
278 +      }    
279 +    }
280 +
281      // do some sanity checking
282  
283      int selectionCount = seleMan_.getSelectionCount();
# Line 96 | Line 285 | namespace OpenMD {
285  
286      if (selectionCount > nIntegrable) {
287        sprintf(painCave.errMsg,
288 <              "RNEMD: The current RNEMD_objectSelection,\n"
288 >              "RNEMD: The current objectSelection,\n"
289                "\t\t%s\n"
290                "\thas resulted in %d selected objects.  However,\n"
291                "\tthe total number of integrable objects in the system\n"
# Line 109 | Line 298 | namespace OpenMD {
298        painCave.severity = OPENMD_WARNING;
299        simError();
300      }
112    
113    const string st = simParams->getRNEMD_exchangeType();
301  
302 <    map<string, RNEMDTypeEnum>::iterator i;
116 <    i = stringToEnumMap_.find(st);
117 <    rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
118 <    if (rnemdType_ == rnemdUnknown) {
119 <      sprintf(painCave.errMsg,
120 <              "RNEMD: The current RNEMD_exchangeType,\n"
121 <              "\t\t%s\n"
122 <              "\tis not one of the recognized exchange types.\n",
123 <              st.c_str());
124 <      painCave.isFatal = 1;
125 <      painCave.severity = OPENMD_ERROR;
126 <      simError();
127 <    }
128 <    
129 <    outputTemp_ = false;
130 <    if (simParams->haveRNEMD_outputTemperature()) {
131 <      outputTemp_ = simParams->getRNEMD_outputTemperature();
132 <    } else if ((rnemdType_ == rnemdKineticSwap) ||
133 <               (rnemdType_ == rnemdKineticScale) ||
134 <               (rnemdType_ == rnemdKineticScaleVAM) ||
135 <               (rnemdType_ == rnemdKineticScaleAM)) {
136 <      outputTemp_ = true;
137 <    }
138 <    outputVx_ = false;
139 <    if (simParams->haveRNEMD_outputVx()) {
140 <      outputVx_ = simParams->getRNEMD_outputVx();
141 <    } else if ((rnemdType_ == rnemdPx) || (rnemdType_ == rnemdPxScale)) {
142 <      outputVx_ = true;
143 <    }
144 <    outputVy_ = false;
145 <    if (simParams->haveRNEMD_outputVy()) {
146 <      outputVy_ = simParams->getRNEMD_outputVy();
147 <    } else if ((rnemdType_ == rnemdPy) || (rnemdType_ == rnemdPyScale)) {
148 <      outputVy_ = true;
149 <    }
150 <    output3DTemp_ = false;
151 <    if (simParams->haveRNEMD_outputXyzTemperature()) {
152 <      output3DTemp_ = simParams->getRNEMD_outputXyzTemperature();
153 <    }
154 <    outputRotTemp_ = false;
155 <    if (simParams->haveRNEMD_outputRotTemperature()) {
156 <      outputRotTemp_ = simParams->getRNEMD_outputRotTemperature();
157 <    }
302 >    areaAccumulator_ = new Accumulator();
303  
304 < #ifdef IS_MPI
160 <    if (worldRank == 0) {
161 < #endif
304 >    nBins_ = rnemdParams->getOutputBins();
305  
306 <      //may have rnemdWriter separately
307 <      string rnemdFileName;
306 >    data_.resize(RNEMD::ENDINDEX);
307 >    OutputData z;
308 >    z.units =  "Angstroms";
309 >    z.title =  "Z";
310 >    z.dataType = "RealType";
311 >    z.accumulator.reserve(nBins_);
312 >    for (int i = 0; i < nBins_; i++)
313 >      z.accumulator.push_back( new Accumulator() );
314 >    data_[Z] = z;
315 >    outputMap_["Z"] =  Z;
316  
317 <      if (outputTemp_) {
318 <        rnemdFileName = "temperature.log";
319 <        tempLog_.open(rnemdFileName.c_str());
320 <      }
321 <      if (outputVx_) {
322 <        rnemdFileName = "velocityX.log";
323 <        vxzLog_.open(rnemdFileName.c_str());
324 <      }
325 <      if (outputVy_) {
175 <        rnemdFileName = "velocityY.log";
176 <        vyzLog_.open(rnemdFileName.c_str());
177 <      }
317 >    OutputData temperature;
318 >    temperature.units =  "K";
319 >    temperature.title =  "Temperature";
320 >    temperature.dataType = "RealType";
321 >    temperature.accumulator.reserve(nBins_);
322 >    for (int i = 0; i < nBins_; i++)
323 >      temperature.accumulator.push_back( new Accumulator() );
324 >    data_[TEMPERATURE] = temperature;
325 >    outputMap_["TEMPERATURE"] =  TEMPERATURE;
326  
327 <      if (output3DTemp_) {
328 <        rnemdFileName = "temperatureX.log";
329 <        xTempLog_.open(rnemdFileName.c_str());
330 <        rnemdFileName = "temperatureY.log";
331 <        yTempLog_.open(rnemdFileName.c_str());
332 <        rnemdFileName = "temperatureZ.log";
333 <        zTempLog_.open(rnemdFileName.c_str());
334 <      }
335 <      if (outputRotTemp_) {
188 <        rnemdFileName = "temperatureR.log";
189 <        rotTempLog_.open(rnemdFileName.c_str());
190 <      }
327 >    OutputData velocity;
328 >    velocity.units = "angstroms/fs";
329 >    velocity.title =  "Velocity";  
330 >    velocity.dataType = "Vector3d";
331 >    velocity.accumulator.reserve(nBins_);
332 >    for (int i = 0; i < nBins_; i++)
333 >      velocity.accumulator.push_back( new VectorAccumulator() );
334 >    data_[VELOCITY] = velocity;
335 >    outputMap_["VELOCITY"] = VELOCITY;
336  
337 < #ifdef IS_MPI
338 <    }
339 < #endif
337 >    OutputData density;
338 >    density.units =  "g cm^-3";
339 >    density.title =  "Density";
340 >    density.dataType = "RealType";
341 >    density.accumulator.reserve(nBins_);
342 >    for (int i = 0; i < nBins_; i++)
343 >      density.accumulator.push_back( new Accumulator() );
344 >    data_[DENSITY] = density;
345 >    outputMap_["DENSITY"] =  DENSITY;
346  
347 <    set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
348 <    set_RNEMD_nBins(simParams->getRNEMD_nBins());
198 <    midBin_ = nBins_ / 2;
199 <    if (simParams->haveRNEMD_binShift()) {
200 <      if (simParams->getRNEMD_binShift()) {
201 <        zShift_ = 0.5 / (RealType)(nBins_);
202 <      } else {
203 <        zShift_ = 0.0;
204 <      }
347 >    if (hasOutputFields) {
348 >      parseOutputFileFormat(rnemdParams->getOutputFields());
349      } else {
350 <      zShift_ = 0.0;
350 >      outputMask_.set(Z);
351 >      switch (rnemdFluxType_) {
352 >      case rnemdKE:
353 >      case rnemdRotKE:
354 >      case rnemdFullKE:
355 >        outputMask_.set(TEMPERATURE);
356 >        break;
357 >      case rnemdPx:
358 >      case rnemdPy:
359 >        outputMask_.set(VELOCITY);
360 >        break;
361 >      case rnemdPz:        
362 >      case rnemdPvector:
363 >        outputMask_.set(VELOCITY);
364 >        outputMask_.set(DENSITY);
365 >        break;
366 >      case rnemdKePx:
367 >      case rnemdKePy:
368 >        outputMask_.set(TEMPERATURE);
369 >        outputMask_.set(VELOCITY);
370 >        break;
371 >      case rnemdKePvector:
372 >        outputMask_.set(TEMPERATURE);
373 >        outputMask_.set(VELOCITY);
374 >        outputMask_.set(DENSITY);        
375 >        break;
376 >      default:
377 >        break;
378 >      }
379      }
380 <    //cerr << "I shift slabs by " << zShift_ << " Lz\n";
381 <    //shift slabs by half slab width, maybe useful in heterogeneous systems
382 <    //set to 0.0 if not using it; N/A in status output yet
211 <    if (simParams->haveRNEMD_logWidth()) {
212 <      set_RNEMD_logWidth(simParams->getRNEMD_logWidth());
213 <      /*arbitary rnemdLogWidth_, no checking;
214 <      if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
215 <        cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
216 <        cerr << "Automaically set back to default.\n";
217 <        rnemdLogWidth_ = nBins_;
218 <      }*/
380 >      
381 >    if (hasOutputFileName) {
382 >      rnemdFileName_ = rnemdParams->getOutputFileName();
383      } else {
384 <      set_RNEMD_logWidth(nBins_);
385 <    }
222 <    tempHist_.resize(rnemdLogWidth_, 0.0);
223 <    tempCount_.resize(rnemdLogWidth_, 0);
224 <    pxzHist_.resize(rnemdLogWidth_, 0.0);
225 <    //vxzCount_.resize(rnemdLogWidth_, 0);
226 <    pyzHist_.resize(rnemdLogWidth_, 0.0);
227 <    //vyzCount_.resize(rnemdLogWidth_, 0);
384 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
385 >    }          
386  
387 <    mHist_.resize(rnemdLogWidth_, 0.0);
230 <    xTempHist_.resize(rnemdLogWidth_, 0.0);
231 <    yTempHist_.resize(rnemdLogWidth_, 0.0);
232 <    zTempHist_.resize(rnemdLogWidth_, 0.0);
233 <    xyzTempCount_.resize(rnemdLogWidth_, 0);
234 <    rotTempHist_.resize(rnemdLogWidth_, 0.0);
235 <    rotTempCount_.resize(rnemdLogWidth_, 0);
387 >    exchangeTime_ = rnemdParams->getExchangeTime();
388  
389 <    set_RNEMD_exchange_total(0.0);
390 <    if (simParams->haveRNEMD_targetFlux()) {
391 <      set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
392 <    } else {
393 <      set_RNEMD_target_flux(0.0);
394 <    }
395 <    if (simParams->haveRNEMD_targetJzKE()) {
244 <      set_RNEMD_target_JzKE(simParams->getRNEMD_targetJzKE());
245 <    } else {
246 <      set_RNEMD_target_JzKE(0.0);
247 <    }
248 <    if (simParams->haveRNEMD_targetJzpx()) {
249 <      set_RNEMD_target_jzpx(simParams->getRNEMD_targetJzpx());
250 <    } else {
251 <      set_RNEMD_target_jzpx(0.0);
252 <    }
253 <    jzp_.x() = targetJzpx_;
254 <    njzp_.x() = -targetJzpx_;
255 <    if (simParams->haveRNEMD_targetJzpy()) {
256 <      set_RNEMD_target_jzpy(simParams->getRNEMD_targetJzpy());
257 <    } else {
258 <      set_RNEMD_target_jzpy(0.0);
259 <    }
260 <    jzp_.y() = targetJzpy_;
261 <    njzp_.y() = -targetJzpy_;
262 <    if (simParams->haveRNEMD_targetJzpz()) {
263 <      set_RNEMD_target_jzpz(simParams->getRNEMD_targetJzpz());
264 <    } else {
265 <      set_RNEMD_target_jzpz(0.0);
266 <    }
267 <    jzp_.z() = targetJzpz_;
268 <    njzp_.z() = -targetJzpz_;
389 >    Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
390 >    Mat3x3d hmat = currentSnap_->getHmat();
391 >  
392 >    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
393 >    // Lx, Ly = box dimensions in x & y
394 >    // dt = exchange time interval
395 >    // flux = target flux
396  
397 < #ifndef IS_MPI
398 <    if (simParams->haveSeed()) {
399 <      seedValue = simParams->getSeed();
400 <      randNumGen_ = new SeqRandNumGen(seedValue);
401 <    }else {
402 <      randNumGen_ = new SeqRandNumGen();
403 <    }    
404 < #else
405 <    if (simParams->haveSeed()) {
406 <      seedValue = simParams->getSeed();
407 <      randNumGen_ = new ParallelRandNumGen(seedValue);
408 <    }else {
409 <      randNumGen_ = new ParallelRandNumGen();
283 <    }    
284 < #endif
285 <  }
397 >    RealType area = currentSnap_->getXYarea();
398 >    kineticTarget_ = 2.0 * kineticFlux_ * exchangeTime_ * area;
399 >    momentumTarget_ = 2.0 * momentumFluxVector_ * exchangeTime_ * area;
400 >
401 >    // total exchange sums are zeroed out at the beginning:
402 >
403 >    kineticExchange_ = 0.0;
404 >    momentumExchange_ = V3Zero;
405 >
406 >    if (hasSlabWidth)
407 >      slabWidth_ = rnemdParams->getSlabWidth();
408 >    else
409 >      slabWidth_ = hmat(2,2) / 10.0;
410    
411 <  RNEMD::~RNEMD() {
412 <    delete randNumGen_;
411 >    if (hasSlabACenter)
412 >      slabACenter_ = rnemdParams->getSlabACenter();
413 >    else
414 >      slabACenter_ = 0.0;
415      
416 +    if (hasSlabBCenter)
417 +      slabBCenter_ = rnemdParams->getSlabBCenter();
418 +    else
419 +      slabBCenter_ = hmat(2,2) / 2.0;
420 +    
421 +  }
422 +  
423 +  RNEMD::~RNEMD() {
424 +    if (!doRNEMD_) return;
425   #ifdef IS_MPI
426      if (worldRank == 0) {
427   #endif
293      
294      sprintf(painCave.errMsg,
295              "RNEMD: total failed trials: %d\n",
296              failTrialCount_);
297      painCave.isFatal = 0;
298      painCave.severity = OPENMD_INFO;
299      simError();
428  
429 <      if (outputTemp_) tempLog_.close();
302 <      if (outputVx_)   vxzLog_.close();
303 <      if (outputVy_)   vyzLog_.close();
429 >      writeOutputFile();
430  
431 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
432 <          rnemdType_ == rnemdPyScale) {
307 <        sprintf(painCave.errMsg,
308 <                "RNEMD: total root-checking warnings: %d\n",
309 <                failRootCount_);
310 <        painCave.isFatal = 0;
311 <        painCave.severity = OPENMD_INFO;
312 <        simError();
313 <      }
314 <      if (output3DTemp_) {
315 <        xTempLog_.close();
316 <        yTempLog_.close();
317 <        zTempLog_.close();
318 <      }
319 <      if (outputRotTemp_) rotTempLog_.close();
320 <
431 >      rnemdFile_.close();
432 >      
433   #ifdef IS_MPI
434      }
435   #endif
436    }
437 +  
438 +  bool RNEMD::inSlabA(Vector3d pos) {
439 +    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
440 +  }
441 +  bool RNEMD::inSlabB(Vector3d pos) {
442 +    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
443 +  }
444  
445    void RNEMD::doSwap() {
446 <
446 >    if (!doRNEMD_) return;
447      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
448      Mat3x3d hmat = currentSnap_->getHmat();
449  
# Line 353 | Line 472 | namespace OpenMD {
472  
473        if (usePeriodicBoundaryConditions_)
474          currentSnap_->wrapVector(pos);
475 +      bool inA = inSlabA(pos);
476 +      bool inB = inSlabB(pos);
477  
478 <      // which bin is this stuntdouble in?
358 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
359 <
360 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
361 <
362 <
363 <      // if we're in bin 0 or the middleBin
364 <      if (binNo == 0 || binNo == midBin_) {
478 >      if (inA || inB) {
479          
480          RealType mass = sd->getMass();
481          Vector3d vel = sd->getVel();
482          RealType value;
483 <
484 <        switch(rnemdType_) {
485 <        case rnemdKineticSwap :
483 >        
484 >        switch(rnemdFluxType_) {
485 >        case rnemdKE :
486            
487            value = mass * vel.lengthSquare();
488            
# Line 388 | Line 502 | namespace OpenMD {
502                  + angMom[2]*angMom[2]/I(2, 2);
503              }
504            } //angular momenta exchange enabled
391          //energyConvert temporarily disabled
392          //make exchangeSum_ comparable between swap & scale
393          //value = value * 0.5 / PhysicalConstants::energyConvert;
505            value *= 0.5;
506            break;
507          case rnemdPx :
# Line 406 | Line 517 | namespace OpenMD {
517            break;
518          }
519          
520 <        if (binNo == 0) {
520 >        if (inA == 0) {
521            if (!min_found) {
522              min_val = value;
523              min_sd = sd;
# Line 417 | Line 528 | namespace OpenMD {
528                min_sd = sd;
529              }
530            }
531 <        } else { //midBin_
531 >        } else {
532            if (!max_found) {
533              max_val = value;
534              max_sd = sd;
# Line 431 | Line 542 | namespace OpenMD {
542          }
543        }
544      }
545 <
545 >    
546   #ifdef IS_MPI
547      int nProc, worldRank;
548 <
548 >    
549      nProc = MPI::COMM_WORLD.Get_size();
550      worldRank = MPI::COMM_WORLD.Get_rank();
551  
# Line 454 | Line 565 | namespace OpenMD {
565          RealType val;
566          int rank;
567        } max_vals, min_vals;
568 <    
568 >      
569        if (my_min_found) {
570          min_vals.val = min_val;
571        } else {
# Line 492 | Line 603 | namespace OpenMD {
603            Vector3d max_vel = max_sd->getVel();
604            RealType temp_vel;
605            
606 <          switch(rnemdType_) {
607 <          case rnemdKineticSwap :
606 >          switch(rnemdFluxType_) {
607 >          case rnemdKE :
608              min_sd->setVel(max_vel);
609              max_sd->setVel(min_vel);
610              if (min_sd->isDirectional() && max_sd->isDirectional()) {
# Line 544 | Line 655 | namespace OpenMD {
655                                     min_vel.getArrayPointer(), 3, MPI::REALTYPE,
656                                     min_vals.rank, 0, status);
657            
658 <          switch(rnemdType_) {
659 <          case rnemdKineticSwap :
658 >          switch(rnemdFluxType_) {
659 >          case rnemdKE :
660              max_sd->setVel(min_vel);
661              //angular momenta exchange enabled
662              if (max_sd->isDirectional()) {
# Line 590 | Line 701 | namespace OpenMD {
701                                     max_vel.getArrayPointer(), 3, MPI::REALTYPE,
702                                     max_vals.rank, 0, status);
703            
704 <          switch(rnemdType_) {
705 <          case rnemdKineticSwap :
704 >          switch(rnemdFluxType_) {
705 >          case rnemdKE :
706              min_sd->setVel(max_vel);
707              //angular momenta exchange enabled
708              if (min_sd->isDirectional()) {
# Line 625 | Line 736 | namespace OpenMD {
736            }
737          }
738   #endif
739 <        exchangeSum_ += max_val - min_val;
739 >        
740 >        switch(rnemdFluxType_) {
741 >        case rnemdKE:
742 >          kineticExchange_ += max_val - min_val;
743 >          break;
744 >        case rnemdPx:
745 >          momentumExchange_.x() += max_val - min_val;
746 >          break;
747 >        case rnemdPy:
748 >          momentumExchange_.y() += max_val - min_val;
749 >          break;
750 >        case rnemdPz:
751 >          momentumExchange_.z() += max_val - min_val;
752 >          break;
753 >        default:
754 >          break;
755 >        }
756        } else {        
757          sprintf(painCave.errMsg,
758 <                "RNEMD: exchange NOT performed because min_val > max_val\n");
758 >                "RNEMD::doSwap exchange NOT performed because min_val > max_val\n");
759          painCave.isFatal = 0;
760          painCave.severity = OPENMD_INFO;
761          simError();        
# Line 636 | Line 763 | namespace OpenMD {
763        }
764      } else {
765        sprintf(painCave.errMsg,
766 <              "RNEMD: exchange NOT performed because selected object\n"
767 <              "\tnot present in at least one of the two slabs.\n");
766 >              "RNEMD::doSwap exchange NOT performed because selected object\n"
767 >              "\twas not present in at least one of the two slabs.\n");
768        painCave.isFatal = 0;
769        painCave.severity = OPENMD_INFO;
770        simError();        
771        failTrialCount_++;
772 <    }
646 <    
772 >    }    
773    }
774    
775 <  void RNEMD::doScale() {
776 <
775 >  void RNEMD::doNIVS() {
776 >    if (!doRNEMD_) return;
777      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
778      Mat3x3d hmat = currentSnap_->getHmat();
779  
# Line 687 | Line 813 | namespace OpenMD {
813          currentSnap_->wrapVector(pos);
814  
815        // which bin is this stuntdouble in?
816 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
816 >      bool inA = inSlabA(pos);
817 >      bool inB = inSlabB(pos);
818  
819 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
820 <
694 <      // if we're in bin 0 or the middleBin
695 <      if (binNo == 0 || binNo == midBin_) {
696 <        
819 >      if (inA || inB) {
820 >              
821          RealType mass = sd->getMass();
822          Vector3d vel = sd->getVel();
823        
824 <        if (binNo == 0) {
824 >        if (inA) {
825            hotBin.push_back(sd);
826            Phx += mass * vel.x();
827            Phy += mass * vel.y();
# Line 705 | Line 829 | namespace OpenMD {
829            Khx += mass * vel.x() * vel.x();
830            Khy += mass * vel.y() * vel.y();
831            Khz += mass * vel.z() * vel.z();
708          //if (rnemdType_ == rnemdKineticScaleVAM) {
832            if (sd->isDirectional()) {
833              Vector3d angMom = sd->getJ();
834              Mat3x3d I = sd->getI();
# Line 721 | Line 844 | namespace OpenMD {
844                  + angMom[2]*angMom[2]/I(2, 2);
845              }
846            }
847 <          //}
725 <        } else { //midBin_
847 >        } else {
848            coldBin.push_back(sd);
849            Pcx += mass * vel.x();
850            Pcy += mass * vel.y();
# Line 730 | Line 852 | namespace OpenMD {
852            Kcx += mass * vel.x() * vel.x();
853            Kcy += mass * vel.y() * vel.y();
854            Kcz += mass * vel.z() * vel.z();
733          //if (rnemdType_ == rnemdKineticScaleVAM) {
855            if (sd->isDirectional()) {
856              Vector3d angMom = sd->getJ();
857              Mat3x3d I = sd->getI();
# Line 746 | Line 867 | namespace OpenMD {
867                  + angMom[2]*angMom[2]/I(2, 2);
868              }
869            }
749          //}
870          }
871        }
872      }
# Line 760 | Line 880 | namespace OpenMD {
880      Kcz *= 0.5;
881      Kcw *= 0.5;
882  
763    std::cerr << "Khx= " << Khx << "\tKhy= " << Khy << "\tKhz= " << Khz
764              << "\tKhw= " << Khw << "\tKcx= " << Kcx << "\tKcy= " << Kcy
765              << "\tKcz= " << Kcz << "\tKcw= " << Kcw << "\n";
766    std::cerr << "Phx= " << Phx << "\tPhy= " << Phy << "\tPhz= " << Phz
767              << "\tPcx= " << Pcx << "\tPcy= " << Pcy << "\tPcz= " <<Pcz<<"\n";
768
883   #ifdef IS_MPI
884      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
885      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
# Line 791 | Line 905 | namespace OpenMD {
905      RealType pz = Pcz / Phz;
906      RealType c, x, y, z;
907      bool successfulScale = false;
908 <    if ((rnemdType_ == rnemdKineticScaleVAM) ||
909 <        (rnemdType_ == rnemdKineticScaleAM)) {
908 >    if ((rnemdFluxType_ == rnemdFullKE) ||
909 >        (rnemdFluxType_ == rnemdRotKE)) {
910        //may need sanity check Khw & Kcw > 0
911  
912 <      if (rnemdType_ == rnemdKineticScaleVAM) {
913 <        c = 1.0 - targetFlux_ / (Kcx + Kcy + Kcz + Kcw);
912 >      if (rnemdFluxType_ == rnemdFullKE) {
913 >        c = 1.0 - kineticTarget_ / (Kcx + Kcy + Kcz + Kcw);
914        } else {
915 <        c = 1.0 - targetFlux_ / Kcw;
915 >        c = 1.0 - kineticTarget_ / Kcw;
916        }
917  
918        if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
919          c = sqrt(c);
920 <        std::cerr << "cold slab scaling coefficient: " << c << endl;
920 >        //std::cerr << "cold slab scaling coefficient: " << c << endl;
921          //now convert to hotBin coefficient
922          RealType w = 0.0;
923 <        if (rnemdType_ ==  rnemdKineticScaleVAM) {
923 >        if (rnemdFluxType_ ==  rnemdFullKE) {
924            x = 1.0 + px * (1.0 - c);
925            y = 1.0 + py * (1.0 - c);
926            z = 1.0 + pz * (1.0 - c);
# Line 820 | Line 934 | namespace OpenMD {
934            */
935            if ((fabs(x - 1.0) < 0.1) && (fabs(y - 1.0) < 0.1) &&
936                (fabs(z - 1.0) < 0.1)) {
937 <            w = 1.0 + (targetFlux_ + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
937 >            w = 1.0 + (kineticTarget_
938 >                       + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
939                         + Khz * (1.0 - z * z)) / Khw;
940            }//no need to calculate w if x, y or z is out of range
941          } else {
942 <          w = 1.0 + targetFlux_ / Khw;
942 >          w = 1.0 + kineticTarget_ / Khw;
943          }
944          if ((w > 0.81) && (w < 1.21)) {//restrict scaling coefficients
945            //if w is in the right range, so should be x, y, z.
946            vector<StuntDouble*>::iterator sdi;
947            Vector3d vel;
948            for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
949 <            if (rnemdType_ == rnemdKineticScaleVAM) {
949 >            if (rnemdFluxType_ == rnemdFullKE) {
950                vel = (*sdi)->getVel() * c;
836              //vel.x() *= c;
837              //vel.y() *= c;
838              //vel.z() *= c;
951                (*sdi)->setVel(vel);
952              }
953              if ((*sdi)->isDirectional()) {
954                Vector3d angMom = (*sdi)->getJ() * c;
843              //angMom[0] *= c;
844              //angMom[1] *= c;
845              //angMom[2] *= c;
955                (*sdi)->setJ(angMom);
956              }
957            }
958            w = sqrt(w);
959 <          std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
960 <                    << "\twh= " << w << endl;
959 >          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
960 >          //           << "\twh= " << w << endl;
961            for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
962 <            if (rnemdType_ == rnemdKineticScaleVAM) {
962 >            if (rnemdFluxType_ == rnemdFullKE) {
963                vel = (*sdi)->getVel();
964                vel.x() *= x;
965                vel.y() *= y;
# Line 859 | Line 968 | namespace OpenMD {
968              }
969              if ((*sdi)->isDirectional()) {
970                Vector3d angMom = (*sdi)->getJ() * w;
862              //angMom[0] *= w;
863              //angMom[1] *= w;
864              //angMom[2] *= w;
971                (*sdi)->setJ(angMom);
972              }
973            }
974            successfulScale = true;
975 <          exchangeSum_ += targetFlux_;
975 >          kineticExchange_ += kineticTarget_;
976          }
977        }
978      } else {
979        RealType a000, a110, c0, a001, a111, b01, b11, c1;
980 <      switch(rnemdType_) {
981 <      case rnemdKineticScale :
980 >      switch(rnemdFluxType_) {
981 >      case rnemdKE :
982          /* used hotBin coeff's & only scale x & y dimensions
983             RealType px = Phx / Pcx;
984             RealType py = Phy / Pcy;
985             a110 = Khy;
986 <           c0 = - Khx - Khy - targetFlux_;
986 >           c0 = - Khx - Khy - kineticTarget_;
987             a000 = Khx;
988             a111 = Kcy * py * py;
989             b11 = -2.0 * Kcy * py * (1.0 + py);
990 <           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
990 >           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + kineticTarget_;
991             b01 = -2.0 * Kcx * px * (1.0 + px);
992             a001 = Kcx * px * px;
993          */
994          //scale all three dimensions, let c_x = c_y
995          a000 = Kcx + Kcy;
996          a110 = Kcz;
997 <        c0 = targetFlux_ - Kcx - Kcy - Kcz;
997 >        c0 = kineticTarget_ - Kcx - Kcy - Kcz;
998          a001 = Khx * px * px + Khy * py * py;
999          a111 = Khz * pz * pz;
1000          b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
1001          b11 = -2.0 * Khz * pz * (1.0 + pz);
1002          c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
1003 <          + Khz * pz * (2.0 + pz) - targetFlux_;
1003 >          + Khz * pz * (2.0 + pz) - kineticTarget_;
1004          break;
1005 <      case rnemdPxScale :
1006 <        c = 1 - targetFlux_ / Pcx;
1005 >      case rnemdPx :
1006 >        c = 1 - momentumTarget_.x() / Pcx;
1007          a000 = Kcy;
1008          a110 = Kcz;
1009          c0 = Kcx * c * c - Kcx - Kcy - Kcz;
# Line 908 | Line 1014 | namespace OpenMD {
1014          c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
1015            + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
1016          break;
1017 <      case rnemdPyScale :
1018 <        c = 1 - targetFlux_ / Pcy;
1017 >      case rnemdPy :
1018 >        c = 1 - momentumTarget_.y() / Pcy;
1019          a000 = Kcx;
1020          a110 = Kcz;
1021          c0 = Kcy * c * c - Kcx - Kcy - Kcz;
# Line 920 | Line 1026 | namespace OpenMD {
1026          c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
1027            + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
1028          break;
1029 <      case rnemdPzScale ://we don't really do this, do we?
1030 <        c = 1 - targetFlux_ / Pcz;
1029 >      case rnemdPz ://we don't really do this, do we?
1030 >        c = 1 - momentumTarget_.z() / Pcz;
1031          a000 = Kcx;
1032          a110 = Kcy;
1033          c0 = Kcz * c * c - Kcx - Kcy - Kcz;
# Line 1006 | Line 1112 | namespace OpenMD {
1112          for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1113            r1 = (*rpi).first;
1114            r2 = (*rpi).second;
1115 <          switch(rnemdType_) {
1116 <          case rnemdKineticScale :
1115 >          switch(rnemdFluxType_) {
1116 >          case rnemdKE :
1117              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1118                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1119                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1120              break;
1121 <          case rnemdPxScale :
1121 >          case rnemdPx :
1122              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1123                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1124              break;
1125 <          case rnemdPyScale :
1125 >          case rnemdPy :
1126              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1127                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1128              break;
1129 <          case rnemdPzScale :
1129 >          case rnemdPz :
1130              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1131                + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1132            default :
# Line 1034 | Line 1140 | namespace OpenMD {
1140   #ifdef IS_MPI
1141          if (worldRank == 0) {
1142   #endif
1143 <          sprintf(painCave.errMsg,
1144 <                  "RNEMD: roots r1= %lf\tr2 = %lf\n",
1145 <                  bestPair.first, bestPair.second);
1146 <          painCave.isFatal = 0;
1147 <          painCave.severity = OPENMD_INFO;
1148 <          simError();
1143 >          // sprintf(painCave.errMsg,
1144 >          //         "RNEMD: roots r1= %lf\tr2 = %lf\n",
1145 >          //         bestPair.first, bestPair.second);
1146 >          // painCave.isFatal = 0;
1147 >          // painCave.severity = OPENMD_INFO;
1148 >          // simError();
1149   #ifdef IS_MPI
1150          }
1151   #endif
1152          
1153 <        switch(rnemdType_) {
1154 <        case rnemdKineticScale :
1153 >        switch(rnemdFluxType_) {
1154 >        case rnemdKE :
1155            x = bestPair.first;
1156            y = bestPair.first;
1157            z = bestPair.second;
1158            break;
1159 <        case rnemdPxScale :
1159 >        case rnemdPx :
1160            x = c;
1161            y = bestPair.first;
1162            z = bestPair.second;
1163            break;
1164 <        case rnemdPyScale :
1164 >        case rnemdPy :
1165            x = bestPair.first;
1166            y = c;
1167            z = bestPair.second;
1168            break;
1169 <        case rnemdPzScale :
1169 >        case rnemdPz :
1170            x = bestPair.first;
1171            y = bestPair.second;
1172            z = c;
# Line 1089 | Line 1195 | namespace OpenMD {
1195            (*sdi)->setVel(vel);
1196          }
1197          successfulScale = true;
1198 <        exchangeSum_ += targetFlux_;
1198 >        switch(rnemdFluxType_) {
1199 >        case rnemdKE :
1200 >          kineticExchange_ += kineticTarget_;
1201 >          break;
1202 >        case rnemdPx :
1203 >        case rnemdPy :
1204 >        case rnemdPz :
1205 >          momentumExchange_ += momentumTarget_;
1206 >          break;          
1207 >        default :
1208 >          break;
1209 >        }      
1210        }
1211      }
1212      if (successfulScale != true) {
1213        sprintf(painCave.errMsg,
1214 <              "RNEMD: exchange NOT performed!\n");
1214 >              "RNEMD::doNIVS exchange NOT performed - roots that solve\n"
1215 >              "\tthe constraint equations may not exist or there may be\n"
1216 >              "\tno selected objects in one or both slabs.\n");
1217        painCave.isFatal = 0;
1218        painCave.severity = OPENMD_INFO;
1219        simError();        
# Line 1102 | Line 1221 | namespace OpenMD {
1221      }
1222    }
1223  
1224 <  void RNEMD::doShiftScale() {
1225 <
1224 >  void RNEMD::doVSS() {
1225 >    if (!doRNEMD_) return;
1226      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1227 +    RealType time = currentSnap_->getTime();    
1228      Mat3x3d hmat = currentSnap_->getHmat();
1229  
1230      seleMan_.setSelectionSet(evaluator_.evaluate());
# Line 1121 | Line 1241 | namespace OpenMD {
1241      Vector3d Pc(V3Zero);
1242      RealType Mc = 0.0;
1243      RealType Kc = 0.0;
1244 +    
1245  
1246      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1247           sd = seleMan_.nextSelected(selei)) {
# Line 1135 | Line 1256 | namespace OpenMD {
1256          currentSnap_->wrapVector(pos);
1257  
1258        // which bin is this stuntdouble in?
1259 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1260 <
1261 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
1262 <
1142 <      // if we're in bin 0 or the middleBin
1143 <      if (binNo == 0 || binNo == midBin_) {
1259 >      bool inA = inSlabA(pos);
1260 >      bool inB = inSlabB(pos);
1261 >      
1262 >      if (inA || inB) {
1263          
1264          RealType mass = sd->getMass();
1265          Vector3d vel = sd->getVel();
1266        
1267 <        if (binNo == 0) {
1267 >        if (inA) {
1268            hotBin.push_back(sd);
1269            //std::cerr << "before, velocity = " << vel << endl;
1270            Ph += mass * vel;
1271            //std::cerr << "after, velocity = " << vel << endl;
1272            Mh += mass;
1273            Kh += mass * vel.lengthSquare();
1274 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1274 >          if (rnemdFluxType_ == rnemdFullKE) {
1275              if (sd->isDirectional()) {
1276                Vector3d angMom = sd->getJ();
1277                Mat3x3d I = sd->getI();
# Line 1174 | Line 1293 | namespace OpenMD {
1293            Pc += mass * vel;
1294            Mc += mass;
1295            Kc += mass * vel.lengthSquare();
1296 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1296 >          if (rnemdFluxType_ == rnemdFullKE) {
1297              if (sd->isDirectional()) {
1298                Vector3d angMom = sd->getJ();
1299                Mat3x3d I = sd->getI();
# Line 1198 | Line 1317 | namespace OpenMD {
1317      Kh *= 0.5;
1318      Kc *= 0.5;
1319  
1320 <    std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1321 <              << "\tKc= " << Kc << endl;
1322 <    std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1323 <
1320 >    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1321 >    //        << "\tKc= " << Kc << endl;
1322 >    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1323 >    
1324   #ifdef IS_MPI
1325      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1326      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
# Line 1214 | Line 1333 | namespace OpenMD {
1333      bool successfulExchange = false;
1334      if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1335        Vector3d vc = Pc / Mc;
1336 <      Vector3d ac = njzp_ / Mc + vc;
1337 <      RealType cNumerator = Kc - targetJzKE_ - 0.5 * Mc * ac.lengthSquare();
1336 >      Vector3d ac = -momentumTarget_ / Mc + vc;
1337 >      Vector3d acrec = -momentumTarget_ / Mc;
1338 >      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1339        if (cNumerator > 0.0) {
1340          RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1341          if (cDenominator > 0.0) {
1342            RealType c = sqrt(cNumerator / cDenominator);
1343            if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1344              Vector3d vh = Ph / Mh;
1345 <            Vector3d ah = jzp_ / Mh + vh;
1346 <            RealType hNumerator = Kh + targetJzKE_
1345 >            Vector3d ah = momentumTarget_ / Mh + vh;
1346 >            Vector3d ahrec = momentumTarget_ / Mh;
1347 >            RealType hNumerator = Kh + kineticTarget_
1348                - 0.5 * Mh * ah.lengthSquare();
1349              if (hNumerator > 0.0) {
1350                RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1351                if (hDenominator > 0.0) {
1352                  RealType h = sqrt(hNumerator / hDenominator);
1353                  if ((h > 0.9) && (h < 1.1)) {
1354 <                  std::cerr << "cold slab scaling coefficient: " << c << "\n";
1355 <                  std::cerr << "hot slab scaling coefficient: " << h << "\n";
1354 >                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1355 >                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1356                    vector<StuntDouble*>::iterator sdi;
1357                    Vector3d vel;
1358                    for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1359                      //vel = (*sdi)->getVel();
1360                      vel = ((*sdi)->getVel() - vc) * c + ac;
1361                      (*sdi)->setVel(vel);
1362 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1362 >                    if (rnemdFluxType_ == rnemdFullKE) {
1363                        if ((*sdi)->isDirectional()) {
1364                          Vector3d angMom = (*sdi)->getJ() * c;
1365                          (*sdi)->setJ(angMom);
# Line 1249 | Line 1370 | namespace OpenMD {
1370                      //vel = (*sdi)->getVel();
1371                      vel = ((*sdi)->getVel() - vh) * h + ah;
1372                      (*sdi)->setVel(vel);
1373 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1373 >                    if (rnemdFluxType_ == rnemdFullKE) {
1374                        if ((*sdi)->isDirectional()) {
1375                          Vector3d angMom = (*sdi)->getJ() * h;
1376                          (*sdi)->setJ(angMom);
# Line 1257 | Line 1378 | namespace OpenMD {
1378                      }
1379                    }
1380                    successfulExchange = true;
1381 <                  exchangeSum_ += targetFlux_;
1382 <                  // this is a redundant variable for doShiftScale() so that
1262 <                  // RNEMD can output one exchange quantity needed in a job.
1263 <                  // need a better way to do this.
1381 >                  kineticExchange_ += kineticTarget_;
1382 >                  momentumExchange_ += momentumTarget_;
1383                  }
1384                }
1385              }
# Line 1270 | Line 1389 | namespace OpenMD {
1389      }
1390      if (successfulExchange != true) {
1391        sprintf(painCave.errMsg,
1392 <              "RNEMD: exchange NOT performed!\n");
1392 >              "RNEMD::doVSS exchange NOT performed - roots that solve\n"
1393 >              "\tthe constraint equations may not exist or there may be\n"
1394 >              "\tno selected objects in one or both slabs.\n");
1395        painCave.isFatal = 0;
1396        painCave.severity = OPENMD_INFO;
1397        simError();        
# Line 1279 | Line 1400 | namespace OpenMD {
1400    }
1401  
1402    void RNEMD::doRNEMD() {
1403 <
1404 <    switch(rnemdType_) {
1405 <    case rnemdKineticScale :
1406 <    case rnemdKineticScaleVAM :
1286 <    case rnemdKineticScaleAM :
1287 <    case rnemdPxScale :
1288 <    case rnemdPyScale :
1289 <    case rnemdPzScale :
1290 <      doScale();
1291 <      break;
1292 <    case rnemdKineticSwap :
1293 <    case rnemdPx :
1294 <    case rnemdPy :
1295 <    case rnemdPz :
1403 >    if (!doRNEMD_) return;
1404 >    trialCount_++;
1405 >    switch(rnemdMethod_) {
1406 >    case rnemdSwap:
1407        doSwap();
1408        break;
1409 <    case rnemdShiftScaleV :
1410 <    case rnemdShiftScaleVAM :
1300 <      doShiftScale();
1409 >    case rnemdNIVS:
1410 >      doNIVS();
1411        break;
1412 <    case rnemdUnknown :
1412 >    case rnemdVSS:
1413 >      doVSS();
1414 >      break;
1415 >    case rnemdUnkownMethod:
1416      default :
1417        break;
1418      }
1419    }
1420  
1421    void RNEMD::collectData() {
1422 <
1422 >    if (!doRNEMD_) return;
1423      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1424      Mat3x3d hmat = currentSnap_->getHmat();
1425  
1426 +    areaAccumulator_->add(currentSnap_->getXYarea());
1427 +
1428      seleMan_.setSelectionSet(evaluator_.evaluate());
1429  
1430      int selei;
1431      StuntDouble* sd;
1432      int idx;
1433  
1434 +    vector<RealType> binMass(nBins_, 0.0);
1435 +    vector<RealType> binPx(nBins_, 0.0);
1436 +    vector<RealType> binPy(nBins_, 0.0);
1437 +    vector<RealType> binPz(nBins_, 0.0);
1438 +    vector<RealType> binKE(nBins_, 0.0);
1439 +    vector<int> binDOF(nBins_, 0);
1440 +    vector<int> binCount(nBins_, 0);
1441 +
1442      // alternative approach, track all molecules instead of only those
1443      // selected for scaling/swapping:
1444      /*
1445      SimInfo::MoleculeIterator miter;
1446      vector<StuntDouble*>::iterator iiter;
1447      Molecule* mol;
1448 <    StuntDouble* integrableObject;
1448 >    StuntDouble* sd;
1449      for (mol = info_->beginMolecule(miter); mol != NULL;
1450 <         mol = info_->nextMolecule(miter))
1451 <      integrableObject is essentially sd
1452 <        for (integrableObject = mol->beginIntegrableObject(iiter);
1453 <             integrableObject != NULL;
1454 <             integrableObject = mol->nextIntegrableObject(iiter))
1450 >      mol = info_->nextMolecule(miter))
1451 >      sd is essentially sd
1452 >        for (sd = mol->beginIntegrableObject(iiter);
1453 >             sd != NULL;
1454 >             sd = mol->nextIntegrableObject(iiter))
1455      */
1456      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1457           sd = seleMan_.nextSelected(selei)) {
# Line 1341 | Line 1464 | namespace OpenMD {
1464        
1465        if (usePeriodicBoundaryConditions_)
1466          currentSnap_->wrapVector(pos);
1467 <      
1467 >
1468 >
1469        // which bin is this stuntdouble in?
1470        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1471 <      
1472 <      int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
1473 <        rnemdLogWidth_;
1474 <      // no symmetrization allowed due to arbitary rnemdLogWidth_
1475 <      /*
1352 <      if (rnemdLogWidth_ == midBin_ + 1)
1353 <        if (binNo > midBin_)
1354 <          binNo = nBins_ - binNo;
1355 <      */
1471 >      // Shift molecules by half a box to have bins start at 0
1472 >      // The modulo operator is used to wrap the case when we are
1473 >      // beyond the end of the bins back to the beginning.
1474 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1475 >    
1476        RealType mass = sd->getMass();
1357      mHist_[binNo] += mass;
1477        Vector3d vel = sd->getVel();
1359      RealType value;
1360      //RealType xVal, yVal, zVal;
1478  
1479 <      if (outputTemp_) {
1480 <        value = mass * vel.lengthSquare();
1481 <        tempCount_[binNo] += 3;
1482 <        if (sd->isDirectional()) {
1483 <          Vector3d angMom = sd->getJ();
1484 <          Mat3x3d I = sd->getI();
1485 <          if (sd->isLinear()) {
1369 <            int i = sd->linearAxis();
1370 <            int j = (i + 1) % 3;
1371 <            int k = (i + 2) % 3;
1372 <            value += angMom[j] * angMom[j] / I(j, j) +
1373 <              angMom[k] * angMom[k] / I(k, k);
1374 <            tempCount_[binNo] +=2;
1375 <          } else {
1376 <            value += angMom[0] * angMom[0] / I(0, 0) +
1377 <              angMom[1]*angMom[1]/I(1, 1) +
1378 <              angMom[2]*angMom[2]/I(2, 2);
1379 <            tempCount_[binNo] +=3;
1380 <          }
1381 <        }
1382 <        value = value / PhysicalConstants::energyConvert
1383 <          / PhysicalConstants::kb;//may move to getStatus()
1384 <        tempHist_[binNo] += value;
1385 <      }
1386 <      if (outputVx_) {
1387 <        value = mass * vel[0];
1388 <        //vxzCount_[binNo]++;
1389 <        pxzHist_[binNo] += value;
1390 <      }
1391 <      if (outputVy_) {
1392 <        value = mass * vel[1];
1393 <        //vyzCount_[binNo]++;
1394 <        pyzHist_[binNo] += value;
1395 <      }
1479 >      binCount[binNo]++;
1480 >      binMass[binNo] += mass;
1481 >      binPx[binNo] += mass*vel.x();
1482 >      binPy[binNo] += mass*vel.y();
1483 >      binPz[binNo] += mass*vel.z();
1484 >      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1485 >      binDOF[binNo] += 3;
1486  
1487 <      if (output3DTemp_) {
1488 <        value = mass * vel.x() * vel.x();
1489 <        xTempHist_[binNo] += value;
1490 <        value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1491 <          / PhysicalConstants::kb;
1492 <        yTempHist_[binNo] += value;
1493 <        value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1494 <          / PhysicalConstants::kb;
1495 <        zTempHist_[binNo] += value;
1496 <        xyzTempCount_[binNo]++;
1487 >      if (sd->isDirectional()) {
1488 >        Vector3d angMom = sd->getJ();
1489 >        Mat3x3d I = sd->getI();
1490 >        if (sd->isLinear()) {
1491 >          int i = sd->linearAxis();
1492 >          int j = (i + 1) % 3;
1493 >          int k = (i + 2) % 3;
1494 >          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
1495 >                                 angMom[k] * angMom[k] / I(k, k));
1496 >          binDOF[binNo] += 2;
1497 >        } else {
1498 >          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
1499 >                                 angMom[1] * angMom[1] / I(1, 1) +
1500 >                                 angMom[2] * angMom[2] / I(2, 2));
1501 >          binDOF[binNo] += 3;
1502 >        }
1503        }
1504 <      if (outputRotTemp_) {
1505 <        if (sd->isDirectional()) {
1410 <          Vector3d angMom = sd->getJ();
1411 <          Mat3x3d I = sd->getI();
1412 <          if (sd->isLinear()) {
1413 <            int i = sd->linearAxis();
1414 <            int j = (i + 1) % 3;
1415 <            int k = (i + 2) % 3;
1416 <            value = angMom[j] * angMom[j] / I(j, j) +
1417 <              angMom[k] * angMom[k] / I(k, k);
1418 <            rotTempCount_[binNo] +=2;
1419 <          } else {
1420 <            value = angMom[0] * angMom[0] / I(0, 0) +
1421 <              angMom[1] * angMom[1] / I(1, 1) +
1422 <              angMom[2] * angMom[2] / I(2, 2);
1423 <            rotTempCount_[binNo] +=3;
1424 <          }
1425 <        }
1426 <        value = value / PhysicalConstants::energyConvert
1427 <          / PhysicalConstants::kb;//may move to getStatus()
1428 <        rotTempHist_[binNo] += value;
1429 <      }
1504 >    }
1505 >    
1506  
1507 + #ifdef IS_MPI
1508 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
1509 +                              nBins_, MPI::INT, MPI::SUM);
1510 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
1511 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1512 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
1513 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1514 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
1515 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1516 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
1517 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1518 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
1519 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1520 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
1521 +                              nBins_, MPI::INT, MPI::SUM);
1522 + #endif
1523 +
1524 +    Vector3d vel;
1525 +    RealType den;
1526 +    RealType temp;
1527 +    RealType z;
1528 +    for (int i = 0; i < nBins_; i++) {
1529 +      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
1530 +      vel.x() = binPx[i] / binMass[i];
1531 +      vel.y() = binPy[i] / binMass[i];
1532 +      vel.z() = binPz[i] / binMass[i];
1533 +
1534 +      den = binMass[i] * nBins_ * PhysicalConstants::densityConvert
1535 +        / currentSnap_->getVolume() ;
1536 +
1537 +      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
1538 +                               PhysicalConstants::energyConvert);
1539 +
1540 +      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
1541 +        if(outputMask_[j]) {
1542 +          switch(j) {
1543 +          case Z:
1544 +            (data_[j].accumulator[i])->add(z);
1545 +            break;
1546 +          case TEMPERATURE:
1547 +            data_[j].accumulator[i]->add(temp);
1548 +            break;
1549 +          case VELOCITY:
1550 +            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
1551 +            break;
1552 +          case DENSITY:
1553 +            data_[j].accumulator[i]->add(den);
1554 +            break;
1555 +          }
1556 +        }
1557 +      }
1558      }
1559    }
1560  
1561    void RNEMD::getStarted() {
1562 +    if (!doRNEMD_) return;
1563      collectData();
1564 <    /*now can output profile in step 0, but might not be useful;
1437 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1438 <    Stats& stat = currentSnap_->statData;
1439 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1440 <    */
1441 <    //may output a header for the log file here
1442 <    getStatus();
1564 >    writeOutputFile();
1565    }
1566  
1567 <  void RNEMD::getStatus() {
1568 <
1569 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1570 <    Stats& stat = currentSnap_->statData;
1571 <    RealType time = currentSnap_->getTime();
1572 <
1573 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1574 <    //or to be more meaningful, define another item as exchangeSum_ / time
1575 <    int j;
1576 <
1567 >  void RNEMD::parseOutputFileFormat(const std::string& format) {
1568 >    if (!doRNEMD_) return;
1569 >    StringTokenizer tokenizer(format, " ,;|\t\n\r");
1570 >    
1571 >    while(tokenizer.hasMoreTokens()) {
1572 >      std::string token(tokenizer.nextToken());
1573 >      toUpper(token);
1574 >      OutputMapType::iterator i = outputMap_.find(token);
1575 >      if (i != outputMap_.end()) {
1576 >        outputMask_.set(i->second);
1577 >      } else {
1578 >        sprintf( painCave.errMsg,
1579 >                 "RNEMD::parseOutputFileFormat: %s is not a recognized\n"
1580 >                 "\toutputFileFormat keyword.\n", token.c_str() );
1581 >        painCave.isFatal = 0;
1582 >        painCave.severity = OPENMD_ERROR;
1583 >        simError();            
1584 >      }
1585 >    }  
1586 >  }
1587 >  
1588 >  void RNEMD::writeOutputFile() {
1589 >    if (!doRNEMD_) return;
1590 >    
1591   #ifdef IS_MPI
1456
1457    // all processors have the same number of bins, and STL vectors pack their
1458    // arrays, so in theory, this should be safe:
1459
1460    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &mHist_[0],
1461                              rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1462    if (outputTemp_) {
1463      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempHist_[0],
1464                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1465      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempCount_[0],
1466                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1467    }
1468    if (outputVx_) {
1469      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pxzHist_[0],
1470                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1471      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vxzCount_[0],
1472      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1473    }
1474    if (outputVy_) {
1475      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pyzHist_[0],
1476                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1477      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vyzCount_[0],
1478      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1479    }
1480    if (output3DTemp_) {
1481      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xTempHist_[0],
1482                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1483      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &yTempHist_[0],
1484                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1485      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &zTempHist_[0],
1486                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1487      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xyzTempCount_[0],
1488                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1489    }
1490    if (outputRotTemp_) {
1491      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempHist_[0],
1492                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1493      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempCount_[0],
1494                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1495    }
1496
1592      // If we're the root node, should we print out the results
1593      int worldRank = MPI::COMM_WORLD.Get_rank();
1594      if (worldRank == 0) {
1595   #endif
1596 +      rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
1597 +      
1598 +      if( !rnemdFile_ ){        
1599 +        sprintf( painCave.errMsg,
1600 +                 "Could not open \"%s\" for RNEMD output.\n",
1601 +                 rnemdFileName_.c_str());
1602 +        painCave.isFatal = 1;
1603 +        simError();
1604 +      }
1605  
1606 <      if (outputTemp_) {
1607 <        tempLog_ << time;
1608 <        for (j = 0; j < rnemdLogWidth_; j++) {
1609 <          tempLog_ << "\t" << tempHist_[j] / (RealType)tempCount_[j];
1610 <        }
1611 <        tempLog_ << endl;
1606 >      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1607 >
1608 >      RealType time = currentSnap_->getTime();
1609 >      RealType avgArea;
1610 >      areaAccumulator_->getAverage(avgArea);
1611 >      RealType Jz = kineticExchange_ / (2.0 * time * avgArea)
1612 >        / PhysicalConstants::energyConvert;
1613 >      Vector3d JzP = momentumExchange_ / (2.0 * time * avgArea);      
1614 >
1615 >      rnemdFile_ << "#######################################################\n";
1616 >      rnemdFile_ << "# RNEMD {\n";
1617 >
1618 >      map<string, RNEMDMethod>::iterator mi;
1619 >      for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
1620 >        if ( (*mi).second == rnemdMethod_)
1621 >          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
1622        }
1623 <      if (outputVx_) {
1624 <        vxzLog_ << time;
1625 <        for (j = 0; j < rnemdLogWidth_; j++) {
1626 <          vxzLog_ << "\t" << pxzHist_[j] / mHist_[j];
1513 <        }
1514 <        vxzLog_ << endl;
1623 >      map<string, RNEMDFluxType>::iterator fi;
1624 >      for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
1625 >        if ( (*fi).second == rnemdFluxType_)
1626 >          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
1627        }
1628 <      if (outputVy_) {
1629 <        vyzLog_ << time;
1518 <        for (j = 0; j < rnemdLogWidth_; j++) {
1519 <          vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1520 <        }
1521 <        vyzLog_ << endl;
1522 <      }
1628 >      
1629 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
1630  
1631 <      if (output3DTemp_) {
1632 <        RealType temp;
1633 <        xTempLog_ << time;
1634 <        for (j = 0; j < rnemdLogWidth_; j++) {
1635 <          if (outputVx_)
1636 <            xTempHist_[j] -= pxzHist_[j] * pxzHist_[j] / mHist_[j];
1637 <          temp = xTempHist_[j] / (RealType)xyzTempCount_[j]
1638 <            / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1639 <          xTempLog_ << "\t" << temp;
1631 >      rnemdFile_ << "#    objectSelection = \""
1632 >                 << rnemdObjectSelection_ << "\";\n";
1633 >      rnemdFile_ << "#    slabWidth = " << slabWidth_ << ";\n";
1634 >      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << ";\n";
1635 >      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << ";\n";
1636 >      rnemdFile_ << "# }\n";
1637 >      rnemdFile_ << "#######################################################\n";
1638 >      rnemdFile_ << "# RNEMD report:\n";      
1639 >      rnemdFile_ << "#     running time = " << time << " fs\n";
1640 >      rnemdFile_ << "#     target flux:\n";
1641 >      rnemdFile_ << "#         kinetic = "
1642 >                 << kineticFlux_ / PhysicalConstants::energyConvert
1643 >                 << " (kcal/mol/A^2/fs)\n";
1644 >      rnemdFile_ << "#         momentum = " << momentumFluxVector_
1645 >                 << " (amu/A/fs^2)\n";
1646 >      rnemdFile_ << "#     target one-time exchanges:\n";
1647 >      rnemdFile_ << "#         kinetic = "
1648 >                 << kineticTarget_ / PhysicalConstants::energyConvert
1649 >                 << " (kcal/mol)\n";
1650 >      rnemdFile_ << "#         momentum = " << momentumTarget_
1651 >                 << " (amu*A/fs)\n";
1652 >      rnemdFile_ << "#     actual exchange totals:\n";
1653 >      rnemdFile_ << "#         kinetic = "
1654 >                 << kineticExchange_ / PhysicalConstants::energyConvert
1655 >                 << " (kcal/mol)\n";
1656 >      rnemdFile_ << "#         momentum = " << momentumExchange_
1657 >                 << " (amu*A/fs)\n";      
1658 >      rnemdFile_ << "#     actual flux:\n";
1659 >      rnemdFile_ << "#         kinetic = " << Jz
1660 >                 << " (kcal/mol/A^2/fs)\n";
1661 >      rnemdFile_ << "#         momentum = " << JzP
1662 >                 << " (amu/A/fs^2)\n";
1663 >      rnemdFile_ << "#     exchange statistics:\n";
1664 >      rnemdFile_ << "#         attempted = " << trialCount_ << "\n";
1665 >      rnemdFile_ << "#         failed = " << failTrialCount_ << "\n";    
1666 >      if (rnemdMethod_ == rnemdNIVS) {
1667 >        rnemdFile_ << "#         NIVS root-check errors = "
1668 >                   << failRootCount_ << "\n";
1669 >      }
1670 >      rnemdFile_ << "#######################################################\n";
1671 >      
1672 >      
1673 >      
1674 >      //write title
1675 >      rnemdFile_ << "#";
1676 >      for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1677 >        if (outputMask_[i]) {
1678 >          rnemdFile_ << "\t" << data_[i].title <<
1679 >            "(" << data_[i].units << ")";
1680 >          // add some extra tabs for column alignment
1681 >          if (data_[i].dataType == "Vector3d") rnemdFile_ << "\t\t";
1682          }
1534        xTempLog_ << endl;
1535        yTempLog_ << time;
1536        for (j = 0; j < rnemdLogWidth_; j++) {
1537          yTempLog_ << "\t" << yTempHist_[j] / (RealType)xyzTempCount_[j];
1538        }
1539        yTempLog_ << endl;
1540        zTempLog_ << time;
1541        for (j = 0; j < rnemdLogWidth_; j++) {
1542          zTempLog_ << "\t" << zTempHist_[j] / (RealType)xyzTempCount_[j];
1543        }
1544        zTempLog_ << endl;
1683        }
1684 <      if (outputRotTemp_) {
1685 <        rotTempLog_ << time;
1686 <        for (j = 0; j < rnemdLogWidth_; j++) {
1687 <          rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1688 <        }
1689 <        rotTempLog_ << endl;
1690 <      }
1684 >      rnemdFile_ << std::endl;
1685 >      
1686 >      rnemdFile_.precision(8);
1687 >      
1688 >      for (int j = 0; j < nBins_; j++) {        
1689 >        
1690 >        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1691 >          if (outputMask_[i]) {
1692 >            if (data_[i].dataType == "RealType")
1693 >              writeReal(i,j);
1694 >            else if (data_[i].dataType == "Vector3d")
1695 >              writeVector(i,j);
1696 >            else {
1697 >              sprintf( painCave.errMsg,
1698 >                       "RNEMD found an unknown data type for: %s ",
1699 >                       data_[i].title.c_str());
1700 >              painCave.isFatal = 1;
1701 >              simError();
1702 >            }
1703 >          }
1704 >        }
1705 >        rnemdFile_ << std::endl;
1706 >        
1707 >      }        
1708  
1709 +      rnemdFile_ << "#######################################################\n";
1710 +      rnemdFile_ << "# Standard Deviations in those quantities follow:\n";
1711 +      rnemdFile_ << "#######################################################\n";
1712 +
1713 +
1714 +      for (int j = 0; j < nBins_; j++) {        
1715 +        rnemdFile_ << "#";
1716 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1717 +          if (outputMask_[i]) {
1718 +            if (data_[i].dataType == "RealType")
1719 +              writeRealStdDev(i,j);
1720 +            else if (data_[i].dataType == "Vector3d")
1721 +              writeVectorStdDev(i,j);
1722 +            else {
1723 +              sprintf( painCave.errMsg,
1724 +                       "RNEMD found an unknown data type for: %s ",
1725 +                       data_[i].title.c_str());
1726 +              painCave.isFatal = 1;
1727 +              simError();
1728 +            }
1729 +          }
1730 +        }
1731 +        rnemdFile_ << std::endl;
1732 +        
1733 +      }        
1734 +      
1735 +      rnemdFile_.flush();
1736 +      rnemdFile_.close();
1737 +      
1738   #ifdef IS_MPI
1739      }
1740   #endif
1741 <
1742 <    for (j = 0; j < rnemdLogWidth_; j++) {
1743 <      mHist_[j] = 0.0;
1741 >    
1742 >  }
1743 >  
1744 >  void RNEMD::writeReal(int index, unsigned int bin) {
1745 >    if (!doRNEMD_) return;
1746 >    assert(index >=0 && index < ENDINDEX);
1747 >    assert(bin < nBins_);
1748 >    RealType s;
1749 >    
1750 >    data_[index].accumulator[bin]->getAverage(s);
1751 >    
1752 >    if (! isinf(s) && ! isnan(s)) {
1753 >      rnemdFile_ << "\t" << s;
1754 >    } else{
1755 >      sprintf( painCave.errMsg,
1756 >               "RNEMD detected a numerical error writing: %s for bin %d",
1757 >               data_[index].title.c_str(), bin);
1758 >      painCave.isFatal = 1;
1759 >      simError();
1760 >    }    
1761 >  }
1762 >  
1763 >  void RNEMD::writeVector(int index, unsigned int bin) {
1764 >    if (!doRNEMD_) return;
1765 >    assert(index >=0 && index < ENDINDEX);
1766 >    assert(bin < nBins_);
1767 >    Vector3d s;
1768 >    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
1769 >    if (isinf(s[0]) || isnan(s[0]) ||
1770 >        isinf(s[1]) || isnan(s[1]) ||
1771 >        isinf(s[2]) || isnan(s[2]) ) {      
1772 >      sprintf( painCave.errMsg,
1773 >               "RNEMD detected a numerical error writing: %s for bin %d",
1774 >               data_[index].title.c_str(), bin);
1775 >      painCave.isFatal = 1;
1776 >      simError();
1777 >    } else {
1778 >      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1779      }
1780 <    if (outputTemp_)
1562 <      for (j = 0; j < rnemdLogWidth_; j++) {
1563 <        tempCount_[j] = 0;
1564 <        tempHist_[j] = 0.0;
1565 <      }
1566 <    if (outputVx_)
1567 <      for (j = 0; j < rnemdLogWidth_; j++) {
1568 <        //pxzCount_[j] = 0;
1569 <        pxzHist_[j] = 0.0;
1570 <      }
1571 <    if (outputVy_)
1572 <      for (j = 0; j < rnemdLogWidth_; j++) {
1573 <        //pyzCount_[j] = 0;
1574 <        pyzHist_[j] = 0.0;
1575 <      }
1780 >  }  
1781  
1782 <    if (output3DTemp_)
1783 <      for (j = 0; j < rnemdLogWidth_; j++) {
1784 <        xTempHist_[j] = 0.0;
1785 <        yTempHist_[j] = 0.0;
1786 <        zTempHist_[j] = 0.0;
1787 <        xyzTempCount_[j] = 0;
1788 <      }
1789 <    if (outputRotTemp_)
1790 <      for (j = 0; j < rnemdLogWidth_; j++) {
1791 <        rotTempCount_[j] = 0;
1792 <        rotTempHist_[j] = 0.0;
1793 <      }
1782 >  void RNEMD::writeRealStdDev(int index, unsigned int bin) {
1783 >    if (!doRNEMD_) return;
1784 >    assert(index >=0 && index < ENDINDEX);
1785 >    assert(bin < nBins_);
1786 >    RealType s;
1787 >    
1788 >    data_[index].accumulator[bin]->getStdDev(s);
1789 >    
1790 >    if (! isinf(s) && ! isnan(s)) {
1791 >      rnemdFile_ << "\t" << s;
1792 >    } else{
1793 >      sprintf( painCave.errMsg,
1794 >               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1795 >               data_[index].title.c_str(), bin);
1796 >      painCave.isFatal = 1;
1797 >      simError();
1798 >    }    
1799    }
1800 +  
1801 +  void RNEMD::writeVectorStdDev(int index, unsigned int bin) {
1802 +    if (!doRNEMD_) return;
1803 +    assert(index >=0 && index < ENDINDEX);
1804 +    assert(bin < nBins_);
1805 +    Vector3d s;
1806 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getStdDev(s);
1807 +    if (isinf(s[0]) || isnan(s[0]) ||
1808 +        isinf(s[1]) || isnan(s[1]) ||
1809 +        isinf(s[2]) || isnan(s[2]) ) {      
1810 +      sprintf( painCave.errMsg,
1811 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1812 +               data_[index].title.c_str(), bin);
1813 +      painCave.isFatal = 1;
1814 +      simError();
1815 +    } else {
1816 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1817 +    }
1818 +  }  
1819   }
1820  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines