TABLE OF CONTENTS


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).
 If option==1, Reduces to Perdew-Wang LSD , PRB45,13244 (1992).
 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).
 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)
 If option==6, RPBE functional of Hammer, Hansen and Norskov, PRB 59, 7413 (1999)
 If option==7, WC functional of Wu and Cohen, PRB 73, 235116 (2006)

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 .
 For the initials of contributors, see ~abinit/doc/developers/contributors.txt .

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.

NOTES

PARENTS

      drivexc

CHILDREN

      invcb

SOURCE

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