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

Comparing:
branches/development/src/rnemd/RNEMD.cpp (file contents), Revision 1773 by gezelter, Tue Aug 7 18:26:40 2012 UTC vs.
trunk/src/rnemd/RNEMD.cpp (file contents), Revision 1969 by gezelter, Wed Feb 26 14:14:50 2014 UTC

# Line 35 | Line 35
35   *                                                                      
36   * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).            
37   * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).          
38 < * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).          
38 > * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).          
39   * [4]  Vardeman & Gezelter, in progress (2009).                        
40   */
41 + #ifdef IS_MPI
42 + #include <mpi.h>
43 + #endif
44  
45   #include <cmath>
46 + #include <sstream>
47 + #include <string>
48 +
49   #include "rnemd/RNEMD.hpp"
50   #include "math/Vector3.hpp"
51   #include "math/Vector.hpp"
# Line 49 | Line 55
55   #include "primitives/StuntDouble.hpp"
56   #include "utils/PhysicalConstants.hpp"
57   #include "utils/Tuple.hpp"
58 < #ifdef IS_MPI
59 < #include <mpi.h>
58 > #include "brains/Thermo.hpp"
59 > #include "math/ConvexHull.hpp"
60 >
61 > #ifdef _MSC_VER
62 > #define isnan(x) _isnan((x))
63 > #define isinf(x) (!_finite(x) && !_isnan(x))
64   #endif
65  
66   #define HONKING_LARGE_VALUE 1.0e10
# Line 59 | Line 69 | namespace OpenMD {
69   namespace OpenMD {
70    
71    RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info),
72 +                                evaluatorA_(info), seleManA_(info),
73 +                                commonA_(info), evaluatorB_(info),
74 +                                seleManB_(info), commonB_(info),
75 +                                hasData_(false), hasDividingArea_(false),
76                                  usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
77  
78      trialCount_ = 0;
79      failTrialCount_ = 0;
80      failRootCount_ = 0;
81  
82 <    int seedValue;
69 <    Globals * simParams = info->getSimParams();
82 >    Globals* simParams = info->getSimParams();
83      RNEMDParameters* rnemdParams = simParams->getRNEMDParameters();
84  
85 +    doRNEMD_ = rnemdParams->getUseRNEMD();
86 +    if (!doRNEMD_) return;
87 +
88      stringToMethod_["Swap"]  = rnemdSwap;
89      stringToMethod_["NIVS"]  = rnemdNIVS;
90      stringToMethod_["VSS"]   = rnemdVSS;
# Line 77 | Line 93 | namespace OpenMD {
93      stringToFluxType_["Px"]  = rnemdPx;
94      stringToFluxType_["Py"]  = rnemdPy;
95      stringToFluxType_["Pz"]  = rnemdPz;
96 +    stringToFluxType_["Pvector"]  = rnemdPvector;
97 +    stringToFluxType_["Lx"]  = rnemdLx;
98 +    stringToFluxType_["Ly"]  = rnemdLy;
99 +    stringToFluxType_["Lz"]  = rnemdLz;
100 +    stringToFluxType_["Lvector"]  = rnemdLvector;
101      stringToFluxType_["KE+Px"]  = rnemdKePx;
102      stringToFluxType_["KE+Py"]  = rnemdKePy;
103      stringToFluxType_["KE+Pvector"]  = rnemdKePvector;
104 +    stringToFluxType_["KE+Lx"]  = rnemdKeLx;
105 +    stringToFluxType_["KE+Ly"]  = rnemdKeLy;
106 +    stringToFluxType_["KE+Lz"]  = rnemdKeLz;
107 +    stringToFluxType_["KE+Lvector"]  = rnemdKeLvector;
108  
109      runTime_ = simParams->getRunTime();
110      statusTime_ = simParams->getStatusTime();
111  
87    rnemdObjectSelection_ = rnemdParams->getObjectSelection();
88    evaluator_.loadScriptString(rnemdObjectSelection_);
89    seleMan_.setSelectionSet(evaluator_.evaluate());
90
112      const string methStr = rnemdParams->getMethod();
113      bool hasFluxType = rnemdParams->haveFluxType();
114  
115 +    rnemdObjectSelection_ = rnemdParams->getObjectSelection();
116 +
117      string fluxStr;
118      if (hasFluxType) {
119        fluxStr = rnemdParams->getFluxType();
# Line 98 | Line 121 | namespace OpenMD {
121        sprintf(painCave.errMsg,
122                "RNEMD: No fluxType was set in the md file.  This parameter,\n"
123                "\twhich must be one of the following values:\n"
124 <              "\tKE, Px, Py, Pz, KE+Px, KE+Py, KE+Pvector, must be set to\n"
125 <              "\tuse RNEMD\n");
124 >              "\tKE, Px, Py, Pz, Pvector, Lx, Ly, Lz, Lvector,\n"
125 >              "\tKE+Px, KE+Py, KE+Pvector, KE+Lx, KE+Ly, KE+Lz, KE+Lvector\n"
126 >              "\tmust be set to use RNEMD\n");
127        painCave.isFatal = 1;
128        painCave.severity = OPENMD_ERROR;
129        simError();
# Line 108 | Line 132 | namespace OpenMD {
132      bool hasKineticFlux = rnemdParams->haveKineticFlux();
133      bool hasMomentumFlux = rnemdParams->haveMomentumFlux();
134      bool hasMomentumFluxVector = rnemdParams->haveMomentumFluxVector();
135 +    bool hasAngularMomentumFlux = rnemdParams->haveAngularMomentumFlux();
136 +    bool hasAngularMomentumFluxVector = rnemdParams->haveAngularMomentumFluxVector();
137 +    hasSelectionA_ = rnemdParams->haveSelectionA();
138 +    hasSelectionB_ = rnemdParams->haveSelectionB();
139      bool hasSlabWidth = rnemdParams->haveSlabWidth();
140      bool hasSlabACenter = rnemdParams->haveSlabACenter();
141      bool hasSlabBCenter = rnemdParams->haveSlabBCenter();
142 +    bool hasSphereARadius = rnemdParams->haveSphereARadius();
143 +    hasSphereBRadius_ = rnemdParams->haveSphereBRadius();
144 +    bool hasCoordinateOrigin = rnemdParams->haveCoordinateOrigin();
145      bool hasOutputFileName = rnemdParams->haveOutputFileName();
146      bool hasOutputFields = rnemdParams->haveOutputFields();
147      
# Line 195 | Line 226 | namespace OpenMD {
226        case rnemdPz:
227          hasCorrectFlux = hasMomentumFlux;
228          break;
229 +      case rnemdLx:
230 +      case rnemdLy:
231 +      case rnemdLz:
232 +        hasCorrectFlux = hasAngularMomentumFlux;
233 +        break;
234        case rnemdPvector:
235          hasCorrectFlux = hasMomentumFluxVector;
236 +        break;
237 +      case rnemdLvector:
238 +        hasCorrectFlux = hasAngularMomentumFluxVector;
239 +        break;
240        case rnemdKePx:
241        case rnemdKePy:
242          hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
243          break;
244 +      case rnemdKeLx:
245 +      case rnemdKeLy:
246 +      case rnemdKeLz:
247 +        hasCorrectFlux = hasAngularMomentumFlux && hasKineticFlux;
248 +        break;
249        case rnemdKePvector:
250          hasCorrectFlux = hasMomentumFluxVector && hasKineticFlux;
251          break;
252 +      case rnemdKeLvector:
253 +        hasCorrectFlux = hasAngularMomentumFluxVector && hasKineticFlux;
254 +        break;
255        default:
256          methodFluxMismatch = true;
257          break;
# Line 224 | Line 272 | namespace OpenMD {
272      }
273      if (!hasCorrectFlux) {
274        sprintf(painCave.errMsg,
275 <              "RNEMD: The current method,\n"
228 <              "\t%s, and flux type %s\n"
275 >              "RNEMD: The current method, %s, and flux type, %s,\n"
276                "\tdid not have the correct flux value specified. Options\n"
277 <              "\tinclude: kineticFlux, momentumFlux, and momentumFluxVector\n",
277 >              "\tinclude: kineticFlux, momentumFlux, angularMomentumFlux,\n"
278 >              "\tmomentumFluxVector, and angularMomentumFluxVector.\n",
279                methStr.c_str(), fluxStr.c_str());
280        painCave.isFatal = 1;
281        painCave.severity = OPENMD_ERROR;
# Line 235 | Line 283 | namespace OpenMD {
283      }
284  
285      if (hasKineticFlux) {
286 <      kineticFlux_ = rnemdParams->getKineticFlux();
286 >      // convert the kcal / mol / Angstroms^2 / fs values in the md file
287 >      // into  amu / fs^3:
288 >      kineticFlux_ = rnemdParams->getKineticFlux()
289 >        * PhysicalConstants::energyConvert;
290      } else {
291        kineticFlux_ = 0.0;
292      }
# Line 264 | Line 315 | namespace OpenMD {
315          default:
316            break;
317          }
318 <      }    
319 <    }
318 >      }
319 >      if (hasAngularMomentumFluxVector) {
320 >        angularMomentumFluxVector_ = rnemdParams->getAngularMomentumFluxVector();
321 >      } else {
322 >        angularMomentumFluxVector_ = V3Zero;
323 >        if (hasAngularMomentumFlux) {
324 >          RealType angularMomentumFlux = rnemdParams->getAngularMomentumFlux();
325 >          switch (rnemdFluxType_) {
326 >          case rnemdLx:
327 >            angularMomentumFluxVector_.x() = angularMomentumFlux;
328 >            break;
329 >          case rnemdLy:
330 >            angularMomentumFluxVector_.y() = angularMomentumFlux;
331 >            break;
332 >          case rnemdLz:
333 >            angularMomentumFluxVector_.z() = angularMomentumFlux;
334 >            break;
335 >          case rnemdKeLx:
336 >            angularMomentumFluxVector_.x() = angularMomentumFlux;
337 >            break;
338 >          case rnemdKeLy:
339 >            angularMomentumFluxVector_.y() = angularMomentumFlux;
340 >            break;
341 >          case rnemdKeLz:
342 >            angularMomentumFluxVector_.z() = angularMomentumFlux;
343 >            break;
344 >          default:
345 >            break;
346 >          }
347 >        }        
348 >      }
349  
350 <    // do some sanity checking
350 >      if (hasCoordinateOrigin) {
351 >        coordinateOrigin_ = rnemdParams->getCoordinateOrigin();
352 >      } else {
353 >        coordinateOrigin_ = V3Zero;
354 >      }
355  
356 <    int selectionCount = seleMan_.getSelectionCount();
273 <    int nIntegrable = info->getNGlobalIntegrableObjects();
356 >      // do some sanity checking
357  
358 <    if (selectionCount > nIntegrable) {
276 <      sprintf(painCave.errMsg,
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"
281 <              "\tis only %d.  This is almost certainly not what you want\n"
282 <              "\tto do.  A likely cause of this is forgetting the _RB_0\n"
283 <              "\tselector in the selection script!\n",
284 <              rnemdObjectSelection_.c_str(),
285 <              selectionCount, nIntegrable);
286 <      painCave.isFatal = 0;
287 <      painCave.severity = OPENMD_WARNING;
288 <      simError();
289 <    }
358 >      int selectionCount = seleMan_.getSelectionCount();
359  
360 <    nBins_ = rnemdParams->getOutputBins();
360 >      int nIntegrable = info->getNGlobalIntegrableObjects();
361  
362 <    data_.resize(RNEMD::ENDINDEX);
363 <    OutputData z;
364 <    z.units =  "Angstroms";
365 <    z.title =  "Z";
366 <    z.dataType = "RealType";
367 <    z.accumulator.reserve(nBins_);
368 <    for (unsigned int i = 0; i < nBins_; i++)
369 <      z.accumulator.push_back( new Accumulator() );
370 <    data_[Z] = z;
371 <    outputMap_["Z"] =  Z;
362 >      if (selectionCount > nIntegrable) {
363 >        sprintf(painCave.errMsg,
364 >                "RNEMD: The current objectSelection,\n"
365 >                "\t\t%s\n"
366 >                "\thas resulted in %d selected objects.  However,\n"
367 >                "\tthe total number of integrable objects in the system\n"
368 >                "\tis only %d.  This is almost certainly not what you want\n"
369 >                "\tto do.  A likely cause of this is forgetting the _RB_0\n"
370 >                "\tselector in the selection script!\n",
371 >                rnemdObjectSelection_.c_str(),
372 >                selectionCount, nIntegrable);
373 >        painCave.isFatal = 0;
374 >        painCave.severity = OPENMD_WARNING;
375 >        simError();
376 >      }
377  
378 <    OutputData temperature;
305 <    temperature.units =  "K";
306 <    temperature.title =  "Temperature";
307 <    temperature.dataType = "RealType";
308 <    temperature.accumulator.reserve(nBins_);
309 <    for (unsigned int i = 0; i < nBins_; i++)
310 <      temperature.accumulator.push_back( new Accumulator() );
311 <    data_[TEMPERATURE] = temperature;
312 <    outputMap_["TEMPERATURE"] =  TEMPERATURE;
378 >      areaAccumulator_ = new Accumulator();
379  
380 <    OutputData velocity;
381 <    velocity.units = "amu/fs";
316 <    velocity.title =  "Velocity";  
317 <    velocity.dataType = "Vector3d";
318 <    velocity.accumulator.reserve(nBins_);
319 <    for (unsigned int i = 0; i < nBins_; i++)
320 <      velocity.accumulator.push_back( new VectorAccumulator() );
321 <    data_[VELOCITY] = velocity;
322 <    outputMap_["VELOCITY"] = VELOCITY;
380 >      nBins_ = rnemdParams->getOutputBins();
381 >      binWidth_ = rnemdParams->getOutputBinWidth();
382  
383 <    OutputData density;
384 <    density.units =  "g cm^-3";
385 <    density.title =  "Density";
386 <    density.dataType = "RealType";
387 <    density.accumulator.reserve(nBins_);
388 <    for (unsigned int i = 0; i < nBins_; i++)
389 <      density.accumulator.push_back( new Accumulator() );
390 <    data_[DENSITY] = density;
391 <    outputMap_["DENSITY"] =  DENSITY;
383 >      data_.resize(RNEMD::ENDINDEX);
384 >      OutputData z;
385 >      z.units =  "Angstroms";
386 >      z.title =  "Z";
387 >      z.dataType = "RealType";
388 >      z.accumulator.reserve(nBins_);
389 >      for (int i = 0; i < nBins_; i++)
390 >        z.accumulator.push_back( new Accumulator() );
391 >      data_[Z] = z;
392 >      outputMap_["Z"] =  Z;
393  
394 <    if (hasOutputFields) {
395 <      parseOutputFileFormat(rnemdParams->getOutputFields());
396 <    } else {
397 <      outputMask_.set(Z);
398 <      switch (rnemdFluxType_) {
399 <      case rnemdKE:
400 <      case rnemdRotKE:
401 <      case rnemdFullKE:
402 <        outputMask_.set(TEMPERATURE);
403 <        break;
404 <      case rnemdPx:
405 <      case rnemdPy:
406 <        outputMask_.set(VELOCITY);
407 <        break;
408 <      case rnemdPz:        
409 <      case rnemdPvector:
410 <        outputMask_.set(VELOCITY);
411 <        outputMask_.set(DENSITY);
412 <        break;
413 <      case rnemdKePx:
414 <      case rnemdKePy:
415 <        outputMask_.set(TEMPERATURE);
416 <        outputMask_.set(VELOCITY);
417 <        break;
418 <      case rnemdKePvector:
419 <        outputMask_.set(TEMPERATURE);
420 <        outputMask_.set(VELOCITY);
421 <        outputMask_.set(DENSITY);        
422 <        break;
423 <      default:
424 <        break;
394 >      OutputData r;
395 >      r.units =  "Angstroms";
396 >      r.title =  "R";
397 >      r.dataType = "RealType";
398 >      r.accumulator.reserve(nBins_);
399 >      for (int i = 0; i < nBins_; i++)
400 >        r.accumulator.push_back( new Accumulator() );
401 >      data_[R] = r;
402 >      outputMap_["R"] =  R;
403 >
404 >      OutputData temperature;
405 >      temperature.units =  "K";
406 >      temperature.title =  "Temperature";
407 >      temperature.dataType = "RealType";
408 >      temperature.accumulator.reserve(nBins_);
409 >      for (int i = 0; i < nBins_; i++)
410 >        temperature.accumulator.push_back( new Accumulator() );
411 >      data_[TEMPERATURE] = temperature;
412 >      outputMap_["TEMPERATURE"] =  TEMPERATURE;
413 >
414 >      OutputData velocity;
415 >      velocity.units = "angstroms/fs";
416 >      velocity.title =  "Velocity";  
417 >      velocity.dataType = "Vector3d";
418 >      velocity.accumulator.reserve(nBins_);
419 >      for (int i = 0; i < nBins_; i++)
420 >        velocity.accumulator.push_back( new VectorAccumulator() );
421 >      data_[VELOCITY] = velocity;
422 >      outputMap_["VELOCITY"] = VELOCITY;
423 >
424 >      OutputData angularVelocity;
425 >      angularVelocity.units = "angstroms^2/fs";
426 >      angularVelocity.title =  "AngularVelocity";  
427 >      angularVelocity.dataType = "Vector3d";
428 >      angularVelocity.accumulator.reserve(nBins_);
429 >      for (int i = 0; i < nBins_; i++)
430 >        angularVelocity.accumulator.push_back( new VectorAccumulator() );
431 >      data_[ANGULARVELOCITY] = angularVelocity;
432 >      outputMap_["ANGULARVELOCITY"] = ANGULARVELOCITY;
433 >
434 >      OutputData density;
435 >      density.units =  "g cm^-3";
436 >      density.title =  "Density";
437 >      density.dataType = "RealType";
438 >      density.accumulator.reserve(nBins_);
439 >      for (int i = 0; i < nBins_; i++)
440 >        density.accumulator.push_back( new Accumulator() );
441 >      data_[DENSITY] = density;
442 >      outputMap_["DENSITY"] =  DENSITY;
443 >
444 >      if (hasOutputFields) {
445 >        parseOutputFileFormat(rnemdParams->getOutputFields());
446 >      } else {
447 >        if (usePeriodicBoundaryConditions_)
448 >          outputMask_.set(Z);
449 >        else
450 >          outputMask_.set(R);
451 >        switch (rnemdFluxType_) {
452 >        case rnemdKE:
453 >        case rnemdRotKE:
454 >        case rnemdFullKE:
455 >          outputMask_.set(TEMPERATURE);
456 >          break;
457 >        case rnemdPx:
458 >        case rnemdPy:
459 >          outputMask_.set(VELOCITY);
460 >          break;
461 >        case rnemdPz:        
462 >        case rnemdPvector:
463 >          outputMask_.set(VELOCITY);
464 >          outputMask_.set(DENSITY);
465 >          break;
466 >        case rnemdLx:
467 >        case rnemdLy:
468 >        case rnemdLz:
469 >        case rnemdLvector:
470 >          outputMask_.set(ANGULARVELOCITY);
471 >          break;
472 >        case rnemdKeLx:
473 >        case rnemdKeLy:
474 >        case rnemdKeLz:
475 >        case rnemdKeLvector:
476 >          outputMask_.set(TEMPERATURE);
477 >          outputMask_.set(ANGULARVELOCITY);
478 >          break;
479 >        case rnemdKePx:
480 >        case rnemdKePy:
481 >          outputMask_.set(TEMPERATURE);
482 >          outputMask_.set(VELOCITY);
483 >          break;
484 >        case rnemdKePvector:
485 >          outputMask_.set(TEMPERATURE);
486 >          outputMask_.set(VELOCITY);
487 >          outputMask_.set(DENSITY);        
488 >          break;
489 >        default:
490 >          break;
491 >        }
492        }
366    }
493        
494 <    if (hasOutputFileName) {
495 <      rnemdFileName_ = rnemdParams->getOutputFileName();
496 <    } else {
497 <      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
498 <    }          
494 >      if (hasOutputFileName) {
495 >        rnemdFileName_ = rnemdParams->getOutputFileName();
496 >      } else {
497 >        rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
498 >      }          
499  
500 <    exchangeTime_ = rnemdParams->getExchangeTime();
500 >      exchangeTime_ = rnemdParams->getExchangeTime();
501  
502 <    Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
503 <    Mat3x3d hmat = currentSnap_->getHmat();
378 <  
379 <    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
380 <    // Lx, Ly = box dimensions in x & y
381 <    // dt = exchange time interval
382 <    // flux = target flux
502 >      Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
503 >      // total exchange sums are zeroed out at the beginning:
504  
505 <    kineticTarget_ = 2.0*kineticFlux_*exchangeTime_*hmat(0,0)*hmat(1,1);
506 <    momentumTarget_ = 2.0*momentumFluxVector_*exchangeTime_*hmat(0,0)*hmat(1,1);
505 >      kineticExchange_ = 0.0;
506 >      momentumExchange_ = V3Zero;
507 >      angularMomentumExchange_ = V3Zero;
508  
509 <    // total exchange sums are zeroed out at the beginning:
509 >      std::ostringstream selectionAstream;
510 >      std::ostringstream selectionBstream;
511 >    
512 >      if (hasSelectionA_) {
513 >        selectionA_ = rnemdParams->getSelectionA();
514 >      } else {
515 >        if (usePeriodicBoundaryConditions_) {    
516 >          Mat3x3d hmat = currentSnap_->getHmat();
517 >        
518 >          if (hasSlabWidth)
519 >            slabWidth_ = rnemdParams->getSlabWidth();
520 >          else
521 >            slabWidth_ = hmat(2,2) / 10.0;
522 >        
523 >          if (hasSlabACenter)
524 >            slabACenter_ = rnemdParams->getSlabACenter();
525 >          else
526 >            slabACenter_ = 0.0;
527 >        
528 >          selectionAstream << "select wrappedz > "
529 >                           << slabACenter_ - 0.5*slabWidth_
530 >                           <<  " && wrappedz < "
531 >                           << slabACenter_ + 0.5*slabWidth_;
532 >          selectionA_ = selectionAstream.str();
533 >        } else {
534 >          if (hasSphereARadius)
535 >            sphereARadius_ = rnemdParams->getSphereARadius();
536 >          else {
537 >            // use an initial guess to the size of the inner slab to be 1/10 the
538 >            // radius of an approximately spherical hull:
539 >            Thermo thermo(info);
540 >            RealType hVol = thermo.getHullVolume();
541 >            sphereARadius_ = 0.1 * pow((3.0 * hVol / (4.0 * M_PI)), 1.0/3.0);
542 >          }
543 >          selectionAstream << "select r < " << sphereARadius_;
544 >          selectionA_ = selectionAstream.str();
545 >        }
546 >      }
547 >    
548 >      if (hasSelectionB_) {
549 >        selectionB_ = rnemdParams->getSelectionB();
550  
551 <    kineticExchange_ = 0.0;
552 <    momentumExchange_ = V3Zero;
551 >      } else {
552 >        if (usePeriodicBoundaryConditions_) {    
553 >          Mat3x3d hmat = currentSnap_->getHmat();
554 >        
555 >          if (hasSlabWidth)
556 >            slabWidth_ = rnemdParams->getSlabWidth();
557 >          else
558 >            slabWidth_ = hmat(2,2) / 10.0;
559 >        
560 >          if (hasSlabBCenter)
561 >            slabBCenter_ = rnemdParams->getSlabBCenter();
562 >          else
563 >            slabBCenter_ = hmat(2,2) / 2.0;
564 >        
565 >          selectionBstream << "select wrappedz > "
566 >                           << slabBCenter_ - 0.5*slabWidth_
567 >                           <<  " && wrappedz < "
568 >                           << slabBCenter_ + 0.5*slabWidth_;
569 >          selectionB_ = selectionBstream.str();
570 >        } else {
571 >          if (hasSphereBRadius_) {
572 >            sphereBRadius_ = rnemdParams->getSphereBRadius();
573 >            selectionBstream << "select r > " << sphereBRadius_;
574 >            selectionB_ = selectionBstream.str();
575 >          } else {
576 >            selectionB_ = "select hull";
577 >            BisHull_ = true;
578 >            hasSelectionB_ = true;
579 >          }
580 >        }
581 >      }
582 >    }
583  
584 <    if (hasSlabWidth)
585 <      slabWidth_ = rnemdParams->getSlabWidth();
586 <    else
587 <      slabWidth_ = hmat(2,2) / 10.0;
584 >    // object evaluator:
585 >    evaluator_.loadScriptString(rnemdObjectSelection_);
586 >    seleMan_.setSelectionSet(evaluator_.evaluate());
587 >    evaluatorA_.loadScriptString(selectionA_);
588 >    evaluatorB_.loadScriptString(selectionB_);
589 >    seleManA_.setSelectionSet(evaluatorA_.evaluate());
590 >    seleManB_.setSelectionSet(evaluatorB_.evaluate());
591 >    commonA_ = seleManA_ & seleMan_;
592 >    commonB_ = seleManB_ & seleMan_;  
593 >  }
594    
397    if (hasSlabACenter)
398      slabACenter_ = rnemdParams->getSlabACenter();
399    else
400      slabACenter_ = 0.0;
595      
596 <    if (hasSlabBCenter)
597 <      slabBCenter_ = rnemdParams->getSlabBCenter();
404 <    else
405 <      slabBCenter_ = hmat(2,2) / 2.0;
406 <    
407 <  }
408 <  
409 <  RNEMD::~RNEMD() {
410 <    
596 >  RNEMD::~RNEMD() {
597 >    if (!doRNEMD_) return;
598   #ifdef IS_MPI
599      if (worldRank == 0) {
600   #endif
# Line 419 | Line 606 | namespace OpenMD {
606   #ifdef IS_MPI
607      }
608   #endif
609 +
610 +    // delete all of the objects we created:
611 +    delete areaAccumulator_;    
612 +    data_.clear();
613    }
614    
615 <  bool RNEMD::inSlabA(Vector3d pos) {
616 <    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
617 <  }
618 <  bool RNEMD::inSlabB(Vector3d pos) {
428 <    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
429 <  }
615 >  void RNEMD::doSwap(SelectionManager& smanA, SelectionManager& smanB) {
616 >    if (!doRNEMD_) return;
617 >    int selei;
618 >    int selej;
619  
431  void RNEMD::doSwap() {
432
620      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
621      Mat3x3d hmat = currentSnap_->getHmat();
622  
436    seleMan_.setSelectionSet(evaluator_.evaluate());
437
438    int selei;
623      StuntDouble* sd;
440    int idx;
624  
625      RealType min_val;
626      bool min_found = false;  
# Line 447 | Line 630 | namespace OpenMD {
630      bool max_found = false;
631      StuntDouble* max_sd;
632  
633 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
634 <         sd = seleMan_.nextSelected(selei)) {
633 >    for (sd = seleManA_.beginSelected(selei); sd != NULL;
634 >         sd = seleManA_.nextSelected(selei)) {
635  
453      idx = sd->getLocalIndex();
454
636        Vector3d pos = sd->getPos();
637 <
637 >      
638        // wrap the stuntdouble's position back into the box:
639 <
639 >      
640        if (usePeriodicBoundaryConditions_)
641          currentSnap_->wrapVector(pos);
642 <      bool inA = inSlabA(pos);
643 <      bool inB = inSlabB(pos);
644 <
645 <      if (inA || inB) {
642 >      
643 >      RealType mass = sd->getMass();
644 >      Vector3d vel = sd->getVel();
645 >      RealType value;
646 >      
647 >      switch(rnemdFluxType_) {
648 >      case rnemdKE :
649          
650 <        RealType mass = sd->getMass();
651 <        Vector3d vel = sd->getVel();
652 <        RealType value;
653 <        
654 <        switch(rnemdFluxType_) {
471 <        case rnemdKE :
650 >        value = mass * vel.lengthSquare();
651 >        
652 >        if (sd->isDirectional()) {
653 >          Vector3d angMom = sd->getJ();
654 >          Mat3x3d I = sd->getI();
655            
656 <          value = mass * vel.lengthSquare();
657 <          
658 <          if (sd->isDirectional()) {
659 <            Vector3d angMom = sd->getJ();
660 <            Mat3x3d I = sd->getI();
661 <            
662 <            if (sd->isLinear()) {
663 <              int i = sd->linearAxis();
664 <              int j = (i + 1) % 3;
665 <              int k = (i + 2) % 3;
666 <              value += angMom[j] * angMom[j] / I(j, j) +
667 <                angMom[k] * angMom[k] / I(k, k);
668 <            } else {                        
669 <              value += angMom[0]*angMom[0]/I(0, 0)
670 <                + angMom[1]*angMom[1]/I(1, 1)
671 <                + angMom[2]*angMom[2]/I(2, 2);
672 <            }
673 <          } //angular momenta exchange enabled
674 <          //energyConvert temporarily disabled
675 <          //make kineticExchange_ comparable between swap & scale
676 <          //value = value * 0.5 / PhysicalConstants::energyConvert;
677 <          value *= 0.5;
678 <          break;
679 <        case rnemdPx :
680 <          value = mass * vel[0];
681 <          break;
682 <        case rnemdPy :
683 <          value = mass * vel[1];
684 <          break;
685 <        case rnemdPz :
686 <          value = mass * vel[2];
687 <          break;
688 <        default :
689 <          break;
656 >          if (sd->isLinear()) {
657 >            int i = sd->linearAxis();
658 >            int j = (i + 1) % 3;
659 >            int k = (i + 2) % 3;
660 >            value += angMom[j] * angMom[j] / I(j, j) +
661 >              angMom[k] * angMom[k] / I(k, k);
662 >          } else {                        
663 >            value += angMom[0]*angMom[0]/I(0, 0)
664 >              + angMom[1]*angMom[1]/I(1, 1)
665 >              + angMom[2]*angMom[2]/I(2, 2);
666 >          }
667 >        } //angular momenta exchange enabled
668 >        value *= 0.5;
669 >        break;
670 >      case rnemdPx :
671 >        value = mass * vel[0];
672 >        break;
673 >      case rnemdPy :
674 >        value = mass * vel[1];
675 >        break;
676 >      case rnemdPz :
677 >        value = mass * vel[2];
678 >        break;
679 >      default :
680 >        break;
681 >      }
682 >      if (!max_found) {
683 >        max_val = value;
684 >        max_sd = sd;
685 >        max_found = true;
686 >      } else {
687 >        if (max_val < value) {
688 >          max_val = value;
689 >          max_sd = sd;
690          }
691 +      }  
692 +    }
693          
694 <        if (inA == 0) {
695 <          if (!min_found) {
696 <            min_val = value;
697 <            min_sd = sd;
698 <            min_found = true;
699 <          } else {
700 <            if (min_val > value) {
701 <              min_val = value;
702 <              min_sd = sd;
703 <            }
704 <          }
705 <        } else {
706 <          if (!max_found) {
707 <            max_val = value;
708 <            max_sd = sd;
709 <            max_found = true;
710 <          } else {
711 <            if (max_val < value) {
712 <              max_val = value;
713 <              max_sd = sd;
714 <            }
715 <          }      
716 <        }
694 >    for (sd = seleManB_.beginSelected(selej); sd != NULL;
695 >         sd = seleManB_.nextSelected(selej)) {
696 >
697 >      Vector3d pos = sd->getPos();
698 >      
699 >      // wrap the stuntdouble's position back into the box:
700 >      
701 >      if (usePeriodicBoundaryConditions_)
702 >        currentSnap_->wrapVector(pos);
703 >      
704 >      RealType mass = sd->getMass();
705 >      Vector3d vel = sd->getVel();
706 >      RealType value;
707 >      
708 >      switch(rnemdFluxType_) {
709 >      case rnemdKE :
710 >        
711 >        value = mass * vel.lengthSquare();
712 >        
713 >        if (sd->isDirectional()) {
714 >          Vector3d angMom = sd->getJ();
715 >          Mat3x3d I = sd->getI();
716 >          
717 >          if (sd->isLinear()) {
718 >            int i = sd->linearAxis();
719 >            int j = (i + 1) % 3;
720 >            int k = (i + 2) % 3;
721 >            value += angMom[j] * angMom[j] / I(j, j) +
722 >              angMom[k] * angMom[k] / I(k, k);
723 >          } else {                        
724 >            value += angMom[0]*angMom[0]/I(0, 0)
725 >              + angMom[1]*angMom[1]/I(1, 1)
726 >              + angMom[2]*angMom[2]/I(2, 2);
727 >          }
728 >        } //angular momenta exchange enabled
729 >        value *= 0.5;
730 >        break;
731 >      case rnemdPx :
732 >        value = mass * vel[0];
733 >        break;
734 >      case rnemdPy :
735 >        value = mass * vel[1];
736 >        break;
737 >      case rnemdPz :
738 >        value = mass * vel[2];
739 >        break;
740 >      default :
741 >        break;
742        }
743 +      
744 +      if (!min_found) {
745 +        min_val = value;
746 +        min_sd = sd;
747 +        min_found = true;
748 +      } else {
749 +        if (min_val > value) {
750 +          min_val = value;
751 +          min_sd = sd;
752 +        }
753 +      }
754      }
755      
756 < #ifdef IS_MPI
757 <    int nProc, worldRank;
758 <    
759 <    nProc = MPI::COMM_WORLD.Get_size();
760 <    worldRank = MPI::COMM_WORLD.Get_rank();
756 > #ifdef IS_MPI    
757 >    int worldRank;
758 >    MPI_Comm_rank( MPI_COMM_WORLD, &worldRank);
759 >        
760 >    int my_min_found = min_found;
761 >    int my_max_found = max_found;
762  
541    bool my_min_found = min_found;
542    bool my_max_found = max_found;
543
763      // Even if we didn't find a minimum, did someone else?
764 <    MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found, 1, MPI::BOOL, MPI::LOR);
764 >    MPI_Allreduce(&my_min_found, &min_found, 1, MPI_INT, MPI_LOR,
765 >                  MPI_COMM_WORLD);
766      // Even if we didn't find a maximum, did someone else?
767 <    MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found, 1, MPI::BOOL, MPI::LOR);
767 >    MPI_Allreduce(&my_max_found, &max_found, 1, MPI_INT, MPI_LOR,
768 >                  MPI_COMM_WORLD);
769   #endif
770  
771      if (max_found && min_found) {
# Line 563 | Line 784 | namespace OpenMD {
784        min_vals.rank = worldRank;    
785        
786        // Who had the minimum?
787 <      MPI::COMM_WORLD.Allreduce(&min_vals, &min_vals,
788 <                                1, MPI::REALTYPE_INT, MPI::MINLOC);
787 >      MPI_Allreduce(&min_vals, &min_vals,
788 >                    1, MPI_REALTYPE_INT, MPI_MINLOC, MPI_COMM_WORLD);
789        min_val = min_vals.val;
790        
791        if (my_max_found) {
# Line 575 | Line 796 | namespace OpenMD {
796        max_vals.rank = worldRank;    
797        
798        // Who had the maximum?
799 <      MPI::COMM_WORLD.Allreduce(&max_vals, &max_vals,
800 <                                1, MPI::REALTYPE_INT, MPI::MAXLOC);
799 >      MPI_Allreduce(&max_vals, &max_vals,
800 >                    1, MPI_REALTYPE_INT, MPI_MAXLOC, MPI_COMM_WORLD);
801        max_val = max_vals.val;
802   #endif
803        
# Line 636 | Line 857 | namespace OpenMD {
857            
858            Vector3d min_vel;
859            Vector3d max_vel = max_sd->getVel();
860 <          MPI::Status status;
860 >          MPI_Status* status;
861  
862            // point-to-point swap of the velocity vector
863 <          MPI::COMM_WORLD.Sendrecv(max_vel.getArrayPointer(), 3, MPI::REALTYPE,
864 <                                   min_vals.rank, 0,
865 <                                   min_vel.getArrayPointer(), 3, MPI::REALTYPE,
866 <                                   min_vals.rank, 0, status);
863 >          MPI_Sendrecv(max_vel.getArrayPointer(), 3, MPI_REALTYPE,
864 >                       min_vals.rank, 0,
865 >                       min_vel.getArrayPointer(), 3, MPI_REALTYPE,
866 >                       min_vals.rank, 0, MPI_COMM_WORLD, status);
867            
868            switch(rnemdFluxType_) {
869            case rnemdKE :
# Line 653 | Line 874 | namespace OpenMD {
874                Vector3d max_angMom = max_sd->getJ();
875                
876                // point-to-point swap of the angular momentum vector
877 <              MPI::COMM_WORLD.Sendrecv(max_angMom.getArrayPointer(), 3,
878 <                                       MPI::REALTYPE, min_vals.rank, 1,
879 <                                       min_angMom.getArrayPointer(), 3,
880 <                                       MPI::REALTYPE, min_vals.rank, 1,
881 <                                       status);
877 >              MPI_Sendrecv(max_angMom.getArrayPointer(), 3,
878 >                           MPI_REALTYPE, min_vals.rank, 1,
879 >                           min_angMom.getArrayPointer(), 3,
880 >                           MPI_REALTYPE, min_vals.rank, 1,
881 >                           MPI_COMM_WORLD, status);
882                
883                max_sd->setJ(min_angMom);
884              }
# Line 682 | Line 903 | namespace OpenMD {
903            
904            Vector3d max_vel;
905            Vector3d min_vel = min_sd->getVel();
906 <          MPI::Status status;
906 >          MPI_Status* status;
907            
908            // point-to-point swap of the velocity vector
909 <          MPI::COMM_WORLD.Sendrecv(min_vel.getArrayPointer(), 3, MPI::REALTYPE,
910 <                                   max_vals.rank, 0,
911 <                                   max_vel.getArrayPointer(), 3, MPI::REALTYPE,
912 <                                   max_vals.rank, 0, status);
909 >          MPI_Sendrecv(min_vel.getArrayPointer(), 3, MPI_REALTYPE,
910 >                       max_vals.rank, 0,
911 >                       max_vel.getArrayPointer(), 3, MPI_REALTYPE,
912 >                       max_vals.rank, 0, MPI_COMM_WORLD, status);
913            
914            switch(rnemdFluxType_) {
915            case rnemdKE :
# Line 699 | Line 920 | namespace OpenMD {
920                Vector3d max_angMom;
921                
922                // point-to-point swap of the angular momentum vector
923 <              MPI::COMM_WORLD.Sendrecv(min_angMom.getArrayPointer(), 3,
924 <                                       MPI::REALTYPE, max_vals.rank, 1,
925 <                                       max_angMom.getArrayPointer(), 3,
926 <                                       MPI::REALTYPE, max_vals.rank, 1,
927 <                                       status);
923 >              MPI_Sendrecv(min_angMom.getArrayPointer(), 3,
924 >                           MPI_REALTYPE, max_vals.rank, 1,
925 >                           max_angMom.getArrayPointer(), 3,
926 >                           MPI_REALTYPE, max_vals.rank, 1,
927 >                           MPI_COMM_WORLD, status);
928                
929                min_sd->setJ(max_angMom);
930              }
# Line 728 | Line 949 | namespace OpenMD {
949          
950          switch(rnemdFluxType_) {
951          case rnemdKE:
731          cerr << "KE\n";
952            kineticExchange_ += max_val - min_val;
953            break;
954          case rnemdPx:
# Line 741 | Line 961 | namespace OpenMD {
961            momentumExchange_.z() += max_val - min_val;
962            break;
963          default:
744          cerr << "default\n";
964            break;
965          }
966        } else {        
# Line 763 | Line 982 | namespace OpenMD {
982      }    
983    }
984    
985 <  void RNEMD::doNIVS() {
985 >  void RNEMD::doNIVS(SelectionManager& smanA, SelectionManager& smanB) {
986 >    if (!doRNEMD_) return;
987 >    int selei;
988 >    int selej;
989  
990      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
991 +    RealType time = currentSnap_->getTime();    
992      Mat3x3d hmat = currentSnap_->getHmat();
770
771    seleMan_.setSelectionSet(evaluator_.evaluate());
993  
773    int selei;
994      StuntDouble* sd;
775    int idx;
995  
996      vector<StuntDouble*> hotBin, coldBin;
997  
# Line 791 | Line 1010 | namespace OpenMD {
1010      RealType Kcz = 0.0;
1011      RealType Kcw = 0.0;
1012  
1013 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1014 <         sd = seleMan_.nextSelected(selei)) {
1013 >    for (sd = smanA.beginSelected(selei); sd != NULL;
1014 >         sd = smanA.nextSelected(selei)) {
1015  
797      idx = sd->getLocalIndex();
798
1016        Vector3d pos = sd->getPos();
1017 <
1017 >      
1018        // wrap the stuntdouble's position back into the box:
1019 <
1019 >      
1020        if (usePeriodicBoundaryConditions_)
1021          currentSnap_->wrapVector(pos);
1022 +      
1023 +      
1024 +      RealType mass = sd->getMass();
1025 +      Vector3d vel = sd->getVel();
1026 +      
1027 +      hotBin.push_back(sd);
1028 +      Phx += mass * vel.x();
1029 +      Phy += mass * vel.y();
1030 +      Phz += mass * vel.z();
1031 +      Khx += mass * vel.x() * vel.x();
1032 +      Khy += mass * vel.y() * vel.y();
1033 +      Khz += mass * vel.z() * vel.z();
1034 +      if (sd->isDirectional()) {
1035 +        Vector3d angMom = sd->getJ();
1036 +        Mat3x3d I = sd->getI();
1037 +        if (sd->isLinear()) {
1038 +          int i = sd->linearAxis();
1039 +          int j = (i + 1) % 3;
1040 +          int k = (i + 2) % 3;
1041 +          Khw += angMom[j] * angMom[j] / I(j, j) +
1042 +            angMom[k] * angMom[k] / I(k, k);
1043 +        } else {
1044 +          Khw += angMom[0]*angMom[0]/I(0, 0)
1045 +            + angMom[1]*angMom[1]/I(1, 1)
1046 +            + angMom[2]*angMom[2]/I(2, 2);
1047 +        }
1048 +      }
1049 +    }
1050 +    for (sd = smanB.beginSelected(selej); sd != NULL;
1051 +         sd = smanB.nextSelected(selej)) {
1052 +      Vector3d pos = sd->getPos();
1053 +      
1054 +      // wrap the stuntdouble's position back into the box:
1055 +      
1056 +      if (usePeriodicBoundaryConditions_)
1057 +        currentSnap_->wrapVector(pos);
1058 +            
1059 +      RealType mass = sd->getMass();
1060 +      Vector3d vel = sd->getVel();
1061  
1062 <      // which bin is this stuntdouble in?
1063 <      bool inA = inSlabA(pos);
1064 <      bool inB = inSlabB(pos);
1065 <
1066 <      if (inA || inB) {
1067 <              
1068 <        RealType mass = sd->getMass();
1069 <        Vector3d vel = sd->getVel();
1070 <      
1071 <        if (inA) {
1072 <          hotBin.push_back(sd);
1073 <          Phx += mass * vel.x();
1074 <          Phy += mass * vel.y();
1075 <          Phz += mass * vel.z();
1076 <          Khx += mass * vel.x() * vel.x();
1077 <          Khy += mass * vel.y() * vel.y();
1078 <          Khz += mass * vel.z() * vel.z();
1079 <          if (sd->isDirectional()) {
1080 <            Vector3d angMom = sd->getJ();
1081 <            Mat3x3d I = sd->getI();
1082 <            if (sd->isLinear()) {
827 <              int i = sd->linearAxis();
828 <              int j = (i + 1) % 3;
829 <              int k = (i + 2) % 3;
830 <              Khw += angMom[j] * angMom[j] / I(j, j) +
831 <                angMom[k] * angMom[k] / I(k, k);
832 <            } else {
833 <              Khw += angMom[0]*angMom[0]/I(0, 0)
834 <                + angMom[1]*angMom[1]/I(1, 1)
835 <                + angMom[2]*angMom[2]/I(2, 2);
836 <            }
837 <          }
838 <        } else {
839 <          coldBin.push_back(sd);
840 <          Pcx += mass * vel.x();
841 <          Pcy += mass * vel.y();
842 <          Pcz += mass * vel.z();
843 <          Kcx += mass * vel.x() * vel.x();
844 <          Kcy += mass * vel.y() * vel.y();
845 <          Kcz += mass * vel.z() * vel.z();
846 <          if (sd->isDirectional()) {
847 <            Vector3d angMom = sd->getJ();
848 <            Mat3x3d I = sd->getI();
849 <            if (sd->isLinear()) {
850 <              int i = sd->linearAxis();
851 <              int j = (i + 1) % 3;
852 <              int k = (i + 2) % 3;
853 <              Kcw += angMom[j] * angMom[j] / I(j, j) +
854 <                angMom[k] * angMom[k] / I(k, k);
855 <            } else {
856 <              Kcw += angMom[0]*angMom[0]/I(0, 0)
857 <                + angMom[1]*angMom[1]/I(1, 1)
858 <                + angMom[2]*angMom[2]/I(2, 2);
859 <            }
860 <          }
861 <        }
1062 >      coldBin.push_back(sd);
1063 >      Pcx += mass * vel.x();
1064 >      Pcy += mass * vel.y();
1065 >      Pcz += mass * vel.z();
1066 >      Kcx += mass * vel.x() * vel.x();
1067 >      Kcy += mass * vel.y() * vel.y();
1068 >      Kcz += mass * vel.z() * vel.z();
1069 >      if (sd->isDirectional()) {
1070 >        Vector3d angMom = sd->getJ();
1071 >        Mat3x3d I = sd->getI();
1072 >        if (sd->isLinear()) {
1073 >          int i = sd->linearAxis();
1074 >          int j = (i + 1) % 3;
1075 >          int k = (i + 2) % 3;
1076 >          Kcw += angMom[j] * angMom[j] / I(j, j) +
1077 >            angMom[k] * angMom[k] / I(k, k);
1078 >        } else {
1079 >          Kcw += angMom[0]*angMom[0]/I(0, 0)
1080 >            + angMom[1]*angMom[1]/I(1, 1)
1081 >            + angMom[2]*angMom[2]/I(2, 2);
1082 >        }
1083        }
1084      }
1085      
# Line 872 | Line 1093 | namespace OpenMD {
1093      Kcw *= 0.5;
1094  
1095   #ifdef IS_MPI
1096 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
1097 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
1098 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phz, 1, MPI::REALTYPE, MPI::SUM);
1099 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcx, 1, MPI::REALTYPE, MPI::SUM);
1100 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcy, 1, MPI::REALTYPE, MPI::SUM);
1101 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcz, 1, MPI::REALTYPE, MPI::SUM);
1096 >    MPI_Allreduce(MPI_IN_PLACE, &Phx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1097 >    MPI_Allreduce(MPI_IN_PLACE, &Phy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1098 >    MPI_Allreduce(MPI_IN_PLACE, &Phz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1099 >    MPI_Allreduce(MPI_IN_PLACE, &Pcx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1100 >    MPI_Allreduce(MPI_IN_PLACE, &Pcy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1101 >    MPI_Allreduce(MPI_IN_PLACE, &Pcz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1102  
1103 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
1104 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
1105 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
1106 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khw, 1, MPI::REALTYPE, MPI::SUM);
1103 >    MPI_Allreduce(MPI_IN_PLACE, &Khx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1104 >    MPI_Allreduce(MPI_IN_PLACE, &Khy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1105 >    MPI_Allreduce(MPI_IN_PLACE, &Khz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1106 >    MPI_Allreduce(MPI_IN_PLACE, &Khw, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1107  
1108 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
1109 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
1110 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
1111 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcw, 1, MPI::REALTYPE, MPI::SUM);
1108 >    MPI_Allreduce(MPI_IN_PLACE, &Kcx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1109 >    MPI_Allreduce(MPI_IN_PLACE, &Kcy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1110 >    MPI_Allreduce(MPI_IN_PLACE, &Kcz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1111 >    MPI_Allreduce(MPI_IN_PLACE, &Kcw, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1112   #endif
1113  
1114      //solve coldBin coeff's first
# Line 908 | Line 1129 | namespace OpenMD {
1129  
1130        if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
1131          c = sqrt(c);
1132 <        //std::cerr << "cold slab scaling coefficient: " << c << endl;
912 <        //now convert to hotBin coefficient
1132 >
1133          RealType w = 0.0;
1134          if (rnemdFluxType_ ==  rnemdFullKE) {
1135            x = 1.0 + px * (1.0 - c);
# Line 936 | Line 1156 | namespace OpenMD {
1156            //if w is in the right range, so should be x, y, z.
1157            vector<StuntDouble*>::iterator sdi;
1158            Vector3d vel;
1159 <          for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1159 >          for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1160              if (rnemdFluxType_ == rnemdFullKE) {
1161                vel = (*sdi)->getVel() * c;
1162                (*sdi)->setVel(vel);
# Line 947 | Line 1167 | namespace OpenMD {
1167              }
1168            }
1169            w = sqrt(w);
1170 <          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
951 <          //           << "\twh= " << w << endl;
952 <          for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1170 >          for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1171              if (rnemdFluxType_ == rnemdFullKE) {
1172                vel = (*sdi)->getVel();
1173                vel.x() *= x;
# Line 1068 | Line 1286 | namespace OpenMD {
1286        vector<RealType>::iterator ri;
1287        RealType r1, r2, alpha0;
1288        vector<pair<RealType,RealType> > rps;
1289 <      for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
1289 >      for (ri = realRoots.begin(); ri !=realRoots.end(); ++ri) {
1290          r2 = *ri;
1291          //check if FindRealRoots() give the right answer
1292          if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
# Line 1100 | Line 1318 | namespace OpenMD {
1318          RealType diff;
1319          pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
1320          vector<pair<RealType,RealType> >::iterator rpi;
1321 <        for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1321 >        for (rpi = rps.begin(); rpi != rps.end(); ++rpi) {
1322            r1 = (*rpi).first;
1323            r2 = (*rpi).second;
1324            switch(rnemdFluxType_) {
# Line 1167 | Line 1385 | namespace OpenMD {
1385          }
1386          vector<StuntDouble*>::iterator sdi;
1387          Vector3d vel;
1388 <        for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1388 >        for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1389            vel = (*sdi)->getVel();
1390            vel.x() *= x;
1391            vel.y() *= y;
# Line 1178 | Line 1396 | namespace OpenMD {
1396          x = 1.0 + px * (1.0 - x);
1397          y = 1.0 + py * (1.0 - y);
1398          z = 1.0 + pz * (1.0 - z);
1399 <        for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1399 >        for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1400            vel = (*sdi)->getVel();
1401            vel.x() *= x;
1402            vel.y() *= y;
# Line 1211 | Line 1429 | namespace OpenMD {
1429        failTrialCount_++;
1430      }
1431    }
1432 +  
1433 +  void RNEMD::doVSS(SelectionManager& smanA, SelectionManager& smanB) {
1434 +    if (!doRNEMD_) return;
1435 +    int selei;
1436 +    int selej;
1437  
1215  void RNEMD::doVSS() {
1216
1438      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1439      RealType time = currentSnap_->getTime();    
1440      Mat3x3d hmat = currentSnap_->getHmat();
1441  
1221    seleMan_.setSelectionSet(evaluator_.evaluate());
1222
1223    int selei;
1442      StuntDouble* sd;
1225    int idx;
1443  
1444      vector<StuntDouble*> hotBin, coldBin;
1445  
1446      Vector3d Ph(V3Zero);
1447 +    Vector3d Lh(V3Zero);
1448      RealType Mh = 0.0;
1449 +    Mat3x3d Ih(0.0);
1450      RealType Kh = 0.0;
1451      Vector3d Pc(V3Zero);
1452 +    Vector3d Lc(V3Zero);
1453      RealType Mc = 0.0;
1454 +    Mat3x3d Ic(0.0);
1455      RealType Kc = 0.0;
1235    
1456  
1457 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1458 <         sd = seleMan_.nextSelected(selei)) {
1457 >    // Constraints can be on only the linear or angular momentum, but
1458 >    // not both.  Usually, the user will specify which they want, but
1459 >    // in case they don't, the use of periodic boundaries should make
1460 >    // the choice for us.
1461 >    bool doLinearPart = false;
1462 >    bool doAngularPart = false;
1463  
1464 <      idx = sd->getLocalIndex();
1464 >    switch (rnemdFluxType_) {
1465 >    case rnemdPx:
1466 >    case rnemdPy:
1467 >    case rnemdPz:
1468 >    case rnemdPvector:
1469 >    case rnemdKePx:
1470 >    case rnemdKePy:
1471 >    case rnemdKePvector:
1472 >      doLinearPart = true;
1473 >      break;
1474 >    case rnemdLx:
1475 >    case rnemdLy:
1476 >    case rnemdLz:
1477 >    case rnemdLvector:
1478 >    case rnemdKeLx:
1479 >    case rnemdKeLy:
1480 >    case rnemdKeLz:
1481 >    case rnemdKeLvector:
1482 >      doAngularPart = true;
1483 >      break;
1484 >    case rnemdKE:
1485 >    case rnemdRotKE:
1486 >    case rnemdFullKE:
1487 >    default:
1488 >      if (usePeriodicBoundaryConditions_)
1489 >        doLinearPart = true;
1490 >      else
1491 >        doAngularPart = true;
1492 >      break;
1493 >    }
1494 >    
1495 >    for (sd = smanA.beginSelected(selei); sd != NULL;
1496 >         sd = smanA.nextSelected(selei)) {
1497  
1498        Vector3d pos = sd->getPos();
1499  
1500        // wrap the stuntdouble's position back into the box:
1501 <
1501 >      
1502        if (usePeriodicBoundaryConditions_)
1503          currentSnap_->wrapVector(pos);
1504 +      
1505 +      RealType mass = sd->getMass();
1506 +      Vector3d vel = sd->getVel();
1507 +      Vector3d rPos = sd->getPos() - coordinateOrigin_;
1508 +      RealType r2;
1509 +      
1510 +      hotBin.push_back(sd);
1511 +      Ph += mass * vel;
1512 +      Mh += mass;
1513 +      Kh += mass * vel.lengthSquare();
1514 +      Lh += mass * cross(rPos, vel);
1515 +      Ih -= outProduct(rPos, rPos) * mass;
1516 +      r2 = rPos.lengthSquare();
1517 +      Ih(0, 0) += mass * r2;
1518 +      Ih(1, 1) += mass * r2;
1519 +      Ih(2, 2) += mass * r2;
1520 +      
1521 +      if (rnemdFluxType_ == rnemdFullKE) {
1522 +        if (sd->isDirectional()) {
1523 +          Vector3d angMom = sd->getJ();
1524 +          Mat3x3d I = sd->getI();
1525 +          if (sd->isLinear()) {
1526 +            int i = sd->linearAxis();
1527 +            int j = (i + 1) % 3;
1528 +            int k = (i + 2) % 3;
1529 +            Kh += angMom[j] * angMom[j] / I(j, j) +
1530 +              angMom[k] * angMom[k] / I(k, k);
1531 +          } else {
1532 +            Kh += angMom[0] * angMom[0] / I(0, 0) +
1533 +              angMom[1] * angMom[1] / I(1, 1) +
1534 +              angMom[2] * angMom[2] / I(2, 2);
1535 +          }
1536 +        }
1537 +      }
1538 +    }
1539 +    for (sd = smanB.beginSelected(selej); sd != NULL;
1540 +         sd = smanB.nextSelected(selej)) {
1541  
1542 <      // which bin is this stuntdouble in?
1250 <      bool inA = inSlabA(pos);
1251 <      bool inB = inSlabB(pos);
1542 >      Vector3d pos = sd->getPos();
1543        
1544 <      if (inA || inB) {
1545 <        
1546 <        RealType mass = sd->getMass();
1547 <        Vector3d vel = sd->getVel();
1548 <      
1549 <        if (inA) {
1550 <          hotBin.push_back(sd);
1551 <          //std::cerr << "before, velocity = " << vel << endl;
1552 <          Ph += mass * vel;
1553 <          //std::cerr << "after, velocity = " << vel << endl;
1554 <          Mh += mass;
1555 <          Kh += mass * vel.lengthSquare();
1556 <          if (rnemdFluxType_ == rnemdFullKE) {
1557 <            if (sd->isDirectional()) {
1558 <              Vector3d angMom = sd->getJ();
1559 <              Mat3x3d I = sd->getI();
1560 <              if (sd->isLinear()) {
1561 <                int i = sd->linearAxis();
1562 <                int j = (i + 1) % 3;
1563 <                int k = (i + 2) % 3;
1564 <                Kh += angMom[j] * angMom[j] / I(j, j) +
1565 <                  angMom[k] * angMom[k] / I(k, k);
1566 <              } else {
1567 <                Kh += angMom[0] * angMom[0] / I(0, 0) +
1568 <                  angMom[1] * angMom[1] / I(1, 1) +
1569 <                  angMom[2] * angMom[2] / I(2, 2);
1570 <              }
1571 <            }
1572 <          }
1573 <        } else { //midBin_
1574 <          coldBin.push_back(sd);
1575 <          Pc += mass * vel;
1576 <          Mc += mass;
1577 <          Kc += mass * vel.lengthSquare();
1578 <          if (rnemdFluxType_ == rnemdFullKE) {
1579 <            if (sd->isDirectional()) {
1580 <              Vector3d angMom = sd->getJ();
1290 <              Mat3x3d I = sd->getI();
1291 <              if (sd->isLinear()) {
1292 <                int i = sd->linearAxis();
1293 <                int j = (i + 1) % 3;
1294 <                int k = (i + 2) % 3;
1295 <                Kc += angMom[j] * angMom[j] / I(j, j) +
1296 <                  angMom[k] * angMom[k] / I(k, k);
1297 <              } else {
1298 <                Kc += angMom[0] * angMom[0] / I(0, 0) +
1299 <                  angMom[1] * angMom[1] / I(1, 1) +
1300 <                  angMom[2] * angMom[2] / I(2, 2);
1301 <              }
1302 <            }
1303 <          }
1304 <        }
1544 >      // wrap the stuntdouble's position back into the box:
1545 >      
1546 >      if (usePeriodicBoundaryConditions_)
1547 >        currentSnap_->wrapVector(pos);
1548 >      
1549 >      RealType mass = sd->getMass();
1550 >      Vector3d vel = sd->getVel();
1551 >      Vector3d rPos = sd->getPos() - coordinateOrigin_;
1552 >      RealType r2;
1553 >
1554 >      coldBin.push_back(sd);
1555 >      Pc += mass * vel;
1556 >      Mc += mass;
1557 >      Kc += mass * vel.lengthSquare();
1558 >      Lc += mass * cross(rPos, vel);
1559 >      Ic -= outProduct(rPos, rPos) * mass;
1560 >      r2 = rPos.lengthSquare();
1561 >      Ic(0, 0) += mass * r2;
1562 >      Ic(1, 1) += mass * r2;
1563 >      Ic(2, 2) += mass * r2;
1564 >      
1565 >      if (rnemdFluxType_ == rnemdFullKE) {
1566 >        if (sd->isDirectional()) {
1567 >          Vector3d angMom = sd->getJ();
1568 >          Mat3x3d I = sd->getI();
1569 >          if (sd->isLinear()) {
1570 >            int i = sd->linearAxis();
1571 >            int j = (i + 1) % 3;
1572 >            int k = (i + 2) % 3;
1573 >            Kc += angMom[j] * angMom[j] / I(j, j) +
1574 >              angMom[k] * angMom[k] / I(k, k);
1575 >          } else {
1576 >            Kc += angMom[0] * angMom[0] / I(0, 0) +
1577 >              angMom[1] * angMom[1] / I(1, 1) +
1578 >              angMom[2] * angMom[2] / I(2, 2);
1579 >          }
1580 >        }
1581        }
1582      }
1583      
1584      Kh *= 0.5;
1585      Kc *= 0.5;
1310
1311    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1312    //        << "\tKc= " << Kc << endl;
1313    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1586      
1587   #ifdef IS_MPI
1588 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1589 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
1590 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mh, 1, MPI::REALTYPE, MPI::SUM);
1591 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kh, 1, MPI::REALTYPE, MPI::SUM);
1592 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mc, 1, MPI::REALTYPE, MPI::SUM);
1593 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kc, 1, MPI::REALTYPE, MPI::SUM);
1588 >    MPI_Allreduce(MPI_IN_PLACE, &Ph[0], 3, MPI_REALTYPE, MPI_SUM,
1589 >                  MPI_COMM_WORLD);
1590 >    MPI_Allreduce(MPI_IN_PLACE, &Pc[0], 3, MPI_REALTYPE, MPI_SUM,
1591 >                  MPI_COMM_WORLD);
1592 >    MPI_Allreduce(MPI_IN_PLACE, &Lh[0], 3, MPI_REALTYPE, MPI_SUM,
1593 >                  MPI_COMM_WORLD);
1594 >    MPI_Allreduce(MPI_IN_PLACE, &Lc[0], 3, MPI_REALTYPE, MPI_SUM,
1595 >                  MPI_COMM_WORLD);
1596 >    MPI_Allreduce(MPI_IN_PLACE, &Mh, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1597 >    MPI_Allreduce(MPI_IN_PLACE, &Kh, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1598 >    MPI_Allreduce(MPI_IN_PLACE, &Mc, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1599 >    MPI_Allreduce(MPI_IN_PLACE, &Kc, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1600 >    MPI_Allreduce(MPI_IN_PLACE, Ih.getArrayPointer(), 9,
1601 >                  MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1602 >    MPI_Allreduce(MPI_IN_PLACE, Ic.getArrayPointer(), 9,
1603 >                  MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1604   #endif
1605 +    
1606  
1607 +    Vector3d ac, acrec, bc, bcrec;
1608 +    Vector3d ah, ahrec, bh, bhrec;
1609 +
1610      bool successfulExchange = false;
1611      if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1612        Vector3d vc = Pc / Mc;
1613 <      Vector3d ac = -momentumTarget_ / Mc + vc;
1614 <      Vector3d acrec = -momentumTarget_ / Mc;
1615 <      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1613 >      ac = -momentumTarget_ / Mc + vc;
1614 >      acrec = -momentumTarget_ / Mc;
1615 >      
1616 >      // We now need the inverse of the inertia tensor to calculate the
1617 >      // angular velocity of the cold slab;
1618 >      Mat3x3d Ici = Ic.inverse();
1619 >      Vector3d omegac = Ici * Lc;
1620 >      bc  = -(Ici * angularMomentumTarget_) + omegac;
1621 >      bcrec = bc - omegac;
1622 >      
1623 >      RealType cNumerator = Kc - kineticTarget_;
1624 >      if (doLinearPart)
1625 >        cNumerator -= 0.5 * Mc * ac.lengthSquare();
1626 >      
1627 >      if (doAngularPart)
1628 >        cNumerator -= 0.5 * ( dot(bc, Ic * bc));
1629 >
1630        if (cNumerator > 0.0) {
1631 <        RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1631 >        
1632 >        RealType cDenominator = Kc;
1633 >
1634 >        if (doLinearPart)
1635 >          cDenominator -= 0.5 * Mc * vc.lengthSquare();
1636 >
1637 >        if (doAngularPart)
1638 >          cDenominator -= 0.5*(dot(omegac, Ic * omegac));
1639 >        
1640          if (cDenominator > 0.0) {
1641            RealType c = sqrt(cNumerator / cDenominator);
1642            if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1643 +            
1644              Vector3d vh = Ph / Mh;
1645 <            Vector3d ah = momentumTarget_ / Mh + vh;
1646 <            Vector3d ahrec = momentumTarget_ / Mh;
1647 <            RealType hNumerator = Kh + kineticTarget_
1648 <              - 0.5 * Mh * ah.lengthSquare();
1649 <            if (hNumerator > 0.0) {
1650 <              RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1645 >            ah = momentumTarget_ / Mh + vh;
1646 >            ahrec = momentumTarget_ / Mh;
1647 >            
1648 >            // We now need the inverse of the inertia tensor to
1649 >            // calculate the angular velocity of the hot slab;
1650 >            Mat3x3d Ihi = Ih.inverse();
1651 >            Vector3d omegah = Ihi * Lh;
1652 >            bh  = (Ihi * angularMomentumTarget_) + omegah;
1653 >            bhrec = bh - omegah;
1654 >            
1655 >            RealType hNumerator = Kh + kineticTarget_;
1656 >            if (doLinearPart)
1657 >              hNumerator -= 0.5 * Mh * ah.lengthSquare();
1658 >            
1659 >            if (doAngularPart)
1660 >              hNumerator -= 0.5 * ( dot(bh, Ih * bh));
1661 >              
1662 >            if (hNumerator > 0.0) {
1663 >              
1664 >              RealType hDenominator = Kh;
1665 >              if (doLinearPart)
1666 >                hDenominator -= 0.5 * Mh * vh.lengthSquare();
1667 >              if (doAngularPart)
1668 >                hDenominator -= 0.5*(dot(omegah, Ih * omegah));
1669 >              
1670                if (hDenominator > 0.0) {
1671                  RealType h = sqrt(hNumerator / hDenominator);
1672                  if ((h > 0.9) && (h < 1.1)) {
1673 <                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1346 <                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1673 >                  
1674                    vector<StuntDouble*>::iterator sdi;
1675                    Vector3d vel;
1676 <                  for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1676 >                  Vector3d rPos;
1677 >                  
1678 >                  for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1679                      //vel = (*sdi)->getVel();
1680 <                    vel = ((*sdi)->getVel() - vc) * c + ac;
1680 >                    rPos = (*sdi)->getPos() - coordinateOrigin_;
1681 >                    if (doLinearPart)
1682 >                      vel = ((*sdi)->getVel() - vc) * c + ac;
1683 >                    if (doAngularPart)
1684 >                      vel = ((*sdi)->getVel() - cross(omegac, rPos)) * c + cross(bc, rPos);
1685 >
1686                      (*sdi)->setVel(vel);
1687                      if (rnemdFluxType_ == rnemdFullKE) {
1688                        if ((*sdi)->isDirectional()) {
# Line 1357 | Line 1691 | namespace OpenMD {
1691                        }
1692                      }
1693                    }
1694 <                  for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1694 >                  for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1695                      //vel = (*sdi)->getVel();
1696 <                    vel = ((*sdi)->getVel() - vh) * h + ah;
1696 >                    rPos = (*sdi)->getPos() - coordinateOrigin_;
1697 >                    if (doLinearPart)
1698 >                      vel = ((*sdi)->getVel() - vh) * h + ah;    
1699 >                    if (doAngularPart)
1700 >                      vel = ((*sdi)->getVel() - cross(omegah, rPos)) * h + cross(bh, rPos);    
1701 >
1702                      (*sdi)->setVel(vel);
1703                      if (rnemdFluxType_ == rnemdFullKE) {
1704                        if ((*sdi)->isDirectional()) {
# Line 1371 | Line 1710 | namespace OpenMD {
1710                    successfulExchange = true;
1711                    kineticExchange_ += kineticTarget_;
1712                    momentumExchange_ += momentumTarget_;
1713 +                  angularMomentumExchange_ += angularMomentumTarget_;
1714                  }
1715                }
1716              }
# Line 1390 | Line 1730 | namespace OpenMD {
1730      }
1731    }
1732  
1733 <  void RNEMD::doRNEMD() {
1733 >  RealType RNEMD::getDividingArea() {
1734  
1735 +    if (hasDividingArea_) return dividingArea_;
1736 +
1737 +    RealType areaA, areaB;
1738 +    Snapshot* snap = info_->getSnapshotManager()->getCurrentSnapshot();
1739 +
1740 +    if (hasSelectionA_) {
1741 +
1742 +      if (evaluatorA_.hasSurfaceArea())
1743 +        areaA = evaluatorA_.getSurfaceArea();
1744 +      else {
1745 +        
1746 +        cerr << "selection A did not have surface area, recomputing\n";
1747 +        int isd;
1748 +        StuntDouble* sd;
1749 +        vector<StuntDouble*> aSites;
1750 +        seleManA_.setSelectionSet(evaluatorA_.evaluate());
1751 +        for (sd = seleManA_.beginSelected(isd); sd != NULL;
1752 +             sd = seleManA_.nextSelected(isd)) {
1753 +          aSites.push_back(sd);
1754 +        }
1755 + #if defined(HAVE_QHULL)
1756 +        ConvexHull* surfaceMeshA = new ConvexHull();
1757 +        surfaceMeshA->computeHull(aSites);
1758 +        areaA = surfaceMeshA->getArea();
1759 +        delete surfaceMeshA;
1760 + #else
1761 +        sprintf( painCave.errMsg,
1762 +               "RNEMD::getDividingArea : Hull calculation is not possible\n"
1763 +                 "\twithout libqhull. Please rebuild OpenMD with qhull enabled.");
1764 +        painCave.severity = OPENMD_ERROR;
1765 +        painCave.isFatal = 1;
1766 +        simError();
1767 + #endif
1768 +      }
1769 +
1770 +    } else {
1771 +      if (usePeriodicBoundaryConditions_) {
1772 +        // in periodic boundaries, the surface area is twice the x-y
1773 +        // area of the current box:
1774 +        areaA = 2.0 * snap->getXYarea();
1775 +      } else {
1776 +        // in non-periodic simulations, without explicitly setting
1777 +        // selections, the sphere radius sets the surface area of the
1778 +        // dividing surface:
1779 +        areaA = 4.0 * M_PI * pow(sphereARadius_, 2);
1780 +      }
1781 +    }
1782 +
1783 +    if (hasSelectionB_) {
1784 +      if (evaluatorB_.hasSurfaceArea())
1785 +        areaB = evaluatorB_.getSurfaceArea();
1786 +      else {
1787 +        cerr << "selection B did not have surface area, recomputing\n";
1788 +
1789 +        int isd;
1790 +        StuntDouble* sd;
1791 +        vector<StuntDouble*> bSites;
1792 +        seleManB_.setSelectionSet(evaluatorB_.evaluate());
1793 +        for (sd = seleManB_.beginSelected(isd); sd != NULL;
1794 +             sd = seleManB_.nextSelected(isd)) {
1795 +          bSites.push_back(sd);
1796 +        }
1797 +        
1798 + #if defined(HAVE_QHULL)
1799 +        ConvexHull* surfaceMeshB = new ConvexHull();    
1800 +        surfaceMeshB->computeHull(bSites);
1801 +        areaB = surfaceMeshB->getArea();
1802 +        delete surfaceMeshB;
1803 + #else
1804 +        sprintf( painCave.errMsg,
1805 +                 "RNEMD::getDividingArea : Hull calculation is not possible\n"
1806 +                 "\twithout libqhull. Please rebuild OpenMD with qhull enabled.");
1807 +        painCave.severity = OPENMD_ERROR;
1808 +        painCave.isFatal = 1;
1809 +        simError();
1810 + #endif
1811 +      }
1812 +      
1813 +    } else {
1814 +      if (usePeriodicBoundaryConditions_) {
1815 +        // in periodic boundaries, the surface area is twice the x-y
1816 +        // area of the current box:
1817 +        areaB = 2.0 * snap->getXYarea();
1818 +      } else {
1819 +        // in non-periodic simulations, without explicitly setting
1820 +        // selections, but if a sphereBradius has been set, just use that:
1821 +        areaB = 4.0 * M_PI * pow(sphereBRadius_, 2);
1822 +      }
1823 +    }
1824 +      
1825 +    dividingArea_ = min(areaA, areaB);
1826 +    hasDividingArea_ = true;
1827 +    return dividingArea_;
1828 +  }
1829 +  
1830 +  void RNEMD::doRNEMD() {
1831 +    if (!doRNEMD_) return;
1832      trialCount_++;
1833 +
1834 +    // object evaluator:
1835 +    evaluator_.loadScriptString(rnemdObjectSelection_);
1836 +    seleMan_.setSelectionSet(evaluator_.evaluate());
1837 +
1838 +    evaluatorA_.loadScriptString(selectionA_);
1839 +    evaluatorB_.loadScriptString(selectionB_);
1840 +
1841 +    seleManA_.setSelectionSet(evaluatorA_.evaluate());
1842 +    seleManB_.setSelectionSet(evaluatorB_.evaluate());
1843 +
1844 +    commonA_ = seleManA_ & seleMan_;
1845 +    commonB_ = seleManB_ & seleMan_;
1846 +
1847 +    // Target exchange quantities (in each exchange) = dividingArea * dt * flux
1848 +    // dt = exchange time interval
1849 +    // flux = target flux
1850 +    // dividingArea = smallest dividing surface between the two regions
1851 +
1852 +    hasDividingArea_ = false;
1853 +    RealType area = getDividingArea();
1854 +
1855 +    kineticTarget_ = kineticFlux_ * exchangeTime_ * area;
1856 +    momentumTarget_ = momentumFluxVector_ * exchangeTime_ * area;
1857 +    angularMomentumTarget_ = angularMomentumFluxVector_ * exchangeTime_ * area;
1858 +
1859      switch(rnemdMethod_) {
1860      case rnemdSwap:
1861 <      doSwap();
1861 >      doSwap(commonA_, commonB_);
1862        break;
1863      case rnemdNIVS:
1864 <      doNIVS();
1864 >      doNIVS(commonA_, commonB_);
1865        break;
1866      case rnemdVSS:
1867 <      doVSS();
1867 >      doVSS(commonA_, commonB_);
1868        break;
1869      case rnemdUnkownMethod:
1870      default :
# Line 1410 | Line 1873 | namespace OpenMD {
1873    }
1874  
1875    void RNEMD::collectData() {
1876 <
1876 >    if (!doRNEMD_) return;
1877      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1878 +    
1879 +    // collectData can be called more frequently than the doRNEMD, so use the
1880 +    // computed area from the last exchange time:
1881 +    RealType area = getDividingArea();
1882 +    areaAccumulator_->add(area);
1883      Mat3x3d hmat = currentSnap_->getHmat();
1884 +    Vector3d u = angularMomentumFluxVector_;
1885 +    u.normalize();
1886  
1887      seleMan_.setSelectionSet(evaluator_.evaluate());
1888  
1889 <    int selei;
1889 >    int selei(0);
1890      StuntDouble* sd;
1891 <    int idx;
1891 >    int binNo;
1892 >    RealType mass;
1893 >    Vector3d vel;
1894 >    Vector3d rPos;
1895 >    RealType KE;
1896 >    Vector3d L;
1897 >    Mat3x3d I;
1898 >    RealType r2;
1899  
1900      vector<RealType> binMass(nBins_, 0.0);
1901 <    vector<RealType> binPx(nBins_, 0.0);
1902 <    vector<RealType> binPy(nBins_, 0.0);
1903 <    vector<RealType> binPz(nBins_, 0.0);
1901 >    vector<Vector3d> binP(nBins_, V3Zero);
1902 >    vector<RealType> binOmega(nBins_, 0.0);
1903 >    vector<Vector3d> binL(nBins_, V3Zero);
1904 >    vector<Mat3x3d>  binI(nBins_);
1905      vector<RealType> binKE(nBins_, 0.0);
1906      vector<int> binDOF(nBins_, 0);
1907      vector<int> binCount(nBins_, 0);
# Line 1431 | Line 1909 | namespace OpenMD {
1909      // alternative approach, track all molecules instead of only those
1910      // selected for scaling/swapping:
1911      /*
1912 <    SimInfo::MoleculeIterator miter;
1913 <    vector<StuntDouble*>::iterator iiter;
1914 <    Molecule* mol;
1915 <    StuntDouble* sd;
1916 <    for (mol = info_->beginMolecule(miter); mol != NULL;
1912 >      SimInfo::MoleculeIterator miter;
1913 >      vector<StuntDouble*>::iterator iiter;
1914 >      Molecule* mol;
1915 >      StuntDouble* sd;
1916 >      for (mol = info_->beginMolecule(miter); mol != NULL;
1917        mol = info_->nextMolecule(miter))
1918        sd is essentially sd
1919 <        for (sd = mol->beginIntegrableObject(iiter);
1920 <             sd != NULL;
1921 <             sd = mol->nextIntegrableObject(iiter))
1919 >      for (sd = mol->beginIntegrableObject(iiter);
1920 >      sd != NULL;
1921 >      sd = mol->nextIntegrableObject(iiter))
1922      */
1923 +
1924      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1925 <         sd = seleMan_.nextSelected(selei)) {
1926 <      
1448 <      idx = sd->getLocalIndex();
1449 <      
1925 >         sd = seleMan_.nextSelected(selei)) {    
1926 >    
1927        Vector3d pos = sd->getPos();
1928  
1929        // wrap the stuntdouble's position back into the box:
1930        
1931 <      if (usePeriodicBoundaryConditions_)
1931 >      if (usePeriodicBoundaryConditions_) {
1932          currentSnap_->wrapVector(pos);
1933 +        // which bin is this stuntdouble in?
1934 +        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1935 +        // Shift molecules by half a box to have bins start at 0
1936 +        // The modulo operator is used to wrap the case when we are
1937 +        // beyond the end of the bins back to the beginning.
1938 +        binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1939 +      } else {
1940 +        Vector3d rPos = pos - coordinateOrigin_;
1941 +        binNo = int(rPos.length() / binWidth_);
1942 +      }
1943  
1944 <      // which bin is this stuntdouble in?
1945 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1946 <      // Shift molecules by half a box to have bins start at 0
1947 <      // The modulo operator is used to wrap the case when we are
1948 <      // beyond the end of the bins back to the beginning.
1949 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1950 <    
1951 <      RealType mass = sd->getMass();
1952 <      Vector3d vel = sd->getVel();
1944 >      mass = sd->getMass();
1945 >      vel = sd->getVel();
1946 >      rPos = sd->getPos() - coordinateOrigin_;
1947 >      KE = 0.5 * mass * vel.lengthSquare();
1948 >      L = mass * cross(rPos, vel);
1949 >      I = outProduct(rPos, rPos) * mass;
1950 >      r2 = rPos.lengthSquare();
1951 >      I(0, 0) += mass * r2;
1952 >      I(1, 1) += mass * r2;
1953 >      I(2, 2) += mass * r2;
1954  
1955 <      binCount[binNo]++;
1956 <      binMass[binNo] += mass;
1957 <      binPx[binNo] += mass*vel.x();
1958 <      binPy[binNo] += mass*vel.y();
1959 <      binPz[binNo] += mass*vel.z();
1960 <      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1961 <      binDOF[binNo] += 3;
1955 >      // Project the relative position onto a plane perpendicular to
1956 >      // the angularMomentumFluxVector:
1957 >      // Vector3d rProj = rPos - dot(rPos, u) * u;
1958 >      // Project the velocity onto a plane perpendicular to the
1959 >      // angularMomentumFluxVector:
1960 >      // Vector3d vProj = vel  - dot(vel, u) * u;
1961 >      // Compute angular velocity vector (should be nearly parallel to
1962 >      // angularMomentumFluxVector
1963 >      // Vector3d aVel = cross(rProj, vProj);
1964  
1965 <      if (sd->isDirectional()) {
1966 <        Vector3d angMom = sd->getJ();
1967 <        Mat3x3d I = sd->getI();
1968 <        if (sd->isLinear()) {
1969 <          int i = sd->linearAxis();
1970 <          int j = (i + 1) % 3;
1971 <          int k = (i + 2) % 3;
1972 <          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
1973 <                                 angMom[k] * angMom[k] / I(k, k));
1974 <          binDOF[binNo] += 2;
1975 <        } else {
1976 <          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
1977 <                                 angMom[1] * angMom[1] / I(1, 1) +
1978 <                                 angMom[2] * angMom[2] / I(2, 2));
1979 <          binDOF[binNo] += 3;
1965 >      if (binNo >= 0 && binNo < nBins_)  {
1966 >        binCount[binNo]++;
1967 >        binMass[binNo] += mass;
1968 >        binP[binNo] += mass*vel;
1969 >        binKE[binNo] += KE;
1970 >        binI[binNo] += I;
1971 >        binL[binNo] += L;
1972 >        binDOF[binNo] += 3;
1973 >        
1974 >        if (sd->isDirectional()) {
1975 >          Vector3d angMom = sd->getJ();
1976 >          Mat3x3d Ia = sd->getI();
1977 >          if (sd->isLinear()) {
1978 >            int i = sd->linearAxis();
1979 >            int j = (i + 1) % 3;
1980 >            int k = (i + 2) % 3;
1981 >            binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / Ia(j, j) +
1982 >                                   angMom[k] * angMom[k] / Ia(k, k));
1983 >            binDOF[binNo] += 2;
1984 >          } else {
1985 >            binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / Ia(0, 0) +
1986 >                                   angMom[1] * angMom[1] / Ia(1, 1) +
1987 >                                   angMom[2] * angMom[2] / Ia(2, 2));
1988 >            binDOF[binNo] += 3;
1989 >          }
1990          }
1991        }
1992      }
1993      
1494
1994   #ifdef IS_MPI
1995 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
1996 <                              nBins_, MPI::INT, MPI::SUM);
1997 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
1998 <                              nBins_, MPI::REALTYPE, MPI::SUM);
1999 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
2000 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2001 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
2002 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2003 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
2004 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2005 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
2006 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2007 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
2008 <                              nBins_, MPI::INT, MPI::SUM);
1995 >
1996 >    for (int i = 0; i < nBins_; i++) {
1997 >      
1998 >      MPI_Allreduce(MPI_IN_PLACE, &binCount[i],
1999 >                    1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
2000 >      MPI_Allreduce(MPI_IN_PLACE, &binMass[i],
2001 >                    1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2002 >      MPI_Allreduce(MPI_IN_PLACE, &binP[i],
2003 >                    3, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2004 >      MPI_Allreduce(MPI_IN_PLACE, &binL[i],
2005 >                    3, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2006 >      MPI_Allreduce(MPI_IN_PLACE, &binI[i],
2007 >                    9, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2008 >      MPI_Allreduce(MPI_IN_PLACE, &binKE[i],
2009 >                    1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2010 >      MPI_Allreduce(MPI_IN_PLACE, &binDOF[i],
2011 >                    1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
2012 >      //MPI_Allreduce(MPI_IN_PLACE, &binOmega[i],
2013 >      //                          1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2014 >    }
2015 >    
2016   #endif
2017  
2018 <    Vector3d vel;
2018 >    Vector3d omega;
2019      RealType den;
2020      RealType temp;
2021      RealType z;
2022 +    RealType r;
2023      for (int i = 0; i < nBins_; i++) {
2024 <      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
2025 <      vel.x() = binPx[i] / binMass[i];
2026 <      vel.y() = binPy[i] / binMass[i];
2027 <      vel.z() = binPz[i] / binMass[i];
2028 <      den = binCount[i] * nBins_ / (hmat(0,0) * hmat(1,1) * hmat(2,2));
2029 <      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
2030 <                               PhysicalConstants::energyConvert);
2024 >      if (usePeriodicBoundaryConditions_) {
2025 >        z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
2026 >        den = binMass[i] * nBins_ * PhysicalConstants::densityConvert
2027 >          / currentSnap_->getVolume() ;
2028 >      } else {
2029 >        r = (((RealType)i + 0.5) * binWidth_);
2030 >        RealType rinner = (RealType)i * binWidth_;
2031 >        RealType router = (RealType)(i+1) * binWidth_;
2032 >        den = binMass[i] * 3.0 * PhysicalConstants::densityConvert
2033 >          / (4.0 * M_PI * (pow(router,3) - pow(rinner,3)));
2034 >      }
2035 >      vel = binP[i] / binMass[i];
2036  
2037 <      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
2038 <        if(outputMask_[j]) {
2039 <          switch(j) {
2040 <          case Z:
2041 <            (data_[j].accumulator[i])->add(z);
2042 <            break;
2043 <          case TEMPERATURE:
2044 <            data_[j].accumulator[i]->add(temp);
2045 <            break;
2046 <          case VELOCITY:
2047 <            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
2048 <            break;
2049 <          case DENSITY:
2050 <            data_[j].accumulator[i]->add(den);
2051 <            break;
2037 >      omega = binI[i].inverse() * binL[i];
2038 >
2039 >      // omega = binOmega[i] / binCount[i];
2040 >
2041 >      if (binCount[i] > 0) {
2042 >        // only add values if there are things to add
2043 >        temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
2044 >                                 PhysicalConstants::energyConvert);
2045 >        
2046 >        for (unsigned int j = 0; j < outputMask_.size(); ++j) {
2047 >          if(outputMask_[j]) {
2048 >            switch(j) {
2049 >            case Z:
2050 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(z);
2051 >              break;
2052 >            case R:
2053 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(r);
2054 >              break;
2055 >            case TEMPERATURE:
2056 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(temp);
2057 >              break;
2058 >            case VELOCITY:
2059 >              dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
2060 >              break;
2061 >            case ANGULARVELOCITY:  
2062 >              dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(omega);
2063 >              break;
2064 >            case DENSITY:
2065 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(den);
2066 >              break;
2067 >            }
2068            }
2069          }
2070        }
2071      }
2072 +    hasData_ = true;
2073    }
2074  
2075    void RNEMD::getStarted() {
2076 +    if (!doRNEMD_) return;
2077 +    hasDividingArea_ = false;
2078      collectData();
2079      writeOutputFile();
2080    }
2081  
2082    void RNEMD::parseOutputFileFormat(const std::string& format) {
2083 +    if (!doRNEMD_) return;
2084      StringTokenizer tokenizer(format, " ,;|\t\n\r");
2085      
2086      while(tokenizer.hasMoreTokens()) {
# Line 1569 | Line 2101 | namespace OpenMD {
2101    }
2102    
2103    void RNEMD::writeOutputFile() {
2104 +    if (!doRNEMD_) return;
2105 +    if (!hasData_) return;
2106      
2107   #ifdef IS_MPI
2108      // If we're the root node, should we print out the results
2109 <    int worldRank = MPI::COMM_WORLD.Get_rank();
2109 >    int worldRank;
2110 >    MPI_Comm_rank( MPI_COMM_WORLD, &worldRank);
2111 >
2112      if (worldRank == 0) {
2113   #endif
2114        rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
# Line 1588 | Line 2124 | namespace OpenMD {
2124        Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
2125  
2126        RealType time = currentSnap_->getTime();
2127 <      
2128 <      
2127 >      RealType avgArea;
2128 >      areaAccumulator_->getAverage(avgArea);
2129 >
2130 >      RealType Jz(0.0);
2131 >      Vector3d JzP(V3Zero);
2132 >      Vector3d JzL(V3Zero);
2133 >      if (time >= info_->getSimParams()->getDt()) {
2134 >        Jz = kineticExchange_ / (time * avgArea)
2135 >          / PhysicalConstants::energyConvert;
2136 >        JzP = momentumExchange_ / (time * avgArea);
2137 >        JzL = angularMomentumExchange_ / (time * avgArea);
2138 >      }
2139 >
2140        rnemdFile_ << "#######################################################\n";
2141        rnemdFile_ << "# RNEMD {\n";
2142  
2143        map<string, RNEMDMethod>::iterator mi;
2144        for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
2145          if ( (*mi).second == rnemdMethod_)
2146 <          rnemdFile_ << "#    exchangeMethod  = " << (*mi).first << "\n";
2146 >          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
2147        }
2148        map<string, RNEMDFluxType>::iterator fi;
2149        for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
2150          if ( (*fi).second == rnemdFluxType_)
2151 <          rnemdFile_ << "#    fluxType  = " << (*fi).first << "\n";
2151 >          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
2152        }
2153        
2154 <      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << " fs\n";
2154 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
2155  
2156        rnemdFile_ << "#    objectSelection = \""
2157 <                 << rnemdObjectSelection_ << "\"\n";
2158 <      rnemdFile_ << "#    slabWidth = " << slabWidth_ << " angstroms\n";
2159 <      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << " angstroms\n";
1613 <      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << " angstroms\n";
2157 >                 << rnemdObjectSelection_ << "\";\n";
2158 >      rnemdFile_ << "#    selectionA = \"" << selectionA_ << "\";\n";
2159 >      rnemdFile_ << "#    selectionB = \"" << selectionB_ << "\";\n";
2160        rnemdFile_ << "# }\n";
2161        rnemdFile_ << "#######################################################\n";
2162 <      
2163 <      rnemdFile_ << "# running time = " << time << " fs\n";
2164 <      rnemdFile_ << "# target kinetic flux = " << kineticFlux_ << "\n";
2165 <      rnemdFile_ << "# target momentum flux = " << momentumFluxVector_ << "\n";
2166 <      
2167 <      rnemdFile_ << "# target one-time kinetic exchange = " << kineticTarget_
2168 <                 << "\n";
2169 <      rnemdFile_ << "# target one-time momentum exchange = " << momentumTarget_
2170 <                 << "\n";
2171 <      
2172 <      rnemdFile_ << "# actual kinetic exchange = " << kineticExchange_ << "\n";
2173 <      rnemdFile_ << "# actual momentum exchange = " << momentumExchange_
2174 <                 << "\n";
2175 <      
2176 <      rnemdFile_ << "# attempted exchanges: " << trialCount_ << "\n";
2177 <      rnemdFile_ << "# failed exchanges: " << failTrialCount_ << "\n";
2178 <
2179 <      
2162 >      rnemdFile_ << "# RNEMD report:\n";      
2163 >      rnemdFile_ << "#      running time = " << time << " fs\n";
2164 >      rnemdFile_ << "# Target flux:\n";
2165 >      rnemdFile_ << "#           kinetic = "
2166 >                 << kineticFlux_ / PhysicalConstants::energyConvert
2167 >                 << " (kcal/mol/A^2/fs)\n";
2168 >      rnemdFile_ << "#          momentum = " << momentumFluxVector_
2169 >                 << " (amu/A/fs^2)\n";
2170 >      rnemdFile_ << "#  angular momentum = " << angularMomentumFluxVector_
2171 >                 << " (amu/A^2/fs^2)\n";
2172 >      rnemdFile_ << "# Target one-time exchanges:\n";
2173 >      rnemdFile_ << "#          kinetic = "
2174 >                 << kineticTarget_ / PhysicalConstants::energyConvert
2175 >                 << " (kcal/mol)\n";
2176 >      rnemdFile_ << "#          momentum = " << momentumTarget_
2177 >                 << " (amu*A/fs)\n";
2178 >      rnemdFile_ << "#  angular momentum = " << angularMomentumTarget_
2179 >                 << " (amu*A^2/fs)\n";
2180 >      rnemdFile_ << "# Actual exchange totals:\n";
2181 >      rnemdFile_ << "#          kinetic = "
2182 >                 << kineticExchange_ / PhysicalConstants::energyConvert
2183 >                 << " (kcal/mol)\n";
2184 >      rnemdFile_ << "#          momentum = " << momentumExchange_
2185 >                 << " (amu*A/fs)\n";      
2186 >      rnemdFile_ << "#  angular momentum = " << angularMomentumExchange_
2187 >                 << " (amu*A^2/fs)\n";      
2188 >      rnemdFile_ << "# Actual flux:\n";
2189 >      rnemdFile_ << "#          kinetic = " << Jz
2190 >                 << " (kcal/mol/A^2/fs)\n";
2191 >      rnemdFile_ << "#          momentum = " << JzP
2192 >                 << " (amu/A/fs^2)\n";
2193 >      rnemdFile_ << "#  angular momentum = " << JzL
2194 >                 << " (amu/A^2/fs^2)\n";
2195 >      rnemdFile_ << "# Exchange statistics:\n";
2196 >      rnemdFile_ << "#               attempted = " << trialCount_ << "\n";
2197 >      rnemdFile_ << "#                  failed = " << failTrialCount_ << "\n";
2198        if (rnemdMethod_ == rnemdNIVS) {
2199 <        rnemdFile_ << "# NIVS root-check warnings: " << failRootCount_ << "\n";
2199 >        rnemdFile_ << "#  NIVS root-check errors = "
2200 >                   << failRootCount_ << "\n";
2201        }
1637
2202        rnemdFile_ << "#######################################################\n";
2203        
2204        
# Line 1645 | Line 2209 | namespace OpenMD {
2209          if (outputMask_[i]) {
2210            rnemdFile_ << "\t" << data_[i].title <<
2211              "(" << data_[i].units << ")";
2212 +          // add some extra tabs for column alignment
2213 +          if (data_[i].dataType == "Vector3d") rnemdFile_ << "\t\t";
2214          }
2215        }
2216        rnemdFile_ << std::endl;
2217        
2218        rnemdFile_.precision(8);
2219        
2220 <      for (unsigned int j = 0; j < nBins_; j++) {        
2220 >      for (int j = 0; j < nBins_; j++) {        
2221          
2222          for (unsigned int i = 0; i < outputMask_.size(); ++i) {
2223            if (outputMask_[i]) {
2224              if (data_[i].dataType == "RealType")
2225                writeReal(i,j);
2226 <            else if (data_[i].dataType == "Vector3d")
2226 >            else if (data_[i].dataType == "Vector3d")
2227                writeVector(i,j);
2228              else {
2229                sprintf( painCave.errMsg,
# Line 1671 | Line 2237 | namespace OpenMD {
2237          rnemdFile_ << std::endl;
2238          
2239        }        
2240 +
2241 +      rnemdFile_ << "#######################################################\n";
2242 +      rnemdFile_ << "# Standard Deviations in those quantities follow:\n";
2243 +      rnemdFile_ << "#######################################################\n";
2244 +
2245 +
2246 +      for (int j = 0; j < nBins_; j++) {        
2247 +        rnemdFile_ << "#";
2248 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
2249 +          if (outputMask_[i]) {
2250 +            if (data_[i].dataType == "RealType")
2251 +              writeRealStdDev(i,j);
2252 +            else if (data_[i].dataType == "Vector3d")
2253 +              writeVectorStdDev(i,j);
2254 +            else {
2255 +              sprintf( painCave.errMsg,
2256 +                       "RNEMD found an unknown data type for: %s ",
2257 +                       data_[i].title.c_str());
2258 +              painCave.isFatal = 1;
2259 +              simError();
2260 +            }
2261 +          }
2262 +        }
2263 +        rnemdFile_ << std::endl;
2264 +        
2265 +      }        
2266        
2267        rnemdFile_.flush();
2268        rnemdFile_.close();
# Line 1682 | Line 2274 | namespace OpenMD {
2274    }
2275    
2276    void RNEMD::writeReal(int index, unsigned int bin) {
2277 +    if (!doRNEMD_) return;
2278      assert(index >=0 && index < ENDINDEX);
2279 <    assert(bin >=0 && bin < nBins_);
2279 >    assert(int(bin) < nBins_);
2280      RealType s;
2281 +    int count;
2282      
2283 <    data_[index].accumulator[bin]->getAverage(s);
2283 >    count = data_[index].accumulator[bin]->count();
2284 >    if (count == 0) return;
2285      
2286 +    dynamic_cast<Accumulator *>(data_[index].accumulator[bin])->getAverage(s);
2287 +    
2288      if (! isinf(s) && ! isnan(s)) {
2289        rnemdFile_ << "\t" << s;
2290      } else{
2291        sprintf( painCave.errMsg,
2292 <               "RNEMD detected a numerical error writing: %s for bin %d",
2292 >               "RNEMD detected a numerical error writing: %s for bin %u",
2293                 data_[index].title.c_str(), bin);
2294        painCave.isFatal = 1;
2295        simError();
# Line 1700 | Line 2297 | namespace OpenMD {
2297    }
2298    
2299    void RNEMD::writeVector(int index, unsigned int bin) {
2300 +    if (!doRNEMD_) return;
2301      assert(index >=0 && index < ENDINDEX);
2302 <    assert(bin >=0 && bin < nBins_);
2302 >    assert(int(bin) < nBins_);
2303      Vector3d s;
2304 +    int count;
2305 +    
2306 +    count = data_[index].accumulator[bin]->count();
2307 +
2308 +    if (count == 0) return;
2309 +
2310      dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
2311      if (isinf(s[0]) || isnan(s[0]) ||
2312          isinf(s[1]) || isnan(s[1]) ||
2313          isinf(s[2]) || isnan(s[2]) ) {      
2314        sprintf( painCave.errMsg,
2315 <               "RNEMD detected a numerical error writing: %s for bin %d",
2315 >               "RNEMD detected a numerical error writing: %s for bin %u",
2316                 data_[index].title.c_str(), bin);
2317        painCave.isFatal = 1;
2318        simError();
# Line 1716 | Line 2320 | namespace OpenMD {
2320        rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
2321      }
2322    }  
2323 +
2324 +  void RNEMD::writeRealStdDev(int index, unsigned int bin) {
2325 +    if (!doRNEMD_) return;
2326 +    assert(index >=0 && index < ENDINDEX);
2327 +    assert(int(bin) < nBins_);
2328 +    RealType s;
2329 +    int count;
2330 +    
2331 +    count = data_[index].accumulator[bin]->count();
2332 +    if (count == 0) return;
2333 +    
2334 +    dynamic_cast<Accumulator *>(data_[index].accumulator[bin])->getStdDev(s);
2335 +    
2336 +    if (! isinf(s) && ! isnan(s)) {
2337 +      rnemdFile_ << "\t" << s;
2338 +    } else{
2339 +      sprintf( painCave.errMsg,
2340 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %u",
2341 +               data_[index].title.c_str(), bin);
2342 +      painCave.isFatal = 1;
2343 +      simError();
2344 +    }    
2345 +  }
2346 +  
2347 +  void RNEMD::writeVectorStdDev(int index, unsigned int bin) {
2348 +    if (!doRNEMD_) return;
2349 +    assert(index >=0 && index < ENDINDEX);
2350 +    assert(int(bin) < nBins_);
2351 +    Vector3d s;
2352 +    int count;
2353 +    
2354 +    count = data_[index].accumulator[bin]->count();
2355 +    if (count == 0) return;
2356 +
2357 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getStdDev(s);
2358 +    if (isinf(s[0]) || isnan(s[0]) ||
2359 +        isinf(s[1]) || isnan(s[1]) ||
2360 +        isinf(s[2]) || isnan(s[2]) ) {      
2361 +      sprintf( painCave.errMsg,
2362 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %u",
2363 +               data_[index].title.c_str(), bin);
2364 +      painCave.isFatal = 1;
2365 +      simError();
2366 +    } else {
2367 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
2368 +    }
2369 +  }  
2370   }
2371  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines