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, 24107 (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 "FluctuatingChargeNVT.hpp" |
44 |
#include "primitives/Molecule.hpp" |
45 |
#include "utils/simError.h" |
46 |
#include "utils/PhysicalConstants.hpp" |
47 |
|
48 |
namespace OpenMD { |
49 |
|
50 |
FluctuatingChargeNVT::FluctuatingChargeNVT(SimInfo* info) : |
51 |
FluctuatingChargePropagator(info), chiTolerance_ (1e-6), maxIterNum_(4), |
52 |
thermo(info), |
53 |
currentSnapshot_(info->getSnapshotManager()->getCurrentSnapshot()) { |
54 |
|
55 |
if (info_->usesFluctuatingCharges()) { |
56 |
if (info_->getNFluctuatingCharges() > 0) { |
57 |
|
58 |
hasFlucQ_ = true; |
59 |
Globals* simParams = info_->getSimParams(); |
60 |
|
61 |
if (simParams->haveDt()) { |
62 |
dt_ = simParams->getDt(); |
63 |
dt2_ = dt_ * 0.5; |
64 |
} else { |
65 |
sprintf(painCave.errMsg, |
66 |
"FluctuatingChargeNVT Error: dt is not set\n"); |
67 |
painCave.isFatal = 1; |
68 |
simError(); |
69 |
} |
70 |
|
71 |
if (!simParams->getUseIntialExtendedSystemState()) { |
72 |
currentSnapshot_->setChiElectronic(0.0); |
73 |
currentSnapshot_->setIntegralOfChiElectronicDt(0.0); |
74 |
} |
75 |
|
76 |
if (!simParams->haveFlucQTargetTemp()) { |
77 |
sprintf(painCave.errMsg, "You can't use the FluctuatingChargeNVT " |
78 |
"propagator without a flucQ.targetTemp!\n"); |
79 |
painCave.isFatal = 1; |
80 |
painCave.severity = OPENMD_ERROR; |
81 |
simError(); |
82 |
} else { |
83 |
targetTemp_ = simParams->getFlucQTargetTemp(); |
84 |
} |
85 |
|
86 |
// We must set tauThermostat. |
87 |
|
88 |
if (!simParams->haveFlucQtauThermostat()) { |
89 |
sprintf(painCave.errMsg, "If you use the FluctuatingChargeNVT\n" |
90 |
"\tpropagator, you must set flucQ.tauThermostat .\n"); |
91 |
|
92 |
painCave.severity = OPENMD_ERROR; |
93 |
painCave.isFatal = 1; |
94 |
simError(); |
95 |
} else { |
96 |
tauThermostat_ = simParams->getFlucQtauThermostat(); |
97 |
} |
98 |
updateSizes(); |
99 |
} |
100 |
} |
101 |
} |
102 |
|
103 |
void FluctuatingChargeNVT::initialize() { |
104 |
|
105 |
if (!hasFlucQ_) return; |
106 |
|
107 |
SimInfo::MoleculeIterator i; |
108 |
Molecule::FluctuatingChargeIterator j; |
109 |
Molecule* mol; |
110 |
Atom* atom; |
111 |
|
112 |
for (mol = info_->beginMolecule(i); mol != NULL; |
113 |
mol = info_->nextMolecule(i)) { |
114 |
for (atom = mol->beginFluctuatingCharge(j); atom != NULL; |
115 |
atom = mol->nextFluctuatingCharge(j)) { |
116 |
atom->setFlucQPos(0.0); |
117 |
atom->setFlucQVel(0.0); |
118 |
} |
119 |
} |
120 |
|
121 |
cerr << "Yeah, you should probably implement this\n"; |
122 |
} |
123 |
|
124 |
void FluctuatingChargeNVT::moveA() { |
125 |
|
126 |
if (!hasFlucQ_) return; |
127 |
|
128 |
SimInfo::MoleculeIterator i; |
129 |
Molecule::FluctuatingChargeIterator j; |
130 |
Molecule* mol; |
131 |
Atom* atom; |
132 |
RealType cvel, cpos, cfrc, cmass; |
133 |
|
134 |
RealType chi = currentSnapshot_->getChiElectronic(); |
135 |
RealType integralOfChidt = currentSnapshot_->getIntegralOfChiElectronicDt(); |
136 |
RealType instTemp = thermo.getElectronicTemperature(); |
137 |
|
138 |
cerr << "why are we here?\n"; |
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 |
cvel = atom->getFlucQVel(); |
146 |
cpos = atom->getFlucQPos(); |
147 |
cfrc = atom->getFlucQFrc(); |
148 |
cmass = atom->getChargeMass(); |
149 |
|
150 |
// velocity half step |
151 |
cvel += dt2_ *PhysicalConstants::energyConvert/cmass*cfrc - dt2_*chi*cvel; |
152 |
// position whole step |
153 |
cpos += dt_ * cvel; |
154 |
|
155 |
atom->setFlucQVel(cvel); |
156 |
atom->setFlucQPos(cpos); |
157 |
} |
158 |
} |
159 |
|
160 |
chi += dt2_ * (instTemp / targetTemp_ - 1.0) / |
161 |
(tauThermostat_ * tauThermostat_); |
162 |
|
163 |
integralOfChidt += chi * dt2_; |
164 |
currentSnapshot_->setChiElectronic(chi); |
165 |
currentSnapshot_->setIntegralOfChiElectronicDt(integralOfChidt); |
166 |
|
167 |
} |
168 |
|
169 |
void FluctuatingChargeNVT::updateSizes() { |
170 |
if (!hasFlucQ_) return; |
171 |
oldVel_.resize(info_->getNFluctuatingCharges()); |
172 |
} |
173 |
|
174 |
void FluctuatingChargeNVT::moveB() { |
175 |
if (!hasFlucQ_) return; |
176 |
SimInfo::MoleculeIterator i; |
177 |
Molecule::FluctuatingChargeIterator j; |
178 |
Molecule* mol; |
179 |
Atom* atom; |
180 |
RealType instTemp; |
181 |
RealType chi = currentSnapshot_->getChiElectronic(); |
182 |
RealType oldChi = chi; |
183 |
RealType prevChi; |
184 |
RealType integralOfChidt = currentSnapshot_->getIntegralOfChiElectronicDt(); |
185 |
int index; |
186 |
RealType cfrc, cvel, cmass; |
187 |
|
188 |
index = 0; |
189 |
for (mol = info_->beginMolecule(i); mol != NULL; |
190 |
mol = info_->nextMolecule(i)) { |
191 |
for (atom = mol->beginFluctuatingCharge(j); atom != NULL; |
192 |
atom = mol->nextFluctuatingCharge(j)) { |
193 |
|
194 |
oldVel_[index] = atom->getFlucQVel(); |
195 |
++index; |
196 |
} |
197 |
} |
198 |
|
199 |
// do the iteration: |
200 |
|
201 |
for(int k = 0; k < maxIterNum_; k++) { |
202 |
index = 0; |
203 |
instTemp = thermo.getElectronicTemperature(); |
204 |
|
205 |
// evolve chi another half step using the temperature at t + dt/2 |
206 |
prevChi = chi; |
207 |
chi = oldChi + dt2_ * (instTemp / targetTemp_ - 1.0) / |
208 |
(tauThermostat_ * tauThermostat_); |
209 |
|
210 |
for (mol = info_->beginMolecule(i); mol != NULL; |
211 |
mol = info_->nextMolecule(i)) { |
212 |
for (atom = mol->beginFluctuatingCharge(j); atom != NULL; |
213 |
atom = mol->nextFluctuatingCharge(j)) { |
214 |
|
215 |
cfrc = atom->getFlucQFrc(); |
216 |
cvel =atom->getFlucQVel(); |
217 |
cmass = atom->getChargeMass(); |
218 |
|
219 |
// velocity half step |
220 |
cvel = oldVel_[index] + dt2_/cmass*PhysicalConstants::energyConvert * cfrc - dt2_*chi*oldVel_[index]; |
221 |
|
222 |
atom->setFlucQVel(cvel); |
223 |
++index; |
224 |
} |
225 |
} |
226 |
if (fabs(prevChi - chi) <= chiTolerance_) |
227 |
break; |
228 |
} |
229 |
integralOfChidt += dt2_ * chi; |
230 |
currentSnapshot_->setChiElectronic(chi); |
231 |
currentSnapshot_->setIntegralOfChiElectronicDt(integralOfChidt); |
232 |
} |
233 |
|
234 |
void FluctuatingChargeNVT::resetPropagator() { |
235 |
if (!hasFlucQ_) return; |
236 |
currentSnapshot_->setChiElectronic(0.0); |
237 |
currentSnapshot_->setIntegralOfChiElectronicDt(0.0); |
238 |
} |
239 |
|
240 |
RealType FluctuatingChargeNVT::calcConservedQuantity() { |
241 |
if (!hasFlucQ_) return 0.0; |
242 |
RealType chi = currentSnapshot_->getChiElectronic(); |
243 |
RealType integralOfChidt = currentSnapshot_->getIntegralOfChiElectronicDt(); |
244 |
RealType fkBT = info_->getNFluctuatingCharges() * |
245 |
PhysicalConstants::kB *targetTemp_; |
246 |
|
247 |
RealType thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ * |
248 |
chi * chi / (2.0 * PhysicalConstants::energyConvert); |
249 |
|
250 |
RealType thermostat_potential = fkBT * integralOfChidt / |
251 |
PhysicalConstants::energyConvert; |
252 |
|
253 |
return thermostat_kinetic + thermostat_potential; |
254 |
} |
255 |
} |