"platforms/hip/tests/TestHipPeriodicTorsionForce.cpp" did not exist on "85da5e0f9018719e95350c76b483a2160c84d9d3"
CpuNeighborList.h 6.79 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
59
    /**
     * 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
     */
60
    void computeNeighborList(int numAtoms, const AlignedArray<float>& atomLocations, const std::vector<std::set<int> >& exclusions,
peastman's avatar
peastman committed
61
            const Vec3* periodicBoxVectors, bool usePeriodic, float maxDistance, ThreadPool& threads);
62
63
64
65
66
67
68
    /**
     * 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);
69
    int getNumBlocks() const;
70
    int getBlockSize() const;
71
72
73
74
    /**
     * Get an object for iterating over the neighbors of an atom block.
     */
    NeighborIterator getNeighborIterator(int blockIndex) const;
Daniel Towner's avatar
Daniel Towner committed
75
    const std::vector<int32_t>& getSortedAtoms() const;
76
    const std::vector<int>& getBlockNeighbors(int blockIndex) const;
77
78
79
80
81
82
83
84
85

    /**
     * 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;

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

110
111
112
113
114
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
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;
};

145
146
} // namespace OpenMM

147
#endif // OPENMM_CPU_NEIGHBORLIST_H_