HSEFunctional.C 33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// HSEFunctional.C
//
////////////////////////////////////////////////////////////////////////////////
//
// Implements the HSE hybrid functional
20 21 22 23 24 25 26
// Heyd et al.,      J. Chem. Phys. 118, 8207 (2003)
// Heyd, Scuseria    J. Chem. Phys. 120, 7274 (2004)
// Krukau et al.,    J. Chem. Phys. 125, 224106 (2006)
// The PBE exchange hole J is defined here
// Ernzerhof, Perdew J. Chem. Phys. 109, 3313 (1998)
// Parts of this code are taken from the implementation in FLAPW
// Schlipf et al.    Phys. Rev. B 84, 125142 (2011)
27 28
//
////////////////////////////////////////////////////////////////////////////////
29 30

#include "HSEFunctional.h"
31
#include "ExponentialIntegral.h"
32
#include <cassert>
33
#include <cmath>
34
#include <numeric>
35
#include <algorithm>
36 37 38

using namespace std;

39 40
// parameters of the exchange hole
const double A = 1.0161144, B = -0.37170836, C = -0.077215461, D = 0.57786348,
41
  E = -0.051955731;
42

43 44
// constructor
HSEFunctional::HSEFunctional(const vector<vector<double> > &rhoe) :
Francois Gygi committed
45
  x_coeff_(0.75), c_coeff_(1.0), omega(0.11)
