TABLE OF CONTENTS


ABINIT/m_xcpbe [ Modules ]

[ Top ] [ Modules ]

NAME

  m_xcpbe

FUNCTION

 Treat XC functionals closely linked with the Perdew-Wang 92 LSD and the PBE GGA.

COPYRIGHT

  Copyright (C) 1998-2018 ABINIT group (XG,MF,LG,CE)
  This file is distributed under the terms of the
  GNU General Public License, see ~abinit/COPYING
  or http://www.gnu.org/copyleft/gpl.txt .

PARENTS

CHILDREN

SOURCE

21 #if defined HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "abi_common.h"
26 
27 module m_xcpbe
28 
29  use defs_basis
30  use m_abicore
31  use m_errors
32 
33  use m_numeric_tools,      only : invcb
34 
35  implicit none
36 
37  private

ABINIT/xcpbe [ Functions ]

[ Top ] [ Functions ]

NAME

 xcpbe

FUNCTION

 Treat XC functionals closely linked with the Perdew-Wang 92 LSD and the PBE GGA.

 For a series of values of the density and, if GGA, the square of the
 gradient of the density, return the associated Exc energy,
 potential, and, in case of response-function, functions needed
 to build the XC kernel.

 If option==2, Exchange-correlation functional from Perdew-Burke-Ernzerhof,
 Phys.Rev.Lett. 77, 3866 (1996) [[cite:Perdew1996]].
 If option==1, Reduces to Perdew-Wang LSD , PRB45,13244 (1992) [[cite:Perdew1992]].
 If option==-1 or -2, take only exchange part of PW (-1) or PBE (-2) functionals.
 If option==-4, C09x exchange functional of V. R. Cooper, PRB 81, 161104(R) (2010) [[cite:Cooper2010]].
 If option==3 or 4, take exchange plus RPA correlation
   part of LSD PW (3) or GGA PBE (4) functionals.
 If option==5, revPBE functional of Zhang and Yang, PRL 80, 890 (1998) [[cite:Zhang1998]]
 If option==6, RPBE functional of Hammer, Hansen and Norskov, PRB 59, 7413 (1999) [[cite:Hammer1999]]
 If option==7, WC functional of Wu and Cohen, PRB 73, 235116 (2006) [[cite:Wu2006]]

INPUTS

  exexch= choice of local exact exchange. Active if exexch=1
  npts= number of points to be computed
  nspden=1 for unpolarized, 2 for spin-polarized
  grho2_updn(npts,ngr2)=square of the gradient of the spin-up,
     and, if nspden==2, spin-down, and total density (Hartree/Bohr**2),
     only used if gradient corrected functional (option=2,-2,-4 and 4 or beyond)
  option= see above
  order=its absolute value gives the maximal derivative of Exc to be computed.
  rho_updn(npts,nspden)=spin-up and spin-down density (Hartree/bohr**3)
  ndvxci= size of dvxci(npts,ndvxci)
  ngr2= size of grho2_updn(npts,ngr2)
  nd2vxci=size of d2vxci(npts,nd2vxci)

