customHbondForce.cc 6.1 KB
Newer Older
1
/**
Peter Eastman's avatar
Peter Eastman committed
2
 * Compute the difference between two vectors, optionally taking periodic boundary conditions into account
3
4
 * and setting the fourth component to the squared magnitude.
 */
5
inline DEVICE real4 delta(real3 vec1, real3 vec2, real4 periodicBoxSize, real4 invPeriodicBoxSize, real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ) {
6
    real4 result = make_real4(vec1.x-vec2.x, vec1.y-vec2.y, vec1.z-vec2.z, 0);
7
#ifdef USE_PERIODIC
8
    APPLY_PERIODIC_TO_DELTA(result)
9
10
11
12
13
14
15
16
#endif
    result.w = result.x*result.x + result.y*result.y + result.z*result.z;
    return result;
}

/**
 * Compute the angle between two vectors.  The w component of each vector should contain the squared magnitude.
 */
17
inline DEVICE real computeAngle(real4 vec1, real4 vec2) {
18
19
20
    real dotProduct = vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z;
    real cosine = dotProduct*RSQRT(vec1.w*vec2.w);
    real angle;
21
22
23
    if (cosine > 0.99f || cosine < -0.99f) {
        // We're close to the singularity in acos(), so take the cross product and use asin() instead.

24
        real3 crossProduct = cross(trimTo3(vec1), trimTo3(vec2));
25
        real scale = vec1.w*vec2.w;
26
27
28
        angle = ASIN(SQRT(dot(crossProduct, crossProduct)/scale));
        if (cosine < 0)
            angle = M_PI-angle;
29
30
    }
    else
31
       angle = ACOS(cosine);
32
33
34
35
    return angle;
}

/**
36
 * Compute the cross product of two vectors, setting the fourth component to the squared magnitude.
37
 */
38
39
40
inline DEVICE real4 computeCross(real4 vec1, real4 vec2) {
    real3 cp = cross(trimTo3(vec1), trimTo3(vec2));
    return make_real4(cp.x, cp.y, cp.z, cp.x*cp.x+cp.y*cp.y+cp.z*cp.z);
41
}
42