46
{
47 48 49
  // nonmagnetic or magnetic
  _nspin = rhoe.size();
  // in magnetic calculations both density arrays must have the same size
50
  if ( _nspin > 1 ) assert(rhoe[0].size() == rhoe[1].size());
51 52 53

  // allocate arrays
  _np = rhoe[0].size();
54 55
  if ( _nspin == 1 )
  {
56 57 58 59 60 61 62 63 64 65 66 67 68 69
    // nonmagnetic arrays used
    _exc.resize(_np);
    _vxc1.resize(_np);
    _vxc2.resize(_np);
    _grad_rho[0].resize(_np);
    _grad_rho[1].resize(_np);
    _grad_rho[2].resize(_np);
    rho = &rhoe[0][0];
    grad_rho[0] = &_grad_rho[0][0];
    grad_rho[1] = &_grad_rho[1][0];
    grad_rho[2] = &_grad_rho[2][0];
    exc = &_exc[0];
    vxc1 = &_vxc1[0];
    vxc2 = &_vxc2[0];
70 71 72
  }
  else
  {
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 105 106 107 108
    // magnetic arrays used
    _exc_up.resize(_np);
    _exc_dn.resize(_np);
    _vxc1_up.resize(_np);
    _vxc1_dn.resize(_np);
    _vxc2_upup.resize(_np);
    _vxc2_updn.resize(_np);
    _vxc2_dnup.resize(_np);
    _vxc2_dndn.resize(_np);
    _grad_rho_up[0].resize(_np);
    _grad_rho_up[1].resize(_np);
    _grad_rho_up[2].resize(_np);
    _grad_rho_dn[0].resize(_np);
    _grad_rho_dn[1].resize(_np);
    _grad_rho_dn[2].resize(_np);

    rho_up = &rhoe[0][0];
    rho_dn = &rhoe[1][0];
    grad_rho_up[0] = &_grad_rho_up[0][0];
    grad_rho_up[1] = &_grad_rho_up[1][0];
    grad_rho_up[2] = &_grad_rho_up[2][0];
    grad_rho_dn[0] = &_grad_rho_dn[0][0];
    grad_rho_dn[1] = &_grad_rho_dn[1][0];
    grad_rho_dn[2] = &_grad_rho_dn[2][0];
    exc_up = &_exc_up[0];
    exc_dn = &_exc_dn[0];
    vxc1_up = &_vxc1_up[0];
    vxc1_dn = &_vxc1_dn[0];
    vxc2_upup = &_vxc2_upup[0];
    vxc2_updn = &_vxc2_updn[0];
    vxc2_dnup = &_vxc2_dnup[0];
    vxc2_dndn = &_vxc2_dndn[0];
  }

}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
// helper function to calculate the integral
//
// inf
//   /                   /        2                  \
//  |         / w y \   |  A   -Dy           A        |     /        2  2 \
//  | dy Erfc( ----- )  | --- e     - --------------  | Exp( - H(s) s  y   )
//  |         \ k   /   |  y            /    4   2\   |     \             /
// /             F       \            y( 1 + - Ay  ) /
//  0                                   \    9    /
//
// complementary error function is approximated by polynomial
//             8
//           -----             2
//            \        i  - b x
// erfc(x) =   )   a  x  e          for x < 14
//            /     i
//           -----
//           i = 1
// and by exp( -2 x^2 ) above x = 14
// then the integrals with the first part of the exchange hole
// become analytically solvable
void approximateIntegral(const double omega_kF, const double Hs2,
131 132 133
  const double D_term, const double dHs2_ds, double *appInt,
  double *dAppInt_ds, double *dAppInt_dkF)
{
134 135 136

  // constant parameterization of error function
  const double a[] = { 1.0, -1.128223946706117, 1.452736265762971,
137 138
    -1.243162299390327, 0.971824836115601, -0.568861079687373,
    0.246880514820192, -0.065032363850763, 0.008401793031216 };
139 140 141 142 143 144 145 146 147 148 149
  const double b = 1.455915450052607, cutoff = 14.0;

  // helper variables
  const double SQRT_PI = sqrt(M_PI);
  const double A_2 = 0.5 * A, r9_4A = 2.25 / A, sqrtA = sqrt(A);
  const double w2 = omega_kF * omega_kF;
  const double bw2 = b * w2;
  const double r2bw2 = 2.0 * bw2;
  const double bw2_Hs2 = bw2 + Hs2;
  const double bw2_D_term = bw2 + D_term;

150 151
  if ( bw2_Hs2 < cutoff )
  {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    // small x limit

    // ### begin calculation of integrals ###

    // inf
    //   /        /        2                  \      /         2              \
    //  |     n  |  A   -Dy           A        |    |    /    w         2\   2 |
    //  | dy y   | --- e     - --------------  | Exp| - (  b --2-- + H s  ) y  |
    //  |        |  y            /    4   2\   |    |    \    k          /     |
    // /          \            y( 1 + - Ay  ) /      \         F              /
    //  0                        \    9    /
    //

    // maximum n in above expression
    // note: to calculate higher derivatives you may need to increase this value
    const int no_integral = 11;

    // Calculate more helper variables
    const double sqrt_bw2_Hs2 = sqrt(bw2_Hs2);
    const double sqrt_bw2_D_term = sqrt(bw2_D_term);

    // arg = 9/4 * (b w^2/kF^2 + H s^2) / A
    const double arg = r9_4A * bw2_Hs2;
    const double sqrt_arg = sqrt(arg);

    // calculate e^(arg), E1(arg), and erfc(sqrt(arg))
    const double exp_arg = exp(arg);
    const double exp_erfc = exp_arg * erfc(sqrt_arg);

    // evaluate exponenential integral
183 184
    double term2 = ( arg < util::series_cutoff ) ? exp_arg * util::E1(arg)
      : util::gauss_laguerre(arg);
185 186 187 188 189 190 191

    // allocate array
    vector<double> integral(no_integral);

    // The n = 0 integral is
    // A/2 ( ln((b (w/kF)^2 + H s^2) / (b (w/kF)^2 + D + H s^2))
    //     + e^(arg) E1(arg) )
192
    integral[0] = A_2 * ( log(bw2_Hs2 / bw2_D_term) + term2 );
193 194 195 196 197 198 199 200 201 202 203

    // Calculate now all even n's by successive derivation
    // The log(...) term gives term proportional to 1/(b (w/kF)^2 + D + H s^2)^i
    // The e^(arg) E1(arg) term reproduces itself with a prefactor and
    // generates an additional 1/arg term which produces higher 1/arg^i terms
    // when further deriviated
    double term1 = A_2 / bw2_D_term;
    double factor2 = -1.125;
    double arg_n = -1.0 / arg;
    integral[2] = term1 + factor2 * term2;

204 205
    for ( int i = 1; i < no_integral / 2; i++ )
    {
206
      term1 = i * term1 / bw2_D_term;
207 208 209
      factor2 = -factor2 * r9_4A;
      term2 = term2 + arg_n;

210
      integral[2 * ( i + 1 )] = term1 + factor2 * term2;
211

212
      arg_n = -arg_n * i / arg;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    }

    // The n = 1 integral is
    // A/2 ( sqrt(pi) / sqrt( b (w/kF)^2 + D + H s2 )
    // - 3/4 sqrt(A) pi e^(arg) erfc(sqrt(arg))
    term1 = A_2 * SQRT_PI / sqrt_bw2_D_term;
    term2 = M_PI * exp_erfc;
    factor2 = -0.75 * sqrtA;

    integral[1] = term1 + factor2 * term2;

    // Calculate now all uneven n's by successive derivation
    // The 1 / sqrt(...) term gives higher orders of 1 / (...)^((2i+1)/2)
    // The e^(arg) erfc(sqrt(arg)) term reproduces itself with a prefactor
    // and generates an additional 1/sqrt(arg) term which produces higher
    // 1/(arg)^((2i+1)/2) terms when further deriviated
    double sum_term = -1.125 * SQRT_PI / sqrt_bw2_Hs2;
    double add_term = sum_term;
    double half_i2_1 = -0.5;

233 234
    for ( int i = 3; i < no_integral; i += 2 )
    {
235 236 237 238 239 240 241 242 243 244 245
      factor2 = -factor2 * r9_4A;
      term1 = -term1 * half_i2_1 / bw2_D_term;
      integral[i] = term1 + term2 * factor2 + sum_term;

      add_term = -add_term * half_i2_1 / bw2_Hs2;
      sum_term = -sum_term * r9_4A + add_term;
      half_i2_1 = half_i2_1 - 1.0;
    }

    // ### end calculation of integrals ###

246
    const int no_coeff = sizeof( a ) / sizeof(double);
247 248 249 250 251 252 253 254 255 256

    // allocate vector
    vector<double> a_wi, ai_wi;
    // will contain a[i] * (w/kF)^i
    a_wi.reserve(no_coeff);
    // will contain a[i] * i * (w/kF)^i
    ai_wi.reserve(no_coeff);

    // initialize array
    double wi = 1.0;
257 258
    for ( int i = 0; i < no_coeff; i++ )
    {
259 260 261 262 263 264
      a_wi.push_back(a[i] * wi);
      ai_wi.push_back(a_wi[i] * i);
      wi *= omega_kF;
    }

    //  combine the solutions of the integrals with the appropriate prefactors
265
    *appInt = inner_product(a_wi.begin(),a_wi.end(),integral.begin(),0.0);
266
    // for derivative shift integral index by 2
267 268
    const double dotpr = inner_product(a_wi.begin(),a_wi.end(),integral.begin()
      + 2,0.0);
269
    *dAppInt_ds = -dotpr * dHs2_ds;
270 271
    *dAppInt_dkF = -inner_product(ai_wi.begin(),ai_wi.end(),integral.begin(),
      -r2bw2 * dotpr);
272

273 274 275
  }
  else
  {
276 277 278

    // large x limit

279 280 281 282 283 284 285 286 287 288 289 290 291 292
    // inf
    //   /    /        2                  \      /         2              \
    //  |    |  A   -Dy           A        |    |    /    w         2\   2 |
    //  | dy | --- e     - --------------  | Exp| - (  2 --2-- + H s  ) y  |
    //  |    |  y            /    4   2\   |    |    \    k          /     |
    // /      \            y( 1 + - Ay  ) /      \         F              /
    //  0                    \    9    /

    const double r2w2 = 2.0 * w2;
    const double r4w2 = 4.0 * w2;
    const double r2w2_Hs2 = r2w2 + Hs2;
    const double r2w2_D_term = r2w2 + D_term;
    const double arg = r9_4A * r2w2_Hs2;
    const double exp_e1 = util::gauss_laguerre(arg);
293
    *appInt = A_2 * ( log(r2w2_Hs2 / r2w2_D_term) + exp_e1 );
294 295 296 297
    const double dAppInt_dh = -A_2 / r2w2_D_term + 1.125 * exp_e1;
    *dAppInt_ds = dAppInt_dh * dHs2_ds;
    *dAppInt_dkF = -dAppInt_dh * r4w2;

298 299 300 301
  }

}

