| 1 | gezelter | 1329 | /* | 
| 2 |  |  | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 |  |  | * | 
| 4 |  |  | * The University of Notre Dame grants you ("Licensee") a | 
| 5 |  |  | * non-exclusive, royalty free, license to use, modify and | 
| 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 | 
| 19 |  |  | *    notice, this list of conditions and the following disclaimer. | 
| 20 |  |  | * | 
| 21 |  |  | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 |  |  | *    notice, this list of conditions and the following disclaimer in the | 
| 23 |  |  | *    documentation and/or other materials provided with the | 
| 24 |  |  | *    distribution. | 
| 25 |  |  | * | 
| 26 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 27 |  |  | * kind. All express or implied conditions, representations and | 
| 28 |  |  | * warranties, including any implied warranty of merchantability, | 
| 29 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 32 |  |  | * using, modifying or distributing the software or its | 
| 33 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 34 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 36 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 37 |  |  | * arising out of the use of or inability to use software, even if the | 
| 38 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 39 |  |  | * such damages. | 
| 40 |  |  | */ | 
| 41 |  |  |  | 
| 42 |  |  | #include "integrators/RNEMD.hpp" | 
| 43 | gezelter | 1332 | #include "math/Vector3.hpp" | 
| 44 | gezelter | 1329 | #include "math/SquareMatrix3.hpp" | 
| 45 |  |  | #include "primitives/Molecule.hpp" | 
| 46 |  |  | #include "primitives/StuntDouble.hpp" | 
| 47 | gezelter | 1332 | #include "utils/OOPSEConstant.hpp" | 
| 48 |  |  | #include "utils/Tuple.hpp" | 
| 49 | gezelter | 1329 |  | 
| 50 |  |  | #ifndef IS_MPI | 
| 51 |  |  | #include "math/SeqRandNumGen.hpp" | 
| 52 |  |  | #else | 
| 53 |  |  | #include "math/ParallelRandNumGen.hpp" | 
| 54 |  |  | #endif | 
| 55 |  |  |  | 
| 56 |  |  | /* Remove me after testing*/ | 
| 57 |  |  | /* | 
| 58 |  |  | #include <cstdio> | 
| 59 |  |  | #include <iostream> | 
| 60 |  |  | */ | 
| 61 |  |  | /*End remove me*/ | 
| 62 |  |  |  | 
| 63 |  |  | namespace oopse { | 
| 64 |  |  |  | 
| 65 | gezelter | 1334 | RNEMD::RNEMD(SimInfo* info) : info_(info), evaluator_(info), seleMan_(info), usePeriodicBoundaryConditions_(info->getSimParams()->getUsePeriodicBoundaryConditions()) { | 
| 66 | gezelter | 1329 |  | 
| 67 |  |  | int seedValue; | 
| 68 |  |  | Globals * simParams = info->getSimParams(); | 
| 69 | skuang | 1330 |  | 
| 70 |  |  | stringToEnumMap_["Kinetic"] = rnemdKinetic; | 
| 71 |  |  | stringToEnumMap_["Px"] = rnemdPx; | 
| 72 |  |  | stringToEnumMap_["Py"] = rnemdPy; | 
| 73 |  |  | stringToEnumMap_["Pz"] = rnemdPz; | 
| 74 |  |  | stringToEnumMap_["Unknown"] = rnemdUnknown; | 
| 75 |  |  |  | 
| 76 | gezelter | 1331 | rnemdObjectSelection_ = simParams->getRNEMD_objectSelection(); | 
| 77 |  |  |  | 
| 78 |  |  | std::cerr << "calling  evaluator with " << rnemdObjectSelection_ << "\n"; | 
| 79 |  |  | evaluator_.loadScriptString(rnemdObjectSelection_); | 
| 80 |  |  | std::cerr << "done"; | 
| 81 |  |  |  | 
| 82 | skuang | 1330 | const std::string st = simParams->getRNEMD_swapType(); | 
| 83 |  |  |  | 
| 84 |  |  | std::map<std::string, RNEMDTypeEnum>::iterator i; | 
| 85 |  |  | i = stringToEnumMap_.find(st); | 
| 86 |  |  | rnemdType_  = (i == stringToEnumMap_.end()) ? RNEMD::rnemdUnknown : i->second; | 
| 87 |  |  |  | 
| 88 |  |  | set_RNEMD_swapTime(simParams->getRNEMD_swapTime()); | 
| 89 |  |  | set_RNEMD_nBins(simParams->getRNEMD_nBins()); | 
| 90 |  |  | exchangeSum_ = 0.0; | 
| 91 | gezelter | 1329 |  | 
| 92 |  |  | #ifndef IS_MPI | 
| 93 |  |  | if (simParams->haveSeed()) { | 
| 94 |  |  | seedValue = simParams->getSeed(); | 
| 95 |  |  | randNumGen_ = new SeqRandNumGen(seedValue); | 
| 96 |  |  | }else { | 
| 97 |  |  | randNumGen_ = new SeqRandNumGen(); | 
| 98 |  |  | } | 
| 99 |  |  | #else | 
| 100 |  |  | if (simParams->haveSeed()) { | 
| 101 |  |  | seedValue = simParams->getSeed(); | 
| 102 |  |  | randNumGen_ = new ParallelRandNumGen(seedValue); | 
| 103 |  |  | }else { | 
| 104 |  |  | randNumGen_ = new ParallelRandNumGen(); | 
| 105 |  |  | } | 
| 106 |  |  | #endif | 
| 107 |  |  | } | 
| 108 |  |  |  | 
| 109 |  |  | RNEMD::~RNEMD() { | 
| 110 |  |  | delete randNumGen_; | 
| 111 |  |  | } | 
| 112 | skuang | 1330 |  | 
| 113 | gezelter | 1329 | void RNEMD::doSwap() { | 
| 114 |  |  | std::cerr << "in RNEMD!\n"; | 
| 115 | skuang | 1330 | std::cerr << "nBins = " << nBins_ << "\n"; | 
| 116 | gezelter | 1332 | int midBin = nBins_ / 2; | 
| 117 |  |  | std::cerr << "midBin = " << midBin << "\n"; | 
| 118 | skuang | 1330 | std::cerr << "swapTime = " << swapTime_ << "\n"; | 
| 119 |  |  | std::cerr << "exchangeSum = " << exchangeSum_ << "\n"; | 
| 120 |  |  | std::cerr << "swapType = " << rnemdType_ << "\n"; | 
| 121 | gezelter | 1331 | std::cerr << "selection = " << rnemdObjectSelection_ << "\n"; | 
| 122 |  |  |  | 
| 123 | gezelter | 1332 | Snapshot* currentSnap_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 124 |  |  | Mat3x3d hmat = currentSnap_->getHmat(); | 
| 125 |  |  |  | 
| 126 | gezelter | 1333 | std::cerr << "hmat = " << hmat << "\n"; | 
| 127 |  |  |  | 
| 128 | gezelter | 1331 | seleMan_.setSelectionSet(evaluator_.evaluate()); | 
| 129 |  |  |  | 
| 130 |  |  | std::cerr << "selectionCount = " << seleMan_.getSelectionCount() << "\n\n"; | 
| 131 |  |  |  | 
| 132 | gezelter | 1333 | int selei; | 
| 133 | gezelter | 1331 | StuntDouble* sd; | 
| 134 | gezelter | 1333 | int idx; | 
| 135 | gezelter | 1331 |  | 
| 136 | gezelter | 1332 | std::vector<tuple3<RealType, int, StuntDouble* > > endSlice; | 
| 137 |  |  | std::vector<tuple3<RealType, int, StuntDouble* > > midSlice; | 
| 138 |  |  |  | 
| 139 | gezelter | 1333 | for (sd = seleMan_.beginSelected(selei); sd != NULL; | 
| 140 |  |  | sd = seleMan_.nextSelected(selei)) { | 
| 141 | gezelter | 1332 |  | 
| 142 | gezelter | 1333 | idx = sd->getLocalIndex(); | 
| 143 |  |  |  | 
| 144 | gezelter | 1331 | Vector3d pos = sd->getPos(); | 
| 145 | gezelter | 1332 |  | 
| 146 |  |  | // wrap the stuntdouble's position back into the box: | 
| 147 |  |  |  | 
| 148 | gezelter | 1331 | if (usePeriodicBoundaryConditions_) | 
| 149 | gezelter | 1332 | currentSnap_->wrapVector(pos); | 
| 150 |  |  |  | 
| 151 |  |  | // which bin is this stuntdouble in? | 
| 152 | gezelter | 1334 | // wrapped positions are in the range [-0.5*hmat(2,2), +0.5*hmat(2,2)] | 
| 153 | gezelter | 1332 |  | 
| 154 | gezelter | 1334 | int binNo = midBin + int(nBins_ * (pos.z()) / hmat(2,2)); | 
| 155 | gezelter | 1332 |  | 
| 156 | gezelter | 1334 | std::cerr << "pos.z() = " << pos.z() << " bin = " << binNo << "\n"; | 
| 157 | gezelter | 1333 |  | 
| 158 | gezelter | 1332 | // if we're in bin 0 or the middleBin | 
| 159 |  |  | if (binNo == 0 || binNo == midBin) { | 
| 160 |  |  |  | 
| 161 |  |  | RealType mass = sd->getMass(); | 
| 162 |  |  | Vector3d vel = sd->getVel(); | 
| 163 |  |  | RealType value; | 
| 164 |  |  |  | 
| 165 |  |  | switch(rnemdType_) { | 
| 166 |  |  | case rnemdKinetic : | 
| 167 |  |  |  | 
| 168 |  |  | value = mass * (vel[0]*vel[0] + vel[1]*vel[1] + | 
| 169 |  |  | vel[2]*vel[2]); | 
| 170 |  |  |  | 
| 171 |  |  | if (sd->isDirectional()) { | 
| 172 |  |  | Vector3d angMom = sd->getJ(); | 
| 173 |  |  | Mat3x3d I = sd->getI(); | 
| 174 |  |  |  | 
| 175 |  |  | if (sd->isLinear()) { | 
| 176 |  |  | int i = sd->linearAxis(); | 
| 177 |  |  | int j = (i + 1) % 3; | 
| 178 |  |  | int k = (i + 2) % 3; | 
| 179 |  |  | value += angMom[j] * angMom[j] / I(j, j) + | 
| 180 |  |  | angMom[k] * angMom[k] / I(k, k); | 
| 181 |  |  | } else { | 
| 182 |  |  | value += angMom[0]*angMom[0]/I(0, 0) | 
| 183 |  |  | + angMom[1]*angMom[1]/I(1, 1) | 
| 184 |  |  | + angMom[2]*angMom[2]/I(2, 2); | 
| 185 |  |  | } | 
| 186 |  |  | } | 
| 187 |  |  | value = value * 0.5 / OOPSEConstant::energyConvert; | 
| 188 |  |  | break; | 
| 189 |  |  | case rnemdPx : | 
| 190 |  |  | value = mass * vel[0]; | 
| 191 |  |  | break; | 
| 192 |  |  | case rnemdPy : | 
| 193 |  |  | value = mass * vel[1]; | 
| 194 |  |  | break; | 
| 195 |  |  | case rnemdPz : | 
| 196 |  |  | value = mass * vel[2]; | 
| 197 |  |  | break; | 
| 198 |  |  | case rnemdUnknown : | 
| 199 |  |  | default : | 
| 200 |  |  | break; | 
| 201 |  |  | } | 
| 202 |  |  |  | 
| 203 |  |  | if (binNo == 0) | 
| 204 |  |  | endSlice.push_back( make_tuple3(value, idx, sd)); | 
| 205 |  |  |  | 
| 206 |  |  | if (binNo == midBin) | 
| 207 |  |  | midSlice.push_back( make_tuple3(value, idx, sd)); | 
| 208 |  |  | } | 
| 209 | gezelter | 1331 | } | 
| 210 | gezelter | 1334 |  | 
| 211 |  |  | // find smallest value in endSlice: | 
| 212 |  |  | std::sort(endSlice.begin(), endSlice.end()); | 
| 213 |  |  | RealType min_val = endSlice[0].first; | 
| 214 |  |  | int min_idx = endSlice[0].second; | 
| 215 |  |  | StuntDouble* min_sd = endSlice[0].third; | 
| 216 |  |  |  | 
| 217 |  |  | std::cerr << "smallest value = " << min_val << " idx = " << min_idx << "\n"; | 
| 218 |  |  |  | 
| 219 |  |  | // find largest value in midSlice: | 
| 220 |  |  | std::sort(midSlice.rbegin(), midSlice.rend()); | 
| 221 |  |  | RealType max_val = midSlice[0].first; | 
| 222 |  |  | int max_idx = midSlice[0].second; | 
| 223 |  |  | StuntDouble* max_sd = midSlice[0].third; | 
| 224 |  |  |  | 
| 225 |  |  | std::cerr << "largest value = " << max_val << " idx = " << max_idx << "\n"; | 
| 226 |  |  |  | 
| 227 |  |  | } | 
| 228 |  |  | } |