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 1776 by gezelter, Thu Aug 9 15:52:59 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_["KE+Px"]  = rnemdKePx;
84 >    stringToFluxType_["KE+Py"]  = rnemdKePy;
85 >    stringToFluxType_["KE+Pvector"]  = rnemdKePvector;
86 >
87 >    runTime_ = simParams->getRunTime();
88 >    statusTime_ = simParams->getStatusTime();
89 >
90 >    rnemdObjectSelection_ = rnemdParams->getObjectSelection();
91      evaluator_.loadScriptString(rnemdObjectSelection_);
92      seleMan_.setSelectionSet(evaluator_.evaluate());
93 +
94 +    const string methStr = rnemdParams->getMethod();
95 +    bool hasFluxType = rnemdParams->haveFluxType();
96 +
97 +    string fluxStr;
98 +    if (hasFluxType) {
99 +      fluxStr = rnemdParams->getFluxType();
100 +    } else {
101 +      sprintf(painCave.errMsg,
102 +              "RNEMD: No fluxType was set in the md file.  This parameter,\n"
103 +              "\twhich must be one of the following values:\n"
104 +              "\tKE, Px, Py, Pz, KE+Px, KE+Py, KE+Pvector, must be set to\n"
105 +              "\tuse RNEMD\n");
106 +      painCave.isFatal = 1;
107 +      painCave.severity = OPENMD_ERROR;
108 +      simError();
109 +    }
110 +
111 +    bool hasKineticFlux = rnemdParams->haveKineticFlux();
112 +    bool hasMomentumFlux = rnemdParams->haveMomentumFlux();
113 +    bool hasMomentumFluxVector = rnemdParams->haveMomentumFluxVector();
114 +    bool hasSlabWidth = rnemdParams->haveSlabWidth();
115 +    bool hasSlabACenter = rnemdParams->haveSlabACenter();
116 +    bool hasSlabBCenter = rnemdParams->haveSlabBCenter();
117 +    bool hasOutputFileName = rnemdParams->haveOutputFileName();
118 +    bool hasOutputFields = rnemdParams->haveOutputFields();
119 +    
120 +    map<string, RNEMDMethod>::iterator i;
121 +    i = stringToMethod_.find(methStr);
122 +    if (i != stringToMethod_.end())
123 +      rnemdMethod_ = i->second;
124 +    else {
125 +      sprintf(painCave.errMsg,
126 +              "RNEMD: The current method,\n"
127 +              "\t\t%s is not one of the recognized\n"
128 +              "\texchange methods: Swap, NIVS, or VSS\n",
129 +              methStr.c_str());
130 +      painCave.isFatal = 1;
131 +      painCave.severity = OPENMD_ERROR;
132 +      simError();
133 +    }
134 +
135 +    map<string, RNEMDFluxType>::iterator j;
136 +    j = stringToFluxType_.find(fluxStr);
137 +    if (j != stringToFluxType_.end())
138 +      rnemdFluxType_ = j->second;
139 +    else {
140 +      sprintf(painCave.errMsg,
141 +              "RNEMD: The current fluxType,\n"
142 +              "\t\t%s\n"
143 +              "\tis not one of the recognized flux types.\n",
144 +              fluxStr.c_str());
145 +      painCave.isFatal = 1;
146 +      painCave.severity = OPENMD_ERROR;
147 +      simError();
148 +    }
149 +
150 +    bool methodFluxMismatch = false;
151 +    bool hasCorrectFlux = false;
152 +    switch(rnemdMethod_) {
153 +    case rnemdSwap:
154 +      switch (rnemdFluxType_) {
155 +      case rnemdKE:
156 +        hasCorrectFlux = hasKineticFlux;
157 +        break;
158 +      case rnemdPx:
159 +      case rnemdPy:
160 +      case rnemdPz:
161 +        hasCorrectFlux = hasMomentumFlux;
162 +        break;
163 +      default :
164 +        methodFluxMismatch = true;
165 +        break;
166 +      }
167 +      break;
168 +    case rnemdNIVS:
169 +      switch (rnemdFluxType_) {
170 +      case rnemdKE:
171 +      case rnemdRotKE:
172 +      case rnemdFullKE:
173 +        hasCorrectFlux = hasKineticFlux;
174 +        break;
175 +      case rnemdPx:
176 +      case rnemdPy:
177 +      case rnemdPz:
178 +        hasCorrectFlux = hasMomentumFlux;
179 +        break;
180 +      case rnemdKePx:
181 +      case rnemdKePy:
182 +        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
183 +        break;
184 +      default:
185 +        methodFluxMismatch = true;
186 +        break;
187 +      }
188 +      break;
189 +    case rnemdVSS:
190 +      switch (rnemdFluxType_) {
191 +      case rnemdKE:
192 +      case rnemdRotKE:
193 +      case rnemdFullKE:
194 +        hasCorrectFlux = hasKineticFlux;
195 +        break;
196 +      case rnemdPx:
197 +      case rnemdPy:
198 +      case rnemdPz:
199 +        hasCorrectFlux = hasMomentumFlux;
200 +        break;
201 +      case rnemdPvector:
202 +        hasCorrectFlux = hasMomentumFluxVector;
203 +      case rnemdKePx:
204 +      case rnemdKePy:
205 +        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
206 +        break;
207 +      case rnemdKePvector:
208 +        hasCorrectFlux = hasMomentumFluxVector && hasKineticFlux;
209 +        break;
210 +      default:
211 +        methodFluxMismatch = true;
212 +        break;
213 +      }
214 +    default:
215 +      break;
216 +    }
217 +
218 +    if (methodFluxMismatch) {
219 +      sprintf(painCave.errMsg,
220 +              "RNEMD: The current method,\n"
221 +              "\t\t%s\n"
222 +              "\tcannot be used with the current flux type, %s\n",
223 +              methStr.c_str(), fluxStr.c_str());
224 +      painCave.isFatal = 1;
225 +      painCave.severity = OPENMD_ERROR;
226 +      simError();        
227 +    }
228 +    if (!hasCorrectFlux) {
229 +      sprintf(painCave.errMsg,
230 +              "RNEMD: The current method,\n"
231 +              "\t%s, and flux type %s\n"
232 +              "\tdid not have the correct flux value specified. Options\n"
233 +              "\tinclude: kineticFlux, momentumFlux, and momentumFluxVector\n",
234 +              methStr.c_str(), fluxStr.c_str());
235 +      painCave.isFatal = 1;
236 +      painCave.severity = OPENMD_ERROR;
237 +      simError();        
238 +    }
239  
240 +    if (hasKineticFlux) {
241 +      kineticFlux_ = rnemdParams->getKineticFlux();
242 +    } else {
243 +      kineticFlux_ = 0.0;
244 +    }
245 +    if (hasMomentumFluxVector) {
246 +      momentumFluxVector_ = rnemdParams->getMomentumFluxVector();
247 +    } else {
248 +      momentumFluxVector_ = V3Zero;
249 +      if (hasMomentumFlux) {
250 +        RealType momentumFlux = rnemdParams->getMomentumFlux();
251 +        switch (rnemdFluxType_) {
252 +        case rnemdPx:
253 +          momentumFluxVector_.x() = momentumFlux;
254 +          break;
255 +        case rnemdPy:
256 +          momentumFluxVector_.y() = momentumFlux;
257 +          break;
258 +        case rnemdPz:
259 +          momentumFluxVector_.z() = momentumFlux;
260 +          break;
261 +        case rnemdKePx:
262 +          momentumFluxVector_.x() = momentumFlux;
263 +          break;
264 +        case rnemdKePy:
265 +          momentumFluxVector_.y() = momentumFlux;
266 +          break;
267 +        default:
268 +          break;
269 +        }
270 +      }    
271 +    }
272 +
273      // do some sanity checking
274  
275      int selectionCount = seleMan_.getSelectionCount();
# Line 96 | Line 277 | namespace OpenMD {
277  
278      if (selectionCount > nIntegrable) {
279        sprintf(painCave.errMsg,
280 <              "RNEMD: The current RNEMD_objectSelection,\n"
280 >              "RNEMD: The current objectSelection,\n"
281                "\t\t%s\n"
282                "\thas resulted in %d selected objects.  However,\n"
283                "\tthe total number of integrable objects in the system\n"
# Line 109 | Line 290 | namespace OpenMD {
290        painCave.severity = OPENMD_WARNING;
291        simError();
292      }
112    
113    const string st = simParams->getRNEMD_exchangeType();
293  
294 <    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 <    }
294 >    areaAccumulator_ = new Accumulator();
295  
296 < #ifdef IS_MPI
160 <    if (worldRank == 0) {
161 < #endif
296 >    nBins_ = rnemdParams->getOutputBins();
297  
298 <      //may have rnemdWriter separately
299 <      string rnemdFileName;
298 >    data_.resize(RNEMD::ENDINDEX);
299 >    OutputData z;
300 >    z.units =  "Angstroms";
301 >    z.title =  "Z";
302 >    z.dataType = "RealType";
303 >    z.accumulator.reserve(nBins_);
304 >    for (unsigned int i = 0; i < nBins_; i++)
305 >      z.accumulator.push_back( new Accumulator() );
306 >    data_[Z] = z;
307 >    outputMap_["Z"] =  Z;
308  
309 <      if (outputTemp_) {
310 <        rnemdFileName = "temperature.log";
311 <        tempLog_.open(rnemdFileName.c_str());
312 <      }
313 <      if (outputVx_) {
314 <        rnemdFileName = "velocityX.log";
315 <        vxzLog_.open(rnemdFileName.c_str());
316 <      }
317 <      if (outputVy_) {
175 <        rnemdFileName = "velocityY.log";
176 <        vyzLog_.open(rnemdFileName.c_str());
177 <      }
309 >    OutputData temperature;
310 >    temperature.units =  "K";
311 >    temperature.title =  "Temperature";
312 >    temperature.dataType = "RealType";
313 >    temperature.accumulator.reserve(nBins_);
314 >    for (unsigned int i = 0; i < nBins_; i++)
315 >      temperature.accumulator.push_back( new Accumulator() );
316 >    data_[TEMPERATURE] = temperature;
317 >    outputMap_["TEMPERATURE"] =  TEMPERATURE;
318  
319 <      if (output3DTemp_) {
320 <        rnemdFileName = "temperatureX.log";
321 <        xTempLog_.open(rnemdFileName.c_str());
322 <        rnemdFileName = "temperatureY.log";
323 <        yTempLog_.open(rnemdFileName.c_str());
324 <        rnemdFileName = "temperatureZ.log";
325 <        zTempLog_.open(rnemdFileName.c_str());
326 <      }
327 <      if (outputRotTemp_) {
188 <        rnemdFileName = "temperatureR.log";
189 <        rotTempLog_.open(rnemdFileName.c_str());
190 <      }
319 >    OutputData velocity;
320 >    velocity.units = "amu/fs";
321 >    velocity.title =  "Velocity";  
322 >    velocity.dataType = "Vector3d";
323 >    velocity.accumulator.reserve(nBins_);
324 >    for (unsigned int i = 0; i < nBins_; i++)
325 >      velocity.accumulator.push_back( new VectorAccumulator() );
326 >    data_[VELOCITY] = velocity;
327 >    outputMap_["VELOCITY"] = VELOCITY;
328  
329 < #ifdef IS_MPI
330 <    }
331 < #endif
329 >    OutputData density;
330 >    density.units =  "g cm^-3";
331 >    density.title =  "Density";
332 >    density.dataType = "RealType";
333 >    density.accumulator.reserve(nBins_);
334 >    for (unsigned int i = 0; i < nBins_; i++)
335 >      density.accumulator.push_back( new Accumulator() );
336 >    data_[DENSITY] = density;
337 >    outputMap_["DENSITY"] =  DENSITY;
338  
339 <    set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
340 <    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 <      }
339 >    if (hasOutputFields) {
340 >      parseOutputFileFormat(rnemdParams->getOutputFields());
341      } else {
342 <      zShift_ = 0.0;
342 >      outputMask_.set(Z);
343 >      switch (rnemdFluxType_) {
344 >      case rnemdKE:
345 >      case rnemdRotKE:
346 >      case rnemdFullKE:
347 >        outputMask_.set(TEMPERATURE);
348 >        break;
349 >      case rnemdPx:
350 >      case rnemdPy:
351 >        outputMask_.set(VELOCITY);
352 >        break;
353 >      case rnemdPz:        
354 >      case rnemdPvector:
355 >        outputMask_.set(VELOCITY);
356 >        outputMask_.set(DENSITY);
357 >        break;
358 >      case rnemdKePx:
359 >      case rnemdKePy:
360 >        outputMask_.set(TEMPERATURE);
361 >        outputMask_.set(VELOCITY);
362 >        break;
363 >      case rnemdKePvector:
364 >        outputMask_.set(TEMPERATURE);
365 >        outputMask_.set(VELOCITY);
366 >        outputMask_.set(DENSITY);        
367 >        break;
368 >      default:
369 >        break;
370 >      }
371      }
372 <    //cerr << "I shift slabs by " << zShift_ << " Lz\n";
373 <    //shift slabs by half slab width, maybe useful in heterogeneous systems
374 <    //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 <      }*/
372 >      
373 >    if (hasOutputFileName) {
374 >      rnemdFileName_ = rnemdParams->getOutputFileName();
375      } else {
376 <      set_RNEMD_logWidth(nBins_);
377 <    }
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);
376 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
377 >    }          
378  
379 <    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);
379 >    exchangeTime_ = rnemdParams->getExchangeTime();
380  
381 <    set_RNEMD_exchange_total(0.0);
382 <    if (simParams->haveRNEMD_targetFlux()) {
383 <      set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
384 <    } else {
385 <      set_RNEMD_target_flux(0.0);
386 <    }
387 <    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_;
381 >    Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
382 >    Mat3x3d hmat = currentSnap_->getHmat();
383 >  
384 >    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
385 >    // Lx, Ly = box dimensions in x & y
386 >    // dt = exchange time interval
387 >    // flux = target flux
388  
389 < #ifndef IS_MPI
390 <    if (simParams->haveSeed()) {
391 <      seedValue = simParams->getSeed();
392 <      randNumGen_ = new SeqRandNumGen(seedValue);
393 <    }else {
394 <      randNumGen_ = new SeqRandNumGen();
395 <    }    
396 < #else
397 <    if (simParams->haveSeed()) {
398 <      seedValue = simParams->getSeed();
399 <      randNumGen_ = new ParallelRandNumGen(seedValue);
400 <    }else {
401 <      randNumGen_ = new ParallelRandNumGen();
283 <    }    
284 < #endif
285 <  }
389 >    RealType area = currentSnap_->getXYarea();
390 >    kineticTarget_ = 2.0 * kineticFlux_ * exchangeTime_ * area;
391 >    momentumTarget_ = 2.0 * momentumFluxVector_ * exchangeTime_ * area;
392 >
393 >    // total exchange sums are zeroed out at the beginning:
394 >
395 >    kineticExchange_ = 0.0;
396 >    momentumExchange_ = V3Zero;
397 >
398 >    if (hasSlabWidth)
399 >      slabWidth_ = rnemdParams->getSlabWidth();
400 >    else
401 >      slabWidth_ = hmat(2,2) / 10.0;
402    
403 <  RNEMD::~RNEMD() {
404 <    delete randNumGen_;
403 >    if (hasSlabACenter)
404 >      slabACenter_ = rnemdParams->getSlabACenter();
405 >    else
406 >      slabACenter_ = 0.0;
407      
408 +    if (hasSlabBCenter)
409 +      slabBCenter_ = rnemdParams->getSlabBCenter();
410 +    else
411 +      slabBCenter_ = hmat(2,2) / 2.0;
412 +    
413 +  }
414 +  
415 +  RNEMD::~RNEMD() {
416 +    if (!doRNEMD_) return;
417   #ifdef IS_MPI
418      if (worldRank == 0) {
419   #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();
420  
421 <      if (outputTemp_) tempLog_.close();
302 <      if (outputVx_)   vxzLog_.close();
303 <      if (outputVy_)   vyzLog_.close();
421 >      writeOutputFile();
422  
423 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
424 <          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 <
423 >      rnemdFile_.close();
424 >      
425   #ifdef IS_MPI
426      }
427   #endif
428    }
429 +  
430 +  bool RNEMD::inSlabA(Vector3d pos) {
431 +    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
432 +  }
433 +  bool RNEMD::inSlabB(Vector3d pos) {
434 +    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
435 +  }
436  
437    void RNEMD::doSwap() {
438 <
438 >    if (!doRNEMD_) return;
439      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
440      Mat3x3d hmat = currentSnap_->getHmat();
441  
# Line 353 | Line 464 | namespace OpenMD {
464  
465        if (usePeriodicBoundaryConditions_)
466          currentSnap_->wrapVector(pos);
467 +      bool inA = inSlabA(pos);
468 +      bool inB = inSlabB(pos);
469  
470 <      // 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_) {
470 >      if (inA || inB) {
471          
472          RealType mass = sd->getMass();
473          Vector3d vel = sd->getVel();
474          RealType value;
475 <
476 <        switch(rnemdType_) {
477 <        case rnemdKineticSwap :
475 >        
476 >        switch(rnemdFluxType_) {
477 >        case rnemdKE :
478            
479            value = mass * vel.lengthSquare();
480            
# Line 389 | Line 495 | namespace OpenMD {
495              }
496            } //angular momenta exchange enabled
497            //energyConvert temporarily disabled
498 <          //make exchangeSum_ comparable between swap & scale
498 >          //make kineticExchange_ comparable between swap & scale
499            //value = value * 0.5 / PhysicalConstants::energyConvert;
500            value *= 0.5;
501            break;
# Line 406 | Line 512 | namespace OpenMD {
512            break;
513          }
514          
515 <        if (binNo == 0) {
515 >        if (inA == 0) {
516            if (!min_found) {
517              min_val = value;
518              min_sd = sd;
# Line 417 | Line 523 | namespace OpenMD {
523                min_sd = sd;
524              }
525            }
526 <        } else { //midBin_
526 >        } else {
527            if (!max_found) {
528              max_val = value;
529              max_sd = sd;
# Line 431 | Line 537 | namespace OpenMD {
537          }
538        }
539      }
540 <
540 >    
541   #ifdef IS_MPI
542      int nProc, worldRank;
543 <
543 >    
544      nProc = MPI::COMM_WORLD.Get_size();
545      worldRank = MPI::COMM_WORLD.Get_rank();
546  
# Line 454 | Line 560 | namespace OpenMD {
560          RealType val;
561          int rank;
562        } max_vals, min_vals;
563 <    
563 >      
564        if (my_min_found) {
565          min_vals.val = min_val;
566        } else {
# Line 492 | Line 598 | namespace OpenMD {
598            Vector3d max_vel = max_sd->getVel();
599            RealType temp_vel;
600            
601 <          switch(rnemdType_) {
602 <          case rnemdKineticSwap :
601 >          switch(rnemdFluxType_) {
602 >          case rnemdKE :
603              min_sd->setVel(max_vel);
604              max_sd->setVel(min_vel);
605              if (min_sd->isDirectional() && max_sd->isDirectional()) {
# Line 544 | Line 650 | namespace OpenMD {
650                                     min_vel.getArrayPointer(), 3, MPI::REALTYPE,
651                                     min_vals.rank, 0, status);
652            
653 <          switch(rnemdType_) {
654 <          case rnemdKineticSwap :
653 >          switch(rnemdFluxType_) {
654 >          case rnemdKE :
655              max_sd->setVel(min_vel);
656              //angular momenta exchange enabled
657              if (max_sd->isDirectional()) {
# Line 590 | Line 696 | namespace OpenMD {
696                                     max_vel.getArrayPointer(), 3, MPI::REALTYPE,
697                                     max_vals.rank, 0, status);
698            
699 <          switch(rnemdType_) {
700 <          case rnemdKineticSwap :
699 >          switch(rnemdFluxType_) {
700 >          case rnemdKE :
701              min_sd->setVel(max_vel);
702              //angular momenta exchange enabled
703              if (min_sd->isDirectional()) {
# Line 625 | Line 731 | namespace OpenMD {
731            }
732          }
733   #endif
734 <        exchangeSum_ += max_val - min_val;
734 >        
735 >        switch(rnemdFluxType_) {
736 >        case rnemdKE:
737 >          cerr << "KE\n";
738 >          kineticExchange_ += max_val - min_val;
739 >          break;
740 >        case rnemdPx:
741 >          momentumExchange_.x() += max_val - min_val;
742 >          break;
743 >        case rnemdPy:
744 >          momentumExchange_.y() += max_val - min_val;
745 >          break;
746 >        case rnemdPz:
747 >          momentumExchange_.z() += max_val - min_val;
748 >          break;
749 >        default:
750 >          cerr << "default\n";
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;
917 >        //std::cerr << "cold slab scaling coefficient: " << c << endl;
918          //now convert to hotBin coefficient
919          RealType w = 0.0;
920 <        if (rnemdType_ ==  rnemdKineticScaleVAM) {
920 >        if (rnemdFluxType_ ==  rnemdFullKE) {
921            x = 1.0 + px * (1.0 - c);
922            y = 1.0 + py * (1.0 - c);
923            z = 1.0 + pz * (1.0 - c);
# Line 820 | Line 931 | namespace OpenMD {
931            */
932            if ((fabs(x - 1.0) < 0.1) && (fabs(y - 1.0) < 0.1) &&
933                (fabs(z - 1.0) < 0.1)) {
934 <            w = 1.0 + (targetFlux_ + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
934 >            w = 1.0 + (kineticTarget_
935 >                       + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
936                         + Khz * (1.0 - z * z)) / Khw;
937            }//no need to calculate w if x, y or z is out of range
938          } else {
939 <          w = 1.0 + targetFlux_ / Khw;
939 >          w = 1.0 + kineticTarget_ / Khw;
940          }
941          if ((w > 0.81) && (w < 1.21)) {//restrict scaling coefficients
942            //if w is in the right range, so should be x, y, z.
943            vector<StuntDouble*>::iterator sdi;
944            Vector3d vel;
945            for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
946 <            if (rnemdType_ == rnemdKineticScaleVAM) {
946 >            if (rnemdFluxType_ == rnemdFullKE) {
947                vel = (*sdi)->getVel() * c;
836              //vel.x() *= c;
837              //vel.y() *= c;
838              //vel.z() *= c;
948                (*sdi)->setVel(vel);
949              }
950              if ((*sdi)->isDirectional()) {
951                Vector3d angMom = (*sdi)->getJ() * c;
843              //angMom[0] *= c;
844              //angMom[1] *= c;
845              //angMom[2] *= c;
952                (*sdi)->setJ(angMom);
953              }
954            }
955            w = sqrt(w);
956 <          std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
957 <                    << "\twh= " << w << endl;
956 >          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
957 >          //           << "\twh= " << w << endl;
958            for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
959 <            if (rnemdType_ == rnemdKineticScaleVAM) {
959 >            if (rnemdFluxType_ == rnemdFullKE) {
960                vel = (*sdi)->getVel();
961                vel.x() *= x;
962                vel.y() *= y;
# Line 859 | Line 965 | namespace OpenMD {
965              }
966              if ((*sdi)->isDirectional()) {
967                Vector3d angMom = (*sdi)->getJ() * w;
862              //angMom[0] *= w;
863              //angMom[1] *= w;
864              //angMom[2] *= w;
968                (*sdi)->setJ(angMom);
969              }
970            }
971            successfulScale = true;
972 <          exchangeSum_ += targetFlux_;
972 >          kineticExchange_ += kineticTarget_;
973          }
974        }
975      } else {
976        RealType a000, a110, c0, a001, a111, b01, b11, c1;
977 <      switch(rnemdType_) {
978 <      case rnemdKineticScale :
977 >      switch(rnemdFluxType_) {
978 >      case rnemdKE :
979          /* used hotBin coeff's & only scale x & y dimensions
980             RealType px = Phx / Pcx;
981             RealType py = Phy / Pcy;
982             a110 = Khy;
983 <           c0 = - Khx - Khy - targetFlux_;
983 >           c0 = - Khx - Khy - kineticTarget_;
984             a000 = Khx;
985             a111 = Kcy * py * py;
986             b11 = -2.0 * Kcy * py * (1.0 + py);
987 <           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
987 >           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + kineticTarget_;
988             b01 = -2.0 * Kcx * px * (1.0 + px);
989             a001 = Kcx * px * px;
990          */
991          //scale all three dimensions, let c_x = c_y
992          a000 = Kcx + Kcy;
993          a110 = Kcz;
994 <        c0 = targetFlux_ - Kcx - Kcy - Kcz;
994 >        c0 = kineticTarget_ - Kcx - Kcy - Kcz;
995          a001 = Khx * px * px + Khy * py * py;
996          a111 = Khz * pz * pz;
997          b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
998          b11 = -2.0 * Khz * pz * (1.0 + pz);
999          c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
1000 <          + Khz * pz * (2.0 + pz) - targetFlux_;
1000 >          + Khz * pz * (2.0 + pz) - kineticTarget_;
1001          break;
1002 <      case rnemdPxScale :
1003 <        c = 1 - targetFlux_ / Pcx;
1002 >      case rnemdPx :
1003 >        c = 1 - momentumTarget_.x() / Pcx;
1004          a000 = Kcy;
1005          a110 = Kcz;
1006          c0 = Kcx * c * c - Kcx - Kcy - Kcz;
# Line 908 | Line 1011 | namespace OpenMD {
1011          c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
1012            + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
1013          break;
1014 <      case rnemdPyScale :
1015 <        c = 1 - targetFlux_ / Pcy;
1014 >      case rnemdPy :
1015 >        c = 1 - momentumTarget_.y() / Pcy;
1016          a000 = Kcx;
1017          a110 = Kcz;
1018          c0 = Kcy * c * c - Kcx - Kcy - Kcz;
# Line 920 | Line 1023 | namespace OpenMD {
1023          c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
1024            + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
1025          break;
1026 <      case rnemdPzScale ://we don't really do this, do we?
1027 <        c = 1 - targetFlux_ / Pcz;
1026 >      case rnemdPz ://we don't really do this, do we?
1027 >        c = 1 - momentumTarget_.z() / Pcz;
1028          a000 = Kcx;
1029          a110 = Kcy;
1030          c0 = Kcz * c * c - Kcx - Kcy - Kcz;
# Line 1006 | Line 1109 | namespace OpenMD {
1109          for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1110            r1 = (*rpi).first;
1111            r2 = (*rpi).second;
1112 <          switch(rnemdType_) {
1113 <          case rnemdKineticScale :
1112 >          switch(rnemdFluxType_) {
1113 >          case rnemdKE :
1114              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1115                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1116                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1117              break;
1118 <          case rnemdPxScale :
1118 >          case rnemdPx :
1119              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1120                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1121              break;
1122 <          case rnemdPyScale :
1122 >          case rnemdPy :
1123              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1124                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1125              break;
1126 <          case rnemdPzScale :
1126 >          case rnemdPz :
1127              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1128                + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1129            default :
# Line 1034 | Line 1137 | namespace OpenMD {
1137   #ifdef IS_MPI
1138          if (worldRank == 0) {
1139   #endif
1140 <          sprintf(painCave.errMsg,
1141 <                  "RNEMD: roots r1= %lf\tr2 = %lf\n",
1142 <                  bestPair.first, bestPair.second);
1143 <          painCave.isFatal = 0;
1144 <          painCave.severity = OPENMD_INFO;
1145 <          simError();
1140 >          // sprintf(painCave.errMsg,
1141 >          //         "RNEMD: roots r1= %lf\tr2 = %lf\n",
1142 >          //         bestPair.first, bestPair.second);
1143 >          // painCave.isFatal = 0;
1144 >          // painCave.severity = OPENMD_INFO;
1145 >          // simError();
1146   #ifdef IS_MPI
1147          }
1148   #endif
1149          
1150 <        switch(rnemdType_) {
1151 <        case rnemdKineticScale :
1150 >        switch(rnemdFluxType_) {
1151 >        case rnemdKE :
1152            x = bestPair.first;
1153            y = bestPair.first;
1154            z = bestPair.second;
1155            break;
1156 <        case rnemdPxScale :
1156 >        case rnemdPx :
1157            x = c;
1158            y = bestPair.first;
1159            z = bestPair.second;
1160            break;
1161 <        case rnemdPyScale :
1161 >        case rnemdPy :
1162            x = bestPair.first;
1163            y = c;
1164            z = bestPair.second;
1165            break;
1166 <        case rnemdPzScale :
1166 >        case rnemdPz :
1167            x = bestPair.first;
1168            y = bestPair.second;
1169            z = c;
# Line 1089 | Line 1192 | namespace OpenMD {
1192            (*sdi)->setVel(vel);
1193          }
1194          successfulScale = true;
1195 <        exchangeSum_ += targetFlux_;
1195 >        switch(rnemdFluxType_) {
1196 >        case rnemdKE :
1197 >          kineticExchange_ += kineticTarget_;
1198 >          break;
1199 >        case rnemdPx :
1200 >        case rnemdPy :
1201 >        case rnemdPz :
1202 >          momentumExchange_ += momentumTarget_;
1203 >          break;          
1204 >        default :
1205 >          break;
1206 >        }      
1207        }
1208      }
1209      if (successfulScale != true) {
1210        sprintf(painCave.errMsg,
1211 <              "RNEMD: exchange NOT performed!\n");
1211 >              "RNEMD::doNIVS exchange NOT performed - roots that solve\n"
1212 >              "\tthe constraint equations may not exist or there may be\n"
1213 >              "\tno selected objects in one or both slabs.\n");
1214        painCave.isFatal = 0;
1215        painCave.severity = OPENMD_INFO;
1216        simError();        
# Line 1102 | Line 1218 | namespace OpenMD {
1218      }
1219    }
1220  
1221 <  void RNEMD::doShiftScale() {
1222 <
1221 >  void RNEMD::doVSS() {
1222 >    if (!doRNEMD_) return;
1223      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1224 +    RealType time = currentSnap_->getTime();    
1225      Mat3x3d hmat = currentSnap_->getHmat();
1226  
1227      seleMan_.setSelectionSet(evaluator_.evaluate());
# Line 1121 | Line 1238 | namespace OpenMD {
1238      Vector3d Pc(V3Zero);
1239      RealType Mc = 0.0;
1240      RealType Kc = 0.0;
1241 +    
1242  
1243      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1244           sd = seleMan_.nextSelected(selei)) {
# Line 1135 | Line 1253 | namespace OpenMD {
1253          currentSnap_->wrapVector(pos);
1254  
1255        // which bin is this stuntdouble in?
1256 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1257 <
1258 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
1259 <
1142 <      // if we're in bin 0 or the middleBin
1143 <      if (binNo == 0 || binNo == midBin_) {
1256 >      bool inA = inSlabA(pos);
1257 >      bool inB = inSlabB(pos);
1258 >      
1259 >      if (inA || inB) {
1260          
1261          RealType mass = sd->getMass();
1262          Vector3d vel = sd->getVel();
1263        
1264 <        if (binNo == 0) {
1264 >        if (inA) {
1265            hotBin.push_back(sd);
1266            //std::cerr << "before, velocity = " << vel << endl;
1267            Ph += mass * vel;
1268            //std::cerr << "after, velocity = " << vel << endl;
1269            Mh += mass;
1270            Kh += mass * vel.lengthSquare();
1271 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1271 >          if (rnemdFluxType_ == rnemdFullKE) {
1272              if (sd->isDirectional()) {
1273                Vector3d angMom = sd->getJ();
1274                Mat3x3d I = sd->getI();
# Line 1174 | Line 1290 | namespace OpenMD {
1290            Pc += mass * vel;
1291            Mc += mass;
1292            Kc += mass * vel.lengthSquare();
1293 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1293 >          if (rnemdFluxType_ == rnemdFullKE) {
1294              if (sd->isDirectional()) {
1295                Vector3d angMom = sd->getJ();
1296                Mat3x3d I = sd->getI();
# Line 1198 | Line 1314 | namespace OpenMD {
1314      Kh *= 0.5;
1315      Kc *= 0.5;
1316  
1317 <    std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1318 <              << "\tKc= " << Kc << endl;
1319 <    std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1320 <
1317 >    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1318 >    //        << "\tKc= " << Kc << endl;
1319 >    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1320 >    
1321   #ifdef IS_MPI
1322      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1323      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
# Line 1214 | Line 1330 | namespace OpenMD {
1330      bool successfulExchange = false;
1331      if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1332        Vector3d vc = Pc / Mc;
1333 <      Vector3d ac = njzp_ / Mc + vc;
1334 <      RealType cNumerator = Kc - targetJzKE_ - 0.5 * Mc * ac.lengthSquare();
1333 >      Vector3d ac = -momentumTarget_ / Mc + vc;
1334 >      Vector3d acrec = -momentumTarget_ / Mc;
1335 >      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1336        if (cNumerator > 0.0) {
1337          RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1338          if (cDenominator > 0.0) {
1339            RealType c = sqrt(cNumerator / cDenominator);
1340            if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1341              Vector3d vh = Ph / Mh;
1342 <            Vector3d ah = jzp_ / Mh + vh;
1343 <            RealType hNumerator = Kh + targetJzKE_
1342 >            Vector3d ah = momentumTarget_ / Mh + vh;
1343 >            Vector3d ahrec = momentumTarget_ / Mh;
1344 >            RealType hNumerator = Kh + kineticTarget_
1345                - 0.5 * Mh * ah.lengthSquare();
1346              if (hNumerator > 0.0) {
1347                RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1348                if (hDenominator > 0.0) {
1349                  RealType h = sqrt(hNumerator / hDenominator);
1350                  if ((h > 0.9) && (h < 1.1)) {
1351 <                  std::cerr << "cold slab scaling coefficient: " << c << "\n";
1352 <                  std::cerr << "hot slab scaling coefficient: " << h << "\n";
1351 >                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1352 >                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1353                    vector<StuntDouble*>::iterator sdi;
1354                    Vector3d vel;
1355                    for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1356                      //vel = (*sdi)->getVel();
1357                      vel = ((*sdi)->getVel() - vc) * c + ac;
1358                      (*sdi)->setVel(vel);
1359 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1359 >                    if (rnemdFluxType_ == rnemdFullKE) {
1360                        if ((*sdi)->isDirectional()) {
1361                          Vector3d angMom = (*sdi)->getJ() * c;
1362                          (*sdi)->setJ(angMom);
# Line 1249 | Line 1367 | namespace OpenMD {
1367                      //vel = (*sdi)->getVel();
1368                      vel = ((*sdi)->getVel() - vh) * h + ah;
1369                      (*sdi)->setVel(vel);
1370 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1370 >                    if (rnemdFluxType_ == rnemdFullKE) {
1371                        if ((*sdi)->isDirectional()) {
1372                          Vector3d angMom = (*sdi)->getJ() * h;
1373                          (*sdi)->setJ(angMom);
# Line 1257 | Line 1375 | namespace OpenMD {
1375                      }
1376                    }
1377                    successfulExchange = true;
1378 <                  exchangeSum_ += targetFlux_;
1379 <                  // 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.
1378 >                  kineticExchange_ += kineticTarget_;
1379 >                  momentumExchange_ += momentumTarget_;
1380                  }
1381                }
1382              }
# Line 1270 | Line 1386 | namespace OpenMD {
1386      }
1387      if (successfulExchange != true) {
1388        sprintf(painCave.errMsg,
1389 <              "RNEMD: exchange NOT performed!\n");
1389 >              "RNEMD::doVSS exchange NOT performed - roots that solve\n"
1390 >              "\tthe constraint equations may not exist or there may be\n"
1391 >              "\tno selected objects in one or both slabs.\n");
1392        painCave.isFatal = 0;
1393        painCave.severity = OPENMD_INFO;
1394        simError();        
# Line 1279 | Line 1397 | namespace OpenMD {
1397    }
1398  
1399    void RNEMD::doRNEMD() {
1400 <
1401 <    switch(rnemdType_) {
1402 <    case rnemdKineticScale :
1403 <    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 :
1400 >    if (!doRNEMD_) return;
1401 >    trialCount_++;
1402 >    switch(rnemdMethod_) {
1403 >    case rnemdSwap:
1404        doSwap();
1405        break;
1406 <    case rnemdShiftScaleV :
1407 <    case rnemdShiftScaleVAM :
1300 <      doShiftScale();
1406 >    case rnemdNIVS:
1407 >      doNIVS();
1408        break;
1409 <    case rnemdUnknown :
1409 >    case rnemdVSS:
1410 >      doVSS();
1411 >      break;
1412 >    case rnemdUnkownMethod:
1413      default :
1414        break;
1415      }
1416    }
1417  
1418    void RNEMD::collectData() {
1419 <
1419 >    if (!doRNEMD_) return;
1420      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1421      Mat3x3d hmat = currentSnap_->getHmat();
1422  
1423 +    areaAccumulator_->add(currentSnap_->getXYarea());
1424 +
1425      seleMan_.setSelectionSet(evaluator_.evaluate());
1426  
1427      int selei;
1428      StuntDouble* sd;
1429      int idx;
1430  
1431 +    vector<RealType> binMass(nBins_, 0.0);
1432 +    vector<RealType> binPx(nBins_, 0.0);
1433 +    vector<RealType> binPy(nBins_, 0.0);
1434 +    vector<RealType> binPz(nBins_, 0.0);
1435 +    vector<RealType> binKE(nBins_, 0.0);
1436 +    vector<int> binDOF(nBins_, 0);
1437 +    vector<int> binCount(nBins_, 0);
1438 +
1439      // alternative approach, track all molecules instead of only those
1440      // selected for scaling/swapping:
1441      /*
1442      SimInfo::MoleculeIterator miter;
1443      vector<StuntDouble*>::iterator iiter;
1444      Molecule* mol;
1445 <    StuntDouble* integrableObject;
1445 >    StuntDouble* sd;
1446      for (mol = info_->beginMolecule(miter); mol != NULL;
1447 <         mol = info_->nextMolecule(miter))
1448 <      integrableObject is essentially sd
1449 <        for (integrableObject = mol->beginIntegrableObject(iiter);
1450 <             integrableObject != NULL;
1451 <             integrableObject = mol->nextIntegrableObject(iiter))
1447 >      mol = info_->nextMolecule(miter))
1448 >      sd is essentially sd
1449 >        for (sd = mol->beginIntegrableObject(iiter);
1450 >             sd != NULL;
1451 >             sd = mol->nextIntegrableObject(iiter))
1452      */
1453      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1454           sd = seleMan_.nextSelected(selei)) {
# Line 1341 | Line 1461 | namespace OpenMD {
1461        
1462        if (usePeriodicBoundaryConditions_)
1463          currentSnap_->wrapVector(pos);
1464 <      
1464 >
1465 >
1466        // which bin is this stuntdouble in?
1467        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1468 <      
1469 <      int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
1470 <        rnemdLogWidth_;
1471 <      // no symmetrization allowed due to arbitary rnemdLogWidth_
1472 <      /*
1473 <      if (rnemdLogWidth_ == midBin_ + 1)
1474 <        if (binNo > midBin_)
1354 <          binNo = nBins_ - binNo;
1355 <      */
1356 <      RealType mass = sd->getMass();
1357 <      mHist_[binNo] += mass;
1358 <      Vector3d vel = sd->getVel();
1359 <      RealType value;
1360 <      //RealType xVal, yVal, zVal;
1468 >      // Shift molecules by half a box to have bins start at 0
1469 >      // The modulo operator is used to wrap the case when we are
1470 >      // beyond the end of the bins back to the beginning.
1471 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1472 >    
1473 >      RealType mass = sd->getMass();
1474 >      Vector3d vel = sd->getVel();
1475  
1476 <      if (outputTemp_) {
1477 <        value = mass * vel.lengthSquare();
1478 <        tempCount_[binNo] += 3;
1479 <        if (sd->isDirectional()) {
1480 <          Vector3d angMom = sd->getJ();
1481 <          Mat3x3d I = sd->getI();
1482 <          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 <      }
1476 >      binCount[binNo]++;
1477 >      binMass[binNo] += mass;
1478 >      binPx[binNo] += mass*vel.x();
1479 >      binPy[binNo] += mass*vel.y();
1480 >      binPz[binNo] += mass*vel.z();
1481 >      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1482 >      binDOF[binNo] += 3;
1483  
1484 <      if (output3DTemp_) {
1485 <        value = mass * vel.x() * vel.x();
1486 <        xTempHist_[binNo] += value;
1487 <        value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1488 <          / PhysicalConstants::kb;
1489 <        yTempHist_[binNo] += value;
1490 <        value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1491 <          / PhysicalConstants::kb;
1492 <        zTempHist_[binNo] += value;
1493 <        xyzTempCount_[binNo]++;
1484 >      if (sd->isDirectional()) {
1485 >        Vector3d angMom = sd->getJ();
1486 >        Mat3x3d I = sd->getI();
1487 >        if (sd->isLinear()) {
1488 >          int i = sd->linearAxis();
1489 >          int j = (i + 1) % 3;
1490 >          int k = (i + 2) % 3;
1491 >          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
1492 >                                 angMom[k] * angMom[k] / I(k, k));
1493 >          binDOF[binNo] += 2;
1494 >        } else {
1495 >          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
1496 >                                 angMom[1] * angMom[1] / I(1, 1) +
1497 >                                 angMom[2] * angMom[2] / I(2, 2));
1498 >          binDOF[binNo] += 3;
1499 >        }
1500        }
1501 <      if (outputRotTemp_) {
1502 <        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 <      }
1501 >    }
1502 >    
1503  
1504 + #ifdef IS_MPI
1505 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
1506 +                              nBins_, MPI::INT, MPI::SUM);
1507 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
1508 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1509 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
1510 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1511 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
1512 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1513 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
1514 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1515 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
1516 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1517 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
1518 +                              nBins_, MPI::INT, MPI::SUM);
1519 + #endif
1520 +
1521 +    Vector3d vel;
1522 +    RealType den;
1523 +    RealType temp;
1524 +    RealType z;
1525 +    for (int i = 0; i < nBins_; i++) {
1526 +      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
1527 +      vel.x() = binPx[i] / binMass[i];
1528 +      vel.y() = binPy[i] / binMass[i];
1529 +      vel.z() = binPz[i] / binMass[i];
1530 +      den = binCount[i] * nBins_ / currentSnap_->getVolume();
1531 +      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
1532 +                               PhysicalConstants::energyConvert);
1533 +
1534 +      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
1535 +        if(outputMask_[j]) {
1536 +          switch(j) {
1537 +          case Z:
1538 +            (data_[j].accumulator[i])->add(z);
1539 +            break;
1540 +          case TEMPERATURE:
1541 +            data_[j].accumulator[i]->add(temp);
1542 +            break;
1543 +          case VELOCITY:
1544 +            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
1545 +            break;
1546 +          case DENSITY:
1547 +            data_[j].accumulator[i]->add(den);
1548 +            break;
1549 +          }
1550 +        }
1551 +      }
1552      }
1553    }
1554  
1555    void RNEMD::getStarted() {
1556 +    if (!doRNEMD_) return;
1557      collectData();
1558 <    /*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();
1558 >    writeOutputFile();
1559    }
1560  
1561 <  void RNEMD::getStatus() {
1562 <
1563 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1564 <    Stats& stat = currentSnap_->statData;
1565 <    RealType time = currentSnap_->getTime();
1566 <
1567 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1568 <    //or to be more meaningful, define another item as exchangeSum_ / time
1569 <    int j;
1570 <
1561 >  void RNEMD::parseOutputFileFormat(const std::string& format) {
1562 >    if (!doRNEMD_) return;
1563 >    StringTokenizer tokenizer(format, " ,;|\t\n\r");
1564 >    
1565 >    while(tokenizer.hasMoreTokens()) {
1566 >      std::string token(tokenizer.nextToken());
1567 >      toUpper(token);
1568 >      OutputMapType::iterator i = outputMap_.find(token);
1569 >      if (i != outputMap_.end()) {
1570 >        outputMask_.set(i->second);
1571 >      } else {
1572 >        sprintf( painCave.errMsg,
1573 >                 "RNEMD::parseOutputFileFormat: %s is not a recognized\n"
1574 >                 "\toutputFileFormat keyword.\n", token.c_str() );
1575 >        painCave.isFatal = 0;
1576 >        painCave.severity = OPENMD_ERROR;
1577 >        simError();            
1578 >      }
1579 >    }  
1580 >  }
1581 >  
1582 >  void RNEMD::writeOutputFile() {
1583 >    if (!doRNEMD_) return;
1584 >    
1585   #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
1586      // If we're the root node, should we print out the results
1587      int worldRank = MPI::COMM_WORLD.Get_rank();
1588      if (worldRank == 0) {
1589   #endif
1590 +      rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
1591 +      
1592 +      if( !rnemdFile_ ){        
1593 +        sprintf( painCave.errMsg,
1594 +                 "Could not open \"%s\" for RNEMD output.\n",
1595 +                 rnemdFileName_.c_str());
1596 +        painCave.isFatal = 1;
1597 +        simError();
1598 +      }
1599  
1600 <      if (outputTemp_) {
1601 <        tempLog_ << time;
1602 <        for (j = 0; j < rnemdLogWidth_; j++) {
1603 <          tempLog_ << "\t" << tempHist_[j] / (RealType)tempCount_[j];
1604 <        }
1605 <        tempLog_ << endl;
1600 >      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1601 >
1602 >      RealType time = currentSnap_->getTime();
1603 >      RealType avgArea;
1604 >      areaAccumulator_->getAverage(avgArea);
1605 >      RealType Jz = kineticExchange_ / (2.0 * time * avgArea);
1606 >      Vector3d JzP = momentumExchange_ / (2.0 * time * avgArea);      
1607 >
1608 >      rnemdFile_ << "#######################################################\n";
1609 >      rnemdFile_ << "# RNEMD {\n";
1610 >
1611 >      map<string, RNEMDMethod>::iterator mi;
1612 >      for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
1613 >        if ( (*mi).second == rnemdMethod_)
1614 >          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
1615        }
1616 <      if (outputVx_) {
1617 <        vxzLog_ << time;
1618 <        for (j = 0; j < rnemdLogWidth_; j++) {
1619 <          vxzLog_ << "\t" << pxzHist_[j] / mHist_[j];
1513 <        }
1514 <        vxzLog_ << endl;
1616 >      map<string, RNEMDFluxType>::iterator fi;
1617 >      for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
1618 >        if ( (*fi).second == rnemdFluxType_)
1619 >          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
1620        }
1621 <      if (outputVy_) {
1622 <        vyzLog_ << time;
1518 <        for (j = 0; j < rnemdLogWidth_; j++) {
1519 <          vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1520 <        }
1521 <        vyzLog_ << endl;
1522 <      }
1621 >      
1622 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
1623  
1624 <      if (output3DTemp_) {
1625 <        RealType temp;
1626 <        xTempLog_ << time;
1627 <        for (j = 0; j < rnemdLogWidth_; j++) {
1628 <          if (outputVx_)
1629 <            xTempHist_[j] -= pxzHist_[j] * pxzHist_[j] / mHist_[j];
1630 <          temp = xTempHist_[j] / (RealType)xyzTempCount_[j]
1631 <            / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1632 <          xTempLog_ << "\t" << temp;
1624 >      rnemdFile_ << "#    objectSelection = \""
1625 >                 << rnemdObjectSelection_ << "\";\n";
1626 >      rnemdFile_ << "#    slabWidth = " << slabWidth_ << ";\n";
1627 >      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << ";\n";
1628 >      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << ";\n";
1629 >      rnemdFile_ << "# }\n";
1630 >      rnemdFile_ << "#######################################################\n";
1631 >      rnemdFile_ << "# RNEMD report:\n";      
1632 >      rnemdFile_ << "#     running time = " << time << " fs\n";
1633 >      rnemdFile_ << "#     target flux:\n";
1634 >      rnemdFile_ << "#         kinetic = " << kineticFlux_ << "\n";
1635 >      rnemdFile_ << "#         momentum = " << momentumFluxVector_ << "\n";
1636 >      rnemdFile_ << "#     target one-time exchanges:\n";
1637 >      rnemdFile_ << "#         kinetic = " << kineticTarget_ << "\n";
1638 >      rnemdFile_ << "#         momentum = " << momentumTarget_ << "\n";
1639 >      rnemdFile_ << "#     actual exchange totals:\n";
1640 >      rnemdFile_ << "#         kinetic = " << kineticExchange_ << "\n";
1641 >      rnemdFile_ << "#         momentum = " << momentumExchange_  << "\n";
1642 >      rnemdFile_ << "#     actual flux:\n";
1643 >      rnemdFile_ << "#         kinetic = " << Jz << "\n";
1644 >      rnemdFile_ << "#         momentum = " << JzP  << "\n";
1645 >      rnemdFile_ << "#     exchange statistics:\n";
1646 >      rnemdFile_ << "#         attempted = " << trialCount_ << "\n";
1647 >      rnemdFile_ << "#         failed = " << failTrialCount_ << "\n";    
1648 >      if (rnemdMethod_ == rnemdNIVS) {
1649 >        rnemdFile_ << "#         NIVS root-check errors = "
1650 >                   << failRootCount_ << "\n";
1651 >      }
1652 >      rnemdFile_ << "#######################################################\n";
1653 >      
1654 >      
1655 >      
1656 >      //write title
1657 >      rnemdFile_ << "#";
1658 >      for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1659 >        if (outputMask_[i]) {
1660 >          rnemdFile_ << "\t" << data_[i].title <<
1661 >            "(" << data_[i].units << ")";
1662          }
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;
1663        }
1664 <      if (outputRotTemp_) {
1665 <        rotTempLog_ << time;
1666 <        for (j = 0; j < rnemdLogWidth_; j++) {
1667 <          rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1668 <        }
1669 <        rotTempLog_ << endl;
1670 <      }
1664 >      rnemdFile_ << std::endl;
1665 >      
1666 >      rnemdFile_.precision(8);
1667 >      
1668 >      for (unsigned int j = 0; j < nBins_; j++) {        
1669 >        
1670 >        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1671 >          if (outputMask_[i]) {
1672 >            if (data_[i].dataType == "RealType")
1673 >              writeReal(i,j);
1674 >            else if (data_[i].dataType == "Vector3d")
1675 >              writeVector(i,j);
1676 >            else {
1677 >              sprintf( painCave.errMsg,
1678 >                       "RNEMD found an unknown data type for: %s ",
1679 >                       data_[i].title.c_str());
1680 >              painCave.isFatal = 1;
1681 >              simError();
1682 >            }
1683 >          }
1684 >        }
1685 >        rnemdFile_ << std::endl;
1686 >        
1687 >      }        
1688  
1689 +      rnemdFile_ << "#######################################################\n";
1690 +      rnemdFile_ << "# Standard Deviations in those quantities follow:\n";
1691 +      rnemdFile_ << "#######################################################\n";
1692 +
1693 +
1694 +      for (unsigned int j = 0; j < nBins_; j++) {        
1695 +        rnemdFile_ << "#";
1696 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1697 +          if (outputMask_[i]) {
1698 +            if (data_[i].dataType == "RealType")
1699 +              writeRealStdDev(i,j);
1700 +            else if (data_[i].dataType == "Vector3d")
1701 +              writeVectorStdDev(i,j);
1702 +            else {
1703 +              sprintf( painCave.errMsg,
1704 +                       "RNEMD found an unknown data type for: %s ",
1705 +                       data_[i].title.c_str());
1706 +              painCave.isFatal = 1;
1707 +              simError();
1708 +            }
1709 +          }
1710 +        }
1711 +        rnemdFile_ << std::endl;
1712 +        
1713 +      }        
1714 +      
1715 +      rnemdFile_.flush();
1716 +      rnemdFile_.close();
1717 +      
1718   #ifdef IS_MPI
1719      }
1720   #endif
1721 <
1722 <    for (j = 0; j < rnemdLogWidth_; j++) {
1723 <      mHist_[j] = 0.0;
1721 >    
1722 >  }
1723 >  
1724 >  void RNEMD::writeReal(int index, unsigned int bin) {
1725 >    if (!doRNEMD_) return;
1726 >    assert(index >=0 && index < ENDINDEX);
1727 >    assert(bin < nBins_);
1728 >    RealType s;
1729 >    
1730 >    data_[index].accumulator[bin]->getAverage(s);
1731 >    
1732 >    if (! isinf(s) && ! isnan(s)) {
1733 >      rnemdFile_ << "\t" << s;
1734 >    } else{
1735 >      sprintf( painCave.errMsg,
1736 >               "RNEMD detected a numerical error writing: %s for bin %d",
1737 >               data_[index].title.c_str(), bin);
1738 >      painCave.isFatal = 1;
1739 >      simError();
1740 >    }    
1741 >  }
1742 >  
1743 >  void RNEMD::writeVector(int index, unsigned int bin) {
1744 >    if (!doRNEMD_) return;
1745 >    assert(index >=0 && index < ENDINDEX);
1746 >    assert(bin < nBins_);
1747 >    Vector3d s;
1748 >    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
1749 >    if (isinf(s[0]) || isnan(s[0]) ||
1750 >        isinf(s[1]) || isnan(s[1]) ||
1751 >        isinf(s[2]) || isnan(s[2]) ) {      
1752 >      sprintf( painCave.errMsg,
1753 >               "RNEMD detected a numerical error writing: %s for bin %d",
1754 >               data_[index].title.c_str(), bin);
1755 >      painCave.isFatal = 1;
1756 >      simError();
1757 >    } else {
1758 >      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1759      }
1760 <    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 <      }
1760 >  }  
1761  
1762 <    if (output3DTemp_)
1763 <      for (j = 0; j < rnemdLogWidth_; j++) {
1764 <        xTempHist_[j] = 0.0;
1765 <        yTempHist_[j] = 0.0;
1766 <        zTempHist_[j] = 0.0;
1767 <        xyzTempCount_[j] = 0;
1768 <      }
1769 <    if (outputRotTemp_)
1770 <      for (j = 0; j < rnemdLogWidth_; j++) {
1771 <        rotTempCount_[j] = 0;
1772 <        rotTempHist_[j] = 0.0;
1773 <      }
1762 >  void RNEMD::writeRealStdDev(int index, unsigned int bin) {
1763 >    if (!doRNEMD_) return;
1764 >    assert(index >=0 && index < ENDINDEX);
1765 >    assert(bin < nBins_);
1766 >    RealType s;
1767 >    
1768 >    data_[index].accumulator[bin]->getStdDev(s);
1769 >    
1770 >    if (! isinf(s) && ! isnan(s)) {
1771 >      rnemdFile_ << "\t" << s;
1772 >    } else{
1773 >      sprintf( painCave.errMsg,
1774 >               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1775 >               data_[index].title.c_str(), bin);
1776 >      painCave.isFatal = 1;
1777 >      simError();
1778 >    }    
1779    }
1780 +  
1781 +  void RNEMD::writeVectorStdDev(int index, unsigned int bin) {
1782 +    if (!doRNEMD_) return;
1783 +    assert(index >=0 && index < ENDINDEX);
1784 +    assert(bin < nBins_);
1785 +    Vector3d s;
1786 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getStdDev(s);
1787 +    if (isinf(s[0]) || isnan(s[0]) ||
1788 +        isinf(s[1]) || isnan(s[1]) ||
1789 +        isinf(s[2]) || isnan(s[2]) ) {      
1790 +      sprintf( painCave.errMsg,
1791 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1792 +               data_[index].title.c_str(), bin);
1793 +      painCave.isFatal = 1;
1794 +      simError();
1795 +    } else {
1796 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1797 +    }
1798 +  }  
1799   }
1800  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines