langevin.cl 3.4 KB
Newer Older
1
enum {VelScale, ForceScale, NoiseScale, MaxParams};
2
3
4
5
6

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

7
__kernel void integrateLangevinPart1(__global float4* velm, __global float4* force, __global float4* posDelta,
8
9
10
11
12
        __global float* paramBuffer, __global float2* dt, __global float4* random, unsigned int randomIndex) {
    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
13
    int index = get_global_id(0);
14
    randomIndex += index;
15
    while (index < NUM_ATOMS) {
16
17
        float4 velocity = velm[index];
        float sqrtInvMass = sqrt(velocity.w);
18
        velocity.xyz = vscale*velocity.xyz + fscale*velocity.w*force[index].xyz + noisescale*sqrtInvMass*random[randomIndex].xyz;
19
        velm[index] = velocity;
20
        posDelta[index] = stepSize*velocity;
21
22
23
24
25
26
        randomIndex += get_global_size(0);
        index += get_global_size(0);
    }
}

/**
27
 * Perform the second step of Langevin integration.
28
29
 */

30
__kernel void integrateLangevinPart2(__global float4* posq, __global float4* posDelta) {
31
    int index = get_global_id(0);
32
    while (index < NUM_ATOMS) {
33
34
35
36
37
38
39
        float4 pos = posq[index];
        float4 delta = posDelta[index];
        pos.xyz += delta.xyz;
        posq[index] = pos;
        index += get_global_size(0);
    }
}
40
41
42
43
44

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

45
__kernel void selectLangevinStepSize(float maxStepSize, float errorTol, float tau, float kT, __global float2* dt,
46
47
48
49
50
        __global float4* velm, __global float4* force, __global float* paramBuffer, __local float* params, __local float* error) {
    // Calculate the error.

    float err = 0.0f;
    unsigned int index = get_local_id(0);
51
    while (index < NUM_ATOMS) {
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
52
        float4 f = force[index];
53
        float invMass = velm[index].w;
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
54
        err += (f.x*f.x + f.y*f.y + f.z*f.z)*invMass;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        index += get_global_size(0);
    }
    error[get_local_id(0)] = err;
    barrier(CLK_LOCAL_MEM_FENCE);

    // Sum the errors from all threads.

    for (int offset = 1; offset < get_local_size(0); offset *= 2) {
        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.

70
        float totalError = sqrt(error[0]/(NUM_ATOMS*3));
71
72
73
74
75
76
77
78
79
80
81
82
        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.

83
84
85
86
87
88
        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;
89
90
91
92
93
    }
    barrier(CLK_LOCAL_MEM_FENCE);
    if (get_local_id(0) < MaxParams)
        paramBuffer[get_local_id(0)] = params[get_local_id(0)];
}