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

Comparing trunk/src/constraints/Rattle.cpp (property svn:keywords):
Revision 246 by gezelter, Wed Jan 12 22:41:40 2005 UTC vs.
Revision 1981 by gezelter, Mon Apr 14 18:32:51 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines