XCPotential.C 60.2 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// 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/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19
// XCPotential.C
//
////////////////////////////////////////////////////////////////////////////////

#include "XCPotential.h"
20
#include "LDAFunctional.h"
Francois Gygi committed
21
#include "VWNFunctional.h"
22 23
#include "PBEFunctional.h"
#include "BLYPFunctional.h"
24
#include "HSEFunctional.h"
Francois Gygi committed
25
#include "RSHFunctional.h"
Francois Gygi committed
26
#include "B3LYPFunctional.h"
27
#include "BHandHLYPFunctional.h"
Francois Gygi committed
28 29 30 31
#include "SCANFunctional.h"
#include "Sample.h"
#include "Wavefunction.h"
#include "SlaterDet.h"
Francois Gygi committed
32 33 34 35 36 37 38
#include "Basis.h"
#include "FourierTransform.h"
#include "blas.h" // daxpy, dcopy
#include <cassert>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
39
XCPotential::XCPotential(const ChargeDensity& cd, const string functional_name,
Francois Gygi committed
40
  const Sample& s): cd_(cd), vft_(*cd_.vft()), vbasis_(*cd_.vbasis()), s_(s)
Francois Gygi committed
41
{
42 43 44
  // copy arrays to resize rhototal_r_ and rhototal_g_
  rhototal_r_ = cd_.rhor;
  rhototal_g_ = cd_.rhog;
Francois Gygi committed
45 46
  if ( functional_name == "LDA" )
  {
47
    xcf_ = new LDAFunctional(rhototal_r_);
Francois Gygi committed
48
  }
Francois Gygi committed
49 50
  else if ( functional_name == "VWN" )
  {
51
    xcf_ = new VWNFunctional(rhototal_r_);
Francois Gygi committed
52
  }
Francois Gygi committed
53 54
  else if ( functional_name == "PBE" )
  {
55
    xcf_ = new PBEFunctional(rhototal_r_);
Francois Gygi committed
56 57 58
  }
  else if ( functional_name == "BLYP" )
  {
59
    xcf_ = new BLYPFunctional(rhototal_r_);
Francois Gygi committed
60
  }
Francois Gygi committed
61 62 63 64
  else if ( functional_name == "SCAN" )
  {
    xcf_ = new SCANFunctional(cd_.rhor);
  }
Francois Gygi committed
65 66
  else if ( functional_name == "PBE0" )
  {
Francois Gygi committed
67
    const double x_coeff = 1.0 - s_.ctrl.alpha_PBE0;
Francois Gygi committed
68
    const double c_coeff = 1.0;
69
    xcf_ = new PBEFunctional(rhototal_r_,x_coeff,c_coeff);
Francois Gygi committed
70
  }
71 72
  else if ( functional_name == "HSE" )
  {
73
    xcf_ = new HSEFunctional(rhototal_r_);
74
  }
Francois Gygi committed
75 76
  else if ( functional_name == "RSH" )
  {
Francois Gygi committed
77 78
    xcf_ = new RSHFunctional(rhototal_r_,s_.ctrl.alpha_RSH,s_.ctrl.beta_RSH,
           s_.ctrl.mu_RSH);
Francois Gygi committed
79
  }
Francois Gygi committed
80 81
  else if ( functional_name == "B3LYP" )
  {
82
    xcf_ = new B3LYPFunctional(rhototal_r_);
Francois Gygi committed
83
  }
84 85
  else if ( functional_name == "BHandHLYP" )
  {
86
    xcf_ = new BHandHLYPFunctional(rhototal_r_);
87
  }
Francois Gygi committed
88 89 90 91 92 93 94
  else
  {
    throw XCPotentialException("unknown functional name");
  }
  nspin_ = cd_.rhor.size();
  ngloc_ = vbasis_.localsize();
  np012loc_ = vft_.np012loc();
95

Francois Gygi committed
96
  if ( isGGA() )
Francois Gygi committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  {
    tmp1.resize(ngloc_);
    if ( nspin_ > 1 )
      tmp2.resize(ngloc_);
    vxctmp.resize(nspin_);
    for ( int ispin = 0; ispin < nspin_; ispin++ )
      vxctmp[ispin].resize(np012loc_);
    tmpr.resize(np012loc_);
  }
}

////////////////////////////////////////////////////////////////////////////////
XCPotential::~XCPotential(void)
{
  delete xcf_;
}

Francois Gygi committed
114 115 116 117 118 119
////////////////////////////////////////////////////////////////////////////////
bool XCPotential::isGGA(void)
{
  return xcf_->isGGA();
}

Francois Gygi committed
120 121 122 123 124 125
////////////////////////////////////////////////////////////////////////////////
bool XCPotential::isMeta(void)
{
  return xcf_->isMeta();
}

