| 1 | /* | 
| 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. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 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. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 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 OpenMD { | 
| 47 |  | 
| 48 | Rattle::Rattle(SimInfo* info) : info_(info), maxConsIteration_(10), consTolerance_(1.0e-6), doRattle_(false) { | 
| 49 |  | 
| 50 | if (info_->getNConstraints() > 0) | 
| 51 | doRattle_ = true; | 
| 52 |  | 
| 53 |  | 
| 54 | if (info_->getSimParams()->haveDt()) { | 
| 55 | dt_ = info_->getSimParams()->getDt(); | 
| 56 | } else { | 
| 57 | sprintf(painCave.errMsg, | 
| 58 | "Integrator Error: dt is not set\n"); | 
| 59 | painCave.isFatal = 1; | 
| 60 | simError(); | 
| 61 | } | 
| 62 | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 63 | } | 
| 64 |  | 
| 65 | void Rattle::constraintA() { | 
| 66 | if (!doRattle_) return; | 
| 67 | doConstraint(&Rattle::constraintPairA); | 
| 68 | } | 
| 69 | void Rattle::constraintB() { | 
| 70 | if (!doRattle_) return; | 
| 71 | doConstraint(&Rattle::constraintPairB); | 
| 72 | } | 
| 73 |  | 
| 74 | void Rattle::doConstraint(ConstraintPairFuncPtr func) { | 
| 75 | if (!doRattle_) return; | 
| 76 |  | 
| 77 | Molecule* mol; | 
| 78 | SimInfo::MoleculeIterator mi; | 
| 79 | ConstraintElem* consElem; | 
| 80 | Molecule::ConstraintElemIterator cei; | 
| 81 | ConstraintPair* consPair; | 
| 82 | Molecule::ConstraintPairIterator cpi; | 
| 83 |  | 
| 84 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 85 | mol = info_->nextMolecule(mi)) { | 
| 86 | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; | 
| 87 | consElem = mol->nextConstraintElem(cei)) { | 
| 88 | consElem->setMoved(true); | 
| 89 | consElem->setMoving(false); | 
| 90 | } | 
| 91 | } | 
| 92 |  | 
| 93 | //main loop of constraint algorithm | 
| 94 | bool done = false; | 
| 95 | int iteration = 0; | 
| 96 | while(!done && iteration < maxConsIteration_){ | 
| 97 | done = true; | 
| 98 |  | 
| 99 | //loop over every constraint pair | 
| 100 |  | 
| 101 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 102 | mol = info_->nextMolecule(mi)) { | 
| 103 | for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; | 
| 104 | consPair = mol->nextConstraintPair(cpi)) { | 
| 105 |  | 
| 106 |  | 
| 107 | //dispatch constraint algorithm | 
| 108 | if(consPair->isMoved()) { | 
| 109 | int exeStatus = (this->*func)(consPair); | 
| 110 |  | 
| 111 | switch(exeStatus){ | 
| 112 | case consFail: | 
| 113 | sprintf(painCave.errMsg, | 
| 114 | "Constraint failure in Rattle::constrainA, " | 
| 115 | "Constraint Fail\n"); | 
| 116 | painCave.isFatal = 1; | 
| 117 | simError(); | 
| 118 |  | 
| 119 | break; | 
| 120 | case consSuccess: | 
| 121 | // constrain the pair by moving two elements | 
| 122 | done = false; | 
| 123 | consPair->getConsElem1()->setMoving(true); | 
| 124 | consPair->getConsElem2()->setMoving(true); | 
| 125 | break; | 
| 126 | case consAlready: | 
| 127 | // current pair is already constrained, do not need to | 
| 128 | // move the elements | 
| 129 | break; | 
| 130 | default: | 
| 131 | sprintf(painCave.errMsg, "ConstraintAlgorithm::doConstraint() " | 
| 132 | "Error: unrecognized status"); | 
| 133 | painCave.isFatal = 1; | 
| 134 | simError(); | 
| 135 | break; | 
| 136 | } | 
| 137 | } | 
| 138 | } | 
| 139 | }//end for(iter->first()) | 
| 140 |  | 
| 141 |  | 
| 142 | for (mol = info_->beginMolecule(mi); mol != NULL; | 
| 143 | mol = info_->nextMolecule(mi)) { | 
| 144 | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; | 
| 145 | consElem = mol->nextConstraintElem(cei)) { | 
| 146 | consElem->setMoved(consElem->getMoving()); | 
| 147 | consElem->setMoving(false); | 
| 148 | } | 
| 149 | } | 
| 150 |  | 
| 151 | iteration++; | 
| 152 | }//end while | 
| 153 |  | 
| 154 | if (!done){ | 
| 155 | sprintf(painCave.errMsg, | 
| 156 | "Constraint failure in Rattle::constrainA, " | 
| 157 | "too many iterations: %d\n", | 
| 158 | iteration); | 
| 159 | painCave.isFatal = 1; | 
| 160 | simError(); | 
| 161 | } | 
| 162 | } | 
| 163 |  | 
| 164 | int Rattle::constraintPairA(ConstraintPair* consPair){ | 
| 165 |  | 
| 166 | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 167 | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 168 |  | 
| 169 | Vector3d posA = consElem1->getPos(); | 
| 170 | Vector3d posB = consElem2->getPos(); | 
| 171 |  | 
| 172 | Vector3d pab = posA -posB; | 
| 173 |  | 
| 174 | //periodic boundary condition | 
| 175 |  | 
| 176 | currentSnapshot_->wrapVector(pab); | 
| 177 |  | 
| 178 | RealType pabsq = pab.lengthSquare(); | 
| 179 |  | 
| 180 | RealType rabsq = consPair->getConsDistSquare(); | 
| 181 | RealType diffsq = rabsq - pabsq; | 
| 182 |  | 
| 183 | // the original rattle code from alan tidesley | 
| 184 | if (fabs(diffsq) > (consTolerance_ * rabsq * 2)){ | 
| 185 |  | 
| 186 | Vector3d oldPosA = consElem1->getPrevPos(); | 
| 187 | Vector3d oldPosB = consElem2->getPrevPos(); | 
| 188 |  | 
| 189 | Vector3d rab = oldPosA - oldPosB; | 
| 190 |  | 
| 191 | currentSnapshot_->wrapVector(rab); | 
| 192 |  | 
| 193 | RealType rpab = dot(rab, pab); | 
| 194 | RealType rpabsq = rpab * rpab; | 
| 195 |  | 
| 196 | if (rpabsq < (rabsq * -diffsq)){ | 
| 197 | return consFail; | 
| 198 | } | 
| 199 |  | 
| 200 | RealType rma = 1.0 / consElem1->getMass(); | 
| 201 | RealType rmb = 1.0 / consElem2->getMass(); | 
| 202 |  | 
| 203 | RealType gab = diffsq / (2.0 * (rma + rmb) * rpab); | 
| 204 |  | 
| 205 | Vector3d delta = rab * gab; | 
| 206 |  | 
| 207 | //set atom1's position | 
| 208 | posA += rma * delta; | 
| 209 | consElem1->setPos(posA); | 
| 210 |  | 
| 211 | //set atom2's position | 
| 212 | posB -= rmb * delta; | 
| 213 | consElem2->setPos(posB); | 
| 214 |  | 
| 215 | delta /= dt_; | 
| 216 |  | 
| 217 | //set atom1's velocity | 
| 218 | Vector3d velA = consElem1->getVel(); | 
| 219 | velA += rma * delta; | 
| 220 | consElem1->setVel(velA); | 
| 221 |  | 
| 222 | //set atom2's velocity | 
| 223 | Vector3d velB = consElem2->getVel(); | 
| 224 | velB -= rmb * delta; | 
| 225 | consElem2->setVel(velB); | 
| 226 |  | 
| 227 | // report the constraint force back to the constraint pair: | 
| 228 | consPair->setConstraintForce(gab); | 
| 229 | return consSuccess; | 
| 230 | } | 
| 231 | else | 
| 232 | return consAlready; | 
| 233 |  | 
| 234 | } | 
| 235 |  | 
| 236 |  | 
| 237 | int Rattle::constraintPairB(ConstraintPair* consPair){ | 
| 238 | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 239 | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 240 |  | 
| 241 |  | 
| 242 | Vector3d velA = consElem1->getVel(); | 
| 243 | Vector3d velB = consElem2->getVel(); | 
| 244 |  | 
| 245 | Vector3d dv = velA - velB; | 
| 246 |  | 
| 247 | Vector3d posA = consElem1->getPos(); | 
| 248 | Vector3d posB = consElem2->getPos(); | 
| 249 |  | 
| 250 | Vector3d rab = posA - posB; | 
| 251 |  | 
| 252 | currentSnapshot_->wrapVector(rab); | 
| 253 |  | 
| 254 | RealType rma = 1.0 / consElem1->getMass(); | 
| 255 | RealType rmb = 1.0 / consElem2->getMass(); | 
| 256 |  | 
| 257 | RealType rvab = dot(rab, dv); | 
| 258 |  | 
| 259 | RealType gab = -rvab / ((rma + rmb) * consPair->getConsDistSquare()); | 
| 260 |  | 
| 261 | if (fabs(gab) > consTolerance_){ | 
| 262 | Vector3d delta = rab * gab; | 
| 263 |  | 
| 264 | velA += rma * delta; | 
| 265 | consElem1->setVel(velA); | 
| 266 |  | 
| 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 | } | 
| 278 |  | 
| 279 | } |