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 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 <    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 >    areaAccumulator_ = new Accumulator();
292  
293 < #ifdef IS_MPI
160 <    if (worldRank == 0) {
161 < #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_) {
175 <        rnemdFileName = "velocityY.log";
176 <        vyzLog_.open(rnemdFileName.c_str());
177 <      }
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_) {
188 <        rnemdFileName = "temperatureR.log";
189 <        rotTempLog_.open(rnemdFileName.c_str());
190 <      }
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 < #ifdef IS_MPI
327 <    }
328 < #endif
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 <    set_RNEMD_exchange_time(simParams->getRNEMD_exchangeTime());
337 <    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 <      }
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
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 <      }*/
369 >      
370 >    if (hasOutputFileName) {
371 >      rnemdFileName_ = rnemdParams->getOutputFileName();
372      } else {
373 <      set_RNEMD_logWidth(nBins_);
374 <    }
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);
373 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
374 >    }          
375  
376 <    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);
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()) {
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_;
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();
283 <    }    
284 < #endif
285 <  }
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() {
288    delete randNumGen_;
413      
414   #ifdef IS_MPI
415      if (worldRank == 0) {
416   #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();
417  
418 <      if (outputTemp_) tempLog_.close();
302 <      if (outputVx_)   vxzLog_.close();
303 <      if (outputVy_)   vyzLog_.close();
418 >      writeOutputFile();
419  
420 <      if (rnemdType_ == rnemdKineticScale || rnemdType_ == rnemdPxScale ||
421 <          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 <
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() {
435  
# Line 353 | 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?
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_) {
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 389 | 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 406 | 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 417 | 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 431 | 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 454 | Line 557 | namespace OpenMD {
557          RealType val;
558          int rank;
559        } max_vals, min_vals;
560 <    
560 >      
561        if (my_min_found) {
562          min_vals.val = min_val;
563        } else {
# Line 492 | 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 544 | 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 590 | 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 625 | 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 636 | 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 <    }
646 <    
766 >    }    
767    }
768    
769 <  void RNEMD::doScale() {
769 >  void RNEMD::doNIVS() {
770  
771      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
772      Mat3x3d hmat = currentSnap_->getHmat();
# Line 687 | 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 <
694 <      // if we're in bin 0 or the middleBin
695 <      if (binNo == 0 || binNo == midBin_) {
696 <        
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 705 | 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();
708          //if (rnemdType_ == rnemdKineticScaleVAM) {
826            if (sd->isDirectional()) {
827              Vector3d angMom = sd->getJ();
828              Mat3x3d I = sd->getI();
# Line 721 | Line 838 | namespace OpenMD {
838                  + angMom[2]*angMom[2]/I(2, 2);
839              }
840            }
841 <          //}
725 <        } else { //midBin_
841 >        } else {
842            coldBin.push_back(sd);
843            Pcx += mass * vel.x();
844            Pcy += mass * vel.y();
# Line 730 | 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();
733          //if (rnemdType_ == rnemdKineticScaleVAM) {
849            if (sd->isDirectional()) {
850              Vector3d angMom = sd->getJ();
851              Mat3x3d I = sd->getI();
# Line 746 | Line 861 | namespace OpenMD {
861                  + angMom[2]*angMom[2]/I(2, 2);
862              }
863            }
749          //}
864          }
865        }
866      }
# Line 760 | Line 874 | namespace OpenMD {
874      Kcz *= 0.5;
875      Kcw *= 0.5;
876  
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
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 791 | 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 820 | 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;
836              //vel.x() *= c;
837              //vel.y() *= c;
838              //vel.z() *= c;
945                (*sdi)->setVel(vel);
946              }
947              if ((*sdi)->isDirectional()) {
948                Vector3d angMom = (*sdi)->getJ() * c;
843              //angMom[0] *= c;
844              //angMom[1] *= c;
845              //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 859 | Line 962 | namespace OpenMD {
962              }
963              if ((*sdi)->isDirectional()) {
964                Vector3d angMom = (*sdi)->getJ() * w;
862              //angMom[0] *= w;
863              //angMom[1] *= w;
864              //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 908 | 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 920 | 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 1006 | 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 1034 | 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 1089 | 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 1102 | 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();    
1222      Mat3x3d hmat = currentSnap_->getHmat();
1223  
1224      seleMan_.setSelectionSet(evaluator_.evaluate());
# Line 1121 | Line 1235 | namespace OpenMD {
1235      Vector3d Pc(V3Zero);
1236      RealType Mc = 0.0;
1237      RealType Kc = 0.0;
1238 +    
1239  
1240      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1241           sd = seleMan_.nextSelected(selei)) {
# Line 1135 | 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 <
1142 <      // if we're in bin 0 or the middleBin
1143 <      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 1174 | 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 1198 | Line 1311 | namespace OpenMD {
1311      Kh *= 0.5;
1312      Kc *= 0.5;
1313  
1314 <    std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1315 <              << "\tKc= " << Kc << endl;
1316 <    std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1317 <
1314 >    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1315 >    //        << "\tKc= " << Kc << endl;
1316 >    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1317 >    
1318   #ifdef IS_MPI
1319      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1320      MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
# Line 1214 | 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 <      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 <            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();
1345                if (hDenominator > 0.0) {
1346                  RealType h = sqrt(hNumerator / hDenominator);
1347                  if ((h > 0.9) && (h < 1.1)) {
1348 <                  std::cerr << "cold slab scaling coefficient: " << c << "\n";
1349 <                  std::cerr << "hot slab scaling coefficient: " << h << "\n";
1348 >                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1349 >                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1350                    vector<StuntDouble*>::iterator sdi;
1351                    Vector3d vel;
1352                    for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
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 1249 | 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 1257 | Line 1372 | namespace OpenMD {
1372                      }
1373                    }
1374                    successfulExchange = true;
1375 <                  exchangeSum_ += targetFlux_;
1376 <                  // 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.
1375 >                  kineticExchange_ += kineticTarget_;
1376 >                  momentumExchange_ += momentumTarget_;
1377                  }
1378                }
1379              }
# Line 1270 | Line 1383 | namespace OpenMD {
1383      }
1384      if (successfulExchange != true) {
1385        sprintf(painCave.errMsg,
1386 <              "RNEMD: exchange NOT performed!\n");
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();        
# Line 1280 | Line 1395 | namespace OpenMD {
1395  
1396    void RNEMD::doRNEMD() {
1397  
1398 <    switch(rnemdType_) {
1399 <    case rnemdKineticScale :
1400 <    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 :
1398 >    trialCount_++;
1399 >    switch(rnemdMethod_) {
1400 >    case rnemdSwap:
1401        doSwap();
1402        break;
1403 <    case rnemdShiftScaleV :
1404 <    case rnemdShiftScaleVAM :
1300 <      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 1310 | 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 +    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:
1438      /*
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))
1444 >      mol = info_->nextMolecule(miter))
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 1341 | 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 <      /*
1352 <      if (rnemdLogWidth_ == midBin_ + 1)
1353 <        if (binNo > midBin_)
1354 <          binNo = nBins_ - binNo;
1355 <      */
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();
1357      mHist_[binNo] += mass;
1471        Vector3d vel = sd->getVel();
1359      RealType value;
1360      //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()) {
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 <      }
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 (output3DTemp_) {
1482 <        value = mass * vel.x() * vel.x();
1483 <        xTempHist_[binNo] += value;
1484 <        value = mass * vel.y() * vel.y() / PhysicalConstants::energyConvert
1485 <          / PhysicalConstants::kb;
1486 <        yTempHist_[binNo] += value;
1487 <        value = mass * vel.z() * vel.z() / PhysicalConstants::energyConvert
1488 <          / PhysicalConstants::kb;
1489 <        zTempHist_[binNo] += value;
1490 <        xyzTempCount_[binNo]++;
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 (outputRotTemp_) {
1499 <        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 <      }
1498 >    }
1499 >    
1500  
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 +      }
1549      }
1550    }
1551  
1552    void RNEMD::getStarted() {
1553      collectData();
1554 <    /*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();
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
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
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];
1513 <        }
1514 <        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;
1518 <        for (j = 0; j < rnemdLogWidth_; j++) {
1519 <          vyzLog_ << "\t" << pyzHist_[j] / mHist_[j];
1520 <        }
1521 <        vyzLog_ << endl;
1522 <      }
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          }
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;
1657        }
1658 <      if (outputRotTemp_) {
1659 <        rotTempLog_ << time;
1660 <        for (j = 0; j < rnemdLogWidth_; j++) {
1661 <          rotTempLog_ << "\t" << rotTempHist_[j] / (RealType)rotTempCount_[j];
1662 <        }
1663 <        rotTempLog_ << endl;
1664 <      }
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 >        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 +        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_)
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 <      }
1752 >  }  
1753  
1754 <    if (output3DTemp_)
1755 <      for (j = 0; j < rnemdLogWidth_; j++) {
1756 <        xTempHist_[j] = 0.0;
1757 <        yTempHist_[j] = 0.0;
1758 <        zTempHist_[j] = 0.0;
1759 <        xyzTempCount_[j] = 0;
1760 <      }
1761 <    if (outputRotTemp_)
1762 <      for (j = 0; j < rnemdLogWidth_; j++) {
1763 <        rotTempCount_[j] = 0;
1764 <        rotTempHist_[j] = 0.0;
1765 <      }
1754 >  void RNEMD::writeRealStdDev(int index, unsigned int bin) {
1755 >    assert(index >=0 && index < ENDINDEX);
1756 >    assert(bin < nBins_);
1757 >    RealType s;
1758 >    
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