ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/constraints/Rattle.cpp
Revision: 2204
Committed: Fri Apr 15 22:04:00 2005 UTC (19 years, 2 months ago) by gezelter
File size: 7896 byte(s)
Log Message:
xemacs has been drafted to perform our indentation services

File Contents

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

Properties

Name Value
svn:executable *