ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/brains/SimInfo.cpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/brains/SimInfo.cpp (file contents):
Revision 1492 by tim, Fri Sep 24 16:27:58 2004 UTC vs.
Revision 2116 by tim, Fri Mar 11 15:00:20 2005 UTC

# Line 1 | Line 1
1 < #include <stdlib.h>
2 < #include <string.h>
3 < #include <math.h>
1 > /*
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. Acknowledgement of the program authors must be made in any
10 > *    publication of scientific results based in part on use of the
11 > *    program.  An acceptable form of acknowledgement is citation of
12 > *    the article in which the program was described (Matthew
13 > *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 > *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 > *    Parallel Simulation Engine for Molecular Dynamics,"
16 > *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 > *
18 > * 2. Redistributions of source code must retain the above copyright
19 > *    notice, this list of conditions and the following disclaimer.
20 > *
21 > * 3. Redistributions in binary form must reproduce the above copyright
22 > *    notice, this list of conditions and the following disclaimer in the
23 > *    documentation and/or other materials provided with the
24 > *    distribution.
25 > *
26 > * This software is provided "AS IS," without a warranty of any
27 > * kind. All express or implied conditions, representations and
28 > * warranties, including any implied warranty of merchantability,
29 > * fitness for a particular purpose or non-infringement, are hereby
30 > * excluded.  The University of Notre Dame and its licensors shall not
31 > * be liable for any damages suffered by licensee as a result of
32 > * using, modifying or distributing the software or its
33 > * derivatives. In no event will the University of Notre Dame or its
34 > * licensors be liable for any lost revenue, profit or data, or for
35 > * direct, indirect, special, consequential, incidental or punitive
36 > * damages, however caused and regardless of the theory of liability,
37 > * arising out of the use of or inability to use software, even if the
38 > * University of Notre Dame has been advised of the possibility of
39 > * such damages.
40 > */
41 >
42 > /**
43 > * @file SimInfo.cpp
44 > * @author    tlin
45 > * @date  11/02/2004
46 > * @version 1.0
47 > */
48  
49 < #include <iostream>
50 < using namespace std;
49 > #include <algorithm>
50 > #include <set>
51  
52   #include "brains/SimInfo.hpp"
53 < #define __C
54 < #include "brains/fSimulation.h"
53 > #include "math/Vector3.hpp"
54 > #include "primitives/Molecule.hpp"
55 > #include "UseTheForce/doForces_interface.h"
56 > #include "UseTheForce/notifyCutoffs_interface.h"
57 > #include "utils/MemoryUtils.hpp"
58   #include "utils/simError.h"
59 + #include "selection/SelectionManager.hpp"
60  
61 < #include "UseTheForce/fortranWrappers.hpp"
61 > #ifdef IS_MPI
62 > #include "UseTheForce/mpiComponentPlan.h"
63 > #include "UseTheForce/DarkSide/simParallel_interface.h"
64 > #endif
65  
66 < #include "math/MatVec3.h"
66 > namespace oopse {
67  
68 < #ifdef IS_MPI
69 < #include "brains/mpiSimulation.hpp"
70 < #endif
68 > SimInfo::SimInfo(std::vector<std::pair<MoleculeStamp*, int> >& molStampPairs,
69 >                                ForceField* ff, Globals* simParams) :
70 >                                forceField_(ff), simParams_(simParams),
71 >                                ndf_(0), ndfRaw_(0), ndfTrans_(0), nZconstraint_(0),
72 >                                nGlobalMols_(0), nGlobalAtoms_(0), nGlobalCutoffGroups_(0),
73 >                                nGlobalIntegrableObjects_(0), nGlobalRigidBodies_(0),
74 >                                nAtoms_(0), nBonds_(0),  nBends_(0), nTorsions_(0), nRigidBodies_(0),
75 >                                nIntegrableObjects_(0),  nCutoffGroups_(0), nConstraints_(0),
76 >                                sman_(NULL), fortranInitialized_(false) {
77  
78 < inline double roundMe( double x ){
79 <  return ( x >= 0 ) ? floor( x + 0.5 ) : ceil( x - 0.5 );
80 < }
81 <          
82 < inline double min( double a, double b ){
83 <  return (a < b ) ? a : b;
84 < }
78 >            
79 >    std::vector<std::pair<MoleculeStamp*, int> >::iterator i;
80 >    MoleculeStamp* molStamp;
81 >    int nMolWithSameStamp;
82 >    int nCutoffAtoms = 0; // number of atoms belong to cutoff groups
83 >    int nGroups = 0;          //total cutoff groups defined in meta-data file
84 >    CutoffGroupStamp* cgStamp;    
85 >    RigidBodyStamp* rbStamp;
86 >    int nRigidAtoms = 0;
87 >    
88 >    for (i = molStampPairs.begin(); i !=molStampPairs.end(); ++i) {
89 >        molStamp = i->first;
90 >        nMolWithSameStamp = i->second;
91 >        
92 >        addMoleculeStamp(molStamp, nMolWithSameStamp);
93  
94 < SimInfo* currentInfo;
94 >        //calculate atoms in molecules
95 >        nGlobalAtoms_ += molStamp->getNAtoms() *nMolWithSameStamp;  
96  
31 SimInfo::SimInfo(){
97  
98 <  n_constraints = 0;
99 <  nZconstraints = 0;
100 <  n_oriented = 0;
101 <  n_dipoles = 0;
102 <  ndf = 0;
103 <  ndfRaw = 0;
104 <  nZconstraints = 0;
105 <  the_integrator = NULL;
41 <  setTemp = 0;
42 <  thermalTime = 0.0;
43 <  currentTime = 0.0;
44 <  rCut = 0.0;
45 <  rSw = 0.0;
98 >        //calculate atoms in cutoff groups
99 >        int nAtomsInGroups = 0;
100 >        int nCutoffGroupsInStamp = molStamp->getNCutoffGroups();
101 >        
102 >        for (int j=0; j < nCutoffGroupsInStamp; j++) {
103 >            cgStamp = molStamp->getCutoffGroup(j);
104 >            nAtomsInGroups += cgStamp->getNMembers();
105 >        }
106  
107 <  haveRcut = 0;
108 <  haveRsw = 0;
49 <  boxIsInit = 0;
50 <  
51 <  resetTime = 1e99;
107 >        nGroups += nCutoffGroupsInStamp * nMolWithSameStamp;
108 >        nCutoffAtoms += nAtomsInGroups * nMolWithSameStamp;            
109  
110 <  orthoRhombic = 0;
111 <  orthoTolerance = 1E-6;
112 <  useInitXSstate = true;
110 >        //calculate atoms in rigid bodies
111 >        int nAtomsInRigidBodies = 0;
112 >        int nRigidBodiesInStamp = molStamp->getNRigidBodies();
113 >        
114 >        for (int j=0; j < nRigidBodiesInStamp; j++) {
115 >            rbStamp = molStamp->getRigidBody(j);
116 >            nAtomsInRigidBodies += rbStamp->getNMembers();
117 >        }
118  
119 <  usePBC = 0;
120 <  useLJ = 0;
121 <  useSticky = 0;
122 <  useCharges = 0;
61 <  useDipoles = 0;
62 <  useReactionField = 0;
63 <  useGB = 0;
64 <  useEAM = 0;
65 <  useSolidThermInt = 0;
66 <  useLiquidThermInt = 0;
119 >        nGlobalRigidBodies_ += nRigidBodiesInStamp * nMolWithSameStamp;
120 >        nRigidAtoms += nAtomsInRigidBodies * nMolWithSameStamp;            
121 >        
122 >    }
123  
124 <  haveCutoffGroups = false;
124 >    //every free atom (atom does not belong to cutoff groups) is a cutoff group
125 >    //therefore the total number of cutoff groups in the system is equal to
126 >    //the total number of atoms minus number of atoms belong to cutoff group defined in meta-data
127 >    //file plus the number of cutoff groups defined in meta-data file
128 >    nGlobalCutoffGroups_ = nGlobalAtoms_ - nCutoffAtoms + nGroups;
129  
130 <  excludes = Exclude::Instance();
130 >    //every free atom (atom does not belong to rigid bodies) is an integrable object
131 >    //therefore the total number of  integrable objects in the system is equal to
132 >    //the total number of atoms minus number of atoms belong to  rigid body defined in meta-data
133 >    //file plus the number of  rigid bodies defined in meta-data file
134 >    nGlobalIntegrableObjects_ = nGlobalAtoms_ - nRigidAtoms + nGlobalRigidBodies_;
135  
136 <  myConfiguration = new SimState();
136 >    nGlobalMols_ = molStampIds_.size();
137  
138 <  has_minimizer = false;
139 <  the_minimizer =NULL;
138 > #ifdef IS_MPI    
139 >    molToProcMap_.resize(nGlobalMols_);
140 > #endif
141  
142 <  ngroup = 0;
142 > }
143  
144 <  wrapMeSimInfo( this );
144 > SimInfo::~SimInfo() {
145 >    std::map<int, Molecule*>::iterator i;
146 >    for (i = molecules_.begin(); i != molecules_.end(); ++i) {
147 >        delete i->second;
148 >    }
149 >    molecules_.clear();
150 >    
151 >    MemoryUtils::deletePointers(moleculeStamps_);
152 >    
153 >    delete sman_;
154 >    delete simParams_;
155 >    delete forceField_;
156   }
157  
158 + int SimInfo::getNGlobalConstraints() {
159 +    int nGlobalConstraints;
160 + #ifdef IS_MPI
161 +    MPI_Allreduce(&nConstraints_, &nGlobalConstraints, 1, MPI_INT, MPI_SUM,
162 +                  MPI_COMM_WORLD);    
163 + #else
164 +    nGlobalConstraints =  nConstraints_;
165 + #endif
166 +    return nGlobalConstraints;
167 + }
168  
169 < SimInfo::~SimInfo(){
169 > bool SimInfo::addMolecule(Molecule* mol) {
170 >    MoleculeIterator i;
171  
172 <  delete myConfiguration;
172 >    i = molecules_.find(mol->getGlobalIndex());
173 >    if (i == molecules_.end() ) {
174  
175 <  map<string, GenericData*>::iterator i;
176 <  
177 <  for(i = properties.begin(); i != properties.end(); i++)
178 <    delete (*i).second;
175 >        molecules_.insert(std::make_pair(mol->getGlobalIndex(), mol));
176 >        
177 >        nAtoms_ += mol->getNAtoms();
178 >        nBonds_ += mol->getNBonds();
179 >        nBends_ += mol->getNBends();
180 >        nTorsions_ += mol->getNTorsions();
181 >        nRigidBodies_ += mol->getNRigidBodies();
182 >        nIntegrableObjects_ += mol->getNIntegrableObjects();
183 >        nCutoffGroups_ += mol->getNCutoffGroups();
184 >        nConstraints_ += mol->getNConstraintPairs();
185  
186 +        addExcludePairs(mol);
187 +        
188 +        return true;
189 +    } else {
190 +        return false;
191 +    }
192   }
193  
194 < void SimInfo::setBox(double newBox[3]) {
195 <  
196 <  int i, j;
97 <  double tempMat[3][3];
194 > bool SimInfo::removeMolecule(Molecule* mol) {
195 >    MoleculeIterator i;
196 >    i = molecules_.find(mol->getGlobalIndex());
197  
198 <  for(i=0; i<3; i++)
100 <    for (j=0; j<3; j++) tempMat[i][j] = 0.0;;
198 >    if (i != molecules_.end() ) {
199  
200 <  tempMat[0][0] = newBox[0];
201 <  tempMat[1][1] = newBox[1];
202 <  tempMat[2][2] = newBox[2];
200 >        assert(mol == i->second);
201 >        
202 >        nAtoms_ -= mol->getNAtoms();
203 >        nBonds_ -= mol->getNBonds();
204 >        nBends_ -= mol->getNBends();
205 >        nTorsions_ -= mol->getNTorsions();
206 >        nRigidBodies_ -= mol->getNRigidBodies();
207 >        nIntegrableObjects_ -= mol->getNIntegrableObjects();
208 >        nCutoffGroups_ -= mol->getNCutoffGroups();
209 >        nConstraints_ -= mol->getNConstraintPairs();
210  
211 <  setBoxM( tempMat );
211 >        removeExcludePairs(mol);
212 >        molecules_.erase(mol->getGlobalIndex());
213  
214 < }
214 >        delete mol;
215 >        
216 >        return true;
217 >    } else {
218 >        return false;
219 >    }
220  
110 void SimInfo::setBoxM( double theBox[3][3] ){
111  
112  int i, j;
113  double FortranHmat[9]; // to preserve compatibility with Fortran the
114                         // ordering in the array is as follows:
115                         // [ 0 3 6 ]
116                         // [ 1 4 7 ]
117                         // [ 2 5 8 ]
118  double FortranHmatInv[9]; // the inverted Hmat (for Fortran);
221  
222 <  if( !boxIsInit ) boxIsInit = 1;
222 > }    
223  
224 <  for(i=0; i < 3; i++)
225 <    for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j];
226 <  
227 <  calcBoxL();
228 <  calcHmatInv();
224 >        
225 > Molecule* SimInfo::beginMolecule(MoleculeIterator& i) {
226 >    i = molecules_.begin();
227 >    return i == molecules_.end() ? NULL : i->second;
228 > }    
229  
230 <  for(i=0; i < 3; i++) {
231 <    for (j=0; j < 3; j++) {
232 <      FortranHmat[3*j + i] = Hmat[i][j];
131 <      FortranHmatInv[3*j + i] = HmatInv[i][j];
132 <    }
133 <  }
134 <
135 <  setFortranBoxSize(FortranHmat, FortranHmatInv, &orthoRhombic);
136 <
230 > Molecule* SimInfo::nextMolecule(MoleculeIterator& i) {
231 >    ++i;
232 >    return i == molecules_.end() ? NULL : i->second;    
233   }
138
234  
140 void SimInfo::getBoxM (double theBox[3][3]) {
235  
236 <  int i, j;
237 <  for(i=0; i<3; i++)
238 <    for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j];
239 < }
236 > void SimInfo::calcNdf() {
237 >    int ndf_local;
238 >    MoleculeIterator i;
239 >    std::vector<StuntDouble*>::iterator j;
240 >    Molecule* mol;
241 >    StuntDouble* integrableObject;
242  
243 +    ndf_local = 0;
244 +    
245 +    for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
246 +        for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
247 +               integrableObject = mol->nextIntegrableObject(j)) {
248  
249 < void SimInfo::scaleBox(double scale) {
149 <  double theBox[3][3];
150 <  int i, j;
249 >            ndf_local += 3;
250  
251 <  // cerr << "Scaling box by " << scale << "\n";
251 >            if (integrableObject->isDirectional()) {
252 >                if (integrableObject->isLinear()) {
253 >                    ndf_local += 2;
254 >                } else {
255 >                    ndf_local += 3;
256 >                }
257 >            }
258 >            
259 >        }//end for (integrableObject)
260 >    }// end for (mol)
261 >    
262 >    // n_constraints is local, so subtract them on each processor
263 >    ndf_local -= nConstraints_;
264  
265 <  for(i=0; i<3; i++)
266 <    for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]*scale;
265 > #ifdef IS_MPI
266 >    MPI_Allreduce(&ndf_local,&ndf_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
267 > #else
268 >    ndf_ = ndf_local;
269 > #endif
270  
271 <  setBoxM(theBox);
271 >    // nZconstraints_ is global, as are the 3 COM translations for the
272 >    // entire system:
273 >    ndf_ = ndf_ - 3 - nZconstraint_;
274  
275   }
276  
277 < void SimInfo::calcHmatInv( void ) {
278 <  
163 <  int oldOrtho;
164 <  int i,j;
165 <  double smallDiag;
166 <  double tol;
167 <  double sanity[3][3];
277 > void SimInfo::calcNdfRaw() {
278 >    int ndfRaw_local;
279  
280 <  invertMat3( Hmat, HmatInv );
280 >    MoleculeIterator i;
281 >    std::vector<StuntDouble*>::iterator j;
282 >    Molecule* mol;
283 >    StuntDouble* integrableObject;
284  
285 <  // check to see if Hmat is orthorhombic
286 <  
287 <  oldOrtho = orthoRhombic;
285 >    // Raw degrees of freedom that we have to set
286 >    ndfRaw_local = 0;
287 >    
288 >    for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
289 >        for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
290 >               integrableObject = mol->nextIntegrableObject(j)) {
291  
292 <  smallDiag = fabs(Hmat[0][0]);
176 <  if(smallDiag > fabs(Hmat[1][1])) smallDiag = fabs(Hmat[1][1]);
177 <  if(smallDiag > fabs(Hmat[2][2])) smallDiag = fabs(Hmat[2][2]);
178 <  tol = smallDiag * orthoTolerance;
292 >            ndfRaw_local += 3;
293  
294 <  orthoRhombic = 1;
295 <  
296 <  for (i = 0; i < 3; i++ ) {
297 <    for (j = 0 ; j < 3; j++) {
298 <      if (i != j) {
299 <        if (orthoRhombic) {
300 <          if ( fabs(Hmat[i][j]) >= tol) orthoRhombic = 0;
301 <        }        
302 <      }
294 >            if (integrableObject->isDirectional()) {
295 >                if (integrableObject->isLinear()) {
296 >                    ndfRaw_local += 2;
297 >                } else {
298 >                    ndfRaw_local += 3;
299 >                }
300 >            }
301 >            
302 >        }
303      }
190  }
191
192  if( oldOrtho != orthoRhombic ){
304      
305 <    if( orthoRhombic ) {
306 <      sprintf( painCave.errMsg,
307 <               "OOPSE is switching from the default Non-Orthorhombic\n"
308 <               "\tto the faster Orthorhombic periodic boundary computations.\n"
309 <               "\tThis is usually a good thing, but if you wan't the\n"
199 <               "\tNon-Orthorhombic computations, make the orthoBoxTolerance\n"
200 <               "\tvariable ( currently set to %G ) smaller.\n",
201 <               orthoTolerance);
202 <      painCave.severity = OOPSE_INFO;
203 <      simError();
204 <    }
205 <    else {
206 <      sprintf( painCave.errMsg,
207 <               "OOPSE is switching from the faster Orthorhombic to the more\n"
208 <               "\tflexible Non-Orthorhombic periodic boundary computations.\n"
209 <               "\tThis is usually because the box has deformed under\n"
210 <               "\tNPTf integration. If you wan't to live on the edge with\n"
211 <               "\tthe Orthorhombic computations, make the orthoBoxTolerance\n"
212 <               "\tvariable ( currently set to %G ) larger.\n",
213 <               orthoTolerance);
214 <      painCave.severity = OOPSE_WARNING;
215 <      simError();
216 <    }
217 <  }
305 > #ifdef IS_MPI
306 >    MPI_Allreduce(&ndfRaw_local,&ndfRaw_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
307 > #else
308 >    ndfRaw_ = ndfRaw_local;
309 > #endif
310   }
311  
312 < void SimInfo::calcBoxL( void ){
312 > void SimInfo::calcNdfTrans() {
313 >    int ndfTrans_local;
314  
315 <  double dx, dy, dz, dsq;
315 >    ndfTrans_local = 3 * nIntegrableObjects_ - nConstraints_;
316  
224  // boxVol = Determinant of Hmat
317  
318 <  boxVol = matDet3( Hmat );
318 > #ifdef IS_MPI
319 >    MPI_Allreduce(&ndfTrans_local,&ndfTrans_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
320 > #else
321 >    ndfTrans_ = ndfTrans_local;
322 > #endif
323  
324 <  // boxLx
325 <  
326 <  dx = Hmat[0][0]; dy = Hmat[1][0]; dz = Hmat[2][0];
231 <  dsq = dx*dx + dy*dy + dz*dz;
232 <  boxL[0] = sqrt( dsq );
233 <  //maxCutoff = 0.5 * boxL[0];
324 >    ndfTrans_ = ndfTrans_ - 3 - nZconstraint_;
325 >
326 > }
327  
328 <  // boxLy
329 <  
330 <  dx = Hmat[0][1]; dy = Hmat[1][1]; dz = Hmat[2][1];
331 <  dsq = dx*dx + dy*dy + dz*dz;
332 <  boxL[1] = sqrt( dsq );
333 <  //if( (0.5 * boxL[1]) < maxCutoff ) maxCutoff = 0.5 * boxL[1];
328 > void SimInfo::addExcludePairs(Molecule* mol) {
329 >    std::vector<Bond*>::iterator bondIter;
330 >    std::vector<Bend*>::iterator bendIter;
331 >    std::vector<Torsion*>::iterator torsionIter;
332 >    Bond* bond;
333 >    Bend* bend;
334 >    Torsion* torsion;
335 >    int a;
336 >    int b;
337 >    int c;
338 >    int d;
339 >    
340 >    for (bond= mol->beginBond(bondIter); bond != NULL; bond = mol->nextBond(bondIter)) {
341 >        a = bond->getAtomA()->getGlobalIndex();
342 >        b = bond->getAtomB()->getGlobalIndex();        
343 >        exclude_.addPair(a, b);
344 >    }
345  
346 +    for (bend= mol->beginBend(bendIter); bend != NULL; bend = mol->nextBend(bendIter)) {
347 +        a = bend->getAtomA()->getGlobalIndex();
348 +        b = bend->getAtomB()->getGlobalIndex();        
349 +        c = bend->getAtomC()->getGlobalIndex();
350  
351 <  // boxLz
352 <  
353 <  dx = Hmat[0][2]; dy = Hmat[1][2]; dz = Hmat[2][2];
354 <  dsq = dx*dx + dy*dy + dz*dz;
247 <  boxL[2] = sqrt( dsq );
248 <  //if( (0.5 * boxL[2]) < maxCutoff ) maxCutoff = 0.5 * boxL[2];
351 >        exclude_.addPair(a, b);
352 >        exclude_.addPair(a, c);
353 >        exclude_.addPair(b, c);        
354 >    }
355  
356 <  //calculate the max cutoff
357 <  maxCutoff =  calcMaxCutOff();
358 <  
359 <  checkCutOffs();
356 >    for (torsion= mol->beginTorsion(torsionIter); torsion != NULL; torsion = mol->nextTorsion(torsionIter)) {
357 >        a = torsion->getAtomA()->getGlobalIndex();
358 >        b = torsion->getAtomB()->getGlobalIndex();        
359 >        c = torsion->getAtomC()->getGlobalIndex();        
360 >        d = torsion->getAtomD()->getGlobalIndex();        
361  
362 < }
362 >        exclude_.addPair(a, b);
363 >        exclude_.addPair(a, c);
364 >        exclude_.addPair(a, d);
365 >        exclude_.addPair(b, c);
366 >        exclude_.addPair(b, d);
367 >        exclude_.addPair(c, d);        
368 >    }
369  
370 +    Molecule::RigidBodyIterator rbIter;
371 +    RigidBody* rb;
372 +    for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
373 +        std::vector<Atom*> atoms = rb->getAtoms();
374 +        for (int i = 0; i < atoms.size() -1 ; ++i) {
375 +            for (int j = i + 1; j < atoms.size(); ++j) {
376 +                a = atoms[i]->getGlobalIndex();
377 +                b = atoms[j]->getGlobalIndex();
378 +                exclude_.addPair(a, b);
379 +            }
380 +        }
381 +    }        
382  
383 < double SimInfo::calcMaxCutOff(){
383 >    Molecule::CutoffGroupIterator cgIter;
384 >    CutoffGroup* cg;
385 >    for (cg = mol->beginCutoffGroup(cgIter); cg != NULL; cg = mol->nextCutoffGroup(cgIter)) {
386 >        std::vector<Atom*> atoms = cg->getAtoms();
387 >        for (int i = 0; i < atoms.size() -1 ; ++i) {
388 >            for (int j = i + 1; j < atoms.size(); ++j) {
389 >                a = atoms[i]->getGlobalIndex();
390 >                b = atoms[j]->getGlobalIndex();
391 >                exclude_.addPair(a, b);
392 >            }
393 >        }
394 >    }  
395  
396 <  double ri[3], rj[3], rk[3];
261 <  double rij[3], rjk[3], rki[3];
262 <  double minDist;
396 > }
397  
398 <  ri[0] = Hmat[0][0];
399 <  ri[1] = Hmat[1][0];
400 <  ri[2] = Hmat[2][0];
398 > void SimInfo::removeExcludePairs(Molecule* mol) {
399 >    std::vector<Bond*>::iterator bondIter;
400 >    std::vector<Bend*>::iterator bendIter;
401 >    std::vector<Torsion*>::iterator torsionIter;
402 >    Bond* bond;
403 >    Bend* bend;
404 >    Torsion* torsion;
405 >    int a;
406 >    int b;
407 >    int c;
408 >    int d;
409 >    
410 >    for (bond= mol->beginBond(bondIter); bond != NULL; bond = mol->nextBond(bondIter)) {
411 >        a = bond->getAtomA()->getGlobalIndex();
412 >        b = bond->getAtomB()->getGlobalIndex();        
413 >        exclude_.removePair(a, b);
414 >    }
415  
416 <  rj[0] = Hmat[0][1];
417 <  rj[1] = Hmat[1][1];
418 <  rj[2] = Hmat[2][1];
416 >    for (bend= mol->beginBend(bendIter); bend != NULL; bend = mol->nextBend(bendIter)) {
417 >        a = bend->getAtomA()->getGlobalIndex();
418 >        b = bend->getAtomB()->getGlobalIndex();        
419 >        c = bend->getAtomC()->getGlobalIndex();
420  
421 <  rk[0] = Hmat[0][2];
422 <  rk[1] = Hmat[1][2];
423 <  rk[2] = Hmat[2][2];
424 <    
276 <  crossProduct3(ri, rj, rij);
277 <  distXY = dotProduct3(rk,rij) / norm3(rij);
421 >        exclude_.removePair(a, b);
422 >        exclude_.removePair(a, c);
423 >        exclude_.removePair(b, c);        
424 >    }
425  
426 <  crossProduct3(rj,rk, rjk);
427 <  distYZ = dotProduct3(ri,rjk) / norm3(rjk);
426 >    for (torsion= mol->beginTorsion(torsionIter); torsion != NULL; torsion = mol->nextTorsion(torsionIter)) {
427 >        a = torsion->getAtomA()->getGlobalIndex();
428 >        b = torsion->getAtomB()->getGlobalIndex();        
429 >        c = torsion->getAtomC()->getGlobalIndex();        
430 >        d = torsion->getAtomD()->getGlobalIndex();        
431  
432 <  crossProduct3(rk,ri, rki);
433 <  distZX = dotProduct3(rj,rki) / norm3(rki);
432 >        exclude_.removePair(a, b);
433 >        exclude_.removePair(a, c);
434 >        exclude_.removePair(a, d);
435 >        exclude_.removePair(b, c);
436 >        exclude_.removePair(b, d);
437 >        exclude_.removePair(c, d);        
438 >    }
439  
440 <  minDist = min(min(distXY, distYZ), distZX);
441 <  return minDist/2;
442 <  
443 < }
440 >    Molecule::RigidBodyIterator rbIter;
441 >    RigidBody* rb;
442 >    for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
443 >        std::vector<Atom*> atoms = rb->getAtoms();
444 >        for (int i = 0; i < atoms.size() -1 ; ++i) {
445 >            for (int j = i + 1; j < atoms.size(); ++j) {
446 >                a = atoms[i]->getGlobalIndex();
447 >                b = atoms[j]->getGlobalIndex();
448 >                exclude_.removePair(a, b);
449 >            }
450 >        }
451 >    }        
452  
453 < void SimInfo::wrapVector( double thePos[3] ){
453 >    Molecule::CutoffGroupIterator cgIter;
454 >    CutoffGroup* cg;
455 >    for (cg = mol->beginCutoffGroup(cgIter); cg != NULL; cg = mol->nextCutoffGroup(cgIter)) {
456 >        std::vector<Atom*> atoms = cg->getAtoms();
457 >        for (int i = 0; i < atoms.size() -1 ; ++i) {
458 >            for (int j = i + 1; j < atoms.size(); ++j) {
459 >                a = atoms[i]->getGlobalIndex();
460 >                b = atoms[j]->getGlobalIndex();
461 >                exclude_.removePair(a, b);
462 >            }
463 >        }
464 >    }  
465  
466 <  int i;
293 <  double scaled[3];
466 > }
467  
295  if( !orthoRhombic ){
296    // calc the scaled coordinates.
297  
468  
469 <    matVecMul3(HmatInv, thePos, scaled);
470 <    
301 <    for(i=0; i<3; i++)
302 <      scaled[i] -= roundMe(scaled[i]);
303 <    
304 <    // calc the wrapped real coordinates from the wrapped scaled coordinates
305 <    
306 <    matVecMul3(Hmat, scaled, thePos);
469 > void SimInfo::addMoleculeStamp(MoleculeStamp* molStamp, int nmol) {
470 >    int curStampId;
471  
472 <  }
473 <  else{
474 <    // calc the scaled coordinates.
475 <    
476 <    for(i=0; i<3; i++)
313 <      scaled[i] = thePos[i]*HmatInv[i][i];
314 <    
315 <    // wrap the scaled coordinates
316 <    
317 <    for(i=0; i<3; i++)
318 <      scaled[i] -= roundMe(scaled[i]);
319 <    
320 <    // calc the wrapped real coordinates from the wrapped scaled coordinates
321 <    
322 <    for(i=0; i<3; i++)
323 <      thePos[i] = scaled[i]*Hmat[i][i];
324 <  }
325 <    
472 >    //index from 0
473 >    curStampId = moleculeStamps_.size();
474 >
475 >    moleculeStamps_.push_back(molStamp);
476 >    molStampIds_.insert(molStampIds_.end(), nmol, curStampId);
477   }
478  
479 + void SimInfo::update() {
480  
481 < int SimInfo::getNDF(){
330 <  int ndf_local;
481 >    setupSimType();
482  
483 <  ndf_local = 0;
484 <  
485 <  for(int i = 0; i < integrableObjects.size(); i++){
486 <    ndf_local += 3;
487 <    if (integrableObjects[i]->isDirectional()) {
488 <      if (integrableObjects[i]->isLinear())
489 <        ndf_local += 2;
490 <      else
491 <        ndf_local += 3;
483 > #ifdef IS_MPI
484 >    setupFortranParallel();
485 > #endif
486 >
487 >    setupFortranSim();
488 >
489 >    //setup fortran force field
490 >    /** @deprecate */    
491 >    int isError = 0;
492 >    initFortranFF( &fInfo_.SIM_uses_RF , &isError );
493 >    if(isError){
494 >        sprintf( painCave.errMsg,
495 >         "ForceField error: There was an error initializing the forceField in fortran.\n" );
496 >        painCave.isFatal = 1;
497 >        simError();
498      }
499 <  }
499 >  
500 >    
501 >    setupCutoff();
502  
503 <  // n_constraints is local, so subtract them on each processor:
503 >    calcNdf();
504 >    calcNdfRaw();
505 >    calcNdfTrans();
506  
507 <  ndf_local -= n_constraints;
507 >    fortranInitialized_ = true;
508 > }
509  
510 < #ifdef IS_MPI
511 <  MPI_Allreduce(&ndf_local,&ndf,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
512 < #else
513 <  ndf = ndf_local;
514 < #endif
510 > std::set<AtomType*> SimInfo::getUniqueAtomTypes() {
511 >    SimInfo::MoleculeIterator mi;
512 >    Molecule* mol;
513 >    Molecule::AtomIterator ai;
514 >    Atom* atom;
515 >    std::set<AtomType*> atomTypes;
516  
517 <  // nZconstraints is global, as are the 3 COM translations for the
355 <  // entire system:
517 >    for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {
518  
519 <  ndf = ndf - 3 - nZconstraints;
519 >        for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
520 >            atomTypes.insert(atom->getAtomType());
521 >        }
522 >        
523 >    }
524  
525 <  return ndf;
525 >    return atomTypes;        
526   }
527  
528 < int SimInfo::getNDFraw() {
529 <  int ndfRaw_local;
528 > void SimInfo::setupSimType() {
529 >    std::set<AtomType*>::iterator i;
530 >    std::set<AtomType*> atomTypes;
531 >    atomTypes = getUniqueAtomTypes();
532 >    
533 >    int useLennardJones = 0;
534 >    int useElectrostatic = 0;
535 >    int useEAM = 0;
536 >    int useCharge = 0;
537 >    int useDirectional = 0;
538 >    int useDipole = 0;
539 >    int useGayBerne = 0;
540 >    int useSticky = 0;
541 >    int useShape = 0;
542 >    int useFLARB = 0; //it is not in AtomType yet
543 >    int useDirectionalAtom = 0;    
544 >    int useElectrostatics = 0;
545 >    //usePBC and useRF are from simParams
546 >    int usePBC = simParams_->getPBC();
547 >    int useRF = simParams_->getUseRF();
548  
549 <  // Raw degrees of freedom that we have to set
550 <  ndfRaw_local = 0;
549 >    //loop over all of the atom types
550 >    for (i = atomTypes.begin(); i != atomTypes.end(); ++i) {
551 >        useLennardJones |= (*i)->isLennardJones();
552 >        useElectrostatic |= (*i)->isElectrostatic();
553 >        useEAM |= (*i)->isEAM();
554 >        useCharge |= (*i)->isCharge();
555 >        useDirectional |= (*i)->isDirectional();
556 >        useDipole |= (*i)->isDipole();
557 >        useGayBerne |= (*i)->isGayBerne();
558 >        useSticky |= (*i)->isSticky();
559 >        useShape |= (*i)->isShape();
560 >    }
561  
562 <  for(int i = 0; i < integrableObjects.size(); i++){
563 <    ndfRaw_local += 3;
370 <    if (integrableObjects[i]->isDirectional()) {
371 <       if (integrableObjects[i]->isLinear())
372 <        ndfRaw_local += 2;
373 <      else
374 <        ndfRaw_local += 3;
562 >    if (useSticky || useDipole || useGayBerne || useShape) {
563 >        useDirectionalAtom = 1;
564      }
376  }
377    
378 #ifdef IS_MPI
379  MPI_Allreduce(&ndfRaw_local,&ndfRaw,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
380 #else
381  ndfRaw = ndfRaw_local;
382 #endif
565  
566 <  return ndfRaw;
567 < }
566 >    if (useCharge || useDipole) {
567 >        useElectrostatics = 1;
568 >    }
569  
570 < int SimInfo::getNDFtranslational() {
571 <  int ndfTrans_local;
570 > #ifdef IS_MPI    
571 >    int temp;
572  
573 <  ndfTrans_local = 3 * integrableObjects.size() - n_constraints;
573 >    temp = usePBC;
574 >    MPI_Allreduce(&temp, &usePBC, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
575  
576 +    temp = useDirectionalAtom;
577 +    MPI_Allreduce(&temp, &useDirectionalAtom, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
578  
579 < #ifdef IS_MPI
580 <  MPI_Allreduce(&ndfTrans_local,&ndfTrans,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
395 < #else
396 <  ndfTrans = ndfTrans_local;
397 < #endif
579 >    temp = useLennardJones;
580 >    MPI_Allreduce(&temp, &useLennardJones, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
581  
582 <  ndfTrans = ndfTrans - 3 - nZconstraints;
582 >    temp = useElectrostatics;
583 >    MPI_Allreduce(&temp, &useElectrostatics, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
584  
585 <  return ndfTrans;
586 < }
585 >    temp = useCharge;
586 >    MPI_Allreduce(&temp, &useCharge, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
587  
588 < int SimInfo::getTotIntegrableObjects() {
589 <  int nObjs_local;
406 <  int nObjs;
588 >    temp = useDipole;
589 >    MPI_Allreduce(&temp, &useDipole, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
590  
591 <  nObjs_local =  integrableObjects.size();
591 >    temp = useSticky;
592 >    MPI_Allreduce(&temp, &useSticky, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
593  
594 +    temp = useGayBerne;
595 +    MPI_Allreduce(&temp, &useGayBerne, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
596  
597 < #ifdef IS_MPI
598 <  MPI_Allreduce(&nObjs_local,&nObjs,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
599 < #else
600 <  nObjs = nObjs_local;
597 >    temp = useEAM;
598 >    MPI_Allreduce(&temp, &useEAM, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
599 >
600 >    temp = useShape;
601 >    MPI_Allreduce(&temp, &useShape, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);  
602 >
603 >    temp = useFLARB;
604 >    MPI_Allreduce(&temp, &useFLARB, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
605 >
606 >    temp = useRF;
607 >    MPI_Allreduce(&temp, &useRF, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD);    
608 >    
609   #endif
610  
611 +    fInfo_.SIM_uses_PBC = usePBC;    
612 +    fInfo_.SIM_uses_DirectionalAtoms = useDirectionalAtom;
613 +    fInfo_.SIM_uses_LennardJones = useLennardJones;
614 +    fInfo_.SIM_uses_Electrostatics = useElectrostatics;    
615 +    fInfo_.SIM_uses_Charges = useCharge;
616 +    fInfo_.SIM_uses_Dipoles = useDipole;
617 +    fInfo_.SIM_uses_Sticky = useSticky;
618 +    fInfo_.SIM_uses_GayBerne = useGayBerne;
619 +    fInfo_.SIM_uses_EAM = useEAM;
620 +    fInfo_.SIM_uses_Shapes = useShape;
621 +    fInfo_.SIM_uses_FLARB = useFLARB;
622 +    fInfo_.SIM_uses_RF = useRF;
623  
624 <  return nObjs;
624 >    if( fInfo_.SIM_uses_Dipoles && fInfo_.SIM_uses_RF) {
625 >
626 >        if (simParams_->haveDielectric()) {
627 >            fInfo_.dielect = simParams_->getDielectric();
628 >        } else {
629 >            sprintf(painCave.errMsg,
630 >                    "SimSetup Error: No Dielectric constant was set.\n"
631 >                    "\tYou are trying to use Reaction Field without"
632 >                    "\tsetting a dielectric constant!\n");
633 >            painCave.isFatal = 1;
634 >            simError();
635 >        }
636 >        
637 >    } else {
638 >        fInfo_.dielect = 0.0;
639 >    }
640 >
641   }
642  
643 < void SimInfo::refreshSim(){
643 > void SimInfo::setupFortranSim() {
644 >    int isError;
645 >    int nExclude;
646 >    std::vector<int> fortranGlobalGroupMembership;
647 >    
648 >    nExclude = exclude_.getSize();
649 >    isError = 0;
650  
651 <  simtype fInfo;
652 <  int isError;
653 <  int n_global;
654 <  int* excl;
651 >    //globalGroupMembership_ is filled by SimCreator    
652 >    for (int i = 0; i < nGlobalAtoms_; i++) {
653 >        fortranGlobalGroupMembership.push_back(globalGroupMembership_[i] + 1);
654 >    }
655  
656 <  fInfo.dielect = 0.0;
656 >    //calculate mass ratio of cutoff group
657 >    std::vector<double> mfact;
658 >    SimInfo::MoleculeIterator mi;
659 >    Molecule* mol;
660 >    Molecule::CutoffGroupIterator ci;
661 >    CutoffGroup* cg;
662 >    Molecule::AtomIterator ai;
663 >    Atom* atom;
664 >    double totalMass;
665  
666 <  if( useDipoles ){
667 <    if( useReactionField )fInfo.dielect = dielectric;
668 <  }
666 >    //to avoid memory reallocation, reserve enough space for mfact
667 >    mfact.reserve(getNCutoffGroups());
668 >    
669 >    for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {        
670 >        for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
671  
672 <  fInfo.SIM_uses_PBC = usePBC;
673 <  //fInfo.SIM_uses_LJ = 0;
674 <  fInfo.SIM_uses_LJ = useLJ;
675 <  fInfo.SIM_uses_sticky = useSticky;
438 <  //fInfo.SIM_uses_sticky = 0;
439 <  fInfo.SIM_uses_charges = useCharges;
440 <  fInfo.SIM_uses_dipoles = useDipoles;
441 <  //fInfo.SIM_uses_dipoles = 0;
442 <  fInfo.SIM_uses_RF = useReactionField;
443 <  //fInfo.SIM_uses_RF = 0;
444 <  fInfo.SIM_uses_GB = useGB;
445 <  fInfo.SIM_uses_EAM = useEAM;
672 >            totalMass = cg->getMass();
673 >            for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) {
674 >                        mfact.push_back(atom->getMass()/totalMass);
675 >            }
676  
677 <  n_exclude = excludes->getSize();
678 <  excl = excludes->getFortranArray();
449 <  
450 < #ifdef IS_MPI
451 <  n_global = mpiSim->getNAtomsGlobal();
452 < #else
453 <  n_global = n_atoms;
454 < #endif
455 <  
456 <  isError = 0;
457 <  
458 <  getFortranGroupArrays(this, FglobalGroupMembership, mfact);
459 <  //it may not be a good idea to pass the address of first element in vector
460 <  //since c++ standard does not require vector to be stored continuously in meomory
461 <  //Most of the compilers will organize the memory of vector continuously
462 <  setFsimulation( &fInfo, &n_global, &n_atoms, identArray, &n_exclude, excl,
463 <                  &nGlobalExcludes, globalExcludes, molMembershipArray,
464 <                  &mfact[0], &ngroup, &FglobalGroupMembership[0], &isError);
677 >        }      
678 >    }
679  
680 <  if( isError ){
680 >    //fill ident array of local atoms (it is actually ident of AtomType, it is so confusing !!!)
681 >    std::vector<int> identArray;
682 >
683 >    //to avoid memory reallocation, reserve enough space identArray
684 >    identArray.reserve(getNAtoms());
685      
686 <    sprintf( painCave.errMsg,
687 <             "There was an error setting the simulation information in fortran.\n" );
688 <    painCave.isFatal = 1;
689 <    painCave.severity = OOPSE_ERROR;
690 <    simError();
691 <  }
692 <  
686 >    for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {        
687 >        for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
688 >            identArray.push_back(atom->getIdent());
689 >        }
690 >    }    
691 >
692 >    //fill molMembershipArray
693 >    //molMembershipArray is filled by SimCreator    
694 >    std::vector<int> molMembershipArray(nGlobalAtoms_);
695 >    for (int i = 0; i < nGlobalAtoms_; i++) {
696 >        molMembershipArray[i] = globalMolMembership_[i] + 1;
697 >    }
698 >    
699 >    //setup fortran simulation
700 >    int nGlobalExcludes = 0;
701 >    int* globalExcludes = NULL;
702 >    int* excludeList = exclude_.getExcludeList();
703 >    setFortranSim( &fInfo_, &nGlobalAtoms_, &nAtoms_, &identArray[0], &nExclude, excludeList ,
704 >                  &nGlobalExcludes, globalExcludes, &molMembershipArray[0],
705 >                  &mfact[0], &nCutoffGroups_, &fortranGlobalGroupMembership[0], &isError);
706 >
707 >    if( isError ){
708 >
709 >        sprintf( painCave.errMsg,
710 >                 "There was an error setting the simulation information in fortran.\n" );
711 >        painCave.isFatal = 1;
712 >        painCave.severity = OOPSE_ERROR;
713 >        simError();
714 >    }
715 >
716   #ifdef IS_MPI
717 <  sprintf( checkPointMsg,
718 <           "succesfully sent the simulation information to fortran.\n");
719 <  MPIcheckPoint();
717 >    sprintf( checkPointMsg,
718 >       "succesfully sent the simulation information to fortran.\n");
719 >    MPIcheckPoint();
720   #endif // is_mpi
480  
481  this->ndf = this->getNDF();
482  this->ndfRaw = this->getNDFraw();
483  this->ndfTrans = this->getNDFtranslational();
721   }
722  
486 void SimInfo::setDefaultRcut( double theRcut ){
487  
488  haveRcut = 1;
489  rCut = theRcut;
490  rList = rCut + 1.0;
491  
492  notifyFortranCutOffs( &rCut, &rSw, &rList );
493 }
723  
724 < void SimInfo::setDefaultRcut( double theRcut, double theRsw ){
724 > #ifdef IS_MPI
725 > void SimInfo::setupFortranParallel() {
726 >    
727 >    //SimInfo is responsible for creating localToGlobalAtomIndex and localToGlobalGroupIndex
728 >    std::vector<int> localToGlobalAtomIndex(getNAtoms(), 0);
729 >    std::vector<int> localToGlobalCutoffGroupIndex;
730 >    SimInfo::MoleculeIterator mi;
731 >    Molecule::AtomIterator ai;
732 >    Molecule::CutoffGroupIterator ci;
733 >    Molecule* mol;
734 >    Atom* atom;
735 >    CutoffGroup* cg;
736 >    mpiSimData parallelData;
737 >    int isError;
738  
739 <  rSw = theRsw;
498 <  setDefaultRcut( theRcut );
499 < }
739 >    for (mol = beginMolecule(mi); mol != NULL; mol  = nextMolecule(mi)) {
740  
741 +        //local index(index in DataStorge) of atom is important
742 +        for (atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
743 +            localToGlobalAtomIndex[atom->getLocalIndex()] = atom->getGlobalIndex() + 1;
744 +        }
745  
746 < void SimInfo::checkCutOffs( void ){
747 <  
748 <  if( boxIsInit ){
749 <    
750 <    //we need to check cutOffs against the box
751 <    
752 <    if( rCut > maxCutoff ){
753 <      sprintf( painCave.errMsg,
754 <               "cutoffRadius is too large for the current periodic box.\n"
755 <               "\tCurrent Value of cutoffRadius = %G at time %G\n "
756 <               "\tThis is larger than half of at least one of the\n"
757 <               "\tperiodic box vectors.  Right now, the Box matrix is:\n"
758 <               "\n"
759 <               "\t[ %G %G %G ]\n"
760 <               "\t[ %G %G %G ]\n"
761 <               "\t[ %G %G %G ]\n",
762 <               rCut, currentTime,
763 <               Hmat[0][0], Hmat[0][1], Hmat[0][2],
764 <               Hmat[1][0], Hmat[1][1], Hmat[1][2],
765 <               Hmat[2][0], Hmat[2][1], Hmat[2][2]);
766 <      painCave.severity = OOPSE_ERROR;
767 <      painCave.isFatal = 1;
768 <      simError();
769 <    }    
770 <  } else {
771 <    // initialize this stuff before using it, OK?
772 <    sprintf( painCave.errMsg,
773 <             "Trying to check cutoffs without a box.\n"
774 <             "\tOOPSE should have better programmers than that.\n" );
775 <    painCave.severity = OOPSE_ERROR;
776 <    painCave.isFatal = 1;
777 <    simError();      
778 <  }
535 <  
746 >        //local index of cutoff group is trivial, it only depends on the order of travesing
747 >        for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
748 >            localToGlobalCutoffGroupIndex.push_back(cg->getGlobalIndex() + 1);
749 >        }        
750 >        
751 >    }
752 >
753 >    //fill up mpiSimData struct
754 >    parallelData.nMolGlobal = getNGlobalMolecules();
755 >    parallelData.nMolLocal = getNMolecules();
756 >    parallelData.nAtomsGlobal = getNGlobalAtoms();
757 >    parallelData.nAtomsLocal = getNAtoms();
758 >    parallelData.nGroupsGlobal = getNGlobalCutoffGroups();
759 >    parallelData.nGroupsLocal = getNCutoffGroups();
760 >    parallelData.myNode = worldRank;
761 >    MPI_Comm_size(MPI_COMM_WORLD, &(parallelData.nProcessors));
762 >
763 >    //pass mpiSimData struct and index arrays to fortran
764 >    setFsimParallel(&parallelData, &(parallelData.nAtomsLocal),
765 >                    &localToGlobalAtomIndex[0],  &(parallelData.nGroupsLocal),
766 >                    &localToGlobalCutoffGroupIndex[0], &isError);
767 >
768 >    if (isError) {
769 >        sprintf(painCave.errMsg,
770 >                "mpiRefresh errror: fortran didn't like something we gave it.\n");
771 >        painCave.isFatal = 1;
772 >        simError();
773 >    }
774 >
775 >    sprintf(checkPointMsg, " mpiRefresh successful.\n");
776 >    MPIcheckPoint();
777 >
778 >
779   }
780  
781 < void SimInfo::addProperty(GenericData* prop){
781 > #endif
782  
783 <  map<string, GenericData*>::iterator result;
784 <  result = properties.find(prop->getID());
785 <  
786 <  //we can't simply use  properties[prop->getID()] = prop,
787 <  //it will cause memory leak if we already contain a propery which has the same name of prop
788 <  
789 <  if(result != properties.end()){
783 > double SimInfo::calcMaxCutoffRadius() {
784 >
785 >
786 >    std::set<AtomType*> atomTypes;
787 >    std::set<AtomType*>::iterator i;
788 >    std::vector<double> cutoffRadius;
789 >
790 >    //get the unique atom types
791 >    atomTypes = getUniqueAtomTypes();
792 >
793 >    //query the max cutoff radius among these atom types
794 >    for (i = atomTypes.begin(); i != atomTypes.end(); ++i) {
795 >        cutoffRadius.push_back(forceField_->getRcutFromAtomType(*i));
796 >    }
797 >
798 >    double maxCutoffRadius = *(std::max_element(cutoffRadius.begin(), cutoffRadius.end()));
799 > #ifdef IS_MPI
800 >    //pick the max cutoff radius among the processors
801 > #endif
802 >
803 >    return maxCutoffRadius;
804 > }
805 >
806 > void SimInfo::getCutoff(double& rcut, double& rsw) {
807      
808 <    delete (*result).second;
809 <    (*result).second = prop;
808 >    if (fInfo_.SIM_uses_Charges | fInfo_.SIM_uses_Dipoles | fInfo_.SIM_uses_RF) {
809 >        
810 >        if (!simParams_->haveRcut()){
811 >            sprintf(painCave.errMsg,
812 >                "SimCreator Warning: No value was set for the cutoffRadius.\n"
813 >                "\tOOPSE will use a default value of 15.0 angstroms"
814 >                "\tfor the cutoffRadius.\n");
815 >            painCave.isFatal = 0;
816 >            simError();
817 >            rcut = 15.0;
818 >        } else{
819 >            rcut = simParams_->getRcut();
820 >        }
821 >
822 >        if (!simParams_->haveRsw()){
823 >            sprintf(painCave.errMsg,
824 >                "SimCreator Warning: No value was set for switchingRadius.\n"
825 >                "\tOOPSE will use a default value of\n"
826 >                "\t0.95 * cutoffRadius for the switchingRadius\n");
827 >            painCave.isFatal = 0;
828 >            simError();
829 >            rsw = 0.95 * rcut;
830 >        } else{
831 >            rsw = simParams_->getRsw();
832 >        }
833 >
834 >    } else {
835 >        // if charge, dipole or reaction field is not used and the cutofff radius is not specified in
836 >        //meta-data file, the maximum cutoff radius calculated from forcefiled will be used
837 >        
838 >        if (simParams_->haveRcut()) {
839 >            rcut = simParams_->getRcut();
840 >        } else {
841 >            //set cutoff radius to the maximum cutoff radius based on atom types in the whole system
842 >            rcut = calcMaxCutoffRadius();
843 >        }
844 >
845 >        if (simParams_->haveRsw()) {
846 >            rsw  = simParams_->getRsw();
847 >        } else {
848 >            rsw = rcut;
849 >        }
850 >    
851 >    }
852 > }
853 >
854 > void SimInfo::setupCutoff() {
855 >    getCutoff(rcut_, rsw_);    
856 >    double rnblist = rcut_ + 1; // skin of neighbor list
857 >
858 >    //Pass these cutoff radius etc. to fortran. This function should be called once and only once
859 >    notifyFortranCutoffs(&rcut_, &rsw_, &rnblist);
860 > }
861 >
862 > void SimInfo::addProperty(GenericData* genData) {
863 >    properties_.addProperty(genData);  
864 > }
865 >
866 > void SimInfo::removeProperty(const std::string& propName) {
867 >    properties_.removeProperty(propName);  
868 > }
869 >
870 > void SimInfo::clearProperties() {
871 >    properties_.clearProperties();
872 > }
873 >
874 > std::vector<std::string> SimInfo::getPropertyNames() {
875 >    return properties_.getPropertyNames();  
876 > }
877        
878 <  }
879 <  else{
878 > std::vector<GenericData*> SimInfo::getProperties() {
879 >    return properties_.getProperties();
880 > }
881  
882 <    properties[prop->getID()] = prop;
882 > GenericData* SimInfo::getPropertyByName(const std::string& propName) {
883 >    return properties_.getPropertyByName(propName);
884 > }
885  
886 <  }
886 > void SimInfo::setSnapshotManager(SnapshotManager* sman) {
887 >    if (sman_ == sman) {
888 >        return;
889 >    }    
890 >    delete sman_;
891 >    sman_ = sman;
892 >
893 >    Molecule* mol;
894 >    RigidBody* rb;
895 >    Atom* atom;
896 >    SimInfo::MoleculeIterator mi;
897 >    Molecule::RigidBodyIterator rbIter;
898 >    Molecule::AtomIterator atomIter;;
899 >
900 >    for (mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) {
901 >        
902 >        for (atom = mol->beginAtom(atomIter); atom != NULL; atom = mol->nextAtom(atomIter)) {
903 >            atom->setSnapshotManager(sman_);
904 >        }
905 >        
906 >        for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) {
907 >            rb->setSnapshotManager(sman_);
908 >        }
909 >    }    
910      
911   }
912  
913 < GenericData* SimInfo::getProperty(const string& propName){
913 > Vector3d SimInfo::getComVel(){
914 >    SimInfo::MoleculeIterator i;
915 >    Molecule* mol;
916 >
917 >    Vector3d comVel(0.0);
918 >    double totalMass = 0.0;
919 >    
920  
921 <  map<string, GenericData*>::iterator result;
922 <  
923 <  //string lowerCaseName = ();
924 <  
925 <  result = properties.find(propName);
926 <  
927 <  if(result != properties.end())
928 <    return (*result).second;  
929 <  else  
930 <    return NULL;  
921 >    for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
922 >        double mass = mol->getMass();
923 >        totalMass += mass;
924 >        comVel += mass * mol->getComVel();
925 >    }  
926 >
927 > #ifdef IS_MPI
928 >    double tmpMass = totalMass;
929 >    Vector3d tmpComVel(comVel);    
930 >    MPI_Allreduce(&tmpMass,&totalMass,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
931 >    MPI_Allreduce(tmpComVel.getArrayPointer(), comVel.getArrayPointer(),3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
932 > #endif
933 >
934 >    comVel /= totalMass;
935 >
936 >    return comVel;
937   }
938  
939 + Vector3d SimInfo::getCom(){
940 +    SimInfo::MoleculeIterator i;
941 +    Molecule* mol;
942  
943 < void SimInfo::getFortranGroupArrays(SimInfo* info,
944 <                                    vector<int>& FglobalGroupMembership,
945 <                                    vector<double>& mfact){
946 <  
947 <  Molecule* myMols;
948 <  Atom** myAtoms;
949 <  int numAtom;
950 <  double mtot;
583 <  int numMol;
584 <  int numCutoffGroups;
585 <  CutoffGroup* myCutoffGroup;
586 <  vector<CutoffGroup*>::iterator iterCutoff;
587 <  Atom* cutoffAtom;
588 <  vector<Atom*>::iterator iterAtom;
589 <  int atomIndex;
590 <  double totalMass;
591 <  
592 <  mfact.clear();
593 <  FglobalGroupMembership.clear();
594 <  
943 >    Vector3d com(0.0);
944 >    double totalMass = 0.0;
945 >    
946 >    for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) {
947 >        double mass = mol->getMass();
948 >        totalMass += mass;
949 >        com += mass * mol->getCom();
950 >    }  
951  
596  // Fix the silly fortran indexing problem
952   #ifdef IS_MPI
953 <  numAtom = mpiSim->getNAtomsGlobal();
954 < #else
955 <  numAtom = n_atoms;
953 >    double tmpMass = totalMass;
954 >    Vector3d tmpCom(com);    
955 >    MPI_Allreduce(&tmpMass,&totalMass,1,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
956 >    MPI_Allreduce(tmpCom.getArrayPointer(), com.getArrayPointer(),3,MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);
957   #endif
602  for (int i = 0; i < numAtom; i++)
603    FglobalGroupMembership.push_back(globalGroupMembership[i] + 1);
604  
958  
959 <  myMols = info->molecules;
607 <  numMol = info->n_mol;
608 <  for(int i  = 0; i < numMol; i++){
609 <    numCutoffGroups = myMols[i].getNCutoffGroups();
610 <    for(myCutoffGroup =myMols[i].beginCutoffGroup(iterCutoff);
611 <        myCutoffGroup != NULL;
612 <        myCutoffGroup =myMols[i].nextCutoffGroup(iterCutoff)){
959 >    com /= totalMass;
960  
961 <      totalMass = myCutoffGroup->getMass();
615 <      
616 <      for(cutoffAtom = myCutoffGroup->beginAtom(iterAtom);
617 <          cutoffAtom != NULL;
618 <          cutoffAtom = myCutoffGroup->nextAtom(iterAtom)){
619 <        mfact.push_back(cutoffAtom->getMass()/totalMass);
620 <      }  
621 <    }
622 <  }
961 >    return com;
962  
963 + }        
964 +
965 + std::ostream& operator <<(std::ostream& o, SimInfo& info) {
966 +
967 +    return o;
968   }
969 +
970 + }//end namespace oopse
971 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines