CpuNeighborList.h 7.21 KB
Newer Older
1
2
3
#ifndef OPENMM_CPU_NEIGHBORLIST_H_
#define OPENMM_CPU_NEIGHBORLIST_H_

4
5
6
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
Evan Pretti's avatar
Evan Pretti committed
7
8
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
9
 *                                                                            *
10
 * Portions copyright (c) 2013-2022 Stanford University and the Authors.      *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

33
#include "AlignedArray.h"
peastman's avatar
peastman committed
34
#include "openmm/Vec3.h"
35
#include "windowsExportCpu.h"
36
#include "openmm/internal/ThreadPool.h"
peastman's avatar
peastman committed
37
#include <atomic>
38
39
40
41
42
#include <set>
#include <utility>
#include <vector>

namespace OpenMM {
43

44
45
class OPENMM_EXPORT_CPU CpuNeighborList {
public:
46
    class Voxels;
47
    class NeighborIterator;
48
    CpuNeighborList(int blockSize);
49
50
51
52
53
54
55
56
57
58
    /**
     * Compute the neighbor list based on the current positions of atoms.
     * 
     * @param numAtoms            the number of atoms in the system
     * @param atomLocations       the positions of the atoms
     * @param exclusions          exclusions[i] contains the indices of all atoms with which atom i should not interact
     * @param periodicBoxVectors  the current periodic box vectors
     * @param usePeriodic         whether to apply periodic boundary conditions
     * @param maxDistance         the neighbor list will contain all pairs that are within this distance of each other
     * @param threads             used for parallelization
Evan Pretti's avatar
Evan Pretti committed
59
     * @param indices             numAtoms indices into the atomLocations array for indirect lookup
60
     */
61
    void computeNeighborList(int numAtoms, const AlignedArray<float>& atomLocations, const std::vector<std::set<int> >& exclusions,
Evan Pretti's avatar
Evan Pretti committed
62
            const Vec3* periodicBoxVectors, bool usePeriodic, float maxDistance, ThreadPool& threads, const std::vector<int>* indices = NULL);
63
64
65
66
67
68
69
    /**
     * Build a dense neighbor list, in which every atom interacts with every other (except exclusions), regardless of distance.
     * 
     * @param numAtoms            the number of atoms in the system
     * @param exclusions          exclusions[i] contains the indices of all atoms with which atom i should not interact
     */
    void createDenseNeighborList(int numAtoms, const std::vector<std::set<int> >& exclusions);
70
    int getNumBlocks() const;
71
    int getBlockSize() const;
72
73
74
75
    /**
     * Get an object for iterating over the neighbors of an atom block.
     */
    NeighborIterator getNeighborIterator(int blockIndex) const;
Daniel Towner's avatar
Daniel Towner committed
76
    const std::vector<int32_t>& getSortedAtoms() const;
77
    const std::vector<int>& getBlockNeighbors(int blockIndex) const;
78
79
80
81
82
83
84
85
86

    /**
     * Bitset for a single block, marking which indexes should be excluded. This data type needs to be big
     * enough to store all the bits for any possible block size.
     */
    using BlockExclusionMask = int16_t;

    const std::vector<BlockExclusionMask>& getBlockExclusions(int blockIndex) const;

Evan Pretti's avatar
Evan Pretti committed
87
88
89
90
91
private:
    template<bool USE_INDICES>
    void computeNeighborList(int numAtoms, const AlignedArray<float>& atomLocations, const std::vector<std::set<int> >& exclusions,
            const Vec3* periodicBoxVectors, bool usePeriodic, float maxDistance, ThreadPool& threads);

92
93
94
    /**
     * This routine contains the code executed by each thread.
     */
Evan Pretti's avatar
Evan Pretti committed
95
    template<bool USE_INDICES>
96
    void threadComputeNeighborList(ThreadPool& threads, int threadIndex);
97
private:
98
    int blockSize;
99
    std::vector<int> sortedAtoms;
100
    std::vector<float> sortedPositions;
101
    std::vector<std::vector<int> > blockNeighbors, blockExclusionIndices;
102
    std::vector<std::vector<BlockExclusionMask> > blockExclusions;
103
    // The following variables are used to make information accessible to the individual threads.
104
105
    float minx, maxx, miny, maxy, minz, maxz;
    std::vector<std::pair<int, int> > atomBins;
106
    Voxels* voxels;
107
108
    const std::vector<std::set<int> >* exclusions;
    const float* atomLocations;
Evan Pretti's avatar
Evan Pretti committed
109
    const int* indices;
peastman's avatar
peastman committed
110
    Vec3 periodicBoxVectors[3];
111
    int numAtoms;
112
    bool usePeriodic, dense;
113
    float maxDistance;
peastman's avatar
peastman committed
114
    std::atomic<int> atomicCounter;
115
116
};

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class OPENMM_EXPORT_CPU CpuNeighborList::NeighborIterator {
public:
    /**
     * This constructor is used for standard neighbor lists.  Do not call it directly.  Obtain a
     * NeighborIterator by calling getNeighborIterator() on a neighbor list.
     */
    NeighborIterator(const std::vector<int>& neighbors, const std::vector<BlockExclusionMask>& exclusions);
    /**
     * This constructor is used for dense neighbor lists.  Do not call it directly.  Obtain a
     * NeighborIterator by calling getNeighborIterator() on a neighbor list.
     */
    NeighborIterator(int firstAtom, int lastAtom, const std::vector<int>& exclusionIndices, const std::vector<BlockExclusionMask>& exclusions);
    /**
     * Advance the iterator to the next neighbor.
     * 
     * @return false if there are no more neighbors, true otherwise.
     */
    bool next();
    /**
     * Get the index of the current neighbor.
     */
    int getNeighbor() const;
    /**
     * Get bit flags marking which atoms in the block the current atom is excluded from interacting with.
     */
    BlockExclusionMask getExclusions() const;
private:
    bool dense;
    int currentAtom, currentIndex, lastAtom;
    BlockExclusionMask currentExclusions;
    const std::vector<int>* neighbors;
    const std::vector<int>* exclusionIndices;
    const std::vector<BlockExclusionMask>* exclusions;
};

152
153
} // namespace OpenMM

154
#endif // OPENMM_CPU_NEIGHBORLIST_H_