Francois Gygi committed
126
////////////////////////////////////////////////////////////////////////////////
127
void XCPotential::update(vector<vector<double> >& vr)
Francois Gygi committed
128
{
129
  // compute exchange-correlation energy and vxc potential vr[ispin][ir]
130

Francois Gygi committed
131 132 133 134 135
  // Input: total electronic density in:
  //   vector<vector<double> >           cd_.rhor[ispin][ir] (real space)
  //   vector<vector<complex<double> > > cd_.rhog[ispin][ig] (Fourier coeffs)
  // The array cd_.rhog is only used if xcf->isGGA() == true
  // to compute the density gradients
136

137 138 139 140
  // if a non-linear core correction is included,
  // rhototal_r_ = cd_.rhor+cd_.rhocore_r. Otherwise
  // rhototal_r_ = cd_.rhor

Francois Gygi committed
141 142
  // Output: (through member function xcf())
  //
143
  // exc_, dxc_
Francois Gygi committed
144 145
  //
  // LDA Functional:
146
  //   exc_, dxc_
Francois Gygi committed
147 148 149 150
  //   spin unpolarized: xcf()->exc, xcf()->vxc1
  //   spin polarized:   xcf()->exc, xcf()->vxc1_up, xcf()->vxc1_dn
  //
  // GGA Functional: (through member function xcf())
151
  //   exc_, dxc_
Francois Gygi committed
152
  //   spin unpolarized: xcf()->exc, xcf()->vxc1, xcf()->vxc2
153
  //   spin polarized:   xcf()->exc_up, xcf()->exc_dn,
Francois Gygi committed
154
  //                     xcf()->vxc1_up, xcf()->vxc1_dn
155
  //                     xcf()->vxc2_upup, xcf()->vxc2_dndn,
Francois Gygi committed
156
  //                     xcf()->vxc2_updn, xcf()->vxc2_dnup
157

158 159 160 161 162 163 164 165 166 167 168
  rhototal_r_ = cd_.rhor;
  rhototal_g_ = cd_.rhog;
  // test if a non-linear core correction is used
  // if so, add core density to rhototal_r_ and rhototal_g_
  if ( !cd_.rhocore_r.empty() )
  {
    // add core charge
    // note: if nspin==2, the cd_.rhocore_{rg} vectors each
    // contain half of the total core charge
    for ( int ispin = 0; ispin < rhototal_r_.size(); ispin++ )
    {
169 170 171 172 173 174 175 176 177 178 179 180 181
      //for ( int i = 0; i < rhototal_r_[ispin].size(); i++ )
      //  rhototal_r_[ispin][i] += cd_.rhocore_r[i];
      int len = rhototal_r_[ispin].size();
      int inc1 = 1;
      double one = 1.0;
      daxpy(&len,&one,(double*)&cd_.rhocore_r[0],&inc1,
            &rhototal_r_[ispin][0],&inc1);

      //for ( int i = 0; i < rhototal_g_[ispin].size(); i++ )
      //  rhototal_g_[ispin][i] += cd_.rhocore_g[i];
      len = 2*rhototal_g_[ispin].size();
      daxpy(&len,&one,(double*)&cd_.rhocore_g[0],&inc1,
            (double*)&rhototal_g_[ispin][0],&inc1);
182 183 184
    }
  }

Francois Gygi committed
185
  if ( !isGGA() )
Francois Gygi committed
186 187
  {
    // LDA functional
188

189
    xcf_->setxc();
190

Francois Gygi committed
191
    exc_ = 0.0;
192
    dxc_ = 0.0;
Francois Gygi committed
193
    const double *const e = xcf_->exc;
Francois Gygi committed
194
    const int size = xcf_->np();
195

Francois Gygi committed
196 197 198 199 200 201 202
    if ( nspin_ == 1 )
    {
      // unpolarized
      const double *const rh = xcf_->rho;
      const double *const v = xcf_->vxc1;
      for ( int i = 0; i < size; i++ )
      {
203 204 205 206 207
        const double e_i = e[i];
        const double v_i = v[i];
        const double rh_i = rh[i];
        exc_ += rh_i * e_i;
        dxc_ += rh_i * ( e_i - v_i );
208
        vr[0][i] = v_i;
Francois Gygi committed
209 210 211 212 213 214 215 216 217
      }
    }
    else
    {
      // spin polarized
      const double *const rh_up = xcf_->rho_up;
      const double *const rh_dn = xcf_->rho_dn;
      const double *const v_up = xcf_->vxc1_up;
      const double *const v_dn = xcf_->vxc1_dn;
218 219
      for ( int i = 0; i < size; i++ )
      {
220 221 222
        const double r_i = rh_up[i] + rh_dn[i];
        exc_ += r_i * e[i];
        dxc_ += r_i * e[i] - rh_up[i] * v_up[i] - rh_dn[i] * v_dn[i];
223 224
        vr[0][i] = v_up[i];
        vr[1][i] = v_dn[i];
225
      }
Francois Gygi committed
226
    }
227 228 229 230 231 232
    double sum[2],tsum[2];
    sum[0] = exc_ * vbasis_.cell().volume() / vft_.np012();
    sum[1] = dxc_ * vbasis_.cell().volume() / vft_.np012();
    MPI_Allreduce(&sum,&tsum,2,MPI_DOUBLE,MPI_SUM,vbasis_.comm());
    exc_ = tsum[0];
    dxc_ = tsum[1];
Francois Gygi committed
233 234 235 236 237
  }
  else
  {
    // GGA functional
    exc_ = 0.0;
238

Francois Gygi committed
239 240 241 242 243 244 245 246 247 248
    // compute grad_rho
    const double omega_inv = 1.0 / vbasis_.cell().volume();
    if ( nspin_ == 1 )
    {
      for ( int j = 0; j < 3; j++ )
      {
        const double *const gxj = vbasis_.gx_ptr(j);
        for ( int ig = 0; ig < ngloc_; ig++ )
        {
          /* i*G_j*c(G) */
249
          tmp1[ig] = complex<double>(0.0,omega_inv*gxj[ig])*rhototal_g_[0][ig];
Francois Gygi committed
250 251 252 253
        }
        vft_.backward(&tmp1[0],&tmpr[0]);
        int inc2=2, inc1=1;
        double *grj = xcf_->grad_rho[j];
Francois Gygi committed
254
        dcopy(&np012loc_,(double*)&tmpr[0],&inc2,grj,&inc1);
Francois Gygi committed
255 256 257 258 259 260 261
      }
    }
    else
    {
      for ( int j = 0; j < 3; j++ )
      {
        const double *const gxj = vbasis_.gx_ptr(j);
262 263
        const complex<double>* rhg0 = &rhototal_g_[0][0];
        const complex<double>* rhg1 = &rhototal_g_[1][0];
Francois Gygi committed
264 265 266 267 268 269
        for ( int ig = 0; ig < ngloc_; ig++ )
        {
          /* i*G_j*c(G) */
          const complex<double> igxj(0.0,omega_inv*gxj[ig]);
          const complex<double> c0 = *rhg0++;
          const complex<double> c1 = *rhg1++;
270 271
          tmp1[ig] = igxj * c0;
          tmp2[ig] = igxj * c1;
Francois Gygi committed
272 273 274 275 276 277
        }
        vft_.backward(&tmp1[0],&tmp2[0],&tmpr[0]);
        double *grj_up = xcf_->grad_rho_up[j];
        double *grj_dn = xcf_->grad_rho_dn[j];
        int inc2=2, inc1=1;
        double* p = (double*) &tmpr[0];
Francois Gygi committed
278 279
        dcopy(&np012loc_,p,  &inc2,grj_up,&inc1);
        dcopy(&np012loc_,p+1,&inc2,grj_dn,&inc1);
Francois Gygi committed
280 281
      } // j
    }
Francois Gygi committed
282 283 284 285 286 287 288 289 290 291 292 293
    if ( isMeta() )
    {
      // compute tau
      if (nspin_ == 1)
      {
        cd_.update_taur(xcf_->tau);
      }
      else
      {
        cd_.update_taur(xcf_->tau_up, xcf_->tau_dn);
      }
    }
294

Francois Gygi committed
295
    xcf_->setxc();
296

Francois Gygi committed
297 298 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
    // compute xc potential
    // take divergence of grad(rho)*vxc2

    // compute components of grad(rho) * vxc2
    if ( nspin_ == 1 )
    {
      for ( int j = 0; j < 3; j++ )
      {
        const double *const gxj = vbasis_.gx_ptr(j);
        const double *const grj = xcf_->grad_rho[j];
        const double *const v2 = xcf_->vxc2;
        for ( int ir = 0; ir < np012loc_; ir++ )
        {
          tmpr[ir] = grj[ir] * v2[ir];
        }
        // derivative
        vft_.forward(&tmpr[0],&tmp1[0]);
        for ( int ig = 0; ig < ngloc_; ig++ )
        {
          // i*G_j*c(G)
          tmp1[ig] *= complex<double>(0.0,gxj[ig]);
        }
        // back to real space
        vft_.backward(&tmp1[0],&tmpr[0]);
        // accumulate div(vxc2*grad_rho) in vxctmp
        double one = 1.0;
        int inc1 = 1, inc2 = 2;
        if ( j == 0 )
        {
Francois Gygi committed
326
          dcopy(&np012loc_,(double*)&tmpr[0],&inc2,&vxctmp[0][0],&inc1);
Francois Gygi committed
327 328 329
        }
        else
        {
Francois Gygi committed
330
          daxpy(&np012loc_,&one,(double*)&tmpr[0],&inc2,&vxctmp[0][0],&inc1);
Francois Gygi committed
331 332 333 334 335 336 337 338 339 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
        }
      }
    }
    else
    {
      double *v2_upup = xcf_->vxc2_upup;
      double *v2_updn = xcf_->vxc2_updn;
      double *v2_dnup = xcf_->vxc2_dnup;
      double *v2_dndn = xcf_->vxc2_dndn;
      for ( int j = 0; j < 3; j++ )
      {
        const double *gxj = vbasis_.gx_ptr(j);
        const double *grj_up = xcf_->grad_rho_up[j];
        const double *grj_dn = xcf_->grad_rho_dn[j];
        for ( int ir = 0; ir < np012loc_; ir++ )
        {
          const double re = v2_upup[ir] * grj_up[ir] + v2_updn[ir] * grj_dn[ir];
          const double im = v2_dnup[ir] * grj_up[ir] + v2_dndn[ir] * grj_dn[ir];
          tmpr[ir] = complex<double>(re,im);
        }
        // derivative
        vft_.forward(&tmpr[0],&tmp1[0],&tmp2[0]);
        for ( int ig = 0; ig < ngloc_; ig++ )
        {
          // i*G_j*c(G)
          const complex<double> igxj(0.0,gxj[ig]);
          tmp1[ig] *= igxj;
          tmp2[ig] *= igxj;
        }
        vft_.backward(&tmp1[0],&tmp2[0],&tmpr[0]);
        // accumulate div(vxc2*grad_rho) in vxctmp
        double one = 1.0;
        int inc1 = 1, inc2 = 2;
        double* p = (double*) &tmpr[0];
        if ( j == 0 )
        {
Francois Gygi committed
367 368
          dcopy(&np012loc_,p  ,&inc2,&vxctmp[0][0],&inc1);
          dcopy(&np012loc_,p+1,&inc2,&vxctmp[1][0],&inc1);
Francois Gygi committed
369 370 371
        }
        else
        {
Francois Gygi committed
372 373
          daxpy(&np012loc_,&one,p  ,&inc2,&vxctmp[0][0],&inc1);
          daxpy(&np012loc_,&one,p+1,&inc2,&vxctmp[1][0],&inc1);
Francois Gygi committed
374 375 376
        }
      } // j
    }
377

378
    // xc potential vr[i]
Francois Gygi committed
379 380
    // div(vxc2*grad_rho) is stored in vxctmp[ispin][ir]

Francois Gygi committed
381
    double esum=0.0;
382
    double dsum=0.0;
Francois Gygi committed
383 384 385 386 387 388 389 390
    if ( nspin_ == 1 )
    {
      const double *const e = xcf_->exc;
      const double *const v1 = xcf_->vxc1;
      const double *const rh = xcf_->rho;
      {
        for ( int ir = 0; ir < np012loc_; ir++ )
        {
391 392 393 394 395
          const double e_i = e[ir];
          const double rh_i = rh[ir];
          const double v_i = v1[ir] + vxctmp[0][ir];
          esum += rh_i * e_i;
          dsum += rh_i * ( e_i - v_i );
396
          vr[0][ir] = v_i;
Francois Gygi committed
397 398 399 400 401 402 403 404 405 406 407
        }
      }
    }
    else
    {
      const double *const v1_up = xcf_->vxc1_up;
      const double *const v1_dn = xcf_->vxc1_dn;
      const double *const eup = xcf_->exc_up;
      const double *const edn = xcf_->exc_dn;
      const double *const rh_up = xcf_->rho_up;
      const double *const rh_dn = xcf_->rho_dn;
408
      for ( int ir = 0; ir < np012loc_; ir++ )
Francois Gygi committed
409
      {
410 411 412 413 414 415
        const double r_up_i = rh_up[ir];
        const double r_dn_i = rh_dn[ir];
        esum += r_up_i * eup[ir] + r_dn_i * edn[ir];
        const double v_up = v1_up[ir] + vxctmp[0][ir];
        const double v_dn = v1_dn[ir] + vxctmp[1][ir];
        dsum += r_up_i * ( eup[ir] - v_up ) + r_dn_i * ( edn[ir] - v_dn );
416 417
        vr[0][ir] = v_up;
        vr[1][ir] = v_dn;
Francois Gygi committed
418 419
      }
    }
420 421 422 423 424 425
    double sum[2], tsum[2];
    sum[0] = esum * vbasis_.cell().volume() / vft_.np012();
    sum[1] = dsum * vbasis_.cell().volume() / vft_.np012();
    MPI_Allreduce(&sum,&tsum,2,MPI_DOUBLE,MPI_SUM,vbasis_.comm());
    exc_ = tsum[0];
    dxc_ = tsum[1];
Francois Gygi committed
426
  }
Francois Gygi committed
427 428 429 430 431 432 433 434 435 436 437 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

  if ( isMeta() )
  {
    if ( nspin_ == 1 )
    {
      double sum = 0.0;
      const double *const v3 = xcf_->vxc3;
      const double *const tau = xcf_->tau;
      for ( int ir = 0; ir < np012loc_; ir++ )
        {
          sum += tau[ir] * v3[ir];
        }
      sum *= vbasis_.cell().volume() / vft_.np012();
      double tsum = 0.0;
      MPI_Allreduce(&sum,&tsum,1,MPI_DOUBLE,MPI_SUM,vbasis_.comm());
      dxc_ -= tsum;
    }
    else
    {
      double sum_up = 0.0;
      double sum_dn = 0.0;
      const double *const v3_up = xcf_->vxc3_up;
      const double *const v3_dn = xcf_->vxc3_dn;
      const double *const tau_up = xcf_->tau_up;
      const double *const tau_dn = xcf_->tau_dn;
      for ( int ir = 0; ir < np012loc_; ir++ )
        {
          sum_up += tau_up[ir] * v3_up[ir];
          sum_dn += tau_dn[ir] * v3_dn[ir];
        }
      sum_up *= vbasis_.cell().volume() / vft_.np012();
      sum_dn *= vbasis_.cell().volume() / vft_.np012();
      double tsum_up = 0.0;
      double tsum_dn = 0.0;
      MPI_Allreduce(&sum_up,&tsum_up,1,MPI_DOUBLE,MPI_SUM,vbasis_.comm());
      MPI_Allreduce(&sum_dn,&tsum_dn,1,MPI_DOUBLE,MPI_SUM,vbasis_.comm());
      dxc_ -= (tsum_up + tsum_dn);
    }
  }
Francois Gygi committed
466 467 468 469 470
}
////////////////////////////////////////////////////////////////////////////////
void XCPotential::compute_stress(valarray<double>& sigma_exc)
{
  // compute exchange-correlation contributions to the stress tensor
471

Francois Gygi committed
472
  if ( !isGGA() && !isMeta())
Francois Gygi committed
473 474
  {
    // LDA functional
475

476
    double dsum = 0.0;
Francois Gygi committed
477
    const double *const e = xcf_->exc;
Francois Gygi committed
478
    const int size = xcf_->np();
479

Francois Gygi committed
480 481 482 483 484 485 486
    if ( nspin_ == 1 )
    {
      // unpolarized
      const double *const rh = xcf_->rho;
      const double *const v = xcf_->vxc1;
      for ( int i = 0; i < size; i++ )
      {
487
        dsum += rh[i] * (e[i] - v[i]);
Francois Gygi committed
488 489 490 491 492 493 494 495 496
      }
    }
    else
    {
      // spin polarized
      const double *const rh_up = xcf_->rho_up;
      const double *const rh_dn = xcf_->rho_dn;
      const double *const v_up = xcf_->vxc1_up;
      const double *const v_dn = xcf_->vxc1_dn;
497 498 499
      for ( int i = 0; i < size; i++ )
      {
        const double rh = rh_up[i] + rh_dn[i];
500
        dsum += rh * e[i] - rh_up[i] * v_up[i] - rh_dn[i] * v_dn[i];
501
      }
Francois Gygi committed
502 503
    }
    const double fac = 1.0 / vft_.np012();
504
    double sum, tsum;
505
    // Next line: factor omega in volume element cancels 1/omega in
Francois Gygi committed
506
    // definition of sigma_exc
507
    sum = - fac * dsum;
508
    MPI_Allreduce(&sum,&tsum,1,MPI_DOUBLE,MPI_SUM,vbasis_.comm());
509

Francois Gygi committed
510 511 512 513 514 515 516 517
    // Note: contribution to sigma_exc is a multiple of the identity
    sigma_exc[0] = tsum;
    sigma_exc[1] = tsum;
    sigma_exc[2] = tsum;
    sigma_exc[3] = 0.0;
    sigma_exc[4] = 0.0;
    sigma_exc[5] = 0.0;
  }
Francois Gygi committed
518
  else if ( isGGA() && !isMeta())
Francois Gygi committed
519 520
  {
    // GGA functional
521

Francois Gygi committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    double dsum=0.0,sum0=0.0,sum1=0.0,sum2=0.0,
           sum3=0.0,sum4=0.0,sum5=0.0;
    if ( nspin_ == 1 )
    {
      const double *const e = xcf_->exc;
      const double *const v1 = xcf_->vxc1;
      const double *const v2 = xcf_->vxc2;
      const double *const rh = xcf_->rho;
      for ( int ir = 0; ir < np012loc_; ir++ )
      {
        dsum += rh[ir] * ( e[ir] - v1[ir] );
        const double grx = xcf_->grad_rho[0][ir];
        const double gry = xcf_->grad_rho[1][ir];
        const double grz = xcf_->grad_rho[2][ir];
        const double grx2 = grx * grx;
        const double gry2 = gry * gry;
        const double grz2 = grz * grz;
        const double grad2 = grx2 + gry2 + grz2;
        const double v2t = v2[ir];
        sum0 += ( grad2 + grx2 ) * v2t;
        sum1 += ( grad2 + gry2 ) * v2t;
        sum2 += ( grad2 + grz2 ) * v2t;
        sum3 += grx * gry * v2t;
        sum4 += gry * grz * v2t;
        sum5 += grx * grz * v2t;
      }
    }
    else
    {
      const double *const v1_up = xcf_->vxc1_up;
      const double *const v1_dn = xcf_->vxc1_dn;
      const double *const v2_upup = xcf_->vxc2_upup;
      const double *const v2_updn = xcf_->vxc2_updn;
      const double *const v2_dnup = xcf_->vxc2_dnup;
      const double *const v2_dndn = xcf_->vxc2_dndn;
      const double *const eup = xcf_->exc_up;
      const double *const edn = xcf_->exc_dn;
      const double *const rh_up = xcf_->rho_up;
      const double *const rh_dn = xcf_->rho_dn;
      for ( int ir = 0; ir < np012loc_; ir++ )
      {
        const double r_up = rh_up[ir];
        const double r_dn = rh_dn[ir];
        dsum += r_up * ( eup[ir] - v1_up[ir] ) +
                r_dn * ( edn[ir] - v1_dn[ir] );
567

Francois Gygi committed
568 569 570 571 572 573 574
        const double grx_up = xcf_->grad_rho_up[0][ir];
        const double gry_up = xcf_->grad_rho_up[1][ir];
        const double grz_up = xcf_->grad_rho_up[2][ir];
        const double grx2_up = grx_up * grx_up;
        const double gry2_up = gry_up * gry_up;
        const double grz2_up = grz_up * grz_up;
        const double grad2_up = grx2_up + gry2_up + grz2_up;
575

Francois Gygi committed
576 577 578 579 580 581 582
        const double grx_dn = xcf_->grad_rho_dn[0][ir];
        const double gry_dn = xcf_->grad_rho_dn[1][ir];
        const double grz_dn = xcf_->grad_rho_dn[2][ir];
        const double grx2_dn = grx_dn * grx_dn;
        const double gry2_dn = gry_dn * gry_dn;
        const double grz2_dn = grz_dn * grz_dn;
        const double grad2_dn = grx2_dn + gry2_dn + grz2_dn;
583

Francois Gygi committed
584 585 586
        const double grad_up_grad_dn = grx_up * grx_dn +
                                       gry_up * gry_dn +
                                       grz_up * grz_dn;
Francois Gygi committed
587

Francois Gygi committed
588 589 590 591
        const double v2_upup_ir = v2_upup[ir];
        const double v2_updn_ir = v2_updn[ir];
        const double v2_dnup_ir = v2_dnup[ir];
        const double v2_dndn_ir = v2_dndn[ir];
592

Francois Gygi committed
593 594 595 596
        sum0 += v2_upup_ir * ( grad2_up + grx2_up ) +
                v2_updn_ir * ( grad_up_grad_dn + grx_up * grx_dn ) +
                v2_dnup_ir * ( grad_up_grad_dn + grx_dn * grx_up ) +
                v2_dndn_ir * ( grad2_dn + grx2_dn );
597

Francois Gygi committed
598 599 600 601
        sum1 += v2_upup_ir * ( grad2_up + gry2_up ) +
                v2_updn_ir * ( grad_up_grad_dn + gry_up * gry_dn ) +
                v2_dnup_ir * ( grad_up_grad_dn + gry_dn * gry_up ) +
                v2_dndn_ir * ( grad2_dn + gry2_dn );
602

Francois Gygi committed
603 604 605 606
        sum2 += v2_upup_ir * ( grad2_up + grz2_up ) +
                v2_updn_ir * ( grad_up_grad_dn + grz_up * grz_dn ) +
                v2_dnup_ir * ( grad_up_grad_dn + grz_dn * grz_up ) +
                v2_dndn_ir * ( grad2_dn + grz2_dn );
607

Francois Gygi committed
608 609 610 611
        sum3 += v2_upup_ir * grx_up * gry_up +
                v2_updn_ir * grx_up * gry_dn +
                v2_dnup_ir * grx_dn * gry_up +
                v2_dndn_ir * grx_dn * gry_dn;
612

Francois Gygi committed
613 614 615 616
        sum4 += v2_upup_ir * gry_up * grz_up +
                v2_updn_ir * gry_up * grz_dn +
                v2_dnup_ir * gry_dn * grz_up +
                v2_dndn_ir * gry_dn * grz_dn;
617

Francois Gygi committed
618 619 620 621
        sum5 += v2_upup_ir * grx_up * grz_up +
                v2_updn_ir * grx_up * grz_dn +
                v2_dnup_ir * grx_dn * grz_up +
                v2_dndn_ir * grx_dn * grz_dn;
Francois Gygi committed
622 623
      }
    }
624
    double fac = 1.0 / vft_.np012();
625
    double sum[6],tsum[6];
626
    // Next line: factor omega in volume element cancels 1/omega in
627
    // definition of sigma_exc
628 629 630 631 632 633 634
    sum[0] = - fac * ( dsum + sum0 );
    sum[1] = - fac * ( dsum + sum1 );
    sum[2] = - fac * ( dsum + sum2 );
    sum[3] = - fac * sum3;
    sum[4] = - fac * sum4;
    sum[5] = - fac * sum5;
    MPI_Allreduce(sum,tsum,6,MPI_DOUBLE,MPI_SUM,vbasis_.comm());
635

Francois Gygi committed
636 637 638 639 640 641
    sigma_exc[0] = tsum[0];
    sigma_exc[1] = tsum[1];
    sigma_exc[2] = tsum[2];
    sigma_exc[3] = tsum[3];
    sigma_exc[4] = tsum[4];
    sigma_exc[5] = tsum[5];
Francois Gygi committed
642
  }
Francois Gygi committed
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 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
  else
  {
    // MetaGGA functional
    double dsum=0.0,sum0=0.0,sum1=0.0,sum2=0.0,
           sum3=0.0,sum4=0.0,sum5=0.0;
    if ( nspin_ == 1 )
    {
      const double *const e = xcf_->exc;
      const double *const v1 = xcf_->vxc1;
      const double *const v2 = xcf_->vxc2;
      const double *const v3 = xcf_->vxc3;
      const double *const rh = xcf_->rho;
      const double *const tau = xcf_->tau;

      //V3 Stress -V3 * sum_n[grad_i rho_n * grad_j rho_n]
      const Wavefunction& wf0 = s_.wf;

      std::vector<std::complex<double> > tmprx(np012loc_),tmpry(np012loc_),
                                         tmprz(np012loc_);
      std::vector<double> v3txx(np012loc_), v3txy(np012loc_),v3txz(np012loc_),
                          v3tyy(np012loc_), v3tyz(np012loc_),v3tzz(np012loc_);
      for ( int ispin = 0; ispin < wf0.nspin(); ispin++ )
      {
        for ( int ikp = 0; ikp < wf0.nkp(); ikp++ )
        {
          double omega_inv = 1.0 / wf0.sd(ispin,ikp)->basis().cell().volume();
          if ( wf0.sd(ispin,ikp)->basis().real() )
          {
            const int ngwloc = wf0.sd(ispin,ikp)->basis().localsize();
            vector<complex<double> > tmp0(ngwloc);
            const int mloc = wf0.sd(ispin,ikp)->c().mloc();
            const complex<double>* p = wf0.sd(ispin,ikp)->c().cvalptr();
            const double *const gxjx = wf0.sd(ispin,ikp)->basis().gx_ptr(0);
            const double *const gxjy = wf0.sd(ispin,ikp)->basis().gx_ptr(1);
            const double *const gxjz = wf0.sd(ispin,ikp)->basis().gx_ptr(2);
            for ( int n = 0; n < wf0.sd(ispin,ikp)->nstloc()-1; n++, n++ )
            {
              double nn = wf0.sd(ispin,ikp)->context().mycol() *
                          wf0.sd(ispin,ikp)->c().nb() + n;
              double occ = wf0.sd(ispin,ikp)->occ(nn);
              if (occ > 0.0)
              {
                // Compute Grad_j psi_n(ikp)
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjx[ig]) * p[ig+n*mloc];
                  tmp1[ig] = complex<double>(0.0,gxjx[ig]) * p[ig+(n+1)*mloc];
                }
                // Compute Grad_x psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmprx[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjy[ig]) * p[ig+n*mloc];
                  tmp1[ig] = complex<double>(0.0,gxjy[ig]) * p[ig+(n+1)*mloc];
                }
                // Compute Grad_y psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmpry[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjz[ig]) * p[ig+n*mloc];
                  tmp1[ig] = complex<double>(0.0,gxjz[ig]) * p[ig+(n+1)*mloc];
                }
                // Compute Grad_z psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmprz[0]);

                // Compute V3 * Grad_j1 psi_n(ikp) * Grad_j2 psi_n(ikp)
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txx[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprx[i].real() +
                               tmprx[i].imag() * tmprx[i].imag());
                  v3tyy[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmpry[i].real() +
                               tmpry[i].imag() * tmpry[i].imag());
                  v3tzz[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprz[i].real() * tmprz[i].real() +
                               tmprz[i].imag() * tmprz[i].imag());
                  v3txy[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmpry[i].real() +
                               tmprx[i].imag() * tmpry[i].imag());
                  v3txz[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprz[i].real() +
                               tmprx[i].imag() * tmprz[i].imag());
                  v3tyz[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmprz[i].real() +
                               tmpry[i].imag() * tmprz[i].imag());
                }
              }
            }
            if ( wf0.sd(ispin,ikp)->nstloc() % 2 != 0 )
            {
              const int n = wf0.sd(ispin,ikp)->nstloc()-1;
              double nn = wf0.sd(ispin,ikp)->context().mycol() *
                          wf0.sd(ispin,ikp)->c().nb() + n;
              double occ = wf0.sd(ispin,ikp)->occ(nn);
              if (occ > 0.0)
              {
                // Compute Grad_j psi_n(ikp)
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjx[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_x psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprx[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjy[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_y psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmpry[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjz[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_z psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprz[0]);

                // Compute V3 * Grad_j1 psi_n(ikp) * Grad_j2 psi_n(ikp)
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txx[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprx[i].real());
                  v3tyy[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmpry[i].real());
                  v3tzz[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprz[i].real() * tmprz[i].real());
                  v3txy[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmpry[i].real());
                  v3txz[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprz[i].real());
                  v3tyz[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmprz[i].real());
                }
              }
            }
          }
          else
          {
            const int ngwloc = wf0.sd(ispin,ikp)->basis().localsize();
            vector<complex<double> > tmp0(ngwloc);
            const int mloc = wf0.sd(ispin,ikp)->c().mloc();
            const complex<double>* p = wf0.sd(ispin,ikp)->c().cvalptr();
            const double *const kpgxjx = wf0.sd(ispin,ikp)->basis().kpgx_ptr(0);
            const double *const kpgxjy = wf0.sd(ispin,ikp)->basis().kpgx_ptr(1);
            const double *const kpgxjz = wf0.sd(ispin,ikp)->basis().kpgx_ptr(2);
            for ( int n = 0; n < wf0.sd(ispin,ikp)->nstloc(); n++)
            {
              double nn = wf0.sd(ispin,ikp)->context().mycol() *
                          wf0.sd(ispin,ikp)->c().nb() + n;
              double occ = wf0.sd(ispin,ikp)->occ(nn);
              if (occ > 0.0)
              {
                // Compute Grad_j psi_n(ikp)
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,kpgxjx[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_x psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprx[0]);

                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,kpgxjy[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_y psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmpry[0]);

                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,kpgxjz[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_z psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprz[0]);

                // Compute V3 * Grad_j1 psi_n(ikp) * Grad_j2 psi_n(ikp)
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txx[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmprx[i]);
                  v3tyy[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmpry[i]) * tmpry[i]);
                  v3tzz[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprz[i]) * tmprz[i]);
                  v3txy[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmpry[i]);
                  v3txz[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmprz[i]);
                  v3tyz[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmpry[i]) * tmprz[i]);
                }
              }
            }
          }
        }
      }
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txx[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tyy[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tzz[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txy[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txz[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tyz[0],np012loc_);
      for ( int ir = 0; ir < np012loc_; ir++ )
      {
        //V1 Stress rho * (Exc - V1)
        dsum += rh[ir] * (e[ir] - v1[ir] );
        //V2 Stress V2 * (delta_ij * grad^2 + grad_i * grad_j)
        const double grx = xcf_->grad_rho[0][ir];
        const double gry = xcf_->grad_rho[1][ir];
        const double grz = xcf_->grad_rho[2][ir];
        const double grx2 = grx * grx;
        const double gry2 = gry * gry;
        const double grz2 = grz * grz;
        const double grad2 = grx2 + gry2 + grz2;
        const double v2t = v2[ir];
        const double v3t = v3[ir];
        const double taur = tau[ir];
        sum0 += ( grad2 + grx2 ) * v2t - v3t * (taur + v3txx[ir]);
        sum1 += ( grad2 + gry2 ) * v2t - v3t * (taur + v3tyy[ir]);
        sum2 += ( grad2 + grz2 ) * v2t - v3t * (taur + v3tzz[ir]);
        sum3 += grx * gry * v2t - v3t * (v3txy[ir]);
        sum4 += gry * grz * v2t - v3t * (v3tyz[ir]);
        sum5 += grx * grz * v2t - v3t * (v3txz[ir]);
      }
    }
    else
    {
      const double *const v1_up = xcf_->vxc1_up;
      const double *const v1_dn = xcf_->vxc1_dn;
      const double *const v2_upup = xcf_->vxc2_upup;
      const double *const v2_updn = xcf_->vxc2_updn;
      const double *const v2_dnup = xcf_->vxc2_dnup;
      const double *const v2_dndn = xcf_->vxc2_dndn;
      const double *const v3_up = xcf_->vxc3_up;
      const double *const v3_dn = xcf_->vxc3_dn;
      const double *const eup = xcf_->exc_up;
      const double *const edn = xcf_->exc_dn;
      const double *const rh_up = xcf_->rho_up;
      const double *const rh_dn = xcf_->rho_dn;
      const double *const tau_up = xcf_->tau_up;
      const double *const tau_dn = xcf_->tau_dn;

      //V3 Stress -V3 * sum_n[grad_i rho_n * grad_j rho_n]
      const Wavefunction& wf0 = s_.wf;

      std::vector<std::complex<double> > tmprx(np012loc_),tmpry(np012loc_),
                                         tmprz(np012loc_);
      std::vector<double> v3txxup(np012loc_), v3txyup(np012loc_),
                          v3txzup(np012loc_), v3tyyup(np012loc_),
                          v3tyzup(np012loc_), v3tzzup(np012loc_),
                          v3txxdn(np012loc_), v3txydn(np012loc_),
                          v3txzdn(np012loc_), v3tyydn(np012loc_),
                          v3tyzdn(np012loc_), v3tzzdn(np012loc_);
      for ( int ispin = 0; ispin < wf0.nspin(); ispin++ )
      {
        for ( int ikp = 0; ikp < wf0.nkp(); ikp++ )
        {
          double omega_inv = 1.0 / wf0.sd(ispin,ikp)->basis().cell().volume();
          if ( wf0.sd(ispin,ikp)->basis().real() )
          {
            const int ngwloc = wf0.sd(ispin,ikp)->basis().localsize();
            vector<complex<double> > tmp0(ngwloc);
            const int mloc = wf0.sd(ispin,ikp)->c().mloc();
            const complex<double>* p = wf0.sd(ispin,ikp)->c().cvalptr();
            const double *const gxjx = wf0.sd(ispin,ikp)->basis().gx_ptr(0);
            const double *const gxjy = wf0.sd(ispin,ikp)->basis().gx_ptr(1);
            const double *const gxjz = wf0.sd(ispin,ikp)->basis().gx_ptr(2);
            for ( int n = 0; n < wf0.sd(ispin,ikp)->nstloc()-1; n++, n++ )
            {
              double nn = wf0.sd(ispin,ikp)->context().mycol() *
                          wf0.sd(ispin,ikp)->c().nb() + n;
              double occ = wf0.sd(ispin,ikp)->occ(nn);
              if (occ > 0.0)
              {
                // Compute Grad_j psi_n(ikp)
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjx[ig]) * p[ig+n*mloc];
                  tmp1[ig] = complex<double>(0.0,gxjx[ig]) * p[ig+(n+1)*mloc];
                }
                // Compute Grad_x psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmprx[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjy[ig]) * p[ig+n*mloc];
                  tmp1[ig] = complex<double>(0.0,gxjy[ig]) * p[ig+(n+1)*mloc];
                }
                // Compute Grad_y psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmpry[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjz[ig]) * p[ig+n*mloc];
                  tmp1[ig] = complex<double>(0.0,gxjz[ig]) * p[ig+(n+1)*mloc];
                }
                // Compute Grad_z psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmprz[0]);

                // Compute V3 * Grad_j1 psi_n(ikp) * Grad_j2 psi_n(ikp)
                if(ispin==0)
                {
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txxup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprx[i].real() +
                               tmprx[i].imag() * tmprx[i].imag());
                  v3tyyup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmpry[i].real() +
                               tmpry[i].imag() * tmpry[i].imag());
                  v3tzzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprz[i].real() * tmprz[i].real() +
                               tmprz[i].imag() * tmprz[i].imag());
                  v3txyup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmpry[i].real() +
                               tmprx[i].imag() * tmpry[i].imag());
                  v3txzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprz[i].real() +
                               tmprx[i].imag() * tmprz[i].imag());
                  v3tyzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmprz[i].real() +
                               tmpry[i].imag() * tmprz[i].imag());
                }
                }
                else if(ispin==1)
                {
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txxdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprx[i].real() +
                               tmprx[i].imag() * tmprx[i].imag());
                  v3tyydn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmpry[i].real() +
                               tmpry[i].imag() * tmpry[i].imag());
                  v3tzzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprz[i].real() * tmprz[i].real() +
                               tmprz[i].imag() * tmprz[i].imag());
                  v3txydn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmpry[i].real() +
                               tmprx[i].imag() * tmpry[i].imag());
                  v3txzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprz[i].real() +
                               tmprx[i].imag() * tmprz[i].imag());
                  v3tyzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmprz[i].real() +
                               tmpry[i].imag() * tmprz[i].imag());
                }
                }
              }
            }
            if ( wf0.sd(ispin,ikp)->nstloc() % 2 != 0 )
            {
              const int n = wf0.sd(ispin,ikp)->nstloc()-1;
              double nn = wf0.sd(ispin,ikp)->context().mycol() *
                          wf0.sd(ispin,ikp)->c().nb() + n;
              double occ = wf0.sd(ispin,ikp)->occ(nn);
              if (occ > 0.0)
              {
                // Compute Grad_j psi_n(ikp)
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjx[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_x psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprx[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjy[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_y psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmpry[0]);
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,gxjz[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_z psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprz[0]);

                // Compute V3 * Grad_j1 psi_n(ikp) * Grad_j2 psi_n(ikp)
                if(ispin==0)
                {
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txxup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprx[i].real());
                  v3tyyup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmpry[i].real());
                  v3tzzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprz[i].real() * tmprz[i].real());
                  v3txyup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmpry[i].real());
                  v3txzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprz[i].real());
                  v3tyzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmprz[i].real());
                }
                }
                else if(ispin==1)
                {
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txxdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprx[i].real());
                  v3tyydn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmpry[i].real());
                  v3tzzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprz[i].real() * tmprz[i].real());
                  v3txydn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmpry[i].real());
                  v3txzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmprx[i].real() * tmprz[i].real());
                  v3tyzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              (tmpry[i].real() * tmprz[i].real());
                }
                }
              }
            }
          }
          else
          {
            const int ngwloc = wf0.sd(ispin,ikp)->basis().localsize();
            vector<complex<double> > tmp0(ngwloc);
            const int mloc = wf0.sd(ispin,ikp)->c().mloc();
            const complex<double>* p = wf0.sd(ispin,ikp)->c().cvalptr();
            const double *const kpgxjx = wf0.sd(ispin,ikp)->basis().kpgx_ptr(0);
            const double *const kpgxjy = wf0.sd(ispin,ikp)->basis().kpgx_ptr(1);
            const double *const kpgxjz = wf0.sd(ispin,ikp)->basis().kpgx_ptr(2);
            for ( int n = 0; n < wf0.sd(ispin,ikp)->nstloc(); n++)
            {
              double nn = wf0.sd(ispin,ikp)->context().mycol() *
                          wf0.sd(ispin,ikp)->c().nb() + n;
              double occ = wf0.sd(ispin,ikp)->occ(nn);
              if (occ > 0.0)
              {
                // Compute Grad_j psi_n(ikp)
                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,kpgxjx[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_x psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprx[0]);

                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,kpgxjy[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_y psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmpry[0]);

                for ( int ig = 0; ig < ngwloc; ig++ )
                {
                  /* i*G_j1*c(G) */
                  tmp0[ig] = complex<double>(0.0,kpgxjz[ig]) * p[ig+n*mloc];
                }
                // Compute Grad_z psi_n(ikp)
                cd_.ft(ikp)->backward(&tmp0[0],&tmprz[0]);

                // Compute V3 * Grad_j1 psi_n(ikp) * Grad_j2 psi_n(ikp)
                if(ispin==0)
                {
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txxup[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmprx[i]);
                  v3tyyup[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmpry[i]) * tmpry[i]);
                  v3tzzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprz[i]) * tmprz[i]);
                  v3txyup[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmpry[i]);
                  v3txzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmprz[i]);
                  v3tyzup[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmpry[i]) * tmprz[i]);
                }
                }
                else if(ispin==1)
                {
                for ( int i = 0; i < np012loc_; i++ )
                {
                  v3txxdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmprx[i]);
                  v3tyydn[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmpry[i]) * tmpry[i]);
                  v3tzzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprz[i]) * tmprz[i]);
                  v3txydn[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmpry[i]);
                  v3txzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmprx[i]) * tmprz[i]);
                  v3tyzdn[i] += wf0.weight(ikp) * omega_inv * occ *
                              std::real(std::conj(tmpry[i]) * tmprz[i]);
                }
                }
              }
            }
          }
        }
      }
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txxup[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tyyup[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tzzup[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txyup[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txzup[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tyzup[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txxdn[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tyydn[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tzzdn[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txydn[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3txzdn[0],np012loc_);
      wf0.kpcontext()->dsum('r',np012loc_,1,&v3tyzdn[0],np012loc_);

      for ( int ir = 0; ir < np012loc_; ir++ )
      {
        const double r_up = rh_up[ir];
        const double r_dn = rh_dn[ir];
        dsum += r_up * ( eup[ir] - v1_up[ir] ) +
                r_dn * ( edn[ir] - v1_dn[ir] );

        const double grx_up = xcf_->grad_rho_up[0][ir];
        const double gry_up = xcf_->grad_rho_up[1][ir];
        const double grz_up = xcf_->grad_rho_up[2][ir];
        const double grx2_up = grx_up * grx_up;
        const double gry2_up = gry_up * gry_up;
        const double grz2_up = grz_up * grz_up;
        const double grad2_up = grx2_up + gry2_up + grz2_up;

        const double grx_dn = xcf_->grad_rho_dn[0][ir];
        const double gry_dn = xcf_->grad_rho_dn[1][ir];
        const double grz_dn = xcf_->grad_rho_dn[2][ir];
        const double grx2_dn = grx_dn * grx_dn;
        const double gry2_dn = gry_dn * gry_dn;
        const double grz2_dn = grz_dn * grz_dn;
        const double grad2_dn = grx2_dn + gry2_dn + grz2_dn;

        const double grad_up_grad_dn = grx_up * grx_dn +
                                       gry_up * gry_dn +
                                       grz_up * grz_dn;

        const double v2_upup_ir = v2_upup[ir];
        const double v2_updn_ir = v2_updn[ir];
        const double v2_dnup_ir = v2_dnup[ir];
        const double v2_dndn_ir = v2_dndn[ir];
        const double v3tup = v3_up[ir];
        const double v3tdn = v3_dn[ir];
        const double taurup = tau_up[ir];
        const double taurdn = tau_dn[ir];

        sum0 += v2_upup_ir * ( grad2_up + grx2_up ) +
                v2_updn_ir * ( grad_up_grad_dn + grx_up * grx_dn ) +
                v2_dnup_ir * ( grad_up_grad_dn + grx_dn * grx_up ) +
                v2_dndn_ir * ( grad2_dn + grx2_dn ) -
                v3tup * (taurup + v3txxup[ir]) - v3tdn * (taurdn + v3txxdn[ir]);

        sum1 += v2_upup_ir * ( grad2_up + gry2_up ) +
                v2_updn_ir * ( grad_up_grad_dn + gry_up * gry_dn ) +
                v2_dnup_ir * ( grad_up_grad_dn + gry_dn * gry_up ) +
                v2_dndn_ir * ( grad2_dn + gry2_dn ) -
                v3tup * (taurup + v3tyyup[ir]) - v3tdn * (taurdn + v3tyydn[ir]);

        sum2 += v2_upup_ir * ( grad2_up + grz2_up ) +
                v2_updn_ir * ( grad_up_grad_dn + grz_up * grz_dn ) +
                v2_dnup_ir * ( grad_up_grad_dn + grz_dn * grz_up ) +
                v2_dndn_ir * ( grad2_dn + grz2_dn ) -
                v3tup * (taurup + v3tzzup[ir]) - v3tdn * (taurdn + v3tzzdn[ir]);

        sum3 += v2_upup_ir * grx_up * gry_up +
                v2_updn_ir * grx_up * gry_dn +
                v2_dnup_ir * grx_dn * gry_up +
                v2_dndn_ir * grx_dn * gry_dn -
                v3tup * (v3txyup[ir]) - v3tdn * (v3txydn[ir]);

        sum4 += v2_upup_ir * gry_up * grz_up +
                v2_updn_ir * gry_up * grz_dn +
                v2_dnup_ir * gry_dn * grz_up +
                v2_dndn_ir * gry_dn * grz_dn -
                v3tup * (v3tyzup[ir]) - v3tdn * (v3tyzdn[ir]);

        sum5 += v2_upup_ir * grx_up * grz_up +
                v2_updn_ir * grx_up * grz_dn +
                v2_dnup_ir * grx_dn * grz_up +
                v2_dndn_ir * grx_dn * grz_dn -
                v3tup * (v3txzup[ir]) - v3tdn * (v3txzdn[ir]);
      }
    }
    double fac = 1.0 / vft_.np012();
    double sum[6],tsum[6];
    // Next line: factor omega in volume element cancels 1/omega in
    // definition of sigma_exc
    sum[0] = - fac * ( dsum + sum0 );
    sum[1] = - fac * ( dsum + sum1 );
    sum[2] = - fac * ( dsum + sum2 );
    sum[3] = - fac * sum3;
    sum[4] = - fac * sum4;
    sum[5] = - fac * sum5;
    MPI_Allreduce(sum,tsum,6,MPI_DOUBLE,MPI_SUM,vbasis_.comm());

    sigma_exc[0] = tsum[0];
    sigma_exc[1] = tsum[1];
    sigma_exc[2] = tsum[2];
    sigma_exc[3] = tsum[3];
    sigma_exc[4] = tsum[4];
    sigma_exc[5] = tsum[5];
  }
}

////////////////////////////////////////////////////////////////////////////////
void XCPotential::apply_meta_operator(Wavefunction& dwf)
{
  const Wavefunction& wf0 = s_.wf;

  if (wf0.nspin() == 1)
  {
    for ( int ispin = 0; ispin < wf0.nspin(); ispin++ )
    {
      for ( int ikp = 0; ikp < wf0.nkp(); ikp++ )
      {
        if ( wf0.sd(ispin,ikp)->basis().real() )
        {
          const int ngwloc = wf0.sd(ispin,ikp)->basis().localsize();
          vector<complex<double> > tmp0(ngwloc);
          const int mloc = wf0.sd(ispin,ikp)->c().mloc();
          const complex<double>* p = wf0.sd(ispin,ikp)->c().cvalptr();
          complex<double>* dp = dwf.sd(ispin,ikp)->c().valptr();
          for ( int n = 0; n < wf0.sd(ispin,ikp)->nstloc()-1; n++, n++ )
          {
            for ( int j = 0; j < 3; j++ )
            {
              // Compute Grad_j psi_n(ikp)
              const double *const gxj = wf0.sd(ispin,ikp)->basis().gx_ptr(j);
              for ( int ig = 0; ig < ngwloc; ig++ )
              {
                /* i*G_j*c(G) */
                tmp0[ig] = complex<double>(0.0,gxj[ig]) * p[ig+n*mloc];
                tmp1[ig] = complex<double>(0.0,gxj[ig]) * p[ig+(n+1)*mloc];
              }
              cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmpr[0]);
              // Compute V3 * Grad_j psi_n(ikp)
              for ( int i = 0; i < np012loc_; i++ )
                tmpr[i] *= xcf_->vxc3[i];
              // Transform to k-space
              cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0],&tmp1[0]);
              // Compute Div_j[V3 * Grad_j psi_n(ikp)]
              // Note -1/2 comes from definition of V3
              for ( int ig = 0; ig < ngwloc; ig++ )
              {
                /* i*G_j*c(G) */
                dp[ig+n*mloc] += -0.5 * complex<double>(0.0,gxj[ig]) * tmp0[ig];
                dp[ig+(n+1)*mloc] += -0.5 * complex<double>(0.0,gxj[ig]) *
                                     tmp1[ig];
              }
            }
          }
          if ( wf0.sd(ispin,ikp)->nstloc() % 2 != 0 )
          {
            const int n = wf0.sd(ispin,ikp)->nstloc()-1;
            for ( int j = 0; j < 3; j++ )
            {
              const double *const gxj = wf0.sd(ispin,ikp)->basis().gx_ptr(j);
              for ( int ig = 0; ig < ngwloc; ig++ )
              {
                tmp0[ig] = complex<double>(0.0,gxj[ig]) * p[ig+n*mloc];
              }
              cd_.ft(ikp)->backward(&tmp0[0],&tmpr[0]);
              for ( int i = 0; i < np012loc_; i++ )
                tmpr[i] *= xcf_->vxc3[i];
              cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0]);
              for ( int ig = 0; ig < ngwloc; ig++ )
              {
                dp[ig+n*mloc] += -0.5 * complex<double>(0.0,gxj[ig]) * tmp0[ig];
              }
            }
          }
        }
        else
        {
          const int ngwloc = wf0.sd(ispin,ikp)->basis().localsize();
          vector<complex<double> > tmp0(ngwloc);
          const int mloc = wf0.sd(ispin,ikp)->c().mloc();
          const complex<double>* p = wf0.sd(ispin,ikp)->c().cvalptr();
          complex<double>* dp = dwf.sd(ispin,ikp)->c().valptr();
          for ( int n = 0; n < wf0.sd(ispin,ikp)->nstloc(); n++ )
          {
            for ( int j = 0; j < 3; j++ )
            {
              // Compute Grad_j psi_n(ikp)
              const double *const kpgxj =
                                  wf0.sd(ispin,ikp)->basis().kpgx_ptr(j);
              for ( int ig = 0; ig < ngwloc; ig++ )
              {
                // i*(k+G)_j*c(G)
                tmp0[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+n*mloc];
              }
              cd_.ft(ikp)->backward(&tmp0[0],&tmpr[0]);
              // Compute V3 * Grad_j psi_n(ikp)
              for ( int i = 0; i < np012loc_; i++ )
                tmpr[i] *= xcf_->vxc3[i];
              // Transform to k-space
              cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0]);
              // Compute Div_j[V3 * Grad_j psi_n(ikp)]
              // Note -1/2 comes from definition of V3
              for ( int ig = 0; ig < ngwloc; ig++ )
              {
                // i*(k+G)_j*c(G)
                dp[ig+n*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) *
                                 tmp0[ig];
              }
            }
          }
        }
      }
    }
  }
  else
  {
    //Tau_up first
    for ( int ikp = 0; ikp < wf0.nkp(); ikp++ )
    {
      if ( wf0.sd(0,ikp)->basis().real() )
      {
        const int ngwloc = wf0.sd(0,ikp)->basis().localsize();
        vector<complex<double> > tmp0(ngwloc);
        const int mloc = wf0.sd(0,ikp)->c().mloc();
        const complex<double>* p = wf0.sd(0,ikp)->c().cvalptr();
        complex<double>* dp = dwf.sd(0,ikp)->c().valptr();
        for ( int n = 0; n < wf0.sd(0,ikp)->nstloc()-1; n++, n++ )
        {
          for ( int j = 0; j < 3; j++ )
          {
            // Compute Grad_j psi_n(ikp)
            const double *const kpgxj = wf0.sd(0,ikp)->basis().kpgx_ptr(j);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              /* i*G_j*c(G) */
              tmp0[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+n*mloc];
              tmp1[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+(n+1)*mloc];
            }
            cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmpr[0]);
            // Compute V3 * Grad_j psi_n(ikp)
            for ( int i = 0; i < np012loc_; i++ )
              tmpr[i] *= xcf_->vxc3_up[i];
            // Transform to k-space
            cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0],&tmp1[0]);
            // Compute Div_j[V3 * Grad_j psi_n(ikp)]
            // Note -1/2 comes from definition of V3
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              /* i*G_j*c(G) */
              dp[ig+n*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) * tmp0[ig];
              dp[ig+(n+1)*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) *
                                   tmp1[ig];
            }
          }
        }
        if ( wf0.sd(0,ikp)->nstloc() % 2 != 0 )
        {
          const int n = wf0.sd(0,ikp)->nstloc()-1;
          for ( int j = 0; j < 3; j++ )
          {
            const double *const kpgxj = wf0.sd(0,ikp)->basis().kpgx_ptr(j);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              tmp0[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+n*mloc];
            }
            cd_.ft(ikp)->backward(&tmp0[0],&tmpr[0]);
            for ( int i = 0; i < np012loc_; i++ )
              tmpr[i] *= xcf_->vxc3_up[i];
            cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0]);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              dp[ig+n*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) * tmp0[ig];
            }
          }
        }
      }
      else
      {
        const int ngwloc = wf0.sd(0,ikp)->basis().localsize();
        vector<complex<double> > tmp0(ngwloc);
        const int mloc = wf0.sd(0,ikp)->c().mloc();
        const complex<double>* p = wf0.sd(0,ikp)->c().cvalptr();
        complex<double>* dp = dwf.sd(0,ikp)->c().valptr();
        for ( int n = 0; n < wf0.sd(0,ikp)->nstloc(); n++ )
        {
          for ( int j = 0; j < 3; j++ )
          {
            // Compute Grad_j psi_n(ikp)
            const double *const kpgxj = wf0.sd(0,ikp)->basis().kpgx_ptr(j);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              // i*(k+G)_j*c(G)
              tmp0[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+n*mloc];
            }
            cd_.ft(ikp)->backward(&tmp0[0],&tmpr[0]);
            // Compute V3 * Grad_j psi_n(ikp)
            for ( int i = 0; i < np012loc_; i++ )
              tmpr[i] *= xcf_->vxc3_up[i];
            // Transform to k-space
            cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0]);
            // Compute Div_j[V3 * Grad_j psi_n(ikp)]
            // Note -1/2 comes from definition of V3
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              // i*(k+G)_j*c(G)
              dp[ig+n*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) * tmp0[ig];
            }
          }
        }
      }
    }
    //Tau_dn second
    for ( int ikp = 0; ikp < wf0.nkp(); ikp++ )
    {
      if ( wf0.sd(1,ikp)->basis().real() )
      {
        const int ngwloc = wf0.sd(1,ikp)->basis().localsize();
        vector<complex<double> > tmp0(ngwloc);
        const int mloc = wf0.sd(1,ikp)->c().mloc();
        const complex<double>* p = wf0.sd(1,ikp)->c().cvalptr();
        complex<double>* dp = dwf.sd(1,ikp)->c().valptr();
        for ( int n = 0; n < wf0.sd(1,ikp)->nstloc()-1; n++, n++ )
        {
          for ( int j = 0; j < 3; j++ )
          {
            // Compute Grad_j psi_n(ikp)
            const double *const kpgxj = wf0.sd(1,ikp)->basis().kpgx_ptr(j);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              /* i*G_j*c(G) */
              tmp0[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+n*mloc];
              tmp1[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+(n+1)*mloc];
            }
            cd_.ft(ikp)->backward(&tmp0[0],&tmp1[0],&tmpr[0]);
            // Compute V3 * Grad_j psi_n(ikp)
            for ( int i = 0; i < np012loc_; i++ )
              tmpr[i] *= xcf_->vxc3_dn[i];
            // Transform to k-space
            cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0],&tmp1[0]);
            // Compute Div_j[V3 * Grad_j psi_n(ikp)]
            // Note -1/2 comes from definition of V3
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              /* i*G_j*c(G) */
              dp[ig+n*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) * tmp0[ig];
              dp[ig+(n+1)*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) *
                                   tmp1[ig];
            }
          }
        }
        if ( wf0.sd(1,ikp)->nstloc() % 2 != 0 )
        {
          const int n = wf0.sd(1,ikp)->nstloc()-1;
          for ( int j = 0; j < 3; j++ )
          {
            const double *const kpgxj = wf0.sd(1,ikp)->basis().kpgx_ptr(j);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              tmp0[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+n*mloc];
            }
            cd_.ft(ikp)->backward(&tmp0[0],&tmpr[0]);
            for ( int i = 0; i < np012loc_; i++ )
              tmpr[i] *= xcf_->vxc3_dn[i];
            cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0]);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              dp[ig+n*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) * tmp0[ig];
            }
          }
        }
      }
      else
      {
        const int ngwloc = wf0.sd(1,ikp)->basis().localsize();
        vector<complex<double> > tmp0(ngwloc);
        const int mloc = wf0.sd(1,ikp)->c().mloc();
        const complex<double>* p = wf0.sd(1,ikp)->c().cvalptr();
        complex<double>* dp = dwf.sd(1,ikp)->c().valptr();
        for ( int n = 0; n < wf0.sd(1,ikp)->nstloc(); n++ )
        {
          for ( int j = 0; j < 3; j++ )
          {
            // Compute Grad_j psi_n(ikp)
            const double *const kpgxj = wf0.sd(1,ikp)->basis().kpgx_ptr(j);
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              // i*(k+G)_j*c(G)
              tmp0[ig] = complex<double>(0.0,kpgxj[ig]) * p[ig+n*mloc];
            }
            cd_.ft(ikp)->backward(&tmp0[0],&tmpr[0]);
            // Compute V3 * Grad_j psi_n(ikp)
            for ( int i = 0; i < np012loc_; i++ )
              tmpr[i] *= xcf_->vxc3_dn[i];
            // Transform to k-space
            cd_.ft(ikp)->forward(&tmpr[0],&tmp0[0]);
            // Compute Div_j[V3 * Grad_j psi_n(ikp)]
            // Note -1/2 comes from definition of V3
            for ( int ig = 0; ig < ngwloc; ig++ )
            {
              // i*(k+G)_j*c(G)
              dp[ig+n*mloc] += -0.5 * complex<double>(0.0,kpgxj[ig]) * tmp0[ig];
            }
          }
        }
      }
    }
  }
Francois Gygi committed
1563
}