302 303 304 305
// helper function to calculate the HSE enhancement factor
// and its derivative w.r.t. s and k_F
//                inf
//                  /
306 307 308 309
//  HSE         8  |                    w y
// F (s,k ) = - -  | dy y J(s,y) erfc( ----- )
//  x    F      9  |                    k
//                /                      F
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
//                 0
// where s:   reduced density gradient
//       k_F: Fermi vector
//       w:   screening of the functional
// the PBE exchange hole is defined by the following expression
//           /
//          |    A          1          /  A                  2         2
// J(s,y) = | - --2- ------------2- + |  --2- + B + C [ 1 + s  F(s) ] y
//          |    y    1 + (4/9)Ay      \  y
//           \
//                                           2 \      2       2
//                       2         4 \   -D y   |  - s  H(s) y
//            + E [ 1 + s  G(s) ] y   | e       | e
//                                   /          |
//                                             /
// see references for a definition of the functions F(s), G(s), and H(s)
326
void HSE_enhance(const double s_inp, const double kF, const double w,
327 328
  double *fx, double *dfx_ds, double* dfx_dkf)
{
329
  // Correction of the reduced gradient to ensure Lieb-Oxford bound
Francois Gygi committed
330 331
  // If a large value of s would violate the Lieb-Oxford bound, the
  // value of s is reduced, so that this condition is fullfilled
332 333
  const double s_thresh = 8.3, s_max = 8.572844, s_chg = 18.796223;
  const bool correction = s_inp > s_thresh;
334
  const double s = ( correction ) ? s_max - s_chg / ( s_inp * s_inp ) : s_inp;
335

336
  // sanity check
337
  assert(s > 0);
338

339 340 341
  // prefactor for exchange hole
  const double r8_9 = 8.0 / 9.0;

342 343
  // derived quantities
  const double SQRT_PI = sqrt(M_PI);
344 345
  const double sqrtA = sqrt(A);
  const double r9_4A = 2.25 / A;
346 347 348 349 350 351 352

  // evaluate
  //                 2       4
  //             a1 s  + a2 s
  // H(s) = ---------4------5------6-
  //         1 + a3 s + a4 s + a5 s
  // and
Francois Gygi committed
353 354 355 356
  //   2                 3         5            3        4        5          2
  // d(s H(s))   ( 4 a1 s  + 6 a2 s ) - ( 4 a3 s + 5 a4 s + 6 a5 s ) * H(s) s
  // --------- = ---------------------4------5------6--------------------------
  //    d s                    1 + a3 s + a4 s + a5 s
357 358

  const double a1 = 0.00979681, a2 = 0.0410834, a3 = 0.187440, a4 = 0.00120824,
359
    a5 = 0.0347188;
360 361 362 363 364 365 366 367 368 369

  // helper variables
  const double s2 = s * s;
  const double s3 = s2 * s;
  const double s4 = s2 * s2;
  const double s5 = s3 * s2;
  const double s6 = s3 * s3;

  // calculate numerator and reciprocal of denominator
  const double numerator = a1 * s2 + a2 * s4;
370
  const double r_denom = 1.0 / ( 1.0 + a3 * s4 + a4 * s5 + a5 * s6 );
371 372 373 374 375 376 377
  // helper for derivatives
  const double first = 4.0 * a1 * s3 + 6.0 * a2 * s5;
  const double second = 4.0 * a3 * s3 + 5.0 * a4 * s4 + 6.0 * a5 * s5;

  // put everything together
  const double H = numerator * r_denom;
  const double Hs2 = H * s2;
378
  const double dHs2_ds = ( first - second * Hs2 ) * r_denom;
379 380

  // evaluate
381
  // F(s) = Int + Sl * H(s)
382 383 384
  // d( s^2 F(s) )                     d( s^2 H(s) )
  // ------------- = 2 * Int *s + Sl * -------------
  //     d s                                d s
385 386 387 388
  // Int - intercept
  // Sl  - slope
  const double slope = 6.4753871;
  const double intercept = 0.4796583;
389 390 391

  const double F = slope * H + intercept;
  const double Fs2 = F * s2;
392
  const double dFs2_ds = slope * dHs2_ds + 2 * intercept * s;
393

394 395 396 397 398
  // evaluate alpha and beta
  // alpha = part1 - part2
  //               __  2
  //           15 VPi s
  // beta = -----------------
399
  //                    2  7/2
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  //         16 (D + H s  )
  // with
  //
  //                          2      2           2 2          2 3
  //          __ 15E + 6C(1+Fs )(D+Hs ) + 4B(D+Hs )  + 8A(D+Hs )
  // part1 = VPi ------------------------------------------------
  //                                    2  7/2
  //                        16 ( D + H s  )
  // and
  //                                            ________
  //                       /     2 \       /   /     2   \
  //         3 Pi  ___    | 9 H s   |     |   / 9 H s     |
  // part2 = ---- V A  Exp| ------- | Erfc|  /  -------   |
  //          4           |   4 A   |     | V     4 A     |
  //                       \       /       \             /
  // and the derivatives with respect to s

  // calculate the helper variables
  const double AHs2_1_2 = sqrtA * sqrt(Hs2);
  const double r1_Fs2 = 1.0 + Fs2;
  const double D_Hs2 = D + Hs2;
  const double D_Hs2Sqr = D_Hs2 * D_Hs2;
  const double D_Hs2Cub = D_Hs2 * D_Hs2Sqr;
  const double D_Hs2_5_2 = D_Hs2Sqr * sqrt(D_Hs2);
  const double D_Hs2_7_2 = D_Hs2_5_2 * D_Hs2;
  const double D_Hs2_9_2 = D_Hs2_5_2 * D_Hs2Sqr;

  // part 1 and derivatives w.r.t. Hs^2 and Fs^2
428 429 430 431
  const double part1 = SQRT_PI * ( 15.0 * E + 6.0 * C * r1_Fs2 * D_Hs2 + 4.0
    * B * D_Hs2Sqr + 8.0 * A * D_Hs2Cub ) / ( 16.0 * D_Hs2_7_2 );
  const double dpart1_dh = -SQRT_PI * ( 105.0 * E + 30.0 * C * r1_Fs2 * D_Hs2
    + 12.0 * B * D_Hs2Sqr + 8.0 * A * D_Hs2Cub ) / ( 32.0 * D_Hs2_9_2 );
432 433 434 435 436 437 438
  const double dpart1_df = SQRT_PI * 0.375 * C / D_Hs2_5_2;

  // part 2 and derivative w.r.t. Hs^2
  const double arg1 = r9_4A * Hs2;
  const double arg2 = sqrt(arg1);
  const double exp_erfc = exp(arg1) * erfc(arg2);
  const double part2 = 0.75 * M_PI * sqrtA * exp_erfc;
439 440
  const double dpart2_dh = 0.75 * M_PI * sqrtA * ( r9_4A * exp_erfc - 1.5
    / ( SQRT_PI * AHs2_1_2 ) );
441

442 443 444 445 446 447
  // combine parts and derivatives
  const double alpha = part1 - part2;
  const double dalpha_dh = dpart1_dh - dpart2_dh;
  const double dalpha_df = dpart1_df;

  // calculate beta / s^2, its derivative w.r.t. Hs^2 and E * beta
448 449
  const double Ebeta_s2 = E * 0.9375 * SQRT_PI / D_Hs2_7_2;
  const double dEbeta_dh = -3.5 * Ebeta_s2 / D_Hs2;
450 451 452 453

  // combine alpha and beta to function G
  //       3 Pi / 4 + alpha
  // G = - ----------------
454
  //            E beta
455
  const double r3Pi_4_alpha = 0.75 * M_PI + alpha;
456 457
  // Gs2 = G * s^2
  const double Gs2 = -r3Pi_4_alpha / Ebeta_s2;
458 459 460

  // calculate derivative w.r.t. s
  //
Francois Gygi committed
461 462 463 464 465 466 467 468 469 470 471 472
  //             /  /3 Pi    \  d(b/s^2)   /           d a    \  d(Hs^2)
  //            <  ( ---- + a ) -------   /  b/s^2  - -------  > -------
  // d (Gs^2)    \  \ 4      /  d(Hs^2)  /            d(Hs^2) /    d s
  // -------- = --------------------------------------------------------
  //    ds                             E b/s^2
  //
  //
  //        d a   d(Fs^2)
  //      ------- -------
  //      d(Fs^2)   ds
  //  - -----------------
  //        E b/s^2
473 474
  //
  // notice that alpha and beta are abbreviated by a and b in this equation
Francois Gygi committed
475 476
  const double dGs2_ds = ( ( r3Pi_4_alpha * dEbeta_dh / Ebeta_s2 - dalpha_dh ) *
    dHs2_ds - dalpha_df * dFs2_ds ) / Ebeta_s2;
477

478
  // helper variables for the integration of the exchange hole
479
  const double C_term = C * ( 1 + s2 * F );
480
  const double dCt_ds = C * dFs2_ds;
481
  const double E_term = E * ( 1 + Gs2 );
482 483 484 485 486 487 488 489 490
  const double dEt_ds = E * dGs2_ds;
  const double D_term = D + Hs2;
  const double r1_D_term = 1.0 / D_term;
  const double r1_kF = 1.0 / kF;
  const double w_kF = w * r1_kF;
  const double w_kF_Sqr = w_kF * w_kF;

  // approximate the integral using an expansion of the error function
  double appInt, dAppInt_ds, dAppInt_dkF;
491
  approximateIntegral(w_kF,Hs2,D_term,dHs2_ds,&appInt,&dAppInt_ds,&dAppInt_dkF);
492 493 494 495 496 497 498 499 500 501 502

  // Calculate the integrals
  //
  // inf
  //   /                       2   2      /     \
  //  |     2 n + 1   -(D+H(s)s ) y      |  w    |
  //  | dy y         e               Erfc| --- y |
  //  |                                  |  k    |
  // /                                    \  F  /
  //  0
  // we use that
Francois Gygi committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516
  // inf
  //   /                  2
  //  |     2 n + 1  - q y              n!
  //  | dy y        e      Erfc(y) = ----n+1-  *
  //  |                               2 q
  // /
  //  0
  //            n
  //    /     -----                                     \
  //   |       \     (2m - 1)!!  m             -(2m + 1) |
  //   |  1 -   )    ---------- q  sqrt( q + 1 )         |
  //   |       /       (2m)!!                            |
  //    \     -----                                     /
  //          m = 0
517
  // with q = (D + H(s)s^2) k_F^2 / w^2
518 519 520 521 522 523
  // and n!! = n * (n-2) * (n-4) ...; if (n <= 0) := 1

  // allocate array
  vector<double> intYExpErfc(0);
  intYExpErfc.reserve(4);

524 525
  // helper variables
  const double q = D_term / w_kF_Sqr;
526
  const double q_q_1 = q / ( q + 1 );
527 528
  const double sqrtq_1 = sqrt(q + 1);

529 530
  // initialize
  double prefactor = 0.5 / D_term; // note w_kF cancels in transformation
531
  double summand = 1.0 / sqrtq_1;
532 533 534 535
  double sum = 1 - summand;
  intYExpErfc.push_back(prefactor * sum);

  // calculate higher n integrals
536 537
  for ( int i = 1; i < 4; i++ )
  {
538 539

    // update values
540 541
    prefactor *= i * r1_D_term;
    summand *= (2.0 * i - 1.0) / (2.0 * i) * q_q_1;
542 543 544 545 546 547 548 549 550
    sum -= summand;

    intYExpErfc.push_back(prefactor * sum);

  }

  // Calculate the integrals
  //
  //       inf
551 552
  //         /                 /                    2      \
  //   2    |     2 (n+1)     |          2   2     w     2  |
553 554 555 556 557 558
  // -----  | dy y         Exp| -(D+H(s)s ) y  - ------ y   |
  //  ____  |                 |                   k ^2      |
  // V Pi  /                   \                   F       /
  //        0
  // for n = 0, 1, 2
  //
559
  const double r1_arg = 1.0 / ( D_term + w_kF_Sqr );
560 561 562 563 564 565
  // allocate array
  vector<double> intYGauss(3);
  intYGauss[0] = 0.5 * sqrt(r1_arg) * r1_arg;
  intYGauss[1] = 1.5 * intYGauss[0] * r1_arg;
  intYGauss[2] = 2.5 * intYGauss[1] * r1_arg;

566 567 568 569 570 571 572 573 574 575 576 577
  // put everything together

  // Calculate the integral
  //  inf
  //    /                 /      \
  //   |                 |  w     |
  //   | dy y J(s,y) Erfc| ---- y |
  //   |                 |  k     |
  //  /                   \  F   /
  //   0
  // where J(s, y) is the exchange hole defined in the references
  // the exchange factor is proportional to this integral
578 579
  *fx = -r8_9 * ( appInt + B * intYExpErfc[0] + C_term * intYExpErfc[1]
    + E_term * intYExpErfc[2] );
580

Francois Gygi committed
581 582 583
  // Calculate the derivatives with respect to s using that the derivatative
  // of the integral yields higher orders of the same kind of integral
  //  intY1 -> -intY3 -> intY5 ... times the derivative of the exponent
584 585 586 587 588
  *dfx_ds = -r8_9 * ( dAppInt_ds - ( B * intYExpErfc[1] + C_term
    * intYExpErfc[2] + E_term * intYExpErfc[3] ) * dHs2_ds + dCt_ds
    * intYExpErfc[1] + dEt_ds * intYExpErfc[2] );
  *dfx_dkf = -r8_9 * r1_kF * ( w_kF * ( B * intYGauss[0] + C_term
    * intYGauss[1] + E_term * intYGauss[2] ) + dAppInt_dkF );
589

Francois Gygi committed
590 591
  // if the value of s has been corrected to satisfy Lieb-Oxford bound,
  // derivative must be adjusted as well
592 593 594
  if ( correction )
  {
    *dfx_ds *= 2.0 * s_chg * pow(s_inp,-3);
595 596
  }

597
}
598

