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