OUTPUT

  d2vxci=third derivative of the xc energy with respect to the density, only
    only if local-density approximation
   calculated if order==3
   In case of local energy functional (option=1,-1 or 3):
    d2vxci(npts,nd2vxc)=              (Hartree*bohr^3)
     if(nspden=1): d2vxci(:,1)=-(2/3)*dvxci/d$\rho$
                                  (dvxci is the second derivative, see below)
     if(nspden=2): d2vxci(:,1)=3rd order derivative of XC energy with respect to rhouprhouprhoup,
                   d2vxci(:,2)=3rd order derivative of XC energy with respect to rhouprhouprhodn
                   d2vxci(:,3)=3rd order derivative of XC energy with respect to rhodnrhouprhodn
                   d2vxci(:,4)=3rd order derivative of XC energy with respect to rhodnrhodnrhodn
  dvxcdgr(npts,3)=partial derivative of the exchange-correlation
    energy (exci*$\rho$) with respect to the spin-up (dvxcdgr(:,1)),
    spin-down (dvxcdgr(:,2)), or total spin (dvxcdgr(:,3)) gradients of the density
    divided by the norm of the gradient (the definition changed in v3.3)

  dvxci=partial second derivatives of the xc energy, only if abs(order)>1
   In case of local energy functional (option=1,-1 or 3):
    dvxci(npts,1+nspden)=              (Hartree*bohr^3)
     if(nspden=1 .and. order==2): dvxci(:,1)=dvxc/d$\rho$ , dvxc(:,2) empty
     if(nspden=1 .and. order==-2): also compute dvxci(:,2)=dvxc($\uparrow$)/d$\rho(\downarrow)$
     if(nspden=2): dvxci(:,1)=dvxc($\uparrow$)/d$\rho(\uparrow)$,
                   dvxci(:,2)=dvxc($\uparrow$)/d$\rho(\downarrow)$,
                   dvxci(:,3)=dvxc($\downarrow$)/d$\rho(\downarrow)$
   In case of gradient corrected functional (option=2,-2, 4, 5, 6, -4):
    dvxci(npts,15)=
     dvxci(:,1)= d2Ex/drho_up drho_up
     dvxci(:,2)= d2Ex/drho_dn drho_dn
     dvxci(:,3)= dEx/d(abs(grad(rho_up))) / abs(grad(rho_up))
     dvxci(:,4)= dEx/d(abs(grad(rho_dn))) / abs(grad(rho_dn))
     dvxci(:,5)= d2Ex/d(abs(grad(rho_up))) drho_up / abs(grad(rho_up))
     dvxci(:,6)= d2Ex/d(abs(grad(rho_dn))) drho_dn / abs(grad(rho_dn))
     dvxci(:,7)= 1/abs(grad(rho_up)) * d/drho_up (dEx/d(abs(grad(rho_up))) /abs(grad(rho_up)))
     dvxci(:,8)= 1/abs(grad(rho_dn)) * d/drho_dn (dEx/d(abs(grad(rho_dn))) /abs(grad(rho_dn)))
     dvxci(:,9)= d2Ec/drho_up drho_up
     dvxci(:,10)=d2Ec/drho_up drho_dn
     dvxci(:,11)=d2Ec/drho_dn drho_dn
     dvxci(:,12)=dEc/d(abs(grad(rho))) / abs(grad(rho))
     dvxci(:,13)=d2Ec/d(abs(grad(rho))) drho_up / abs(grad(rho))
     dvxci(:,14)=d2Ec/d(abs(grad(rho))) drho_dn / abs(grad(rho))
     dvxci(:,15)=1/abs(grad(rho)) * d/drho (dEc/d(abs(grad(rho))) /abs(grad(rho)))

  exci(npts)=exchange-correlation energy density (hartree)
  vxci(npts,nspden)=partial derivative of the exchange-correlation energy (exci*$\rho$)
    with respect to the spin-down (vxci(:,1)) and spin-up (vxci(:,2) densities
 Normalization: Exc=$\int (exc(r)*\rho (r) d^3 r)$ for $\rho$(r)=electron density.

TODO

  WARNING: option=4 not yet implemented.

PARENTS

      drivexc

CHILDREN

      invcb

SOURCE

 142 subroutine xcpbe(exci,npts,nspden,option,order,rho_updn,vxci,ndvxci,ngr2,nd2vxci, & !Mandatory Arguments
 143 &                d2vxci,dvxcdgr,dvxci,exexch,grho2_updn)                          !Optional Arguments
 144 
 145 
 146 !This section has been created automatically by the script Abilint (TD).
 147 !Do not modify the following lines by hand.
 148 #undef ABI_FUNC
 149 #define ABI_FUNC 'xcpbe'
 150 !End of the abilint section
 151 
 152  implicit none
 153 
 154 !Arguments ------------------------------------
 155 !scalars
 156  integer,intent(in) :: ndvxci,nd2vxci,ngr2,npts,nspden,option,order
 157  integer,intent(in),optional :: exexch
 158 !arrays
 159  real(dp),intent(in) :: rho_updn(npts,nspden)
 160  real(dp),intent(in),optional :: grho2_updn(npts,ngr2)
 161  real(dp),intent(out) :: exci(npts),vxci(npts,nspden)
 162  real(dp),intent(out),optional :: d2vxci(npts,nd2vxci),dvxcdgr(npts,3)
 163  real(dp),intent(out),optional :: dvxci(npts,ndvxci)
 164 
 165 !Local variables-------------------------------
 166 ! The "accurate" value of mu is taken from the PBE code.
 167 ! The value of mu_c09 is taken from the paper (see above) in order to recover
 168 ! the GEA behaviour of the enhancement factor for small values of s rather than
 169 ! the LSD linear response limit used in revPBE.
 170 !scalars
 171  integer,save :: initialized=0
 172  integer :: exexch_,ipts,ispden
 173  real(dp),parameter :: alpha_c09=0.0483_dp
 174 !GMR (to match Libxc one should comment the next line and uncomment the following )
 175  real(dp),parameter :: alpha_zeta2=1.0_dp-1.0e-6_dp,alpha_zeta=1.0_dp-1.0e-6_dp
 176 !real(dp),parameter :: alpha_zeta2=1.0_dp,alpha_zeta=1.0_dp
 177 !GMR
 178 !GMR (to match Libxc one should comment the next line and uncomment the following )
 179  real(dp),parameter :: b_wc=0.123456790123_dp,beta=0.066725_dp
 180 !real(dp),parameter :: b_wc=0.123456790123_dp,beta=0.06672455060314922_dp
 181 !GMR
 182  real(dp),parameter :: beta_inv=1.0_dp/beta,c_wc=0.00793746933516_dp
 183 !GMR (to match Libxc one should comment the next line and uncomment the following two)
 184  real(dp),parameter :: fsec_inv=1.0_dp/1.709921_dp,kappa_pbe=0.804_dp
 185 !real(dp),parameter :: fsec_inv=1.0_dp/1.709920934161365617563962776245_dp
 186 !real(dp),parameter :: kappa_pbe=0.804_dp
 187 !GMR
 188  real(dp),parameter :: kappa_revpbe=1.245_dp,mu=0.2195149727645171_dp
 189  real(dp),parameter :: kappa_c09=1.245_dp, mu_c09=0.0617_dp
 190  real(dp),parameter :: mu_divkappa_pbe=mu/kappa_pbe
 191  real(dp),parameter :: mu_divkappa_revpbe=mu/kappa_revpbe
 192  real(dp),parameter :: rsfac=0.6203504908994000_dp,tolgrad=tol10
 193  real(dp),save :: beta_gamma,coeff_tt,factf_zeta,factfp_zeta,gamma,gamma_inv
 194  real(dp),save :: sixpi2_1_3,sixpi2m1_3,sq_rsfac,sq_rsfac_inv,threefourth_divpi,twom1_3
 195  real(dp) :: aa,arg_rr,bb,cc,coeff_aa,alphs2,alphmu,coeff_qq,coeffss,d2aa_drs2,d2aa_drsdzeta
 196  real(dp) :: d2aa_dzeta2,d2bb_drs2,d2bb_drsdzeta,d2bb_dzeta2,d2cc_dbb2
 197  real(dp) :: d2cc_drs2,d2cc_drsdzeta,d2cc_dzeta2,d2ecrs0_drs2,d2ecrs1_drs2
 198  real(dp) :: d2ecrs_drdn2,d2ecrs_drdndrup,d2ecrs_drho2,d2ecrs_drs2
 199  real(dp) :: d2ecrs_drsdzeta,d2ecrs_drup2,d2ecrs_dzeta2,d2fxdg2,d2fxdn2
 200  real(dp) :: d2fxdndg,d2fxdss2,d2fzeta4_dzeta2,d2gcrs_drs2,d2hh_drs2
 201  real(dp) :: d2hh_drsdtt,d2hh_drsdzeta,d2hh_dtt2,d2hh_dttdzeta,d2hh_dzeta2
 202  real(dp) :: d2macrs_drs2,d2pade_drs2,d2pade_drsdtt,d2pade_drsdzeta,d2pade_dtt2
 203  real(dp) :: d2pade_dttdzeta,d2pade_dxx2,d2pade_dzeta2,d2qq_drs2,d2qq_drsdtt
 204  real(dp) :: d2qq_drsdzeta,d2qq_dtt2,d2qq_dttdzeta,d2qq_dzeta2,d2rhohh_drho2
 205  real(dp) :: d2rhohh_drhodg,d2rr_dqq2,d2rr_drs2,d2rr_drsdtt,d2rr_drsdzeta
 206  real(dp) :: d2rr_dtt2,d2rr_dttdzeta,d2rr_dzeta2,d2rs_dn2,d2ssdn2,d2ssdndg
 207  real(dp) :: d2vcrs_drs2,d2xx_drs2,d2xx_drsdtt,d2xx_drsdzeta,d2xx_dttdzeta
 208  real(dp) :: d2xx_dzeta2,d3ecrs0_drs3,d3ecrs_drup3,d3ecrs_drup2drdn
 209  real(dp) :: d3ecrs_drupdrdn2,d3ecrs_drdn3,d3ecrs_dzeta3
 210  real(dp) :: d3ecrs_drs2dzeta,d3ecrs_dzeta2drs,d3ecrs1_drs3,d3gcrs_drs3
 211  real(dp) :: d3ecrs_drs3,d3macrs_drs3
 212  real(dp) :: d_wc,daa_drs,daa_dzeta,dbb_drs,dbb_dzeta
 213  real(dp) :: dcc_dbb,dcc_drs,dcc_dzeta,decrs0_drs,decrs1_drs,decrs_drs
 214  real(dp) :: decrs_dzeta,dfxdg,dfxdn,dfxdss,dfzeta4_dzeta,dgcrs_drs
 215  real(dp) :: dhh_drs,dhh_dtt,dhh_dzeta,div_rr,divss,dmacrs_drs,dpade_drs
 216  real(dp) :: dpade_dtt,dpade_dxx,dpade_dzeta,dqq_drs,dqq_dtt,dqq_dzeta
 217  real(dp) :: drhohh_drho,drr_dqq,drr_drs,drr_dtt,drr_dzeta,drs_dn,dssdg,dssdn
 218  real(dp) :: dtt_dg,dvcrs_drs,dxx_drs,dxx_dtt,dxx_dzeta,ec0_a1,ec0_aa,ec0_b1
 219  real(dp) :: ec0_b2,ec0_b3,ec0_b4,ec0_den,ec0_f1,ec0_f2,ec0_log,ec0_q0,ec0_q1
 220  real(dp) :: ec0_q1p,ec0_q1pp,ec0_q1ppp,ec1_a1,ec1_aa,ec1_b1,ec1_b2,ec1_b3
 221  real(dp) :: ec1_b4,ec1_den,ec1_f1,ec1_f2,ec1_log,ec1_q0,ec1_q1,ec1_q1p,ec1_q1pp
 222  real(dp) :: ec1_q1ppp,ecrs,ecrs0,factfppp_zeta
 223  real(dp) :: ecrs1,ex_gga,ex_lsd,exc,exp_pbe,expss,f_zeta,factfpp_zeta
 224  real(dp) :: fp_zeta,fpp_zeta,fppp_zeta,fx,gamphi3inv,gcrs,grrho2,hh,kappa
 225  real(dp) :: mac_a1,mac_aa,mac_b1,mac_b2,mac_b3,mac_b4,mac_den,mac_f1,mac_f2,mac_log,mac_q0
 226  real(dp) :: mac_q1,mac_q1ppp
 227  real(dp) :: mac_q1p,mac_q1pp,macrs,mu_divkappa,p1_wc,p2_wc,pade,pade_den
 228  real(dp) :: phi3_zeta,phi_logder,phi_zeta,phi_zeta_inv,phip_zeta,phipp_zeta,qq
 229  real(dp) :: rho,rho_inv,rhomot
 230  real(dp) :: rhotmo6,rhotmot,rhoto6,rhotot,rhotot_inv,rr,rs,rsm1_2,sqr_rs
 231  real(dp) :: sqr_sqr_rs,ss,tt,vxcadd,xx,zeta,zeta4,zetm_1_3,zetp_1_3
 232  real(dp) :: a1fa,a2fa,b1fa,b2fa,c1fa,c2fa,e1fa,e2fa,f1fa,f2fa,g1fa,g2fa,h1fa,h2fa
 233  real(dp) :: i1fa,i2fa,m1fa,m2fa,n1fa,n2fa
 234  real(dp) :: sp1_up3,sp1_up2dn,sp1_updn2,sp1_dn3
 235  real(dp) :: sp2_up3,sp2_up2dn,sp2_updn2,sp2_dn3
 236  real(dp) :: sp3_up3,sp3_up2dn,sp3_updn2,sp3_dn3
 237  real(dp) :: d3ecrs_sp0,d3ecrs_sp1,d3ecrs_sp2,d3ecrs_sp3
 238  character(len=500) :: message
 239 !arrays
 240  real(dp),allocatable :: rho_updnm1_3(:,:),rhoarr(:),rhom1_3(:),zetm(:)
 241  real(dp),allocatable :: zetmm1_3(:),zetp(:),zetpm1_3(:)
 242 !no_abirules
 243 !integer :: debug
 244 !real(dp) :: delta,factor,grr,rho_dn,rho_dnm,rho_dnp,rho_up,rho_upm,rho_upp,zeta_mean
 245 !real(dp), allocatable :: wecrsz(:,:),d1wecrsz(:,:),d2wecrsz(:,:),d3wecrsz(:,:)
 246 !real(dp) :: d3ecrs_drho3,d3ecrs_drhodndrho2,d3ecrs_drhoupdrho2
 247 !real(dp) :: ec1_q0p,mac_q0p,sigma1,sigma2,sigma3
 248 
 249 ! *************************************************************************
 250 
 251 !DEBUG
 252 !write(std_out,*)' xcpbe : enter'
 253 !ENDDEBUG
 254 
 255  d_wc=mu-b_wc
 256  exexch_=0;if(present(exexch)) exexch_=exexch
 257 
 258 !DEBUG
 259 !allocate(wecrsz(npts,8),d1wecrsz(npts,8),d2wecrsz(npts,8),d3wecrsz(npts,8))
 260 !ENDDEBUG
 261 
 262  if (option<=-4 .or. option==0 .or. option==4 .or. option>=8 ) then
 263    write(message, '(a,a,a,a,i12,a)' ) ch10,&
 264 &   ' xcpbe : BUG -',ch10,&
 265 &   '  Option must be 1, 2, 3, 5, 6, 7, 8, -1 or -2 ; argument was ',option,'.'
 266 !  MSG_BUG(message)
 267  end if
 268 
 269 !Checks the compatibility between the presence of dvxci and ndvxci
 270  if(ndvxci /=0 .neqv. present(dvxci))then
 271    message = ' If ndvxci/=0 there must the optional argument dvxci'
 272    MSG_BUG(message)
 273  end if
 274 
 275 !Checks the compatibility between the inputs and the presence of the optional arguments
 276  if(ndvxci /= 0 .and. abs(order) <= 1)then
 277    write(message, '(3a,i8,a)' )&
 278 &   'The order does not require the presence of dvxci',ch10,&
 279 &   'that is allowed when |order|>1, while we have',order,'.'
 280    MSG_BUG(message)
 281  end if
 282 
 283  if(ndvxci /= 0 .and. (&
 284 & ((option == 1 .or. option == -1 .or. option == 3) .and. ndvxci /= nspden + 1)&
 285 & .or. (option == -2 .and. ndvxci /= 8)&
 286 & .or. ((option == 2 .or. option == 5 .or. option == 6 .or. option == 7 .or. option == 8) .and. ndvxci /= 15)&
 287 & ))then
 288    write(message, '(12a,4(a,i5))' )&
 289 &   '  The option is not consistent with the value of ndvxci',ch10,&
 290 &   '  Allowed values are:',ch10,&
 291 &   '  ndvxci     option',ch10,&
 292 &   ' nspden+1    1,-1,3',ch10,&
 293 &   '    8          -2',ch10,&
 294 &   '    15       2, 5,6,7',ch10,&
 295 &   '  While we have: ndvxc=',ndvxci,', option=',option,', nspden=',nspden,', order=',order
 296    MSG_BUG(message)
 297  end if
 298 
 299  if (present(grho2_updn)) then
 300    if (ngr2/=2*nspden-1 ) then
 301      write(message, '(a,2i6)' )&
 302 &     ' ngr2 must be 2*nspden-1 ! ngr2,nspden=',ngr2,nspden
 303      MSG_BUG(message)
 304    end if
 305  end if
 306 
 307  if ((option == 1 .or. option == -1 .or. option ==3) .and.  (present(grho2_updn) .or. present(dvxcdgr))) then
 308    write(message, '(a,a,a,i6,a)' )&
 309 &   'The option chosen does not need the presence',ch10,&
 310 &   'of the gradient, or of the array dvxcdgr in the input, needed if option/=1,-1,3 , while we have',option,'.'
 311    MSG_BUG(message)
 312  end if
 313 
 314  if (order /= 3 .and. present(d2vxci)) then
 315    write(message, '(a,a,a,i6,a)' )&
 316 &   'The order chosen does not need the presence',ch10,&
 317 &   'of the array d2vxci, needed if order=3 , while we have',order,'.'
 318    MSG_BUG(message)
 319  end if
 320 
 321  if(initialized==0)then
 322    twom1_3=two**(-third)
 323    sixpi2_1_3=(six*pi**2)**third
 324    sixpi2m1_3=one/sixpi2_1_3
 325    threefourth_divpi=three_quarters*piinv
 326    gamma=(one-log(two))*piinv**2
 327    gamma_inv=one/gamma
 328    beta_gamma=beta*gamma_inv
 329    factf_zeta= one / ( two**(four/three)-two )
 330    factfp_zeta= four_thirds * factf_zeta * alpha_zeta2
 331    coeff_tt= one/ (four*four*piinv*(three*pi**2)**third)
 332 !  coeff_tt= two * sqrt(four*piinv*(three*pi**2)**third)
 333    sq_rsfac=sqrt(rsfac)
 334    sq_rsfac_inv=one/sq_rsfac
 335    initialized=1
 336  end if
 337 
 338 !Parameters for the Perdew-Wang 92 LSD as well as LSD-RPA,
 339 !see Table I of Phys.Rev.B 45,13244 (1992) [[cite:Perdew1992]]
 340 !GMR (to match Libxc one should comment the next line and uncomment the following two)
 341  ec0_aa=0.031091_dp  ; ec1_aa=0.015545_dp ; mac_aa=0.016887_dp
 342 !ec0_aa=0.0310907_dp  ; ec1_aa=0.01554535_dp ; mac_aa=0.0168869_dp
 343 !GMR
 344  if(option/=3 .and. option/=4)then
 345    ec0_a1=0.21370_dp  ; ec1_a1=0.20548_dp  ; mac_a1=0.11125_dp
 346    ec0_b1=7.5957_dp   ; ec1_b1=14.1189_dp  ; mac_b1=10.357_dp
 347    ec0_b2=3.5876_dp   ; ec1_b2=6.1977_dp   ; mac_b2=3.6231_dp
 348    ec0_b3=1.6382_dp   ; ec1_b3=3.3662_dp   ; mac_b3=0.88026_dp
 349    ec0_b4=0.49294_dp  ; ec1_b4=0.62517_dp  ; mac_b4=0.49671_dp
 350  else  ! RPA values
 351    ec0_a1=0.082477_dp ; ec1_a1=0.035374_dp ; mac_a1=0.028829_dp
 352    ec0_b1=5.1486_dp   ; ec1_b1=6.4869_dp   ; mac_b1=10.357_dp
 353    ec0_b2=1.6483_dp   ; ec1_b2=1.3083_dp   ; mac_b2=3.6231_dp
 354    ec0_b3=0.23647_dp  ; ec1_b3=0.11518_dp  ; mac_b3=0.479_dp
 355    ec0_b4=0.20614_dp  ; ec1_b4=0.082349_dp ; mac_b4=0.112279_dp
 356  end if
 357 
 358  if(option/=5 .and. option/=-4)then
 359    kappa=kappa_pbe
 360    mu_divkappa=mu_divkappa_pbe
 361  end if
 362  if(option==5)then
 363    kappa=kappa_revpbe
 364    mu_divkappa=mu_divkappa_revpbe
 365  end if
 366  if(option==-4)then
 367    kappa=kappa_c09
 368  end if
 369 !DEBUG
 370 !Finite-difference debugging, do not take away
 371 !Note : here work with collinear gradients. Might be generalized ...
 372 !debug=2  ! Choose 1 (rho grads) or 2 (grho grads)
 373 !if(order==3)debug=1
 374 !factor=1.0_dp
 375 !zeta_mean=0.98_dp
 376 !!zeta_mean=zero
 377 !delta=0.000025*factor
 378 !delta=0.0000125*factor
 379 !if(debug/=0)then
 380 !do ipts=1,npts-4,5
 381 !rho=ipts*0.01_dp*factor
 382 !rho_up=rho*(1.0_dp+zeta_mean)*0.5_dp
 383 !rho_dn=rho*(1.0_dp-zeta_mean)*0.5_dp
 384 !rho_upp=rho_up+delta
 385 !rho_upm=rho_up-delta
 386 !rho_dnp=rho_dn+delta
 387 !rho_dnm=rho_dn-delta
 388 !! Here, vary rho
 389 !if(debug==1)then
 390 !rho_updn(ipts  ,1)=rho_up ; rho_updn(ipts  ,2)=rho_dn
 391 !rho_updn(ipts+1,1)=rho_upp; rho_updn(ipts+1,2)=rho_dn
 392 !rho_updn(ipts+2,1)=rho_upm; rho_updn(ipts+2,2)=rho_dn
 393 !rho_updn(ipts+3,1)=rho_up ; rho_updn(ipts+3,2)=rho_dnp
 394 !rho_updn(ipts+4,1)=rho_up ; rho_updn(ipts+4,2)=rho_dnm
 395 !grho2_updn(ipts:ipts+4,1)=(0.2_dp*factor)**2     ! grad2 of spin up density
 396 !grho2_updn(ipts:ipts+4,2)=(0.2_dp*factor)**2     ! grad2 of spin down density
 397 !grho2_updn(ipts:ipts+4,3)=(0.3_dp*factor)**2     ! grad2 of total density
 398 !else
 399 !!  Here, vary grho (interchange rho and grho)
 400 !grho2_updn(ipts  ,1)=rho_up**2 ; grho2_updn(ipts  ,2)=rho_dn**2
 401 !grho2_updn(ipts+1,1)=rho_upp**2; grho2_updn(ipts+1,2)=rho_dn**2
 402 !grho2_updn(ipts+2,1)=rho_upm**2; grho2_updn(ipts+2,2)=rho_dn**2
 403 !grho2_updn(ipts+3,1)=rho_up**2 ; grho2_updn(ipts+3,2)=rho_dnp**2
 404 !grho2_updn(ipts+4,1)=rho_up**2 ; grho2_updn(ipts+4,2)=rho_dnm**2
 405 !grho2_updn(ipts  ,3)=(ipts*0.01_dp*factor)**2
 406 !grho2_updn(ipts+1,3)=(ipts*0.01_dp*factor+delta)**2
 407 !grho2_updn(ipts+2,3)=(ipts*0.01_dp*factor-delta)**2
 408 !grho2_updn(ipts+3,3)=(ipts*0.01_dp*factor+delta)**2   ! identical to ipts+1
 409 !grho2_updn(ipts+4,3)=(ipts*0.01_dp*factor-delta)**2   ! identical to ipts+2
 410 !rho_updn(ipts:ipts+4,1)=0.2_dp*factor*(1.0_dp+zeta_mean)*0.5_dp    ! spin up density
 411 !rho_updn(ipts:ipts+4,2)=0.2_dp*factor*(1.0_dp-zeta_mean)*0.5_dp    ! spin down density
 412 !end if
 413 !end do
 414 !end if
 415 !Usual option :
 416 !nspden=2 ; order=2
 417 !GGA
 418 !nspden=2 ; order=1
 419 !Might take also, although finite difference later is meaningless
 420 !nspden=1 ; order=-2
 421 !Here, alternative specification, in terms of defined rs and zeta
 422 !do ipts=1,5
 423 !if(ipts==1)then ;rs=0.01_dp ; zeta=0.98_dp ; endif
 424 !if(ipts==2)then ;rs=0.01_dp+delta ; zeta=0.98_dp ; endif
 425 !if(ipts==3)then ;rs=0.01_dp-delta ; zeta=0.98_dp ; endif
 426 !if(ipts==4)then ;rs=0.01_dp ; zeta=0.98_dp+delta ; endif
 427 !if(ipts==5)then ;rs=0.01_dp ; zeta=0.98_dp-delta ; endif
 428 !rho=(rsfac/rs)**3
 429 !rho_up=rho*(1.0_dp+zeta)*0.5_dp
 430 !rho_dn=rho*(1.0_dp-zeta)*0.5_dp
 431 !rho_updn(ipts  ,1)=rho_up ; rho_updn(ipts  ,2)=rho_dn
 432 !enddo
 433 !ENDDEBUG
 434 
 435  if(order**2 >1)then
 436    factfpp_zeta= third * factfp_zeta * alpha_zeta2
 437  end if
 438 
 439 
 440  ABI_ALLOCATE(rhoarr,(npts))
 441  ABI_ALLOCATE(rhom1_3,(npts))
 442  ABI_ALLOCATE(rho_updnm1_3,(npts,2))
 443  ABI_ALLOCATE(zetm,(npts))
 444  ABI_ALLOCATE(zetmm1_3,(npts))
 445  ABI_ALLOCATE(zetp,(npts))
 446  ABI_ALLOCATE(zetpm1_3,(npts))
 447 
 448  do ispden=1,nspden
 449    call invcb(rho_updn(:,ispden),rho_updnm1_3(:,ispden),npts)
 450  end do
 451 
 452 
 453  if(nspden==1)then
 454    rhoarr(:)=two*rho_updn(:,1)
 455    rhom1_3(:)=twom1_3*rho_updnm1_3(:,1)
 456    rho_updnm1_3(:,2)=rho_updnm1_3(:,1)
 457  else
 458    rhoarr(:)=rho_updn(:,1)+rho_updn(:,2)
 459    call invcb(rhoarr,rhom1_3,npts)
 460    do ipts=1,npts
 461      rhotmot=rhom1_3(ipts)
 462      rhotot_inv=rhotmot*rhotmot*rhotmot
 463      zeta=(rho_updn(ipts,1)-rho_updn(ipts,2))*rhotot_inv
 464      zetp(ipts)=1.0_dp+zeta*alpha_zeta
 465      zetm(ipts)=1.0_dp-zeta*alpha_zeta
 466    end do
 467    call invcb(zetp,zetpm1_3,npts)
 468    call invcb(zetm,zetmm1_3,npts)
 469  end if
 470 
 471 
 472 !fab: eliminate the following restriction
 473 
 474 !if (order==3 .and. nspden == 1) d2vxci(:,:)=0._dp
 475 
 476 
 477  if (order==3) d2vxci(:,:)=0._dp
 478 
 479 !!!Loop unrolling summary
 480 !Completely unrolled for spin non-polarized case
 481 !To be optimized for spin-polarized cases
 482 !The loops are unrolled as follows:
 483 !nspden=1     (line 433)
 484 !order^2<=1  (line 460)
 485 !option=2,5 (line 462)
 486 !option=6,7 (line 630)
 487 !option=-1  (line 825)
 488 !option=-2  (line 853)
 489 !option=1   (line 904)
 490 !option=3   (line 963)
 491 !order=3     (line 1024)
 492 !option=2,5
 493 !option=6,7
 494 !option=-1
 495 !option=-2
 496 !option=1
 497 !option=3
 498 !order=-2    (line 1983)
 499 !option=2,5
 500 !option=6,7
 501 !option=-1
 502 !option=-2
 503 !option=1
 504 !option=3
 505 !order^2>1   (line 2875)
 506 !option=2,5
 507 !option=6,7
 508 !option=-1
 509 !option=-2
 510 !option=1
 511 !option=3
 512 !nspden=2     (line 3750)
 513 !order^2<=1  (line 3754)
 514 !order^2>1 (with if statements inside distinguishing between order=3 or -2)   (line 4000)
 515 !!!End loop unrolling summary
 516 
 517 !we separate different cases, depending on nspden
 518  if (nspden==1) then
 519 !  we separate different cases, depending on order
 520    if (order**2<=1) then
 521 !    we separate different cases, depending on option
 522      if(option==2 .or. option==5)then
 523 
 524        do ipts=1,npts
 525 
 526          rhotot=rhoarr(ipts)
 527          rhotmot=rhom1_3(ipts)
 528          rhotot_inv=rhotmot*rhotmot*rhotmot
 529          rhotmo6=sqrt(rhotmot)
 530          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
 531 !        -----------------------------------------------------------------------
 532 !        First take care of the exchange part of the functional
 533 
 534          exc=zero
 535          dvxcdgr(ipts,3)=zero
 536 !        loop over the spin
 537          ispden=1
 538          rho   =rho_updn(ipts,ispden)
 539          rhomot=rho_updnm1_3(ipts,ispden)
 540          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
 541 !        Perdew-Burke-Ernzerhof GGA, exchange part
 542          rho_inv=rhomot*rhomot*rhomot
 543          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
 544          ss=grho2_updn(ipts,ispden)*coeffss
 545          divss=one/(one+mu_divkappa*ss)
 546          dfxdss= mu*divss*divss
 547          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
 548          fx    = one+kappa*(one-divss)
 549          ex_gga= ex_lsd*fx
 550          dssdn=-eight*third*ss*rho_inv
 551          dfxdn  = dfxdss*dssdn
 552          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
 553 !        The new definition (v3.3) includes the division by the norm of the gradient
 554          dssdg =two*coeffss
 555          dfxdg=dfxdss*dssdg
 556          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
 557          exc=exc+ex_gga*rho
 558 
 559 !        end of loop over the spin
 560 !        If non spin-polarized, treat spin down contribution now, similar to spin up
 561          exc=exc*2
 562          exci(ipts)=exc*rhotot_inv
 563          if(exexch_==1) cycle
 564 !        -----------------------------------------------------------------------------
 565 !        Then takes care of the LSD correlation part of the functional
 566 
 567 
 568          rs=rsfac*rhotmot
 569          sqr_rs=sq_rsfac*rhotmo6
 570          rsm1_2=sq_rsfac_inv*rhoto6
 571 
 572 !        Formulas A6-A8 of PW92LSD
 573          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
 574          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
 575          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
 576          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
 577 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
 578          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
 579          ecrs0=ec0_q0*ec0_log
 580          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
 581 
 582          ecrs=ecrs0
 583          decrs_drs=decrs0_drs
 584          decrs_dzeta=0.0_dp
 585          zeta=0.0_dp
 586 
 587 !        Add LSD correlation functional to GGA exchange functional
 588          exci(ipts)=exci(ipts)+ecrs
 589          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
 590 
 591 
 592 !        -----------------------------------------------------------------------------
 593 !        Eventually add the GGA correlation part of the PBE functional
 594 !        Note : the computation of the potential in the spin-unpolarized
 595 !        case could be optimized much further. Other optimizations are left to do.
 596 
 597          phi_zeta=1.0_dp
 598          phip_zeta=0.0_dp
 599          phi_zeta_inv=1.0_dp
 600          phi_logder=0.0_dp
 601          phi3_zeta=1.0_dp
 602          gamphi3inv=gamma_inv
 603          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
 604 
 605 !        From ec to bb
 606          bb=ecrs*gamphi3inv
 607          dbb_drs=decrs_drs*gamphi3inv
 608          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
 609 
 610 !        From bb to cc
 611          exp_pbe=exp(-bb)
 612          cc=one/(exp_pbe-one)
 613          dcc_dbb=cc*cc*exp_pbe
 614          dcc_drs=dcc_dbb*dbb_drs
 615          dcc_dzeta=dcc_dbb*dbb_dzeta
 616 
 617 !        From cc to aa
 618          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
 619          aa=coeff_aa*cc
 620          daa_drs=coeff_aa*dcc_drs
 621          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
 622 
 623 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
 624          grrho2=four*grho2_updn(ipts,1)
 625          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
 626 !        Note that tt is (the t variable of PBE divided by phi) squared
 627          tt=half*grrho2*dtt_dg
 628 
 629 !        Get xx from aa and tt
 630          xx=aa*tt
 631          dxx_drs=daa_drs*tt
 632          dxx_dzeta=daa_dzeta*tt
 633          dxx_dtt=aa
 634 
 635 !        From xx to pade
 636          pade_den=one/(one+xx*(one+xx))
 637          pade=(one+xx)*pade_den
 638          dpade_dxx=-xx*(two+xx)*pade_den**2
 639          dpade_drs=dpade_dxx*dxx_drs
 640          dpade_dtt=dpade_dxx*dxx_dtt
 641          dpade_dzeta=dpade_dxx*dxx_dzeta
 642 
 643 !        From pade to qq
 644          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
 645          qq=coeff_qq*pade
 646          dqq_drs=coeff_qq*dpade_drs
 647          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
 648          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
 649 
 650 !        From qq to rr
 651          arg_rr=one+beta*gamma_inv*qq
 652          div_rr=one/arg_rr
 653          rr=gamma*log(arg_rr)
 654          drr_dqq=beta*div_rr
 655          drr_drs=drr_dqq*dqq_drs
 656          drr_dtt=drr_dqq*dqq_dtt
 657          drr_dzeta=drr_dqq*dqq_dzeta
 658 
 659 !        From rr to hh
 660          hh=phi3_zeta*rr
 661          dhh_drs=phi3_zeta*drr_drs
 662          dhh_dtt=phi3_zeta*drr_dtt
 663          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
 664 
 665 !        The GGA correlation energy is added
 666          exci(ipts)=exci(ipts)+hh
 667 
 668 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
 669 
 670 !        From hh to the derivative of the energy wrt the density
 671          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
 672          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
 673 
 674 !        From hh to the derivative of the energy wrt to the gradient of the
 675 !        density, divided by the gradient of the density
 676 !        (The v3.3 definition includes the division by the norm of the gradient)
 677          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
 678 
 679 !        End condition of GGA
 680 
 681 !        Correlation has been added
 682 !        -----------------------------------------------------------------------------
 683 
 684 !        vxci(ipts,2)=vxci(ipts,1)
 685          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
 686 
 687        end do
 688      else if((option==6) .or. (option==7)) then
 689 
 690        do ipts=1,npts
 691 
 692          rhotot=rhoarr(ipts)
 693          rhotmot=rhom1_3(ipts)
 694          rhotot_inv=rhotmot*rhotmot*rhotmot
 695          rhotmo6=sqrt(rhotmot)
 696          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
 697 !        -----------------------------------------------------------------------
 698 !        First take care of the exchange part of the functional
 699 
 700          exc=zero
 701          dvxcdgr(ipts,3)=zero
 702 !        loop over the spin
 703          ispden=1
 704          rho   =rho_updn(ipts,ispden)
 705          rhomot=rho_updnm1_3(ipts,ispden)
 706          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
 707 !        Perdew-Burke-Ernzerhof GGA, exchange part
 708          rho_inv=rhomot*rhomot*rhomot
 709          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
 710          ss=grho2_updn(ipts,ispden)*coeffss
 711 
 712 !        This is RPBE modification
 713          if (option==6) then
 714            divss=exp(-mu_divkappa*ss)
 715            dfxdss= mu*divss
 716            d2fxdss2=-mu*mu_divkappa*divss
 717 
 718            fx    = one+kappa*(one-divss)
 719            ex_gga= ex_lsd*fx
 720            dssdn=-eight*third*ss*rho_inv
 721            dfxdn  = dfxdss*dssdn
 722            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
 723 !          The new definition (v3.3) includes the division by the norm of the gradient
 724            dssdg =two*coeffss
 725            dfxdg=dfxdss*dssdg
 726            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
 727            exc=exc+ex_gga*rho
 728 !          This is the Wu and Cohen modification
 729          else
 730            expss=exp(-ss)
 731            p1_wc=b_wc+(mu-b_wc)*(one-ss)*expss+two*c_wc*ss/(one+c_wc*ss*ss)
 732            p2_wc=d_wc*(ss-two)*expss+two*c_wc/(one+c_wc*ss*ss)-&
 733 &           four*c_wc*c_wc*ss*ss/((one+c_wc*ss*ss)*(one+c_wc*ss*ss))
 734            divss=one/(one+(b_wc*ss+d_wc*ss*expss+log(one+c_wc*ss*ss))/kappa)
 735            dfxdss=p1_wc*divss*divss
 736            d2fxdss2=p2_wc*divss*divss-two*divss*divss*divss*p1_wc*p1_wc/kappa
 737 
 738            fx    = one+kappa*(one-divss)
 739            ex_gga= ex_lsd*fx
 740            dssdn=-eight*third*ss*rho_inv
 741            dfxdn  = dfxdss*dssdn
 742            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
 743 !          The new definition (v3.3) includes the division by the norm of the gradient
 744            dssdg =two*coeffss
 745            dfxdg=dfxdss*dssdg
 746            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
 747            exc=exc+ex_gga*rho
 748          end if
 749 
 750 !        end of loop over the spin
 751 !        If non spin-polarized, treat spin down contribution now, similar to spin up
 752          exc=exc*2
 753          exci(ipts)=exc*rhotot_inv
 754          if(exexch_==1) cycle
 755 !        -----------------------------------------------------------------------------
 756 !        Then takes care of the LSD correlation part of the functional
 757 
 758 
 759          rs=rsfac*rhotmot
 760          sqr_rs=sq_rsfac*rhotmo6
 761          rsm1_2=sq_rsfac_inv*rhoto6
 762 
 763 !        Formulas A6-A8 of PW92LSD
 764          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
 765          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
 766          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
 767          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
 768 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
 769          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
 770          ecrs0=ec0_q0*ec0_log
 771          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
 772 
 773          ecrs=ecrs0
 774          decrs_drs=decrs0_drs
 775          decrs_dzeta=0.0_dp
 776          zeta=0.0_dp
 777 
 778 !        Add LSD correlation functional to GGA exchange functional
 779          exci(ipts)=exci(ipts)+ecrs
 780          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
 781 
 782 
 783 !        -----------------------------------------------------------------------------
 784 !        Eventually add the GGA correlation part of the PBE functional
 785 !        Note : the computation of the potential in the spin-unpolarized
 786 !        case could be optimized much further. Other optimizations are left to do.
 787 
 788          phi_zeta=1.0_dp
 789          phip_zeta=0.0_dp
 790          phi_zeta_inv=1.0_dp
 791          phi_logder=0.0_dp
 792          phi3_zeta=1.0_dp
 793          gamphi3inv=gamma_inv
 794          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
 795 
 796 !        From ec to bb
 797          bb=ecrs*gamphi3inv
 798          dbb_drs=decrs_drs*gamphi3inv
 799          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
 800 
 801 !        From bb to cc
 802          exp_pbe=exp(-bb)
 803          cc=one/(exp_pbe-one)
 804          dcc_dbb=cc*cc*exp_pbe
 805          dcc_drs=dcc_dbb*dbb_drs
 806          dcc_dzeta=dcc_dbb*dbb_dzeta
 807 
 808 !        From cc to aa
 809          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
 810          aa=coeff_aa*cc
 811          daa_drs=coeff_aa*dcc_drs
 812          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
 813 
 814 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
 815          grrho2=four*grho2_updn(ipts,1)
 816          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
 817 !        Note that tt is (the t variable of PBE divided by phi) squared
 818          tt=half*grrho2*dtt_dg
 819 
 820 !        Get xx from aa and tt
 821          xx=aa*tt
 822          dxx_drs=daa_drs*tt
 823          dxx_dzeta=daa_dzeta*tt
 824          dxx_dtt=aa
 825 
 826 !        From xx to pade
 827          pade_den=one/(one+xx*(one+xx))
 828          pade=(one+xx)*pade_den
 829          dpade_dxx=-xx*(two+xx)*pade_den**2
 830          dpade_drs=dpade_dxx*dxx_drs
 831          dpade_dtt=dpade_dxx*dxx_dtt
 832          dpade_dzeta=dpade_dxx*dxx_dzeta
 833 
 834 !        From pade to qq
 835          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
 836          qq=coeff_qq*pade
 837          dqq_drs=coeff_qq*dpade_drs
 838          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
 839          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
 840 
 841 !        From qq to rr
 842          arg_rr=one+beta*gamma_inv*qq
 843          div_rr=one/arg_rr
 844          rr=gamma*log(arg_rr)
 845          drr_dqq=beta*div_rr
 846          drr_drs=drr_dqq*dqq_drs
 847          drr_dtt=drr_dqq*dqq_dtt
 848          drr_dzeta=drr_dqq*dqq_dzeta
 849 
 850 !        From rr to hh
 851          hh=phi3_zeta*rr
 852          dhh_drs=phi3_zeta*drr_drs
 853          dhh_dtt=phi3_zeta*drr_dtt
 854          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
 855 
 856 !        The GGA correlation energy is added
 857          exci(ipts)=exci(ipts)+hh
 858 
 859 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
 860 
 861 !        From hh to the derivative of the energy wrt the density
 862          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
 863          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
 864 
 865 !        From hh to the derivative of the energy wrt to the gradient of the
 866 !        density, divided by the gradient of the density
 867 !        (The v3.3 definition includes the division by the norm of the gradient)
 868          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
 869 
 870 !        End condition of GGA
 871 
 872 !        Correlation has been added
 873 !        -----------------------------------------------------------------------------
 874 
 875 !        vxci(ipts,2)=vxci(ipts,1)
 876          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
 877 
 878        end do
 879 
 880 
 881      else if (option==-1) then
 882 
 883        do ipts=1,npts
 884 
 885          rhotot=rhoarr(ipts)
 886          rhotmot=rhom1_3(ipts)
 887          rhotot_inv=rhotmot*rhotmot*rhotmot
 888          rhotmo6=sqrt(rhotmot)
 889          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
 890 !        -----------------------------------------------------------------------
 891 !        First take care of the exchange part of the functional
 892 
 893          exc=zero
 894 !        loop over the spin
 895          ispden=1
 896          rho   =rho_updn(ipts,ispden)
 897          rhomot=rho_updnm1_3(ipts,ispden)
 898          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
 899 !        Perdew_Wang 91 LSD
 900          vxci(ipts,ispden)=four_thirds*ex_lsd
 901          exc=exc+ex_lsd*rho
 902 
 903 !        end of loop over the spin
 904 !        If non spin-polarized, treat spin down contribution now, similar to spin up
 905          exc=exc*2
 906          exci(ipts)=exc*rhotot_inv
 907        end do
 908 
 909      else if(option==-2) then
 910 
 911 
 912        do ipts=1,npts
 913 
 914          rhotot=rhoarr(ipts)
 915          rhotmot=rhom1_3(ipts)
 916          rhotot_inv=rhotmot*rhotmot*rhotmot
 917          rhotmo6=sqrt(rhotmot)
 918          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
 919 !        -----------------------------------------------------------------------
 920 !        First take care of the exchange part of the functional
 921 
 922          exc=zero
 923          dvxcdgr(ipts,3)=zero
 924 !        loop over the spin
 925          ispden=1
 926          rho   =rho_updn(ipts,ispden)
 927          rhomot=rho_updnm1_3(ipts,ispden)
 928          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
 929 !        Perdew-Burke-Ernzerhof GGA, exchange part
 930          rho_inv=rhomot*rhomot*rhomot
 931          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
 932          ss=grho2_updn(ipts,ispden)*coeffss
 933          divss=one/(one+mu_divkappa*ss)
 934          dfxdss= mu*divss*divss
 935          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
 936          fx    = one+kappa*(one-divss)
 937          ex_gga= ex_lsd*fx
 938          dssdn=-eight*third*ss*rho_inv
 939          dfxdn  = dfxdss*dssdn
 940          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
 941 !        The new definition (v3.3) includes the division by the norm of the gradient
 942          dssdg =two*coeffss
 943          dfxdg=dfxdss*dssdg
 944          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
 945          exc=exc+ex_gga*rho
 946 
 947 !        end of loop over the spin
 948 !        If non spin-polarized, treat spin down contribution now, similar to spin up
 949          exc=exc*2
 950          exci(ipts)=exc*rhotot_inv
 951 
 952 !        Correlation has been added
 953 !        -----------------------------------------------------------------------------
 954 
 955 !        vxci(ipts,2)=vxci(ipts,1)
 956          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
 957 
 958        end do
 959 
 960 
 961      else if(option==-4) then
 962 
 963 
 964        do ipts=1,npts
 965 
 966          rhotot=rhoarr(ipts)
 967          rhotmot=rhom1_3(ipts)
 968          rhotot_inv=rhotmot*rhotmot*rhotmot
 969          rhotmo6=sqrt(rhotmot)
 970          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
 971 !        -----------------------------------------------------------------------
 972 !        First take care of the exchange part of the functional
 973 
 974          exc=zero
 975          dvxcdgr(ipts,3)=zero
 976 !        loop over the spin
 977          ispden=1
 978          rho   =rho_updn(ipts,ispden)
 979          rhomot=rho_updnm1_3(ipts,ispden)
 980          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
 981 !        VALENTINO R. COOPER C09x GGA, This is an exchange term proposed
 982 !        to use together with vdw-DF (see above).
 983          rho_inv=rhomot*rhomot*rhomot
 984          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
 985 !        the quarter that is lacking is compensated by the grho2_updn in the
 986 !        next line.
 987          ss=grho2_updn(ipts,ispden)*coeffss
 988          alphs2=alpha_c09*ss
 989 ! jmb : overflow if alphs2 > 100
 990          if (alphs2 > 100.0 ) alphs2=100.0
 991          alphmu=alpha_c09*mu_c09
 992          dfxdss= mu_c09*exp(-alphs2)*(one-alphs2)+&
 993 &         kappa*alpha_c09*exp(-alphs2/two)/two
 994          d2fxdss2=-alphmu*exp(-alphs2)*(two-alphs2)-&
 995 &         kappa*(alpha_c09**two)*exp(alphs2/two)/four
 996          fx    = one+mu_c09*ss*exp(-alphs2)+kappa*(one-exp(-alphs2/two))
 997          ex_gga= ex_lsd*fx
 998          dssdn=-eight*third*ss*rho_inv
 999          dfxdn  = dfxdss*dssdn
1000          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
1001 !
1002 !        The new definition (v3.3) includes the division by the norm of the gradient
1003          dssdg =two*coeffss
1004          dfxdg=dfxdss*dssdg
1005          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg !here also
1006          exc=exc+ex_gga*rho
1007 
1008 !        end of loop over the spin
1009 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1010          exc=exc*2
1011          exci(ipts)=exc*rhotot_inv
1012 
1013 !        Correlation has been added
1014 !        -----------------------------------------------------------------------------
1015 
1016 !        vxci(ipts,2)=vxci(ipts,1)
1017          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
1018 
1019        end do
1020 
1021 
1022      else if(option==1)then
1023 
1024 
1025        do ipts=1,npts
1026 
1027          rhotot=rhoarr(ipts)
1028          rhotmot=rhom1_3(ipts)
1029          rhotot_inv=rhotmot*rhotmot*rhotmot
1030          rhotmo6=sqrt(rhotmot)
1031          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1032 !        -----------------------------------------------------------------------
1033 !        First take care of the exchange part of the functional
1034 
1035          exc=zero
1036 !        loop over the spin
1037          ispden=1
1038          rho   =rho_updn(ipts,ispden)
1039          rhomot=rho_updnm1_3(ipts,ispden)
1040          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1041 !        Perdew_Wang 91 LSD
1042          vxci(ipts,ispden)=four_thirds*ex_lsd
1043          exc=exc+ex_lsd*rho
1044 
1045 !        end of loop over the spin
1046 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1047          exc=exc*2
1048          exci(ipts)=exc*rhotot_inv
1049 !        -----------------------------------------------------------------------------
1050 !        Then takes care of the LSD correlation part of the functional
1051 
1052 
1053          rs=rsfac*rhotmot
1054          sqr_rs=sq_rsfac*rhotmo6
1055          rsm1_2=sq_rsfac_inv*rhoto6
1056 
1057 !        Formulas A6-A8 of PW92LSD
1058          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
1059          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
1060          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
1061          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
1062 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
1063          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
1064          ecrs0=ec0_q0*ec0_log
1065          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
1066 
1067          ecrs=ecrs0
1068          decrs_drs=decrs0_drs
1069          decrs_dzeta=0.0_dp
1070          zeta=0.0_dp
1071 
1072 !        Add LSD correlation functional to GGA exchange functional
1073          exci(ipts)=exci(ipts)+ecrs
1074          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
1075 
1076 !        Correlation has been added
1077 !        -----------------------------------------------------------------------------
1078 
1079        end do
1080 
1081      else if (option==3) then
1082        do ipts=1,npts
1083 
1084          rhotot=rhoarr(ipts)
1085          rhotmot=rhom1_3(ipts)
1086          rhotot_inv=rhotmot*rhotmot*rhotmot
1087          rhotmo6=sqrt(rhotmot)
1088          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1089 !        -----------------------------------------------------------------------
1090 !        First take care of the exchange part of the functional
1091 
1092          exc=zero
1093 !        loop over the spin
1094          ispden=1
1095          rho   =rho_updn(ipts,ispden)
1096          rhomot=rho_updnm1_3(ipts,ispden)
1097          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1098 !        Perdew_Wang 91 LSD
1099          vxci(ipts,ispden)=four_thirds*ex_lsd
1100          exc=exc+ex_lsd*rho
1101 
1102 !        end of loop over the spin
1103 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1104          exc=exc*2
1105          exci(ipts)=exc*rhotot_inv
1106 !        -----------------------------------------------------------------------------
1107 !        Then takes care of the LSD correlation part of the functional
1108 
1109 
1110          rs=rsfac*rhotmot
1111          sqr_rs=sq_rsfac*rhotmo6
1112          rsm1_2=sq_rsfac_inv*rhoto6
1113 
1114 !        Formulas A6-A8 of PW92LSD
1115          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
1116          sqr_sqr_rs=max(1.e-15_dp,sqrt(sqr_rs))
1117          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs/sqr_sqr_rs)
1118          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+3.5_dp*ec0_b4*rs/sqr_sqr_rs)
1119          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
1120 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
1121          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
1122          ecrs0=ec0_q0*ec0_log
1123          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
1124 
1125          ecrs=ecrs0
1126          decrs_drs=decrs0_drs
1127          decrs_dzeta=0.0_dp
1128          zeta=0.0_dp
1129 
1130 !        Add LSD correlation functional to GGA exchange functional
1131          exci(ipts)=exci(ipts)+ecrs
1132          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
1133 
1134 !        Correlation has been added
1135 !        -----------------------------------------------------------------------------
1136 
1137        end do
1138 
1139 
1140      end if
1141 
1142    else if (order==3) then
1143 !    separate cases with respect to option
1144      if(option==2 .or. option==5) then
1145 
1146        do ipts=1,npts
1147 
1148          rhotot=rhoarr(ipts)
1149          rhotmot=rhom1_3(ipts)
1150          rhotot_inv=rhotmot*rhotmot*rhotmot
1151          rhotmo6=sqrt(rhotmot)
1152          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1153 !        -----------------------------------------------------------------------
1154 !        First take care of the exchange part of the functional
1155 
1156          exc=zero
1157          dvxcdgr(ipts,3)=zero
1158 !        loop over the spin
1159          ispden=1
1160          rho   =rho_updn(ipts,ispden)
1161          rhomot=rho_updnm1_3(ipts,ispden)
1162          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1163 !        Perdew-Burke-Ernzerhof GGA, exchange part
1164          rho_inv=rhomot*rhomot*rhomot
1165          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
1166          ss=grho2_updn(ipts,ispden)*coeffss
1167          divss=one/(one+mu_divkappa*ss)
1168          dfxdss= mu*divss*divss
1169          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
1170          fx    = one+kappa*(one-divss)
1171          ex_gga= ex_lsd*fx
1172          dssdn=-eight*third*ss*rho_inv
1173          dfxdn  = dfxdss*dssdn
1174          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
1175 !        The new definition (v3.3) includes the division by the norm of the gradient
1176          dssdg =two*coeffss
1177          dfxdg=dfxdss*dssdg
1178          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
1179          exc=exc+ex_gga*rho
1180 
1181 !        Perdew-Burke-Ernzerhof GGA, exchange part
1182 !        Components 3 or 4
1183          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
1184 !        Components 1 or 2
1185          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
1186          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
1187          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
1188 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
1189 !        Components 5 or 6
1190          d2ssdndg=-eight*third*dssdg*rho_inv
1191          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
1192          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
1193 !        Components 7 or 8
1194          d2fxdg2=d2fxdss2*dssdg**2
1195          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
1196 !        For the time being, treat non-spin-polarized like spin-polarized
1197          dvxci(ipts,2)=dvxci(ipts,1)
1198          dvxci(ipts,4)=dvxci(ipts,3)
1199          dvxci(ipts,6)=dvxci(ipts,5)
1200          dvxci(ipts,8)=dvxci(ipts,7)
1201 
1202 !        end of loop over the spin
1203 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1204          exc=exc*2
1205          exci(ipts)=exc*rhotot_inv
1206 !        -----------------------------------------------------------------------------
1207 !        Then takes care of the LSD correlation part of the functional
1208 
1209 
1210          rs=rsfac*rhotmot
1211          sqr_rs=sq_rsfac*rhotmo6
1212          rsm1_2=sq_rsfac_inv*rhoto6
1213 
1214 !        Formulas A6-A8 of PW92LSD
1215          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
1216          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
1217          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
1218          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
1219 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
1220          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
1221          ecrs0=ec0_q0*ec0_log
1222          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
1223          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
1224          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
1225 &         -ec0_q0*ec0_q1pp*ec0_den                        &
1226 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
1227          ec0_q1ppp = 0.75_dp*ec0_aa*(rsm1_2**5)*(ec0_b1-ec0_b3*rs)
1228          ec0_f1 = 1._dp/(ec0_q1*ec0_q1*(1._dp + ec0_q1))
1229          ec0_f2 = 1._dp/(ec0_q1*(1+ec0_q1))
1230          d3ecrs0_drs3 = 6._dp*ec0_q1p*ec0_f1*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + &
1231 &         ec0_q0*ec0_q1pp) - &
1232 &         ec0_f2*(-6._dp*ec0_aa*ec0_a1*ec0_q1pp + ec0_q0*ec0_q1ppp + &
1233 &         ec0_f2*(3._dp*ec0_q1p*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + ec0_q0*ec0_q1pp) + &
1234 &         ec0_f2*2._dp*ec0_q0*(ec0_q1p**3)*(1._dp + 3._dp*ec0_q1*(1._dp + ec0_q1))))
1235 
1236          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
1237          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
1238          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
1239          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
1240          mac_log=-log( mac_q1*mac_q1*mac_den )
1241          macrs=mac_q0*mac_log
1242          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
1243 
1244          ecrs=ecrs0
1245          decrs_drs=decrs0_drs
1246          decrs_dzeta=0.0_dp
1247          d2ecrs_drs2=d2ecrs0_drs2
1248          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
1249          d2ecrs_drsdzeta=zero
1250          zeta=0.0_dp
1251 
1252 
1253 !        Add LSD correlation functional to GGA exchange functional
1254          exci(ipts)=exci(ipts)+ecrs
1255          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
1256 
1257          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
1258 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
1259          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
1260          dvxci(ipts,9)=d2ecrs_drho2
1261          dvxci(ipts,10)=d2ecrs_drho2
1262          dvxci(ipts,11)=d2ecrs_drho2
1263 
1264 !        -----------------------------------------------------------------------------
1265 !        Eventually add the GGA correlation part of the PBE functional
1266 !        Note : the computation of the potential in the spin-unpolarized
1267 !        case could be optimized much further. Other optimizations are left to do.
1268 
1269          phi_zeta=1.0_dp
1270          phip_zeta=0.0_dp
1271          phi_zeta_inv=1.0_dp
1272          phi_logder=0.0_dp
1273          phi3_zeta=1.0_dp
1274          gamphi3inv=gamma_inv
1275          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
1276 
1277 !        From ec to bb
1278          bb=ecrs*gamphi3inv
1279          dbb_drs=decrs_drs*gamphi3inv
1280          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
1281          d2bb_drs2=d2ecrs_drs2*gamphi3inv
1282          d2bb_drsdzeta=gamphi3inv*(d2ecrs_drsdzeta-three*decrs_drs*phi_logder)
1283          d2bb_dzeta2=gamphi3inv*(d2ecrs_dzeta2-six*decrs_dzeta*phi_logder+&
1284 &         12.0_dp*ecrs*phi_logder*phi_logder-three*ecrs*phi_zeta_inv*phipp_zeta)
1285 
1286 !        From bb to cc
1287          exp_pbe=exp(-bb)
1288          cc=one/(exp_pbe-one)
1289          dcc_dbb=cc*cc*exp_pbe
1290          dcc_drs=dcc_dbb*dbb_drs
1291          dcc_dzeta=dcc_dbb*dbb_dzeta
1292          d2cc_dbb2=cc*cc*exp_pbe*(two*cc*exp_pbe-one)
1293          d2cc_drs2=d2cc_dbb2*dbb_drs*dbb_drs+dcc_dbb*d2bb_drs2
1294          d2cc_drsdzeta=d2cc_dbb2*dbb_drs*dbb_dzeta+dcc_dbb*d2bb_drsdzeta
1295          d2cc_dzeta2=d2cc_dbb2*dbb_dzeta*dbb_dzeta+dcc_dbb*d2bb_dzeta2
1296 
1297 !        From cc to aa
1298          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
1299          aa=coeff_aa*cc
1300          daa_drs=coeff_aa*dcc_drs
1301          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
1302          d2aa_drs2=coeff_aa*d2cc_drs2
1303          d2aa_drsdzeta=-two*daa_drs*phi_logder+coeff_aa*d2cc_drsdzeta
1304          d2aa_dzeta2=aa*(-two*phi_zeta_inv*phipp_zeta+six*phi_logder*phi_logder)+&
1305 &         coeff_aa*(-four*dcc_dzeta*phi_logder+d2cc_dzeta2)
1306 
1307 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
1308          grrho2=four*grho2_updn(ipts,1)
1309          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
1310 !        Note that tt is (the t variable of PBE divided by phi) squared
1311          tt=half*grrho2*dtt_dg
1312 
1313 !        Get xx from aa and tt
1314          xx=aa*tt
1315          dxx_drs=daa_drs*tt
1316          dxx_dzeta=daa_dzeta*tt
1317          dxx_dtt=aa
1318          d2xx_drs2=d2aa_drs2*tt
1319          d2xx_drsdzeta=d2aa_drsdzeta*tt
1320          d2xx_drsdtt=daa_drs
1321          d2xx_dttdzeta=daa_dzeta
1322          d2xx_dzeta2=d2aa_dzeta2*tt
1323 
1324 !        From xx to pade
1325          pade_den=one/(one+xx*(one+xx))
1326          pade=(one+xx)*pade_den
1327          dpade_dxx=-xx*(two+xx)*pade_den**2
1328          dpade_drs=dpade_dxx*dxx_drs
1329          dpade_dtt=dpade_dxx*dxx_dtt
1330          dpade_dzeta=dpade_dxx*dxx_dzeta
1331          d2pade_dxx2=two*(-one+xx*xx*(three+xx))*pade_den*pade_den*pade_den
1332          d2pade_drs2=d2pade_dxx2*dxx_drs*dxx_drs+dpade_dxx*d2xx_drs2
1333          d2pade_drsdtt=d2pade_dxx2*dxx_drs*dxx_dtt+dpade_dxx*d2xx_drsdtt
1334          d2pade_drsdzeta=d2pade_dxx2*dxx_drs*dxx_dzeta+dpade_dxx*d2xx_drsdzeta
1335          d2pade_dtt2=d2pade_dxx2*dxx_dtt*dxx_dtt
1336          d2pade_dttdzeta=d2pade_dxx2*dxx_dtt*dxx_dzeta+dpade_dxx*d2xx_dttdzeta
1337          d2pade_dzeta2=d2pade_dxx2*dxx_dzeta*dxx_dzeta+dpade_dxx*d2xx_dzeta2
1338 
1339 !        From pade to qq
1340          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
1341          qq=coeff_qq*pade
1342          dqq_drs=coeff_qq*dpade_drs
1343          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
1344          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
1345          d2qq_drs2=coeff_qq*d2pade_drs2
1346          d2qq_drsdtt=phi_zeta_inv*phi_zeta_inv*(dpade_drs+tt*d2pade_drsdtt)
1347          d2qq_drsdzeta=coeff_qq*(d2pade_drsdzeta-two*dpade_drs*phi_logder)
1348          d2qq_dtt2=phi_zeta_inv*phi_zeta_inv*(two*dpade_dtt+tt*d2pade_dtt2)
1349          d2qq_dttdzeta=phi_zeta_inv*phi_zeta_inv*(dpade_dzeta-two*pade*phi_logder)+&
1350 &         coeff_qq*(d2pade_dttdzeta-two*dpade_dtt*phi_logder)
1351          d2qq_dzeta2=coeff_qq*( d2pade_dzeta2-four*dpade_dzeta*phi_logder &
1352 &         +six*pade*phi_logder*phi_logder            &
1353 &         -two*pade*phi_zeta_inv*phipp_zeta)
1354 
1355 !        From qq to rr
1356          arg_rr=one+beta*gamma_inv*qq
1357          div_rr=one/arg_rr
1358          rr=gamma*log(arg_rr)
1359          drr_dqq=beta*div_rr
1360          drr_drs=drr_dqq*dqq_drs
1361          drr_dtt=drr_dqq*dqq_dtt
1362          drr_dzeta=drr_dqq*dqq_dzeta
1363          d2rr_dqq2=-div_rr**2*beta*beta*gamma_inv
1364          d2rr_drs2=d2rr_dqq2*dqq_drs*dqq_drs+drr_dqq*d2qq_drs2
1365          d2rr_drsdtt=d2rr_dqq2*dqq_drs*dqq_dtt+drr_dqq*d2qq_drsdtt
1366          d2rr_drsdzeta=d2rr_dqq2*dqq_drs*dqq_dzeta+drr_dqq*d2qq_drsdzeta
1367          d2rr_dtt2=d2rr_dqq2*dqq_dtt*dqq_dtt+drr_dqq*d2qq_dtt2
1368          d2rr_dttdzeta=d2rr_dqq2*dqq_dtt*dqq_dzeta+drr_dqq*d2qq_dttdzeta
1369          d2rr_dzeta2=d2rr_dqq2*dqq_dzeta*dqq_dzeta+drr_dqq*d2qq_dzeta2
1370 
1371 !        From rr to hh
1372          hh=phi3_zeta*rr
1373          dhh_drs=phi3_zeta*drr_drs
1374          dhh_dtt=phi3_zeta*drr_dtt
1375          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
1376          d2hh_drs2=phi3_zeta*d2rr_drs2
1377          d2hh_drsdtt=phi3_zeta*d2rr_drsdtt
1378          d2hh_drsdzeta=phi3_zeta*(d2rr_drsdzeta+three*drr_drs*phi_logder)
1379          d2hh_dtt2=phi3_zeta*d2rr_dtt2
1380          d2hh_dttdzeta=phi3_zeta*(d2rr_dttdzeta+three*drr_dtt*phi_logder)
1381          d2hh_dzeta2=phi3_zeta*(six*rr*phi_logder*phi_logder+&
1382 &         six*phi_logder*drr_dzeta+d2rr_dzeta2)  &
1383 &         +three*phi_zeta*phi_zeta*rr*phipp_zeta
1384 
1385 
1386 !        The GGA correlation energy is added
1387          exci(ipts)=exci(ipts)+hh
1388 
1389 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
1390 
1391 
1392 
1393 !        From hh to the derivative of the energy wrt the density
1394          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
1395          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
1396 
1397 !        From hh to the derivative of the energy wrt to the gradient of the
1398 !        density, divided by the gradient of the density
1399 !        (The v3.3 definition includes the division by the norm of the gradient)
1400          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
1401 
1402          d2rhohh_drho2=rhotot_inv*&
1403 &         (-two*ninth*rs*dhh_drs +seven*four*ninth*tt*dhh_dtt &
1404 &         +ninth*rs*rs*d2hh_drs2+zeta*zeta*d2hh_dzeta2+(seven*third*tt)**2*d2hh_dtt2 &
1405 &         +two*third*rs*zeta*d2hh_drsdzeta+two*seven*ninth*rs*tt*d2hh_drsdtt &
1406 &         +two*seven*third*tt*zeta*d2hh_dttdzeta)
1407          d2rhohh_drhodg=dtt_dg*(-four*third*dhh_dtt-third*rs*d2hh_drsdtt &
1408 &         -zeta*d2hh_dttdzeta-seven*third*tt*d2hh_dtt2)
1409 
1410 !        Component 12 : first derivative with respect to the gradient
1411 !        of the density, div by the grad of the density
1412          dvxci(ipts,12)=dvxcdgr(ipts,3)
1413 !        Components 9, 10 and 11 : second derivatives with respect to the spin-density
1414 !        Note that there is already a contribution from LSDA
1415          dvxci(ipts,9)=dvxci(ipts,9)+d2rhohh_drho2+rhotot_inv*           &
1416 &         ( d2hh_dzeta2*(one-two*zeta) &
1417 &         -two*third*rs*d2hh_drsdzeta-14.0_dp*third*tt*d2hh_dttdzeta)
1418          dvxci(ipts,10)=dvxci(ipts,10)+d2rhohh_drho2-rhotot_inv*d2hh_dzeta2
1419          dvxci(ipts,11)=dvxci(ipts,11)+d2rhohh_drho2+rhotot_inv*           &
1420 &         ( d2hh_dzeta2*(one+two*zeta) &
1421 &         +two*third*rs*d2hh_drsdzeta+14.0_dp*third*tt*d2hh_dttdzeta)
1422 !        Components 13 and 14 : second derivatives with respect to spin density
1423 !        and gradient, divided by the gradient
1424          dvxci(ipts,13)=d2rhohh_drhodg+dtt_dg*d2hh_dttdzeta
1425          dvxci(ipts,14)=d2rhohh_drhodg-dtt_dg*d2hh_dttdzeta
1426 !        Component 15 : derivative of the (derivative wrt the gradient div by the grad),
1427 !        divided by the grad
1428          dvxci(ipts,15)=rhotot*d2hh_dtt2*dtt_dg*dtt_dg
1429 
1430 
1431 !        End condition of GGA
1432 
1433 !        Correlation has been added
1434 !        -----------------------------------------------------------------------------
1435 
1436 !        vxci(ipts,2)=vxci(ipts,1)
1437          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
1438 
1439        end do
1440      else if ((option==6) .or. (option==7)) then
1441 
1442        do ipts=1,npts
1443 
1444          rhotot=rhoarr(ipts)
1445          rhotmot=rhom1_3(ipts)
1446          rhotot_inv=rhotmot*rhotmot*rhotmot
1447          rhotmo6=sqrt(rhotmot)
1448          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1449 !        -----------------------------------------------------------------------
1450 !        First take care of the exchange part of the functional
1451 
1452          exc=zero
1453          dvxcdgr(ipts,3)=zero
1454 !        loop over the spin
1455          ispden=1
1456          rho   =rho_updn(ipts,ispden)
1457          rhomot=rho_updnm1_3(ipts,ispden)
1458          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1459 !        Perdew-Burke-Ernzerhof GGA, exchange part
1460          rho_inv=rhomot*rhomot*rhomot
1461          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
1462          ss=grho2_updn(ipts,ispden)*coeffss
1463 
1464          if (option==6) then
1465            divss=exp(-mu_divkappa*ss)
1466            dfxdss= mu*divss
1467            d2fxdss2=-mu*mu_divkappa*divss
1468 
1469            fx    = one+kappa*(one-divss)
1470            ex_gga= ex_lsd*fx
1471            dssdn=-eight*third*ss*rho_inv
1472            dfxdn  = dfxdss*dssdn
1473            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
1474 !          The new definition (v3.3) includes the division by the norm of the gradient
1475            dssdg =two*coeffss
1476            dfxdg=dfxdss*dssdg
1477            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
1478            exc=exc+ex_gga*rho
1479 !          This is the Wu and Cohen modification
1480          else
1481            expss=exp(-ss)
1482            p1_wc=b_wc+(mu-b_wc)*(one-ss)*expss+two*c_wc*ss/(one+c_wc*ss*ss)
1483            p2_wc=d_wc*(ss-two)*expss+two*c_wc/(one+c_wc*ss*ss)-&
1484 &           four*c_wc*c_wc*ss*ss/((one+c_wc*ss*ss)*(one+c_wc*ss*ss))
1485            divss=one/(one+(b_wc*ss+d_wc*ss*expss+log(one+c_wc*ss*ss))/kappa)
1486            dfxdss=p1_wc*divss*divss
1487            d2fxdss2=p2_wc*divss*divss-two*divss*divss*divss*p1_wc*p1_wc/kappa
1488 
1489            fx    = one+kappa*(one-divss)
1490            ex_gga= ex_lsd*fx
1491            dssdn=-eight*third*ss*rho_inv
1492            dfxdn  = dfxdss*dssdn
1493            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
1494 !          The new definition (v3.3) includes the division by the norm of the gradient
1495            dssdg =two*coeffss
1496            dfxdg=dfxdss*dssdg
1497            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
1498            exc=exc+ex_gga*rho
1499          end if
1500 
1501 !        Perdew-Burke-Ernzerhof GGA, exchange part
1502 !        Components 3 or 4
1503          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
1504 !        Components 1 or 2
1505          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
1506          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
1507          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
1508 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
1509 !        Components 5 or 6
1510          d2ssdndg=-eight*third*dssdg*rho_inv
1511          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
1512          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
1513 !        Components 7 or 8
1514          d2fxdg2=d2fxdss2*dssdg**2
1515          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
1516 !        For the time being, treat non-spin-polarized like spin-polarized
1517          dvxci(ipts,2)=dvxci(ipts,1)
1518          dvxci(ipts,4)=dvxci(ipts,3)
1519          dvxci(ipts,6)=dvxci(ipts,5)
1520          dvxci(ipts,8)=dvxci(ipts,7)
1521 
1522 !        end of loop over the spin
1523 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1524          exc=exc*2
1525          exci(ipts)=exc*rhotot_inv
1526 !        -----------------------------------------------------------------------------
1527 !        Then takes care of the LSD correlation part of the functional
1528 
1529 
1530          rs=rsfac*rhotmot
1531          sqr_rs=sq_rsfac*rhotmo6
1532          rsm1_2=sq_rsfac_inv*rhoto6
1533 
1534 !        Formulas A6-A8 of PW92LSD
1535          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
1536          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
1537          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
1538          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
1539 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
1540          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
1541          ecrs0=ec0_q0*ec0_log
1542          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
1543          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
1544          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
1545 &         -ec0_q0*ec0_q1pp*ec0_den                        &
1546 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
1547          ec0_q1ppp = 0.75_dp*ec0_aa*(rsm1_2**5)*(ec0_b1-ec0_b3*rs)
1548          ec0_f1 = 1._dp/(ec0_q1*ec0_q1*(1._dp + ec0_q1))
1549          ec0_f2 = 1._dp/(ec0_q1*(1+ec0_q1))
1550          d3ecrs0_drs3 = 6._dp*ec0_q1p*ec0_f1*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + &
1551 &         ec0_q0*ec0_q1pp) - &
1552 &         ec0_f2*(-6._dp*ec0_aa*ec0_a1*ec0_q1pp + ec0_q0*ec0_q1ppp + &
1553 &         ec0_f2*(3._dp*ec0_q1p*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + ec0_q0*ec0_q1pp) + &
1554 &         ec0_f2*2._dp*ec0_q0*(ec0_q1p**3)*(1._dp + 3._dp*ec0_q1*(1._dp + ec0_q1))))
1555 
1556          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
1557          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
1558          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
1559          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
1560          mac_log=-log( mac_q1*mac_q1*mac_den )
1561          macrs=mac_q0*mac_log
1562          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
1563 
1564          ecrs=ecrs0
1565          decrs_drs=decrs0_drs
1566          decrs_dzeta=0.0_dp
1567          d2ecrs_drs2=d2ecrs0_drs2
1568          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
1569          d2ecrs_drsdzeta=zero
1570          zeta=0.0_dp
1571 
1572 
1573 !        Add LSD correlation functional to GGA exchange functional
1574          exci(ipts)=exci(ipts)+ecrs
1575          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
1576 
1577          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
1578 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
1579          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
1580          dvxci(ipts,9)=d2ecrs_drho2
1581          dvxci(ipts,10)=d2ecrs_drho2
1582          dvxci(ipts,11)=d2ecrs_drho2
1583 
1584 !        -----------------------------------------------------------------------------
1585 !        Eventually add the GGA correlation part of the PBE functional
1586 !        Note : the computation of the potential in the spin-unpolarized
1587 !        case could be optimized much further. Other optimizations are left to do.
1588 
1589          phi_zeta=1.0_dp
1590          phip_zeta=0.0_dp
1591          phi_zeta_inv=1.0_dp
1592          phi_logder=0.0_dp
1593          phi3_zeta=1.0_dp
1594          gamphi3inv=gamma_inv
1595          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
1596 
1597 !        From ec to bb
1598          bb=ecrs*gamphi3inv
1599          dbb_drs=decrs_drs*gamphi3inv
1600          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
1601          d2bb_drs2=d2ecrs_drs2*gamphi3inv
1602          d2bb_drsdzeta=gamphi3inv*(d2ecrs_drsdzeta-three*decrs_drs*phi_logder)
1603          d2bb_dzeta2=gamphi3inv*(d2ecrs_dzeta2-six*decrs_dzeta*phi_logder+&
1604 &         12.0_dp*ecrs*phi_logder*phi_logder-three*ecrs*phi_zeta_inv*phipp_zeta)
1605 
1606 !        From bb to cc
1607          exp_pbe=exp(-bb)
1608          cc=one/(exp_pbe-one)
1609          dcc_dbb=cc*cc*exp_pbe
1610          dcc_drs=dcc_dbb*dbb_drs
1611          dcc_dzeta=dcc_dbb*dbb_dzeta
1612          d2cc_dbb2=cc*cc*exp_pbe*(two*cc*exp_pbe-one)
1613          d2cc_drs2=d2cc_dbb2*dbb_drs*dbb_drs+dcc_dbb*d2bb_drs2
1614          d2cc_drsdzeta=d2cc_dbb2*dbb_drs*dbb_dzeta+dcc_dbb*d2bb_drsdzeta
1615          d2cc_dzeta2=d2cc_dbb2*dbb_dzeta*dbb_dzeta+dcc_dbb*d2bb_dzeta2
1616 
1617 !        From cc to aa
1618          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
1619          aa=coeff_aa*cc
1620          daa_drs=coeff_aa*dcc_drs
1621          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
1622          d2aa_drs2=coeff_aa*d2cc_drs2
1623          d2aa_drsdzeta=-two*daa_drs*phi_logder+coeff_aa*d2cc_drsdzeta
1624          d2aa_dzeta2=aa*(-two*phi_zeta_inv*phipp_zeta+six*phi_logder*phi_logder)+&
1625 &         coeff_aa*(-four*dcc_dzeta*phi_logder+d2cc_dzeta2)
1626 
1627 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
1628          grrho2=four*grho2_updn(ipts,1)
1629          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
1630 !        Note that tt is (the t variable of PBE divided by phi) squared
1631          tt=half*grrho2*dtt_dg
1632 
1633 !        Get xx from aa and tt
1634          xx=aa*tt
1635          dxx_drs=daa_drs*tt
1636          dxx_dzeta=daa_dzeta*tt
1637          dxx_dtt=aa
1638          d2xx_drs2=d2aa_drs2*tt
1639          d2xx_drsdzeta=d2aa_drsdzeta*tt
1640          d2xx_drsdtt=daa_drs
1641          d2xx_dttdzeta=daa_dzeta
1642          d2xx_dzeta2=d2aa_dzeta2*tt
1643 
1644 !        From xx to pade
1645          pade_den=one/(one+xx*(one+xx))
1646          pade=(one+xx)*pade_den
1647          dpade_dxx=-xx*(two+xx)*pade_den**2
1648          dpade_drs=dpade_dxx*dxx_drs
1649          dpade_dtt=dpade_dxx*dxx_dtt
1650          dpade_dzeta=dpade_dxx*dxx_dzeta
1651          d2pade_dxx2=two*(-one+xx*xx*(three+xx))*pade_den*pade_den*pade_den
1652          d2pade_drs2=d2pade_dxx2*dxx_drs*dxx_drs+dpade_dxx*d2xx_drs2
1653          d2pade_drsdtt=d2pade_dxx2*dxx_drs*dxx_dtt+dpade_dxx*d2xx_drsdtt
1654          d2pade_drsdzeta=d2pade_dxx2*dxx_drs*dxx_dzeta+dpade_dxx*d2xx_drsdzeta
1655          d2pade_dtt2=d2pade_dxx2*dxx_dtt*dxx_dtt
1656          d2pade_dttdzeta=d2pade_dxx2*dxx_dtt*dxx_dzeta+dpade_dxx*d2xx_dttdzeta
1657          d2pade_dzeta2=d2pade_dxx2*dxx_dzeta*dxx_dzeta+dpade_dxx*d2xx_dzeta2
1658 
1659 !        From pade to qq
1660          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
1661          qq=coeff_qq*pade
1662          dqq_drs=coeff_qq*dpade_drs
1663          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
1664          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
1665          d2qq_drs2=coeff_qq*d2pade_drs2
1666          d2qq_drsdtt=phi_zeta_inv*phi_zeta_inv*(dpade_drs+tt*d2pade_drsdtt)
1667          d2qq_drsdzeta=coeff_qq*(d2pade_drsdzeta-two*dpade_drs*phi_logder)
1668          d2qq_dtt2=phi_zeta_inv*phi_zeta_inv*(two*dpade_dtt+tt*d2pade_dtt2)
1669          d2qq_dttdzeta=phi_zeta_inv*phi_zeta_inv*(dpade_dzeta-two*pade*phi_logder)+&
1670 &         coeff_qq*(d2pade_dttdzeta-two*dpade_dtt*phi_logder)
1671          d2qq_dzeta2=coeff_qq*( d2pade_dzeta2-four*dpade_dzeta*phi_logder &
1672 &         +six*pade*phi_logder*phi_logder            &
1673 &         -two*pade*phi_zeta_inv*phipp_zeta)
1674 
1675 !        From qq to rr
1676          arg_rr=one+beta*gamma_inv*qq
1677          div_rr=one/arg_rr
1678          rr=gamma*log(arg_rr)
1679          drr_dqq=beta*div_rr
1680          drr_drs=drr_dqq*dqq_drs
1681          drr_dtt=drr_dqq*dqq_dtt
1682          drr_dzeta=drr_dqq*dqq_dzeta
1683          d2rr_dqq2=-div_rr**2*beta*beta*gamma_inv
1684          d2rr_drs2=d2rr_dqq2*dqq_drs*dqq_drs+drr_dqq*d2qq_drs2
1685          d2rr_drsdtt=d2rr_dqq2*dqq_drs*dqq_dtt+drr_dqq*d2qq_drsdtt
1686          d2rr_drsdzeta=d2rr_dqq2*dqq_drs*dqq_dzeta+drr_dqq*d2qq_drsdzeta
1687          d2rr_dtt2=d2rr_dqq2*dqq_dtt*dqq_dtt+drr_dqq*d2qq_dtt2
1688          d2rr_dttdzeta=d2rr_dqq2*dqq_dtt*dqq_dzeta+drr_dqq*d2qq_dttdzeta
1689          d2rr_dzeta2=d2rr_dqq2*dqq_dzeta*dqq_dzeta+drr_dqq*d2qq_dzeta2
1690 
1691 !        From rr to hh
1692          hh=phi3_zeta*rr
1693          dhh_drs=phi3_zeta*drr_drs
1694          dhh_dtt=phi3_zeta*drr_dtt
1695          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
1696          d2hh_drs2=phi3_zeta*d2rr_drs2
1697          d2hh_drsdtt=phi3_zeta*d2rr_drsdtt
1698          d2hh_drsdzeta=phi3_zeta*(d2rr_drsdzeta+three*drr_drs*phi_logder)
1699          d2hh_dtt2=phi3_zeta*d2rr_dtt2
1700          d2hh_dttdzeta=phi3_zeta*(d2rr_dttdzeta+three*drr_dtt*phi_logder)
1701          d2hh_dzeta2=phi3_zeta*(six*rr*phi_logder*phi_logder+&
1702 &         six*phi_logder*drr_dzeta+d2rr_dzeta2)  &
1703 &         +three*phi_zeta*phi_zeta*rr*phipp_zeta
1704 
1705 
1706 !        The GGA correlation energy is added
1707          exci(ipts)=exci(ipts)+hh
1708 
1709 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
1710 
1711 !        From hh to the derivative of the energy wrt the density
1712          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
1713          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
1714 
1715 !        From hh to the derivative of the energy wrt to the gradient of the
1716 !        density, divided by the gradient of the density
1717 !        (The v3.3 definition includes the division by the norm of the gradient)
1718          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
1719 
1720          d2rhohh_drho2=rhotot_inv*&
1721 &         (-two*ninth*rs*dhh_drs +seven*four*ninth*tt*dhh_dtt &
1722 &         +ninth*rs*rs*d2hh_drs2+zeta*zeta*d2hh_dzeta2+(seven*third*tt)**2*d2hh_dtt2 &
1723 &         +two*third*rs*zeta*d2hh_drsdzeta+two*seven*ninth*rs*tt*d2hh_drsdtt &
1724 &         +two*seven*third*tt*zeta*d2hh_dttdzeta)
1725          d2rhohh_drhodg=dtt_dg*(-four*third*dhh_dtt-third*rs*d2hh_drsdtt &
1726 &         -zeta*d2hh_dttdzeta-seven*third*tt*d2hh_dtt2)
1727 
1728 !        Component 12 : first derivative with respect to the gradient
1729 !        of the density, div by the grad of the density
1730          dvxci(ipts,12)=dvxcdgr(ipts,3)
1731 !        Components 9, 10 and 11 : second derivatives with respect to the spin-density
1732 !        Note that there is already a contribution from LSDA
1733          dvxci(ipts,9)=dvxci(ipts,9)+d2rhohh_drho2+rhotot_inv*           &
1734 &         ( d2hh_dzeta2*(one-two*zeta) &
1735 &         -two*third*rs*d2hh_drsdzeta-14.0_dp*third*tt*d2hh_dttdzeta)
1736          dvxci(ipts,10)=dvxci(ipts,10)+d2rhohh_drho2-rhotot_inv*d2hh_dzeta2
1737          dvxci(ipts,11)=dvxci(ipts,11)+d2rhohh_drho2+rhotot_inv*           &
1738 &         ( d2hh_dzeta2*(one+two*zeta) &
1739 &         +two*third*rs*d2hh_drsdzeta+14.0_dp*third*tt*d2hh_dttdzeta)
1740 !        Components 13 and 14 : second derivatives with respect to spin density
1741 !        and gradient, divided by the gradient
1742          dvxci(ipts,13)=d2rhohh_drhodg+dtt_dg*d2hh_dttdzeta
1743          dvxci(ipts,14)=d2rhohh_drhodg-dtt_dg*d2hh_dttdzeta
1744 !        Component 15 : derivative of the (derivative wrt the gradient div by the grad),
1745 !        divided by the grad
1746          dvxci(ipts,15)=rhotot*d2hh_dtt2*dtt_dg*dtt_dg
1747 
1748 
1749 !        End condition of GGA
1750 
1751 
1752 !        Correlation has been added
1753 !        -----------------------------------------------------------------------------
1754 
1755 !        vxci(ipts,2)=vxci(ipts,1)
1756          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
1757        end do
1758 
1759      else if (option==-1) then
1760 
1761        do ipts=1,npts
1762 
1763          rhotot=rhoarr(ipts)
1764          rhotmot=rhom1_3(ipts)
1765          rhotot_inv=rhotmot*rhotmot*rhotmot
1766          rhotmo6=sqrt(rhotmot)
1767          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1768 !        -----------------------------------------------------------------------
1769 !        First take care of the exchange part of the functional
1770 
1771          exc=zero
1772 !        loop over the spin
1773          ispden=1
1774          rho   =rho_updn(ipts,ispden)
1775          rhomot=rho_updnm1_3(ipts,ispden)
1776          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1777 !        Perdew_Wang 91 LSD
1778          vxci(ipts,ispden)=four_thirds*ex_lsd
1779          exc=exc+ex_lsd*rho
1780 !        Perdew_Wang 91 LSD
1781          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
1782 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
1783          dvxci(ipts,2)=zero
1784 !        If non-spin-polarized, first component of dvxci is second
1785 !        derivative with respect to TOTAL density.
1786          dvxci(ipts,1)=dvxci(ipts,1)*half
1787          if(order==3)then
1788 !          Compute the second derivative of vx
1789 !          vx^(2) = -2*vx^(1)/(3*rhotot)
1790            d2vxci(ipts,1) = -2._dp*dvxci(ipts,1)/(3._dp*rhotot)
1791          end if
1792 !        end of loop over the spin
1793 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1794          exc=exc*2
1795          exci(ipts)=exc*rhotot_inv
1796 !        -----------------------------------------------------------------------------
1797 !        Then takes care of the LSD correlation part of the functional
1798 
1799 !        Correlation has been added
1800 !        -----------------------------------------------------------------------------
1801 
1802        end do
1803      else if (option==-2) then
1804 
1805        do ipts=1,npts
1806 
1807          rhotot=rhoarr(ipts)
1808          rhotmot=rhom1_3(ipts)
1809          rhotot_inv=rhotmot*rhotmot*rhotmot
1810          rhotmo6=sqrt(rhotmot)
1811          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1812 !        -----------------------------------------------------------------------
1813 !        First take care of the exchange part of the functional
1814 
1815          exc=zero
1816          dvxcdgr(ipts,3)=zero
1817 !        loop over the spin
1818          ispden=1
1819          rho   =rho_updn(ipts,ispden)
1820          rhomot=rho_updnm1_3(ipts,ispden)
1821          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1822 !        Perdew-Burke-Ernzerhof GGA, exchange part
1823          rho_inv=rhomot*rhomot*rhomot
1824          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
1825          ss=grho2_updn(ipts,ispden)*coeffss
1826          divss=one/(one+mu_divkappa*ss)
1827          dfxdss= mu*divss*divss
1828          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
1829          fx    = one+kappa*(one-divss)
1830          ex_gga= ex_lsd*fx
1831          dssdn=-eight*third*ss*rho_inv
1832          dfxdn  = dfxdss*dssdn
1833          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
1834 !        The new definition (v3.3) includes the division by the norm of the gradient
1835          dssdg =two*coeffss
1836          dfxdg=dfxdss*dssdg
1837          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
1838          exc=exc+ex_gga*rho
1839 
1840 !        Perdew-Burke-Ernzerhof GGA, exchange part
1841 !        Components 3 or 4
1842          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
1843 !        Components 1 or 2
1844          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
1845          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
1846          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
1847 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
1848 !        Components 5 or 6
1849          d2ssdndg=-eight*third*dssdg*rho_inv
1850          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
1851          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
1852 !        Components 7 or 8
1853          d2fxdg2=d2fxdss2*dssdg**2
1854          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
1855 !        For the time being, treat non-spin-polarized like spin-polarized
1856          dvxci(ipts,2)=dvxci(ipts,1)
1857          dvxci(ipts,4)=dvxci(ipts,3)
1858          dvxci(ipts,6)=dvxci(ipts,5)
1859          dvxci(ipts,8)=dvxci(ipts,7)
1860 
1861 !        end of loop over the spin
1862 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1863          exc=exc*2
1864          exci(ipts)=exc*rhotot_inv
1865 !        -----------------------------------------------------------------------------
1866 !        Then takes care of the LSD correlation part of the functional
1867 
1868 !        Correlation has been added
1869 !        -----------------------------------------------------------------------------
1870 
1871 !        vxci(ipts,2)=vxci(ipts,1)
1872          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
1873 
1874        end do
1875 
1876 
1877      else if(option==-4) then
1878 
1879 
1880        do ipts=1,npts
1881 
1882          rhotot=rhoarr(ipts)
1883          rhotmot=rhom1_3(ipts)
1884          rhotot_inv=rhotmot*rhotmot*rhotmot
1885          rhotmo6=sqrt(rhotmot)
1886          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1887 !        -----------------------------------------------------------------------
1888 !        First take care of the exchange part of the functional
1889 
1890          exc=zero
1891          dvxcdgr(ipts,3)=zero
1892 !        loop over the spin
1893          ispden=1
1894          rho   =rho_updn(ipts,ispden)
1895          rhomot=rho_updnm1_3(ipts,ispden)
1896          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1897 !        VALENTINO R. COOPER C09x GGA, This is an exchange term proposed
1898 !        to use together with vdw-DF (see above).
1899          rho_inv=rhomot*rhomot*rhomot
1900          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
1901 !        the quarter that is lacking is compensated by the grho2_updn in the
1902 !        next line.
1903          ss=grho2_updn(ipts,ispden)*coeffss
1904          alphs2=alpha_c09*ss
1905          alphmu=alpha_c09*mu_c09
1906          dfxdss= mu_c09*exp(-alphs2)*(one-alphs2)+&
1907 &         kappa*alpha_c09*exp(-alphs2/two)/two
1908          d2fxdss2=-alphmu*exp(-alphs2)*(two-alphs2)-&
1909 &         kappa*(alpha_c09**two)*exp(alphs2/two)/four
1910          fx    = one+mu_c09*ss*exp(-alphs2)+kappa*(one-exp(-alphs2/two))
1911          ex_gga= ex_lsd*fx
1912          dssdn=-eight*third*ss*rho_inv
1913          dfxdn  = dfxdss*dssdn
1914          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
1915 !        The new definition (v3.3) includes the division by the norm of the gradient
1916          dssdg =two*coeffss
1917          dfxdg=dfxdss*dssdg
1918          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
1919          exc=exc+ex_gga*rho
1920 
1921 !        Cooper C09x GGA exchange
1922 !        Components 3 or 4
1923          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
1924 !        Components 1 or 2
1925          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
1926          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
1927          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
1928 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
1929 !        Components 5 or 6
1930          d2ssdndg=-eight*third*dssdg*rho_inv
1931          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
1932          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
1933 !        Components 7 or 8
1934          d2fxdg2=d2fxdss2*dssdg**2
1935          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
1936 !        For the time being, treat non-spin-polarized like spin-polarized
1937          dvxci(ipts,2)=dvxci(ipts,1)
1938          dvxci(ipts,4)=dvxci(ipts,3)
1939          dvxci(ipts,6)=dvxci(ipts,5)
1940          dvxci(ipts,8)=dvxci(ipts,7)
1941 
1942 !        end of loop over the spin
1943 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1944          exc=exc*2
1945          exci(ipts)=exc*rhotot_inv
1946 !        -----------------------------------------------------------------------------
1947 !        Then takes care of the LSD correlation part of the functional
1948 
1949 !        Correlation has been added
1950 !        -----------------------------------------------------------------------------
1951 
1952 !        vxci(ipts,2)=vxci(ipts,1)
1953          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
1954 
1955        end do
1956 
1957 
1958      else if(option==1) then
1959 
1960        do ipts=1,npts
1961 
1962 
1963          rhotot=rhoarr(ipts)
1964          rhotmot=rhom1_3(ipts)
1965          rhotot_inv=rhotmot*rhotmot*rhotmot
1966          rhotmo6=sqrt(rhotmot)
1967          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
1968 !        -----------------------------------------------------------------------
1969 !        First take care of the exchange part of the functional
1970 
1971          exc=zero
1972 !        loop over the spin
1973          ispden=1
1974          rho   =rho_updn(ipts,ispden)
1975          rhomot=rho_updnm1_3(ipts,ispden)
1976          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
1977 !        Perdew_Wang 91 LSD
1978          vxci(ipts,ispden)=four_thirds*ex_lsd
1979          exc=exc+ex_lsd*rho
1980 
1981 !        Perdew_Wang 91 LSD
1982          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
1983 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
1984          dvxci(ipts,2)=zero
1985 !        If non-spin-polarized, first component of dvxci is second
1986 !        derivative with respect to TOTAL density.
1987          dvxci(ipts,1)=dvxci(ipts,1)*half
1988          if(order==3)then
1989 !          Compute the second derivative of vx
1990 !          vx^(2) = -2*vx^(1)/(3*rhotot)
1991            d2vxci(ipts,1) = -2._dp*dvxci(ipts,1)/(3._dp*rhotot)
1992          end if
1993 !        end of loop over the spin
1994 !        If non spin-polarized, treat spin down contribution now, similar to spin up
1995          exc=exc*2
1996          exci(ipts)=exc*rhotot_inv
1997 !        -----------------------------------------------------------------------------
1998 !        Then takes care of the LSD correlation part of the functional
1999 
2000          rs=rsfac*rhotmot
2001          sqr_rs=sq_rsfac*rhotmo6
2002          rsm1_2=sq_rsfac_inv*rhoto6
2003 
2004 !        Formulas A6-A8 of PW92LSD
2005          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
2006          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
2007          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
2008          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
2009 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
2010          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
2011          ecrs0=ec0_q0*ec0_log
2012          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
2013          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
2014          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
2015 &         -ec0_q0*ec0_q1pp*ec0_den                        &
2016 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
2017          ec0_q1ppp = 0.75_dp*ec0_aa*(rsm1_2**5)*(ec0_b1-ec0_b3*rs)
2018          ec0_f1 = 1._dp/(ec0_q1*ec0_q1*(1._dp + ec0_q1))
2019          ec0_f2 = 1._dp/(ec0_q1*(1+ec0_q1))
2020          d3ecrs0_drs3 = 6._dp*ec0_q1p*ec0_f1*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + &
2021 &         ec0_q0*ec0_q1pp) - &
2022 &         ec0_f2*(-6._dp*ec0_aa*ec0_a1*ec0_q1pp + ec0_q0*ec0_q1ppp + &
2023 &         ec0_f2*(3._dp*ec0_q1p*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + ec0_q0*ec0_q1pp) + &
2024 &         ec0_f2*2._dp*ec0_q0*(ec0_q1p**3)*(1._dp + 3._dp*ec0_q1*(1._dp + ec0_q1))))
2025 
2026          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
2027          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
2028          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
2029          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
2030          mac_log=-log( mac_q1*mac_q1*mac_den )
2031          macrs=mac_q0*mac_log
2032          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
2033 
2034          ecrs=ecrs0
2035          decrs_drs=decrs0_drs
2036          decrs_dzeta=0.0_dp
2037          d2ecrs_drs2=d2ecrs0_drs2
2038          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
2039          d2ecrs_drsdzeta=zero
2040          zeta=0.0_dp
2041 
2042 
2043 !        Add LSD correlation functional to GGA exchange functional
2044          exci(ipts)=exci(ipts)+ecrs
2045          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
2046 
2047          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
2048 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
2049          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
2050          dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drho2
2051          d2vcrs_drs2 = third*(d2ecrs_drs2 - rs*d3ecrs0_drs3)
2052          drs_dn = -1._dp*four_pi*ninth*rs**4
2053          d2rs_dn2 = 64._dp*pi*pi*(rs**7)/81._dp
2054          if(order==3)then
2055            d2vxci(ipts,1) = d2vxci(ipts,1) + d2vcrs_drs2*drs_dn*drs_dn + &
2056 &           dvcrs_drs*d2rs_dn2
2057 
2058 
2059 
2060 
2061 
2062          end if
2063 
2064 !        -----------------------------------------------------------------------------
2065 !        Eventually add the GGA correlation part of the PBE functional
2066 !        Note : the computation of the potential in the spin-unpolarized
2067 !        case could be optimized much further. Other optimizations are left to do.
2068 
2069 
2070 !        Correlation has been added
2071 !        -----------------------------------------------------------------------------
2072        end do
2073      else if (option==3) then
2074 
2075        do ipts=1,npts
2076 
2077          rhotot=rhoarr(ipts)
2078          rhotmot=rhom1_3(ipts)
2079          rhotot_inv=rhotmot*rhotmot*rhotmot
2080          rhotmo6=sqrt(rhotmot)
2081          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
2082 !        -----------------------------------------------------------------------
2083 !        First take care of the exchange part of the functional
2084 
2085          exc=zero
2086 !        loop over the spin
2087          ispden=1
2088          rho   =rho_updn(ipts,ispden)
2089          rhomot=rho_updnm1_3(ipts,ispden)
2090          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
2091 !        Perdew_Wang 91 LSD
2092          vxci(ipts,ispden)=four_thirds*ex_lsd
2093          exc=exc+ex_lsd*rho
2094 
2095 
2096 !        Perdew_Wang 91 LSD
2097          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
2098 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
2099          dvxci(ipts,2)=zero
2100 !        If non-spin-polarized, first component of dvxci is second
2101 !        derivative with respect to TOTAL density.
2102          dvxci(ipts,1)=dvxci(ipts,1)*half
2103          if(order==3)then
2104 !          Compute the second derivative of vx
2105 !          vx^(2) = -2*vx^(1)/(3*rhotot)
2106            d2vxci(ipts,1) = -2._dp*dvxci(ipts,1)/(3._dp*rhotot)
2107          end if
2108 !        end of loop over the spin
2109 !        If non spin-polarized, treat spin down contribution now, similar to spin up
2110          exc=exc*2
2111          exci(ipts)=exc*rhotot_inv
2112 !        -----------------------------------------------------------------------------
2113 !        Then takes care of the LSD correlation part of the functional
2114 
2115          rs=rsfac*rhotmot
2116          sqr_rs=sq_rsfac*rhotmo6
2117          rsm1_2=sq_rsfac_inv*rhoto6
2118 
2119 !        Formulas A6-A8 of PW92LSD
2120          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
2121          sqr_sqr_rs=max(1.e-15_dp,sqrt(sqr_rs))
2122          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs/sqr_sqr_rs)
2123          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+3.5_dp*ec0_b4*rs/sqr_sqr_rs)
2124          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
2125 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
2126          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
2127          ecrs0=ec0_q0*ec0_log
2128          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
2129          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
2130          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
2131 &         -ec0_q0*ec0_q1pp*ec0_den                        &
2132 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
2133          ec0_q1ppp = 0.75_dp*ec0_aa*(rsm1_2**5)*(ec0_b1-ec0_b3*rs)
2134          ec0_f1 = 1._dp/(ec0_q1*ec0_q1*(1._dp + ec0_q1))
2135          ec0_f2 = 1._dp/(ec0_q1*(1+ec0_q1))
2136          d3ecrs0_drs3 = 6._dp*ec0_q1p*ec0_f1*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + &
2137 &         ec0_q0*ec0_q1pp) - &
2138 &         ec0_f2*(-6._dp*ec0_aa*ec0_a1*ec0_q1pp + ec0_q0*ec0_q1ppp + &
2139 &         ec0_f2*(3._dp*ec0_q1p*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + ec0_q0*ec0_q1pp) + &
2140 &         ec0_f2*2._dp*ec0_q0*(ec0_q1p**3)*(1._dp + 3._dp*ec0_q1*(1._dp + ec0_q1))))
2141 
2142          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
2143          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
2144          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
2145          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
2146          mac_log=-log( mac_q1*mac_q1*mac_den )
2147          macrs=mac_q0*mac_log
2148          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
2149 
2150          ecrs=ecrs0
2151          decrs_drs=decrs0_drs
2152          decrs_dzeta=0.0_dp
2153          d2ecrs_drs2=d2ecrs0_drs2
2154          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
2155          d2ecrs_drsdzeta=zero
2156          zeta=0.0_dp
2157 
2158 
2159 !        Add LSD correlation functional to GGA exchange functional
2160          exci(ipts)=exci(ipts)+ecrs
2161          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
2162 
2163          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
2164 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
2165          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
2166 
2167          dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drho2
2168          d2vcrs_drs2 = third*(d2ecrs_drs2 - rs*d3ecrs0_drs3)
2169          drs_dn = -1._dp*four_pi*ninth*rs**4
2170          d2rs_dn2 = 64._dp*pi*pi*(rs**7)/81._dp
2171          if(order==3)then
2172            d2vxci(ipts,1) = d2vxci(ipts,1) + d2vcrs_drs2*drs_dn*drs_dn + &
2173 &           dvcrs_drs*d2rs_dn2
2174 
2175 
2176 
2177          end if
2178 
2179 
2180        end do
2181 
2182      end if
2183 
2184    else if(order==-2) then
2185      if(option==2 .or. option==5) then
2186        do ipts=1,npts
2187 
2188          rhotot=rhoarr(ipts)
2189          rhotmot=rhom1_3(ipts)
2190          rhotot_inv=rhotmot*rhotmot*rhotmot
2191          rhotmo6=sqrt(rhotmot)
2192          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
2193 !        -----------------------------------------------------------------------
2194 !        First take care of the exchange part of the functional
2195 
2196          exc=zero
2197          dvxcdgr(ipts,3)=zero
2198 !        loop over the spin
2199          ispden=1
2200          rho   =rho_updn(ipts,ispden)
2201          rhomot=rho_updnm1_3(ipts,ispden)
2202          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
2203 !        Perdew-Burke-Ernzerhof GGA, exchange part
2204          rho_inv=rhomot*rhomot*rhomot
2205          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
2206          ss=grho2_updn(ipts,ispden)*coeffss
2207          divss=one/(one+mu_divkappa*ss)
2208          dfxdss= mu*divss*divss
2209          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
2210          fx    = one+kappa*(one-divss)
2211          ex_gga= ex_lsd*fx
2212          dssdn=-eight*third*ss*rho_inv
2213          dfxdn  = dfxdss*dssdn
2214          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
2215 !        The new definition (v3.3) includes the division by the norm of the gradient
2216          dssdg =two*coeffss
2217          dfxdg=dfxdss*dssdg
2218          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
2219          exc=exc+ex_gga*rho
2220 
2221 !        Perdew-Burke-Ernzerhof GGA, exchange part
2222 !        Components 3 or 4
2223          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
2224 !        Components 1 or 2
2225          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
2226          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
2227          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
2228 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
2229 !        Components 5 or 6
2230          d2ssdndg=-eight*third*dssdg*rho_inv
2231          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
2232          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
2233 !        Components 7 or 8
2234          d2fxdg2=d2fxdss2*dssdg**2
2235          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
2236 !        For the time being, treat non-spin-polarized like spin-polarized
2237          dvxci(ipts,2)=dvxci(ipts,1)
2238          dvxci(ipts,4)=dvxci(ipts,3)
2239          dvxci(ipts,6)=dvxci(ipts,5)
2240          dvxci(ipts,8)=dvxci(ipts,7)
2241 !        end of loop over the spin
2242 !        If non spin-polarized, treat spin down contribution now, similar to spin up
2243          exc=exc*2
2244          exci(ipts)=exc*rhotot_inv
2245 !        -----------------------------------------------------------------------------
2246 !        Then takes care of the LSD correlation part of the functional
2247 
2248 
2249          rs=rsfac*rhotmot
2250          sqr_rs=sq_rsfac*rhotmo6
2251          rsm1_2=sq_rsfac_inv*rhoto6
2252 
2253 !        Formulas A6-A8 of PW92LSD
2254          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
2255          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
2256          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
2257          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
2258 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
2259          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
2260          ecrs0=ec0_q0*ec0_log
2261          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
2262          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
2263          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
2264 &         -ec0_q0*ec0_q1pp*ec0_den                        &
2265 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
2266 
2267          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
2268          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
2269          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
2270          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
2271          mac_log=-log( mac_q1*mac_q1*mac_den )
2272          macrs=mac_q0*mac_log
2273          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
2274 
2275          ecrs=ecrs0
2276          decrs_drs=decrs0_drs
2277          decrs_dzeta=0.0_dp
2278          d2ecrs_drs2=d2ecrs0_drs2
2279          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
2280          d2ecrs_drsdzeta=zero
2281          zeta=0.0_dp
2282 
2283 
2284 !        Add LSD correlation functional to GGA exchange functional
2285          exci(ipts)=exci(ipts)+ecrs
2286          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
2287 
2288          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
2289 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
2290          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
2291          dvxci(ipts,9)=d2ecrs_drho2
2292          dvxci(ipts,10)=d2ecrs_drho2
2293          dvxci(ipts,11)=d2ecrs_drho2
2294 
2295 !        -----------------------------------------------------------------------------
2296 !        Eventually add the GGA correlation part of the PBE functional
2297 !        Note : the computation of the potential in the spin-unpolarized
2298 !        case could be optimized much further. Other optimizations are left to do.
2299 
2300          phi_zeta=1.0_dp
2301          phip_zeta=0.0_dp
2302          phi_zeta_inv=1.0_dp
2303          phi_logder=0.0_dp
2304          phi3_zeta=1.0_dp
2305          gamphi3inv=gamma_inv
2306          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
2307 
2308 !        From ec to bb
2309          bb=ecrs*gamphi3inv
2310          dbb_drs=decrs_drs*gamphi3inv
2311          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
2312          d2bb_drs2=d2ecrs_drs2*gamphi3inv
2313          d2bb_drsdzeta=gamphi3inv*(d2ecrs_drsdzeta-three*decrs_drs*phi_logder)
2314          d2bb_dzeta2=gamphi3inv*(d2ecrs_dzeta2-six*decrs_dzeta*phi_logder+&
2315 &         12.0_dp*ecrs*phi_logder*phi_logder-three*ecrs*phi_zeta_inv*phipp_zeta)
2316 
2317 !        From bb to cc
2318          exp_pbe=exp(-bb)
2319          cc=one/(exp_pbe-one)
2320          dcc_dbb=cc*cc*exp_pbe
2321          dcc_drs=dcc_dbb*dbb_drs
2322          dcc_dzeta=dcc_dbb*dbb_dzeta
2323          d2cc_dbb2=cc*cc*exp_pbe*(two*cc*exp_pbe-one)
2324          d2cc_drs2=d2cc_dbb2*dbb_drs*dbb_drs+dcc_dbb*d2bb_drs2
2325          d2cc_drsdzeta=d2cc_dbb2*dbb_drs*dbb_dzeta+dcc_dbb*d2bb_drsdzeta
2326          d2cc_dzeta2=d2cc_dbb2*dbb_dzeta*dbb_dzeta+dcc_dbb*d2bb_dzeta2
2327 
2328 !        From cc to aa
2329          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
2330          aa=coeff_aa*cc
2331          daa_drs=coeff_aa*dcc_drs
2332          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
2333          d2aa_drs2=coeff_aa*d2cc_drs2
2334          d2aa_drsdzeta=-two*daa_drs*phi_logder+coeff_aa*d2cc_drsdzeta
2335          d2aa_dzeta2=aa*(-two*phi_zeta_inv*phipp_zeta+six*phi_logder*phi_logder)+&
2336 &         coeff_aa*(-four*dcc_dzeta*phi_logder+d2cc_dzeta2)
2337 
2338 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
2339          grrho2=four*grho2_updn(ipts,1)
2340          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
2341 !        Note that tt is (the t variable of PBE divided by phi) squared
2342          tt=half*grrho2*dtt_dg
2343 
2344 !        Get xx from aa and tt
2345          xx=aa*tt
2346          dxx_drs=daa_drs*tt
2347          dxx_dzeta=daa_dzeta*tt
2348          dxx_dtt=aa
2349          d2xx_drs2=d2aa_drs2*tt
2350          d2xx_drsdzeta=d2aa_drsdzeta*tt
2351          d2xx_drsdtt=daa_drs
2352          d2xx_dttdzeta=daa_dzeta
2353          d2xx_dzeta2=d2aa_dzeta2*tt
2354 
2355 !        From xx to pade
2356          pade_den=one/(one+xx*(one+xx))
2357          pade=(one+xx)*pade_den
2358          dpade_dxx=-xx*(two+xx)*pade_den**2
2359          dpade_drs=dpade_dxx*dxx_drs
2360          dpade_dtt=dpade_dxx*dxx_dtt
2361          dpade_dzeta=dpade_dxx*dxx_dzeta
2362          d2pade_dxx2=two*(-one+xx*xx*(three+xx))*pade_den*pade_den*pade_den
2363          d2pade_drs2=d2pade_dxx2*dxx_drs*dxx_drs+dpade_dxx*d2xx_drs2
2364          d2pade_drsdtt=d2pade_dxx2*dxx_drs*dxx_dtt+dpade_dxx*d2xx_drsdtt
2365          d2pade_drsdzeta=d2pade_dxx2*dxx_drs*dxx_dzeta+dpade_dxx*d2xx_drsdzeta
2366          d2pade_dtt2=d2pade_dxx2*dxx_dtt*dxx_dtt
2367          d2pade_dttdzeta=d2pade_dxx2*dxx_dtt*dxx_dzeta+dpade_dxx*d2xx_dttdzeta
2368          d2pade_dzeta2=d2pade_dxx2*dxx_dzeta*dxx_dzeta+dpade_dxx*d2xx_dzeta2
2369 
2370 !        From pade to qq
2371          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
2372          qq=coeff_qq*pade
2373          dqq_drs=coeff_qq*dpade_drs
2374          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
2375          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
2376          d2qq_drs2=coeff_qq*d2pade_drs2
2377          d2qq_drsdtt=phi_zeta_inv*phi_zeta_inv*(dpade_drs+tt*d2pade_drsdtt)
2378          d2qq_drsdzeta=coeff_qq*(d2pade_drsdzeta-two*dpade_drs*phi_logder)
2379          d2qq_dtt2=phi_zeta_inv*phi_zeta_inv*(two*dpade_dtt+tt*d2pade_dtt2)
2380          d2qq_dttdzeta=phi_zeta_inv*phi_zeta_inv*(dpade_dzeta-two*pade*phi_logder)+&
2381 &         coeff_qq*(d2pade_dttdzeta-two*dpade_dtt*phi_logder)
2382          d2qq_dzeta2=coeff_qq*( d2pade_dzeta2-four*dpade_dzeta*phi_logder &
2383 &         +six*pade*phi_logder*phi_logder            &
2384 &         -two*pade*phi_zeta_inv*phipp_zeta)
2385 
2386 !        From qq to rr
2387          arg_rr=one+beta*gamma_inv*qq
2388          div_rr=one/arg_rr
2389          rr=gamma*log(arg_rr)
2390          drr_dqq=beta*div_rr
2391          drr_drs=drr_dqq*dqq_drs
2392          drr_dtt=drr_dqq*dqq_dtt
2393          drr_dzeta=drr_dqq*dqq_dzeta
2394          d2rr_dqq2=-div_rr**2*beta*beta*gamma_inv
2395          d2rr_drs2=d2rr_dqq2*dqq_drs*dqq_drs+drr_dqq*d2qq_drs2
2396          d2rr_drsdtt=d2rr_dqq2*dqq_drs*dqq_dtt+drr_dqq*d2qq_drsdtt
2397          d2rr_drsdzeta=d2rr_dqq2*dqq_drs*dqq_dzeta+drr_dqq*d2qq_drsdzeta
2398          d2rr_dtt2=d2rr_dqq2*dqq_dtt*dqq_dtt+drr_dqq*d2qq_dtt2
2399          d2rr_dttdzeta=d2rr_dqq2*dqq_dtt*dqq_dzeta+drr_dqq*d2qq_dttdzeta
2400          d2rr_dzeta2=d2rr_dqq2*dqq_dzeta*dqq_dzeta+drr_dqq*d2qq_dzeta2
2401 
2402 !        From rr to hh
2403          hh=phi3_zeta*rr
2404          dhh_drs=phi3_zeta*drr_drs
2405          dhh_dtt=phi3_zeta*drr_dtt
2406          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
2407          d2hh_drs2=phi3_zeta*d2rr_drs2
2408          d2hh_drsdtt=phi3_zeta*d2rr_drsdtt
2409          d2hh_drsdzeta=phi3_zeta*(d2rr_drsdzeta+three*drr_drs*phi_logder)
2410          d2hh_dtt2=phi3_zeta*d2rr_dtt2
2411          d2hh_dttdzeta=phi3_zeta*(d2rr_dttdzeta+three*drr_dtt*phi_logder)
2412          d2hh_dzeta2=phi3_zeta*(six*rr*phi_logder*phi_logder+&
2413 &         six*phi_logder*drr_dzeta+d2rr_dzeta2)  &
2414 &         +three*phi_zeta*phi_zeta*rr*phipp_zeta
2415 
2416 !        The GGA correlation energy is added
2417          exci(ipts)=exci(ipts)+hh
2418 
2419 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
2420 
2421 !        From hh to the derivative of the energy wrt the density
2422          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
2423          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
2424 
2425 !        From hh to the derivative of the energy wrt to the gradient of the
2426 !        density, divided by the gradient of the density
2427 !        (The v3.3 definition includes the division by the norm of the gradient)
2428          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
2429 
2430          d2rhohh_drho2=rhotot_inv*&
2431 &         (-two*ninth*rs*dhh_drs +seven*four*ninth*tt*dhh_dtt &
2432 &         +ninth*rs*rs*d2hh_drs2+zeta*zeta*d2hh_dzeta2+(seven*third*tt)**2*d2hh_dtt2 &
2433 &         +two*third*rs*zeta*d2hh_drsdzeta+two*seven*ninth*rs*tt*d2hh_drsdtt &
2434 &         +two*seven*third*tt*zeta*d2hh_dttdzeta)
2435          d2rhohh_drhodg=dtt_dg*(-four*third*dhh_dtt-third*rs*d2hh_drsdtt &
2436 &         -zeta*d2hh_dttdzeta-seven*third*tt*d2hh_dtt2)
2437 
2438 !        Component 12 : first derivative with respect to the gradient
2439 !        of the density, div by the grad of the density
2440          dvxci(ipts,12)=dvxcdgr(ipts,3)
2441 !        Components 9, 10 and 11 : second derivatives with respect to the spin-density
2442 !        Note that there is already a contribution from LSDA
2443          dvxci(ipts,9)=dvxci(ipts,9)+d2rhohh_drho2+rhotot_inv*           &
2444 &         ( d2hh_dzeta2*(one-two*zeta) &
2445 &         -two*third*rs*d2hh_drsdzeta-14.0_dp*third*tt*d2hh_dttdzeta)
2446          dvxci(ipts,10)=dvxci(ipts,10)+d2rhohh_drho2-rhotot_inv*d2hh_dzeta2
2447          dvxci(ipts,11)=dvxci(ipts,11)+d2rhohh_drho2+rhotot_inv*           &
2448 &         ( d2hh_dzeta2*(one+two*zeta) &
2449 &         +two*third*rs*d2hh_drsdzeta+14.0_dp*third*tt*d2hh_dttdzeta)
2450 !        Components 13 and 14 : second derivatives with respect to spin density
2451 !        and gradient, divided by the gradient
2452          dvxci(ipts,13)=d2rhohh_drhodg+dtt_dg*d2hh_dttdzeta
2453          dvxci(ipts,14)=d2rhohh_drhodg-dtt_dg*d2hh_dttdzeta
2454 !        Component 15 : derivative of the (derivative wrt the gradient div by the grad),
2455 !        divided by the grad
2456          dvxci(ipts,15)=rhotot*d2hh_dtt2*dtt_dg*dtt_dg
2457 
2458 
2459 !        End condition of GGA
2460 
2461 
2462 !        Correlation has been added
2463 !        -----------------------------------------------------------------------------
2464 
2465 !        vxci(ipts,2)=vxci(ipts,1)
2466          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
2467 
2468        end do
2469 
2470      else if ((option==6) .or. (option==7)) then
2471        do ipts=1,npts
2472 
2473          rhotot=rhoarr(ipts)
2474          rhotmot=rhom1_3(ipts)
2475          rhotot_inv=rhotmot*rhotmot*rhotmot
2476          rhotmo6=sqrt(rhotmot)
2477          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
2478 !        -----------------------------------------------------------------------
2479 !        First take care of the exchange part of the functional
2480 
2481          exc=zero
2482          dvxcdgr(ipts,3)=zero
2483 !        loop over the spin
2484          ispden=1
2485          rho   =rho_updn(ipts,ispden)
2486          rhomot=rho_updnm1_3(ipts,ispden)
2487          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
2488 !        Perdew-Burke-Ernzerhof GGA, exchange part
2489          rho_inv=rhomot*rhomot*rhomot
2490          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
2491          ss=grho2_updn(ipts,ispden)*coeffss
2492 
2493          if (option==6) then
2494            divss=exp(-mu_divkappa*ss)
2495            dfxdss= mu*divss
2496            d2fxdss2=-mu*mu_divkappa*divss
2497 
2498            fx    = one+kappa*(one-divss)
2499            ex_gga= ex_lsd*fx
2500            dssdn=-eight*third*ss*rho_inv
2501            dfxdn  = dfxdss*dssdn
2502            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
2503 !          The new definition (v3.3) includes the division by the norm of the gradient
2504            dssdg =two*coeffss
2505            dfxdg=dfxdss*dssdg
2506            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
2507            exc=exc+ex_gga*rho
2508 !          This is the Wu and Cohen modification
2509          else
2510            expss=exp(-ss)
2511            p1_wc=b_wc+(mu-b_wc)*(one-ss)*expss+two*c_wc*ss/(one+c_wc*ss*ss)
2512            p2_wc=d_wc*(ss-two)*expss+two*c_wc/(one+c_wc*ss*ss)-&
2513 &           four*c_wc*c_wc*ss*ss/((one+c_wc*ss*ss)*(one+c_wc*ss*ss))
2514            divss=one/(one+(b_wc*ss+d_wc*ss*expss+log(one+c_wc*ss*ss))/kappa)
2515            dfxdss=p1_wc*divss*divss
2516            d2fxdss2=p2_wc*divss*divss-two*divss*divss*divss*p1_wc*p1_wc/kappa
2517 
2518            fx    = one+kappa*(one-divss)
2519            ex_gga= ex_lsd*fx
2520            dssdn=-eight*third*ss*rho_inv
2521            dfxdn  = dfxdss*dssdn
2522            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
2523 !          The new definition (v3.3) includes the division by the norm of the gradient
2524            dssdg =two*coeffss
2525            dfxdg=dfxdss*dssdg
2526            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
2527            exc=exc+ex_gga*rho
2528          end if
2529 
2530 !        Perdew-Burke-Ernzerhof GGA, exchange part
2531 !        Components 3 or 4
2532          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
2533 !        Components 1 or 2
2534          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
2535          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
2536          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
2537 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
2538 !        Components 5 or 6
2539          d2ssdndg=-eight*third*dssdg*rho_inv
2540          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
2541          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
2542 !        Components 7 or 8
2543          d2fxdg2=d2fxdss2*dssdg**2
2544          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
2545 !        For the time being, treat non-spin-polarized like spin-polarized
2546          dvxci(ipts,2)=dvxci(ipts,1)
2547          dvxci(ipts,4)=dvxci(ipts,3)
2548          dvxci(ipts,6)=dvxci(ipts,5)
2549          dvxci(ipts,8)=dvxci(ipts,7)
2550 !        end of loop over the spin
2551 !        If non spin-polarized, treat spin down contribution now, similar to spin up
2552          exc=exc*2
2553          exci(ipts)=exc*rhotot_inv
2554 !        -----------------------------------------------------------------------------
2555 !        Then takes care of the LSD correlation part of the functional
2556 
2557 
2558          rs=rsfac*rhotmot
2559          sqr_rs=sq_rsfac*rhotmo6
2560          rsm1_2=sq_rsfac_inv*rhoto6
2561 
2562 !        Formulas A6-A8 of PW92LSD
2563          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
2564          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
2565          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
2566          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
2567 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
2568          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
2569          ecrs0=ec0_q0*ec0_log
2570          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
2571          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
2572          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
2573 &         -ec0_q0*ec0_q1pp*ec0_den                        &
2574 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
2575 
2576          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
2577          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
2578          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
2579          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
2580          mac_log=-log( mac_q1*mac_q1*mac_den )
2581          macrs=mac_q0*mac_log
2582          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
2583 
2584          ecrs=ecrs0
2585          decrs_drs=decrs0_drs
2586          decrs_dzeta=0.0_dp
2587          d2ecrs_drs2=d2ecrs0_drs2
2588          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
2589          d2ecrs_drsdzeta=zero
2590          zeta=0.0_dp
2591 
2592 
2593 !        Add LSD correlation functional to GGA exchange functional
2594          exci(ipts)=exci(ipts)+ecrs
2595          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
2596 
2597          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
2598 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
2599          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
2600          dvxci(ipts,9)=d2ecrs_drho2
2601          dvxci(ipts,10)=d2ecrs_drho2
2602          dvxci(ipts,11)=d2ecrs_drho2
2603 
2604 !        -----------------------------------------------------------------------------
2605 !        Eventually add the GGA correlation part of the PBE functional
2606 !        Note : the computation of the potential in the spin-unpolarized
2607 !        case could be optimized much further. Other optimizations are left to do.
2608 
2609          phi_zeta=1.0_dp
2610          phip_zeta=0.0_dp
2611          phi_zeta_inv=1.0_dp
2612          phi_logder=0.0_dp
2613          phi3_zeta=1.0_dp
2614          gamphi3inv=gamma_inv
2615          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
2616 
2617 !        From ec to bb
2618          bb=ecrs*gamphi3inv
2619          dbb_drs=decrs_drs*gamphi3inv
2620          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
2621          d2bb_drs2=d2ecrs_drs2*gamphi3inv
2622          d2bb_drsdzeta=gamphi3inv*(d2ecrs_drsdzeta-three*decrs_drs*phi_logder)
2623          d2bb_dzeta2=gamphi3inv*(d2ecrs_dzeta2-six*decrs_dzeta*phi_logder+&
2624 &         12.0_dp*ecrs*phi_logder*phi_logder-three*ecrs*phi_zeta_inv*phipp_zeta)
2625 
2626 !        From bb to cc
2627          exp_pbe=exp(-bb)
2628          cc=one/(exp_pbe-one)
2629          dcc_dbb=cc*cc*exp_pbe
2630          dcc_drs=dcc_dbb*dbb_drs
2631          dcc_dzeta=dcc_dbb*dbb_dzeta
2632          d2cc_dbb2=cc*cc*exp_pbe*(two*cc*exp_pbe-one)
2633          d2cc_drs2=d2cc_dbb2*dbb_drs*dbb_drs+dcc_dbb*d2bb_drs2
2634          d2cc_drsdzeta=d2cc_dbb2*dbb_drs*dbb_dzeta+dcc_dbb*d2bb_drsdzeta
2635          d2cc_dzeta2=d2cc_dbb2*dbb_dzeta*dbb_dzeta+dcc_dbb*d2bb_dzeta2
2636 
2637 !        From cc to aa
2638          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
2639          aa=coeff_aa*cc
2640          daa_drs=coeff_aa*dcc_drs
2641          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
2642          d2aa_drs2=coeff_aa*d2cc_drs2
2643          d2aa_drsdzeta=-two*daa_drs*phi_logder+coeff_aa*d2cc_drsdzeta
2644          d2aa_dzeta2=aa*(-two*phi_zeta_inv*phipp_zeta+six*phi_logder*phi_logder)+&
2645 &         coeff_aa*(-four*dcc_dzeta*phi_logder+d2cc_dzeta2)
2646 
2647 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
2648          grrho2=four*grho2_updn(ipts,1)
2649          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
2650 !        Note that tt is (the t variable of PBE divided by phi) squared
2651          tt=half*grrho2*dtt_dg
2652 
2653 !        Get xx from aa and tt
2654          xx=aa*tt
2655          dxx_drs=daa_drs*tt
2656          dxx_dzeta=daa_dzeta*tt
2657          dxx_dtt=aa
2658          d2xx_drs2=d2aa_drs2*tt
2659          d2xx_drsdzeta=d2aa_drsdzeta*tt
2660          d2xx_drsdtt=daa_drs
2661          d2xx_dttdzeta=daa_dzeta
2662          d2xx_dzeta2=d2aa_dzeta2*tt
2663 
2664 !        From xx to pade
2665          pade_den=one/(one+xx*(one+xx))
2666          pade=(one+xx)*pade_den
2667          dpade_dxx=-xx*(two+xx)*pade_den**2
2668          dpade_drs=dpade_dxx*dxx_drs
2669          dpade_dtt=dpade_dxx*dxx_dtt
2670          dpade_dzeta=dpade_dxx*dxx_dzeta
2671          d2pade_dxx2=two*(-one+xx*xx*(three+xx))*pade_den*pade_den*pade_den
2672          d2pade_drs2=d2pade_dxx2*dxx_drs*dxx_drs+dpade_dxx*d2xx_drs2
2673          d2pade_drsdtt=d2pade_dxx2*dxx_drs*dxx_dtt+dpade_dxx*d2xx_drsdtt
2674          d2pade_drsdzeta=d2pade_dxx2*dxx_drs*dxx_dzeta+dpade_dxx*d2xx_drsdzeta
2675          d2pade_dtt2=d2pade_dxx2*dxx_dtt*dxx_dtt
2676          d2pade_dttdzeta=d2pade_dxx2*dxx_dtt*dxx_dzeta+dpade_dxx*d2xx_dttdzeta
2677          d2pade_dzeta2=d2pade_dxx2*dxx_dzeta*dxx_dzeta+dpade_dxx*d2xx_dzeta2
2678 
2679 !        From pade to qq
2680          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
2681          qq=coeff_qq*pade
2682          dqq_drs=coeff_qq*dpade_drs
2683          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
2684          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
2685          d2qq_drs2=coeff_qq*d2pade_drs2
2686          d2qq_drsdtt=phi_zeta_inv*phi_zeta_inv*(dpade_drs+tt*d2pade_drsdtt)
2687          d2qq_drsdzeta=coeff_qq*(d2pade_drsdzeta-two*dpade_drs*phi_logder)
2688          d2qq_dtt2=phi_zeta_inv*phi_zeta_inv*(two*dpade_dtt+tt*d2pade_dtt2)
2689          d2qq_dttdzeta=phi_zeta_inv*phi_zeta_inv*(dpade_dzeta-two*pade*phi_logder)+&
2690 &         coeff_qq*(d2pade_dttdzeta-two*dpade_dtt*phi_logder)
2691          d2qq_dzeta2=coeff_qq*( d2pade_dzeta2-four*dpade_dzeta*phi_logder &
2692 &         +six*pade*phi_logder*phi_logder            &
2693 &         -two*pade*phi_zeta_inv*phipp_zeta)
2694 
2695 !        From qq to rr
2696          arg_rr=one+beta*gamma_inv*qq
2697          div_rr=one/arg_rr
2698          rr=gamma*log(arg_rr)
2699          drr_dqq=beta*div_rr
2700          drr_drs=drr_dqq*dqq_drs
2701          drr_dtt=drr_dqq*dqq_dtt
2702          drr_dzeta=drr_dqq*dqq_dzeta
2703          d2rr_dqq2=-div_rr**2*beta*beta*gamma_inv
2704          d2rr_drs2=d2rr_dqq2*dqq_drs*dqq_drs+drr_dqq*d2qq_drs2
2705          d2rr_drsdtt=d2rr_dqq2*dqq_drs*dqq_dtt+drr_dqq*d2qq_drsdtt
2706          d2rr_drsdzeta=d2rr_dqq2*dqq_drs*dqq_dzeta+drr_dqq*d2qq_drsdzeta
2707          d2rr_dtt2=d2rr_dqq2*dqq_dtt*dqq_dtt+drr_dqq*d2qq_dtt2
2708          d2rr_dttdzeta=d2rr_dqq2*dqq_dtt*dqq_dzeta+drr_dqq*d2qq_dttdzeta
2709          d2rr_dzeta2=d2rr_dqq2*dqq_dzeta*dqq_dzeta+drr_dqq*d2qq_dzeta2
2710 
2711 !        From rr to hh
2712          hh=phi3_zeta*rr
2713          dhh_drs=phi3_zeta*drr_drs
2714          dhh_dtt=phi3_zeta*drr_dtt
2715          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
2716          d2hh_drs2=phi3_zeta*d2rr_drs2
2717          d2hh_drsdtt=phi3_zeta*d2rr_drsdtt
2718          d2hh_drsdzeta=phi3_zeta*(d2rr_drsdzeta+three*drr_drs*phi_logder)
2719          d2hh_dtt2=phi3_zeta*d2rr_dtt2
2720          d2hh_dttdzeta=phi3_zeta*(d2rr_dttdzeta+three*drr_dtt*phi_logder)
2721          d2hh_dzeta2=phi3_zeta*(six*rr*phi_logder*phi_logder+&
2722 &         six*phi_logder*drr_dzeta+d2rr_dzeta2)  &
2723 &         +three*phi_zeta*phi_zeta*rr*phipp_zeta
2724 
2725 !        The GGA correlation energy is added
2726          exci(ipts)=exci(ipts)+hh
2727 
2728 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
2729 
2730 !        From hh to the derivative of the energy wrt the density
2731          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
2732          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
2733 
2734 !        From hh to the derivative of the energy wrt to the gradient of the
2735 !        density, divided by the gradient of the density
2736 !        (The v3.3 definition includes the division by the norm of the gradient)
2737          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
2738 
2739          d2rhohh_drho2=rhotot_inv*&
2740 &         (-two*ninth*rs*dhh_drs +seven*four*ninth*tt*dhh_dtt &
2741 &         +ninth*rs*rs*d2hh_drs2+zeta*zeta*d2hh_dzeta2+(seven*third*tt)**2*d2hh_dtt2 &
2742 &         +two*third*rs*zeta*d2hh_drsdzeta+two*seven*ninth*rs*tt*d2hh_drsdtt &
2743 &         +two*seven*third*tt*zeta*d2hh_dttdzeta)
2744          d2rhohh_drhodg=dtt_dg*(-four*third*dhh_dtt-third*rs*d2hh_drsdtt &
2745 &         -zeta*d2hh_dttdzeta-seven*third*tt*d2hh_dtt2)
2746 
2747 !        Component 12 : first derivative with respect to the gradient
2748 !        of the density, div by the grad of the density
2749          dvxci(ipts,12)=dvxcdgr(ipts,3)
2750 !        Components 9, 10 and 11 : second derivatives with respect to the spin-density
2751 !        Note that there is already a contribution from LSDA
2752          dvxci(ipts,9)=dvxci(ipts,9)+d2rhohh_drho2+rhotot_inv*           &
2753 &         ( d2hh_dzeta2*(one-two*zeta) &
2754 &         -two*third*rs*d2hh_drsdzeta-14.0_dp*third*tt*d2hh_dttdzeta)
2755          dvxci(ipts,10)=dvxci(ipts,10)+d2rhohh_drho2-rhotot_inv*d2hh_dzeta2
2756          dvxci(ipts,11)=dvxci(ipts,11)+d2rhohh_drho2+rhotot_inv*           &
2757 &         ( d2hh_dzeta2*(one+two*zeta) &
2758 &         +two*third*rs*d2hh_drsdzeta+14.0_dp*third*tt*d2hh_dttdzeta)
2759 !        Components 13 and 14 : second derivatives with respect to spin density
2760 !        and gradient, divided by the gradient
2761          dvxci(ipts,13)=d2rhohh_drhodg+dtt_dg*d2hh_dttdzeta
2762          dvxci(ipts,14)=d2rhohh_drhodg-dtt_dg*d2hh_dttdzeta
2763 !        Component 15 : derivative of the (derivative wrt the gradient div by the grad),
2764 !        divided by the grad
2765          dvxci(ipts,15)=rhotot*d2hh_dtt2*dtt_dg*dtt_dg
2766 
2767 
2768 !        End condition of GGA
2769 
2770 
2771 !        Correlation has been added
2772 !        -----------------------------------------------------------------------------
2773 
2774 !        vxci(ipts,2)=vxci(ipts,1)
2775          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
2776 
2777        end do
2778 
2779      else if (option==-1) then
2780        do ipts=1,npts
2781 
2782          rhotot=rhoarr(ipts)
2783          rhotmot=rhom1_3(ipts)
2784          rhotot_inv=rhotmot*rhotmot*rhotmot
2785          rhotmo6=sqrt(rhotmot)
2786          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
2787 !        -----------------------------------------------------------------------
2788 !        First take care of the exchange part of the functional
2789 
2790          exc=zero
2791 !        loop over the spin
2792          ispden=1
2793          rho   =rho_updn(ipts,ispden)
2794          rhomot=rho_updnm1_3(ipts,ispden)
2795          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
2796 !        Perdew_Wang 91 LSD
2797          vxci(ipts,ispden)=four_thirds*ex_lsd
2798          exc=exc+ex_lsd*rho
2799 
2800 !        Perdew_Wang 91 LSD
2801          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
2802 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
2803          dvxci(ipts,2)=zero
2804 !        If non-spin-polarized, first component of dvxci is second
2805 !        derivative with respect to TOTAL density.
2806          dvxci(ipts,1)=dvxci(ipts,1)*half
2807 !        Compute the second derivative of vx
2808 !        vx^(2) = -2*vx^(1)/(3*rhotot)
2809 !        end of loop over the spin
2810 !        If non spin-polarized, treat spin down contribution now, similar to spin up
2811          exc=exc*2
2812          exci(ipts)=exc*rhotot_inv
2813 
2814 
2815 !        Correlation has been added
2816 !        -----------------------------------------------------------------------------
2817 
2818 
2819        end do
2820 
2821      else if (option==-2) then
2822        do ipts=1,npts
2823 
2824          rhotot=rhoarr(ipts)
2825          rhotmot=rhom1_3(ipts)
2826          rhotot_inv=rhotmot*rhotmot*rhotmot
2827          rhotmo6=sqrt(rhotmot)
2828          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
2829 !        -----------------------------------------------------------------------
2830 !        First take care of the exchange part of the functional
2831 
2832          exc=zero
2833          dvxcdgr(ipts,3)=zero
2834 !        loop over the spin
2835          ispden=1
2836          rho   =rho_updn(ipts,ispden)
2837          rhomot=rho_updnm1_3(ipts,ispden)
2838          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
2839 !        Perdew-Burke-Ernzerhof GGA, exchange part
2840          rho_inv=rhomot*rhomot*rhomot
2841          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
2842          ss=grho2_updn(ipts,ispden)*coeffss
2843          divss=one/(one+mu_divkappa*ss)
2844          dfxdss= mu*divss*divss
2845          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
2846          fx    = one+kappa*(one-divss)
2847          ex_gga= ex_lsd*fx
2848          dssdn=-eight*third*ss*rho_inv
2849          dfxdn  = dfxdss*dssdn
2850          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
2851 !        The new definition (v3.3) includes the division by the norm of the gradient
2852          dssdg =two*coeffss
2853          dfxdg=dfxdss*dssdg
2854          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
2855          exc=exc+ex_gga*rho
2856 
2857 !        Perdew-Burke-Ernzerhof GGA, exchange part
2858 !        Components 3 or 4
2859          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
2860 !        Components 1 or 2
2861          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
2862          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
2863          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
2864 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
2865 !        Components 5 or 6
2866          d2ssdndg=-eight*third*dssdg*rho_inv
2867          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
2868          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
2869 !        Components 7 or 8
2870          d2fxdg2=d2fxdss2*dssdg**2
2871          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
2872 !        For the time being, treat non-spin-polarized like spin-polarized
2873          dvxci(ipts,2)=dvxci(ipts,1)
2874          dvxci(ipts,4)=dvxci(ipts,3)
2875          dvxci(ipts,6)=dvxci(ipts,5)
2876          dvxci(ipts,8)=dvxci(ipts,7)
2877 !        end of loop over the spin
2878 !        If non spin-polarized, treat spin down contribution now, similar to spin up
2879          exc=exc*2
2880          exci(ipts)=exc*rhotot_inv
2881 !        -----------------------------------------------------------------------------
2882 !        Then takes care of the LSD correlation part of the functional
2883 
2884 
2885 !        Correlation has been added
2886 !        -----------------------------------------------------------------------------
2887 
2888 !        vxci(ipts,2)=vxci(ipts,1)
2889          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
2890 
2891        end do
2892 
2893      else if(option==-4) then
2894 
2895 
2896        do ipts=1,npts
2897 
2898          rhotot=rhoarr(ipts)
2899          rhotmot=rhom1_3(ipts)
2900          rhotot_inv=rhotmot*rhotmot*rhotmot
2901          rhotmo6=sqrt(rhotmot)
2902          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
2903 !        -----------------------------------------------------------------------
2904 !        First take care of the exchange part of the functional
2905 
2906          exc=zero
2907          dvxcdgr(ipts,3)=zero
2908 !        loop over the spin
2909          ispden=1
2910          rho   =rho_updn(ipts,ispden)
2911          rhomot=rho_updnm1_3(ipts,ispden)
2912          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
2913 !        VALENTINO R. COOPER C09x GGA, This is an exchange term proposed
2914 !        to use together with vdw-DF (see above).
2915          rho_inv=rhomot*rhomot*rhomot
2916          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
2917 !        the quarter that is lacking is compensated by the grho2_updn in the
2918 !        next line.
2919          ss=grho2_updn(ipts,ispden)*coeffss
2920          alphs2=alpha_c09*ss
2921          alphmu=alpha_c09*mu_c09
2922          dfxdss= mu_c09*exp(-alphs2)*(one-alphs2)+&
2923 &         kappa*alpha_c09*exp(-alphs2/two)/two
2924          d2fxdss2=-alphmu*exp(-alphs2)*(two-alphs2)-&
2925 &         kappa*(alpha_c09**two)*exp(alphs2/two)/four
2926          fx    = one+mu_c09*ss*exp(-alphs2)+kappa*(one-exp(-alphs2/two))
2927          ex_gga= ex_lsd*fx
2928          dssdn=-eight*third*ss*rho_inv
2929          dfxdn  = dfxdss*dssdn
2930          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
2931 !        The new definition (v3.3) includes the division by the norm of the gradient
2932          dssdg =two*coeffss
2933          dfxdg=dfxdss*dssdg
2934          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
2935          exc=exc+ex_gga*rho
2936 
2937 !        Cooper C09x GGA exchange
2938 !        Components 3 or 4
2939          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
2940 !        Components 1 or 2
2941          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
2942          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
2943          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
2944 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
2945 !        Components 5 or 6
2946          d2ssdndg=-eight*third*dssdg*rho_inv
2947          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
2948          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
2949 !        Components 7 or 8
2950          d2fxdg2=d2fxdss2*dssdg**2
2951          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
2952 !        For the time being, treat non-spin-polarized like spin-polarized
2953          dvxci(ipts,2)=dvxci(ipts,1)
2954          dvxci(ipts,4)=dvxci(ipts,3)
2955          dvxci(ipts,6)=dvxci(ipts,5)
2956          dvxci(ipts,8)=dvxci(ipts,7)
2957 
2958 !        end of loop over the spin
2959 !        If non spin-polarized, treat spin down contribution now, similar to spin up
2960          exc=exc*2
2961          exci(ipts)=exc*rhotot_inv
2962 !        -----------------------------------------------------------------------------
2963 !        Then takes care of the LSD correlation part of the functional
2964 
2965 !        Correlation has been added
2966 !        -----------------------------------------------------------------------------
2967 
2968 !        vxci(ipts,2)=vxci(ipts,1)
2969          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
2970 
2971        end do
2972 
2973 
2974      else if (option==1) then
2975        do ipts=1,npts
2976 
2977          rhotot=rhoarr(ipts)
2978          rhotmot=rhom1_3(ipts)
2979          rhotot_inv=rhotmot*rhotmot*rhotmot
2980          rhotmo6=sqrt(rhotmot)
2981          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
2982 !        -----------------------------------------------------------------------
2983 !        First take care of the exchange part of the functional
2984 
2985          exc=zero
2986 !        loop over the spin
2987          ispden=1
2988          rho   =rho_updn(ipts,ispden)
2989          rhomot=rho_updnm1_3(ipts,ispden)
2990          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
2991 !        Perdew_Wang 91 LSD
2992          vxci(ipts,ispden)=four_thirds*ex_lsd
2993          exc=exc+ex_lsd*rho
2994 
2995 
2996 !        Perdew_Wang 91 LSD
2997          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
2998 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
2999          dvxci(ipts,2)=zero
3000 !        If non-spin-polarized, first component of dvxci is second
3001 !        derivative with respect to TOTAL density.
3002          dvxci(ipts,1)=dvxci(ipts,1)*half
3003 !        Compute the second derivative of vx
3004 !        vx^(2) = -2*vx^(1)/(3*rhotot)
3005 !        end of loop over the spin
3006 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3007          exc=exc*2
3008          exci(ipts)=exc*rhotot_inv
3009 !        -----------------------------------------------------------------------------
3010 !        Then takes care of the LSD correlation part of the functional
3011 
3012          rs=rsfac*rhotmot
3013          sqr_rs=sq_rsfac*rhotmo6
3014          rsm1_2=sq_rsfac_inv*rhoto6
3015 
3016 !        Formulas A6-A8 of PW92LSD
3017          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
3018 
3019          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
3020          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
3021          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
3022 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
3023          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
3024          ecrs0=ec0_q0*ec0_log
3025          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
3026          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
3027          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
3028 &         -ec0_q0*ec0_q1pp*ec0_den                        &
3029 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
3030 
3031          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
3032          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
3033          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
3034          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
3035          mac_log=-log( mac_q1*mac_q1*mac_den )
3036          macrs=mac_q0*mac_log
3037          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
3038 
3039          ecrs=ecrs0
3040          decrs_drs=decrs0_drs
3041          decrs_dzeta=0.0_dp
3042          d2ecrs_drs2=d2ecrs0_drs2
3043          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
3044          d2ecrs_drsdzeta=zero
3045          zeta=0.0_dp
3046 
3047 
3048 !        Add LSD correlation functional to GGA exchange functional
3049          exci(ipts)=exci(ipts)+ecrs
3050          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
3051 
3052          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
3053 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
3054          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
3055 
3056          dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drho2
3057          dvxci(ipts,2)=dvxci(ipts,2)+d2ecrs_drho2-d2ecrs_dzeta2*rhotot_inv
3058 
3059 
3060 !        Correlation has been added
3061 !        -----------------------------------------------------------------------------
3062 
3063        end do
3064 
3065      else if (option==3) then
3066        do ipts=1,npts
3067 
3068          rhotot=rhoarr(ipts)
3069          rhotmot=rhom1_3(ipts)
3070          rhotot_inv=rhotmot*rhotmot*rhotmot
3071          rhotmo6=sqrt(rhotmot)
3072          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
3073 !        -----------------------------------------------------------------------
3074 !        First take care of the exchange part of the functional
3075 
3076          exc=zero
3077 !        loop over the spin
3078          ispden=1
3079          rho   =rho_updn(ipts,ispden)
3080          rhomot=rho_updnm1_3(ipts,ispden)
3081          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
3082 !        Perdew_Wang 91 LSD
3083          vxci(ipts,ispden)=four_thirds*ex_lsd
3084          exc=exc+ex_lsd*rho
3085 
3086 !        Perdew_Wang 91 LSD
3087          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
3088 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
3089          dvxci(ipts,2)=zero
3090 !        If non-spin-polarized, first component of dvxci is second
3091 !        derivative with respect to TOTAL density.
3092          dvxci(ipts,1)=dvxci(ipts,1)*half
3093 !        Compute the second derivative of vx
3094 !        vx^(2) = -2*vx^(1)/(3*rhotot)
3095 !        end of loop over the spin
3096 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3097          exc=exc*2
3098          exci(ipts)=exc*rhotot_inv
3099 !        -----------------------------------------------------------------------------
3100 !        Then takes care of the LSD correlation part of the functional
3101 
3102 
3103          rs=rsfac*rhotmot
3104          sqr_rs=sq_rsfac*rhotmo6
3105          rsm1_2=sq_rsfac_inv*rhoto6
3106 
3107 !        Formulas A6-A8 of PW92LSD
3108          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
3109          sqr_sqr_rs=max(1.e-15_dp,sqrt(sqr_rs))
3110          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs/sqr_sqr_rs)
3111          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+3.5_dp*ec0_b4*rs/sqr_sqr_rs)
3112          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
3113 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
3114          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
3115          ecrs0=ec0_q0*ec0_log
3116          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
3117          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
3118          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
3119 &         -ec0_q0*ec0_q1pp*ec0_den                        &
3120 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
3121 
3122          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
3123          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
3124          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
3125          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
3126          mac_log=-log( mac_q1*mac_q1*mac_den )
3127          macrs=mac_q0*mac_log
3128          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
3129 
3130          ecrs=ecrs0
3131          decrs_drs=decrs0_drs
3132          decrs_dzeta=0.0_dp
3133          d2ecrs_drs2=d2ecrs0_drs2
3134          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
3135          d2ecrs_drsdzeta=zero
3136          zeta=0.0_dp
3137 
3138 
3139 !        Add LSD correlation functional to GGA exchange functional
3140          exci(ipts)=exci(ipts)+ecrs
3141          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
3142 
3143          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
3144 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
3145          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
3146          dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drho2
3147          dvxci(ipts,2)=dvxci(ipts,2)+d2ecrs_drho2-d2ecrs_dzeta2*rhotot_inv
3148 
3149 !        Correlation has been added
3150 !        -----------------------------------------------------------------------------
3151 
3152        end do
3153 
3154      end if
3155 
3156 
3157    else if (order**2>1) then
3158 !    separate cases depending to option
3159      if(option==2 .or. option==5) then
3160        do ipts=1,npts
3161 
3162          rhotot=rhoarr(ipts)
3163          rhotmot=rhom1_3(ipts)
3164          rhotot_inv=rhotmot*rhotmot*rhotmot
3165          rhotmo6=sqrt(rhotmot)
3166          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
3167 !        -----------------------------------------------------------------------
3168 !        First take care of the exchange part of the functional
3169 
3170          exc=zero
3171          dvxcdgr(ipts,3)=zero
3172 !        loop over the spin
3173          ispden=1
3174          rho   =rho_updn(ipts,ispden)
3175          rhomot=rho_updnm1_3(ipts,ispden)
3176          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
3177 !        Perdew-Burke-Ernzerhof GGA, exchange part
3178          rho_inv=rhomot*rhomot*rhomot
3179          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
3180          ss=grho2_updn(ipts,ispden)*coeffss
3181 
3182          divss=one/(one+mu_divkappa*ss)
3183          dfxdss= mu*divss*divss
3184          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
3185 
3186          fx    = one+kappa*(one-divss)
3187          ex_gga= ex_lsd*fx
3188          dssdn=-eight*third*ss*rho_inv
3189          dfxdn  = dfxdss*dssdn
3190          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
3191 !        The new definition (v3.3) includes the division by the norm of the gradient
3192          dssdg =two*coeffss
3193          dfxdg=dfxdss*dssdg
3194          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
3195          exc=exc+ex_gga*rho
3196 
3197 !        Perdew-Burke-Ernzerhof GGA, exchange part
3198 !        Components 3 or 4
3199          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
3200 !        Components 1 or 2
3201          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
3202          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
3203          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
3204 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
3205 !        Components 5 or 6
3206          d2ssdndg=-eight*third*dssdg*rho_inv
3207          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
3208          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
3209 !        Components 7 or 8
3210          d2fxdg2=d2fxdss2*dssdg**2
3211          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
3212 !        For the time being, treat non-spin-polarized like spin-polarized
3213          dvxci(ipts,2)=dvxci(ipts,1)
3214          dvxci(ipts,4)=dvxci(ipts,3)
3215          dvxci(ipts,6)=dvxci(ipts,5)
3216          dvxci(ipts,8)=dvxci(ipts,7)
3217 !        end of loop over the spin
3218 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3219          exc=exc*2
3220          exci(ipts)=exc*rhotot_inv
3221 !        -----------------------------------------------------------------------------
3222 !        Then takes care of the LSD correlation part of the functional
3223 
3224          rs=rsfac*rhotmot
3225          sqr_rs=sq_rsfac*rhotmo6
3226          rsm1_2=sq_rsfac_inv*rhoto6
3227 
3228 !        Formulas A6-A8 of PW92LSD
3229          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
3230          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
3231          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
3232          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
3233 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
3234          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
3235          ecrs0=ec0_q0*ec0_log
3236          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
3237          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
3238          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
3239 &         -ec0_q0*ec0_q1pp*ec0_den                        &
3240 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
3241          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
3242          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
3243          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
3244          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
3245          mac_log=-log( mac_q1*mac_q1*mac_den )
3246          macrs=mac_q0*mac_log
3247          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
3248          ecrs=ecrs0
3249          decrs_drs=decrs0_drs
3250          decrs_dzeta=0.0_dp
3251          d2ecrs_drs2=d2ecrs0_drs2
3252          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
3253          d2ecrs_drsdzeta=zero
3254          zeta=0.0_dp
3255 
3256 !        Add LSD correlation functional to GGA exchange functional
3257          exci(ipts)=exci(ipts)+ecrs
3258          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
3259 
3260          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
3261 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
3262          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
3263          dvxci(ipts,9)=d2ecrs_drho2
3264          dvxci(ipts,10)=d2ecrs_drho2
3265          dvxci(ipts,11)=d2ecrs_drho2
3266 
3267 
3268 !        -----------------------------------------------------------------------------
3269 !        Eventually add the GGA correlation part of the PBE functional
3270 !        Note : the computation of the potential in the spin-unpolarized
3271 !        case could be optimized much further. Other optimizations are left to do.
3272 
3273          phi_zeta=1.0_dp
3274          phip_zeta=0.0_dp
3275          phi_zeta_inv=1.0_dp
3276          phi_logder=0.0_dp
3277          phi3_zeta=1.0_dp
3278          gamphi3inv=gamma_inv
3279          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
3280 
3281 !        From ec to bb
3282          bb=ecrs*gamphi3inv
3283          dbb_drs=decrs_drs*gamphi3inv
3284          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
3285          d2bb_drs2=d2ecrs_drs2*gamphi3inv
3286          d2bb_drsdzeta=gamphi3inv*(d2ecrs_drsdzeta-three*decrs_drs*phi_logder)
3287          d2bb_dzeta2=gamphi3inv*(d2ecrs_dzeta2-six*decrs_dzeta*phi_logder+&
3288 &         12.0_dp*ecrs*phi_logder*phi_logder-three*ecrs*phi_zeta_inv*phipp_zeta)
3289 
3290 !        From bb to cc
3291          exp_pbe=exp(-bb)
3292          cc=one/(exp_pbe-one)
3293          dcc_dbb=cc*cc*exp_pbe
3294          dcc_drs=dcc_dbb*dbb_drs
3295          dcc_dzeta=dcc_dbb*dbb_dzeta
3296          d2cc_dbb2=cc*cc*exp_pbe*(two*cc*exp_pbe-one)
3297          d2cc_drs2=d2cc_dbb2*dbb_drs*dbb_drs+dcc_dbb*d2bb_drs2
3298          d2cc_drsdzeta=d2cc_dbb2*dbb_drs*dbb_dzeta+dcc_dbb*d2bb_drsdzeta
3299          d2cc_dzeta2=d2cc_dbb2*dbb_dzeta*dbb_dzeta+dcc_dbb*d2bb_dzeta2
3300 
3301 !        From cc to aa
3302          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
3303          aa=coeff_aa*cc
3304          daa_drs=coeff_aa*dcc_drs
3305          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
3306          d2aa_drs2=coeff_aa*d2cc_drs2
3307          d2aa_drsdzeta=-two*daa_drs*phi_logder+coeff_aa*d2cc_drsdzeta
3308          d2aa_dzeta2=aa*(-two*phi_zeta_inv*phipp_zeta+six*phi_logder*phi_logder)+&
3309 &         coeff_aa*(-four*dcc_dzeta*phi_logder+d2cc_dzeta2)
3310 
3311 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
3312          grrho2=four*grho2_updn(ipts,1)
3313          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
3314 !        Note that tt is (the t variable of PBE divided by phi) squared
3315          tt=half*grrho2*dtt_dg
3316 
3317 !        Get xx from aa and tt
3318          xx=aa*tt
3319          dxx_drs=daa_drs*tt
3320          dxx_dzeta=daa_dzeta*tt
3321          dxx_dtt=aa
3322          d2xx_drs2=d2aa_drs2*tt
3323          d2xx_drsdzeta=d2aa_drsdzeta*tt
3324          d2xx_drsdtt=daa_drs
3325          d2xx_dttdzeta=daa_dzeta
3326          d2xx_dzeta2=d2aa_dzeta2*tt
3327 
3328 !        From xx to pade
3329          pade_den=one/(one+xx*(one+xx))
3330          pade=(one+xx)*pade_den
3331          dpade_dxx=-xx*(two+xx)*pade_den**2
3332          dpade_drs=dpade_dxx*dxx_drs
3333          dpade_dtt=dpade_dxx*dxx_dtt
3334          dpade_dzeta=dpade_dxx*dxx_dzeta
3335          d2pade_dxx2=two*(-one+xx*xx*(three+xx))*pade_den*pade_den*pade_den
3336          d2pade_drs2=d2pade_dxx2*dxx_drs*dxx_drs+dpade_dxx*d2xx_drs2
3337          d2pade_drsdtt=d2pade_dxx2*dxx_drs*dxx_dtt+dpade_dxx*d2xx_drsdtt
3338          d2pade_drsdzeta=d2pade_dxx2*dxx_drs*dxx_dzeta+dpade_dxx*d2xx_drsdzeta
3339          d2pade_dtt2=d2pade_dxx2*dxx_dtt*dxx_dtt
3340          d2pade_dttdzeta=d2pade_dxx2*dxx_dtt*dxx_dzeta+dpade_dxx*d2xx_dttdzeta
3341          d2pade_dzeta2=d2pade_dxx2*dxx_dzeta*dxx_dzeta+dpade_dxx*d2xx_dzeta2
3342 
3343 
3344 !        From pade to qq
3345          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
3346          qq=coeff_qq*pade
3347          dqq_drs=coeff_qq*dpade_drs
3348          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
3349          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
3350          d2qq_drs2=coeff_qq*d2pade_drs2
3351          d2qq_drsdtt=phi_zeta_inv*phi_zeta_inv*(dpade_drs+tt*d2pade_drsdtt)
3352          d2qq_drsdzeta=coeff_qq*(d2pade_drsdzeta-two*dpade_drs*phi_logder)
3353          d2qq_dtt2=phi_zeta_inv*phi_zeta_inv*(two*dpade_dtt+tt*d2pade_dtt2)
3354          d2qq_dttdzeta=phi_zeta_inv*phi_zeta_inv*(dpade_dzeta-two*pade*phi_logder)+&
3355 &         coeff_qq*(d2pade_dttdzeta-two*dpade_dtt*phi_logder)
3356          d2qq_dzeta2=coeff_qq*( d2pade_dzeta2-four*dpade_dzeta*phi_logder &
3357 &         +six*pade*phi_logder*phi_logder            &
3358 &         -two*pade*phi_zeta_inv*phipp_zeta)
3359 
3360 !        From qq to rr
3361          arg_rr=one+beta*gamma_inv*qq
3362          div_rr=one/arg_rr
3363          rr=gamma*log(arg_rr)
3364          drr_dqq=beta*div_rr
3365          drr_drs=drr_dqq*dqq_drs
3366          drr_dtt=drr_dqq*dqq_dtt
3367          drr_dzeta=drr_dqq*dqq_dzeta
3368          d2rr_dqq2=-div_rr**2*beta*beta*gamma_inv
3369          d2rr_drs2=d2rr_dqq2*dqq_drs*dqq_drs+drr_dqq*d2qq_drs2
3370          d2rr_drsdtt=d2rr_dqq2*dqq_drs*dqq_dtt+drr_dqq*d2qq_drsdtt
3371          d2rr_drsdzeta=d2rr_dqq2*dqq_drs*dqq_dzeta+drr_dqq*d2qq_drsdzeta
3372          d2rr_dtt2=d2rr_dqq2*dqq_dtt*dqq_dtt+drr_dqq*d2qq_dtt2
3373          d2rr_dttdzeta=d2rr_dqq2*dqq_dtt*dqq_dzeta+drr_dqq*d2qq_dttdzeta
3374          d2rr_dzeta2=d2rr_dqq2*dqq_dzeta*dqq_dzeta+drr_dqq*d2qq_dzeta2
3375 
3376 !        From rr to hh
3377          hh=phi3_zeta*rr
3378          dhh_drs=phi3_zeta*drr_drs
3379          dhh_dtt=phi3_zeta*drr_dtt
3380          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
3381          d2hh_drs2=phi3_zeta*d2rr_drs2
3382          d2hh_drsdtt=phi3_zeta*d2rr_drsdtt
3383          d2hh_drsdzeta=phi3_zeta*(d2rr_drsdzeta+three*drr_drs*phi_logder)
3384          d2hh_dtt2=phi3_zeta*d2rr_dtt2
3385          d2hh_dttdzeta=phi3_zeta*(d2rr_dttdzeta+three*drr_dtt*phi_logder)
3386          d2hh_dzeta2=phi3_zeta*(six*rr*phi_logder*phi_logder+&
3387 &         six*phi_logder*drr_dzeta+d2rr_dzeta2)  &
3388 &         +three*phi_zeta*phi_zeta*rr*phipp_zeta
3389 
3390 
3391 !        The GGA correlation energy is added
3392          exci(ipts)=exci(ipts)+hh
3393 
3394 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
3395 
3396 
3397 !        From hh to the derivative of the energy wrt the density
3398          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
3399          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
3400 
3401 !        From hh to the derivative of the energy wrt to the gradient of the
3402 !        density, divided by the gradient of the density
3403 !        (The v3.3 definition includes the division by the norm of the gradient)
3404          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
3405 
3406          d2rhohh_drho2=rhotot_inv*&
3407 &         (-two*ninth*rs*dhh_drs +seven*four*ninth*tt*dhh_dtt &
3408 &         +ninth*rs*rs*d2hh_drs2+zeta*zeta*d2hh_dzeta2+(seven*third*tt)**2*d2hh_dtt2 &
3409 &         +two*third*rs*zeta*d2hh_drsdzeta+two*seven*ninth*rs*tt*d2hh_drsdtt &
3410 &         +two*seven*third*tt*zeta*d2hh_dttdzeta)
3411          d2rhohh_drhodg=dtt_dg*(-four*third*dhh_dtt-third*rs*d2hh_drsdtt &
3412 &         -zeta*d2hh_dttdzeta-seven*third*tt*d2hh_dtt2)
3413 
3414 !        Component 12 : first derivative with respect to the gradient
3415 !        of the density, div by the grad of the density
3416          dvxci(ipts,12)=dvxcdgr(ipts,3)
3417 !        Components 9, 10 and 11 : second derivatives with respect to the spin-density
3418 !        Note that there is already a contribution from LSDA
3419          dvxci(ipts,9)=dvxci(ipts,9)+d2rhohh_drho2+rhotot_inv*           &
3420 &         ( d2hh_dzeta2*(one-two*zeta) &
3421 &         -two*third*rs*d2hh_drsdzeta-14.0_dp*third*tt*d2hh_dttdzeta)
3422          dvxci(ipts,10)=dvxci(ipts,10)+d2rhohh_drho2-rhotot_inv*d2hh_dzeta2
3423          dvxci(ipts,11)=dvxci(ipts,11)+d2rhohh_drho2+rhotot_inv*           &
3424 &         ( d2hh_dzeta2*(one+two*zeta) &
3425 &         +two*third*rs*d2hh_drsdzeta+14.0_dp*third*tt*d2hh_dttdzeta)
3426 !        Components 13 and 14 : second derivatives with respect to spin density
3427 !        and gradient, divided by the gradient
3428          dvxci(ipts,13)=d2rhohh_drhodg+dtt_dg*d2hh_dttdzeta
3429          dvxci(ipts,14)=d2rhohh_drhodg-dtt_dg*d2hh_dttdzeta
3430 !        Component 15 : derivative of the (derivative wrt the gradient div by the grad),
3431 !        divided by the grad
3432          dvxci(ipts,15)=rhotot*d2hh_dtt2*dtt_dg*dtt_dg
3433 
3434 
3435 !        End condition of GGA
3436 
3437 !        Correlation has been added
3438 !        -----------------------------------------------------------------------------
3439 
3440 !        vxci(ipts,2)=vxci(ipts,1)
3441          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
3442 
3443        end do
3444 
3445      else if ((option==6) .or. (option==7)) then
3446        do ipts=1,npts
3447 
3448          rhotot=rhoarr(ipts)
3449          rhotmot=rhom1_3(ipts)
3450          rhotot_inv=rhotmot*rhotmot*rhotmot
3451          rhotmo6=sqrt(rhotmot)
3452          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
3453 !        -----------------------------------------------------------------------
3454 !        First take care of the exchange part of the functional
3455 
3456          exc=zero
3457          dvxcdgr(ipts,3)=zero
3458 !        loop over the spin
3459          ispden=1
3460          rho   =rho_updn(ipts,ispden)
3461          rhomot=rho_updnm1_3(ipts,ispden)
3462          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
3463 !        Perdew-Burke-Ernzerhof GGA, exchange part
3464          rho_inv=rhomot*rhomot*rhomot
3465          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
3466          ss=grho2_updn(ipts,ispden)*coeffss
3467 
3468          if (option==6) then
3469            divss=exp(-mu_divkappa*ss)
3470            dfxdss= mu*divss
3471            d2fxdss2=-mu*mu_divkappa*divss
3472 
3473            fx    = one+kappa*(one-divss)
3474            ex_gga= ex_lsd*fx
3475            dssdn=-eight*third*ss*rho_inv
3476            dfxdn  = dfxdss*dssdn
3477            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
3478 !          The new definition (v3.3) includes the division by the norm of the gradient
3479            dssdg =two*coeffss
3480            dfxdg=dfxdss*dssdg
3481            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
3482            exc=exc+ex_gga*rho
3483 !          This is the Wu and Cohen modification
3484          else
3485            expss=exp(-ss)
3486            p1_wc=b_wc+(mu-b_wc)*(one-ss)*expss+two*c_wc*ss/(one+c_wc*ss*ss)
3487            p2_wc=d_wc*(ss-two)*expss+two*c_wc/(one+c_wc*ss*ss)-&
3488 &           four*c_wc*c_wc*ss*ss/((one+c_wc*ss*ss)*(one+c_wc*ss*ss))
3489            divss=one/(one+(b_wc*ss+d_wc*ss*expss+log(one+c_wc*ss*ss))/kappa)
3490            dfxdss=p1_wc*divss*divss
3491            d2fxdss2=p2_wc*divss*divss-two*divss*divss*divss*p1_wc*p1_wc/kappa
3492 
3493            fx    = one+kappa*(one-divss)
3494            ex_gga= ex_lsd*fx
3495            dssdn=-eight*third*ss*rho_inv
3496            dfxdn  = dfxdss*dssdn
3497            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
3498 !          The new definition (v3.3) includes the division by the norm of the gradient
3499            dssdg =two*coeffss
3500            dfxdg=dfxdss*dssdg
3501            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
3502            exc=exc+ex_gga*rho
3503          end if
3504 
3505 !        Perdew-Burke-Ernzerhof GGA, exchange part
3506 !        Components 3 or 4
3507          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
3508 !        Components 1 or 2
3509          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
3510          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
3511          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
3512 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
3513 !        Components 5 or 6
3514          d2ssdndg=-eight*third*dssdg*rho_inv
3515          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
3516          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
3517 !        Components 7 or 8
3518          d2fxdg2=d2fxdss2*dssdg**2
3519          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
3520 !        For the time being, treat non-spin-polarized like spin-polarized
3521          dvxci(ipts,2)=dvxci(ipts,1)
3522          dvxci(ipts,4)=dvxci(ipts,3)
3523          dvxci(ipts,6)=dvxci(ipts,5)
3524          dvxci(ipts,8)=dvxci(ipts,7)
3525 !        end of loop over the spin
3526 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3527          exc=exc*2
3528          exci(ipts)=exc*rhotot_inv
3529 !        -----------------------------------------------------------------------------
3530 !        Then takes care of the LSD correlation part of the functional
3531 
3532 
3533          rs=rsfac*rhotmot
3534          sqr_rs=sq_rsfac*rhotmo6
3535          rsm1_2=sq_rsfac_inv*rhoto6
3536 
3537 !        Formulas A6-A8 of PW92LSD
3538          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
3539          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
3540          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
3541          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
3542 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
3543          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
3544          ecrs0=ec0_q0*ec0_log
3545          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
3546          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
3547          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
3548 &         -ec0_q0*ec0_q1pp*ec0_den                        &
3549 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
3550          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
3551          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
3552          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
3553          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
3554          mac_log=-log( mac_q1*mac_q1*mac_den )
3555          macrs=mac_q0*mac_log
3556          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
3557          ecrs=ecrs0
3558          decrs_drs=decrs0_drs
3559          decrs_dzeta=0.0_dp
3560          d2ecrs_drs2=d2ecrs0_drs2
3561          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
3562          d2ecrs_drsdzeta=zero
3563          zeta=0.0_dp
3564 
3565 !        Add LSD correlation functional to GGA exchange functional
3566          exci(ipts)=exci(ipts)+ecrs
3567          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
3568 
3569          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
3570 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
3571          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
3572          dvxci(ipts,9)=d2ecrs_drho2
3573          dvxci(ipts,10)=d2ecrs_drho2
3574          dvxci(ipts,11)=d2ecrs_drho2
3575 
3576 
3577 !        -----------------------------------------------------------------------------
3578 !        Eventually add the GGA correlation part of the PBE functional
3579 !        Note : the computation of the potential in the spin-unpolarized
3580 !        case could be optimized much further. Other optimizations are left to do.
3581 
3582          phi_zeta=1.0_dp
3583          phip_zeta=0.0_dp
3584          phi_zeta_inv=1.0_dp
3585          phi_logder=0.0_dp
3586          phi3_zeta=1.0_dp
3587          gamphi3inv=gamma_inv
3588          phipp_zeta=-two*ninth*alpha_zeta*alpha_zeta
3589 
3590 !        From ec to bb
3591          bb=ecrs*gamphi3inv
3592          dbb_drs=decrs_drs*gamphi3inv
3593          dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
3594          d2bb_drs2=d2ecrs_drs2*gamphi3inv
3595          d2bb_drsdzeta=gamphi3inv*(d2ecrs_drsdzeta-three*decrs_drs*phi_logder)
3596          d2bb_dzeta2=gamphi3inv*(d2ecrs_dzeta2-six*decrs_dzeta*phi_logder+&
3597 &         12.0_dp*ecrs*phi_logder*phi_logder-three*ecrs*phi_zeta_inv*phipp_zeta)
3598 
3599 !        From bb to cc
3600          exp_pbe=exp(-bb)
3601          cc=one/(exp_pbe-one)
3602          dcc_dbb=cc*cc*exp_pbe
3603          dcc_drs=dcc_dbb*dbb_drs
3604          dcc_dzeta=dcc_dbb*dbb_dzeta
3605          d2cc_dbb2=cc*cc*exp_pbe*(two*cc*exp_pbe-one)
3606          d2cc_drs2=d2cc_dbb2*dbb_drs*dbb_drs+dcc_dbb*d2bb_drs2
3607          d2cc_drsdzeta=d2cc_dbb2*dbb_drs*dbb_dzeta+dcc_dbb*d2bb_drsdzeta
3608          d2cc_dzeta2=d2cc_dbb2*dbb_dzeta*dbb_dzeta+dcc_dbb*d2bb_dzeta2
3609 
3610 !        From cc to aa
3611          coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
3612          aa=coeff_aa*cc
3613          daa_drs=coeff_aa*dcc_drs
3614          daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
3615          d2aa_drs2=coeff_aa*d2cc_drs2
3616          d2aa_drsdzeta=-two*daa_drs*phi_logder+coeff_aa*d2cc_drsdzeta
3617          d2aa_dzeta2=aa*(-two*phi_zeta_inv*phipp_zeta+six*phi_logder*phi_logder)+&
3618 &         coeff_aa*(-four*dcc_dzeta*phi_logder+d2cc_dzeta2)
3619 
3620 !        Introduce tt : do not assume that the spin-dependent gradients are collinear
3621          grrho2=four*grho2_updn(ipts,1)
3622          dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
3623 !        Note that tt is (the t variable of PBE divided by phi) squared
3624          tt=half*grrho2*dtt_dg
3625 
3626 !        Get xx from aa and tt
3627          xx=aa*tt
3628          dxx_drs=daa_drs*tt
3629          dxx_dzeta=daa_dzeta*tt
3630          dxx_dtt=aa
3631          d2xx_drs2=d2aa_drs2*tt
3632          d2xx_drsdzeta=d2aa_drsdzeta*tt
3633          d2xx_drsdtt=daa_drs
3634          d2xx_dttdzeta=daa_dzeta
3635          d2xx_dzeta2=d2aa_dzeta2*tt
3636 
3637 !        From xx to pade
3638          pade_den=one/(one+xx*(one+xx))
3639          pade=(one+xx)*pade_den
3640          dpade_dxx=-xx*(two+xx)*pade_den**2
3641          dpade_drs=dpade_dxx*dxx_drs
3642          dpade_dtt=dpade_dxx*dxx_dtt
3643          dpade_dzeta=dpade_dxx*dxx_dzeta
3644          d2pade_dxx2=two*(-one+xx*xx*(three+xx))*pade_den*pade_den*pade_den
3645          d2pade_drs2=d2pade_dxx2*dxx_drs*dxx_drs+dpade_dxx*d2xx_drs2
3646          d2pade_drsdtt=d2pade_dxx2*dxx_drs*dxx_dtt+dpade_dxx*d2xx_drsdtt
3647          d2pade_drsdzeta=d2pade_dxx2*dxx_drs*dxx_dzeta+dpade_dxx*d2xx_drsdzeta
3648          d2pade_dtt2=d2pade_dxx2*dxx_dtt*dxx_dtt
3649          d2pade_dttdzeta=d2pade_dxx2*dxx_dtt*dxx_dzeta+dpade_dxx*d2xx_dttdzeta
3650          d2pade_dzeta2=d2pade_dxx2*dxx_dzeta*dxx_dzeta+dpade_dxx*d2xx_dzeta2
3651 
3652 
3653 !        From pade to qq
3654          coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
3655          qq=coeff_qq*pade
3656          dqq_drs=coeff_qq*dpade_drs
3657          dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
3658          dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
3659          d2qq_drs2=coeff_qq*d2pade_drs2
3660          d2qq_drsdtt=phi_zeta_inv*phi_zeta_inv*(dpade_drs+tt*d2pade_drsdtt)
3661          d2qq_drsdzeta=coeff_qq*(d2pade_drsdzeta-two*dpade_drs*phi_logder)
3662          d2qq_dtt2=phi_zeta_inv*phi_zeta_inv*(two*dpade_dtt+tt*d2pade_dtt2)
3663          d2qq_dttdzeta=phi_zeta_inv*phi_zeta_inv*(dpade_dzeta-two*pade*phi_logder)+&
3664 &         coeff_qq*(d2pade_dttdzeta-two*dpade_dtt*phi_logder)
3665          d2qq_dzeta2=coeff_qq*( d2pade_dzeta2-four*dpade_dzeta*phi_logder &
3666 &         +six*pade*phi_logder*phi_logder            &
3667 &         -two*pade*phi_zeta_inv*phipp_zeta)
3668 
3669 !        From qq to rr
3670          arg_rr=one+beta*gamma_inv*qq
3671          div_rr=one/arg_rr
3672          rr=gamma*log(arg_rr)
3673          drr_dqq=beta*div_rr
3674          drr_drs=drr_dqq*dqq_drs
3675          drr_dtt=drr_dqq*dqq_dtt
3676          drr_dzeta=drr_dqq*dqq_dzeta
3677          d2rr_dqq2=-div_rr**2*beta*beta*gamma_inv
3678          d2rr_drs2=d2rr_dqq2*dqq_drs*dqq_drs+drr_dqq*d2qq_drs2
3679          d2rr_drsdtt=d2rr_dqq2*dqq_drs*dqq_dtt+drr_dqq*d2qq_drsdtt
3680          d2rr_drsdzeta=d2rr_dqq2*dqq_drs*dqq_dzeta+drr_dqq*d2qq_drsdzeta
3681          d2rr_dtt2=d2rr_dqq2*dqq_dtt*dqq_dtt+drr_dqq*d2qq_dtt2
3682          d2rr_dttdzeta=d2rr_dqq2*dqq_dtt*dqq_dzeta+drr_dqq*d2qq_dttdzeta
3683          d2rr_dzeta2=d2rr_dqq2*dqq_dzeta*dqq_dzeta+drr_dqq*d2qq_dzeta2
3684 
3685 !        From rr to hh
3686          hh=phi3_zeta*rr
3687          dhh_drs=phi3_zeta*drr_drs
3688          dhh_dtt=phi3_zeta*drr_dtt
3689          dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
3690          d2hh_drs2=phi3_zeta*d2rr_drs2
3691          d2hh_drsdtt=phi3_zeta*d2rr_drsdtt
3692          d2hh_drsdzeta=phi3_zeta*(d2rr_drsdzeta+three*drr_drs*phi_logder)
3693          d2hh_dtt2=phi3_zeta*d2rr_dtt2
3694          d2hh_dttdzeta=phi3_zeta*(d2rr_dttdzeta+three*drr_dtt*phi_logder)
3695          d2hh_dzeta2=phi3_zeta*(six*rr*phi_logder*phi_logder+&
3696 &         six*phi_logder*drr_dzeta+d2rr_dzeta2)  &
3697 &         +three*phi_zeta*phi_zeta*rr*phipp_zeta
3698 
3699 
3700 !        The GGA correlation energy is added
3701          exci(ipts)=exci(ipts)+hh
3702 
3703 !        Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
3704 
3705 
3706 !        From hh to the derivative of the energy wrt the density
3707          drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
3708          vxci(ipts,1)=vxci(ipts,1)+drhohh_drho
3709 
3710 !        From hh to the derivative of the energy wrt to the gradient of the
3711 !        density, divided by the gradient of the density
3712 !        (The v3.3 definition includes the division by the norm of the gradient)
3713          dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
3714 
3715          d2rhohh_drho2=rhotot_inv*&
3716 &         (-two*ninth*rs*dhh_drs +seven*four*ninth*tt*dhh_dtt &
3717 &         +ninth*rs*rs*d2hh_drs2+zeta*zeta*d2hh_dzeta2+(seven*third*tt)**2*d2hh_dtt2 &
3718 &         +two*third*rs*zeta*d2hh_drsdzeta+two*seven*ninth*rs*tt*d2hh_drsdtt &
3719 &         +two*seven*third*tt*zeta*d2hh_dttdzeta)
3720          d2rhohh_drhodg=dtt_dg*(-four*third*dhh_dtt-third*rs*d2hh_drsdtt &
3721 &         -zeta*d2hh_dttdzeta-seven*third*tt*d2hh_dtt2)
3722 
3723 !        Component 12 : first derivative with respect to the gradient
3724 !        of the density, div by the grad of the density
3725          dvxci(ipts,12)=dvxcdgr(ipts,3)
3726 !        Components 9, 10 and 11 : second derivatives with respect to the spin-density
3727 !        Note that there is already a contribution from LSDA
3728          dvxci(ipts,9)=dvxci(ipts,9)+d2rhohh_drho2+rhotot_inv*           &
3729 &         ( d2hh_dzeta2*(one-two*zeta) &
3730 &         -two*third*rs*d2hh_drsdzeta-14.0_dp*third*tt*d2hh_dttdzeta)
3731          dvxci(ipts,10)=dvxci(ipts,10)+d2rhohh_drho2-rhotot_inv*d2hh_dzeta2
3732          dvxci(ipts,11)=dvxci(ipts,11)+d2rhohh_drho2+rhotot_inv*           &
3733 &         ( d2hh_dzeta2*(one+two*zeta) &
3734 &         +two*third*rs*d2hh_drsdzeta+14.0_dp*third*tt*d2hh_dttdzeta)
3735 !        Components 13 and 14 : second derivatives with respect to spin density
3736 !        and gradient, divided by the gradient
3737          dvxci(ipts,13)=d2rhohh_drhodg+dtt_dg*d2hh_dttdzeta
3738          dvxci(ipts,14)=d2rhohh_drhodg-dtt_dg*d2hh_dttdzeta
3739 !        Component 15 : derivative of the (derivative wrt the gradient div by the grad),
3740 !        divided by the grad
3741          dvxci(ipts,15)=rhotot*d2hh_dtt2*dtt_dg*dtt_dg
3742 
3743 
3744 !        End condition of GGA
3745 
3746 !        Correlation has been added
3747 !        -----------------------------------------------------------------------------
3748 
3749 !        vxci(ipts,2)=vxci(ipts,1)
3750          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
3751 
3752        end do
3753 
3754      else if (option==-1) then
3755        do ipts=1,npts
3756 
3757          rhotot=rhoarr(ipts)
3758          rhotmot=rhom1_3(ipts)
3759          rhotot_inv=rhotmot*rhotmot*rhotmot
3760          rhotmo6=sqrt(rhotmot)
3761          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
3762 !        -----------------------------------------------------------------------
3763 !        First take care of the exchange part of the functional
3764 
3765          exc=zero
3766 !        loop over the spin
3767          ispden=1
3768          rho   =rho_updn(ipts,ispden)
3769          rhomot=rho_updnm1_3(ipts,ispden)
3770          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
3771 !        Perdew_Wang 91 LSD
3772          vxci(ipts,ispden)=four_thirds*ex_lsd
3773          exc=exc+ex_lsd*rho
3774 
3775 !        Perdew_Wang 91 LSD
3776          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
3777 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
3778          dvxci(ipts,2)=zero
3779 !        If non-spin-polarized, first component of dvxci is second
3780 !        derivative with respect to TOTAL density.
3781          dvxci(ipts,1)=dvxci(ipts,1)*half
3782 !        Compute the second derivative of vx
3783 !        vx^(2) = -2*vx^(1)/(3*rhotot)
3784 !        end of loop over the spin
3785 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3786          exc=exc*2
3787          exci(ipts)=exc*rhotot_inv
3788 !        -----------------------------------------------------------------------------
3789 !        Then takes care of the LSD correlation part of the functional
3790 
3791        end do
3792 
3793      else if (option==-2) then
3794        do ipts=1,npts
3795 
3796          rhotot=rhoarr(ipts)
3797          rhotmot=rhom1_3(ipts)
3798          rhotot_inv=rhotmot*rhotmot*rhotmot
3799          rhotmo6=sqrt(rhotmot)
3800          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
3801 !        -----------------------------------------------------------------------
3802 !        First take care of the exchange part of the functional
3803 
3804          exc=zero
3805          dvxcdgr(ipts,3)=zero
3806 !        loop over the spin
3807          ispden=1
3808          rho   =rho_updn(ipts,ispden)
3809          rhomot=rho_updnm1_3(ipts,ispden)
3810          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
3811 !        Perdew-Burke-Ernzerhof GGA, exchange part
3812          rho_inv=rhomot*rhomot*rhomot
3813          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
3814          ss=grho2_updn(ipts,ispden)*coeffss
3815          divss=one/(one+mu_divkappa*ss)
3816          dfxdss= mu*divss*divss
3817          d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
3818          fx    = one+kappa*(one-divss)
3819          ex_gga= ex_lsd*fx
3820          dssdn=-eight*third*ss*rho_inv
3821          dfxdn  = dfxdss*dssdn
3822          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
3823 !        The new definition (v3.3) includes the division by the norm of the gradient
3824          dssdg =two*coeffss
3825          dfxdg=dfxdss*dssdg
3826          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
3827          exc=exc+ex_gga*rho
3828 
3829 !        Perdew-Burke-Ernzerhof GGA, exchange part
3830 !        Components 3 or 4
3831          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
3832 !        Components 1 or 2
3833          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
3834          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
3835          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
3836 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
3837 !        Components 5 or 6
3838          d2ssdndg=-eight*third*dssdg*rho_inv
3839          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
3840          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
3841 !        Components 7 or 8
3842          d2fxdg2=d2fxdss2*dssdg**2
3843          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
3844 !        For the time being, treat non-spin-polarized like spin-polarized
3845          dvxci(ipts,2)=dvxci(ipts,1)
3846          dvxci(ipts,4)=dvxci(ipts,3)
3847          dvxci(ipts,6)=dvxci(ipts,5)
3848          dvxci(ipts,8)=dvxci(ipts,7)
3849 !        end of loop over the spin
3850 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3851          exc=exc*2
3852          exci(ipts)=exc*rhotot_inv
3853 !        -----------------------------------------------------------------------------
3854 !        Then takes care of the LSD correlation part of the functional
3855 
3856 !        Correlation has been added
3857 !        -----------------------------------------------------------------------------
3858 
3859 !        vxci(ipts,2)=vxci(ipts,1)
3860          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
3861 
3862        end do
3863 
3864 
3865      else if(option==-4) then
3866 
3867 
3868        do ipts=1,npts
3869 
3870          rhotot=rhoarr(ipts)
3871          rhotmot=rhom1_3(ipts)
3872          rhotot_inv=rhotmot*rhotmot*rhotmot
3873          rhotmo6=sqrt(rhotmot)
3874          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
3875 !        -----------------------------------------------------------------------
3876 !        First take care of the exchange part of the functional
3877 
3878          exc=zero
3879          dvxcdgr(ipts,3)=zero
3880 !        loop over the spin
3881          ispden=1
3882          rho   =rho_updn(ipts,ispden)
3883          rhomot=rho_updnm1_3(ipts,ispden)
3884          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
3885 !        VALENTINO R. COOPER C09x GGA, This is an exchange term proposed
3886 !        to use together with vdw-DF (see above).
3887          rho_inv=rhomot*rhomot*rhomot
3888          coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
3889 !        the quarter that is lacking is compensated by the grho2_updn in the
3890 !        next line.
3891          ss=grho2_updn(ipts,ispden)*coeffss
3892          alphs2=alpha_c09*ss
3893          alphmu=alpha_c09*mu_c09
3894          dfxdss= mu_c09*exp(-alphs2)*(one-alphs2)+&
3895 &         kappa*alpha_c09*exp(-alphs2/two)/two
3896          d2fxdss2=-alphmu*exp(-alphs2)*(two-alphs2)-&
3897 &         kappa*(alpha_c09**two)*exp(alphs2/two)/four
3898          fx    = one+mu_c09*ss*exp(-alphs2)+kappa*(one-exp(-alphs2/two))
3899          ex_gga= ex_lsd*fx
3900          dssdn=-eight*third*ss*rho_inv
3901          dfxdn  = dfxdss*dssdn
3902          vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
3903 !        The new definition (v3.3) includes the division by the norm of the gradient
3904          dssdg =two*coeffss
3905          dfxdg=dfxdss*dssdg
3906          dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
3907          exc=exc+ex_gga*rho
3908 
3909 !        Cooper C09x GGA exchange
3910 !        Components 3 or 4
3911          dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
3912 !        Components 1 or 2
3913          d2ssdn2=-11.0_dp*third*dssdn*rho_inv
3914          d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
3915          dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
3916 &         ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
3917 !        Components 5 or 6
3918          d2ssdndg=-eight*third*dssdg*rho_inv
3919          d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
3920          dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
3921 !        Components 7 or 8
3922          d2fxdg2=d2fxdss2*dssdg**2
3923          dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
3924 !        For the time being, treat non-spin-polarized like spin-polarized
3925          dvxci(ipts,2)=dvxci(ipts,1)
3926          dvxci(ipts,4)=dvxci(ipts,3)
3927          dvxci(ipts,6)=dvxci(ipts,5)
3928          dvxci(ipts,8)=dvxci(ipts,7)
3929 
3930 !        end of loop over the spin
3931 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3932          exc=exc*2
3933          exci(ipts)=exc*rhotot_inv
3934 !        -----------------------------------------------------------------------------
3935 !        Then takes care of the LSD correlation part of the functional
3936 
3937 !        Correlation has been added
3938 !        -----------------------------------------------------------------------------
3939 
3940 !        vxci(ipts,2)=vxci(ipts,1)
3941          dvxcdgr(ipts,2)=dvxcdgr(ipts,1)
3942 
3943        end do
3944 
3945 
3946      else if (option==1) then
3947        do ipts=1,npts
3948 
3949          rhotot=rhoarr(ipts)
3950          rhotmot=rhom1_3(ipts)
3951          rhotot_inv=rhotmot*rhotmot*rhotmot
3952          rhotmo6=sqrt(rhotmot)
3953          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
3954 !        -----------------------------------------------------------------------
3955 !        First take care of the exchange part of the functional
3956 
3957          exc=zero
3958 !        loop over the spin
3959          ispden=1
3960          rho   =rho_updn(ipts,ispden)
3961          rhomot=rho_updnm1_3(ipts,ispden)
3962          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
3963 !        Perdew_Wang 91 LSD
3964          vxci(ipts,ispden)=four_thirds*ex_lsd
3965          exc=exc+ex_lsd*rho
3966 
3967 !        Perdew_Wang 91 LSD
3968          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
3969 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
3970          dvxci(ipts,2)=zero
3971 !        If non-spin-polarized, first component of dvxci is second
3972 !        derivative with respect to TOTAL density.
3973          dvxci(ipts,1)=dvxci(ipts,1)*half
3974 !        Compute the second derivative of vx
3975 !        vx^(2) = -2*vx^(1)/(3*rhotot)
3976 !        end of loop over the spin
3977 !        If non spin-polarized, treat spin down contribution now, similar to spin up
3978          exc=exc*2
3979          exci(ipts)=exc*rhotot_inv
3980 !        -----------------------------------------------------------------------------
3981 !        Then takes care of the LSD correlation part of the functional
3982 
3983 
3984          rs=rsfac*rhotmot
3985          sqr_rs=sq_rsfac*rhotmo6
3986          rsm1_2=sq_rsfac_inv*rhoto6
3987 
3988 !        Formulas A6-A8 of PW92LSD
3989          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
3990          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
3991          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
3992          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
3993 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
3994          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
3995          ecrs0=ec0_q0*ec0_log
3996          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
3997          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
3998          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
3999 &         -ec0_q0*ec0_q1pp*ec0_den                        &
4000 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
4001          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
4002          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
4003          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
4004          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
4005          mac_log=-log( mac_q1*mac_q1*mac_den )
4006          macrs=mac_q0*mac_log
4007          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
4008          ecrs=ecrs0
4009          decrs_drs=decrs0_drs
4010          decrs_dzeta=0.0_dp
4011          d2ecrs_drs2=d2ecrs0_drs2
4012          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
4013          d2ecrs_drsdzeta=zero
4014          zeta=0.0_dp
4015 
4016 !        Add LSD correlation functional to GGA exchange functional
4017          exci(ipts)=exci(ipts)+ecrs
4018          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
4019 
4020          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
4021 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
4022          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
4023 
4024          dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drho2
4025 
4026        end do
4027      else if (option==3) then
4028        do ipts=1,npts
4029 
4030          rhotot=rhoarr(ipts)
4031          rhotmot=rhom1_3(ipts)
4032          rhotot_inv=rhotmot*rhotmot*rhotmot
4033          rhotmo6=sqrt(rhotmot)
4034          rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
4035 !        -----------------------------------------------------------------------
4036 !        First take care of the exchange part of the functional
4037 
4038          exc=zero
4039 !        loop over the spin
4040          ispden=1
4041          rho   =rho_updn(ipts,ispden)
4042          rhomot=rho_updnm1_3(ipts,ispden)
4043          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
4044 !        Perdew_Wang 91 LSD
4045          vxci(ipts,ispden)=four_thirds*ex_lsd
4046          exc=exc+ex_lsd*rho
4047 
4048 !        Perdew_Wang 91 LSD
4049          dvxci(ipts,2*ispden-1)=-four_thirds*third*&
4050 &         threefourth_divpi*sixpi2_1_3*rhomot*rhomot
4051          dvxci(ipts,2)=zero
4052 !        If non-spin-polarized, first component of dvxci is second
4053 !        derivative with respect to TOTAL density.
4054          dvxci(ipts,1)=dvxci(ipts,1)*half
4055 !        Compute the second derivative of vx
4056 !        vx^(2) = -2*vx^(1)/(3*rhotot)
4057 !        end of loop over the spin
4058 !        If non spin-polarized, treat spin down contribution now, similar to spin up
4059          exc=exc*2
4060          exci(ipts)=exc*rhotot_inv
4061 !        -----------------------------------------------------------------------------
4062 !        Then takes care of the LSD correlation part of the functional
4063 
4064 
4065          rs=rsfac*rhotmot
4066          sqr_rs=sq_rsfac*rhotmo6
4067          rsm1_2=sq_rsfac_inv*rhoto6
4068 
4069 !        Formulas A6-A8 of PW92LSD
4070          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
4071          sqr_sqr_rs=max(1.e-15_dp,sqrt(sqr_rs))
4072          ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs/sqr_sqr_rs)
4073          ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+3.5_dp*ec0_b4*rs/sqr_sqr_rs)
4074          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
4075 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
4076          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
4077          ecrs0=ec0_q0*ec0_log
4078          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
4079          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
4080          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
4081 &         -ec0_q0*ec0_q1pp*ec0_den                        &
4082 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
4083          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
4084          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
4085          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
4086          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
4087          mac_log=-log( mac_q1*mac_q1*mac_den )
4088          macrs=mac_q0*mac_log
4089          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
4090          ecrs=ecrs0
4091          decrs_drs=decrs0_drs
4092          decrs_dzeta=0.0_dp
4093          d2ecrs_drs2=d2ecrs0_drs2
4094          d2ecrs_dzeta2=alpha_zeta**2*(-macrs)
4095          d2ecrs_drsdzeta=zero
4096          zeta=0.0_dp
4097 
4098 !        Add LSD correlation functional to GGA exchange functional
4099          exci(ipts)=exci(ipts)+ecrs
4100          vxci(ipts,1)=vxci(ipts,1)+ecrs-rs*third*decrs_drs
4101 
4102          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
4103 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
4104          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
4105          dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drho2
4106 
4107        end do
4108      end if
4109 
4110    end if
4111 
4112 
4113 !  fab: here it starts the spin polarized case
4114 
4115  else if(nspden==2) then
4116 
4117 !  we separate different cases depending on order
4118 
4119    if (order**2<=1) then
4120 
4121      do ipts=1,npts
4122 
4123        rhotot=rhoarr(ipts)
4124        rhotmot=rhom1_3(ipts)
4125        rhotot_inv=rhotmot*rhotmot*rhotmot
4126        rhotmo6=sqrt(rhotmot)
4127        rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
4128 !      -----------------------------------------------------------------------
4129 !      First take care of the exchange part of the functional
4130 
4131        exc=zero
4132        if (present(dvxcdgr)) dvxcdgr(ipts,3)=zero
4133        do ispden=1,nspden
4134          rho   =rho_updn(ipts,ispden)
4135          rhomot=rho_updnm1_3(ipts,ispden)
4136          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
4137          if(option==1 .or. option==-1 .or. option==3)then
4138 !          Perdew_Wang 91 LSD
4139            vxci(ipts,ispden)=four_thirds*ex_lsd
4140            if(present(dvxcdgr)) dvxcdgr(ipts,ispden)=0.0_dp
4141            exc=exc+ex_lsd*rho
4142          else
4143            rho_inv=rhomot*rhomot*rhomot
4144            coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
4145            ss=grho2_updn(ipts,ispden)*coeffss
4146            if(option==7) then ! This is WC
4147              expss=exp(-ss)
4148              p1_wc=b_wc+(mu-b_wc)*(one-ss)*expss+two*c_wc*ss/(one+c_wc*ss*ss)
4149              p2_wc=d_wc*(ss-two)*expss+two*c_wc/(one+c_wc*ss*ss)-&
4150 &             four*c_wc*c_wc*ss*ss/((one+c_wc*ss*ss)*(one+c_wc*ss*ss))
4151              divss=one/(one+(b_wc*ss+d_wc*ss*expss+log(one+c_wc*ss*ss))/kappa)
4152              dfxdss=p1_wc*divss*divss
4153              d2fxdss2=p2_wc*divss*divss-two*divss*divss*divss*p1_wc*p1_wc/kappa
4154            else
4155              if(option/=6)then ! This is Perdew-Burke-Ernzerhof GGA, exchange part
4156                divss=one/(one+mu_divkappa*ss)
4157                dfxdss= mu*divss*divss
4158                d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
4159              else  ! This is RPBE modification
4160                divss=exp(-mu_divkappa*ss)
4161                dfxdss= mu*divss
4162                d2fxdss2=-mu*mu_divkappa*divss
4163              end if
4164            end if
4165            fx    = one+kappa*(one-divss)
4166            ex_gga= ex_lsd*fx
4167            dssdn=-eight*third*ss*rho_inv
4168            dfxdn  = dfxdss*dssdn
4169            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
4170 !          The new definition (v3.3) includes the division by the norm of the gradient
4171            dssdg =two*coeffss
4172            dfxdg=dfxdss*dssdg
4173            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
4174            exc=exc+ex_gga*rho
4175          end if
4176 
4177        end do
4178        exci(ipts)=exc*rhotot_inv
4179        if(exexch_==1)cycle
4180 
4181 !      -----------------------------------------------------------------------------
4182 !      Then takes care of the LSD correlation part of the functional
4183 
4184        if(option>0)then
4185 
4186          rs=rsfac*rhotmot
4187          sqr_rs=sq_rsfac*rhotmo6
4188          rsm1_2=sq_rsfac_inv*rhoto6
4189 
4190 !        Formulas A6-A8 of PW92LSD
4191          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
4192          if(option/=3 .and. option/=4)then
4193            ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
4194            ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
4195          else
4196            sqr_sqr_rs=max(1.e-15_dp,sqrt(sqr_rs))
4197            ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs/sqr_sqr_rs)
4198            ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+3.5_dp*ec0_b4*rs/sqr_sqr_rs)
4199          end if
4200          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
4201 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
4202          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
4203          ecrs0=ec0_q0*ec0_log
4204          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
4205 
4206          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
4207          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
4208          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
4209          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
4210          mac_log=-log( mac_q1*mac_q1*mac_den )
4211          macrs=mac_q0*mac_log
4212          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
4213 
4214          zeta=(rho_updn(ipts,1)-rho_updn(ipts,2))*rhotot_inv
4215          ec1_q0=-2.0_dp*ec1_aa*(1.0_dp+ec1_a1*rs)
4216          if(option/=3 .and. option/=4)then
4217            ec1_q1=2.0_dp*ec1_aa*(ec1_b1*sqr_rs+ec1_b2*rs+ec1_b3*rs*sqr_rs+ec1_b4*rs*rs)
4218            ec1_q1p=ec1_aa*(ec1_b1*rsm1_2+2._dp*ec1_b2+3._dp*ec1_b3*sqr_rs+4._dp*ec1_b4*rs)
4219          else
4220            ec1_q1=2.0_dp*ec1_aa*(ec1_b1*sqr_rs+ec1_b2*rs+ec1_b3*rs*sqr_rs+ec1_b4*rs*rs/sqr_sqr_rs)
4221            ec1_q1p=ec1_aa*(ec1_b1*rsm1_2+2._dp*ec1_b2+3._dp*ec1_b3*sqr_rs+3.5_dp*ec1_b4*rs/sqr_sqr_rs)
4222          end if
4223          ec1_den=1.0_dp/(ec1_q1*ec1_q1+ec1_q1)
4224 !        ec1_log=log( 1.0_dp + 1.0_dp / ec1_q1 )
4225          ec1_log=-log( ec1_q1*ec1_q1*ec1_den )
4226          ecrs1=ec1_q0*ec1_log
4227          decrs1_drs= -2.0_dp*ec1_aa*ec1_a1*ec1_log - ec1_q0*ec1_q1p *ec1_den
4228 
4229 !        alpha_zeta is introduced in order to remove singularities for fully
4230 !        polarized systems.
4231          zetp_1_3=(1.0_dp+zeta*alpha_zeta)*zetpm1_3(ipts)**2
4232          zetm_1_3=(1.0_dp-zeta*alpha_zeta)*zetmm1_3(ipts)**2
4233 
4234          f_zeta=( (1.0_dp+zeta*alpha_zeta2)*zetp_1_3 +                      &
4235 &         (1.0_dp-zeta*alpha_zeta2)*zetm_1_3 - 2.0_dp ) * factf_zeta
4236          fp_zeta=( zetp_1_3 - zetm_1_3 ) * factfp_zeta
4237          zeta4=zeta**4
4238 
4239          gcrs=ecrs1-ecrs0+macrs*fsec_inv
4240 !        ecrs=ecrs0+f_zeta*(-macrs*(1.0_dp-zeta4)*fsec_inv+(ecrs1-ecrs0)*zeta4)
4241          ecrs=ecrs0+f_zeta*(zeta4*gcrs-macrs*fsec_inv)
4242 
4243          dgcrs_drs=decrs1_drs-decrs0_drs+dmacrs_drs*fsec_inv
4244 !        decrs_drs=decrs0_drs+f_zeta*&
4245 !        &        (-dmacrs_drs*(1.0_dp-zeta4)*fsec_inv+(decrs1_drs-decrs0_drs)*zeta4)
4246          decrs_drs=decrs0_drs+f_zeta*(zeta4*dgcrs_drs-dmacrs_drs*fsec_inv)
4247          dfzeta4_dzeta=4.0_dp*zeta**3*f_zeta+fp_zeta*zeta4
4248          decrs_dzeta=dfzeta4_dzeta*gcrs-fp_zeta*macrs*fsec_inv
4249 
4250 !        Add LSD correlation functional to GGA exchange functional
4251          exci(ipts)=exci(ipts)+ecrs
4252          vxcadd=ecrs-rs*third*decrs_drs-zeta*decrs_dzeta
4253          vxci(ipts,1)=vxci(ipts,1)+vxcadd+decrs_dzeta
4254          vxci(ipts,2)=vxci(ipts,2)+vxcadd-decrs_dzeta
4255 
4256 !        -----------------------------------------------------------------------------
4257 !        Eventually add the GGA correlation part of the PBE functional
4258 !        Note : the computation of the potential in the spin-unpolarized
4259 !        case could be optimized much further. Other optimizations are left to do.
4260 
4261          if(option==2 .or. option==5 .or. option==6 .or. option==7)then
4262 !          The definition of phi has been slightly changed, because
4263 !          the original PBE one gives divergent behaviour for fully
4264 !          polarized points
4265 !          zetpm1_3=(1.0_dp+zeta*alpha_zeta)**(-third)
4266 !          zetmm1_3=(1.0_dp-zeta*alpha_zeta)**(-third)
4267            phi_zeta=( zetpm1_3(ipts)*(1.0_dp+zeta*alpha_zeta)+ &
4268 &           zetmm1_3(ipts)*(1.0_dp-zeta*alpha_zeta)   )*0.5_dp
4269            phip_zeta=(zetpm1_3(ipts)-zetmm1_3(ipts))*third*alpha_zeta
4270            phi_zeta_inv=1.0_dp/phi_zeta
4271            phi_logder=phip_zeta*phi_zeta_inv
4272            phi3_zeta=phi_zeta*phi_zeta*phi_zeta
4273            gamphi3inv=gamma_inv*phi_zeta_inv*phi_zeta_inv*phi_zeta_inv
4274 
4275 !          From ec to bb
4276            bb=ecrs*gamphi3inv
4277            dbb_drs=decrs_drs*gamphi3inv
4278            dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
4279 !          From bb to cc
4280            exp_pbe=exp(-bb)
4281            cc=one/(exp_pbe-one)
4282            dcc_dbb=cc*cc*exp_pbe
4283            dcc_drs=dcc_dbb*dbb_drs
4284            dcc_dzeta=dcc_dbb*dbb_dzeta
4285 
4286 !          From cc to aa
4287            coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
4288            aa=coeff_aa*cc
4289            daa_drs=coeff_aa*dcc_drs
4290            daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
4291 !          Introduce tt : do not assume that the spin-dependent gradients are collinear
4292            grrho2=grho2_updn(ipts,3)
4293            dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
4294 !          Note that tt is (the t variable of PBE divided by phi) squared
4295            tt=half*grrho2*dtt_dg
4296 
4297 !          Get xx from aa and tt
4298            xx=aa*tt
4299            dxx_drs=daa_drs*tt
4300            dxx_dzeta=daa_dzeta*tt
4301            dxx_dtt=aa
4302 !          From xx to pade
4303            pade_den=one/(one+xx*(one+xx))
4304            pade=(one+xx)*pade_den
4305            dpade_dxx=-xx*(two+xx)*pade_den**2
4306            dpade_drs=dpade_dxx*dxx_drs
4307            dpade_dtt=dpade_dxx*dxx_dtt
4308            dpade_dzeta=dpade_dxx*dxx_dzeta
4309 
4310 !          From pade to qq
4311            coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
4312            qq=coeff_qq*pade
4313            dqq_drs=coeff_qq*dpade_drs
4314            dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
4315            dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
4316 
4317 !          From qq to rr
4318            arg_rr=one+beta*gamma_inv*qq
4319            div_rr=one/arg_rr
4320            rr=gamma*log(arg_rr)
4321            drr_dqq=beta*div_rr
4322            drr_drs=drr_dqq*dqq_drs
4323            drr_dtt=drr_dqq*dqq_dtt
4324            drr_dzeta=drr_dqq*dqq_dzeta
4325 
4326 !          From rr to hh
4327            hh=phi3_zeta*rr
4328            dhh_drs=phi3_zeta*drr_drs
4329            dhh_dtt=phi3_zeta*drr_dtt
4330            dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
4331 
4332 !          The GGA correlation energy is added
4333            exci(ipts)=exci(ipts)+hh
4334 
4335 !          Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
4336 
4337 !          From hh to the derivative of the energy wrt the density
4338            drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
4339            vxci(ipts,1)=vxci(ipts,1)+drhohh_drho+dhh_dzeta
4340            vxci(ipts,2)=vxci(ipts,2)+drhohh_drho-dhh_dzeta
4341 
4342 
4343 !          From hh to the derivative of the energy wrt to the gradient of the
4344 !          density, divided by the gradient of the density
4345 !          (The v3.3 definition includes the division by the norm of the gradient)
4346            dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
4347 
4348 !          End condition of GGA
4349          end if
4350 
4351        else  ! no correlation
4352 
4353 !        End condition of including correlation, and not only exchange
4354        end if
4355 
4356 !      Correlation has been added
4357 !      -----------------------------------------------------------------------------
4358 
4359      end do
4360 
4361 !    fab: the following is the "else" on order !!!
4362 
4363    else
4364 
4365      do ipts=1,npts
4366 
4367        rhotot=rhoarr(ipts)
4368        rhotmot=rhom1_3(ipts)
4369        rhotot_inv=rhotmot*rhotmot*rhotmot
4370        rhotmo6=sqrt(rhotmot)
4371        rhoto6=rhotot*rhotmot*rhotmot*rhotmo6
4372 !      -----------------------------------------------------------------------
4373 !      First take care of the exchange part of the functional
4374 
4375        exc=zero
4376        if (present(dvxcdgr)) dvxcdgr(ipts,3)=zero
4377        do ispden=1,nspden
4378          rho   =rho_updn(ipts,ispden)
4379          rhomot=rho_updnm1_3(ipts,ispden)
4380          ex_lsd= - threefourth_divpi * sixpi2_1_3*rhomot*rhomot*rho
4381          if(option==1 .or. option==-1 .or. option==3)then
4382 !          Perdew_Wang 91 LSD
4383            vxci(ipts,ispden)=four_thirds*ex_lsd
4384            if(present(dvxcdgr)) dvxcdgr(ipts,ispden)=0.0_dp
4385            exc=exc+ex_lsd*rho
4386          else
4387            rho_inv=rhomot*rhomot*rhomot
4388            coeffss=quarter*sixpi2m1_3*sixpi2m1_3*rho_inv*rho_inv*rhomot*rhomot
4389            ss=grho2_updn(ipts,ispden)*coeffss
4390            if(option==7) then ! This is WC
4391              expss=exp(-ss)
4392              p1_wc=b_wc+(mu-b_wc)*(one-ss)*expss+two*c_wc*ss/(one+c_wc*ss*ss)
4393              p2_wc=d_wc*(ss-two)*expss+two*c_wc/(one+c_wc*ss*ss)-&
4394 &             four*c_wc*c_wc*ss*ss/((one+c_wc*ss*ss)*(one+c_wc*ss*ss))
4395              divss=one/(one+(b_wc*ss+d_wc*ss*expss+log(one+c_wc*ss*ss))/kappa)
4396              dfxdss=p1_wc*divss*divss
4397              d2fxdss2=p2_wc*divss*divss-two*divss*divss*divss*p1_wc*p1_wc/kappa
4398            else
4399              if(option/=6)then ! This is Perdew-Burke-Ernzerhof GGA, exchange part
4400                divss=one/(one+mu_divkappa*ss)
4401                dfxdss= mu*divss*divss
4402                d2fxdss2=-mu*two*mu_divkappa*divss*divss*divss
4403              else  ! This is RPBE modification
4404                divss=exp(-mu_divkappa*ss)
4405                dfxdss= mu*divss
4406                d2fxdss2=-mu*mu_divkappa*divss
4407              end if
4408            end if
4409            fx    = one+kappa*(one-divss)
4410            ex_gga= ex_lsd*fx
4411            dssdn=-eight*third*ss*rho_inv
4412            dfxdn  = dfxdss*dssdn
4413            vxci(ipts,ispden)=ex_lsd*(four_thirds*fx+rho*dfxdn)
4414 !          The new definition (v3.3) includes the division by the norm of the gradient
4415            dssdg =two*coeffss
4416            dfxdg=dfxdss*dssdg
4417            dvxcdgr(ipts,ispden)=ex_lsd*rho*dfxdg
4418            exc=exc+ex_gga*rho
4419          end if
4420 
4421          if(option==1 .or. option==-1 .or. option==3)then
4422 
4423 !          Perdew_Wang 91 LSD
4424            dvxci(ipts,2*ispden-1)=-four_thirds*third*&
4425 &           threefourth_divpi*sixpi2_1_3*rhomot*rhomot
4426            dvxci(ipts,2)=zero
4427            if(order==3)then
4428 !            If non-spin-polarized, first component of dvxci is second
4429 !            derivative with respect to TOTAL density.
4430 !            Compute the second derivative of vx
4431 !            vx^(2) = -2*vx^(1)/(3*rhotot)
4432 
4433 !            fab: third order derivatives of the exchange part in the spin polarized case
4434 
4435              d2vxci(ipts,3*ispden-2) = -2._dp*dvxci(ipts,2*ispden-1)*(rhomot*rhomot*rhomot)/3._dp
4436 
4437 !            mixed thir order derivatives of the exchange energy with respect to rho of
4438 !            different spin polarization are zero
4439              d2vxci(ipts,2)=zero
4440              d2vxci(ipts,3)=zero
4441 
4442            end if
4443 
4444          else
4445 !          Perdew-Burke-Ernzerhof GGA, exchange part
4446 !          Components 3 or 4
4447            dvxci(ipts,2+ispden)=dvxcdgr(ipts,ispden)
4448 !          Components 1 or 2
4449            d2ssdn2=-11.0_dp*third*dssdn*rho_inv
4450            d2fxdn2=d2fxdss2*dssdn**2+dfxdss*d2ssdn2
4451            dvxci(ipts,ispden)=third*rho_inv*vxci(ipts,ispden)+&
4452 &           ex_lsd*(seven*third*dfxdn+rho*d2fxdn2)
4453 !          Components 5 or 6
4454            d2ssdndg=-eight*third*dssdg*rho_inv
4455            d2fxdndg=d2fxdss2*dssdn*dssdg+dfxdss*d2ssdndg
4456            dvxci(ipts,4+ispden)=ex_lsd*(four_thirds*dfxdg+rho*d2fxdndg)
4457 !          Components 7 or 8
4458            d2fxdg2=d2fxdss2*dssdg**2
4459            dvxci(ipts,6+ispden)=ex_lsd*rho*d2fxdg2
4460 !          For the time being, treat non-spin-polarized like spin-polarized
4461          end if
4462        end do
4463        exci(ipts)=exc*rhotot_inv
4464 !      -----------------------------------------------------------------------------
4465 !      Then takes care of the LSD correlation part of the functional
4466 
4467        if(option>0)then
4468 
4469          rs=rsfac*rhotmot
4470          sqr_rs=sq_rsfac*rhotmo6
4471          rsm1_2=sq_rsfac_inv*rhoto6
4472 
4473 !        Formulas A6-A8 of PW92LSD
4474          ec0_q0=-2.0_dp*ec0_aa*(1.0_dp+ec0_a1*rs)
4475          if(option/=3 .and. option/=4)then
4476            ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs)
4477            ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+4._dp*ec0_b4*rs)
4478          else
4479            sqr_sqr_rs=max(1.e-15_dp,sqrt(sqr_rs))
4480            ec0_q1=2.0_dp*ec0_aa*(ec0_b1*sqr_rs+ec0_b2*rs+ec0_b3*rs*sqr_rs+ec0_b4*rs*rs/sqr_sqr_rs)
4481            ec0_q1p=ec0_aa*(ec0_b1*rsm1_2+2._dp*ec0_b2+3._dp*ec0_b3*sqr_rs+3.5_dp*ec0_b4*rs/sqr_sqr_rs)
4482          end if
4483          ec0_den=1.0_dp/(ec0_q1*ec0_q1+ec0_q1)
4484 !        ec0_log=log( 1.0_dp + 1.0_dp / ec0_q1 )
4485          ec0_log=-log( ec0_q1*ec0_q1*ec0_den )
4486          ecrs0=ec0_q0*ec0_log
4487          decrs0_drs= -2.0_dp*ec0_aa*ec0_a1*ec0_log - ec0_q0*ec0_q1p *ec0_den
4488          ec0_q1pp=0.5_dp*ec0_aa*(-ec0_b1*rsm1_2**3+3._dp*ec0_b3*rsm1_2+8._dp*ec0_b4)
4489          d2ecrs0_drs2= 4.0_dp*ec0_aa*ec0_a1*ec0_q1p*ec0_den            &
4490 &         -ec0_q0*ec0_q1pp*ec0_den                        &
4491 &         +ec0_q0*ec0_q1p**2*ec0_den**2*(2._dp*ec0_q1+1.0_dp)
4492          if (order==3) then
4493            ec0_q1ppp = 0.75_dp*ec0_aa*(rsm1_2**5)*(ec0_b1-ec0_b3*rs)
4494            ec0_f1 = 1._dp/(ec0_q1*ec0_q1*(1._dp + ec0_q1))
4495            ec0_f2 = 1._dp/(ec0_q1*(1+ec0_q1))
4496            d3ecrs0_drs3 = 6._dp*ec0_q1p*ec0_f1*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + &
4497 &           ec0_q0*ec0_q1pp) - &
4498 &           ec0_f2*(-6._dp*ec0_aa*ec0_a1*ec0_q1pp + ec0_q0*ec0_q1ppp + &
4499 &           ec0_f2*(3._dp*ec0_q1p*(-2._dp*ec0_aa*ec0_a1*ec0_q1p + ec0_q0*ec0_q1pp) + &
4500 &           ec0_f2*2._dp*ec0_q0*(ec0_q1p**3)*(1._dp + 3._dp*ec0_q1*(1._dp + ec0_q1))))
4501          end if
4502 
4503          mac_q0=-2.0_dp*mac_aa*(1.0_dp+mac_a1*rs)
4504          mac_q1=2.0_dp*mac_aa*(mac_b1*sqr_rs+mac_b2*rs+mac_b3*rs*sqr_rs+mac_b4*rs*rs)
4505          mac_q1p=mac_aa*(mac_b1*rsm1_2+2._dp*mac_b2+3._dp*mac_b3*sqr_rs+4._dp*mac_b4*rs)
4506          mac_den=1.0_dp/(mac_q1*mac_q1+mac_q1)
4507          mac_log=-log( mac_q1*mac_q1*mac_den )
4508          macrs=mac_q0*mac_log
4509          dmacrs_drs= -2.0_dp*mac_aa*mac_a1*mac_log - mac_q0*mac_q1p*mac_den
4510 
4511          zeta=(rho_updn(ipts,1)-rho_updn(ipts,2))*rhotot_inv
4512          ec1_q0=-2.0_dp*ec1_aa*(1.0_dp+ec1_a1*rs)
4513          if(option/=3 .and. option/=4)then
4514            ec1_q1=2.0_dp*ec1_aa*(ec1_b1*sqr_rs+ec1_b2*rs+ec1_b3*rs*sqr_rs+ec1_b4*rs*rs)
4515            ec1_q1p=ec1_aa*(ec1_b1*rsm1_2+2._dp*ec1_b2+3._dp*ec1_b3*sqr_rs+4._dp*ec1_b4*rs)
4516          else
4517            ec1_q1=2.0_dp*ec1_aa*(ec1_b1*sqr_rs+ec1_b2*rs+ec1_b3*rs*sqr_rs+ec1_b4*rs*rs/sqr_sqr_rs)
4518            ec1_q1p=ec1_aa*(ec1_b1*rsm1_2+2._dp*ec1_b2+3._dp*ec1_b3*sqr_rs+3.5_dp*ec1_b4*rs/sqr_sqr_rs)
4519          end if
4520          ec1_den=1.0_dp/(ec1_q1*ec1_q1+ec1_q1)
4521 !        ec1_log=log( 1.0_dp + 1.0_dp / ec1_q1 )
4522          ec1_log=-log( ec1_q1*ec1_q1*ec1_den )
4523          ecrs1=ec1_q0*ec1_log
4524          decrs1_drs= -2.0_dp*ec1_aa*ec1_a1*ec1_log - ec1_q0*ec1_q1p *ec1_den
4525 
4526 !        alpha_zeta is introduced in order to remove singularities for fully
4527 !        polarized systems.
4528          zetp_1_3=(1.0_dp+zeta*alpha_zeta)*zetpm1_3(ipts)**2
4529          zetm_1_3=(1.0_dp-zeta*alpha_zeta)*zetmm1_3(ipts)**2
4530 
4531          f_zeta=( (1.0_dp+zeta*alpha_zeta2)*zetp_1_3 +                      &
4532 &         (1.0_dp-zeta*alpha_zeta2)*zetm_1_3 - 2.0_dp ) * factf_zeta
4533          fp_zeta=( zetp_1_3 - zetm_1_3 ) * factfp_zeta
4534          zeta4=zeta**4
4535 
4536          gcrs=ecrs1-ecrs0+macrs*fsec_inv
4537 !        ecrs=ecrs0+f_zeta*(-macrs*(1.0_dp-zeta4)*fsec_inv+(ecrs1-ecrs0)*zeta4)
4538          ecrs=ecrs0+f_zeta*(zeta4*gcrs-macrs*fsec_inv)
4539 
4540          dgcrs_drs=decrs1_drs-decrs0_drs+dmacrs_drs*fsec_inv
4541 !        decrs_drs=decrs0_drs+f_zeta*&
4542 !        &        (-dmacrs_drs*(1.0_dp-zeta4)*fsec_inv+(decrs1_drs-decrs0_drs)*zeta4)
4543          decrs_drs=decrs0_drs+f_zeta*(zeta4*dgcrs_drs-dmacrs_drs*fsec_inv)
4544          dfzeta4_dzeta=4.0_dp*zeta**3*f_zeta+fp_zeta*zeta4
4545          decrs_dzeta=dfzeta4_dzeta*gcrs-fp_zeta*macrs*fsec_inv
4546 
4547          ec1_q1pp=0.5_dp*ec1_aa*(-ec1_b1*rsm1_2**3+3._dp*ec1_b3*rsm1_2+8._dp*ec1_b4)
4548 
4549          d2ecrs1_drs2= 4.0_dp*ec1_aa*ec1_a1*ec1_q1p*ec1_den            &
4550 &         -ec1_q0*ec1_q1pp*ec1_den                        &
4551 &         +ec1_q0*ec1_q1p**2*ec1_den**2*(2._dp*ec1_q1+1.0_dp)
4552 
4553 
4554          mac_q1pp=0.5_dp*mac_aa*(-mac_b1*rsm1_2**3+3._dp*mac_b3*rsm1_2+8._dp*mac_b4)
4555          d2macrs_drs2= 4.0_dp*mac_aa*mac_a1*mac_q1p*mac_den            &
4556 &         -mac_q0*mac_q1pp*mac_den                        &
4557 &         +mac_q0*mac_q1p**2*mac_den**2*(2._dp*mac_q1+1.0_dp)
4558 
4559          d2gcrs_drs2=d2ecrs1_drs2-d2ecrs0_drs2+d2macrs_drs2*fsec_inv
4560          fpp_zeta=(zetpm1_3(ipts)**2+zetmm1_3(ipts)**2) * factfpp_zeta
4561          d2fzeta4_dzeta2=12.0_dp*zeta**2*f_zeta  &
4562 &         + 8.0_dp*zeta**3*fp_zeta &
4563 &         +       zeta4  *fpp_zeta
4564 
4565          d2ecrs_drs2=d2ecrs0_drs2+&
4566 &         f_zeta*(zeta4*d2gcrs_drs2-d2macrs_drs2*fsec_inv)
4567          d2ecrs_drsdzeta=dfzeta4_dzeta*dgcrs_drs-fp_zeta*dmacrs_drs*fsec_inv
4568          d2ecrs_dzeta2=d2fzeta4_dzeta2*gcrs-fpp_zeta*macrs*fsec_inv
4569 
4570 !        End condition of abs(order)>1
4571 !        Add LSD correlation functional to GGA exchange functional
4572          exci(ipts)=exci(ipts)+ecrs
4573          vxcadd=ecrs-rs*third*decrs_drs-zeta*decrs_dzeta
4574 !        decrs_drup=vxcadd+decrs_dzeta
4575 !        decrs_drdn=vxcadd-decrs_dzeta
4576          vxci(ipts,1)=vxci(ipts,1)+vxcadd+decrs_dzeta
4577          vxci(ipts,2)=vxci(ipts,2)+vxcadd-decrs_dzeta
4578 
4579 
4580 
4581          dvcrs_drs=third*(2._dp*decrs_drs-rs*d2ecrs_drs2)
4582 !        And d(vxc)/d(rho)=(-rs/(3*rho))*d(vxc)/d(rs)
4583          d2ecrs_drho2= -rs**4*(four_pi*third)*third*dvcrs_drs
4584          d2ecrs_drup2=d2ecrs_drho2+&
4585 &         two*(-third*rs*d2ecrs_drsdzeta)*(1._dp-zeta)*rhotot_inv+ &
4586 &         d2ecrs_dzeta2*(1._dp-zeta)**2*rhotot_inv
4587          d2ecrs_drdndrup=d2ecrs_drho2+&
4588 &         2.0_dp*(-third*rs*d2ecrs_drsdzeta)*(-zeta)*rhotot_inv+ &
4589 &         d2ecrs_dzeta2*(1._dp-zeta)*(-1._dp-zeta)*rhotot_inv
4590          d2ecrs_drdn2=d2ecrs_drho2+&
4591 &         2.0_dp*(-third*rs*d2ecrs_drsdzeta)*(-1._dp-zeta)*rhotot_inv+ &
4592 &         d2ecrs_dzeta2*(-1._dp-zeta)**2*rhotot_inv
4593 
4594 
4595 
4596 
4597          if (order==3) then
4598 
4599 
4600 !          fab : INGREDIENTS NEEDED
4601 
4602            a1fa=-third*(threefourth_divpi**(third))*((rhotot)**(-4._dp/3._dp))
4603            a2fa=(1._dp-zeta)/rhotot
4604            b2fa=(-2._dp/3._dp)*((threefourth_divpi)**(third))*((7._dp/3._dp)*(-1._dp+zeta)/((rhotot)**(7._dp/3._dp)))
4605            b1fa=a2fa
4606            c2fa=((1._dp-zeta)**2)*(-3._dp*(1._dp/((rhotot)**2)))
4607            c1fa=((1._dp-zeta)*(1._dp-zeta))/rhotot
4608            e2fa=(2._dp/3._dp)*((threefourth_divpi)**(third))*((1._dp-(7._dp/3._dp)*zeta)/((rhotot)**(7._dp/3._dp)))
4609            e1fa=-zeta/rhotot
4610            f2fa=(2._dp*zeta)*(1._dp/((rhotot)**2))-(3._dp*zeta*zeta)*(1._dp/((rhotot)**2))+1._dp/(((rhotot)**2))
4611            f1fa=(zeta*zeta-1._dp)/rhotot
4612            g1fa=a1fa
4613            g2fa=(-1._dp-zeta)/rhotot
4614            h2fa=(2._dp/3._dp)*((threefourth_divpi)**(third))*((-1._dp-(7._dp/3._dp)*zeta)/((rhotot)**(7._dp/3._dp)))
4615            h1fa=e1fa
4616            i2fa=((-2._dp*zeta)-(3*zeta*zeta)+1)/((rhotot)**2)
4617            i1fa=f1fa
4618            m2fa=(-2._dp/3._dp)*((threefourth_divpi)**(third))*(((7._dp/3._dp)*(zeta+1._dp))/((rhotot)**(7._dp/3._dp)))
4619            m1fa=g2fa
4620            n2fa=(-3._dp*(1._dp+zeta)*(1._dp+zeta))/(rhotot*rhotot)
4621            n1fa=((-1._dp-zeta)**2)/rhotot
4622 
4623 
4624 !          TERMS APPEARING IN THE THIRD ORDER DERIVATIVES
4625 !          terms appearing in the third order derivatives of the spin polarized
4626 !          correlation energy with respect to spin densities
4627 
4628 
4629 !          ec1_q0p=-2.0_dp*ec1_aa*ec1_a1
4630 !          ec1_q1ppp=(3._dp/4._dp)*ec1_aa*(ec1_b1*(rsm1_2**5)-ec1_b3*(rsm1_2**3))
4631 !          This must be erroneous ...
4632 !          d3ecrs1_drs3=(ec1_q1pp*(4._dp*ec1_aa*ec1_a1-ec1_q0p)-ec1_q0*ec1_q1ppp)*ec1_den+ &
4633 !          &           ((-ec1_q1p**2)*(4._dp*ec1_aa*ec1_a1)+ec1_q0*ec1_q1pp*ec1_q1+ec1_q0p*(ec1_q1p**2)+ &
4634 !          &           2._dp*ec1_q0*ec1_q1p*ec1_q1pp)*(ec1_den**2)*(2._dp*ec1_q1+1._dp)- &
4635 !          &           (2._dp*ec1_q0*(ec1_q1p**3)*((2._dp*ec1_q1+1._dp)**2))*(ec1_den**3)+  &
4636 !          &           (2._dp*ec1_q0*(ec1_q1p**3))*(ec1_den**2)
4637 
4638            ec1_q1ppp = 0.75_dp*ec1_aa*(rsm1_2**5)*(ec1_b1-ec1_b3*rs)
4639            ec1_f1 = 1._dp/(ec1_q1*ec1_q1*(1._dp + ec1_q1))
4640            ec1_f2 = 1._dp/(ec1_q1*(1+ec1_q1))
4641            d3ecrs1_drs3 = 6._dp*ec1_q1p*ec1_f1*(-2._dp*ec1_aa*ec1_a1*ec1_q1p + &
4642 &           ec1_q0*ec1_q1pp) - &
4643 &           ec1_f2*(-6._dp*ec1_aa*ec1_a1*ec1_q1pp + ec1_q0*ec1_q1ppp + &
4644 &           ec1_f2*(3._dp*ec1_q1p*(-2._dp*ec1_aa*ec1_a1*ec1_q1p + ec1_q0*ec1_q1pp) + &
4645 &           ec1_f2*2._dp*ec1_q0*(ec1_q1p**3)*(1._dp + 3._dp*ec1_q1*(1._dp + ec1_q1))))
4646 
4647 
4648 !          mac_q0p=-2.0_dp*mac_aa*mac_a1
4649 !          mac_q1ppp=(3._dp/4._dp)*mac_aa*(mac_b1*((rsm1_2)**5)-mac_b3*((rsm1_2)**3))
4650 !          This must be erroneous ...
4651 !          d3macrs_drs3=(mac_q1pp*(4._dp*mac_aa*mac_a1-mac_q0p)-mac_q0*mac_q1ppp)*mac_den+ &
4652 !          &           ((-mac_q1p**2)*(4._dp*mac_aa*mac_a1)+mac_q0*mac_q1pp*mac_q1+mac_q0p*(mac_q1p**2)+ &
4653 !          &           2._dp*mac_q0*mac_q1p*mac_q1pp)*(mac_den**2)*(2._dp*mac_q1+1._dp)- &
4654 !          &           (2._dp*mac_q0*(mac_q1p**3)*((2._dp*mac_q1+1._dp)**2))*(mac_den**3)+  &
4655 !          &           (2._dp*mac_q0*(mac_q1p**3))*(mac_den**2)
4656 
4657            mac_q1ppp = 0.75_dp*mac_aa*(rsm1_2**5)*(mac_b1-mac_b3*rs)
4658            mac_f1 = 1._dp/(mac_q1*mac_q1*(1._dp + mac_q1))
4659            mac_f2 = 1._dp/(mac_q1*(1+mac_q1))
4660            d3macrs_drs3 = 6._dp*mac_q1p*mac_f1*(-2._dp*mac_aa*mac_a1*mac_q1p + &
4661 &           mac_q0*mac_q1pp) - &
4662 &           mac_f2*(-6._dp*mac_aa*mac_a1*mac_q1pp + mac_q0*mac_q1ppp + &
4663 &           mac_f2*(3._dp*mac_q1p*(-2._dp*mac_aa*mac_a1*mac_q1p + mac_q0*mac_q1pp) + &
4664 &           mac_f2*2._dp*mac_q0*(mac_q1p**3)*(1._dp + 3._dp*mac_q1*(1._dp + mac_q1))))
4665 
4666            d3gcrs_drs3=d3ecrs1_drs3-d3ecrs0_drs3+d3macrs_drs3*fsec_inv
4667            d3ecrs_drs3=d3ecrs0_drs3+f_zeta*(zeta4*d3gcrs_drs3-d3macrs_drs3*fsec_inv)
4668            factfppp_zeta=-two*third*factfpp_zeta*alpha_zeta2
4669            fppp_zeta=factfppp_zeta*(((zetpm1_3(ipts))**5)-((zetmm1_3(ipts))**5))
4670 
4671            d3ecrs_dzeta3=(24._dp*zeta*f_zeta+36._dp*(zeta**2)*fp_zeta+  &
4672 &           12._dp*(zeta**3)*fpp_zeta+(zeta**4)*fppp_zeta)*gcrs+   &
4673 &           fppp_zeta*(-macrs)*fsec_inv
4674 
4675            d3ecrs_drs2dzeta=dfzeta4_dzeta*(d2gcrs_drs2)+   &
4676 &           fp_zeta*(-d2macrs_drs2)*fsec_inv
4677 
4678            d3ecrs_dzeta2drs=d2fzeta4_dzeta2*dgcrs_drs+  &
4679 &           fpp_zeta*(-dmacrs_drs)*fsec_inv
4680 
4681 
4682 
4683 
4684 !          ***************** all this part has been commented following the suggestion by xavier
4685 
4686 !          The following is the calculations of only a part of the third order derivatives.
4687 !          the term d3ecrs_drho3 is the part which remains in the
4688 !          non spin polarized limit
4689 !          THIS CODING IS CORRECT, but the alternative one is also correct ...
4690 
4691 !          d3ecrs_drho3=(128._dp*pi*pi/243._dp)*(rs**7)*(decrs_drs)- &
4692 !          &           (48._dp*pi*pi/243._dp)*(rs**8)*d2ecrs_drs2- &
4693 !          &           (16._dp*pi*pi/243._dp)*(rs**9)*d3ecrs_drs3
4694 
4695 !          d3ecrs_drhoupdrho2=d3ecrs_drho3+ &
4696 !          &           a2fa*((-8._dp*pi/27._dp)*(rs**4)*d2ecrs_drsdzeta+ &
4697 !          &           (4._dp*pi/27._dp)*(rs**5)*d3ecrs_drs2dzeta)
4698 
4699 
4700 !          d3ecrs_drhodndrho2=d3ecrs_drho3+ &
4701 !          &           g2fa*((-8._dp*pi/27._dp)*(rs**4)*d2ecrs_drsdzeta+ &
4702 !          &           (4._dp*pi/27._dp)*(rs**5)*d3ecrs_drs2dzeta)
4703 
4704 
4705 !          third order derivatives of the exchange-correlation part
4706 
4707 !          d3ecrs_drup3=d3ecrs_drhoupdrho2+b2fa*d2ecrs_drsdzeta-  &
4708 !          &           (2._dp/3._dp)*rs*b1fa*a1fa*d3ecrs_drs2dzeta- &
4709 !          &           (2._dp/3._dp)*rs*b1fa*b1fa*d3ecrs_dzeta2drs+ &
4710 !          &           c2fa*d2ecrs_dzeta2+c1fa*(a1fa*d3ecrs_dzeta2drs+a2fa*d3ecrs_dzeta3)
4711 
4712 
4713 !          d3ecrs_drup2drdn=d3ecrs_drhoupdrho2+e2fa*d2ecrs_drsdzeta-  &
4714 !          &           (2._dp/3._dp)*rs*e1fa*a1fa*d3ecrs_drs2dzeta- &
4715 !          &           (2._dp/3._dp)*rs*e1fa*b1fa*d3ecrs_dzeta2drs+ &
4716 !          &           f2fa*d2ecrs_dzeta2+f1fa*(a1fa*d3ecrs_dzeta2drs+a2fa*d3ecrs_dzeta3)
4717 
4718 !          d3ecrs_drupdrdn2=d3ecrs_drhodndrho2+h2fa*d2ecrs_drsdzeta-  &
4719 !          &           (2._dp/3._dp)*rs*h1fa*g1fa*d3ecrs_drs2dzeta- &
4720 !          &           (2._dp/3._dp)*rs*h1fa*g2fa*d3ecrs_dzeta2drs+ &
4721 !          &           i2fa*d2ecrs_dzeta2+i1fa*(g1fa*d3ecrs_dzeta2drs+g2fa*d3ecrs_dzeta3)
4722 
4723 
4724 !          d3ecrs_drdn3=d3ecrs_drhodndrho2+m2fa*d2ecrs_drsdzeta-  &
4725 !          &           (2._dp/3._dp)*rs*m1fa*g1fa*d3ecrs_drs2dzeta- &
4726 !          &           (2._dp/3._dp)*rs*m1fa*g2fa*d3ecrs_dzeta2drs+ &
4727 !          &           n2fa*d2ecrs_dzeta2+n1fa*(g1fa*d3ecrs_dzeta2drs+g2fa*d3ecrs_dzeta3)
4728 
4729 
4730 !          ********* suggested by xavier  (now corrected, XG100524)
4731 
4732            sp1_up3=three-three*zeta
4733            sp1_up2dn=one-three*zeta
4734            sp1_updn2=-one-three*zeta
4735            sp1_dn3=-three-three*zeta
4736 
4737            sp2_up3=three-six*zeta+three*zeta*zeta
4738            sp2_up2dn=-one-two*zeta+three*zeta*zeta
4739            sp2_updn2=-one+two*zeta+three*zeta*zeta
4740            sp2_dn3=three+six*zeta+three*zeta*zeta
4741 
4742            sp3_up3=(one-zeta)**3
4743            sp3_up2dn=-one+zeta+zeta**2-zeta**3
4744            sp3_updn2=one+zeta-zeta**2-zeta**3
4745            sp3_dn3=-(one+zeta)**3
4746 
4747            d3ecrs_sp0=(eight*rs*decrs_drs-three*rs*rs*d2ecrs_drs2-rs*rs*rs*d3ecrs_drs3)/(rhotot*rhotot*27.0_dp)
4748            d3ecrs_sp1=(four*rs*d2ecrs_drsdzeta+rs*rs*d3ecrs_drs2dzeta)/(rhotot*rhotot*nine)
4749            d3ecrs_sp2=(-three*d2ecrs_dzeta2-rs*d3ecrs_dzeta2drs)/(rhotot*rhotot*three)
4750            d3ecrs_sp3=d3ecrs_dzeta3/(rhotot*rhotot)
4751 
4752            d3ecrs_drup3=d3ecrs_sp0+d3ecrs_sp1*sp1_up3+d3ecrs_sp2*sp2_up3+d3ecrs_sp3*sp3_up3
4753            d3ecrs_drup2drdn=d3ecrs_sp0+d3ecrs_sp1*sp1_up2dn+d3ecrs_sp2*sp2_up2dn+d3ecrs_sp3*sp3_up2dn
4754            d3ecrs_drupdrdn2=d3ecrs_sp0+d3ecrs_sp1*sp1_updn2+d3ecrs_sp2*sp2_updn2+d3ecrs_sp3*sp3_updn2
4755            d3ecrs_drdn3=d3ecrs_sp0+d3ecrs_sp1*sp1_dn3+d3ecrs_sp2*sp2_dn3+d3ecrs_sp3*sp3_dn3
4756 
4757 !          **** end of the section suggested by xavier...
4758 
4759 
4760 !          fab: this is the end of the if over order==3
4761 
4762 
4763          end if
4764 
4765 !        fab: I think the following lines are wrong..indeed we are in the case option>0, so option cannot be -1
4766 !        I comment them and I put the if only for option 1 and 3
4767 
4768 !        if(option==1 .or. option==-1 .or. option==3)then
4769 !        dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drup2
4770 !        dvxci(ipts,2)=dvxci(ipts,2)+d2ecrs_drdndrup
4771 !        dvxci(ipts,3)=dvxci(ipts,3)+d2ecrs_drdn2
4772 
4773 
4774 !        fab: however, here the thing seems a bit strange...option=3 doesn't seem to be completely implemented
4775 !        (the second derivatives of ec1_q and ec0_q are the derived only in correspondance of the first derivative in the case option !=3 and !=4)
4776 !        so..I think that here the case "or option==3 should be cancelled
4777 
4778          if(option==1 .or. option==3)then
4779            dvxci(ipts,1)=dvxci(ipts,1)+d2ecrs_drup2
4780            dvxci(ipts,2)=dvxci(ipts,2)+d2ecrs_drdndrup
4781            dvxci(ipts,3)=dvxci(ipts,3)+d2ecrs_drdn2
4782 
4783            if(order==3) then
4784 !            third order derivatives of the spin polarized exchange+correlation energy
4785              d2vxci(ipts,1)=d2vxci(ipts,1)+d3ecrs_drup3
4786              d2vxci(ipts,2)=d2vxci(ipts,2)+d3ecrs_drup2drdn
4787              d2vxci(ipts,3)=d2vxci(ipts,3)+d3ecrs_drupdrdn2
4788              d2vxci(ipts,4)=d2vxci(ipts,4)+d3ecrs_drdn3
4789 !            DEBUG
4790 !            wecrsz(ipts,1)=ecrs
4791 !            wecrsz(ipts,1)=ecrs*rhotot
4792 !            wecrsz(ipts,2)=rs
4793 !            wecrsz(ipts,2)=rho_updn(ipts,1)
4794 !            wecrsz(ipts,3)=zeta
4795 !            wecrsz(ipts,3)=rho_updn(ipts,2)
4796 !            wecrsz(ipts,5)=ecrs0
4797 !            wecrsz(ipts,6)=gcrs
4798 !            wecrsz(ipts,7)=macrs
4799 !            wecrsz(ipts,8)=ecrs1
4800 !            d1wecrsz(ipts,1)=decrs_drs
4801 !            d1wecrsz(ipts,1)=decrs_drup
4802 !            d1wecrsz(ipts,2)=decrs_dzeta
4803 !            d1wecrsz(ipts,2)=decrs_drdn
4804 !            d1wecrsz(ipts,5)=decrs0_drs
4805 !            d1wecrsz(ipts,6)=dgcrs_drs
4806 !            d1wecrsz(ipts,7)=dmacrs_drs
4807 !            d1wecrsz(ipts,8)=decrs1_drs
4808 !            d2wecrsz(ipts,1)=d2ecrs_drs2
4809 !            d2wecrsz(ipts,1)=d2ecrs_drup2
4810 !            d2wecrsz(ipts,2)=d2ecrs_drsdzeta
4811 !            d2wecrsz(ipts,2)=d2ecrs_drdndrup
4812 !            d2wecrsz(ipts,3)=d2ecrs_dzeta2
4813 !            d2wecrsz(ipts,3)=d2ecrs_drdn2
4814 !            d2wecrsz(ipts,5)=d2ecrs0_drs2
4815 !            d2wecrsz(ipts,6)=d2gcrs_drs2
4816 !            d2wecrsz(ipts,7)=d2macrs_drs2
4817 !            d2wecrsz(ipts,8)=d2ecrs1_drs2
4818 !            d3wecrsz(ipts,1)=d3ecrs_drs3
4819 !            d3wecrsz(ipts,1)=d3ecrs_drup3
4820 !            d3wecrsz(ipts,2)=d3ecrs_drs2dzeta
4821 !            d3wecrsz(ipts,2)=d3ecrs_drup2drdn
4822 !            d3wecrsz(ipts,3)=d3ecrs_dzeta2drs
4823 !            d3wecrsz(ipts,3)=d3ecrs_drupdrdn2
4824 !            d3wecrsz(ipts,4)=d3ecrs_dzeta3
4825 !            d3wecrsz(ipts,4)=d3ecrs_drdn3
4826 !            d3wecrsz(ipts,5)=d3ecrs0_drs3
4827 !            d3wecrsz(ipts,6)=d3gcrs_drs3
4828 !            d3wecrsz(ipts,7)=d3macrs_drs3
4829 !            d3wecrsz(ipts,8)=d3ecrs1_drs3
4830 !            ENDDEBUG
4831            end if
4832 
4833 
4834          else
4835            dvxci(ipts,9)=d2ecrs_drup2
4836            dvxci(ipts,10)=d2ecrs_drdndrup
4837            dvxci(ipts,11)=d2ecrs_drdn2
4838          end if
4839 
4840 
4841 
4842 
4843 !        -----------------------------------------------------------------------------
4844 !        Eventually add the GGA correlation part of the PBE functional
4845 !        Note : the computation of the potential in the spin-unpolarized
4846 !        case could be optimized much further. Other optimizations are left to do.
4847 
4848          if(option==2 .or. option==5 .or. option==6 .or. option==7)then
4849 !          The definition of phi has been slightly changed, because
4850 !          the original PBE one gives divergent behaviour for fully
4851 !          polarized points
4852 !          zetpm1_3=(1.0_dp+zeta*alpha_zeta)**(-third)
4853 !          zetmm1_3=(1.0_dp-zeta*alpha_zeta)**(-third)
4854            phi_zeta=( zetpm1_3(ipts)*(1.0_dp+zeta*alpha_zeta)+ &
4855 &           zetmm1_3(ipts)*(1.0_dp-zeta*alpha_zeta)   )*0.5_dp
4856            phip_zeta=(zetpm1_3(ipts)-zetmm1_3(ipts))*third*alpha_zeta
4857            phi_zeta_inv=1.0_dp/phi_zeta
4858            phi_logder=phip_zeta*phi_zeta_inv
4859            phi3_zeta=phi_zeta*phi_zeta*phi_zeta
4860            gamphi3inv=gamma_inv*phi_zeta_inv*phi_zeta_inv*phi_zeta_inv
4861            phipp_zeta=-alpha_zeta*alpha_zeta*ninth*&
4862 &           (zetpm1_3(ipts)*zetpm1_3(ipts)*zetpm1_3(ipts)*zetpm1_3(ipts) + &
4863 &           zetmm1_3(ipts)*zetmm1_3(ipts)*zetmm1_3(ipts)*zetmm1_3(ipts)  )
4864 
4865 !          From ec to bb
4866            bb=ecrs*gamphi3inv
4867            dbb_drs=decrs_drs*gamphi3inv
4868            dbb_dzeta=gamphi3inv*(decrs_dzeta-three*ecrs*phi_logder)
4869            d2bb_drs2=d2ecrs_drs2*gamphi3inv
4870            d2bb_drsdzeta=gamphi3inv*(d2ecrs_drsdzeta-three*decrs_drs*phi_logder)
4871            d2bb_dzeta2=gamphi3inv*(d2ecrs_dzeta2-six*decrs_dzeta*phi_logder+&
4872 &           12.0_dp*ecrs*phi_logder*phi_logder-three*ecrs*phi_zeta_inv*phipp_zeta)
4873 
4874 !          From bb to cc
4875            exp_pbe=exp(-bb)
4876            cc=one/(exp_pbe-one)
4877            dcc_dbb=cc*cc*exp_pbe
4878            dcc_drs=dcc_dbb*dbb_drs
4879            dcc_dzeta=dcc_dbb*dbb_dzeta
4880            d2cc_dbb2=cc*cc*exp_pbe*(two*cc*exp_pbe-one)
4881            d2cc_drs2=d2cc_dbb2*dbb_drs*dbb_drs+dcc_dbb*d2bb_drs2
4882            d2cc_drsdzeta=d2cc_dbb2*dbb_drs*dbb_dzeta+dcc_dbb*d2bb_drsdzeta
4883            d2cc_dzeta2=d2cc_dbb2*dbb_dzeta*dbb_dzeta+dcc_dbb*d2bb_dzeta2
4884 
4885 !          From cc to aa
4886            coeff_aa=beta*gamma_inv*phi_zeta_inv*phi_zeta_inv
4887            aa=coeff_aa*cc
4888            daa_drs=coeff_aa*dcc_drs
4889            daa_dzeta=-two*aa*phi_logder+coeff_aa*dcc_dzeta
4890            d2aa_drs2=coeff_aa*d2cc_drs2
4891            d2aa_drsdzeta=-two*daa_drs*phi_logder+coeff_aa*d2cc_drsdzeta
4892            d2aa_dzeta2=aa*(-two*phi_zeta_inv*phipp_zeta+six*phi_logder*phi_logder)+&
4893 &           coeff_aa*(-four*dcc_dzeta*phi_logder+d2cc_dzeta2)
4894 
4895 !          Introduce tt : do not assume that the spin-dependent gradients are collinear
4896            grrho2=grho2_updn(ipts,3)
4897            dtt_dg=two*rhotot_inv*rhotot_inv*rhotmot*coeff_tt
4898 !          Note that tt is (the t variable of PBE divided by phi) squared
4899            tt=half*grrho2*dtt_dg
4900 
4901 !          Get xx from aa and tt
4902            xx=aa*tt
4903            dxx_drs=daa_drs*tt
4904            dxx_dzeta=daa_dzeta*tt
4905            dxx_dtt=aa
4906            d2xx_drs2=d2aa_drs2*tt
4907            d2xx_drsdzeta=d2aa_drsdzeta*tt
4908            d2xx_drsdtt=daa_drs
4909            d2xx_dttdzeta=daa_dzeta
4910            d2xx_dzeta2=d2aa_dzeta2*tt
4911 
4912 !          From xx to pade
4913            pade_den=one/(one+xx*(one+xx))
4914            pade=(one+xx)*pade_den
4915            dpade_dxx=-xx*(two+xx)*pade_den**2
4916            dpade_drs=dpade_dxx*dxx_drs
4917            dpade_dtt=dpade_dxx*dxx_dtt
4918            dpade_dzeta=dpade_dxx*dxx_dzeta
4919            d2pade_dxx2=two*(-one+xx*xx*(three+xx))*pade_den*pade_den*pade_den
4920            d2pade_drs2=d2pade_dxx2*dxx_drs*dxx_drs+dpade_dxx*d2xx_drs2
4921            d2pade_drsdtt=d2pade_dxx2*dxx_drs*dxx_dtt+dpade_dxx*d2xx_drsdtt
4922            d2pade_drsdzeta=d2pade_dxx2*dxx_drs*dxx_dzeta+dpade_dxx*d2xx_drsdzeta
4923            d2pade_dtt2=d2pade_dxx2*dxx_dtt*dxx_dtt
4924            d2pade_dttdzeta=d2pade_dxx2*dxx_dtt*dxx_dzeta+dpade_dxx*d2xx_dttdzeta
4925            d2pade_dzeta2=d2pade_dxx2*dxx_dzeta*dxx_dzeta+dpade_dxx*d2xx_dzeta2
4926 
4927 !          From pade to qq
4928            coeff_qq=tt*phi_zeta_inv*phi_zeta_inv
4929            qq=coeff_qq*pade
4930            dqq_drs=coeff_qq*dpade_drs
4931            dqq_dtt=pade*phi_zeta_inv*phi_zeta_inv+coeff_qq*dpade_dtt
4932            dqq_dzeta=coeff_qq*(dpade_dzeta-two*pade*phi_logder)
4933            d2qq_drs2=coeff_qq*d2pade_drs2
4934            d2qq_drsdtt=phi_zeta_inv*phi_zeta_inv*(dpade_drs+tt*d2pade_drsdtt)
4935            d2qq_drsdzeta=coeff_qq*(d2pade_drsdzeta-two*dpade_drs*phi_logder)
4936            d2qq_dtt2=phi_zeta_inv*phi_zeta_inv*(two*dpade_dtt+tt*d2pade_dtt2)
4937            d2qq_dttdzeta=phi_zeta_inv*phi_zeta_inv*(dpade_dzeta-two*pade*phi_logder)+&
4938 &           coeff_qq*(d2pade_dttdzeta-two*dpade_dtt*phi_logder)
4939            d2qq_dzeta2=coeff_qq*( d2pade_dzeta2-four*dpade_dzeta*phi_logder &
4940 &           +six*pade*phi_logder*phi_logder            &
4941 &           -two*pade*phi_zeta_inv*phipp_zeta)
4942 
4943 !          From qq to rr
4944            arg_rr=one+beta*gamma_inv*qq
4945            div_rr=one/arg_rr
4946            rr=gamma*log(arg_rr)
4947            drr_dqq=beta*div_rr
4948            drr_drs=drr_dqq*dqq_drs
4949            drr_dtt=drr_dqq*dqq_dtt
4950            drr_dzeta=drr_dqq*dqq_dzeta
4951            d2rr_dqq2=-div_rr**2*beta*beta*gamma_inv
4952            d2rr_drs2=d2rr_dqq2*dqq_drs*dqq_drs+drr_dqq*d2qq_drs2
4953            d2rr_drsdtt=d2rr_dqq2*dqq_drs*dqq_dtt+drr_dqq*d2qq_drsdtt
4954            d2rr_drsdzeta=d2rr_dqq2*dqq_drs*dqq_dzeta+drr_dqq*d2qq_drsdzeta
4955            d2rr_dtt2=d2rr_dqq2*dqq_dtt*dqq_dtt+drr_dqq*d2qq_dtt2
4956            d2rr_dttdzeta=d2rr_dqq2*dqq_dtt*dqq_dzeta+drr_dqq*d2qq_dttdzeta
4957            d2rr_dzeta2=d2rr_dqq2*dqq_dzeta*dqq_dzeta+drr_dqq*d2qq_dzeta2
4958 
4959 !          From rr to hh
4960            hh=phi3_zeta*rr
4961            dhh_drs=phi3_zeta*drr_drs
4962            dhh_dtt=phi3_zeta*drr_dtt
4963            dhh_dzeta=phi3_zeta*(drr_dzeta+three*rr*phi_logder)
4964            d2hh_drs2=phi3_zeta*d2rr_drs2
4965            d2hh_drsdtt=phi3_zeta*d2rr_drsdtt
4966            d2hh_drsdzeta=phi3_zeta*(d2rr_drsdzeta+three*drr_drs*phi_logder)
4967            d2hh_dtt2=phi3_zeta*d2rr_dtt2
4968            d2hh_dttdzeta=phi3_zeta*(d2rr_dttdzeta+three*drr_dtt*phi_logder)
4969            d2hh_dzeta2=phi3_zeta*(six*rr*phi_logder*phi_logder+&
4970 &           six*phi_logder*drr_dzeta+d2rr_dzeta2)  &
4971 &           +three*phi_zeta*phi_zeta*rr*phipp_zeta
4972 
4973 !          The GGA correlation energy is added
4974            exci(ipts)=exci(ipts)+hh
4975 
4976 !          Change of variables : from (rs,zeta,tt) to (rhoup,rhodn,grrho)
4977 
4978 
4979 !          From hh to the derivative of the energy wrt the density
4980            drhohh_drho=hh-third*rs*dhh_drs-zeta*dhh_dzeta-seven*third*tt*dhh_dtt
4981            vxci(ipts,1)=vxci(ipts,1)+drhohh_drho+dhh_dzeta
4982            vxci(ipts,2)=vxci(ipts,2)+drhohh_drho-dhh_dzeta
4983 
4984 
4985 !          From hh to the derivative of the energy wrt to the gradient of the
4986 !          density, divided by the gradient of the density
4987 !          (The v3.3 definition includes the division by the norm of the gradient)
4988            dvxcdgr(ipts,3)=rhotot*dtt_dg*dhh_dtt
4989 
4990            d2rhohh_drho2=rhotot_inv*&
4991 &           (-two*ninth*rs*dhh_drs +seven*four*ninth*tt*dhh_dtt &
4992 &           +ninth*rs*rs*d2hh_drs2+zeta*zeta*d2hh_dzeta2+(seven*third*tt)**2*d2hh_dtt2 &
4993 &           +two*third*rs*zeta*d2hh_drsdzeta+two*seven*ninth*rs*tt*d2hh_drsdtt &
4994 &           +two*seven*third*tt*zeta*d2hh_dttdzeta)
4995            d2rhohh_drhodg=dtt_dg*(-four*third*dhh_dtt-third*rs*d2hh_drsdtt &
4996 &           -zeta*d2hh_dttdzeta-seven*third*tt*d2hh_dtt2)
4997 
4998 !          Component 12 : first derivative with respect to the gradient
4999 !          of the density, div by the grad of the density
5000            dvxci(ipts,12)=dvxcdgr(ipts,3)
5001 !          Components 9, 10 and 11 : second derivatives with respect to the spin-density
5002 !          Note that there is already a contribution from LSDA
5003            dvxci(ipts,9)=dvxci(ipts,9)+d2rhohh_drho2+rhotot_inv*           &
5004 &           ( d2hh_dzeta2*(one-two*zeta) &
5005 &           -two*third*rs*d2hh_drsdzeta-14.0_dp*third*tt*d2hh_dttdzeta)
5006            dvxci(ipts,10)=dvxci(ipts,10)+d2rhohh_drho2-rhotot_inv*d2hh_dzeta2
5007            dvxci(ipts,11)=dvxci(ipts,11)+d2rhohh_drho2+rhotot_inv*           &
5008 &           ( d2hh_dzeta2*(one+two*zeta) &
5009 &           +two*third*rs*d2hh_drsdzeta+14.0_dp*third*tt*d2hh_dttdzeta)
5010 !          Components 13 and 14 : second derivatives with respect to spin density
5011 !          and gradient, divided by the gradient
5012            dvxci(ipts,13)=d2rhohh_drhodg+dtt_dg*d2hh_dttdzeta
5013            dvxci(ipts,14)=d2rhohh_drhodg-dtt_dg*d2hh_dttdzeta
5014 !          Component 15 : derivative of the (derivative wrt the gradient div by the grad),
5015 !          divided by the grad
5016            dvxci(ipts,15)=rhotot*d2hh_dtt2*dtt_dg*dtt_dg
5017 
5018 !          End condition of GGA
5019          end if
5020 
5021        else  ! no correlation
5022 
5023          if(ndvxci > 8)then
5024 !          Must zero the correlation part of the xc kernel
5025            dvxci(:,9:15)=zero
5026          end if
5027 
5028 !        End condition of including correlation, and not only exchange
5029        end if
5030 
5031 !      Correlation has been added
5032 !      -----------------------------------------------------------------------------
5033 
5034      end do
5035    end if
5036 
5037 !  fab: this should be the else on nspden
5038  else
5039 !  Disallowed value for nspden
5040    write(message, '(a,a,a,i12,a)' )&
5041 &   '  Argument nspden must be 1 or 2; ',ch10,&
5042 &   '  Value provided as argument was ',nspden,'.'
5043    MSG_BUG(message)
5044  end if
5045 
5046 !DEBUG
5047 !Finite-difference debugging, do not take away
5048 !if(debug/=0)then
5049 !do ipts=1,5,5
5050 
5051 !rho=rho_updn(ipts,1)+rho_updn(ipts,2)
5052 !write(std_out,'(a,i5,a,2es16.8)' ) ' Point number',ipts,' with rhoup,rhodn=',rho_updn(ipts,1),rho_updn(ipts,2)
5053 !write(std_out,'(a,i5,a,2es16.8)' ) ' Point number',ipts+1,' with rhoup,rhodn=',rho_updn(ipts+1,1),rho_updn(ipts+1,2)
5054 !write(std_out,'(a,i5,a,2es16.8)' ) ' Point number',ipts+2,' with rhoup,rhodn=',rho_updn(ipts+2,1),rho_updn(ipts+2,2)
5055 !write(std_out,'(a,i5,a,2es16.8)' ) ' Point number',ipts+3,' with rhoup,rhodn=',rho_updn(ipts+3,1),rho_updn(ipts+3,2)
5056 !write(std_out,'(a,i5,a,2es16.8)' ) ' Point number',ipts+4,' with rhoup,rhodn=',rho_updn(ipts+4,1),rho_updn(ipts+4,2)
5057 
5058 !! For rho
5059 !if(debug==1)then
5060 !write(std_out,'(a)' )' Direct values :'
5061 !write(std_out,'(3es16.8)' )exci(ipts)*rho,vxci(ipts,1),vxci(ipts,2)
5062 !else
5063 !!  For grho2
5064 !write(std_out,'(4es16.8)' )exci(ipts)*rho,dvxcdgr(ipts,1),&
5065 !&  dvxcdgr(ipts,2),dvxcdgr(ipts,3)
5066 !end if
5067 
5068 !write(std_out,'(4es16.8)' )dvxci(ipts,1:3)  ! For LDA
5069 !write(std_out,'(a)' )'     3rd-order '
5070 !write(std_out,'(4es16.8)' )d2vxci(ipts,1:4)  ! For LDA
5071 
5072 !write(std_out,'(4es16.8)' )dvxci(ipts,1:4)  ! For exchange
5073 !write(std_out,'(4es16.8)' )dvxci(ipts,5:8)  ! For exchange
5074 !write(std_out,'(4es16.8)' )dvxci(ipts,9:12) ! For correlation
5075 !write(std_out,'(4es16.8)' )dvxci(ipts,13:15) ! For correlation
5076 
5077 !if(debug==1)then
5078 !!  For rho
5079 !write(std_out,'(a)' )' Finite-difference values :'
5080 !write(std_out,'(3es16.8)' )exci(ipts)*rho,&
5081 !&      ( exci(ipts+1)*(rho+delta) - exci(ipts+2)*(rho-delta) )/2._dp/delta,&
5082 !&      ( exci(ipts+3)*(rho+delta) - exci(ipts+4)*(rho-delta) )/2._dp/delta
5083 !write(std_out,'(3es16.8)' )&
5084 !&    ( vxci(ipts+1,1) - vxci(ipts+2,1) )/2._dp/delta,&
5085 !&    ( vxci(ipts+3,1) - vxci(ipts+4,1) )/2._dp/delta,&
5086 !&    ( vxci(ipts+3,2) - vxci(ipts+4,2) )/2._dp/delta
5087 !!This is for order 3
5088 !write(std_out,'(a)' )'     3rd-order by two methods, giving components 1, 2, 3, then on the next line 2, 3, 4 '
5089 !write(std_out,'(3es16.8)' )&
5090 !&    ( dvxci(ipts+1,1) - dvxci(ipts+2,1) )/2._dp/delta,&
5091 !&    ( dvxci(ipts+1,2) - dvxci(ipts+2,2) )/2._dp/delta,&
5092 !&    ( dvxci(ipts+1,3) - dvxci(ipts+2,3) )/2._dp/delta
5093 !write(std_out,'(3es16.8)' )&
5094 !&    ( dvxci(ipts+3,1) - dvxci(ipts+4,1) )/2._dp/delta,&
5095 !&    ( dvxci(ipts+3,2) - dvxci(ipts+4,2) )/2._dp/delta,&
5096 !&    ( dvxci(ipts+3,3) - dvxci(ipts+4,3) )/2._dp/delta
5097 
5098 !write(std_out,*)
5099 !write(std_out,*)' Now for ecrs and derivatives '
5100 !write(std_out,'(a)' )' ecrs, rs, zeta ='
5101 !write(std_out,'(3es16.8)' )wecrsz(ipts,1:3)
5102 !write(std_out,'(3es16.8)' )wecrsz(ipts+1,1:3)
5103 !write(std_out,'(3es16.8)' )wecrsz(ipts+2,1:3)
5104 !write(std_out,'(3es16.8)' )wecrsz(ipts+3,1:3)
5105 !write(std_out,'(3es16.8)' )wecrsz(ipts+4,1:3)
5106 !write(std_out,'(a)' )' ecrs derivatives :'
5107 !write(std_out,'(3es16.8)' )d1wecrsz(ipts,1:2)
5108 !write(std_out,'(3es16.8)' )d2wecrsz(ipts,1:3)
5109 !write(std_out,'(4es16.8)' )d3wecrsz(ipts,1:4)
5110 !write(std_out,'(a)' )' Finite-differences :'
5111 !write(std_out,'(3es16.8)' )&
5112 !&    ( wecrsz(ipts+1,1) - wecrsz(ipts+2,1) )/( wecrsz(ipts+1,2) - wecrsz(ipts+2,2) ),&
5113 !&    ( wecrsz(ipts+3,1) - wecrsz(ipts+4,1) )/( wecrsz(ipts+3,3) - wecrsz(ipts+4,3) )
5114 !write(std_out,'(3es16.8)' )&
5115 !&    ( d1wecrsz(ipts+1,1) - d1wecrsz(ipts+2,1) )/( wecrsz(ipts+1,2) - wecrsz(ipts+2,2) ),&
5116 !&    ( d1wecrsz(ipts+1,2) - d1wecrsz(ipts+2,2) )/( wecrsz(ipts+1,2) - wecrsz(ipts+2,2) )
5117 !write(std_out,'(3es16.8)' )&
5118 !&    ( d1wecrsz(ipts+3,1) - d1wecrsz(ipts+4,1) )/( wecrsz(ipts+3,3) - wecrsz(ipts+4,3) ),&
5119 !&    ( d1wecrsz(ipts+3,2) - d1wecrsz(ipts+4,2) )/( wecrsz(ipts+3,3) - wecrsz(ipts+4,3) )
5120 !write(std_out,'(a)' )' Finite-differences, 3rd order :'
5121 !write(std_out,'(3es16.8)' )&
5122 !&    ( d2wecrsz(ipts+1,1) - d2wecrsz(ipts+2,1) )/( wecrsz(ipts+1,2) - wecrsz(ipts+2,2) ),&
5123 !&    ( d2wecrsz(ipts+1,2) - d2wecrsz(ipts+2,2) )/( wecrsz(ipts+1,2) - wecrsz(ipts+2,2) ),&
5124 !&    ( d2wecrsz(ipts+1,3) - d2wecrsz(ipts+2,3) )/( wecrsz(ipts+1,2) - wecrsz(ipts+2,2) )
5125 !write(std_out,'(3es16.8)' )&
5126 !&    ( d2wecrsz(ipts+3,1) - d2wecrsz(ipts+4,1) )/( wecrsz(ipts+3,3) - wecrsz(ipts+4,3) ),&
5127 !&    ( d2wecrsz(ipts+3,2) - d2wecrsz(ipts+4,2) )/( wecrsz(ipts+3,3) - wecrsz(ipts+4,3) ),&
5128 !&    ( d2wecrsz(ipts+3,3) - d2wecrsz(ipts+4,3) )/( wecrsz(ipts+3,3) - wecrsz(ipts+4,3) )
5129 
5130 
5131 !!This is for GGA
5132 
5133 !write(std_out,'(4es16.8)' )&
5134 !&    ( dvxcdgr(ipts+1,1) - dvxcdgr(ipts+2,1) )/2._dp/delta,&
5135 !&    ( dvxcdgr(ipts+3,2) - dvxcdgr(ipts+4,2) )/2._dp/delta,&
5136 !&    ( dvxcdgr(ipts+1,3) - dvxcdgr(ipts+2,3) )/2._dp/delta,&
5137 !&    ( dvxcdgr(ipts+3,3) - dvxcdgr(ipts+4,3) )/2._dp/delta
5138 !else
5139 !!  For grho2  (should distinguish exchange and correlation ...)
5140 !grr=sqrt(grho2_updn(ipts,1)) ! Analysis of exchange
5141 !grr=sqrt(grho2_updn(ipts,3)) ! Analysis of correlation
5142 !write(std_out,'(3es16.8)' )exci(ipts)*rho,&
5143 !&      ( exci(ipts+1)*rho - exci(ipts+2)*rho )/2._dp/delta/grr,&
5144 !&      ( exci(ipts+3)*rho - exci(ipts+4)*rho )/2._dp/delta/grr
5145 !write(std_out,'(3es16.8)' )&
5146 !&    ( vxci(ipts+1,1) - vxci(ipts+2,1) )/2._dp/delta/grr,&
5147 !&    ( vxci(ipts+3,1) - vxci(ipts+4,1) )/2._dp/delta/grr,&
5148 !&    ( vxci(ipts+3,2) - vxci(ipts+4,2) )/2._dp/delta/grr
5149 !write(std_out,'(4es16.8)' )&
5150 !&    ( dvxcdgr(ipts+1,1) - dvxcdgr(ipts+2,1) )/2._dp/delta/grr,&
5151 !&    ( dvxcdgr(ipts+3,2) - dvxcdgr(ipts+4,2) )/2._dp/delta/grr,&
5152 !&    ( dvxcdgr(ipts+1,3) - dvxcdgr(ipts+2,3) )/2._dp/delta/grr,&
5153 !&    ( dvxcdgr(ipts+3,3) - dvxcdgr(ipts+4,3) )/2._dp/delta/grr
5154 !end if
5155 !end do
5156 !stop
5157 !end if
5158 !ENDDEBUG
5159 
5160  ABI_DEALLOCATE(rhoarr)
5161  ABI_DEALLOCATE(rhom1_3)
5162  ABI_DEALLOCATE(rho_updnm1_3)
5163  ABI_DEALLOCATE(zetm)
5164  ABI_DEALLOCATE(zetmm1_3)
5165  ABI_DEALLOCATE(zetp)
5166  ABI_DEALLOCATE(zetpm1_3)
5167 
5168 !DEBUG
5169 !deallocate(wecrsz,d1wecrsz,d2wecrsz,d3wecrsz)
5170 !ENDDEBUG
5171 
5172 !DEBUG
5173 !write(std_out,*)' xcpbe : exit'
5174 !write(std_out,*)' nspden=',nspden
5175 !if(order==2)stop
5176 !ENDDEBUG
5177 
5178 end subroutine xcpbe