599 600 601 602 603 604 605 606 607 608
// exchange helper function
// input:
// rho  - charge density
// grad - absolute value of gradient
// a_ex - amount of HF exchange
// w    - screening
// output:
// ex       - exchange energy
// vx1, vx2 - exchange potential such that vx = vx1 + div( vx2 * grad(n) )
void HSE_exchange(const double rho, const double grad, const double a_ex,
609 610
  const double w, double *ex, double *vx1, double *vx2)
{
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

  // constants employed in the PBE/HSE exchange
  const double third = 1.0 / 3.0;
  const double third4 = 4.0 / 3.0;
  const double ax = -0.7385587663820224058; /* -0.75*pow(3.0/pi,third) */
  const double um = 0.2195149727645171;
  const double uk = 0.804;
  const double ul = um / uk;
  const double pi32third = 3.09366772628014; /* (3*pi^2 ) ^(1/3) */

  // intialize
  *ex = 0;
  *vx1 = 0;
  *vx2 = 0;

  // very small densities do not contribute
627
  if ( rho < 1e-18 ) return;
628 629

  // LDA exchange energy
630
  const double rho13 = pow(rho,third);
631 632 633 634 635
  const double exLDA = ax * rho13;

  // Fermi wave vector kF = ( 3 * pi^2 n )^(1/3)
  const double kF = pi32third * rho13;
  // reduced density gradient
636
  const double s = grad / ( 2.0 * kF * rho );
637 638 639 640 641 642

  // calculate PBE enhancement factor
  const double s2 = s * s;
  const double p0 = 1.0 + ul * s2;
  const double fxpbe = 1.0 + uk - uk / p0;
  // fs = (1/s) * d Fx / d s
643
  const double fs = 2.0 * uk * ul / ( p0 * p0 );
644 645
  // calculate HSE enhancement factor and derivatives w.r.t. s and kF
  double fxhse, dfx_ds, dfx_dkf;
646
  HSE_enhance(s,kF,w,&fxhse,&dfx_ds,&dfx_dkf);
647 648 649 650 651

  // calculate exchange energy
  // ex = (1 - a) ex,SR + ex,LR
  //    = (1 - a) ex,SR + ex,PBE - ex,SR
  //    = ex,PBE - a ex,SR
652
  *ex = exLDA * ( fxpbe - a_ex * fxhse );
653 654

  // calculate potential
655 656 657 658
  *vx1 = third4 * exLDA * ( fxpbe - s2 * fs - a_ex * ( fxhse - s * dfx_ds
    + 0.25 * kF * dfx_dkf ) );
  *vx2 = -exLDA * ( fs / ( rho * 4.0 * kF * kF ) - a_ex * dfx_ds / ( 2.0 * kF
    * grad ) );
659 660 661

}

