ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/flucq/FluctuatingChargeConstraints.cpp
Revision: 1939
Committed: Thu Oct 31 18:18:57 2013 UTC (11 years, 6 months ago) by gezelter
File size: 6617 byte(s)
Log Message:
Some fixes for MSVC compilation

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

Properties

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