nonbondedParameters.cc 4.89 KB
Newer Older
1
2
3
/**
 * Compute the nonbonded parameters for particles and exceptions.
 */
4
5
KERNEL void computeParameters(GLOBAL mixed* RESTRICT energyBuffer, int includeSelfEnergy, GLOBAL real* RESTRICT globalParams,
        int numAtoms, GLOBAL const float4* RESTRICT baseParticleParams, GLOBAL real4* RESTRICT posq, GLOBAL real* RESTRICT charge,
6
7
        GLOBAL float2* RESTRICT sigmaEpsilon, GLOBAL float4* RESTRICT particleParamOffsets, GLOBAL int* RESTRICT particleOffsetIndices,
        GLOBAL real* RESTRICT chargeBuffer
8
#ifdef HAS_EXCEPTIONS
9
10
        , int numExceptions, GLOBAL const float4* RESTRICT baseExceptionParams, GLOBAL float4* RESTRICT exceptionParams,
        GLOBAL float4* RESTRICT exceptionParamOffsets, GLOBAL int* RESTRICT exceptionOffsetIndices
11
12
13
#endif
        ) {
    mixed energy = 0;
14
    real totalCharge = 0;
15
16
17

    // Compute particle parameters.
    
18
    for (int i = GLOBAL_ID; i < numAtoms; i += GLOBAL_SIZE) {
19
        float4 params = baseParticleParams[i];
20
#ifdef HAS_PARTICLE_OFFSETS
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        int start = particleOffsetIndices[i], end = particleOffsetIndices[i+1];
        for (int j = start; j < end; j++) {
            float4 offset = particleParamOffsets[j];
            real value = globalParams[(int) offset.w];
            params.x += value*offset.x;
            params.y += value*offset.y;
            params.z += value*offset.z;
        }
#endif
#ifdef USE_POSQ_CHARGES
        posq[i].w = params.x;
#else
        charge[i] = params.x;
#endif
        sigmaEpsilon[i] = make_float2(0.5f*params.y, 2*SQRT(params.z));
36
        totalCharge += params.x;
37
38
#ifdef HAS_OFFSETS
    #ifdef INCLUDE_EWALD
39
        energy -= EWALD_SELF_ENERGY_SCALE*params.x*params.x;
40
41
    #endif
    #ifdef INCLUDE_LJPME
42
43
        real sig3 = params.y*params.y*params.y;
        energy += LJPME_SELF_ENERGY_SCALE*sig3*sig3*params.z;
44
    #endif
45
46
47
48
49
50
#endif
    }

    // Compute exception parameters.
    
#ifdef HAS_EXCEPTIONS
51
    for (int i = GLOBAL_ID; i < numExceptions; i += GLOBAL_SIZE) {
52
        float4 params = baseExceptionParams[i];
53
#ifdef HAS_EXCEPTION_OFFSETS
54
55
56
57
58
59
60
61
62
        int start = exceptionOffsetIndices[i], end = exceptionOffsetIndices[i+1];
        for (int j = start; j < end; j++) {
            float4 offset = exceptionParamOffsets[j];
            real value = globalParams[(int) offset.w];
            params.x += value*offset.x;
            params.y += value*offset.y;
            params.z += value*offset.z;
        }
#endif
63
        exceptionParams[i] = make_float4((float) (ONE_4PI_EPS0*params.x), (float) params.y, (float) (4*params.z), 0);
64
65
    }
#endif
Peter Eastman's avatar
Peter Eastman committed
66
    if (includeSelfEnergy)
67
        energyBuffer[GLOBAL_ID] += energy;
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    // Record the total charge from particles processed by this block.

#if defined(HAS_OFFSETS) && defined(INCLUDE_EWALD)
    LOCAL real temp[WORK_GROUP_SIZE];
    temp[LOCAL_ID] = totalCharge;
    for (int i = 1; i < WORK_GROUP_SIZE; i *= 2) {
        SYNC_THREADS;
        if (LOCAL_ID%(i*2) == 0 && LOCAL_ID+i < WORK_GROUP_SIZE)
            temp[LOCAL_ID] += temp[LOCAL_ID+i];
    }
    if (LOCAL_ID == 0)
        chargeBuffer[GROUP_ID] = temp[0];
#endif
Peter Eastman's avatar
Peter Eastman committed
82
}
83

Peter Eastman's avatar
Peter Eastman committed
84
85
86
/**
 * Compute parameters for subtracting the reciprocal part of excluded interactions.
 */
87
88
89
KERNEL void computeExclusionParameters(GLOBAL real4* RESTRICT posq, GLOBAL real* RESTRICT charge, GLOBAL float2* RESTRICT sigmaEpsilon,
        int numExclusions, GLOBAL const int2* RESTRICT exclusionAtoms, GLOBAL float4* RESTRICT exclusionParams) {
    for (int i = GLOBAL_ID; i < numExclusions; i += GLOBAL_SIZE) {
90
91
92
93
94
95
        int2 atoms = exclusionAtoms[i];
#ifdef USE_POSQ_CHARGES
        real chargeProd = posq[atoms.x].w*posq[atoms.y].w;
#else
        real chargeProd = charge[atoms.x]*charge[atoms.y];
#endif
96
#ifdef INCLUDE_LJPME_EXCEPTIONS
97
98
99
100
101
102
103
104
        float2 sigEps1 = sigmaEpsilon[atoms.x];
        float2 sigEps2 = sigmaEpsilon[atoms.y];
        float sigma = sigEps1.x*sigEps2.x;
        float epsilon = sigEps1.y*sigEps2.y;
#else
        float sigma = 0;
        float epsilon = 0;
#endif
105
        exclusionParams[i] = make_float4((float) (ONE_4PI_EPS0*chargeProd), sigma, epsilon, 0);
106
    }
107
108
109
110
111
112
113
114
115
116
117
}

/**
 * When using Ewald or PME with parameter offsets, the total charge can change each step.
 * We therefore need to compute the correction for the neutralizing plasma on the GPU.
 * This kernel is executed by a single thread block.
 */
KERNEL void computePlasmaCorrection(GLOBAL real* RESTRICT chargeBuffer, GLOBAL mixed* RESTRICT energyBuffer,
        real alpha, real volume) {
    LOCAL real temp[WORK_GROUP_SIZE];
    real sum = 0;
118
    for (unsigned int index = LOCAL_ID; index < CHARGE_BUFFER_SIZE; index += LOCAL_SIZE)
119
120
121
122
123
124
125
126
127
128
129
        sum += chargeBuffer[index];
    temp[LOCAL_ID] = sum;
    for (int i = 1; i < WORK_GROUP_SIZE; i *= 2) {
        SYNC_THREADS;
        if (LOCAL_ID%(i*2) == 0 && LOCAL_ID+i < WORK_GROUP_SIZE)
            temp[LOCAL_ID] += temp[LOCAL_ID+i];
    }
    if (LOCAL_ID == 0) {
        real totalCharge = temp[0];
        energyBuffer[0] -= totalCharge*totalCharge/(8*EPSILON0*volume*alpha*alpha);
    }
Peter Eastman's avatar
Peter Eastman committed
130
}