2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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.
35 /*! \page nbsearch Neighborhood search routines
37 * Functions to find particles within a neighborhood of a set of particles
38 * are defined in nbsearch.h.
39 * The usage is simple: a data structure is allocated with
40 * gmx_ana_nbsearch_create(), and the box shape and reference positions for a
41 * frame are set using gmx_ana_nbsearch_init() or gmx_ana_nbsearch_pos_init().
42 * Searches can then be performed with gmx_ana_nbsearch_is_within() and
43 * gmx_ana_nbsearch_mindist(), or with versions that take the \c gmx_ana_pos_t
45 * When the data structure is no longer required, it can be freed with
46 * gmx_ana_nbsearch_free().
51 * The grid implementation could still be optimized in several different ways:
52 * - Triclinic grid cells are not the most efficient shape, but make PBC
54 * - Precalculating the required PBC shift for a pair of cells outside the
55 * inner loop. After this is done, it should be quite straightforward to
56 * move to rectangular cells.
57 * - Pruning grid cells from the search list if they are completely outside
58 * the sphere that is being considered.
59 * - A better heuristic could be added for falling back to simple loops for a
60 * small number of reference particles.
61 * - A better heuristic for selecting the grid size.
62 * - A multi-level grid implementation could be used to be able to use small
63 * grids for short cutoffs with very inhomogeneous particle distributions
64 * without a memory cost.
68 * Implements functions in nbsearch.h.
70 * \author Teemu Murtola <teemu.murtola@gmail.com>
71 * \ingroup module_selection
79 #include "gromacs/legacyheaders/smalloc.h"
80 #include "gromacs/legacyheaders/typedefs.h"
81 #include "gromacs/legacyheaders/pbc.h"
82 #include "gromacs/legacyheaders/vec.h"
84 #include "gromacs/selection/nbsearch.h"
85 #include "gromacs/selection/position.h"
88 * Data structure for neighborhood searches.
90 struct gmx_ana_nbsearch_t
94 /** The cutoff squared. */
96 /** Maximum number of reference points. */
99 /** Number of reference points for the current frame. */
101 /** Reference point positions. */
103 /** Reference position ids (NULL if not available). */
108 /** Number of excluded reference positions for current test particle. */
110 /** Exclusions for current test particle. */
113 /** Whether to try grid searching. */
115 /** Whether grid searching is actually used for the current positions. */
117 /** Array allocated for storing in-unit-cell reference positions. */
119 /** false if the box is rectangular. */
121 /** Box vectors of a single grid cell. */
123 /** The reciprocal cell vectors as columns; the inverse of \p cellbox. */
125 /** Number of cells along each dimension. */
127 /** Total number of cells. */
129 /** Number of reference positions in each cell. */
131 /** List of reference positions in each cell. */
133 /** Allocation counts for each \p catom[i]. */
135 /** Allocation count for the per-cell arrays. */
137 /** Number of neighboring cells to consider. */
139 /** Offsets of the neighboring cells to consider. */
141 /** Allocation count for \p gnboffs. */
144 /** Stores test position during a pair loop. */
146 /** Stores the previous returned position during a pair loop. */
148 /** Stores the current exclusion index during loops. */
150 /** Stores the test particle cell index during loops. */
152 /** Stores the current cell neighbor index during pair loops. */
154 /** Stores the index within the current cell during pair loops. */
159 * \param[in] cutoff Cutoff distance for the search
160 * (<=0 stands for no cutoff).
161 * \param[in] maxn Maximum number of reference particles.
162 * \returns Created neighborhood search data structure.
165 gmx_ana_nbsearch_create(real cutoff, int maxn)
167 gmx_ana_nbsearch_t *d;
173 cutoff = GMX_REAL_MAX;
177 d->cutoff2 = sqr(cutoff);
184 d->xref_alloc = NULL;
193 d->gnboffs_nalloc = 0;
199 * \param d Data structure to free.
201 * After the call, the pointer \p d is no longer valid.
204 gmx_ana_nbsearch_free(gmx_ana_nbsearch_t *d)
206 sfree(d->xref_alloc);
212 for (ci = 0; ci < d->ncells; ++ci)
218 sfree(d->catom_nalloc);
224 * Calculates offsets to neighboring grid cells that should be considered.
226 * \param[in,out] d Grid information.
227 * \param[in] pbc Information about the box.
230 grid_init_cell_nblist(gmx_ana_nbsearch_t *d, t_pbc *pbc)
232 int maxx, maxy, maxz;
236 /* Find the extent of the sphere in triclinic coordinates */
237 maxz = (int)(d->cutoff * d->recipcell[ZZ][ZZ]) + 1;
238 rvnorm = sqrt(sqr(d->recipcell[YY][YY]) + sqr(d->recipcell[ZZ][YY]));
239 maxy = (int)(d->cutoff * rvnorm) + 1;
240 rvnorm = sqrt(sqr(d->recipcell[XX][XX]) + sqr(d->recipcell[YY][XX])
241 + sqr(d->recipcell[ZZ][XX]));
242 maxx = (int)(d->cutoff * rvnorm) + 1;
244 /* Calculate the number of cells and reallocate if necessary */
245 d->ngridnb = (2 * maxx + 1) * (2 * maxy + 1) * (2 * maxz + 1);
246 if (d->gnboffs_nalloc < d->ngridnb)
248 d->gnboffs_nalloc = d->ngridnb;
249 srenew(d->gnboffs, d->gnboffs_nalloc);
252 /* Store the whole cube */
253 /* TODO: Prune off corners that are not needed */
255 for (x = -maxx; x <= maxx; ++x)
257 for (y = -maxy; y <= maxy; ++y)
259 for (z = -maxz; z <= maxz; ++z)
261 d->gnboffs[i][XX] = x;
262 d->gnboffs[i][YY] = y;
263 d->gnboffs[i][ZZ] = z;
271 * Determines a suitable grid size.
273 * \param[in,out] d Grid information.
274 * \param[in] pbc Information about the box.
275 * \returns false if grid search is not suitable.
278 grid_setup_cells(gmx_ana_nbsearch_t *d, t_pbc *pbc)
284 targetsize = cbrt(pbc->box[XX][XX] * pbc->box[YY][YY] * pbc->box[ZZ][ZZ]
287 targetsize = pow(pbc->box[XX][XX] * pbc->box[YY][YY] * pbc->box[ZZ][ZZ]
288 * 10 / d->nref, (real)(1./3.));
292 for (dd = 0; dd < DIM; ++dd)
294 d->ncelldim[dd] = (int)(pbc->box[dd][dd] / targetsize);
295 d->ncells *= d->ncelldim[dd];
296 if (d->ncelldim[dd] < 3)
301 /* Reallocate if necessary */
302 if (d->cells_nalloc < d->ncells)
306 srenew(d->ncatoms, d->ncells);
307 srenew(d->catom, d->ncells);
308 srenew(d->catom_nalloc, d->ncells);
309 for (i = d->cells_nalloc; i < d->ncells; ++i)
312 d->catom_nalloc[i] = 0;
314 d->cells_nalloc = d->ncells;
320 * Sets ua a search grid for a given box.
322 * \param[in,out] d Grid information.
323 * \param[in] pbc Information about the box.
324 * \returns false if grid search is not suitable.
327 grid_set_box(gmx_ana_nbsearch_t *d, t_pbc *pbc)
331 /* TODO: This check could be improved. */
332 if (0.5*pbc->max_cutoff2 < d->cutoff2)
337 if (!grid_setup_cells(d, pbc))
342 d->bTric = TRICLINIC(pbc->box);
345 for (dd = 0; dd < DIM; ++dd)
347 svmul(1.0 / d->ncelldim[dd], pbc->box[dd], d->cellbox[dd]);
349 m_inv_ur0(d->cellbox, d->recipcell);
353 for (dd = 0; dd < DIM; ++dd)
355 d->cellbox[dd][dd] = pbc->box[dd][dd] / d->ncelldim[dd];
356 d->recipcell[dd][dd] = 1 / d->cellbox[dd][dd];
359 grid_init_cell_nblist(d, pbc);
364 * Maps a point into a grid cell.
366 * \param[in] d Grid information.
367 * \param[in] x Point to map.
368 * \param[out] cell Indices of the grid cell in which \p x lies.
370 * \p x should be in the triclinic unit cell.
373 grid_map_onto(gmx_ana_nbsearch_t *d, const rvec x, ivec cell)
381 tmvmul_ur0(d->recipcell, x, tx);
382 for (dd = 0; dd < DIM; ++dd)
384 cell[dd] = (int)tx[dd];
389 for (dd = 0; dd < DIM; ++dd)
391 cell[dd] = (int)(x[dd] * d->recipcell[dd][dd]);
397 * Calculates linear index of a grid cell.
399 * \param[in] d Grid information.
400 * \param[in] cell Cell indices.
401 * \returns Linear index of \p cell.
404 grid_index(gmx_ana_nbsearch_t *d, const ivec cell)
406 return cell[XX] + cell[YY] * d->ncelldim[XX]
407 + cell[ZZ] * d->ncelldim[XX] * d->ncelldim[YY];
411 * Clears all grid cells.
413 * \param[in,out] d Grid information.
416 grid_clear_cells(gmx_ana_nbsearch_t *d)
420 for (ci = 0; ci < d->ncells; ++ci)
427 * Adds an index into a grid cell.
429 * \param[in,out] d Grid information.
430 * \param[in] cell Cell into which \p i should be added.
431 * \param[in] i Index to add.
434 grid_add_to_cell(gmx_ana_nbsearch_t *d, const ivec cell, int i)
436 int ci = grid_index(d, cell);
438 if (d->ncatoms[ci] == d->catom_nalloc[ci])
440 d->catom_nalloc[ci] += 10;
441 srenew(d->catom[ci], d->catom_nalloc[ci]);
443 d->catom[ci][d->ncatoms[ci]++] = i;
447 * \param[in,out] d Neighborhood search data structure.
448 * \param[in] pbc PBC information for the frame.
449 * \param[in] n Number of reference positions for the frame.
450 * \param[in] x \p n reference positions for the frame.
452 * Initializes the data structure \p d such that it can be used to search
453 * for the neighbors of \p x.
456 gmx_ana_nbsearch_init(gmx_ana_nbsearch_t *d, t_pbc *pbc, int n, const rvec x[])
464 else if (d->bTryGrid)
466 d->bGrid = grid_set_box(d, pbc);
474 snew(d->xref_alloc, d->maxnref);
476 d->xref = d->xref_alloc;
479 for (i = 0; i < n; ++i)
481 copy_rvec(x[i], d->xref[i]);
483 put_atoms_in_triclinic_unitcell(ecenterTRIC, pbc->box, n, d->xref);
484 for (i = 0; i < n; ++i)
488 grid_map_onto(d, d->xref[i], refcell);
489 grid_add_to_cell(d, refcell, i);
494 // Won't be modified in this case, but when a grid is used,
495 // xref _is_ modified, so it can't be const.
496 d->xref = const_cast<rvec *>(x);
502 * \param[in,out] d Neighborhood search data structure.
503 * \param[in] pbc PBC information for the frame.
504 * \param[in] p Reference positions for the frame.
506 * A convenience wrapper for gmx_ana_nbsearch_init().
509 gmx_ana_nbsearch_pos_init(gmx_ana_nbsearch_t *d, t_pbc *pbc, const gmx_ana_pos_t *p)
511 gmx_ana_nbsearch_init(d, pbc, p->nr, p->x);
512 d->refid = (p->nr < d->maxnref ? p->m.refid : NULL);
516 * \param[in,out] d Neighborhood search data structure.
517 * \param[in] nexcl Number of reference positions to exclude from next
519 * \param[in] excl Indices of reference positions to exclude.
521 * The set exclusions remain in effect until the next call of this function.
524 gmx_ana_nbsearch_set_excl(gmx_ana_nbsearch_t *d, int nexcl, int excl[])
532 * Helper function to check whether a reference point should be excluded.
535 is_excluded(gmx_ana_nbsearch_t *d, int j)
537 if (d->exclind < d->nexcl)
541 while (d->exclind < d->nexcl &&d->refid[j] > d->excl[d->exclind])
545 if (d->exclind < d->nexcl && d->refid[j] == d->excl[d->exclind])
553 while (d->bGrid && d->exclind < d->nexcl && d->excl[d->exclind] < j)
557 if (d->excl[d->exclind] == j)
568 * Initializes a grid search to find reference positions neighboring \p x.
571 grid_search_start(gmx_ana_nbsearch_t *d, const rvec x)
573 copy_rvec(x, d->xtest);
576 put_atoms_in_triclinic_unitcell(ecenterTRIC, d->pbc->box, 1, &d->xtest);
577 grid_map_onto(d, d->xtest, d->testcell);
589 * Does a grid search.
592 grid_search(gmx_ana_nbsearch_t *d,
593 bool (*action)(gmx_ana_nbsearch_t *d, int i, real r2))
604 cai = d->prevcai + 1;
606 for (; nbi < d->ngridnb; ++nbi)
610 ivec_add(d->testcell, d->gnboffs[nbi], cell);
611 /* TODO: Support for 2D and screw PBC */
612 cell[XX] = (cell[XX] + d->ncelldim[XX]) % d->ncelldim[XX];
613 cell[YY] = (cell[YY] + d->ncelldim[YY]) % d->ncelldim[YY];
614 cell[ZZ] = (cell[ZZ] + d->ncelldim[ZZ]) % d->ncelldim[ZZ];
615 ci = grid_index(d, cell);
616 /* TODO: Calculate the required PBC shift outside the inner loop */
617 for (; cai < d->ncatoms[ci]; ++cai)
619 i = d->catom[ci][cai];
620 if (is_excluded(d, i))
624 pbc_dx_aiuc(d->pbc, d->xtest, d->xref[i], dx);
626 if (r2 <= d->cutoff2)
628 if (action(d, i, r2))
644 for (; i < d->nref; ++i)
646 if (is_excluded(d, i))
652 pbc_dx(d->pbc, d->xtest, d->xref[i], dx);
656 rvec_sub(d->xtest, d->xref[i], dx);
659 if (r2 <= d->cutoff2)
661 if (action(d, i, r2))
673 * Helper function to use with grid_search() to find the next neighbor.
675 * Simply breaks the loop on the first found neighbor.
678 within_action(gmx_ana_nbsearch_t *d, int i, real r2)
684 * Helper function to use with grid_search() to find the minimum distance.
687 mindist_action(gmx_ana_nbsearch_t *d, int i, real r2)
694 * \param[in] d Neighborhood search data structure.
695 * \param[in] x Test position.
696 * \returns true if \p x is within the cutoff of any reference position,
700 gmx_ana_nbsearch_is_within(gmx_ana_nbsearch_t *d, const rvec x)
702 grid_search_start(d, x);
703 return grid_search(d, &within_action);
707 * \param[in] d Neighborhood search data structure.
708 * \param[in] p Test positions.
709 * \param[in] i Use the i'th position in \p p for testing.
710 * \returns true if the test position is within the cutoff of any reference
711 * position, false otherwise.
714 gmx_ana_nbsearch_pos_is_within(gmx_ana_nbsearch_t *d, const gmx_ana_pos_t *p, int i)
716 return gmx_ana_nbsearch_is_within(d, p->x[i]);
720 * \param[in] d Neighborhood search data structure.
721 * \param[in] x Test position.
722 * \returns The distance to the nearest reference position, or the cutoff
723 * value if there are no reference positions within the cutoff.
726 gmx_ana_nbsearch_mindist(gmx_ana_nbsearch_t *d, const rvec x)
730 grid_search_start(d, x);
731 grid_search(d, &mindist_action);
732 mind = sqrt(d->cutoff2);
733 d->cutoff2 = sqr(d->cutoff);
738 * \param[in] d Neighborhood search data structure.
739 * \param[in] p Test positions.
740 * \param[in] i Use the i'th position in \p p for testing.
741 * \returns The distance to the nearest reference position, or the cutoff
742 * value if there are no reference positions within the cutoff.
745 gmx_ana_nbsearch_pos_mindist(gmx_ana_nbsearch_t *d, const gmx_ana_pos_t *p, int i)
747 return gmx_ana_nbsearch_mindist(d, p->x[i]);
751 * \param[in] d Neighborhood search data structure.
752 * \param[in] x Test positions.
753 * \param[out] jp Index of the reference position in the first pair.
754 * \returns true if there are positions within the cutoff.
757 gmx_ana_nbsearch_first_within(gmx_ana_nbsearch_t *d, const rvec x, int *jp)
759 grid_search_start(d, x);
760 return gmx_ana_nbsearch_next_within(d, jp);
764 * \param[in] d Neighborhood search data structure.
765 * \param[in] p Test positions.
766 * \param[in] i Use the i'th position in \p p.
767 * \param[out] jp Index of the reference position in the first pair.
768 * \returns true if there are positions within the cutoff.
771 gmx_ana_nbsearch_pos_first_within(gmx_ana_nbsearch_t *d, const gmx_ana_pos_t *p,
774 return gmx_ana_nbsearch_first_within(d, p->x[i], jp);
778 * \param[in] d Neighborhood search data structure.
779 * \param[out] jp Index of the test position in the next pair.
780 * \returns true if there are positions within the cutoff.
783 gmx_ana_nbsearch_next_within(gmx_ana_nbsearch_t *d, int *jp)
785 if (grid_search(d, &within_action))