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/Rattle.hpp" |
44 |
#include "primitives/Molecule.hpp" |
45 |
#include "utils/simError.h" |
46 |
namespace OpenMD { |
47 |
|
48 |
Rattle::Rattle(SimInfo* info) : info_(info), maxConsIteration_(10), |
49 |
consTolerance_(1.0e-6), doRattle_(false), |
50 |
currConstraintTime_(0.0) { |
51 |
|
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 |
"Rattle Error: dt is not set\n"); |
62 |
painCave.isFatal = 1; |
63 |
simError(); |
64 |
} |
65 |
|
66 |
currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot(); |
67 |
if (simParams->haveConstraintTime()){ |
68 |
constraintTime_ = simParams->getConstraintTime(); |
69 |
} else { |
70 |
constraintTime_ = simParams->getStatusTime(); |
71 |
} |
72 |
|
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 |
|
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 |
} |
113 |
|
114 |
void Rattle::doConstraint(ConstraintPairFuncPtr func) { |
115 |
if (!doRattle_) return; |
116 |
|
117 |
Molecule* mol; |
118 |
SimInfo::MoleculeIterator mi; |
119 |
ConstraintElem* consElem; |
120 |
Molecule::ConstraintElemIterator cei; |
121 |
ConstraintPair* consPair; |
122 |
Molecule::ConstraintPairIterator cpi; |
123 |
|
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; |
138 |
|
139 |
//loop over every constraint pair |
140 |
|
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); |
150 |
|
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 |
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; |
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++; |
192 |
}//end while |
193 |
|
194 |
if (!done){ |
195 |
sprintf(painCave.errMsg, |
196 |
"Constraint failure in Rattle::constrainA, " |
197 |
"too many iterations: %d\n", |
198 |
iteration); |
199 |
painCave.isFatal = 1; |
200 |
simError(); |
201 |
} |
202 |
} |
203 |
|
204 |
int Rattle::constraintPairA(ConstraintPair* consPair){ |
205 |
|
206 |
ConstraintElem* consElem1 = consPair->getConsElem1(); |
207 |
ConstraintElem* consElem2 = consPair->getConsElem2(); |
208 |
|
209 |
Vector3d posA = consElem1->getPos(); |
210 |
Vector3d posB = consElem2->getPos(); |
211 |
|
212 |
Vector3d pab = posA -posB; |
213 |
|
214 |
//periodic boundary condition |
215 |
|
216 |
currentSnapshot_->wrapVector(pab); |
217 |
|
218 |
RealType pabsq = pab.lengthSquare(); |
219 |
|
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(); |
228 |
|
229 |
Vector3d rab = oldPosA - oldPosB; |
230 |
|
231 |
currentSnapshot_->wrapVector(rab); |
232 |
|
233 |
RealType rpab = dot(rab, pab); |
234 |
RealType rpabsq = rpab * rpab; |
235 |
|
236 |
if (rpabsq < (rabsq * -diffsq)){ |
237 |
return consFail; |
238 |
} |
239 |
|
240 |
RealType rma = 1.0 / consElem1->getMass(); |
241 |
RealType rmb = 1.0 / consElem2->getMass(); |
242 |
|
243 |
RealType gab = diffsq / (2.0 * (rma + rmb) * rpab); |
244 |
|
245 |
Vector3d delta = rab * gab; |
246 |
|
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); |
254 |
|
255 |
delta /= dt_; |
256 |
|
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 |
// report the constraint force back to the constraint pair: |
268 |
consPair->setConstraintForce(gab); |
269 |
return consSuccess; |
270 |
} |
271 |
else |
272 |
return consAlready; |
273 |
|
274 |
} |
275 |
|
276 |
|
277 |
int Rattle::constraintPairB(ConstraintPair* consPair){ |
278 |
ConstraintElem* consElem1 = consPair->getConsElem1(); |
279 |
ConstraintElem* consElem2 = consPair->getConsElem2(); |
280 |
|
281 |
|
282 |
Vector3d velA = consElem1->getVel(); |
283 |
Vector3d velB = consElem2->getVel(); |
284 |
|
285 |
Vector3d dv = velA - velB; |
286 |
|
287 |
Vector3d posA = consElem1->getPos(); |
288 |
Vector3d posB = consElem2->getPos(); |
289 |
|
290 |
Vector3d rab = posA - posB; |
291 |
|
292 |
currentSnapshot_->wrapVector(rab); |
293 |
|
294 |
RealType rma = 1.0 / consElem1->getMass(); |
295 |
RealType rmb = 1.0 / consElem2->getMass(); |
296 |
|
297 |
RealType rvab = dot(rab, dv); |
298 |
|
299 |
RealType gab = -rvab / ((rma + rmb) * consPair->getConsDistSquare()); |
300 |
|
301 |
if (fabs(gab) > consTolerance_){ |
302 |
Vector3d delta = rab * gab; |
303 |
|
304 |
velA += rma * delta; |
305 |
consElem1->setVel(velA); |
306 |
|
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 |
} |
318 |
|
319 |
} |