662 663 664 665 666 667 668 669 670
////////////////////////////////////////////////////////////////////////////////
//
//  gcor2.c: Interpolate LSD correlation energy
//  as given by (10) of Perdew & Wang, Phys Rev B45 13244 (1992)
//  Translated into C by F.Gygi, Dec 9, 1996
//
////////////////////////////////////////////////////////////////////////////////

void gcor2(double a, double a1, double b1, double b2, double b3, double b4,
671 672
  double rtrs, double *gg, double *ggrs)
{
673
  double q0, q1, q2, q3;
674 675
  q0 = -2.0 * a * ( 1.0 + a1 * rtrs * rtrs );
  q1 = 2.0 * a * rtrs * ( b1 + rtrs * ( b2 + rtrs * ( b3 + rtrs * b4 ) ) );
676 677
  q2 = log(1.0 + 1.0 / q1);
  *gg = q0 * q2;
678 679
  q3 = a * ( b1 / rtrs + 2.0 * b2 + rtrs * ( 3.0 * b3 + 4.0 * b4 * rtrs ) );
  *ggrs = -2.0 * a * a1 * q2 - q0 * q3 / ( q1 * ( 1.0 + q1 ) );
680 681 682 683
}

////////////////////////////////////////////////////////////////////////////////
//
684
//  calculate correlation energy of the PBE functional
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
//  K.Burke's modification of PW91 codes, May 14, 1996.
//  Modified again by K.Burke, June 29, 1996, with simpler Fx(s)
//  Translated into C and modified by F.Gygi, Dec 9, 1996.
//
//  input:
//    rho:  density
//    grad: abs(grad(rho))
//  output:
//    exc: exchange-correlation energy per electron
//    vxc1, vxc2 : quantities such that the total exchange potential is:
//
//      vxc = vxc1 + div ( vxc2 * grad(n) )
//
//  References:
//  [a] J.P.Perdew, K.Burke, and M.Ernzerhof,
//      "Generalized gradient approximation made simple,
//      Phys.Rev.Lett. 77, 3865, (1996).
//  [b] J.P.Perdew and Y.Wang, Phys.Rev. B33, 8800 (1986),
//      Phys.Rev. B40, 3399 (1989) (E).
//
////////////////////////////////////////////////////////////////////////////////

void PBE_correlation(const double rho, const double grad, double *ec,
708 709
  double *vc1, double *vc2)
{
710 711 712 713 714 715 716 717
  *ec = 0.0;
  *vc1 = 0.0;
  *vc2 = 0.0;

  if ( rho < 1.e-18  )
  {
    return;
  }
718 719 720 721 722 723 724 725 726 727 728

  const double third = 1.0 / 3.0;
  const double pi32third = 3.09366772628014; /* (3*pi^2 ) ^(1/3) */
  const double alpha = 1.91915829267751; /* pow(9.0*pi/4.0, third)*/
  const double seven_sixth = 7.0 / 6.0;
  const double four_over_pi = 1.27323954473516;
  const double gamma = 0.03109069086965489; /* gamma = (1-ln2)/pi^2 */
  const double bet = 0.06672455060314922; /* see [a] (4) */
  const double delt = bet / gamma;

  // Fermi wave vector kF = ( 3 * pi^2 n )^(1/3)
729
  const double rho13 = pow(rho,third);
730 731 732 733 734 735 736 737 738
  const double fk = pi32third * rho13;

  /* Find LSD contributions, using [c] (10) and Table I of [c]. */
  /* ec = unpolarized LSD correlation energy */
  /* ecrs = d ec / d rs */
  /* construct ec, using [c] (8) */

  const double rs = alpha / fk;
  const double twoks = 2.0 * sqrt(four_over_pi * fk);
739
  const double t = grad / ( twoks * rho );
740 741 742

  const double rtrs = sqrt(rs);
  double ecrs;
743
  gcor2(0.0310907,0.2137,7.5957,3.5876,1.6382,0.49294,rtrs,ec,&ecrs);
744 745 746 747 748 749 750 751 752

  /* LSD potential from [c] (A1) */
  /* ecrs = d ec / d rs [c] (A2) */

  const double vc = *ec - rs * ecrs * third;

  /* PBE correlation energy */
  /* b = A of [a] (8) */

753
  const double pon = -*ec / gamma;
754
  const double b = delt / ( exp(pon) - 1.0 );
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
  const double b2 = b * b;
  const double t2 = t * t;
  const double t4 = t2 * t2;
  const double q4 = 1.0 + b * t2;
  const double q5 = q4 + b2 * t4;
  const double h = gamma * log(1.0 + delt * q4 * t2 / q5);

  // Energy done, now the potential, using appendix E of [b]

  const double t6 = t4 * t2;
  const double rsthrd = rs * third;
  const double fac = delt / b + 1.0;
  const double bec = b2 * fac / bet;
  const double q8 = q5 * q5 + delt * q4 * q5 * t2;
  const double q9 = 1.0 + 2.0 * b * t2;
770
  const double hb = -bet * b * t6 * ( 2.0 + b * t2 ) / q8;
771 772 773
  const double hrs = -rsthrd * hb * bec * ecrs;
  const double ht = 2.0 * bet * q9 / q8;

774
  *ec += h;
775
  *vc1 = vc + h + hrs - t2 * ht * seven_sixth;
776
  *vc2 = -ht / ( rho * twoks * twoks );
777 778 779

}

