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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines