2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements neighborhood searching for analysis (from nbsearch.h).
40 * The grid implementation could still be optimized in several different ways:
41 * - Pruning grid cells from the search list if they are completely outside
42 * the sphere that is being considered.
43 * - A better heuristic could be added for falling back to simple loops for a
44 * small number of reference particles.
45 * - A better heuristic for selecting the grid size.
46 * - A multi-level grid implementation could be used to be able to use small
47 * grids for short cutoffs with very inhomogeneous particle distributions
48 * without a memory cost.
50 * \author Teemu Murtola <teemu.murtola@gmail.com>
51 * \ingroup module_selection
63 #include "thread_mpi/mutex.h"
65 #include "gromacs/legacyheaders/names.h"
66 #include "gromacs/math/vec.h"
67 #include "gromacs/pbcutil/pbc.h"
68 #include "gromacs/selection/position.h"
69 #include "gromacs/topology/block.h"
70 #include "gromacs/utility/arrayref.h"
71 #include "gromacs/utility/exceptions.h"
72 #include "gromacs/utility/gmxassert.h"
73 #include "gromacs/utility/stringutil.h"
82 * Computes the bounding box for a set of positions.
84 * \param[in] posCount Number of positions in \p x.
85 * \param[in] x Positions to compute the bounding box for.
86 * \param[out] origin Origin of the bounding box.
87 * \param[out] size Size of the bounding box.
89 void computeBoundingBox(int posCount, const rvec x[], rvec origin, rvec size)
92 copy_rvec(x[0], origin);
93 copy_rvec(x[0], maxBound);
94 for (int i = 1; i < posCount; ++i)
96 for (int d = 0; d < DIM; ++d)
98 if (origin[d] > x[i][d])
102 if (maxBound[d] < x[i][d])
104 maxBound[d] = x[i][d];
108 rvec_sub(maxBound, origin, size);
116 /********************************************************************
117 * Implementation class declarations
120 class AnalysisNeighborhoodSearchImpl
123 typedef AnalysisNeighborhoodPairSearch::ImplPointer
124 PairSearchImplPointer;
125 typedef std::vector<PairSearchImplPointer> PairSearchList;
126 typedef std::vector<std::vector<int> > CellList;
128 explicit AnalysisNeighborhoodSearchImpl(real cutoff);
129 ~AnalysisNeighborhoodSearchImpl();
132 * Initializes the search with a given box and reference positions.
134 * \param[in] mode Search mode to use.
135 * \param[in] bXY Whether to use 2D searching.
136 * \param[in] excls Exclusions.
137 * \param[in] pbc PBC information.
138 * \param[in] positions Set of reference positions.
140 void init(AnalysisNeighborhood::SearchMode mode,
142 const t_blocka *excls,
144 const AnalysisNeighborhoodPositions &positions);
145 PairSearchImplPointer getPairSearch();
147 real cutoffSquared() const { return cutoff2_; }
148 bool usesGridSearch() const { return bGrid_; }
152 * Checks the efficiency and possibility of doing grid-based searching.
154 * \param[in] bForce If `true`, grid search will be forced if possible.
155 * \returns `false` if grid search is not suitable.
157 bool checkGridSearchEfficiency(bool bForce);
159 * Determines a suitable grid size and sets up the cells.
161 * \param[in] box Box vectors (should not have zero vectors).
162 * \param[in] bSingleCell If `true`, the corresponding dimension will
163 * be forced to use a single cell.
164 * \param[in] posCount Number of positions that will be put on the
166 * \returns `false` if grid search is not suitable.
168 bool initGridCells(const matrix box, bool bSingleCell[DIM],
171 * Sets ua a search grid for a given box.
173 * \param[in] pbc Information about the box.
174 * \param[in] posCount Number of positions in \p x.
175 * \param[in] x Reference positions that will be put on the grid.
176 * \param[in] bForce If `true`, grid searching will be used if at all
177 * possible, even if a simple search might give better performance.
178 * \returns `false` if grid search is not suitable.
180 bool initGrid(const t_pbc &pbc, int posCount, const rvec x[], bool bForce);
182 * Maps a point into a grid cell.
184 * \param[in] x Point to map.
185 * \param[out] cell Fractional cell coordinates of \p x on the grid.
186 * \param[out] xout Coordinates to use.
188 * \p xout will be within the rectangular unit cell in dimensions where
189 * the grid is periodic. For other dimensions, both \p xout and
190 * \p cell can be outside the grid/unit cell.
192 void mapPointToGridCell(const rvec x, rvec cell, rvec xout) const;
194 * Calculates linear index of a grid cell.
196 * \param[in] cell Cell indices (must be within the grid).
197 * \returns Linear index of \p cell.
199 int getGridCellIndex(const ivec cell) const;
201 * Adds an index into a grid cell.
203 * \param[in] cell Fractional cell coordinates into which \p i should
205 * \param[in] i Index to add.
207 * \p cell should satisfy the conditions that \p mapPointToGridCell()
210 void addToGridCell(const rvec cell, int i);
212 * Initializes a cell pair loop for a dimension.
214 * \param[in] centerCell Fractional cell coordiates of the particle
215 * for which pairs are being searched.
216 * \param[in,out] cell Current/initial cell to loop over.
217 * \param[in,out] upperBound Last cell to loop over.
218 * \param[in] dim Dimension to initialize in this call.
220 * Initializes `cell[dim]` and `upperBound[dim]` for looping over
221 * neighbors of a particle at position given by \p centerCell.
222 * If 'dim != ZZ`, `cell[d]` (`d > dim`) set the plane/row of cells
223 * for which the loop is initialized. The loop should then go from
224 * `cell[dim]` until `upperBound[dim]`, inclusive.
225 * `cell[d]` with `d < dim` or `upperBound[d]` with `d != dim` are not
226 * modified by this function.
228 * `cell` and `upperBound` may be outside the grid for periodic
229 * dimensions and need to be shifted separately: to simplify the
230 * looping, the range is always (roughly) symmetric around the value in
233 void initCellRange(const rvec centerCell, ivec cell,
234 ivec upperBound, int dim) const;
236 * Advances cell pair loop to the next cell.
238 * \param[in] centerCell Fractional cell coordiates of the particle
239 * for which pairs are being searched.
240 * \param[in,out] cell Current (in)/next (out) cell in the loop.
241 * \param[in,out] upperBound Last cell in the loop for each dimension.
243 bool nextCell(const rvec centerCell, ivec cell, ivec upperBound) const;
245 * Calculates the index and shift of a grid cell during looping.
247 * \param[in] cell Unshifted cell index.
248 * \param[out] shift Shift to apply to get the periodic distance
249 * for distances between the cells.
250 * \returns Grid cell index corresponding to `cell`.
252 int shiftCell(const ivec cell, rvec shift) const;
254 //! Whether to try grid searching.
258 //! The cutoff squared.
260 //! Whether to do searching in XY plane only.
263 //! Number of reference points for the current frame.
265 //! Reference point positions.
267 //! Reference position exclusion IDs.
268 const int *refExclusionIds_;
269 //! Reference position indices (NULL if no indices).
270 const int *refIndices_;
272 const t_blocka *excls_;
276 //! Whether grid searching is actually used for the current positions.
278 //! false if the box is rectangular.
280 //! Whether the grid is periodic in a dimension.
282 //! Array for storing in-unit-cell reference positions.
283 std::vector<RVec> xrefAlloc_;
284 //! Origin of the grid (zero for periodic dimensions).
286 //! Size of a single grid cell.
288 //! Inverse of \p cellSize_. Zero for dimensions where grid is not used.
291 * Shift in cell coordinates (for triclinic boxes) in X when crossing
292 * the Z periodic boundary.
296 * Shift in cell coordinates (for triclinic boxes) in Y when crossing
297 * the Z periodic boundary.
301 * Shift in cell coordinates (for triclinic boxes) in X when crossing
302 * the Y periodic boundary.
305 //! Number of cells along each dimension.
307 //! Data structure to hold the grid cell contents.
310 tMPI::mutex createPairSearchMutex_;
311 PairSearchList pairSearchList_;
313 friend class AnalysisNeighborhoodPairSearchImpl;
315 GMX_DISALLOW_COPY_AND_ASSIGN(AnalysisNeighborhoodSearchImpl);
318 class AnalysisNeighborhoodPairSearchImpl
321 explicit AnalysisNeighborhoodPairSearchImpl(const AnalysisNeighborhoodSearchImpl &search)
325 testPositions_ = NULL;
326 testExclusionIds_ = NULL;
331 clear_rvec(testcell_);
332 clear_ivec(currCell_);
333 clear_ivec(cellBound_);
337 //! Initializes a search to find reference positions neighboring \p x.
338 void startSearch(const AnalysisNeighborhoodPositions &positions);
339 //! Searches for the next neighbor.
340 template <class Action>
341 bool searchNext(Action action);
342 //! Initializes a pair representing the pair found by searchNext().
343 void initFoundPair(AnalysisNeighborhoodPair *pair) const;
344 //! Advances to the next test position, skipping any remaining pairs.
345 void nextTestPosition();
348 //! Clears the loop indices.
349 void reset(int testIndex);
350 //! Checks whether a reference positiong should be excluded.
351 bool isExcluded(int j);
353 //! Parent search object.
354 const AnalysisNeighborhoodSearchImpl &search_;
355 //! Number of test positions.
357 //! Reference to the test positions.
358 const rvec *testPositions_;
359 //! Reference to the test exclusion indices.
360 const int *testExclusionIds_;
361 //! Reference to the test position indices.
362 const int *testIndices_;
363 //! Number of excluded reference positions for current test particle.
365 //! Exclusions for current test particle.
367 //! Index of the currently active test position in \p testPositions_.
369 //! Stores test position during a pair loop.
371 //! Stores the previous returned position during a pair loop.
373 //! Stores the pair distance corresponding to previ_;
375 //! Stores the shortest distance vector corresponding to previ_;
377 //! Stores the current exclusion index during loops.
379 //! Stores the fractional test particle cell location during loops.
381 //! Stores the current cell during pair loops.
383 //! Stores the current loop upper bounds for each dimension during pair loops.
385 //! Stores the index within the current cell during pair loops.
388 GMX_DISALLOW_COPY_AND_ASSIGN(AnalysisNeighborhoodPairSearchImpl);
391 /********************************************************************
392 * AnalysisNeighborhoodSearchImpl
395 AnalysisNeighborhoodSearchImpl::AnalysisNeighborhoodSearchImpl(real cutoff)
401 cutoff_ = cutoff2_ = GMX_REAL_MAX;
406 cutoff2_ = sqr(cutoff_);
411 refExclusionIds_ = NULL;
413 std::memset(&pbc_, 0, sizeof(pbc_));
417 bGridPBC_[XX] = true;
418 bGridPBC_[YY] = true;
419 bGridPBC_[ZZ] = true;
421 clear_rvec(gridOrigin_);
422 clear_rvec(cellSize_);
423 clear_rvec(invCellSize_);
424 clear_ivec(ncelldim_);
427 AnalysisNeighborhoodSearchImpl::~AnalysisNeighborhoodSearchImpl()
429 PairSearchList::const_iterator i;
430 for (i = pairSearchList_.begin(); i != pairSearchList_.end(); ++i)
432 GMX_RELEASE_ASSERT(i->unique(),
433 "Dangling AnalysisNeighborhoodPairSearch reference");
437 AnalysisNeighborhoodSearchImpl::PairSearchImplPointer
438 AnalysisNeighborhoodSearchImpl::getPairSearch()
440 tMPI::lock_guard<tMPI::mutex> lock(createPairSearchMutex_);
441 // TODO: Consider whether this needs to/can be faster, e.g., by keeping a
442 // separate pool of unused search objects.
443 PairSearchList::const_iterator i;
444 for (i = pairSearchList_.begin(); i != pairSearchList_.end(); ++i)
451 PairSearchImplPointer pairSearch(new AnalysisNeighborhoodPairSearchImpl(*this));
452 pairSearchList_.push_back(pairSearch);
456 bool AnalysisNeighborhoodSearchImpl::checkGridSearchEfficiency(bool bForce)
458 // Find the extent of the sphere in cells.
460 for (int dd = 0; dd < DIM; ++dd)
462 range[dd] = static_cast<int>(ceil(cutoff_ * invCellSize_[dd]));
465 // Calculate the fraction of cell pairs that need to be searched,
466 // and check that the cutoff is not too large for periodic dimensions.
467 real coveredCells = 1.0;
468 for (int dd = 0; dd < DIM; ++dd)
470 const int cellCount = ncelldim_[dd];
471 const int coveredCount = 2 * range[dd] + 1;
474 if (coveredCount > cellCount)
476 // Cutoff is too close to half the box size for grid searching
477 // (it is not possible to find a single shift for every pair of
481 coveredCells *= coveredCount;
485 if (range[dd] >= cellCount - 1)
487 range[dd] = cellCount - 1;
488 coveredCells *= cellCount;
490 else if (coveredCount > cellCount)
492 // The sum of range+1, range+2, ..., range+N/2, ... range+1.
493 coveredCells *= range[dd] +
494 static_cast<real>((cellCount + 1)/2 * (cellCount/2 + 1)) / cellCount;
498 // The sum of range+1, ..., 2*range+1, ..., 2*range+1, ... range+1.
499 coveredCells *= coveredCount -
500 static_cast<real>(range[dd] * (range[dd] + 1)) / cellCount;
504 // Magic constant that would need tuning for optimal performance:
505 // Don't do grid searching if nearly all cell pairs would anyways need to
506 // be looped through.
507 const int totalCellCount = ncelldim_[XX] * ncelldim_[YY] * ncelldim_[ZZ];
508 if (!bForce && coveredCells >= 0.5 * totalCellCount)
515 bool AnalysisNeighborhoodSearchImpl::initGridCells(
516 const matrix box, bool bSingleCell[DIM], int posCount)
518 // Determine the size of cubes where there are on average 10 positions.
519 // The loop takes care of cases where some of the box edges are shorter
520 // than the the desired cube size; in such cases, a single grid cell is
521 // used in these dimensions, and the cube size is determined only from the
522 // larger box vectors. Such boxes should be rare, but the bounding box
523 // approach can result in very flat boxes with certain types of selections
524 // (e.g., for interfacial systems or for small number of atoms).
525 real targetsize = 0.0;
526 int prevDimCount = 4;
531 for (int dd = 0; dd < DIM; ++dd)
533 const real boxSize = box[dd][dd];
534 if (boxSize < targetsize)
536 bSingleCell[dd] = true;
551 if (dimCount == 0 || dimCount == prevDimCount)
555 targetsize = pow(volume * 10 / posCount, static_cast<real>(1./dimCount));
556 prevDimCount = dimCount;
559 int totalCellCount = 1;
560 for (int dd = 0; dd < DIM; ++dd)
569 cellCount = std::max(1, static_cast<int>(box[dd][dd] / targetsize));
570 if (bGridPBC_[dd] && cellCount < 3)
575 totalCellCount *= cellCount;
576 ncelldim_[dd] = cellCount;
578 if (totalCellCount <= 3)
582 // Never decrease the size of the cell vector to avoid reallocating
583 // memory for the nested vectors. The actual size of the vector is not
584 // used outside this function.
585 if (cells_.size() < static_cast<size_t>(totalCellCount))
587 cells_.resize(totalCellCount);
589 for (int ci = 0; ci < totalCellCount; ++ci)
596 bool AnalysisNeighborhoodSearchImpl::initGrid(
597 const t_pbc &pbc, int posCount, const rvec x[], bool bForce)
607 bGridPBC_[XX] = false;
608 bGridPBC_[YY] = false;
609 bGridPBC_[ZZ] = false;
612 bGridPBC_[XX] = true;
613 bGridPBC_[YY] = true;
614 bGridPBC_[ZZ] = false;
617 bGridPBC_[XX] = true;
618 bGridPBC_[YY] = true;
619 bGridPBC_[ZZ] = true;
622 // Grid searching not supported for now with screw.
626 bool bSingleCell[DIM] = {false, false, bXY_};
628 copy_mat(pbc.box, box);
629 // TODO: In principle, we could use the bounding box for periodic
630 // dimensions as well if the bounding box is sufficiently far from the box
632 rvec origin, boundingBoxSize;
633 computeBoundingBox(posCount, x, origin, boundingBoxSize);
634 clear_rvec(gridOrigin_);
635 for (int dd = 0; dd < DIM; ++dd)
637 if (!bGridPBC_[dd] && !bSingleCell[dd])
639 gridOrigin_[dd] = origin[dd];
641 box[dd][dd] = boundingBoxSize[dd];
643 // TODO: In case the zero vector comes from the bounding box, this does
644 // not lead to a very efficient grid search, but that should be rare.
645 if (box[dd][dd] <= 0.0)
647 GMX_ASSERT(!bGridPBC_[dd], "Periodic box vector is zero");
648 bSingleCell[dd] = true;
654 if (!initGridCells(box, bSingleCell, posCount))
659 bTric_ = TRICLINIC(pbc.box);
660 for (int dd = 0; dd < DIM; ++dd)
662 cellSize_[dd] = box[dd][dd] / ncelldim_[dd];
665 invCellSize_[dd] = 0.0;
669 invCellSize_[dd] = 1.0 / cellSize_[dd];
674 cellShiftZY_ = box[ZZ][YY] * invCellSize_[YY];
675 cellShiftZX_ = box[ZZ][XX] * invCellSize_[XX];
676 cellShiftYX_ = box[YY][XX] * invCellSize_[XX];
678 return checkGridSearchEfficiency(bForce);
681 void AnalysisNeighborhoodSearchImpl::mapPointToGridCell(const rvec x,
686 rvec_sub(x, gridOrigin_, xtmp);
687 // The reverse order is necessary for triclinic cells: shifting in Z may
688 // modify also X and Y, and shifting in Y may modify X, so the mapping to
689 // a rectangular grid needs to be done in this order.
690 for (int dd = DIM - 1; dd >= 0; --dd)
692 real cellIndex = xtmp[dd] * invCellSize_[dd];
695 const real cellCount = ncelldim_[dd];
696 while (cellIndex < 0)
698 cellIndex += cellCount;
699 rvec_inc(xtmp, pbc_.box[dd]);
701 while (cellIndex >= cellCount)
703 cellIndex -= cellCount;
704 rvec_dec(xtmp, pbc_.box[dd]);
707 cell[dd] = cellIndex;
709 copy_rvec(xtmp, xout);
712 int AnalysisNeighborhoodSearchImpl::getGridCellIndex(const ivec cell) const
714 GMX_ASSERT(cell[XX] >= 0 && cell[XX] < ncelldim_[XX],
715 "Grid cell X index out of range");
716 GMX_ASSERT(cell[YY] >= 0 && cell[YY] < ncelldim_[YY],
717 "Grid cell Y index out of range");
718 GMX_ASSERT(cell[ZZ] >= 0 && cell[ZZ] < ncelldim_[ZZ],
719 "Grid cell Z index out of range");
720 return cell[XX] + cell[YY] * ncelldim_[XX]
721 + cell[ZZ] * ncelldim_[XX] * ncelldim_[YY];
724 void AnalysisNeighborhoodSearchImpl::addToGridCell(const rvec cell, int i)
727 for (int dd = 0; dd < DIM; ++dd)
729 int cellIndex = static_cast<int>(floor(cell[dd]));
732 const int cellCount = ncelldim_[dd];
737 else if (cellIndex >= cellCount)
739 cellIndex = cellCount - 1;
742 icell[dd] = cellIndex;
744 const int ci = getGridCellIndex(icell);
745 cells_[ci].push_back(i);
748 void AnalysisNeighborhoodSearchImpl::initCellRange(
749 const rvec centerCell, ivec currCell, ivec upperBound, int dim) const
751 // TODO: Prune off cells that are completely outside the cutoff.
752 const real range = cutoff_ * invCellSize_[dim];
753 real startOffset = centerCell[dim] - range;
754 real endOffset = centerCell[dim] + range;
762 if (currCell[ZZ] < 0)
764 startOffset += cellShiftZY_;
765 endOffset += cellShiftZY_;
767 else if (currCell[ZZ] >= ncelldim_[ZZ])
769 startOffset -= cellShiftZY_;
770 endOffset -= cellShiftZY_;
774 if (currCell[ZZ] < 0)
776 startOffset += cellShiftZX_;
777 endOffset += cellShiftZX_;
779 else if (currCell[ZZ] >= ncelldim_[ZZ])
781 startOffset -= cellShiftZX_;
782 endOffset -= cellShiftZX_;
784 if (currCell[YY] < 0)
786 startOffset += cellShiftYX_;
787 endOffset += cellShiftYX_;
789 else if (currCell[YY] >= ncelldim_[YY])
791 startOffset -= cellShiftYX_;
792 endOffset -= cellShiftYX_;
797 // For non-periodic dimensions, clamp to the actual grid edges.
800 // If endOffset < 0 or startOffset > N, these may cause the whole
801 // test position/grid plane/grid row to be skipped.
806 const int cellCount = ncelldim_[dim];
807 if (endOffset > cellCount - 1)
809 endOffset = cellCount - 1;
812 currCell[dim] = static_cast<int>(floor(startOffset));
813 upperBound[dim] = static_cast<int>(floor(endOffset));
816 bool AnalysisNeighborhoodSearchImpl::nextCell(
817 const rvec centerCell, ivec cell, ivec upperBound) const
824 if (cell[dim] > upperBound[dim])
829 for (int d = dim - 1; d >= 0; --d)
831 initCellRange(centerCell, cell, upperBound, d);
832 if (cell[d] > upperBound[d])
843 int AnalysisNeighborhoodSearchImpl::shiftCell(const ivec cell, rvec shift) const
846 copy_ivec(cell, shiftedCell);
849 for (int d = 0; d < DIM; ++d)
851 const int cellCount = ncelldim_[d];
854 // A single shift may not be sufficient if the cell must be shifted
855 // in more than one dimension, although for each individual
856 // dimension it would be.
857 while (shiftedCell[d] < 0)
859 shiftedCell[d] += cellCount;
860 rvec_inc(shift, pbc_.box[d]);
862 while (shiftedCell[d] >= cellCount)
864 shiftedCell[d] -= cellCount;
865 rvec_dec(shift, pbc_.box[d]);
870 return getGridCellIndex(shiftedCell);
873 void AnalysisNeighborhoodSearchImpl::init(
874 AnalysisNeighborhood::SearchMode mode,
876 const t_blocka *excls,
878 const AnalysisNeighborhoodPositions &positions)
880 GMX_RELEASE_ASSERT(positions.index_ == -1,
881 "Individual indexed positions not supported as reference");
883 if (bXY_ && pbc != NULL && pbc->ePBC != epbcNONE)
885 if (pbc->ePBC != epbcXY && pbc->ePBC != epbcXYZ)
887 std::string message =
888 formatString("Computations in the XY plane are not supported with PBC type '%s'",
890 GMX_THROW(NotImplementedError(message));
892 if (pbc->ePBC == epbcXYZ &&
893 (std::fabs(pbc->box[ZZ][XX]) > GMX_REAL_EPS*pbc->box[ZZ][ZZ] ||
894 std::fabs(pbc->box[ZZ][YY]) > GMX_REAL_EPS*pbc->box[ZZ][ZZ]))
896 GMX_THROW(NotImplementedError("Computations in the XY plane are not supported when the last box vector is not parallel to the Z axis"));
898 // Use a single grid cell in Z direction.
900 copy_mat(pbc->box, box);
902 set_pbc(&pbc_, epbcXY, box);
904 else if (pbc != NULL)
910 pbc_.ePBC = epbcNONE;
913 nref_ = positions.count_;
914 if (mode == AnalysisNeighborhood::eSearchMode_Simple)
920 bGrid_ = initGrid(pbc_, positions.count_, positions.x_,
921 mode == AnalysisNeighborhood::eSearchMode_Grid);
923 refIndices_ = positions.indices_;
926 xrefAlloc_.resize(nref_);
927 xref_ = as_rvec_array(&xrefAlloc_[0]);
929 for (int i = 0; i < nref_; ++i)
931 const int ii = (refIndices_ != NULL) ? refIndices_[i] : i;
933 mapPointToGridCell(positions.x_[ii], refcell, xrefAlloc_[i]);
934 addToGridCell(refcell, i);
937 else if (refIndices_ != NULL)
939 xrefAlloc_.resize(nref_);
940 xref_ = as_rvec_array(&xrefAlloc_[0]);
941 for (int i = 0; i < nref_; ++i)
943 copy_rvec(positions.x_[refIndices_[i]], xrefAlloc_[i]);
948 xref_ = positions.x_;
951 refExclusionIds_ = NULL;
954 // TODO: Check that the IDs are ascending, or remove the limitation.
955 refExclusionIds_ = positions.exclusionIds_;
956 GMX_RELEASE_ASSERT(refExclusionIds_ != NULL,
957 "Exclusion IDs must be set for reference positions "
958 "when exclusions are enabled");
962 /********************************************************************
963 * AnalysisNeighborhoodPairSearchImpl
966 void AnalysisNeighborhoodPairSearchImpl::reset(int testIndex)
968 testIndex_ = testIndex;
969 if (testIndex_ >= 0 && testIndex_ < testPosCount_)
972 (testIndices_ != NULL ? testIndices_[testIndex] : testIndex);
975 search_.mapPointToGridCell(testPositions_[index], testcell_, xtest_);
976 search_.initCellRange(testcell_, currCell_, cellBound_, ZZ);
977 search_.initCellRange(testcell_, currCell_, cellBound_, YY);
978 search_.initCellRange(testcell_, currCell_, cellBound_, XX);
982 copy_rvec(testPositions_[index], xtest_);
984 if (search_.excls_ != NULL)
986 const int exclIndex = testExclusionIds_[index];
987 if (exclIndex < search_.excls_->nr)
989 const int startIndex = search_.excls_->index[exclIndex];
990 nexcl_ = search_.excls_->index[exclIndex + 1] - startIndex;
991 excl_ = &search_.excls_->a[startIndex];
1002 clear_rvec(prevdx_);
1007 void AnalysisNeighborhoodPairSearchImpl::nextTestPosition()
1009 if (testIndex_ < testPosCount_)
1016 bool AnalysisNeighborhoodPairSearchImpl::isExcluded(int j)
1018 if (exclind_ < nexcl_)
1021 (search_.refIndices_ != NULL ? search_.refIndices_[j] : j);
1022 const int refId = search_.refExclusionIds_[index];
1023 while (exclind_ < nexcl_ && excl_[exclind_] < refId)
1027 if (exclind_ < nexcl_ && refId == excl_[exclind_])
1036 void AnalysisNeighborhoodPairSearchImpl::startSearch(
1037 const AnalysisNeighborhoodPositions &positions)
1039 testPosCount_ = positions.count_;
1040 testPositions_ = positions.x_;
1041 testExclusionIds_ = positions.exclusionIds_;
1042 testIndices_ = positions.indices_;
1043 GMX_RELEASE_ASSERT(search_.excls_ == NULL || testExclusionIds_ != NULL,
1044 "Exclusion IDs must be set when exclusions are enabled");
1045 if (positions.index_ < 0)
1051 // Somewhat of a hack: setup the array such that only the last position
1053 testPosCount_ = positions.index_ + 1;
1054 reset(positions.index_);
1058 template <class Action>
1059 bool AnalysisNeighborhoodPairSearchImpl::searchNext(Action action)
1061 while (testIndex_ < testPosCount_)
1065 int cai = prevcai_ + 1;
1070 const int ci = search_.shiftCell(currCell_, shift);
1071 const int cellSize = static_cast<int>(search_.cells_[ci].size());
1072 for (; cai < cellSize; ++cai)
1074 const int i = search_.cells_[ci][cai];
1080 rvec_sub(search_.xref_[i], xtest_, dx);
1081 rvec_sub(dx, shift, dx);
1084 ? dx[XX]*dx[XX] + dx[YY]*dx[YY]
1086 if (r2 <= search_.cutoff2_)
1088 if (action(i, r2, dx))
1093 copy_rvec(dx, prevdx_);
1101 while (search_.nextCell(testcell_, currCell_, cellBound_));
1105 for (int i = previ_ + 1; i < search_.nref_; ++i)
1112 if (search_.pbc_.ePBC != epbcNONE)
1114 pbc_dx(&search_.pbc_, search_.xref_[i], xtest_, dx);
1118 rvec_sub(search_.xref_[i], xtest_, dx);
1122 ? dx[XX]*dx[XX] + dx[YY]*dx[YY]
1124 if (r2 <= search_.cutoff2_)
1126 if (action(i, r2, dx))
1130 copy_rvec(dx, prevdx_);
1141 void AnalysisNeighborhoodPairSearchImpl::initFoundPair(
1142 AnalysisNeighborhoodPair *pair) const
1146 *pair = AnalysisNeighborhoodPair();
1150 *pair = AnalysisNeighborhoodPair(previ_, testIndex_, prevr2_, prevdx_);
1154 } // namespace internal
1160 * Search action to find the next neighbor.
1162 * Used as the action for AnalysisNeighborhoodPairSearchImpl::searchNext() to
1163 * find the next neighbor.
1165 * Simply breaks the loop on the first found neighbor.
1167 bool withinAction(int /*i*/, real /*r2*/, const rvec /* dx */)
1173 * Search action find the minimum distance.
1175 * Used as the action for AnalysisNeighborhoodPairSearchImpl::searchNext() to
1176 * find the nearest neighbor.
1178 * With this action, AnalysisNeighborhoodPairSearchImpl::searchNext() always
1179 * returns false, and the output is put into the variables passed by pointer
1180 * into the constructor. If no neighbors are found, the output variables are
1181 * not modified, i.e., the caller must initialize them.
1187 * Initializes the action with given output locations.
1189 * \param[out] closestPoint Index of the closest reference location.
1190 * \param[out] minDist2 Minimum distance squared.
1191 * \param[out] dx Shortest distance vector.
1193 * The constructor call does not modify the pointed values, but only
1194 * stores the pointers for later use.
1195 * See the class description for additional semantics.
1197 MindistAction(int *closestPoint, real *minDist2, rvec *dx)
1198 : closestPoint_(*closestPoint), minDist2_(*minDist2), dx_(*dx)
1202 //! Processes a neighbor to find the nearest point.
1203 bool operator()(int i, real r2, const rvec dx)
1219 GMX_DISALLOW_ASSIGN(MindistAction);
1224 /********************************************************************
1225 * AnalysisNeighborhood::Impl
1228 class AnalysisNeighborhood::Impl
1231 typedef AnalysisNeighborhoodSearch::ImplPointer SearchImplPointer;
1232 typedef std::vector<SearchImplPointer> SearchList;
1235 : cutoff_(0), excls_(NULL), mode_(eSearchMode_Automatic), bXY_(false)
1240 SearchList::const_iterator i;
1241 for (i = searchList_.begin(); i != searchList_.end(); ++i)
1243 GMX_RELEASE_ASSERT(i->unique(),
1244 "Dangling AnalysisNeighborhoodSearch reference");
1248 SearchImplPointer getSearch();
1250 tMPI::mutex createSearchMutex_;
1251 SearchList searchList_;
1253 const t_blocka *excls_;
1258 AnalysisNeighborhood::Impl::SearchImplPointer
1259 AnalysisNeighborhood::Impl::getSearch()
1261 tMPI::lock_guard<tMPI::mutex> lock(createSearchMutex_);
1262 // TODO: Consider whether this needs to/can be faster, e.g., by keeping a
1263 // separate pool of unused search objects.
1264 SearchList::const_iterator i;
1265 for (i = searchList_.begin(); i != searchList_.end(); ++i)
1272 SearchImplPointer search(new internal::AnalysisNeighborhoodSearchImpl(cutoff_));
1273 searchList_.push_back(search);
1277 /********************************************************************
1278 * AnalysisNeighborhood
1281 AnalysisNeighborhood::AnalysisNeighborhood()
1286 AnalysisNeighborhood::~AnalysisNeighborhood()
1290 void AnalysisNeighborhood::setCutoff(real cutoff)
1292 GMX_RELEASE_ASSERT(impl_->searchList_.empty(),
1293 "Changing the cutoff after initSearch() not currently supported");
1294 impl_->cutoff_ = cutoff;
1297 void AnalysisNeighborhood::setXYMode(bool bXY)
1302 void AnalysisNeighborhood::setTopologyExclusions(const t_blocka *excls)
1304 GMX_RELEASE_ASSERT(impl_->searchList_.empty(),
1305 "Changing the exclusions after initSearch() not currently supported");
1306 impl_->excls_ = excls;
1309 void AnalysisNeighborhood::setMode(SearchMode mode)
1311 impl_->mode_ = mode;
1314 AnalysisNeighborhood::SearchMode AnalysisNeighborhood::mode() const
1316 return impl_->mode_;
1319 AnalysisNeighborhoodSearch
1320 AnalysisNeighborhood::initSearch(const t_pbc *pbc,
1321 const AnalysisNeighborhoodPositions &positions)
1323 Impl::SearchImplPointer search(impl_->getSearch());
1324 search->init(mode(), impl_->bXY_, impl_->excls_,
1326 return AnalysisNeighborhoodSearch(search);
1329 /********************************************************************
1330 * AnalysisNeighborhoodSearch
1333 AnalysisNeighborhoodSearch::AnalysisNeighborhoodSearch()
1337 AnalysisNeighborhoodSearch::AnalysisNeighborhoodSearch(const ImplPointer &impl)
1342 void AnalysisNeighborhoodSearch::reset()
1347 AnalysisNeighborhood::SearchMode AnalysisNeighborhoodSearch::mode() const
1349 GMX_RELEASE_ASSERT(impl_, "Accessing an invalid search object");
1350 return (impl_->usesGridSearch()
1351 ? AnalysisNeighborhood::eSearchMode_Grid
1352 : AnalysisNeighborhood::eSearchMode_Simple);
1355 bool AnalysisNeighborhoodSearch::isWithin(
1356 const AnalysisNeighborhoodPositions &positions) const
1358 GMX_RELEASE_ASSERT(impl_, "Accessing an invalid search object");
1359 internal::AnalysisNeighborhoodPairSearchImpl pairSearch(*impl_);
1360 pairSearch.startSearch(positions);
1361 return pairSearch.searchNext(&withinAction);
1364 real AnalysisNeighborhoodSearch::minimumDistance(
1365 const AnalysisNeighborhoodPositions &positions) const
1367 GMX_RELEASE_ASSERT(impl_, "Accessing an invalid search object");
1368 internal::AnalysisNeighborhoodPairSearchImpl pairSearch(*impl_);
1369 pairSearch.startSearch(positions);
1370 real minDist2 = impl_->cutoffSquared();
1371 int closestPoint = -1;
1372 rvec dx = {0.0, 0.0, 0.0};
1373 MindistAction action(&closestPoint, &minDist2, &dx);
1374 (void)pairSearch.searchNext(action);
1375 return sqrt(minDist2);
1378 AnalysisNeighborhoodPair
1379 AnalysisNeighborhoodSearch::nearestPoint(
1380 const AnalysisNeighborhoodPositions &positions) const
1382 GMX_RELEASE_ASSERT(impl_, "Accessing an invalid search object");
1383 internal::AnalysisNeighborhoodPairSearchImpl pairSearch(*impl_);
1384 pairSearch.startSearch(positions);
1385 real minDist2 = impl_->cutoffSquared();
1386 int closestPoint = -1;
1387 rvec dx = {0.0, 0.0, 0.0};
1388 MindistAction action(&closestPoint, &minDist2, &dx);
1389 (void)pairSearch.searchNext(action);
1390 return AnalysisNeighborhoodPair(closestPoint, 0, minDist2, dx);
1393 AnalysisNeighborhoodPairSearch
1394 AnalysisNeighborhoodSearch::startPairSearch(
1395 const AnalysisNeighborhoodPositions &positions) const
1397 GMX_RELEASE_ASSERT(impl_, "Accessing an invalid search object");
1398 Impl::PairSearchImplPointer pairSearch(impl_->getPairSearch());
1399 pairSearch->startSearch(positions);
1400 return AnalysisNeighborhoodPairSearch(pairSearch);
1403 /********************************************************************
1404 * AnalysisNeighborhoodPairSearch
1407 AnalysisNeighborhoodPairSearch::AnalysisNeighborhoodPairSearch(
1408 const ImplPointer &impl)
1413 bool AnalysisNeighborhoodPairSearch::findNextPair(AnalysisNeighborhoodPair *pair)
1415 bool bFound = impl_->searchNext(&withinAction);
1416 impl_->initFoundPair(pair);
1420 void AnalysisNeighborhoodPairSearch::skipRemainingPairsForTestPosition()
1422 impl_->nextTestPosition();