/PP/projwfc.f90

http://github.com/NNemec/quantum-espresso · Fortran Modern · 3396 lines · 2435 code · 88 blank · 873 comment · 76 complexity · c6d124e28c2f01f15117431d03835acc MD5 · raw file

  1. !
  2. ! Copyright (C) 2001-2009 Quantum ESPRESSO group
  3. ! This file is distributed under the terms of the
  4. ! GNU General Public License. See the file `License'
  5. ! in the root directory of the present distribution,
  6. ! or http://www.gnu.org/copyleft/gpl.txt .
  7. !
  8. !-----------------------------------------------------------------------
  9. PROGRAM projwfc
  10. !-----------------------------------------------------------------------
  11. !
  12. ! projects wavefunctions onto orthogonalized atomic wavefunctions,
  13. ! calculates Lowdin charges, spilling parameter, projected DOS
  14. ! or computes the LDOS in a volume given in input as function of energy
  15. !
  16. !
  17. ! Input (namelist &inputpp ... / ): Default value
  18. !
  19. ! prefix prefix of input file produced by pw.x 'pwscf'
  20. ! (wavefunctions are needed)
  21. ! outdir directory containing the input file ./
  22. ! ngauss type of gaussian broadening (optional) 0
  23. ! = 0 Simple Gaussian (default)
  24. ! = 1 Methfessel-Paxton of order 1
  25. ! = -1 Marzari-Vanderbilt "cold smearing"
  26. ! =-99 Fermi-Dirac function
  27. ! degauss gaussian broadening, Ry (not eV!) 0.0
  28. ! Emin, Emax min, max energy (eV) for DOS plot band extrema
  29. ! DeltaE energy grid step (eV) none
  30. ! lsym if true the projections are symmetrized .true.
  31. ! filproj file containing the projections none
  32. ! filpdos prefix for output files containing PDOS(E) prefix
  33. ! lgww if .true. take energies from previous GWW calculation
  34. ! (file bands.dat)
  35. ! kresolveddos if .true. the DOS is written as function .false.
  36. ! of the k-point (not summed over all of them)
  37. ! all k-points but results
  38. ! tdosinboxes if .true., the local DOS in specified .false.
  39. ! volumes (boxes) is computed
  40. ! n_proj_boxes number of volumes for the local DOS 0
  41. ! irmin, irmax first and last point of the FFT grid 1, 0
  42. ! included in the volume
  43. ! plotboxes if .true., the volumes are given in output .false.
  44. !
  45. !
  46. ! Output:
  47. !
  48. ! Projections are written to standard output,
  49. ! and also to file filproj if given as input.
  50. ! The total DOS and the sum of projected DOS are written to file
  51. ! "filpdos".pdos_tot.
  52. ! The format for the collinear, spin-unpolarized case and the
  53. ! non-collinear, spin-orbit case is
  54. ! E DOS(E) PDOS(E)
  55. ! The format for the collinear, spin-polarized case is
  56. ! E DOSup(E) DOSdw(E) PDOSup(E) PDOSdw(E)
  57. ! The format for the non-collinear, non spin-orbit case is
  58. ! E DOS(E) PDOSup(E) PDOSdw(E)
  59. !
  60. ! In the collinear case and the non-collinear, non spin-orbit case
  61. ! projected DOS are written to file "filpdos".pdos_atm#N(X)_wfc#M(l),
  62. ! where N = atom number , X = atom symbol, M = wfc number, l=s,p,d,f
  63. ! (one file per atomic wavefunction found in the pseudopotential file)
  64. ! - The format for the collinear, spin-unpolarized case is
  65. ! E LDOS(E) PDOS_1(E) ... PDOS_2l+1(E)
  66. ! where LDOS = \sum m=1,2l+1 PDOS_m(E)
  67. ! and PDOS_m(E) = projected DOS on atomic wfc with component m
  68. ! - The format for the collinear, spin-polarized case and the
  69. ! non-collinear, non spin-orbit case is as above with
  70. ! two components for both LDOS(E) and PDOS_m(E)
  71. !
  72. ! In the non-collinear, spin-orbit case (i.e. if there is at least one
  73. ! fully relativistic pseudopotential) wavefunctions are projected
  74. ! onto eigenstates of the total angular-momentum.
  75. ! Projected DOS are written to file "filpdos".pdos_atm#N(X)_wfc#M(l_j),
  76. ! where N = atom number , X = atom symbol, M = wfc number, l=s,p,d,f
  77. ! and j is the value of the total angular momentum.
  78. ! In this case the format is
  79. ! E LDOS(E) PDOS_1(E) ... PDOS_2j+1(E)
  80. !
  81. ! All DOS(E) are in states/eV plotted vs E in eV
  82. !
  83. ! If the kresolveddos option is used, the k-point index is prepended
  84. ! to the formats above, e.g. (collinear, spin-unpolarized case)
  85. ! ik E DOS(E) PDOS(E)
  86. !
  87. ! If the local DOS(E) is computed (tdosinboxes),
  88. ! projections are written to file "filproj" if given as input.
  89. ! Volumes are written as xsf files with 3D datagrids, valued 1.0
  90. ! inside the box volume and 0 outside, named filpdos.box#n.xsf
  91. ! The local DOS(E) is written to file "filpdos".ldos_boxes, with format
  92. ! E totDOS(E) SumLDOS(E) LDOS_1(E) ... LDOS_n(E)
  93. !
  94. ! Order of m-components for each l in the output:
  95. !
  96. ! 1, cos(phi), sin(phi), cos(2*phi), sin(2*phi), .., cos(l*phi), sin(l*phi)
  97. !
  98. ! where phi is the polar angle:x=r cos(theta)cos(phi), y=r cos(theta)sin(phi)
  99. ! This is determined in file flib/ylmr2.f90 that calculates spherical harm.
  100. ! L=1 :
  101. ! 1 pz (m=0)
  102. ! 2 px (real combination of m=+/-1 with cosine)
  103. ! 3 py (real combination of m=+/-1 with sine)
  104. ! L=2 :
  105. ! 1 dz2 (m=0)
  106. ! 2 dzx (real combination of m=+/-1 with cosine)
  107. ! 3 dzy (real combination of m=+/-1 with sine)
  108. ! 4 dx2-y2 (real combination of m=+/-2 with cosine)
  109. ! 5 dxy (real combination of m=+/-1 with sine)
  110. !
  111. ! Important notice:
  112. !
  113. ! The tetrahedron method is presently not implemented.
  114. ! Gaussian broadening is used in all cases:
  115. ! - if degauss is set to some value in namelist &inputpp, that value
  116. ! (and the optional value for ngauss) is used
  117. ! - if degauss is NOT set to any value in namelist &inputpp, the
  118. ! value of degauss and of ngauss are read from the input data
  119. ! file (they will be the same used in the pw.x calculations)
  120. ! - if degauss is NOT set to any value in namelist &inputpp, AND
  121. ! there is no value of degauss and of ngauss in the input data
  122. ! file, degauss=DeltaE (in Ry) and ngauss=0 will be used
  123. ! Obsolete variables, ignored:
  124. ! io_choice
  125. ! smoothing
  126. !
  127. USE io_global, ONLY : stdout, ionode, ionode_id
  128. USE constants, ONLY : rytoev
  129. USE kinds, ONLY : DP
  130. USE klist, ONLY : degauss, ngauss, lgauss
  131. USE io_files, ONLY : nd_nmbr, prefix, tmp_dir, trimcheck
  132. USE noncollin_module, ONLY : noncolin
  133. USE mp, ONLY : mp_bcast
  134. USE mp_global, ONLY : mp_startup, nproc_ortho
  135. USE environment, ONLY : environment_start
  136. !
  137. ! for GWW
  138. USE io_files, ONLY : find_free_unit
  139. !
  140. !
  141. IMPLICIT NONE
  142. CHARACTER (len=256) :: filpdos, filproj, io_choice, outdir
  143. REAL (DP) :: Emin, Emax, DeltaE, degauss1, smoothing
  144. INTEGER :: ngauss1, ios
  145. LOGICAL :: lsym, kresolveddos, tdosinboxes, plotboxes
  146. INTEGER, PARAMETER :: N_MAX_BOXES = 999
  147. INTEGER :: n_proj_boxes, irmin(3,N_MAX_BOXES), irmax(3,N_MAX_BOXES)
  148. !
  149. ! for GWW
  150. INTEGER :: iun, idum
  151. REAL(DP) :: rdum1,rdum2,rdum3
  152. LOGICAL :: lex, lgww
  153. !
  154. !
  155. NAMELIST / inputpp / outdir, prefix, ngauss, degauss, lsym, &
  156. Emin, Emax, DeltaE, io_choice, smoothing, filpdos, filproj, &
  157. lgww, & !if .true. use GW QP energies from file bands.dat
  158. kresolveddos, tdosinboxes, n_proj_boxes, irmin, irmax, plotboxes
  159. !
  160. ! initialise environment
  161. !
  162. #ifdef __PARA
  163. CALL mp_startup ( )
  164. #endif
  165. CALL environment_start ( 'PROJWFC' )
  166. !
  167. ! set default values for variables in namelist
  168. !
  169. prefix = 'pwscf'
  170. CALL get_env( 'ESPRESSO_TMPDIR', outdir )
  171. IF ( trim( outdir ) == ' ' ) outdir = './'
  172. filproj= ' '
  173. filpdos= ' '
  174. Emin =-1000000.d0
  175. Emax =+1000000.d0
  176. DeltaE = 0.01d0
  177. ngauss = 0
  178. lsym = .true.
  179. degauss= 0.d0
  180. lgww = .false.
  181. kresolveddos = .false.
  182. tdosinboxes = .false.
  183. plotboxes = .false.
  184. n_proj_boxes= 1
  185. irmin(:,:) = 1
  186. irmax(:,:) = 0
  187. !
  188. ios = 0
  189. !
  190. IF ( ionode ) THEN
  191. !
  192. CALL input_from_file ( )
  193. !
  194. READ (5, inputpp, iostat = ios)
  195. !
  196. tmp_dir = trimcheck (outdir)
  197. ! save the value of degauss and ngauss: they are read from file
  198. degauss1=degauss
  199. ngauss1 = ngauss
  200. !
  201. ENDIF
  202. !
  203. CALL mp_bcast (ios, ionode_id )
  204. !
  205. IF (ios /= 0) CALL errore ('projwfc', 'reading inputpp namelist', abs (ios) )
  206. !
  207. ! ... Broadcast variables
  208. !
  209. CALL mp_bcast( tmp_dir, ionode_id )
  210. CALL mp_bcast( prefix, ionode_id )
  211. CALL mp_bcast( filproj, ionode_id )
  212. CALL mp_bcast( ngauss1, ionode_id )
  213. CALL mp_bcast( degauss1,ionode_id )
  214. CALL mp_bcast( DeltaE, ionode_id )
  215. CALL mp_bcast( lsym, ionode_id )
  216. CALL mp_bcast( Emin, ionode_id )
  217. CALL mp_bcast( Emax, ionode_id )
  218. ! for GWW
  219. CALL mp_bcast( lgww, ionode_id )
  220. ! for projection on boxes
  221. CALL mp_bcast( tdosinboxes, ionode_id )
  222. CALL mp_bcast( n_proj_boxes, ionode_id )
  223. CALL mp_bcast( irmin, ionode_id )
  224. CALL mp_bcast( irmax, ionode_id )
  225. !
  226. ! Now allocate space for pwscf variables, read and check them.
  227. !
  228. CALL read_file ( )
  229. !
  230. CALL openfil_pp ( )
  231. !
  232. ! decide Gaussian broadening
  233. !
  234. IF (degauss1/=0.d0) THEN
  235. degauss=degauss1
  236. ngauss =ngauss1
  237. WRITE( stdout,'(/5x,"Gaussian broadening (read from input): ",&
  238. & "ngauss,degauss=",i4,f12.6/)') ngauss,degauss
  239. lgauss=.true.
  240. ELSEIF (lgauss) THEN
  241. WRITE( stdout,'(/5x,"Gaussian broadening (read from file): ",&
  242. & "ngauss,degauss=",i4,f12.6/)') ngauss,degauss
  243. ELSE
  244. degauss=DeltaE/rytoev
  245. ngauss =0
  246. WRITE( stdout,'(/5x,"Gaussian broadening (default values): ",&
  247. & "ngauss,degauss=",i4,f12.6/)') ngauss,degauss
  248. lgauss=.true.
  249. ENDIF
  250. !
  251. IF ( filpdos == ' ') filpdos = prefix
  252. !
  253. IF ( tdosinboxes ) THEN
  254. IF( nproc_ortho > 1 ) THEN
  255. CALL errore ('projwfc', 'nproc_ortho > 1 not yet implemented', 1)
  256. ELSE
  257. CALL projwave_boxes (filpdos, filproj, n_proj_boxes, irmin, irmax, plotboxes)
  258. ENDIF
  259. ELSE
  260. IF (noncolin) THEN
  261. CALL projwave_nc(filproj, lsym )
  262. ELSE
  263. IF( nproc_ortho > 1 ) THEN
  264. CALL pprojwave (filproj, lsym)
  265. ELSE
  266. CALL projwave (filproj, lsym, lgww)
  267. ENDIF
  268. ENDIF
  269. ENDIF
  270. !
  271. IF ( ionode ) THEN
  272. IF ( tdosinboxes ) THEN
  273. CALL partialdos_boxes (Emin, Emax, DeltaE, kresolveddos, filpdos, n_proj_boxes)
  274. ELSE
  275. IF ( lsym ) THEN
  276. !
  277. IF (noncolin) THEN
  278. CALL partialdos_nc (Emin, Emax, DeltaE, kresolveddos, filpdos)
  279. ELSE
  280. CALL partialdos (Emin, Emax, DeltaE, kresolveddos, filpdos)
  281. ENDIF
  282. !
  283. ENDIF
  284. ENDIF
  285. ENDIF
  286. !
  287. CALL stop_pp
  288. !
  289. END PROGRAM projwfc
  290. !
  291. MODULE projections
  292. USE kinds, ONLY : DP
  293. TYPE wfc_label
  294. INTEGER na, n, l, m
  295. END TYPE wfc_label
  296. TYPE(wfc_label), ALLOCATABLE :: nlmchi(:)
  297. REAL (DP), ALLOCATABLE :: proj (:,:,:)
  298. COMPLEX (DP), ALLOCATABLE :: proj_aux (:,:,:)
  299. END MODULE projections
  300. !
  301. MODULE projections_nc
  302. USE kinds, ONLY : DP
  303. TYPE wfc_label_nc
  304. INTEGER na, n, l, m, ind
  305. REAL (DP) jj
  306. END TYPE wfc_label_nc
  307. TYPE(wfc_label_nc), ALLOCATABLE :: nlmchi(:)
  308. REAL (DP), ALLOCATABLE :: proj (:,:,:)
  309. COMPLEX (DP), ALLOCATABLE :: proj_aux (:,:,:)
  310. END MODULE projections_nc
  311. !
  312. MODULE projections_ldos
  313. USE kinds, ONLY : DP
  314. REAL (DP), ALLOCATABLE :: proj (:,:,:)
  315. END MODULE projections_ldos
  316. !
  317. !-----------------------------------------------------------------------
  318. SUBROUTINE projwave( filproj, lsym, lgww )
  319. !-----------------------------------------------------------------------
  320. !
  321. USE io_global, ONLY : stdout, ionode
  322. USE printout_base, ONLY: title
  323. USE ions_base, ONLY : zv, tau, nat, ntyp => nsp, ityp, atm
  324. USE basis, ONLY : natomwfc
  325. USE cell_base
  326. USE constants, ONLY: rytoev, eps4
  327. USE gvect
  328. USE grid_dimensions, ONLY : nr1, nr2, nr3, nr1x, nr2x, nr3x
  329. USE klist, ONLY: xk, nks, nkstot, nelec
  330. USE ldaU
  331. USE lsda_mod, ONLY: nspin, isk, current_spin
  332. USE symm_base, ONLY: nsym, irt, d1, d2, d3
  333. USE wvfct
  334. USE control_flags, ONLY: gamma_only
  335. USE uspp, ONLY: nkb, vkb
  336. USE uspp_param, ONLY: upf
  337. USE becmod, ONLY: bec_type, becp, calbec, allocate_bec_type, deallocate_bec_type
  338. USE io_files, ONLY: nd_nmbr, prefix, tmp_dir, nwordwfc, iunwfc, find_free_unit
  339. USE spin_orb, ONLY: lspinorb
  340. USE wavefunctions_module, ONLY: evc
  341. !
  342. USE projections
  343. !
  344. IMPLICIT NONE
  345. !
  346. CHARACTER (len=*) :: filproj
  347. INTEGER :: ik, ibnd, i, j, k, na, nb, nt, isym, n, m, m1, l, nwfc,&
  348. nwfc1, lmax_wfc, is, ios, iunproj
  349. REAL(DP), ALLOCATABLE :: e (:)
  350. COMPLEX(DP), ALLOCATABLE :: wfcatom (:,:)
  351. COMPLEX(DP), ALLOCATABLE :: overlap(:,:), work(:,:),work1(:), proj0(:,:)
  352. ! Some workspace for k-point calculation ...
  353. REAL (DP), ALLOCATABLE ::roverlap(:,:), rwork1(:),rproj0(:,:)
  354. ! ... or for gamma-point.
  355. REAL(DP), ALLOCATABLE :: charges(:,:,:), proj1 (:)
  356. REAL(DP) :: psum, totcharge(2)
  357. INTEGER :: nksinit, nkslast
  358. CHARACTER(len=256) :: filename
  359. CHARACTER (len=1) :: l_label(0:3)=(/'s','p','d','f'/)
  360. INTEGER, ALLOCATABLE :: idx(:)
  361. LOGICAL :: lsym
  362. !
  363. !
  364. ! for GWW
  365. INTEGER :: iun, idum
  366. REAL(DP) :: rdum1,rdum2,rdum3
  367. LOGICAL :: lex, lgww
  368. !
  369. !
  370. WRITE( stdout, '(/5x,"Calling projwave .... ")')
  371. IF ( gamma_only ) THEN
  372. WRITE( stdout, '(5x,"gamma-point specific algorithms are used")')
  373. ENDIF
  374. !
  375. ! initialize D_Sl for l=1, l=2 and l=3, for l=0 D_S0 is 1
  376. !
  377. CALL d_matrix (d1, d2, d3)
  378. !
  379. ! fill structure nlmchi
  380. !
  381. ALLOCATE (nlmchi(natomwfc))
  382. nwfc=0
  383. lmax_wfc = 0
  384. DO na = 1, nat
  385. nt = ityp (na)
  386. DO n = 1, upf(nt)%nwfc
  387. IF (upf(nt)%oc (n) >= 0.d0) THEN
  388. l = upf(nt)%lchi (n)
  389. lmax_wfc = max (lmax_wfc, l )
  390. DO m = 1, 2 * l + 1
  391. nwfc=nwfc+1
  392. nlmchi(nwfc)%na = na
  393. nlmchi(nwfc)%n = n
  394. nlmchi(nwfc)%l = l
  395. nlmchi(nwfc)%m = m
  396. ENDDO
  397. ENDIF
  398. ENDDO
  399. ENDDO
  400. !
  401. IF (lmax_wfc > 3) CALL errore ('projwave', 'l > 3 not yet implemented', 1)
  402. IF (nwfc /= natomwfc) CALL errore ('projwave', 'wrong # of atomic wfcs?', 1)
  403. !
  404. ALLOCATE( proj (natomwfc, nbnd, nkstot) )
  405. ALLOCATE( proj_aux (natomwfc, nbnd, nkstot) )
  406. proj = 0.d0
  407. proj_aux = (0.d0, 0.d0)
  408. !
  409. IF (.not. lda_plus_u) ALLOCATE(swfcatom (npwx , natomwfc ) )
  410. ALLOCATE(wfcatom (npwx, natomwfc) )
  411. ALLOCATE(overlap (natomwfc, natomwfc) )
  412. overlap= (0.d0,0.d0)
  413. !
  414. IF ( gamma_only ) THEN
  415. ALLOCATE(roverlap (natomwfc, natomwfc) )
  416. roverlap= 0.d0
  417. ENDIF
  418. CALL allocate_bec_type (nkb, natomwfc, becp )
  419. ALLOCATE(e (natomwfc) )
  420. !
  421. ! loop on k points
  422. !
  423. CALL init_us_1
  424. CALL init_at_1
  425. !
  426. DO ik = 1, nks
  427. CALL gk_sort (xk (1, ik), ngm, g, ecutwfc / tpiba2, npw, igk, g2kin)
  428. CALL davcio (evc, nwordwfc, iunwfc, ik, - 1)
  429. CALL atomic_wfc (ik, wfcatom)
  430. CALL init_us_2 (npw, igk, xk (1, ik), vkb)
  431. CALL calbec ( npw, vkb, wfcatom, becp)
  432. CALL s_psi (npwx, npw, natomwfc, wfcatom, swfcatom)
  433. !
  434. ! wfcatom = |phi_i> , swfcatom = \hat S |phi_i>
  435. ! calculate overlap matrix O_ij = <phi_i|\hat S|\phi_j>
  436. !
  437. IF ( gamma_only ) THEN
  438. CALL calbec ( npw, wfcatom, swfcatom, roverlap )
  439. overlap(:,:)=cmplx(roverlap(:,:),0.0_dp, kind=dp)
  440. ! TEMP: diagonalization routine for real matrix should be used instead
  441. ELSE
  442. CALL calbec ( npw, wfcatom, swfcatom, overlap )
  443. ENDIF
  444. !
  445. ! calculate O^{-1/2}
  446. !
  447. ALLOCATE(work (natomwfc, natomwfc) )
  448. CALL cdiagh (natomwfc, overlap, natomwfc, e, work)
  449. DO i = 1, natomwfc
  450. e (i) = 1.d0 / dsqrt (e (i) )
  451. ENDDO
  452. DO i = 1, natomwfc
  453. DO j = i, natomwfc
  454. overlap (i, j) = (0.d0, 0.d0)
  455. DO k = 1, natomwfc
  456. overlap (i, j) = overlap (i, j) + e (k) * work (j, k) * conjg (work (i, k) )
  457. ENDDO
  458. IF (j /= i) overlap (j, i) = conjg (overlap (i, j))
  459. ENDDO
  460. ENDDO
  461. DEALLOCATE (work)
  462. !
  463. ! calculate wfcatom = O^{-1/2} \hat S | phi>
  464. !
  465. IF ( gamma_only ) THEN
  466. roverlap(:,:)=REAL(overlap(:,:),DP)
  467. ! TEMP: diagonalization routine for real matrix should be used instead
  468. CALL DGEMM ('n', 't', 2*npw, natomwfc, natomwfc, 1.d0 , &
  469. swfcatom, 2*npwx, roverlap, natomwfc, 0.d0, wfcatom, 2*npwx)
  470. ELSE
  471. CALL ZGEMM ('n', 't', npw, natomwfc, natomwfc, (1.d0, 0.d0) , &
  472. swfcatom, npwx, overlap, natomwfc, (0.d0, 0.d0), wfcatom, npwx)
  473. ENDIF
  474. !
  475. ! make the projection <psi_i| O^{-1/2} \hat S | phi_j>
  476. !
  477. IF ( gamma_only ) THEN
  478. !
  479. ALLOCATE(rproj0(natomwfc,nbnd), rwork1 (nbnd) )
  480. CALL calbec ( npw, wfcatom, evc, rproj0)
  481. !
  482. proj_aux(:,:,ik) = cmplx( rproj0(:,:), 0.0_dp, kind=dp )
  483. !
  484. ELSE
  485. !
  486. ALLOCATE(proj0(natomwfc,nbnd), work1 (nbnd) )
  487. CALL calbec ( npw, wfcatom, evc, proj0)
  488. !
  489. proj_aux(:,:,ik) = proj0(:,:)
  490. !
  491. ENDIF
  492. !
  493. ! symmetrize the projections
  494. !
  495. IF (lsym) THEN
  496. DO nwfc = 1, natomwfc
  497. !
  498. ! atomic wavefunction nwfc is on atom na
  499. !
  500. na= nlmchi(nwfc)%na
  501. n = nlmchi(nwfc)%n
  502. l = nlmchi(nwfc)%l
  503. m = nlmchi(nwfc)%m
  504. !
  505. DO isym = 1, nsym
  506. nb = irt (isym, na)
  507. DO nwfc1 =1, natomwfc
  508. IF (nlmchi(nwfc1)%na == nb .and. &
  509. nlmchi(nwfc1)%n == nlmchi(nwfc)%n .and. &
  510. nlmchi(nwfc1)%l == nlmchi(nwfc)%l .and. &
  511. nlmchi(nwfc1)%m == 1 ) GOTO 10
  512. ENDDO
  513. CALL errore('projwave','cannot symmetrize',1)
  514. 10 nwfc1=nwfc1-1
  515. !
  516. ! nwfc1 is the first rotated atomic wfc corresponding to nwfc
  517. !
  518. IF ( gamma_only ) THEN
  519. IF (l == 0) THEN
  520. rwork1(:) = rproj0 (nwfc1 + 1,:)
  521. ELSEIF (l == 1) THEN
  522. rwork1(:) = 0.d0
  523. DO m1 = 1, 3
  524. rwork1(:)=rwork1(:)+d1(m1,m,isym)*rproj0(nwfc1+m1,:)
  525. ENDDO
  526. ELSEIF (l == 2) THEN
  527. rwork1(:) = 0.d0
  528. DO m1 = 1, 5
  529. rwork1(:)=rwork1(:)+d2(m1,m,isym)*rproj0(nwfc1+m1,:)
  530. ENDDO
  531. ELSEIF (l == 3) THEN
  532. rwork1(:) = 0.d0
  533. DO m1 = 1, 7
  534. rwork1(:)=rwork1(:)+d3(m1,m,isym)*rproj0(nwfc1+m1,:)
  535. ENDDO
  536. ENDIF
  537. DO ibnd = 1, nbnd
  538. proj (nwfc, ibnd, ik) = proj (nwfc, ibnd, ik) + &
  539. rwork1(ibnd) * rwork1(ibnd) / nsym
  540. ENDDO
  541. ELSE
  542. IF (l == 0) THEN
  543. work1(:) = proj0 (nwfc1 + 1,:)
  544. ELSEIF (l == 1) THEN
  545. work1(:) = 0.d0
  546. DO m1 = 1, 3
  547. work1(:)=work1(:)+d1(m1,m,isym)*proj0(nwfc1+m1,:)
  548. ENDDO
  549. ELSEIF (l == 2) THEN
  550. work1(:) = 0.d0
  551. DO m1 = 1, 5
  552. work1(:)=work1(:)+d2(m1,m,isym)*proj0(nwfc1+m1,:)
  553. ENDDO
  554. ELSEIF (l == 3) THEN
  555. work1(:) = 0.d0
  556. DO m1 = 1, 7
  557. work1(:)=work1(:)+d3(m1,m,isym)*proj0(nwfc1+m1,:)
  558. ENDDO
  559. ENDIF
  560. DO ibnd = 1, nbnd
  561. proj (nwfc, ibnd, ik) = proj (nwfc, ibnd, ik) + &
  562. work1(ibnd) * conjg (work1(ibnd)) / nsym
  563. ENDDO
  564. ENDIF
  565. ENDDO
  566. ENDDO
  567. ELSE
  568. IF ( gamma_only ) THEN
  569. DO nwfc=1,natomwfc
  570. DO ibnd=1,nbnd
  571. proj(nwfc,ibnd,ik)=abs(rproj0(nwfc,ibnd))**2
  572. ENDDO
  573. ENDDO
  574. ELSE
  575. DO nwfc=1,natomwfc
  576. DO ibnd=1,nbnd
  577. proj(nwfc,ibnd,ik)=abs(proj0(nwfc,ibnd))**2
  578. ENDDO
  579. ENDDO
  580. ENDIF
  581. ENDIF
  582. IF ( gamma_only ) THEN
  583. DEALLOCATE (rwork1)
  584. DEALLOCATE (rproj0)
  585. ELSE
  586. DEALLOCATE (work1)
  587. DEALLOCATE (proj0)
  588. ENDIF
  589. ! on k-points
  590. ENDDO
  591. !
  592. DEALLOCATE (e)
  593. IF ( gamma_only ) THEN
  594. DEALLOCATE (roverlap)
  595. ENDIF
  596. CALL deallocate_bec_type (becp)
  597. DEALLOCATE (overlap)
  598. DEALLOCATE (wfcatom)
  599. IF (.not. lda_plus_u) DEALLOCATE (swfcatom)
  600. !
  601. ! vectors et and proj are distributed across the pools
  602. ! collect data for all k-points to the first pool
  603. !
  604. CALL poolrecover (et, nbnd, nkstot, nks)
  605. CALL poolrecover (proj, nbnd * natomwfc, nkstot, nks)
  606. CALL poolrecover (proj_aux, 2 * nbnd * natomwfc, nkstot, nks)
  607. !
  608. !!!! for GWW
  609. IF(lgww) THEN
  610. INQUIRE ( file='bands.dat', EXIST=lex )
  611. WRITE(stdout,*) 'lex=', lex
  612. CALL flush_unit(stdout)
  613. !
  614. IF(lex) THEN
  615. WRITE(stdout,*) 'Read the file bands.dat => GWA Eigenvalues used.'
  616. CALL flush_unit(stdout)
  617. iun = find_free_unit()
  618. OPEN(unit=iun, file='bands.dat', status='unknown', form='formatted', IOSTAT=ios)
  619. READ(iun,*) idum
  620. DO i=1, nbnd
  621. READ(iun,*) idum,rdum1,rdum2,et(i,1),rdum3
  622. ENDDO
  623. et(:,1)=et(:,1)/rytoev !! because in bands.dat file, the QP energies are in eV
  624. ELSE
  625. WRITE(stdout,*) 'The file bands.dat does not exist.'
  626. WRITE(stdout,*) 'Eigenergies are not modified'
  627. CALL flush_unit(stdout)
  628. ENDIF
  629. !!!! end GWW
  630. !
  631. ENDIF
  632. IF ( ionode ) THEN
  633. !
  634. ! write on the file filproj
  635. !
  636. IF (filproj/=' ') THEN
  637. DO is=1,nspin
  638. IF (nspin==2) THEN
  639. IF (is==1) filename=trim(filproj)//'.up'
  640. IF (is==2) filename=trim(filproj)//'.down'
  641. nksinit=(nkstot/2)*(is-1)+1
  642. nkslast=(nkstot/2)*is
  643. ELSE
  644. filename=trim(filproj)
  645. nksinit=1
  646. nkslast=nkstot
  647. ENDIF
  648. iunproj=33
  649. CALL write_io_header(filename, iunproj, title, nr1x, nr2x, nr3x, &
  650. nr1, nr2, nr3, nat, ntyp, ibrav, celldm, at, gcutm, dual, &
  651. ecutwfc, nkstot/nspin, nbnd, natomwfc)
  652. DO nwfc = 1, natomwfc
  653. WRITE(iunproj,'(2i5,a3,3i5)') &
  654. nwfc, nlmchi(nwfc)%na, atm(ityp(nlmchi(nwfc)%na)), &
  655. nlmchi(nwfc)%n, nlmchi(nwfc)%l, nlmchi(nwfc)%m
  656. DO ik=nksinit,nkslast
  657. DO ibnd=1,nbnd
  658. WRITE(iunproj,'(2i8,f20.10)') ik,ibnd, &
  659. abs(proj(nwfc,ibnd,ik))
  660. ENDDO
  661. ENDDO
  662. ENDDO
  663. CLOSE(iunproj)
  664. ENDDO
  665. ENDIF
  666. !
  667. ! write projections to file using iotk
  668. !
  669. CALL write_proj( "atomic_proj.xml", proj_aux )
  670. !
  671. ! write on the standard output file
  672. !
  673. WRITE( stdout,'(/5x,"Atomic states used for projection")')
  674. WRITE( stdout,'( 5x,"(read from pseudopotential files):"/)')
  675. DO nwfc = 1, natomwfc
  676. WRITE(stdout,1000) &
  677. nwfc, nlmchi(nwfc)%na, atm(ityp(nlmchi(nwfc)%na)), &
  678. nlmchi(nwfc)%n, nlmchi(nwfc)%l, nlmchi(nwfc)%m
  679. ENDDO
  680. 1000 FORMAT (5x,"state #",i4,": atom ",i3," (",a3,"), wfc ",i2, &
  681. " (l=",i1," m=",i2,")")
  682. !
  683. ALLOCATE(idx(natomwfc), proj1 (natomwfc) )
  684. DO ik = 1, nkstot
  685. WRITE( stdout, '(/" k = ",3f14.10)') (xk (i, ik) , i = 1, 3)
  686. DO ibnd = 1, nbnd
  687. WRITE( stdout, '("==== e(",i4,") = ",f11.5," eV ==== ")') &
  688. ibnd, et (ibnd, ik) * rytoev
  689. !
  690. ! sort projections by magnitude, in decreasing order
  691. !
  692. DO nwfc = 1, natomwfc
  693. idx (nwfc) = 0
  694. proj1 (nwfc) = - proj (nwfc, ibnd, ik)
  695. ENDDO
  696. !
  697. ! projections differing by less than 1.d-4 are considered equal
  698. !
  699. CALL hpsort_eps (natomwfc, proj1, idx, eps4)
  700. !
  701. ! only projections that are larger than 0.001 are written
  702. !
  703. DO nwfc = 1, natomwfc
  704. proj1 (nwfc) = - proj1(nwfc)
  705. IF ( abs (proj1(nwfc)) < 0.001d0 ) GOTO 20
  706. ENDDO
  707. nwfc = natomwfc + 1
  708. 20 nwfc = nwfc -1
  709. !
  710. ! fancy (?!?) formatting
  711. !
  712. WRITE( stdout, '(5x,"psi = ",5(f5.3,"*[#",i4,"]+"))') &
  713. (proj1 (i), idx(i), i = 1, min(5,nwfc))
  714. DO j = 1, (nwfc-1)/5
  715. WRITE( stdout, '(10x,"+",5(f5.3,"*[#",i4,"]+"))') &
  716. (proj1 (i), idx(i), i = 5*j+1, min(5*(j+1),nwfc))
  717. ENDDO
  718. psum = 0.d0
  719. DO nwfc = 1, natomwfc
  720. psum = psum + proj (nwfc, ibnd, ik)
  721. ENDDO
  722. WRITE( stdout, '(4x,"|psi|^2 = ",f5.3)') psum
  723. !
  724. ENDDO
  725. ENDDO
  726. DEALLOCATE (idx, proj1)
  727. !
  728. ! estimate partial charges (Loewdin) on each atom
  729. !
  730. ALLOCATE ( charges (nat, 0:lmax_wfc, nspin ) )
  731. charges = 0.0d0
  732. DO ik = 1, nkstot
  733. IF ( nspin == 1 ) THEN
  734. current_spin = 1
  735. ELSEIF ( nspin == 2 ) THEN
  736. current_spin = isk ( ik )
  737. ELSE
  738. CALL errore ('projwfc_nc',' called in the wrong case ',1)
  739. ENDIF
  740. DO ibnd = 1, nbnd
  741. DO nwfc = 1, natomwfc
  742. na= nlmchi(nwfc)%na
  743. l = nlmchi(nwfc)%l
  744. charges(na,l,current_spin) = charges(na,l,current_spin) + &
  745. wg (ibnd,ik) * proj (nwfc, ibnd, ik)
  746. ENDDO
  747. ENDDO
  748. ENDDO
  749. !
  750. WRITE( stdout, '(/"Lowdin Charges: "/)')
  751. !
  752. DO na = 1, nat
  753. DO is = 1, nspin
  754. totcharge(is) = sum(charges(na,0:lmax_wfc,is))
  755. ENDDO
  756. IF ( nspin == 1) THEN
  757. WRITE( stdout, 2000) na, totcharge(1), &
  758. ( l_label(l), charges(na,l,1), l= 0,lmax_wfc)
  759. ELSEIF ( nspin == 2) THEN
  760. WRITE( stdout, 2000) na, totcharge(1) + totcharge(2), &
  761. ( l_label(l), charges(na,l,1) + charges(na,l,2), l=0,lmax_wfc)
  762. WRITE( stdout, 2001) totcharge(1), &
  763. ( l_label(l), charges(na,l,1), l= 0,lmax_wfc)
  764. WRITE( stdout, 2002) totcharge(2), &
  765. ( l_label(l), charges(na,l,2), l= 0,lmax_wfc)
  766. WRITE( stdout, 2003) totcharge(1) - totcharge(2), &
  767. ( l_label(l), charges(na,l,1) - charges(na,l,2), l=0,lmax_wfc)
  768. ENDIF
  769. ENDDO
  770. 2000 FORMAT (5x,"Atom # ",i3,": total charge = ",f8.4,4(", ",a1," =",f8.4))
  771. 2001 FORMAT (15x," spin up = ",f8.4,4(", ",a1," =",f8.4))
  772. 2002 FORMAT (15x," spin down = ",f8.4,4(", ",a1," =",f8.4))
  773. 2003 FORMAT (15x," polarization = ",f8.4,4(", ",a1," =",f8.4))
  774. !
  775. psum = sum(charges(:,:,:)) / nelec
  776. WRITE( stdout, '(5x,"Spilling Parameter: ",f8.4)') 1.0d0 - psum
  777. !
  778. ! Sanchez-Portal et al., Sol. State Commun. 95, 685 (1995).
  779. ! The spilling parameter measures the ability of the basis provided by
  780. ! the pseudo-atomic wfc to represent the PW eigenstates,
  781. ! by measuring how much of the subspace of the Hamiltonian
  782. ! eigenstates falls outside the subspace spanned by the atomic basis
  783. !
  784. DEALLOCATE (charges)
  785. !
  786. ENDIF
  787. !
  788. RETURN
  789. !
  790. END SUBROUTINE projwave
  791. !
  792. !-----------------------------------------------------------------------
  793. SUBROUTINE projwave_nc(filproj, lsym )
  794. !-----------------------------------------------------------------------
  795. !
  796. USE io_global, ONLY : stdout, ionode
  797. USE ions_base, ONLY : zv, tau, nat, ntyp => nsp, ityp, atm
  798. USE basis, ONLY : natomwfc
  799. USE printout_base, ONLY: title
  800. USE cell_base
  801. USE constants, ONLY: rytoev, eps4
  802. USE gvect
  803. USE grid_dimensions, ONLY : nr1, nr2, nr3, nr1x, nr2x, nr3x
  804. USE klist, ONLY: xk, nks, nkstot, nelec
  805. USE ldaU
  806. USE lsda_mod, ONLY: nspin
  807. USE noncollin_module, ONLY: noncolin, npol, angle1, angle2
  808. USE symm_base, ONLY: nsym, irt, t_rev
  809. USE wvfct
  810. USE control_flags, ONLY: gamma_only
  811. USE uspp, ONLY: nkb, vkb
  812. USE uspp_param, ONLY: upf
  813. USE becmod, ONLY: bec_type, becp, calbec, allocate_bec_type, deallocate_bec_type
  814. USE io_files, ONLY: nd_nmbr, prefix, tmp_dir, nwordwfc, iunwfc
  815. USE wavefunctions_module, ONLY: evc
  816. USE mp_global, ONLY : intra_pool_comm
  817. USE mp, ONLY : mp_sum
  818. !
  819. USE spin_orb, ONLY: lspinorb, domag
  820. USE projections_nc
  821. !
  822. IMPLICIT NONE
  823. !
  824. CHARACTER(len=*) :: filproj
  825. LOGICAL :: lsym
  826. INTEGER :: ik, ibnd, i, j, k, na, nb, nt, isym, ind, n, m, m1, n1, &
  827. n2, l, nwfc, nwfc1, lmax_wfc, is, nspin0, iunproj, &
  828. ind0
  829. REAL(DP) :: jj
  830. REAL(DP), ALLOCATABLE :: e (:)
  831. COMPLEX(DP), ALLOCATABLE :: wfcatom (:,:)
  832. COMPLEX(DP), ALLOCATABLE :: overlap(:,:), work(:,:),work1(:), proj0(:,:)
  833. ! Some workspace for k-point calculation ...
  834. REAL(DP), ALLOCATABLE :: charges(:,:,:), proj1 (:)
  835. REAL(DP) :: psum, totcharge(2), fact(2), spinor, compute_mj
  836. INTEGER, ALLOCATABLE :: idx(:)
  837. !
  838. COMPLEX(DP) :: d12(2, 2, 48), d32(4, 4, 48), d52(6, 6, 48), &
  839. d72(8, 8, 48)
  840. COMPLEX(DP) :: d012(2, 2, 48), d112(6, 6, 48), d212(10, 10, 48), &
  841. d312(14, 14, 48)
  842. !
  843. !
  844. !
  845. IF (.not.noncolin) CALL errore('projwave_nc','called in the wrong case',1)
  846. IF (gamma_only) CALL errore('projwave_nc','gamma_only not yet implemented',1)
  847. WRITE( stdout, '(/5x,"Calling projwave_nc .... ")')
  848. !
  849. ! fill structure nlmchi
  850. !
  851. ALLOCATE (nlmchi(natomwfc))
  852. nwfc=0
  853. lmax_wfc = 0
  854. DO na = 1, nat
  855. nt = ityp (na)
  856. n2 = 0
  857. DO n = 1, upf(nt)%nwfc
  858. IF (upf(nt)%oc (n) >= 0.d0) THEN
  859. l = upf(nt)%lchi (n)
  860. lmax_wfc = max (lmax_wfc, l )
  861. IF (lspinorb) THEN
  862. IF (upf(nt)%has_so) THEN
  863. jj = upf(nt)%jchi (n)
  864. ind = 0
  865. DO m = -l-1, l
  866. fact(1) = spinor(l,jj,m,1)
  867. fact(2) = spinor(l,jj,m,2)
  868. IF (abs(fact(1)) > 1.d-8 .or. abs(fact(2)) > 1.d-8) THEN
  869. nwfc = nwfc + 1
  870. ind = ind + 1
  871. nlmchi(nwfc)%na = na
  872. nlmchi(nwfc)%n = n
  873. nlmchi(nwfc)%l = l
  874. nlmchi(nwfc)%m = m
  875. nlmchi(nwfc)%ind = ind
  876. nlmchi(nwfc)%jj = jj
  877. ENDIF
  878. ENDDO
  879. ELSE
  880. DO n1 = l, l+1
  881. jj= dble(n1) - 0.5d0
  882. ind = 0
  883. IF (jj>0.d0) THEN
  884. n2 = n2 + 1
  885. DO m = -l-1, l
  886. fact(1) = spinor(l,jj,m,1)
  887. fact(2) = spinor(l,jj,m,2)
  888. IF (abs(fact(1)) > 1.d-8 .or. abs(fact(2)) > 1.d-8) THEN
  889. nwfc = nwfc + 1
  890. ind = ind + 1
  891. nlmchi(nwfc)%na = na
  892. nlmchi(nwfc)%n = n2
  893. nlmchi(nwfc)%l = l
  894. nlmchi(nwfc)%m = m
  895. nlmchi(nwfc)%ind = ind
  896. nlmchi(nwfc)%jj = jj
  897. ENDIF
  898. ENDDO
  899. ENDIF
  900. ENDDO
  901. ENDIF
  902. ELSE
  903. DO m = 1, 2 * l + 1
  904. nwfc=nwfc+1
  905. nlmchi(nwfc)%na = na
  906. nlmchi(nwfc)%n = n
  907. nlmchi(nwfc)%l = l
  908. nlmchi(nwfc)%m = m
  909. nlmchi(nwfc)%ind = m
  910. nlmchi(nwfc)%jj = 0.d0
  911. nlmchi(nwfc+2*l+1)%na = na
  912. nlmchi(nwfc+2*l+1)%n = n
  913. nlmchi(nwfc+2*l+1)%l = l
  914. nlmchi(nwfc+2*l+1)%m = m
  915. nlmchi(nwfc+2*l+1)%ind = m+2*l+1
  916. nlmchi(nwfc+2*l+1)%jj = 0.d0
  917. ENDDO
  918. nwfc=nwfc+2*l+1
  919. ENDIF
  920. ENDIF
  921. ENDDO
  922. ENDDO
  923. !
  924. IF (lmax_wfc > 3) CALL errore ('projwave_nc', 'l > 3 not yet implemented', 1)
  925. IF (nwfc /= natomwfc) CALL errore ('projwave_nc','wrong # of atomic wfcs?',1)
  926. !
  927. ALLOCATE(wfcatom (npwx*npol,natomwfc) )
  928. IF (.not. lda_plus_u) ALLOCATE(swfcatom (npwx*npol, natomwfc ) )
  929. CALL allocate_bec_type (nkb, natomwfc, becp )
  930. ALLOCATE(e (natomwfc) )
  931. ALLOCATE(work (natomwfc, natomwfc) )
  932. !
  933. ALLOCATE(overlap (natomwfc, natomwfc) )
  934. ALLOCATE(proj0(natomwfc,nbnd), work1 (nbnd) )
  935. ALLOCATE(proj (natomwfc, nbnd, nkstot) )
  936. ALLOCATE(proj_aux (natomwfc, nbnd, nkstot) )
  937. overlap = (0.d0,0.d0)
  938. proj0 = (0.d0,0.d0)
  939. proj = 0.d0
  940. proj_aux = (0.d0,0.d0)
  941. !
  942. ! loop on k points
  943. !
  944. CALL init_us_1
  945. CALL init_at_1
  946. !
  947. IF (lspinorb) THEN
  948. !
  949. ! initialize D_Sj for j=1/2, j=3/2, j=5/2 and j=7/2
  950. !
  951. CALL d_matrix_so (d12, d32, d52, d72)
  952. !
  953. ELSE
  954. !
  955. ! initialize D_Sl for l=0, l=1, l=2 and l=3
  956. !
  957. CALL d_matrix_nc (d012, d112, d212, d312)
  958. !
  959. ENDIF
  960. !
  961. DO ik = 1, nks
  962. wfcatom = (0.d0,0.d0)
  963. swfcatom= (0.d0,0.d0)
  964. CALL gk_sort (xk (1, ik), ngm, g, ecutwfc / tpiba2, npw, igk, g2kin)
  965. CALL davcio (evc, nwordwfc, iunwfc, ik, - 1)
  966. !
  967. CALL atomic_wfc_nc_proj (ik, wfcatom)
  968. !
  969. CALL init_us_2 (npw, igk, xk (1, ik), vkb)
  970. CALL calbec ( npw, vkb, wfcatom, becp )
  971. CALL s_psi (npwx, npw, natomwfc, wfcatom, swfcatom)
  972. !
  973. ! wfcatom = |phi_i> , swfcatom = \hat S |phi_i>
  974. ! calculate overlap matrix O_ij = <phi_i|\hat S|\phi_j>
  975. !
  976. CALL ZGEMM ('C', 'N', natomwfc, natomwfc, npwx*npol, (1.d0, 0.d0), wfcatom, &
  977. npwx*npol, swfcatom, npwx*npol, (0.d0, 0.d0), overlap, natomwfc)
  978. CALL mp_sum ( overlap, intra_pool_comm )
  979. !
  980. ! calculate O^{-1/2}
  981. !
  982. CALL cdiagh (natomwfc, overlap, natomwfc, e, work)
  983. DO i = 1, natomwfc
  984. e (i) = 1.d0 / dsqrt (e (i) )
  985. ENDDO
  986. DO i = 1, natomwfc
  987. DO j = i, natomwfc
  988. overlap (i, j) = (0.d0, 0.d0)
  989. DO k = 1, natomwfc
  990. overlap(i, j) = overlap(i, j) + e(k) * work(j, k) * conjg(work (i, k) )
  991. ENDDO
  992. IF (j /= i) overlap (j, i) = conjg (overlap (i, j))
  993. ENDDO
  994. ENDDO
  995. !
  996. ! calculate wfcatom = O^{-1/2} \hat S | phi>
  997. !
  998. CALL ZGEMM ('n', 't', npwx*npol, natomwfc, natomwfc, (1.d0, 0.d0) , &
  999. swfcatom, npwx*npol, overlap, natomwfc, (0.d0, 0.d0), wfcatom, npwx*npol)
  1000. !
  1001. ! make the projection <psi_i| O^{-1/2} \hat S | phi_j>
  1002. !
  1003. CALL ZGEMM ('C','N',natomwfc, nbnd, npwx*npol, (1.d0, 0.d0), wfcatom, &
  1004. npwx*npol, evc, npwx*npol, (0.d0, 0.d0), proj0, natomwfc)
  1005. CALL mp_sum ( proj0( :, 1:nbnd ), intra_pool_comm )
  1006. !
  1007. proj_aux(:,:,ik) = proj0(:,:)
  1008. !
  1009. IF (lsym) THEN
  1010. DO nwfc = 1, natomwfc
  1011. !
  1012. ! atomic wavefunction nwfc is on atom na
  1013. !
  1014. IF (lspinorb) THEN
  1015. na= nlmchi(nwfc)%na
  1016. n = nlmchi(nwfc)%n
  1017. l = nlmchi(nwfc)%l
  1018. m = nlmchi(nwfc)%m
  1019. ind0 = nlmchi(nwfc)%ind
  1020. jj = nlmchi(nwfc)%jj
  1021. !
  1022. DO isym = 1, nsym
  1023. !-- check for the time reversal
  1024. IF (t_rev(isym) == 1) THEN
  1025. ind = 2*jj + 2 - ind0
  1026. ELSE
  1027. ind = ind0
  1028. ENDIF
  1029. !--
  1030. nb = irt (isym, na)
  1031. DO nwfc1 =1, natomwfc
  1032. IF (nlmchi(nwfc1)%na == nb .and. &
  1033. nlmchi(nwfc1)%n == nlmchi(nwfc)%n .and. &
  1034. nlmchi(nwfc1)%l == nlmchi(nwfc)%l .and. &
  1035. nlmchi(nwfc1)%jj == nlmchi(nwfc)%jj .and. &
  1036. nlmchi(nwfc1)%ind == 1 ) GOTO 10
  1037. ENDDO
  1038. CALL errore('projwave_nc','cannot symmetrize',1)
  1039. 10 nwfc1=nwfc1-1
  1040. !
  1041. ! nwfc1 is the first rotated atomic wfc corresponding to nwfc
  1042. !
  1043. IF (abs(jj-0.5d0)<1.d-8) THEN
  1044. work1(:) = 0.d0
  1045. DO m1 = 1, 2
  1046. work1(:)=work1(:)+d12(m1,ind,isym)*proj0(nwfc1+m1,:)
  1047. ENDDO
  1048. ELSEIF (abs(jj-1.5d0)<1.d-8) THEN
  1049. work1(:) = 0.d0
  1050. DO m1 = 1, 4
  1051. work1(:)=work1(:)+d32(m1,ind,isym)*proj0(nwfc1 + m1,:)
  1052. ENDDO
  1053. ELSEIF (abs(jj-2.5d0)<1.d-8) THEN
  1054. work1(:) = 0.d0
  1055. DO m1 = 1, 6
  1056. work1(:)=work1(:)+d52(m1,ind,isym)*proj0(nwfc1+m1,:)
  1057. ENDDO
  1058. ELSEIF (abs(jj-3.5d0)<1.d-8) THEN
  1059. work1(:) = 0.d0
  1060. DO m1 = 1, 8
  1061. work1(:)=work1(:)+d72(m1,ind,isym)*proj0(nwfc1+m1,:)
  1062. ENDDO
  1063. ENDIF
  1064. DO ibnd = 1, nbnd
  1065. proj (nwfc, ibnd, ik) = proj (nwfc, ibnd, ik) + &
  1066. work1(ibnd) * conjg (work1(ibnd)) / nsym
  1067. ENDDO
  1068. ! on symmetries
  1069. !-- in a nonmagnetic case - another loop with the time reversal
  1070. IF (.not.domag.and.ind==ind0) THEN
  1071. ind = 2*jj + 2 - ind0
  1072. nwfc1 = nwfc1 + 1
  1073. GOTO 10
  1074. ENDIF
  1075. !--
  1076. ENDDO
  1077. !-- in a nonmagnetic case - rescale
  1078. IF (.not.domag) THEN
  1079. DO ibnd = 1, nbnd
  1080. proj(nwfc,ibnd,ik) = 0.5d0*proj(nwfc,ibnd,ik)
  1081. ENDDO
  1082. ENDIF
  1083. !--
  1084. ELSE
  1085. na= nlmchi(nwfc)%na
  1086. n = nlmchi(nwfc)%n
  1087. l = nlmchi(nwfc)%l
  1088. m = nlmchi(nwfc)%m
  1089. ind0 = nlmchi(nwfc)%ind
  1090. !
  1091. DO isym = 1, nsym
  1092. !-- check for the time reversal
  1093. IF (t_rev(isym) == 1) THEN
  1094. ind = 2*m - ind0 + 2*l + 1
  1095. ELSE
  1096. ind = ind0
  1097. ENDIF
  1098. !--
  1099. nb = irt (isym, na)
  1100. DO nwfc1 =1, natomwfc
  1101. IF (nlmchi(nwfc1)%na == nb .and. &
  1102. nlmchi(nwfc1)%n == nlmchi(nwfc)%n .and. &
  1103. nlmchi(nwfc1)%l == nlmchi(nwfc)%l .and. &
  1104. nlmchi(nwfc1)%m == 1 .and. &
  1105. nlmchi(nwfc1)%ind == 1) GOTO 15
  1106. ENDDO
  1107. CALL errore('projwave_nc','cannot symmetrize',1)
  1108. 15 nwfc1=nwfc1-1
  1109. IF (l == 0) THEN
  1110. work1(:) = 0.d0
  1111. DO m1 = 1, 2
  1112. work1(:) = work1(:) + d012 (m1, ind, isym) * &
  1113. proj0 (nwfc1 + m1,:)
  1114. ENDDO
  1115. ELSEIF (l == 1) THEN
  1116. work1(:) = 0.d0
  1117. DO m1 = 1, 6
  1118. work1(:) = work1(:) + d112 (m1, ind, isym) * &
  1119. proj0 (nwfc1 + m1,:)
  1120. ENDDO
  1121. ELSEIF (l == 2) THEN
  1122. work1(:) = 0.d0
  1123. DO m1 = 1, 10
  1124. work1(:) = work1(:) + d212 (m1, ind, isym) * &
  1125. proj0 (nwfc1 + m1,:)
  1126. ENDDO
  1127. ELSEIF (l == 3) THEN
  1128. work1(:) = 0.d0
  1129. DO m1 = 1, 14
  1130. work1(:) = work1(:) + d312 (m1, ind, isym) * &
  1131. proj0 (nwfc1 + m1,:)
  1132. ENDDO
  1133. ENDIF
  1134. DO ibnd = 1, nbnd
  1135. proj (nwfc, ibnd, ik) = proj (nwfc, ibnd, ik) + &
  1136. work1(ibnd) * conjg (work1(ibnd)) / nsym
  1137. ENDDO
  1138. ! on symmetries
  1139. ENDDO
  1140. ENDIF
  1141. ! on atomic wavefunctions
  1142. ENDDO
  1143. ELSE
  1144. DO nwfc=1,natomwfc
  1145. DO ibnd=1,nbnd
  1146. proj(nwfc,ibnd,ik)=abs(proj0(nwfc,ibnd))**2
  1147. ENDDO
  1148. ENDDO
  1149. ENDIF
  1150. ! on k-points
  1151. ENDDO
  1152. !
  1153. DEALLOCATE (work)
  1154. DEALLOCATE (work1)
  1155. DEALLOCATE (proj0)
  1156. DEALLOCATE (e)
  1157. CALL deallocate_bec_type (becp)
  1158. DEALLOCATE (overlap)
  1159. DEALLOCATE (wfcatom)
  1160. IF (.not. lda_plus_u) DEALLOCATE (swfcatom)
  1161. !
  1162. ! vectors et and proj are distributed across the pools
  1163. ! collect data for all k-points to the first pool
  1164. !
  1165. CALL poolrecover (et, nbnd, nkstot, nks)
  1166. CALL poolrecover (proj, nbnd * natomwfc, nkstot, nks)
  1167. CALL poolrecover (proj_aux, 2 * nbnd * natomwfc, nkstot, nks)
  1168. !
  1169. IF ( ionode ) THEN
  1170. !
  1171. ! write on the file filproj
  1172. !
  1173. IF (filproj/=' ') THEN
  1174. iunproj=33
  1175. CALL write_io_header(filproj, iunproj, title, nr1x, nr2x, nr3x, &
  1176. nr1, nr2, nr3, nat, ntyp, ibrav, celldm, at, gcutm, dual, ecutwfc, &
  1177. nkstot,nbnd,natomwfc)
  1178. DO nwfc = 1, natomwfc
  1179. IF (lspinorb) THEN
  1180. WRITE(iunproj,1000) &
  1181. nwfc, nlmchi(nwfc)%na,atm(ityp(nlmchi(nwfc)%na)), &
  1182. nlmchi(nwfc)%n,nlmchi(nwfc)%jj,nlmchi(nwfc)%l, &
  1183. compute_mj(nlmchi(nwfc)%jj,nlmchi(nwfc)%l,nlmchi(nwfc)%m)
  1184. ELSE
  1185. WRITE(iunproj,1500) &
  1186. nwfc, nlmchi(nwfc)%na, atm(ityp(nlmchi(nwfc)%na)), &
  1187. nlmchi(nwfc)%n, nlmchi(nwfc)%l, nlmchi(nwfc)%m, &
  1188. 0.5d0-int(nlmchi(nwfc)%ind/(2*nlmchi(nwfc)%l+2))
  1189. ENDIF
  1190. DO ik=1,nkstot
  1191. DO ibnd=1,nbnd
  1192. WRITE(iunproj,'(2i8,f20.10)') ik,ibnd,abs(proj(nwfc,ibnd,ik))
  1193. ENDDO
  1194. ENDDO
  1195. ENDDO
  1196. CLOSE(iunproj)
  1197. ENDIF
  1198. !
  1199. ! write projections to file using iotk
  1200. !
  1201. CALL write_proj( "atomic_proj.xml", proj_aux )
  1202. !
  1203. ! write on the standard output file
  1204. !
  1205. WRITE( stdout,'(/5x,"Atomic states used for projection")')
  1206. WRITE( stdout,'( 5x,"(read from pseudopotential files):"/)')
  1207. IF (lspinorb) THEN
  1208. DO nwfc = 1, natomwfc
  1209. WRITE(stdout,1000) &
  1210. nwfc, nlmchi(nwfc)%na, atm(ityp(nlmchi(nwfc)%na)), &
  1211. nlmchi(nwfc)%n, nlmchi(nwfc)%jj, nlmchi(nwfc)%l, &
  1212. compute_mj(nlmchi(nwfc)%jj,nlmchi(nwfc)%l,nlmchi(nwfc)%m)
  1213. ENDDO
  1214. 1000 FORMAT (5x,"state #",i3,": atom ",i3," (",a3,"), wfc ",i2, &
  1215. " (j=",f3.1," l=",i1," m_j=",f4.1,")")
  1216. ELSE
  1217. DO nwfc = 1, natomwfc
  1218. WRITE(stdout,1500) &
  1219. nwfc, nlmchi(nwfc)%na, atm(ityp(nlmchi(nwfc)%na)), &
  1220. nlmchi(nwfc)%n, nlmchi(nwfc)%l, nlmchi(nwfc)%m, &
  1221. 0.5d0-int(nlmchi(nwfc)%ind/(2*nlmchi(nwfc)%l+2))
  1222. ENDDO
  1223. 1500 FORMAT (5x,"state #",i3,": atom ",i3," (",a3,"), wfc ",i2, &
  1224. " (l=",i1," m=",i2," s_z=",f4.1,")")
  1225. ENDIF
  1226. !
  1227. ALLOCATE(idx (natomwfc), proj1 (natomwfc) )
  1228. DO ik = 1, nkstot
  1229. WRITE( stdout, '(/" k = ",3f14.10)') (xk (i, ik) , i = 1, 3)
  1230. DO ibnd = 1, nbnd
  1231. WRITE( stdout, '("==== e(",i4,") = ",f11.5," eV ==== ")') &
  1232. ibnd, et (ibnd, ik) * rytoev
  1233. !
  1234. ! sort projections by magnitude, in decreasing order
  1235. !
  1236. DO nwfc = 1, natomwfc
  1237. idx (nwfc) = 0
  1238. proj1 (nwfc) = - proj (nwfc, ibnd, ik)
  1239. ENDDO
  1240. CALL hpsort_eps (natomwfc, proj1, idx, eps4)
  1241. !
  1242. ! only projections that are larger than 0.001 are written
  1243. !
  1244. DO nwfc = 1, natomwfc
  1245. proj1 (nwfc) = - proj1(nwfc)
  1246. IF ( abs (proj1(nwfc)) < 0.001d0 ) GOTO 20
  1247. ENDDO
  1248. nwfc = natomwfc + 1
  1249. 20 nwfc = nwfc -1
  1250. !
  1251. ! fancy (?!?) formatting
  1252. !
  1253. WRITE( stdout, '(5x,"psi = ",5(f5.3,"*[#",i3,"]+"))') &
  1254. (proj1 (i), idx(i), i = 1, min(5,nwfc))
  1255. DO j = 1, (nwfc-1)/5
  1256. WRITE( stdout, '(10x,"+",5(f5.3,"*[#",i3,"]+"))') &
  1257. (proj1 (i), idx(i), i = 5*j+1, min(5*(j+1),nwfc))
  1258. ENDDO
  1259. psum = 0.d0
  1260. DO nwfc = 1, natomwfc
  1261. psum = psum + proj (nwfc, ibnd, ik)
  1262. ENDDO
  1263. WRITE( stdout, '(4x,"|psi|^2 = ",f5.3)') psum
  1264. !
  1265. ENDDO
  1266. ENDDO
  1267. DEALLOCATE (idx, proj1)
  1268. !
  1269. ! estimate partial charges (Loewdin) on each atom
  1270. !
  1271. IF (lspinorb) THEN
  1272. nspin0 = 1
  1273. ELSE
  1274. nspin0 = 2
  1275. ENDIF
  1276. ALLOCATE ( charges (nat, 0:lmax_wfc, nspin0 ) )
  1277. charges = 0.0d0
  1278. IF (lspinorb) THEN
  1279. DO ik = 1, nkstot
  1280. DO ibnd = 1, nbnd
  1281. DO nwfc = 1, natomwfc
  1282. na= nlmchi(nwfc)%na
  1283. l = nlmchi(nwfc)%l
  1284. charges(na,l,1) = charges(na,l,1) + &
  1285. wg (ibnd,ik) * proj (nwfc, ibnd, ik)
  1286. ENDDO
  1287. ENDDO
  1288. ENDDO
  1289. ELSE
  1290. DO ik = 1, nkstot
  1291. DO ibnd = 1, nbnd
  1292. DO nwfc = 1, natomwfc
  1293. na= nlmchi(nwfc)%na
  1294. l = nlmchi(nwfc)%l
  1295. IF ( nlmchi(nwfc)%ind<=(2*l+1)) THEN
  1296. charges(na,l,1) = charges(na,l,1) + &
  1297. wg (ibnd,ik) * proj (nwfc, ibnd, ik)
  1298. ELSE
  1299. charges(na,l,2) = charges(na,l,2) + &
  1300. wg (ibnd,ik) * proj (nwfc, ibnd, ik)
  1301. ENDIF
  1302. ENDDO
  1303. ENDDO
  1304. ENDDO
  1305. ENDIF
  1306. !
  1307. WRITE( stdout, '(/"Lowdin Charges: "/)')
  1308. !
  1309. DO na = 1, nat
  1310. DO is = 1, nspin0
  1311. totcharge(is) = sum(charges(na,0:lmax_wfc,is))
  1312. ENDDO
  1313. IF ( nspin0 == 1) THEN
  1314. WRITE( stdout, 2000) na, totcharge(1), &
  1315. ( charges(na,l,1), l= 0,lmax_wfc)
  1316. ELSEIF ( nspin0 == 2) THEN
  1317. WRITE( stdout, 2000) na, totcharge(1) + totcharge(2), &
  1318. ( charges(na,l,1) + charges(na,l,2), l=0,lmax_wfc)
  1319. WRITE( stdout, 2001) totcharge(1), &
  1320. ( charges(na,l,1), l= 0,lmax_wfc)
  1321. WRITE( stdout, 2002) totcharge(2), &
  1322. ( charges(na,l,2), l= 0,lmax_wfc)
  1323. WRITE( stdout, 2003) totcharge(1) - totcharge(2), &
  1324. ( charges(na,l,1) - charges(na,l,2), l=0,lmax_wfc)
  1325. ENDIF
  1326. ENDDO
  1327. 2000 FORMAT (5x,"Atom # ",i3,": total charge = ",f8.4 ,&
  1328. & ", s, p, d, f = ",4f8.4)
  1329. 2001 FORMAT (15x," spin up = ",f8.4 , &
  1330. & ", s, p, d, f = ",4f8.4)
  1331. 2002 FORMAT (15x," spin down = ",f8.4 , &
  1332. & ", s, p, d, f = ",4f8.4)
  1333. 2003 FORMAT (15x," polarization = ",f8.4 , &
  1334. & ", s, p, d, f = ",4f8.4)
  1335. !
  1336. psum = sum(charges(:,:,:)) / nelec
  1337. WRITE( stdout, '(5x,"Spilling Parameter: ",f8.4)') 1.0d0 - psum
  1338. !
  1339. ! Sanchez-Portal et al., Sol. State Commun. 95, 685 (1995).
  1340. ! The spilling parameter measures the ability of the basis provided by
  1341. ! the pseudo-atomic wfc to represent the PW eigenstates,
  1342. ! by measuring how much of the subspace of the Hamiltonian
  1343. ! eigenstates falls outside the subspace spanned by the atomic basis
  1344. !
  1345. DEALLOCATE (charges)
  1346. !
  1347. ENDIF
  1348. !
  1349. RETURN
  1350. !
  1351. END SUBROUTINE projwave_nc
  1352. !
  1353. !-----------------------------------------------------------------------
  1354. SUBROUTINE partialdos (Emin, Emax, DeltaE, kresolveddos, filpdos)
  1355. !-----------------------------------------------------------------------
  1356. !
  1357. USE io_global, ONLY : stdout
  1358. USE basis, ONLY : natomwfc
  1359. USE ions_base, ONLY : ityp, atm
  1360. USE klist, ONLY: wk, nkstot, degauss, ngauss, lgauss
  1361. USE lsda_mod, ONLY: nspin, isk, current_spin
  1362. USE wvfct, ONLY: et, nbnd
  1363. USE constants, ONLY: rytoev
  1364. !
  1365. USE projections
  1366. !
  1367. IMPLICIT NONE
  1368. CHARACTER (len=256) :: filpdos
  1369. REAL(DP) :: Emin, Emax, DeltaE
  1370. LOGICAL :: kresolveddos
  1371. !
  1372. CHARACTER (len=33) :: filextension
  1373. CHARACTER (len=256):: fileout
  1374. CHARACTER (len=1) :: l_label(0:3)=(/'s','p','d','f'/)
  1375. !
  1376. INTEGER :: ik, ibnd, m, &
  1377. c_tab, nwfc, ne, ie_mid, ie_delta, ie, is, nkseff, ikeff
  1378. REAL(DP) :: etev, delta, Elw, Eup, wkeff
  1379. REAL(DP), ALLOCATABLE :: dostot(:,:,:), pdos(:,:,:,:), pdostot(:,:,:), &
  1380. ldos(:,:,:)
  1381. REAL(DP), EXTERNAL :: w0gauss
  1382. !
  1383. !
  1384. ! find band extrema
  1385. !
  1386. Elw = et (1, 1)
  1387. Eup = et (nbnd, 1)
  1388. DO ik = 2, nkstot
  1389. Elw = min (Elw, et (1, ik) )
  1390. Eup = max (Eup, et (nbnd, ik) )
  1391. ENDDO
  1392. IF (degauss/=0.d0) THEN
  1393. Eup = Eup + 3d0 * degauss
  1394. Elw = Elw - 3d0 * degauss
  1395. ENDIF
  1396. Emin = max (Emin/rytoev, Elw)
  1397. Emax = min (Emax/rytoev, Eup)
  1398. DeltaE = DeltaE/rytoev
  1399. ne = nint ( (Emax - Emin) / DeltaE+0.500001d0)
  1400. !
  1401. IF (kresolveddos) THEN
  1402. IF ( nspin==2 ) THEN
  1403. nkseff=nkstot/2
  1404. ELSE
  1405. nkseff=nkstot
  1406. ENDIF
  1407. ELSE
  1408. nkseff=1
  1409. ENDIF
  1410. !
  1411. ALLOCATE (pdos(0:ne,natomwfc,nspin,nkseff))
  1412. ALLOCATE (dostot(0:ne,nspin,nkseff), pdostot(0:ne,nspin,nkseff), ldos(0:ne,nspin,nkseff) )
  1413. pdos(:,:,:,:) = 0.d0
  1414. dostot(:,:,:) = 0.d0
  1415. pdostot(:,:,:)= 0.d0
  1416. !
  1417. current_spin = 1
  1418. ie_delta = 5 * degauss / DeltaE + 1
  1419. DO ik = 1,nkstot
  1420. !
  1421. IF (kresolveddos) THEN
  1422. ! set equal weight to all k-points
  1423. wkeff=1.D0
  1424. !
  1425. IF (( nspin==2 ).AND.( isk(ik)==2 )) THEN
  1426. ikeff=ik-nkstot/2
  1427. ELSE
  1428. ikeff=ik
  1429. ENDIF
  1430. ELSE
  1431. ! use true weights
  1432. wkeff=wk(ik)
  1433. ! contributions from all k-points are summed in pdos(:,:,:,ikeff)
  1434. ikeff=1
  1435. ENDIF
  1436. !
  1437. IF ( nspin == 2 ) current_spin = isk ( ik )
  1438. DO ibnd = 1, nbnd
  1439. etev = et(ibnd,ik)
  1440. ie_mid = nint( (etev-Emin)/DeltaE )
  1441. DO ie = max(ie_mid-ie_delta, 0), min(ie_mid+ie_delta, ne)
  1442. delta = w0gauss((Emin+DeltaE*ie-etev)/degauss,ngauss) &
  1443. / degauss / rytoev
  1444. !
  1445. ! pdos(:,nwfc,ns,ik) = DOS (states/eV) for spin "ns"
  1446. ! projected over atomic wfc "nwfc"
  1447. ! for k-point "ik" (or summed over all kp)
  1448. !
  1449. DO nwfc = 1, natomwfc
  1450. pdos(ie,nwfc,current_spin,ikeff) = pdos(ie,nwfc,current_spin,ikeff) + &
  1451. wkeff * delta * proj (nwfc, ibnd, ik)
  1452. ENDDO
  1453. !
  1454. ! dostot(:,ns,ik) = total DOS (states/eV) for spin "ns"
  1455. ! for k-point "ik" (or summed over all kp)
  1456. !
  1457. dostot(ie,current_spin,ikeff) = dostot(ie,current_spin,ikeff) + &
  1458. wkeff * delta
  1459. ENDDO
  1460. ENDDO
  1461. ENDDO
  1462. !
  1463. ! pdostot(:,ns,ik) = sum of all projected DOS
  1464. !
  1465. DO ik=1,nkseff
  1466. DO is=1,nspin
  1467. DO ie=0,ne
  1468. pdostot(ie,is,ik) = sum(pdos(ie,:,is,ik))
  1469. ENDDO
  1470. ENDDO
  1471. ENDDO
  1472. DO nwfc = 1, natomwfc
  1473. IF (nlmchi(nwfc)%m == 1) THEN
  1474. filextension='.pdos_atm#'
  1475. ! 12345678901
  1476. c_tab = 11
  1477. IF (nlmchi(nwfc)%na < 10) THEN
  1478. WRITE (filextension( c_tab : c_tab ),'(i1)') nlmchi(nwfc)%na
  1479. c_tab = c_tab + 1
  1480. ELSEIF (nlmchi(nwfc)%na < 100) THEN
  1481. WRITE (filextension( c_tab : c_tab+1 ),'(i2)') nlmchi(nwfc)%na
  1482. c_tab = c_tab + 2
  1483. ELSEIF (nlmchi(nwfc)%na < 1000) THEN
  1484. WRITE (filextension( c_tab : c_tab+2 ),'(i3)') nlmchi(nwfc)%na
  1485. c_tab = c_tab + 3
  1486. ELSEIF (nlmchi(nwfc)%na < 10000) THEN
  1487. WRITE (filextension( c_tab : c_tab+3 ),'(i4)') nlmchi(nwfc)%na
  1488. c_tab = c_tab + 4
  1489. ELSE
  1490. CALL errore('partialdos',&
  1491. 'file extension not supporting so many atoms', nwfc)
  1492. ENDIF
  1493. WRITE (filextension(c_tab:c_tab+4),'(a1,a)') &
  1494. '(',trim(atm(ityp(nlmchi(nwfc)%na)))
  1495. c_tab = c_tab + len_trim(atm(ityp(nlmchi(nwfc)%na))) + 1
  1496. IF (nlmchi(nwfc)%n >= 10) &
  1497. CALL errore('partialdos',&
  1498. 'file extension not supporting so many atomic wfc', nwfc)
  1499. IF (nlmchi(nwfc)%l > 3) &
  1500. CALL errore('partialdos',&
  1501. 'file extension not supporting so many l', nwfc)
  1502. WRITE (filextension(c_tab:),'(")_wfc#",i1,"(",a1,")")') &
  1503. nlmchi(nwfc)%n, l_label(nlmchi(nwfc)%l)
  1504. fileout = trim(filpdos)//trim(filextension)
  1505. OPEN (4,file=fileout,form='formatted', &
  1506. status='unknown')
  1507. IF (kresolveddos) THEN
  1508. WRITE (4,'("# ik ",$)')
  1509. ELSE
  1510. WRITE (4,'("#",$)')
  1511. ENDIF
  1512. IF (nspin == 1) THEN
  1513. WRITE (4,'(" E (eV) ldos(E) ",$)')
  1514. ELSE
  1515. WRITE (4,'(" E (eV) ldosup(E) ldosdw(E)",$)')
  1516. ENDIF
  1517. DO m=1,2 * nlmchi(nwfc)%l + 1
  1518. IF (nspin == 1) THEN
  1519. WRITE(4,'(" pdos(E) ",$)')
  1520. ELSE
  1521. WRITE(4,'(" pdosup(E) ",$)')
  1522. WRITE(4,'(" pdosdw(E) ",$)')
  1523. ENDIF
  1524. ENDDO
  1525. WRITE(4,*)
  1526. !
  1527. ! ldos = PDOS summed over m (m=-l:+l)
  1528. !
  1529. ldos (:,:,:) = 0.d0
  1530. DO ik=1,nkseff
  1531. DO ie= 0, ne
  1532. DO is=1, nspin
  1533. DO m=1,2 * nlmchi(nwfc)%l + 1
  1534. ldos (ie, is, ik) = ldos (ie, is, ik) + pdos(ie,nwfc+m-1,is,ik)
  1535. ENDDO
  1536. ENDDO
  1537. ENDDO
  1538. ENDDO
  1539. DO ik=1,nkseff
  1540. DO ie= 0, ne
  1541. IF (kresolveddos) THEN
  1542. WRITE (4,'(i5," ",$)') ik
  1543. ENDIF
  1544. etev = Emin + ie * DeltaE
  1545. WRITE (4,'(f7.3,2e11.3,14e11.3)') etev*rytoev, &
  1546. (ldos(ie,is,ik), is=1,nspin), &
  1547. ((pdos(ie,nwfc+m-1,is,ik), is=1,nspin), &
  1548. m=1,2*nlmchi(nwfc)%l+1)
  1549. ENDDO
  1550. IF (kresolveddos) WRITE (4,*)
  1551. ENDDO
  1552. CLOSE (4)
  1553. ENDIF
  1554. ENDDO
  1555. fileout = trim(filpdos)//".pdos_tot"
  1556. OPEN (4,file=fileout,form='formatted', status='unknown')
  1557. IF (kresolveddos) THEN
  1558. WRITE (4,'("# ik ",$)')
  1559. ELSE
  1560. WRITE (4,'("#",$)')
  1561. ENDIF
  1562. IF (nspin == 1) THEN
  1563. WRITE (4,'(" E (eV) dos(E) pdos(E)")')
  1564. ELSE
  1565. WRITE (4,'(" E (eV) dosup(E) dosdw(E) pdosup(E) pdosdw(E)")')
  1566. ENDIF
  1567. DO ik=1,nkseff
  1568. DO ie= 0, ne
  1569. IF (kresolveddos) THEN
  1570. WRITE (4,'(i5," ",$)') ik
  1571. ENDIF
  1572. etev = Emin + ie * DeltaE
  1573. WRITE (4,'(f7.3,4e11.3)') etev*rytoev, (dostot(ie,is,ik), is=1,nspin), &
  1574. (pdostot(ie,is,ik), is=1,nspin)
  1575. ENDDO
  1576. IF (kresolveddos) WRITE (4,*)
  1577. ENDDO
  1578. CLOSE (4)
  1579. DEALLOCATE (ldos, dostot, pdostot)
  1580. DEALLOCATE (pdos)
  1581. !
  1582. DEALLOCATE (nlmchi)
  1583. DEALLOCATE (proj)
  1584. DEALLOCATE (proj_aux)
  1585. !
  1586. RETURN
  1587. END SUBROUTINE partialdos
  1588. !
  1589. !-----------------------------------------------------------------------
  1590. SUBROUTINE partialdos_nc (Emin, Emax, DeltaE, kresolveddos, filpdos)
  1591. !-----------------------------------------------------------------------
  1592. !
  1593. USE io_global, ONLY : stdout
  1594. USE basis, ONLY : natomwfc
  1595. USE ions_base, ONLY : ityp, atm
  1596. USE klist, ONLY: wk, nkstot, degauss, ngauss, lgauss
  1597. USE lsda_mod, ONLY: nspin
  1598. USE wvfct, ONLY: et, nbnd
  1599. USE constants, ONLY: rytoev
  1600. !
  1601. USE spin_orb, ONLY: lspinorb
  1602. USE projections_nc
  1603. !
  1604. IMPLICIT NONE
  1605. CHARACTER (len=256) :: filpdos
  1606. REAL(DP) :: Emin, Emax, DeltaE
  1607. LOGICAL :: kresolveddos
  1608. !
  1609. CHARACTER (len=33) :: filextension
  1610. CHARACTER (len=256):: fileout
  1611. CHARACTER (len=1) :: l_label(0:3)=(/'s','p','d','f'/)
  1612. !
  1613. INTEGER :: ik, ibnd, ind, m, &
  1614. c_tab, nwfc, ne, ie_mid, ie_delta, ie, is, nkseff, ikeff, nspin0
  1615. REAL(DP) :: etev, delta, Elw, Eup, wkeff, fact(2), spinor
  1616. REAL(DP), ALLOCATABLE :: dostot(:,:), pdos(:,:,:,:), pdostot(:,:,:), &
  1617. ldos(:,:,:)
  1618. REAL(DP), EXTERNAL :: w0gauss
  1619. !
  1620. !
  1621. ! find band extrema
  1622. !
  1623. Elw = et (1, 1)
  1624. Eup = et (nbnd, 1)
  1625. DO ik = 2, nkstot
  1626. Elw = min (Elw, et (1, ik) )
  1627. Eup = max (Eup, et (nbnd, ik) )
  1628. ENDDO
  1629. IF (degauss/=0.d0) THEN
  1630. Eup = Eup + 3d0 * degauss
  1631. Elw = Elw - 3d0 * degauss
  1632. ENDIF
  1633. Emin = max (Emin/rytoev, Elw)
  1634. Emax = min (Emax/rytoev, Eup)
  1635. DeltaE = DeltaE/rytoev
  1636. ne = nint ( (Emax - Emin) / DeltaE+0.500001d0)
  1637. !
  1638. IF (lspinorb) THEN
  1639. nspin0 = 1
  1640. ELSE
  1641. nspin0 = 2
  1642. ENDIF
  1643. !
  1644. IF (kresolveddos) THEN
  1645. nkseff=nkstot
  1646. ELSE
  1647. nkseff=1
  1648. ENDIF
  1649. !
  1650. ALLOCATE (pdos(0:ne,natomwfc,nspin0,nkseff))
  1651. ALLOCATE (dostot(0:ne,nkseff), pdostot(0:ne,nspin0,nkseff), ldos(0:ne,nspin0,nkseff) )
  1652. pdos(:,:,:,:) = 0.d0
  1653. dostot(:,:) = 0.d0
  1654. pdostot(:,:,:)= 0.d0
  1655. ie_delta = 5 * degauss / DeltaE + 1
  1656. DO ik = 1,nkstot
  1657. !
  1658. IF (kresolveddos) THEN
  1659. ! set equal weight to all k-points
  1660. wkeff=1.D0
  1661. ikeff=ik
  1662. ELSE
  1663. wkeff=wk(ik)
  1664. ! contributions from all k-points are summed in pdos(:,:,:,ikeff)
  1665. ikeff=1
  1666. ENDIF
  1667. !
  1668. DO ibnd = 1, nbnd
  1669. etev = et(ibnd,ik)
  1670. ie_mid = nint( (etev-Emin)/DeltaE )
  1671. DO ie = max(ie_mid-ie_delta, 0), min(ie_mid+ie_delta, ne)
  1672. delta = w0gauss((Emin+DeltaE*ie-etev)/degauss,ngauss) &
  1673. / degauss / rytoev
  1674. !
  1675. ! pdos(:,nwfc,ns,ik) = DOS (states/eV) for spin "ns"
  1676. ! projected over atomic wfc "nwfc"
  1677. ! for k-point "ik" (or summed over all kp)
  1678. !
  1679. !
  1680. ! dostot(:,ik) = total DOS (states/eV)
  1681. ! for k-point "ik" (or summed over all kp)
  1682. !
  1683. IF (lspinorb) THEN
  1684. DO nwfc = 1, natomwfc
  1685. pdos(ie,nwfc,1,ikeff) = pdos(ie,nwfc,1,ikeff) + &
  1686. wkeff * delta * proj (nwfc, ibnd, ik)
  1687. ENDDO
  1688. dostot(ie,ikeff) = dostot(ie,ikeff) + wkeff * delta
  1689. ELSE
  1690. DO nwfc = 1, natomwfc
  1691. IF ( nlmchi(nwfc)%ind<=(2* nlmchi(nwfc)%l+1)) THEN
  1692. pdos(ie,nwfc,1,ikeff) = pdos(ie,nwfc,1,ikeff) + &
  1693. wkeff * delta * proj (nwfc, ibnd, ik)
  1694. pdos(ie,nwfc,2,ikeff) = 0.d0
  1695. ELSE
  1696. pdos(ie,nwfc,1,ikeff) = 0.d0
  1697. pdos(ie,nwfc,2,ikeff) = pdos(ie,nwfc,2,ikeff) + &
  1698. wkeff * delta * proj (nwfc, ibnd, ik)
  1699. ENDIF
  1700. ENDDO
  1701. dostot(ie,ikeff) = dostot(ie,ikeff) + wkeff * delta
  1702. ENDIF
  1703. ENDDO
  1704. ENDDO
  1705. ENDDO
  1706. !
  1707. ! pdostot(:,ns,ik) = sum of all projected DOS
  1708. !
  1709. DO ik=1,nkseff
  1710. DO is=1,nspin0
  1711. DO ie=0,ne
  1712. pdostot(ie,is,ik) = sum(pdos(ie,:,is,ik))
  1713. ENDDO
  1714. ENDDO
  1715. ENDDO
  1716. DO nwfc = 1, natomwfc
  1717. IF (nlmchi(nwfc)%ind == 1) THEN
  1718. filextension='.pdos_atm#'
  1719. ! 12345678901
  1720. c_tab = 11
  1721. IF (nlmchi(nwfc)%na < 10) THEN
  1722. WRITE (filextension( c_tab : c_tab ),'(i1)') nlmchi(nwfc)%na
  1723. c_tab = c_tab + 1
  1724. ELSEIF (nlmchi(nwfc)%na < 100) THEN
  1725. WRITE (filextension( c_tab : c_tab+1 ),'(i2)') nlmchi(nwfc)%na
  1726. c_tab = c_tab + 2
  1727. ELSEIF (nlmchi(nwfc)%na < 1000) THEN
  1728. WRITE (filextension( c_tab : c_tab+2 ),'(i3)') nlmchi(nwfc)%na
  1729. c_tab = c_tab + 3
  1730. ELSEIF (nlmchi(nwfc)%na < 10000) THEN
  1731. WRITE (filextension( c_tab : c_tab+3 ),'(i4)') nlmchi(nwfc)%na
  1732. c_tab = c_tab + 4
  1733. ELSE
  1734. CALL errore('partialdos_nc',&
  1735. 'file extension not supporting so many atoms', nwfc)
  1736. ENDIF
  1737. WRITE (filextension(c_tab:c_tab+4),'(a1,a)') &
  1738. '(',trim(atm(ityp(nlmchi(nwfc)%na)))
  1739. c_tab = c_tab + len_trim(atm(ityp(nlmchi(nwfc)%na))) + 1
  1740. IF (nlmchi(nwfc)%n >= 10) &
  1741. CALL errore('partialdos_nc',&
  1742. 'file extension not supporting so many atomic wfc', nwfc)
  1743. IF (nlmchi(nwfc)%l > 3) &
  1744. CALL errore('partialdos_nc',&
  1745. 'file extension not supporting so many l', nwfc)
  1746. IF (lspinorb) THEN
  1747. WRITE (filextension(c_tab:),'(")_wfc#",i1,"(",a1,"_j",f3.1,")")') &
  1748. nlmchi(nwfc)%n, l_label(nlmchi(nwfc)%l),nlmchi(nwfc)%jj
  1749. ELSE
  1750. WRITE (filextension(c_tab:),'(")_wfc#",i1,"(",a1,")")') &
  1751. nlmchi(nwfc)%n, l_label(nlmchi(nwfc)%l)
  1752. ENDIF
  1753. fileout = trim(filpdos)//trim(filextension)
  1754. OPEN (4,file=fileout,form='formatted', &
  1755. status='unknown')
  1756. IF (kresolveddos) THEN
  1757. WRITE (4,'("# ik ",$)')
  1758. ELSE
  1759. WRITE (4,'("#",$)')
  1760. ENDIF
  1761. IF (nspin0 == 1) THEN
  1762. WRITE (4,'(" E(eV) ldos(E) ",$)')
  1763. ELSE
  1764. WRITE (4,'(" E(eV) ldosup(E) ldosdw(E)",$)')
  1765. ENDIF
  1766. IF (lspinorb) THEN
  1767. ind = 0
  1768. DO m = -nlmchi(nwfc)%l-1, nlmchi(nwfc)%l
  1769. fact(1) = spinor(nlmchi(nwfc)%l,nlmchi(nwfc)%jj,m,1)
  1770. fact(2) = spinor(nlmchi(nwfc)%l,nlmchi(nwfc)%jj,m,2)
  1771. IF (abs(fact(1))>1.d-8.or.abs(fact(2))>1.d-8) THEN
  1772. ind = ind + 1
  1773. WRITE(4,'("pdos(E)_",i1," ",$)') ind
  1774. ENDIF
  1775. ENDDO
  1776. ELSE
  1777. DO ind=1,2 * nlmchi(nwfc)%l + 1
  1778. WRITE(4,'(" pdosup(E) ",$)')
  1779. WRITE(4,'(" pdosdw(E) ",$)')
  1780. ENDDO
  1781. ENDIF
  1782. WRITE(4,*)
  1783. !
  1784. ! ldos = PDOS summed over m (m=-l:+l)
  1785. !
  1786. ldos (:,:,:) = 0.d0
  1787. IF (lspinorb) THEN
  1788. DO ik=1,nkseff
  1789. DO ie= 0, ne
  1790. IF (abs(nlmchi(nwfc)%jj-nlmchi(nwfc)%l-0.5d0)<1.d-8) THEN
  1791. DO ind = 1, 2 * nlmchi(nwfc)%l + 2
  1792. ldos (ie, 1, ik) = ldos (ie, 1, ik) + pdos(ie,nwfc+ind-1,1,ik)
  1793. ENDDO
  1794. ELSEIF (abs(nlmchi(nwfc)%jj-nlmchi(nwfc)%l+0.5d0)<1.d-8) THEN
  1795. DO ind = 1, 2 * nlmchi(nwfc)%l
  1796. ldos (ie, 1, ik) = ldos (ie, 1, ik) + pdos(ie,nwfc+ind-1,1,ik)
  1797. ENDDO
  1798. ENDIF
  1799. ENDDO
  1800. ENDDO
  1801. DO ik=1,nkseff
  1802. DO ie= 0, ne
  1803. IF (kresolveddos) THEN
  1804. WRITE (4,'(i5," ",$)') ik
  1805. ENDIF
  1806. etev = Emin + ie * DeltaE
  1807. IF (abs(nlmchi(nwfc)%jj-nlmchi(nwfc)%l-0.5d0)<1.d-8) THEN
  1808. WRITE (4,'(f7.3,2e11.3,14e11.3)') etev*rytoev, ldos(ie,1,ik), &
  1809. (pdos(ie,nwfc+ind-1,1,ik), ind=1,2*nlmchi(nwfc)%l+2)
  1810. ELSEIF (abs(nlmchi(nwfc)%jj-nlmchi(nwfc)%l+0.5d0)<1.d-8) THEN
  1811. WRITE (4,'(f7.3,2e11.3,14e11.3)') etev*rytoev, ldos(ie,1,ik), &
  1812. (pdos(ie,nwfc+ind-1,1,ik), ind=1,2*nlmchi(nwfc)%l)
  1813. ENDIF
  1814. ENDDO
  1815. IF (kresolveddos) WRITE (4,*)
  1816. ENDDO
  1817. ELSE
  1818. DO ik=1,nkseff
  1819. DO ie= 0, ne
  1820. DO is=1, nspin0
  1821. DO ind=1,4 * nlmchi(nwfc)%l + 2
  1822. ldos (ie, is, ik) = ldos (ie, is, ik) + pdos(ie,nwfc+ind-1,is, ik)
  1823. ENDDO
  1824. ENDDO
  1825. ENDDO
  1826. ENDDO
  1827. DO ik=1,nkseff
  1828. DO ie= 0, ne
  1829. IF (kresolveddos) THEN
  1830. WRITE (4,'(i5," ",$)') ik
  1831. ENDIF
  1832. etev = Emin + ie * DeltaE
  1833. WRITE (4,'(f7.3,2e11.3,14e11.3)') etev*rytoev, &
  1834. (ldos(ie,is,ik), is=1,nspin0), &
  1835. ((pdos(ie,nwfc+ind-1+(is-1)*(2*nlmchi(nwfc)%l+1),is,ik), is=1,nspin0), &
  1836. ind=1,2*nlmchi(nwfc)%l+1)
  1837. ENDDO
  1838. IF (kresolveddos) WRITE (4,*)
  1839. ENDDO
  1840. ENDIF
  1841. CLOSE (4)
  1842. ENDIF
  1843. ENDDO
  1844. fileout = trim(filpdos)//".pdos_tot"
  1845. OPEN (4,file=fileout,form='formatted', status='unknown')
  1846. IF (kresolveddos) THEN
  1847. WRITE (4,'("# ik ",$)')
  1848. ELSE
  1849. WRITE (4,'("#",$)')
  1850. ENDIF
  1851. IF (nspin0 == 1) THEN
  1852. WRITE (4,'(" E (eV) dos(E) pdos(E)")')
  1853. ELSE
  1854. WRITE (4,'(" E (eV) dos(E) pdosup(E) pdosdw(E)")')
  1855. ENDIF
  1856. DO ik=1,nkseff
  1857. DO ie= 0, ne
  1858. IF (kresolveddos) THEN
  1859. WRITE (4,'(i5," ",$)') ik
  1860. ENDIF
  1861. etev = Emin + ie * DeltaE
  1862. WRITE (4,'(f7.3,4e11.3)') etev*rytoev, dostot(ie,ik), &
  1863. (pdostot(ie,is,ik), is=1,nspin0)
  1864. ENDDO
  1865. IF (kresolveddos) WRITE (4,*)
  1866. ENDDO
  1867. CLOSE (4)
  1868. DEALLOCATE (ldos, dostot, pdostot)
  1869. DEALLOCATE (pdos)
  1870. !
  1871. DEALLOCATE (nlmchi)
  1872. DEALLOCATE (proj)
  1873. DEALLOCATE (proj_aux)
  1874. !
  1875. RETURN
  1876. END SUBROUTINE partialdos_nc
  1877. !
  1878. !-----------------------------------------------------------------------
  1879. SUBROUTINE write_io_header(filplot, iunplot, title, nr1x, nr2x, nr3x, &
  1880. nr1, nr2, nr3, nat, ntyp, ibrav, celldm, at, gcutm, dual, ecutwfc, &
  1881. nkstot,nbnd,natomwfc)
  1882. !-----------------------------------------------------------------------
  1883. USE kinds, ONLY: DP
  1884. USE ions_base, ONLY : zv, atm, tau, ityp
  1885. USE noncollin_module, ONLY: noncolin
  1886. USE spin_orb, ONLY: lspinorb
  1887. IMPLICIT NONE
  1888. CHARACTER (len=*) :: filplot
  1889. CHARACTER (len=*) :: title
  1890. INTEGER :: nr1x, nr2x, nr3x, nr1, nr2, nr3, nat, ntyp, ibrav
  1891. REAL(DP) :: celldm (6), gcutm, dual, ecutwfc, at(3,3)
  1892. INTEGER :: iunplot, ios, na, nt, i
  1893. INTEGER :: nkstot,nbnd,natomwfc
  1894. !
  1895. IF (filplot == ' ') CALL errore ('write_io_h', 'filename missing', 1)
  1896. OPEN (UNIT = iunplot, FILE = filplot, FORM = 'formatted', &
  1897. STATUS = 'unknown', ERR = 101, IOSTAT = ios)
  1898. 101 CALL errore ('write_io_h', 'opening file '//trim(filplot), abs (ios) )
  1899. WRITE (iunplot, '(a)') title
  1900. WRITE (iunplot, '(8i8)') nr1x, nr2x, nr3x, nr1, nr2, nr3, nat, ntyp
  1901. WRITE (iunplot, '(i6,6f12.8)') ibrav, celldm
  1902. IF (ibrav == 0) THEN
  1903. WRITE ( iunplot, * ) at(:,1)
  1904. WRITE ( iunplot, * ) at(:,2)
  1905. WRITE ( iunplot, * ) at(:,3)
  1906. ENDIF
  1907. WRITE (iunplot, '(3f20.10,i6)') gcutm, dual, ecutwfc, 9
  1908. WRITE (iunplot, '(i4,3x,a2,3x,f5.2)') &
  1909. (nt, atm (nt), zv (nt), nt=1, ntyp)
  1910. WRITE (iunplot, '(i4,3x,3f15.9,3x,i2)') (na, &
  1911. (tau (i, na), i = 1, 3), ityp (na), na = 1, nat)
  1912. WRITE (iunplot, '(3i8)') natomwfc, nkstot, nbnd
  1913. WRITE (iunplot, '(2l5)') noncolin, lspinorb
  1914. RETURN
  1915. END SUBROUTINE write_io_header
  1916. !
  1917. !-----------------------------------------------------------------------
  1918. FUNCTION compute_mj(j,l,m)
  1919. !-----------------------------------------------------------------------
  1920. USE kinds, ONLY: DP
  1921. IMPLICIT NONE
  1922. !
  1923. REAL(DP) :: compute_mj, j
  1924. INTEGER :: l, m
  1925. IF (abs(j-l-0.5d0)<1.d-4) THEN
  1926. compute_mj=m+0.5d0
  1927. ELSEIF (abs(j-l+0.5d0)<1.d-4) THEN
  1928. compute_mj=m-0.5d0
  1929. ELSE
  1930. CALL errore('compute_mj','l and j not compatible',1)
  1931. ENDIF
  1932. RETURN
  1933. END FUNCTION compute_mj
  1934. !
  1935. !-----------------------------------------------------------------------
  1936. SUBROUTINE write_proj (filename, projs)
  1937. !-----------------------------------------------------------------------
  1938. !
  1939. USE kinds
  1940. USE io_files, ONLY : iun => iunsat, prefix, tmp_dir
  1941. USE basis, ONLY : natomwfc
  1942. USE cell_base
  1943. USE klist, ONLY : wk, xk, nkstot, nelec
  1944. USE noncollin_module, ONLY : noncolin
  1945. USE lsda_mod, ONLY : nspin, isk
  1946. USE ener, ONLY : ef
  1947. USE wvfct, ONLY : et, nbnd
  1948. USE iotk_module
  1949. IMPLICIT NONE
  1950. CHARACTER(*), INTENT(in) :: filename
  1951. COMPLEX(DP), INTENT(in) :: projs(natomwfc,nbnd,nkstot)
  1952. !
  1953. CHARACTER(256) :: tmp
  1954. CHARACTER(iotk_attlenx) :: attr
  1955. INTEGER :: ik, ik_eff, ia, ierr, num_k_points
  1956. !
  1957. ! subroutine body
  1958. !
  1959. tmp = trim( tmp_dir ) // trim( prefix ) // '.save/' //trim(filename)
  1960. !
  1961. CALL iotk_open_write(iun, FILE=trim(tmp), ROOT="ATOMIC_PROJECTIONS", IERR=ierr )
  1962. IF ( ierr /= 0 ) RETURN
  1963. !
  1964. !
  1965. num_k_points = nkstot
  1966. IF ( nspin == 2 ) num_k_points = nkstot / 2
  1967. !
  1968. CALL iotk_write_begin(iun, "HEADER")
  1969. !
  1970. CALL iotk_write_dat(iun, "NUMBER_OF_BANDS", nbnd)
  1971. CALL iotk_write_dat(iun, "NUMBER_OF_K-POINTS", num_k_points )
  1972. CALL iotk_write_dat(iun, "NUMBER_OF_SPIN_COMPONENTS", nspin)
  1973. CALL iotk_write_dat(iun, "NON-COLINEAR_CALCULATION",noncolin)
  1974. CALL iotk_write_dat(iun, "NUMBER_OF_ATOMIC_WFC", natomwfc)
  1975. CALL iotk_write_dat(iun, "NUMBER_OF_ELECTRONS", nelec )
  1976. CALL iotk_write_attr(attr, "UNITS", " 2 pi / a", FIRST=.true. )
  1977. CALL iotk_write_empty (iun, "UNITS_FOR_K-POINTS", ATTR=attr)
  1978. CALL iotk_write_attr(attr, "UNITS", "Rydberg", FIRST=.true. )
  1979. CALL iotk_write_empty (iun, "UNITS_FOR_ENERGY", ATTR=attr)
  1980. CALL iotk_write_dat(iun, "FERMI_ENERGY", ef )
  1981. !
  1982. CALL iotk_write_end(iun, "HEADER")
  1983. !
  1984. !
  1985. CALL iotk_write_dat(iun, "K-POINTS", xk(:,1:num_k_points) , COLUMNS=3 )
  1986. CALL iotk_write_dat(iun, "WEIGHT_OF_K-POINTS", wk(1:num_k_points), COLUMNS=8 )
  1987. !
  1988. CALL iotk_write_begin(iun, "EIGENVALUES")
  1989. !
  1990. DO ik=1,num_k_points
  1991. !
  1992. CALL iotk_write_begin( iun, "K-POINT"//trim(iotk_index(ik)) )
  1993. !
  1994. IF ( nspin == 2 ) THEN
  1995. !
  1996. ik_eff = ik + num_k_points
  1997. !
  1998. CALL iotk_write_dat( iun, "EIG.1", et(:,ik) )
  1999. CALL iotk_write_dat( iun, "EIG.2", et(:,ik_eff) )
  2000. !
  2001. ELSE
  2002. !
  2003. CALL iotk_write_dat( iun, "EIG", et(:,ik) )
  2004. !
  2005. ENDIF
  2006. !
  2007. CALL iotk_write_end( iun, "K-POINT"//trim(iotk_index(ik)) )
  2008. !
  2009. ENDDO
  2010. !
  2011. CALL iotk_write_end(iun, "EIGENVALUES")
  2012. !
  2013. ! main loop atomic wfc
  2014. !
  2015. CALL iotk_write_begin(iun, "PROJECTIONS")
  2016. !
  2017. DO ik=1,num_k_points
  2018. !
  2019. CALL iotk_write_begin( iun, "K-POINT"//trim(iotk_index(ik)) )
  2020. !
  2021. IF ( nspin == 2 ) THEN
  2022. !
  2023. CALL iotk_write_begin ( iun, "SPIN.1" )
  2024. !
  2025. DO ia = 1, natomwfc
  2026. CALL iotk_write_dat(iun, "ATMWFC"//trim(iotk_index(ia)), projs(ia,:,ik) )
  2027. ENDDO
  2028. !
  2029. CALL iotk_write_end ( iun, "SPIN.1" )
  2030. !
  2031. ik_eff = ik + num_k_points
  2032. !
  2033. CALL iotk_write_begin ( iun, "SPIN.2" )
  2034. !
  2035. DO ia = 1, natomwfc
  2036. CALL iotk_write_dat(iun, "ATMWFC"//trim(iotk_index(ia)), projs(ia,:,ik_eff) )
  2037. ENDDO
  2038. !
  2039. CALL iotk_write_end ( iun, "SPIN.2" )
  2040. !
  2041. ELSE
  2042. !
  2043. DO ia = 1,natomwfc
  2044. CALL iotk_write_dat(iun, "ATMWFC"//trim(iotk_index(ia)), projs(ia,:,ik) )
  2045. ENDDO
  2046. !
  2047. ENDIF
  2048. !
  2049. CALL iotk_write_end( iun, "K-POINT"//trim(iotk_index(ik)) )
  2050. !
  2051. ENDDO
  2052. !
  2053. CALL iotk_write_end(iun, "PROJECTIONS")
  2054. !
  2055. ! closing the file
  2056. !
  2057. CALL iotk_close_write(iun)
  2058. END SUBROUTINE write_proj
  2059. !
  2060. ! projwave with distributed matrixes
  2061. !
  2062. !-----------------------------------------------------------------------
  2063. SUBROUTINE pprojwave( filproj, lsym )
  2064. !-----------------------------------------------------------------------
  2065. !
  2066. USE io_global, ONLY : stdout, ionode
  2067. USE printout_base, ONLY: title
  2068. USE ions_base, ONLY : zv, tau, nat, ntyp => nsp, ityp, atm
  2069. USE basis, ONLY : natomwfc
  2070. USE cell_base
  2071. USE constants, ONLY: rytoev, eps4
  2072. USE gvect
  2073. USE grid_dimensions, ONLY : nr1, nr2, nr3, nr1x, nr2x, nr3x
  2074. USE klist, ONLY: xk, nks, nkstot, nelec
  2075. USE ldaU
  2076. USE lsda_mod, ONLY: nspin, isk, current_spin
  2077. USE symm_base, ONLY: nsym, irt, d1, d2, d3
  2078. USE wvfct
  2079. USE control_flags, ONLY: gamma_only
  2080. USE uspp, ONLY: nkb, vkb
  2081. USE uspp_param, ONLY: upf
  2082. USE becmod, ONLY: bec_type, becp, calbec, allocate_bec_type, deallocate_bec_type
  2083. USE io_files, ONLY: nd_nmbr, prefix, tmp_dir, nwordwfc, iunwfc, find_free_unit
  2084. USE spin_orb, ONLY: lspinorb
  2085. USE mp, ONLY: mp_bcast
  2086. USE mp_global, ONLY : npool, nproc_pool, me_pool, root_pool, &
  2087. intra_pool_comm, me_image, &
  2088. ortho_comm, np_ortho, me_ortho, ortho_comm_id, &
  2089. leg_ortho, mpime
  2090. USE wavefunctions_module, ONLY: evc
  2091. USE parallel_toolkit, ONLY : zsqmred, zsqmher, zsqmdst, zsqmcll, dsqmsym
  2092. USE zhpev_module, ONLY : pzhpev_drv, zhpev_drv
  2093. USE descriptors
  2094. USE projections
  2095. !
  2096. IMPLICIT NONE
  2097. !
  2098. COMPLEX(DP), PARAMETER :: zero = ( 0.0d0, 0.0d0 )
  2099. COMPLEX(DP), PARAMETER :: one = ( 1.0d0, 0.0d0 )
  2100. CHARACTER (len=*) :: filproj
  2101. INTEGER :: ik, ibnd, i, j, na, nb, nt, isym, n, m, m1, l, nwfc,&
  2102. nwfc1, lmax_wfc, is, iunproj, iunaux
  2103. REAL(DP), ALLOCATABLE :: e (:)
  2104. COMPLEX(DP), ALLOCATABLE :: wfcatom (:,:)
  2105. COMPLEX(DP), ALLOCATABLE :: work1(:), proj0(:,:)
  2106. COMPLEX(DP), ALLOCATABLE :: overlap_d(:,:), work_d(:,:), diag(:,:), vv(:,:)
  2107. COMPLEX(DP), ALLOCATABLE :: e_work_d(:,:)
  2108. ! Some workspace for k-point calculation ...
  2109. REAL (DP), ALLOCATABLE ::rwork1(:),rproj0(:,:)
  2110. REAL (DP), ALLOCATABLE ::roverlap_d(:,:)
  2111. ! ... or for gamma-point.
  2112. REAL(DP), ALLOCATABLE :: charges(:,:,:), proj1 (:)
  2113. REAL(DP) :: psum, totcharge(2)
  2114. INTEGER :: nksinit, nkslast
  2115. CHARACTER(len=256) :: filename
  2116. CHARACTER(len=256) :: auxname
  2117. CHARACTER (len=1) :: l_label(0:3)=(/'s','p','d','f'/)
  2118. INTEGER, ALLOCATABLE :: idx(:)
  2119. LOGICAL :: lsym
  2120. INTEGER :: desc( descla_siz_ )
  2121. INTEGER, ALLOCATABLE :: desc_ip( :, :, : )
  2122. INTEGER, ALLOCATABLE :: rank_ip( :, : )
  2123. ! matrix distribution descriptors
  2124. INTEGER :: nx, nrl, nrlx
  2125. ! maximum local block dimension
  2126. LOGICAL :: la_proc
  2127. ! flag to distinguish procs involved in linear algebra
  2128. INTEGER, ALLOCATABLE :: notcnv_ip( : )
  2129. INTEGER, ALLOCATABLE :: ic_notcnv( : )
  2130. !
  2131. !
  2132. WRITE( stdout, '(/5x,"Calling pprojwave .... ")')
  2133. IF ( gamma_only ) THEN
  2134. WRITE( stdout, '(5x,"gamma-point specific algorithms are used")')
  2135. ENDIF
  2136. !
  2137. ! Open file as temporary storage
  2138. !
  2139. iunaux = find_free_unit()
  2140. WRITE( auxname, fmt='(I6.1)' ) mpime
  2141. auxname = TRIM( tmp_dir ) // 'AUX' // TRIM( ADJUSTL( auxname ) )
  2142. OPEN( unit=iunaux, file=trim(auxname), status='unknown', form='unformatted')
  2143. !
  2144. !
  2145. ALLOCATE( ic_notcnv( np_ortho(2) ) )
  2146. ALLOCATE( notcnv_ip( np_ortho(2) ) )
  2147. ALLOCATE( desc_ip( descla_siz_ , np_ortho(1), np_ortho(2) ) )
  2148. ALLOCATE( rank_ip( np_ortho(1), np_ortho(2) ) )
  2149. !
  2150. CALL desc_init( natomwfc, desc, desc_ip )
  2151. !
  2152. ! initialize D_Sl for l=1, l=2 and l=3, for l=0 D_S0 is 1
  2153. !
  2154. CALL d_matrix (d1, d2, d3)
  2155. !
  2156. ! fill structure nlmchi
  2157. !
  2158. ALLOCATE (nlmchi(natomwfc))
  2159. nwfc=0
  2160. lmax_wfc = 0
  2161. DO na = 1, nat
  2162. nt = ityp (na)
  2163. DO n = 1, upf(nt)%nwfc
  2164. IF (upf(nt)%oc (n) >= 0.d0) THEN
  2165. l = upf(nt)%lchi (n)
  2166. lmax_wfc = max (lmax_wfc, l )
  2167. DO m = 1, 2 * l + 1
  2168. nwfc=nwfc+1
  2169. nlmchi(nwfc)%na = na
  2170. nlmchi(nwfc)%n = n
  2171. nlmchi(nwfc)%l = l
  2172. nlmchi(nwfc)%m = m
  2173. ENDDO
  2174. ENDIF
  2175. ENDDO
  2176. ENDDO
  2177. !
  2178. IF (lmax_wfc > 3) CALL errore ('projwave', 'l > 3 not yet implemented', 1)
  2179. IF (nwfc /= natomwfc) CALL errore ('projwave', 'wrong # of atomic wfcs?', 1)
  2180. !
  2181. !
  2182. IF( ionode ) THEN
  2183. WRITE( stdout, * )
  2184. WRITE( stdout, * ) ' Problem Sizes '
  2185. WRITE( stdout, * ) ' natomwfc = ', natomwfc
  2186. WRITE( stdout, * ) ' nbnd = ', nbnd
  2187. WRITE( stdout, * ) ' nkstot = ', nkstot
  2188. WRITE( stdout, * ) ' npwx = ', npwx
  2189. WRITE( stdout, * ) ' nkb = ', nkb
  2190. WRITE( stdout, * )
  2191. ENDIF
  2192. !
  2193. ALLOCATE( proj (natomwfc, nbnd, nkstot) )
  2194. proj = 0.d0
  2195. !
  2196. IF (.not. lda_plus_u) ALLOCATE(swfcatom (npwx , natomwfc ) )
  2197. ALLOCATE(wfcatom (npwx, natomwfc) )
  2198. !
  2199. ALLOCATE(e (natomwfc) )
  2200. !
  2201. ! loop on k points
  2202. !
  2203. CALL init_us_1
  2204. CALL init_at_1
  2205. !
  2206. DO ik = 1, nks
  2207. !
  2208. CALL gk_sort (xk (1, ik), ngm, g, ecutwfc / tpiba2, npw, igk, g2kin)
  2209. CALL davcio (evc, nwordwfc, iunwfc, ik, - 1)
  2210. CALL atomic_wfc (ik, wfcatom)
  2211. CALL init_us_2 (npw, igk, xk (1, ik), vkb)
  2212. CALL allocate_bec_type ( nkb, natomwfc, becp )
  2213. CALL calbec ( npw, vkb, wfcatom, becp)
  2214. CALL s_psi (npwx, npw, natomwfc, wfcatom, swfcatom)
  2215. CALL deallocate_bec_type (becp)
  2216. !
  2217. ! wfcatom = |phi_i> , swfcatom = \hat S |phi_i>
  2218. ! calculate overlap matrix O_ij = <phi_i|\hat S|\phi_j>
  2219. !
  2220. IF( la_proc ) THEN
  2221. ALLOCATE(overlap_d (nx, nx) )
  2222. ELSE
  2223. ALLOCATE(overlap_d (1, 1) )
  2224. ENDIF
  2225. overlap_d = (0.d0,0.d0)
  2226. IF ( gamma_only ) THEN
  2227. IF( la_proc ) THEN
  2228. ALLOCATE(roverlap_d (nx, nx) )
  2229. ELSE
  2230. ALLOCATE(roverlap_d (1, 1) )
  2231. ENDIF
  2232. roverlap_d = 0.d0
  2233. CALL calbec_ddistmat( npw, wfcatom, swfcatom, natomwfc, nx, roverlap_d )
  2234. overlap_d(:,:)=cmplx(roverlap_d(:,:),0.0_dp, kind=dp)
  2235. ! TEMP: diagonalization routine for real matrix should be used instead
  2236. ELSE
  2237. CALL calbec_zdistmat( npw, wfcatom, swfcatom, natomwfc, nx, overlap_d )
  2238. ENDIF
  2239. !
  2240. ! calculate O^{-1/2}
  2241. !
  2242. IF ( desc( lambda_node_ ) > 0 ) THEN
  2243. !
  2244. ! Compute local dimension of the cyclically distributed matrix
  2245. !
  2246. ALLOCATE(work_d (nx, nx) )
  2247. nrl = desc( la_nrl_ )
  2248. nrlx = desc( la_nrlx_ )
  2249. ALLOCATE( diag( nrlx, natomwfc ) )
  2250. ALLOCATE( vv( nrlx, natomwfc ) )
  2251. !
  2252. CALL blk2cyc_zredist( natomwfc, diag, nrlx, natomwfc, overlap_d, nx, nx, desc )
  2253. !
  2254. CALL pzhpev_drv( 'V', diag, nrlx, e, vv, nrlx, nrl, natomwfc, &
  2255. desc( la_npc_ ) * desc( la_npr_ ), desc( la_me_ ), desc( la_comm_ ) )
  2256. !
  2257. CALL cyc2blk_zredist( natomwfc, vv, nrlx, natomwfc, work_d, nx, nx, desc )
  2258. !
  2259. DEALLOCATE( vv )
  2260. DEALLOCATE( diag )
  2261. !
  2262. ELSE
  2263. ALLOCATE(work_d (1, 1) )
  2264. ENDIF
  2265. CALL mp_bcast( e, root_pool, intra_pool_comm )
  2266. DO i = 1, natomwfc
  2267. e (i) = 1.d0 / dsqrt (e (i) )
  2268. ENDDO
  2269. IF ( desc( lambda_node_ ) > 0 ) THEN
  2270. ALLOCATE(e_work_d (nx, nx) )
  2271. DO j = 1, desc( nlac_ )
  2272. DO i = 1, desc( nlar_ )
  2273. e_work_d( i, j ) = e( j + desc( ilac_ ) - 1 ) * work_d( i, j )
  2274. ENDDO
  2275. ENDDO
  2276. CALL sqr_zmm_cannon( 'N', 'C', natomwfc, ONE, e_work_d, nx, work_d, nx, ZERO, overlap_d, nx, desc )
  2277. CALL zsqmher( natomwfc, overlap_d, nx, desc )
  2278. DEALLOCATE( e_work_d )
  2279. ENDIF
  2280. !
  2281. DEALLOCATE( work_d )
  2282. !
  2283. ! calculate wfcatom = O^{-1/2} \hat S | phi>
  2284. !
  2285. IF ( gamma_only ) THEN
  2286. ! TEMP: diagonalization routine for real matrix should be used instead
  2287. roverlap_d(:,:)=REAL(overlap_d(:,:),DP)
  2288. CALL wf_times_roverlap( swfcatom, roverlap_d, wfcatom )
  2289. DEALLOCATE( roverlap_d )
  2290. ELSE
  2291. CALL wf_times_overlap( swfcatom, overlap_d, wfcatom )
  2292. DEALLOCATE( overlap_d )
  2293. ENDIF
  2294. !
  2295. ! make the projection <psi_i| O^{-1/2} \hat S | phi_j>
  2296. !
  2297. IF ( gamma_only ) THEN
  2298. !
  2299. ALLOCATE( rproj0(natomwfc,nbnd), rwork1 (nbnd) )
  2300. CALL calbec ( npw, wfcatom, evc, rproj0)
  2301. !
  2302. WRITE( iunaux ) rproj0
  2303. !
  2304. ELSE
  2305. !
  2306. ALLOCATE( proj0(natomwfc,nbnd), work1 (nbnd) )
  2307. CALL calbec ( npw, wfcatom, evc, proj0)
  2308. !
  2309. WRITE( iunaux ) proj0
  2310. !
  2311. ENDIF
  2312. !
  2313. ! symmetrize the projections
  2314. !
  2315. IF (lsym) THEN
  2316. !
  2317. DO nwfc = 1, natomwfc
  2318. !
  2319. ! atomic wavefunction nwfc is on atom na
  2320. !
  2321. na= nlmchi(nwfc)%na
  2322. n = nlmchi(nwfc)%n
  2323. l = nlmchi(nwfc)%l
  2324. m = nlmchi(nwfc)%m
  2325. !
  2326. DO isym = 1, nsym
  2327. !
  2328. nb = irt (isym, na)
  2329. DO nwfc1 =1, natomwfc
  2330. IF (nlmchi(nwfc1)%na == nb .and. &
  2331. nlmchi(nwfc1)%n == nlmchi(nwfc)%n .and. &
  2332. nlmchi(nwfc1)%l == nlmchi(nwfc)%l .and. &
  2333. nlmchi(nwfc1)%m == 1 ) GOTO 10
  2334. ENDDO
  2335. CALL errore('projwave','cannot symmetrize',1)
  2336. 10 nwfc1=nwfc1-1
  2337. !
  2338. ! nwfc1 is the first rotated atomic wfc corresponding to nwfc
  2339. !
  2340. IF ( gamma_only ) THEN
  2341. IF (l == 0) THEN
  2342. rwork1(:) = rproj0 (nwfc1 + 1,:)
  2343. ELSEIF (l == 1) THEN
  2344. rwork1(:) = 0.d0
  2345. DO m1 = 1, 3
  2346. rwork1(:)=rwork1(:)+d1(m1,m,isym)*rproj0(nwfc1+m1,:)
  2347. ENDDO
  2348. ELSEIF (l == 2) THEN
  2349. rwork1(:) = 0.d0
  2350. DO m1 = 1, 5
  2351. rwork1(:)=rwork1(:)+d2(m1,m,isym)*rproj0(nwfc1+m1,:)
  2352. ENDDO
  2353. ELSEIF (l == 3) THEN
  2354. rwork1(:) = 0.d0
  2355. DO m1 = 1, 7
  2356. rwork1(:)=rwork1(:)+d3(m1,m,isym)*rproj0(nwfc1+m1,:)
  2357. ENDDO
  2358. ENDIF
  2359. DO ibnd = 1, nbnd
  2360. proj (nwfc, ibnd, ik) = proj (nwfc, ibnd, ik) + &
  2361. rwork1(ibnd) * rwork1(ibnd) / nsym
  2362. ENDDO
  2363. ELSE
  2364. IF (l == 0) THEN
  2365. work1(:) = proj0 (nwfc1 + 1,:)
  2366. ELSEIF (l == 1) THEN
  2367. work1(:) = 0.d0
  2368. DO m1 = 1, 3
  2369. work1(:)=work1(:)+d1(m1,m,isym)*proj0(nwfc1+m1,:)
  2370. ENDDO
  2371. ELSEIF (l == 2) THEN
  2372. work1(:) = 0.d0
  2373. DO m1 = 1, 5
  2374. work1(:)=work1(:)+d2(m1,m,isym)*proj0(nwfc1+m1,:)
  2375. ENDDO
  2376. ELSEIF (l == 3) THEN
  2377. work1(:) = 0.d0
  2378. DO m1 = 1, 7
  2379. work1(:)=work1(:)+d3(m1,m,isym)*proj0(nwfc1+m1,:)
  2380. ENDDO
  2381. ENDIF
  2382. DO ibnd = 1, nbnd
  2383. proj (nwfc, ibnd, ik) = proj (nwfc, ibnd, ik) + &
  2384. work1(ibnd) * conjg (work1(ibnd)) / nsym
  2385. ENDDO
  2386. ENDIF
  2387. ENDDO
  2388. ENDDO
  2389. !
  2390. ELSE
  2391. !
  2392. IF ( gamma_only ) THEN
  2393. DO nwfc=1,natomwfc
  2394. DO ibnd=1,nbnd
  2395. proj(nwfc,ibnd,ik)=abs(rproj0(nwfc,ibnd))**2
  2396. ENDDO
  2397. ENDDO
  2398. ELSE
  2399. DO nwfc=1,natomwfc
  2400. DO ibnd=1,nbnd
  2401. proj(nwfc,ibnd,ik)=abs(proj0(nwfc,ibnd))**2
  2402. ENDDO
  2403. ENDDO
  2404. ENDIF
  2405. !
  2406. ENDIF
  2407. !
  2408. IF ( gamma_only ) THEN
  2409. DEALLOCATE (rwork1)
  2410. DEALLOCATE (rproj0)
  2411. ELSE
  2412. DEALLOCATE (work1)
  2413. DEALLOCATE (proj0)
  2414. ENDIF
  2415. !
  2416. ENDDO
  2417. !
  2418. !
  2419. DEALLOCATE (e)
  2420. !
  2421. DEALLOCATE (wfcatom)
  2422. IF (.not. lda_plus_u) DEALLOCATE (swfcatom)
  2423. !
  2424. CLOSE( unit=iunaux )
  2425. !
  2426. !
  2427. ! vectors et and proj are distributed across the pools
  2428. ! collect data for all k-points to the first pool
  2429. !
  2430. CALL poolrecover (et, nbnd, nkstot, nks)
  2431. CALL poolrecover (proj, nbnd * natomwfc, nkstot, nks)
  2432. !
  2433. ! Recover proj_aux
  2434. !
  2435. OPEN( unit=iunaux, file=trim(auxname), status='old', form='unformatted')
  2436. ALLOCATE( proj_aux (natomwfc, nbnd, nkstot) )
  2437. proj_aux = (0.d0, 0.d0)
  2438. DO ik = 1, nks
  2439. !
  2440. IF( gamma_only ) THEN
  2441. ALLOCATE( rproj0( natomwfc, nbnd ) )
  2442. READ( iunaux ) rproj0(:,:)
  2443. proj_aux(:,:,ik) = cmplx( rproj0(:,:), 0.00_dp, kind=dp )
  2444. DEALLOCATE ( rproj0 )
  2445. ELSE
  2446. READ( iunaux ) proj_aux(:,:,ik)
  2447. ENDIF
  2448. !
  2449. ENDDO
  2450. !
  2451. CALL poolrecover (proj_aux, 2 * nbnd * natomwfc, nkstot, nks)
  2452. !
  2453. CLOSE( unit=iunaux, status='delete' )
  2454. !
  2455. IF ( ionode ) THEN
  2456. !
  2457. ! write on the file filproj
  2458. !
  2459. IF (filproj/=' ') THEN
  2460. DO is=1,nspin
  2461. IF (nspin==2) THEN
  2462. IF (is==1) filename=trim(filproj)//'.up'
  2463. IF (is==2) filename=trim(filproj)//'.down'
  2464. nksinit=(nkstot/2)*(is-1)+1
  2465. nkslast=(nkstot/2)*is
  2466. ELSE
  2467. filename=trim(filproj)
  2468. nksinit=1
  2469. nkslast=nkstot
  2470. ENDIF
  2471. iunproj=33
  2472. CALL write_io_header(filename, iunproj, title, nr1x, nr2x, nr3x, &
  2473. nr1, nr2, nr3, nat, ntyp, ibrav, celldm, at, gcutm, dual, &
  2474. ecutwfc, nkstot/nspin,nbnd,natomwfc)
  2475. DO nwfc = 1, natomwfc
  2476. WRITE(iunproj,'(2i5,a3,3i5)') &
  2477. nwfc, nlmchi(nwfc)%na, atm(ityp(nlmchi(nwfc)%na)), &
  2478. nlmchi(nwfc)%n, nlmchi(nwfc)%l, nlmchi(nwfc)%m
  2479. DO ik=nksinit,nkslast
  2480. DO ibnd=1,nbnd
  2481. WRITE(iunproj,'(2i8,f20.10)') ik,ibnd, &
  2482. abs(proj(nwfc,ibnd,ik))
  2483. ENDDO
  2484. ENDDO
  2485. ENDDO
  2486. CLOSE(iunproj)
  2487. ENDDO
  2488. ENDIF
  2489. !
  2490. ! write projections to file using iotk
  2491. !
  2492. CALL write_proj( "atomic_proj.xml", proj_aux )
  2493. !
  2494. ! write on the standard output file
  2495. !
  2496. WRITE( stdout,'(/5x,"Atomic states used for projection")')
  2497. WRITE( stdout,'( 5x,"(read from pseudopotential files):"/)')
  2498. DO nwfc = 1, natomwfc
  2499. WRITE(stdout,1000) &
  2500. nwfc, nlmchi(nwfc)%na, atm(ityp(nlmchi(nwfc)%na)), &
  2501. nlmchi(nwfc)%n, nlmchi(nwfc)%l, nlmchi(nwfc)%m
  2502. ENDDO
  2503. 1000 FORMAT (5x,"state #",i4,": atom ",i3," (",a3,"), wfc ",i2, &
  2504. " (l=",i1," m=",i2,")")
  2505. !
  2506. ALLOCATE(idx(natomwfc), proj1 (natomwfc) )
  2507. !
  2508. DO ik = 1, nkstot
  2509. WRITE( stdout, '(/" k = ",3f14.10)') (xk (i, ik) , i = 1, 3)
  2510. DO ibnd = 1, nbnd
  2511. WRITE( stdout, '(5x,"e = ",f11.5," eV")') et (ibnd, ik) * rytoev
  2512. !
  2513. ! sort projections by magnitude, in decreasing order
  2514. !
  2515. DO nwfc = 1, natomwfc
  2516. idx (nwfc) = 0
  2517. proj1 (nwfc) = - proj (nwfc, ibnd, ik)
  2518. ENDDO
  2519. !
  2520. ! projections differing by less than 1.d-4 are considered equal
  2521. !
  2522. CALL hpsort_eps (natomwfc, proj1, idx, eps4)
  2523. !
  2524. ! only projections that are larger than 0.001 are written
  2525. !
  2526. DO nwfc = 1, natomwfc
  2527. proj1 (nwfc) = - proj1(nwfc)
  2528. IF ( abs (proj1(nwfc)) < 0.001d0 ) GOTO 20
  2529. ENDDO
  2530. nwfc = natomwfc + 1
  2531. 20 nwfc = nwfc -1
  2532. !
  2533. ! fancy (?!?) formatting
  2534. !
  2535. WRITE( stdout, '(5x,"psi = ",5(f5.3,"*[#",i4,"]+"))') &
  2536. (proj1 (i), idx(i), i = 1, min(5,nwfc))
  2537. DO j = 1, (nwfc-1)/5
  2538. WRITE( stdout, '(10x,"+",5(f5.3,"*[#",i4,"]+"))') &
  2539. (proj1 (i), idx(i), i = 5*j+1, min(5*(j+1),nwfc))
  2540. ENDDO
  2541. psum = 0.d0
  2542. DO nwfc = 1, natomwfc
  2543. psum = psum + proj (nwfc, ibnd, ik)
  2544. ENDDO
  2545. WRITE( stdout, '(4x,"|psi|^2 = ",f5.3)') psum
  2546. !
  2547. ENDDO
  2548. ENDDO
  2549. !
  2550. DEALLOCATE (idx, proj1)
  2551. !
  2552. ! estimate partial charges (Loewdin) on each atom
  2553. !
  2554. ALLOCATE ( charges (nat, 0:lmax_wfc, nspin ) )
  2555. charges = 0.0d0
  2556. DO ik = 1, nkstot
  2557. IF ( nspin == 1 ) THEN
  2558. current_spin = 1
  2559. ELSEIF ( nspin == 2 ) THEN
  2560. current_spin = isk ( ik )
  2561. ELSE
  2562. CALL errore ('projwfc_nc',' called in the wrong case ',1)
  2563. ENDIF
  2564. DO ibnd = 1, nbnd
  2565. DO nwfc = 1, natomwfc
  2566. na= nlmchi(nwfc)%na
  2567. l = nlmchi(nwfc)%l
  2568. charges(na,l,current_spin) = charges(na,l,current_spin) + &
  2569. wg (ibnd,ik) * proj (nwfc, ibnd, ik)
  2570. ENDDO
  2571. ENDDO
  2572. ENDDO
  2573. !
  2574. WRITE( stdout, '(/"Lowdin Charges: "/)')
  2575. !
  2576. DO na = 1, nat
  2577. DO is = 1, nspin
  2578. totcharge(is) = sum(charges(na,0:lmax_wfc,is))
  2579. ENDDO
  2580. IF ( nspin == 1) THEN
  2581. WRITE( stdout, 2000) na, totcharge(1), &
  2582. ( l_label(l), charges(na,l,1), l= 0,lmax_wfc)
  2583. ELSEIF ( nspin == 2) THEN
  2584. WRITE( stdout, 2000) na, totcharge(1) + totcharge(2), &
  2585. ( l_label(l), charges(na,l,1) + charges(na,l,2), l=0,lmax_wfc)
  2586. WRITE( stdout, 2001) totcharge(1), &
  2587. ( l_label(l), charges(na,l,1), l= 0,lmax_wfc)
  2588. WRITE( stdout, 2002) totcharge(2), &
  2589. ( l_label(l), charges(na,l,2), l= 0,lmax_wfc)
  2590. WRITE( stdout, 2003) totcharge(1) - totcharge(2), &
  2591. ( l_label(l), charges(na,l,1) - charges(na,l,2), l=0,lmax_wfc)
  2592. ENDIF
  2593. ENDDO
  2594. 2000 FORMAT (5x,"Atom # ",i3,": total charge = ",f8.4,4(", ",a1," =",f8.4))
  2595. 2001 FORMAT (15x," spin up = ",f8.4,4(", ",a1," =",f8.4))
  2596. 2002 FORMAT (15x," spin down = ",f8.4,4(", ",a1," =",f8.4))
  2597. 2003 FORMAT (15x," polarization = ",f8.4,4(", ",a1," =",f8.4))
  2598. !
  2599. psum = sum(charges(:,:,:)) / nelec
  2600. WRITE( stdout, '(5x,"Spilling Parameter: ",f8.4)') 1.0d0 - psum
  2601. !
  2602. ! Sanchez-Portal et al., Sol. State Commun. 95, 685 (1995).
  2603. ! The spilling parameter measures the ability of the basis provided by
  2604. ! the pseudo-atomic wfc to represent the PW eigenstates,
  2605. ! by measuring how much of the subspace of the Hamiltonian
  2606. ! eigenstates falls outside the subspace spanned by the atomic basis
  2607. !
  2608. DEALLOCATE (charges)
  2609. !
  2610. ENDIF
  2611. !
  2612. RETURN
  2613. !
  2614. CONTAINS
  2615. !
  2616. SUBROUTINE desc_init( nsiz, desc, desc_ip )
  2617. !
  2618. INTEGER, INTENT(in) :: nsiz
  2619. INTEGER, INTENT(out) :: desc(:)
  2620. INTEGER, INTENT(out) :: desc_ip(:,:,:)
  2621. INTEGER :: i, j, rank
  2622. INTEGER :: coor_ip( 2 )
  2623. !
  2624. CALL descla_init( desc, nsiz, nsiz, np_ortho, me_ortho, ortho_comm, ortho_comm_id )
  2625. !
  2626. nx = desc( nlax_ )
  2627. !
  2628. DO j = 0, desc( la_npc_ ) - 1
  2629. DO i = 0, desc( la_npr_ ) - 1
  2630. coor_ip( 1 ) = i
  2631. coor_ip( 2 ) = j
  2632. CALL descla_init( desc_ip(:,i+1,j+1), desc( la_n_ ), desc( la_nx_ ), &
  2633. np_ortho, coor_ip, ortho_comm, 1 )
  2634. CALL GRID2D_RANK( 'R', desc( la_npr_ ), desc( la_npc_ ), i, j, rank )
  2635. rank_ip( i+1, j+1 ) = rank * leg_ortho
  2636. ENDDO
  2637. ENDDO
  2638. !
  2639. la_proc = .false.
  2640. IF( desc( lambda_node_ ) > 0 ) la_proc = .true.
  2641. !
  2642. RETURN
  2643. END SUBROUTINE desc_init
  2644. !
  2645. SUBROUTINE calbec_zdistmat( npw, v, w, n, nx, dm )
  2646. !
  2647. ! This subroutine compute <vi|wj> and store the
  2648. ! result in distributed matrix dm
  2649. !
  2650. USE mp, ONLY : mp_root_sum
  2651. !
  2652. IMPLICIT NONE
  2653. !
  2654. INTEGER :: ipc, ipr
  2655. INTEGER :: nr, nc, ir, ic, root, ldv, ldw
  2656. INTEGER, INTENT(in) :: npw ! local number of plane wave
  2657. INTEGER, INTENT(in) :: n ! global dimension of matrix dm
  2658. INTEGER, INTENT(in) :: nx ! local leading dimension of matrix dm
  2659. ! WARNING: nx is the same on all proc, SIZE( dm, 1 ) NO!
  2660. COMPLEX(DP), INTENT(out) :: dm( :, : )
  2661. COMPLEX(DP) :: v(:,:), w(:,:)
  2662. COMPLEX(DP), ALLOCATABLE :: work( :, : )
  2663. !
  2664. ALLOCATE( work( nx, nx ) )
  2665. !
  2666. work = zero
  2667. !
  2668. ldv = size( v, 1 )
  2669. ldw = size( w, 1 )
  2670. !
  2671. DO ipc = 1, desc( la_npc_ ) ! loop on column procs
  2672. !
  2673. nc = desc_ip( nlac_ , 1, ipc )
  2674. ic = desc_ip( ilac_ , 1, ipc )
  2675. !
  2676. DO ipr = 1, ipc ! desc( la_npr_ ) ! ipc ! use symmetry for the loop on row procs
  2677. !
  2678. nr = desc_ip( nlar_ , ipr, ipc )
  2679. ir = desc_ip( ilar_ , ipr, ipc )
  2680. !
  2681. ! rank of the processor for which this block (ipr,ipc) is destinated
  2682. !
  2683. root = rank_ip( ipr, ipc )
  2684. ! use blas subs. on the matrix block
  2685. CALL ZGEMM( 'C', 'N', nr, nc, npw, ONE , &
  2686. v(1,ir), ldv, w(1,ic), ldw, ZERO, work, nx )
  2687. ! accumulate result on dm of root proc.
  2688. CALL mp_root_sum( work, dm, root, intra_pool_comm )
  2689. ENDDO
  2690. !
  2691. ENDDO
  2692. !
  2693. CALL zsqmher( n, dm, nx, desc )
  2694. !
  2695. DEALLOCATE( work )
  2696. !
  2697. RETURN
  2698. END SUBROUTINE calbec_zdistmat
  2699. !
  2700. SUBROUTINE calbec_ddistmat( npw, v, w, n, nx, dm )
  2701. !
  2702. ! This subroutine compute <vi|wj> and store the
  2703. ! result in distributed matrix dm
  2704. !
  2705. USE mp, ONLY : mp_root_sum
  2706. USE gvect, ONLY : gstart
  2707. !
  2708. IMPLICIT NONE
  2709. !
  2710. INTEGER :: ipc, ipr
  2711. INTEGER :: nr, nc, ir, ic, root, ldv, ldw, npw2, npwx2
  2712. INTEGER, INTENT(in) :: npw ! local number of plane wave
  2713. INTEGER, INTENT(in) :: n ! global dimension of matrix dm
  2714. INTEGER, INTENT(in) :: nx ! local leading dimension of matrix dm
  2715. ! WARNING: nx is the same on all proc, SIZE( dm, 1 ) NO!
  2716. REAL(DP), INTENT(out) :: dm( :, : )
  2717. COMPLEX(DP) :: v(:,:), w(:,:)
  2718. REAL(DP), ALLOCATABLE :: work( :, : )
  2719. !
  2720. ALLOCATE( work( nx, nx ) )
  2721. !
  2722. npw2 = 2*npw
  2723. npwx2 = 2*npwx
  2724. !
  2725. work = zero
  2726. !
  2727. ldv = size( v, 1 )
  2728. ldw = size( w, 1 )
  2729. !
  2730. DO ipc = 1, desc( la_npc_ ) ! loop on column procs
  2731. !
  2732. nc = desc_ip( nlac_ , 1, ipc )
  2733. ic = desc_ip( ilac_ , 1, ipc )
  2734. !
  2735. DO ipr = 1, ipc ! desc( la_npr_ ) ! ipc ! use symmetry for the loop on row procs
  2736. !
  2737. nr = desc_ip( nlar_ , ipr, ipc )
  2738. ir = desc_ip( ilar_ , ipr, ipc )
  2739. !
  2740. ! rank of the processor for which this block (ipr,ipc) is destinated
  2741. !
  2742. root = rank_ip( ipr, ipc )
  2743. ! use blas subs. on the matrix block
  2744. ! use blas subs. on the matrix block
  2745. CALL DGEMM( 'T', 'N', nr, nc, npw2, 2.D0 , &
  2746. v(1,ir), npwx2, w(1,ic), npwx2, 0.D0, work, nx )
  2747. IF ( gstart == 2 ) &
  2748. CALL DGER( nr, nc, -1.D0, v(1,ir), npwx2, w(1,ic), npwx2, work, nx )
  2749. ! accumulate result on dm of root proc.
  2750. CALL mp_root_sum( work, dm, root, intra_pool_comm )
  2751. ENDDO
  2752. !
  2753. ENDDO
  2754. !
  2755. CALL dsqmsym( n, dm, nx, desc )
  2756. !
  2757. DEALLOCATE( work )
  2758. !
  2759. RETURN
  2760. END SUBROUTINE calbec_ddistmat
  2761. !
  2762. !
  2763. !
  2764. SUBROUTINE wf_times_overlap( swfc, ovr, wfc )
  2765. COMPLEX(DP) :: swfc( :, : ), ovr( :, : ), wfc( :, : )
  2766. !
  2767. INTEGER :: ipc, ipr
  2768. INTEGER :: nr, nc, ir, ic, root
  2769. COMPLEX(DP), ALLOCATABLE :: vtmp( :, : )
  2770. COMPLEX(DP) :: beta
  2771. ALLOCATE( vtmp( nx, nx ) )
  2772. !
  2773. DO ipc = 1, desc( la_npc_ )
  2774. !
  2775. nc = desc_ip( nlac_ , 1, ipc )
  2776. ic = desc_ip( ilac_ , 1, ipc )
  2777. !
  2778. beta = ZERO
  2779. DO ipr = 1, desc( la_npr_ )
  2780. !
  2781. nr = desc_ip( nlar_ , ipr, ipc )
  2782. ir = desc_ip( ilar_ , ipr, ipc )
  2783. !
  2784. root = rank_ip( ipr, ipc )
  2785. IF( ipr-1 == desc( la_myr_ ) .and. ipc-1 == desc( la_myc_ ) .and. la_proc ) THEN
  2786. !
  2787. ! this proc sends his block
  2788. !
  2789. CALL mp_bcast( ovr, root, intra_pool_comm )
  2790. CALL ZGEMM( 'N', 'N', npw, nc, nr, ONE, &
  2791. swfc(1,ir), npwx, ovr, nx, beta, wfc(1,ic), npwx )
  2792. ELSE
  2793. !
  2794. ! all other procs receive
  2795. !
  2796. CALL mp_bcast( vtmp, root, intra_pool_comm )
  2797. CALL ZGEMM( 'N', 'N', npw, nc, nr, ONE, &
  2798. swfc(1,ir), npwx, vtmp, nx, beta, wfc(1,ic), npwx )
  2799. ENDIF
  2800. !
  2801. beta = ONE
  2802. ENDDO
  2803. !
  2804. ENDDO
  2805. !
  2806. DEALLOCATE( vtmp )
  2807. RETURN
  2808. END SUBROUTINE wf_times_overlap
  2809. !
  2810. SUBROUTINE wf_times_roverlap( swfc, ovr, wfc )
  2811. USE gvect, ONLY : gstart
  2812. COMPLEX(DP) :: swfc( :, : ), wfc( :, : )
  2813. REAL(DP) :: ovr( :, : )
  2814. !
  2815. INTEGER :: ipc, ipr, npw2, npwx2
  2816. INTEGER :: nr, nc, ir, ic, root
  2817. REAL(DP), ALLOCATABLE :: vtmp( :, : )
  2818. REAL(DP) :: beta
  2819. npw2 = 2*npw
  2820. npwx2 = 2*npwx
  2821. ALLOCATE( vtmp( nx, nx ) )
  2822. !
  2823. DO ipc = 1, desc( la_npc_ )
  2824. !
  2825. nc = desc_ip( nlac_ , 1, ipc )
  2826. ic = desc_ip( ilac_ , 1, ipc )
  2827. !
  2828. beta = 0.0d0
  2829. DO ipr = 1, desc( la_npr_ )
  2830. !
  2831. nr = desc_ip( nlar_ , ipr, ipc )
  2832. ir = desc_ip( ilar_ , ipr, ipc )
  2833. !
  2834. root = rank_ip( ipr, ipc )
  2835. IF( ipr-1 == desc( la_myr_ ) .and. ipc-1 == desc( la_myc_ ) .and. la_proc ) THEN
  2836. !
  2837. ! this proc sends his block
  2838. !
  2839. CALL mp_bcast( ovr, root, intra_pool_comm )
  2840. CALL DGEMM( 'N', 'N', npw2, nc, nr, 1.D0, &
  2841. swfc(1,ir), npwx2, ovr, nx, beta, wfc(1,ic), npwx2 )
  2842. !
  2843. ELSE
  2844. !
  2845. ! all other procs receive
  2846. !
  2847. CALL mp_bcast( vtmp, root, intra_pool_comm )
  2848. CALL DGEMM( 'N', 'N', npw2, nc, nr, 1.D0, &
  2849. swfc(1,ir), npwx2, vtmp, nx, beta, wfc(1,ic), npwx2 )
  2850. !
  2851. ENDIF
  2852. !
  2853. beta = 1.0d0
  2854. ENDDO
  2855. !
  2856. ENDDO
  2857. !
  2858. DEALLOCATE( vtmp )
  2859. RETURN
  2860. END SUBROUTINE wf_times_roverlap
  2861. !
  2862. END SUBROUTINE pprojwave
  2863. !
  2864. !-----------------------------------------------------------------------
  2865. SUBROUTINE projwave_boxes( filpdos, filproj, n_proj_boxes, irmin, irmax, plotboxes )
  2866. !-----------------------------------------------------------------------
  2867. !
  2868. USE io_global, ONLY : stdout, ionode
  2869. USE printout_base, ONLY: title
  2870. USE atom
  2871. USE ions_base, ONLY : zv, tau, nat, ntyp => nsp, ityp, atm
  2872. USE basis, ONLY : natomwfc
  2873. USE cell_base
  2874. USE constants, ONLY: rytoev
  2875. USE gvect
  2876. USE klist, ONLY: xk, nks, nkstot
  2877. USE lsda_mod, ONLY: nspin, isk, current_spin, lsda
  2878. USE wvfct
  2879. USE control_flags, ONLY: gamma_only
  2880. USE uspp, ONLY: okvan
  2881. USE noncollin_module, ONLY: noncolin, npol
  2882. USE wavefunctions_module, ONLY: evc, psic
  2883. USE wavefunctions_module, ONLY: psic_nc
  2884. USE io_files, ONLY : iunwfc, nwordwfc
  2885. USE scf, ONLY : rho
  2886. USE projections_ldos
  2887. USE fft_base, ONLY : grid_scatter, dfftp
  2888. USE fft_interfaces, ONLY : invfft
  2889. USE mp_global, ONLY : intra_pool_comm
  2890. USE mp, ONLY : mp_sum
  2891. !
  2892. !
  2893. IMPLICIT NONE
  2894. !
  2895. INTEGER, PARAMETER :: N_MAX_BOXES = 999
  2896. CHARACTER (len=256) :: filpdos
  2897. CHARACTER (len=*) :: filproj
  2898. INTEGER :: n_proj_boxes, irmin(3,*), irmax(3,*)
  2899. LOGICAL :: plotboxes
  2900. !
  2901. INTEGER :: ik, ibnd, i, ir, ig, ipol, ibox, ir1, ir2, ir3, c_tab, is, iunproj
  2902. INTEGER :: nri(3)
  2903. CHARACTER (len=33) :: filextension
  2904. CHARACTER (len=256):: fileout
  2905. COMPLEX(DP), ALLOCATABLE :: caux(:)
  2906. REAL(DP), ALLOCATABLE :: thetabox(:), raux(:), thetathisproc(:,:), union(:), intersection(:)
  2907. LOGICAL, ALLOCATABLE :: isInside(:,:)
  2908. REAL(DP), EXTERNAL :: DDOT
  2909. REAL(DP), ALLOCATABLE :: boxvolume(:), boxcharge(:)
  2910. !
  2911. WRITE( stdout, '(/5x,"Calling projwave_boxes .... ")')
  2912. IF ( gamma_only ) THEN
  2913. WRITE( stdout, '(5x,"gamma-point specific algorithms are used")')
  2914. ENDIF
  2915. !
  2916. IF (noncolin) THEN
  2917. WRITE( stdout, '(/5x,"Non spin-resolved DOS will be computed")')
  2918. ENDIF
  2919. !
  2920. IF (okvan) THEN
  2921. CALL errore( 'projwave_boxes', 'Augmentation contributions are currently not included to the DOS in boxes',-1)
  2922. ENDIF
  2923. !
  2924. IF ( ( n_proj_boxes > N_MAX_BOXES ) .or. ( n_proj_boxes < 1 ) ) &
  2925. CALL errore ('projwave_boxes', 'n_proj_boxes not correct', abs (n_proj_boxes) )
  2926. !
  2927. ! ... Define functions with values 1.0
  2928. ! ... on the specified boxes and 0.0 elsewhere.
  2929. !
  2930. ALLOCATE( thetabox (dfftp%nr1x*dfftp%nr2x*dfftp%nr3x) )
  2931. !
  2932. ALLOCATE( thetathisproc(dfftp%nnr,1:n_proj_boxes) )
  2933. !
  2934. ALLOCATE ( isInside ( max(dfftp%nr1,dfftp%nr2,dfftp%nr3), 3 ) )
  2935. !
  2936. DO ibox = 1, n_proj_boxes
  2937. !
  2938. ! A. Do the three directions independently:
  2939. nri(1)=dfftp%nr1
  2940. nri(2)=dfftp%nr2
  2941. nri(3)=dfftp%nr3
  2942. DO i = 1, 3
  2943. ! boxes include the points in [irmin,irmax] if irmin<=irmax
  2944. ! and the points in [1,irmax] and [irmin,nr] if irmin > irmax
  2945. irmin(i,ibox)=mod(irmin(i,ibox),nri(i))
  2946. IF (irmin(i,ibox)<=0) irmin(i,ibox)=irmin(i,ibox)+nri(i)
  2947. irmax(i,ibox)=mod(irmax(i,ibox),nri(i))
  2948. IF (irmax(i,ibox)<=0) irmax(i,ibox)=irmax(i,ibox)+nri(i)
  2949. DO ir = 1, nri(i)
  2950. IF (irmin(i,ibox)<=irmax(i,ibox)) THEN
  2951. isInside(ir,i)=(ir>=irmin(i,ibox)).and.(ir<=irmax(i,ibox))
  2952. ELSE
  2953. isInside(ir,i)=(ir>=irmin(i,ibox)).or. (ir<=irmax(i,ibox))
  2954. ENDIF
  2955. ENDDO
  2956. ENDDO
  2957. !
  2958. ! B. Combine the conditions for the three directions to form a box
  2959. ir=0
  2960. DO ir3 = 1, dfftp%nr3
  2961. DO ir2 = 1, dfftp%nr2
  2962. DO ir1 = 1, dfftp%nr1
  2963. ir=ir+1
  2964. IF ( isInside(ir1,1) .and. &
  2965. isInside(ir2,2) .and. &
  2966. isInside(ir3,3) ) THEN
  2967. thetabox(ir)=1._DP
  2968. ELSE
  2969. thetabox(ir)=0._DP
  2970. ENDIF
  2971. ENDDO
  2972. ENDDO
  2973. !
  2974. ENDDO
  2975. !
  2976. ! C. Output the functions thetabox in the XCrySDen format,
  2977. ! so that the projection boxes can be visualised.
  2978. IF ( ionode .and. plotboxes ) THEN
  2979. filextension='.box#'
  2980. ! 123456
  2981. c_tab = 6
  2982. IF (ibox < 10) THEN
  2983. WRITE (filextension( c_tab : c_tab ),'(i1)') ibox
  2984. c_tab = c_tab + 1
  2985. ELSEIF (ibox < 100) THEN
  2986. WRITE (filextension( c_tab : c_tab+1 ),'(i2)') ibox
  2987. c_tab = c_tab + 2
  2988. ELSEIF (ibox < 1000) THEN
  2989. WRITE (filextension( c_tab : c_tab+2 ),'(i3)') ibox
  2990. c_tab = c_tab + 3
  2991. ELSE
  2992. CALL errore('projwave_boxes',&
  2993. 'file extension not supporting so many boxes', n_proj_boxes)
  2994. ENDIF
  2995. !
  2996. fileout = trim(filpdos)//trim(filextension)//'.xsf'
  2997. OPEN (4,file=fileout,form='formatted', status='unknown')
  2998. CALL xsf_struct (alat, at, nat, tau, atm, ityp, 4)
  2999. CALL xsf_fast_datagrid_3d(thetabox(1:dfftp%nr1x*dfftp%nr2x*dfftp%nr3x),&
  3000. dfftp%nr1, dfftp%nr2, dfftp%nr3, &
  3001. dfftp%nr1x, dfftp%nr2x, dfftp%nr3x, at, alat, 4)
  3002. CLOSE (4)
  3003. !
  3004. ENDIF
  3005. !
  3006. CALL grid_scatter ( thetabox(:), thetathisproc(:,ibox) )
  3007. !
  3008. ENDDO
  3009. !
  3010. DEALLOCATE ( isInside )
  3011. DEALLOCATE ( thetabox )
  3012. !
  3013. !
  3014. ! ... For each box output the volume and the electronic charge contained
  3015. !
  3016. ALLOCATE ( boxvolume (1:n_proj_boxes) )
  3017. ALLOCATE ( boxcharge (1:n_proj_boxes) )
  3018. ALLOCATE ( raux (dfftp%nnr) )
  3019. !
  3020. ! A. Integrate the volume
  3021. DO ibox = 1, n_proj_boxes
  3022. boxvolume(ibox) = sum(thetathisproc(1:dfftp%nnr,ibox))
  3023. CALL mp_sum ( boxvolume(ibox) , intra_pool_comm )
  3024. ENDDO
  3025. !
  3026. ! B1. Copy the total charge density to raux
  3027. IF (noncolin) THEN
  3028. CALL DCOPY (dfftp%nnr, rho%of_r, 1, raux, 1)
  3029. ELSE
  3030. CALL DCOPY (dfftp%nnr, rho%of_r (1, 1), 1, raux, 1)
  3031. DO is = 2, nspin
  3032. CALL DAXPY (dfftp%nnr, 1.d0, rho%of_r (1, is), 1, raux, 1)
  3033. ENDDO
  3034. ENDIF
  3035. !
  3036. ! B2. Integrate the charge
  3037. ! the correct integral has dv = omega/(nr1*nr2*nr3)
  3038. ! not omega/(nr1x*nr2x*nr3x) . PG 24 Oct 2010
  3039. DO ibox = 1, n_proj_boxes
  3040. boxcharge(ibox) = DDOT(dfftp%nnr,raux(:),1,thetathisproc(:,ibox),1) &
  3041. & * omega / (dfftp%nr1*dfftp%nr2*dfftp%nr3)
  3042. CALL mp_sum ( boxcharge(ibox) , intra_pool_comm )
  3043. ENDDO
  3044. !
  3045. ! C. Write the result
  3046. IF (ionode) THEN
  3047. WRITE (stdout,*)
  3048. DO ibox = 1, n_proj_boxes
  3049. WRITE (stdout, &
  3050. '(5x,"Box #",i3," : vol ",f10.6," % = ",f14.6," (a.u.)^3; ",e13.6," elec")') &
  3051. ibox, 100* boxvolume(ibox) /(dfftp%nr1*dfftp%nr2*dfftp%nr3), &
  3052. omega* boxvolume(ibox)/(dfftp%nr1*dfftp%nr2*dfftp%nr3), boxcharge(ibox)
  3053. ENDDO
  3054. ENDIF
  3055. !
  3056. DEALLOCATE ( boxvolume , boxcharge )
  3057. !
  3058. ! ... Here we sum for each k point the contribution
  3059. ! ... of the wavefunctions to the charge in the specified box
  3060. !
  3061. ALLOCATE( proj(1:n_proj_boxes,nbnd,nkstot) )
  3062. proj(:,:,:)=0._DP
  3063. !
  3064. ALLOCATE( caux(dfftp%nnr) )
  3065. !
  3066. k_loop: DO ik = 1, nks
  3067. !
  3068. IF ( lsda ) current_spin = isk(ik)
  3069. CALL gk_sort (xk (1, ik), ngm, g, ecutwfc / tpiba2, npw, igk, g2kin)
  3070. CALL davcio (evc, nwordwfc, iunwfc, ik, - 1)
  3071. !
  3072. bnd_loop: DO ibnd = 1, nbnd
  3073. !
  3074. IF (noncolin) THEN
  3075. !
  3076. psic_nc = (0.d0,0.d0)
  3077. DO ig = 1, npw
  3078. psic_nc(nl(igk(ig)),1)=evc(ig ,ibnd)
  3079. psic_nc(nl(igk(ig)),2)=evc(ig+npwx,ibnd)
  3080. ENDDO
  3081. raux=0._DP
  3082. DO ipol=1,npol
  3083. CALL invfft ('Dense', psic_nc(:,ipol), dfftp)
  3084. raux(:) = raux(:)+dble( psic_nc(:,ipol) )**2 &
  3085. + aimag( psic_nc(:,ipol) )**2
  3086. ENDDO
  3087. !
  3088. ELSE
  3089. !
  3090. caux(1:dfftp%nnr) = (0._DP,0._DP)
  3091. DO ig = 1, npw
  3092. caux (nl (igk (ig) ) ) = evc (ig, ibnd)
  3093. ENDDO
  3094. IF (gamma_only) THEN
  3095. DO ig = 1, npw
  3096. caux (nlm(igk (ig) ) ) = conjg(evc (ig, ibnd))
  3097. ENDDO
  3098. ENDIF
  3099. CALL invfft ('Dense', caux, dfftp)
  3100. !
  3101. raux(:) = dble( caux(:) )**2 + aimag( caux(:) )**2
  3102. !
  3103. ENDIF
  3104. !
  3105. ! The contribution of this wavefunction to the LDOS
  3106. ! integrated in the volume is the projection of the
  3107. ! squared wfc on a function =1 in the volume itself:
  3108. !
  3109. DO ibox = 1, n_proj_boxes
  3110. proj(ibox,ibnd,ik) = DDOT(dfftp%nnr,raux(:),1,thetathisproc(:,ibox),1) &
  3111. & / (dfftp%nr1*dfftp%nr2*dfftp%nr3)
  3112. ENDDO
  3113. !
  3114. ENDDO bnd_loop
  3115. !
  3116. CALL mp_sum ( proj(:,:,ik) , intra_pool_comm )
  3117. !
  3118. ENDDO k_loop
  3119. !
  3120. DEALLOCATE ( caux )
  3121. DEALLOCATE ( raux )
  3122. DEALLOCATE ( thetathisproc )
  3123. !
  3124. ! vector proj is distributed across the pools
  3125. ! collect data for all k-points to the first pool
  3126. !
  3127. CALL poolrecover (proj, n_proj_boxes*nbnd, nkstot, nks)
  3128. !
  3129. ! Output the projections
  3130. IF ( ionode ) THEN
  3131. IF (filproj/=' ') THEN
  3132. iunproj=33
  3133. CALL write_io_header(filproj, iunproj, title, dfftp%nr1x, dfftp%nr2x, &
  3134. dfftp%nr3x, dfftp%nr1, dfftp%nr2, dfftp%nr3, nat, ntyp, ibrav, &
  3135. celldm, at, gcutm, dual, ecutwfc, nkstot,nbnd,natomwfc)
  3136. DO ibox = 1, n_proj_boxes
  3137. WRITE (iunproj,'(3i6)') ibox, n_proj_boxes
  3138. WRITE (iunproj,'(i6,i6,f9.4,e13.6)') &
  3139. ((ik,ibnd,et(ibnd,ik)*rytoev,proj(ibox,ibnd,ik),ibnd=1,nbnd),ik=1,nkstot)
  3140. ENDDO
  3141. CLOSE (iunproj)
  3142. ENDIF
  3143. ENDIF
  3144. !
  3145. RETURN
  3146. !
  3147. END SUBROUTINE projwave_boxes
  3148. !
  3149. !-----------------------------------------------------------------------
  3150. SUBROUTINE partialdos_boxes(Emin, Emax, DeltaE, kresolveddos, filpdos, n_proj_boxes)
  3151. !-----------------------------------------------------------------------
  3152. !
  3153. USE io_global, ONLY : stdout
  3154. USE klist, ONLY: wk, nkstot, degauss, ngauss, lgauss
  3155. USE lsda_mod, ONLY: nspin, isk, current_spin
  3156. USE wvfct, ONLY: et, nbnd
  3157. USE constants, ONLY: rytoev
  3158. USE projections_ldos
  3159. !
  3160. IMPLICIT NONE
  3161. CHARACTER (len=256) :: filpdos
  3162. REAL(DP) :: Emin, Emax, DeltaE
  3163. LOGICAL :: kresolveddos
  3164. INTEGER :: n_proj_boxes
  3165. !
  3166. CHARACTER (len=33) :: filextension
  3167. CHARACTER (len=256):: fileout
  3168. !
  3169. INTEGER :: ik, ibnd, ne, ie_mid, ie_delta, ie, is, nkseff, ikeff, ibox, nspin0
  3170. REAL(DP) :: etev, delta, Elw, Eup, wkeff
  3171. REAL(DP), ALLOCATABLE :: dostot(:,:,:), dosbox(:,:,:,:), dosboxtot(:,:,:)
  3172. REAL(DP), EXTERNAL :: w0gauss
  3173. !
  3174. ! find band extrema
  3175. !
  3176. Elw = et (1, 1)
  3177. Eup = et (nbnd, 1)
  3178. DO ik = 2, nkstot
  3179. Elw = min (Elw, et (1, ik) )
  3180. Eup = max (Eup, et (nbnd, ik) )
  3181. ENDDO
  3182. IF (degauss/=0.d0) THEN
  3183. Eup = Eup + 3d0 * degauss
  3184. Elw = Elw - 3d0 * degauss
  3185. ENDIF
  3186. Emin = max (Emin/rytoev, Elw)
  3187. Emax = min (Emax/rytoev, Eup)
  3188. DeltaE = DeltaE/rytoev
  3189. ne = nint ( (Emax - Emin) / DeltaE+0.500001d0)
  3190. !
  3191. IF (nspin==2) THEN
  3192. nspin0 = 2
  3193. ELSE
  3194. nspin0 = 1
  3195. ENDIF
  3196. !
  3197. IF (kresolveddos) THEN
  3198. IF ( nspin==2 ) THEN
  3199. nkseff=nkstot/2
  3200. ELSE
  3201. nkseff=nkstot
  3202. ENDIF
  3203. ELSE
  3204. nkseff=1
  3205. ENDIF
  3206. !
  3207. ALLOCATE (dosbox(0:ne,1:n_proj_boxes,nspin0,nkseff))
  3208. ALLOCATE (dostot(0:ne,nspin0,nkseff), dosboxtot(0:ne,nspin0,nkseff) )
  3209. dosbox(:,:,:,:) = 0.d0
  3210. dostot(:,:,:) = 0.d0
  3211. dosboxtot(:,:,:)= 0.d0
  3212. current_spin = 1
  3213. ie_delta = 5 * degauss / DeltaE + 1
  3214. !
  3215. DO ik = 1,nkstot
  3216. !
  3217. IF (kresolveddos) THEN
  3218. ! set equal weight to all k-points
  3219. wkeff=1.D0
  3220. !
  3221. IF (( nspin==2 ).AND.( isk(ik)==2 )) THEN
  3222. ikeff=ik-nkstot/2
  3223. ELSE
  3224. ikeff=ik
  3225. ENDIF
  3226. ELSE
  3227. ! use true weights
  3228. wkeff=wk(ik)
  3229. ! contributions from all k-points are summed in pdos(:,:,:,ikeff)
  3230. ikeff=1
  3231. ENDIF
  3232. !
  3233. IF ( nspin == 2 ) current_spin = isk ( ik )
  3234. DO ibnd = 1, nbnd
  3235. etev = et(ibnd,ik)
  3236. ie_mid = nint( (etev-Emin)/DeltaE )
  3237. DO ie = max(ie_mid-ie_delta, 0), min(ie_mid+ie_delta, ne)
  3238. delta = w0gauss((Emin+DeltaE*ie-etev)/degauss,ngauss) &
  3239. / degauss / rytoev
  3240. !
  3241. DO ibox = 1, n_proj_boxes
  3242. dosbox(ie,ibox,current_spin,ikeff) = &
  3243. dosbox(ie,ibox,current_spin,ikeff) + &
  3244. wkeff * delta * proj (ibox, ibnd, ik)
  3245. ENDDO
  3246. !
  3247. ! dostot(:,ns,ik) = total DOS (states/eV) for spin "ns"
  3248. ! for k-point "ik" (or summed over all kp)
  3249. !
  3250. dostot(ie,current_spin,ikeff) = dostot(ie,current_spin,ikeff) + &
  3251. wkeff * delta
  3252. ENDDO
  3253. ENDDO
  3254. ENDDO
  3255. !
  3256. ! dosboxtot(:,ns,ik) = sum of all projected DOS
  3257. !
  3258. DO ik=1,nkseff
  3259. DO is=1,nspin0
  3260. DO ie=0,ne
  3261. dosboxtot(ie,is,ik) = sum(dosbox(ie,1:n_proj_boxes,is,ik))
  3262. ENDDO
  3263. ENDDO
  3264. ENDDO
  3265. !
  3266. fileout = trim(filpdos)//'.ldos_boxes'
  3267. !
  3268. OPEN (4,file=fileout,form='formatted', &
  3269. status='unknown')
  3270. IF (kresolveddos) THEN
  3271. WRITE (4,'("# ik ",$)')
  3272. ELSE
  3273. WRITE (4,'("#",$)')
  3274. ENDIF
  3275. IF (nspin0 == 2) THEN
  3276. WRITE (4,'(" E (eV) tot_up(E) tot_dw(E) totldos_up totldos_dw ",$)')
  3277. ELSE
  3278. WRITE (4,'(" E (eV) tot(E) totldos ",$)')
  3279. ENDIF
  3280. DO ibox=1, n_proj_boxes
  3281. IF (nspin0 == 2) THEN
  3282. WRITE(4,'("#",i3," up(E) ",$)') ibox
  3283. WRITE(4,'("#",i3," dw(E) ",$)') ibox
  3284. ELSE
  3285. WRITE(4,'("#",i3," (E) ",$)') ibox
  3286. ENDIF
  3287. ENDDO
  3288. WRITE (4,*)
  3289. DO ik=1,nkseff
  3290. DO ie= 0, ne
  3291. IF (kresolveddos) THEN
  3292. WRITE (4,'(i5," ",$)') ik
  3293. ENDIF
  3294. etev = Emin + ie * DeltaE
  3295. WRITE (4,'(f7.3,4(2e11.3),999(2e11.3))') etev*rytoev, &
  3296. dostot(ie,1:nspin0,ik), dosboxtot(ie,1:nspin0,ik), &
  3297. ( dosbox(ie,ibox,1:nspin0,ik), ibox = 1, n_proj_boxes )
  3298. ENDDO
  3299. IF (kresolveddos) WRITE (4,*)
  3300. ENDDO
  3301. CLOSE (4)
  3302. DEALLOCATE (dostot, dosboxtot)
  3303. DEALLOCATE (dosbox)
  3304. !
  3305. DEALLOCATE (proj)
  3306. !
  3307. RETURN
  3308. END SUBROUTINE partialdos_boxes