ccma.cl 5.24 KB
Newer Older
1
2
3
/**
 * Compute the direction each constraint is pointing in.  This is called once at the beginning of constraint evaluation.
 */
4
__kernel void computeConstraintDirections(__global const int2* restrict constraintAtoms, __global float4* restrict constraintDistance, __global const float4* restrict atomPositions) {
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    for (int index = get_global_id(0); index < NUM_CONSTRAINTS; index += get_global_size(0)) {
        // Compute the direction for this constraint.

        int2 atoms = constraintAtoms[index];
        float4 dir = constraintDistance[index];
        float4 oldPos1 = atomPositions[atoms.x];
        float4 oldPos2 = atomPositions[atoms.y];
        dir.x = oldPos1.x-oldPos2.x;
        dir.y = oldPos1.y-oldPos2.y;
        dir.z = oldPos1.z-oldPos2.z;
        constraintDistance[index] = dir;
    }
}

/**
 * Compute the force applied by each constraint.
 */
22
23
__kernel void computeConstraintForce(__global const int2* restrict constraintAtoms, __global const float4* restrict constraintDistance, __global const float4* restrict atomPositions,
        __global const float* restrict reducedMass, __global float* restrict delta1, __global int* restrict converged, float tol, int iteration) {
24
    __local int groupConverged;
Peter Eastman's avatar
Peter Eastman committed
25
26
27
28
29
    if (converged[1-iteration%2]) {
        if (get_global_id(0) == 0)
            converged[iteration%2] = 1;
        return; // The constraint iteration has already converged.
    }
30
31
32
    if (get_local_id(0) == 0)
        groupConverged = 1;
    barrier(CLK_LOCAL_MEM_FENCE);
33
34
35
36
37
38
39
40
    float lowerTol = 1.0f-2.0f*tol+tol*tol;
    float upperTol = 1.0f+2.0f*tol+tol*tol;
    for (int index = get_global_id(0); index < NUM_CONSTRAINTS; index += get_global_size(0)) {
        // Compute the force due to this constraint.

        int2 atoms = constraintAtoms[index];
        float4 dir = constraintDistance[index];
        float4 rp_ij = atomPositions[atoms.x]-atomPositions[atoms.y];
41
#ifndef CONSTRAIN_VELOCITIES
42
        rp_ij.xyz += dir.xyz;
43
44
45
46
47
48
49
50
51
52
53
54
55
#endif
        float rrpr = rp_ij.x*dir.x + rp_ij.y*dir.y + rp_ij.z*dir.z;
        float d_ij2 = dir.x*dir.x + dir.y*dir.y + dir.z*dir.z;
#ifdef CONSTRAIN_VELOCITIES
        delta1[index] = -2.0f*reducedMass[index]*rrpr/d_ij2;

        // See whether it has converged.

        if (groupConverged && fabs(delta1[index]) > tol) {
            groupConverged = 0;
            converged[iteration%2] = 0;
        }
#else
56
57
58
59
60
61
62
        float rp2 = rp_ij.x*rp_ij.x + rp_ij.y*rp_ij.y + rp_ij.z*rp_ij.z;
        float dist2 = dir.w*dir.w;
        float diff = dist2 - rp2;
        delta1[index] = (rrpr > d_ij2*1e-6f ? reducedMass[index]*diff/rrpr : 0.0f);

        // See whether it has converged.

63
64
        if (groupConverged && (rp2 < lowerTol*dist2 || rp2 > upperTol*dist2)) {
            groupConverged = 0;
Peter Eastman's avatar
Peter Eastman committed
65
            converged[iteration%2] = 0;
66
        }
67
#endif
68
69
70
71
72
73
    }
}

/**
 * Multiply the vector of constraint forces by the constraint matrix.
 */
74
75
__kernel void multiplyByConstraintMatrix(__global const float* restrict delta1, __global float* restrict delta2, __global const int* restrict constraintMatrixColumn,
        __global const float* restrict constraintMatrixValue, __global const int* restrict converged, int iteration) {
Peter Eastman's avatar
Peter Eastman committed
76
    if (converged[iteration%2])
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        return; // The constraint iteration has already converged.

    // Multiply by the inverse constraint matrix.

    for (int index = get_global_id(0); index < NUM_CONSTRAINTS; index += get_global_size(0)) {
        float sum = 0.0f;
        for (int i = 0; ; i++) {
            int element = index+i*NUM_CONSTRAINTS;
            int column = constraintMatrixColumn[element];
            if (column >= NUM_CONSTRAINTS)
                break;
            sum += delta1[column]*constraintMatrixValue[element];
        }
        delta2[index] = sum;
    }
}

/**
 * Update the atom positions based on constraint forces.
 */
97
98
__kernel void updateAtomPositions(__global const int* restrict numAtomConstraints, __global const int* restrict atomConstraints, __global const float4* restrict constraintDistance,
        __global float4* restrict atomPositions, __global const float4* restrict velm, __global const float* restrict delta1, __global const float* restrict delta2, __global int* restrict converged, int iteration) {
Peter Eastman's avatar
Peter Eastman committed
99
100
101
    if (get_global_id(0) == 0)
        converged[1-iteration%2] = 1;
    if (converged[iteration%2])
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        return; // The constraint iteration has already converged.
    float damping = (iteration < 2 ? 0.5f : 1.0f);
    for (int index = get_global_id(0); index < NUM_ATOMS; index += get_global_size(0)) {
        // Compute the new position of this atom.

        float4 atomPos = atomPositions[index];
        float invMass = velm[index].w;
        int num = numAtomConstraints[index];
        for (int i = 0; i < num; i++) {
            int constraint = atomConstraints[index+i*NUM_ATOMS];
            bool forward = (constraint > 0);
            constraint = (forward ? constraint-1 : -constraint-1);
            float constraintForce = damping*invMass*delta2[constraint];
            constraintForce = (forward ? constraintForce : -constraintForce);
            float4 dir = constraintDistance[constraint];
            atomPos.x += constraintForce*dir.x;
            atomPos.y += constraintForce*dir.y;
            atomPos.z += constraintForce*dir.z;
        }
        atomPositions[index] = atomPos;
    }
}