780 781
// spin polarized case
void PBE_correlation_sp(const double rho_up, const double rho_dn,
782 783 784
  const double grad_up, const double grad_dn, const double grad, double *ec,
  double *vc1_up, double *vc1_dn, double *vc2)
{
785 786 787 788 789 790 791
  *ec = 0.0;
  *vc1_up = 0.0;
  *vc1_dn = 0.0;
  *vc2 = 0.0;

  const double rh_up = ( rho_up < 1.e-18 ) ? 0.0 : rho_up;
  const double rh_dn = ( rho_dn < 1.e-18 ) ? 0.0 : rho_dn;
792 793 794 795 796 797 798 799 800 801

  const double third = 1.0 / 3.0;
  const double third2 = 2.0 / 3.0;
  const double third4 = 4.0 / 3.0;
  const double sixthm = -1.0 / 6.0;
  const double pi32third = 3.09366772628014; /* (3*pi^2 ) ^(1/3) */
  const double alpha = 1.91915829267751; /* pow(9.0*pi/4.0, third)*/
  const double seven_sixth = 7.0 / 6.0;
  const double four_over_pi = 1.27323954473516;
  const double gam = 0.5198420997897463; /* gam = 2^(4/3) - 2 */
802
  const double fzz = 8.0 / ( 9.0 * gam );
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
  const double gamma = 0.03109069086965489; /* gamma = (1-ln2)/pi^2 */
  const double bet = 0.06672455060314922; /* see [a] (4) */
  const double delt = bet / gamma;
  const double eta = 1.e-12; // small number to avoid blowup as |zeta|->1

  /* correlation */

  // Find LSD contributions, using [c] (10) and Table I of [c].
  // eu = unpolarized LSD correlation energy
  // eurs = d eu / d rs
  // ep = fully polarized LSD correlation energy
  // eprs = d ep / d rs
  // alfm = - spin stiffness, [c] (3)
  // alfrsm = -d alpha / d rs
  // f = spin-scaling factor from [c] (9)
  // construct ec, using [c] (8)

820
  const double rhotot = rh_up + rh_dn;
821

822
  const double rh13 = pow(rhotot,third);
823
  const double zet = ( rh_up - rh_dn ) / rhotot;
824
  const double g = 0.5 * ( pow(1.0 + zet,third2) + pow(1.0 - zet,third2) );
825 826 827
  const double fk = pi32third * rh13;
  const double rs = alpha / fk;
  const double twoksg = 2.0 * sqrt(four_over_pi * fk) * g;
828
  const double t = grad / ( twoksg * rhotot );
829 830 831

  const double rtrs = sqrt(rs);
  double eu, eurs, ep, eprs, alfm, alfrsm;
832 833 834
  gcor2(0.0310907,0.2137,7.5957,3.5876,1.6382,0.49294,rtrs,&eu,&eurs);
  gcor2(0.01554535,0.20548,14.1189,6.1977,3.3662,0.62517,rtrs,&ep,&eprs);
  gcor2(0.0168869,0.11125,10.357,3.6231,0.88026,0.49671,rtrs,&alfm,&alfrsm);
835
  const double z4 = zet * zet * zet * zet;
836 837 838
  const double f = ( pow(1.0 + zet,third4) + pow(1.0 - zet,third4) - 2.0 )
    / gam;
  *ec = eu * ( 1.0 - f * z4 ) + ep * f * z4 - alfm * f * ( 1.0 - z4 ) / fzz;
839 840 841

  /* LSD potential from [c] (A1) */
  /* ecrs = d ec / d rs [c] (A2) */
842 843 844 845 846 847
  const double ecrs = eurs * ( 1.0 - f * z4 ) + eprs * f * z4 - alfrsm * f
    * ( 1.0 - z4 ) / fzz;
  const double fz = third4 * ( pow(1.0 + zet,third) - pow(1.0 - zet,third) )
    / gam;
  const double eczet = 4.0 * ( zet * zet * zet ) * f * ( ep - eu + alfm / fzz )
    + fz * ( z4 * ep - z4 * eu - ( 1.0 - z4 ) * alfm / fzz );
848 849 850 851 852 853 854 855
  const double comm = *ec - rs * ecrs * third - zet * eczet;
  *vc1_up = comm + eczet;
  *vc1_dn = comm - eczet;

  /* PBE correlation energy */
  /* b = A of [a] (8) */

  const double g3 = g * g * g;
856 857
  const double pon = -*ec / ( g3 * gamma );
  const double b = delt / ( exp(pon) - 1.0 );
858 859 860 861 862 863 864 865 866 867 868 869
  const double b2 = b * b;
  const double t2 = t * t;
  const double t4 = t2 * t2;
  const double q4 = 1.0 + b * t2;
  const double q5 = q4 + b2 * t4;
  const double h = g3 * gamma * log(1.0 + delt * q4 * t2 / q5);

  /* Energy done, now the potential, using appendix E of [b] */

  const double g4 = g3 * g;
  const double t6 = t4 * t2;
  const double rsthrd = rs * third;
870 871
  const double gz = ( pow(( 1.0 + zet ) * ( 1.0 + zet ) + eta,sixthm) - pow(
    ( 1.0 - zet ) * ( 1.0 - zet ) + eta,sixthm) ) * third;
872
  const double fac = delt / b + 1.0;
873 874
  const double bg = -3.0 * b2 * *ec * fac / ( bet * g4 );
  const double bec = b2 * fac / ( bet * g3 );
875 876
  const double q8 = q5 * q5 + delt * q4 * q5 * t2;
  const double q9 = 1.0 + 2.0 * b * t2;
877
  const double hb = -bet * g3 * b * t6 * ( 2.0 + b * t2 ) / q8;
878
  const double hrs = -rsthrd * hb * bec * ecrs;
879
  const double hzed = 3.0 * gz * h / g + hb * ( bg * gz + bec * eczet );
880 881 882 883 884 885 886 887 888 889 890 891
  const double ht = 2.0 * bet * g3 * q9 / q8;

  double ccomm = h + hrs - t2 * ht * seven_sixth;
  const double pref = hzed - gz * t2 * ht / g;

  ccomm -= pref * zet;

  *ec += h;

  *vc1_up += ccomm + pref;
  *vc1_dn += ccomm - pref;

892
  *vc2 = -ht / ( rhotot * twoksg * twoksg );
893 894 895

}

