HSEFunctional.C 26.6 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 31
//
////////////////////////////////////////////////////////////////////////////////
//
// Author: Martin Schlipf (2013)
// Contact: martin.schlipf@gmail.com
32 33 34
//

#include "HSEFunctional.h"
35
#include "ExponentialIntegral.h"
36
#include <cassert>
37
#include <cmath>
38
#include <numeric>
39 40 41

using namespace std;

42 43 44 45 46 47
// screening parameter of the HSE functional
const double omega = 0.11;
// parameters of the exchange hole
const double A = 1.0161144, B = -0.37170836, C = -0.077215461, D = 0.57786348,
    E = -0.051955731;

48 49
// constructor
HSEFunctional::HSEFunctional(const vector<vector<double> > &rhoe) :
50
  x_coeff_(0.75), c_coeff_(1.0) {
51 52 53
  // nonmagnetic or magnetic
  _nspin = rhoe.size();
  // in magnetic calculations both density arrays must have the same size
54 55
  if (_nspin > 1)
    assert(rhoe[0].size() == rhoe[1].size());
56 57 58

  // allocate arrays
  _np = rhoe[0].size();
59
  if (_nspin == 1) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73
    // 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];
74
  } else {
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 109 110
    // 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];
  }

}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
// 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,
    const double D_term, const double dHs2_ds, double *appInt,
    double *dAppInt_ds, double *dAppInt_dkF) {

  // constant parameterization of error function
  const double a[] = { 1.0, -1.128223946706117, 1.452736265762971,
      -1.243162299390327, 0.971824836115601, -0.568861079687373,
      0.246880514820192, -0.065032363850763, 0.008401793031216 };
  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;

  if (bw2_Hs2 < cutoff) {

    // 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 bw2_Hs2_Sqr = bw2_Hs2 * bw2_Hs2;
    const double bw2_Hs2_Cub = bw2_Hs2 * bw2_Hs2_Sqr;
    const double sqrt_bw2_Hs2 = sqrt(bw2_Hs2);
    const double bw2_D_term_Sqr = bw2_D_term * bw2_D_term;
    const double bw2_D_term_Cub = bw2_D_term * bw2_D_term_Sqr;
    const double bw2_D_term_Tet = bw2_D_term_Sqr * bw2_D_term_Sqr;
    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);
    const double r1_arg = 1.0 / 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
    double term2 = (arg < util::series_cutoff) ? exp_arg * util::E1(arg)
        : util::gauss_laguerre(arg);

    // 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) )
    integral[0] = A_2 * (log(bw2_Hs2 / bw2_D_term) + term2);

    // 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;

    for (int i = 1; i < no_integral / 2; i++) {
      term1 = term1 / bw2_D_term * static_cast<double> (i);
      factor2 = -factor2 * r9_4A;
      term2 = term2 + arg_n;

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

      arg_n = -arg_n * static_cast<double> (i) / arg;
    }

    // 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;

    for (int i = 3; i < no_integral; i += 2) {
      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 ###

    const int no_coeff = sizeof(a) / sizeof(double);

    // 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;
    for (int i = 0; i < no_coeff; i++) {
      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
    *appInt = inner_product(a_wi.begin(), a_wi.end(), integral.begin(), 0.0);
    // for derivative shift integral index by 2
    const double dotpr = inner_product(a_wi.begin(), a_wi.end(),
        integral.begin() + 2, 0.0);
    *dAppInt_ds = -dotpr * dHs2_ds;
    *dAppInt_dkF = -inner_product(ai_wi.begin(), ai_wi.end(), integral.begin(),
        r2bw2 * dotpr);

  } else {

    // large x limit

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    // 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);
    *appInt = A_2 * (log(r2w2_Hs2 / r2w2_D_term) + exp_e1);
    const double dAppInt_dh = -A_2 / r2w2_D_term + 1.125 * exp_e1;
    *dAppInt_ds = dAppInt_dh * dHs2_ds;
    *dAppInt_dkF = -dAppInt_dh * r4w2;

299 300 301 302
  }

}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
// helper function to calculate the HSE enhancement factor
// and its derivative w.r.t. s and k_F
//                inf
//                  /
//  HSE         8  |                  w y
// F (s,k ) = - -  | dy J(s,y) erfc( ----- )
//  x    F      9  |                   k
//                /                     F
//                 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)
327 328
void HSE_enhance(const double s_inp, const double kF, const double w,
    double *fx, double *dfx_ds, double* dfx_dkf) {
329

330 331 332 333 334 335 336
  // Correction of the reduced gradient to ensure Lieb-Oxford bound
  // If a large value of s would violate the Lieb-Oxford bound, the value of s is reduced,
  // so that this condition is fullfilled
  const double s_thresh = 8.3, s_max = 8.572844, s_chg = 18.796223;
  const bool correction = s_inp > s_thresh;
  const double s = (correction) ? s_max - s_chg / (s_inp * s_inp) : s_inp;

337 338 339
  // prefactor for exchange hole
  const double r8_9 = 8.0 / 9.0;

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  // derived quantities
  const double SQRT_PI = sqrt(M_PI);
  const double sqrtA = sqrt(A), r9_4A = 2.25 * A;

  // evaluate
  //                 2       4
  //             a1 s  + a2 s
  // H(s) = ---------4------5------6-
  //         1 + a3 s + a4 s + a5 s
  // and
  //     2                 3         5            3        4        5
  // d( s H(s) )   ( 4 a1 s  + 6 a2 s ) - ( 4 a3 s + 5 a4 s + 6 a5 s ) * H(s)
  // ----------- = ---------------------4------5------6----------------------
  //     d s                    1 + a3 s + a4 s + a5 s

  const double a1 = 0.00979681, a2 = 0.0410834, a3 = 0.187440, a4 = 0.00120824,
      a5 = 0.0347188;

  // 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;
  const double r_denom = 1.0 / (1.0 + a3 * s4 + a4 * s5 + a5 * s6);
  // 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;
  const double dHs2_ds = (first - second * H) * r_denom;

  // evaluate
  // F(s) = 6.475 H(s) + 0.4797
  // d( s^2 F(s) )         d( s^2 H(s) )
  // ------------- = 6.475 ------------- + 2 * 0.4797
  //     d s
  const double slope = 6.475;
  const double intercept = 0.4797;

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

389 390 391 392 393 394 395 396 397 398 399 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 428 429 430 431 432 433 434 435 436 437
  // evaluate alpha and beta
  // alpha = part1 - part2
  //               __  2
  //           15 VPi s
  // beta = -----------------
  //                  2  7/2
  //         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 AHs2_3_2 = AHs2_1_2 * A * 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;
  const double D_Hs2_11_2 = D_Hs2_5_2 * D_Hs2Cub;

  // part 1 and derivatives w.r.t. Hs^2 and Fs^2
  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);
  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;
  const double dpart2_dh = 0.75 * M_PI * sqrtA * (r9_4A * exp_erfc - 1.5
      / (SQRT_PI * AHs2_1_2));
438

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  // 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
  const double beta_s2 = 0.9375 * SQRT_PI / D_Hs2_7_2;
  const double Ebeta = E * beta_s2 * s2;
  const double dbeta_dh = -3.5 * beta_s2 / D_Hs2;

  // combine alpha and beta to function G
  //       3 Pi / 4 + alpha
  // G = - ----------------
  //             beta
  const double r3Pi_4_alpha = 0.75 * M_PI + alpha;
  const double G = -r3Pi_4_alpha / Ebeta;

  // calculate derivative w.r.t. s
  //
  //             /  /3 Pi    \  d(b/s^2)   /           d a    \  d(Hs^2)       d a   d(Fs^2)
  //            <  ( ---- + a ) -------   /  b/s^2  - -------  > -------  -  ------- -------
  // d (Gs^2)    \  \ 4      /  d(Hs^2)  /            d(Hs^2) /    d s       d(Fs^2)   ds
  // -------- = ----------------------------------------------------------------------------
  //    ds                                     E b/s^2
  //
  // notice that alpha and beta are abbreviated by a and b in this equation
  const double Ebeta_s2 = E * beta_s2;
  const double dGs2_ds = ((r3Pi_4_alpha * dbeta_dh / beta_s2 - dalpha_dh)
      * dHs2_ds - dalpha_df * dFs2_ds) / Ebeta_s2;

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  // helper variables for the integration of the exchange hole
  const double C_term = C * (1 + s2 * F);
  const double dCt_ds = C * dFs2_ds;
  const double E_term = E * (1 + s2 * G);
  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;
  const double r1_a = w_kF_Sqr / D_term;
  const double r1_sqrta = sqrt(r1_a);

  // approximate the integral using an expansion of the error function
  double appInt, dAppInt_ds, dAppInt_dkF;
  approximateIntegral(w_kF, Hs2, D_term, dHs2_ds, &appInt, &dAppInt_ds,
      &dAppInt_dkF);

  // 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
  // inf                                                      n
  //   /                  2                           /     -----                                  \
  //  |     2 n + 1  - a y                 n   n!    |       \     (2n - 1)!!             -(2n + 1) |
  //  | dy y        e      Erfc(y) = ( -1 )  ----n-  |  1 -   )    ---------- sqrt( a + 1 )         |
  //  |                                       2 a    |       /       (2n)!!                         |
  // /                                                \     -----                                  /
  //  0                                                     m = 0
  // with a = (D + H(s)s^2) k_F^2 / w^2
  // and n!! = n * (n-2) * (n-4) ...; if (n <= 0) := 1

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

  // initialize
  double prefactor = 0.5 / D_term; // note w_kF cancels in transformation
  double summand = r1_sqrta;
  double sum = 1 - summand;
  intYExpErfc.push_back(prefactor * sum);

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

    // update values
    prefactor *= -static_cast<double> (i) * r1_D_term;
    summand *= static_cast<double> (2 * i - 1) / static_cast<double> (2 * i)
        * r1_a;
    sum -= summand;

    intYExpErfc.push_back(prefactor * sum);

  }

  // Calculate the integrals
  //
  //       inf
533 534
  //         /                 /                    2      \
  //   2    |     2 (n+1)     |          2   2     w     2  |
535 536 537 538 539 540 541 542 543 544 545 546 547
  // -----  | dy y         Exp| -(D+H(s)s ) y  - ------ y   |
  //  ____  |                 |                   k ^2      |
  // V Pi  /                   \                   F       /
  //        0
  // for n = 0, 1, 2
  //
  const double r1_arg = 1.0 / (D_term + w_kF_Sqr);
  // 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;

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  // 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
  *fx = -r8_9 * (appInt + B * intYExpErfc[0] + C_term * intYExpErfc[1] + E_term
      * intYExpErfc[2]);

  // 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
  *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);

572 573
  // if the value of s has been corrected to satisfy Lieb-Oxford bound, derivative
  // must be adjusted as well
574 575
  if (correction) {
    *dfx_ds *= 2.0 * s_chg * pow(s_inp, -3);
576 577
  }

578
}
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
// 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,
    const double w, double *ex, double *vx1, double *vx2) {

  // 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
  if (rho < 1e-18)
    return;

  // LDA exchange energy
  const double rho13 = pow(rho, third);
  const double exLDA = ax * rho13;

  // Fermi wave vector kF = ( 3 * pi^2 n )^(1/3)
  const double kF = pi32third * rho13;
  // reduced density gradient
  const double s = grad / (2.0 * kF * rho);

  // 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
  const double fs = 2.0 * uk * ul / (p0 * p0);
  // calculate HSE enhancement factor and derivatives w.r.t. s and kF
  double fxhse, dfx_ds, dfx_dkf;
  HSE_enhance(s, kF, w, &fxhse, &dfx_ds, &dfx_dkf);

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

  // calculate potential
  *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));

}

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
////////////////////////////////////////////////////////////////////////////////
//
//  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,
    double rtrs, double *gg, double *ggrs) {
  double q0, q1, q2, q3;
  q0 = -2.0 * a * (1.0 + a1 * rtrs * rtrs);
  q1 = 2.0 * a * rtrs * (b1 + rtrs * (b2 + rtrs * (b3 + rtrs * b4)));
  q2 = log(1.0 + 1.0 / q1);
  *gg = q0 * q2;
  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));
}

////////////////////////////////////////////////////////////////////////////////
//
//  calculate correletation energy of PBE functional
//  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,
    double *vc1, double *vc2) {

  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) */
  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)
  const double rho13 = pow(rho, third);
  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);
  const double t = grad / (twoks * rho);

  const double rtrs = sqrt(rs);
  double ecrs;
  gcor2(0.0310907, 0.2137, 7.5957, 3.5876, 1.6382, 0.49294, rtrs, ec, &ecrs);

  /* 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) */

  const double pon = - *ec / gamma;
  const double b = delt / (exp(pon) - 1.0);
  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;
  const double hb = -bet * b * t6 * (2.0 + b * t2) / q8;
  const double hrs = -rsthrd * hb * bec * ecrs;
  const double ht = 2.0 * bet * q9 / q8;

  *vc1 = vc + h + hrs - t2 * ht * seven_sixth;
  *vc2 = -ht / (rho * twoks * twoks);

}

755
void HSEFunctional::setxc(void) {
756 757
  // dummy
}