coulombLennardJones.cl 3.93 KB
Newer Older
1
#if USE_EWALD
2
bool needCorrection = hasExclusions && isExcluded && atom1 != atom2 && atom1 < NUM_ATOMS && atom2 < NUM_ATOMS;
Peter Eastman's avatar
Peter Eastman committed
3
4
5
6
if ((!isExcluded && r2 < CUTOFF_SQUARED) || needCorrection) {
    const real alphaR = EWALD_ALPHA*r;
    const real expAlphaRSqr = EXP(-alphaR*alphaR);
    const real prefactor = 138.935456f*posq1.w*posq2.w*invR;
7

8
#ifdef USE_DOUBLE_PRECISION
Peter Eastman's avatar
Peter Eastman committed
9
    const real erfcAlphaR = erfc(alphaR);
10
#else
Peter Eastman's avatar
Peter Eastman committed
11
12
13
    // 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
    // error of 3e-7.
14

Peter Eastman's avatar
Peter Eastman committed
15
16
17
18
19
    real t = 1.0f+(0.0705230784f+(0.0422820123f+(0.0092705272f+(0.0001520143f+(0.0002765672f+0.0000430638f*alphaR)*alphaR)*alphaR)*alphaR)*alphaR)*alphaR;
    t *= t;
    t *= t;
    t *= t;
    const real erfcAlphaR = RECIP(t*t);
20
#endif
Peter Eastman's avatar
Peter Eastman committed
21
22
23
    real tempForce = 0;
    if (needCorrection) {
        // Subtract off the part of this interaction that was included in the reciprocal space contribution.
24

25
        if (1-erfcAlphaR > 1e-6) {
26
27
28
            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;
29
        }
Peter Eastman's avatar
Peter Eastman committed
30
31
    }
    else {
32
#if HAS_LENNARD_JONES
Peter Eastman's avatar
Peter Eastman committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        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);
        tempEnergy += ljEnergy + prefactor*erfcAlphaR;
51
#else
Peter Eastman's avatar
Peter Eastman committed
52
53
        tempForce = prefactor*(erfcAlphaR+alphaR*expAlphaRSqr*TWO_OVER_SQRT_PI);
        tempEnergy += prefactor*erfcAlphaR;
54
#endif
55
    }
Peter Eastman's avatar
Peter Eastman committed
56
    dEdR += tempForce*invR*invR;
57
58
}
#else
59
{
60
61
62
63
64
#ifdef USE_DOUBLE_PRECISION
    unsigned long includeInteraction;
#else
    unsigned int includeInteraction;
#endif
65
#ifdef USE_CUTOFF
66
    includeInteraction = (!isExcluded && r2 < CUTOFF_SQUARED);
67
#else
68
    includeInteraction = (!isExcluded);
69
#endif
70
    real tempForce = 0;
71
  #if HAS_LENNARD_JONES
72
73
    real sig = sigmaEpsilon1.x + sigmaEpsilon2.x;
    real sig2 = invR*sig;
74
    sig2 *= sig2;
75
76
    real sig6 = sig2*sig2*sig2;
    real epssig6 = sig6*(sigmaEpsilon1.y*sigmaEpsilon2.y);
77
    tempForce = epssig6*(12.0f*sig6 - 6.0f);
78
    real ljEnergy = epssig6*(sig6-1);
79
80
81
82
83
84
85
86
87
    #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
88
    ljEnergy = select((real) 0, ljEnergy, includeInteraction);
89
    tempEnergy += ljEnergy;
90
91
92
  #endif
#if HAS_COULOMB
  #ifdef USE_CUTOFF
93
    const real prefactor = 138.935456f*posq1.w*posq2.w;
94
    tempForce += prefactor*(invR - 2.0f*REACTION_FIELD_K*r2);
95
    tempEnergy += select((real) 0, prefactor*(invR + REACTION_FIELD_K*r2 - REACTION_FIELD_C), includeInteraction);
96
  #else
97
    const real prefactor = 138.935456f*posq1.w*posq2.w*invR;
98
    tempForce += prefactor;
99
    tempEnergy += select((real) 0, prefactor, includeInteraction);
100
  #endif
101
#endif
102
    dEdR += select((real) 0, tempForce*invR*invR, includeInteraction);
103
}
104
#endif