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