verlet.cu 4.72 KB
Newer Older
1
2
3
4
/**
 * Perform the first step of Verlet integration.
 */

5
6
7
8
9
extern "C" __global__ void integrateVerletPart1(const mixed2* __restrict__ dt, const real4* __restrict__ posq,
        const real4* __restrict__ posqCorrection, mixed4* __restrict__ velm, const long long* __restrict__ force, mixed4* __restrict__ posDelta) {
    const mixed2 stepSize = dt[0];
    const mixed dtPos = stepSize.y;
    const mixed dtVel = 0.5f*(stepSize.x+stepSize.y);
10
    const mixed scale = dtVel/(mixed) 0x100000000;
11
    for (int index = blockIdx.x*blockDim.x+threadIdx.x; index < NUM_ATOMS; index += blockDim.x*gridDim.x) {
12
        mixed4 velocity = velm[index];
13
        if (velocity.w != 0.0) {
14
15
16
17
18
#ifdef USE_MIXED_PRECISION
            real4 pos1 = posq[index];
            real4 pos2 = posqCorrection[index];
            mixed4 pos = make_mixed4(pos1.x+(mixed)pos2.x, pos1.y+(mixed)pos2.y, pos1.z+(mixed)pos2.z, pos1.w);
#else
19
            real4 pos = posq[index];
20
#endif
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
            velocity.x += scale*force[index]*velocity.w;
            velocity.y += scale*force[index+PADDED_NUM_ATOMS]*velocity.w;
            velocity.z += scale*force[index+PADDED_NUM_ATOMS*2]*velocity.w;
            pos.x = velocity.x*dtPos;
            pos.y = velocity.y*dtPos;
            pos.z = velocity.z*dtPos;
            posDelta[index] = pos;
            velm[index] = velocity;
        }
    }
}

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

37
38
39
extern "C" __global__ void integrateVerletPart2(mixed2* __restrict__ dt, real4* __restrict__ posq,
        real4* __restrict__ posqCorrection, mixed4* __restrict__ velm, const mixed4* __restrict__ posDelta) {
    mixed2 stepSize = dt[0];
40
#if __CUDA_ARCH__ >= 130
41
    double oneOverDt = 1.0/stepSize.y;
42
43
44
45
#else
    float oneOverDt = 1.0f/stepSize.y;
    float correction = (1.0f-oneOverDt*stepSize.y)/stepSize.y;
#endif
46
47
48
49
    int index = blockIdx.x*blockDim.x+threadIdx.x;
    if (index == 0)
        dt[0].x = stepSize.y;
    for (; index < NUM_ATOMS; index += blockDim.x*gridDim.x) {
50
        mixed4 velocity = velm[index];
51
        if (velocity.w != 0.0) {
52
53
54
55
56
#ifdef USE_MIXED_PRECISION
            real4 pos1 = posq[index];
            real4 pos2 = posqCorrection[index];
            mixed4 pos = make_mixed4(pos1.x+(mixed)pos2.x, pos1.y+(mixed)pos2.y, pos1.z+(mixed)pos2.z, pos1.w);
#else
57
            real4 pos = posq[index];
58
59
#endif
            mixed4 delta = posDelta[index];
60
61
62
            pos.x += delta.x;
            pos.y += delta.y;
            pos.z += delta.z;
63
#if __CUDA_ARCH__ >= 130
64
            velocity = make_mixed4((mixed) (delta.x*oneOverDt), (mixed) (delta.y*oneOverDt), (mixed) (delta.z*oneOverDt), velocity.w);
65
66
67
#else
            velocity = make_mixed4((mixed) (delta.x*oneOverDt+delta.x*correction), (mixed) (delta.y*oneOverDt+delta.y*correction), (mixed) (delta.z*oneOverDt+delta.z*correction), velocity.w);
#endif
68
69
70
71
#ifdef USE_MIXED_PRECISION
            posq[index] = make_real4((real) pos.x, (real) pos.y, (real) pos.z, (real) pos.w);
            posqCorrection[index] = make_real4(pos.x-(real) pos.x, pos.y-(real) pos.y, pos.z-(real) pos.z, 0);
#else
72
            posq[index] = pos;
73
#endif
74
75
76
77
78
79
80
81
            velm[index] = velocity;
        }
    }
}

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

83
extern "C" __global__ void selectVerletStepSize(mixed maxStepSize, mixed errorTol, mixed2* __restrict__ dt, const mixed4* __restrict__ velm, const long long* __restrict__ force) {
84
85
    // Calculate the error.

86
87
    extern __shared__ mixed error[];
    mixed err = 0.0f;
88
    const mixed scale = RECIP((mixed) 0x100000000);
89
    for (int index = threadIdx.x; index < NUM_ATOMS; index += blockDim.x*gridDim.x) {
90
91
        mixed3 f = make_mixed3(scale*force[index], scale*force[index+PADDED_NUM_ATOMS], scale*force[index+PADDED_NUM_ATOMS*2]);
        mixed invMass = velm[index].w;
92
93
94
95
96
97
98
99
100
101
102
103
104
        err += (f.x*f.x + f.y*f.y + f.z*f.z)*invMass;
    }
    error[threadIdx.x] = err;
    __syncthreads();

    // Sum the errors from all threads.

    for (unsigned int offset = 1; offset < blockDim.x; offset *= 2) {
        if (threadIdx.x+offset < blockDim.x && (threadIdx.x&(2*offset-1)) == 0)
            error[threadIdx.x] += error[threadIdx.x+offset];
        __syncthreads();
    }
    if (threadIdx.x == 0) {
105
106
        mixed totalError = SQRT(error[0]/(NUM_ATOMS*3));
        mixed newStepSize = SQRT(errorTol/totalError);
107
        mixed oldStepSize = dt[0].y;
108
109
110
111
112
113
114
115
116
        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;
    }
}