coulombLennardJones.cl 4.02 KB
Newer Older
1
2
3
4
5
6
{
#ifdef USE_DOUBLE_PRECISION
    unsigned long includeInteraction;
#else
    unsigned int includeInteraction;
#endif
7
#if USE_EWALD
8
9
    bool needCorrection = hasExclusions && isExcluded && atom1 != atom2 && atom1 < NUM_ATOMS && atom2 < NUM_ATOMS;
    includeInteraction = ((!isExcluded && r2 < CUTOFF_SQUARED) || needCorrection);
Peter Eastman's avatar
Peter Eastman committed
10
11
12
    const real alphaR = EWALD_ALPHA*r;
    const real expAlphaRSqr = EXP(-alphaR*alphaR);
    const real prefactor = 138.935456f*posq1.w*posq2.w*invR;
13

14
#ifdef USE_DOUBLE_PRECISION
Peter Eastman's avatar
Peter Eastman committed
15
    const real erfcAlphaR = erfc(alphaR);
16
#else
Peter Eastman's avatar
Peter Eastman committed
17
18
    // This approximation for erfc is from Abramowitz and Stegun (1964) p. 299.  They cite the following as
    // the original source: C. Hastings, Jr., Approximations for Digital Computers (1955).  It has a maximum
19
    // error of 1.5e-7.
20

21
22
    const real t = RECIP(1.0f+0.3275911f*alphaR);
    const real erfcAlphaR = (0.254829592f+(-0.284496736f+(1.421413741f+(-1.453152027f+1.061405429f*t)*t)*t)*t)*t*expAlphaRSqr;
23
#endif
Peter Eastman's avatar
Peter Eastman committed
24
25
26
    real tempForce = 0;
    if (needCorrection) {
        // Subtract off the part of this interaction that was included in the reciprocal space contribution.
27

28
        if (1-erfcAlphaR > 1e-6) {
29
30
31
            real erfAlphaR = erf(alphaR); // Our erfc approximation is not accurate enough when r is very small, which happens with Drude particles.
            tempForce = -prefactor*(erfAlphaR-alphaR*expAlphaRSqr*TWO_OVER_SQRT_PI);
            tempEnergy += -prefactor*erfAlphaR;
32
        }
Peter Eastman's avatar
Peter Eastman committed
33
34
    }
    else {
35
#if HAS_LENNARD_JONES
Peter Eastman's avatar
Peter Eastman committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        real sig = sigmaEpsilon1.x + sigmaEpsilon2.x;
        real sig2 = invR*sig;
        sig2 *= sig2;
        real sig6 = sig2*sig2*sig2;
        real epssig6 = sig6*(sigmaEpsilon1.y*sigmaEpsilon2.y);
        tempForce = epssig6*(12.0f*sig6 - 6.0f);
        real ljEnergy = epssig6*(sig6 - 1.0f);
        #if USE_LJ_SWITCH
        if (r > LJ_SWITCH_CUTOFF) {
            real x = r-LJ_SWITCH_CUTOFF;
            real switchValue = 1+x*x*x*(LJ_SWITCH_C3+x*(LJ_SWITCH_C4+x*LJ_SWITCH_C5));
            real switchDeriv = x*x*(3*LJ_SWITCH_C3+x*(4*LJ_SWITCH_C4+x*5*LJ_SWITCH_C5));
            tempForce = tempForce*switchValue - ljEnergy*switchDeriv*r;
            ljEnergy *= switchValue;
        }
        #endif
        tempForce += prefactor*(erfcAlphaR+alphaR*expAlphaRSqr*TWO_OVER_SQRT_PI);
53
        tempEnergy += select((real) 0, ljEnergy + prefactor*erfcAlphaR, includeInteraction);
54
#else
Peter Eastman's avatar
Peter Eastman committed
55
        tempForce = prefactor*(erfcAlphaR+alphaR*expAlphaRSqr*TWO_OVER_SQRT_PI);
56
        tempEnergy += select((real) 0, prefactor*erfcAlphaR, includeInteraction);
57
#endif
58
    }
59
    dEdR += select((real) 0, tempForce*invR*invR, includeInteraction);
60
#else
61
#ifdef USE_CUTOFF
62
    includeInteraction = (!isExcluded && r2 < CUTOFF_SQUARED);
63
#else
64
    includeInteraction = (!isExcluded);
65
#endif
66
    real tempForce = 0;
67
  #if HAS_LENNARD_JONES
68
69
    real sig = sigmaEpsilon1.x + sigmaEpsilon2.x;
    real sig2 = invR*sig;
70
    sig2 *= sig2;
71
72
    real sig6 = sig2*sig2*sig2;
    real epssig6 = sig6*(sigmaEpsilon1.y*sigmaEpsilon2.y);
73
    tempForce = epssig6*(12.0f*sig6 - 6.0f);
74
    real ljEnergy = epssig6*(sig6-1);
75
76
77
78
79
80
81
82
83
    #if USE_LJ_SWITCH
    if (r > LJ_SWITCH_CUTOFF) {
        real x = r-LJ_SWITCH_CUTOFF;
        real switchValue = 1+x*x*x*(LJ_SWITCH_C3+x*(LJ_SWITCH_C4+x*LJ_SWITCH_C5));
        real switchDeriv = x*x*(3*LJ_SWITCH_C3+x*(4*LJ_SWITCH_C4+x*5*LJ_SWITCH_C5));
        tempForce = tempForce*switchValue - ljEnergy*switchDeriv*r;
        ljEnergy *= switchValue;
    }
    #endif
84
    ljEnergy = select((real) 0, ljEnergy, includeInteraction);
85
    tempEnergy += ljEnergy;
86
87
88
  #endif
#if HAS_COULOMB
  #ifdef USE_CUTOFF
89
    const real prefactor = 138.935456f*posq1.w*posq2.w;
90
    tempForce += prefactor*(invR - 2.0f*REACTION_FIELD_K*r2);
91
    tempEnergy += select((real) 0, prefactor*(invR + REACTION_FIELD_K*r2 - REACTION_FIELD_C), includeInteraction);
92
  #else
93
    const real prefactor = 138.935456f*posq1.w*posq2.w*invR;
94
    tempForce += prefactor;
95
    tempEnergy += select((real) 0, prefactor, includeInteraction);
96
  #endif
97
#endif
98
    dEdR += select((real) 0, tempForce*invR*invR, includeInteraction);
99
#endif
100
}