verlet.cl 2.88 KB
Newer Older
1
2
3
4
/**
 * Perform the first step of verlet integration.
 */

5
__kernel void integrateVerletPart1(int numAtoms, __global float2* dt, __global float4* posq, __global float4* velm, __global float4* force, __global float4* posDelta) {
6
7
8
    float2 stepSize = dt[0];
    float dtPos = stepSize.y;
    float dtVel = 0.5f*(stepSize.x+stepSize.y);
9
10
11
12
    int index = get_global_id(0);
    while (index < numAtoms) {
        float4 pos = posq[index];
        float4 velocity = velm[index];
13
14
        velocity.xyz += force[index].xyz*dtVel*velocity.w;
        pos.xyz = velocity.xyz*dtPos;
15
16
17
18
19
20
21
22
23
24
        posDelta[index] = pos;
        velm[index] = velocity;
        index += get_global_size(0);
    }
}

/**
 * Perform the second step of verlet integration.
 */

25
__kernel void integrateVerletPart2(int numAtoms, __global float2* dt, __global float4* posq, __global float4* velm, __global float4* posDelta) {
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
26
27
28
29
    float2 stepSize = dt[0];
    float oneOverDt = 1.0f/stepSize.y;
    if (get_global_id(0) == 0)
        dt[0].x = stepSize.y;
30
    barrier(CLK_LOCAL_MEM_FENCE);
31
32
33
34
35
36
37
38
39
40
41
42
    int index = get_global_id(0);
    while (index < numAtoms) {
        float4 pos = posq[index];
        float4 delta = posDelta[index];
        float4 velocity = velm[index];
        pos.xyz += delta.xyz;
        velocity.xyz = delta.xyz*oneOverDt;
        posq[index] = pos;
        velm[index] = velocity;
        index += get_global_size(0);
    }
}
43
44
45
46
47
48
49
50
51
52
53

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

__kernel void selectVerletStepSize(int numAtoms, float maxStepSize, float errorTol, __global float2* dt, __global float4* velm, __global float4* force, __local float* error) {
    // Calculate the error.

    float err = 0.0f;
    unsigned int index = get_local_id(0);
    while (index < numAtoms) {
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
54
        float4 f = force[index];
55
        float invMass = velm[index].w;
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
56
        err += (f.x*f.x + f.y*f.y + f.z*f.z)*invMass;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        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_local_id(0) == 0) {
        float totalError = sqrt(error[0]/(numAtoms*3));
        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;
    }
}