| 1 | < | /* | 
| 1 | > | /* | 
| 2 |  | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 |  | * | 
| 4 |  | * The University of Notre Dame grants you ("Licensee") a | 
| 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. | 
| 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, 234107 (2008). | 
| 39 | + | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | + | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 |  | */ | 
| 42 |  |  | 
| 43 |  | #include "constraints/Rattle.hpp" | 
| 44 |  | #include "primitives/Molecule.hpp" | 
| 45 |  | #include "utils/simError.h" | 
| 46 | < | namespace oopse { | 
| 46 | > | namespace OpenMD { | 
| 47 |  |  | 
| 48 | < | Rattle::Rattle(SimInfo* info) : info_(info), maxConsIteration_(10), consTolerance_(1.0e-6) { | 
| 48 | > | Rattle::Rattle(SimInfo* info) : info_(info), maxConsIteration_(10), | 
| 49 | > | consTolerance_(1.0e-6), doRattle_(false), | 
| 50 | > | currConstraintTime_(0.0) { | 
| 51 |  |  | 
| 52 | < | if (info_->getSimParams()->haveDt()) { | 
| 53 | < | dt_ = info_->getSimParams()->getDt(); | 
| 52 | > | if (info_->getNGlobalConstraints() > 0) | 
| 53 | > | doRattle_ = true; | 
| 54 | > |  | 
| 55 | > | if (!doRattle_) return; | 
| 56 | > |  | 
| 57 | > | Globals* simParams = info_->getSimParams(); | 
| 58 | > |  | 
| 59 | > | if (simParams->haveDt()) { | 
| 60 | > | dt_ = simParams->getDt(); | 
| 61 |  | } else { | 
| 62 | < | sprintf(painCave.errMsg, | 
| 63 | < | "Integrator Error: dt is not set\n"); | 
| 64 | < | painCave.isFatal = 1; | 
| 65 | < | simError(); | 
| 62 | > | sprintf(painCave.errMsg, | 
| 63 | > | "Rattle Error: dt is not set\n"); | 
| 64 | > | painCave.isFatal = 1; | 
| 65 | > | simError(); | 
| 66 |  | } | 
| 67 | < |  | 
| 67 | > |  | 
| 68 |  | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 69 | < | } | 
| 69 | > | if (simParams->haveConstraintTime()){ | 
| 70 | > | constraintTime_ = simParams->getConstraintTime(); | 
| 71 | > | } else { | 
| 72 | > | constraintTime_ = simParams->getStatusTime(); | 
| 73 | > | } | 
| 74 |  |  | 
| 75 | < | void Rattle::constraintA() { | 
| 76 | < | if (info_->getNConstraints() > 0) { | 
| 77 | < | doConstraint(&Rattle::constraintPairA); | 
| 75 | > | constraintOutputFile_ = getPrefix(info_->getFinalConfigFileName()) + | 
| 76 | > | ".constraintForces"; | 
| 77 | > |  | 
| 78 | > |  | 
| 79 | > | // create ConstraintWriter | 
| 80 | > | constraintWriter_ = new ConstraintWriter(info_, | 
| 81 | > | constraintOutputFile_.c_str()); | 
| 82 | > |  | 
| 83 | > | if (!constraintWriter_){ | 
| 84 | > | sprintf(painCave.errMsg, "Failed to create ConstraintWriter\n"); | 
| 85 | > | painCave.isFatal = 1; | 
| 86 | > | simError(); | 
| 87 |  | } | 
| 88 | < | } | 
| 89 | < | void Rattle::constraintB() { | 
| 90 | < | if (info_->getNConstraints() > 0) { | 
| 91 | < | doConstraint(&Rattle::constraintPairB); | 
| 88 | > | } | 
| 89 | > |  | 
| 90 | > | void Rattle::constraintA() { | 
| 91 | > | if (!doRattle_) return; | 
| 92 | > | doConstraint(&Rattle::constraintPairA); | 
| 93 | > | } | 
| 94 | > | void Rattle::constraintB() { | 
| 95 | > | if (!doRattle_) return; | 
| 96 | > | doConstraint(&Rattle::constraintPairB); | 
| 97 | > |  | 
| 98 | > | if (currentSnapshot_->getTime() >= currConstraintTime_){ | 
| 99 | > | Molecule* mol; | 
| 100 | > | SimInfo::MoleculeIterator mi; | 
| 101 | > | ConstraintPair* consPair; | 
| 102 | > | Molecule::ConstraintPairIterator cpi; | 
| 103 | > | std::list<ConstraintPair*> constraints; | 
| 104 | > | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 105 | > | mol = info_->nextMolecule(mi)) { | 
| 106 | > | for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; | 
| 107 | > | consPair = mol->nextConstraintPair(cpi)) { | 
| 108 | > |  | 
| 109 | > | constraints.push_back(consPair); | 
| 110 | > | } | 
| 111 | > | } | 
| 112 | > | constraintWriter_->writeConstraintForces(constraints); | 
| 113 | > | currConstraintTime_ += constraintTime_; | 
| 114 |  | } | 
| 115 | < | } | 
| 115 | > | } | 
| 116 |  |  | 
| 117 | < | void Rattle::doConstraint(ConstraintPairFuncPtr func) { | 
| 117 | > | void Rattle::doConstraint(ConstraintPairFuncPtr func) { | 
| 118 | > | if (!doRattle_) return; | 
| 119 | > |  | 
| 120 |  | Molecule* mol; | 
| 121 |  | SimInfo::MoleculeIterator mi; | 
| 122 |  | ConstraintElem* consElem; | 
| 124 |  | ConstraintPair* consPair; | 
| 125 |  | Molecule::ConstraintPairIterator cpi; | 
| 126 |  |  | 
| 127 | < | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 128 | < | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; consElem = mol->nextConstraintElem(cei)) { | 
| 129 | < | consElem->setMoved(true); | 
| 130 | < | consElem->setMoving(false); | 
| 131 | < | } | 
| 127 | > | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 128 | > | mol = info_->nextMolecule(mi)) { | 
| 129 | > | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; | 
| 130 | > | consElem = mol->nextConstraintElem(cei)) { | 
| 131 | > | consElem->setMoved(true); | 
| 132 | > | consElem->setMoving(false); | 
| 133 | > | } | 
| 134 |  | } | 
| 135 |  |  | 
| 136 |  | //main loop of constraint algorithm | 
| 137 |  | bool done = false; | 
| 138 |  | int iteration = 0; | 
| 139 |  | while(!done && iteration < maxConsIteration_){ | 
| 140 | < | done = true; | 
| 140 | > | done = true; | 
| 141 |  |  | 
| 142 | < | //loop over every constraint pair | 
| 142 | > | //loop over every constraint pair | 
| 143 |  |  | 
| 144 | < | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 145 | < | for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; consPair = mol->nextConstraintPair(cpi)) { | 
| 144 | > | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 145 | > | mol = info_->nextMolecule(mi)) { | 
| 146 | > | for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; | 
| 147 | > | consPair = mol->nextConstraintPair(cpi)) { | 
| 148 |  |  | 
| 149 |  |  | 
| 150 | < | //dispatch constraint algorithm | 
| 151 | < | if(consPair->isMoved()) { | 
| 152 | < | int exeStatus = (this->*func)(consPair); | 
| 150 | > | //dispatch constraint algorithm | 
| 151 | > | if(consPair->isMoved()) { | 
| 152 | > | int exeStatus = (this->*func)(consPair); | 
| 153 |  |  | 
| 154 | < | switch(exeStatus){ | 
| 155 | < | case consFail: | 
| 156 | < | sprintf(painCave.errMsg, | 
| 157 | < | "Constraint failure in Rattle::constrainA, Constraint Fail\n"); | 
| 158 | < | painCave.isFatal = 1; | 
| 159 | < | simError(); | 
| 154 | > | switch(exeStatus){ | 
| 155 | > | case consFail: | 
| 156 | > | sprintf(painCave.errMsg, | 
| 157 | > | "Constraint failure in Rattle::constrainA, " | 
| 158 | > | "Constraint Fail\n"); | 
| 159 | > | painCave.isFatal = 1; | 
| 160 | > | simError(); | 
| 161 |  |  | 
| 162 | < | break; | 
| 163 | < | case consSuccess: | 
| 164 | < | //constrain the pair by moving two elements | 
| 165 | < | done = false; | 
| 166 | < | consPair->getConsElem1()->setMoving(true); | 
| 167 | < | consPair->getConsElem2()->setMoving(true); | 
| 168 | < | break; | 
| 169 | < | case consAlready: | 
| 170 | < | //current pair is already constrained, do not need to move the elements | 
| 171 | < | break; | 
| 172 | < | default: | 
| 173 | < | sprintf(painCave.errMsg, "ConstraintAlgorithm::doConstrain() Error: unrecognized status"); | 
| 174 | < | painCave.isFatal = 1; | 
| 175 | < | simError(); | 
| 176 | < | break; | 
| 177 | < | } | 
| 178 | < | } | 
| 179 | < | } | 
| 180 | < | }//end for(iter->first()) | 
| 162 | > | break; | 
| 163 | > | case consSuccess: | 
| 164 | > | // constrain the pair by moving two elements | 
| 165 | > | done = false; | 
| 166 | > | consPair->getConsElem1()->setMoving(true); | 
| 167 | > | consPair->getConsElem2()->setMoving(true); | 
| 168 | > | break; | 
| 169 | > | case consAlready: | 
| 170 | > | // current pair is already constrained, do not need to | 
| 171 | > | // move the elements | 
| 172 | > | break; | 
| 173 | > | default: | 
| 174 | > | sprintf(painCave.errMsg, "ConstraintAlgorithm::doConstraint() " | 
| 175 | > | "Error: unrecognized status"); | 
| 176 | > | painCave.isFatal = 1; | 
| 177 | > | simError(); | 
| 178 | > | break; | 
| 179 | > | } | 
| 180 | > | } | 
| 181 | > | } | 
| 182 | > | }//end for(iter->first()) | 
| 183 |  |  | 
| 184 |  |  | 
| 185 | < | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 186 | < | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; consElem = mol->nextConstraintElem(cei)) { | 
| 187 | < | consElem->setMoved(consElem->getMoving()); | 
| 188 | < | consElem->setMoving(false); | 
| 189 | < | } | 
| 190 | < | } | 
| 185 | > | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 186 | > | mol = info_->nextMolecule(mi)) { | 
| 187 | > | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; | 
| 188 | > | consElem = mol->nextConstraintElem(cei)) { | 
| 189 | > | consElem->setMoved(consElem->getMoving()); | 
| 190 | > | consElem->setMoving(false); | 
| 191 | > | } | 
| 192 | > | } | 
| 193 |  |  | 
| 194 | < | iteration++; | 
| 194 | > | iteration++; | 
| 195 |  | }//end while | 
| 196 |  |  | 
| 197 |  | if (!done){ | 
| 198 |  | sprintf(painCave.errMsg, | 
| 199 | < | "Constraint failure in Rattle::constrainA, too many iterations: %d\n", | 
| 199 | > | "Constraint failure in Rattle::constrainA, " | 
| 200 | > | "too many iterations: %d\n", | 
| 201 |  | iteration); | 
| 202 |  | painCave.isFatal = 1; | 
| 203 |  | simError(); | 
| 204 |  | } | 
| 205 | < | } | 
| 205 | > | } | 
| 206 |  |  | 
| 207 | < | int Rattle::constraintPairA(ConstraintPair* consPair){ | 
| 151 | < | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 152 | < | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 207 | > | int Rattle::constraintPairA(ConstraintPair* consPair){ | 
| 208 |  |  | 
| 209 | < | Vector3d posA = consElem1->getPos(); | 
| 210 | < | Vector3d posB = consElem2->getPos(); | 
| 209 | > | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 210 | > | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 211 |  |  | 
| 212 | < | Vector3d pab = posA -posB; | 
| 212 | > | Vector3d posA = consElem1->getPos(); | 
| 213 | > | Vector3d posB = consElem2->getPos(); | 
| 214 |  |  | 
| 215 | < | //periodic boundary condition | 
| 215 | > | Vector3d pab = posA -posB; | 
| 216 |  |  | 
| 217 | < | currentSnapshot_->wrapVector(pab); | 
| 217 | > | //periodic boundary condition | 
| 218 |  |  | 
| 219 | < | double pabsq = pab.lengthSquare(); | 
| 219 | > | currentSnapshot_->wrapVector(pab); | 
| 220 |  |  | 
| 221 | < | double rabsq = consPair->getConsDistSquare(); | 
| 166 | < | double diffsq = rabsq - pabsq; | 
| 221 | > | RealType pabsq = pab.lengthSquare(); | 
| 222 |  |  | 
| 223 | < | // the original rattle code from alan tidesley | 
| 224 | < | if (fabs(diffsq) > (consTolerance_ * rabsq * 2)){ | 
| 223 | > | RealType rabsq = consPair->getConsDistSquare(); | 
| 224 | > | RealType diffsq = rabsq - pabsq; | 
| 225 | > |  | 
| 226 | > | // the original rattle code from alan tidesley | 
| 227 | > | if (fabs(diffsq) > (consTolerance_ * rabsq * 2)){ | 
| 228 |  |  | 
| 229 | < | Vector3d oldPosA = consElem1->getPrevPos(); | 
| 230 | < | Vector3d oldPosB = consElem2->getPrevPos(); | 
| 229 | > | Vector3d oldPosA = consElem1->getPrevPos(); | 
| 230 | > | Vector3d oldPosB = consElem2->getPrevPos(); | 
| 231 |  |  | 
| 232 | < | Vector3d rab = oldPosA - oldPosB; | 
| 232 | > | Vector3d rab = oldPosA - oldPosB; | 
| 233 |  |  | 
| 234 | < | currentSnapshot_->wrapVector(rab); | 
| 234 | > | currentSnapshot_->wrapVector(rab); | 
| 235 |  |  | 
| 236 | < | double rpab = dot(rab, pab); | 
| 237 | < | double rpabsq = rpab * rpab; | 
| 236 | > | RealType rpab = dot(rab, pab); | 
| 237 | > | RealType rpabsq = rpab * rpab; | 
| 238 |  |  | 
| 239 | < | if (rpabsq < (rabsq * -diffsq)){ | 
| 240 | < | return consFail; | 
| 241 | < | } | 
| 239 | > | if (rpabsq < (rabsq * -diffsq)){ | 
| 240 | > | return consFail; | 
| 241 | > | } | 
| 242 |  |  | 
| 243 | < | double rma = 1.0 / consElem1->getMass(); | 
| 244 | < | double rmb = 1.0 / consElem2->getMass(); | 
| 243 | > | RealType rma = 1.0 / consElem1->getMass(); | 
| 244 | > | RealType rmb = 1.0 / consElem2->getMass(); | 
| 245 |  |  | 
| 246 | < | double gab = diffsq / (2.0 * (rma + rmb) * rpab); | 
| 246 | > | RealType gab = diffsq / (2.0 * (rma + rmb) * rpab); | 
| 247 |  |  | 
| 248 | < | Vector3d delta = rab * gab; | 
| 248 | > | Vector3d delta = rab * gab; | 
| 249 |  |  | 
| 250 | < | //set atom1's position | 
| 251 | < | posA += rma * delta; | 
| 252 | < | consElem1->setPos(posA); | 
| 250 | > | //set atom1's position | 
| 251 | > | posA += rma * delta; | 
| 252 | > | consElem1->setPos(posA); | 
| 253 |  |  | 
| 254 | < | //set atom2's position | 
| 255 | < | posB -= rmb * delta; | 
| 256 | < | consElem2->setPos(posB); | 
| 254 | > | //set atom2's position | 
| 255 | > | posB -= rmb * delta; | 
| 256 | > | consElem2->setPos(posB); | 
| 257 |  |  | 
| 258 | < | delta /= dt_; | 
| 258 | > | delta /= dt_; | 
| 259 |  |  | 
| 260 | < | //set atom1's velocity | 
| 261 | < | Vector3d velA = consElem1->getVel(); | 
| 262 | < | velA += rma * delta; | 
| 263 | < | consElem1->setVel(velA); | 
| 260 | > | //set atom1's velocity | 
| 261 | > | Vector3d velA = consElem1->getVel(); | 
| 262 | > | velA += rma * delta; | 
| 263 | > | consElem1->setVel(velA); | 
| 264 |  |  | 
| 265 | < | //set atom2's velocity | 
| 266 | < | Vector3d velB = consElem2->getVel(); | 
| 267 | < | velB -= rmb * delta; | 
| 268 | < | consElem2->setVel(velB); | 
| 269 | < |  | 
| 270 | < | return consSuccess; | 
| 271 | < | } | 
| 272 | < | else | 
| 273 | < | return consAlready; | 
| 265 | > | //set atom2's velocity | 
| 266 | > | Vector3d velB = consElem2->getVel(); | 
| 267 | > | velB -= rmb * delta; | 
| 268 | > | consElem2->setVel(velB); | 
| 269 | > |  | 
| 270 | > | // report the constraint force back to the constraint pair: | 
| 271 | > | consPair->setConstraintForce(gab); | 
| 272 | > | return consSuccess; | 
| 273 | > | } | 
| 274 | > | else | 
| 275 | > | return consAlready; | 
| 276 |  |  | 
| 277 | < | } | 
| 277 | > | } | 
| 278 |  |  | 
| 279 |  |  | 
| 280 | < | int Rattle::constraintPairB(ConstraintPair* consPair){ | 
| 280 | > | int Rattle::constraintPairB(ConstraintPair* consPair){ | 
| 281 |  | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 282 |  | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 283 |  |  | 
| 294 |  |  | 
| 295 |  | currentSnapshot_->wrapVector(rab); | 
| 296 |  |  | 
| 297 | < | double rma = 1.0 / consElem1->getMass(); | 
| 298 | < | double rmb = 1.0 / consElem2->getMass(); | 
| 297 | > | RealType rma = 1.0 / consElem1->getMass(); | 
| 298 | > | RealType rmb = 1.0 / consElem2->getMass(); | 
| 299 |  |  | 
| 300 | < | double rvab = dot(rab, dv); | 
| 300 | > | RealType rvab = dot(rab, dv); | 
| 301 |  |  | 
| 302 | < | double gab = -rvab / ((rma + rmb) * consPair->getConsDistSquare()); | 
| 302 | > | RealType gab = -rvab / ((rma + rmb) * consPair->getConsDistSquare()); | 
| 303 |  |  | 
| 304 |  | if (fabs(gab) > consTolerance_){ | 
| 305 |  | Vector3d delta = rab * gab; | 
| 310 |  | velB -= rmb * delta; | 
| 311 |  | consElem2->setVel(velB); | 
| 312 |  |  | 
| 313 | + | // report the constraint force back to the constraint pair: | 
| 314 | + | consPair->setConstraintForce(gab); | 
| 315 |  | return consSuccess; | 
| 316 |  | } | 
| 317 |  | else | 
| 318 |  | return consAlready; | 
| 319 |  |  | 
| 320 | < | } | 
| 320 | > | } | 
| 321 |  |  | 
| 322 |  | } |