896
// update exchange correlation energy and potential
897 898 899 900 901 902 903 904 905 906
void HSEFunctional::setxc(void)
{
  if ( _np == 0 ) return;
  if ( _nspin == 1 )
  {
    assert(rho != 0);
    assert(grad_rho[0] != 0 && grad_rho[1] != 0 && grad_rho[2] != 0);
    assert(exc != 0);
    assert(vxc1 != 0);
    assert(vxc2 != 0);
907 908

#pragma omp parallel for
909 910
    for ( int i = 0; i < _np; i++ )
    {
911 912
      // evaluate gradient
      const double grad = sqrt(grad_rho[0][i] * grad_rho[0][i] + grad_rho[1][i]
913
        * grad_rho[1][i] + grad_rho[2][i] * grad_rho[2][i]);
914 915 916

      // calculate HSE exchange and PBE correlation
      double ex, vx1, vx2, ec, vc1, vc2;
917 918
      HSE_exchange(rho[i],grad,1 - x_coeff_,omega,&ex,&vx1,&vx2);
      PBE_correlation(rho[i],grad,&ec,&vc1,&vc2);
919 920 921 922 923 924 925

      // combine exchange and correlation energy
      exc[i] = ex + c_coeff_ * ec;
      vxc1[i] = vx1 + c_coeff_ * vc1;
      vxc2[i] = vx2 + c_coeff_ * vc2;
    }

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
  }
  else
  {
    assert(rho_up != 0);
    assert(rho_dn != 0);
    assert(grad_rho_up[0] != 0 && grad_rho_up[1] != 0 && grad_rho_up[2] != 0);
    assert(grad_rho_dn[0] != 0 && grad_rho_dn[1] != 0 && grad_rho_dn[2] != 0);
    assert(exc_up != 0);
    assert(exc_dn != 0);
    assert(vxc1_up != 0);
    assert(vxc1_dn != 0);
    assert(vxc2_upup != 0);
    assert(vxc2_updn != 0);
    assert(vxc2_dnup != 0);
    assert(vxc2_dndn != 0);
941 942

#pragma omp parallel for
943 944
    for ( int i = 0; i < _np; i++ )
    {
945 946 947 948 949 950 951 952 953 954 955
      // evaluate gradient
      double grx_up = grad_rho_up[0][i];
      double gry_up = grad_rho_up[1][i];
      double grz_up = grad_rho_up[2][i];
      double grx_dn = grad_rho_dn[0][i];
      double gry_dn = grad_rho_dn[1][i];
      double grz_dn = grad_rho_dn[2][i];
      double grx = grx_up + grx_dn;
      double gry = gry_up + gry_dn;
      double grz = grz_up + grz_dn;
      double grad_up =
956
        sqrt(grx_up * grx_up + gry_up * gry_up + grz_up * grz_up);
957
      double grad_dn =
958
        sqrt(grx_dn * grx_dn + gry_dn * gry_dn + grz_dn * grz_dn);
959 960 961 962 963
      double grad = sqrt(grx * grx + gry * gry + grz * grz);

      // calculate HSE exchange and PBE correlation
      double ex_up, vx1_up, vx2_up, ex_dn, vx1_dn, vx2_dn;
      double ec, vc1_up, vc1_dn, vc2;
964 965 966 967
      HSE_exchange(2.0 * rho_up[i],2.0 * grad_up,1 - x_coeff_,omega,&ex_up,
        &vx1_up,&vx2_up);
      HSE_exchange(2.0 * rho_dn[i],2.0 * grad_dn,1 - x_coeff_,omega,&ex_dn,
        &vx1_dn,&vx2_dn);
968 969
      PBE_correlation_sp(rho_up[i],rho_dn[i],grad_up,grad_dn,grad,&ec,&vc1_up,
        &vc1_dn,&vc2);
970 971 972 973 974 975 976 977 978 979 980 981

      // combine exchange and correlation energy
      exc_up[i] = ex_up + c_coeff_ * ec;
      exc_dn[i] = ex_dn + c_coeff_ * ec;
      vxc1_up[i] = vx1_up + c_coeff_ * vc1_up;
      vxc1_dn[i] = vx1_dn + c_coeff_ * vc1_dn;
      vxc2_upup[i] = 2 * vx2_up + c_coeff_ * vc2;
      vxc2_dndn[i] = 2 * vx2_dn + c_coeff_ * vc2;
      vxc2_updn[i] = c_coeff_ * vc2;
      vxc2_dnup[i] = c_coeff_ * vc2;
    }
  }
982
}