ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/flucq/FluctuatingChargeNVT.cpp
Revision: 1761
Committed: Fri Jun 22 20:01:37 2012 UTC (12 years, 11 months ago) by gezelter
File size: 8061 byte(s)
Log Message:
fixes for fluctuating charges

File Contents

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

Properties

Name Value
svn:eol-style native
svn:executable *