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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines