ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/constraints/Rattle.cpp
(Generate patch)

Comparing trunk/src/constraints/Rattle.cpp (file contents):
Revision 246 by gezelter, Wed Jan 12 22:41:40 2005 UTC vs.
Revision 1983 by gezelter, Tue Apr 15 20:36:19 2014 UTC

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

Comparing trunk/src/constraints/Rattle.cpp (property svn:keywords):
Revision 246 by gezelter, Wed Jan 12 22:41:40 2005 UTC vs.
Revision 1983 by gezelter, Tue Apr 15 20:36:19 2014 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines