ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/devel_omp/src/parallel/ForceMatrixDecomposition.cpp
Revision: 1588
Committed: Sat Jul 9 15:05:59 2011 UTC (13 years, 9 months ago) by gezelter
Original Path: branches/development/src/parallel/ForceMatrixDecomposition.cpp
File size: 38952 byte(s)
Log Message:
fixed one MPI bug, working on another

File Contents

# User Rev Content
1 gezelter 1539 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 chuckv 1538 *
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] Vardeman & Gezelter, in progress (2009).
40     */
41 gezelter 1549 #include "parallel/ForceMatrixDecomposition.hpp"
42 gezelter 1539 #include "math/SquareMatrix3.hpp"
43 gezelter 1544 #include "nonbonded/NonBondedInteraction.hpp"
44     #include "brains/SnapshotManager.hpp"
45 gezelter 1570 #include "brains/PairList.hpp"
46 chuckv 1538
47 gezelter 1541 using namespace std;
48 gezelter 1539 namespace OpenMD {
49 chuckv 1538
50 gezelter 1544 /**
51     * distributeInitialData is essentially a copy of the older fortran
52     * SimulationSetup
53     */
54    
55 gezelter 1549 void ForceMatrixDecomposition::distributeInitialData() {
56 gezelter 1551 snap_ = sman_->getCurrentSnapshot();
57     storageLayout_ = sman_->getStorageLayout();
58 gezelter 1571 ff_ = info_->getForceField();
59 gezelter 1567 nLocal_ = snap_->getNumberOfAtoms();
60 gezelter 1587
61 gezelter 1577 nGroups_ = info_->getNLocalCutoffGroups();
62 gezelter 1569 // gather the information for atomtype IDs (atids):
63 gezelter 1583 idents = info_->getIdentArray();
64 gezelter 1569 AtomLocalToGlobal = info_->getGlobalAtomIndices();
65     cgLocalToGlobal = info_->getGlobalGroupIndices();
66     vector<int> globalGroupMembership = info_->getGlobalGroupMembership();
67 gezelter 1586
68 gezelter 1581 massFactors = info_->getMassFactors();
69 gezelter 1584
70 gezelter 1587 PairList* excludes = info_->getExcludedInteractions();
71     PairList* oneTwo = info_->getOneTwoInteractions();
72     PairList* oneThree = info_->getOneThreeInteractions();
73     PairList* oneFour = info_->getOneFourInteractions();
74 gezelter 1569
75 gezelter 1567 #ifdef IS_MPI
76    
77     AtomCommIntRow = new Communicator<Row,int>(nLocal_);
78     AtomCommRealRow = new Communicator<Row,RealType>(nLocal_);
79     AtomCommVectorRow = new Communicator<Row,Vector3d>(nLocal_);
80     AtomCommMatrixRow = new Communicator<Row,Mat3x3d>(nLocal_);
81 gezelter 1575 AtomCommPotRow = new Communicator<Row,potVec>(nLocal_);
82 chuckv 1538
83 gezelter 1567 AtomCommIntColumn = new Communicator<Column,int>(nLocal_);
84     AtomCommRealColumn = new Communicator<Column,RealType>(nLocal_);
85     AtomCommVectorColumn = new Communicator<Column,Vector3d>(nLocal_);
86     AtomCommMatrixColumn = new Communicator<Column,Mat3x3d>(nLocal_);
87 gezelter 1575 AtomCommPotColumn = new Communicator<Column,potVec>(nLocal_);
88 gezelter 1541
89 gezelter 1567 cgCommIntRow = new Communicator<Row,int>(nGroups_);
90     cgCommVectorRow = new Communicator<Row,Vector3d>(nGroups_);
91     cgCommIntColumn = new Communicator<Column,int>(nGroups_);
92     cgCommVectorColumn = new Communicator<Column,Vector3d>(nGroups_);
93 gezelter 1551
94 gezelter 1567 nAtomsInRow_ = AtomCommIntRow->getSize();
95     nAtomsInCol_ = AtomCommIntColumn->getSize();
96     nGroupsInRow_ = cgCommIntRow->getSize();
97     nGroupsInCol_ = cgCommIntColumn->getSize();
98    
99 gezelter 1551 // Modify the data storage objects with the correct layouts and sizes:
100 gezelter 1567 atomRowData.resize(nAtomsInRow_);
101 gezelter 1551 atomRowData.setStorageLayout(storageLayout_);
102 gezelter 1567 atomColData.resize(nAtomsInCol_);
103 gezelter 1551 atomColData.setStorageLayout(storageLayout_);
104 gezelter 1567 cgRowData.resize(nGroupsInRow_);
105 gezelter 1551 cgRowData.setStorageLayout(DataStorage::dslPosition);
106 gezelter 1567 cgColData.resize(nGroupsInCol_);
107 gezelter 1551 cgColData.setStorageLayout(DataStorage::dslPosition);
108 gezelter 1575
109 gezelter 1577 identsRow.resize(nAtomsInRow_);
110     identsCol.resize(nAtomsInCol_);
111 gezelter 1549
112 gezelter 1583 AtomCommIntRow->gather(idents, identsRow);
113     AtomCommIntColumn->gather(idents, identsCol);
114 gezelter 1549
115 gezelter 1588 vector<int>::iterator it;
116     for (it = AtomLocalToGlobal.begin(); it != AtomLocalToGlobal.end(); ++it) {
117     cerr << "my AtomLocalToGlobal = " << (*it) << "\n";
118     }
119 gezelter 1549 AtomCommIntRow->gather(AtomLocalToGlobal, AtomRowToGlobal);
120     AtomCommIntColumn->gather(AtomLocalToGlobal, AtomColToGlobal);
121    
122     cgCommIntRow->gather(cgLocalToGlobal, cgRowToGlobal);
123     cgCommIntColumn->gather(cgLocalToGlobal, cgColToGlobal);
124 gezelter 1541
125 gezelter 1581 AtomCommRealRow->gather(massFactors, massFactorsRow);
126     AtomCommRealColumn->gather(massFactors, massFactorsCol);
127 gezelter 1569
128     groupListRow_.clear();
129 gezelter 1577 groupListRow_.resize(nGroupsInRow_);
130 gezelter 1569 for (int i = 0; i < nGroupsInRow_; i++) {
131     int gid = cgRowToGlobal[i];
132     for (int j = 0; j < nAtomsInRow_; j++) {
133     int aid = AtomRowToGlobal[j];
134     if (globalGroupMembership[aid] == gid)
135     groupListRow_[i].push_back(j);
136     }
137     }
138    
139     groupListCol_.clear();
140 gezelter 1577 groupListCol_.resize(nGroupsInCol_);
141 gezelter 1569 for (int i = 0; i < nGroupsInCol_; i++) {
142     int gid = cgColToGlobal[i];
143     for (int j = 0; j < nAtomsInCol_; j++) {
144     int aid = AtomColToGlobal[j];
145     if (globalGroupMembership[aid] == gid)
146     groupListCol_[i].push_back(j);
147     }
148     }
149    
150 gezelter 1587 excludesForAtom.clear();
151     excludesForAtom.resize(nAtomsInRow_);
152 gezelter 1579 toposForAtom.clear();
153     toposForAtom.resize(nAtomsInRow_);
154     topoDist.clear();
155     topoDist.resize(nAtomsInRow_);
156 gezelter 1570 for (int i = 0; i < nAtomsInRow_; i++) {
157 gezelter 1571 int iglob = AtomRowToGlobal[i];
158 gezelter 1579
159 gezelter 1570 for (int j = 0; j < nAtomsInCol_; j++) {
160 gezelter 1579 int jglob = AtomColToGlobal[j];
161    
162 gezelter 1587 if (excludes->hasPair(iglob, jglob))
163     excludesForAtom[i].push_back(j);
164 gezelter 1579
165 gezelter 1587 if (oneTwo->hasPair(iglob, jglob)) {
166 gezelter 1579 toposForAtom[i].push_back(j);
167     topoDist[i].push_back(1);
168     } else {
169 gezelter 1587 if (oneThree->hasPair(iglob, jglob)) {
170 gezelter 1579 toposForAtom[i].push_back(j);
171     topoDist[i].push_back(2);
172     } else {
173 gezelter 1587 if (oneFour->hasPair(iglob, jglob)) {
174 gezelter 1579 toposForAtom[i].push_back(j);
175     topoDist[i].push_back(3);
176     }
177     }
178 gezelter 1570 }
179     }
180     }
181    
182 gezelter 1569 #endif
183 gezelter 1579
184 gezelter 1569 groupList_.clear();
185 gezelter 1577 groupList_.resize(nGroups_);
186 gezelter 1569 for (int i = 0; i < nGroups_; i++) {
187     int gid = cgLocalToGlobal[i];
188     for (int j = 0; j < nLocal_; j++) {
189     int aid = AtomLocalToGlobal[j];
190 gezelter 1577 if (globalGroupMembership[aid] == gid) {
191 gezelter 1569 groupList_[i].push_back(j);
192 gezelter 1577 }
193 gezelter 1569 }
194     }
195    
196 gezelter 1587 excludesForAtom.clear();
197     excludesForAtom.resize(nLocal_);
198 gezelter 1579 toposForAtom.clear();
199     toposForAtom.resize(nLocal_);
200     topoDist.clear();
201     topoDist.resize(nLocal_);
202 gezelter 1569
203 gezelter 1570 for (int i = 0; i < nLocal_; i++) {
204     int iglob = AtomLocalToGlobal[i];
205 gezelter 1579
206 gezelter 1570 for (int j = 0; j < nLocal_; j++) {
207 gezelter 1579 int jglob = AtomLocalToGlobal[j];
208    
209 gezelter 1587 if (excludes->hasPair(iglob, jglob))
210     excludesForAtom[i].push_back(j);
211 gezelter 1579
212 gezelter 1587 if (oneTwo->hasPair(iglob, jglob)) {
213 gezelter 1579 toposForAtom[i].push_back(j);
214     topoDist[i].push_back(1);
215     } else {
216 gezelter 1587 if (oneThree->hasPair(iglob, jglob)) {
217 gezelter 1579 toposForAtom[i].push_back(j);
218     topoDist[i].push_back(2);
219     } else {
220 gezelter 1587 if (oneFour->hasPair(iglob, jglob)) {
221 gezelter 1579 toposForAtom[i].push_back(j);
222     topoDist[i].push_back(3);
223     }
224     }
225 gezelter 1570 }
226     }
227 gezelter 1579 }
228    
229     createGtypeCutoffMap();
230 gezelter 1587
231 gezelter 1576 }
232    
233     void ForceMatrixDecomposition::createGtypeCutoffMap() {
234 gezelter 1586
235 gezelter 1576 RealType tol = 1e-6;
236     RealType rc;
237     int atid;
238     set<AtomType*> atypes = info_->getSimulatedAtomTypes();
239 gezelter 1587 map<int, RealType> atypeCutoff;
240 gezelter 1583
241 gezelter 1579 for (set<AtomType*>::iterator at = atypes.begin();
242     at != atypes.end(); ++at){
243 gezelter 1576 atid = (*at)->getIdent();
244 gezelter 1587 if (userChoseCutoff_)
245 gezelter 1583 atypeCutoff[atid] = userCutoff_;
246 gezelter 1587 else
247 gezelter 1583 atypeCutoff[atid] = interactionMan_->getSuggestedCutoffRadius(*at);
248 gezelter 1570 }
249 gezelter 1576
250     vector<RealType> gTypeCutoffs;
251     // first we do a single loop over the cutoff groups to find the
252     // largest cutoff for any atypes present in this group.
253     #ifdef IS_MPI
254     vector<RealType> groupCutoffRow(nGroupsInRow_, 0.0);
255 gezelter 1579 groupRowToGtype.resize(nGroupsInRow_);
256 gezelter 1576 for (int cg1 = 0; cg1 < nGroupsInRow_; cg1++) {
257     vector<int> atomListRow = getAtomsInGroupRow(cg1);
258     for (vector<int>::iterator ia = atomListRow.begin();
259     ia != atomListRow.end(); ++ia) {
260     int atom1 = (*ia);
261     atid = identsRow[atom1];
262     if (atypeCutoff[atid] > groupCutoffRow[cg1]) {
263     groupCutoffRow[cg1] = atypeCutoff[atid];
264     }
265     }
266    
267     bool gTypeFound = false;
268     for (int gt = 0; gt < gTypeCutoffs.size(); gt++) {
269     if (abs(groupCutoffRow[cg1] - gTypeCutoffs[gt]) < tol) {
270     groupRowToGtype[cg1] = gt;
271     gTypeFound = true;
272     }
273     }
274     if (!gTypeFound) {
275     gTypeCutoffs.push_back( groupCutoffRow[cg1] );
276     groupRowToGtype[cg1] = gTypeCutoffs.size() - 1;
277     }
278    
279     }
280     vector<RealType> groupCutoffCol(nGroupsInCol_, 0.0);
281 gezelter 1579 groupColToGtype.resize(nGroupsInCol_);
282 gezelter 1576 for (int cg2 = 0; cg2 < nGroupsInCol_; cg2++) {
283     vector<int> atomListCol = getAtomsInGroupColumn(cg2);
284     for (vector<int>::iterator jb = atomListCol.begin();
285     jb != atomListCol.end(); ++jb) {
286     int atom2 = (*jb);
287     atid = identsCol[atom2];
288     if (atypeCutoff[atid] > groupCutoffCol[cg2]) {
289     groupCutoffCol[cg2] = atypeCutoff[atid];
290     }
291     }
292     bool gTypeFound = false;
293     for (int gt = 0; gt < gTypeCutoffs.size(); gt++) {
294     if (abs(groupCutoffCol[cg2] - gTypeCutoffs[gt]) < tol) {
295     groupColToGtype[cg2] = gt;
296     gTypeFound = true;
297     }
298     }
299     if (!gTypeFound) {
300     gTypeCutoffs.push_back( groupCutoffCol[cg2] );
301     groupColToGtype[cg2] = gTypeCutoffs.size() - 1;
302     }
303     }
304     #else
305 gezelter 1579
306 gezelter 1576 vector<RealType> groupCutoff(nGroups_, 0.0);
307 gezelter 1579 groupToGtype.resize(nGroups_);
308 gezelter 1576 for (int cg1 = 0; cg1 < nGroups_; cg1++) {
309 gezelter 1579
310 gezelter 1576 groupCutoff[cg1] = 0.0;
311     vector<int> atomList = getAtomsInGroupRow(cg1);
312 gezelter 1579
313 gezelter 1576 for (vector<int>::iterator ia = atomList.begin();
314     ia != atomList.end(); ++ia) {
315     int atom1 = (*ia);
316 gezelter 1583 atid = idents[atom1];
317 gezelter 1576 if (atypeCutoff[atid] > groupCutoff[cg1]) {
318     groupCutoff[cg1] = atypeCutoff[atid];
319     }
320     }
321    
322     bool gTypeFound = false;
323     for (int gt = 0; gt < gTypeCutoffs.size(); gt++) {
324     if (abs(groupCutoff[cg1] - gTypeCutoffs[gt]) < tol) {
325     groupToGtype[cg1] = gt;
326     gTypeFound = true;
327     }
328     }
329     if (!gTypeFound) {
330     gTypeCutoffs.push_back( groupCutoff[cg1] );
331     groupToGtype[cg1] = gTypeCutoffs.size() - 1;
332     }
333     }
334     #endif
335    
336     // Now we find the maximum group cutoff value present in the simulation
337    
338 gezelter 1579 RealType groupMax = *max_element(gTypeCutoffs.begin(), gTypeCutoffs.end());
339 gezelter 1576
340     #ifdef IS_MPI
341     MPI::COMM_WORLD.Allreduce(&groupMax, &groupMax, 1, MPI::REALTYPE, MPI::MAX);
342     #endif
343    
344     RealType tradRcut = groupMax;
345    
346     for (int i = 0; i < gTypeCutoffs.size(); i++) {
347 gezelter 1579 for (int j = 0; j < gTypeCutoffs.size(); j++) {
348 gezelter 1576 RealType thisRcut;
349     switch(cutoffPolicy_) {
350     case TRADITIONAL:
351     thisRcut = tradRcut;
352 gezelter 1579 break;
353 gezelter 1576 case MIX:
354     thisRcut = 0.5 * (gTypeCutoffs[i] + gTypeCutoffs[j]);
355 gezelter 1579 break;
356 gezelter 1576 case MAX:
357     thisRcut = max(gTypeCutoffs[i], gTypeCutoffs[j]);
358 gezelter 1579 break;
359 gezelter 1576 default:
360     sprintf(painCave.errMsg,
361     "ForceMatrixDecomposition::createGtypeCutoffMap "
362     "hit an unknown cutoff policy!\n");
363     painCave.severity = OPENMD_ERROR;
364     painCave.isFatal = 1;
365 gezelter 1579 simError();
366     break;
367 gezelter 1576 }
368    
369     pair<int,int> key = make_pair(i,j);
370     gTypeCutoffMap[key].first = thisRcut;
371    
372     if (thisRcut > largestRcut_) largestRcut_ = thisRcut;
373    
374     gTypeCutoffMap[key].second = thisRcut*thisRcut;
375    
376     gTypeCutoffMap[key].third = pow(thisRcut + skinThickness_, 2);
377    
378     // sanity check
379    
380     if (userChoseCutoff_) {
381     if (abs(gTypeCutoffMap[key].first - userCutoff_) > 0.0001) {
382     sprintf(painCave.errMsg,
383     "ForceMatrixDecomposition::createGtypeCutoffMap "
384 gezelter 1583 "user-specified rCut (%lf) does not match computed group Cutoff\n", userCutoff_);
385 gezelter 1576 painCave.severity = OPENMD_ERROR;
386     painCave.isFatal = 1;
387     simError();
388     }
389     }
390     }
391     }
392 gezelter 1539 }
393 gezelter 1576
394    
395     groupCutoffs ForceMatrixDecomposition::getGroupCutoffs(int cg1, int cg2) {
396 gezelter 1579 int i, j;
397 gezelter 1576 #ifdef IS_MPI
398     i = groupRowToGtype[cg1];
399     j = groupColToGtype[cg2];
400     #else
401     i = groupToGtype[cg1];
402     j = groupToGtype[cg2];
403 gezelter 1579 #endif
404 gezelter 1576 return gTypeCutoffMap[make_pair(i,j)];
405     }
406    
407 gezelter 1579 int ForceMatrixDecomposition::getTopologicalDistance(int atom1, int atom2) {
408     for (int j = 0; j < toposForAtom[atom1].size(); j++) {
409     if (toposForAtom[atom1][j] == atom2)
410     return topoDist[atom1][j];
411     }
412     return 0;
413     }
414 gezelter 1576
415 gezelter 1575 void ForceMatrixDecomposition::zeroWorkArrays() {
416 gezelter 1583 pairwisePot = 0.0;
417     embeddingPot = 0.0;
418 gezelter 1575
419     #ifdef IS_MPI
420     if (storageLayout_ & DataStorage::dslForce) {
421     fill(atomRowData.force.begin(), atomRowData.force.end(), V3Zero);
422     fill(atomColData.force.begin(), atomColData.force.end(), V3Zero);
423     }
424    
425     if (storageLayout_ & DataStorage::dslTorque) {
426     fill(atomRowData.torque.begin(), atomRowData.torque.end(), V3Zero);
427     fill(atomColData.torque.begin(), atomColData.torque.end(), V3Zero);
428     }
429    
430     fill(pot_row.begin(), pot_row.end(),
431     Vector<RealType, N_INTERACTION_FAMILIES> (0.0));
432    
433     fill(pot_col.begin(), pot_col.end(),
434 gezelter 1583 Vector<RealType, N_INTERACTION_FAMILIES> (0.0));
435 gezelter 1575
436     if (storageLayout_ & DataStorage::dslParticlePot) {
437     fill(atomRowData.particlePot.begin(), atomRowData.particlePot.end(), 0.0);
438     fill(atomColData.particlePot.begin(), atomColData.particlePot.end(), 0.0);
439     }
440    
441     if (storageLayout_ & DataStorage::dslDensity) {
442     fill(atomRowData.density.begin(), atomRowData.density.end(), 0.0);
443     fill(atomColData.density.begin(), atomColData.density.end(), 0.0);
444     }
445    
446     if (storageLayout_ & DataStorage::dslFunctional) {
447     fill(atomRowData.functional.begin(), atomRowData.functional.end(), 0.0);
448     fill(atomColData.functional.begin(), atomColData.functional.end(), 0.0);
449     }
450    
451     if (storageLayout_ & DataStorage::dslFunctionalDerivative) {
452     fill(atomRowData.functionalDerivative.begin(),
453     atomRowData.functionalDerivative.end(), 0.0);
454     fill(atomColData.functionalDerivative.begin(),
455     atomColData.functionalDerivative.end(), 0.0);
456     }
457    
458 gezelter 1586 if (storageLayout_ & DataStorage::dslSkippedCharge) {
459 gezelter 1587 fill(atomRowData.skippedCharge.begin(),
460     atomRowData.skippedCharge.end(), 0.0);
461     fill(atomColData.skippedCharge.begin(),
462     atomColData.skippedCharge.end(), 0.0);
463 gezelter 1586 }
464    
465 gezelter 1575 #else
466    
467     if (storageLayout_ & DataStorage::dslParticlePot) {
468     fill(snap_->atomData.particlePot.begin(),
469     snap_->atomData.particlePot.end(), 0.0);
470     }
471    
472     if (storageLayout_ & DataStorage::dslDensity) {
473     fill(snap_->atomData.density.begin(),
474     snap_->atomData.density.end(), 0.0);
475     }
476     if (storageLayout_ & DataStorage::dslFunctional) {
477     fill(snap_->atomData.functional.begin(),
478     snap_->atomData.functional.end(), 0.0);
479     }
480     if (storageLayout_ & DataStorage::dslFunctionalDerivative) {
481     fill(snap_->atomData.functionalDerivative.begin(),
482     snap_->atomData.functionalDerivative.end(), 0.0);
483     }
484 gezelter 1586 if (storageLayout_ & DataStorage::dslSkippedCharge) {
485     fill(snap_->atomData.skippedCharge.begin(),
486     snap_->atomData.skippedCharge.end(), 0.0);
487     }
488 gezelter 1575 #endif
489    
490     }
491    
492    
493 gezelter 1549 void ForceMatrixDecomposition::distributeData() {
494 gezelter 1551 snap_ = sman_->getCurrentSnapshot();
495     storageLayout_ = sman_->getStorageLayout();
496 chuckv 1538 #ifdef IS_MPI
497 gezelter 1540
498 gezelter 1539 // gather up the atomic positions
499 gezelter 1551 AtomCommVectorRow->gather(snap_->atomData.position,
500     atomRowData.position);
501     AtomCommVectorColumn->gather(snap_->atomData.position,
502     atomColData.position);
503 gezelter 1539
504     // gather up the cutoff group positions
505 gezelter 1551 cgCommVectorRow->gather(snap_->cgData.position,
506     cgRowData.position);
507     cgCommVectorColumn->gather(snap_->cgData.position,
508     cgColData.position);
509 gezelter 1539
510     // if needed, gather the atomic rotation matrices
511 gezelter 1551 if (storageLayout_ & DataStorage::dslAmat) {
512     AtomCommMatrixRow->gather(snap_->atomData.aMat,
513     atomRowData.aMat);
514     AtomCommMatrixColumn->gather(snap_->atomData.aMat,
515     atomColData.aMat);
516 gezelter 1539 }
517    
518     // if needed, gather the atomic eletrostatic frames
519 gezelter 1551 if (storageLayout_ & DataStorage::dslElectroFrame) {
520     AtomCommMatrixRow->gather(snap_->atomData.electroFrame,
521     atomRowData.electroFrame);
522     AtomCommMatrixColumn->gather(snap_->atomData.electroFrame,
523     atomColData.electroFrame);
524 gezelter 1539 }
525     #endif
526     }
527    
528 gezelter 1575 /* collects information obtained during the pre-pair loop onto local
529     * data structures.
530     */
531 gezelter 1549 void ForceMatrixDecomposition::collectIntermediateData() {
532 gezelter 1551 snap_ = sman_->getCurrentSnapshot();
533     storageLayout_ = sman_->getStorageLayout();
534 gezelter 1539 #ifdef IS_MPI
535    
536 gezelter 1551 if (storageLayout_ & DataStorage::dslDensity) {
537    
538     AtomCommRealRow->scatter(atomRowData.density,
539     snap_->atomData.density);
540    
541     int n = snap_->atomData.density.size();
542 gezelter 1575 vector<RealType> rho_tmp(n, 0.0);
543 gezelter 1551 AtomCommRealColumn->scatter(atomColData.density, rho_tmp);
544 gezelter 1539 for (int i = 0; i < n; i++)
545 gezelter 1551 snap_->atomData.density[i] += rho_tmp[i];
546 gezelter 1539 }
547 chuckv 1538 #endif
548 gezelter 1539 }
549 gezelter 1575
550     /*
551     * redistributes information obtained during the pre-pair loop out to
552     * row and column-indexed data structures
553     */
554 gezelter 1549 void ForceMatrixDecomposition::distributeIntermediateData() {
555 gezelter 1551 snap_ = sman_->getCurrentSnapshot();
556     storageLayout_ = sman_->getStorageLayout();
557 chuckv 1538 #ifdef IS_MPI
558 gezelter 1551 if (storageLayout_ & DataStorage::dslFunctional) {
559     AtomCommRealRow->gather(snap_->atomData.functional,
560     atomRowData.functional);
561     AtomCommRealColumn->gather(snap_->atomData.functional,
562     atomColData.functional);
563 gezelter 1539 }
564    
565 gezelter 1551 if (storageLayout_ & DataStorage::dslFunctionalDerivative) {
566     AtomCommRealRow->gather(snap_->atomData.functionalDerivative,
567     atomRowData.functionalDerivative);
568     AtomCommRealColumn->gather(snap_->atomData.functionalDerivative,
569     atomColData.functionalDerivative);
570 gezelter 1539 }
571 chuckv 1538 #endif
572     }
573 gezelter 1539
574    
575 gezelter 1549 void ForceMatrixDecomposition::collectData() {
576 gezelter 1551 snap_ = sman_->getCurrentSnapshot();
577     storageLayout_ = sman_->getStorageLayout();
578     #ifdef IS_MPI
579     int n = snap_->atomData.force.size();
580 gezelter 1544 vector<Vector3d> frc_tmp(n, V3Zero);
581 gezelter 1541
582 gezelter 1551 AtomCommVectorRow->scatter(atomRowData.force, frc_tmp);
583 gezelter 1541 for (int i = 0; i < n; i++) {
584 gezelter 1551 snap_->atomData.force[i] += frc_tmp[i];
585 gezelter 1541 frc_tmp[i] = 0.0;
586     }
587 gezelter 1540
588 gezelter 1551 AtomCommVectorColumn->scatter(atomColData.force, frc_tmp);
589 gezelter 1540 for (int i = 0; i < n; i++)
590 gezelter 1551 snap_->atomData.force[i] += frc_tmp[i];
591 gezelter 1540
592    
593 gezelter 1551 if (storageLayout_ & DataStorage::dslTorque) {
594 gezelter 1541
595 gezelter 1587 int nt = snap_->atomData.torque.size();
596 gezelter 1544 vector<Vector3d> trq_tmp(nt, V3Zero);
597 gezelter 1541
598 gezelter 1551 AtomCommVectorRow->scatter(atomRowData.torque, trq_tmp);
599 gezelter 1587 for (int i = 0; i < nt; i++) {
600 gezelter 1551 snap_->atomData.torque[i] += trq_tmp[i];
601 gezelter 1541 trq_tmp[i] = 0.0;
602     }
603 gezelter 1540
604 gezelter 1551 AtomCommVectorColumn->scatter(atomColData.torque, trq_tmp);
605 gezelter 1587 for (int i = 0; i < nt; i++)
606 gezelter 1551 snap_->atomData.torque[i] += trq_tmp[i];
607 gezelter 1540 }
608 gezelter 1587
609     if (storageLayout_ & DataStorage::dslSkippedCharge) {
610    
611     int ns = snap_->atomData.skippedCharge.size();
612     vector<RealType> skch_tmp(ns, 0.0);
613    
614     AtomCommRealRow->scatter(atomRowData.skippedCharge, skch_tmp);
615     for (int i = 0; i < ns; i++) {
616     snap_->atomData.skippedCharge[i] = skch_tmp[i];
617     skch_tmp[i] = 0.0;
618     }
619    
620     AtomCommRealColumn->scatter(atomColData.skippedCharge, skch_tmp);
621     for (int i = 0; i < ns; i++)
622     snap_->atomData.skippedCharge[i] += skch_tmp[i];
623     }
624 gezelter 1540
625 gezelter 1567 nLocal_ = snap_->getNumberOfAtoms();
626 gezelter 1544
627 gezelter 1575 vector<potVec> pot_temp(nLocal_,
628     Vector<RealType, N_INTERACTION_FAMILIES> (0.0));
629    
630     // scatter/gather pot_row into the members of my column
631    
632     AtomCommPotRow->scatter(pot_row, pot_temp);
633    
634     for (int ii = 0; ii < pot_temp.size(); ii++ )
635 gezelter 1583 pairwisePot += pot_temp[ii];
636 gezelter 1540
637 gezelter 1575 fill(pot_temp.begin(), pot_temp.end(),
638     Vector<RealType, N_INTERACTION_FAMILIES> (0.0));
639    
640     AtomCommPotColumn->scatter(pot_col, pot_temp);
641    
642     for (int ii = 0; ii < pot_temp.size(); ii++ )
643 gezelter 1583 pairwisePot += pot_temp[ii];
644 gezelter 1539 #endif
645 gezelter 1583
646 chuckv 1538 }
647 gezelter 1551
648 gezelter 1570 int ForceMatrixDecomposition::getNAtomsInRow() {
649     #ifdef IS_MPI
650     return nAtomsInRow_;
651     #else
652     return nLocal_;
653     #endif
654     }
655    
656 gezelter 1569 /**
657     * returns the list of atoms belonging to this group.
658     */
659     vector<int> ForceMatrixDecomposition::getAtomsInGroupRow(int cg1){
660     #ifdef IS_MPI
661     return groupListRow_[cg1];
662     #else
663     return groupList_[cg1];
664     #endif
665     }
666    
667     vector<int> ForceMatrixDecomposition::getAtomsInGroupColumn(int cg2){
668     #ifdef IS_MPI
669     return groupListCol_[cg2];
670     #else
671     return groupList_[cg2];
672     #endif
673     }
674 chuckv 1538
675 gezelter 1551 Vector3d ForceMatrixDecomposition::getIntergroupVector(int cg1, int cg2){
676     Vector3d d;
677    
678     #ifdef IS_MPI
679     d = cgColData.position[cg2] - cgRowData.position[cg1];
680     #else
681     d = snap_->cgData.position[cg2] - snap_->cgData.position[cg1];
682     #endif
683    
684     snap_->wrapVector(d);
685     return d;
686     }
687    
688    
689     Vector3d ForceMatrixDecomposition::getAtomToGroupVectorRow(int atom1, int cg1){
690    
691     Vector3d d;
692    
693     #ifdef IS_MPI
694     d = cgRowData.position[cg1] - atomRowData.position[atom1];
695     #else
696     d = snap_->cgData.position[cg1] - snap_->atomData.position[atom1];
697     #endif
698    
699     snap_->wrapVector(d);
700     return d;
701     }
702    
703     Vector3d ForceMatrixDecomposition::getAtomToGroupVectorColumn(int atom2, int cg2){
704     Vector3d d;
705    
706     #ifdef IS_MPI
707     d = cgColData.position[cg2] - atomColData.position[atom2];
708     #else
709     d = snap_->cgData.position[cg2] - snap_->atomData.position[atom2];
710     #endif
711    
712     snap_->wrapVector(d);
713     return d;
714     }
715 gezelter 1569
716     RealType ForceMatrixDecomposition::getMassFactorRow(int atom1) {
717     #ifdef IS_MPI
718     return massFactorsRow[atom1];
719     #else
720 gezelter 1581 return massFactors[atom1];
721 gezelter 1569 #endif
722     }
723    
724     RealType ForceMatrixDecomposition::getMassFactorColumn(int atom2) {
725     #ifdef IS_MPI
726     return massFactorsCol[atom2];
727     #else
728 gezelter 1581 return massFactors[atom2];
729 gezelter 1569 #endif
730    
731     }
732 gezelter 1551
733     Vector3d ForceMatrixDecomposition::getInteratomicVector(int atom1, int atom2){
734     Vector3d d;
735    
736     #ifdef IS_MPI
737     d = atomColData.position[atom2] - atomRowData.position[atom1];
738     #else
739     d = snap_->atomData.position[atom2] - snap_->atomData.position[atom1];
740     #endif
741    
742     snap_->wrapVector(d);
743     return d;
744     }
745    
746 gezelter 1587 vector<int> ForceMatrixDecomposition::getExcludesForAtom(int atom1) {
747     return excludesForAtom[atom1];
748 gezelter 1570 }
749    
750     /**
751 gezelter 1587 * We need to exclude some overcounted interactions that result from
752 gezelter 1575 * the parallel decomposition.
753 gezelter 1570 */
754     bool ForceMatrixDecomposition::skipAtomPair(int atom1, int atom2) {
755     int unique_id_1, unique_id_2;
756    
757     #ifdef IS_MPI
758     // in MPI, we have to look up the unique IDs for each atom
759     unique_id_1 = AtomRowToGlobal[atom1];
760     unique_id_2 = AtomColToGlobal[atom2];
761    
762     // this situation should only arise in MPI simulations
763     if (unique_id_1 == unique_id_2) return true;
764    
765     // this prevents us from doing the pair on multiple processors
766     if (unique_id_1 < unique_id_2) {
767     if ((unique_id_1 + unique_id_2) % 2 == 0) return true;
768     } else {
769     if ((unique_id_1 + unique_id_2) % 2 == 1) return true;
770     }
771 gezelter 1587 #endif
772     return false;
773     }
774    
775     /**
776     * We need to handle the interactions for atoms who are involved in
777     * the same rigid body as well as some short range interactions
778     * (bonds, bends, torsions) differently from other interactions.
779     * We'll still visit the pairwise routines, but with a flag that
780     * tells those routines to exclude the pair from direct long range
781     * interactions. Some indirect interactions (notably reaction
782     * field) must still be handled for these pairs.
783     */
784     bool ForceMatrixDecomposition::excludeAtomPair(int atom1, int atom2) {
785     int unique_id_2;
786    
787     #ifdef IS_MPI
788     // in MPI, we have to look up the unique IDs for the row atom.
789     unique_id_2 = AtomColToGlobal[atom2];
790 gezelter 1570 #else
791     // in the normal loop, the atom numbers are unique
792     unique_id_2 = atom2;
793     #endif
794    
795 gezelter 1587 for (vector<int>::iterator i = excludesForAtom[atom1].begin();
796     i != excludesForAtom[atom1].end(); ++i) {
797 gezelter 1570 if ( (*i) == unique_id_2 ) return true;
798 gezelter 1583 }
799 gezelter 1579
800 gezelter 1583 return false;
801 gezelter 1570 }
802    
803    
804 gezelter 1551 void ForceMatrixDecomposition::addForceToAtomRow(int atom1, Vector3d fg){
805     #ifdef IS_MPI
806     atomRowData.force[atom1] += fg;
807     #else
808     snap_->atomData.force[atom1] += fg;
809     #endif
810     }
811    
812     void ForceMatrixDecomposition::addForceToAtomColumn(int atom2, Vector3d fg){
813     #ifdef IS_MPI
814     atomColData.force[atom2] += fg;
815     #else
816     snap_->atomData.force[atom2] += fg;
817     #endif
818     }
819    
820     // filling interaction blocks with pointers
821 gezelter 1582 void ForceMatrixDecomposition::fillInteractionData(InteractionData &idat,
822 gezelter 1587 int atom1, int atom2) {
823    
824     idat.excluded = excludeAtomPair(atom1, atom2);
825    
826 gezelter 1551 #ifdef IS_MPI
827 gezelter 1571
828     idat.atypes = make_pair( ff_->getAtomType(identsRow[atom1]),
829     ff_->getAtomType(identsCol[atom2]) );
830 gezelter 1575
831 gezelter 1551 if (storageLayout_ & DataStorage::dslAmat) {
832 gezelter 1554 idat.A1 = &(atomRowData.aMat[atom1]);
833     idat.A2 = &(atomColData.aMat[atom2]);
834 gezelter 1551 }
835 gezelter 1567
836 gezelter 1551 if (storageLayout_ & DataStorage::dslElectroFrame) {
837 gezelter 1554 idat.eFrame1 = &(atomRowData.electroFrame[atom1]);
838     idat.eFrame2 = &(atomColData.electroFrame[atom2]);
839 gezelter 1551 }
840    
841     if (storageLayout_ & DataStorage::dslTorque) {
842 gezelter 1554 idat.t1 = &(atomRowData.torque[atom1]);
843     idat.t2 = &(atomColData.torque[atom2]);
844 gezelter 1551 }
845    
846     if (storageLayout_ & DataStorage::dslDensity) {
847 gezelter 1554 idat.rho1 = &(atomRowData.density[atom1]);
848     idat.rho2 = &(atomColData.density[atom2]);
849 gezelter 1551 }
850    
851 gezelter 1575 if (storageLayout_ & DataStorage::dslFunctional) {
852     idat.frho1 = &(atomRowData.functional[atom1]);
853     idat.frho2 = &(atomColData.functional[atom2]);
854     }
855    
856 gezelter 1551 if (storageLayout_ & DataStorage::dslFunctionalDerivative) {
857 gezelter 1554 idat.dfrho1 = &(atomRowData.functionalDerivative[atom1]);
858     idat.dfrho2 = &(atomColData.functionalDerivative[atom2]);
859 gezelter 1551 }
860 gezelter 1570
861 gezelter 1575 if (storageLayout_ & DataStorage::dslParticlePot) {
862     idat.particlePot1 = &(atomRowData.particlePot[atom1]);
863     idat.particlePot2 = &(atomColData.particlePot[atom2]);
864     }
865    
866 gezelter 1587 if (storageLayout_ & DataStorage::dslSkippedCharge) {
867     idat.skippedCharge1 = &(atomRowData.skippedCharge[atom1]);
868     idat.skippedCharge2 = &(atomColData.skippedCharge[atom2]);
869     }
870    
871 gezelter 1562 #else
872 gezelter 1571
873 gezelter 1583 idat.atypes = make_pair( ff_->getAtomType(idents[atom1]),
874     ff_->getAtomType(idents[atom2]) );
875 gezelter 1571
876 gezelter 1562 if (storageLayout_ & DataStorage::dslAmat) {
877     idat.A1 = &(snap_->atomData.aMat[atom1]);
878     idat.A2 = &(snap_->atomData.aMat[atom2]);
879     }
880    
881     if (storageLayout_ & DataStorage::dslElectroFrame) {
882     idat.eFrame1 = &(snap_->atomData.electroFrame[atom1]);
883     idat.eFrame2 = &(snap_->atomData.electroFrame[atom2]);
884     }
885    
886     if (storageLayout_ & DataStorage::dslTorque) {
887     idat.t1 = &(snap_->atomData.torque[atom1]);
888     idat.t2 = &(snap_->atomData.torque[atom2]);
889     }
890    
891 gezelter 1583 if (storageLayout_ & DataStorage::dslDensity) {
892 gezelter 1562 idat.rho1 = &(snap_->atomData.density[atom1]);
893     idat.rho2 = &(snap_->atomData.density[atom2]);
894     }
895    
896 gezelter 1575 if (storageLayout_ & DataStorage::dslFunctional) {
897     idat.frho1 = &(snap_->atomData.functional[atom1]);
898     idat.frho2 = &(snap_->atomData.functional[atom2]);
899     }
900    
901 gezelter 1562 if (storageLayout_ & DataStorage::dslFunctionalDerivative) {
902     idat.dfrho1 = &(snap_->atomData.functionalDerivative[atom1]);
903     idat.dfrho2 = &(snap_->atomData.functionalDerivative[atom2]);
904     }
905 gezelter 1575
906     if (storageLayout_ & DataStorage::dslParticlePot) {
907     idat.particlePot1 = &(snap_->atomData.particlePot[atom1]);
908     idat.particlePot2 = &(snap_->atomData.particlePot[atom2]);
909     }
910    
911 gezelter 1587 if (storageLayout_ & DataStorage::dslSkippedCharge) {
912     idat.skippedCharge1 = &(snap_->atomData.skippedCharge[atom1]);
913     idat.skippedCharge2 = &(snap_->atomData.skippedCharge[atom2]);
914     }
915 gezelter 1551 #endif
916     }
917 gezelter 1567
918 gezelter 1575
919 gezelter 1582 void ForceMatrixDecomposition::unpackInteractionData(InteractionData &idat, int atom1, int atom2) {
920 gezelter 1575 #ifdef IS_MPI
921     pot_row[atom1] += 0.5 * *(idat.pot);
922     pot_col[atom2] += 0.5 * *(idat.pot);
923    
924     atomRowData.force[atom1] += *(idat.f1);
925     atomColData.force[atom2] -= *(idat.f1);
926     #else
927 gezelter 1583 pairwisePot += *(idat.pot);
928    
929 gezelter 1575 snap_->atomData.force[atom1] += *(idat.f1);
930     snap_->atomData.force[atom2] -= *(idat.f1);
931     #endif
932 gezelter 1586
933 gezelter 1575 }
934    
935 gezelter 1562 /*
936     * buildNeighborList
937     *
938     * first element of pair is row-indexed CutoffGroup
939     * second element of pair is column-indexed CutoffGroup
940     */
941 gezelter 1567 vector<pair<int, int> > ForceMatrixDecomposition::buildNeighborList() {
942    
943     vector<pair<int, int> > neighborList;
944 gezelter 1576 groupCutoffs cuts;
945 gezelter 1587 bool doAllPairs = false;
946    
947 gezelter 1567 #ifdef IS_MPI
948 gezelter 1568 cellListRow_.clear();
949     cellListCol_.clear();
950 gezelter 1567 #else
951 gezelter 1568 cellList_.clear();
952 gezelter 1567 #endif
953 gezelter 1562
954 gezelter 1576 RealType rList_ = (largestRcut_ + skinThickness_);
955 gezelter 1567 RealType rl2 = rList_ * rList_;
956     Snapshot* snap_ = sman_->getCurrentSnapshot();
957 gezelter 1562 Mat3x3d Hmat = snap_->getHmat();
958     Vector3d Hx = Hmat.getColumn(0);
959     Vector3d Hy = Hmat.getColumn(1);
960     Vector3d Hz = Hmat.getColumn(2);
961    
962 gezelter 1568 nCells_.x() = (int) ( Hx.length() )/ rList_;
963     nCells_.y() = (int) ( Hy.length() )/ rList_;
964     nCells_.z() = (int) ( Hz.length() )/ rList_;
965 gezelter 1562
966 gezelter 1587 // handle small boxes where the cell offsets can end up repeating cells
967    
968     if (nCells_.x() < 3) doAllPairs = true;
969     if (nCells_.y() < 3) doAllPairs = true;
970     if (nCells_.z() < 3) doAllPairs = true;
971    
972 gezelter 1567 Mat3x3d invHmat = snap_->getInvHmat();
973     Vector3d rs, scaled, dr;
974     Vector3i whichCell;
975     int cellIndex;
976 gezelter 1579 int nCtot = nCells_.x() * nCells_.y() * nCells_.z();
977 gezelter 1567
978     #ifdef IS_MPI
979 gezelter 1579 cellListRow_.resize(nCtot);
980     cellListCol_.resize(nCtot);
981     #else
982     cellList_.resize(nCtot);
983     #endif
984 gezelter 1582
985 gezelter 1587 if (!doAllPairs) {
986 gezelter 1579 #ifdef IS_MPI
987 gezelter 1581
988 gezelter 1587 for (int i = 0; i < nGroupsInRow_; i++) {
989     rs = cgRowData.position[i];
990    
991     // scaled positions relative to the box vectors
992     scaled = invHmat * rs;
993    
994     // wrap the vector back into the unit box by subtracting integer box
995     // numbers
996     for (int j = 0; j < 3; j++) {
997     scaled[j] -= roundMe(scaled[j]);
998     scaled[j] += 0.5;
999     }
1000    
1001     // find xyz-indices of cell that cutoffGroup is in.
1002     whichCell.x() = nCells_.x() * scaled.x();
1003     whichCell.y() = nCells_.y() * scaled.y();
1004     whichCell.z() = nCells_.z() * scaled.z();
1005    
1006     // find single index of this cell:
1007     cellIndex = Vlinear(whichCell, nCells_);
1008    
1009     // add this cutoff group to the list of groups in this cell;
1010     cellListRow_[cellIndex].push_back(i);
1011 gezelter 1581 }
1012 gezelter 1587
1013     for (int i = 0; i < nGroupsInCol_; i++) {
1014     rs = cgColData.position[i];
1015    
1016     // scaled positions relative to the box vectors
1017     scaled = invHmat * rs;
1018    
1019     // wrap the vector back into the unit box by subtracting integer box
1020     // numbers
1021     for (int j = 0; j < 3; j++) {
1022     scaled[j] -= roundMe(scaled[j]);
1023     scaled[j] += 0.5;
1024     }
1025    
1026     // find xyz-indices of cell that cutoffGroup is in.
1027     whichCell.x() = nCells_.x() * scaled.x();
1028     whichCell.y() = nCells_.y() * scaled.y();
1029     whichCell.z() = nCells_.z() * scaled.z();
1030    
1031     // find single index of this cell:
1032     cellIndex = Vlinear(whichCell, nCells_);
1033    
1034     // add this cutoff group to the list of groups in this cell;
1035     cellListCol_[cellIndex].push_back(i);
1036 gezelter 1581 }
1037 gezelter 1567 #else
1038 gezelter 1587 for (int i = 0; i < nGroups_; i++) {
1039     rs = snap_->cgData.position[i];
1040    
1041     // scaled positions relative to the box vectors
1042     scaled = invHmat * rs;
1043    
1044     // wrap the vector back into the unit box by subtracting integer box
1045     // numbers
1046     for (int j = 0; j < 3; j++) {
1047     scaled[j] -= roundMe(scaled[j]);
1048     scaled[j] += 0.5;
1049     }
1050    
1051     // find xyz-indices of cell that cutoffGroup is in.
1052     whichCell.x() = nCells_.x() * scaled.x();
1053     whichCell.y() = nCells_.y() * scaled.y();
1054     whichCell.z() = nCells_.z() * scaled.z();
1055    
1056     // find single index of this cell:
1057     cellIndex = Vlinear(whichCell, nCells_);
1058    
1059     // add this cutoff group to the list of groups in this cell;
1060     cellList_[cellIndex].push_back(i);
1061 gezelter 1581 }
1062 gezelter 1567 #endif
1063    
1064 gezelter 1587 for (int m1z = 0; m1z < nCells_.z(); m1z++) {
1065     for (int m1y = 0; m1y < nCells_.y(); m1y++) {
1066     for (int m1x = 0; m1x < nCells_.x(); m1x++) {
1067     Vector3i m1v(m1x, m1y, m1z);
1068     int m1 = Vlinear(m1v, nCells_);
1069 gezelter 1568
1070 gezelter 1587 for (vector<Vector3i>::iterator os = cellOffsets_.begin();
1071     os != cellOffsets_.end(); ++os) {
1072    
1073     Vector3i m2v = m1v + (*os);
1074    
1075     if (m2v.x() >= nCells_.x()) {
1076     m2v.x() = 0;
1077     } else if (m2v.x() < 0) {
1078     m2v.x() = nCells_.x() - 1;
1079     }
1080    
1081     if (m2v.y() >= nCells_.y()) {
1082     m2v.y() = 0;
1083     } else if (m2v.y() < 0) {
1084     m2v.y() = nCells_.y() - 1;
1085     }
1086    
1087     if (m2v.z() >= nCells_.z()) {
1088     m2v.z() = 0;
1089     } else if (m2v.z() < 0) {
1090     m2v.z() = nCells_.z() - 1;
1091     }
1092    
1093     int m2 = Vlinear (m2v, nCells_);
1094    
1095 gezelter 1567 #ifdef IS_MPI
1096 gezelter 1587 for (vector<int>::iterator j1 = cellListRow_[m1].begin();
1097     j1 != cellListRow_[m1].end(); ++j1) {
1098     for (vector<int>::iterator j2 = cellListCol_[m2].begin();
1099     j2 != cellListCol_[m2].end(); ++j2) {
1100    
1101     // Always do this if we're in different cells or if
1102     // we're in the same cell and the global index of the
1103     // j2 cutoff group is less than the j1 cutoff group
1104    
1105     if (m2 != m1 || cgColToGlobal[(*j2)] < cgRowToGlobal[(*j1)]) {
1106     dr = cgColData.position[(*j2)] - cgRowData.position[(*j1)];
1107     snap_->wrapVector(dr);
1108     cuts = getGroupCutoffs( (*j1), (*j2) );
1109     if (dr.lengthSquare() < cuts.third) {
1110     neighborList.push_back(make_pair((*j1), (*j2)));
1111     }
1112 gezelter 1562 }
1113     }
1114     }
1115 gezelter 1567 #else
1116 gezelter 1587
1117     for (vector<int>::iterator j1 = cellList_[m1].begin();
1118     j1 != cellList_[m1].end(); ++j1) {
1119     for (vector<int>::iterator j2 = cellList_[m2].begin();
1120     j2 != cellList_[m2].end(); ++j2) {
1121    
1122     // Always do this if we're in different cells or if
1123     // we're in the same cell and the global index of the
1124     // j2 cutoff group is less than the j1 cutoff group
1125    
1126     if (m2 != m1 || (*j2) < (*j1)) {
1127     dr = snap_->cgData.position[(*j2)] - snap_->cgData.position[(*j1)];
1128     snap_->wrapVector(dr);
1129     cuts = getGroupCutoffs( (*j1), (*j2) );
1130     if (dr.lengthSquare() < cuts.third) {
1131     neighborList.push_back(make_pair((*j1), (*j2)));
1132     }
1133 gezelter 1567 }
1134     }
1135     }
1136 gezelter 1587 #endif
1137 gezelter 1567 }
1138 gezelter 1562 }
1139     }
1140     }
1141 gezelter 1587 } else {
1142     // branch to do all cutoff group pairs
1143     #ifdef IS_MPI
1144     for (int j1 = 0; j1 < nGroupsInRow_; j1++) {
1145     for (int j2 = 0; j2 < nGroupsInCol_; j2++) {
1146     dr = cgColData.position[j2] - cgRowData.position[j1];
1147     snap_->wrapVector(dr);
1148     cuts = getGroupCutoffs( j1, j2 );
1149     if (dr.lengthSquare() < cuts.third) {
1150     neighborList.push_back(make_pair(j1, j2));
1151     }
1152     }
1153     }
1154     #else
1155     for (int j1 = 0; j1 < nGroups_ - 1; j1++) {
1156     for (int j2 = j1 + 1; j2 < nGroups_; j2++) {
1157     dr = snap_->cgData.position[j2] - snap_->cgData.position[j1];
1158     snap_->wrapVector(dr);
1159     cuts = getGroupCutoffs( j1, j2 );
1160     if (dr.lengthSquare() < cuts.third) {
1161     neighborList.push_back(make_pair(j1, j2));
1162     }
1163     }
1164     }
1165     #endif
1166 gezelter 1562 }
1167 gezelter 1587
1168 gezelter 1568 // save the local cutoff group positions for the check that is
1169     // done on each loop:
1170     saved_CG_positions_.clear();
1171     for (int i = 0; i < nGroups_; i++)
1172     saved_CG_positions_.push_back(snap_->cgData.position[i]);
1173 gezelter 1587
1174 gezelter 1567 return neighborList;
1175 gezelter 1562 }
1176 gezelter 1539 } //end namespace OpenMD