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

Comparing:
trunk/src/integrators/RNEMD.cpp (file contents), Revision 1350 by gezelter, Thu May 21 18:56:45 2009 UTC vs.
trunk/src/rnemd/RNEMD.cpp (file contents), Revision 1789 by gezelter, Wed Aug 29 20:52:19 2012 UTC

# Line 6 | Line 6
6   * redistribute this software in source and binary code form, provided
7   * that the following conditions are met:
8   *
9 < * 1. Acknowledgement of the program authors must be made in any
10 < *    publication of scientific results based in part on use of the
11 < *    program.  An acceptable form of acknowledgement is citation of
12 < *    the article in which the program was described (Matthew
13 < *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 < *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 < *    Parallel Simulation Engine for Molecular Dynamics,"
16 < *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 < *
18 < * 2. Redistributions of source code must retain the above copyright
9 > * 1. Redistributions of source code must retain the above copyright
10   *    notice, this list of conditions and the following disclaimer.
11   *
12 < * 3. Redistributions in binary form must reproduce the above copyright
12 > * 2. Redistributions in binary form must reproduce the above copyright
13   *    notice, this list of conditions and the following disclaimer in the
14   *    documentation and/or other materials provided with the
15   *    distribution.
# Line 37 | Line 28
28   * arising out of the use of or inability to use software, even if the
29   * University of Notre Dame has been advised of the possibility of
30   * such damages.
31 + *
32 + * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your
33 + * research, please cite the appropriate papers when you publish your
34 + * work.  Good starting points are:
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).          
39 + * [4]  Vardeman & Gezelter, in progress (2009).                        
40   */
41  
42 < #include "integrators/RNEMD.hpp"
42 > #include <cmath>
43 > #include "rnemd/RNEMD.hpp"
44   #include "math/Vector3.hpp"
45 + #include "math/Vector.hpp"
46   #include "math/SquareMatrix3.hpp"
47 + #include "math/Polynomial.hpp"
48   #include "primitives/Molecule.hpp"
49   #include "primitives/StuntDouble.hpp"
50 < #include "utils/OOPSEConstant.hpp"
50 > #include "utils/PhysicalConstants.hpp"
51   #include "utils/Tuple.hpp"
52 + #ifdef IS_MPI
53 + #include <mpi.h>
54 + #endif
55  
56 < #ifndef IS_MPI
57 < #include "math/SeqRandNumGen.hpp"
58 < #else
53 < #include "math/ParallelRandNumGen.hpp"
56 > #ifdef _MSC_VER
57 > #define isnan(x) _isnan((x))
58 > #define isinf(x) (!_finite(x) && !_isnan(x))
59   #endif
60  
61   #define HONKING_LARGE_VALUE 1.0e10
62  
63 < namespace oopse {
63 > using namespace std;
64 > namespace OpenMD {
65    
66 <  RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info), usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
67 <    
68 <    int seedValue;
66 >  RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info),
67 >                                usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) {
68 >
69 >    trialCount_ = 0;
70 >    failTrialCount_ = 0;
71 >    failRootCount_ = 0;
72 >
73      Globals * simParams = info->getSimParams();
74 +    RNEMDParameters* rnemdParams = simParams->getRNEMDParameters();
75  
76 <    stringToEnumMap_["Kinetic"] = rnemdKinetic;
77 <    stringToEnumMap_["Px"] = rnemdPx;
67 <    stringToEnumMap_["Py"] = rnemdPy;
68 <    stringToEnumMap_["Pz"] = rnemdPz;
69 <    stringToEnumMap_["Unknown"] = rnemdUnknown;
76 >    doRNEMD_ = rnemdParams->getUseRNEMD();
77 >    if (!doRNEMD_) return;
78  
79 <    rnemdObjectSelection_ = simParams->getRNEMD_objectSelection();
79 >    stringToMethod_["Swap"]  = rnemdSwap;
80 >    stringToMethod_["NIVS"]  = rnemdNIVS;
81 >    stringToMethod_["VSS"]   = rnemdVSS;
82 >
83 >    stringToFluxType_["KE"]  = rnemdKE;
84 >    stringToFluxType_["Px"]  = rnemdPx;
85 >    stringToFluxType_["Py"]  = rnemdPy;
86 >    stringToFluxType_["Pz"]  = rnemdPz;
87 >    stringToFluxType_["Pvector"]  = rnemdPvector;
88 >    stringToFluxType_["KE+Px"]  = rnemdKePx;
89 >    stringToFluxType_["KE+Py"]  = rnemdKePy;
90 >    stringToFluxType_["KE+Pvector"]  = rnemdKePvector;
91 >
92 >    runTime_ = simParams->getRunTime();
93 >    statusTime_ = simParams->getStatusTime();
94 >
95 >    rnemdObjectSelection_ = rnemdParams->getObjectSelection();
96      evaluator_.loadScriptString(rnemdObjectSelection_);
97      seleMan_.setSelectionSet(evaluator_.evaluate());
98  
99 +    const string methStr = rnemdParams->getMethod();
100 +    bool hasFluxType = rnemdParams->haveFluxType();
101  
102 +    string fluxStr;
103 +    if (hasFluxType) {
104 +      fluxStr = rnemdParams->getFluxType();
105 +    } else {
106 +      sprintf(painCave.errMsg,
107 +              "RNEMD: No fluxType was set in the md file.  This parameter,\n"
108 +              "\twhich must be one of the following values:\n"
109 +              "\tKE, Px, Py, Pz, Pvector, KE+Px, KE+Py, KE+Pvector\n"
110 +              "\tmust be set to use RNEMD\n");
111 +      painCave.isFatal = 1;
112 +      painCave.severity = OPENMD_ERROR;
113 +      simError();
114 +    }
115 +
116 +    bool hasKineticFlux = rnemdParams->haveKineticFlux();
117 +    bool hasMomentumFlux = rnemdParams->haveMomentumFlux();
118 +    bool hasMomentumFluxVector = rnemdParams->haveMomentumFluxVector();
119 +    bool hasSlabWidth = rnemdParams->haveSlabWidth();
120 +    bool hasSlabACenter = rnemdParams->haveSlabACenter();
121 +    bool hasSlabBCenter = rnemdParams->haveSlabBCenter();
122 +    bool hasOutputFileName = rnemdParams->haveOutputFileName();
123 +    bool hasOutputFields = rnemdParams->haveOutputFields();
124 +    
125 +    map<string, RNEMDMethod>::iterator i;
126 +    i = stringToMethod_.find(methStr);
127 +    if (i != stringToMethod_.end())
128 +      rnemdMethod_ = i->second;
129 +    else {
130 +      sprintf(painCave.errMsg,
131 +              "RNEMD: The current method,\n"
132 +              "\t\t%s is not one of the recognized\n"
133 +              "\texchange methods: Swap, NIVS, or VSS\n",
134 +              methStr.c_str());
135 +      painCave.isFatal = 1;
136 +      painCave.severity = OPENMD_ERROR;
137 +      simError();
138 +    }
139 +
140 +    map<string, RNEMDFluxType>::iterator j;
141 +    j = stringToFluxType_.find(fluxStr);
142 +    if (j != stringToFluxType_.end())
143 +      rnemdFluxType_ = j->second;
144 +    else {
145 +      sprintf(painCave.errMsg,
146 +              "RNEMD: The current fluxType,\n"
147 +              "\t\t%s\n"
148 +              "\tis not one of the recognized flux types.\n",
149 +              fluxStr.c_str());
150 +      painCave.isFatal = 1;
151 +      painCave.severity = OPENMD_ERROR;
152 +      simError();
153 +    }
154 +
155 +    bool methodFluxMismatch = false;
156 +    bool hasCorrectFlux = false;
157 +    switch(rnemdMethod_) {
158 +    case rnemdSwap:
159 +      switch (rnemdFluxType_) {
160 +      case rnemdKE:
161 +        hasCorrectFlux = hasKineticFlux;
162 +        break;
163 +      case rnemdPx:
164 +      case rnemdPy:
165 +      case rnemdPz:
166 +        hasCorrectFlux = hasMomentumFlux;
167 +        break;
168 +      default :
169 +        methodFluxMismatch = true;
170 +        break;
171 +      }
172 +      break;
173 +    case rnemdNIVS:
174 +      switch (rnemdFluxType_) {
175 +      case rnemdKE:
176 +      case rnemdRotKE:
177 +      case rnemdFullKE:
178 +        hasCorrectFlux = hasKineticFlux;
179 +        break;
180 +      case rnemdPx:
181 +      case rnemdPy:
182 +      case rnemdPz:
183 +        hasCorrectFlux = hasMomentumFlux;
184 +        break;
185 +      case rnemdKePx:
186 +      case rnemdKePy:
187 +        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
188 +        break;
189 +      default:
190 +        methodFluxMismatch = true;
191 +        break;
192 +      }
193 +      break;
194 +    case rnemdVSS:
195 +      switch (rnemdFluxType_) {
196 +      case rnemdKE:
197 +      case rnemdRotKE:
198 +      case rnemdFullKE:
199 +        hasCorrectFlux = hasKineticFlux;
200 +        break;
201 +      case rnemdPx:
202 +      case rnemdPy:
203 +      case rnemdPz:
204 +        hasCorrectFlux = hasMomentumFlux;
205 +        break;
206 +      case rnemdPvector:
207 +        hasCorrectFlux = hasMomentumFluxVector;
208 +        break;
209 +      case rnemdKePx:
210 +      case rnemdKePy:
211 +        hasCorrectFlux = hasMomentumFlux && hasKineticFlux;
212 +        break;
213 +      case rnemdKePvector:
214 +        hasCorrectFlux = hasMomentumFluxVector && hasKineticFlux;
215 +        break;
216 +      default:
217 +        methodFluxMismatch = true;
218 +        break;
219 +      }
220 +    default:
221 +      break;
222 +    }
223 +
224 +    if (methodFluxMismatch) {
225 +      sprintf(painCave.errMsg,
226 +              "RNEMD: The current method,\n"
227 +              "\t\t%s\n"
228 +              "\tcannot be used with the current flux type, %s\n",
229 +              methStr.c_str(), fluxStr.c_str());
230 +      painCave.isFatal = 1;
231 +      painCave.severity = OPENMD_ERROR;
232 +      simError();        
233 +    }
234 +    if (!hasCorrectFlux) {
235 +      sprintf(painCave.errMsg,
236 +              "RNEMD: The current method, %s, and flux type, %s,\n"
237 +              "\tdid not have the correct flux value specified. Options\n"
238 +              "\tinclude: kineticFlux, momentumFlux, and momentumFluxVector\n",
239 +              methStr.c_str(), fluxStr.c_str());
240 +      painCave.isFatal = 1;
241 +      painCave.severity = OPENMD_ERROR;
242 +      simError();        
243 +    }
244 +
245 +    if (hasKineticFlux) {
246 +      // convert the kcal / mol / Angstroms^2 / fs values in the md file
247 +      // into  amu / fs^3:
248 +      kineticFlux_ = rnemdParams->getKineticFlux()
249 +        * PhysicalConstants::energyConvert;
250 +    } else {
251 +      kineticFlux_ = 0.0;
252 +    }
253 +    if (hasMomentumFluxVector) {
254 +      momentumFluxVector_ = rnemdParams->getMomentumFluxVector();
255 +    } else {
256 +      momentumFluxVector_ = V3Zero;
257 +      if (hasMomentumFlux) {
258 +        RealType momentumFlux = rnemdParams->getMomentumFlux();
259 +        switch (rnemdFluxType_) {
260 +        case rnemdPx:
261 +          momentumFluxVector_.x() = momentumFlux;
262 +          break;
263 +        case rnemdPy:
264 +          momentumFluxVector_.y() = momentumFlux;
265 +          break;
266 +        case rnemdPz:
267 +          momentumFluxVector_.z() = momentumFlux;
268 +          break;
269 +        case rnemdKePx:
270 +          momentumFluxVector_.x() = momentumFlux;
271 +          break;
272 +        case rnemdKePy:
273 +          momentumFluxVector_.y() = momentumFlux;
274 +          break;
275 +        default:
276 +          break;
277 +        }
278 +      }    
279 +    }
280 +
281      // do some sanity checking
282  
283      int selectionCount = seleMan_.getSelectionCount();
# Line 80 | Line 285 | namespace oopse {
285  
286      if (selectionCount > nIntegrable) {
287        sprintf(painCave.errMsg,
288 <              "RNEMD warning: The current RNEMD_objectSelection,\n"
288 >              "RNEMD: The current objectSelection,\n"
289                "\t\t%s\n"
290                "\thas resulted in %d selected objects.  However,\n"
291                "\tthe total number of integrable objects in the system\n"
# Line 90 | Line 295 | namespace oopse {
295                rnemdObjectSelection_.c_str(),
296                selectionCount, nIntegrable);
297        painCave.isFatal = 0;
298 +      painCave.severity = OPENMD_WARNING;
299        simError();
300 +    }
301  
302 +    areaAccumulator_ = new Accumulator();
303 +
304 +    nBins_ = rnemdParams->getOutputBins();
305 +
306 +    data_.resize(RNEMD::ENDINDEX);
307 +    OutputData z;
308 +    z.units =  "Angstroms";
309 +    z.title =  "Z";
310 +    z.dataType = "RealType";
311 +    z.accumulator.reserve(nBins_);
312 +    for (int i = 0; i < nBins_; i++)
313 +      z.accumulator.push_back( new Accumulator() );
314 +    data_[Z] = z;
315 +    outputMap_["Z"] =  Z;
316 +
317 +    OutputData temperature;
318 +    temperature.units =  "K";
319 +    temperature.title =  "Temperature";
320 +    temperature.dataType = "RealType";
321 +    temperature.accumulator.reserve(nBins_);
322 +    for (int i = 0; i < nBins_; i++)
323 +      temperature.accumulator.push_back( new Accumulator() );
324 +    data_[TEMPERATURE] = temperature;
325 +    outputMap_["TEMPERATURE"] =  TEMPERATURE;
326 +
327 +    OutputData velocity;
328 +    velocity.units = "angstroms/fs";
329 +    velocity.title =  "Velocity";  
330 +    velocity.dataType = "Vector3d";
331 +    velocity.accumulator.reserve(nBins_);
332 +    for (int i = 0; i < nBins_; i++)
333 +      velocity.accumulator.push_back( new VectorAccumulator() );
334 +    data_[VELOCITY] = velocity;
335 +    outputMap_["VELOCITY"] = VELOCITY;
336 +
337 +    OutputData density;
338 +    density.units =  "g cm^-3";
339 +    density.title =  "Density";
340 +    density.dataType = "RealType";
341 +    density.accumulator.reserve(nBins_);
342 +    for (int i = 0; i < nBins_; i++)
343 +      density.accumulator.push_back( new Accumulator() );
344 +    data_[DENSITY] = density;
345 +    outputMap_["DENSITY"] =  DENSITY;
346 +
347 +    if (hasOutputFields) {
348 +      parseOutputFileFormat(rnemdParams->getOutputFields());
349 +    } else {
350 +      outputMask_.set(Z);
351 +      switch (rnemdFluxType_) {
352 +      case rnemdKE:
353 +      case rnemdRotKE:
354 +      case rnemdFullKE:
355 +        outputMask_.set(TEMPERATURE);
356 +        break;
357 +      case rnemdPx:
358 +      case rnemdPy:
359 +        outputMask_.set(VELOCITY);
360 +        break;
361 +      case rnemdPz:        
362 +      case rnemdPvector:
363 +        outputMask_.set(VELOCITY);
364 +        outputMask_.set(DENSITY);
365 +        break;
366 +      case rnemdKePx:
367 +      case rnemdKePy:
368 +        outputMask_.set(TEMPERATURE);
369 +        outputMask_.set(VELOCITY);
370 +        break;
371 +      case rnemdKePvector:
372 +        outputMask_.set(TEMPERATURE);
373 +        outputMask_.set(VELOCITY);
374 +        outputMask_.set(DENSITY);        
375 +        break;
376 +      default:
377 +        break;
378 +      }
379      }
380 <    
381 <    const std::string st = simParams->getRNEMD_swapType();
380 >      
381 >    if (hasOutputFileName) {
382 >      rnemdFileName_ = rnemdParams->getOutputFileName();
383 >    } else {
384 >      rnemdFileName_ = getPrefix(info->getFinalConfigFileName()) + ".rnemd";
385 >    }          
386  
387 <    std::map<std::string, RNEMDTypeEnum>::iterator i;
100 <    i = stringToEnumMap_.find(st);
101 <    rnemdType_  = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second;
387 >    exchangeTime_ = rnemdParams->getExchangeTime();
388  
389 <    set_RNEMD_swapTime(simParams->getRNEMD_swapTime());
390 <    set_RNEMD_nBins(simParams->getRNEMD_nBins());
391 <    exchangeSum_ = 0.0;
389 >    Snapshot* currentSnap_ = info->getSnapshotManager()->getCurrentSnapshot();
390 >    Mat3x3d hmat = currentSnap_->getHmat();
391 >  
392 >    // Target exchange quantities (in each exchange) =  2 Lx Ly dt flux
393 >    // Lx, Ly = box dimensions in x & y
394 >    // dt = exchange time interval
395 >    // flux = target flux
396  
397 < #ifndef IS_MPI
398 <    if (simParams->haveSeed()) {
399 <      seedValue = simParams->getSeed();
400 <      randNumGen_ = new SeqRandNumGen(seedValue);
401 <    }else {
402 <      randNumGen_ = new SeqRandNumGen();
403 <    }    
404 < #else
405 <    if (simParams->haveSeed()) {
406 <      seedValue = simParams->getSeed();
407 <      randNumGen_ = new ParallelRandNumGen(seedValue);
408 <    }else {
409 <      randNumGen_ = new ParallelRandNumGen();
120 <    }    
121 < #endif
122 <  }
397 >    RealType area = currentSnap_->getXYarea();
398 >    kineticTarget_ = 2.0 * kineticFlux_ * exchangeTime_ * area;
399 >    momentumTarget_ = 2.0 * momentumFluxVector_ * exchangeTime_ * area;
400 >
401 >    // total exchange sums are zeroed out at the beginning:
402 >
403 >    kineticExchange_ = 0.0;
404 >    momentumExchange_ = V3Zero;
405 >
406 >    if (hasSlabWidth)
407 >      slabWidth_ = rnemdParams->getSlabWidth();
408 >    else
409 >      slabWidth_ = hmat(2,2) / 10.0;
410    
411 +    if (hasSlabACenter)
412 +      slabACenter_ = rnemdParams->getSlabACenter();
413 +    else
414 +      slabACenter_ = 0.0;
415 +    
416 +    if (hasSlabBCenter)
417 +      slabBCenter_ = rnemdParams->getSlabBCenter();
418 +    else
419 +      slabBCenter_ = hmat(2,2) / 2.0;
420 +    
421 +  }
422 +  
423    RNEMD::~RNEMD() {
424 <    delete randNumGen_;
424 >    if (!doRNEMD_) return;
425 > #ifdef IS_MPI
426 >    if (worldRank == 0) {
427 > #endif
428 >
429 >      writeOutputFile();
430 >
431 >      rnemdFile_.close();
432 >      
433 > #ifdef IS_MPI
434 >    }
435 > #endif
436    }
437 +  
438 +  bool RNEMD::inSlabA(Vector3d pos) {
439 +    return (abs(pos.z() - slabACenter_) < 0.5*slabWidth_);
440 +  }
441 +  bool RNEMD::inSlabB(Vector3d pos) {
442 +    return (abs(pos.z() - slabBCenter_) < 0.5*slabWidth_);
443 +  }
444  
445    void RNEMD::doSwap() {
446 <    int midBin = nBins_ / 2;
130 <
446 >    if (!doRNEMD_) return;
447      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
448      Mat3x3d hmat = currentSnap_->getHmat();
449  
# Line 156 | Line 472 | namespace oopse {
472  
473        if (usePeriodicBoundaryConditions_)
474          currentSnap_->wrapVector(pos);
475 +      bool inA = inSlabA(pos);
476 +      bool inB = inSlabB(pos);
477  
478 <      // which bin is this stuntdouble in?
161 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
162 <
163 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
164 <
165 <
166 <      // if we're in bin 0 or the middleBin
167 <      if (binNo == 0 || binNo == midBin) {
478 >      if (inA || inB) {
479          
480          RealType mass = sd->getMass();
481          Vector3d vel = sd->getVel();
482          RealType value;
483 <
484 <        switch(rnemdType_) {
485 <        case rnemdKinetic :
483 >        
484 >        switch(rnemdFluxType_) {
485 >        case rnemdKE :
486            
487 <          value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
488 <                          vel[2]*vel[2]);
489 <          if (sd->isDirectional()) {
487 >          value = mass * vel.lengthSquare();
488 >          
489 >          if (sd->isDirectional()) {
490              Vector3d angMom = sd->getJ();
491              Mat3x3d I = sd->getI();
492              
493              if (sd->isLinear()) {
494 <              int i = sd->linearAxis();
495 <              int j = (i + 1) % 3;
496 <              int k = (i + 2) % 3;
497 <              value += angMom[j] * angMom[j] / I(j, j) +
498 <                angMom[k] * angMom[k] / I(k, k);
494 >              int i = sd->linearAxis();
495 >              int j = (i + 1) % 3;
496 >              int k = (i + 2) % 3;
497 >              value += angMom[j] * angMom[j] / I(j, j) +
498 >                angMom[k] * angMom[k] / I(k, k);
499              } else {                        
500 <              value += angMom[0]*angMom[0]/I(0, 0)
501 <                + angMom[1]*angMom[1]/I(1, 1)
502 <                + angMom[2]*angMom[2]/I(2, 2);
500 >              value += angMom[0]*angMom[0]/I(0, 0)
501 >                + angMom[1]*angMom[1]/I(1, 1)
502 >                + angMom[2]*angMom[2]/I(2, 2);
503              }
504 <          }
505 <          value = value * 0.5 / OOPSEConstant::energyConvert;
504 >          } //angular momenta exchange enabled
505 >          value *= 0.5;
506            break;
507          case rnemdPx :
508            value = mass * vel[0];
# Line 202 | Line 513 | namespace oopse {
513          case rnemdPz :
514            value = mass * vel[2];
515            break;
205        case rnemdUnknown :
516          default :
517            break;
518          }
519          
520 <        if (binNo == 0) {
520 >        if (inA == 0) {
521            if (!min_found) {
522              min_val = value;
523              min_sd = sd;
# Line 218 | Line 528 | namespace oopse {
528                min_sd = sd;
529              }
530            }
531 <        } else {
531 >        } else {
532            if (!max_found) {
533              max_val = value;
534              max_sd = sd;
# Line 232 | Line 542 | namespace oopse {
542          }
543        }
544      }
545 <
545 >    
546   #ifdef IS_MPI
547      int nProc, worldRank;
548 <
548 >    
549      nProc = MPI::COMM_WORLD.Get_size();
550      worldRank = MPI::COMM_WORLD.Get_rank();
551  
# Line 243 | Line 553 | namespace oopse {
553      bool my_max_found = max_found;
554  
555      // Even if we didn't find a minimum, did someone else?
556 <    MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found,
247 <                              1, MPI::BOOL, MPI::LAND);
248 <    
556 >    MPI::COMM_WORLD.Allreduce(&my_min_found, &min_found, 1, MPI::BOOL, MPI::LOR);
557      // Even if we didn't find a maximum, did someone else?
558 <    MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found,
559 <                              1, MPI::BOOL, MPI::LAND);
560 <    
561 <    struct {
562 <      RealType val;
563 <      int rank;
564 <    } max_vals, min_vals;
565 <    
566 <    if (min_found) {
567 <      if (my_min_found)
558 >    MPI::COMM_WORLD.Allreduce(&my_max_found, &max_found, 1, MPI::BOOL, MPI::LOR);
559 > #endif
560 >
561 >    if (max_found && min_found) {
562 >
563 > #ifdef IS_MPI
564 >      struct {
565 >        RealType val;
566 >        int rank;
567 >      } max_vals, min_vals;
568 >      
569 >      if (my_min_found) {
570          min_vals.val = min_val;
571 <      else
571 >      } else {
572          min_vals.val = HONKING_LARGE_VALUE;
573 <      
573 >      }
574        min_vals.rank = worldRank;    
575        
576        // Who had the minimum?
577        MPI::COMM_WORLD.Allreduce(&min_vals, &min_vals,
578                                  1, MPI::REALTYPE_INT, MPI::MINLOC);
579        min_val = min_vals.val;
270    }
580        
581 <    if (max_found) {
273 <      if (my_max_found)
581 >      if (my_max_found) {
582          max_vals.val = max_val;
583 <      else
583 >      } else {
584          max_vals.val = -HONKING_LARGE_VALUE;
585 <      
585 >      }
586        max_vals.rank = worldRank;    
587        
588        // Who had the maximum?
589        MPI::COMM_WORLD.Allreduce(&max_vals, &max_vals,
590                                  1, MPI::REALTYPE_INT, MPI::MAXLOC);
591        max_val = max_vals.val;
284    }
592   #endif
593 <
594 <    if (max_found && min_found) {
595 <      if (min_val< max_val) {
289 <
593 >      
594 >      if (min_val < max_val) {
595 >        
596   #ifdef IS_MPI      
597          if (max_vals.rank == worldRank && min_vals.rank == worldRank) {
598            // I have both maximum and minimum, so proceed like a single
599            // processor version:
600   #endif
601 <          // objects to be swapped: velocity & angular velocity
601 >
602            Vector3d min_vel = min_sd->getVel();
603            Vector3d max_vel = max_sd->getVel();
604            RealType temp_vel;
605            
606 <          switch(rnemdType_) {
607 <          case rnemdKinetic :
606 >          switch(rnemdFluxType_) {
607 >          case rnemdKE :
608              min_sd->setVel(max_vel);
609              max_sd->setVel(min_vel);
610 <            if (min_sd->isDirectional() && max_sd->isDirectional()) {
610 >            if (min_sd->isDirectional() && max_sd->isDirectional()) {
611                Vector3d min_angMom = min_sd->getJ();
612                Vector3d max_angMom = max_sd->getJ();
613                min_sd->setJ(max_angMom);
614                max_sd->setJ(min_angMom);
615 <            }
615 >            }//angular momenta exchange enabled
616 >            //assumes same rigid body identity
617              break;
618            case rnemdPx :
619              temp_vel = min_vel.x();
# Line 329 | Line 636 | namespace oopse {
636              min_sd->setVel(min_vel);
637              max_sd->setVel(max_vel);
638              break;
332          case rnemdUnknown :
639            default :
640              break;
641            }
642 +
643   #ifdef IS_MPI
644            // the rest of the cases only apply in parallel simulations:
645          } else if (max_vals.rank == worldRank) {
# Line 348 | Line 655 | namespace oopse {
655                                     min_vel.getArrayPointer(), 3, MPI::REALTYPE,
656                                     min_vals.rank, 0, status);
657            
658 <          switch(rnemdType_) {
659 <          case rnemdKinetic :
658 >          switch(rnemdFluxType_) {
659 >          case rnemdKE :
660              max_sd->setVel(min_vel);
661 <            
661 >            //angular momenta exchange enabled
662              if (max_sd->isDirectional()) {
663                Vector3d min_angMom;
664                Vector3d max_angMom = max_sd->getJ();
665 <
665 >              
666                // point-to-point swap of the angular momentum vector
667                MPI::COMM_WORLD.Sendrecv(max_angMom.getArrayPointer(), 3,
668                                         MPI::REALTYPE, min_vals.rank, 1,
669                                         min_angMom.getArrayPointer(), 3,
670                                         MPI::REALTYPE, min_vals.rank, 1,
671                                         status);
672 <
672 >              
673                max_sd->setJ(min_angMom);
674 <            }
674 >            }
675              break;
676            case rnemdPx :
677              max_vel.x() = min_vel.x();
# Line 378 | Line 685 | namespace oopse {
685              max_vel.z() = min_vel.z();
686              max_sd->setVel(max_vel);
687              break;
381          case rnemdUnknown :
688            default :
689              break;
690            }
# Line 395 | Line 701 | namespace oopse {
701                                     max_vel.getArrayPointer(), 3, MPI::REALTYPE,
702                                     max_vals.rank, 0, status);
703            
704 <          switch(rnemdType_) {
705 <          case rnemdKinetic :
704 >          switch(rnemdFluxType_) {
705 >          case rnemdKE :
706              min_sd->setVel(max_vel);
707 <            
707 >            //angular momenta exchange enabled
708              if (min_sd->isDirectional()) {
709                Vector3d min_angMom = min_sd->getJ();
710                Vector3d max_angMom;
711 <
711 >              
712                // point-to-point swap of the angular momentum vector
713                MPI::COMM_WORLD.Sendrecv(min_angMom.getArrayPointer(), 3,
714                                         MPI::REALTYPE, max_vals.rank, 1,
715                                         max_angMom.getArrayPointer(), 3,
716                                         MPI::REALTYPE, max_vals.rank, 1,
717                                         status);
718 <
718 >              
719                min_sd->setJ(max_angMom);
720              }
721              break;
# Line 425 | Line 731 | namespace oopse {
731              min_vel.z() = max_vel.z();
732              min_sd->setVel(min_vel);
733              break;
428          case rnemdUnknown :
734            default :
735              break;
736            }
737          }
738   #endif
739 <        exchangeSum_ += max_val - min_val;
740 <      } else {
741 <        std::cerr << "exchange NOT performed.\nmin_val > max_val.\n";
739 >        
740 >        switch(rnemdFluxType_) {
741 >        case rnemdKE:
742 >          kineticExchange_ += max_val - min_val;
743 >          break;
744 >        case rnemdPx:
745 >          momentumExchange_.x() += max_val - min_val;
746 >          break;
747 >        case rnemdPy:
748 >          momentumExchange_.y() += max_val - min_val;
749 >          break;
750 >        case rnemdPz:
751 >          momentumExchange_.z() += max_val - min_val;
752 >          break;
753 >        default:
754 >          break;
755 >        }
756 >      } else {        
757 >        sprintf(painCave.errMsg,
758 >                "RNEMD::doSwap exchange NOT performed because min_val > max_val\n");
759 >        painCave.isFatal = 0;
760 >        painCave.severity = OPENMD_INFO;
761 >        simError();        
762 >        failTrialCount_++;
763        }
764      } else {
765 <      std::cerr << "exchange NOT performed.\none of the two slabs empty.\n";
766 <    }
767 <    
765 >      sprintf(painCave.errMsg,
766 >              "RNEMD::doSwap exchange NOT performed because selected object\n"
767 >              "\twas not present in at least one of the two slabs.\n");
768 >      painCave.isFatal = 0;
769 >      painCave.severity = OPENMD_INFO;
770 >      simError();        
771 >      failTrialCount_++;
772 >    }    
773    }
774    
775 <  void RNEMD::getStatus() {
776 <
775 >  void RNEMD::doNIVS() {
776 >    if (!doRNEMD_) return;
777      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
778      Mat3x3d hmat = currentSnap_->getHmat();
448    Stats& stat = currentSnap_->statData;
449    RealType time = currentSnap_->getTime();
779  
451    stat[Stats::RNEMD_SWAP_TOTAL] = exchangeSum_;
452
780      seleMan_.setSelectionSet(evaluator_.evaluate());
781  
782      int selei;
783      StuntDouble* sd;
784      int idx;
785  
786 <    std::vector<RealType> valueHist(nBins_, 0.0); // keeps track of what's
460 <                                                  // being averaged
461 <    std::vector<int> valueCount(nBins_, 0);       // keeps track of the
462 <                                                  // number of degrees of
463 <                                                  // freedom being averaged
786 >    vector<StuntDouble*> hotBin, coldBin;
787  
788 +    RealType Phx = 0.0;
789 +    RealType Phy = 0.0;
790 +    RealType Phz = 0.0;
791 +    RealType Khx = 0.0;
792 +    RealType Khy = 0.0;
793 +    RealType Khz = 0.0;
794 +    RealType Khw = 0.0;
795 +    RealType Pcx = 0.0;
796 +    RealType Pcy = 0.0;
797 +    RealType Pcz = 0.0;
798 +    RealType Kcx = 0.0;
799 +    RealType Kcy = 0.0;
800 +    RealType Kcz = 0.0;
801 +    RealType Kcw = 0.0;
802 +
803      for (sd = seleMan_.beginSelected(selei); sd != NULL;
804           sd = seleMan_.nextSelected(selei)) {
805 <      
805 >
806        idx = sd->getLocalIndex();
807 <      
807 >
808        Vector3d pos = sd->getPos();
809  
810        // wrap the stuntdouble's position back into the box:
811 <      
811 >
812        if (usePeriodicBoundaryConditions_)
813          currentSnap_->wrapVector(pos);
814 <      
814 >
815        // which bin is this stuntdouble in?
816 <      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
817 <      
480 <      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;    
481 <      
482 <      RealType mass = sd->getMass();
483 <      Vector3d vel = sd->getVel();
484 <      RealType value;
816 >      bool inA = inSlabA(pos);
817 >      bool inB = inSlabB(pos);
818  
819 <      switch(rnemdType_) {
820 <      case rnemdKinetic :
821 <        
822 <        value = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
823 <                        vel[2]*vel[2]);
824 <        
825 <        valueCount[binNo] += 3;
826 <        if (sd->isDirectional()) {
827 <          Vector3d angMom = sd->getJ();
828 <          Mat3x3d I = sd->getI();
829 <          
830 <          if (sd->isLinear()) {
831 <            int i = sd->linearAxis();
832 <            int j = (i + 1) % 3;
833 <            int k = (i + 2) % 3;
834 <            value += angMom[j] * angMom[j] / I(j, j) +
835 <              angMom[k] * angMom[k] / I(k, k);
819 >      if (inA || inB) {
820 >              
821 >        RealType mass = sd->getMass();
822 >        Vector3d vel = sd->getVel();
823 >      
824 >        if (inA) {
825 >          hotBin.push_back(sd);
826 >          Phx += mass * vel.x();
827 >          Phy += mass * vel.y();
828 >          Phz += mass * vel.z();
829 >          Khx += mass * vel.x() * vel.x();
830 >          Khy += mass * vel.y() * vel.y();
831 >          Khz += mass * vel.z() * vel.z();
832 >          if (sd->isDirectional()) {
833 >            Vector3d angMom = sd->getJ();
834 >            Mat3x3d I = sd->getI();
835 >            if (sd->isLinear()) {
836 >              int i = sd->linearAxis();
837 >              int j = (i + 1) % 3;
838 >              int k = (i + 2) % 3;
839 >              Khw += angMom[j] * angMom[j] / I(j, j) +
840 >                angMom[k] * angMom[k] / I(k, k);
841 >            } else {
842 >              Khw += angMom[0]*angMom[0]/I(0, 0)
843 >                + angMom[1]*angMom[1]/I(1, 1)
844 >                + angMom[2]*angMom[2]/I(2, 2);
845 >            }
846 >          }
847 >        } else {
848 >          coldBin.push_back(sd);
849 >          Pcx += mass * vel.x();
850 >          Pcy += mass * vel.y();
851 >          Pcz += mass * vel.z();
852 >          Kcx += mass * vel.x() * vel.x();
853 >          Kcy += mass * vel.y() * vel.y();
854 >          Kcz += mass * vel.z() * vel.z();
855 >          if (sd->isDirectional()) {
856 >            Vector3d angMom = sd->getJ();
857 >            Mat3x3d I = sd->getI();
858 >            if (sd->isLinear()) {
859 >              int i = sd->linearAxis();
860 >              int j = (i + 1) % 3;
861 >              int k = (i + 2) % 3;
862 >              Kcw += angMom[j] * angMom[j] / I(j, j) +
863 >                angMom[k] * angMom[k] / I(k, k);
864 >            } else {
865 >              Kcw += angMom[0]*angMom[0]/I(0, 0)
866 >                + angMom[1]*angMom[1]/I(1, 1)
867 >                + angMom[2]*angMom[2]/I(2, 2);
868 >            }
869 >          }
870 >        }
871 >      }
872 >    }
873 >    
874 >    Khx *= 0.5;
875 >    Khy *= 0.5;
876 >    Khz *= 0.5;
877 >    Khw *= 0.5;
878 >    Kcx *= 0.5;
879 >    Kcy *= 0.5;
880 >    Kcz *= 0.5;
881 >    Kcw *= 0.5;
882  
883 <            valueCount[binNo] +=2;
883 > #ifdef IS_MPI
884 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phx, 1, MPI::REALTYPE, MPI::SUM);
885 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phy, 1, MPI::REALTYPE, MPI::SUM);
886 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Phz, 1, MPI::REALTYPE, MPI::SUM);
887 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcx, 1, MPI::REALTYPE, MPI::SUM);
888 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcy, 1, MPI::REALTYPE, MPI::SUM);
889 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pcz, 1, MPI::REALTYPE, MPI::SUM);
890  
891 <          } else {
892 <            value += angMom[0]*angMom[0]/I(0, 0)
893 <              + angMom[1]*angMom[1]/I(1, 1)
894 <              + angMom[2]*angMom[2]/I(2, 2);
895 <            valueCount[binNo] +=3;
891 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khx, 1, MPI::REALTYPE, MPI::SUM);
892 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khy, 1, MPI::REALTYPE, MPI::SUM);
893 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khz, 1, MPI::REALTYPE, MPI::SUM);
894 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Khw, 1, MPI::REALTYPE, MPI::SUM);
895 >
896 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcx, 1, MPI::REALTYPE, MPI::SUM);
897 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcy, 1, MPI::REALTYPE, MPI::SUM);
898 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcz, 1, MPI::REALTYPE, MPI::SUM);
899 >    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kcw, 1, MPI::REALTYPE, MPI::SUM);
900 > #endif
901 >
902 >    //solve coldBin coeff's first
903 >    RealType px = Pcx / Phx;
904 >    RealType py = Pcy / Phy;
905 >    RealType pz = Pcz / Phz;
906 >    RealType c, x, y, z;
907 >    bool successfulScale = false;
908 >    if ((rnemdFluxType_ == rnemdFullKE) ||
909 >        (rnemdFluxType_ == rnemdRotKE)) {
910 >      //may need sanity check Khw & Kcw > 0
911 >
912 >      if (rnemdFluxType_ == rnemdFullKE) {
913 >        c = 1.0 - kineticTarget_ / (Kcx + Kcy + Kcz + Kcw);
914 >      } else {
915 >        c = 1.0 - kineticTarget_ / Kcw;
916 >      }
917 >
918 >      if ((c > 0.81) && (c < 1.21)) {//restrict scaling coefficients
919 >        c = sqrt(c);
920 >        //std::cerr << "cold slab scaling coefficient: " << c << endl;
921 >        //now convert to hotBin coefficient
922 >        RealType w = 0.0;
923 >        if (rnemdFluxType_ ==  rnemdFullKE) {
924 >          x = 1.0 + px * (1.0 - c);
925 >          y = 1.0 + py * (1.0 - c);
926 >          z = 1.0 + pz * (1.0 - c);
927 >          /* more complicated way
928 >             w = 1.0 + (Kcw - Kcw * c * c - (c * c * (Kcx + Kcy + Kcz
929 >             + Khx * px * px + Khy * py * py + Khz * pz * pz)
930 >             - 2.0 * c * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py)
931 >             + Khz * pz * (1.0 + pz)) + Khx * px * (2.0 + px)
932 >             + Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
933 >             - Kcx - Kcy - Kcz)) / Khw; the following is simpler
934 >          */
935 >          if ((fabs(x - 1.0) < 0.1) && (fabs(y - 1.0) < 0.1) &&
936 >              (fabs(z - 1.0) < 0.1)) {
937 >            w = 1.0 + (kineticTarget_
938 >                       + Khx * (1.0 - x * x) + Khy * (1.0 - y * y)
939 >                       + Khz * (1.0 - z * z)) / Khw;
940 >          }//no need to calculate w if x, y or z is out of range
941 >        } else {
942 >          w = 1.0 + kineticTarget_ / Khw;
943 >        }
944 >        if ((w > 0.81) && (w < 1.21)) {//restrict scaling coefficients
945 >          //if w is in the right range, so should be x, y, z.
946 >          vector<StuntDouble*>::iterator sdi;
947 >          Vector3d vel;
948 >          for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
949 >            if (rnemdFluxType_ == rnemdFullKE) {
950 >              vel = (*sdi)->getVel() * c;
951 >              (*sdi)->setVel(vel);
952 >            }
953 >            if ((*sdi)->isDirectional()) {
954 >              Vector3d angMom = (*sdi)->getJ() * c;
955 >              (*sdi)->setJ(angMom);
956 >            }
957            }
958 +          w = sqrt(w);
959 +          // std::cerr << "xh= " << x << "\tyh= " << y << "\tzh= " << z
960 +          //           << "\twh= " << w << endl;
961 +          for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
962 +            if (rnemdFluxType_ == rnemdFullKE) {
963 +              vel = (*sdi)->getVel();
964 +              vel.x() *= x;
965 +              vel.y() *= y;
966 +              vel.z() *= z;
967 +              (*sdi)->setVel(vel);
968 +            }
969 +            if ((*sdi)->isDirectional()) {
970 +              Vector3d angMom = (*sdi)->getJ() * w;
971 +              (*sdi)->setJ(angMom);
972 +            }
973 +          }
974 +          successfulScale = true;
975 +          kineticExchange_ += kineticTarget_;
976          }
977 <        value = value / OOPSEConstant::energyConvert / OOPSEConstant::kb;
978 <
979 <        break;
980 <      case rnemdPx :
981 <        value = mass * vel[0];
982 <        valueCount[binNo]++;
977 >      }
978 >    } else {
979 >      RealType a000, a110, c0, a001, a111, b01, b11, c1;
980 >      switch(rnemdFluxType_) {
981 >      case rnemdKE :
982 >        /* used hotBin coeff's & only scale x & y dimensions
983 >           RealType px = Phx / Pcx;
984 >           RealType py = Phy / Pcy;
985 >           a110 = Khy;
986 >           c0 = - Khx - Khy - kineticTarget_;
987 >           a000 = Khx;
988 >           a111 = Kcy * py * py;
989 >           b11 = -2.0 * Kcy * py * (1.0 + py);
990 >           c1 = Kcy * py * (2.0 + py) + Kcx * px * ( 2.0 + px) + kineticTarget_;
991 >           b01 = -2.0 * Kcx * px * (1.0 + px);
992 >           a001 = Kcx * px * px;
993 >        */
994 >        //scale all three dimensions, let c_x = c_y
995 >        a000 = Kcx + Kcy;
996 >        a110 = Kcz;
997 >        c0 = kineticTarget_ - Kcx - Kcy - Kcz;
998 >        a001 = Khx * px * px + Khy * py * py;
999 >        a111 = Khz * pz * pz;
1000 >        b01 = -2.0 * (Khx * px * (1.0 + px) + Khy * py * (1.0 + py));
1001 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
1002 >        c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
1003 >          + Khz * pz * (2.0 + pz) - kineticTarget_;
1004          break;
1005 +      case rnemdPx :
1006 +        c = 1 - momentumTarget_.x() / Pcx;
1007 +        a000 = Kcy;
1008 +        a110 = Kcz;
1009 +        c0 = Kcx * c * c - Kcx - Kcy - Kcz;
1010 +        a001 = py * py * Khy;
1011 +        a111 = pz * pz * Khz;
1012 +        b01 = -2.0 * Khy * py * (1.0 + py);
1013 +        b11 = -2.0 * Khz * pz * (1.0 + pz);
1014 +        c1 = Khy * py * (2.0 + py) + Khz * pz * (2.0 + pz)
1015 +          + Khx * (fastpow(c * px - px - 1.0, 2) - 1.0);
1016 +        break;
1017        case rnemdPy :
1018 <        value = mass * vel[1];
1019 <        valueCount[binNo]++;
1018 >        c = 1 - momentumTarget_.y() / Pcy;
1019 >        a000 = Kcx;
1020 >        a110 = Kcz;
1021 >        c0 = Kcy * c * c - Kcx - Kcy - Kcz;
1022 >        a001 = px * px * Khx;
1023 >        a111 = pz * pz * Khz;
1024 >        b01 = -2.0 * Khx * px * (1.0 + px);
1025 >        b11 = -2.0 * Khz * pz * (1.0 + pz);
1026 >        c1 = Khx * px * (2.0 + px) + Khz * pz * (2.0 + pz)
1027 >          + Khy * (fastpow(c * py - py - 1.0, 2) - 1.0);
1028          break;
1029 <      case rnemdPz :
1030 <        value = mass * vel[2];
1031 <        valueCount[binNo]++;
1029 >      case rnemdPz ://we don't really do this, do we?
1030 >        c = 1 - momentumTarget_.z() / Pcz;
1031 >        a000 = Kcx;
1032 >        a110 = Kcy;
1033 >        c0 = Kcz * c * c - Kcx - Kcy - Kcz;
1034 >        a001 = px * px * Khx;
1035 >        a111 = py * py * Khy;
1036 >        b01 = -2.0 * Khx * px * (1.0 + px);
1037 >        b11 = -2.0 * Khy * py * (1.0 + py);
1038 >        c1 = Khx * px * (2.0 + px) + Khy * py * (2.0 + py)
1039 >          + Khz * (fastpow(c * pz - pz - 1.0, 2) - 1.0);
1040          break;
528      case rnemdUnknown :
1041        default :
1042          break;
1043        }
1044 <      valueHist[binNo] += value;
1044 >      
1045 >      RealType v1 = a000 * a111 - a001 * a110;
1046 >      RealType v2 = a000 * b01;
1047 >      RealType v3 = a000 * b11;
1048 >      RealType v4 = a000 * c1 - a001 * c0;
1049 >      RealType v8 = a110 * b01;
1050 >      RealType v10 = - b01 * c0;
1051 >      
1052 >      RealType u0 = v2 * v10 - v4 * v4;
1053 >      RealType u1 = -2.0 * v3 * v4;
1054 >      RealType u2 = -v2 * v8 - v3 * v3 - 2.0 * v1 * v4;
1055 >      RealType u3 = -2.0 * v1 * v3;
1056 >      RealType u4 = - v1 * v1;
1057 >      //rescale coefficients
1058 >      RealType maxAbs = fabs(u0);
1059 >      if (maxAbs < fabs(u1)) maxAbs = fabs(u1);
1060 >      if (maxAbs < fabs(u2)) maxAbs = fabs(u2);
1061 >      if (maxAbs < fabs(u3)) maxAbs = fabs(u3);
1062 >      if (maxAbs < fabs(u4)) maxAbs = fabs(u4);
1063 >      u0 /= maxAbs;
1064 >      u1 /= maxAbs;
1065 >      u2 /= maxAbs;
1066 >      u3 /= maxAbs;
1067 >      u4 /= maxAbs;
1068 >      //max_element(start, end) is also available.
1069 >      Polynomial<RealType> poly; //same as DoublePolynomial poly;
1070 >      poly.setCoefficient(4, u4);
1071 >      poly.setCoefficient(3, u3);
1072 >      poly.setCoefficient(2, u2);
1073 >      poly.setCoefficient(1, u1);
1074 >      poly.setCoefficient(0, u0);
1075 >      vector<RealType> realRoots = poly.FindRealRoots();
1076 >      
1077 >      vector<RealType>::iterator ri;
1078 >      RealType r1, r2, alpha0;
1079 >      vector<pair<RealType,RealType> > rps;
1080 >      for (ri = realRoots.begin(); ri !=realRoots.end(); ri++) {
1081 >        r2 = *ri;
1082 >        //check if FindRealRoots() give the right answer
1083 >        if ( fabs(u0 + r2 * (u1 + r2 * (u2 + r2 * (u3 + r2 * u4)))) > 1e-6 ) {
1084 >          sprintf(painCave.errMsg,
1085 >                  "RNEMD Warning: polynomial solve seems to have an error!");
1086 >          painCave.isFatal = 0;
1087 >          simError();
1088 >          failRootCount_++;
1089 >        }
1090 >        //might not be useful w/o rescaling coefficients
1091 >        alpha0 = -c0 - a110 * r2 * r2;
1092 >        if (alpha0 >= 0.0) {
1093 >          r1 = sqrt(alpha0 / a000);
1094 >          if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
1095 >              < 1e-6)
1096 >            { rps.push_back(make_pair(r1, r2)); }
1097 >          if (r1 > 1e-6) { //r1 non-negative
1098 >            r1 = -r1;
1099 >            if (fabs(c1 + r1 * (b01 + r1 * a001) + r2 * (b11 + r2 * a111))
1100 >                < 1e-6)
1101 >              { rps.push_back(make_pair(r1, r2)); }
1102 >          }
1103 >        }
1104 >      }
1105 >      // Consider combining together the solving pair part w/ the searching
1106 >      // best solution part so that we don't need the pairs vector
1107 >      if (!rps.empty()) {
1108 >        RealType smallestDiff = HONKING_LARGE_VALUE;
1109 >        RealType diff;
1110 >        pair<RealType,RealType> bestPair = make_pair(1.0, 1.0);
1111 >        vector<pair<RealType,RealType> >::iterator rpi;
1112 >        for (rpi = rps.begin(); rpi != rps.end(); rpi++) {
1113 >          r1 = (*rpi).first;
1114 >          r2 = (*rpi).second;
1115 >          switch(rnemdFluxType_) {
1116 >          case rnemdKE :
1117 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1118 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2)
1119 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1120 >            break;
1121 >          case rnemdPx :
1122 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1123 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcy, 2);
1124 >            break;
1125 >          case rnemdPy :
1126 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1127 >              + fastpow(r1 * r1 / r2 / r2 - Kcz/Kcx, 2);
1128 >            break;
1129 >          case rnemdPz :
1130 >            diff = fastpow(1.0 - r1, 2) + fastpow(1.0 - r2, 2)
1131 >              + fastpow(r1 * r1 / r2 / r2 - Kcy/Kcx, 2);
1132 >          default :
1133 >            break;
1134 >          }
1135 >          if (diff < smallestDiff) {
1136 >            smallestDiff = diff;
1137 >            bestPair = *rpi;
1138 >          }
1139 >        }
1140 > #ifdef IS_MPI
1141 >        if (worldRank == 0) {
1142 > #endif
1143 >          // sprintf(painCave.errMsg,
1144 >          //         "RNEMD: roots r1= %lf\tr2 = %lf\n",
1145 >          //         bestPair.first, bestPair.second);
1146 >          // painCave.isFatal = 0;
1147 >          // painCave.severity = OPENMD_INFO;
1148 >          // simError();
1149 > #ifdef IS_MPI
1150 >        }
1151 > #endif
1152 >        
1153 >        switch(rnemdFluxType_) {
1154 >        case rnemdKE :
1155 >          x = bestPair.first;
1156 >          y = bestPair.first;
1157 >          z = bestPair.second;
1158 >          break;
1159 >        case rnemdPx :
1160 >          x = c;
1161 >          y = bestPair.first;
1162 >          z = bestPair.second;
1163 >          break;
1164 >        case rnemdPy :
1165 >          x = bestPair.first;
1166 >          y = c;
1167 >          z = bestPair.second;
1168 >          break;
1169 >        case rnemdPz :
1170 >          x = bestPair.first;
1171 >          y = bestPair.second;
1172 >          z = c;
1173 >          break;          
1174 >        default :
1175 >          break;
1176 >        }
1177 >        vector<StuntDouble*>::iterator sdi;
1178 >        Vector3d vel;
1179 >        for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1180 >          vel = (*sdi)->getVel();
1181 >          vel.x() *= x;
1182 >          vel.y() *= y;
1183 >          vel.z() *= z;
1184 >          (*sdi)->setVel(vel);
1185 >        }
1186 >        //convert to hotBin coefficient
1187 >        x = 1.0 + px * (1.0 - x);
1188 >        y = 1.0 + py * (1.0 - y);
1189 >        z = 1.0 + pz * (1.0 - z);
1190 >        for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1191 >          vel = (*sdi)->getVel();
1192 >          vel.x() *= x;
1193 >          vel.y() *= y;
1194 >          vel.z() *= z;
1195 >          (*sdi)->setVel(vel);
1196 >        }
1197 >        successfulScale = true;
1198 >        switch(rnemdFluxType_) {
1199 >        case rnemdKE :
1200 >          kineticExchange_ += kineticTarget_;
1201 >          break;
1202 >        case rnemdPx :
1203 >        case rnemdPy :
1204 >        case rnemdPz :
1205 >          momentumExchange_ += momentumTarget_;
1206 >          break;          
1207 >        default :
1208 >          break;
1209 >        }      
1210 >      }
1211      }
1212 +    if (successfulScale != true) {
1213 +      sprintf(painCave.errMsg,
1214 +              "RNEMD::doNIVS exchange NOT performed - roots that solve\n"
1215 +              "\tthe constraint equations may not exist or there may be\n"
1216 +              "\tno selected objects in one or both slabs.\n");
1217 +      painCave.isFatal = 0;
1218 +      painCave.severity = OPENMD_INFO;
1219 +      simError();        
1220 +      failTrialCount_++;
1221 +    }
1222 +  }
1223  
1224 < #ifdef IS_MPI
1224 >  void RNEMD::doVSS() {
1225 >    if (!doRNEMD_) return;
1226 >    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1227 >    RealType time = currentSnap_->getTime();    
1228 >    Mat3x3d hmat = currentSnap_->getHmat();
1229  
1230 <    // all processors have the same number of bins, and STL vectors pack their
538 <    // arrays, so in theory, this should be safe:
1230 >    seleMan_.setSelectionSet(evaluator_.evaluate());
1231  
1232 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueHist[0],
1233 <                              nBins_, MPI::REALTYPE, MPI::SUM);
1234 <    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &valueCount[0],
543 <                              nBins_, MPI::INT, MPI::SUM);
1232 >    int selei;
1233 >    StuntDouble* sd;
1234 >    int idx;
1235  
1236 +    vector<StuntDouble*> hotBin, coldBin;
1237 +
1238 +    Vector3d Ph(V3Zero);
1239 +    RealType Mh = 0.0;
1240 +    RealType Kh = 0.0;
1241 +    Vector3d Pc(V3Zero);
1242 +    RealType Mc = 0.0;
1243 +    RealType Kc = 0.0;
1244 +    
1245 +
1246 +    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1247 +         sd = seleMan_.nextSelected(selei)) {
1248 +
1249 +      idx = sd->getLocalIndex();
1250 +
1251 +      Vector3d pos = sd->getPos();
1252 +
1253 +      // wrap the stuntdouble's position back into the box:
1254 +
1255 +      if (usePeriodicBoundaryConditions_)
1256 +        currentSnap_->wrapVector(pos);
1257 +
1258 +      // which bin is this stuntdouble in?
1259 +      bool inA = inSlabA(pos);
1260 +      bool inB = inSlabB(pos);
1261 +      
1262 +      if (inA || inB) {
1263 +        
1264 +        RealType mass = sd->getMass();
1265 +        Vector3d vel = sd->getVel();
1266 +      
1267 +        if (inA) {
1268 +          hotBin.push_back(sd);
1269 +          //std::cerr << "before, velocity = " << vel << endl;
1270 +          Ph += mass * vel;
1271 +          //std::cerr << "after, velocity = " << vel << endl;
1272 +          Mh += mass;
1273 +          Kh += mass * vel.lengthSquare();
1274 +          if (rnemdFluxType_ == rnemdFullKE) {
1275 +            if (sd->isDirectional()) {
1276 +              Vector3d angMom = sd->getJ();
1277 +              Mat3x3d I = sd->getI();
1278 +              if (sd->isLinear()) {
1279 +                int i = sd->linearAxis();
1280 +                int j = (i + 1) % 3;
1281 +                int k = (i + 2) % 3;
1282 +                Kh += angMom[j] * angMom[j] / I(j, j) +
1283 +                  angMom[k] * angMom[k] / I(k, k);
1284 +              } else {
1285 +                Kh += angMom[0] * angMom[0] / I(0, 0) +
1286 +                  angMom[1] * angMom[1] / I(1, 1) +
1287 +                  angMom[2] * angMom[2] / I(2, 2);
1288 +              }
1289 +            }
1290 +          }
1291 +        } else { //midBin_
1292 +          coldBin.push_back(sd);
1293 +          Pc += mass * vel;
1294 +          Mc += mass;
1295 +          Kc += mass * vel.lengthSquare();
1296 +          if (rnemdFluxType_ == rnemdFullKE) {
1297 +            if (sd->isDirectional()) {
1298 +              Vector3d angMom = sd->getJ();
1299 +              Mat3x3d I = sd->getI();
1300 +              if (sd->isLinear()) {
1301 +                int i = sd->linearAxis();
1302 +                int j = (i + 1) % 3;
1303 +                int k = (i + 2) % 3;
1304 +                Kc += angMom[j] * angMom[j] / I(j, j) +
1305 +                  angMom[k] * angMom[k] / I(k, k);
1306 +              } else {
1307 +                Kc += angMom[0] * angMom[0] / I(0, 0) +
1308 +                  angMom[1] * angMom[1] / I(1, 1) +
1309 +                  angMom[2] * angMom[2] / I(2, 2);
1310 +              }
1311 +            }
1312 +          }
1313 +        }
1314 +      }
1315 +    }
1316 +    
1317 +    Kh *= 0.5;
1318 +    Kc *= 0.5;
1319 +
1320 +    // std::cerr << "Mh= " << Mh << "\tKh= " << Kh << "\tMc= " << Mc
1321 +    //        << "\tKc= " << Kc << endl;
1322 +    // std::cerr << "Ph= " << Ph << "\tPc= " << Pc << endl;
1323 +    
1324 + #ifdef IS_MPI
1325 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Ph[0], 3, MPI::REALTYPE, MPI::SUM);
1326 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Pc[0], 3, MPI::REALTYPE, MPI::SUM);
1327 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mh, 1, MPI::REALTYPE, MPI::SUM);
1328 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kh, 1, MPI::REALTYPE, MPI::SUM);
1329 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Mc, 1, MPI::REALTYPE, MPI::SUM);
1330 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &Kc, 1, MPI::REALTYPE, MPI::SUM);
1331 + #endif
1332 +
1333 +    bool successfulExchange = false;
1334 +    if ((Mh > 0.0) && (Mc > 0.0)) {//both slabs are not empty
1335 +      Vector3d vc = Pc / Mc;
1336 +      Vector3d ac = -momentumTarget_ / Mc + vc;
1337 +      Vector3d acrec = -momentumTarget_ / Mc;
1338 +      RealType cNumerator = Kc - kineticTarget_ - 0.5 * Mc * ac.lengthSquare();
1339 +      if (cNumerator > 0.0) {
1340 +        RealType cDenominator = Kc - 0.5 * Mc * vc.lengthSquare();
1341 +        if (cDenominator > 0.0) {
1342 +          RealType c = sqrt(cNumerator / cDenominator);
1343 +          if ((c > 0.9) && (c < 1.1)) {//restrict scaling coefficients
1344 +            Vector3d vh = Ph / Mh;
1345 +            Vector3d ah = momentumTarget_ / Mh + vh;
1346 +            Vector3d ahrec = momentumTarget_ / Mh;
1347 +            RealType hNumerator = Kh + kineticTarget_
1348 +              - 0.5 * Mh * ah.lengthSquare();
1349 +            if (hNumerator > 0.0) {
1350 +              RealType hDenominator = Kh - 0.5 * Mh * vh.lengthSquare();
1351 +              if (hDenominator > 0.0) {
1352 +                RealType h = sqrt(hNumerator / hDenominator);
1353 +                if ((h > 0.9) && (h < 1.1)) {
1354 +                  // std::cerr << "cold slab scaling coefficient: " << c << "\n";
1355 +                  // std::cerr << "hot slab scaling coefficient: " << h <<  "\n";
1356 +                  vector<StuntDouble*>::iterator sdi;
1357 +                  Vector3d vel;
1358 +                  for (sdi = coldBin.begin(); sdi != coldBin.end(); sdi++) {
1359 +                    //vel = (*sdi)->getVel();
1360 +                    vel = ((*sdi)->getVel() - vc) * c + ac;
1361 +                    (*sdi)->setVel(vel);
1362 +                    if (rnemdFluxType_ == rnemdFullKE) {
1363 +                      if ((*sdi)->isDirectional()) {
1364 +                        Vector3d angMom = (*sdi)->getJ() * c;
1365 +                        (*sdi)->setJ(angMom);
1366 +                      }
1367 +                    }
1368 +                  }
1369 +                  for (sdi = hotBin.begin(); sdi != hotBin.end(); sdi++) {
1370 +                    //vel = (*sdi)->getVel();
1371 +                    vel = ((*sdi)->getVel() - vh) * h + ah;
1372 +                    (*sdi)->setVel(vel);
1373 +                    if (rnemdFluxType_ == rnemdFullKE) {
1374 +                      if ((*sdi)->isDirectional()) {
1375 +                        Vector3d angMom = (*sdi)->getJ() * h;
1376 +                        (*sdi)->setJ(angMom);
1377 +                      }
1378 +                    }
1379 +                  }
1380 +                  successfulExchange = true;
1381 +                  kineticExchange_ += kineticTarget_;
1382 +                  momentumExchange_ += momentumTarget_;
1383 +                }
1384 +              }
1385 +            }
1386 +          }
1387 +        }
1388 +      }
1389 +    }
1390 +    if (successfulExchange != true) {
1391 +      sprintf(painCave.errMsg,
1392 +              "RNEMD::doVSS exchange NOT performed - roots that solve\n"
1393 +              "\tthe constraint equations may not exist or there may be\n"
1394 +              "\tno selected objects in one or both slabs.\n");
1395 +      painCave.isFatal = 0;
1396 +      painCave.severity = OPENMD_INFO;
1397 +      simError();        
1398 +      failTrialCount_++;
1399 +    }
1400 +  }
1401 +
1402 +  void RNEMD::doRNEMD() {
1403 +    if (!doRNEMD_) return;
1404 +    trialCount_++;
1405 +    switch(rnemdMethod_) {
1406 +    case rnemdSwap:
1407 +      doSwap();
1408 +      break;
1409 +    case rnemdNIVS:
1410 +      doNIVS();
1411 +      break;
1412 +    case rnemdVSS:
1413 +      doVSS();
1414 +      break;
1415 +    case rnemdUnkownMethod:
1416 +    default :
1417 +      break;
1418 +    }
1419 +  }
1420 +
1421 +  void RNEMD::collectData() {
1422 +    if (!doRNEMD_) return;
1423 +    Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1424 +    Mat3x3d hmat = currentSnap_->getHmat();
1425 +
1426 +    areaAccumulator_->add(currentSnap_->getXYarea());
1427 +
1428 +    seleMan_.setSelectionSet(evaluator_.evaluate());
1429 +
1430 +    int selei;
1431 +    StuntDouble* sd;
1432 +    int idx;
1433 +
1434 +    vector<RealType> binMass(nBins_, 0.0);
1435 +    vector<RealType> binPx(nBins_, 0.0);
1436 +    vector<RealType> binPy(nBins_, 0.0);
1437 +    vector<RealType> binPz(nBins_, 0.0);
1438 +    vector<RealType> binKE(nBins_, 0.0);
1439 +    vector<int> binDOF(nBins_, 0);
1440 +    vector<int> binCount(nBins_, 0);
1441 +
1442 +    // alternative approach, track all molecules instead of only those
1443 +    // selected for scaling/swapping:
1444 +    /*
1445 +    SimInfo::MoleculeIterator miter;
1446 +    vector<StuntDouble*>::iterator iiter;
1447 +    Molecule* mol;
1448 +    StuntDouble* sd;
1449 +    for (mol = info_->beginMolecule(miter); mol != NULL;
1450 +      mol = info_->nextMolecule(miter))
1451 +      sd is essentially sd
1452 +        for (sd = mol->beginIntegrableObject(iiter);
1453 +             sd != NULL;
1454 +             sd = mol->nextIntegrableObject(iiter))
1455 +    */
1456 +    for (sd = seleMan_.beginSelected(selei); sd != NULL;
1457 +         sd = seleMan_.nextSelected(selei)) {
1458 +      
1459 +      idx = sd->getLocalIndex();
1460 +      
1461 +      Vector3d pos = sd->getPos();
1462 +
1463 +      // wrap the stuntdouble's position back into the box:
1464 +      
1465 +      if (usePeriodicBoundaryConditions_)
1466 +        currentSnap_->wrapVector(pos);
1467 +
1468 +
1469 +      // which bin is this stuntdouble in?
1470 +      // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)]
1471 +      // Shift molecules by half a box to have bins start at 0
1472 +      // The modulo operator is used to wrap the case when we are
1473 +      // beyond the end of the bins back to the beginning.
1474 +      int binNo = int(nBins_ * (pos.z() / hmat(2,2) + 0.5)) % nBins_;
1475 +    
1476 +      RealType mass = sd->getMass();
1477 +      Vector3d vel = sd->getVel();
1478 +
1479 +      binCount[binNo]++;
1480 +      binMass[binNo] += mass;
1481 +      binPx[binNo] += mass*vel.x();
1482 +      binPy[binNo] += mass*vel.y();
1483 +      binPz[binNo] += mass*vel.z();
1484 +      binKE[binNo] += 0.5 * (mass * vel.lengthSquare());
1485 +      binDOF[binNo] += 3;
1486 +
1487 +      if (sd->isDirectional()) {
1488 +        Vector3d angMom = sd->getJ();
1489 +        Mat3x3d I = sd->getI();
1490 +        if (sd->isLinear()) {
1491 +          int i = sd->linearAxis();
1492 +          int j = (i + 1) % 3;
1493 +          int k = (i + 2) % 3;
1494 +          binKE[binNo] += 0.5 * (angMom[j] * angMom[j] / I(j, j) +
1495 +                                 angMom[k] * angMom[k] / I(k, k));
1496 +          binDOF[binNo] += 2;
1497 +        } else {
1498 +          binKE[binNo] += 0.5 * (angMom[0] * angMom[0] / I(0, 0) +
1499 +                                 angMom[1] * angMom[1] / I(1, 1) +
1500 +                                 angMom[2] * angMom[2] / I(2, 2));
1501 +          binDOF[binNo] += 3;
1502 +        }
1503 +      }
1504 +    }
1505 +    
1506 +
1507 + #ifdef IS_MPI
1508 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binCount[0],
1509 +                              nBins_, MPI::INT, MPI::SUM);
1510 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binMass[0],
1511 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1512 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPx[0],
1513 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1514 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPy[0],
1515 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1516 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binPz[0],
1517 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1518 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binKE[0],
1519 +                              nBins_, MPI::REALTYPE, MPI::SUM);
1520 +    MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &binDOF[0],
1521 +                              nBins_, MPI::INT, MPI::SUM);
1522 + #endif
1523 +
1524 +    Vector3d vel;
1525 +    RealType den;
1526 +    RealType temp;
1527 +    RealType z;
1528 +    for (int i = 0; i < nBins_; i++) {
1529 +      z = (((RealType)i + 0.5) / (RealType)nBins_) * hmat(2,2);
1530 +      vel.x() = binPx[i] / binMass[i];
1531 +      vel.y() = binPy[i] / binMass[i];
1532 +      vel.z() = binPz[i] / binMass[i];
1533 +
1534 +      den = binMass[i] * nBins_ * PhysicalConstants::densityConvert
1535 +        / currentSnap_->getVolume() ;
1536 +
1537 +      temp = 2.0 * binKE[i] / (binDOF[i] * PhysicalConstants::kb *
1538 +                               PhysicalConstants::energyConvert);
1539 +
1540 +      for (unsigned int j = 0; j < outputMask_.size(); ++j) {
1541 +        if(outputMask_[j]) {
1542 +          switch(j) {
1543 +          case Z:
1544 +            (data_[j].accumulator[i])->add(z);
1545 +            break;
1546 +          case TEMPERATURE:
1547 +            data_[j].accumulator[i]->add(temp);
1548 +            break;
1549 +          case VELOCITY:
1550 +            dynamic_cast<VectorAccumulator *>(data_[j].accumulator[i])->add(vel);
1551 +            break;
1552 +          case DENSITY:
1553 +            data_[j].accumulator[i]->add(den);
1554 +            break;
1555 +          }
1556 +        }
1557 +      }
1558 +    }
1559 +  }
1560 +
1561 +  void RNEMD::getStarted() {
1562 +    if (!doRNEMD_) return;
1563 +    collectData();
1564 +    writeOutputFile();
1565 +  }
1566 +
1567 +  void RNEMD::parseOutputFileFormat(const std::string& format) {
1568 +    if (!doRNEMD_) return;
1569 +    StringTokenizer tokenizer(format, " ,;|\t\n\r");
1570 +    
1571 +    while(tokenizer.hasMoreTokens()) {
1572 +      std::string token(tokenizer.nextToken());
1573 +      toUpper(token);
1574 +      OutputMapType::iterator i = outputMap_.find(token);
1575 +      if (i != outputMap_.end()) {
1576 +        outputMask_.set(i->second);
1577 +      } else {
1578 +        sprintf( painCave.errMsg,
1579 +                 "RNEMD::parseOutputFileFormat: %s is not a recognized\n"
1580 +                 "\toutputFileFormat keyword.\n", token.c_str() );
1581 +        painCave.isFatal = 0;
1582 +        painCave.severity = OPENMD_ERROR;
1583 +        simError();            
1584 +      }
1585 +    }  
1586 +  }
1587 +  
1588 +  void RNEMD::writeOutputFile() {
1589 +    if (!doRNEMD_) return;
1590 +    
1591 + #ifdef IS_MPI
1592      // If we're the root node, should we print out the results
1593      int worldRank = MPI::COMM_WORLD.Get_rank();
1594      if (worldRank == 0) {
1595   #endif
1596 <      
550 <      std::cout << time;
551 <      for (int j = 0; j < nBins_; j++)
552 <        std::cout << "\t" << valueHist[j] / (RealType)valueCount[j];
553 <      std::cout << "\n";
1596 >      rnemdFile_.open(rnemdFileName_.c_str(), std::ios::out | std::ios::trunc );
1597        
1598 +      if( !rnemdFile_ ){        
1599 +        sprintf( painCave.errMsg,
1600 +                 "Could not open \"%s\" for RNEMD output.\n",
1601 +                 rnemdFileName_.c_str());
1602 +        painCave.isFatal = 1;
1603 +        simError();
1604 +      }
1605 +
1606 +      Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot();
1607 +
1608 +      RealType time = currentSnap_->getTime();
1609 +      RealType avgArea;
1610 +      areaAccumulator_->getAverage(avgArea);
1611 +      RealType Jz = kineticExchange_ / (2.0 * time * avgArea)
1612 +        / PhysicalConstants::energyConvert;
1613 +      Vector3d JzP = momentumExchange_ / (2.0 * time * avgArea);      
1614 +
1615 +      rnemdFile_ << "#######################################################\n";
1616 +      rnemdFile_ << "# RNEMD {\n";
1617 +
1618 +      map<string, RNEMDMethod>::iterator mi;
1619 +      for(mi = stringToMethod_.begin(); mi != stringToMethod_.end(); ++mi) {
1620 +        if ( (*mi).second == rnemdMethod_)
1621 +          rnemdFile_ << "#    exchangeMethod  = \"" << (*mi).first << "\";\n";
1622 +      }
1623 +      map<string, RNEMDFluxType>::iterator fi;
1624 +      for(fi = stringToFluxType_.begin(); fi != stringToFluxType_.end(); ++fi) {
1625 +        if ( (*fi).second == rnemdFluxType_)
1626 +          rnemdFile_ << "#    fluxType  = \"" << (*fi).first << "\";\n";
1627 +      }
1628 +      
1629 +      rnemdFile_ << "#    exchangeTime = " << exchangeTime_ << ";\n";
1630 +
1631 +      rnemdFile_ << "#    objectSelection = \""
1632 +                 << rnemdObjectSelection_ << "\";\n";
1633 +      rnemdFile_ << "#    slabWidth = " << slabWidth_ << ";\n";
1634 +      rnemdFile_ << "#    slabAcenter = " << slabACenter_ << ";\n";
1635 +      rnemdFile_ << "#    slabBcenter = " << slabBCenter_ << ";\n";
1636 +      rnemdFile_ << "# }\n";
1637 +      rnemdFile_ << "#######################################################\n";
1638 +      rnemdFile_ << "# RNEMD report:\n";      
1639 +      rnemdFile_ << "#     running time = " << time << " fs\n";
1640 +      rnemdFile_ << "#     target flux:\n";
1641 +      rnemdFile_ << "#         kinetic = "
1642 +                 << kineticFlux_ / PhysicalConstants::energyConvert
1643 +                 << " (kcal/mol/A^2/fs)\n";
1644 +      rnemdFile_ << "#         momentum = " << momentumFluxVector_
1645 +                 << " (amu/A/fs^2)\n";
1646 +      rnemdFile_ << "#     target one-time exchanges:\n";
1647 +      rnemdFile_ << "#         kinetic = "
1648 +                 << kineticTarget_ / PhysicalConstants::energyConvert
1649 +                 << " (kcal/mol)\n";
1650 +      rnemdFile_ << "#         momentum = " << momentumTarget_
1651 +                 << " (amu*A/fs)\n";
1652 +      rnemdFile_ << "#     actual exchange totals:\n";
1653 +      rnemdFile_ << "#         kinetic = "
1654 +                 << kineticExchange_ / PhysicalConstants::energyConvert
1655 +                 << " (kcal/mol)\n";
1656 +      rnemdFile_ << "#         momentum = " << momentumExchange_
1657 +                 << " (amu*A/fs)\n";      
1658 +      rnemdFile_ << "#     actual flux:\n";
1659 +      rnemdFile_ << "#         kinetic = " << Jz
1660 +                 << " (kcal/mol/A^2/fs)\n";
1661 +      rnemdFile_ << "#         momentum = " << JzP
1662 +                 << " (amu/A/fs^2)\n";
1663 +      rnemdFile_ << "#     exchange statistics:\n";
1664 +      rnemdFile_ << "#         attempted = " << trialCount_ << "\n";
1665 +      rnemdFile_ << "#         failed = " << failTrialCount_ << "\n";    
1666 +      if (rnemdMethod_ == rnemdNIVS) {
1667 +        rnemdFile_ << "#         NIVS root-check errors = "
1668 +                   << failRootCount_ << "\n";
1669 +      }
1670 +      rnemdFile_ << "#######################################################\n";
1671 +      
1672 +      
1673 +      
1674 +      //write title
1675 +      rnemdFile_ << "#";
1676 +      for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1677 +        if (outputMask_[i]) {
1678 +          rnemdFile_ << "\t" << data_[i].title <<
1679 +            "(" << data_[i].units << ")";
1680 +          // add some extra tabs for column alignment
1681 +          if (data_[i].dataType == "Vector3d") rnemdFile_ << "\t\t";
1682 +        }
1683 +      }
1684 +      rnemdFile_ << std::endl;
1685 +      
1686 +      rnemdFile_.precision(8);
1687 +      
1688 +      for (int j = 0; j < nBins_; j++) {        
1689 +        
1690 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1691 +          if (outputMask_[i]) {
1692 +            if (data_[i].dataType == "RealType")
1693 +              writeReal(i,j);
1694 +            else if (data_[i].dataType == "Vector3d")
1695 +              writeVector(i,j);
1696 +            else {
1697 +              sprintf( painCave.errMsg,
1698 +                       "RNEMD found an unknown data type for: %s ",
1699 +                       data_[i].title.c_str());
1700 +              painCave.isFatal = 1;
1701 +              simError();
1702 +            }
1703 +          }
1704 +        }
1705 +        rnemdFile_ << std::endl;
1706 +        
1707 +      }        
1708 +
1709 +      rnemdFile_ << "#######################################################\n";
1710 +      rnemdFile_ << "# Standard Deviations in those quantities follow:\n";
1711 +      rnemdFile_ << "#######################################################\n";
1712 +
1713 +
1714 +      for (int j = 0; j < nBins_; j++) {        
1715 +        rnemdFile_ << "#";
1716 +        for (unsigned int i = 0; i < outputMask_.size(); ++i) {
1717 +          if (outputMask_[i]) {
1718 +            if (data_[i].dataType == "RealType")
1719 +              writeRealStdDev(i,j);
1720 +            else if (data_[i].dataType == "Vector3d")
1721 +              writeVectorStdDev(i,j);
1722 +            else {
1723 +              sprintf( painCave.errMsg,
1724 +                       "RNEMD found an unknown data type for: %s ",
1725 +                       data_[i].title.c_str());
1726 +              painCave.isFatal = 1;
1727 +              simError();
1728 +            }
1729 +          }
1730 +        }
1731 +        rnemdFile_ << std::endl;
1732 +        
1733 +      }        
1734 +      
1735 +      rnemdFile_.flush();
1736 +      rnemdFile_.close();
1737 +      
1738   #ifdef IS_MPI
1739      }
1740   #endif
1741 +    
1742    }
1743 +  
1744 +  void RNEMD::writeReal(int index, unsigned int bin) {
1745 +    if (!doRNEMD_) return;
1746 +    assert(index >=0 && index < ENDINDEX);
1747 +    assert(bin < nBins_);
1748 +    RealType s;
1749 +    
1750 +    data_[index].accumulator[bin]->getAverage(s);
1751 +    
1752 +    if (! isinf(s) && ! isnan(s)) {
1753 +      rnemdFile_ << "\t" << s;
1754 +    } else{
1755 +      sprintf( painCave.errMsg,
1756 +               "RNEMD detected a numerical error writing: %s for bin %d",
1757 +               data_[index].title.c_str(), bin);
1758 +      painCave.isFatal = 1;
1759 +      simError();
1760 +    }    
1761 +  }
1762 +  
1763 +  void RNEMD::writeVector(int index, unsigned int bin) {
1764 +    if (!doRNEMD_) return;
1765 +    assert(index >=0 && index < ENDINDEX);
1766 +    assert(bin < nBins_);
1767 +    Vector3d s;
1768 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getAverage(s);
1769 +    if (isinf(s[0]) || isnan(s[0]) ||
1770 +        isinf(s[1]) || isnan(s[1]) ||
1771 +        isinf(s[2]) || isnan(s[2]) ) {      
1772 +      sprintf( painCave.errMsg,
1773 +               "RNEMD detected a numerical error writing: %s for bin %d",
1774 +               data_[index].title.c_str(), bin);
1775 +      painCave.isFatal = 1;
1776 +      simError();
1777 +    } else {
1778 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1779 +    }
1780 +  }  
1781 +
1782 +  void RNEMD::writeRealStdDev(int index, unsigned int bin) {
1783 +    if (!doRNEMD_) return;
1784 +    assert(index >=0 && index < ENDINDEX);
1785 +    assert(bin < nBins_);
1786 +    RealType s;
1787 +    
1788 +    data_[index].accumulator[bin]->getStdDev(s);
1789 +    
1790 +    if (! isinf(s) && ! isnan(s)) {
1791 +      rnemdFile_ << "\t" << s;
1792 +    } else{
1793 +      sprintf( painCave.errMsg,
1794 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1795 +               data_[index].title.c_str(), bin);
1796 +      painCave.isFatal = 1;
1797 +      simError();
1798 +    }    
1799 +  }
1800 +  
1801 +  void RNEMD::writeVectorStdDev(int index, unsigned int bin) {
1802 +    if (!doRNEMD_) return;
1803 +    assert(index >=0 && index < ENDINDEX);
1804 +    assert(bin < nBins_);
1805 +    Vector3d s;
1806 +    dynamic_cast<VectorAccumulator*>(data_[index].accumulator[bin])->getStdDev(s);
1807 +    if (isinf(s[0]) || isnan(s[0]) ||
1808 +        isinf(s[1]) || isnan(s[1]) ||
1809 +        isinf(s[2]) || isnan(s[2]) ) {      
1810 +      sprintf( painCave.errMsg,
1811 +               "RNEMD detected a numerical error writing: %s std. dev. for bin %d",
1812 +               data_[index].title.c_str(), bin);
1813 +      painCave.isFatal = 1;
1814 +      simError();
1815 +    } else {
1816 +      rnemdFile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
1817 +    }
1818 +  }  
1819   }
1820 +

Comparing:
trunk/src/integrators/RNEMD.cpp (property svn:keywords), Revision 1350 by gezelter, Thu May 21 18:56:45 2009 UTC vs.
trunk/src/rnemd/RNEMD.cpp (property svn:keywords), Revision 1789 by gezelter, Wed Aug 29 20:52:19 2012 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines