CpuGayBerneForce.h 6.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2016 Stanford University and the Authors.           *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

#ifndef OPENMM_CPU_GAYBERNEFORCE_H__
#define OPENMM_CPU_GAYBERNEFORCE_H__

#include "openmm/GayBerneForce.h"
36
#include "openmm/internal/ThreadPool.h"
37
38
39
40
41
42
43
44
#include "CpuNeighborList.h"
#include "CpuPlatform.h"
#include "RealVec.h"
#include <set>
#include <utility>

namespace OpenMM {

peastman's avatar
peastman committed
45
class CpuGayBerneForce {
46
47
public:
    struct Matrix;
48
49
    class ComputeTask;

50
51
52
53
54
55
56
57
58
59
    /**
     * Constructor.
     */
    CpuGayBerneForce(const GayBerneForce& force);

    /**
     * Compute the interaction.
     *
     * @param positions     the positions of the atoms
     * @param forces        forces will be added to this vector
60
     * @param threadForce   individual threads can add their forces to this vector
61
62
63
64
     * @param boxVectors    the periodic box vectors
     * @param data          the platform data for the current context
     * @return the energy of the interaction
     */
65
66
67
68
69
    RealOpenMM calculateForce(const std::vector<RealVec>& positions, std::vector<RealVec>& forces, std::vector<AlignedArray<float> >& threadForce, RealVec* boxVectors, CpuPlatform::PlatformData& data);

    /**
     * This routine contains the code executed by each thread.
     */
70
71
72
73
74
75
    void threadComputeForce(ThreadPool& threads, int threadIndex, CpuNeighborList* neighborList);
    
    /**
     * Get the exclusions being used by the force.
     */
    const std::vector<std::set<int> >& getExclusions() const;
76
77
78
79
80
81
82
83
84
85
86
87
88

private:
    struct ParticleInfo;
    struct ExceptionInfo;
    std::vector<ParticleInfo> particles;
    std::vector<ExceptionInfo> exceptions;
    std::set<std::pair<int, int> > exclusions;
    std::vector<std::set<int> > particleExclusions;
    GayBerneForce::NonbondedMethod nonbondedMethod;
    RealOpenMM cutoffDistance, switchingDistance;
    bool useSwitchingFunction;
    std::vector<RealOpenMM> s;
    std::vector<Matrix> A, B, G;
89
90
91
92
93
94
95
    std::vector<double> threadEnergy;
    std::vector<std::vector<RealVec> > threadTorque;
    // The following variables are used to make information accessible to the individual threads.
    RealVec const* positions;
    std::vector<AlignedArray<float> >* threadForce;
    RealVec* boxVectors;
    void* atomicCounter;
96
97
98

    void computeEllipsoidFrames(const std::vector<RealVec>& positions);
    
99
    void applyTorques(const std::vector<RealVec>& positions, std::vector<RealVec>& forces);
100

101
102
    RealOpenMM computeOneInteraction(int particle1, int particle2, RealOpenMM sigma, RealOpenMM epsilon, const RealVec* positions,
            float* forces, std::vector<RealVec>& torques, const RealVec* boxVectors);
103
104
105
106
107
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
};

struct CpuGayBerneForce::ParticleInfo {
    int xparticle, yparticle;
    RealOpenMM sigmaOver2, sqrtEpsilon, rx, ry, rz, ex, ey, ez;
    bool isPointParticle;
};

struct CpuGayBerneForce::ExceptionInfo {
    int particle1, particle2;
    RealOpenMM sigma, epsilon;
};

struct CpuGayBerneForce::Matrix {
    RealOpenMM v[3][3];
    RealVec operator*(const RealVec& r) {
        return RealVec(v[0][0]*r[0] + v[0][1]*r[1] + v[0][2]*r[2],
                       v[1][0]*r[0] + v[1][1]*r[1] + v[1][2]*r[2],
                       v[2][0]*r[0] + v[2][1]*r[1] + v[2][2]*r[2]);
    }

    Matrix operator+(const Matrix& m) {
        Matrix result;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                result.v[i][j] = v[i][j]+m.v[i][j];
        return result;
    }

    RealOpenMM determinant() {
        return (v[0][0]*v[1][1]*v[2][2] + v[0][1]*v[1][2]*v[2][0] + v[0][2]*v[1][0]*v[2][1] -
                v[0][0]*v[1][2]*v[2][1] - v[0][1]*v[1][0]*v[2][2] - v[0][2]*v[1][1]*v[2][0]);
    }
    
    Matrix inverse() {
        RealOpenMM invDet = 1/determinant();
        Matrix result;
        result.v[0][0] = invDet*(v[1][1]*v[2][2] - v[1][2]*v[2][1]);
        result.v[1][0] = -invDet*(v[1][0]*v[2][2] - v[1][2]*v[2][0]);
        result.v[2][0] = invDet*(v[1][0]*v[2][1] - v[1][1]*v[2][0]);
        result.v[0][1] = -invDet*(v[0][1]*v[2][2] - v[0][2]*v[2][1]);
        result.v[1][1] = invDet*(v[0][0]*v[2][2] - v[0][2]*v[2][0]);
        result.v[2][1] = -invDet*(v[0][0]*v[2][1] - v[0][1]*v[2][0]);
        result.v[0][2] = invDet*(v[0][1]*v[1][2] - v[0][2]*v[1][1]);
        result.v[1][2] = -invDet*(v[0][0]*v[1][2] - v[0][2]*v[1][0]);
        result.v[2][2] = invDet*(v[0][0]*v[1][1] - v[0][1]*v[1][0]);
        return result;
    }
};

static RealVec operator*(const RealVec& r, CpuGayBerneForce::Matrix& m) {
    return RealVec(m.v[0][0]*r[0] + m.v[1][0]*r[1] + m.v[2][0]*r[2],
                   m.v[0][1]*r[0] + m.v[1][1]*r[1] + m.v[2][1]*r[2],
                   m.v[0][2]*r[0] + m.v[1][2]*r[1] + m.v[2][2]*r[2]);
}

} // namespace OpenMM

#endif // OPENMM_CPU_GAYBERNEFORCE_H__