ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/flucq/FluctuatingChargeConstraints.cpp
Revision: 1913
Committed: Wed Jul 24 20:00:51 2013 UTC (11 years, 9 months ago) by jmichalk
File size: 6572 byte(s)
Log Message:
Fixed some bugs in the flucQ constraints.

File Contents

# User Rev Content
1 gezelter 1740 /*
2     * Copyright (c) 2012 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 1879 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1740 * [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 "FluctuatingChargeConstraints.hpp"
44     #include "primitives/Molecule.hpp"
45    
46     #ifdef IS_MPI
47     #include <mpi.h>
48     #endif
49    
50     namespace OpenMD {
51    
52     FluctuatingChargeConstraints::FluctuatingChargeConstraints(SimInfo* info) :
53 jmichalk 1913 info_(info), constrainRegions_(false), hasFlucQ_(false), initialized_(false) {
54 gezelter 1740
55 jmichalk 1913 }
56    
57     void FluctuatingChargeConstraints::initialize(){
58     if(info_->usesFluctuatingCharges()){
59     if(info_->getNFluctuatingCharges() > 0){
60     hasFlucQ_ = true;
61 gezelter 1740 }
62     }
63 jmichalk 1913 initialized_ = true;
64 gezelter 1740 }
65    
66 gezelter 1908 void FluctuatingChargeConstraints::setConstrainRegions(bool cr) {
67     constrainRegions_ = cr;
68     regionKeys_.clear();
69     regionForce_.clear();
70     regionCharges_.clear();
71    
72     if (constrainRegions_) {
73     SimInfo::MoleculeIterator i;
74     Molecule* mol;
75     int reg;
76     std::set<int> regions;
77    
78     for (mol = info_->beginMolecule(i); mol != NULL;
79     mol = info_->nextMolecule(i)) {
80     reg = mol->getRegion();
81     if (reg >= 0) regions.insert(reg);
82     }
83 jmichalk 1913 // resize the keys vector to the largest found value for regions.
84     regionKeys_.resize( *(regions.end()) );
85     int which = 0;
86 gezelter 1908 for (std::set<int>::iterator r=regions.begin(); r!=regions.end(); ++r) {
87 jmichalk 1913 regionKeys_[ (*r) ] = which;
88     which++;
89 gezelter 1908 }
90     regionForce_.resize( regionKeys_.size() );
91     regionCharges_.resize( regionKeys_.size() );
92     }
93     }
94    
95    
96 gezelter 1740 void FluctuatingChargeConstraints::applyConstraints() {
97 jmichalk 1913 if (!initialized_) initialize();
98 gezelter 1740 if (!hasFlucQ_) return;
99     SimInfo::MoleculeIterator i;
100     Molecule::FluctuatingChargeIterator j;
101     Molecule* mol;
102     Atom* atom;
103    
104 gezelter 1908 RealType totalFrc, totalMolFrc, regionFrc, constrainedFrc;
105 gezelter 1740 // accumulate the total system fluctuating charge forces
106     totalFrc = 0.0;
107 gezelter 1908 if (constrainRegions_) {
108     std::fill(regionForce_.begin(), regionForce_.end(), 0.0);
109     std::fill(regionCharges_.begin(), regionCharges_.end(), 0);
110     }
111 gezelter 1740
112     for (mol = info_->beginMolecule(i); mol != NULL;
113     mol = info_->nextMolecule(i)) {
114    
115 gezelter 1908 int region = mol->getRegion();
116    
117 gezelter 1740 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
118     atom = mol->nextFluctuatingCharge(j)) {
119 gezelter 1908
120     RealType frc = atom->getFlucQFrc();
121     totalFrc += frc;
122     if (constrainRegions_) {
123 jmichalk 1913 if (region >= 0) {
124     regionForce_[regionKeys_[region]] += frc;
125     regionCharges_[regionKeys_[region]] += 1;
126     }
127 gezelter 1908 }
128 gezelter 1740 }
129     }
130    
131     #ifdef IS_MPI
132     // in parallel, we need to add up the contributions from all
133     // processors:
134     MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &totalFrc, 1, MPI::REALTYPE,
135     MPI::SUM);
136 gezelter 1908
137     if (constrainRegions_) {
138     MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &regionForce_[0],
139     regionForce_.size(), MPI::REALTYPE, MPI::SUM);
140     MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &regionCharges_[0],
141     regionCharges_.size(), MPI::INT, MPI::SUM);
142     }
143    
144 gezelter 1740 #endif
145 gezelter 1908
146 gezelter 1740 // divide by the total number of fluctuating charges:
147     totalFrc /= info_->getNFluctuatingCharges();
148 gezelter 1908
149     // do the same in the regions:
150     if (constrainRegions_) {
151     for (int i = 0; i < regionForce_.size(); ++i) {
152     regionForce_[ i ] /= regionCharges_[ i ];
153     }
154     }
155 gezelter 1740
156     for (mol = info_->beginMolecule(i); mol != NULL;
157     mol = info_->nextMolecule(i)) {
158    
159 gezelter 1908 if (constrainRegions_) {
160     int region = mol->getRegion();
161 jmichalk 1913 if (region >= 0)
162     regionFrc = regionForce_[regionKeys_[region]];
163     else
164     regionFrc = 0.0;
165 gezelter 1908 } else {
166     regionFrc = 0.0;
167     }
168    
169 gezelter 1740 totalMolFrc = 0.0;
170 gezelter 1908
171 gezelter 1740 // molecular constraints can be done with a second loop.
172     if (mol->constrainTotalCharge()) {
173     for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
174     atom = mol->nextFluctuatingCharge(j)) {
175     totalMolFrc += atom->getFlucQFrc();
176     }
177     totalMolFrc /= mol->getNFluctuatingCharges();
178     }
179    
180     for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
181     atom = mol->nextFluctuatingCharge(j)) {
182 gezelter 1879 //constrainedFrc = atom->getFlucQFrc() - totalFrc - totalMolFrc;
183 gezelter 1908
184 gezelter 1879 constrainedFrc = atom->getFlucQFrc() - totalMolFrc;
185 gezelter 1908
186     if (constrainRegions_)
187     constrainedFrc -= regionFrc;
188    
189 gezelter 1740 atom->setFlucQFrc(constrainedFrc);
190     }
191     }
192     }
193     }

Properties

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