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 2056 by gezelter, Fri Feb 20 15:12:07 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;  
658 >    int min_found = 0;  
659      StuntDouble* min_sd;
660  
661      RealType max_val;
662 <    bool max_found = false;
662 >    int max_found = 0;
663      StuntDouble* max_sd;
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 +    RealType time = currentSnap_->getTime();    
1024      Mat3x3d hmat = currentSnap_->getHmat();
1025  
771    seleMan_.setSelectionSet(evaluator_.evaluate());
772
773    int selei;
1026      StuntDouble* sd;
775    int idx;
1027  
1028      vector<StuntDouble*> hotBin, coldBin;
1029  
# Line 791 | Line 1042 | namespace OpenMD {
1042      RealType Kcz = 0.0;
1043      RealType Kcw = 0.0;
1044  
1045 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1046 <         sd = seleMan_.nextSelected(selei)) {
1045 >    for (sd = smanA.beginSelected(selei); sd != NULL;
1046 >         sd = smanA.nextSelected(selei)) {
1047  
797      idx = sd->getLocalIndex();
798
1048        Vector3d pos = sd->getPos();
1049 <
1049 >      
1050        // wrap the stuntdouble's position back into the box:
1051 <
1051 >      
1052        if (usePeriodicBoundaryConditions_)
1053          currentSnap_->wrapVector(pos);
1054 <
1055 <      // which bin is this stuntdouble in?
1056 <      bool inA = inSlabA(pos);
1057 <      bool inB = inSlabB(pos);
1054 >      
1055 >      
1056 >      RealType mass = sd->getMass();
1057 >      Vector3d vel = sd->getVel();
1058 >      
1059 >      hotBin.push_back(sd);
1060 >      Phx += mass * vel.x();
1061 >      Phy += mass * vel.y();
1062 >      Phz += mass * vel.z();
1063 >      Khx += mass * vel.x() * vel.x();
1064 >      Khy += mass * vel.y() * vel.y();
1065 >      Khz += mass * vel.z() * vel.z();
1066 >      if (sd->isDirectional()) {
1067 >        Vector3d angMom = sd->getJ();
1068 >        Mat3x3d I = sd->getI();
1069 >        if (sd->isLinear()) {
1070 >          int i = sd->linearAxis();
1071 >          int j = (i + 1) % 3;
1072 >          int k = (i + 2) % 3;
1073 >          Khw += angMom[j] * angMom[j] / I(j, j) +
1074 >            angMom[k] * angMom[k] / I(k, k);
1075 >        } else {
1076 >          Khw += angMom[0]*angMom[0]/I(0, 0)
1077 >            + angMom[1]*angMom[1]/I(1, 1)
1078 >            + angMom[2]*angMom[2]/I(2, 2);
1079 >        }
1080 >      }
1081 >    }
1082 >    for (sd = smanB.beginSelected(selej); sd != NULL;
1083 >         sd = smanB.nextSelected(selej)) {
1084 >      Vector3d pos = sd->getPos();
1085 >      
1086 >      // wrap the stuntdouble's position back into the box:
1087 >      
1088 >      if (usePeriodicBoundaryConditions_)
1089 >        currentSnap_->wrapVector(pos);
1090 >            
1091 >      RealType mass = sd->getMass();
1092 >      Vector3d vel = sd->getVel();
1093  
1094 <      if (inA || inB) {
1095 <              
1096 <        RealType mass = sd->getMass();
1097 <        Vector3d vel = sd->getVel();
1098 <      
1099 <        if (inA) {
1100 <          hotBin.push_back(sd);
1101 <          Phx += mass * vel.x();
1102 <          Phy += mass * vel.y();
1103 <          Phz += mass * vel.z();
1104 <          Khx += mass * vel.x() * vel.x();
1105 <          Khy += mass * vel.y() * vel.y();
1106 <          Khz += mass * vel.z() * vel.z();
1107 <          if (sd->isDirectional()) {
1108 <            Vector3d angMom = sd->getJ();
1109 <            Mat3x3d I = sd->getI();
1110 <            if (sd->isLinear()) {
1111 <              int i = sd->linearAxis();
1112 <              int j = (i + 1) % 3;
1113 <              int k = (i + 2) % 3;
1114 <              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 <        }
1094 >      coldBin.push_back(sd);
1095 >      Pcx += mass * vel.x();
1096 >      Pcy += mass * vel.y();
1097 >      Pcz += mass * vel.z();
1098 >      Kcx += mass * vel.x() * vel.x();
1099 >      Kcy += mass * vel.y() * vel.y();
1100 >      Kcz += mass * vel.z() * vel.z();
1101 >      if (sd->isDirectional()) {
1102 >        Vector3d angMom = sd->getJ();
1103 >        Mat3x3d I = sd->getI();
1104 >        if (sd->isLinear()) {
1105 >          int i = sd->linearAxis();
1106 >          int j = (i + 1) % 3;
1107 >          int k = (i + 2) % 3;
1108 >          Kcw += angMom[j] * angMom[j] / I(j, j) +
1109 >            angMom[k] * angMom[k] / I(k, k);
1110 >        } else {
1111 >          Kcw += angMom[0]*angMom[0]/I(0, 0)
1112 >            + angMom[1]*angMom[1]/I(1, 1)
1113 >            + angMom[2]*angMom[2]/I(2, 2);
1114 >        }
1115        }
1116      }
1117      
# Line 872 | Line 1125 | namespace OpenMD {
1125      Kcw *= 0.5;
1126  
1127   #ifdef IS_MPI
1128 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
1129 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
1130 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phz, 1, MPI::REALTYPE, MPI::SUM);
1131 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcx, 1, MPI::REALTYPE, MPI::SUM);
1132 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcy, 1, MPI::REALTYPE, MPI::SUM);
1133 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcz, 1, MPI::REALTYPE, MPI::SUM);
1128 >    MPI_Allreduce(MPI_IN_PLACE, &Phx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1129 >    MPI_Allreduce(MPI_IN_PLACE, &Phy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1130 >    MPI_Allreduce(MPI_IN_PLACE, &Phz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1131 >    MPI_Allreduce(MPI_IN_PLACE, &Pcx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1132 >    MPI_Allreduce(MPI_IN_PLACE, &Pcy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1133 >    MPI_Allreduce(MPI_IN_PLACE, &Pcz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1134  
1135 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
1136 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
1137 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
1138 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khw, 1, MPI::REALTYPE, MPI::SUM);
1135 >    MPI_Allreduce(MPI_IN_PLACE, &Khx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1136 >    MPI_Allreduce(MPI_IN_PLACE, &Khy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1137 >    MPI_Allreduce(MPI_IN_PLACE, &Khz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1138 >    MPI_Allreduce(MPI_IN_PLACE, &Khw, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1139  
1140 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
1141 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
1142 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
1143 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcw, 1, MPI::REALTYPE, MPI::SUM);
1140 >    MPI_Allreduce(MPI_IN_PLACE, &Kcx, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1141 >    MPI_Allreduce(MPI_IN_PLACE, &Kcy, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1142 >    MPI_Allreduce(MPI_IN_PLACE, &Kcz, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1143 >    MPI_Allreduce(MPI_IN_PLACE, &Kcw, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1144   #endif
1145  
1146      //solve coldBin coeff's first
# Line 908 | Line 1161 | namespace OpenMD {
1161  
1162        if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
1163          c = sqrt(c);
1164 <        //std::cerr << "cold slab scaling coefficient: " << c << endl;
912 <        //now convert to hotBin coefficient
1164 >
1165          RealType w = 0.0;
1166          if (rnemdFluxType_ ==  rnemdFullKE) {
1167            x = 1.0 + px * (1.0 - c);
# Line 936 | Line 1188 | namespace OpenMD {
1188            //if w is in the right range, so should be x, y, z.
1189            vector<StuntDouble*>::iterator sdi;
1190            Vector3d vel;
1191 <          for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1191 >          for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1192              if (rnemdFluxType_ == rnemdFullKE) {
1193                vel = (*sdi)->getVel() * c;
1194                (*sdi)->setVel(vel);
# Line 947 | Line 1199 | namespace OpenMD {
1199              }
1200            }
1201            w = sqrt(w);
1202 <          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
951 <          //           << "\twh= " << w << endl;
952 <          for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1202 >          for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1203              if (rnemdFluxType_ == rnemdFullKE) {
1204                vel = (*sdi)->getVel();
1205                vel.x() *= x;
# Line 1068 | Line 1318 | namespace OpenMD {
1318        vector<RealType>::iterator ri;
1319        RealType r1, r2, alpha0;
1320        vector<pair<RealType,RealType> > rps;
1321 <      for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
1321 >      for (ri = realRoots.begin(); ri !=realRoots.end(); ++ri) {
1322          r2 = *ri;
1323          //check if FindRealRoots() give the right answer
1324          if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
# Line 1100 | Line 1350 | namespace OpenMD {
1350          RealType diff;
1351          pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
1352          vector<pair<RealType,RealType> >::iterator rpi;
1353 <        for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1353 >        for (rpi = rps.begin(); rpi != rps.end(); ++rpi) {
1354            r1 = (*rpi).first;
1355            r2 = (*rpi).second;
1356            switch(rnemdFluxType_) {
# Line 1167 | Line 1417 | namespace OpenMD {
1417          }
1418          vector<StuntDouble*>::iterator sdi;
1419          Vector3d vel;
1420 <        for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1420 >        for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1421            vel = (*sdi)->getVel();
1422            vel.x() *= x;
1423            vel.y() *= y;
# Line 1178 | Line 1428 | namespace OpenMD {
1428          x = 1.0 + px * (1.0 - x);
1429          y = 1.0 + py * (1.0 - y);
1430          z = 1.0 + pz * (1.0 - z);
1431 <        for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1431 >        for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1432            vel = (*sdi)->getVel();
1433            vel.x() *= x;
1434            vel.y() *= y;
# Line 1211 | Line 1461 | namespace OpenMD {
1461        failTrialCount_++;
1462      }
1463    }
1464 <
1465 <  void RNEMD::doVSS() {
1464 >  
1465 >  void RNEMD::doVSS(SelectionManager& smanA, SelectionManager& smanB) {
1466 >    if (!doRNEMD_) return;
1467 >    int selei;
1468 >    int selej;
1469  
1470      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1471      RealType time = currentSnap_->getTime();    
1472      Mat3x3d hmat = currentSnap_->getHmat();
1473  
1221    seleMan_.setSelectionSet(evaluator_.evaluate());
1222
1223    int selei;
1474      StuntDouble* sd;
1225    int idx;
1475  
1476      vector<StuntDouble*> hotBin, coldBin;
1477  
1478      Vector3d Ph(V3Zero);
1479 +    Vector3d Lh(V3Zero);
1480      RealType Mh = 0.0;
1481 +    Mat3x3d Ih(0.0);
1482      RealType Kh = 0.0;
1483      Vector3d Pc(V3Zero);
1484 +    Vector3d Lc(V3Zero);
1485      RealType Mc = 0.0;
1486 +    Mat3x3d Ic(0.0);
1487      RealType Kc = 0.0;
1235    
1488  
1489 <    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1490 <         sd = seleMan_.nextSelected(selei)) {
1489 >    // Constraints can be on only the linear or angular momentum, but
1490 >    // not both.  Usually, the user will specify which they want, but
1491 >    // in case they don't, the use of periodic boundaries should make
1492 >    // the choice for us.
1493 >    bool doLinearPart = false;
1494 >    bool doAngularPart = false;
1495  
1496 <      idx = sd->getLocalIndex();
1496 >    switch (rnemdFluxType_) {
1497 >    case rnemdPx:
1498 >    case rnemdPy:
1499 >    case rnemdPz:
1500 >    case rnemdPvector:
1501 >    case rnemdKePx:
1502 >    case rnemdKePy:
1503 >    case rnemdKePvector:
1504 >      doLinearPart = true;
1505 >      break;
1506 >    case rnemdLx:
1507 >    case rnemdLy:
1508 >    case rnemdLz:
1509 >    case rnemdLvector:
1510 >    case rnemdKeLx:
1511 >    case rnemdKeLy:
1512 >    case rnemdKeLz:
1513 >    case rnemdKeLvector:
1514 >      doAngularPart = true;
1515 >      break;
1516 >    case rnemdKE:
1517 >    case rnemdRotKE:
1518 >    case rnemdFullKE:
1519 >    default:
1520 >      if (usePeriodicBoundaryConditions_)
1521 >        doLinearPart = true;
1522 >      else
1523 >        doAngularPart = true;
1524 >      break;
1525 >    }
1526 >    
1527 >    for (sd = smanA.beginSelected(selei); sd != NULL;
1528 >         sd = smanA.nextSelected(selei)) {
1529  
1530        Vector3d pos = sd->getPos();
1531  
1532        // wrap the stuntdouble's position back into the box:
1533 +      
1534 +      if (usePeriodicBoundaryConditions_)
1535 +        currentSnap_->wrapVector(pos);
1536 +      
1537 +      RealType mass = sd->getMass();
1538 +      Vector3d vel = sd->getVel();
1539 +      Vector3d rPos = sd->getPos() - coordinateOrigin_;
1540 +      RealType r2;
1541 +      
1542 +      hotBin.push_back(sd);
1543 +      Ph += mass * vel;
1544 +      Mh += mass;
1545 +      Kh += mass * vel.lengthSquare();
1546 +      Lh += mass * cross(rPos, vel);
1547 +      Ih -= outProduct(rPos, rPos) * mass;
1548 +      r2 = rPos.lengthSquare();
1549 +      Ih(0, 0) += mass * r2;
1550 +      Ih(1, 1) += mass * r2;
1551 +      Ih(2, 2) += mass * r2;
1552 +      
1553 +      if (rnemdFluxType_ == rnemdFullKE) {
1554 +        if (sd->isDirectional()) {
1555 +          Vector3d angMom = sd->getJ();
1556 +          Mat3x3d I = sd->getI();
1557 +          if (sd->isLinear()) {
1558 +            int i = sd->linearAxis();
1559 +            int j = (i + 1) % 3;
1560 +            int k = (i + 2) % 3;
1561 +            Kh += angMom[j] * angMom[j] / I(j, j) +
1562 +              angMom[k] * angMom[k] / I(k, k);
1563 +          } else {
1564 +            Kh += angMom[0] * angMom[0] / I(0, 0) +
1565 +              angMom[1] * angMom[1] / I(1, 1) +
1566 +              angMom[2] * angMom[2] / I(2, 2);
1567 +          }
1568 +        }
1569 +      }
1570 +    }
1571 +    for (sd = smanB.beginSelected(selej); sd != NULL;
1572 +         sd = smanB.nextSelected(selej)) {
1573  
1574 +      Vector3d pos = sd->getPos();
1575 +      
1576 +      // wrap the stuntdouble's position back into the box:
1577 +      
1578        if (usePeriodicBoundaryConditions_)
1579          currentSnap_->wrapVector(pos);
1580 +      
1581 +      RealType mass = sd->getMass();
1582 +      Vector3d vel = sd->getVel();
1583 +      Vector3d rPos = sd->getPos() - coordinateOrigin_;
1584 +      RealType r2;
1585  
1586 <      // which bin is this stuntdouble in?
1587 <      bool inA = inSlabA(pos);
1588 <      bool inB = inSlabB(pos);
1586 >      coldBin.push_back(sd);
1587 >      Pc += mass * vel;
1588 >      Mc += mass;
1589 >      Kc += mass * vel.lengthSquare();
1590 >      Lc += mass * cross(rPos, vel);
1591 >      Ic -= outProduct(rPos, rPos) * mass;
1592 >      r2 = rPos.lengthSquare();
1593 >      Ic(0, 0) += mass * r2;
1594 >      Ic(1, 1) += mass * r2;
1595 >      Ic(2, 2) += mass * r2;
1596        
1597 <      if (inA || inB) {
1598 <        
1599 <        RealType mass = sd->getMass();
1600 <        Vector3d vel = sd->getVel();
1601 <      
1602 <        if (inA) {
1603 <          hotBin.push_back(sd);
1604 <          //std::cerr << "before, velocity = " << vel << endl;
1605 <          Ph += mass * vel;
1606 <          //std::cerr << "after, velocity = " << vel << endl;
1607 <          Mh += mass;
1608 <          Kh += mass * vel.lengthSquare();
1609 <          if (rnemdFluxType_ == rnemdFullKE) {
1610 <            if (sd->isDirectional()) {
1611 <              Vector3d angMom = sd->getJ();
1612 <              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 <        }
1597 >      if (rnemdFluxType_ == rnemdFullKE) {
1598 >        if (sd->isDirectional()) {
1599 >          Vector3d angMom = sd->getJ();
1600 >          Mat3x3d I = sd->getI();
1601 >          if (sd->isLinear()) {
1602 >            int i = sd->linearAxis();
1603 >            int j = (i + 1) % 3;
1604 >            int k = (i + 2) % 3;
1605 >            Kc += angMom[j] * angMom[j] / I(j, j) +
1606 >              angMom[k] * angMom[k] / I(k, k);
1607 >          } else {
1608 >            Kc += angMom[0] * angMom[0] / I(0, 0) +
1609 >              angMom[1] * angMom[1] / I(1, 1) +
1610 >              angMom[2] * angMom[2] / I(2, 2);
1611 >          }
1612 >        }
1613        }
1614      }
1615      
1616      Kh *= 0.5;
1617      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;
1618      
1619   #ifdef IS_MPI
1620 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1621 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
1622 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mh, 1, MPI::REALTYPE, MPI::SUM);
1623 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kh, 1, MPI::REALTYPE, MPI::SUM);
1624 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mc, 1, MPI::REALTYPE, MPI::SUM);
1625 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kc, 1, MPI::REALTYPE, MPI::SUM);
1620 >    MPI_Allreduce(MPI_IN_PLACE, &Ph[0], 3, MPI_REALTYPE, MPI_SUM,
1621 >                  MPI_COMM_WORLD);
1622 >    MPI_Allreduce(MPI_IN_PLACE, &Pc[0], 3, MPI_REALTYPE, MPI_SUM,
1623 >                  MPI_COMM_WORLD);
1624 >    MPI_Allreduce(MPI_IN_PLACE, &Lh[0], 3, MPI_REALTYPE, MPI_SUM,
1625 >                  MPI_COMM_WORLD);
1626 >    MPI_Allreduce(MPI_IN_PLACE, &Lc[0], 3, MPI_REALTYPE, MPI_SUM,
1627 >                  MPI_COMM_WORLD);
1628 >    MPI_Allreduce(MPI_IN_PLACE, &Mh, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1629 >    MPI_Allreduce(MPI_IN_PLACE, &Kh, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1630 >    MPI_Allreduce(MPI_IN_PLACE, &Mc, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1631 >    MPI_Allreduce(MPI_IN_PLACE, &Kc, 1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1632 >    MPI_Allreduce(MPI_IN_PLACE, Ih.getArrayPointer(), 9,
1633 >                  MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1634 >    MPI_Allreduce(MPI_IN_PLACE, Ic.getArrayPointer(), 9,
1635 >                  MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
1636   #endif
1637 +    
1638  
1639 +    Vector3d ac, acrec, bc, bcrec;
1640 +    Vector3d ah, ahrec, bh, bhrec;
1641 +
1642      bool successfulExchange = false;
1643      if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1644        Vector3d vc = Pc / Mc;
1645 <      Vector3d ac = -momentumTarget_ / Mc + vc;
1646 <      Vector3d acrec = -momentumTarget_ / Mc;
1647 <      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1645 >      ac = -momentumTarget_ / Mc + vc;
1646 >      acrec = -momentumTarget_ / Mc;
1647 >      
1648 >      // We now need the inverse of the inertia tensor to calculate the
1649 >      // angular velocity of the cold slab;
1650 >      Mat3x3d Ici = Ic.inverse();
1651 >      Vector3d omegac = Ici * Lc;
1652 >      bc  = -(Ici * angularMomentumTarget_) + omegac;
1653 >      bcrec = bc - omegac;
1654 >      
1655 >      RealType cNumerator = Kc - kineticTarget_;
1656 >      if (doLinearPart)
1657 >        cNumerator -= 0.5 * Mc * ac.lengthSquare();
1658 >      
1659 >      if (doAngularPart)
1660 >        cNumerator -= 0.5 * ( dot(bc, Ic * bc));
1661 >
1662        if (cNumerator > 0.0) {
1663 <        RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1663 >        
1664 >        RealType cDenominator = Kc;
1665 >
1666 >        if (doLinearPart)
1667 >          cDenominator -= 0.5 * Mc * vc.lengthSquare();
1668 >
1669 >        if (doAngularPart)
1670 >          cDenominator -= 0.5*(dot(omegac, Ic * omegac));
1671 >        
1672          if (cDenominator > 0.0) {
1673            RealType c = sqrt(cNumerator / cDenominator);
1674            if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1675 +            
1676              Vector3d vh = Ph / Mh;
1677 <            Vector3d ah = momentumTarget_ / Mh + vh;
1678 <            Vector3d ahrec = momentumTarget_ / Mh;
1679 <            RealType hNumerator = Kh + kineticTarget_
1680 <              - 0.5 * Mh * ah.lengthSquare();
1681 <            if (hNumerator > 0.0) {
1682 <              RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1677 >            ah = momentumTarget_ / Mh + vh;
1678 >            ahrec = momentumTarget_ / Mh;
1679 >            
1680 >            // We now need the inverse of the inertia tensor to
1681 >            // calculate the angular velocity of the hot slab;
1682 >            Mat3x3d Ihi = Ih.inverse();
1683 >            Vector3d omegah = Ihi * Lh;
1684 >            bh  = (Ihi * angularMomentumTarget_) + omegah;
1685 >            bhrec = bh - omegah;
1686 >            
1687 >            RealType hNumerator = Kh + kineticTarget_;
1688 >            if (doLinearPart)
1689 >              hNumerator -= 0.5 * Mh * ah.lengthSquare();
1690 >            
1691 >            if (doAngularPart)
1692 >              hNumerator -= 0.5 * ( dot(bh, Ih * bh));
1693 >              
1694 >            if (hNumerator > 0.0) {
1695 >              
1696 >              RealType hDenominator = Kh;
1697 >              if (doLinearPart)
1698 >                hDenominator -= 0.5 * Mh * vh.lengthSquare();
1699 >              if (doAngularPart)
1700 >                hDenominator -= 0.5*(dot(omegah, Ih * omegah));
1701 >              
1702                if (hDenominator > 0.0) {
1703                  RealType h = sqrt(hNumerator / hDenominator);
1704                  if ((h > 0.9) && (h < 1.1)) {
1705 <                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1346 <                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1705 >                  
1706                    vector<StuntDouble*>::iterator sdi;
1707                    Vector3d vel;
1708 <                  for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1708 >                  Vector3d rPos;
1709 >                  
1710 >                  for (sdi = coldBin.begin(); sdi != coldBin.end(); ++sdi) {
1711                      //vel = (*sdi)->getVel();
1712 <                    vel = ((*sdi)->getVel() - vc) * c + ac;
1712 >                    rPos = (*sdi)->getPos() - coordinateOrigin_;
1713 >                    if (doLinearPart)
1714 >                      vel = ((*sdi)->getVel() - vc) * c + ac;
1715 >                    if (doAngularPart)
1716 >                      vel = ((*sdi)->getVel() - cross(omegac, rPos)) * c + cross(bc, rPos);
1717 >
1718                      (*sdi)->setVel(vel);
1719                      if (rnemdFluxType_ == rnemdFullKE) {
1720                        if ((*sdi)->isDirectional()) {
# Line 1357 | Line 1723 | namespace OpenMD {
1723                        }
1724                      }
1725                    }
1726 <                  for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1726 >                  for (sdi = hotBin.begin(); sdi != hotBin.end(); ++sdi) {
1727                      //vel = (*sdi)->getVel();
1728 <                    vel = ((*sdi)->getVel() - vh) * h + ah;
1728 >                    rPos = (*sdi)->getPos() - coordinateOrigin_;
1729 >                    if (doLinearPart)
1730 >                      vel = ((*sdi)->getVel() - vh) * h + ah;    
1731 >                    if (doAngularPart)
1732 >                      vel = ((*sdi)->getVel() - cross(omegah, rPos)) * h + cross(bh, rPos);    
1733 >
1734                      (*sdi)->setVel(vel);
1735                      if (rnemdFluxType_ == rnemdFullKE) {
1736                        if ((*sdi)->isDirectional()) {
# Line 1371 | Line 1742 | namespace OpenMD {
1742                    successfulExchange = true;
1743                    kineticExchange_ += kineticTarget_;
1744                    momentumExchange_ += momentumTarget_;
1745 +                  angularMomentumExchange_ += angularMomentumTarget_;
1746                  }
1747                }
1748              }
# Line 1390 | Line 1762 | namespace OpenMD {
1762      }
1763    }
1764  
1765 <  void RNEMD::doRNEMD() {
1765 >  RealType RNEMD::getDividingArea() {
1766  
1767 +    if (hasDividingArea_) return dividingArea_;
1768 +
1769 +    RealType areaA, areaB;
1770 +    Snapshot* snap = info_->getSnapshotManager()->getCurrentSnapshot();
1771 +
1772 +    if (hasSelectionA_) {
1773 +
1774 +      if (evaluatorA_.hasSurfaceArea())
1775 +        areaA = evaluatorA_.getSurfaceArea();
1776 +      else {
1777 +        
1778 +        int isd;
1779 +        StuntDouble* sd;
1780 +        vector<StuntDouble*> aSites;
1781 +        seleManA_.setSelectionSet(evaluatorA_.evaluate());
1782 +        for (sd = seleManA_.beginSelected(isd); sd != NULL;
1783 +             sd = seleManA_.nextSelected(isd)) {
1784 +          aSites.push_back(sd);
1785 +        }
1786 + #if defined(HAVE_QHULL)
1787 +        ConvexHull* surfaceMeshA = new ConvexHull();
1788 +        surfaceMeshA->computeHull(aSites);
1789 +        areaA = surfaceMeshA->getArea();
1790 +        delete surfaceMeshA;
1791 + #else
1792 +        sprintf( painCave.errMsg,
1793 +               "RNEMD::getDividingArea : Hull calculation is not possible\n"
1794 +                 "\twithout libqhull. Please rebuild OpenMD with qhull enabled.");
1795 +        painCave.severity = OPENMD_ERROR;
1796 +        painCave.isFatal = 1;
1797 +        simError();
1798 + #endif
1799 +      }
1800 +
1801 +    } else {
1802 +      if (usePeriodicBoundaryConditions_) {
1803 +        // in periodic boundaries, the surface area is twice the x-y
1804 +        // area of the current box:
1805 +        areaA = 2.0 * snap->getXYarea();
1806 +      } else {
1807 +        // in non-periodic simulations, without explicitly setting
1808 +        // selections, the sphere radius sets the surface area of the
1809 +        // dividing surface:
1810 +        areaA = 4.0 * M_PI * pow(sphereARadius_, 2);
1811 +      }
1812 +    }
1813 +
1814 +    if (hasSelectionB_) {
1815 +      if (evaluatorB_.hasSurfaceArea()) {
1816 +        areaB = evaluatorB_.getSurfaceArea();
1817 +      } else {
1818 +
1819 +        int isd;
1820 +        StuntDouble* sd;
1821 +        vector<StuntDouble*> bSites;
1822 +        seleManB_.setSelectionSet(evaluatorB_.evaluate());
1823 +        for (sd = seleManB_.beginSelected(isd); sd != NULL;
1824 +             sd = seleManB_.nextSelected(isd)) {
1825 +          bSites.push_back(sd);
1826 +        }
1827 +        
1828 + #if defined(HAVE_QHULL)
1829 +        ConvexHull* surfaceMeshB = new ConvexHull();    
1830 +        surfaceMeshB->computeHull(bSites);
1831 +        areaB = surfaceMeshB->getArea();
1832 +        delete surfaceMeshB;
1833 + #else
1834 +        sprintf( painCave.errMsg,
1835 +                 "RNEMD::getDividingArea : Hull calculation is not possible\n"
1836 +                 "\twithout libqhull. Please rebuild OpenMD with qhull enabled.");
1837 +        painCave.severity = OPENMD_ERROR;
1838 +        painCave.isFatal = 1;
1839 +        simError();
1840 + #endif
1841 +      }
1842 +      
1843 +    } else {
1844 +      if (usePeriodicBoundaryConditions_) {
1845 +        // in periodic boundaries, the surface area is twice the x-y
1846 +        // area of the current box:
1847 +        areaB = 2.0 * snap->getXYarea();
1848 +      } else {
1849 +        // in non-periodic simulations, without explicitly setting
1850 +        // selections, but if a sphereBradius has been set, just use that:
1851 +        areaB = 4.0 * M_PI * pow(sphereBRadius_, 2);
1852 +      }
1853 +    }
1854 +      
1855 +    dividingArea_ = min(areaA, areaB);
1856 +    hasDividingArea_ = true;
1857 +    return dividingArea_;
1858 +  }
1859 +  
1860 +  void RNEMD::doRNEMD() {
1861 +    if (!doRNEMD_) return;
1862      trialCount_++;
1863 +
1864 +    // object evaluator:
1865 +    evaluator_.loadScriptString(rnemdObjectSelection_);
1866 +    seleMan_.setSelectionSet(evaluator_.evaluate());
1867 +
1868 +    evaluatorA_.loadScriptString(selectionA_);
1869 +    evaluatorB_.loadScriptString(selectionB_);
1870 +
1871 +    seleManA_.setSelectionSet(evaluatorA_.evaluate());
1872 +    seleManB_.setSelectionSet(evaluatorB_.evaluate());
1873 +
1874 +    commonA_ = seleManA_ & seleMan_;
1875 +    commonB_ = seleManB_ & seleMan_;
1876 +
1877 +    // Target exchange quantities (in each exchange) = dividingArea * dt * flux
1878 +    // dt = exchange time interval
1879 +    // flux = target flux
1880 +    // dividingArea = smallest dividing surface between the two regions
1881 +
1882 +    hasDividingArea_ = false;
1883 +    RealType area = getDividingArea();
1884 +
1885 +    kineticTarget_ = kineticFlux_ * exchangeTime_ * area;
1886 +    momentumTarget_ = momentumFluxVector_ * exchangeTime_ * area;
1887 +    angularMomentumTarget_ = angularMomentumFluxVector_ * exchangeTime_ * area;
1888 +
1889      switch(rnemdMethod_) {
1890      case rnemdSwap:
1891 <      doSwap();
1891 >      doSwap(commonA_, commonB_);
1892        break;
1893      case rnemdNIVS:
1894 <      doNIVS();
1894 >      doNIVS(commonA_, commonB_);
1895        break;
1896      case rnemdVSS:
1897 <      doVSS();
1897 >      doVSS(commonA_, commonB_);
1898        break;
1899      case rnemdUnkownMethod:
1900      default :
# Line 1410 | Line 1903 | namespace OpenMD {
1903    }
1904  
1905    void RNEMD::collectData() {
1906 <
1906 >    if (!doRNEMD_) return;
1907      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1908 +
1909 +    // collectData can be called more frequently than the doRNEMD, so use the
1910 +    // computed area from the last exchange time:
1911 +    RealType area = getDividingArea();
1912 +    areaAccumulator_->add(area);
1913      Mat3x3d hmat = currentSnap_->getHmat();
1914 +    Vector3d u = angularMomentumFluxVector_;
1915 +    u.normalize();
1916  
1917      seleMan_.setSelectionSet(evaluator_.evaluate());
1918  
1919 <    int selei;
1919 >    int selei(0);
1920      StuntDouble* sd;
1921 <    int idx;
1921 >    int binNo;
1922 >    RealType mass;
1923 >    Vector3d vel;
1924 >    Vector3d rPos;
1925 >    RealType KE;
1926 >    Vector3d L;
1927 >    Mat3x3d I;
1928 >    RealType r2;
1929  
1930      vector<RealType> binMass(nBins_, 0.0);
1931 <    vector<RealType> binPx(nBins_, 0.0);
1932 <    vector<RealType> binPy(nBins_, 0.0);
1933 <    vector<RealType> binPz(nBins_, 0.0);
1931 >    vector<Vector3d> binP(nBins_, V3Zero);
1932 >    vector<RealType> binOmega(nBins_, 0.0);
1933 >    vector<Vector3d> binL(nBins_, V3Zero);
1934 >    vector<Mat3x3d>  binI(nBins_);
1935      vector<RealType> binKE(nBins_, 0.0);
1936      vector<int> binDOF(nBins_, 0);
1937      vector<int> binCount(nBins_, 0);
# Line 1431 | Line 1939 | namespace OpenMD {
1939      // alternative approach, track all molecules instead of only those
1940      // selected for scaling/swapping:
1941      /*
1942 <    SimInfo::MoleculeIterator miter;
1943 <    vector<StuntDouble*>::iterator iiter;
1944 <    Molecule* mol;
1945 <    StuntDouble* sd;
1946 <    for (mol = info_->beginMolecule(miter); mol != NULL;
1942 >      SimInfo::MoleculeIterator miter;
1943 >      vector<StuntDouble*>::iterator iiter;
1944 >      Molecule* mol;
1945 >      StuntDouble* sd;
1946 >      for (mol = info_->beginMolecule(miter); mol != NULL;
1947        mol = info_->nextMolecule(miter))
1948        sd is essentially sd
1949 <        for (sd = mol->beginIntegrableObject(iiter);
1950 <             sd != NULL;
1951 <             sd = mol->nextIntegrableObject(iiter))
1949 >      for (sd = mol->beginIntegrableObject(iiter);
1950 >      sd != NULL;
1951 >      sd = mol->nextIntegrableObject(iiter))
1952      */
1953 +
1954      for (sd = seleMan_.beginSelected(selei); sd != NULL;
1955 <         sd = seleMan_.nextSelected(selei)) {
1956 <      
1448 <      idx = sd->getLocalIndex();
1449 <      
1955 >         sd = seleMan_.nextSelected(selei)) {    
1956 >    
1957        Vector3d pos = sd->getPos();
1958  
1959        // wrap the stuntdouble's position back into the box:
1960        
1961 <      if (usePeriodicBoundaryConditions_)
1961 >      if (usePeriodicBoundaryConditions_) {
1962          currentSnap_->wrapVector(pos);
1963 +        // which bin is this stuntdouble in?
1964 +        // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1965 +        // Shift molecules by half a box to have bins start at 0
1966 +        // The modulo operator is used to wrap the case when we are
1967 +        // beyond the end of the bins back to the beginning.
1968 +        binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1969 +      } else {
1970 +        Vector3d rPos = pos - coordinateOrigin_;
1971 +        binNo = int(rPos.length() / binWidth_);
1972 +      }
1973  
1974 <      // which bin is this stuntdouble in?
1975 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1976 <      // Shift molecules by half a box to have bins start at 0
1977 <      // The modulo operator is used to wrap the case when we are
1978 <      // beyond the end of the bins back to the beginning.
1979 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1980 <    
1981 <      RealType mass = sd->getMass();
1982 <      Vector3d vel = sd->getVel();
1974 >      mass = sd->getMass();
1975 >      vel = sd->getVel();
1976 >      rPos = sd->getPos() - coordinateOrigin_;
1977 >      KE = 0.5 * mass * vel.lengthSquare();
1978 >      L = mass * cross(rPos, vel);
1979 >      I = outProduct(rPos, rPos) * mass;
1980 >      r2 = rPos.lengthSquare();
1981 >      I(0, 0) += mass * r2;
1982 >      I(1, 1) += mass * r2;
1983 >      I(2, 2) += mass * r2;
1984  
1985 <      binCount[binNo]++;
1986 <      binMass[binNo] += mass;
1987 <      binPx[binNo] += mass*vel.x();
1988 <      binPy[binNo] += mass*vel.y();
1989 <      binPz[binNo] += mass*vel.z();
1990 <      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1991 <      binDOF[binNo] += 3;
1985 >      // Project the relative position onto a plane perpendicular to
1986 >      // the angularMomentumFluxVector:
1987 >      // Vector3d rProj = rPos - dot(rPos, u) * u;
1988 >      // Project the velocity onto a plane perpendicular to the
1989 >      // angularMomentumFluxVector:
1990 >      // Vector3d vProj = vel  - dot(vel, u) * u;
1991 >      // Compute angular velocity vector (should be nearly parallel to
1992 >      // angularMomentumFluxVector
1993 >      // Vector3d aVel = cross(rProj, vProj);
1994  
1995 <      if (sd->isDirectional()) {
1996 <        Vector3d angMom = sd->getJ();
1997 <        Mat3x3d I = sd->getI();
1998 <        if (sd->isLinear()) {
1999 <          int i = sd->linearAxis();
2000 <          int j = (i + 1) % 3;
2001 <          int k = (i + 2) % 3;
2002 <          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
2003 <                                 angMom[k] * angMom[k] / I(k, k));
2004 <          binDOF[binNo] += 2;
2005 <        } else {
2006 <          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
2007 <                                 angMom[1] * angMom[1] / I(1, 1) +
2008 <                                 angMom[2] * angMom[2] / I(2, 2));
2009 <          binDOF[binNo] += 3;
1995 >      if (binNo >= 0 && binNo < nBins_)  {
1996 >        binCount[binNo]++;
1997 >        binMass[binNo] += mass;
1998 >        binP[binNo] += mass*vel;
1999 >        binKE[binNo] += KE;
2000 >        binI[binNo] += I;
2001 >        binL[binNo] += L;
2002 >        binDOF[binNo] += 3;
2003 >        
2004 >        if (sd->isDirectional()) {
2005 >          Vector3d angMom = sd->getJ();
2006 >          Mat3x3d Ia = sd->getI();
2007 >          if (sd->isLinear()) {
2008 >            int i = sd->linearAxis();
2009 >            int j = (i + 1) % 3;
2010 >            int k = (i + 2) % 3;
2011 >            binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / Ia(j, j) +
2012 >                                   angMom[k] * angMom[k] / Ia(k, k));
2013 >            binDOF[binNo] += 2;
2014 >          } else {
2015 >            binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / Ia(0, 0) +
2016 >                                   angMom[1] * angMom[1] / Ia(1, 1) +
2017 >                                   angMom[2] * angMom[2] / Ia(2, 2));
2018 >            binDOF[binNo] += 3;
2019 >          }
2020          }
2021        }
2022      }
1493    
2023  
2024   #ifdef IS_MPI
2025 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
2026 <                              nBins_, MPI::INT, MPI::SUM);
2027 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
2028 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2029 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
2030 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2031 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
2032 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2033 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
2034 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2035 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
2036 <                              nBins_, MPI::REALTYPE, MPI::SUM);
2037 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
2038 <                              nBins_, MPI::INT, MPI::SUM);
2025 >
2026 >    for (int i = 0; i < nBins_; i++) {
2027 >      
2028 >      MPI_Allreduce(MPI_IN_PLACE, &binCount[i],
2029 >                    1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
2030 >      MPI_Allreduce(MPI_IN_PLACE, &binMass[i],
2031 >                    1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2032 >      MPI_Allreduce(MPI_IN_PLACE, binP[i].getArrayPointer(),
2033 >                    3, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2034 >      MPI_Allreduce(MPI_IN_PLACE, binL[i].getArrayPointer(),
2035 >                    3, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2036 >      MPI_Allreduce(MPI_IN_PLACE, binI[i].getArrayPointer(),
2037 >                    9, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2038 >      MPI_Allreduce(MPI_IN_PLACE, &binKE[i],
2039 >                    1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2040 >      MPI_Allreduce(MPI_IN_PLACE, &binDOF[i],
2041 >                    1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
2042 >      //MPI_Allreduce(MPI_IN_PLACE, &binOmega[i],
2043 >      //                          1, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD);
2044 >    }
2045 >    
2046   #endif
2047  
2048 <    Vector3d vel;
2048 >    Vector3d omega;
2049      RealType den;
2050      RealType temp;
2051      RealType z;
2052 +    RealType r;
2053      for (int i = 0; i < nBins_; i++) {
2054 <      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
2055 <      vel.x() = binPx[i] / binMass[i];
2056 <      vel.y() = binPy[i] / binMass[i];
2057 <      vel.z() = binPz[i] / binMass[i];
2058 <      den = binCount[i] * nBins_ / (hmat(0,0) * hmat(1,1) * hmat(2,2));
2059 <      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
2060 <                               PhysicalConstants::energyConvert);
2054 >      if (usePeriodicBoundaryConditions_) {
2055 >        z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
2056 >        den = binMass[i] * nBins_ * PhysicalConstants::densityConvert
2057 >          / currentSnap_->getVolume() ;
2058 >      } else {
2059 >        r = (((RealType)i + 0.5) * binWidth_);
2060 >        RealType rinner = (RealType)i * binWidth_;
2061 >        RealType router = (RealType)(i+1) * binWidth_;
2062 >        den = binMass[i] * 3.0 * PhysicalConstants::densityConvert
2063 >          / (4.0 * M_PI * (pow(router,3) - pow(rinner,3)));
2064 >      }
2065 >      vel = binP[i] / binMass[i];
2066  
2067 <      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
2068 <        if(outputMask_[j]) {
2069 <          switch(j) {
2070 <          case Z:
2071 <            (data_[j].accumulator[i])->add(z);
2072 <            break;
2073 <          case TEMPERATURE:
2074 <            data_[j].accumulator[i]->add(temp);
2075 <            break;
2076 <          case VELOCITY:
2077 <            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
2078 <            break;
2079 <          case DENSITY:
2080 <            data_[j].accumulator[i]->add(den);
2081 <            break;
2067 >      omega = binI[i].inverse() * binL[i];
2068 >
2069 >      // omega = binOmega[i] / binCount[i];
2070 >
2071 >      if (binCount[i] > 0) {
2072 >        // only add values if there are things to add
2073 >        temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
2074 >                                 PhysicalConstants::energyConvert);
2075 >        
2076 >        for (unsigned int j = 0; j < outputMask_.size(); ++j) {
2077 >          if(outputMask_[j]) {
2078 >            switch(j) {
2079 >            case Z:
2080 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(z);
2081 >              break;
2082 >            case R:
2083 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(r);
2084 >              break;
2085 >            case TEMPERATURE:
2086 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(temp);
2087 >              break;
2088 >            case VELOCITY:
2089 >              dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
2090 >              break;
2091 >            case ANGULARVELOCITY:  
2092 >              dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(omega);
2093 >              break;
2094 >            case DENSITY:
2095 >              dynamic_cast<Accumulator *>(data_[j].accumulator[i])->add(den);
2096 >              break;
2097 >            }
2098            }
2099          }
2100        }
2101      }
2102 +    hasData_ = true;
2103    }
2104  
2105    void RNEMD::getStarted() {
2106 +    if (!doRNEMD_) return;
2107 +    hasDividingArea_ = false;
2108      collectData();
2109      writeOutputFile();
2110    }
2111  
2112    void RNEMD::parseOutputFileFormat(const std::string& format) {
2113 +    if (!doRNEMD_) return;
2114      StringTokenizer tokenizer(format, " ,;|\t\n\r");
2115      
2116      while(tokenizer.hasMoreTokens()) {
# Line 1565 | Line 2127 | namespace OpenMD {
2127          painCave.severity = OPENMD_ERROR;
2128          simError();            
2129        }
2130 <    }  
2130 >    }
2131    }
2132    
2133    void RNEMD::writeOutputFile() {
2134 +    if (!doRNEMD_) return;
2135 +    if (!hasData_) return;
2136      
2137   #ifdef IS_MPI
2138      // If we're the root node, should we print out the results
2139 <    int worldRank = MPI::COMM_WORLD.Get_rank();
2139 >    int worldRank;
2140 >    MPI_Comm_rank( MPI_COMM_WORLD, &worldRank);
2141 >
2142      if (worldRank == 0) {
2143   #endif
2144        rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
# Line 1588 | Line 2154 | namespace OpenMD {
2154        Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
2155  
2156        RealType time = currentSnap_->getTime();
2157 <      
2158 <      
2157 >      RealType avgArea;
2158 >      areaAccumulator_->getAverage(avgArea);
2159 >
2160 >      RealType Jz(0.0);
2161 >      Vector3d JzP(V3Zero);
2162 >      Vector3d JzL(V3Zero);
2163 >      if (time >= info_->getSimParams()->getDt()) {
2164 >        Jz = kineticExchange_ / (time * avgArea)
2165 >          / PhysicalConstants::energyConvert;
2166 >        JzP = momentumExchange_ / (time * avgArea);
2167 >        JzL = angularMomentumExchange_ / (time * avgArea);
2168 >      }
2169 >
2170        rnemdFile_ << "#######################################################\n";
2171        rnemdFile_ << "# RNEMD {\n";
2172  
2173        map<string, RNEMDMethod>::iterator mi;
2174        for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
2175          if ( (*mi).second == rnemdMethod_)
2176 <          rnemdFile_ << "#    exchangeMethod  = " << (*mi).first << "\n";
2176 >          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
2177        }
2178        map<string, RNEMDFluxType>::iterator fi;
2179        for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
2180          if ( (*fi).second == rnemdFluxType_)
2181 <          rnemdFile_ << "#    fluxType  = " << (*fi).first << "\n";
2181 >          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
2182        }
2183        
2184 <      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << " fs\n";
2184 >      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
2185  
2186        rnemdFile_ << "#    objectSelection = \""
2187 <                 << rnemdObjectSelection_ << "\"\n";
2188 <      rnemdFile_ << "#    slabWidth = " << slabWidth_ << " angstroms\n";
2189 <      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << " angstroms\n";
1613 <      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << " angstroms\n";
2187 >                 << rnemdObjectSelection_ << "\";\n";
2188 >      rnemdFile_ << "#    selectionA = \"" << selectionA_ << "\";\n";
2189 >      rnemdFile_ << "#    selectionB = \"" << selectionB_ << "\";\n";
2190        rnemdFile_ << "# }\n";
2191        rnemdFile_ << "#######################################################\n";
2192 <      
2193 <      rnemdFile_ << "# running time = " << time << " fs\n";
2194 <      rnemdFile_ << "# target kinetic flux = " << kineticFlux_ << "\n";
2195 <      rnemdFile_ << "# target momentum flux = " << momentumFluxVector_ << "\n";
2196 <      
2197 <      rnemdFile_ << "# target one-time kinetic exchange = " << kineticTarget_
2198 <                 << "\n";
2199 <      rnemdFile_ << "# target one-time momentum exchange = " << momentumTarget_
2200 <                 << "\n";
2201 <      
2202 <      rnemdFile_ << "# actual kinetic exchange = " << kineticExchange_ << "\n";
2203 <      rnemdFile_ << "# actual momentum exchange = " << momentumExchange_
2204 <                 << "\n";
2205 <      
2206 <      rnemdFile_ << "# attempted exchanges: " << trialCount_ << "\n";
2207 <      rnemdFile_ << "# failed exchanges: " << failTrialCount_ << "\n";
2208 <
2209 <      
2192 >      rnemdFile_ << "# RNEMD report:\n";      
2193 >      rnemdFile_ << "#      running time = " << time << " fs\n";
2194 >      rnemdFile_ << "# Target flux:\n";
2195 >      rnemdFile_ << "#           kinetic = "
2196 >                 << kineticFlux_ / PhysicalConstants::energyConvert
2197 >                 << " (kcal/mol/A^2/fs)\n";
2198 >      rnemdFile_ << "#          momentum = " << momentumFluxVector_
2199 >                 << " (amu/A/fs^2)\n";
2200 >      rnemdFile_ << "#  angular momentum = " << angularMomentumFluxVector_
2201 >                 << " (amu/A^2/fs^2)\n";
2202 >      rnemdFile_ << "# Target one-time exchanges:\n";
2203 >      rnemdFile_ << "#          kinetic = "
2204 >                 << kineticTarget_ / PhysicalConstants::energyConvert
2205 >                 << " (kcal/mol)\n";
2206 >      rnemdFile_ << "#          momentum = " << momentumTarget_
2207 >                 << " (amu*A/fs)\n";
2208 >      rnemdFile_ << "#  angular momentum = " << angularMomentumTarget_
2209 >                 << " (amu*A^2/fs)\n";
2210 >      rnemdFile_ << "# Actual exchange totals:\n";
2211 >      rnemdFile_ << "#          kinetic = "
2212 >                 << kineticExchange_ / PhysicalConstants::energyConvert
2213 >                 << " (kcal/mol)\n";
2214 >      rnemdFile_ << "#          momentum = " << momentumExchange_
2215 >                 << " (amu*A/fs)\n";      
2216 >      rnemdFile_ << "#  angular momentum = " << angularMomentumExchange_
2217 >                 << " (amu*A^2/fs)\n";      
2218 >      rnemdFile_ << "# Actual flux:\n";
2219 >      rnemdFile_ << "#          kinetic = " << Jz
2220 >                 << " (kcal/mol/A^2/fs)\n";
2221 >      rnemdFile_ << "#          momentum = " << JzP
2222 >                 << " (amu/A/fs^2)\n";
2223 >      rnemdFile_ << "#  angular momentum = " << JzL
2224 >                 << " (amu/A^2/fs^2)\n";
2225 >      rnemdFile_ << "# Exchange statistics:\n";
2226 >      rnemdFile_ << "#               attempted = " << trialCount_ << "\n";
2227 >      rnemdFile_ << "#                  failed = " << failTrialCount_ << "\n";
2228        if (rnemdMethod_ == rnemdNIVS) {
2229 <        rnemdFile_ << "# NIVS root-check warnings: " << failRootCount_ << "\n";
2229 >        rnemdFile_ << "#  NIVS root-check errors = "
2230 >                   << failRootCount_ << "\n";
2231        }
1637
2232        rnemdFile_ << "#######################################################\n";
2233        
2234        
# Line 1645 | Line 2239 | namespace OpenMD {
2239          if (outputMask_[i]) {
2240            rnemdFile_ << "\t" << data_[i].title <<
2241              "(" << data_[i].units << ")";
2242 +          // add some extra tabs for column alignment
2243 +          if (data_[i].dataType == "Vector3d") rnemdFile_ << "\t\t";
2244          }
2245        }
2246        rnemdFile_ << std::endl;
2247        
2248        rnemdFile_.precision(8);
2249        
2250 <      for (unsigned int j = 0; j < nBins_; j++) {        
2250 >      for (int j = 0; j < nBins_; j++) {        
2251          
2252          for (unsigned int i = 0; i < outputMask_.size(); ++i) {
2253            if (outputMask_[i]) {
2254              if (data_[i].dataType == "RealType")
2255                writeReal(i,j);
2256 <            else if (data_[i].dataType == "Vector3d")
2256 >            else if (data_[i].dataType == "Vector3d")
2257                writeVector(i,j);
2258              else {
2259                sprintf( painCave.errMsg,
# Line 1671 | Line 2267 | namespace OpenMD {
2267          rnemdFile_ << std::endl;
2268          
2269        }        
2270 +
2271 +      rnemdFile_ << "#######################################################\n";
2272 +      rnemdFile_ << "# 95% confidence intervals in those quantities follow:\n";
2273 +      rnemdFile_ << "#######################################################\n";
2274 +
2275 +
2276 +      for (int j = 0; j < nBins_; j++) {        
2277 +        rnemdFile_ << "#";
2278 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
2279 +          if (outputMask_[i]) {
2280 +            if (data_[i].dataType == "RealType")
2281 +              writeRealErrorBars(i,j);
2282 +            else if (data_[i].dataType == "Vector3d")
2283 +              writeVectorErrorBars(i,j);
2284 +            else {
2285 +              sprintf( painCave.errMsg,
2286 +                       "RNEMD found an unknown data type for: %s ",
2287 +                       data_[i].title.c_str());
2288 +              painCave.isFatal = 1;
2289 +              simError();
2290 +            }
2291 +          }
2292 +        }
2293 +        rnemdFile_ << std::endl;
2294 +        
2295 +      }        
2296        
2297        rnemdFile_.flush();
2298        rnemdFile_.close();
# Line 1682 | Line 2304 | namespace OpenMD {
2304    }
2305    
2306    void RNEMD::writeReal(int index, unsigned int bin) {
2307 +    if (!doRNEMD_) return;
2308      assert(index >=0 && index < ENDINDEX);
2309 <    assert(bin >=0 && bin < nBins_);
2309 >    assert(int(bin) < nBins_);
2310      RealType s;
2311 +    int count;
2312      
2313 <    data_[index].accumulator[bin]->getAverage(s);
2313 >    count = data_[index].accumulator[bin]->count();
2314 >    if (count == 0) return;
2315      
2316 +    dynamic_cast<Accumulator *>(data_[index].accumulator[bin])->getAverage(s);
2317 +    
2318      if (! isinf(s) && ! isnan(s)) {
2319        rnemdFile_ << "\t" << s;
2320      } else{
2321        sprintf( painCave.errMsg,
2322 <               "RNEMD detected a numerical error writing: %s for bin %d",
2322 >               "RNEMD detected a numerical error writing: %s for bin %u",
2323                 data_[index].title.c_str(), bin);
2324        painCave.isFatal = 1;
2325        simError();
# Line 1700 | Line 2327 | namespace OpenMD {
2327    }
2328    
2329    void RNEMD::writeVector(int index, unsigned int bin) {
2330 +    if (!doRNEMD_) return;
2331      assert(index >=0 && index < ENDINDEX);
2332 <    assert(bin >=0 && bin < nBins_);
2332 >    assert(int(bin) < nBins_);
2333      Vector3d s;
2334 +    int count;
2335 +    
2336 +    count = data_[index].accumulator[bin]->count();
2337 +
2338 +    if (count == 0) return;
2339 +
2340      dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
2341      if (isinf(s[0]) || isnan(s[0]) ||
2342          isinf(s[1]) || isnan(s[1]) ||
2343          isinf(s[2]) || isnan(s[2]) ) {      
2344        sprintf( painCave.errMsg,
2345 <               "RNEMD detected a numerical error writing: %s for bin %d",
2345 >               "RNEMD detected a numerical error writing: %s for bin %u",
2346                 data_[index].title.c_str(), bin);
2347        painCave.isFatal = 1;
2348        simError();
# Line 1716 | Line 2350 | namespace OpenMD {
2350        rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
2351      }
2352    }  
2353 +
2354 +  void RNEMD::writeRealErrorBars(int index, unsigned int bin) {
2355 +    if (!doRNEMD_) return;
2356 +    assert(index >=0 && index < ENDINDEX);
2357 +    assert(int(bin) < nBins_);
2358 +    RealType s;
2359 +    int count;
2360 +    
2361 +    count = data_[index].accumulator[bin]->count();
2362 +    if (count == 0) return;
2363 +    
2364 +    dynamic_cast<Accumulator *>(data_[index].accumulator[bin])->get95percentConfidenceInterval(s);
2365 +    
2366 +    if (! isinf(s) && ! isnan(s)) {
2367 +      rnemdFile_ << "\t" << s;
2368 +    } else{
2369 +      sprintf( painCave.errMsg,
2370 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %u",
2371 +               data_[index].title.c_str(), bin);
2372 +      painCave.isFatal = 1;
2373 +      simError();
2374 +    }    
2375 +  }
2376 +  
2377 +  void RNEMD::writeVectorErrorBars(int index, unsigned int bin) {
2378 +    if (!doRNEMD_) return;
2379 +    assert(index >=0 && index < ENDINDEX);
2380 +    assert(int(bin) < nBins_);
2381 +    Vector3d s;
2382 +    int count;
2383 +    
2384 +    count = data_[index].accumulator[bin]->count();
2385 +    if (count == 0) return;
2386 +
2387 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->get95percentConfidenceInterval(s);
2388 +    if (isinf(s[0]) || isnan(s[0]) ||
2389 +        isinf(s[1]) || isnan(s[1]) ||
2390 +        isinf(s[2]) || isnan(s[2]) ) {      
2391 +      sprintf( painCave.errMsg,
2392 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %u",
2393 +               data_[index].title.c_str(), bin);
2394 +      painCave.isFatal = 1;
2395 +      simError();
2396 +    } else {
2397 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
2398 +    }
2399 +  }  
2400   }
2401  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines