| 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), 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(); | 
| 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(); | 
| 57 | > | sprintf(painCave.errMsg, | 
| 58 | > | "Integrator Error: dt is not set\n"); | 
| 59 | > | painCave.isFatal = 1; | 
| 60 | > | simError(); | 
| 61 |  | } | 
| 57 | – |  | 
| 62 |  | currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); | 
| 63 | < | } | 
| 63 | > | } | 
| 64 |  |  | 
| 65 | < | void Rattle::constraintA() { | 
| 66 | < | if (info_->getNConstraints() > 0) { | 
| 67 | < | doConstraint(&Rattle::constraintPairA); | 
| 68 | < | } | 
| 69 | < | } | 
| 70 | < | void Rattle::constraintB() { | 
| 71 | < | if (info_->getNConstraints() > 0) { | 
| 72 | < | doConstraint(&Rattle::constraintPairB); | 
| 69 | < | } | 
| 70 | < | } | 
| 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) { | 
| 74 | > | void Rattle::doConstraint(ConstraintPairFuncPtr func) { | 
| 75 | > | if (!doRattle_) return; | 
| 76 | > |  | 
| 77 |  | Molecule* mol; | 
| 78 |  | SimInfo::MoleculeIterator mi; | 
| 79 |  | ConstraintElem* consElem; | 
| 81 |  | ConstraintPair* consPair; | 
| 82 |  | Molecule::ConstraintPairIterator cpi; | 
| 83 |  |  | 
| 84 | < | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 85 | < | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; consElem = mol->nextConstraintElem(cei)) { | 
| 86 | < | consElem->setMoved(true); | 
| 87 | < | consElem->setMoving(false); | 
| 88 | < | } | 
| 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; | 
| 97 | > | done = true; | 
| 98 |  |  | 
| 99 | < | //loop over every constraint pair | 
| 99 | > | //loop over every constraint pair | 
| 100 |  |  | 
| 101 | < | for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { | 
| 102 | < | for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; consPair = mol->nextConstraintPair(cpi)) { | 
| 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); | 
| 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, Constraint Fail\n"); | 
| 115 | < | painCave.isFatal = 1; | 
| 116 | < | simError(); | 
| 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 move the elements | 
| 128 | < | break; | 
| 129 | < | default: | 
| 130 | < | sprintf(painCave.errMsg, "ConstraintAlgorithm::doConstrain() Error: unrecognized status"); | 
| 131 | < | painCave.isFatal = 1; | 
| 132 | < | simError(); | 
| 133 | < | break; | 
| 134 | < | } | 
| 135 | < | } | 
| 136 | < | } | 
| 137 | < | }//end for(iter->first()) | 
| 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; mol = info_->nextMolecule(mi)) { | 
| 143 | < | for (consElem = mol->beginConstraintElem(cei); consElem != NULL; consElem = mol->nextConstraintElem(cei)) { | 
| 144 | < | consElem->setMoved(consElem->getMoving()); | 
| 145 | < | consElem->setMoving(false); | 
| 146 | < | } | 
| 147 | < | } | 
| 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++; | 
| 151 | > | iteration++; | 
| 152 |  | }//end while | 
| 153 |  |  | 
| 154 |  | if (!done){ | 
| 155 |  | sprintf(painCave.errMsg, | 
| 156 | < | "Constraint failure in Rattle::constrainA, too many iterations: %d\n", | 
| 156 | > | "Constraint failure in Rattle::constrainA, " | 
| 157 | > | "too many iterations: %d\n", | 
| 158 |  | iteration); | 
| 159 |  | painCave.isFatal = 1; | 
| 160 |  | simError(); | 
| 161 |  | } | 
| 162 | < | } | 
| 162 | > | } | 
| 163 |  |  | 
| 164 | < | int Rattle::constraintPairA(ConstraintPair* consPair){ | 
| 151 | < | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 152 | < | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 164 | > | int Rattle::constraintPairA(ConstraintPair* consPair){ | 
| 165 |  |  | 
| 166 | < | Vector3d posA = consElem1->getPos(); | 
| 167 | < | Vector3d posB = consElem2->getPos(); | 
| 166 | > | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 167 | > | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 168 |  |  | 
| 169 | < | Vector3d pab = posA -posB; | 
| 169 | > | Vector3d posA = consElem1->getPos(); | 
| 170 | > | Vector3d posB = consElem2->getPos(); | 
| 171 |  |  | 
| 172 | < | //periodic boundary condition | 
| 172 | > | Vector3d pab = posA -posB; | 
| 173 |  |  | 
| 174 | < | currentSnapshot_->wrapVector(pab); | 
| 174 | > | //periodic boundary condition | 
| 175 |  |  | 
| 176 | < | double pabsq = pab.lengthSquare(); | 
| 176 | > | currentSnapshot_->wrapVector(pab); | 
| 177 |  |  | 
| 178 | < | double rabsq = consPair->getConsDistSquare(); | 
| 166 | < | double diffsq = rabsq - pabsq; | 
| 178 | > | RealType pabsq = pab.lengthSquare(); | 
| 179 |  |  | 
| 180 | < | // the original rattle code from alan tidesley | 
| 181 | < | if (fabs(diffsq) > (consTolerance_ * rabsq * 2)){ | 
| 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(); | 
| 186 | > | Vector3d oldPosA = consElem1->getPrevPos(); | 
| 187 | > | Vector3d oldPosB = consElem2->getPrevPos(); | 
| 188 |  |  | 
| 189 | < | Vector3d rab = oldPosA - oldPosB; | 
| 189 | > | Vector3d rab = oldPosA - oldPosB; | 
| 190 |  |  | 
| 191 | < | currentSnapshot_->wrapVector(rab); | 
| 191 | > | currentSnapshot_->wrapVector(rab); | 
| 192 |  |  | 
| 193 | < | double rpab = dot(rab, pab); | 
| 194 | < | double rpabsq = rpab * rpab; | 
| 193 | > | RealType rpab = dot(rab, pab); | 
| 194 | > | RealType rpabsq = rpab * rpab; | 
| 195 |  |  | 
| 196 | < | if (rpabsq < (rabsq * -diffsq)){ | 
| 197 | < | return consFail; | 
| 198 | < | } | 
| 196 | > | if (rpabsq < (rabsq * -diffsq)){ | 
| 197 | > | return consFail; | 
| 198 | > | } | 
| 199 |  |  | 
| 200 | < | double rma = 1.0 / consElem1->getMass(); | 
| 201 | < | double rmb = 1.0 / consElem2->getMass(); | 
| 200 | > | RealType rma = 1.0 / consElem1->getMass(); | 
| 201 | > | RealType rmb = 1.0 / consElem2->getMass(); | 
| 202 |  |  | 
| 203 | < | double gab = diffsq / (2.0 * (rma + rmb) * rpab); | 
| 203 | > | RealType gab = diffsq / (2.0 * (rma + rmb) * rpab); | 
| 204 |  |  | 
| 205 | < | Vector3d delta = rab * gab; | 
| 205 | > | Vector3d delta = rab * gab; | 
| 206 |  |  | 
| 207 | < | //set atom1's position | 
| 208 | < | posA += rma * delta; | 
| 209 | < | consElem1->setPos(posA); | 
| 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); | 
| 211 | > | //set atom2's position | 
| 212 | > | posB -= rmb * delta; | 
| 213 | > | consElem2->setPos(posB); | 
| 214 |  |  | 
| 215 | < | delta /= dt_; | 
| 215 | > | delta /= dt_; | 
| 216 |  |  | 
| 217 | < | //set atom1's velocity | 
| 218 | < | Vector3d velA = consElem1->getVel(); | 
| 219 | < | velA += rma * delta; | 
| 220 | < | consElem1->setVel(velA); | 
| 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 | < | return consSuccess; | 
| 228 | < | } | 
| 229 | < | else | 
| 230 | < | return consAlready; | 
| 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 | < | } | 
| 234 | > | } | 
| 235 |  |  | 
| 236 |  |  | 
| 237 | < | int Rattle::constraintPairB(ConstraintPair* consPair){ | 
| 237 | > | int Rattle::constraintPairB(ConstraintPair* consPair){ | 
| 238 |  | ConstraintElem* consElem1 = consPair->getConsElem1(); | 
| 239 |  | ConstraintElem* consElem2 = consPair->getConsElem2(); | 
| 240 |  |  | 
| 251 |  |  | 
| 252 |  | currentSnapshot_->wrapVector(rab); | 
| 253 |  |  | 
| 254 | < | double rma = 1.0 / consElem1->getMass(); | 
| 255 | < | double rmb = 1.0 / consElem2->getMass(); | 
| 254 | > | RealType rma = 1.0 / consElem1->getMass(); | 
| 255 | > | RealType rmb = 1.0 / consElem2->getMass(); | 
| 256 |  |  | 
| 257 | < | double rvab = dot(rab, dv); | 
| 257 | > | RealType rvab = dot(rab, dv); | 
| 258 |  |  | 
| 259 | < | double gab = -rvab / ((rma + rmb) * consPair->getConsDistSquare()); | 
| 259 | > | RealType gab = -rvab / ((rma + rmb) * consPair->getConsDistSquare()); | 
| 260 |  |  | 
| 261 |  | if (fabs(gab) > consTolerance_){ | 
| 262 |  | Vector3d delta = rab * gab; | 
| 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 |  | } |