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

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

7
8
KERNEL void integrateLangevinPart1(int numAtoms, int paddedNumAtoms, GLOBAL mixed4* RESTRICT velm, GLOBAL const mm_long* RESTRICT force, GLOBAL mixed4* RESTRICT posDelta,
        GLOBAL const mixed* RESTRICT paramBuffer, GLOBAL const mixed2* RESTRICT dt, GLOBAL const float4* RESTRICT random, unsigned int randomIndex) {
9
    mixed vscale = paramBuffer[VelScale];
10
    mixed fscale = paramBuffer[ForceScale]/(mixed) 0x100000000;
11
12
    mixed noisescale = paramBuffer[NoiseScale];
    mixed stepSize = dt[0].y;
13
    int index = GLOBAL_ID;
14
    randomIndex += index;
15
    while (index < numAtoms) {
16
        mixed4 velocity = velm[index];
17
        if (velocity.w != 0) {
18
            mixed sqrtInvMass = SQRT(velocity.w);
19
            velocity.x = vscale*velocity.x + fscale*velocity.w*force[index] + noisescale*sqrtInvMass*random[randomIndex].x;
20
21
            velocity.y = vscale*velocity.y + fscale*velocity.w*force[index+paddedNumAtoms] + noisescale*sqrtInvMass*random[randomIndex].y;
            velocity.z = vscale*velocity.z + fscale*velocity.w*force[index+paddedNumAtoms*2] + noisescale*sqrtInvMass*random[randomIndex].z;
22
            velm[index] = velocity;
23
            posDelta[index] = make_mixed4(stepSize*velocity.x, stepSize*velocity.y, stepSize*velocity.z, 0);
24
        }
25
26
        randomIndex += GLOBAL_SIZE;
        index += GLOBAL_SIZE;
27
28
29
30
31
32
33
    }
}

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

34
35
36
37
38
39
KERNEL void integrateLangevinPart2(int numAtoms, GLOBAL real4* RESTRICT posq, GLOBAL const mixed4* RESTRICT posDelta, GLOBAL mixed4* RESTRICT velm, GLOBAL const mixed2* RESTRICT dt
#ifdef USE_MIXED_PRECISION
        , GLOBAL real4* RESTRICT posqCorrection
#endif
        ) {
#ifdef SUPPORTS_DOUBLE_PRECISION
40
    double invStepSize = 1.0/dt[0].y;
41
42
43
44
#else
    float invStepSize = 1.0f/dt[0].y;
    float correction = (1.0f-invStepSize*dt[0].y)/dt[0].y;
#endif
45
    int index = GLOBAL_ID;
46
    while (index < numAtoms) {
47
        mixed4 vel = velm[index];
48
        if (vel.w != 0) {
49
50
51
52
53
#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
54
            real4 pos = posq[index];
55
56
#endif
            mixed4 delta = posDelta[index];
57
58
59
            pos.x += delta.x;
            pos.y += delta.y;
            pos.z += delta.z;
60
#ifdef SUPPORTS_DOUBLE_PRECISION
61
62
63
            vel.x = (mixed) (invStepSize*delta.x);
            vel.y = (mixed) (invStepSize*delta.y);
            vel.z = (mixed) (invStepSize*delta.z);
64
65
#else
            vel.x = invStepSize*delta.x + correction*delta.x;
66
67
            vel.y = invStepSize*delta.y + correction*delta.y;
            vel.z = invStepSize*delta.z + correction*delta.z;
68
#endif
69
70
71
72
#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
73
            posq[index] = pos;
74
#endif
75
76
            velm[index] = vel;
        }
77
        index += GLOBAL_SIZE;
78
79
80
81
82
83
84
    }
}

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

85
86
KERNEL void selectLangevinStepSize(int numAtoms, int paddedNumAtoms, mixed maxStepSize, mixed errorTol, mixed friction, mixed kT, GLOBAL mixed2* RESTRICT dt,
        GLOBAL const mixed4* RESTRICT velm, GLOBAL const mm_long* RESTRICT force, GLOBAL mixed* RESTRICT paramBuffer) {
87
88
    // Calculate the error.

89
90
    LOCAL mixed error[256];
    LOCAL mixed params[MaxParams];
91
    mixed err = 0;
92
    const mixed scale = RECIP((mixed) 0x100000000);
93
    for (int index = LOCAL_ID; index < numAtoms; index += LOCAL_SIZE) {
94
        mixed3 f = make_mixed3(scale*force[index], scale*force[index+paddedNumAtoms], scale*force[index+paddedNumAtoms*2]);
95
        mixed invMass = velm[index].w;
96
        err += (f.x*f.x + f.y*f.y + f.z*f.z)*invMass*invMass;
97
    }
98
99
    error[LOCAL_ID] = err;
    SYNC_THREADS;
100
101
102

    // Sum the errors from all threads.

103
104
105
106
    for (unsigned int offset = 1; offset < LOCAL_SIZE; offset *= 2) {
        if (LOCAL_ID+offset < LOCAL_SIZE && (LOCAL_ID&(2*offset-1)) == 0)
            error[LOCAL_ID] += error[LOCAL_ID+offset];
        SYNC_THREADS;
107
    }
108
    if (GLOBAL_ID == 0) {
109
110
        // Select the new step size.

111
        mixed totalError = SQRT(error[0]/(numAtoms*3));
112
        mixed newStepSize = SQRT(errorTol/totalError);
113
        mixed oldStepSize = dt[0].y;
114
115
116
117
118
119
120
121
122
123
        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.

Peter Eastman's avatar
Peter Eastman committed
124
        mixed vscale = exp(-newStepSize*friction);
125
        mixed fscale = (friction == 0 ? newStepSize : (1-vscale)/friction);
Peter Eastman's avatar
Peter Eastman committed
126
        mixed noisescale = sqrt(kT*(1-vscale*vscale));
127
128
129
130
        params[VelScale] = vscale;
        params[ForceScale] = fscale;
        params[NoiseScale] = noisescale;
    }
131
132
133
    SYNC_THREADS;
    if (LOCAL_ID < MaxParams)
        paramBuffer[LOCAL_ID] = params[LOCAL_ID];
134
}