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/rnemd/RNEMD.cpp (file contents):
Revision 1769 by gezelter, Mon Jul 9 14:15:52 2012 UTC vs.
Revision 1776 by gezelter, Thu Aug 9 15:52:59 2012 UTC

# 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  
# Line 72 | Line 69 | namespace OpenMD {
69      Globals * simParams = info->getSimParams();
70      RNEMDParameters* rnemdParams = simParams->getRNEMDParameters();
71  
72 <    stringToEnumMap_["KineticSwap"] = rnemdKineticSwap;
73 <    stringToEnumMap_["KineticScale"] = rnemdKineticScale;
77 <    stringToEnumMap_["KineticScaleVAM"] = rnemdKineticScaleVAM;
78 <    stringToEnumMap_["KineticScaleAM"] = rnemdKineticScaleAM;
79 <    stringToEnumMap_["PxScale"] = rnemdPxScale;
80 <    stringToEnumMap_["PyScale"] = rnemdPyScale;
81 <    stringToEnumMap_["PzScale"] = rnemdPzScale;
82 <    stringToEnumMap_["Px"] = rnemdPx;
83 <    stringToEnumMap_["Py"] = rnemdPy;
84 <    stringToEnumMap_["Pz"] = rnemdPz;
85 <    stringToEnumMap_["ShiftScaleV"] = rnemdShiftScaleV;
86 <    stringToEnumMap_["ShiftScaleVAM"] = rnemdShiftScaleVAM;
87 <    stringToEnumMap_["Unknown"] = rnemdUnknown;
72 >    doRNEMD_ = rnemdParams->getUseRNEMD();
73 >    if (!doRNEMD_) return;
74  
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  
# Line 100 | 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 113 | Line 290 | namespace OpenMD {
290        painCave.severity = OPENMD_WARNING;
291        simError();
292      }
116    
117    const string st = rnemdParams->getExchangeType();
293  
294 <    map<string, RNEMDTypeEnum>::iterator i;
120 <    i = stringToEnumMap_.find(st);
121 <    rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
122 <    if (rnemdType_ == rnemdUnknown) {
123 <      sprintf(painCave.errMsg,
124 <              "RNEMD: The current RNEMD_exchangeType,\n"
125 <              "\t\t%s\n"
126 <              "\tis not one of the recognized exchange types.\n",
127 <              st.c_str());
128 <      painCave.isFatal = 1;
129 <      painCave.severity = OPENMD_ERROR;
130 <      simError();
131 <    }
132 <    
133 <    outputTemp_ = false;
134 <    if (rnemdParams->haveOutputTemperature()) {
135 <      outputTemp_ = rnemdParams->getOutputTemperature();
136 <    } else if ((rnemdType_ == rnemdKineticSwap) ||
137 <               (rnemdType_ == rnemdKineticScale) ||
138 <               (rnemdType_ == rnemdKineticScaleVAM) ||
139 <               (rnemdType_ == rnemdKineticScaleAM)) {
140 <      outputTemp_ = true;
141 <    }
142 <    outputVx_ = false;
143 <    if (rnemdParams->haveOutputVx()) {
144 <      outputVx_ = rnemdParams->getOutputVx();
145 <    } else if ((rnemdType_ == rnemdPx) || (rnemdType_ == rnemdPxScale)) {
146 <      outputVx_ = true;
147 <    }
148 <    outputVy_ = false;
149 <    if (rnemdParams->haveOutputVy()) {
150 <      outputVy_ = rnemdParams->getOutputVy();
151 <    } else if ((rnemdType_ == rnemdPy) || (rnemdType_ == rnemdPyScale)) {
152 <      outputVy_ = true;
153 <    }
154 <    output3DTemp_ = false;
155 <    if (rnemdParams->haveOutputXyzTemperature()) {
156 <      output3DTemp_ = rnemdParams->getOutputXyzTemperature();
157 <    }
158 <    outputRotTemp_ = false;
159 <    if (rnemdParams->haveOutputRotTemperature()) {
160 <      outputRotTemp_ = rnemdParams->getOutputRotTemperature();
161 <    }
162 <    // James put this in.
163 <    outputDen_ = false;
164 <    if (rnemdParams->haveOutputDen()) {
165 <      outputDen_ = rnemdParams->getOutputDen();
166 <    }
167 <    outputAh_ = false;
168 <    if (rnemdParams->haveOutputAh()) {
169 <      outputAh_ = rnemdParams->getOutputAh();
170 <    }    
171 <    outputVz_ = false;
172 <    if (rnemdParams->haveOutputVz()) {
173 <      outputVz_ = rnemdParams->getOutputVz();
174 <    } else if ((rnemdType_ == rnemdPz) || (rnemdType_ == rnemdPzScale)) {
175 <      outputVz_ = true;
176 <    }
177 <    
294 >    areaAccumulator_ = new Accumulator();
295  
296 < #ifdef IS_MPI
180 <    if (worldRank == 0) {
181 < #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_) {
195 <        rnemdFileName = "velocityY.log";
196 <        vyzLog_.open(rnemdFileName.c_str());
197 <      }
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_) {
208 <        rnemdFileName = "temperatureR.log";
209 <        rotTempLog_.open(rnemdFileName.c_str());
210 <      }
211 <      
212 <      //James put this in
213 <      if (outputDen_) {
214 <        rnemdFileName = "Density.log";
215 <        denLog_.open(rnemdFileName.c_str());
216 <      }
217 <      if (outputAh_) {
218 <        rnemdFileName = "Ah.log";
219 <        AhLog_.open(rnemdFileName.c_str());
220 <      }
221 <      if (outputVz_) {
222 <        rnemdFileName = "velocityZ.log";
223 <        vzzLog_.open(rnemdFileName.c_str());
224 <      }
225 <      logFrameCount_ = 0;
226 < #ifdef IS_MPI
227 <    }
228 < #endif
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 <    set_RNEMD_exchange_time(rnemdParams->getExchangeTime());
330 <    set_RNEMD_nBins(rnemdParams->getNbins());
331 <    midBin_ = nBins_ / 2;
332 <    if (rnemdParams->haveBinShift()) {
333 <      if (rnemdParams->getBinShift()) {
334 <        zShift_ = 0.5 / (RealType)(nBins_);
335 <      } else {
336 <        zShift_ = 0.0;
337 <      }
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 >    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
245 <    if (rnemdParams->haveLogWidth()) {
246 <      set_RNEMD_logWidth(rnemdParams->getLogWidth());
247 <      /*arbitary rnemdLogWidth_, no checking;
248 <      if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
249 <        cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
250 <        cerr << "Automaically set back to default.\n";
251 <        rnemdLogWidth_ = nBins_;
252 <      }*/
372 >      
373 >    if (hasOutputFileName) {
374 >      rnemdFileName_ = rnemdParams->getOutputFileName();
375      } else {
376 <      set_RNEMD_logWidth(nBins_);
377 <    }
256 <    tempHist_.resize(rnemdLogWidth_, 0.0);
257 <    tempCount_.resize(rnemdLogWidth_, 0);
258 <    pxzHist_.resize(rnemdLogWidth_, 0.0);
259 <    //vxzCount_.resize(rnemdLogWidth_, 0);
260 <    pyzHist_.resize(rnemdLogWidth_, 0.0);
261 <    //vyzCount_.resize(rnemdLogWidth_, 0);
376 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
377 >    }          
378  
379 <    mHist_.resize(rnemdLogWidth_, 0.0);
264 <    xTempHist_.resize(rnemdLogWidth_, 0.0);
265 <    yTempHist_.resize(rnemdLogWidth_, 0.0);
266 <    zTempHist_.resize(rnemdLogWidth_, 0.0);
267 <    xyzTempCount_.resize(rnemdLogWidth_, 0);
268 <    rotTempHist_.resize(rnemdLogWidth_, 0.0);
269 <    rotTempCount_.resize(rnemdLogWidth_, 0);
270 <    // James put this in
271 <    DenHist_.resize(rnemdLogWidth_, 0.0);
272 <    pzzHist_.resize(rnemdLogWidth_, 0.0);
379 >    exchangeTime_ = rnemdParams->getExchangeTime();
380  
381 <    set_RNEMD_exchange_total(0.0);
382 <    if (rnemdParams->haveTargetFlux()) {
383 <      set_RNEMD_target_flux(rnemdParams->getTargetFlux());
384 <    } else {
385 <      set_RNEMD_target_flux(0.0);
386 <    }
387 <    if (rnemdParams->haveTargetJzKE()) {
281 <      set_RNEMD_target_JzKE(rnemdParams->getTargetJzKE());
282 <    } else {
283 <      set_RNEMD_target_JzKE(0.0);
284 <    }
285 <    if (rnemdParams->haveTargetJzpx()) {
286 <      set_RNEMD_target_jzpx(rnemdParams->getTargetJzpx());
287 <    } else {
288 <      set_RNEMD_target_jzpx(0.0);
289 <    }
290 <    jzp_.x() = targetJzpx_;
291 <    njzp_.x() = -targetJzpx_;
292 <    if (rnemdParams->haveTargetJzpy()) {
293 <      set_RNEMD_target_jzpy(rnemdParams->getTargetJzpy());
294 <    } else {
295 <      set_RNEMD_target_jzpy(0.0);
296 <    }
297 <    jzp_.y() = targetJzpy_;
298 <    njzp_.y() = -targetJzpy_;
299 <    if (rnemdParams->haveTargetJzpz()) {
300 <      set_RNEMD_target_jzpz(rnemdParams->getTargetJzpz());
301 <    } else {
302 <      set_RNEMD_target_jzpz(0.0);
303 <    }
304 <    jzp_.z() = targetJzpz_;
305 <    njzp_.z() = -targetJzpz_;
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();
320 <    }    
321 < #endif
322 <  }
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
330      
331      sprintf(painCave.errMsg,
332              "RNEMD: total failed trials: %d\n",
333              failTrialCount_);
334      painCave.isFatal = 0;
335      painCave.severity = OPENMD_INFO;
336      simError();
337      
338      if (outputTemp_) tempLog_.close();
339      if (outputVx_)   vxzLog_.close();
340      if (outputVy_)   vyzLog_.close();
420  
421 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
422 <          rnemdType_ == rnemdPyScale) {
423 <        sprintf(painCave.errMsg,
345 <                "RNEMD: total root-checking warnings: %d\n",
346 <                failRootCount_);
347 <        painCave.isFatal = 0;
348 <        painCave.severity = OPENMD_INFO;
349 <        simError();
350 <      }
351 <      if (output3DTemp_) {
352 <        xTempLog_.close();
353 <        yTempLog_.close();
354 <        zTempLog_.close();
355 <      }
356 <      if (outputRotTemp_) rotTempLog_.close();
357 <      // James put this in
358 <      if (outputDen_) denLog_.close();
359 <      if (outputAh_)  AhLog_.close();
360 <      if (outputVz_)  vzzLog_.close();
421 >      writeOutputFile();
422 >
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 394 | 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?
399 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
400 <
401 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
402 <
403 <
404 <      // if we're in bin 0 or the middleBin
405 <      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 430 | 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 447 | 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 458 | 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 472 | 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 533 | 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 585 | 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 631 | 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 666 | 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 677 | 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 <    }
687 <    
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 728 | 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 <
735 <      // if we're in bin 0 or the middleBin
736 <      if (binNo == 0 || binNo == midBin_) {
737 <        
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 746 | 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();
749          //if (rnemdType_ == rnemdKineticScaleVAM) {
829            if (sd->isDirectional()) {
830              Vector3d angMom = sd->getJ();
831              Mat3x3d I = sd->getI();
# Line 762 | Line 841 | namespace OpenMD {
841                  + angMom[2]*angMom[2]/I(2, 2);
842              }
843            }
844 <          //}
766 <        } else { //midBin_
844 >        } else {
845            coldBin.push_back(sd);
846            Pcx += mass * vel.x();
847            Pcy += mass * vel.y();
# Line 771 | 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();
774          //if (rnemdType_ == rnemdKineticScaleVAM) {
852            if (sd->isDirectional()) {
853              Vector3d angMom = sd->getJ();
854              Mat3x3d I = sd->getI();
# Line 787 | Line 864 | namespace OpenMD {
864                  + angMom[2]*angMom[2]/I(2, 2);
865              }
866            }
790          //}
867          }
868        }
869      }
# Line 801 | Line 877 | namespace OpenMD {
877      Kcz *= 0.5;
878      Kcw *= 0.5;
879  
804    // std::cerr << "Khx= " << Khx << "\tKhy= " << Khy << "\tKhz= " << Khz
805    //        << "\tKhw= " << Khw << "\tKcx= " << Kcx << "\tKcy= " << Kcy
806    //        << "\tKcz= " << Kcz << "\tKcw= " << Kcw << "\n";
807    // std::cerr << "Phx= " << Phx << "\tPhy= " << Phy << "\tPhz= " << Phz
808    //        << "\tPcx= " << Pcx << "\tPcy= " << Pcy << "\tPcz= " <<Pcz<<"\n";
809
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 832 | 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 861 | 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;
877              //vel.x() *= c;
878              //vel.y() *= c;
879              //vel.z() *= c;
948                (*sdi)->setVel(vel);
949              }
950              if ((*sdi)->isDirectional()) {
951                Vector3d angMom = (*sdi)->getJ() * c;
884              //angMom[0] *= c;
885              //angMom[1] *= c;
886              //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 900 | Line 965 | namespace OpenMD {
965              }
966              if ((*sdi)->isDirectional()) {
967                Vector3d angMom = (*sdi)->getJ() * w;
903              //angMom[0] *= w;
904              //angMom[1] *= w;
905              //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 949 | 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 961 | 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 1047 | 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 1075 | 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 1130 | 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 1143 | 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();
# Line 1178 | 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 <
1185 <      // if we're in bin 0 or the middleBin
1186 <      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 1217 | 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 1257 | 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 <      Vector3d acrec = njzp_ / Mc;
1335 <      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 <            Vector3d ahrec = jzp_ / Mh;
1344 <            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();
# Line 1283 | Line 1356 | namespace OpenMD {
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 1294 | 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 1302 | Line 1375 | namespace OpenMD {
1375                      }
1376                    }
1377                    successfulExchange = true;
1378 <                  exchangeSum_ += targetFlux_;
1379 <                  // this is a redundant variable for doShiftScale() so that
1307 <                  // RNEMD can output one exchange quantity needed in a job.
1308 <                  // need a better way to do this.
1309 <                  //cerr << "acx =" << ac.x() << "ahx =" << ah.x() << '\n';
1310 <                  //cerr << "acy =" << ac.y() << "ahy =" << ah.y() << '\n';
1311 <                  //cerr << "acz =" << ac.z() << "ahz =" << ah.z() << '\n';
1312 <                  Asum_ += (ahrec.z() - acrec.z());
1313 <                  Jsum_ += (jzp_.z()*((1/Mh)+(1/Mc)));
1314 <                  AhCount_ = ahrec.z();
1315 <                  if (outputAh_) {
1316 <                    AhLog_ << time << "   ";
1317 <                    AhLog_ << AhCount_;
1318 <                    AhLog_ << endl;
1319 <                  }              
1378 >                  kineticExchange_ += kineticTarget_;
1379 >                  momentumExchange_ += momentumTarget_;
1380                  }
1381                }
1382              }
# Line 1325 | Line 1385 | namespace OpenMD {
1385        }
1386      }
1387      if (successfulExchange != true) {
1388 <      //   sprintf(painCave.errMsg,
1389 <      //              "RNEMD: exchange NOT performed!\n");
1390 <      //   painCave.isFatal = 0;
1391 <      //   painCave.severity = OPENMD_INFO;
1392 <      //   simError();        
1388 >      sprintf(painCave.errMsg,
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();        
1395        failTrialCount_++;
1396      }
1397    }
1398  
1399    void RNEMD::doRNEMD() {
1400 <
1401 <    switch(rnemdType_) {
1402 <    case rnemdKineticScale :
1403 <    case rnemdKineticScaleVAM :
1342 <    case rnemdKineticScaleAM :
1343 <    case rnemdPxScale :
1344 <    case rnemdPyScale :
1345 <    case rnemdPzScale :
1346 <      doScale();
1347 <      break;
1348 <    case rnemdKineticSwap :
1349 <    case rnemdPx :
1350 <    case rnemdPy :
1351 <    case rnemdPz :
1400 >    if (!doRNEMD_) return;
1401 >    trialCount_++;
1402 >    switch(rnemdMethod_) {
1403 >    case rnemdSwap:
1404        doSwap();
1405        break;
1406 <    case rnemdShiftScaleV :
1407 <    case rnemdShiftScaleVAM :
1356 <      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 <    logFrameCount_++;
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:
# Line 1399 | 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 <      /*
1410 <      if (rnemdLogWidth_ == midBin_ + 1)
1411 <        if (binNo > midBin_)
1412 <          binNo = nBins_ - binNo;
1413 <      */
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();
1415      mHist_[binNo] += mass;
1474        Vector3d vel = sd->getVel();
1417      RealType value;
1418      //RealType xVal, yVal, zVal;
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()) {
1483 <            int i = sd->linearAxis();
1484 <            int j = (i + 1) % 3;
1485 <            int k = (i + 2) % 3;
1486 <            value += angMom[j] * angMom[j] / I(j, j) +
1487 <              angMom[k] * angMom[k] / I(k, k);
1488 <            tempCount_[binNo] +=2;
1489 <          } else {
1490 <            value += angMom[0] * angMom[0] / I(0, 0) +
1491 <              angMom[1]*angMom[1]/I(1, 1) +
1492 <              angMom[2]*angMom[2]/I(2, 2);
1493 <            tempCount_[binNo] +=3;
1494 <          }
1495 <        }
1496 <        value = value / PhysicalConstants::energyConvert
1497 <          / PhysicalConstants::kb;//may move to getStatus()
1498 <        tempHist_[binNo] += value;
1499 <      }
1444 <      if (outputVx_) {
1445 <        value = mass * vel[0];
1446 <        //vxzCount_[binNo]++;
1447 <        pxzHist_[binNo] += value;
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 (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 (outputVy_) {
1502 <        value = mass * vel[1];
1451 <        //vyzCount_[binNo]++;
1452 <        pyzHist_[binNo] += value;
1453 <      }
1501 >    }
1502 >    
1503  
1504 <      if (output3DTemp_) {
1505 <        value = mass * vel.x() * vel.x();
1506 <        xTempHist_[binNo] += value;
1507 <        value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1508 <          / PhysicalConstants::kb;
1509 <        yTempHist_[binNo] += value;
1510 <        value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1511 <          / PhysicalConstants::kb;
1512 <        zTempHist_[binNo] += value;
1513 <        xyzTempCount_[binNo]++;
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        }
1466      if (outputRotTemp_) {
1467        if (sd->isDirectional()) {
1468          Vector3d angMom = sd->getJ();
1469          Mat3x3d I = sd->getI();
1470          if (sd->isLinear()) {
1471            int i = sd->linearAxis();
1472            int j = (i + 1) % 3;
1473            int k = (i + 2) % 3;
1474            value = angMom[j] * angMom[j] / I(j, j) +
1475              angMom[k] * angMom[k] / I(k, k);
1476            rotTempCount_[binNo] +=2;
1477          } else {
1478            value = angMom[0] * angMom[0] / I(0, 0) +
1479              angMom[1] * angMom[1] / I(1, 1) +
1480              angMom[2] * angMom[2] / I(2, 2);
1481            rotTempCount_[binNo] +=3;
1482          }
1483        }
1484        value = value / PhysicalConstants::energyConvert
1485          / PhysicalConstants::kb;//may move to getStatus()
1486        rotTempHist_[binNo] += value;
1487      }
1488      // James put this in.
1489      if (outputDen_) {
1490        //value = 1.0;
1491        DenHist_[binNo] += 1;
1492      }
1493      if (outputVz_) {
1494        value = mass * vel[2];
1495        //vyzCount_[binNo]++;
1496        pzzHist_[binNo] += value;
1497      }    
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;
1504 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1505 <    Stats& stat = currentSnap_->statData;
1506 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1507 <    */
1508 <    //may output a header for the log file here
1509 <    getStatus();
1558 >    writeOutputFile();
1559    }
1560  
1561 <  void RNEMD::getStatus() {
1562 <
1563 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1564 <    RealType time = currentSnap_->getTime();
1565 <    //or to be more meaningful, define another item as exchangeSum_ / time
1566 <    int j;
1567 <
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
1520
1521    // all processors have the same number of bins, and STL vectors pack their
1522    // arrays, so in theory, this should be safe:
1523
1524    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &mHist_[0],
1525                              rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1526    if (outputTemp_) {
1527      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempHist_[0],
1528                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1529      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempCount_[0],
1530                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1531    }
1532    if (outputVx_) {
1533      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pxzHist_[0],
1534                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1535      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vxzCount_[0],
1536      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1537    }
1538    if (outputVy_) {
1539      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pyzHist_[0],
1540                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1541      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vyzCount_[0],
1542      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1543    }
1544    if (output3DTemp_) {
1545      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xTempHist_[0],
1546                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1547      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &yTempHist_[0],
1548                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1549      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &zTempHist_[0],
1550                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1551      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xyzTempCount_[0],
1552                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1553    }
1554    if (outputRotTemp_) {
1555      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempHist_[0],
1556                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1557      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempCount_[0],
1558                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1559    }
1560    // James put this in
1561    if (outputDen_) {
1562      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &DenHist_[0],
1563                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1564    }
1565    if (outputAh_) {
1566      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &AhCount_,
1567                                1, MPI::REALTYPE, MPI::SUM);
1568    }
1569    if (outputVz_) {
1570      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pzzHist_[0],
1571                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1572    }
1573    
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];
1590 <        }
1591 <        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;
1595 <        for (j = 0; j < rnemdLogWidth_; j++) {
1596 <          vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1597 <        }
1598 <        vyzLog_ << endl;
1599 <      }
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          }
1663 <        xTempLog_ << endl;
1664 <        yTempLog_ << time;
1665 <        for (j = 0; j < rnemdLogWidth_; j++) {
1666 <          yTempLog_ << "\t" << yTempHist_[j] / (RealType)xyzTempCount_[j];
1663 >      }
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 <        yTempLog_ << endl;
1686 <        zTempLog_ << time;
1687 <        for (j = 0; j < rnemdLogWidth_; j++) {
1688 <          zTempLog_ << "\t" << zTempHist_[j] / (RealType)xyzTempCount_[j];
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 <        zTempLog_ << endl;
1712 <      }
1713 <      if (outputRotTemp_) {
1714 <        rotTempLog_ << time;
1715 <        for (j = 0; j < rnemdLogWidth_; j++) {
1716 <          rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1717 <        }
1628 <        rotTempLog_ << endl;
1629 <      }
1630 <      // James put this in.
1631 <      Mat3x3d hmat = currentSnap_->getHmat();
1632 <      if (outputDen_) {
1633 <        denLog_ << time;
1634 <        for (j = 0; j < rnemdLogWidth_; j++) {
1635 <          
1636 <          RealType binVol = hmat(0,0) * hmat(1,1) * (hmat(2,2) / float(nBins_));
1637 <          denLog_ << "\t" << DenHist_[j] / (float(logFrameCount_) * binVol);
1638 <        }
1639 <        denLog_ << endl;
1640 <      }
1641 <      if (outputVz_) {
1642 <        vzzLog_ << time;
1643 <        for (j = 0; j < rnemdLogWidth_; j++) {
1644 <          vzzLog_ << "\t" << pzzHist_[j] / mHist_[j];
1645 <        }
1646 <        vzzLog_ << endl;
1647 <      }      
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_)
1656 <      for (j = 0; j < rnemdLogWidth_; j++) {
1657 <        tempCount_[j] = 0;
1658 <        tempHist_[j] = 0.0;
1659 <      }
1660 <    if (outputVx_)
1661 <      for (j = 0; j < rnemdLogWidth_; j++) {
1662 <        //pxzCount_[j] = 0;
1663 <        pxzHist_[j] = 0.0;
1664 <      }
1665 <    if (outputVy_)
1666 <      for (j = 0; j < rnemdLogWidth_; j++) {
1667 <        //pyzCount_[j] = 0;
1668 <        pyzHist_[j] = 0.0;
1669 <      }
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;
1676 <        xyzTempCount_[j] = 0;
1677 <      }
1678 <    if (outputRotTemp_)
1679 <      for (j = 0; j < rnemdLogWidth_; j++) {
1680 <        rotTempCount_[j] = 0;
1681 <        rotTempHist_[j] = 0.0;
1682 <      }
1683 <    // James put this in
1684 <    if (outputDen_)
1685 <      for (j = 0; j < rnemdLogWidth_; j++) {
1686 <        //pyzCount_[j] = 0;
1687 <        DenHist_[j] = 0.0;
1688 <      }
1689 <    if (outputVz_)
1690 <      for (j = 0; j < rnemdLogWidth_; j++) {
1691 <        //pyzCount_[j] = 0;
1692 <        pzzHist_[j] = 0.0;
1693 <      }    
1694 <     // reset the counter
1695 <    
1696 <    Numcount_++;
1697 <    if (Numcount_ > int(runTime_/statusTime_))
1698 <      cerr << "time =" << time << "  Asum =" << Asum_ << '\n';
1699 <    if (Numcount_ > int(runTime_/statusTime_))
1700 <      cerr << "time =" << time << "  Jsum =" << Jsum_ << '\n';
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 <    logFrameCount_ = 0;
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