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 1723 by gezelter, Thu May 24 20:59:54 2012 UTC vs.
branches/development/src/rnemd/RNEMD.cpp (file contents), Revision 1812 by gezelter, Fri Nov 16 21:18:42 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  
# 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_["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;
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 +    const string methStr = rnemdParams->getMethod();
96 +    bool hasFluxType = rnemdParams->haveFluxType();
97 +
98 +    string fluxStr;
99 +    if (hasFluxType) {
100 +      fluxStr = rnemdParams->getFluxType();
101 +    } else {
102 +      sprintf(painCave.errMsg,
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 +    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, 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 fluxType,\n"
143 +              "\t\t%s\n"
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 +    }
150 +
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 rnemdPy:
161 +      case rnemdPz:
162 +        hasCorrectFlux = hasMomentumFlux;
163 +        break;
164 +      default :
165 +        methodFluxMismatch = true;
166 +        break;
167 +      }
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 +      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 +    default:
217 +      break;
218 +    }
219 +
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 +      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      // do some sanity checking
278  
279      int selectionCount = seleMan_.getSelectionCount();
280 +
281      int nIntegrable = info->getNGlobalIntegrableObjects();
282  
283      if (selectionCount > nIntegrable) {
284        sprintf(painCave.errMsg,
285 <              "RNEMD: The current RNEMD_objectSelection,\n"
285 >              "RNEMD: The current objectSelection,\n"
286                "\t\t%s\n"
287                "\thas resulted in %d selected objects.  However,\n"
288                "\tthe total number of integrable objects in the system\n"
# Line 109 | Line 295 | namespace OpenMD {
295        painCave.severity = OPENMD_WARNING;
296        simError();
297      }
112    
113    const string st = simParams->getRNEMD_exchangeType();
298  
299 <    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 <    }
299 >    areaAccumulator_ = new Accumulator();
300  
301 < #ifdef IS_MPI
160 <    if (worldRank == 0) {
161 < #endif
301 >    nBins_ = rnemdParams->getOutputBins();
302  
303 <      //may have rnemdWriter separately
304 <      string rnemdFileName;
303 >    data_.resize(RNEMD::ENDINDEX);
304 >    OutputData z;
305 >    z.units =  "Angstroms";
306 >    z.title =  "Z";
307 >    z.dataType = "RealType";
308 >    z.accumulator.reserve(nBins_);
309 >    for (unsigned int i = 0; i < nBins_; i++)
310 >      z.accumulator.push_back( new Accumulator() );
311 >    data_[Z] = z;
312 >    outputMap_["Z"] =  Z;
313  
314 <      if (outputTemp_) {
315 <        rnemdFileName = "temperature.log";
316 <        tempLog_.open(rnemdFileName.c_str());
317 <      }
318 <      if (outputVx_) {
319 <        rnemdFileName = "velocityX.log";
320 <        vxzLog_.open(rnemdFileName.c_str());
321 <      }
322 <      if (outputVy_) {
175 <        rnemdFileName = "velocityY.log";
176 <        vyzLog_.open(rnemdFileName.c_str());
177 <      }
314 >    OutputData temperature;
315 >    temperature.units =  "K";
316 >    temperature.title =  "Temperature";
317 >    temperature.dataType = "RealType";
318 >    temperature.accumulator.reserve(nBins_);
319 >    for (unsigned int i = 0; i < nBins_; i++)
320 >      temperature.accumulator.push_back( new Accumulator() );
321 >    data_[TEMPERATURE] = temperature;
322 >    outputMap_["TEMPERATURE"] =  TEMPERATURE;
323  
324 <      if (output3DTemp_) {
325 <        rnemdFileName = "temperatureX.log";
326 <        xTempLog_.open(rnemdFileName.c_str());
327 <        rnemdFileName = "temperatureY.log";
328 <        yTempLog_.open(rnemdFileName.c_str());
329 <        rnemdFileName = "temperatureZ.log";
330 <        zTempLog_.open(rnemdFileName.c_str());
331 <      }
332 <      if (outputRotTemp_) {
188 <        rnemdFileName = "temperatureR.log";
189 <        rotTempLog_.open(rnemdFileName.c_str());
190 <      }
324 >    OutputData velocity;
325 >    velocity.units = "angstroms/fs";
326 >    velocity.title =  "Velocity";  
327 >    velocity.dataType = "Vector3d";
328 >    velocity.accumulator.reserve(nBins_);
329 >    for (unsigned int i = 0; i < nBins_; i++)
330 >      velocity.accumulator.push_back( new VectorAccumulator() );
331 >    data_[VELOCITY] = velocity;
332 >    outputMap_["VELOCITY"] = VELOCITY;
333  
334 < #ifdef IS_MPI
335 <    }
336 < #endif
334 >    OutputData density;
335 >    density.units =  "g cm^-3";
336 >    density.title =  "Density";
337 >    density.dataType = "RealType";
338 >    density.accumulator.reserve(nBins_);
339 >    for (unsigned int i = 0; i < nBins_; i++)
340 >      density.accumulator.push_back( new Accumulator() );
341 >    data_[DENSITY] = density;
342 >    outputMap_["DENSITY"] =  DENSITY;
343  
344 <    set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
345 <    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 <      }
344 >    if (hasOutputFields) {
345 >      parseOutputFileFormat(rnemdParams->getOutputFields());
346      } else {
347 <      zShift_ = 0.0;
348 <    }
349 <    //cerr << "I shift slabs by " << zShift_ << " Lz\n";
350 <    //shift slabs by half slab width, maybe useful in heterogeneous systems
351 <    //set to 0.0 if not using it; N/A in status output yet
352 <    if (simParams->haveRNEMD_logWidth()) {
353 <      set_RNEMD_logWidth(simParams->getRNEMD_logWidth());
354 <      /*arbitary rnemdLogWidth_, no checking;
355 <      if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
356 <        cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
357 <        cerr << "Automaically set back to default.\n";
358 <        rnemdLogWidth_ = nBins_;
359 <      }*/
360 <    } else {
361 <      set_RNEMD_logWidth(nBins_);
347 >      outputMask_.set(Z);
348 >      switch (rnemdFluxType_) {
349 >      case rnemdKE:
350 >      case rnemdRotKE:
351 >      case rnemdFullKE:
352 >        outputMask_.set(TEMPERATURE);
353 >        break;
354 >      case rnemdPx:
355 >      case rnemdPy:
356 >        outputMask_.set(VELOCITY);
357 >        break;
358 >      case rnemdPz:        
359 >      case rnemdPvector:
360 >        outputMask_.set(VELOCITY);
361 >        outputMask_.set(DENSITY);
362 >        break;
363 >      case rnemdKePx:
364 >      case rnemdKePy:
365 >        outputMask_.set(TEMPERATURE);
366 >        outputMask_.set(VELOCITY);
367 >        break;
368 >      case rnemdKePvector:
369 >        outputMask_.set(TEMPERATURE);
370 >        outputMask_.set(VELOCITY);
371 >        outputMask_.set(DENSITY);        
372 >        break;
373 >      default:
374 >        break;
375 >      }
376      }
377 <    tempHist_.resize(rnemdLogWidth_, 0.0);
378 <    tempCount_.resize(rnemdLogWidth_, 0);
379 <    pxzHist_.resize(rnemdLogWidth_, 0.0);
380 <    //vxzCount_.resize(rnemdLogWidth_, 0);
381 <    pyzHist_.resize(rnemdLogWidth_, 0.0);
382 <    //vyzCount_.resize(rnemdLogWidth_, 0);
377 >      
378 >    if (hasOutputFileName) {
379 >      rnemdFileName_ = rnemdParams->getOutputFileName();
380 >    } else {
381 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
382 >    }          
383  
384 <    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);
384 >    exchangeTime_ = rnemdParams->getExchangeTime();
385  
386 <    set_RNEMD_exchange_total(0.0);
387 <    if (simParams->haveRNEMD_targetFlux()) {
388 <      set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
389 <    } else {
390 <      set_RNEMD_target_flux(0.0);
391 <    }
392 <    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_;
386 >    Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
387 >    Mat3x3d hmat = currentSnap_->getHmat();
388 >  
389 >    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
390 >    // Lx, Ly = box dimensions in x & y
391 >    // dt = exchange time interval
392 >    // flux = target flux
393  
394 < #ifndef IS_MPI
395 <    if (simParams->haveSeed()) {
396 <      seedValue = simParams->getSeed();
397 <      randNumGen_ = new SeqRandNumGen(seedValue);
398 <    }else {
399 <      randNumGen_ = new SeqRandNumGen();
400 <    }    
401 < #else
402 <    if (simParams->haveSeed()) {
403 <      seedValue = simParams->getSeed();
404 <      randNumGen_ = new ParallelRandNumGen(seedValue);
405 <    }else {
406 <      randNumGen_ = new ParallelRandNumGen();
283 <    }    
284 < #endif
285 <  }
394 >    RealType area = currentSnap_->getXYarea();
395 >    kineticTarget_ = 2.0 * kineticFlux_ * exchangeTime_ * area;
396 >    momentumTarget_ = 2.0 * momentumFluxVector_ * exchangeTime_ * area;
397 >
398 >    // total exchange sums are zeroed out at the beginning:
399 >
400 >    kineticExchange_ = 0.0;
401 >    momentumExchange_ = V3Zero;
402 >
403 >    if (hasSlabWidth)
404 >      slabWidth_ = rnemdParams->getSlabWidth();
405 >    else
406 >      slabWidth_ = hmat(2,2) / 10.0;
407    
408 <  RNEMD::~RNEMD() {
409 <    delete randNumGen_;
408 >    if (hasSlabACenter)
409 >      slabACenter_ = rnemdParams->getSlabACenter();
410 >    else
411 >      slabACenter_ = 0.0;
412      
413 +    if (hasSlabBCenter)
414 +      slabBCenter_ = rnemdParams->getSlabBCenter();
415 +    else
416 +      slabBCenter_ = hmat(2,2) / 2.0;
417 +    
418 +  }
419 +  
420 +  RNEMD::~RNEMD() {
421 +    if (!doRNEMD_) return;
422   #ifdef IS_MPI
423      if (worldRank == 0) {
424   #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();
425  
426 <      if (outputTemp_) tempLog_.close();
302 <      if (outputVx_)   vxzLog_.close();
303 <      if (outputVy_)   vyzLog_.close();
426 >      writeOutputFile();
427  
428 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
429 <          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 <
428 >      rnemdFile_.close();
429 >      
430   #ifdef IS_MPI
431      }
432   #endif
433    }
434 +  
435 +  bool RNEMD::inSlabA(Vector3d pos) {
436 +    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
437 +  }
438 +  bool RNEMD::inSlabB(Vector3d pos) {
439 +    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
440 +  }
441  
442    void RNEMD::doSwap() {
443 <
443 >    if (!doRNEMD_) return;
444      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
445      Mat3x3d hmat = currentSnap_->getHmat();
446  
# Line 353 | Line 469 | namespace OpenMD {
469  
470        if (usePeriodicBoundaryConditions_)
471          currentSnap_->wrapVector(pos);
472 <
473 <      // 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 <
472 >      bool inA = inSlabA(pos);
473 >      bool inB = inSlabB(pos);
474  
475 <      // if we're in bin 0 or the middleBin
364 <      if (binNo == 0 || binNo == midBin_) {
475 >      if (inA || inB) {
476          
477          RealType mass = sd->getMass();
478          Vector3d vel = sd->getVel();
479          RealType value;
480 <
481 <        switch(rnemdType_) {
482 <        case rnemdKineticSwap :
480 >        
481 >        switch(rnemdFluxType_) {
482 >        case rnemdKE :
483            
484            value = mass * vel.lengthSquare();
485            
# Line 388 | Line 499 | namespace OpenMD {
499                  + angMom[2]*angMom[2]/I(2, 2);
500              }
501            } //angular momenta exchange enabled
391          //energyConvert temporarily disabled
392          //make exchangeSum_ comparable between swap & scale
393          //value = value * 0.5 / PhysicalConstants::energyConvert;
502            value *= 0.5;
503            break;
504          case rnemdPx :
# Line 406 | Line 514 | namespace OpenMD {
514            break;
515          }
516          
517 <        if (binNo == 0) {
517 >        if (inA == 0) {
518            if (!min_found) {
519              min_val = value;
520              min_sd = sd;
# Line 417 | Line 525 | namespace OpenMD {
525                min_sd = sd;
526              }
527            }
528 <        } else { //midBin_
528 >        } else {
529            if (!max_found) {
530              max_val = value;
531              max_sd = sd;
# Line 431 | Line 539 | namespace OpenMD {
539          }
540        }
541      }
542 <
542 >    
543   #ifdef IS_MPI
544      int nProc, worldRank;
545 <
545 >    
546      nProc = MPI::COMM_WORLD.Get_size();
547      worldRank = MPI::COMM_WORLD.Get_rank();
548  
# Line 454 | Line 562 | namespace OpenMD {
562          RealType val;
563          int rank;
564        } max_vals, min_vals;
565 <    
565 >      
566        if (my_min_found) {
567          min_vals.val = min_val;
568        } else {
# Line 492 | Line 600 | namespace OpenMD {
600            Vector3d max_vel = max_sd->getVel();
601            RealType temp_vel;
602            
603 <          switch(rnemdType_) {
604 <          case rnemdKineticSwap :
603 >          switch(rnemdFluxType_) {
604 >          case rnemdKE :
605              min_sd->setVel(max_vel);
606              max_sd->setVel(min_vel);
607              if (min_sd->isDirectional() && max_sd->isDirectional()) {
# Line 544 | Line 652 | namespace OpenMD {
652                                     min_vel.getArrayPointer(), 3, MPI::REALTYPE,
653                                     min_vals.rank, 0, status);
654            
655 <          switch(rnemdType_) {
656 <          case rnemdKineticSwap :
655 >          switch(rnemdFluxType_) {
656 >          case rnemdKE :
657              max_sd->setVel(min_vel);
658              //angular momenta exchange enabled
659              if (max_sd->isDirectional()) {
# Line 590 | Line 698 | namespace OpenMD {
698                                     max_vel.getArrayPointer(), 3, MPI::REALTYPE,
699                                     max_vals.rank, 0, status);
700            
701 <          switch(rnemdType_) {
702 <          case rnemdKineticSwap :
701 >          switch(rnemdFluxType_) {
702 >          case rnemdKE :
703              min_sd->setVel(max_vel);
704              //angular momenta exchange enabled
705              if (min_sd->isDirectional()) {
# Line 625 | Line 733 | namespace OpenMD {
733            }
734          }
735   #endif
736 <        exchangeSum_ += max_val - min_val;
736 >        
737 >        switch(rnemdFluxType_) {
738 >        case rnemdKE:
739 >          kineticExchange_ += max_val - min_val;
740 >          break;
741 >        case rnemdPx:
742 >          momentumExchange_.x() += max_val - min_val;
743 >          break;
744 >        case rnemdPy:
745 >          momentumExchange_.y() += max_val - min_val;
746 >          break;
747 >        case rnemdPz:
748 >          momentumExchange_.z() += max_val - min_val;
749 >          break;
750 >        default:
751 >          break;
752 >        }
753        } else {        
754          sprintf(painCave.errMsg,
755 <                "RNEMD: exchange NOT performed because min_val > max_val\n");
755 >                "RNEMD::doSwap exchange NOT performed because min_val > max_val\n");
756          painCave.isFatal = 0;
757          painCave.severity = OPENMD_INFO;
758          simError();        
# Line 636 | Line 760 | namespace OpenMD {
760        }
761      } else {
762        sprintf(painCave.errMsg,
763 <              "RNEMD: exchange NOT performed because selected object\n"
764 <              "\tnot present in at least one of the two slabs.\n");
763 >              "RNEMD::doSwap exchange NOT performed because selected object\n"
764 >              "\twas not present in at least one of the two slabs.\n");
765        painCave.isFatal = 0;
766        painCave.severity = OPENMD_INFO;
767        simError();        
768        failTrialCount_++;
769 <    }
646 <    
769 >    }    
770    }
771    
772 <  void RNEMD::doScale() {
773 <
772 >  void RNEMD::doNIVS() {
773 >    if (!doRNEMD_) return;
774      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
775      Mat3x3d hmat = currentSnap_->getHmat();
776  
# Line 687 | Line 810 | namespace OpenMD {
810          currentSnap_->wrapVector(pos);
811  
812        // which bin is this stuntdouble in?
813 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
813 >      bool inA = inSlabA(pos);
814 >      bool inB = inSlabB(pos);
815  
816 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
817 <
694 <      // if we're in bin 0 or the middleBin
695 <      if (binNo == 0 || binNo == midBin_) {
696 <        
816 >      if (inA || inB) {
817 >              
818          RealType mass = sd->getMass();
819          Vector3d vel = sd->getVel();
820        
821 <        if (binNo == 0) {
821 >        if (inA) {
822            hotBin.push_back(sd);
823            Phx += mass * vel.x();
824            Phy += mass * vel.y();
# Line 705 | Line 826 | namespace OpenMD {
826            Khx += mass * vel.x() * vel.x();
827            Khy += mass * vel.y() * vel.y();
828            Khz += mass * vel.z() * vel.z();
708          //if (rnemdType_ == rnemdKineticScaleVAM) {
829            if (sd->isDirectional()) {
830              Vector3d angMom = sd->getJ();
831              Mat3x3d I = sd->getI();
# Line 721 | Line 841 | namespace OpenMD {
841                  + angMom[2]*angMom[2]/I(2, 2);
842              }
843            }
844 <          //}
725 <        } else { //midBin_
844 >        } else {
845            coldBin.push_back(sd);
846            Pcx += mass * vel.x();
847            Pcy += mass * vel.y();
# Line 730 | Line 849 | namespace OpenMD {
849            Kcx += mass * vel.x() * vel.x();
850            Kcy += mass * vel.y() * vel.y();
851            Kcz += mass * vel.z() * vel.z();
733          //if (rnemdType_ == rnemdKineticScaleVAM) {
852            if (sd->isDirectional()) {
853              Vector3d angMom = sd->getJ();
854              Mat3x3d I = sd->getI();
# Line 746 | Line 864 | namespace OpenMD {
864                  + angMom[2]*angMom[2]/I(2, 2);
865              }
866            }
749          //}
867          }
868        }
869      }
# Line 760 | Line 877 | namespace OpenMD {
877      Kcz *= 0.5;
878      Kcw *= 0.5;
879  
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
880   #ifdef IS_MPI
881      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
882      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
# Line 791 | Line 902 | namespace OpenMD {
902      RealType pz = Pcz / Phz;
903      RealType c, x, y, z;
904      bool successfulScale = false;
905 <    if ((rnemdType_ == rnemdKineticScaleVAM) ||
906 <        (rnemdType_ == rnemdKineticScaleAM)) {
905 >    if ((rnemdFluxType_ == rnemdFullKE) ||
906 >        (rnemdFluxType_ == rnemdRotKE)) {
907        //may need sanity check Khw & Kcw > 0
908  
909 <      if (rnemdType_ == rnemdKineticScaleVAM) {
910 <        c = 1.0 - targetFlux_ / (Kcx + Kcy + Kcz + Kcw);
909 >      if (rnemdFluxType_ == rnemdFullKE) {
910 >        c = 1.0 - kineticTarget_ / (Kcx + Kcy + Kcz + Kcw);
911        } else {
912 <        c = 1.0 - targetFlux_ / Kcw;
912 >        c = 1.0 - kineticTarget_ / Kcw;
913        }
914  
915        if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
916          c = sqrt(c);
917 <        std::cerr << "cold slab scaling coefficient: " << c << endl;
807 <        //now convert to hotBin coefficient
917 >
918          RealType w = 0.0;
919 <        if (rnemdType_ ==  rnemdKineticScaleVAM) {
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);
# Line 820 | Line 930 | namespace OpenMD {
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 + (targetFlux_ + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
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 + targetFlux_ / Khw;
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 (rnemdType_ == rnemdKineticScaleVAM) {
945 >            if (rnemdFluxType_ == rnemdFullKE) {
946                vel = (*sdi)->getVel() * c;
836              //vel.x() *= c;
837              //vel.y() *= c;
838              //vel.z() *= c;
947                (*sdi)->setVel(vel);
948              }
949              if ((*sdi)->isDirectional()) {
950                Vector3d angMom = (*sdi)->getJ() * c;
843              //angMom[0] *= c;
844              //angMom[1] *= c;
845              //angMom[2] *= c;
951                (*sdi)->setJ(angMom);
952              }
953            }
954            w = sqrt(w);
850          std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
851                    << "\twh= " << w << endl;
955            for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
956 <            if (rnemdType_ == rnemdKineticScaleVAM) {
956 >            if (rnemdFluxType_ == rnemdFullKE) {
957                vel = (*sdi)->getVel();
958                vel.x() *= x;
959                vel.y() *= y;
# Line 859 | Line 962 | namespace OpenMD {
962              }
963              if ((*sdi)->isDirectional()) {
964                Vector3d angMom = (*sdi)->getJ() * w;
862              //angMom[0] *= w;
863              //angMom[1] *= w;
864              //angMom[2] *= w;
965                (*sdi)->setJ(angMom);
966              }
967            }
968            successfulScale = true;
969 <          exchangeSum_ += targetFlux_;
969 >          kineticExchange_ += kineticTarget_;
970          }
971        }
972      } else {
973        RealType a000, a110, c0, a001, a111, b01, b11, c1;
974 <      switch(rnemdType_) {
975 <      case rnemdKineticScale :
974 >      switch(rnemdFluxType_) {
975 >      case rnemdKE :
976          /* used hotBin coeff's & only scale x & y dimensions
977             RealType px = Phx / Pcx;
978             RealType py = Phy / Pcy;
979             a110 = Khy;
980 <           c0 = - Khx - Khy - targetFlux_;
980 >           c0 = - Khx - Khy - kineticTarget_;
981             a000 = Khx;
982             a111 = Kcy * py * py;
983             b11 = -2.0 * Kcy * py * (1.0 + py);
984 <           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
984 >           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + kineticTarget_;
985             b01 = -2.0 * Kcx * px * (1.0 + px);
986             a001 = Kcx * px * px;
987          */
988          //scale all three dimensions, let c_x = c_y
989          a000 = Kcx + Kcy;
990          a110 = Kcz;
991 <        c0 = targetFlux_ - Kcx - Kcy - Kcz;
991 >        c0 = kineticTarget_ - Kcx - Kcy - Kcz;
992          a001 = Khx * px * px + Khy * py * py;
993          a111 = Khz * pz * pz;
994          b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
995          b11 = -2.0 * Khz * pz * (1.0 + pz);
996          c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
997 <          + Khz * pz * (2.0 + pz) - targetFlux_;
997 >          + Khz * pz * (2.0 + pz) - kineticTarget_;
998          break;
999 <      case rnemdPxScale :
1000 <        c = 1 - targetFlux_ / Pcx;
999 >      case rnemdPx :
1000 >        c = 1 - momentumTarget_.x() / Pcx;
1001          a000 = Kcy;
1002          a110 = Kcz;
1003          c0 = Kcx * c * c - Kcx - Kcy - Kcz;
# Line 908 | Line 1008 | namespace OpenMD {
1008          c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
1009            + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
1010          break;
1011 <      case rnemdPyScale :
1012 <        c = 1 - targetFlux_ / Pcy;
1011 >      case rnemdPy :
1012 >        c = 1 - momentumTarget_.y() / Pcy;
1013          a000 = Kcx;
1014          a110 = Kcz;
1015          c0 = Kcy * c * c - Kcx - Kcy - Kcz;
# Line 920 | Line 1020 | namespace OpenMD {
1020          c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
1021            + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
1022          break;
1023 <      case rnemdPzScale ://we don't really do this, do we?
1024 <        c = 1 - targetFlux_ / Pcz;
1023 >      case rnemdPz ://we don't really do this, do we?
1024 >        c = 1 - momentumTarget_.z() / Pcz;
1025          a000 = Kcx;
1026          a110 = Kcy;
1027          c0 = Kcz * c * c - Kcx - Kcy - Kcz;
# Line 1006 | Line 1106 | namespace OpenMD {
1106          for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1107            r1 = (*rpi).first;
1108            r2 = (*rpi).second;
1109 <          switch(rnemdType_) {
1110 <          case rnemdKineticScale :
1109 >          switch(rnemdFluxType_) {
1110 >          case rnemdKE :
1111              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1112                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1113                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1114              break;
1115 <          case rnemdPxScale :
1115 >          case rnemdPx :
1116              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1117                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1118              break;
1119 <          case rnemdPyScale :
1119 >          case rnemdPy :
1120              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1121                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1122              break;
1123 <          case rnemdPzScale :
1123 >          case rnemdPz :
1124              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1125                + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1126            default :
# Line 1034 | Line 1134 | namespace OpenMD {
1134   #ifdef IS_MPI
1135          if (worldRank == 0) {
1136   #endif
1137 <          sprintf(painCave.errMsg,
1138 <                  "RNEMD: roots r1= %lf\tr2 = %lf\n",
1139 <                  bestPair.first, bestPair.second);
1140 <          painCave.isFatal = 0;
1141 <          painCave.severity = OPENMD_INFO;
1142 <          simError();
1137 >          // sprintf(painCave.errMsg,
1138 >          //         "RNEMD: roots r1= %lf\tr2 = %lf\n",
1139 >          //         bestPair.first, bestPair.second);
1140 >          // painCave.isFatal = 0;
1141 >          // painCave.severity = OPENMD_INFO;
1142 >          // simError();
1143   #ifdef IS_MPI
1144          }
1145   #endif
1146          
1147 <        switch(rnemdType_) {
1148 <        case rnemdKineticScale :
1147 >        switch(rnemdFluxType_) {
1148 >        case rnemdKE :
1149            x = bestPair.first;
1150            y = bestPair.first;
1151            z = bestPair.second;
1152            break;
1153 <        case rnemdPxScale :
1153 >        case rnemdPx :
1154            x = c;
1155            y = bestPair.first;
1156            z = bestPair.second;
1157            break;
1158 <        case rnemdPyScale :
1158 >        case rnemdPy :
1159            x = bestPair.first;
1160            y = c;
1161            z = bestPair.second;
1162            break;
1163 <        case rnemdPzScale :
1163 >        case rnemdPz :
1164            x = bestPair.first;
1165            y = bestPair.second;
1166            z = c;
# Line 1089 | Line 1189 | namespace OpenMD {
1189            (*sdi)->setVel(vel);
1190          }
1191          successfulScale = true;
1192 <        exchangeSum_ += targetFlux_;
1192 >        switch(rnemdFluxType_) {
1193 >        case rnemdKE :
1194 >          kineticExchange_ += kineticTarget_;
1195 >          break;
1196 >        case rnemdPx :
1197 >        case rnemdPy :
1198 >        case rnemdPz :
1199 >          momentumExchange_ += momentumTarget_;
1200 >          break;          
1201 >        default :
1202 >          break;
1203 >        }      
1204        }
1205      }
1206      if (successfulScale != true) {
1207        sprintf(painCave.errMsg,
1208 <              "RNEMD: exchange NOT performed!\n");
1208 >              "RNEMD::doNIVS exchange NOT performed - roots that solve\n"
1209 >              "\tthe constraint equations may not exist or there may be\n"
1210 >              "\tno selected objects in one or both slabs.\n");
1211        painCave.isFatal = 0;
1212        painCave.severity = OPENMD_INFO;
1213        simError();        
# Line 1102 | Line 1215 | namespace OpenMD {
1215      }
1216    }
1217  
1218 <  void RNEMD::doShiftScale() {
1219 <
1218 >  void RNEMD::doVSS() {
1219 >    if (!doRNEMD_) return;
1220      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1221 +    RealType time = currentSnap_->getTime();    
1222      Mat3x3d hmat = currentSnap_->getHmat();
1223  
1224      seleMan_.setSelectionSet(evaluator_.evaluate());
# Line 1121 | Line 1235 | namespace OpenMD {
1235      Vector3d Pc(V3Zero);
1236      RealType Mc = 0.0;
1237      RealType Kc = 0.0;
1238 +    
1239  
1240      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1241           sd = seleMan_.nextSelected(selei)) {
# Line 1135 | Line 1250 | namespace OpenMD {
1250          currentSnap_->wrapVector(pos);
1251  
1252        // which bin is this stuntdouble in?
1253 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1254 <
1255 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
1256 <
1142 <      // if we're in bin 0 or the middleBin
1143 <      if (binNo == 0 || binNo == midBin_) {
1253 >      bool inA = inSlabA(pos);
1254 >      bool inB = inSlabB(pos);
1255 >      
1256 >      if (inA || inB) {
1257          
1258          RealType mass = sd->getMass();
1259          Vector3d vel = sd->getVel();
1260        
1261 <        if (binNo == 0) {
1261 >        if (inA) {
1262            hotBin.push_back(sd);
1150          //std::cerr << "before, velocity = " << vel << endl;
1263            Ph += mass * vel;
1152          //std::cerr << "after, velocity = " << vel << endl;
1264            Mh += mass;
1265            Kh += mass * vel.lengthSquare();
1266 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1266 >          if (rnemdFluxType_ == rnemdFullKE) {
1267              if (sd->isDirectional()) {
1268                Vector3d angMom = sd->getJ();
1269                Mat3x3d I = sd->getI();
# Line 1174 | Line 1285 | namespace OpenMD {
1285            Pc += mass * vel;
1286            Mc += mass;
1287            Kc += mass * vel.lengthSquare();
1288 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1288 >          if (rnemdFluxType_ == rnemdFullKE) {
1289              if (sd->isDirectional()) {
1290                Vector3d angMom = sd->getJ();
1291                Mat3x3d I = sd->getI();
# Line 1197 | Line 1308 | namespace OpenMD {
1308      
1309      Kh *= 0.5;
1310      Kc *= 0.5;
1311 <
1201 <    std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1202 <              << "\tKc= " << Kc << endl;
1203 <    std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1204 <
1311 >    
1312   #ifdef IS_MPI
1313      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1314      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
# Line 1214 | Line 1321 | namespace OpenMD {
1321      bool successfulExchange = false;
1322      if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1323        Vector3d vc = Pc / Mc;
1324 <      Vector3d ac = njzp_ / Mc + vc;
1325 <      RealType cNumerator = Kc - targetJzKE_ - 0.5 * Mc * ac.lengthSquare();
1324 >      Vector3d ac = -momentumTarget_ / Mc + vc;
1325 >      Vector3d acrec = -momentumTarget_ / Mc;
1326 >      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1327        if (cNumerator > 0.0) {
1328          RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1329          if (cDenominator > 0.0) {
1330            RealType c = sqrt(cNumerator / cDenominator);
1331            if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1332              Vector3d vh = Ph / Mh;
1333 <            Vector3d ah = jzp_ / Mh + vh;
1334 <            RealType hNumerator = Kh + targetJzKE_
1333 >            Vector3d ah = momentumTarget_ / Mh + vh;
1334 >            Vector3d ahrec = momentumTarget_ / Mh;
1335 >            RealType hNumerator = Kh + kineticTarget_
1336                - 0.5 * Mh * ah.lengthSquare();
1337              if (hNumerator > 0.0) {
1338                RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1339                if (hDenominator > 0.0) {
1340                  RealType h = sqrt(hNumerator / hDenominator);
1341                  if ((h > 0.9) && (h < 1.1)) {
1342 <                  std::cerr << "cold slab scaling coefficient: " << c << "\n";
1234 <                  std::cerr << "hot slab scaling coefficient: " << h << "\n";
1342 >
1343                    vector<StuntDouble*>::iterator sdi;
1344                    Vector3d vel;
1345                    for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1346                      //vel = (*sdi)->getVel();
1347                      vel = ((*sdi)->getVel() - vc) * c + ac;
1348                      (*sdi)->setVel(vel);
1349 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1349 >                    if (rnemdFluxType_ == rnemdFullKE) {
1350                        if ((*sdi)->isDirectional()) {
1351                          Vector3d angMom = (*sdi)->getJ() * c;
1352                          (*sdi)->setJ(angMom);
# Line 1249 | Line 1357 | namespace OpenMD {
1357                      //vel = (*sdi)->getVel();
1358                      vel = ((*sdi)->getVel() - vh) * h + ah;
1359                      (*sdi)->setVel(vel);
1360 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1360 >                    if (rnemdFluxType_ == rnemdFullKE) {
1361                        if ((*sdi)->isDirectional()) {
1362                          Vector3d angMom = (*sdi)->getJ() * h;
1363                          (*sdi)->setJ(angMom);
# Line 1257 | Line 1365 | namespace OpenMD {
1365                      }
1366                    }
1367                    successfulExchange = true;
1368 <                  exchangeSum_ += targetFlux_;
1369 <                  // 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.
1368 >                  kineticExchange_ += kineticTarget_;
1369 >                  momentumExchange_ += momentumTarget_;
1370                  }
1371                }
1372              }
# Line 1270 | Line 1376 | namespace OpenMD {
1376      }
1377      if (successfulExchange != true) {
1378        sprintf(painCave.errMsg,
1379 <              "RNEMD: exchange NOT performed!\n");
1379 >              "RNEMD::doVSS exchange NOT performed - roots that solve\n"
1380 >              "\tthe constraint equations may not exist or there may be\n"
1381 >              "\tno selected objects in one or both slabs.\n");
1382        painCave.isFatal = 0;
1383        painCave.severity = OPENMD_INFO;
1384        simError();        
# Line 1279 | Line 1387 | namespace OpenMD {
1387    }
1388  
1389    void RNEMD::doRNEMD() {
1390 <
1391 <    switch(rnemdType_) {
1392 <    case rnemdKineticScale :
1393 <    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 :
1390 >    if (!doRNEMD_) return;
1391 >    trialCount_++;
1392 >    switch(rnemdMethod_) {
1393 >    case rnemdSwap:
1394        doSwap();
1395        break;
1396 <    case rnemdShiftScaleV :
1397 <    case rnemdShiftScaleVAM :
1300 <      doShiftScale();
1396 >    case rnemdNIVS:
1397 >      doNIVS();
1398        break;
1399 <    case rnemdUnknown :
1399 >    case rnemdVSS:
1400 >      doVSS();
1401 >      break;
1402 >    case rnemdUnkownMethod:
1403      default :
1404        break;
1405      }
1406    }
1407  
1408    void RNEMD::collectData() {
1409 <
1409 >    if (!doRNEMD_) return;
1410      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1411      Mat3x3d hmat = currentSnap_->getHmat();
1412  
1413 +    areaAccumulator_->add(currentSnap_->getXYarea());
1414 +
1415      seleMan_.setSelectionSet(evaluator_.evaluate());
1416  
1417 <    int selei;
1417 >    int selei(0);
1418      StuntDouble* sd;
1419      int idx;
1420  
1421 +    vector<RealType> binMass(nBins_, 0.0);
1422 +    vector<RealType> binPx(nBins_, 0.0);
1423 +    vector<RealType> binPy(nBins_, 0.0);
1424 +    vector<RealType> binPz(nBins_, 0.0);
1425 +    vector<RealType> binKE(nBins_, 0.0);
1426 +    vector<int> binDOF(nBins_, 0);
1427 +    vector<int> binCount(nBins_, 0);
1428 +
1429      // alternative approach, track all molecules instead of only those
1430      // selected for scaling/swapping:
1431      /*
1432      SimInfo::MoleculeIterator miter;
1433      vector<StuntDouble*>::iterator iiter;
1434      Molecule* mol;
1435 <    StuntDouble* integrableObject;
1435 >    StuntDouble* sd;
1436      for (mol = info_->beginMolecule(miter); mol != NULL;
1437 <         mol = info_->nextMolecule(miter))
1438 <      integrableObject is essentially sd
1439 <        for (integrableObject = mol->beginIntegrableObject(iiter);
1440 <             integrableObject != NULL;
1441 <             integrableObject = mol->nextIntegrableObject(iiter))
1437 >      mol = info_->nextMolecule(miter))
1438 >      sd is essentially sd
1439 >        for (sd = mol->beginIntegrableObject(iiter);
1440 >             sd != NULL;
1441 >             sd = mol->nextIntegrableObject(iiter))
1442      */
1443 +
1444      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1445           sd = seleMan_.nextSelected(selei)) {
1446 <      
1446 >    
1447        idx = sd->getLocalIndex();
1448        
1449        Vector3d pos = sd->getPos();
# Line 1341 | Line 1452 | namespace OpenMD {
1452        
1453        if (usePeriodicBoundaryConditions_)
1454          currentSnap_->wrapVector(pos);
1455 <      
1455 >
1456 >
1457        // which bin is this stuntdouble in?
1458        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1459 <      
1460 <      int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
1461 <        rnemdLogWidth_;
1462 <      // no symmetrization allowed due to arbitary rnemdLogWidth_
1463 <      /*
1352 <      if (rnemdLogWidth_ == midBin_ + 1)
1353 <        if (binNo > midBin_)
1354 <          binNo = nBins_ - binNo;
1355 <      */
1459 >      // Shift molecules by half a box to have bins start at 0
1460 >      // The modulo operator is used to wrap the case when we are
1461 >      // beyond the end of the bins back to the beginning.
1462 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1463 >
1464        RealType mass = sd->getMass();
1357      mHist_[binNo] += mass;
1465        Vector3d vel = sd->getVel();
1359      RealType value;
1360      //RealType xVal, yVal, zVal;
1466  
1467 <      if (outputTemp_) {
1468 <        value = mass * vel.lengthSquare();
1469 <        tempCount_[binNo] += 3;
1470 <        if (sd->isDirectional()) {
1471 <          Vector3d angMom = sd->getJ();
1472 <          Mat3x3d I = sd->getI();
1473 <          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 <      }
1467 >      binCount[binNo]++;
1468 >      binMass[binNo] += mass;
1469 >      binPx[binNo] += mass*vel.x();
1470 >      binPy[binNo] += mass*vel.y();
1471 >      binPz[binNo] += mass*vel.z();
1472 >      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1473 >      binDOF[binNo] += 3;
1474  
1475 <      if (output3DTemp_) {
1476 <        value = mass * vel.x() * vel.x();
1477 <        xTempHist_[binNo] += value;
1478 <        value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1479 <          / PhysicalConstants::kb;
1480 <        yTempHist_[binNo] += value;
1481 <        value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1482 <          / PhysicalConstants::kb;
1483 <        zTempHist_[binNo] += value;
1484 <        xyzTempCount_[binNo]++;
1475 >      if (sd->isDirectional()) {
1476 >        Vector3d angMom = sd->getJ();
1477 >        Mat3x3d I = sd->getI();
1478 >        if (sd->isLinear()) {
1479 >          int i = sd->linearAxis();
1480 >          int j = (i + 1) % 3;
1481 >          int k = (i + 2) % 3;
1482 >          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
1483 >                                 angMom[k] * angMom[k] / I(k, k));
1484 >          binDOF[binNo] += 2;
1485 >        } else {
1486 >          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
1487 >                                 angMom[1] * angMom[1] / I(1, 1) +
1488 >                                 angMom[2] * angMom[2] / I(2, 2));
1489 >          binDOF[binNo] += 3;
1490 >        }
1491        }
1492 <      if (outputRotTemp_) {
1493 <        if (sd->isDirectional()) {
1494 <          Vector3d angMom = sd->getJ();
1495 <          Mat3x3d I = sd->getI();
1496 <          if (sd->isLinear()) {
1497 <            int i = sd->linearAxis();
1498 <            int j = (i + 1) % 3;
1499 <            int k = (i + 2) % 3;
1500 <            value = angMom[j] * angMom[j] / I(j, j) +
1501 <              angMom[k] * angMom[k] / I(k, k);
1502 <            rotTempCount_[binNo] +=2;
1503 <          } else {
1504 <            value = angMom[0] * angMom[0] / I(0, 0) +
1505 <              angMom[1] * angMom[1] / I(1, 1) +
1506 <              angMom[2] * angMom[2] / I(2, 2);
1507 <            rotTempCount_[binNo] +=3;
1508 <          }
1509 <        }
1426 <        value = value / PhysicalConstants::energyConvert
1427 <          / PhysicalConstants::kb;//may move to getStatus()
1428 <        rotTempHist_[binNo] += value;
1429 <      }
1492 >    }
1493 >    
1494 > #ifdef IS_MPI
1495 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
1496 >                              nBins_, MPI::INT, MPI::SUM);
1497 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
1498 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1499 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
1500 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1501 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
1502 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1503 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
1504 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1505 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
1506 >                              nBins_, MPI::REALTYPE, MPI::SUM);
1507 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
1508 >                              nBins_, MPI::INT, MPI::SUM);
1509 > #endif
1510  
1511 +    Vector3d vel;
1512 +    RealType den;
1513 +    RealType temp;
1514 +    RealType z;
1515 +    for (int i = 0; i < nBins_; i++) {
1516 +      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
1517 +      vel.x() = binPx[i] / binMass[i];
1518 +      vel.y() = binPy[i] / binMass[i];
1519 +      vel.z() = binPz[i] / binMass[i];
1520 +
1521 +      den = binMass[i] * nBins_ * PhysicalConstants::densityConvert
1522 +        / currentSnap_->getVolume() ;
1523 +
1524 +      if (binCount[i] > 0) {
1525 +        // only add values if there are things to add
1526 +        temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
1527 +                                 PhysicalConstants::energyConvert);
1528 +        
1529 +        for (unsigned int j = 0; j < outputMask_.size(); ++j) {
1530 +          if(outputMask_[j]) {
1531 +            switch(j) {
1532 +            case Z:
1533 +              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(z);
1534 +              break;
1535 +            case TEMPERATURE:
1536 +              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(temp);
1537 +              break;
1538 +            case VELOCITY:
1539 +              dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
1540 +              break;
1541 +            case DENSITY:
1542 +              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(den);
1543 +              break;
1544 +            }
1545 +          }
1546 +        }
1547 +      }
1548      }
1549    }
1550  
1551    void RNEMD::getStarted() {
1552 +    if (!doRNEMD_) return;
1553      collectData();
1554 <    /*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();
1554 >    writeOutputFile();
1555    }
1556  
1557 <  void RNEMD::getStatus() {
1558 <
1559 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1560 <    Stats& stat = currentSnap_->statData;
1561 <    RealType time = currentSnap_->getTime();
1562 <
1563 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1564 <    //or to be more meaningful, define another item as exchangeSum_ / time
1565 <    int j;
1566 <
1557 >  void RNEMD::parseOutputFileFormat(const std::string& format) {
1558 >    if (!doRNEMD_) return;
1559 >    StringTokenizer tokenizer(format, " ,;|\t\n\r");
1560 >    
1561 >    while(tokenizer.hasMoreTokens()) {
1562 >      std::string token(tokenizer.nextToken());
1563 >      toUpper(token);
1564 >      OutputMapType::iterator i = outputMap_.find(token);
1565 >      if (i != outputMap_.end()) {
1566 >        outputMask_.set(i->second);
1567 >      } else {
1568 >        sprintf( painCave.errMsg,
1569 >                 "RNEMD::parseOutputFileFormat: %s is not a recognized\n"
1570 >                 "\toutputFileFormat keyword.\n", token.c_str() );
1571 >        painCave.isFatal = 0;
1572 >        painCave.severity = OPENMD_ERROR;
1573 >        simError();            
1574 >      }
1575 >    }  
1576 >  }
1577 >  
1578 >  void RNEMD::writeOutputFile() {
1579 >    if (!doRNEMD_) return;
1580 >    
1581   #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
1582      // If we're the root node, should we print out the results
1583      int worldRank = MPI::COMM_WORLD.Get_rank();
1584      if (worldRank == 0) {
1585   #endif
1586 +      rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
1587 +      
1588 +      if( !rnemdFile_ ){        
1589 +        sprintf( painCave.errMsg,
1590 +                 "Could not open \"%s\" for RNEMD output.\n",
1591 +                 rnemdFileName_.c_str());
1592 +        painCave.isFatal = 1;
1593 +        simError();
1594 +      }
1595  
1596 <      if (outputTemp_) {
1597 <        tempLog_ << time;
1598 <        for (j = 0; j < rnemdLogWidth_; j++) {
1599 <          tempLog_ << "\t" << tempHist_[j] / (RealType)tempCount_[j];
1600 <        }
1601 <        tempLog_ << endl;
1596 >      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1597 >
1598 >      RealType time = currentSnap_->getTime();
1599 >      RealType avgArea;
1600 >      areaAccumulator_->getAverage(avgArea);
1601 >      RealType Jz = kineticExchange_ / (2.0 * time * avgArea)
1602 >        / PhysicalConstants::energyConvert;
1603 >      Vector3d JzP = momentumExchange_ / (2.0 * time * avgArea);      
1604 >
1605 >      rnemdFile_ << "#######################################################\n";
1606 >      rnemdFile_ << "# RNEMD {\n";
1607 >
1608 >      map<string, RNEMDMethod>::iterator mi;
1609 >      for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
1610 >        if ( (*mi).second == rnemdMethod_)
1611 >          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
1612        }
1613 <      if (outputVx_) {
1614 <        vxzLog_ << time;
1615 <        for (j = 0; j < rnemdLogWidth_; j++) {
1616 <          vxzLog_ << "\t" << pxzHist_[j] / mHist_[j];
1513 <        }
1514 <        vxzLog_ << endl;
1613 >      map<string, RNEMDFluxType>::iterator fi;
1614 >      for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
1615 >        if ( (*fi).second == rnemdFluxType_)
1616 >          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
1617        }
1618 <      if (outputVy_) {
1619 <        vyzLog_ << time;
1518 <        for (j = 0; j < rnemdLogWidth_; j++) {
1519 <          vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1520 <        }
1521 <        vyzLog_ << endl;
1522 <      }
1618 >      
1619 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
1620  
1621 <      if (output3DTemp_) {
1622 <        RealType temp;
1623 <        xTempLog_ << time;
1624 <        for (j = 0; j < rnemdLogWidth_; j++) {
1625 <          if (outputVx_)
1626 <            xTempHist_[j] -= pxzHist_[j] * pxzHist_[j] / mHist_[j];
1627 <          temp = xTempHist_[j] / (RealType)xyzTempCount_[j]
1628 <            / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1629 <          xTempLog_ << "\t" << temp;
1621 >      rnemdFile_ << "#    objectSelection = \""
1622 >                 << rnemdObjectSelection_ << "\";\n";
1623 >      rnemdFile_ << "#    slabWidth = " << slabWidth_ << ";\n";
1624 >      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << ";\n";
1625 >      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << ";\n";
1626 >      rnemdFile_ << "# }\n";
1627 >      rnemdFile_ << "#######################################################\n";
1628 >      rnemdFile_ << "# RNEMD report:\n";      
1629 >      rnemdFile_ << "#     running time = " << time << " fs\n";
1630 >      rnemdFile_ << "#     target flux:\n";
1631 >      rnemdFile_ << "#         kinetic = "
1632 >                 << kineticFlux_ / PhysicalConstants::energyConvert
1633 >                 << " (kcal/mol/A^2/fs)\n";
1634 >      rnemdFile_ << "#         momentum = " << momentumFluxVector_
1635 >                 << " (amu/A/fs^2)\n";
1636 >      rnemdFile_ << "#     target one-time exchanges:\n";
1637 >      rnemdFile_ << "#         kinetic = "
1638 >                 << kineticTarget_ / PhysicalConstants::energyConvert
1639 >                 << " (kcal/mol)\n";
1640 >      rnemdFile_ << "#         momentum = " << momentumTarget_
1641 >                 << " (amu*A/fs)\n";
1642 >      rnemdFile_ << "#     actual exchange totals:\n";
1643 >      rnemdFile_ << "#         kinetic = "
1644 >                 << kineticExchange_ / PhysicalConstants::energyConvert
1645 >                 << " (kcal/mol)\n";
1646 >      rnemdFile_ << "#         momentum = " << momentumExchange_
1647 >                 << " (amu*A/fs)\n";      
1648 >      rnemdFile_ << "#     actual flux:\n";
1649 >      rnemdFile_ << "#         kinetic = " << Jz
1650 >                 << " (kcal/mol/A^2/fs)\n";
1651 >      rnemdFile_ << "#         momentum = " << JzP
1652 >                 << " (amu/A/fs^2)\n";
1653 >      rnemdFile_ << "#     exchange statistics:\n";
1654 >      rnemdFile_ << "#         attempted = " << trialCount_ << "\n";
1655 >      rnemdFile_ << "#         failed = " << failTrialCount_ << "\n";    
1656 >      if (rnemdMethod_ == rnemdNIVS) {
1657 >        rnemdFile_ << "#         NIVS root-check errors = "
1658 >                   << failRootCount_ << "\n";
1659 >      }
1660 >      rnemdFile_ << "#######################################################\n";
1661 >      
1662 >      
1663 >      
1664 >      //write title
1665 >      rnemdFile_ << "#";
1666 >      for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1667 >        if (outputMask_[i]) {
1668 >          rnemdFile_ << "\t" << data_[i].title <<
1669 >            "(" << data_[i].units << ")";
1670 >          // add some extra tabs for column alignment
1671 >          if (data_[i].dataType == "Vector3d") rnemdFile_ << "\t\t";
1672          }
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;
1673        }
1674 <      if (outputRotTemp_) {
1675 <        rotTempLog_ << time;
1676 <        for (j = 0; j < rnemdLogWidth_; j++) {
1677 <          rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1678 <        }
1679 <        rotTempLog_ << endl;
1680 <      }
1674 >      rnemdFile_ << std::endl;
1675 >      
1676 >      rnemdFile_.precision(8);
1677 >      
1678 >      for (unsigned int j = 0; j < nBins_; j++) {        
1679 >        
1680 >        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1681 >          if (outputMask_[i]) {
1682 >            if (data_[i].dataType == "RealType")
1683 >              writeReal(i,j);
1684 >            else if (data_[i].dataType == "Vector3d")
1685 >              writeVector(i,j);
1686 >            else {
1687 >              sprintf( painCave.errMsg,
1688 >                       "RNEMD found an unknown data type for: %s ",
1689 >                       data_[i].title.c_str());
1690 >              painCave.isFatal = 1;
1691 >              simError();
1692 >            }
1693 >          }
1694 >        }
1695 >        rnemdFile_ << std::endl;
1696 >        
1697 >      }        
1698  
1699 +      rnemdFile_ << "#######################################################\n";
1700 +      rnemdFile_ << "# Standard Deviations in those quantities follow:\n";
1701 +      rnemdFile_ << "#######################################################\n";
1702 +
1703 +
1704 +      for (unsigned int j = 0; j < nBins_; j++) {        
1705 +        rnemdFile_ << "#";
1706 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1707 +          if (outputMask_[i]) {
1708 +            if (data_[i].dataType == "RealType")
1709 +              writeRealStdDev(i,j);
1710 +            else if (data_[i].dataType == "Vector3d")
1711 +              writeVectorStdDev(i,j);
1712 +            else {
1713 +              sprintf( painCave.errMsg,
1714 +                       "RNEMD found an unknown data type for: %s ",
1715 +                       data_[i].title.c_str());
1716 +              painCave.isFatal = 1;
1717 +              simError();
1718 +            }
1719 +          }
1720 +        }
1721 +        rnemdFile_ << std::endl;
1722 +        
1723 +      }        
1724 +      
1725 +      rnemdFile_.flush();
1726 +      rnemdFile_.close();
1727 +      
1728   #ifdef IS_MPI
1729      }
1730   #endif
1731 <
1732 <    for (j = 0; j < rnemdLogWidth_; j++) {
1733 <      mHist_[j] = 0.0;
1731 >    
1732 >  }
1733 >  
1734 >  void RNEMD::writeReal(int index, unsigned int bin) {
1735 >    if (!doRNEMD_) return;
1736 >    assert(index >=0 && index < ENDINDEX);
1737 >    assert(bin < nBins_);
1738 >    RealType s;
1739 >    
1740 >    data_[index].accumulator[bin]->getAverage(s);
1741 >    
1742 >    if (! isinf(s) && ! isnan(s)) {
1743 >      rnemdFile_ << "\t" << s;
1744 >    } else{
1745 >      sprintf( painCave.errMsg,
1746 >               "RNEMD detected a numerical error writing: %s for bin %d",
1747 >               data_[index].title.c_str(), bin);
1748 >      painCave.isFatal = 1;
1749 >      simError();
1750 >    }    
1751 >  }
1752 >  
1753 >  void RNEMD::writeVector(int index, unsigned int bin) {
1754 >    if (!doRNEMD_) return;
1755 >    assert(index >=0 && index < ENDINDEX);
1756 >    assert(bin < nBins_);
1757 >    Vector3d s;
1758 >    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
1759 >    if (isinf(s[0]) || isnan(s[0]) ||
1760 >        isinf(s[1]) || isnan(s[1]) ||
1761 >        isinf(s[2]) || isnan(s[2]) ) {      
1762 >      sprintf( painCave.errMsg,
1763 >               "RNEMD detected a numerical error writing: %s for bin %d",
1764 >               data_[index].title.c_str(), bin);
1765 >      painCave.isFatal = 1;
1766 >      simError();
1767 >    } else {
1768 >      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1769      }
1770 <    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 <      }
1770 >  }  
1771  
1772 <    if (output3DTemp_)
1773 <      for (j = 0; j < rnemdLogWidth_; j++) {
1774 <        xTempHist_[j] = 0.0;
1775 <        yTempHist_[j] = 0.0;
1776 <        zTempHist_[j] = 0.0;
1777 <        xyzTempCount_[j] = 0;
1778 <      }
1779 <    if (outputRotTemp_)
1780 <      for (j = 0; j < rnemdLogWidth_; j++) {
1781 <        rotTempCount_[j] = 0;
1782 <        rotTempHist_[j] = 0.0;
1783 <      }
1772 >  void RNEMD::writeRealStdDev(int index, unsigned int bin) {
1773 >    if (!doRNEMD_) return;
1774 >    assert(index >=0 && index < ENDINDEX);
1775 >    assert(bin < nBins_);
1776 >    RealType s;
1777 >    
1778 >    data_[index].accumulator[bin]->getStdDev(s);
1779 >    
1780 >    if (! isinf(s) && ! isnan(s)) {
1781 >      rnemdFile_ << "\t" << s;
1782 >    } else{
1783 >      sprintf( painCave.errMsg,
1784 >               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1785 >               data_[index].title.c_str(), bin);
1786 >      painCave.isFatal = 1;
1787 >      simError();
1788 >    }    
1789    }
1790 +  
1791 +  void RNEMD::writeVectorStdDev(int index, unsigned int bin) {
1792 +    if (!doRNEMD_) return;
1793 +    assert(index >=0 && index < ENDINDEX);
1794 +    assert(bin < nBins_);
1795 +    Vector3d s;
1796 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getStdDev(s);
1797 +    if (isinf(s[0]) || isnan(s[0]) ||
1798 +        isinf(s[1]) || isnan(s[1]) ||
1799 +        isinf(s[2]) || isnan(s[2]) ) {      
1800 +      sprintf( painCave.errMsg,
1801 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1802 +               data_[index].title.c_str(), bin);
1803 +      painCave.isFatal = 1;
1804 +      simError();
1805 +    } else {
1806 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1807 +    }
1808 +  }  
1809   }
1810  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines