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 "FluctuatingChargeLangevin.hpp" |
44 |
#include "primitives/Molecule.hpp" |
45 |
#include "utils/simError.h" |
46 |
#include "utils/PhysicalConstants.hpp" |
47 |
|
48 |
|
49 |
namespace OpenMD { |
50 |
|
51 |
FluctuatingChargeLangevin::FluctuatingChargeLangevin(SimInfo* info) : |
52 |
FluctuatingChargePropagator(info), maxIterNum_(4), |
53 |
forceTolerance_(1e-6), |
54 |
snap(info->getSnapshotManager()->getCurrentSnapshot()) { |
55 |
} |
56 |
|
57 |
void FluctuatingChargeLangevin::initialize() { |
58 |
FluctuatingChargePropagator::initialize(); |
59 |
if (hasFlucQ_) { |
60 |
if (info_->getSimParams()->haveDt()) { |
61 |
dt_ = info_->getSimParams()->getDt(); |
62 |
dt2_ = dt_ * 0.5; |
63 |
} else { |
64 |
sprintf(painCave.errMsg, |
65 |
"FluctuatingChargeLangevin Error: dt is not set\n"); |
66 |
painCave.isFatal = 1; |
67 |
simError(); |
68 |
} |
69 |
|
70 |
if (!fqParams_->haveTargetTemp()) { |
71 |
sprintf(painCave.errMsg, "You can't use the FluctuatingChargeLangevin " |
72 |
"propagator without a flucQ.targetTemp!\n"); |
73 |
painCave.isFatal = 1; |
74 |
painCave.severity = OPENMD_ERROR; |
75 |
simError(); |
76 |
} else { |
77 |
targetTemp_ = fqParams_->getTargetTemp(); |
78 |
} |
79 |
|
80 |
// We must set tauThermostat. |
81 |
|
82 |
if (!fqParams_->haveDragCoefficient()) { |
83 |
sprintf(painCave.errMsg, "If you use the FluctuatingChargeLangevin\n" |
84 |
"\tpropagator, you must set flucQ.dragCoefficient .\n"); |
85 |
|
86 |
painCave.severity = OPENMD_ERROR; |
87 |
painCave.isFatal = 1; |
88 |
simError(); |
89 |
} else { |
90 |
drag_ = fqParams_->getDragCoefficient(); |
91 |
} |
92 |
} |
93 |
|
94 |
variance_ = 2.0 * PhysicalConstants::kb * targetTemp_ * drag_ / dt_; |
95 |
} |
96 |
|
97 |
|
98 |
void FluctuatingChargeLangevin::moveA() { |
99 |
|
100 |
if (!hasFlucQ_) return; |
101 |
|
102 |
SimInfo::MoleculeIterator i; |
103 |
Molecule::FluctuatingChargeIterator j; |
104 |
Molecule* mol; |
105 |
Atom* atom; |
106 |
RealType cvel, cpos, cfrc, cmass; |
107 |
|
108 |
for (mol = info_->beginMolecule(i); mol != NULL; |
109 |
mol = info_->nextMolecule(i)) { |
110 |
for (atom = mol->beginFluctuatingCharge(j); atom != NULL; |
111 |
atom = mol->nextFluctuatingCharge(j)) { |
112 |
|
113 |
cvel = atom->getFlucQVel(); |
114 |
cpos = atom->getFlucQPos(); |
115 |
cfrc = atom->getFlucQFrc(); |
116 |
cmass = atom->getChargeMass(); |
117 |
|
118 |
// velocity half step |
119 |
cvel += dt2_ * cfrc / cmass; |
120 |
// position whole step |
121 |
cpos += dt_ * cvel; |
122 |
|
123 |
atom->setFlucQVel(cvel); |
124 |
atom->setFlucQPos(cpos); |
125 |
} |
126 |
} |
127 |
} |
128 |
|
129 |
void FluctuatingChargeLangevin::applyConstraints() { |
130 |
|
131 |
if (!hasFlucQ_) return; |
132 |
|
133 |
SimInfo::MoleculeIterator i; |
134 |
Molecule::FluctuatingChargeIterator j; |
135 |
Molecule* mol; |
136 |
Atom* atom; |
137 |
RealType cvel, cfrc, cmass, randomForce, frictionForce; |
138 |
RealType velStep, oldFF; // used to test for convergence |
139 |
|
140 |
for (mol = info_->beginMolecule(i); mol != NULL; |
141 |
mol = info_->nextMolecule(i)) { |
142 |
for (atom = mol->beginFluctuatingCharge(j); atom != NULL; |
143 |
atom = mol->nextFluctuatingCharge(j)) { |
144 |
|
145 |
randomForce = randNumGen_.randNorm(0, variance_ ); |
146 |
atom->addFlucQFrc(randomForce); |
147 |
|
148 |
// What remains contains velocity explicitly, but the velocity |
149 |
// required is at the full step: v(t + h), while we have |
150 |
// initially the velocity at the half step: v(t + h/2). We |
151 |
// need to iterate to converge the friction force vector. |
152 |
|
153 |
// this is the velocity at the half-step: |
154 |
|
155 |
cvel = atom->getFlucQVel(); |
156 |
|
157 |
// estimate velocity at full-step using everything but |
158 |
// friction forces: |
159 |
|
160 |
cfrc = atom->getFlucQFrc(); |
161 |
cmass = atom->getChargeMass(); |
162 |
velStep = cvel + dt2_ * cfrc / cmass; |
163 |
|
164 |
frictionForce = 0.0; |
165 |
|
166 |
//iteration starts here: |
167 |
|
168 |
for (int k = 0; k < maxIterNum_; k++) { |
169 |
|
170 |
oldFF = frictionForce; |
171 |
frictionForce = -drag_ * velStep; |
172 |
// re-estimate velocities at full-step using friction forces: |
173 |
|
174 |
velStep = cvel + dt2_ * (cfrc + frictionForce) / cmass; |
175 |
|
176 |
// check for convergence |
177 |
|
178 |
if (fabs(frictionForce - oldFF) <= forceTolerance_) |
179 |
break; // iteration ends here |
180 |
} |
181 |
//cerr << "rand = " << randomForce << " fric = " << frictionForce << "\n"; |
182 |
atom->addFlucQFrc(frictionForce); |
183 |
} |
184 |
} |
185 |
fqConstraints_->applyConstraints(); |
186 |
} |
187 |
|
188 |
void FluctuatingChargeLangevin::moveB() { |
189 |
if (!hasFlucQ_) return; |
190 |
SimInfo::MoleculeIterator i; |
191 |
Molecule::FluctuatingChargeIterator j; |
192 |
Molecule* mol; |
193 |
Atom* atom; |
194 |
RealType cfrc, cvel, cmass; |
195 |
|
196 |
for (mol = info_->beginMolecule(i); mol != NULL; |
197 |
mol = info_->nextMolecule(i)) { |
198 |
for (atom = mol->beginFluctuatingCharge(j); atom != NULL; |
199 |
atom = mol->nextFluctuatingCharge(j)) { |
200 |
|
201 |
cvel =atom->getFlucQVel(); |
202 |
cfrc = atom->getFlucQFrc(); |
203 |
cmass = atom->getChargeMass(); |
204 |
|
205 |
// velocity half step |
206 |
cvel += (dt2_ * cfrc) / cmass; |
207 |
|
208 |
atom->setFlucQVel(cvel); |
209 |
} |
210 |
} |
211 |
} |
212 |
|
213 |
void FluctuatingChargeLangevin::updateSizes() { } |
214 |
|
215 |
RealType FluctuatingChargeLangevin::calcConservedQuantity() { |
216 |
return 0.0; |
217 |
} |
218 |
} |