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