ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/flucq/FluctuatingChargeNVT.cpp
Revision: 1874
Committed: Wed May 15 15:09:35 2013 UTC (11 years, 11 months ago) by gezelter
File size: 7887 byte(s)
Log Message:
Fixed a bunch of cppcheck warnings.

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 gezelter 1850 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1716 * [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 gezelter 1870 snap(info->getSnapshotManager()->getCurrentSnapshot()) {
55 gezelter 1761 }
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 gezelter 1764 snap->setElectronicThermostat(make_pair(0.0, 0.0));
72 gezelter 1739 }
73    
74     if (!fqParams_->haveTargetTemp()) {
75     sprintf(painCave.errMsg, "You can't use the FluctuatingChargeNVT "
76     "propagator without a flucQ.targetTemp!\n");
77     painCave.isFatal = 1;
78     painCave.severity = OPENMD_ERROR;
79     simError();
80     } else {
81     targetTemp_ = fqParams_->getTargetTemp();
82     }
83    
84     // We must set tauThermostat.
85    
86     if (!fqParams_->haveTauThermostat()) {
87     sprintf(painCave.errMsg, "If you use the FluctuatingChargeNVT\n"
88     "\tpropagator, you must set flucQ.tauThermostat .\n");
89 gezelter 1716
90 gezelter 1739 painCave.severity = OPENMD_ERROR;
91     painCave.isFatal = 1;
92     simError();
93     } else {
94     tauThermostat_ = fqParams_->getTauThermostat();
95 gezelter 1716 }
96 gezelter 1761 updateSizes();
97 gezelter 1739 }
98 gezelter 1716 }
99    
100    
101     void FluctuatingChargeNVT::moveA() {
102    
103     if (!hasFlucQ_) return;
104    
105     SimInfo::MoleculeIterator i;
106     Molecule::FluctuatingChargeIterator j;
107     Molecule* mol;
108     Atom* atom;
109     RealType cvel, cpos, cfrc, cmass;
110    
111 gezelter 1764 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
112     RealType chi = thermostat.first;
113     RealType integralOfChidt = thermostat.second;
114 gezelter 1716 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 gezelter 1764 snap->setElectronicThermostat(make_pair(chi, integralOfChidt));
141 gezelter 1716 }
142    
143     void FluctuatingChargeNVT::updateSizes() {
144     oldVel_.resize(info_->getNFluctuatingCharges());
145     }
146    
147     void FluctuatingChargeNVT::moveB() {
148     if (!hasFlucQ_) return;
149     SimInfo::MoleculeIterator i;
150     Molecule::FluctuatingChargeIterator j;
151     Molecule* mol;
152     Atom* atom;
153     RealType instTemp;
154 gezelter 1764 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
155     RealType chi = thermostat.first;
156 gezelter 1716 RealType oldChi = chi;
157     RealType prevChi;
158 gezelter 1764 RealType integralOfChidt = thermostat.second;
159 gezelter 1716 int index;
160     RealType cfrc, cvel, cmass;
161    
162     index = 0;
163     for (mol = info_->beginMolecule(i); mol != NULL;
164     mol = info_->nextMolecule(i)) {
165     for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
166     atom = mol->nextFluctuatingCharge(j)) {
167    
168     oldVel_[index] = atom->getFlucQVel();
169     ++index;
170     }
171     }
172    
173     // do the iteration:
174    
175     for(int k = 0; k < maxIterNum_; k++) {
176     index = 0;
177     instTemp = thermo.getElectronicTemperature();
178     // evolve chi another half step using the temperature at t + dt/2
179     prevChi = chi;
180     chi = oldChi + dt2_ * (instTemp / targetTemp_ - 1.0) /
181     (tauThermostat_ * tauThermostat_);
182    
183     for (mol = info_->beginMolecule(i); mol != NULL;
184     mol = info_->nextMolecule(i)) {
185     for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
186     atom = mol->nextFluctuatingCharge(j)) {
187    
188     cfrc = atom->getFlucQFrc();
189     cmass = atom->getChargeMass();
190    
191     // velocity half step
192 jmichalk 1735 cvel = oldVel_[index] + dt2_ * cfrc / cmass - dt2_*chi*oldVel_[index];
193 gezelter 1716 atom->setFlucQVel(cvel);
194     ++index;
195     }
196     }
197     if (fabs(prevChi - chi) <= chiTolerance_)
198     break;
199     }
200     integralOfChidt += dt2_ * chi;
201 gezelter 1764 snap->setElectronicThermostat(make_pair(chi, integralOfChidt));
202 gezelter 1716 }
203    
204     void FluctuatingChargeNVT::resetPropagator() {
205     if (!hasFlucQ_) return;
206 gezelter 1764 snap->setElectronicThermostat(make_pair(0.0, 0.0));
207 gezelter 1716 }
208    
209     RealType FluctuatingChargeNVT::calcConservedQuantity() {
210     if (!hasFlucQ_) return 0.0;
211 gezelter 1764 pair<RealType, RealType> thermostat = snap->getElectronicThermostat();
212     RealType chi = thermostat.first;
213     RealType integralOfChidt = thermostat.second;
214 gezelter 1716 RealType fkBT = info_->getNFluctuatingCharges() *
215     PhysicalConstants::kB *targetTemp_;
216    
217     RealType thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ *
218     chi * chi / (2.0 * PhysicalConstants::energyConvert);
219    
220     RealType thermostat_potential = fkBT * integralOfChidt /
221     PhysicalConstants::energyConvert;
222    
223     return thermostat_kinetic + thermostat_potential;
224     }
225     }

Properties

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