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 2068 by gezelter, Thu Mar 5 15:40:58 2015 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      }
293      if (hasMomentumFluxVector) {
294 <      momentumFluxVector_ = rnemdParams->getMomentumFluxVector();
294 >      std::vector<RealType> mf = rnemdParams->getMomentumFluxVector();
295 >      if (mf.size() != 3) {
296 >          sprintf(painCave.errMsg,
297 >                  "RNEMD: Incorrect number of parameters specified for momentumFluxVector.\n"
298 >                  "\tthere should be 3 parameters, but %lu were specified.\n",
299 >                  mf.size());
300 >          painCave.isFatal = 1;
301 >          simError();      
302 >      }
303 >      momentumFluxVector_.x() = mf[0];
304 >      momentumFluxVector_.y() = mf[1];
305 >      momentumFluxVector_.z() = mf[2];
306      } else {
307        momentumFluxVector_ = V3Zero;
308        if (hasMomentumFlux) {
# Line 264 | Line 326 | namespace OpenMD {
326          default:
327            break;
328          }
329 <      }    
329 >      }
330 >    }
331 >    if (hasAngularMomentumFluxVector) {
332 >      std::vector<RealType> amf = rnemdParams->getAngularMomentumFluxVector();
333 >      if (amf.size() != 3) {
334 >        sprintf(painCave.errMsg,
335 >                "RNEMD: Incorrect number of parameters specified for angularMomentumFluxVector.\n"
336 >                "\tthere should be 3 parameters, but %lu were specified.\n",
337 >                amf.size());
338 >        painCave.isFatal = 1;
339 >        simError();      
340 >      }
341 >      angularMomentumFluxVector_.x() = amf[0];
342 >      angularMomentumFluxVector_.y() = amf[1];
343 >      angularMomentumFluxVector_.z() = amf[2];
344 >    } else {
345 >      angularMomentumFluxVector_ = V3Zero;
346 >      if (hasAngularMomentumFlux) {
347 >        RealType angularMomentumFlux = rnemdParams->getAngularMomentumFlux();
348 >        switch (rnemdFluxType_) {
349 >        case rnemdLx:
350 >          angularMomentumFluxVector_.x() = angularMomentumFlux;
351 >          break;
352 >        case rnemdLy:
353 >          angularMomentumFluxVector_.y() = angularMomentumFlux;
354 >          break;
355 >        case rnemdLz:
356 >          angularMomentumFluxVector_.z() = angularMomentumFlux;
357 >          break;
358 >        case rnemdKeLx:
359 >          angularMomentumFluxVector_.x() = angularMomentumFlux;
360 >          break;
361 >        case rnemdKeLy:
362 >          angularMomentumFluxVector_.y() = angularMomentumFlux;
363 >          break;
364 >        case rnemdKeLz:
365 >          angularMomentumFluxVector_.z() = angularMomentumFlux;
366 >          break;
367 >        default:
368 >          break;
369 >        }
370 >      }        
371      }
372  
373 +    if (hasCoordinateOrigin) {
374 +      std::vector<RealType> co = rnemdParams->getCoordinateOrigin();
375 +      if (co.size() != 3) {
376 +        sprintf(painCave.errMsg,
377 +                "RNEMD: Incorrect number of parameters specified for coordinateOrigin.\n"
378 +                "\tthere should be 3 parameters, but %lu were specified.\n",
379 +                co.size());
380 +        painCave.isFatal = 1;
381 +        simError();      
382 +      }
383 +      coordinateOrigin_.x() = co[0];
384 +      coordinateOrigin_.y() = co[1];
385 +      coordinateOrigin_.z() = co[2];
386 +    } else {
387 +      coordinateOrigin_ = V3Zero;
388 +    }
389 +    
390      // do some sanity checking
391 <
392 <    int selectionCount = seleMan_.getSelectionCount();
391 >    
392 >    int selectionCount = seleMan_.getSelectionCount();    
393      int nIntegrable = info->getNGlobalIntegrableObjects();
274
394      if (selectionCount > nIntegrable) {
395        sprintf(painCave.errMsg,
396                "RNEMD: The current objectSelection,\n"
# Line 287 | Line 406 | namespace OpenMD {
406        painCave.severity = OPENMD_WARNING;
407        simError();
408      }
409 <
409 >    
410 >    areaAccumulator_ = new Accumulator();
411 >    
412      nBins_ = rnemdParams->getOutputBins();
413 <
413 >    binWidth_ = rnemdParams->getOutputBinWidth();
414 >    
415      data_.resize(RNEMD::ENDINDEX);
416      OutputData z;
417      z.units =  "Angstroms";
418      z.title =  "Z";
419      z.dataType = "RealType";
420      z.accumulator.reserve(nBins_);
421 <    for (unsigned int i = 0; i < nBins_; i++)
421 >    for (int i = 0; i < nBins_; i++)
422        z.accumulator.push_back( new Accumulator() );
423      data_[Z] = z;
424      outputMap_["Z"] =  Z;
425 <
425 >    
426 >    OutputData r;
427 >    r.units =  "Angstroms";
428 >    r.title =  "R";
429 >    r.dataType = "RealType";
430 >    r.accumulator.reserve(nBins_);
431 >    for (int i = 0; i < nBins_; i++)
432 >      r.accumulator.push_back( new Accumulator() );
433 >    data_[R] = r;
434 >    outputMap_["R"] =  R;
435 >    
436      OutputData temperature;
437      temperature.units =  "K";
438      temperature.title =  "Temperature";
439      temperature.dataType = "RealType";
440      temperature.accumulator.reserve(nBins_);
441 <    for (unsigned int i = 0; i < nBins_; i++)
441 >    for (int i = 0; i < nBins_; i++)
442        temperature.accumulator.push_back( new Accumulator() );
443      data_[TEMPERATURE] = temperature;
444      outputMap_["TEMPERATURE"] =  TEMPERATURE;
445 <
445 >    
446      OutputData velocity;
447 <    velocity.units = "amu/fs";
447 >    velocity.units = "angstroms/fs";
448      velocity.title =  "Velocity";  
449      velocity.dataType = "Vector3d";
450      velocity.accumulator.reserve(nBins_);
451 <    for (unsigned int i = 0; i < nBins_; i++)
451 >    for (int i = 0; i < nBins_; i++)
452        velocity.accumulator.push_back( new VectorAccumulator() );
453      data_[VELOCITY] = velocity;
454      outputMap_["VELOCITY"] = VELOCITY;
455 <
455 >    
456 >    OutputData angularVelocity;
457 >    angularVelocity.units = "angstroms^2/fs";
458 >    angularVelocity.title =  "AngularVelocity";  
459 >    angularVelocity.dataType = "Vector3d";
460 >    angularVelocity.accumulator.reserve(nBins_);
461 >    for (int i = 0; i < nBins_; i++)
462 >      angularVelocity.accumulator.push_back( new VectorAccumulator() );
463 >    data_[ANGULARVELOCITY] = angularVelocity;
464 >    outputMap_["ANGULARVELOCITY"] = ANGULARVELOCITY;
465 >    
466      OutputData density;
467      density.units =  "g cm^-3";
468      density.title =  "Density";
469      density.dataType = "RealType";
470      density.accumulator.reserve(nBins_);
471 <    for (unsigned int i = 0; i < nBins_; i++)
471 >    for (int i = 0; i < nBins_; i++)
472        density.accumulator.push_back( new Accumulator() );
473      data_[DENSITY] = density;
474      outputMap_["DENSITY"] =  DENSITY;
475 <
475 >    
476      if (hasOutputFields) {
477        parseOutputFileFormat(rnemdParams->getOutputFields());
478      } else {
479 <      outputMask_.set(Z);
479 >      if (usePeriodicBoundaryConditions_)
480 >        outputMask_.set(Z);
481 >      else
482 >        outputMask_.set(R);
483        switch (rnemdFluxType_) {
484        case rnemdKE:
485        case rnemdRotKE:
# Line 350 | Line 495 | namespace OpenMD {
495          outputMask_.set(VELOCITY);
496          outputMask_.set(DENSITY);
497          break;
498 +      case rnemdLx:
499 +      case rnemdLy:
500 +      case rnemdLz:
501 +      case rnemdLvector:
502 +        outputMask_.set(ANGULARVELOCITY);
503 +        break;
504 +      case rnemdKeLx:
505 +      case rnemdKeLy:
506 +      case rnemdKeLz:
507 +      case rnemdKeLvector:
508 +        outputMask_.set(TEMPERATURE);
509 +        outputMask_.set(ANGULARVELOCITY);
510 +        break;
511        case rnemdKePx:
512        case rnemdKePy:
513          outputMask_.set(TEMPERATURE);
# Line 364 | Line 522 | namespace OpenMD {
522          break;
523        }
524      }
525 <      
525 >    
526      if (hasOutputFileName) {
527        rnemdFileName_ = rnemdParams->getOutputFileName();
528      } else {
529        rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
530      }          
531 <
531 >    
532      exchangeTime_ = rnemdParams->getExchangeTime();
533 <
533 >    
534      Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
377    Mat3x3d hmat = currentSnap_->getHmat();
378  
379    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
380    // Lx, Ly = box dimensions in x & y
381    // dt = exchange time interval
382    // flux = target flux
383
384    kineticTarget_ = 2.0*kineticFlux_*exchangeTime_*hmat(0,0)*hmat(1,1);
385    momentumTarget_ = 2.0*momentumFluxVector_*exchangeTime_*hmat(0,0)*hmat(1,1);
386
535      // total exchange sums are zeroed out at the beginning:
536 <
536 >    
537      kineticExchange_ = 0.0;
538      momentumExchange_ = V3Zero;
539 <
392 <    if (hasSlabWidth)
393 <      slabWidth_ = rnemdParams->getSlabWidth();
394 <    else
395 <      slabWidth_ = hmat(2,2) / 10.0;
396 <  
397 <    if (hasSlabACenter)
398 <      slabACenter_ = rnemdParams->getSlabACenter();
399 <    else
400 <      slabACenter_ = 0.0;
539 >    angularMomentumExchange_ = V3Zero;
540      
541 <    if (hasSlabBCenter)
542 <      slabBCenter_ = rnemdParams->getSlabBCenter();
404 <    else
405 <      slabBCenter_ = hmat(2,2) / 2.0;
541 >    std::ostringstream selectionAstream;
542 >    std::ostringstream selectionBstream;
543      
544 +    if (hasSelectionA_) {
545 +      selectionA_ = rnemdParams->getSelectionA();
546 +    } else {
547 +      if (usePeriodicBoundaryConditions_) {    
548 +        Mat3x3d hmat = currentSnap_->getHmat();
549 +        
550 +        if (hasSlabWidth)
551 +          slabWidth_ = rnemdParams->getSlabWidth();
552 +        else
553 +          slabWidth_ = hmat(2,2) / 10.0;
554 +        
555 +        if (hasSlabACenter)
556 +          slabACenter_ = rnemdParams->getSlabACenter();
557 +        else
558 +          slabACenter_ = 0.0;
559 +        
560 +        selectionAstream << "select wrappedz > "
561 +                         << slabACenter_ - 0.5*slabWidth_
562 +                         <<  " && wrappedz < "
563 +                         << slabACenter_ + 0.5*slabWidth_;
564 +        selectionA_ = selectionAstream.str();
565 +      } else {
566 +        if (hasSphereARadius)
567 +          sphereARadius_ = rnemdParams->getSphereARadius();
568 +        else {
569 +          // use an initial guess to the size of the inner slab to be 1/10 the
570 +          // radius of an approximately spherical hull:
571 +          Thermo thermo(info);
572 +          RealType hVol = thermo.getHullVolume();
573 +          sphereARadius_ = 0.1 * pow((3.0 * hVol / (4.0 * M_PI)), 1.0/3.0);
574 +        }
575 +        selectionAstream << "select r < " << sphereARadius_;
576 +        selectionA_ = selectionAstream.str();
577 +      }
578 +    }
579 +    
580 +    if (hasSelectionB_) {
581 +      selectionB_ = rnemdParams->getSelectionB();
582 +      
583 +    } else {
584 +      if (usePeriodicBoundaryConditions_) {    
585 +        Mat3x3d hmat = currentSnap_->getHmat();
586 +        
587 +        if (hasSlabWidth)
588 +          slabWidth_ = rnemdParams->getSlabWidth();
589 +        else
590 +          slabWidth_ = hmat(2,2) / 10.0;
591 +        
592 +        if (hasSlabBCenter)
593 +          slabBCenter_ = rnemdParams->getSlabBCenter();
594 +        else
595 +          slabBCenter_ = hmat(2,2) / 2.0;
596 +        
597 +        selectionBstream << "select wrappedz > "
598 +                         << slabBCenter_ - 0.5*slabWidth_
599 +                         <<  " && wrappedz < "
600 +                         << slabBCenter_ + 0.5*slabWidth_;
601 +        selectionB_ = selectionBstream.str();
602 +      } else {
603 +        if (hasSphereBRadius_) {
604 +          sphereBRadius_ = rnemdParams->getSphereBRadius();
605 +          selectionBstream << "select r > " << sphereBRadius_;
606 +          selectionB_ = selectionBstream.str();
607 +        } else {
608 +          selectionB_ = "select hull";
609 +          BisHull_ = true;
610 +          hasSelectionB_ = true;
611 +        }
612 +      }
613 +    }
614 +  
615 +  
616 +    // object evaluator:
617 +    evaluator_.loadScriptString(rnemdObjectSelection_);
618 +    seleMan_.setSelectionSet(evaluator_.evaluate());
619 +    evaluatorA_.loadScriptString(selectionA_);
620 +    evaluatorB_.loadScriptString(selectionB_);
621 +    seleManA_.setSelectionSet(evaluatorA_.evaluate());
622 +    seleManB_.setSelectionSet(evaluatorB_.evaluate());
623 +    commonA_ = seleManA_ & seleMan_;
624 +    commonB_ = seleManB_ & seleMan_;  
625    }
626 <  
409 <  RNEMD::~RNEMD() {
626 >  
627      
628 +  RNEMD::~RNEMD() {
629 +    if (!doRNEMD_) return;
630   #ifdef IS_MPI
631      if (worldRank == 0) {
632   #endif
# Line 419 | Line 638 | namespace OpenMD {
638   #ifdef IS_MPI
639      }
640   #endif
641 +
642 +    // delete all of the objects we created:
643 +    delete areaAccumulator_;    
644 +    data_.clear();
645    }
646    
647 <  bool RNEMD::inSlabA(Vector3d pos) {
648 <    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
649 <  }
650 <  bool RNEMD::inSlabB(Vector3d pos) {
428 <    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
429 <  }
647 >  void RNEMD::doSwap(SelectionManager& smanA, SelectionManager& smanB) {
648 >    if (!doRNEMD_) return;
649 >    int selei;
650 >    int selej;
651  
431  void RNEMD::doSwap() {
432
652      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
653      Mat3x3d hmat = currentSnap_->getHmat();
654  
436    seleMan_.setSelectionSet(evaluator_.evaluate());
437
438    int selei;
655      StuntDouble* sd;
440    int idx;
656  
657 <    RealType min_val;
658 <    bool min_found = false;  
659 <    StuntDouble* min_sd;
657 >    RealType min_val(0.0);
658 >    int min_found = 0;  
659 >    StuntDouble* min_sd = NULL;
660  
661 <    RealType max_val;
662 <    bool max_found = false;
663 <    StuntDouble* max_sd;
661 >    RealType max_val(0.0);
662 >    int max_found = 0;
663 >    StuntDouble* max_sd = NULL;
664  
665 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
666 <         sd = seleMan_.nextSelected(selei)) {
665 >    for (sd = seleManA_.beginSelected(selei); sd != NULL;
666 >         sd = seleManA_.nextSelected(selei)) {
667  
453      idx = sd->getLocalIndex();
454
668        Vector3d pos = sd->getPos();
669 <
669 >      
670        // wrap the stuntdouble's position back into the box:
671 <
671 >      
672        if (usePeriodicBoundaryConditions_)
673          currentSnap_->wrapVector(pos);
674 <      bool inA = inSlabA(pos);
675 <      bool inB = inSlabB(pos);
676 <
677 <      if (inA || inB) {
674 >      
675 >      RealType mass = sd->getMass();
676 >      Vector3d vel = sd->getVel();
677 >      RealType value;
678 >      
679 >      switch(rnemdFluxType_) {
680 >      case rnemdKE :
681          
682 <        RealType mass = sd->getMass();
683 <        Vector3d vel = sd->getVel();
684 <        RealType value;
685 <        
686 <        switch(rnemdFluxType_) {
471 <        case rnemdKE :
682 >        value = mass * vel.lengthSquare();
683 >        
684 >        if (sd->isDirectional()) {
685 >          Vector3d angMom = sd->getJ();
686 >          Mat3x3d I = sd->getI();
687            
688 <          value = mass * vel.lengthSquare();
689 <          
690 <          if (sd->isDirectional()) {
691 <            Vector3d angMom = sd->getJ();
692 <            Mat3x3d I = sd->getI();
693 <            
694 <            if (sd->isLinear()) {
695 <              int i = sd->linearAxis();
696 <              int j = (i + 1) % 3;
697 <              int k = (i + 2) % 3;
698 <              value += angMom[j] * angMom[j] / I(j, j) +
699 <                angMom[k] * angMom[k] / I(k, k);
700 <            } else {                        
701 <              value += angMom[0]*angMom[0]/I(0, 0)
702 <                + angMom[1]*angMom[1]/I(1, 1)
703 <                + angMom[2]*angMom[2]/I(2, 2);
704 <            }
705 <          } //angular momenta exchange enabled
706 <          //energyConvert temporarily disabled
707 <          //make kineticExchange_ comparable between swap & scale
708 <          //value = value * 0.5 / PhysicalConstants::energyConvert;
709 <          value *= 0.5;
710 <          break;
711 <        case rnemdPx :
712 <          value = mass * vel[0];
713 <          break;
714 <        case rnemdPy :
715 <          value = mass * vel[1];
716 <          break;
717 <        case rnemdPz :
718 <          value = mass * vel[2];
719 <          break;
720 <        default :
721 <          break;
688 >          if (sd->isLinear()) {
689 >            int i = sd->linearAxis();
690 >            int j = (i + 1) % 3;
691 >            int k = (i + 2) % 3;
692 >            value += angMom[j] * angMom[j] / I(j, j) +
693 >              angMom[k] * angMom[k] / I(k, k);
694 >          } else {                        
695 >            value += angMom[0]*angMom[0]/I(0, 0)
696 >              + angMom[1]*angMom[1]/I(1, 1)
697 >              + angMom[2]*angMom[2]/I(2, 2);
698 >          }
699 >        } //angular momenta exchange enabled
700 >        value *= 0.5;
701 >        break;
702 >      case rnemdPx :
703 >        value = mass * vel[0];
704 >        break;
705 >      case rnemdPy :
706 >        value = mass * vel[1];
707 >        break;
708 >      case rnemdPz :
709 >        value = mass * vel[2];
710 >        break;
711 >      default :
712 >        break;
713 >      }
714 >      if (!max_found) {
715 >        max_val = value;
716 >        max_sd = sd;
717 >        max_found = 1;
718 >      } else {
719 >        if (max_val < value) {
720 >          max_val = value;
721 >          max_sd = sd;
722          }
723 +      }  
724 +    }
725          
726 <        if (inA == 0) {
727 <          if (!min_found) {
728 <            min_val = value;
729 <            min_sd = sd;
730 <            min_found = true;
731 <          } else {
732 <            if (min_val > value) {
733 <              min_val = value;
734 <              min_sd = sd;
735 <            }
736 <          }
737 <        } else {
738 <          if (!max_found) {
739 <            max_val = value;
740 <            max_sd = sd;
741 <            max_found = true;
742 <          } else {
743 <            if (max_val < value) {
744 <              max_val = value;
745 <              max_sd = sd;
746 <            }
747 <          }      
748 <        }
726 >    for (sd = seleManB_.beginSelected(selej); sd != NULL;
727 >         sd = seleManB_.nextSelected(selej)) {
728 >
729 >      Vector3d pos = sd->getPos();
730 >      
731 >      // wrap the stuntdouble's position back into the box:
732 >      
733 >      if (usePeriodicBoundaryConditions_)
734 >        currentSnap_->wrapVector(pos);
735 >      
736 >      RealType mass = sd->getMass();
737 >      Vector3d vel = sd->getVel();
738 >      RealType value;
739 >      
740 >      switch(rnemdFluxType_) {
741 >      case rnemdKE :
742 >        
743 >        value = mass * vel.lengthSquare();
744 >        
745 >        if (sd->isDirectional()) {
746 >          Vector3d angMom = sd->getJ();
747 >          Mat3x3d I = sd->getI();
748 >          
749 >          if (sd->isLinear()) {
750 >            int i = sd->linearAxis();
751 >            int j = (i + 1) % 3;
752 >            int k = (i + 2) % 3;
753 >            value += angMom[j] * angMom[j] / I(j, j) +
754 >              angMom[k] * angMom[k] / I(k, k);
755 >          } else {                        
756 >            value += angMom[0]*angMom[0]/I(0, 0)
757 >              + angMom[1]*angMom[1]/I(1, 1)
758 >              + angMom[2]*angMom[2]/I(2, 2);
759 >          }
760 >        } //angular momenta exchange enabled
761 >        value *= 0.5;
762 >        break;
763 >      case rnemdPx :
764 >        value = mass * vel[0];
765 >        break;
766 >      case rnemdPy :
767 >        value = mass * vel[1];
768 >        break;
769 >      case rnemdPz :
770 >        value = mass * vel[2];
771 >        break;
772 >      default :
773 >        break;
774        }
775 +      
776 +      if (!min_found) {
777 +        min_val = value;
778 +        min_sd = sd;
779 +        min_found = 1;
780 +      } else {
781 +        if (min_val > value) {
782 +          min_val = value;
783 +          min_sd = sd;
784 +        }
785 +      }
786      }
787      
788 < #ifdef IS_MPI
789 <    int nProc, worldRank;
790 <    
791 <    nProc = MPI::COMM_WORLD.Get_size();
792 <    worldRank = MPI::COMM_WORLD.Get_rank();
788 > #ifdef IS_MPI    
789 >    int worldRank;
790 >    MPI_Comm_rank( MPI_COMM_WORLD, &worldRank);
791 >        
792 >    int my_min_found = min_found;
793 >    int my_max_found = max_found;
794  
541    bool my_min_found = min_found;
542    bool my_max_found = max_found;
543
795      // Even if we didn't find a minimum, did someone else?
796 <    MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found, 1, MPI::BOOL, MPI::LOR);
796 >    MPI_Allreduce(&my_min_found, &min_found, 1, MPI_INT, MPI_LOR,
797 >                  MPI_COMM_WORLD);
798      // Even if we didn't find a maximum, did someone else?
799 <    MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found, 1, MPI::BOOL, MPI::LOR);
799 >    MPI_Allreduce(&my_max_found, &max_found, 1, MPI_INT, MPI_LOR,
800 >                  MPI_COMM_WORLD);
801   #endif
802  
803      if (max_found && min_found) {
# Line 563 | Line 816 | namespace OpenMD {
816        min_vals.rank = worldRank;    
817        
818        // Who had the minimum?
819 <      MPI::COMM_WORLD.Allreduce(&min_vals, &min_vals,
820 <                                1, MPI::REALTYPE_INT, MPI::MINLOC);
819 >      MPI_Allreduce(&min_vals, &min_vals,
820 >                    1, MPI_REALTYPE_INT, MPI_MINLOC, MPI_COMM_WORLD);
821        min_val = min_vals.val;
822        
823        if (my_max_found) {
# Line 575 | Line 828 | namespace OpenMD {
828        max_vals.rank = worldRank;    
829        
830        // Who had the maximum?
831 <      MPI::COMM_WORLD.Allreduce(&max_vals, &max_vals,
832 <                                1, MPI::REALTYPE_INT, MPI::MAXLOC);
831 >      MPI_Allreduce(&max_vals, &max_vals,
832 >                    1, MPI_REALTYPE_INT, MPI_MAXLOC, MPI_COMM_WORLD);
833        max_val = max_vals.val;
834   #endif
835        
# Line 636 | Line 889 | namespace OpenMD {
889            
890            Vector3d min_vel;
891            Vector3d max_vel = max_sd->getVel();
892 <          MPI::Status status;
892 >          MPI_Status status;
893  
894            // point-to-point swap of the velocity vector
895 <          MPI::COMM_WORLD.Sendrecv(max_vel.getArrayPointer(), 3, MPI::REALTYPE,
896 <                                   min_vals.rank, 0,
897 <                                   min_vel.getArrayPointer(), 3, MPI::REALTYPE,
898 <                                   min_vals.rank, 0, status);
895 >          MPI_Sendrecv(max_vel.getArrayPointer(), 3, MPI_REALTYPE,
896 >                       min_vals.rank, 0,
897 >                       min_vel.getArrayPointer(), 3, MPI_REALTYPE,
898 >                       min_vals.rank, 0, MPI_COMM_WORLD, &status);
899            
900            switch(rnemdFluxType_) {
901            case rnemdKE :
# Line 653 | Line 906 | namespace OpenMD {
906                Vector3d max_angMom = max_sd->getJ();
907                
908                // point-to-point swap of the angular momentum vector
909 <              MPI::COMM_WORLD.Sendrecv(max_angMom.getArrayPointer(), 3,
910 <                                       MPI::REALTYPE, min_vals.rank, 1,
911 <                                       min_angMom.getArrayPointer(), 3,
912 <                                       MPI::REALTYPE, min_vals.rank, 1,
913 <                                       status);
909 >              MPI_Sendrecv(max_angMom.getArrayPointer(), 3,
910 >                           MPI_REALTYPE, min_vals.rank, 1,
911 >                           min_angMom.getArrayPointer(), 3,
912 >                           MPI_REALTYPE, min_vals.rank, 1,
913 >                           MPI_COMM_WORLD, &status);
914                
915                max_sd->setJ(min_angMom);
916              }
# Line 682 | Line 935 | namespace OpenMD {
935            
936            Vector3d max_vel;
937            Vector3d min_vel = min_sd->getVel();
938 <          MPI::Status status;
938 >          MPI_Status status;
939            
940            // point-to-point swap of the velocity vector
941 <          MPI::COMM_WORLD.Sendrecv(min_vel.getArrayPointer(), 3, MPI::REALTYPE,
942 <                                   max_vals.rank, 0,
943 <                                   max_vel.getArrayPointer(), 3, MPI::REALTYPE,
944 <                                   max_vals.rank, 0, status);
941 >          MPI_Sendrecv(min_vel.getArrayPointer(), 3, MPI_REALTYPE,
942 >                       max_vals.rank, 0,
943 >                       max_vel.getArrayPointer(), 3, MPI_REALTYPE,
944 >                       max_vals.rank, 0, MPI_COMM_WORLD, &status);
945            
946            switch(rnemdFluxType_) {
947            case rnemdKE :
# Line 699 | Line 952 | namespace OpenMD {
952                Vector3d max_angMom;
953                
954                // point-to-point swap of the angular momentum vector
955 <              MPI::COMM_WORLD.Sendrecv(min_angMom.getArrayPointer(), 3,
956 <                                       MPI::REALTYPE, max_vals.rank, 1,
957 <                                       max_angMom.getArrayPointer(), 3,
958 <                                       MPI::REALTYPE, max_vals.rank, 1,
959 <                                       status);
955 >              MPI_Sendrecv(min_angMom.getArrayPointer(), 3,
956 >                           MPI_REALTYPE, max_vals.rank, 1,
957 >                           max_angMom.getArrayPointer(), 3,
958 >                           MPI_REALTYPE, max_vals.rank, 1,
959 >                           MPI_COMM_WORLD, &status);
960                
961                min_sd->setJ(max_angMom);
962              }
# Line 728 | Line 981 | namespace OpenMD {
981          
982          switch(rnemdFluxType_) {
983          case rnemdKE:
731          cerr << "KE\n";
984            kineticExchange_ += max_val - min_val;
985            break;
986          case rnemdPx:
# Line 741 | Line 993 | namespace OpenMD {
993            momentumExchange_.z() += max_val - min_val;
994            break;
995          default:
744          cerr << "default\n";
996            break;
997          }
998        } else {        
# Line 763 | Line 1014 | namespace OpenMD {
1014      }    
1015    }
1016    
1017 <  void RNEMD::doNIVS() {
1017 >  void RNEMD::doNIVS(SelectionManager& smanA, SelectionManager& smanB) {
1018 >    if (!doRNEMD_) return;
1019 >    int selei;
1020 >    int selej;
1021  
1022      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1023      Mat3x3d hmat = currentSnap_->getHmat();
1024  
771    seleMan_.setSelectionSet(evaluator_.evaluate());
772
773    int selei;
1025      StuntDouble* sd;
775    int idx;
1026  
1027      vector<StuntDouble*> hotBin, coldBin;
1028  
# Line 791 | Line 1041 | namespace OpenMD {
1041      RealType Kcz = 0.0;
1042      RealType Kcw = 0.0;
1043  
1044 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1045 <         sd = seleMan_.nextSelected(selei)) {
1044 >    for (sd = smanA.beginSelected(selei); sd != NULL;
1045 >         sd = smanA.nextSelected(selei)) {
1046  
797      idx = sd->getLocalIndex();
798
1047        Vector3d pos = sd->getPos();
1048 <
1048 >      
1049        // wrap the stuntdouble's position back into the box:
1050 <
1050 >      
1051        if (usePeriodicBoundaryConditions_)
1052          currentSnap_->wrapVector(pos);
1053 +      
1054 +      
1055 +      RealType mass = sd->getMass();
1056 +      Vector3d vel = sd->getVel();
1057 +      
1058 +      hotBin.push_back(sd);
1059 +      Phx += mass * vel.x();
1060 +      Phy += mass * vel.y();
1061 +      Phz += mass * vel.z();
1062 +      Khx += mass * vel.x() * vel.x();
1063 +      Khy += mass * vel.y() * vel.y();
1064 +      Khz += mass * vel.z() * vel.z();
1065 +      if (sd->isDirectional()) {
1066 +        Vector3d angMom = sd->getJ();
1067 +        Mat3x3d I = sd->getI();
1068 +        if (sd->isLinear()) {
1069 +          int i = sd->linearAxis();
1070 +          int j = (i + 1) % 3;
1071 +          int k = (i + 2) % 3;
1072 +          Khw += angMom[j] * angMom[j] / I(j, j) +
1073 +            angMom[k] * angMom[k] / I(k, k);
1074 +        } else {
1075 +          Khw += angMom[0]*angMom[0]/I(0, 0)
1076 +            + angMom[1]*angMom[1]/I(1, 1)
1077 +            + angMom[2]*angMom[2]/I(2, 2);
1078 +        }
1079 +      }
1080 +    }
1081 +    for (sd = smanB.beginSelected(selej); sd != NULL;
1082 +         sd = smanB.nextSelected(selej)) {
1083 +      Vector3d pos = sd->getPos();
1084 +      
1085 +      // wrap the stuntdouble's position back into the box:
1086 +      
1087 +      if (usePeriodicBoundaryConditions_)
1088 +        currentSnap_->wrapVector(pos);
1089 +            
1090 +      RealType mass = sd->getMass();
1091 +      Vector3d vel = sd->getVel();
1092  
1093 <      // which bin is this stuntdouble in?
1094 <      bool inA = inSlabA(pos);
1095 <      bool inB = inSlabB(pos);
1096 <
1097 <      if (inA || inB) {
1098 <              
1099 <        RealType mass = sd->getMass();
1100 <        Vector3d vel = sd->getVel();
1101 <      
1102 <        if (inA) {
1103 <          hotBin.push_back(sd);
1104 <          Phx += mass * vel.x();
1105 <          Phy += mass * vel.y();
1106 <          Phz += mass * vel.z();
1107 <          Khx += mass * vel.x() * vel.x();
1108 <          Khy += mass * vel.y() * vel.y();
1109 <          Khz += mass * vel.z() * vel.z();
1110 <          if (sd->isDirectional()) {
1111 <            Vector3d angMom = sd->getJ();
1112 <            Mat3x3d I = sd->getI();
1113 <            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 <        }
1093 >      coldBin.push_back(sd);
1094 >      Pcx += mass * vel.x();
1095 >      Pcy += mass * vel.y();
1096 >      Pcz += mass * vel.z();
1097 >      Kcx += mass * vel.x() * vel.x();
1098 >      Kcy += mass * vel.y() * vel.y();
1099 >      Kcz += mass * vel.z() * vel.z();
1100 >      if (sd->isDirectional()) {
1101 >        Vector3d angMom = sd->getJ();
1102 >        Mat3x3d I = sd->getI();
1103 >        if (sd->isLinear()) {
1104 >          int i = sd->linearAxis();
1105 >          int j = (i + 1) % 3;
1106 >          int k = (i + 2) % 3;
1107 >          Kcw += angMom[j] * angMom[j] / I(j, j) +
1108 >            angMom[k] * angMom[k] / I(k, k);
1109 >        } else {
1110 >          Kcw += angMom[0]*angMom[0]/I(0, 0)
1111 >            + angMom[1]*angMom[1]/I(1, 1)
1112 >            + angMom[2]*angMom[2]/I(2, 2);
1113 >        }
1114        }
1115      }
1116      
# Line 872 | Line 1124 | namespace OpenMD {
1124      Kcw *= 0.5;
1125  
1126   #ifdef IS_MPI
1127 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
1128 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
1129 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phz, 1, MPI::REALTYPE, MPI::SUM);
1130 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcx, 1, MPI::REALTYPE, MPI::SUM);
1131 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcy, 1, MPI::REALTYPE, MPI::SUM);
1132 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcz, 1, MPI::REALTYPE, MPI::SUM);
1127 >    MPI_Allreduce(MPI_IN_PLACE, &Phx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1128 >    MPI_Allreduce(MPI_IN_PLACE, &Phy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1129 >    MPI_Allreduce(MPI_IN_PLACE, &Phz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1130 >    MPI_Allreduce(MPI_IN_PLACE, &Pcx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1131 >    MPI_Allreduce(MPI_IN_PLACE, &Pcy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1132 >    MPI_Allreduce(MPI_IN_PLACE, &Pcz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1133  
1134 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
1135 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
1136 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
1137 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khw, 1, MPI::REALTYPE, MPI::SUM);
1134 >    MPI_Allreduce(MPI_IN_PLACE, &Khx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1135 >    MPI_Allreduce(MPI_IN_PLACE, &Khy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1136 >    MPI_Allreduce(MPI_IN_PLACE, &Khz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1137 >    MPI_Allreduce(MPI_IN_PLACE, &Khw, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1138  
1139 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
1140 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
1141 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
1142 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcw, 1, MPI::REALTYPE, MPI::SUM);
1139 >    MPI_Allreduce(MPI_IN_PLACE, &Kcx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1140 >    MPI_Allreduce(MPI_IN_PLACE, &Kcy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1141 >    MPI_Allreduce(MPI_IN_PLACE, &Kcz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1142 >    MPI_Allreduce(MPI_IN_PLACE, &Kcw, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1143   #endif
1144  
1145      //solve coldBin coeff's first
# Line 908 | Line 1160 | namespace OpenMD {
1160  
1161        if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
1162          c = sqrt(c);
1163 <        //std::cerr << "cold slab scaling coefficient: " << c << endl;
912 <        //now convert to hotBin coefficient
1163 >
1164          RealType w = 0.0;
1165          if (rnemdFluxType_ ==  rnemdFullKE) {
1166            x = 1.0 + px * (1.0 - c);
# Line 936 | Line 1187 | namespace OpenMD {
1187            //if w is in the right range, so should be x, y, z.
1188            vector<StuntDouble*>::iterator sdi;
1189            Vector3d vel;
1190 <          for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1190 >          for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1191              if (rnemdFluxType_ == rnemdFullKE) {
1192                vel = (*sdi)->getVel() * c;
1193                (*sdi)->setVel(vel);
# Line 947 | Line 1198 | namespace OpenMD {
1198              }
1199            }
1200            w = sqrt(w);
1201 <          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
951 <          //           << "\twh= " << w << endl;
952 <          for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1201 >          for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1202              if (rnemdFluxType_ == rnemdFullKE) {
1203                vel = (*sdi)->getVel();
1204                vel.x() *= x;
# Line 1068 | Line 1317 | namespace OpenMD {
1317        vector<RealType>::iterator ri;
1318        RealType r1, r2, alpha0;
1319        vector<pair<RealType,RealType> > rps;
1320 <      for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
1320 >      for (ri = realRoots.begin(); ri !=realRoots.end(); ++ri) {
1321          r2 = *ri;
1322          //check if FindRealRoots() give the right answer
1323          if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
# Line 1100 | Line 1349 | namespace OpenMD {
1349          RealType diff;
1350          pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
1351          vector<pair<RealType,RealType> >::iterator rpi;
1352 <        for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1352 >        for (rpi = rps.begin(); rpi != rps.end(); ++rpi) {
1353            r1 = (*rpi).first;
1354            r2 = (*rpi).second;
1355            switch(rnemdFluxType_) {
# Line 1167 | Line 1416 | namespace OpenMD {
1416          }
1417          vector<StuntDouble*>::iterator sdi;
1418          Vector3d vel;
1419 <        for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1419 >        for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1420            vel = (*sdi)->getVel();
1421            vel.x() *= x;
1422            vel.y() *= y;
# Line 1178 | Line 1427 | namespace OpenMD {
1427          x = 1.0 + px * (1.0 - x);
1428          y = 1.0 + py * (1.0 - y);
1429          z = 1.0 + pz * (1.0 - z);
1430 <        for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1430 >        for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1431            vel = (*sdi)->getVel();
1432            vel.x() *= x;
1433            vel.y() *= y;
# Line 1211 | Line 1460 | namespace OpenMD {
1460        failTrialCount_++;
1461      }
1462    }
1463 <
1464 <  void RNEMD::doVSS() {
1463 >  
1464 >  void RNEMD::doVSS(SelectionManager& smanA, SelectionManager& smanB) {
1465 >    if (!doRNEMD_) return;
1466 >    int selei;
1467 >    int selej;
1468  
1469      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1218    RealType time = currentSnap_->getTime();    
1470      Mat3x3d hmat = currentSnap_->getHmat();
1471  
1221    seleMan_.setSelectionSet(evaluator_.evaluate());
1222
1223    int selei;
1472      StuntDouble* sd;
1225    int idx;
1473  
1474      vector<StuntDouble*> hotBin, coldBin;
1475  
1476      Vector3d Ph(V3Zero);
1477 +    Vector3d Lh(V3Zero);
1478      RealType Mh = 0.0;
1479 +    Mat3x3d Ih(0.0);
1480      RealType Kh = 0.0;
1481      Vector3d Pc(V3Zero);
1482 +    Vector3d Lc(V3Zero);
1483      RealType Mc = 0.0;
1484 +    Mat3x3d Ic(0.0);
1485      RealType Kc = 0.0;
1235    
1486  
1487 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1488 <         sd = seleMan_.nextSelected(selei)) {
1487 >    // Constraints can be on only the linear or angular momentum, but
1488 >    // not both.  Usually, the user will specify which they want, but
1489 >    // in case they don't, the use of periodic boundaries should make
1490 >    // the choice for us.
1491 >    bool doLinearPart = false;
1492 >    bool doAngularPart = false;
1493  
1494 <      idx = sd->getLocalIndex();
1494 >    switch (rnemdFluxType_) {
1495 >    case rnemdPx:
1496 >    case rnemdPy:
1497 >    case rnemdPz:
1498 >    case rnemdPvector:
1499 >    case rnemdKePx:
1500 >    case rnemdKePy:
1501 >    case rnemdKePvector:
1502 >      doLinearPart = true;
1503 >      break;
1504 >    case rnemdLx:
1505 >    case rnemdLy:
1506 >    case rnemdLz:
1507 >    case rnemdLvector:
1508 >    case rnemdKeLx:
1509 >    case rnemdKeLy:
1510 >    case rnemdKeLz:
1511 >    case rnemdKeLvector:
1512 >      doAngularPart = true;
1513 >      break;
1514 >    case rnemdKE:
1515 >    case rnemdRotKE:
1516 >    case rnemdFullKE:
1517 >    default:
1518 >      if (usePeriodicBoundaryConditions_)
1519 >        doLinearPart = true;
1520 >      else
1521 >        doAngularPart = true;
1522 >      break;
1523 >    }
1524 >    
1525 >    for (sd = smanA.beginSelected(selei); sd != NULL;
1526 >         sd = smanA.nextSelected(selei)) {
1527  
1528        Vector3d pos = sd->getPos();
1529  
1530        // wrap the stuntdouble's position back into the box:
1531 +      
1532 +      if (usePeriodicBoundaryConditions_)
1533 +        currentSnap_->wrapVector(pos);
1534 +      
1535 +      RealType mass = sd->getMass();
1536 +      Vector3d vel = sd->getVel();
1537 +      Vector3d rPos = sd->getPos() - coordinateOrigin_;
1538 +      RealType r2;
1539 +      
1540 +      hotBin.push_back(sd);
1541 +      Ph += mass * vel;
1542 +      Mh += mass;
1543 +      Kh += mass * vel.lengthSquare();
1544 +      Lh += mass * cross(rPos, vel);
1545 +      Ih -= outProduct(rPos, rPos) * mass;
1546 +      r2 = rPos.lengthSquare();
1547 +      Ih(0, 0) += mass * r2;
1548 +      Ih(1, 1) += mass * r2;
1549 +      Ih(2, 2) += mass * r2;
1550 +      
1551 +      if (rnemdFluxType_ == rnemdFullKE) {
1552 +        if (sd->isDirectional()) {
1553 +          Vector3d angMom = sd->getJ();
1554 +          Mat3x3d I = sd->getI();
1555 +          if (sd->isLinear()) {
1556 +            int i = sd->linearAxis();
1557 +            int j = (i + 1) % 3;
1558 +            int k = (i + 2) % 3;
1559 +            Kh += angMom[j] * angMom[j] / I(j, j) +
1560 +              angMom[k] * angMom[k] / I(k, k);
1561 +          } else {
1562 +            Kh += angMom[0] * angMom[0] / I(0, 0) +
1563 +              angMom[1] * angMom[1] / I(1, 1) +
1564 +              angMom[2] * angMom[2] / I(2, 2);
1565 +          }
1566 +        }
1567 +      }
1568 +    }
1569 +    for (sd = smanB.beginSelected(selej); sd != NULL;
1570 +         sd = smanB.nextSelected(selej)) {
1571  
1572 +      Vector3d pos = sd->getPos();
1573 +      
1574 +      // wrap the stuntdouble's position back into the box:
1575 +      
1576        if (usePeriodicBoundaryConditions_)
1577          currentSnap_->wrapVector(pos);
1578 +      
1579 +      RealType mass = sd->getMass();
1580 +      Vector3d vel = sd->getVel();
1581 +      Vector3d rPos = sd->getPos() - coordinateOrigin_;
1582 +      RealType r2;
1583  
1584 <      // which bin is this stuntdouble in?
1585 <      bool inA = inSlabA(pos);
1586 <      bool inB = inSlabB(pos);
1584 >      coldBin.push_back(sd);
1585 >      Pc += mass * vel;
1586 >      Mc += mass;
1587 >      Kc += mass * vel.lengthSquare();
1588 >      Lc += mass * cross(rPos, vel);
1589 >      Ic -= outProduct(rPos, rPos) * mass;
1590 >      r2 = rPos.lengthSquare();
1591 >      Ic(0, 0) += mass * r2;
1592 >      Ic(1, 1) += mass * r2;
1593 >      Ic(2, 2) += mass * r2;
1594        
1595 <      if (inA || inB) {
1596 <        
1597 <        RealType mass = sd->getMass();
1598 <        Vector3d vel = sd->getVel();
1599 <      
1600 <        if (inA) {
1601 <          hotBin.push_back(sd);
1602 <          //std::cerr << "before, velocity = " << vel << endl;
1603 <          Ph += mass * vel;
1604 <          //std::cerr << "after, velocity = " << vel << endl;
1605 <          Mh += mass;
1606 <          Kh += mass * vel.lengthSquare();
1607 <          if (rnemdFluxType_ == rnemdFullKE) {
1608 <            if (sd->isDirectional()) {
1609 <              Vector3d angMom = sd->getJ();
1610 <              Mat3x3d I = sd->getI();
1269 <              if (sd->isLinear()) {
1270 <                int i = sd->linearAxis();
1271 <                int j = (i + 1) % 3;
1272 <                int k = (i + 2) % 3;
1273 <                Kh += angMom[j] * angMom[j] / I(j, j) +
1274 <                  angMom[k] * angMom[k] / I(k, k);
1275 <              } else {
1276 <                Kh += angMom[0] * angMom[0] / I(0, 0) +
1277 <                  angMom[1] * angMom[1] / I(1, 1) +
1278 <                  angMom[2] * angMom[2] / I(2, 2);
1279 <              }
1280 <            }
1281 <          }
1282 <        } else { //midBin_
1283 <          coldBin.push_back(sd);
1284 <          Pc += mass * vel;
1285 <          Mc += mass;
1286 <          Kc += mass * vel.lengthSquare();
1287 <          if (rnemdFluxType_ == rnemdFullKE) {
1288 <            if (sd->isDirectional()) {
1289 <              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 <        }
1595 >      if (rnemdFluxType_ == rnemdFullKE) {
1596 >        if (sd->isDirectional()) {
1597 >          Vector3d angMom = sd->getJ();
1598 >          Mat3x3d I = sd->getI();
1599 >          if (sd->isLinear()) {
1600 >            int i = sd->linearAxis();
1601 >            int j = (i + 1) % 3;
1602 >            int k = (i + 2) % 3;
1603 >            Kc += angMom[j] * angMom[j] / I(j, j) +
1604 >              angMom[k] * angMom[k] / I(k, k);
1605 >          } else {
1606 >            Kc += angMom[0] * angMom[0] / I(0, 0) +
1607 >              angMom[1] * angMom[1] / I(1, 1) +
1608 >              angMom[2] * angMom[2] / I(2, 2);
1609 >          }
1610 >        }
1611        }
1612      }
1613      
1614      Kh *= 0.5;
1615      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;
1616      
1617   #ifdef IS_MPI
1618 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1619 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
1620 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mh, 1, MPI::REALTYPE, MPI::SUM);
1621 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kh, 1, MPI::REALTYPE, MPI::SUM);
1622 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mc, 1, MPI::REALTYPE, MPI::SUM);
1623 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kc, 1, MPI::REALTYPE, MPI::SUM);
1618 >    MPI_Allreduce(MPI_IN_PLACE, &Ph[0], 3, MPI_REALTYPE, MPI_SUM,
1619 >                  MPI_COMM_WORLD);
1620 >    MPI_Allreduce(MPI_IN_PLACE, &Pc[0], 3, MPI_REALTYPE, MPI_SUM,
1621 >                  MPI_COMM_WORLD);
1622 >    MPI_Allreduce(MPI_IN_PLACE, &Lh[0], 3, MPI_REALTYPE, MPI_SUM,
1623 >                  MPI_COMM_WORLD);
1624 >    MPI_Allreduce(MPI_IN_PLACE, &Lc[0], 3, MPI_REALTYPE, MPI_SUM,
1625 >                  MPI_COMM_WORLD);
1626 >    MPI_Allreduce(MPI_IN_PLACE, &Mh, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1627 >    MPI_Allreduce(MPI_IN_PLACE, &Kh, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1628 >    MPI_Allreduce(MPI_IN_PLACE, &Mc, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1629 >    MPI_Allreduce(MPI_IN_PLACE, &Kc, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1630 >    MPI_Allreduce(MPI_IN_PLACE, Ih.getArrayPointer(), 9,
1631 >                  MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1632 >    MPI_Allreduce(MPI_IN_PLACE, Ic.getArrayPointer(), 9,
1633 >                  MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1634   #endif
1635 +    
1636  
1637 +    Vector3d ac, acrec, bc, bcrec;
1638 +    Vector3d ah, ahrec, bh, bhrec;
1639 +
1640      bool successfulExchange = false;
1641      if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1642        Vector3d vc = Pc / Mc;
1643 <      Vector3d ac = -momentumTarget_ / Mc + vc;
1644 <      Vector3d acrec = -momentumTarget_ / Mc;
1645 <      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1643 >      ac = -momentumTarget_ / Mc + vc;
1644 >      acrec = -momentumTarget_ / Mc;
1645 >      
1646 >      // We now need the inverse of the inertia tensor to calculate the
1647 >      // angular velocity of the cold slab;
1648 >      Mat3x3d Ici = Ic.inverse();
1649 >      Vector3d omegac = Ici * Lc;
1650 >      bc  = -(Ici * angularMomentumTarget_) + omegac;
1651 >      bcrec = bc - omegac;
1652 >      
1653 >      RealType cNumerator = Kc - kineticTarget_;
1654 >      if (doLinearPart)
1655 >        cNumerator -= 0.5 * Mc * ac.lengthSquare();
1656 >      
1657 >      if (doAngularPart)
1658 >        cNumerator -= 0.5 * ( dot(bc, Ic * bc));
1659 >
1660        if (cNumerator > 0.0) {
1661 <        RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1661 >        
1662 >        RealType cDenominator = Kc;
1663 >
1664 >        if (doLinearPart)
1665 >          cDenominator -= 0.5 * Mc * vc.lengthSquare();
1666 >
1667 >        if (doAngularPart)
1668 >          cDenominator -= 0.5*(dot(omegac, Ic * omegac));
1669 >        
1670          if (cDenominator > 0.0) {
1671            RealType c = sqrt(cNumerator / cDenominator);
1672            if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1673 +            
1674              Vector3d vh = Ph / Mh;
1675 <            Vector3d ah = momentumTarget_ / Mh + vh;
1676 <            Vector3d ahrec = momentumTarget_ / Mh;
1677 <            RealType hNumerator = Kh + kineticTarget_
1678 <              - 0.5 * Mh * ah.lengthSquare();
1679 <            if (hNumerator > 0.0) {
1680 <              RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1675 >            ah = momentumTarget_ / Mh + vh;
1676 >            ahrec = momentumTarget_ / Mh;
1677 >            
1678 >            // We now need the inverse of the inertia tensor to
1679 >            // calculate the angular velocity of the hot slab;
1680 >            Mat3x3d Ihi = Ih.inverse();
1681 >            Vector3d omegah = Ihi * Lh;
1682 >            bh  = (Ihi * angularMomentumTarget_) + omegah;
1683 >            bhrec = bh - omegah;
1684 >            
1685 >            RealType hNumerator = Kh + kineticTarget_;
1686 >            if (doLinearPart)
1687 >              hNumerator -= 0.5 * Mh * ah.lengthSquare();
1688 >            
1689 >            if (doAngularPart)
1690 >              hNumerator -= 0.5 * ( dot(bh, Ih * bh));
1691 >              
1692 >            if (hNumerator > 0.0) {
1693 >              
1694 >              RealType hDenominator = Kh;
1695 >              if (doLinearPart)
1696 >                hDenominator -= 0.5 * Mh * vh.lengthSquare();
1697 >              if (doAngularPart)
1698 >                hDenominator -= 0.5*(dot(omegah, Ih * omegah));
1699 >              
1700                if (hDenominator > 0.0) {
1701                  RealType h = sqrt(hNumerator / hDenominator);
1702                  if ((h > 0.9) && (h < 1.1)) {
1703 <                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1346 <                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1703 >                  
1704                    vector<StuntDouble*>::iterator sdi;
1705                    Vector3d vel;
1706 <                  for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1706 >                  Vector3d rPos;
1707 >                  
1708 >                  for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1709                      //vel = (*sdi)->getVel();
1710 <                    vel = ((*sdi)->getVel() - vc) * c + ac;
1710 >                    rPos = (*sdi)->getPos() - coordinateOrigin_;
1711 >                    if (doLinearPart)
1712 >                      vel = ((*sdi)->getVel() - vc) * c + ac;
1713 >                    if (doAngularPart)
1714 >                      vel = ((*sdi)->getVel() - cross(omegac, rPos)) * c + cross(bc, rPos);
1715 >
1716                      (*sdi)->setVel(vel);
1717                      if (rnemdFluxType_ == rnemdFullKE) {
1718                        if ((*sdi)->isDirectional()) {
# Line 1357 | Line 1721 | namespace OpenMD {
1721                        }
1722                      }
1723                    }
1724 <                  for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1724 >                  for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1725                      //vel = (*sdi)->getVel();
1726 <                    vel = ((*sdi)->getVel() - vh) * h + ah;
1726 >                    rPos = (*sdi)->getPos() - coordinateOrigin_;
1727 >                    if (doLinearPart)
1728 >                      vel = ((*sdi)->getVel() - vh) * h + ah;    
1729 >                    if (doAngularPart)
1730 >                      vel = ((*sdi)->getVel() - cross(omegah, rPos)) * h + cross(bh, rPos);    
1731 >
1732                      (*sdi)->setVel(vel);
1733                      if (rnemdFluxType_ == rnemdFullKE) {
1734                        if ((*sdi)->isDirectional()) {
# Line 1371 | Line 1740 | namespace OpenMD {
1740                    successfulExchange = true;
1741                    kineticExchange_ += kineticTarget_;
1742                    momentumExchange_ += momentumTarget_;
1743 +                  angularMomentumExchange_ += angularMomentumTarget_;
1744                  }
1745                }
1746              }
# Line 1390 | Line 1760 | namespace OpenMD {
1760      }
1761    }
1762  
1763 <  void RNEMD::doRNEMD() {
1763 >  RealType RNEMD::getDividingArea() {
1764  
1765 +    if (hasDividingArea_) return dividingArea_;
1766 +
1767 +    RealType areaA, areaB;
1768 +    Snapshot* snap = info_->getSnapshotManager()->getCurrentSnapshot();
1769 +
1770 +    if (hasSelectionA_) {
1771 +
1772 +      if (evaluatorA_.hasSurfaceArea())
1773 +        areaA = evaluatorA_.getSurfaceArea();
1774 +      else {
1775 +        
1776 +        int isd;
1777 +        StuntDouble* sd;
1778 +        vector<StuntDouble*> aSites;
1779 +        seleManA_.setSelectionSet(evaluatorA_.evaluate());
1780 +        for (sd = seleManA_.beginSelected(isd); sd != NULL;
1781 +             sd = seleManA_.nextSelected(isd)) {
1782 +          aSites.push_back(sd);
1783 +        }
1784 + #if defined(HAVE_QHULL)
1785 +        ConvexHull* surfaceMeshA = new ConvexHull();
1786 +        surfaceMeshA->computeHull(aSites);
1787 +        areaA = surfaceMeshA->getArea();
1788 +        delete surfaceMeshA;
1789 + #else
1790 +        sprintf( painCave.errMsg,
1791 +               "RNEMD::getDividingArea : Hull calculation is not possible\n"
1792 +                 "\twithout libqhull. Please rebuild OpenMD with qhull enabled.");
1793 +        painCave.severity = OPENMD_ERROR;
1794 +        painCave.isFatal = 1;
1795 +        simError();
1796 + #endif
1797 +      }
1798 +
1799 +    } else {
1800 +      if (usePeriodicBoundaryConditions_) {
1801 +        // in periodic boundaries, the surface area is twice the x-y
1802 +        // area of the current box:
1803 +        areaA = 2.0 * snap->getXYarea();
1804 +      } else {
1805 +        // in non-periodic simulations, without explicitly setting
1806 +        // selections, the sphere radius sets the surface area of the
1807 +        // dividing surface:
1808 +        areaA = 4.0 * M_PI * pow(sphereARadius_, 2);
1809 +      }
1810 +    }
1811 +
1812 +    if (hasSelectionB_) {
1813 +      if (evaluatorB_.hasSurfaceArea()) {
1814 +        areaB = evaluatorB_.getSurfaceArea();
1815 +      } else {
1816 +
1817 +        int isd;
1818 +        StuntDouble* sd;
1819 +        vector<StuntDouble*> bSites;
1820 +        seleManB_.setSelectionSet(evaluatorB_.evaluate());
1821 +        for (sd = seleManB_.beginSelected(isd); sd != NULL;
1822 +             sd = seleManB_.nextSelected(isd)) {
1823 +          bSites.push_back(sd);
1824 +        }
1825 +        
1826 + #if defined(HAVE_QHULL)
1827 +        ConvexHull* surfaceMeshB = new ConvexHull();    
1828 +        surfaceMeshB->computeHull(bSites);
1829 +        areaB = surfaceMeshB->getArea();
1830 +        delete surfaceMeshB;
1831 + #else
1832 +        sprintf( painCave.errMsg,
1833 +                 "RNEMD::getDividingArea : Hull calculation is not possible\n"
1834 +                 "\twithout libqhull. Please rebuild OpenMD with qhull enabled.");
1835 +        painCave.severity = OPENMD_ERROR;
1836 +        painCave.isFatal = 1;
1837 +        simError();
1838 + #endif
1839 +      }
1840 +      
1841 +    } else {
1842 +      if (usePeriodicBoundaryConditions_) {
1843 +        // in periodic boundaries, the surface area is twice the x-y
1844 +        // area of the current box:
1845 +        areaB = 2.0 * snap->getXYarea();
1846 +      } else {
1847 +        // in non-periodic simulations, without explicitly setting
1848 +        // selections, but if a sphereBradius has been set, just use that:
1849 +        areaB = 4.0 * M_PI * pow(sphereBRadius_, 2);
1850 +      }
1851 +    }
1852 +      
1853 +    dividingArea_ = min(areaA, areaB);
1854 +    hasDividingArea_ = true;
1855 +    return dividingArea_;
1856 +  }
1857 +  
1858 +  void RNEMD::doRNEMD() {
1859 +    if (!doRNEMD_) return;
1860      trialCount_++;
1861 +
1862 +    // object evaluator:
1863 +    evaluator_.loadScriptString(rnemdObjectSelection_);
1864 +    seleMan_.setSelectionSet(evaluator_.evaluate());
1865 +
1866 +    evaluatorA_.loadScriptString(selectionA_);
1867 +    evaluatorB_.loadScriptString(selectionB_);
1868 +
1869 +    seleManA_.setSelectionSet(evaluatorA_.evaluate());
1870 +    seleManB_.setSelectionSet(evaluatorB_.evaluate());
1871 +
1872 +    commonA_ = seleManA_ & seleMan_;
1873 +    commonB_ = seleManB_ & seleMan_;
1874 +
1875 +    // Target exchange quantities (in each exchange) = dividingArea * dt * flux
1876 +    // dt = exchange time interval
1877 +    // flux = target flux
1878 +    // dividingArea = smallest dividing surface between the two regions
1879 +
1880 +    hasDividingArea_ = false;
1881 +    RealType area = getDividingArea();
1882 +
1883 +    kineticTarget_ = kineticFlux_ * exchangeTime_ * area;
1884 +    momentumTarget_ = momentumFluxVector_ * exchangeTime_ * area;
1885 +    angularMomentumTarget_ = angularMomentumFluxVector_ * exchangeTime_ * area;
1886 +
1887      switch(rnemdMethod_) {
1888      case rnemdSwap:
1889 <      doSwap();
1889 >      doSwap(commonA_, commonB_);
1890        break;
1891      case rnemdNIVS:
1892 <      doNIVS();
1892 >      doNIVS(commonA_, commonB_);
1893        break;
1894      case rnemdVSS:
1895 <      doVSS();
1895 >      doVSS(commonA_, commonB_);
1896        break;
1897      case rnemdUnkownMethod:
1898      default :
# Line 1410 | Line 1901 | namespace OpenMD {
1901    }
1902  
1903    void RNEMD::collectData() {
1904 <
1904 >    if (!doRNEMD_) return;
1905      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1906 +
1907 +    // collectData can be called more frequently than the doRNEMD, so use the
1908 +    // computed area from the last exchange time:
1909 +    RealType area = getDividingArea();
1910 +    areaAccumulator_->add(area);
1911      Mat3x3d hmat = currentSnap_->getHmat();
1912 +    Vector3d u = angularMomentumFluxVector_;
1913 +    u.normalize();
1914  
1915      seleMan_.setSelectionSet(evaluator_.evaluate());
1916  
1917 <    int selei;
1917 >    int selei(0);
1918      StuntDouble* sd;
1919 <    int idx;
1919 >    int binNo;
1920 >    RealType mass;
1921 >    Vector3d vel;
1922 >    Vector3d rPos;
1923 >    RealType KE;
1924 >    Vector3d L;
1925 >    Mat3x3d I;
1926 >    RealType r2;
1927  
1928      vector<RealType> binMass(nBins_, 0.0);
1929 <    vector<RealType> binPx(nBins_, 0.0);
1930 <    vector<RealType> binPy(nBins_, 0.0);
1931 <    vector<RealType> binPz(nBins_, 0.0);
1929 >    vector<Vector3d> binP(nBins_, V3Zero);
1930 >    vector<RealType> binOmega(nBins_, 0.0);
1931 >    vector<Vector3d> binL(nBins_, V3Zero);
1932 >    vector<Mat3x3d>  binI(nBins_);
1933      vector<RealType> binKE(nBins_, 0.0);
1934      vector<int> binDOF(nBins_, 0);
1935      vector<int> binCount(nBins_, 0);
# Line 1431 | Line 1937 | namespace OpenMD {
1937      // alternative approach, track all molecules instead of only those
1938      // selected for scaling/swapping:
1939      /*
1940 <    SimInfo::MoleculeIterator miter;
1941 <    vector<StuntDouble*>::iterator iiter;
1942 <    Molecule* mol;
1943 <    StuntDouble* sd;
1944 <    for (mol = info_->beginMolecule(miter); mol != NULL;
1940 >      SimInfo::MoleculeIterator miter;
1941 >      vector<StuntDouble*>::iterator iiter;
1942 >      Molecule* mol;
1943 >      StuntDouble* sd;
1944 >      for (mol = info_->beginMolecule(miter); mol != NULL;
1945        mol = info_->nextMolecule(miter))
1946        sd is essentially sd
1947 <        for (sd = mol->beginIntegrableObject(iiter);
1948 <             sd != NULL;
1949 <             sd = mol->nextIntegrableObject(iiter))
1947 >      for (sd = mol->beginIntegrableObject(iiter);
1948 >      sd != NULL;
1949 >      sd = mol->nextIntegrableObject(iiter))
1950      */
1951 +
1952      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1953 <         sd = seleMan_.nextSelected(selei)) {
1954 <      
1448 <      idx = sd->getLocalIndex();
1449 <      
1953 >         sd = seleMan_.nextSelected(selei)) {    
1954 >    
1955        Vector3d pos = sd->getPos();
1956  
1957        // wrap the stuntdouble's position back into the box:
1958        
1959 <      if (usePeriodicBoundaryConditions_)
1959 >      if (usePeriodicBoundaryConditions_) {
1960          currentSnap_->wrapVector(pos);
1961 +        // which bin is this stuntdouble in?
1962 +        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1963 +        // Shift molecules by half a box to have bins start at 0
1964 +        // The modulo operator is used to wrap the case when we are
1965 +        // beyond the end of the bins back to the beginning.
1966 +        binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1967 +      } else {
1968 +        Vector3d rPos = pos - coordinateOrigin_;
1969 +        binNo = int(rPos.length() / binWidth_);
1970 +      }
1971  
1972 <      // which bin is this stuntdouble in?
1973 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1974 <      // Shift molecules by half a box to have bins start at 0
1975 <      // The modulo operator is used to wrap the case when we are
1976 <      // beyond the end of the bins back to the beginning.
1977 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1978 <    
1979 <      RealType mass = sd->getMass();
1980 <      Vector3d vel = sd->getVel();
1972 >      mass = sd->getMass();
1973 >      vel = sd->getVel();
1974 >      rPos = sd->getPos() - coordinateOrigin_;
1975 >      KE = 0.5 * mass * vel.lengthSquare();
1976 >      L = mass * cross(rPos, vel);
1977 >      I = outProduct(rPos, rPos) * mass;
1978 >      r2 = rPos.lengthSquare();
1979 >      I(0, 0) += mass * r2;
1980 >      I(1, 1) += mass * r2;
1981 >      I(2, 2) += mass * r2;
1982  
1983 <      binCount[binNo]++;
1984 <      binMass[binNo] += mass;
1985 <      binPx[binNo] += mass*vel.x();
1986 <      binPy[binNo] += mass*vel.y();
1987 <      binPz[binNo] += mass*vel.z();
1988 <      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1989 <      binDOF[binNo] += 3;
1983 >      // Project the relative position onto a plane perpendicular to
1984 >      // the angularMomentumFluxVector:
1985 >      // Vector3d rProj = rPos - dot(rPos, u) * u;
1986 >      // Project the velocity onto a plane perpendicular to the
1987 >      // angularMomentumFluxVector:
1988 >      // Vector3d vProj = vel  - dot(vel, u) * u;
1989 >      // Compute angular velocity vector (should be nearly parallel to
1990 >      // angularMomentumFluxVector
1991 >      // Vector3d aVel = cross(rProj, vProj);
1992  
1993 <      if (sd->isDirectional()) {
1994 <        Vector3d angMom = sd->getJ();
1995 <        Mat3x3d I = sd->getI();
1996 <        if (sd->isLinear()) {
1997 <          int i = sd->linearAxis();
1998 <          int j = (i + 1) % 3;
1999 <          int k = (i + 2) % 3;
2000 <          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
2001 <                                 angMom[k] * angMom[k] / I(k, k));
2002 <          binDOF[binNo] += 2;
2003 <        } else {
2004 <          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
2005 <                                 angMom[1] * angMom[1] / I(1, 1) +
2006 <                                 angMom[2] * angMom[2] / I(2, 2));
2007 <          binDOF[binNo] += 3;
1993 >      if (binNo >= 0 && binNo < nBins_)  {
1994 >        binCount[binNo]++;
1995 >        binMass[binNo] += mass;
1996 >        binP[binNo] += mass*vel;
1997 >        binKE[binNo] += KE;
1998 >        binI[binNo] += I;
1999 >        binL[binNo] += L;
2000 >        binDOF[binNo] += 3;
2001 >        
2002 >        if (sd->isDirectional()) {
2003 >          Vector3d angMom = sd->getJ();
2004 >          Mat3x3d Ia = sd->getI();
2005 >          if (sd->isLinear()) {
2006 >            int i = sd->linearAxis();
2007 >            int j = (i + 1) % 3;
2008 >            int k = (i + 2) % 3;
2009 >            binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / Ia(j, j) +
2010 >                                   angMom[k] * angMom[k] / Ia(k, k));
2011 >            binDOF[binNo] += 2;
2012 >          } else {
2013 >            binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / Ia(0, 0) +
2014 >                                   angMom[1] * angMom[1] / Ia(1, 1) +
2015 >                                   angMom[2] * angMom[2] / Ia(2, 2));
2016 >            binDOF[binNo] += 3;
2017 >          }
2018          }
2019        }
2020      }
1493    
2021  
2022   #ifdef IS_MPI
2023 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
2024 <                              nBins_, MPI::INT, MPI::SUM);
2025 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
2026 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2027 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
2028 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2029 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
2030 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2031 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
2032 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2033 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
2034 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2035 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
2036 <                              nBins_, MPI::INT, MPI::SUM);
2023 >
2024 >    for (int i = 0; i < nBins_; i++) {
2025 >      
2026 >      MPI_Allreduce(MPI_IN_PLACE, &binCount[i],
2027 >                    1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
2028 >      MPI_Allreduce(MPI_IN_PLACE, &binMass[i],
2029 >                    1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2030 >      MPI_Allreduce(MPI_IN_PLACE, binP[i].getArrayPointer(),
2031 >                    3, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2032 >      MPI_Allreduce(MPI_IN_PLACE, binL[i].getArrayPointer(),
2033 >                    3, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2034 >      MPI_Allreduce(MPI_IN_PLACE, binI[i].getArrayPointer(),
2035 >                    9, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2036 >      MPI_Allreduce(MPI_IN_PLACE, &binKE[i],
2037 >                    1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2038 >      MPI_Allreduce(MPI_IN_PLACE, &binDOF[i],
2039 >                    1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
2040 >      //MPI_Allreduce(MPI_IN_PLACE, &binOmega[i],
2041 >      //                          1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2042 >    }
2043 >    
2044   #endif
2045  
2046 <    Vector3d vel;
2046 >    Vector3d omega;
2047      RealType den;
2048      RealType temp;
2049      RealType z;
2050 +    RealType r;
2051      for (int i = 0; i < nBins_; i++) {
2052 <      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
2053 <      vel.x() = binPx[i] / binMass[i];
2054 <      vel.y() = binPy[i] / binMass[i];
2055 <      vel.z() = binPz[i] / binMass[i];
2056 <      den = binCount[i] * nBins_ / (hmat(0,0) * hmat(1,1) * hmat(2,2));
2057 <      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
2058 <                               PhysicalConstants::energyConvert);
2052 >      if (usePeriodicBoundaryConditions_) {
2053 >        z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
2054 >        den = binMass[i] * nBins_ * PhysicalConstants::densityConvert
2055 >          / currentSnap_->getVolume() ;
2056 >      } else {
2057 >        r = (((RealType)i + 0.5) * binWidth_);
2058 >        RealType rinner = (RealType)i * binWidth_;
2059 >        RealType router = (RealType)(i+1) * binWidth_;
2060 >        den = binMass[i] * 3.0 * PhysicalConstants::densityConvert
2061 >          / (4.0 * M_PI * (pow(router,3) - pow(rinner,3)));
2062 >      }
2063 >      vel = binP[i] / binMass[i];
2064  
2065 <      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
2066 <        if(outputMask_[j]) {
2067 <          switch(j) {
2068 <          case Z:
2069 <            (data_[j].accumulator[i])->add(z);
2070 <            break;
2071 <          case TEMPERATURE:
2072 <            data_[j].accumulator[i]->add(temp);
2073 <            break;
2074 <          case VELOCITY:
2075 <            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
2076 <            break;
2077 <          case DENSITY:
2078 <            data_[j].accumulator[i]->add(den);
2079 <            break;
2065 >      omega = binI[i].inverse() * binL[i];
2066 >
2067 >      // omega = binOmega[i] / binCount[i];
2068 >
2069 >      if (binCount[i] > 0) {
2070 >        // only add values if there are things to add
2071 >        temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
2072 >                                 PhysicalConstants::energyConvert);
2073 >        
2074 >        for (unsigned int j = 0; j < outputMask_.size(); ++j) {
2075 >          if(outputMask_[j]) {
2076 >            switch(j) {
2077 >            case Z:
2078 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(z);
2079 >              break;
2080 >            case R:
2081 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(r);
2082 >              break;
2083 >            case TEMPERATURE:
2084 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(temp);
2085 >              break;
2086 >            case VELOCITY:
2087 >              dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
2088 >              break;
2089 >            case ANGULARVELOCITY:  
2090 >              dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(omega);
2091 >              break;
2092 >            case DENSITY:
2093 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(den);
2094 >              break;
2095 >            }
2096            }
2097          }
2098        }
2099      }
2100 +    hasData_ = true;
2101    }
2102  
2103    void RNEMD::getStarted() {
2104 +    if (!doRNEMD_) return;
2105 +    hasDividingArea_ = false;
2106      collectData();
2107      writeOutputFile();
2108    }
2109  
2110    void RNEMD::parseOutputFileFormat(const std::string& format) {
2111 +    if (!doRNEMD_) return;
2112      StringTokenizer tokenizer(format, " ,;|\t\n\r");
2113      
2114      while(tokenizer.hasMoreTokens()) {
# Line 1565 | Line 2125 | namespace OpenMD {
2125          painCave.severity = OPENMD_ERROR;
2126          simError();            
2127        }
2128 <    }  
2128 >    }
2129    }
2130    
2131    void RNEMD::writeOutputFile() {
2132 +    if (!doRNEMD_) return;
2133 +    if (!hasData_) return;
2134      
2135   #ifdef IS_MPI
2136      // If we're the root node, should we print out the results
2137 <    int worldRank = MPI::COMM_WORLD.Get_rank();
2137 >    int worldRank;
2138 >    MPI_Comm_rank( MPI_COMM_WORLD, &worldRank);
2139 >
2140      if (worldRank == 0) {
2141   #endif
2142        rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
# Line 1588 | Line 2152 | namespace OpenMD {
2152        Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
2153  
2154        RealType time = currentSnap_->getTime();
2155 <      
2156 <      
2155 >      RealType avgArea;
2156 >      areaAccumulator_->getAverage(avgArea);
2157 >
2158 >      RealType Jz(0.0);
2159 >      Vector3d JzP(V3Zero);
2160 >      Vector3d JzL(V3Zero);
2161 >      if (time >= info_->getSimParams()->getDt()) {
2162 >        Jz = kineticExchange_ / (time * avgArea)
2163 >          / PhysicalConstants::energyConvert;
2164 >        JzP = momentumExchange_ / (time * avgArea);
2165 >        JzL = angularMomentumExchange_ / (time * avgArea);
2166 >      }
2167 >
2168        rnemdFile_ << "#######################################################\n";
2169        rnemdFile_ << "# RNEMD {\n";
2170  
2171        map<string, RNEMDMethod>::iterator mi;
2172        for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
2173          if ( (*mi).second == rnemdMethod_)
2174 <          rnemdFile_ << "#    exchangeMethod  = " << (*mi).first << "\n";
2174 >          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
2175        }
2176        map<string, RNEMDFluxType>::iterator fi;
2177        for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
2178          if ( (*fi).second == rnemdFluxType_)
2179 <          rnemdFile_ << "#    fluxType  = " << (*fi).first << "\n";
2179 >          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
2180        }
2181        
2182 <      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << " fs\n";
2182 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
2183  
2184        rnemdFile_ << "#    objectSelection = \""
2185 <                 << rnemdObjectSelection_ << "\"\n";
2186 <      rnemdFile_ << "#    slabWidth = " << slabWidth_ << " angstroms\n";
2187 <      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << " angstroms\n";
1613 <      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << " angstroms\n";
2185 >                 << rnemdObjectSelection_ << "\";\n";
2186 >      rnemdFile_ << "#    selectionA = \"" << selectionA_ << "\";\n";
2187 >      rnemdFile_ << "#    selectionB = \"" << selectionB_ << "\";\n";
2188        rnemdFile_ << "# }\n";
2189        rnemdFile_ << "#######################################################\n";
2190 <      
2191 <      rnemdFile_ << "# running time = " << time << " fs\n";
2192 <      rnemdFile_ << "# target kinetic flux = " << kineticFlux_ << "\n";
2193 <      rnemdFile_ << "# target momentum flux = " << momentumFluxVector_ << "\n";
2194 <      
2195 <      rnemdFile_ << "# target one-time kinetic exchange = " << kineticTarget_
2196 <                 << "\n";
2197 <      rnemdFile_ << "# target one-time momentum exchange = " << momentumTarget_
2198 <                 << "\n";
2199 <      
2200 <      rnemdFile_ << "# actual kinetic exchange = " << kineticExchange_ << "\n";
2201 <      rnemdFile_ << "# actual momentum exchange = " << momentumExchange_
2202 <                 << "\n";
2203 <      
2204 <      rnemdFile_ << "# attempted exchanges: " << trialCount_ << "\n";
2205 <      rnemdFile_ << "# failed exchanges: " << failTrialCount_ << "\n";
2206 <
2207 <      
2190 >      rnemdFile_ << "# RNEMD report:\n";      
2191 >      rnemdFile_ << "#      running time = " << time << " fs\n";
2192 >      rnemdFile_ << "# Target flux:\n";
2193 >      rnemdFile_ << "#           kinetic = "
2194 >                 << kineticFlux_ / PhysicalConstants::energyConvert
2195 >                 << " (kcal/mol/A^2/fs)\n";
2196 >      rnemdFile_ << "#          momentum = " << momentumFluxVector_
2197 >                 << " (amu/A/fs^2)\n";
2198 >      rnemdFile_ << "#  angular momentum = " << angularMomentumFluxVector_
2199 >                 << " (amu/A^2/fs^2)\n";
2200 >      rnemdFile_ << "# Target one-time exchanges:\n";
2201 >      rnemdFile_ << "#          kinetic = "
2202 >                 << kineticTarget_ / PhysicalConstants::energyConvert
2203 >                 << " (kcal/mol)\n";
2204 >      rnemdFile_ << "#          momentum = " << momentumTarget_
2205 >                 << " (amu*A/fs)\n";
2206 >      rnemdFile_ << "#  angular momentum = " << angularMomentumTarget_
2207 >                 << " (amu*A^2/fs)\n";
2208 >      rnemdFile_ << "# Actual exchange totals:\n";
2209 >      rnemdFile_ << "#          kinetic = "
2210 >                 << kineticExchange_ / PhysicalConstants::energyConvert
2211 >                 << " (kcal/mol)\n";
2212 >      rnemdFile_ << "#          momentum = " << momentumExchange_
2213 >                 << " (amu*A/fs)\n";      
2214 >      rnemdFile_ << "#  angular momentum = " << angularMomentumExchange_
2215 >                 << " (amu*A^2/fs)\n";      
2216 >      rnemdFile_ << "# Actual flux:\n";
2217 >      rnemdFile_ << "#          kinetic = " << Jz
2218 >                 << " (kcal/mol/A^2/fs)\n";
2219 >      rnemdFile_ << "#          momentum = " << JzP
2220 >                 << " (amu/A/fs^2)\n";
2221 >      rnemdFile_ << "#  angular momentum = " << JzL
2222 >                 << " (amu/A^2/fs^2)\n";
2223 >      rnemdFile_ << "# Exchange statistics:\n";
2224 >      rnemdFile_ << "#               attempted = " << trialCount_ << "\n";
2225 >      rnemdFile_ << "#                  failed = " << failTrialCount_ << "\n";
2226        if (rnemdMethod_ == rnemdNIVS) {
2227 <        rnemdFile_ << "# NIVS root-check warnings: " << failRootCount_ << "\n";
2227 >        rnemdFile_ << "#  NIVS root-check errors = "
2228 >                   << failRootCount_ << "\n";
2229        }
1637
2230        rnemdFile_ << "#######################################################\n";
2231        
2232        
# Line 1645 | Line 2237 | namespace OpenMD {
2237          if (outputMask_[i]) {
2238            rnemdFile_ << "\t" << data_[i].title <<
2239              "(" << data_[i].units << ")";
2240 +          // add some extra tabs for column alignment
2241 +          if (data_[i].dataType == "Vector3d") rnemdFile_ << "\t\t";
2242          }
2243        }
2244        rnemdFile_ << std::endl;
2245        
2246        rnemdFile_.precision(8);
2247        
2248 <      for (unsigned int j = 0; j < nBins_; j++) {        
2248 >      for (int j = 0; j < nBins_; j++) {        
2249          
2250          for (unsigned int i = 0; i < outputMask_.size(); ++i) {
2251            if (outputMask_[i]) {
2252              if (data_[i].dataType == "RealType")
2253                writeReal(i,j);
2254 <            else if (data_[i].dataType == "Vector3d")
2254 >            else if (data_[i].dataType == "Vector3d")
2255                writeVector(i,j);
2256              else {
2257                sprintf( painCave.errMsg,
# Line 1671 | Line 2265 | namespace OpenMD {
2265          rnemdFile_ << std::endl;
2266          
2267        }        
2268 +
2269 +      rnemdFile_ << "#######################################################\n";
2270 +      rnemdFile_ << "# 95% confidence intervals in those quantities follow:\n";
2271 +      rnemdFile_ << "#######################################################\n";
2272 +
2273 +
2274 +      for (int j = 0; j < nBins_; j++) {        
2275 +        rnemdFile_ << "#";
2276 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
2277 +          if (outputMask_[i]) {
2278 +            if (data_[i].dataType == "RealType")
2279 +              writeRealErrorBars(i,j);
2280 +            else if (data_[i].dataType == "Vector3d")
2281 +              writeVectorErrorBars(i,j);
2282 +            else {
2283 +              sprintf( painCave.errMsg,
2284 +                       "RNEMD found an unknown data type for: %s ",
2285 +                       data_[i].title.c_str());
2286 +              painCave.isFatal = 1;
2287 +              simError();
2288 +            }
2289 +          }
2290 +        }
2291 +        rnemdFile_ << std::endl;
2292 +        
2293 +      }        
2294        
2295        rnemdFile_.flush();
2296        rnemdFile_.close();
# Line 1682 | Line 2302 | namespace OpenMD {
2302    }
2303    
2304    void RNEMD::writeReal(int index, unsigned int bin) {
2305 +    if (!doRNEMD_) return;
2306      assert(index >=0 && index < ENDINDEX);
2307 <    assert(bin >=0 && bin < nBins_);
2307 >    assert(int(bin) < nBins_);
2308      RealType s;
2309 +    int count;
2310      
2311 <    data_[index].accumulator[bin]->getAverage(s);
2311 >    count = data_[index].accumulator[bin]->count();
2312 >    if (count == 0) return;
2313      
2314 +    dynamic_cast<Accumulator *>(data_[index].accumulator[bin])->getAverage(s);
2315 +    
2316      if (! isinf(s) && ! isnan(s)) {
2317        rnemdFile_ << "\t" << s;
2318      } else{
2319        sprintf( painCave.errMsg,
2320 <               "RNEMD detected a numerical error writing: %s for bin %d",
2320 >               "RNEMD detected a numerical error writing: %s for bin %u",
2321                 data_[index].title.c_str(), bin);
2322        painCave.isFatal = 1;
2323        simError();
# Line 1700 | Line 2325 | namespace OpenMD {
2325    }
2326    
2327    void RNEMD::writeVector(int index, unsigned int bin) {
2328 +    if (!doRNEMD_) return;
2329      assert(index >=0 && index < ENDINDEX);
2330 <    assert(bin >=0 && bin < nBins_);
2330 >    assert(int(bin) < nBins_);
2331      Vector3d s;
2332 +    int count;
2333 +    
2334 +    count = data_[index].accumulator[bin]->count();
2335 +
2336 +    if (count == 0) return;
2337 +
2338      dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
2339      if (isinf(s[0]) || isnan(s[0]) ||
2340          isinf(s[1]) || isnan(s[1]) ||
2341          isinf(s[2]) || isnan(s[2]) ) {      
2342        sprintf( painCave.errMsg,
2343 <               "RNEMD detected a numerical error writing: %s for bin %d",
2343 >               "RNEMD detected a numerical error writing: %s for bin %u",
2344                 data_[index].title.c_str(), bin);
2345        painCave.isFatal = 1;
2346        simError();
# Line 1716 | Line 2348 | namespace OpenMD {
2348        rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
2349      }
2350    }  
2351 +
2352 +  void RNEMD::writeRealErrorBars(int index, unsigned int bin) {
2353 +    if (!doRNEMD_) return;
2354 +    assert(index >=0 && index < ENDINDEX);
2355 +    assert(int(bin) < nBins_);
2356 +    RealType s;
2357 +    int count;
2358 +    
2359 +    count = data_[index].accumulator[bin]->count();
2360 +    if (count == 0) return;
2361 +    
2362 +    dynamic_cast<Accumulator *>(data_[index].accumulator[bin])->get95percentConfidenceInterval(s);
2363 +    
2364 +    if (! isinf(s) && ! isnan(s)) {
2365 +      rnemdFile_ << "\t" << s;
2366 +    } else{
2367 +      sprintf( painCave.errMsg,
2368 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %u",
2369 +               data_[index].title.c_str(), bin);
2370 +      painCave.isFatal = 1;
2371 +      simError();
2372 +    }    
2373 +  }
2374 +  
2375 +  void RNEMD::writeVectorErrorBars(int index, unsigned int bin) {
2376 +    if (!doRNEMD_) return;
2377 +    assert(index >=0 && index < ENDINDEX);
2378 +    assert(int(bin) < nBins_);
2379 +    Vector3d s;
2380 +    int count;
2381 +    
2382 +    count = data_[index].accumulator[bin]->count();
2383 +    if (count == 0) return;
2384 +
2385 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->get95percentConfidenceInterval(s);
2386 +    if (isinf(s[0]) || isnan(s[0]) ||
2387 +        isinf(s[1]) || isnan(s[1]) ||
2388 +        isinf(s[2]) || isnan(s[2]) ) {      
2389 +      sprintf( painCave.errMsg,
2390 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %u",
2391 +               data_[index].title.c_str(), bin);
2392 +      painCave.isFatal = 1;
2393 +      simError();
2394 +    } else {
2395 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
2396 +    }
2397 +  }  
2398   }
2399  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines