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