ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/flucq/FluctuatingChargeConstraints.cpp
(Generate patch)

Comparing trunk/src/flucq/FluctuatingChargeConstraints.cpp (file contents):
Revision 1908 by gezelter, Fri Jul 19 21:25:45 2013 UTC vs.
Revision 1915 by gezelter, Mon Jul 29 17:55:17 2013 UTC

# Line 50 | Line 50 | namespace OpenMD {
50   namespace OpenMD {
51  
52    FluctuatingChargeConstraints::FluctuatingChargeConstraints(SimInfo* info) :
53 <    info_(info), constrainRegions_(false), hasFlucQ_(false) {
53 >    info_(info), constrainRegions_(false), hasFlucQ_(false), initialized_(false) {
54      
55 <    if (info_->usesFluctuatingCharges()) {
56 <      if (info_->getNFluctuatingCharges() > 0) {
57 <        hasFlucQ_ = true;
55 >  }
56 >
57 >  void FluctuatingChargeConstraints::initialize(){
58 >    if(info_->usesFluctuatingCharges()){
59 >      if(info_->getNFluctuatingCharges() > 0){
60 >        hasFlucQ_ = true;
61        }
62      }
63 +    initialized_ = true;
64    }
65  
66 +
67    void FluctuatingChargeConstraints::setConstrainRegions(bool cr) {
68      constrainRegions_ = cr;
69 +
70 +    if (!initialized_) initialize();
71 +
72      regionKeys_.clear();
73      regionForce_.clear();
74      regionCharges_.clear();
# Line 75 | Line 83 | namespace OpenMD {
83             mol = info_->nextMolecule(i)) {
84          reg = mol->getRegion();
85          if (reg >= 0) regions.insert(reg);
78        
86        }
87 +      // resize the keys vector to the largest found value for regions.
88 +      regionKeys_.resize( *(regions.end()) );
89 +      int which = 0;
90        for (std::set<int>::iterator r=regions.begin(); r!=regions.end(); ++r) {
91 <        regionKeys_.push_back( (*r) );
91 >        regionKeys_[ (*r) ] = which;
92 >        which++;
93        }
94        regionForce_.resize( regionKeys_.size() );
95        regionCharges_.resize( regionKeys_.size() );
# Line 87 | Line 98 | namespace OpenMD {
98  
99  
100    void FluctuatingChargeConstraints::applyConstraints() {
101 +    if (!initialized_) initialize();
102      if (!hasFlucQ_) return;
91    
103      SimInfo::MoleculeIterator i;
104      Molecule::FluctuatingChargeIterator  j;
105      Molecule* mol;
106      Atom* atom;
107      
108      RealType totalFrc, totalMolFrc, regionFrc, constrainedFrc;
98    
109      // accumulate the total system fluctuating charge forces
110      totalFrc = 0.0;
111      if (constrainRegions_) {
# Line 114 | Line 124 | namespace OpenMD {
124          RealType frc = atom->getFlucQFrc();
125          totalFrc += frc;
126          if (constrainRegions_) {
127 <          regionForce_[regionKeys_[region]] += frc;
128 <          regionCharges_[regionKeys_[region]] += 1;
127 >          if (region >= 0) {        
128 >            regionForce_[regionKeys_[region]] += frc;
129 >            regionCharges_[regionKeys_[region]] += 1;
130 >          }
131          }
132        }
133      }
# Line 150 | Line 162 | namespace OpenMD {
162        
163        if (constrainRegions_) {
164          int region = mol->getRegion();
165 <        regionFrc = regionForce_[regionKeys_[region]];
165 >        if (region >= 0)
166 >          regionFrc = regionForce_[regionKeys_[region]];
167 >        else
168 >          regionFrc = 0.0;
169        } else {
170          regionFrc = 0.0;
171        }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines