noseHooverChain.cu 4.94 KB
Newer Older
1
2
3
4
5
6

#include <initializer_list>

/**
 * Propagate the Nose-Hoover chain with one yoshida-suzuki term
 */
Andy Simmonett's avatar
Andy Simmonett committed
7
8
extern "C" __global__ void propagateNoseHooverChain(mixed2* __restrict__ chainData, const mixed * __restrict__ energySum, mixed* __restrict__ scaleFactor,
                                                    mixed* __restrict__ chainMasses, mixed* __restrict__ chainForces, 
9
                                                    int chainLength, int numMTS, int numDOFs, float timeStep,
Andy Simmonett's avatar
Andy Simmonett committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
                                                    mixed kT, float frequency){
    mixed &scale = scaleFactor[0];
    scale = (mixed) 1;
    const mixed & kineticEnergy = energySum[0];
    if(kineticEnergy < 1e-8) return;
    for (int bead = 0; bead < chainLength; ++bead) chainMasses[bead] = kT / (frequency * frequency);
    chainMasses[0] *= numDOFs;
    mixed KE2 = 2.0f * kineticEnergy;
    mixed timeOverMTS = timeStep / numMTS;
    chainForces[0] = (KE2 - numDOFs * kT) / chainMasses[0];
    for (int bead = 0; bead < chainLength - 1; ++bead) {
        chainForces[bead + 1] = (chainMasses[bead] * chainData[bead].y * chainData[bead].y - kT) / chainMasses[bead + 1];
    }
    for (int mts = 0; mts < numMTS; ++mts) {
        BEGIN_YS_LOOP
            mixed wdt = ys * timeOverMTS;
            chainData[chainLength-1].y += 0.25f * wdt * chainForces[chainLength-1];
            for (int bead = chainLength - 2; bead >= 0; --bead) {
28
                mixed aa = MIXEDEXP(-0.125f * wdt * chainData[bead + 1].y);
Andy Simmonett's avatar
Andy Simmonett committed
29
30
31
                chainData[bead].y = aa * (chainData[bead].y * aa + 0.25f * wdt * chainForces[bead]);
            }
            // update particle velocities
32
            mixed aa = MIXEDEXP(-0.5f * wdt * chainData[0].y);
Andy Simmonett's avatar
Andy Simmonett committed
33
34
35
36
37
38
39
40
41
            scale *= aa;
            // update the thermostat positions
            for (int bead = 0; bead < chainLength; ++bead) {
                chainData[bead].x += 0.5f * chainData[bead].y * wdt;
            }
            // update the forces
            chainForces[0] = (scale * scale * KE2 - numDOFs * kT) / chainMasses[0];
            // update thermostat velocities
            for (int bead = 0; bead < chainLength - 1; ++bead) {
42
                mixed aa = MIXEDEXP(-0.125f * wdt * chainData[bead + 1].y);
Andy Simmonett's avatar
Andy Simmonett committed
43
44
45
46
47
48
                chainData[bead].y = aa * (aa * chainData[bead].y + 0.25f * wdt * chainForces[bead]);
                chainForces[bead + 1] = (chainMasses[bead] * chainData[bead].y * chainData[bead].y - kT) / chainMasses[bead + 1];
            }
            chainData[chainLength-1].y += 0.25f * wdt * chainForces[chainLength-1];
        END_YS_LOOP
    } // MTS loop
49
50
}

Andy Simmonett's avatar
Andy Simmonett committed
51

52
53
54
/**
 * Compute total (potential + kinetic) energy of the Nose-Hoover beads
 */
Andy Simmonett's avatar
Andy Simmonett committed
55
56
extern "C" __global__ void computeHeatBathEnergy(mixed* __restrict__ heatBathEnergy, int chainLength, int numDOFs, 
                                                 mixed kT, float frequency, const mixed2* __restrict__ chainData){
57

Andy Simmonett's avatar
Andy Simmonett committed
58
59
    mixed &energy = heatBathEnergy[0];
    energy = (mixed) 0;
60
61

    for(int i = 0; i < chainLength; ++i) {
Andy Simmonett's avatar
Andy Simmonett committed
62
63
64
        mixed prefac = i ? 1 : numDOFs;
        mixed mass = prefac * kT / (frequency * frequency);
        mixed velocity = chainData[i].y; 
65
66
67
        // The kinetic energy of this bead
        energy += 0.5f * mass * velocity * velocity;
        // The potential energy of this bead
Andy Simmonett's avatar
Andy Simmonett committed
68
        mixed position = chainData[i].x;
69
70
71
        energy += prefac * kT * position;
    }
}
Andy Simmonett's avatar
Andy Simmonett committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

extern "C" __global__ void computeMaskedKineticEnergy(mixed * __restrict__ energyBuffer, int paddedNumAtoms,
                                                      const mixed4* __restrict__ velm, const int *__restrict__ mask){
    mixed energy = 0;
    //energy = 1; return;
    for (int index = blockIdx.x*blockDim.x+threadIdx.x; index < paddedNumAtoms; index += blockDim.x*gridDim.x) {
        mixed4 v = velm[index];
        mixed mass = v.w == 0 ? 0 : 1 / v.w;
        if (mask[index] >= 0){
            const mixed4& vparent = velm[mask[index]];
            mixed massp = vparent.w == 0 ? 0 : 1/vparent.w;
            mass = (massp + mass) == 0 ? 0 : (massp * mass) / ( massp + mass );
            v.x -= vparent.x;
            v.y -= vparent.y;
            v.z -= vparent.z;
        }
        energy += 0.5f * mass * (v.x*v.x + v.y*v.y + v.z*v.z);
    }
    energyBuffer[blockIdx.x*blockDim.x+threadIdx.x] = energy;
}

extern "C" __global__ void scaleVelocities(mixed * __restrict__ scaleFactor, int paddedNumAtoms,
                                           mixed4* __restrict__ velm, const int *__restrict__ mask){
    const mixed &scale = scaleFactor[0];
    for (int index = blockIdx.x*blockDim.x+threadIdx.x; index < paddedNumAtoms; index += blockDim.x*gridDim.x) {
        mixed4 &v = velm[index];
        mixed4 vparent = mask[index] >= 0 ? velm[mask[index]] : make_mixed4(0.0f, 0.0f, 0.0f, 0.0f);
        mixed maskedScale = mask[index] == index ? 1 : scale;
        v.x = vparent.x + maskedScale * (v.x - vparent.x);
        v.y = vparent.y + maskedScale * (v.y - vparent.y);
        v.z = vparent.z + maskedScale * (v.z - vparent.z);
    }
}