langevin.cl 4.06 KB
Newer Older
1
#ifdef SUPPORTS_DOUBLE_PRECISION
2
3
4
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#endif

5
enum {VelScale, ForceScale, NoiseScale, MaxParams};
6
7
8
9
10

/**
 * Perform the first step of Langevin integration.
 */

11
12
__kernel void integrateLangevinPart1(__global float4* restrict velm, __global const float4* restrict force, __global float4* restrict posDelta,
        __global const float* restrict paramBuffer, __global const float2* restrict dt, __global const float4* restrict random, unsigned int randomIndex) {
13
14
15
16
    float vscale = paramBuffer[VelScale];
    float fscale = paramBuffer[ForceScale];
    float noisescale = paramBuffer[NoiseScale];
    float stepSize = dt[0].y;
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
17
    int index = get_global_id(0);
18
    randomIndex += index;
19
    while (index < NUM_ATOMS) {
20
21
        float4 velocity = velm[index];
        float sqrtInvMass = sqrt(velocity.w);
22
        velocity.xyz = vscale*velocity.xyz + fscale*velocity.w*force[index].xyz + noisescale*sqrtInvMass*random[randomIndex].xyz;
23
        velm[index] = velocity;
24
        posDelta[index] = stepSize*velocity;
25
26
27
28
29
30
        randomIndex += get_global_size(0);
        index += get_global_size(0);
    }
}

/**
31
 * Perform the second step of Langevin integration.
32
33
 */

34
__kernel void integrateLangevinPart2(__global float4* restrict posq, __global const float4* restrict posDelta, __global float4* restrict velm, __global const float2* restrict dt) {
35
#ifdef SUPPORTS_DOUBLE_PRECISION
36
37
    double invStepSize = 1.0/dt[0].y;
#else
38
    float invStepSize = 1.0f/dt[0].y;
39
#endif
40
    int index = get_global_id(0);
41
    while (index < NUM_ATOMS) {
42
43
        float4 pos = posq[index];
        float4 delta = posDelta[index];
44
        float4 vel = velm[index];
45
        pos.xyz += delta.xyz;
46
#ifdef SUPPORTS_DOUBLE_PRECISION
47
48
        vel.xyz = convert_float4(invStepSize*convert_double4(delta)).xyz;
#else
49
        vel.xyz = invStepSize*delta.xyz;
50
#endif
51
        posq[index] = pos;
52
        velm[index] = vel;
53
54
55
        index += get_global_size(0);
    }
}
56
57
58
59
60

/**
 * Select the step size to use for the next step.
 */

61
62
__kernel void selectLangevinStepSize(float maxStepSize, float errorTol, float tau, float kT, __global float2* restrict dt,
        __global const float4* restrict velm, __global const float4* restrict force, __global float* restrict paramBuffer, __local float* restrict params, __local float* restrict error) {
63
64
65
66
    // Calculate the error.

    float err = 0.0f;
    unsigned int index = get_local_id(0);
67
    while (index < NUM_ATOMS) {
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
68
        float4 f = force[index];
69
        float invMass = velm[index].w;
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
70
        err += (f.x*f.x + f.y*f.y + f.z*f.z)*invMass;
71
72
73
74
75
76
77
        index += get_global_size(0);
    }
    error[get_local_id(0)] = err;
    barrier(CLK_LOCAL_MEM_FENCE);

    // Sum the errors from all threads.

78
    for (unsigned int offset = 1; offset < get_local_size(0); offset *= 2) {
79
80
81
82
83
84
85
        if (get_local_id(0)+offset < get_local_size(0) && (get_local_id(0)&(2*offset-1)) == 0)
            error[get_local_id(0)] += error[get_local_id(0)+offset];
        barrier(CLK_LOCAL_MEM_FENCE);
    }
    if (get_global_id(0) == 0) {
        // Select the new step size.

86
        float totalError = sqrt(error[0]/(NUM_ATOMS*3));
87
88
89
90
91
92
93
94
95
96
97
98
        float newStepSize = sqrt(errorTol/totalError);
        float oldStepSize = dt[0].y;
        if (oldStepSize > 0.0f)
            newStepSize = min(newStepSize, oldStepSize*2.0f); // For safety, limit how quickly dt can increase.
        if (newStepSize > oldStepSize && newStepSize < 1.1f*oldStepSize)
            newStepSize = oldStepSize; // Keeping dt constant between steps improves the behavior of the integrator.
        if (newStepSize > maxStepSize)
            newStepSize = maxStepSize;
        dt[0].y = newStepSize;

        // Recalculate the integration parameters.

99
100
101
102
103
104
        float vscale = exp(-newStepSize/tau);
        float fscale = (1-vscale)*tau;
        float noisescale = sqrt(2*kT/tau)*sqrt(0.5f*(1-vscale*vscale)*tau);
        params[VelScale] = vscale;
        params[ForceScale] = fscale;
        params[NoiseScale] = noisescale;
105
106
107
108
109
    }
    barrier(CLK_LOCAL_MEM_FENCE);
    if (get_local_id(0) < MaxParams)
        paramBuffer[get_local_id(0)] = params[get_local_id(0)];
}