43
/**
44
 * Write the force on an atom to global memory.
45
 */
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
inline DEVICE void applyForce(int atom, real3 f, GLOBAL mm_ulong* force) {
    if (atom > -1) {
        if (f.x != 0)
            ATOMIC_ADD(&force[atom], (mm_ulong) realToFixedPoint(f.x));
        if (f.y != 0)
            ATOMIC_ADD(&force[atom+PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(f.y));
        if (f.z != 0)
            ATOMIC_ADD(&force[atom+2*PADDED_NUM_ATOMS], (mm_ulong) realToFixedPoint(f.z));
        MEM_FENCE;
    }
}

typedef struct {
    real3 pos1, pos2, pos3;
    real3 f1, f2, f3;
} AcceptorData;

/**
 * Compute forces on donors and acceptors.
 */
KERNEL void computeHbondForces(
67
68
69
	GLOBAL mm_ulong* RESTRICT force,
	GLOBAL mixed* RESTRICT energyBuffer, GLOBAL const real4* RESTRICT posq, GLOBAL const int4* RESTRICT exclusions,
        GLOBAL const int4* RESTRICT donorAtoms, GLOBAL const int4* RESTRICT acceptorAtoms, real4 periodicBoxSize, real4 invPeriodicBoxSize,
70
        real4 periodicBoxVecX, real4 periodicBoxVecY, real4 periodicBoxVecZ
71
        PARAMETER_ARGUMENTS) {
72
73
74
75
76
    const unsigned int totalWarps = GLOBAL_SIZE/32;
    const unsigned int warp = GLOBAL_ID/32;
    const int indexInWarp = GLOBAL_ID%32;
    const int tbx = LOCAL_ID-indexInWarp;
    LOCAL AcceptorData localData[THREAD_BLOCK_SIZE];
77
    mixed energy = 0;
78
79
80
81
    for (int tile = warp; tile < NUM_DONOR_BLOCKS*NUM_ACCEPTOR_BLOCKS; tile += totalWarps) {
        int donorStart = (tile/NUM_ACCEPTOR_BLOCKS)*32;
        int acceptorStart = (tile%NUM_ACCEPTOR_BLOCKS)*32;

82
83
        // Load information about the donor this thread will compute forces on.

84
85
86
87
        real3 f1 = make_real3(0);
        real3 f2 = make_real3(0);
        real3 f3 = make_real3(0);
        int donorIndex = donorStart+indexInWarp;
88
        int4 atoms, exclusionIndices;
89
        real3 d1, d2, d3;
90
91
        if (donorIndex < NUM_DONORS) {
            atoms = donorAtoms[donorIndex];
92
93
94
            d1 = (atoms.x > -1 ? trimTo3(posq[atoms.x]) : make_real3(0));
            d2 = (atoms.y > -1 ? trimTo3(posq[atoms.y]) : make_real3(0));
            d3 = (atoms.z > -1 ? trimTo3(posq[atoms.z]) : make_real3(0));
95
96
97
#ifdef USE_EXCLUSIONS
            exclusionIndices = exclusions[donorIndex];
#endif
98
99
        }
        else
100
            atoms = make_int4(-1, -1, -1, -1);
101

102
        // Load information about the acceptors into local memory.
103

104
105
106
107
108
109
110
111
112
113
        SYNC_WARPS;
        localData[LOCAL_ID].f1 = make_real3(0);
        localData[LOCAL_ID].f2 = make_real3(0);
        localData[LOCAL_ID].f3 = make_real3(0);
        int blockSize = min(32, NUM_ACCEPTORS-acceptorStart);
        int4 atoms2 = (indexInWarp < blockSize ? acceptorAtoms[acceptorStart+indexInWarp] : make_int4(-1));
        if (indexInWarp < blockSize) {
            localData[LOCAL_ID].pos1 = (atoms2.x > -1 ? trimTo3(posq[atoms2.x]) : make_real3(0));
            localData[LOCAL_ID].pos2 = (atoms2.y > -1 ? trimTo3(posq[atoms2.y]) : make_real3(0));
            localData[LOCAL_ID].pos3 = (atoms2.z > -1 ? trimTo3(posq[atoms2.z]) : make_real3(0));
114
        }
115
        SYNC_WARPS;
116
        if (donorIndex < NUM_DONORS) {
117
118
119
            int index = indexInWarp;
            for (int j = 0; j < 32; j++) {
                int acceptorIndex = acceptorStart+index;
120
#ifdef USE_EXCLUSIONS
121
122
123
                if (acceptorIndex < NUM_ACCEPTORS && acceptorIndex != exclusionIndices.x && acceptorIndex != exclusionIndices.y && acceptorIndex != exclusionIndices.z && acceptorIndex != exclusionIndices.w) {
#else
                if (acceptorIndex < NUM_ACCEPTORS) {
124
#endif
125
126
                    // Compute the interaction between a donor and an acceptor.

127
128
129
                    real3 a1 = localData[tbx+index].pos1;
                    real3 a2 = localData[tbx+index].pos2;
                    real3 a3 = localData[tbx+index].pos3;
Peter Eastman's avatar
Peter Eastman committed
130
                    real4 deltaD1A1 = delta(d1, a1, periodicBoxSize, invPeriodicBoxSize, periodicBoxVecX, periodicBoxVecY, periodicBoxVecZ);
131
132
#ifdef USE_CUTOFF
                    if (deltaD1A1.w < CUTOFF_SQUARED) {
133
#endif
134
                        COMPUTE_FORCE
135
#ifdef USE_CUTOFF
136
                    }
137
#endif
138
                }
139
                index = (index+1)%32;
140
141
142
143
144
            }
        }

        // Write results

145
146
147
148
        if (donorIndex < NUM_DONORS) {
            applyForce(atoms.x, f1, force);
            applyForce(atoms.y, f2, force);
            applyForce(atoms.z, f3, force);
149
        }
150
151
152
153
        SYNC_WARPS;
        applyForce(atoms2.x, localData[LOCAL_ID].f1, force);
        applyForce(atoms2.y, localData[LOCAL_ID].f2, force);
        applyForce(atoms2.z, localData[LOCAL_ID].f3, force);
154
    }
155
    energyBuffer[GLOBAL_ID] += energy;
156
}