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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines