ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/rnemd/RNEMD.cpp
(Generate patch)

Comparing:
branches/development/src/integrators/RNEMD.cpp (file contents), Revision 1723 by gezelter, Thu May 24 20:59:54 2012 UTC vs.
branches/development/src/rnemd/RNEMD.cpp (file contents), Revision 1773 by gezelter, Tue Aug 7 18:26:40 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 <    rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
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_ = 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  
272      int selectionCount = seleMan_.getSelectionCount();
# Line 96 | 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 109 | Line 287 | namespace OpenMD {
287        painCave.severity = OPENMD_WARNING;
288        simError();
289      }
112    
113    const string st = simParams->getRNEMD_exchangeType();
290  
291 <    map<string, RNEMDTypeEnum>::iterator i;
116 <    i = stringToEnumMap_.find(st);
117 <    rnemdType_ = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
118 <    if (rnemdType_ == rnemdUnknown) {
119 <      sprintf(painCave.errMsg,
120 <              "RNEMD: The current RNEMD_exchangeType,\n"
121 <              "\t\t%s\n"
122 <              "\tis not one of the recognized exchange types.\n",
123 <              st.c_str());
124 <      painCave.isFatal = 1;
125 <      painCave.severity = OPENMD_ERROR;
126 <      simError();
127 <    }
128 <    
129 <    outputTemp_ = false;
130 <    if (simParams->haveRNEMD_outputTemperature()) {
131 <      outputTemp_ = simParams->getRNEMD_outputTemperature();
132 <    } else if ((rnemdType_ == rnemdKineticSwap) ||
133 <               (rnemdType_ == rnemdKineticScale) ||
134 <               (rnemdType_ == rnemdKineticScaleVAM) ||
135 <               (rnemdType_ == rnemdKineticScaleAM)) {
136 <      outputTemp_ = true;
137 <    }
138 <    outputVx_ = false;
139 <    if (simParams->haveRNEMD_outputVx()) {
140 <      outputVx_ = simParams->getRNEMD_outputVx();
141 <    } else if ((rnemdType_ == rnemdPx) || (rnemdType_ == rnemdPxScale)) {
142 <      outputVx_ = true;
143 <    }
144 <    outputVy_ = false;
145 <    if (simParams->haveRNEMD_outputVy()) {
146 <      outputVy_ = simParams->getRNEMD_outputVy();
147 <    } else if ((rnemdType_ == rnemdPy) || (rnemdType_ == rnemdPyScale)) {
148 <      outputVy_ = true;
149 <    }
150 <    output3DTemp_ = false;
151 <    if (simParams->haveRNEMD_outputXyzTemperature()) {
152 <      output3DTemp_ = simParams->getRNEMD_outputXyzTemperature();
153 <    }
154 <    outputRotTemp_ = false;
155 <    if (simParams->haveRNEMD_outputRotTemperature()) {
156 <      outputRotTemp_ = simParams->getRNEMD_outputRotTemperature();
157 <    }
291 >    nBins_ = rnemdParams->getOutputBins();
292  
293 < #ifdef IS_MPI
294 <    if (worldRank == 0) {
295 < #endif
293 >    data_.resize(RNEMD::ENDINDEX);
294 >    OutputData z;
295 >    z.units =  "Angstroms";
296 >    z.title =  "Z";
297 >    z.dataType = "RealType";
298 >    z.accumulator.reserve(nBins_);
299 >    for (unsigned int i = 0; i < nBins_; i++)
300 >      z.accumulator.push_back( new Accumulator() );
301 >    data_[Z] = z;
302 >    outputMap_["Z"] =  Z;
303  
304 <      //may have rnemdWriter separately
305 <      string rnemdFileName;
304 >    OutputData temperature;
305 >    temperature.units =  "K";
306 >    temperature.title =  "Temperature";
307 >    temperature.dataType = "RealType";
308 >    temperature.accumulator.reserve(nBins_);
309 >    for (unsigned int i = 0; i < nBins_; i++)
310 >      temperature.accumulator.push_back( new Accumulator() );
311 >    data_[TEMPERATURE] = temperature;
312 >    outputMap_["TEMPERATURE"] =  TEMPERATURE;
313  
314 <      if (outputTemp_) {
315 <        rnemdFileName = "temperature.log";
316 <        tempLog_.open(rnemdFileName.c_str());
317 <      }
318 <      if (outputVx_) {
319 <        rnemdFileName = "velocityX.log";
320 <        vxzLog_.open(rnemdFileName.c_str());
321 <      }
322 <      if (outputVy_) {
175 <        rnemdFileName = "velocityY.log";
176 <        vyzLog_.open(rnemdFileName.c_str());
177 <      }
314 >    OutputData velocity;
315 >    velocity.units = "amu/fs";
316 >    velocity.title =  "Velocity";  
317 >    velocity.dataType = "Vector3d";
318 >    velocity.accumulator.reserve(nBins_);
319 >    for (unsigned int i = 0; i < nBins_; i++)
320 >      velocity.accumulator.push_back( new VectorAccumulator() );
321 >    data_[VELOCITY] = velocity;
322 >    outputMap_["VELOCITY"] = VELOCITY;
323  
324 <      if (output3DTemp_) {
325 <        rnemdFileName = "temperatureX.log";
326 <        xTempLog_.open(rnemdFileName.c_str());
327 <        rnemdFileName = "temperatureY.log";
328 <        yTempLog_.open(rnemdFileName.c_str());
329 <        rnemdFileName = "temperatureZ.log";
330 <        zTempLog_.open(rnemdFileName.c_str());
331 <      }
332 <      if (outputRotTemp_) {
188 <        rnemdFileName = "temperatureR.log";
189 <        rotTempLog_.open(rnemdFileName.c_str());
190 <      }
324 >    OutputData density;
325 >    density.units =  "g cm^-3";
326 >    density.title =  "Density";
327 >    density.dataType = "RealType";
328 >    density.accumulator.reserve(nBins_);
329 >    for (unsigned int i = 0; i < nBins_; i++)
330 >      density.accumulator.push_back( new Accumulator() );
331 >    data_[DENSITY] = density;
332 >    outputMap_["DENSITY"] =  DENSITY;
333  
334 < #ifdef IS_MPI
335 <    }
194 < #endif
195 <
196 <    set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
197 <    set_RNEMD_nBins(simParams->getRNEMD_nBins());
198 <    midBin_ = nBins_ / 2;
199 <    if (simParams->haveRNEMD_binShift()) {
200 <      if (simParams->getRNEMD_binShift()) {
201 <        zShift_ = 0.5 / (RealType)(nBins_);
202 <      } else {
203 <        zShift_ = 0.0;
204 <      }
334 >    if (hasOutputFields) {
335 >      parseOutputFileFormat(rnemdParams->getOutputFields());
336      } else {
337 <      zShift_ = 0.0;
337 >      outputMask_.set(Z);
338 >      switch (rnemdFluxType_) {
339 >      case rnemdKE:
340 >      case rnemdRotKE:
341 >      case rnemdFullKE:
342 >        outputMask_.set(TEMPERATURE);
343 >        break;
344 >      case rnemdPx:
345 >      case rnemdPy:
346 >        outputMask_.set(VELOCITY);
347 >        break;
348 >      case rnemdPz:        
349 >      case rnemdPvector:
350 >        outputMask_.set(VELOCITY);
351 >        outputMask_.set(DENSITY);
352 >        break;
353 >      case rnemdKePx:
354 >      case rnemdKePy:
355 >        outputMask_.set(TEMPERATURE);
356 >        outputMask_.set(VELOCITY);
357 >        break;
358 >      case rnemdKePvector:
359 >        outputMask_.set(TEMPERATURE);
360 >        outputMask_.set(VELOCITY);
361 >        outputMask_.set(DENSITY);        
362 >        break;
363 >      default:
364 >        break;
365 >      }
366      }
367 <    //cerr << "I shift slabs by " << zShift_ << " Lz\n";
368 <    //shift slabs by half slab width, maybe useful in heterogeneous systems
369 <    //set to 0.0 if not using it; N/A in status output yet
211 <    if (simParams->haveRNEMD_logWidth()) {
212 <      set_RNEMD_logWidth(simParams->getRNEMD_logWidth());
213 <      /*arbitary rnemdLogWidth_, no checking;
214 <      if (rnemdLogWidth_ != nBins_ && rnemdLogWidth_ != midBin_ + 1) {
215 <        cerr << "WARNING! RNEMD_logWidth has abnormal value!\n";
216 <        cerr << "Automaically set back to default.\n";
217 <        rnemdLogWidth_ = nBins_;
218 <      }*/
367 >      
368 >    if (hasOutputFileName) {
369 >      rnemdFileName_ = rnemdParams->getOutputFileName();
370      } else {
371 <      set_RNEMD_logWidth(nBins_);
372 <    }
222 <    tempHist_.resize(rnemdLogWidth_, 0.0);
223 <    tempCount_.resize(rnemdLogWidth_, 0);
224 <    pxzHist_.resize(rnemdLogWidth_, 0.0);
225 <    //vxzCount_.resize(rnemdLogWidth_, 0);
226 <    pyzHist_.resize(rnemdLogWidth_, 0.0);
227 <    //vyzCount_.resize(rnemdLogWidth_, 0);
371 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
372 >    }          
373  
374 <    mHist_.resize(rnemdLogWidth_, 0.0);
230 <    xTempHist_.resize(rnemdLogWidth_, 0.0);
231 <    yTempHist_.resize(rnemdLogWidth_, 0.0);
232 <    zTempHist_.resize(rnemdLogWidth_, 0.0);
233 <    xyzTempCount_.resize(rnemdLogWidth_, 0);
234 <    rotTempHist_.resize(rnemdLogWidth_, 0.0);
235 <    rotTempCount_.resize(rnemdLogWidth_, 0);
374 >    exchangeTime_ = rnemdParams->getExchangeTime();
375  
376 <    set_RNEMD_exchange_total(0.0);
377 <    if (simParams->haveRNEMD_targetFlux()) {
378 <      set_RNEMD_target_flux(simParams->getRNEMD_targetFlux());
379 <    } else {
380 <      set_RNEMD_target_flux(0.0);
381 <    }
382 <    if (simParams->haveRNEMD_targetJzKE()) {
244 <      set_RNEMD_target_JzKE(simParams->getRNEMD_targetJzKE());
245 <    } else {
246 <      set_RNEMD_target_JzKE(0.0);
247 <    }
248 <    if (simParams->haveRNEMD_targetJzpx()) {
249 <      set_RNEMD_target_jzpx(simParams->getRNEMD_targetJzpx());
250 <    } else {
251 <      set_RNEMD_target_jzpx(0.0);
252 <    }
253 <    jzp_.x() = targetJzpx_;
254 <    njzp_.x() = -targetJzpx_;
255 <    if (simParams->haveRNEMD_targetJzpy()) {
256 <      set_RNEMD_target_jzpy(simParams->getRNEMD_targetJzpy());
257 <    } else {
258 <      set_RNEMD_target_jzpy(0.0);
259 <    }
260 <    jzp_.y() = targetJzpy_;
261 <    njzp_.y() = -targetJzpy_;
262 <    if (simParams->haveRNEMD_targetJzpz()) {
263 <      set_RNEMD_target_jzpz(simParams->getRNEMD_targetJzpz());
264 <    } else {
265 <      set_RNEMD_target_jzpz(0.0);
266 <    }
267 <    jzp_.z() = targetJzpz_;
268 <    njzp_.z() = -targetJzpz_;
376 >    Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
377 >    Mat3x3d hmat = currentSnap_->getHmat();
378 >  
379 >    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
380 >    // Lx, Ly = box dimensions in x & y
381 >    // dt = exchange time interval
382 >    // flux = target flux
383  
384 < #ifndef IS_MPI
385 <    if (simParams->haveSeed()) {
386 <      seedValue = simParams->getSeed();
387 <      randNumGen_ = new SeqRandNumGen(seedValue);
388 <    }else {
389 <      randNumGen_ = new SeqRandNumGen();
390 <    }    
391 < #else
392 <    if (simParams->haveSeed()) {
393 <      seedValue = simParams->getSeed();
394 <      randNumGen_ = new ParallelRandNumGen(seedValue);
395 <    }else {
282 <      randNumGen_ = new ParallelRandNumGen();
283 <    }    
284 < #endif
285 <  }
384 >    kineticTarget_ = 2.0*kineticFlux_*exchangeTime_*hmat(0,0)*hmat(1,1);
385 >    momentumTarget_ = 2.0*momentumFluxVector_*exchangeTime_*hmat(0,0)*hmat(1,1);
386 >
387 >    // total exchange sums are zeroed out at the beginning:
388 >
389 >    kineticExchange_ = 0.0;
390 >    momentumExchange_ = V3Zero;
391 >
392 >    if (hasSlabWidth)
393 >      slabWidth_ = rnemdParams->getSlabWidth();
394 >    else
395 >      slabWidth_ = hmat(2,2) / 10.0;
396    
397 +    if (hasSlabACenter)
398 +      slabACenter_ = rnemdParams->getSlabACenter();
399 +    else
400 +      slabACenter_ = 0.0;
401 +    
402 +    if (hasSlabBCenter)
403 +      slabBCenter_ = rnemdParams->getSlabBCenter();
404 +    else
405 +      slabBCenter_ = hmat(2,2) / 2.0;
406 +    
407 +  }
408 +  
409    RNEMD::~RNEMD() {
288    delete randNumGen_;
410      
411   #ifdef IS_MPI
412      if (worldRank == 0) {
413   #endif
293      
294      sprintf(painCave.errMsg,
295              "RNEMD: total failed trials: %d\n",
296              failTrialCount_);
297      painCave.isFatal = 0;
298      painCave.severity = OPENMD_INFO;
299      simError();
414  
415 <      if (outputTemp_) tempLog_.close();
302 <      if (outputVx_)   vxzLog_.close();
303 <      if (outputVy_)   vyzLog_.close();
415 >      writeOutputFile();
416  
417 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
418 <          rnemdType_ == rnemdPyScale) {
307 <        sprintf(painCave.errMsg,
308 <                "RNEMD: total root-checking warnings: %d\n",
309 <                failRootCount_);
310 <        painCave.isFatal = 0;
311 <        painCave.severity = OPENMD_INFO;
312 <        simError();
313 <      }
314 <      if (output3DTemp_) {
315 <        xTempLog_.close();
316 <        yTempLog_.close();
317 <        zTempLog_.close();
318 <      }
319 <      if (outputRotTemp_) rotTempLog_.close();
320 <
417 >      rnemdFile_.close();
418 >      
419   #ifdef IS_MPI
420      }
421   #endif
422    }
423 +  
424 +  bool RNEMD::inSlabA(Vector3d pos) {
425 +    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
426 +  }
427 +  bool RNEMD::inSlabB(Vector3d pos) {
428 +    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
429 +  }
430  
431    void RNEMD::doSwap() {
432  
# Line 353 | Line 458 | namespace OpenMD {
458  
459        if (usePeriodicBoundaryConditions_)
460          currentSnap_->wrapVector(pos);
461 +      bool inA = inSlabA(pos);
462 +      bool inB = inSlabB(pos);
463  
464 <      // which bin is this stuntdouble in?
358 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
359 <
360 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
361 <
362 <
363 <      // if we're in bin 0 or the middleBin
364 <      if (binNo == 0 || binNo == midBin_) {
464 >      if (inA || inB) {
465          
466          RealType mass = sd->getMass();
467          Vector3d vel = sd->getVel();
468          RealType value;
469 <
470 <        switch(rnemdType_) {
471 <        case rnemdKineticSwap :
469 >        
470 >        switch(rnemdFluxType_) {
471 >        case rnemdKE :
472            
473            value = mass * vel.lengthSquare();
474            
# Line 389 | Line 489 | namespace OpenMD {
489              }
490            } //angular momenta exchange enabled
491            //energyConvert temporarily disabled
492 <          //make exchangeSum_ comparable between swap & scale
492 >          //make kineticExchange_ comparable between swap & scale
493            //value = value * 0.5 / PhysicalConstants::energyConvert;
494            value *= 0.5;
495            break;
# Line 406 | Line 506 | namespace OpenMD {
506            break;
507          }
508          
509 <        if (binNo == 0) {
509 >        if (inA == 0) {
510            if (!min_found) {
511              min_val = value;
512              min_sd = sd;
# Line 417 | Line 517 | namespace OpenMD {
517                min_sd = sd;
518              }
519            }
520 <        } else { //midBin_
520 >        } else {
521            if (!max_found) {
522              max_val = value;
523              max_sd = sd;
# Line 431 | Line 531 | namespace OpenMD {
531          }
532        }
533      }
534 <
534 >    
535   #ifdef IS_MPI
536      int nProc, worldRank;
537 <
537 >    
538      nProc = MPI::COMM_WORLD.Get_size();
539      worldRank = MPI::COMM_WORLD.Get_rank();
540  
# Line 454 | Line 554 | namespace OpenMD {
554          RealType val;
555          int rank;
556        } max_vals, min_vals;
557 <    
557 >      
558        if (my_min_found) {
559          min_vals.val = min_val;
560        } else {
# Line 492 | Line 592 | namespace OpenMD {
592            Vector3d max_vel = max_sd->getVel();
593            RealType temp_vel;
594            
595 <          switch(rnemdType_) {
596 <          case rnemdKineticSwap :
595 >          switch(rnemdFluxType_) {
596 >          case rnemdKE :
597              min_sd->setVel(max_vel);
598              max_sd->setVel(min_vel);
599              if (min_sd->isDirectional() && max_sd->isDirectional()) {
# Line 544 | Line 644 | namespace OpenMD {
644                                     min_vel.getArrayPointer(), 3, MPI::REALTYPE,
645                                     min_vals.rank, 0, status);
646            
647 <          switch(rnemdType_) {
648 <          case rnemdKineticSwap :
647 >          switch(rnemdFluxType_) {
648 >          case rnemdKE :
649              max_sd->setVel(min_vel);
650              //angular momenta exchange enabled
651              if (max_sd->isDirectional()) {
# Line 590 | Line 690 | namespace OpenMD {
690                                     max_vel.getArrayPointer(), 3, MPI::REALTYPE,
691                                     max_vals.rank, 0, status);
692            
693 <          switch(rnemdType_) {
694 <          case rnemdKineticSwap :
693 >          switch(rnemdFluxType_) {
694 >          case rnemdKE :
695              min_sd->setVel(max_vel);
696              //angular momenta exchange enabled
697              if (min_sd->isDirectional()) {
# Line 625 | Line 725 | namespace OpenMD {
725            }
726          }
727   #endif
728 <        exchangeSum_ += max_val - min_val;
728 >        
729 >        switch(rnemdFluxType_) {
730 >        case rnemdKE:
731 >          cerr << "KE\n";
732 >          kineticExchange_ += max_val - min_val;
733 >          break;
734 >        case rnemdPx:
735 >          momentumExchange_.x() += max_val - min_val;
736 >          break;
737 >        case rnemdPy:
738 >          momentumExchange_.y() += max_val - min_val;
739 >          break;
740 >        case rnemdPz:
741 >          momentumExchange_.z() += max_val - min_val;
742 >          break;
743 >        default:
744 >          cerr << "default\n";
745 >          break;
746 >        }
747        } else {        
748          sprintf(painCave.errMsg,
749 <                "RNEMD: exchange NOT performed because min_val > max_val\n");
749 >                "RNEMD::doSwap exchange NOT performed because min_val > max_val\n");
750          painCave.isFatal = 0;
751          painCave.severity = OPENMD_INFO;
752          simError();        
# Line 636 | Line 754 | namespace OpenMD {
754        }
755      } else {
756        sprintf(painCave.errMsg,
757 <              "RNEMD: exchange NOT performed because selected object\n"
758 <              "\tnot present in at least one of the two slabs.\n");
757 >              "RNEMD::doSwap exchange NOT performed because selected object\n"
758 >              "\twas not present in at least one of the two slabs.\n");
759        painCave.isFatal = 0;
760        painCave.severity = OPENMD_INFO;
761        simError();        
762        failTrialCount_++;
763 <    }
646 <    
763 >    }    
764    }
765    
766 <  void RNEMD::doScale() {
766 >  void RNEMD::doNIVS() {
767  
768      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
769      Mat3x3d hmat = currentSnap_->getHmat();
# Line 687 | Line 804 | namespace OpenMD {
804          currentSnap_->wrapVector(pos);
805  
806        // which bin is this stuntdouble in?
807 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
807 >      bool inA = inSlabA(pos);
808 >      bool inB = inSlabB(pos);
809  
810 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
811 <
694 <      // if we're in bin 0 or the middleBin
695 <      if (binNo == 0 || binNo == midBin_) {
696 <        
810 >      if (inA || inB) {
811 >              
812          RealType mass = sd->getMass();
813          Vector3d vel = sd->getVel();
814        
815 <        if (binNo == 0) {
815 >        if (inA) {
816            hotBin.push_back(sd);
817            Phx += mass * vel.x();
818            Phy += mass * vel.y();
# Line 705 | Line 820 | namespace OpenMD {
820            Khx += mass * vel.x() * vel.x();
821            Khy += mass * vel.y() * vel.y();
822            Khz += mass * vel.z() * vel.z();
708          //if (rnemdType_ == rnemdKineticScaleVAM) {
823            if (sd->isDirectional()) {
824              Vector3d angMom = sd->getJ();
825              Mat3x3d I = sd->getI();
# Line 721 | Line 835 | namespace OpenMD {
835                  + angMom[2]*angMom[2]/I(2, 2);
836              }
837            }
838 <          //}
725 <        } else { //midBin_
838 >        } else {
839            coldBin.push_back(sd);
840            Pcx += mass * vel.x();
841            Pcy += mass * vel.y();
# Line 730 | Line 843 | namespace OpenMD {
843            Kcx += mass * vel.x() * vel.x();
844            Kcy += mass * vel.y() * vel.y();
845            Kcz += mass * vel.z() * vel.z();
733          //if (rnemdType_ == rnemdKineticScaleVAM) {
846            if (sd->isDirectional()) {
847              Vector3d angMom = sd->getJ();
848              Mat3x3d I = sd->getI();
# Line 746 | Line 858 | namespace OpenMD {
858                  + angMom[2]*angMom[2]/I(2, 2);
859              }
860            }
749          //}
861          }
862        }
863      }
# Line 760 | Line 871 | namespace OpenMD {
871      Kcz *= 0.5;
872      Kcw *= 0.5;
873  
763    std::cerr << "Khx= " << Khx << "\tKhy= " << Khy << "\tKhz= " << Khz
764              << "\tKhw= " << Khw << "\tKcx= " << Kcx << "\tKcy= " << Kcy
765              << "\tKcz= " << Kcz << "\tKcw= " << Kcw << "\n";
766    std::cerr << "Phx= " << Phx << "\tPhy= " << Phy << "\tPhz= " << Phz
767              << "\tPcx= " << Pcx << "\tPcy= " << Pcy << "\tPcz= " <<Pcz<<"\n";
768
874   #ifdef IS_MPI
875      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
876      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
# Line 791 | Line 896 | namespace OpenMD {
896      RealType pz = Pcz / Phz;
897      RealType c, x, y, z;
898      bool successfulScale = false;
899 <    if ((rnemdType_ == rnemdKineticScaleVAM) ||
900 <        (rnemdType_ == rnemdKineticScaleAM)) {
899 >    if ((rnemdFluxType_ == rnemdFullKE) ||
900 >        (rnemdFluxType_ == rnemdRotKE)) {
901        //may need sanity check Khw & Kcw > 0
902  
903 <      if (rnemdType_ == rnemdKineticScaleVAM) {
904 <        c = 1.0 - targetFlux_ / (Kcx + Kcy + Kcz + Kcw);
903 >      if (rnemdFluxType_ == rnemdFullKE) {
904 >        c = 1.0 - kineticTarget_ / (Kcx + Kcy + Kcz + Kcw);
905        } else {
906 <        c = 1.0 - targetFlux_ / Kcw;
906 >        c = 1.0 - kineticTarget_ / Kcw;
907        }
908  
909        if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
910          c = sqrt(c);
911 <        std::cerr << "cold slab scaling coefficient: " << c << endl;
911 >        //std::cerr << "cold slab scaling coefficient: " << c << endl;
912          //now convert to hotBin coefficient
913          RealType w = 0.0;
914 <        if (rnemdType_ ==  rnemdKineticScaleVAM) {
914 >        if (rnemdFluxType_ ==  rnemdFullKE) {
915            x = 1.0 + px * (1.0 - c);
916            y = 1.0 + py * (1.0 - c);
917            z = 1.0 + pz * (1.0 - c);
# Line 820 | Line 925 | namespace OpenMD {
925            */
926            if ((fabs(x - 1.0) < 0.1) && (fabs(y - 1.0) < 0.1) &&
927                (fabs(z - 1.0) < 0.1)) {
928 <            w = 1.0 + (targetFlux_ + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
928 >            w = 1.0 + (kineticTarget_
929 >                       + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
930                         + Khz * (1.0 - z * z)) / Khw;
931            }//no need to calculate w if x, y or z is out of range
932          } else {
933 <          w = 1.0 + targetFlux_ / Khw;
933 >          w = 1.0 + kineticTarget_ / Khw;
934          }
935          if ((w > 0.81) && (w < 1.21)) {//restrict scaling coefficients
936            //if w is in the right range, so should be x, y, z.
937            vector<StuntDouble*>::iterator sdi;
938            Vector3d vel;
939            for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
940 <            if (rnemdType_ == rnemdKineticScaleVAM) {
940 >            if (rnemdFluxType_ == rnemdFullKE) {
941                vel = (*sdi)->getVel() * c;
836              //vel.x() *= c;
837              //vel.y() *= c;
838              //vel.z() *= c;
942                (*sdi)->setVel(vel);
943              }
944              if ((*sdi)->isDirectional()) {
945                Vector3d angMom = (*sdi)->getJ() * c;
843              //angMom[0] *= c;
844              //angMom[1] *= c;
845              //angMom[2] *= c;
946                (*sdi)->setJ(angMom);
947              }
948            }
949            w = sqrt(w);
950 <          std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
951 <                    << "\twh= " << w << endl;
950 >          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
951 >          //           << "\twh= " << w << endl;
952            for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
953 <            if (rnemdType_ == rnemdKineticScaleVAM) {
953 >            if (rnemdFluxType_ == rnemdFullKE) {
954                vel = (*sdi)->getVel();
955                vel.x() *= x;
956                vel.y() *= y;
# Line 859 | Line 959 | namespace OpenMD {
959              }
960              if ((*sdi)->isDirectional()) {
961                Vector3d angMom = (*sdi)->getJ() * w;
862              //angMom[0] *= w;
863              //angMom[1] *= w;
864              //angMom[2] *= w;
962                (*sdi)->setJ(angMom);
963              }
964            }
965            successfulScale = true;
966 <          exchangeSum_ += targetFlux_;
966 >          kineticExchange_ += kineticTarget_;
967          }
968        }
969      } else {
970        RealType a000, a110, c0, a001, a111, b01, b11, c1;
971 <      switch(rnemdType_) {
972 <      case rnemdKineticScale :
971 >      switch(rnemdFluxType_) {
972 >      case rnemdKE :
973          /* used hotBin coeff's & only scale x & y dimensions
974             RealType px = Phx / Pcx;
975             RealType py = Phy / Pcy;
976             a110 = Khy;
977 <           c0 = - Khx - Khy - targetFlux_;
977 >           c0 = - Khx - Khy - kineticTarget_;
978             a000 = Khx;
979             a111 = Kcy * py * py;
980             b11 = -2.0 * Kcy * py * (1.0 + py);
981 <           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + targetFlux_;
981 >           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + kineticTarget_;
982             b01 = -2.0 * Kcx * px * (1.0 + px);
983             a001 = Kcx * px * px;
984          */
985          //scale all three dimensions, let c_x = c_y
986          a000 = Kcx + Kcy;
987          a110 = Kcz;
988 <        c0 = targetFlux_ - Kcx - Kcy - Kcz;
988 >        c0 = kineticTarget_ - Kcx - Kcy - Kcz;
989          a001 = Khx * px * px + Khy * py * py;
990          a111 = Khz * pz * pz;
991          b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
992          b11 = -2.0 * Khz * pz * (1.0 + pz);
993          c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
994 <          + Khz * pz * (2.0 + pz) - targetFlux_;
994 >          + Khz * pz * (2.0 + pz) - kineticTarget_;
995          break;
996 <      case rnemdPxScale :
997 <        c = 1 - targetFlux_ / Pcx;
996 >      case rnemdPx :
997 >        c = 1 - momentumTarget_.x() / Pcx;
998          a000 = Kcy;
999          a110 = Kcz;
1000          c0 = Kcx * c * c - Kcx - Kcy - Kcz;
# Line 908 | Line 1005 | namespace OpenMD {
1005          c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
1006            + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
1007          break;
1008 <      case rnemdPyScale :
1009 <        c = 1 - targetFlux_ / Pcy;
1008 >      case rnemdPy :
1009 >        c = 1 - momentumTarget_.y() / Pcy;
1010          a000 = Kcx;
1011          a110 = Kcz;
1012          c0 = Kcy * c * c - Kcx - Kcy - Kcz;
# Line 920 | Line 1017 | namespace OpenMD {
1017          c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
1018            + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
1019          break;
1020 <      case rnemdPzScale ://we don't really do this, do we?
1021 <        c = 1 - targetFlux_ / Pcz;
1020 >      case rnemdPz ://we don't really do this, do we?
1021 >        c = 1 - momentumTarget_.z() / Pcz;
1022          a000 = Kcx;
1023          a110 = Kcy;
1024          c0 = Kcz * c * c - Kcx - Kcy - Kcz;
# Line 1006 | Line 1103 | namespace OpenMD {
1103          for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1104            r1 = (*rpi).first;
1105            r2 = (*rpi).second;
1106 <          switch(rnemdType_) {
1107 <          case rnemdKineticScale :
1106 >          switch(rnemdFluxType_) {
1107 >          case rnemdKE :
1108              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1109                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1110                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1111              break;
1112 <          case rnemdPxScale :
1112 >          case rnemdPx :
1113              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1114                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1115              break;
1116 <          case rnemdPyScale :
1116 >          case rnemdPy :
1117              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1118                + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1119              break;
1120 <          case rnemdPzScale :
1120 >          case rnemdPz :
1121              diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1122                + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1123            default :
# Line 1034 | Line 1131 | namespace OpenMD {
1131   #ifdef IS_MPI
1132          if (worldRank == 0) {
1133   #endif
1134 <          sprintf(painCave.errMsg,
1135 <                  "RNEMD: roots r1= %lf\tr2 = %lf\n",
1136 <                  bestPair.first, bestPair.second);
1137 <          painCave.isFatal = 0;
1138 <          painCave.severity = OPENMD_INFO;
1139 <          simError();
1134 >          // sprintf(painCave.errMsg,
1135 >          //         "RNEMD: roots r1= %lf\tr2 = %lf\n",
1136 >          //         bestPair.first, bestPair.second);
1137 >          // painCave.isFatal = 0;
1138 >          // painCave.severity = OPENMD_INFO;
1139 >          // simError();
1140   #ifdef IS_MPI
1141          }
1142   #endif
1143          
1144 <        switch(rnemdType_) {
1145 <        case rnemdKineticScale :
1144 >        switch(rnemdFluxType_) {
1145 >        case rnemdKE :
1146            x = bestPair.first;
1147            y = bestPair.first;
1148            z = bestPair.second;
1149            break;
1150 <        case rnemdPxScale :
1150 >        case rnemdPx :
1151            x = c;
1152            y = bestPair.first;
1153            z = bestPair.second;
1154            break;
1155 <        case rnemdPyScale :
1155 >        case rnemdPy :
1156            x = bestPair.first;
1157            y = c;
1158            z = bestPair.second;
1159            break;
1160 <        case rnemdPzScale :
1160 >        case rnemdPz :
1161            x = bestPair.first;
1162            y = bestPair.second;
1163            z = c;
# Line 1089 | Line 1186 | namespace OpenMD {
1186            (*sdi)->setVel(vel);
1187          }
1188          successfulScale = true;
1189 <        exchangeSum_ += targetFlux_;
1189 >        switch(rnemdFluxType_) {
1190 >        case rnemdKE :
1191 >          kineticExchange_ += kineticTarget_;
1192 >          break;
1193 >        case rnemdPx :
1194 >        case rnemdPy :
1195 >        case rnemdPz :
1196 >          momentumExchange_ += momentumTarget_;
1197 >          break;          
1198 >        default :
1199 >          break;
1200 >        }      
1201        }
1202      }
1203      if (successfulScale != true) {
1204        sprintf(painCave.errMsg,
1205 <              "RNEMD: exchange NOT performed!\n");
1205 >              "RNEMD::doNIVS exchange NOT performed - roots that solve\n"
1206 >              "\tthe constraint equations may not exist or there may be\n"
1207 >              "\tno selected objects in one or both slabs.\n");
1208        painCave.isFatal = 0;
1209        painCave.severity = OPENMD_INFO;
1210        simError();        
# Line 1102 | Line 1212 | namespace OpenMD {
1212      }
1213    }
1214  
1215 <  void RNEMD::doShiftScale() {
1215 >  void RNEMD::doVSS() {
1216  
1217      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1218 +    RealType time = currentSnap_->getTime();    
1219      Mat3x3d hmat = currentSnap_->getHmat();
1220  
1221      seleMan_.setSelectionSet(evaluator_.evaluate());
# Line 1121 | Line 1232 | namespace OpenMD {
1232      Vector3d Pc(V3Zero);
1233      RealType Mc = 0.0;
1234      RealType Kc = 0.0;
1235 +    
1236  
1237      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1238           sd = seleMan_.nextSelected(selei)) {
# Line 1135 | Line 1247 | namespace OpenMD {
1247          currentSnap_->wrapVector(pos);
1248  
1249        // which bin is this stuntdouble in?
1250 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1251 <
1252 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + zShift_ + 0.5)) % nBins_;
1253 <
1142 <      // if we're in bin 0 or the middleBin
1143 <      if (binNo == 0 || binNo == midBin_) {
1250 >      bool inA = inSlabA(pos);
1251 >      bool inB = inSlabB(pos);
1252 >      
1253 >      if (inA || inB) {
1254          
1255          RealType mass = sd->getMass();
1256          Vector3d vel = sd->getVel();
1257        
1258 <        if (binNo == 0) {
1258 >        if (inA) {
1259            hotBin.push_back(sd);
1260            //std::cerr << "before, velocity = " << vel << endl;
1261            Ph += mass * vel;
1262            //std::cerr << "after, velocity = " << vel << endl;
1263            Mh += mass;
1264            Kh += mass * vel.lengthSquare();
1265 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1265 >          if (rnemdFluxType_ == rnemdFullKE) {
1266              if (sd->isDirectional()) {
1267                Vector3d angMom = sd->getJ();
1268                Mat3x3d I = sd->getI();
# Line 1174 | Line 1284 | namespace OpenMD {
1284            Pc += mass * vel;
1285            Mc += mass;
1286            Kc += mass * vel.lengthSquare();
1287 <          if (rnemdType_ == rnemdShiftScaleVAM) {
1287 >          if (rnemdFluxType_ == rnemdFullKE) {
1288              if (sd->isDirectional()) {
1289                Vector3d angMom = sd->getJ();
1290                Mat3x3d I = sd->getI();
# Line 1198 | Line 1308 | namespace OpenMD {
1308      Kh *= 0.5;
1309      Kc *= 0.5;
1310  
1311 <    std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1312 <              << "\tKc= " << Kc << endl;
1313 <    std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1314 <
1311 >    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1312 >    //        << "\tKc= " << Kc << endl;
1313 >    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1314 >    
1315   #ifdef IS_MPI
1316      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1317      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
# Line 1214 | Line 1324 | namespace OpenMD {
1324      bool successfulExchange = false;
1325      if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1326        Vector3d vc = Pc / Mc;
1327 <      Vector3d ac = njzp_ / Mc + vc;
1328 <      RealType cNumerator = Kc - targetJzKE_ - 0.5 * Mc * ac.lengthSquare();
1327 >      Vector3d ac = -momentumTarget_ / Mc + vc;
1328 >      Vector3d acrec = -momentumTarget_ / Mc;
1329 >      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1330        if (cNumerator > 0.0) {
1331          RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1332          if (cDenominator > 0.0) {
1333            RealType c = sqrt(cNumerator / cDenominator);
1334            if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1335              Vector3d vh = Ph / Mh;
1336 <            Vector3d ah = jzp_ / Mh + vh;
1337 <            RealType hNumerator = Kh + targetJzKE_
1336 >            Vector3d ah = momentumTarget_ / Mh + vh;
1337 >            Vector3d ahrec = momentumTarget_ / Mh;
1338 >            RealType hNumerator = Kh + kineticTarget_
1339                - 0.5 * Mh * ah.lengthSquare();
1340              if (hNumerator > 0.0) {
1341                RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1342                if (hDenominator > 0.0) {
1343                  RealType h = sqrt(hNumerator / hDenominator);
1344                  if ((h > 0.9) && (h < 1.1)) {
1345 <                  std::cerr << "cold slab scaling coefficient: " << c << "\n";
1346 <                  std::cerr << "hot slab scaling coefficient: " << h << "\n";
1345 >                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1346 >                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1347                    vector<StuntDouble*>::iterator sdi;
1348                    Vector3d vel;
1349                    for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1350                      //vel = (*sdi)->getVel();
1351                      vel = ((*sdi)->getVel() - vc) * c + ac;
1352                      (*sdi)->setVel(vel);
1353 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1353 >                    if (rnemdFluxType_ == rnemdFullKE) {
1354                        if ((*sdi)->isDirectional()) {
1355                          Vector3d angMom = (*sdi)->getJ() * c;
1356                          (*sdi)->setJ(angMom);
# Line 1249 | Line 1361 | namespace OpenMD {
1361                      //vel = (*sdi)->getVel();
1362                      vel = ((*sdi)->getVel() - vh) * h + ah;
1363                      (*sdi)->setVel(vel);
1364 <                    if (rnemdType_ == rnemdShiftScaleVAM) {
1364 >                    if (rnemdFluxType_ == rnemdFullKE) {
1365                        if ((*sdi)->isDirectional()) {
1366                          Vector3d angMom = (*sdi)->getJ() * h;
1367                          (*sdi)->setJ(angMom);
# Line 1257 | Line 1369 | namespace OpenMD {
1369                      }
1370                    }
1371                    successfulExchange = true;
1372 <                  exchangeSum_ += targetFlux_;
1373 <                  // this is a redundant variable for doShiftScale() so that
1262 <                  // RNEMD can output one exchange quantity needed in a job.
1263 <                  // need a better way to do this.
1372 >                  kineticExchange_ += kineticTarget_;
1373 >                  momentumExchange_ += momentumTarget_;
1374                  }
1375                }
1376              }
# Line 1270 | Line 1380 | namespace OpenMD {
1380      }
1381      if (successfulExchange != true) {
1382        sprintf(painCave.errMsg,
1383 <              "RNEMD: exchange NOT performed!\n");
1383 >              "RNEMD::doVSS exchange NOT performed - roots that solve\n"
1384 >              "\tthe constraint equations may not exist or there may be\n"
1385 >              "\tno selected objects in one or both slabs.\n");
1386        painCave.isFatal = 0;
1387        painCave.severity = OPENMD_INFO;
1388        simError();        
# Line 1280 | Line 1392 | namespace OpenMD {
1392  
1393    void RNEMD::doRNEMD() {
1394  
1395 <    switch(rnemdType_) {
1396 <    case rnemdKineticScale :
1397 <    case rnemdKineticScaleVAM :
1286 <    case rnemdKineticScaleAM :
1287 <    case rnemdPxScale :
1288 <    case rnemdPyScale :
1289 <    case rnemdPzScale :
1290 <      doScale();
1291 <      break;
1292 <    case rnemdKineticSwap :
1293 <    case rnemdPx :
1294 <    case rnemdPy :
1295 <    case rnemdPz :
1395 >    trialCount_++;
1396 >    switch(rnemdMethod_) {
1397 >    case rnemdSwap:
1398        doSwap();
1399        break;
1400 <    case rnemdShiftScaleV :
1401 <    case rnemdShiftScaleVAM :
1300 <      doShiftScale();
1400 >    case rnemdNIVS:
1401 >      doNIVS();
1402        break;
1403 <    case rnemdUnknown :
1403 >    case rnemdVSS:
1404 >      doVSS();
1405 >      break;
1406 >    case rnemdUnkownMethod:
1407      default :
1408        break;
1409      }
# Line 1316 | Line 1420 | namespace OpenMD {
1420      StuntDouble* sd;
1421      int idx;
1422  
1423 +    vector<RealType> binMass(nBins_, 0.0);
1424 +    vector<RealType> binPx(nBins_, 0.0);
1425 +    vector<RealType> binPy(nBins_, 0.0);
1426 +    vector<RealType> binPz(nBins_, 0.0);
1427 +    vector<RealType> binKE(nBins_, 0.0);
1428 +    vector<int> binDOF(nBins_, 0);
1429 +    vector<int> binCount(nBins_, 0);
1430 +
1431      // alternative approach, track all molecules instead of only those
1432      // selected for scaling/swapping:
1433      /*
1434      SimInfo::MoleculeIterator miter;
1435      vector<StuntDouble*>::iterator iiter;
1436      Molecule* mol;
1437 <    StuntDouble* integrableObject;
1437 >    StuntDouble* sd;
1438      for (mol = info_->beginMolecule(miter); mol != NULL;
1439 <         mol = info_->nextMolecule(miter))
1440 <      integrableObject is essentially sd
1441 <        for (integrableObject = mol->beginIntegrableObject(iiter);
1442 <             integrableObject != NULL;
1443 <             integrableObject = mol->nextIntegrableObject(iiter))
1439 >      mol = info_->nextMolecule(miter))
1440 >      sd is essentially sd
1441 >        for (sd = mol->beginIntegrableObject(iiter);
1442 >             sd != NULL;
1443 >             sd = mol->nextIntegrableObject(iiter))
1444      */
1445      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1446           sd = seleMan_.nextSelected(selei)) {
# Line 1341 | Line 1453 | namespace OpenMD {
1453        
1454        if (usePeriodicBoundaryConditions_)
1455          currentSnap_->wrapVector(pos);
1456 <      
1456 >
1457        // which bin is this stuntdouble in?
1458        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1459 <      
1460 <      int binNo = int(rnemdLogWidth_ * (pos.z() / hmat(2,2) + 0.5)) %
1461 <        rnemdLogWidth_;
1462 <      // no symmetrization allowed due to arbitary rnemdLogWidth_
1463 <      /*
1352 <      if (rnemdLogWidth_ == midBin_ + 1)
1353 <        if (binNo > midBin_)
1354 <          binNo = nBins_ - binNo;
1355 <      */
1459 >      // Shift molecules by half a box to have bins start at 0
1460 >      // The modulo operator is used to wrap the case when we are
1461 >      // beyond the end of the bins back to the beginning.
1462 >      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1463 >    
1464        RealType mass = sd->getMass();
1357      mHist_[binNo] += mass;
1465        Vector3d vel = sd->getVel();
1359      RealType value;
1360      //RealType xVal, yVal, zVal;
1466  
1467 <      if (outputTemp_) {
1468 <        value = mass * vel.lengthSquare();
1469 <        tempCount_[binNo] += 3;
1470 <        if (sd->isDirectional()) {
1471 <          Vector3d angMom = sd->getJ();
1472 <          Mat3x3d I = sd->getI();
1473 <          if (sd->isLinear()) {
1369 <            int i = sd->linearAxis();
1370 <            int j = (i + 1) % 3;
1371 <            int k = (i + 2) % 3;
1372 <            value += angMom[j] * angMom[j] / I(j, j) +
1373 <              angMom[k] * angMom[k] / I(k, k);
1374 <            tempCount_[binNo] +=2;
1375 <          } else {
1376 <            value += angMom[0] * angMom[0] / I(0, 0) +
1377 <              angMom[1]*angMom[1]/I(1, 1) +
1378 <              angMom[2]*angMom[2]/I(2, 2);
1379 <            tempCount_[binNo] +=3;
1380 <          }
1381 <        }
1382 <        value = value / PhysicalConstants::energyConvert
1383 <          / PhysicalConstants::kb;//may move to getStatus()
1384 <        tempHist_[binNo] += value;
1385 <      }
1386 <      if (outputVx_) {
1387 <        value = mass * vel[0];
1388 <        //vxzCount_[binNo]++;
1389 <        pxzHist_[binNo] += value;
1390 <      }
1391 <      if (outputVy_) {
1392 <        value = mass * vel[1];
1393 <        //vyzCount_[binNo]++;
1394 <        pyzHist_[binNo] += value;
1395 <      }
1467 >      binCount[binNo]++;
1468 >      binMass[binNo] += mass;
1469 >      binPx[binNo] += mass*vel.x();
1470 >      binPy[binNo] += mass*vel.y();
1471 >      binPz[binNo] += mass*vel.z();
1472 >      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1473 >      binDOF[binNo] += 3;
1474  
1475 <      if (output3DTemp_) {
1476 <        value = mass * vel.x() * vel.x();
1477 <        xTempHist_[binNo] += value;
1478 <        value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1479 <          / PhysicalConstants::kb;
1480 <        yTempHist_[binNo] += value;
1481 <        value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1482 <          / PhysicalConstants::kb;
1483 <        zTempHist_[binNo] += value;
1484 <        xyzTempCount_[binNo]++;
1475 >      if (sd->isDirectional()) {
1476 >        Vector3d angMom = sd->getJ();
1477 >        Mat3x3d I = sd->getI();
1478 >        if (sd->isLinear()) {
1479 >          int i = sd->linearAxis();
1480 >          int j = (i + 1) % 3;
1481 >          int k = (i + 2) % 3;
1482 >          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
1483 >                                 angMom[k] * angMom[k] / I(k, k));
1484 >          binDOF[binNo] += 2;
1485 >        } else {
1486 >          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
1487 >                                 angMom[1] * angMom[1] / I(1, 1) +
1488 >                                 angMom[2] * angMom[2] / I(2, 2));
1489 >          binDOF[binNo] += 3;
1490 >        }
1491        }
1492 <      if (outputRotTemp_) {
1493 <        if (sd->isDirectional()) {
1410 <          Vector3d angMom = sd->getJ();
1411 <          Mat3x3d I = sd->getI();
1412 <          if (sd->isLinear()) {
1413 <            int i = sd->linearAxis();
1414 <            int j = (i + 1) % 3;
1415 <            int k = (i + 2) % 3;
1416 <            value = angMom[j] * angMom[j] / I(j, j) +
1417 <              angMom[k] * angMom[k] / I(k, k);
1418 <            rotTempCount_[binNo] +=2;
1419 <          } else {
1420 <            value = angMom[0] * angMom[0] / I(0, 0) +
1421 <              angMom[1] * angMom[1] / I(1, 1) +
1422 <              angMom[2] * angMom[2] / I(2, 2);
1423 <            rotTempCount_[binNo] +=3;
1424 <          }
1425 <        }
1426 <        value = value / PhysicalConstants::energyConvert
1427 <          / PhysicalConstants::kb;//may move to getStatus()
1428 <        rotTempHist_[binNo] += value;
1429 <      }
1492 >    }
1493 >    
1494  
1495 + #ifdef IS_MPI
1496 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
1497 +                              nBins_, MPI::INT, MPI::SUM);
1498 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
1499 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1500 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
1501 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1502 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
1503 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1504 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
1505 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1506 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
1507 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1508 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
1509 +                              nBins_, MPI::INT, MPI::SUM);
1510 + #endif
1511 +
1512 +    Vector3d vel;
1513 +    RealType den;
1514 +    RealType temp;
1515 +    RealType z;
1516 +    for (int i = 0; i < nBins_; i++) {
1517 +      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
1518 +      vel.x() = binPx[i] / binMass[i];
1519 +      vel.y() = binPy[i] / binMass[i];
1520 +      vel.z() = binPz[i] / binMass[i];
1521 +      den = binCount[i] * nBins_ / (hmat(0,0) * hmat(1,1) * hmat(2,2));
1522 +      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
1523 +                               PhysicalConstants::energyConvert);
1524 +
1525 +      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
1526 +        if(outputMask_[j]) {
1527 +          switch(j) {
1528 +          case Z:
1529 +            (data_[j].accumulator[i])->add(z);
1530 +            break;
1531 +          case TEMPERATURE:
1532 +            data_[j].accumulator[i]->add(temp);
1533 +            break;
1534 +          case VELOCITY:
1535 +            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
1536 +            break;
1537 +          case DENSITY:
1538 +            data_[j].accumulator[i]->add(den);
1539 +            break;
1540 +          }
1541 +        }
1542 +      }
1543      }
1544    }
1545  
1546    void RNEMD::getStarted() {
1547      collectData();
1548 <    /*now can output profile in step 0, but might not be useful;
1437 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1438 <    Stats& stat = currentSnap_->statData;
1439 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1440 <    */
1441 <    //may output a header for the log file here
1442 <    getStatus();
1548 >    writeOutputFile();
1549    }
1550  
1551 <  void RNEMD::getStatus() {
1552 <
1553 <    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1554 <    Stats& stat = currentSnap_->statData;
1555 <    RealType time = currentSnap_->getTime();
1556 <
1557 <    stat[Stats::RNEMD_EXCHANGE_TOTAL] = exchangeSum_;
1558 <    //or to be more meaningful, define another item as exchangeSum_ / time
1559 <    int j;
1560 <
1551 >  void RNEMD::parseOutputFileFormat(const std::string& format) {
1552 >    StringTokenizer tokenizer(format, " ,;|\t\n\r");
1553 >    
1554 >    while(tokenizer.hasMoreTokens()) {
1555 >      std::string token(tokenizer.nextToken());
1556 >      toUpper(token);
1557 >      OutputMapType::iterator i = outputMap_.find(token);
1558 >      if (i != outputMap_.end()) {
1559 >        outputMask_.set(i->second);
1560 >      } else {
1561 >        sprintf( painCave.errMsg,
1562 >                 "RNEMD::parseOutputFileFormat: %s is not a recognized\n"
1563 >                 "\toutputFileFormat keyword.\n", token.c_str() );
1564 >        painCave.isFatal = 0;
1565 >        painCave.severity = OPENMD_ERROR;
1566 >        simError();            
1567 >      }
1568 >    }  
1569 >  }
1570 >  
1571 >  void RNEMD::writeOutputFile() {
1572 >    
1573   #ifdef IS_MPI
1456
1457    // all processors have the same number of bins, and STL vectors pack their
1458    // arrays, so in theory, this should be safe:
1459
1460    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &mHist_[0],
1461                              rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1462    if (outputTemp_) {
1463      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempHist_[0],
1464                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1465      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &tempCount_[0],
1466                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1467    }
1468    if (outputVx_) {
1469      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pxzHist_[0],
1470                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1471      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vxzCount_[0],
1472      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1473    }
1474    if (outputVy_) {
1475      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &pyzHist_[0],
1476                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1477      //MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &vyzCount_[0],
1478      //                        rnemdLogWidth_, MPI::INT, MPI::SUM);
1479    }
1480    if (output3DTemp_) {
1481      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xTempHist_[0],
1482                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1483      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &yTempHist_[0],
1484                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1485      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &zTempHist_[0],
1486                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1487      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &xyzTempCount_[0],
1488                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1489    }
1490    if (outputRotTemp_) {
1491      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempHist_[0],
1492                                rnemdLogWidth_, MPI::REALTYPE, MPI::SUM);
1493      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &rotTempCount_[0],
1494                                rnemdLogWidth_, MPI::INT, MPI::SUM);
1495    }
1496
1574      // If we're the root node, should we print out the results
1575      int worldRank = MPI::COMM_WORLD.Get_rank();
1576      if (worldRank == 0) {
1577   #endif
1578 +      rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
1579 +      
1580 +      if( !rnemdFile_ ){        
1581 +        sprintf( painCave.errMsg,
1582 +                 "Could not open \"%s\" for RNEMD output.\n",
1583 +                 rnemdFileName_.c_str());
1584 +        painCave.isFatal = 1;
1585 +        simError();
1586 +      }
1587  
1588 <      if (outputTemp_) {
1589 <        tempLog_ << time;
1590 <        for (j = 0; j < rnemdLogWidth_; j++) {
1591 <          tempLog_ << "\t" << tempHist_[j] / (RealType)tempCount_[j];
1592 <        }
1593 <        tempLog_ << endl;
1588 >      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1589 >
1590 >      RealType time = currentSnap_->getTime();
1591 >      
1592 >      
1593 >      rnemdFile_ << "#######################################################\n";
1594 >      rnemdFile_ << "# RNEMD {\n";
1595 >
1596 >      map<string, RNEMDMethod>::iterator mi;
1597 >      for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
1598 >        if ( (*mi).second == rnemdMethod_)
1599 >          rnemdFile_ << "#    exchangeMethod  = " << (*mi).first << "\n";
1600        }
1601 <      if (outputVx_) {
1602 <        vxzLog_ << time;
1603 <        for (j = 0; j < rnemdLogWidth_; j++) {
1604 <          vxzLog_ << "\t" << pxzHist_[j] / mHist_[j];
1513 <        }
1514 <        vxzLog_ << endl;
1601 >      map<string, RNEMDFluxType>::iterator fi;
1602 >      for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
1603 >        if ( (*fi).second == rnemdFluxType_)
1604 >          rnemdFile_ << "#    fluxType  = " << (*fi).first << "\n";
1605        }
1606 <      if (outputVy_) {
1607 <        vyzLog_ << time;
1608 <        for (j = 0; j < rnemdLogWidth_; j++) {
1609 <          vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1610 <        }
1611 <        vyzLog_ << endl;
1606 >      
1607 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << " fs\n";
1608 >
1609 >      rnemdFile_ << "#    objectSelection = \""
1610 >                 << rnemdObjectSelection_ << "\"\n";
1611 >      rnemdFile_ << "#    slabWidth = " << slabWidth_ << " angstroms\n";
1612 >      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << " angstroms\n";
1613 >      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << " angstroms\n";
1614 >      rnemdFile_ << "# }\n";
1615 >      rnemdFile_ << "#######################################################\n";
1616 >      
1617 >      rnemdFile_ << "# running time = " << time << " fs\n";
1618 >      rnemdFile_ << "# target kinetic flux = " << kineticFlux_ << "\n";
1619 >      rnemdFile_ << "# target momentum flux = " << momentumFluxVector_ << "\n";
1620 >      
1621 >      rnemdFile_ << "# target one-time kinetic exchange = " << kineticTarget_
1622 >                 << "\n";
1623 >      rnemdFile_ << "# target one-time momentum exchange = " << momentumTarget_
1624 >                 << "\n";
1625 >      
1626 >      rnemdFile_ << "# actual kinetic exchange = " << kineticExchange_ << "\n";
1627 >      rnemdFile_ << "# actual momentum exchange = " << momentumExchange_
1628 >                 << "\n";
1629 >      
1630 >      rnemdFile_ << "# attempted exchanges: " << trialCount_ << "\n";
1631 >      rnemdFile_ << "# failed exchanges: " << failTrialCount_ << "\n";
1632 >
1633 >      
1634 >      if (rnemdMethod_ == rnemdNIVS) {
1635 >        rnemdFile_ << "# NIVS root-check warnings: " << failRootCount_ << "\n";
1636        }
1637  
1638 <      if (output3DTemp_) {
1639 <        RealType temp;
1640 <        xTempLog_ << time;
1641 <        for (j = 0; j < rnemdLogWidth_; j++) {
1642 <          if (outputVx_)
1643 <            xTempHist_[j] -= pxzHist_[j] * pxzHist_[j] / mHist_[j];
1644 <          temp = xTempHist_[j] / (RealType)xyzTempCount_[j]
1645 <            / PhysicalConstants::energyConvert / PhysicalConstants::kb;
1646 <          xTempLog_ << "\t" << temp;
1638 >      rnemdFile_ << "#######################################################\n";
1639 >      
1640 >      
1641 >      
1642 >      //write title
1643 >      rnemdFile_ << "#";
1644 >      for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1645 >        if (outputMask_[i]) {
1646 >          rnemdFile_ << "\t" << data_[i].title <<
1647 >            "(" << data_[i].units << ")";
1648          }
1534        xTempLog_ << endl;
1535        yTempLog_ << time;
1536        for (j = 0; j < rnemdLogWidth_; j++) {
1537          yTempLog_ << "\t" << yTempHist_[j] / (RealType)xyzTempCount_[j];
1538        }
1539        yTempLog_ << endl;
1540        zTempLog_ << time;
1541        for (j = 0; j < rnemdLogWidth_; j++) {
1542          zTempLog_ << "\t" << zTempHist_[j] / (RealType)xyzTempCount_[j];
1543        }
1544        zTempLog_ << endl;
1649        }
1650 <      if (outputRotTemp_) {
1651 <        rotTempLog_ << time;
1652 <        for (j = 0; j < rnemdLogWidth_; j++) {
1653 <          rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1654 <        }
1655 <        rotTempLog_ << endl;
1656 <      }
1657 <
1650 >      rnemdFile_ << std::endl;
1651 >      
1652 >      rnemdFile_.precision(8);
1653 >      
1654 >      for (unsigned int j = 0; j < nBins_; j++) {        
1655 >        
1656 >        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1657 >          if (outputMask_[i]) {
1658 >            if (data_[i].dataType == "RealType")
1659 >              writeReal(i,j);
1660 >            else if (data_[i].dataType == "Vector3d")
1661 >              writeVector(i,j);
1662 >            else {
1663 >              sprintf( painCave.errMsg,
1664 >                       "RNEMD found an unknown data type for: %s ",
1665 >                       data_[i].title.c_str());
1666 >              painCave.isFatal = 1;
1667 >              simError();
1668 >            }
1669 >          }
1670 >        }
1671 >        rnemdFile_ << std::endl;
1672 >        
1673 >      }        
1674 >      
1675 >      rnemdFile_.flush();
1676 >      rnemdFile_.close();
1677 >      
1678   #ifdef IS_MPI
1679      }
1680   #endif
1681 <
1558 <    for (j = 0; j < rnemdLogWidth_; j++) {
1559 <      mHist_[j] = 0.0;
1560 <    }
1561 <    if (outputTemp_)
1562 <      for (j = 0; j < rnemdLogWidth_; j++) {
1563 <        tempCount_[j] = 0;
1564 <        tempHist_[j] = 0.0;
1565 <      }
1566 <    if (outputVx_)
1567 <      for (j = 0; j < rnemdLogWidth_; j++) {
1568 <        //pxzCount_[j] = 0;
1569 <        pxzHist_[j] = 0.0;
1570 <      }
1571 <    if (outputVy_)
1572 <      for (j = 0; j < rnemdLogWidth_; j++) {
1573 <        //pyzCount_[j] = 0;
1574 <        pyzHist_[j] = 0.0;
1575 <      }
1576 <
1577 <    if (output3DTemp_)
1578 <      for (j = 0; j < rnemdLogWidth_; j++) {
1579 <        xTempHist_[j] = 0.0;
1580 <        yTempHist_[j] = 0.0;
1581 <        zTempHist_[j] = 0.0;
1582 <        xyzTempCount_[j] = 0;
1583 <      }
1584 <    if (outputRotTemp_)
1585 <      for (j = 0; j < rnemdLogWidth_; j++) {
1586 <        rotTempCount_[j] = 0;
1587 <        rotTempHist_[j] = 0.0;
1588 <      }
1681 >    
1682    }
1683 +  
1684 +  void RNEMD::writeReal(int index, unsigned int bin) {
1685 +    assert(index >=0 && index < ENDINDEX);
1686 +    assert(bin >=0 && bin < nBins_);
1687 +    RealType s;
1688 +    
1689 +    data_[index].accumulator[bin]->getAverage(s);
1690 +    
1691 +    if (! isinf(s) && ! isnan(s)) {
1692 +      rnemdFile_ << "\t" << s;
1693 +    } else{
1694 +      sprintf( painCave.errMsg,
1695 +               "RNEMD detected a numerical error writing: %s for bin %d",
1696 +               data_[index].title.c_str(), bin);
1697 +      painCave.isFatal = 1;
1698 +      simError();
1699 +    }    
1700 +  }
1701 +  
1702 +  void RNEMD::writeVector(int index, unsigned int bin) {
1703 +    assert(index >=0 && index < ENDINDEX);
1704 +    assert(bin >=0 && bin < nBins_);
1705 +    Vector3d s;
1706 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
1707 +    if (isinf(s[0]) || isnan(s[0]) ||
1708 +        isinf(s[1]) || isnan(s[1]) ||
1709 +        isinf(s[2]) || isnan(s[2]) ) {      
1710 +      sprintf( painCave.errMsg,
1711 +               "RNEMD detected a numerical error writing: %s for bin %d",
1712 +               data_[index].title.c_str(), bin);
1713 +      painCave.isFatal = 1;
1714 +      simError();
1715 +    } else {
1716 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1717 +    }
1718 +  }  
1719   }
1720  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines