CpuGayBerneForce.h 6.78 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
peastman's avatar
peastman committed
9
 * Portions copyright (c) 2016-2018 Stanford University and the Authors.      *
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
 * 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
#include "CpuNeighborList.h"
#include "CpuPlatform.h"
peastman's avatar
peastman committed
39
#include "openmm/Vec3.h"
40
41
42
43
44
#include <set>
#include <utility>

namespace OpenMM {

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

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

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

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

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;
peastman's avatar
peastman committed
84
    double cutoffDistance, switchingDistance;
85
    bool useSwitchingFunction;
peastman's avatar
peastman committed
86
    std::vector<double> s;
87
    std::vector<Matrix> A, B, G;
88
    std::vector<double> threadEnergy;
peastman's avatar
peastman committed
89
    std::vector<std::vector<Vec3> > threadTorque;
90
    // The following variables are used to make information accessible to the individual threads.
peastman's avatar
peastman committed
91
    Vec3 const* positions;
92
    std::vector<AlignedArray<float> >* threadForce;
peastman's avatar
peastman committed
93
    Vec3* boxVectors;
peastman's avatar
peastman committed
94
    std::atomic<int> atomicCounter;
95

peastman's avatar
peastman committed
96
    void computeEllipsoidFrames(const std::vector<Vec3>& positions);
97
    
peastman's avatar
peastman committed
98
    void applyTorques(const std::vector<Vec3>& positions, std::vector<Vec3>& forces);
99

peastman's avatar
peastman committed
100
101
    double computeOneInteraction(int particle1, int particle2, double sigma, double epsilon, const Vec3* positions,
            float* forces, std::vector<Vec3>& torques, const Vec3* boxVectors);
102
103
104
105
};

struct CpuGayBerneForce::ParticleInfo {
    int xparticle, yparticle;
peastman's avatar
peastman committed
106
    double sigmaOver2, sqrtEpsilon, rx, ry, rz, ex, ey, ez;
107
108
109
110
111
    bool isPointParticle;
};

struct CpuGayBerneForce::ExceptionInfo {
    int particle1, particle2;
peastman's avatar
peastman committed
112
    double sigma, epsilon;
113
114
115
};

struct CpuGayBerneForce::Matrix {
peastman's avatar
peastman committed
116
117
118
119
120
    double v[3][3];
    Vec3 operator*(const Vec3& r) {
        return Vec3(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]);
121
122
123
124
125
126
127
128
129
130
    }

    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;
    }

peastman's avatar
peastman committed
131
    double determinant() {
132
133
134
135
136
        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() {
peastman's avatar
peastman committed
137
        double invDet = 1/determinant();
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        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;
    }
};

peastman's avatar
peastman committed
152
153
154
155
static Vec3 operator*(const Vec3& r, CpuGayBerneForce::Matrix& m) {
    return Vec3(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]);
156
157
158
159
160
}

} // namespace OpenMM

#endif // OPENMM_CPU_GAYBERNEFORCE_H__