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. 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 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
*/ |
42 |
|
43 |
/** |
44 |
* @file SimInfo.cpp |
45 |
* @author tlin |
46 |
* @date 11/02/2004 |
47 |
* @version 1.0 |
48 |
*/ |
49 |
|
50 |
#include <algorithm> |
51 |
#include <set> |
52 |
#include <map> |
53 |
|
54 |
#include "brains/SimInfo.hpp" |
55 |
#include "math/Vector3.hpp" |
56 |
#include "primitives/Molecule.hpp" |
57 |
#include "primitives/StuntDouble.hpp" |
58 |
#include "utils/MemoryUtils.hpp" |
59 |
#include "utils/simError.h" |
60 |
#include "selection/SelectionManager.hpp" |
61 |
#include "io/ForceFieldOptions.hpp" |
62 |
#include "brains/ForceField.hpp" |
63 |
#include "nonbonded/SwitchingFunction.hpp" |
64 |
#ifdef IS_MPI |
65 |
#include <mpi.h> |
66 |
#endif |
67 |
|
68 |
using namespace std; |
69 |
namespace OpenMD { |
70 |
|
71 |
SimInfo::SimInfo(ForceField* ff, Globals* simParams) : |
72 |
forceField_(ff), simParams_(simParams), |
73 |
ndf_(0), fdf_local(0), ndfRaw_(0), ndfTrans_(0), nZconstraint_(0), |
74 |
nGlobalMols_(0), nGlobalAtoms_(0), nGlobalCutoffGroups_(0), |
75 |
nGlobalIntegrableObjects_(0), nGlobalRigidBodies_(0), nGlobalFluctuatingCharges_(0), |
76 |
nAtoms_(0), nBonds_(0), nBends_(0), nTorsions_(0), nInversions_(0), |
77 |
nRigidBodies_(0), nIntegrableObjects_(0), nCutoffGroups_(0), |
78 |
nConstraints_(0), nFluctuatingCharges_(0), sman_(NULL), topologyDone_(false), |
79 |
calcBoxDipole_(false), useAtomicVirial_(true) { |
80 |
|
81 |
MoleculeStamp* molStamp; |
82 |
int nMolWithSameStamp; |
83 |
int nCutoffAtoms = 0; // number of atoms belong to cutoff groups |
84 |
int nGroups = 0; //total cutoff groups defined in meta-data file |
85 |
CutoffGroupStamp* cgStamp; |
86 |
RigidBodyStamp* rbStamp; |
87 |
int nRigidAtoms = 0; |
88 |
|
89 |
vector<Component*> components = simParams->getComponents(); |
90 |
|
91 |
for (vector<Component*>::iterator i = components.begin(); i !=components.end(); ++i) { |
92 |
molStamp = (*i)->getMoleculeStamp(); |
93 |
nMolWithSameStamp = (*i)->getNMol(); |
94 |
|
95 |
addMoleculeStamp(molStamp, nMolWithSameStamp); |
96 |
|
97 |
//calculate atoms in molecules |
98 |
nGlobalAtoms_ += molStamp->getNAtoms() *nMolWithSameStamp; |
99 |
|
100 |
//calculate atoms in cutoff groups |
101 |
int nAtomsInGroups = 0; |
102 |
int nCutoffGroupsInStamp = molStamp->getNCutoffGroups(); |
103 |
|
104 |
for (int j=0; j < nCutoffGroupsInStamp; j++) { |
105 |
cgStamp = molStamp->getCutoffGroupStamp(j); |
106 |
nAtomsInGroups += cgStamp->getNMembers(); |
107 |
} |
108 |
|
109 |
nGroups += nCutoffGroupsInStamp * nMolWithSameStamp; |
110 |
|
111 |
nCutoffAtoms += nAtomsInGroups * nMolWithSameStamp; |
112 |
|
113 |
//calculate atoms in rigid bodies |
114 |
int nAtomsInRigidBodies = 0; |
115 |
int nRigidBodiesInStamp = molStamp->getNRigidBodies(); |
116 |
|
117 |
for (int j=0; j < nRigidBodiesInStamp; j++) { |
118 |
rbStamp = molStamp->getRigidBodyStamp(j); |
119 |
nAtomsInRigidBodies += rbStamp->getNMembers(); |
120 |
} |
121 |
|
122 |
nGlobalRigidBodies_ += nRigidBodiesInStamp * nMolWithSameStamp; |
123 |
nRigidAtoms += nAtomsInRigidBodies * nMolWithSameStamp; |
124 |
|
125 |
} |
126 |
|
127 |
//every free atom (atom does not belong to cutoff groups) is a cutoff |
128 |
//group therefore the total number of cutoff groups in the system is |
129 |
//equal to the total number of atoms minus number of atoms belong to |
130 |
//cutoff group defined in meta-data file plus the number of cutoff |
131 |
//groups defined in meta-data file |
132 |
|
133 |
nGlobalCutoffGroups_ = nGlobalAtoms_ - nCutoffAtoms + nGroups; |
134 |
|
135 |
//every free atom (atom does not belong to rigid bodies) is an |
136 |
//integrable object therefore the total number of integrable objects |
137 |
//in the system is equal to the total number of atoms minus number of |
138 |
//atoms belong to rigid body defined in meta-data file plus the number |
139 |
//of rigid bodies defined in meta-data file |
140 |
nGlobalIntegrableObjects_ = nGlobalAtoms_ - nRigidAtoms |
141 |
+ nGlobalRigidBodies_; |
142 |
|
143 |
nGlobalMols_ = molStampIds_.size(); |
144 |
molToProcMap_.resize(nGlobalMols_); |
145 |
} |
146 |
|
147 |
SimInfo::~SimInfo() { |
148 |
map<int, Molecule*>::iterator i; |
149 |
for (i = molecules_.begin(); i != molecules_.end(); ++i) { |
150 |
delete i->second; |
151 |
} |
152 |
molecules_.clear(); |
153 |
|
154 |
delete sman_; |
155 |
delete simParams_; |
156 |
delete forceField_; |
157 |
} |
158 |
|
159 |
|
160 |
bool SimInfo::addMolecule(Molecule* mol) { |
161 |
MoleculeIterator i; |
162 |
|
163 |
i = molecules_.find(mol->getGlobalIndex()); |
164 |
if (i == molecules_.end() ) { |
165 |
|
166 |
molecules_.insert(make_pair(mol->getGlobalIndex(), mol)); |
167 |
|
168 |
nAtoms_ += mol->getNAtoms(); |
169 |
nBonds_ += mol->getNBonds(); |
170 |
nBends_ += mol->getNBends(); |
171 |
nTorsions_ += mol->getNTorsions(); |
172 |
nInversions_ += mol->getNInversions(); |
173 |
nRigidBodies_ += mol->getNRigidBodies(); |
174 |
nIntegrableObjects_ += mol->getNIntegrableObjects(); |
175 |
nCutoffGroups_ += mol->getNCutoffGroups(); |
176 |
nConstraints_ += mol->getNConstraintPairs(); |
177 |
|
178 |
addInteractionPairs(mol); |
179 |
|
180 |
return true; |
181 |
} else { |
182 |
return false; |
183 |
} |
184 |
} |
185 |
|
186 |
bool SimInfo::removeMolecule(Molecule* mol) { |
187 |
MoleculeIterator i; |
188 |
i = molecules_.find(mol->getGlobalIndex()); |
189 |
|
190 |
if (i != molecules_.end() ) { |
191 |
|
192 |
assert(mol == i->second); |
193 |
|
194 |
nAtoms_ -= mol->getNAtoms(); |
195 |
nBonds_ -= mol->getNBonds(); |
196 |
nBends_ -= mol->getNBends(); |
197 |
nTorsions_ -= mol->getNTorsions(); |
198 |
nInversions_ -= mol->getNInversions(); |
199 |
nRigidBodies_ -= mol->getNRigidBodies(); |
200 |
nIntegrableObjects_ -= mol->getNIntegrableObjects(); |
201 |
nCutoffGroups_ -= mol->getNCutoffGroups(); |
202 |
nConstraints_ -= mol->getNConstraintPairs(); |
203 |
|
204 |
removeInteractionPairs(mol); |
205 |
molecules_.erase(mol->getGlobalIndex()); |
206 |
|
207 |
delete mol; |
208 |
|
209 |
return true; |
210 |
} else { |
211 |
return false; |
212 |
} |
213 |
} |
214 |
|
215 |
|
216 |
Molecule* SimInfo::beginMolecule(MoleculeIterator& i) { |
217 |
i = molecules_.begin(); |
218 |
return i == molecules_.end() ? NULL : i->second; |
219 |
} |
220 |
|
221 |
Molecule* SimInfo::nextMolecule(MoleculeIterator& i) { |
222 |
++i; |
223 |
return i == molecules_.end() ? NULL : i->second; |
224 |
} |
225 |
|
226 |
|
227 |
void SimInfo::calcNdf() { |
228 |
int ndf_local, nfq_local; |
229 |
MoleculeIterator i; |
230 |
vector<StuntDouble*>::iterator j; |
231 |
vector<Atom*>::iterator k; |
232 |
|
233 |
Molecule* mol; |
234 |
StuntDouble* integrableObject; |
235 |
Atom* atom; |
236 |
|
237 |
ndf_local = 0; |
238 |
nfq_local = 0; |
239 |
|
240 |
for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) { |
241 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
242 |
integrableObject = mol->nextIntegrableObject(j)) { |
243 |
|
244 |
ndf_local += 3; |
245 |
|
246 |
if (integrableObject->isDirectional()) { |
247 |
if (integrableObject->isLinear()) { |
248 |
ndf_local += 2; |
249 |
} else { |
250 |
ndf_local += 3; |
251 |
} |
252 |
} |
253 |
} |
254 |
for (atom = mol->beginFluctuatingCharge(k); atom != NULL; |
255 |
atom = mol->nextFluctuatingCharge(k)) { |
256 |
if (atom->isFluctuatingCharge()) { |
257 |
nfq_local++; |
258 |
} |
259 |
} |
260 |
} |
261 |
|
262 |
ndfLocal_ = ndf_local; |
263 |
|
264 |
// n_constraints is local, so subtract them on each processor |
265 |
ndf_local -= nConstraints_; |
266 |
|
267 |
#ifdef IS_MPI |
268 |
MPI_Allreduce(&ndf_local,&ndf_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
269 |
MPI_Allreduce(&nfq_local,&nGlobalFluctuatingCharges_,1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
270 |
#else |
271 |
ndf_ = ndf_local; |
272 |
nGlobalFluctuatingCharges_ = nfq_local; |
273 |
#endif |
274 |
|
275 |
// nZconstraints_ is global, as are the 3 COM translations for the |
276 |
// entire system: |
277 |
ndf_ = ndf_ - 3 - nZconstraint_; |
278 |
|
279 |
} |
280 |
|
281 |
int SimInfo::getFdf() { |
282 |
#ifdef IS_MPI |
283 |
MPI_Allreduce(&fdf_local,&fdf_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
284 |
#else |
285 |
fdf_ = fdf_local; |
286 |
#endif |
287 |
return fdf_; |
288 |
} |
289 |
|
290 |
unsigned int SimInfo::getNLocalCutoffGroups(){ |
291 |
int nLocalCutoffAtoms = 0; |
292 |
Molecule* mol; |
293 |
MoleculeIterator mi; |
294 |
CutoffGroup* cg; |
295 |
Molecule::CutoffGroupIterator ci; |
296 |
|
297 |
for (mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) { |
298 |
|
299 |
for (cg = mol->beginCutoffGroup(ci); cg != NULL; |
300 |
cg = mol->nextCutoffGroup(ci)) { |
301 |
nLocalCutoffAtoms += cg->getNumAtom(); |
302 |
|
303 |
} |
304 |
} |
305 |
|
306 |
return nAtoms_ - nLocalCutoffAtoms + nCutoffGroups_; |
307 |
} |
308 |
|
309 |
void SimInfo::calcNdfRaw() { |
310 |
int ndfRaw_local; |
311 |
|
312 |
MoleculeIterator i; |
313 |
vector<StuntDouble*>::iterator j; |
314 |
Molecule* mol; |
315 |
StuntDouble* integrableObject; |
316 |
|
317 |
// Raw degrees of freedom that we have to set |
318 |
ndfRaw_local = 0; |
319 |
|
320 |
for (mol = beginMolecule(i); mol != NULL; mol = nextMolecule(i)) { |
321 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
322 |
integrableObject = mol->nextIntegrableObject(j)) { |
323 |
|
324 |
ndfRaw_local += 3; |
325 |
|
326 |
if (integrableObject->isDirectional()) { |
327 |
if (integrableObject->isLinear()) { |
328 |
ndfRaw_local += 2; |
329 |
} else { |
330 |
ndfRaw_local += 3; |
331 |
} |
332 |
} |
333 |
|
334 |
} |
335 |
} |
336 |
|
337 |
#ifdef IS_MPI |
338 |
MPI_Allreduce(&ndfRaw_local,&ndfRaw_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
339 |
#else |
340 |
ndfRaw_ = ndfRaw_local; |
341 |
#endif |
342 |
} |
343 |
|
344 |
void SimInfo::calcNdfTrans() { |
345 |
int ndfTrans_local; |
346 |
|
347 |
ndfTrans_local = 3 * nIntegrableObjects_ - nConstraints_; |
348 |
|
349 |
|
350 |
#ifdef IS_MPI |
351 |
MPI_Allreduce(&ndfTrans_local,&ndfTrans_,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
352 |
#else |
353 |
ndfTrans_ = ndfTrans_local; |
354 |
#endif |
355 |
|
356 |
ndfTrans_ = ndfTrans_ - 3 - nZconstraint_; |
357 |
|
358 |
} |
359 |
|
360 |
void SimInfo::addInteractionPairs(Molecule* mol) { |
361 |
ForceFieldOptions& options_ = forceField_->getForceFieldOptions(); |
362 |
vector<Bond*>::iterator bondIter; |
363 |
vector<Bend*>::iterator bendIter; |
364 |
vector<Torsion*>::iterator torsionIter; |
365 |
vector<Inversion*>::iterator inversionIter; |
366 |
Bond* bond; |
367 |
Bend* bend; |
368 |
Torsion* torsion; |
369 |
Inversion* inversion; |
370 |
int a; |
371 |
int b; |
372 |
int c; |
373 |
int d; |
374 |
|
375 |
// atomGroups can be used to add special interaction maps between |
376 |
// groups of atoms that are in two separate rigid bodies. |
377 |
// However, most site-site interactions between two rigid bodies |
378 |
// are probably not special, just the ones between the physically |
379 |
// bonded atoms. Interactions *within* a single rigid body should |
380 |
// always be excluded. These are done at the bottom of this |
381 |
// function. |
382 |
|
383 |
map<int, set<int> > atomGroups; |
384 |
Molecule::RigidBodyIterator rbIter; |
385 |
RigidBody* rb; |
386 |
Molecule::IntegrableObjectIterator ii; |
387 |
StuntDouble* integrableObject; |
388 |
|
389 |
for (integrableObject = mol->beginIntegrableObject(ii); |
390 |
integrableObject != NULL; |
391 |
integrableObject = mol->nextIntegrableObject(ii)) { |
392 |
|
393 |
if (integrableObject->isRigidBody()) { |
394 |
rb = static_cast<RigidBody*>(integrableObject); |
395 |
vector<Atom*> atoms = rb->getAtoms(); |
396 |
set<int> rigidAtoms; |
397 |
for (int i = 0; i < static_cast<int>(atoms.size()); ++i) { |
398 |
rigidAtoms.insert(atoms[i]->getGlobalIndex()); |
399 |
} |
400 |
for (int i = 0; i < static_cast<int>(atoms.size()); ++i) { |
401 |
atomGroups.insert(map<int, set<int> >::value_type(atoms[i]->getGlobalIndex(), rigidAtoms)); |
402 |
} |
403 |
} else { |
404 |
set<int> oneAtomSet; |
405 |
oneAtomSet.insert(integrableObject->getGlobalIndex()); |
406 |
atomGroups.insert(map<int, set<int> >::value_type(integrableObject->getGlobalIndex(), oneAtomSet)); |
407 |
} |
408 |
} |
409 |
|
410 |
for (bond= mol->beginBond(bondIter); bond != NULL; |
411 |
bond = mol->nextBond(bondIter)) { |
412 |
|
413 |
a = bond->getAtomA()->getGlobalIndex(); |
414 |
b = bond->getAtomB()->getGlobalIndex(); |
415 |
|
416 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
417 |
oneTwoInteractions_.addPair(a, b); |
418 |
} else { |
419 |
excludedInteractions_.addPair(a, b); |
420 |
} |
421 |
} |
422 |
|
423 |
for (bend= mol->beginBend(bendIter); bend != NULL; |
424 |
bend = mol->nextBend(bendIter)) { |
425 |
|
426 |
a = bend->getAtomA()->getGlobalIndex(); |
427 |
b = bend->getAtomB()->getGlobalIndex(); |
428 |
c = bend->getAtomC()->getGlobalIndex(); |
429 |
|
430 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
431 |
oneTwoInteractions_.addPair(a, b); |
432 |
oneTwoInteractions_.addPair(b, c); |
433 |
} else { |
434 |
excludedInteractions_.addPair(a, b); |
435 |
excludedInteractions_.addPair(b, c); |
436 |
} |
437 |
|
438 |
if (options_.havevdw13scale() || options_.haveelectrostatic13scale()) { |
439 |
oneThreeInteractions_.addPair(a, c); |
440 |
} else { |
441 |
excludedInteractions_.addPair(a, c); |
442 |
} |
443 |
} |
444 |
|
445 |
for (torsion= mol->beginTorsion(torsionIter); torsion != NULL; |
446 |
torsion = mol->nextTorsion(torsionIter)) { |
447 |
|
448 |
a = torsion->getAtomA()->getGlobalIndex(); |
449 |
b = torsion->getAtomB()->getGlobalIndex(); |
450 |
c = torsion->getAtomC()->getGlobalIndex(); |
451 |
d = torsion->getAtomD()->getGlobalIndex(); |
452 |
|
453 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
454 |
oneTwoInteractions_.addPair(a, b); |
455 |
oneTwoInteractions_.addPair(b, c); |
456 |
oneTwoInteractions_.addPair(c, d); |
457 |
} else { |
458 |
excludedInteractions_.addPair(a, b); |
459 |
excludedInteractions_.addPair(b, c); |
460 |
excludedInteractions_.addPair(c, d); |
461 |
} |
462 |
|
463 |
if (options_.havevdw13scale() || options_.haveelectrostatic13scale()) { |
464 |
oneThreeInteractions_.addPair(a, c); |
465 |
oneThreeInteractions_.addPair(b, d); |
466 |
} else { |
467 |
excludedInteractions_.addPair(a, c); |
468 |
excludedInteractions_.addPair(b, d); |
469 |
} |
470 |
|
471 |
if (options_.havevdw14scale() || options_.haveelectrostatic14scale()) { |
472 |
oneFourInteractions_.addPair(a, d); |
473 |
} else { |
474 |
excludedInteractions_.addPair(a, d); |
475 |
} |
476 |
} |
477 |
|
478 |
for (inversion= mol->beginInversion(inversionIter); inversion != NULL; |
479 |
inversion = mol->nextInversion(inversionIter)) { |
480 |
|
481 |
a = inversion->getAtomA()->getGlobalIndex(); |
482 |
b = inversion->getAtomB()->getGlobalIndex(); |
483 |
c = inversion->getAtomC()->getGlobalIndex(); |
484 |
d = inversion->getAtomD()->getGlobalIndex(); |
485 |
|
486 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
487 |
oneTwoInteractions_.addPair(a, b); |
488 |
oneTwoInteractions_.addPair(a, c); |
489 |
oneTwoInteractions_.addPair(a, d); |
490 |
} else { |
491 |
excludedInteractions_.addPair(a, b); |
492 |
excludedInteractions_.addPair(a, c); |
493 |
excludedInteractions_.addPair(a, d); |
494 |
} |
495 |
|
496 |
if (options_.havevdw13scale() || options_.haveelectrostatic13scale()) { |
497 |
oneThreeInteractions_.addPair(b, c); |
498 |
oneThreeInteractions_.addPair(b, d); |
499 |
oneThreeInteractions_.addPair(c, d); |
500 |
} else { |
501 |
excludedInteractions_.addPair(b, c); |
502 |
excludedInteractions_.addPair(b, d); |
503 |
excludedInteractions_.addPair(c, d); |
504 |
} |
505 |
} |
506 |
|
507 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
508 |
rb = mol->nextRigidBody(rbIter)) { |
509 |
vector<Atom*> atoms = rb->getAtoms(); |
510 |
for (int i = 0; i < static_cast<int>(atoms.size()) -1 ; ++i) { |
511 |
for (int j = i + 1; j < static_cast<int>(atoms.size()); ++j) { |
512 |
a = atoms[i]->getGlobalIndex(); |
513 |
b = atoms[j]->getGlobalIndex(); |
514 |
excludedInteractions_.addPair(a, b); |
515 |
} |
516 |
} |
517 |
} |
518 |
|
519 |
} |
520 |
|
521 |
void SimInfo::removeInteractionPairs(Molecule* mol) { |
522 |
ForceFieldOptions& options_ = forceField_->getForceFieldOptions(); |
523 |
vector<Bond*>::iterator bondIter; |
524 |
vector<Bend*>::iterator bendIter; |
525 |
vector<Torsion*>::iterator torsionIter; |
526 |
vector<Inversion*>::iterator inversionIter; |
527 |
Bond* bond; |
528 |
Bend* bend; |
529 |
Torsion* torsion; |
530 |
Inversion* inversion; |
531 |
int a; |
532 |
int b; |
533 |
int c; |
534 |
int d; |
535 |
|
536 |
map<int, set<int> > atomGroups; |
537 |
Molecule::RigidBodyIterator rbIter; |
538 |
RigidBody* rb; |
539 |
Molecule::IntegrableObjectIterator ii; |
540 |
StuntDouble* integrableObject; |
541 |
|
542 |
for (integrableObject = mol->beginIntegrableObject(ii); |
543 |
integrableObject != NULL; |
544 |
integrableObject = mol->nextIntegrableObject(ii)) { |
545 |
|
546 |
if (integrableObject->isRigidBody()) { |
547 |
rb = static_cast<RigidBody*>(integrableObject); |
548 |
vector<Atom*> atoms = rb->getAtoms(); |
549 |
set<int> rigidAtoms; |
550 |
for (int i = 0; i < static_cast<int>(atoms.size()); ++i) { |
551 |
rigidAtoms.insert(atoms[i]->getGlobalIndex()); |
552 |
} |
553 |
for (int i = 0; i < static_cast<int>(atoms.size()); ++i) { |
554 |
atomGroups.insert(map<int, set<int> >::value_type(atoms[i]->getGlobalIndex(), rigidAtoms)); |
555 |
} |
556 |
} else { |
557 |
set<int> oneAtomSet; |
558 |
oneAtomSet.insert(integrableObject->getGlobalIndex()); |
559 |
atomGroups.insert(map<int, set<int> >::value_type(integrableObject->getGlobalIndex(), oneAtomSet)); |
560 |
} |
561 |
} |
562 |
|
563 |
for (bond= mol->beginBond(bondIter); bond != NULL; |
564 |
bond = mol->nextBond(bondIter)) { |
565 |
|
566 |
a = bond->getAtomA()->getGlobalIndex(); |
567 |
b = bond->getAtomB()->getGlobalIndex(); |
568 |
|
569 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
570 |
oneTwoInteractions_.removePair(a, b); |
571 |
} else { |
572 |
excludedInteractions_.removePair(a, b); |
573 |
} |
574 |
} |
575 |
|
576 |
for (bend= mol->beginBend(bendIter); bend != NULL; |
577 |
bend = mol->nextBend(bendIter)) { |
578 |
|
579 |
a = bend->getAtomA()->getGlobalIndex(); |
580 |
b = bend->getAtomB()->getGlobalIndex(); |
581 |
c = bend->getAtomC()->getGlobalIndex(); |
582 |
|
583 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
584 |
oneTwoInteractions_.removePair(a, b); |
585 |
oneTwoInteractions_.removePair(b, c); |
586 |
} else { |
587 |
excludedInteractions_.removePair(a, b); |
588 |
excludedInteractions_.removePair(b, c); |
589 |
} |
590 |
|
591 |
if (options_.havevdw13scale() || options_.haveelectrostatic13scale()) { |
592 |
oneThreeInteractions_.removePair(a, c); |
593 |
} else { |
594 |
excludedInteractions_.removePair(a, c); |
595 |
} |
596 |
} |
597 |
|
598 |
for (torsion= mol->beginTorsion(torsionIter); torsion != NULL; |
599 |
torsion = mol->nextTorsion(torsionIter)) { |
600 |
|
601 |
a = torsion->getAtomA()->getGlobalIndex(); |
602 |
b = torsion->getAtomB()->getGlobalIndex(); |
603 |
c = torsion->getAtomC()->getGlobalIndex(); |
604 |
d = torsion->getAtomD()->getGlobalIndex(); |
605 |
|
606 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
607 |
oneTwoInteractions_.removePair(a, b); |
608 |
oneTwoInteractions_.removePair(b, c); |
609 |
oneTwoInteractions_.removePair(c, d); |
610 |
} else { |
611 |
excludedInteractions_.removePair(a, b); |
612 |
excludedInteractions_.removePair(b, c); |
613 |
excludedInteractions_.removePair(c, d); |
614 |
} |
615 |
|
616 |
if (options_.havevdw13scale() || options_.haveelectrostatic13scale()) { |
617 |
oneThreeInteractions_.removePair(a, c); |
618 |
oneThreeInteractions_.removePair(b, d); |
619 |
} else { |
620 |
excludedInteractions_.removePair(a, c); |
621 |
excludedInteractions_.removePair(b, d); |
622 |
} |
623 |
|
624 |
if (options_.havevdw14scale() || options_.haveelectrostatic14scale()) { |
625 |
oneFourInteractions_.removePair(a, d); |
626 |
} else { |
627 |
excludedInteractions_.removePair(a, d); |
628 |
} |
629 |
} |
630 |
|
631 |
for (inversion= mol->beginInversion(inversionIter); inversion != NULL; |
632 |
inversion = mol->nextInversion(inversionIter)) { |
633 |
|
634 |
a = inversion->getAtomA()->getGlobalIndex(); |
635 |
b = inversion->getAtomB()->getGlobalIndex(); |
636 |
c = inversion->getAtomC()->getGlobalIndex(); |
637 |
d = inversion->getAtomD()->getGlobalIndex(); |
638 |
|
639 |
if (options_.havevdw12scale() || options_.haveelectrostatic12scale()) { |
640 |
oneTwoInteractions_.removePair(a, b); |
641 |
oneTwoInteractions_.removePair(a, c); |
642 |
oneTwoInteractions_.removePair(a, d); |
643 |
} else { |
644 |
excludedInteractions_.removePair(a, b); |
645 |
excludedInteractions_.removePair(a, c); |
646 |
excludedInteractions_.removePair(a, d); |
647 |
} |
648 |
|
649 |
if (options_.havevdw13scale() || options_.haveelectrostatic13scale()) { |
650 |
oneThreeInteractions_.removePair(b, c); |
651 |
oneThreeInteractions_.removePair(b, d); |
652 |
oneThreeInteractions_.removePair(c, d); |
653 |
} else { |
654 |
excludedInteractions_.removePair(b, c); |
655 |
excludedInteractions_.removePair(b, d); |
656 |
excludedInteractions_.removePair(c, d); |
657 |
} |
658 |
} |
659 |
|
660 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
661 |
rb = mol->nextRigidBody(rbIter)) { |
662 |
vector<Atom*> atoms = rb->getAtoms(); |
663 |
for (int i = 0; i < static_cast<int>(atoms.size()) -1 ; ++i) { |
664 |
for (int j = i + 1; j < static_cast<int>(atoms.size()); ++j) { |
665 |
a = atoms[i]->getGlobalIndex(); |
666 |
b = atoms[j]->getGlobalIndex(); |
667 |
excludedInteractions_.removePair(a, b); |
668 |
} |
669 |
} |
670 |
} |
671 |
|
672 |
} |
673 |
|
674 |
|
675 |
void SimInfo::addMoleculeStamp(MoleculeStamp* molStamp, int nmol) { |
676 |
int curStampId; |
677 |
|
678 |
//index from 0 |
679 |
curStampId = moleculeStamps_.size(); |
680 |
|
681 |
moleculeStamps_.push_back(molStamp); |
682 |
molStampIds_.insert(molStampIds_.end(), nmol, curStampId); |
683 |
} |
684 |
|
685 |
|
686 |
/** |
687 |
* update |
688 |
* |
689 |
* Performs the global checks and variable settings after the |
690 |
* objects have been created. |
691 |
* |
692 |
*/ |
693 |
void SimInfo::update() { |
694 |
setupSimVariables(); |
695 |
calcNdf(); |
696 |
calcNdfRaw(); |
697 |
calcNdfTrans(); |
698 |
} |
699 |
|
700 |
/** |
701 |
* getSimulatedAtomTypes |
702 |
* |
703 |
* Returns an STL set of AtomType* that are actually present in this |
704 |
* simulation. Must query all processors to assemble this information. |
705 |
* |
706 |
*/ |
707 |
set<AtomType*> SimInfo::getSimulatedAtomTypes() { |
708 |
SimInfo::MoleculeIterator mi; |
709 |
Molecule* mol; |
710 |
Molecule::AtomIterator ai; |
711 |
Atom* atom; |
712 |
set<AtomType*> atomTypes; |
713 |
|
714 |
for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) { |
715 |
for(atom = mol->beginAtom(ai); atom != NULL; |
716 |
atom = mol->nextAtom(ai)) { |
717 |
atomTypes.insert(atom->getAtomType()); |
718 |
} |
719 |
} |
720 |
|
721 |
#ifdef IS_MPI |
722 |
|
723 |
// loop over the found atom types on this processor, and add their |
724 |
// numerical idents to a vector: |
725 |
|
726 |
vector<int> foundTypes; |
727 |
set<AtomType*>::iterator i; |
728 |
for (i = atomTypes.begin(); i != atomTypes.end(); ++i) |
729 |
foundTypes.push_back( (*i)->getIdent() ); |
730 |
|
731 |
// count_local holds the number of found types on this processor |
732 |
int count_local = foundTypes.size(); |
733 |
|
734 |
int nproc = MPI::COMM_WORLD.Get_size(); |
735 |
|
736 |
// we need arrays to hold the counts and displacement vectors for |
737 |
// all processors |
738 |
vector<int> counts(nproc, 0); |
739 |
vector<int> disps(nproc, 0); |
740 |
|
741 |
// fill the counts array |
742 |
MPI::COMM_WORLD.Allgather(&count_local, 1, MPI::INT, &counts[0], |
743 |
1, MPI::INT); |
744 |
|
745 |
// use the processor counts to compute the displacement array |
746 |
disps[0] = 0; |
747 |
int totalCount = counts[0]; |
748 |
for (int iproc = 1; iproc < nproc; iproc++) { |
749 |
disps[iproc] = disps[iproc-1] + counts[iproc-1]; |
750 |
totalCount += counts[iproc]; |
751 |
} |
752 |
|
753 |
// we need a (possibly redundant) set of all found types: |
754 |
vector<int> ftGlobal(totalCount); |
755 |
|
756 |
// now spray out the foundTypes to all the other processors: |
757 |
MPI::COMM_WORLD.Allgatherv(&foundTypes[0], count_local, MPI::INT, |
758 |
&ftGlobal[0], &counts[0], &disps[0], |
759 |
MPI::INT); |
760 |
|
761 |
vector<int>::iterator j; |
762 |
|
763 |
// foundIdents is a stl set, so inserting an already found ident |
764 |
// will have no effect. |
765 |
set<int> foundIdents; |
766 |
|
767 |
for (j = ftGlobal.begin(); j != ftGlobal.end(); ++j) |
768 |
foundIdents.insert((*j)); |
769 |
|
770 |
// now iterate over the foundIdents and get the actual atom types |
771 |
// that correspond to these: |
772 |
set<int>::iterator it; |
773 |
for (it = foundIdents.begin(); it != foundIdents.end(); ++it) |
774 |
atomTypes.insert( forceField_->getAtomType((*it)) ); |
775 |
|
776 |
#endif |
777 |
|
778 |
return atomTypes; |
779 |
} |
780 |
|
781 |
void SimInfo::setupSimVariables() { |
782 |
useAtomicVirial_ = simParams_->getUseAtomicVirial(); |
783 |
// we only call setAccumulateBoxDipole if the accumulateBoxDipole parameter is true |
784 |
calcBoxDipole_ = false; |
785 |
if ( simParams_->haveAccumulateBoxDipole() ) |
786 |
if ( simParams_->getAccumulateBoxDipole() ) { |
787 |
calcBoxDipole_ = true; |
788 |
} |
789 |
|
790 |
set<AtomType*>::iterator i; |
791 |
set<AtomType*> atomTypes; |
792 |
atomTypes = getSimulatedAtomTypes(); |
793 |
bool usesElectrostatic = false; |
794 |
bool usesMetallic = false; |
795 |
bool usesDirectional = false; |
796 |
bool usesFluctuatingCharges = false; |
797 |
//loop over all of the atom types |
798 |
for (i = atomTypes.begin(); i != atomTypes.end(); ++i) { |
799 |
usesElectrostatic |= (*i)->isElectrostatic(); |
800 |
usesMetallic |= (*i)->isMetal(); |
801 |
usesDirectional |= (*i)->isDirectional(); |
802 |
usesFluctuatingCharges |= (*i)->isFluctuatingCharge(); |
803 |
} |
804 |
|
805 |
#ifdef IS_MPI |
806 |
bool temp; |
807 |
temp = usesDirectional; |
808 |
MPI::COMM_WORLD.Allreduce(&temp, &usesDirectionalAtoms_, 1, MPI::BOOL, |
809 |
MPI::LOR); |
810 |
|
811 |
temp = usesMetallic; |
812 |
MPI::COMM_WORLD.Allreduce(&temp, &usesMetallicAtoms_, 1, MPI::BOOL, |
813 |
MPI::LOR); |
814 |
|
815 |
temp = usesElectrostatic; |
816 |
MPI::COMM_WORLD.Allreduce(&temp, &usesElectrostaticAtoms_, 1, MPI::BOOL, |
817 |
MPI::LOR); |
818 |
|
819 |
temp = usesFluctuatingCharges; |
820 |
MPI::COMM_WORLD.Allreduce(&temp, &usesFluctuatingCharges_, 1, MPI::BOOL, |
821 |
MPI::LOR); |
822 |
#else |
823 |
|
824 |
usesDirectionalAtoms_ = usesDirectional; |
825 |
usesMetallicAtoms_ = usesMetallic; |
826 |
usesElectrostaticAtoms_ = usesElectrostatic; |
827 |
usesFluctuatingCharges_ = usesFluctuatingCharges; |
828 |
|
829 |
#endif |
830 |
|
831 |
requiresPrepair_ = usesMetallicAtoms_ ? true : false; |
832 |
requiresSkipCorrection_ = usesElectrostaticAtoms_ ? true : false; |
833 |
requiresSelfCorrection_ = usesElectrostaticAtoms_ ? true : false; |
834 |
} |
835 |
|
836 |
|
837 |
vector<int> SimInfo::getGlobalAtomIndices() { |
838 |
SimInfo::MoleculeIterator mi; |
839 |
Molecule* mol; |
840 |
Molecule::AtomIterator ai; |
841 |
Atom* atom; |
842 |
|
843 |
vector<int> GlobalAtomIndices(getNAtoms(), 0); |
844 |
|
845 |
for (mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) { |
846 |
|
847 |
for (atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
848 |
GlobalAtomIndices[atom->getLocalIndex()] = atom->getGlobalIndex(); |
849 |
} |
850 |
} |
851 |
return GlobalAtomIndices; |
852 |
} |
853 |
|
854 |
|
855 |
vector<int> SimInfo::getGlobalGroupIndices() { |
856 |
SimInfo::MoleculeIterator mi; |
857 |
Molecule* mol; |
858 |
Molecule::CutoffGroupIterator ci; |
859 |
CutoffGroup* cg; |
860 |
|
861 |
vector<int> GlobalGroupIndices; |
862 |
|
863 |
for (mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) { |
864 |
|
865 |
//local index of cutoff group is trivial, it only depends on the |
866 |
//order of travesing |
867 |
for (cg = mol->beginCutoffGroup(ci); cg != NULL; |
868 |
cg = mol->nextCutoffGroup(ci)) { |
869 |
GlobalGroupIndices.push_back(cg->getGlobalIndex()); |
870 |
} |
871 |
} |
872 |
return GlobalGroupIndices; |
873 |
} |
874 |
|
875 |
|
876 |
void SimInfo::prepareTopology() { |
877 |
int nExclude, nOneTwo, nOneThree, nOneFour; |
878 |
|
879 |
//calculate mass ratio of cutoff group |
880 |
SimInfo::MoleculeIterator mi; |
881 |
Molecule* mol; |
882 |
Molecule::CutoffGroupIterator ci; |
883 |
CutoffGroup* cg; |
884 |
Molecule::AtomIterator ai; |
885 |
Atom* atom; |
886 |
RealType totalMass; |
887 |
|
888 |
/** |
889 |
* The mass factor is the relative mass of an atom to the total |
890 |
* mass of the cutoff group it belongs to. By default, all atoms |
891 |
* are their own cutoff groups, and therefore have mass factors of |
892 |
* 1. We need some special handling for massless atoms, which |
893 |
* will be treated as carrying the entire mass of the cutoff |
894 |
* group. |
895 |
*/ |
896 |
massFactors_.clear(); |
897 |
massFactors_.resize(getNAtoms(), 1.0); |
898 |
|
899 |
for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) { |
900 |
for (cg = mol->beginCutoffGroup(ci); cg != NULL; |
901 |
cg = mol->nextCutoffGroup(ci)) { |
902 |
|
903 |
totalMass = cg->getMass(); |
904 |
for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) { |
905 |
// Check for massless groups - set mfact to 1 if true |
906 |
if (totalMass != 0) |
907 |
massFactors_[atom->getLocalIndex()] = atom->getMass()/totalMass; |
908 |
else |
909 |
massFactors_[atom->getLocalIndex()] = 1.0; |
910 |
} |
911 |
} |
912 |
} |
913 |
|
914 |
// Build the identArray_ |
915 |
|
916 |
identArray_.clear(); |
917 |
identArray_.reserve(getNAtoms()); |
918 |
for(mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) { |
919 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
920 |
identArray_.push_back(atom->getIdent()); |
921 |
} |
922 |
} |
923 |
|
924 |
//scan topology |
925 |
|
926 |
nExclude = excludedInteractions_.getSize(); |
927 |
nOneTwo = oneTwoInteractions_.getSize(); |
928 |
nOneThree = oneThreeInteractions_.getSize(); |
929 |
nOneFour = oneFourInteractions_.getSize(); |
930 |
|
931 |
int* excludeList = excludedInteractions_.getPairList(); |
932 |
int* oneTwoList = oneTwoInteractions_.getPairList(); |
933 |
int* oneThreeList = oneThreeInteractions_.getPairList(); |
934 |
int* oneFourList = oneFourInteractions_.getPairList(); |
935 |
|
936 |
topologyDone_ = true; |
937 |
} |
938 |
|
939 |
void SimInfo::addProperty(GenericData* genData) { |
940 |
properties_.addProperty(genData); |
941 |
} |
942 |
|
943 |
void SimInfo::removeProperty(const string& propName) { |
944 |
properties_.removeProperty(propName); |
945 |
} |
946 |
|
947 |
void SimInfo::clearProperties() { |
948 |
properties_.clearProperties(); |
949 |
} |
950 |
|
951 |
vector<string> SimInfo::getPropertyNames() { |
952 |
return properties_.getPropertyNames(); |
953 |
} |
954 |
|
955 |
vector<GenericData*> SimInfo::getProperties() { |
956 |
return properties_.getProperties(); |
957 |
} |
958 |
|
959 |
GenericData* SimInfo::getPropertyByName(const string& propName) { |
960 |
return properties_.getPropertyByName(propName); |
961 |
} |
962 |
|
963 |
void SimInfo::setSnapshotManager(SnapshotManager* sman) { |
964 |
if (sman_ == sman) { |
965 |
return; |
966 |
} |
967 |
delete sman_; |
968 |
sman_ = sman; |
969 |
|
970 |
Molecule* mol; |
971 |
RigidBody* rb; |
972 |
Atom* atom; |
973 |
CutoffGroup* cg; |
974 |
SimInfo::MoleculeIterator mi; |
975 |
Molecule::RigidBodyIterator rbIter; |
976 |
Molecule::AtomIterator atomIter; |
977 |
Molecule::CutoffGroupIterator cgIter; |
978 |
|
979 |
for (mol = beginMolecule(mi); mol != NULL; mol = nextMolecule(mi)) { |
980 |
|
981 |
for (atom = mol->beginAtom(atomIter); atom != NULL; atom = mol->nextAtom(atomIter)) { |
982 |
atom->setSnapshotManager(sman_); |
983 |
} |
984 |
|
985 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; rb = mol->nextRigidBody(rbIter)) { |
986 |
rb->setSnapshotManager(sman_); |
987 |
} |
988 |
|
989 |
for (cg = mol->beginCutoffGroup(cgIter); cg != NULL; cg = mol->nextCutoffGroup(cgIter)) { |
990 |
cg->setSnapshotManager(sman_); |
991 |
} |
992 |
} |
993 |
|
994 |
} |
995 |
|
996 |
|
997 |
ostream& operator <<(ostream& o, SimInfo& info) { |
998 |
|
999 |
return o; |
1000 |
} |
1001 |
|
1002 |
|
1003 |
StuntDouble* SimInfo::getIOIndexToIntegrableObject(int index) { |
1004 |
return IOIndexToIntegrableObject.at(index); |
1005 |
} |
1006 |
|
1007 |
void SimInfo::setIOIndexToIntegrableObject(const vector<StuntDouble*>& v) { |
1008 |
IOIndexToIntegrableObject= v; |
1009 |
} |
1010 |
/* |
1011 |
void SimInfo::setStuntDoubleFromGlobalIndex(vector<StuntDouble*> v) { |
1012 |
assert( v.size() == nAtoms_ + nRigidBodies_); |
1013 |
sdByGlobalIndex_ = v; |
1014 |
} |
1015 |
|
1016 |
StuntDouble* SimInfo::getStuntDoubleFromGlobalIndex(int index) { |
1017 |
//assert(index < nAtoms_ + nRigidBodies_); |
1018 |
return sdByGlobalIndex_.at(index); |
1019 |
} |
1020 |
*/ |
1021 |
int SimInfo::getNGlobalConstraints() { |
1022 |
int nGlobalConstraints; |
1023 |
#ifdef IS_MPI |
1024 |
MPI_Allreduce(&nConstraints_, &nGlobalConstraints, 1, MPI_INT, MPI_SUM, |
1025 |
MPI_COMM_WORLD); |
1026 |
#else |
1027 |
nGlobalConstraints = nConstraints_; |
1028 |
#endif |
1029 |
return nGlobalConstraints; |
1030 |
} |
1031 |
|
1032 |
}//end namespace OpenMD |
1033 |
|