2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2009, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
38 /*! \page nbsearch Neighborhood search routines
40 * Functions to find particles within a neighborhood of a set of particles
41 * are defined in nbsearch.h.
42 * The usage is simple: a data structure is allocated with
43 * gmx_ana_nbsearch_create(), and the box shape and reference positions for a
44 * frame are set using gmx_ana_nbsearch_init() or gmx_ana_nbsearch_pos_init().
45 * Searches can then be performed with gmx_ana_nbsearch_is_within() and
46 * gmx_ana_nbsearch_mindist(), or with versions that take the \c gmx_ana_pos_t
48 * When the data structure is no longer required, it can be freed with
49 * gmx_ana_nbsearch_free().
54 * The grid implementation could still be optimized in several different ways:
55 * - Triclinic grid cells are not the most efficient shape, but make PBC
57 * - Precalculating the required PBC shift for a pair of cells outside the
58 * inner loop. After this is done, it should be quite straightforward to
59 * move to rectangular cells.
60 * - Pruning grid cells from the search list if they are completely outside
61 * the sphere that is being considered.
62 * - A better heuristic could be added for falling back to simple loops for a
63 * small number of reference particles.
64 * - A better heuristic for selecting the grid size.
65 * - A multi-level grid implementation could be used to be able to use small
66 * grids for short cutoffs with very inhomogeneous particle distributions
67 * without a memory cost.
70 * \brief Implementation of functions in nbsearch.h.
87 * Data structure for neighborhood searches.
89 struct gmx_ana_nbsearch_t
93 /** The cutoff squared. */
95 /** Maximum number of reference points. */
98 /** Number of reference points for the current frame. */
100 /** Reference point positions. */
102 /** Reference position ids (NULL if not available). */
107 /** Number of excluded reference positions for current test particle. */
109 /** Exclusions for current test particle. */
112 /** Whether to try grid searching. */
114 /** Whether grid searching is actually used for the current positions. */
116 /** Array allocated for storing in-unit-cell reference positions. */
118 /** FALSE if the box is rectangular. */
120 /** Box vectors of a single grid cell. */
122 /** The reciprocal cell vectors as columns; the inverse of \p cellbox. */
124 /** Number of cells along each dimension. */
126 /** Total number of cells. */
128 /** Number of reference positions in each cell. */
130 /** List of reference positions in each cell. */
132 /** Allocation counts for each \p catom[i]. */
134 /** Allocation count for the per-cell arrays. */
136 /** Number of neighboring cells to consider. */
138 /** Offsets of the neighboring cells to consider. */
140 /** Allocation count for \p gnboffs. */
143 /** Stores test position during a pair loop. */
145 /** Stores the previous returned position during a pair loop. */
147 /** Stores the current exclusion index during loops. */
149 /** Stores the test particle cell index during loops. */
151 /** Stores the current cell neighbor index during pair loops. */
153 /** Stores the index within the current cell during pair loops. */
158 * \param[out] data Neighborhood search data structure pointer to initialize.
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 0 on success.
165 gmx_ana_nbsearch_create(gmx_ana_nbsearch_t **data, 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;
200 * \param d Data structure to free.
202 * After the call, the pointer \p d is no longer valid.
205 gmx_ana_nbsearch_free(gmx_ana_nbsearch_t *d)
207 sfree(d->xref_alloc);
213 for (ci = 0; ci < d->ncells; ++ci)
219 sfree(d->catom_nalloc);
225 * Calculates offsets to neighboring grid cells that should be considered.
227 * \param[in,out] d Grid information.
228 * \param[in] pbc Information about the box.
231 grid_init_cell_nblist(gmx_ana_nbsearch_t *d, t_pbc *pbc)
233 int maxx, maxy, maxz;
237 /* Find the extent of the sphere in triclinic coordinates */
238 maxz = (int)(d->cutoff * d->recipcell[ZZ][ZZ]) + 1;
239 rvnorm = sqrt(sqr(d->recipcell[YY][YY]) + sqr(d->recipcell[ZZ][YY]));
240 maxy = (int)(d->cutoff * rvnorm) + 1;
241 rvnorm = sqrt(sqr(d->recipcell[XX][XX]) + sqr(d->recipcell[YY][XX])
242 + sqr(d->recipcell[ZZ][XX]));
243 maxx = (int)(d->cutoff * rvnorm) + 1;
245 /* Calculate the number of cells and reallocate if necessary */
246 d->ngridnb = (2 * maxx + 1) * (2 * maxy + 1) * (2 * maxz + 1);
247 if (d->gnboffs_nalloc < d->ngridnb)
249 d->gnboffs_nalloc = d->ngridnb;
250 srenew(d->gnboffs, d->gnboffs_nalloc);
253 /* Store the whole cube */
254 /* TODO: Prune off corners that are not needed */
256 for (x = -maxx; x <= maxx; ++x)
258 for (y = -maxy; y <= maxy; ++y)
260 for (z = -maxz; z <= maxz; ++z)
262 d->gnboffs[i][XX] = x;
263 d->gnboffs[i][YY] = y;
264 d->gnboffs[i][ZZ] = z;
272 * Determines a suitable grid size.
274 * \param[in,out] d Grid information.
275 * \param[in] pbc Information about the box.
276 * \returns FALSE if grid search is not suitable.
279 grid_setup_cells(gmx_ana_nbsearch_t *d, t_pbc *pbc)
285 targetsize = cbrt(pbc->box[XX][XX] * pbc->box[YY][YY] * pbc->box[ZZ][ZZ]
288 targetsize = pow(pbc->box[XX][XX] * pbc->box[YY][YY] * pbc->box[ZZ][ZZ]
289 * 10 / d->nref, 1./3.);
293 for (dd = 0; dd < DIM; ++dd)
295 d->ncelldim[dd] = (int)(pbc->box[dd][dd] / targetsize);
296 d->ncells *= d->ncelldim[dd];
297 if (d->ncelldim[dd] < 3)
302 /* Reallocate if necessary */
303 if (d->cells_nalloc < d->ncells)
307 srenew(d->ncatoms, d->ncells);
308 srenew(d->catom, d->ncells);
309 srenew(d->catom_nalloc, d->ncells);
310 for (i = d->cells_nalloc; i < d->ncells; ++i)
313 d->catom_nalloc[i] = 0;
315 d->cells_nalloc = d->ncells;
321 * Sets ua a search grid for a given box.
323 * \param[in,out] d Grid information.
324 * \param[in] pbc Information about the box.
325 * \returns FALSE if grid search is not suitable.
328 grid_set_box(gmx_ana_nbsearch_t *d, t_pbc *pbc)
332 /* TODO: This check could be improved. */
333 if (0.5*pbc->max_cutoff2 < d->cutoff2)
338 if (!grid_setup_cells(d, pbc))
343 d->bTric = TRICLINIC(pbc->box);
346 for (dd = 0; dd < DIM; ++dd)
348 svmul(1.0 / d->ncelldim[dd], pbc->box[dd], d->cellbox[dd]);
350 m_inv_ur0(d->cellbox, d->recipcell);
354 for (dd = 0; dd < DIM; ++dd)
356 d->cellbox[dd][dd] = pbc->box[dd][dd] / d->ncelldim[dd];
357 d->recipcell[dd][dd] = 1 / d->cellbox[dd][dd];
360 grid_init_cell_nblist(d, pbc);
365 * Maps a point into a grid cell.
367 * \param[in] d Grid information.
368 * \param[in] x Point to map.
369 * \param[out] cell Indices of the grid cell in which \p x lies.
371 * \p x should be in the triclinic unit cell.
374 grid_map_onto(gmx_ana_nbsearch_t *d, const rvec x, ivec cell)
382 tmvmul_ur0(d->recipcell, x, tx);
383 for (dd = 0; dd < DIM; ++dd)
385 cell[dd] = (int)tx[dd];
390 for (dd = 0; dd < DIM; ++dd)
392 cell[dd] = (int)(x[dd] * d->recipcell[dd][dd]);
398 * Calculates linear index of a grid cell.
400 * \param[in] d Grid information.
401 * \param[in] cell Cell indices.
402 * \returns Linear index of \p cell.
405 grid_index(gmx_ana_nbsearch_t *d, const ivec cell)
407 return cell[XX] + cell[YY] * d->ncelldim[XX]
408 + cell[ZZ] * d->ncelldim[XX] * d->ncelldim[YY];
412 * Clears all grid cells.
414 * \param[in,out] d Grid information.
417 grid_clear_cells(gmx_ana_nbsearch_t *d)
421 for (ci = 0; ci < d->ncells; ++ci)
428 * Adds an index into a grid cell.
430 * \param[in,out] d Grid information.
431 * \param[in] cell Cell into which \p i should be added.
432 * \param[in] i Index to add.
435 grid_add_to_cell(gmx_ana_nbsearch_t *d, const ivec cell, int i)
437 int ci = grid_index(d, cell);
439 if (d->ncatoms[ci] == d->catom_nalloc[ci])
441 d->catom_nalloc[ci] += 10;
442 srenew(d->catom[ci], d->catom_nalloc[ci]);
444 d->catom[ci][d->ncatoms[ci]++] = i;
448 * \param[in,out] d Neighborhood search data structure.
449 * \param[in] pbc PBC information for the frame.
450 * \param[in] n Number of reference positions for the frame.
451 * \param[in] x \p n reference positions for the frame.
452 * \returns 0 on success.
454 * Initializes the data structure \p d such that it can be used to search
455 * for the neighbors of \p x.
458 gmx_ana_nbsearch_init(gmx_ana_nbsearch_t *d, t_pbc *pbc, int n, rvec x[])
466 else if (d->bTryGrid)
468 d->bGrid = grid_set_box(d, pbc);
476 snew(d->xref_alloc, d->maxnref);
478 d->xref = d->xref_alloc;
481 for (i = 0; i < n; ++i)
483 copy_rvec(x[i], d->xref[i]);
485 put_atoms_in_triclinic_unitcell(ecenterTRIC, pbc->box, n, d->xref);
486 for (i = 0; i < n; ++i)
490 grid_map_onto(d, d->xref[i], refcell);
491 grid_add_to_cell(d, refcell, i);
503 * \param[in,out] d Neighborhood search data structure.
504 * \param[in] pbc PBC information for the frame.
505 * \param[in] p Reference positions for the frame.
506 * \returns 0 on success.
508 * A convenience wrapper for gmx_ana_nbsearch_init().
511 gmx_ana_nbsearch_pos_init(gmx_ana_nbsearch_t *d, t_pbc *pbc, gmx_ana_pos_t *p)
515 rc = gmx_ana_nbsearch_init(d, pbc, p->nr, p->x);
516 d->refid = (p->nr < d->maxnref ? p->m.refid : NULL);
521 * \param[in,out] d Neighborhood search data structure.
522 * \param[in] nexcl Number of reference positions to exclude from next
524 * \param[in] excl Indices of reference positions to exclude.
525 * \returns 0 on success.
527 * The set exclusions remain in effect until the next call of this function.
530 gmx_ana_nbsearch_set_excl(gmx_ana_nbsearch_t *d, int nexcl, int excl[])
539 * Helper function to check whether a reference point should be excluded.
542 is_excluded(gmx_ana_nbsearch_t *d, int j)
544 if (d->exclind < d->nexcl)
548 while (d->exclind < d->nexcl && d->refid[j] > d->excl[d->exclind])
552 if (d->exclind < d->nexcl && d->refid[j] == d->excl[d->exclind])
560 while (d->bGrid && d->exclind < d->nexcl && d->excl[d->exclind] < j)
564 if (d->excl[d->exclind] == j)
575 * Initializes a grid search to find reference positions neighboring \p x.
578 grid_search_start(gmx_ana_nbsearch_t *d, rvec x)
580 copy_rvec(x, d->xtest);
583 put_atoms_in_triclinic_unitcell(ecenterTRIC, d->pbc->box, 1, &d->xtest);
584 grid_map_onto(d, d->xtest, d->testcell);
596 * Does a grid search.
599 grid_search(gmx_ana_nbsearch_t *d,
600 gmx_bool (*action)(gmx_ana_nbsearch_t *d, int i, real r2))
611 cai = d->prevcai + 1;
613 for ( ; nbi < d->ngridnb; ++nbi)
617 ivec_add(d->testcell, d->gnboffs[nbi], cell);
618 /* TODO: Support for 2D and screw PBC */
619 cell[XX] = (cell[XX] + d->ncelldim[XX]) % d->ncelldim[XX];
620 cell[YY] = (cell[YY] + d->ncelldim[YY]) % d->ncelldim[YY];
621 cell[ZZ] = (cell[ZZ] + d->ncelldim[ZZ]) % d->ncelldim[ZZ];
622 ci = grid_index(d, cell);
623 /* TODO: Calculate the required PBC shift outside the inner loop */
624 for ( ; cai < d->ncatoms[ci]; ++cai)
626 i = d->catom[ci][cai];
627 if (is_excluded(d, i))
631 pbc_dx_aiuc(d->pbc, d->xtest, d->xref[i], dx);
633 if (r2 <= d->cutoff2)
635 if (action(d, i, r2))
651 for ( ; i < d->nref; ++i)
653 if (is_excluded(d, i))
659 pbc_dx(d->pbc, d->xtest, d->xref[i], dx);
663 rvec_sub(d->xtest, d->xref[i], dx);
666 if (r2 <= d->cutoff2)
668 if (action(d, i, r2))
680 * Helper function to use with grid_search() to find the next neighbor.
682 * Simply breaks the loop on the first found neighbor.
685 within_action(gmx_ana_nbsearch_t *d, int i, real r2)
691 * Helper function to use with grid_search() to find the minimum distance.
694 mindist_action(gmx_ana_nbsearch_t *d, int i, real r2)
701 * \param[in] d Neighborhood search data structure.
702 * \param[in] x Test position.
703 * \returns TRUE if \p x is within the cutoff of any reference position,
707 gmx_ana_nbsearch_is_within(gmx_ana_nbsearch_t *d, rvec x)
709 grid_search_start(d, x);
710 return grid_search(d, &within_action);
714 * \param[in] d Neighborhood search data structure.
715 * \param[in] p Test positions.
716 * \param[in] i Use the i'th position in \p p for testing.
717 * \returns TRUE if the test position is within the cutoff of any reference
718 * position, FALSE otherwise.
721 gmx_ana_nbsearch_pos_is_within(gmx_ana_nbsearch_t *d, gmx_ana_pos_t *p, int i)
723 return gmx_ana_nbsearch_is_within(d, p->x[i]);
727 * \param[in] d Neighborhood search data structure.
728 * \param[in] x Test position.
729 * \returns The distance to the nearest reference position, or the cutoff
730 * value if there are no reference positions within the cutoff.
733 gmx_ana_nbsearch_mindist(gmx_ana_nbsearch_t *d, rvec x)
737 grid_search_start(d, x);
738 grid_search(d, &mindist_action);
739 mind = sqrt(d->cutoff2);
740 d->cutoff2 = sqr(d->cutoff);
745 * \param[in] d Neighborhood search data structure.
746 * \param[in] p Test positions.
747 * \param[in] i Use the i'th position in \p p for testing.
748 * \returns The distance to the nearest reference position, or the cutoff
749 * value if there are no reference positions within the cutoff.
752 gmx_ana_nbsearch_pos_mindist(gmx_ana_nbsearch_t *d, gmx_ana_pos_t *p, int i)
754 return gmx_ana_nbsearch_mindist(d, p->x[i]);
758 * \param[in] d Neighborhood search data structure.
759 * \param[in] n Number of test positions in \p x.
760 * \param[in] x Test positions.
761 * \param[out] jp Index of the reference position in the first pair.
762 * \returns TRUE if there are positions within the cutoff.
765 gmx_ana_nbsearch_first_within(gmx_ana_nbsearch_t *d, rvec x, int *jp)
767 grid_search_start(d, x);
768 return gmx_ana_nbsearch_next_within(d, jp);
772 * \param[in] d Neighborhood search data structure.
773 * \param[in] p Test positions.
774 * \param[in] i Use the i'th position in \p p.
775 * \param[out] jp Index of the reference position in the first pair.
776 * \returns TRUE if there are positions within the cutoff.
779 gmx_ana_nbsearch_pos_first_within(gmx_ana_nbsearch_t *d, gmx_ana_pos_t *p,
782 return gmx_ana_nbsearch_first_within(d, p->x[i], jp);
786 * \param[in] d Neighborhood search data structure.
787 * \param[out] jp Index of the test position in the next pair.
788 * \returns TRUE if there are positions within the cutoff.
791 gmx_ana_nbsearch_next_within(gmx_ana_nbsearch_t *d, int *jp)
793 if (grid_search(d, &within_action))