Advertisement
Guest User

Untitled

a guest
May 12th, 2017
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Fortran 77.17 KB | None | 0 0
  1. !
  2. ! Copyright (C) 2005-2014 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. ! TB
  10. ! included monopole related variables, search for 'TB'
  11. !----------------------------------------------------------------------------
  12. !
  13. !----------------------------------------------------------------------------
  14. Module pw_restart
  15.     !----------------------------------------------------------------------------
  16.     !
  17.     ! ... this module contains methods to read and write data produced by PWscf
  18.     !
  19.     ! ... originally written by Carlo Sbraccia  (2005)
  20.     !
  21. #if ! defined(__XSD)
  22. !
  23.     Use iotk_module
  24.     !
  25.     Use qexml_module, Only: qexml_init, qexml_openfile, qexml_closefile, qexml_write_header, qexml_write_control, qexml_write_cell, qexml_write_moving_cell, qexml_write_ions, qexml_write_symmetry, qexml_write_efield, qexml_write_planewaves, qexml_write_spin, qexml_write_magnetization, qexml_write_xc, qexml_write_exx, qexml_write_occ, qexml_write_bz, qexml_write_para, qexml_write_bands_info, qexml_write_bands_pw, qexml_write_esm, qexml_wfc_filename, default_fmt_version => qexml_default_version, qexml_kpoint_dirname, qexml_read_header, qexml_read_cell, qexml_read_moving_cell, qexml_read_planewaves, qexml_read_ions, qexml_read_spin, qexml_read_magnetization, qexml_read_xc, qexml_read_occ, qexml_read_bz, qexml_read_bands_info, qexml_read_bands_pw, qexml_read_symmetry, qexml_read_efield, qexml_read_para, qexml_read_exx, qexml_read_esm
  26.     !
  27.     Use xml_io_base, Only: rho_binary, read_wfc, write_wfc, create_directory
  28.     !
  29.     !
  30.     Use kinds, Only: DP
  31.     Use constants, Only: e2, PI
  32.     !
  33.     Use io_files, Only: tmp_dir, prefix, iunpun, xmlpun, delete_if_present, qexml_version, qexml_version_init, pseudo_dir
  34.     !
  35.     Use io_global, Only: ionode, ionode_id
  36.     Use mp_images, Only: intra_image_comm
  37.     Use mp_pools, Only: my_pool_id
  38.     Use mp_bands, Only: intra_bgrp_comm
  39.     Use mp, Only: mp_bcast, mp_sum, mp_max
  40.     Use parser, Only: version_compare
  41.     !
  42.     !
  43.     Implicit None
  44.     !
  45.     Character (Len=256), External :: trimcheck
  46.     !
  47.     Save
  48.     !
  49.     Private
  50.     !
  51.     Public :: pw_readfile, pw_writefile
  52.     Public :: gk_l2gmap, gk_l2gmap_kdip
  53.     !
  54.     Integer, Private :: iunout
  55.     !
  56.     Logical :: lcell_read = .False., lpw_read = .False., lions_read = .False., lspin_read = .False., lstarting_mag_read = .False., lxc_read = .False., locc_read = .False., lbz_read = .False., lbs_read = .False., lefield_read = .False., lwfc_read = .False., lsymm_read = .False.
  57.     !
  58.     !
  59. Contains
  60. !
  61. !------------------------------------------------------------------------
  62.     Subroutine pw_writefile (what)
  63.     !------------------------------------------------------------------------
  64.     !
  65.         Use control_flags, Only: twfcollect, conv_ions, lscf, lkpoint_dir, gamma_only, tqr, tq_smoothing, tbeta_smoothing, noinv, do_makov_payne, smallmem, llondon, lxdm, ts_vdw
  66.         Use realus, Only: real_space
  67.         Use global_version, Only: version_number
  68.         Use cell_base, Only: at, bg, alat, tpiba, tpiba2, ibrav, celldm
  69.         Use gvect, Only: ig_l2g
  70.         Use ions_base, Only: nsp, ityp, atm, nat, tau, if_pos
  71.         Use noncollin_module, Only: noncolin, npol
  72.         Use io_files, Only: nwordwfc, iunwfc, psfile
  73.         Use buffers, Only: get_buffer
  74.         Use wavefunctions_module, Only: evc
  75.         Use klist, Only: nks, nkstot, xk, ngk, igk_k, wk, qnorm, lgauss, ngauss, degauss, nelec, two_fermi_energies, nelup, neldw
  76.         Use start_k, Only: nk1, nk2, nk3, k1, k2, k3, nks_start, xk_start, wk_start
  77.         Use ktetra, Only: ntetra, tetra
  78.         Use klist, Only: ltetra
  79.         Use gvect, Only: ngm, ngm_g, g, mill
  80.         Use fft_base, Only: dfftp
  81.         Use basis, Only: natomwfc
  82.         Use gvecs, Only: ngms_g, dual
  83.         Use fft_base, Only: dffts
  84.         Use wvfct, Only: npw, npwx, et, wg, nbnd
  85.         Use gvecw, Only: ecutwfc
  86.         Use ener, Only: ef, ef_up, ef_dw
  87.         Use fixed_occ, Only: tfixed_occ, f_inp
  88.         Use ldaU, Only: lda_plus_u, lda_plus_u_kind, U_projection, Hubbard_lmax, Hubbard_l, Hubbard_U, Hubbard_J, Hubbard_alpha, Hubbard_J0, Hubbard_beta
  89.         Use spin_orb, Only: lspinorb, domag, lforcet
  90.         Use symm_base, Only: nrot, nsym, invsym, s, ft, irt, t_rev, sname, time_reversal, no_t_rev
  91.         Use lsda_mod, Only: nspin, isk, lsda, starting_magnetization
  92.         Use noncollin_module, Only: angle1, angle2, i_cons, mcons, bfield, lambda
  93.         Use ions_base, Only: amass
  94.         Use funct, Only: get_dft_name, get_inlc
  95.         Use kernel_table, Only: vdw_table_name
  96.         Use scf, Only: rho
  97.         Use extfield, Only: tefield, dipfield, edir, emaxpos, eopreg, eamp, monopole, zmon, block, block_1, block_2, block_height, relaxz
  98.         Use io_rho_xml, Only: write_rho
  99.         Use mp_world, Only: nproc
  100.         Use mp_images, Only: nproc_image
  101.         Use mp_pools, Only: kunit, nproc_pool, me_pool, root_pool, intra_pool_comm, inter_pool_comm
  102.         Use mp_bands, Only: nproc_bgrp, me_bgrp, root_bgrp, intra_bgrp_comm, nbgrp, ntask_groups
  103.         Use mp_diag, Only: nproc_ortho
  104.         Use funct, Only: get_exx_fraction, dft_is_hybrid, get_gau_parameter, get_screening_parameter, exx_is_active
  105.         Use exx, Only: x_gamma_extrapolation, nq1, nq2, nq3, exxdiv_treatment, yukawa, ecutvcut, ecutfock
  106.         Use cellmd, Only: lmovecell, cell_factor
  107.         Use martyna_tuckerman, Only: do_comp_mt
  108.         Use esm, Only: do_comp_esm, esm_nfit, esm_efield, esm_w, esm_a, esm_bc
  109.         Use acfdt_ener, Only: acfdt_in_pw
  110.         Use london_module, Only: scal6, lon_rcut
  111.         Use tsvdw_module, Only: vdw_isolated
  112.         !
  113.         !
  114.         Implicit None
  115.         !
  116.         Character (Len=*), Intent (In) :: what
  117.         !
  118.         Character (Len=20) :: dft_name
  119.         Character (Len=256) :: dirname, filename
  120.         Integer :: i, ig, ik, ngg, ierr, ipol, num_k_points
  121.         Integer :: nkl, nkr, npwx_g
  122.         Integer :: ike, iks, npw_g, ispin, inlc
  123.         Integer, External :: global_kpoint_index
  124.         Integer, Allocatable :: ngk_g (:)
  125.         Integer, Allocatable :: igk_l2g (:, :), igk_l2g_kdip (:, :), mill_g (:, :)
  126.         Logical :: lwfc, lrho, lxsd
  127.         Character (iotk_attlenx) :: attr
  128.         !
  129.         !
  130.         Select Case (what)
  131.         Case ("all")
  132.         !
  133.         ! ... do not overwrite the scf charge density with a non-scf one
  134.         ! ... (except in the 'force theorem' calculation of MAE where the
  135.         ! ...  charge density differs from the one read from disk)
  136.         !
  137.             lrho = lscf .Or. lforcet
  138.             lwfc = twfcollect
  139.             !
  140.         Case ("config")
  141.         !
  142.         ! ... write just the xml data file, not the charge density and the wavefunctions
  143.         !
  144.             lwfc = .False.
  145.             lrho = .False.
  146.             !
  147.         Case Default
  148.         !
  149.             Call errore ('pw_writefile', 'unexpected case: '// TRIM(what), 1)
  150.             !
  151.         End Select
  152.         !
  153.         If (ionode) Then
  154.         !
  155.         ! ... look for an empty unit (only ionode needs it)
  156.         !
  157.             Call iotk_free_unit (iunout, ierr)
  158.             !
  159.         End If
  160.         !
  161.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  162.         !
  163.         Call errore ('pw_writefile ', 'no free units to write wavefunctions', ierr)
  164.         !
  165.         dirname = TRIM (tmp_dir) // TRIM (prefix) // '.save'
  166.         !
  167.         ! ... create the main restart directory
  168.         !
  169.         Call create_directory (dirname)
  170.         !
  171.         ! ... create the k-points subdirectories
  172.         !
  173.         If (nspin == 2) Then
  174.             num_k_points = nkstot / 2
  175.         Else
  176.             num_k_points = nkstot
  177.         End If
  178.         !
  179.         If (lkpoint_dir) Then
  180.         !
  181.             Do i = 1, num_k_points
  182.             !
  183.                 Call create_directory (qexml_kpoint_dirname(dirname, i))
  184.                 !
  185.             End Do
  186.             !
  187.         End If
  188.         !
  189.         iks = global_kpoint_index (nkstot, 1)
  190.         ike = iks + nks - 1
  191.         !
  192.         ! ... find out the global number of G vectors: ngm_g
  193.         !
  194.         ngm_g = ngm
  195.         !
  196.         Call mp_sum (ngm_g, intra_bgrp_comm)
  197.         !
  198.         ! ... collect all G-vectors across processors within the pools
  199.         !
  200.         Allocate (mill_g(3, ngm_g))
  201.         !
  202.         mill_g = 0
  203.         !
  204.         Do ig = 1, ngm
  205.         !
  206.             mill_g (1, ig_l2g(ig)) = mill (1, ig)
  207.             mill_g (2, ig_l2g(ig)) = mill (2, ig)
  208.             mill_g (3, ig_l2g(ig)) = mill (3, ig)
  209.             !
  210.         End Do
  211.         !
  212.         Call mp_sum (mill_g, intra_bgrp_comm)
  213.         !
  214.         ! ... build the igk_l2g array, yielding the correspondence between
  215.         ! ... the local k+G index and the global G index - see also ig_l2g
  216.         ! ... igk_l2g is build from arrays igk, previously stored in hinit0
  217.         ! ... Beware: for variable-cell case, one has to use starting G and
  218.         ! ... k+G vectors
  219.         !
  220.         Allocate (igk_l2g(npwx, nks))
  221.         igk_l2g = 0
  222.         !
  223.         Do ik = 1, nks
  224.             npw = ngk (ik)
  225.             Call gk_l2gmap (ngm, ig_l2g(1), npw, igk_k(1, ik), igk_l2g(1, ik))
  226.         End Do
  227.         !
  228.         ! ... compute the global number of G+k vectors for each k point
  229.         !
  230.         Allocate (ngk_g(nkstot))
  231.         !
  232.         ngk_g = 0
  233.         ngk_g (iks:ike) = ngk (1:nks)
  234.         !
  235.         Call mp_sum (ngk_g, inter_pool_comm)
  236.         Call mp_sum (ngk_g, intra_pool_comm)
  237.         !
  238.         ngk_g = ngk_g / nbgrp
  239.         !
  240.         ! ... compute the maximum G vector index among all G+k an processors
  241.         !
  242.         npw_g = MAXVAL (igk_l2g(:, :))
  243.         !
  244.         Call mp_max (npw_g, inter_pool_comm)
  245.         Call mp_max (npw_g, intra_pool_comm)
  246.         !
  247.         ! ... compute the maximum number of G vector among all k points
  248.         !
  249.         npwx_g = MAXVAL (ngk_g(1:nkstot))
  250.         !
  251.         ! ... define a further l2g map to write gkvectors and wfc coherently
  252.         !
  253.         Allocate (igk_l2g_kdip(npwx_g, nks))
  254.         !
  255.         igk_l2g_kdip = 0
  256.         !
  257.         Do ik = iks, ike
  258.         !
  259.             Call gk_l2gmap_kdip (npw_g, ngk_g(ik), ngk(ik-iks+1), igk_l2g(1, ik-iks+1), igk_l2g_kdip(1, ik-iks+1))
  260.         End Do
  261.         !
  262.         If (ionode) Then
  263.         !
  264.         ! ... open XML descriptor
  265.         !
  266.             Call qexml_init (iunpun)
  267.             Call qexml_openfile (TRIM(dirname)//'/'//TRIM(xmlpun), 'write', BINARY=.False., ierr=ierr)
  268.             !
  269.             If ( .Not. (lkpoint_dir)) Call iotk_open_write (iunout, FILE=TRIM(dirname)//'/'//TRIM(xmlpun)//'.eig', BINARY=.False., ierr=ierr)
  270.             !
  271.         End If
  272.         !
  273.         !
  274.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  275.         !
  276.         Call errore ('pw_writefile ', 'cannot open restart file for writing', ierr)
  277.         !
  278.         If (ionode) Then
  279.         !
  280.         ! ... here we start writing the punch-file
  281.         !
  282.         !-------------------------------------------------------------------------------
  283.         ! ... HEADER
  284.         !-------------------------------------------------------------------------------
  285.         !
  286.             Call qexml_write_header ("PWSCF", TRIM(version_number))
  287.             !
  288.             !-------------------------------------------------------------------------------
  289.             ! ... CONTROL
  290.             !-------------------------------------------------------------------------------
  291.             !
  292.             Call qexml_write_control (PP_CHECK_FLAG=conv_ions, lkpoint_dir=lkpoint_dir, Q_REAL_SPACE=tqr, tq_smoothing=tq_smoothing, BETA_REAL_SPACE=real_space, tbeta_smoothing=tbeta_smoothing)
  293.             !
  294.             !-------------------------------------------------------------------------------
  295.             ! ... CELL
  296.             !-------------------------------------------------------------------------------
  297.             !
  298.             Call qexml_write_cell (ibrav, celldm, alat, at(:, 1), at(:, 2), at(:, 3), bg(:, 1), bg(:, 2), bg(:, 3), "Bohr", "Bohr",  "2 pi / a", do_makov_payne, do_comp_mt, do_comp_esm)
  299.             !
  300.             If (lmovecell) Call qexml_write_moving_cell (lmovecell, cell_factor)
  301.             !
  302.             !-------------------------------------------------------------------------------
  303.             ! ... IONS
  304.             !-------------------------------------------------------------------------------
  305.             !
  306.             Call qexml_write_ions (nsp, nat, atm, ityp, psfile, pseudo_dir, amass, 'a.m.u.', tau, 'Bohr', if_pos, dirname, alat)
  307.             !
  308.             !-------------------------------------------------------------------------------
  309.             ! ... SYMMETRIES
  310.             !-------------------------------------------------------------------------------
  311.             !
  312.             Call qexml_write_symmetry (ibrav, nrot, nsym, invsym, noinv, time_reversal, no_t_rev, ft, s, sname, "Crystal", irt, nat, t_rev)
  313.             !
  314.             !-------------------------------------------------------------------------------
  315.             ! ... ELECTRIC FIELD
  316.             !-------------------------------------------------------------------------------
  317.             !
  318.             Call qexml_write_efield (tefield, dipfield, edir, emaxpos, eopreg, eamp, monopole, zmon, relaxz, block, block_1, block_2, block_height)
  319.             !
  320.             !
  321.             !-------------------------------------------------------------------------------
  322.             ! ... PLANE_WAVES
  323.             !-------------------------------------------------------------------------------
  324.             !
  325.             Call qexml_write_planewaves (ecutwfc/e2, ecutwfc*dual/e2, npwx_g, gamma_only, dfftp%nr1, dfftp%nr2, dfftp%nr3, ngm_g, dffts%nr1, dffts%nr2, dffts%nr3, ngms_g, dfftp%nr1, dfftp%nr2, dfftp%nr3, mill_g, lwfc, 'Hartree')
  326.             !
  327.             !-------------------------------------------------------------------------------
  328.             ! ... SPIN
  329.             !-------------------------------------------------------------------------------
  330.             !
  331.             Call qexml_write_spin (lsda, noncolin, npol, lspinorb, domag)
  332.             !
  333.             Call qexml_write_magnetization (starting_magnetization, angle1*180.0_DP/PI, angle2*180.0_DP/PI, nsp, two_fermi_energies, i_cons, mcons, bfield, ef_up/e2, ef_dw/e2, nelup, neldw, lambda, 'Hartree')
  334.             !
  335.             !-------------------------------------------------------------------------------
  336.             ! ... EXCHANGE_CORRELATION
  337.             !-------------------------------------------------------------------------------
  338.             !
  339.             dft_name = get_dft_name ()
  340.             inlc = get_inlc ()
  341.             !
  342.             Call qexml_write_xc (DFT=dft_name, nsp=nsp, lda_plus_u=lda_plus_u, lda_plus_u_kind=lda_plus_u_kind, U_projection=U_projection, Hubbard_lmax=Hubbard_lmax, Hubbard_l=Hubbard_l, Hubbard_U=Hubbard_U, Hubbard_J=Hubbard_J, Hubbard_J0=Hubbard_J0, Hubbard_beta=Hubbard_beta, Hubbard_alpha=Hubbard_alpha, inlc=inlc, vdw_table_name=vdw_table_name, pseudo_dir=pseudo_dir, dirname=dirname, acfdt_in_pw=acfdt_in_pw, llondon=llondon, LONDON_S6=scal6, LONDON_RCUT=lon_rcut, lxdm=lxdm, ts_vdw=ts_vdw, vdw_isolated=vdw_isolated)
  343.             !
  344.             !
  345.             If (dft_is_hybrid()) Call qexml_write_exx (x_gamma_extrapolation, nq1, nq2, nq3, exxdiv_treatment, yukawa, ecutvcut, get_exx_fraction(), get_gau_parameter(), get_screening_parameter(), exx_is_active(), ecutfock)
  346.             !
  347.             !-------------------------------------------------------------------------------
  348.             ! ... ESM
  349.             !-------------------------------------------------------------------------------
  350.             !
  351.             Call qexml_write_esm (esm_nfit, esm_efield, esm_w, esm_a, esm_bc)
  352.             !
  353.             !-------------------------------------------------------------------------------
  354.             ! ... OCCUPATIONS
  355.             !-------------------------------------------------------------------------------
  356.             !
  357.             Call qexml_write_occ (lgauss=lgauss, ngauss=ngauss, degauss=degauss/e2, DEGAUSS_UNITS='Hartree', ltetra=ltetra, ntetra=ntetra, tetra=tetra, tfixed_occ=tfixed_occ, lsda=lsda, NSTATES_UP=nbnd, NSTATES_DW=nbnd, INPUT_OCC=f_inp)
  358.             !
  359.             !-------------------------------------------------------------------------------
  360.             ! ... BRILLOUIN_ZONE
  361.             !-------------------------------------------------------------------------------
  362.             !
  363.             Call qexml_write_bz (num_k_points, xk, wk, k1, k2, k3, nk1, nk2, nk3, '2 pi / a', qnorm, nks_start, xk_start, wk_start)
  364.             !
  365.             !-------------------------------------------------------------------------------
  366.             ! ... PARALLELISM
  367.             !-------------------------------------------------------------------------------
  368.             !
  369.             !
  370.             Call qexml_write_para (kunit, nproc, nproc_pool, nproc_image, ntask_groups, nproc_bgrp, nproc_ortho)
  371.             !
  372.             !-------------------------------------------------------------------------------
  373.             ! ... CHARGE DENSITY
  374.             !-------------------------------------------------------------------------------
  375.             !
  376.             !
  377.             filename = "./charge-density.dat"
  378.             If ( .Not. rho_binary) filename = "./charge-density.xml"
  379.             !
  380.             Call iotk_link (iunpun, "CHARGE-DENSITY", TRIM(filename), CREATE=.False., BINARY=.True.)
  381.             !
  382.             !-------------------------------------------------------------------------------
  383.             ! ... BAND_STRUCTURE_INFO
  384.             !-------------------------------------------------------------------------------
  385.             !
  386.             Call qexml_write_bands_info (num_k_points, natomwfc, nbnd, nbnd, nbnd, nspin, nelec, Nint(nelup), Nint(neldw), "Hartree", "2 pi / a", ef=ef/e2, two_fermi_energies=two_fermi_energies, ef_up=ef_up/e2, ef_down=ef_dw/e2, noncolin=noncolin)
  387.             !
  388.             !-------------------------------------------------------------------------------
  389.             ! ... EIGENVALUES
  390.             !-------------------------------------------------------------------------------
  391.             !
  392.             Call qexml_write_bands_pw (nbnd, num_k_points, nspin, xk, wk, wg, et/e2, "Hartree", lkpoint_dir, iunout, dirname)
  393.             !
  394.             !
  395.             If ( .Not. lkpoint_dir) Call iotk_close_write (iunout)
  396.             !
  397.             !-------------------------------------------------------------------------------
  398.             ! ... EIGENVECTORS
  399.             !-------------------------------------------------------------------------------
  400.             !
  401.             Call iotk_write_begin (iunpun, "EIGENVECTORS")
  402.             !
  403.             Call iotk_write_dat (iunpun, "MAX_NUMBER_OF_GK-VECTORS", npwx_g)
  404.             !
  405.         End If
  406.         !
  407.         k_points_loop2: Do ik = 1, num_k_points
  408.         !
  409.             If (ionode) Then
  410.             !
  411.                 Call iotk_write_begin (iunpun, "K-POINT"// TRIM(iotk_index(ik)))
  412.                 !
  413.                 ! ... G+K vectors
  414.                 !
  415.                 Call iotk_write_dat (iunpun, "NUMBER_OF_GK-VECTORS", ngk_g(ik))
  416.                 !
  417.                 If (lwfc) Then
  418.                 !
  419.                     filename = qexml_wfc_filename (".", 'gkvectors', ik, DIR=lkpoint_dir)
  420.                     !
  421.                     Call iotk_link (iunpun, "GK-VECTORS", filename, CREATE=.False., BINARY=.True.)
  422.                     !
  423.                     filename = qexml_wfc_filename (dirname, 'gkvectors', ik, DIR=lkpoint_dir)
  424.                 End If
  425.                 !
  426.             End If
  427.             !
  428.             If (lwfc) Then
  429.             !
  430.                 If ( .Not. smallmem) Call write_gk (iunout, ik, filename)
  431.                 !
  432.                 Call write_this_wfc (iunout, ik)
  433.                 !
  434.             End If
  435.             !
  436.             If (ionode) Then
  437.             !
  438.                 Call iotk_write_end (iunpun, "K-POINT"// TRIM(iotk_index(ik)))
  439.                 !
  440.             End If
  441.             !
  442.         End Do k_points_loop2
  443.         !
  444.         If (ionode) Then
  445.         !
  446.             Call iotk_write_end (iunpun, "EIGENVECTORS")
  447.             !
  448.             Call qexml_closefile ('write', ierr=ierr)
  449.             !
  450.             !
  451.             Call delete_if_present (TRIM(dirname)//'/'//TRIM(xmlpun)//'.bck')
  452.             !
  453.         End If
  454.         !
  455.         Deallocate (igk_l2g)
  456.         Deallocate (igk_l2g_kdip)
  457.         !
  458.         !-------------------------------------------------------------------------------
  459.         ! ... CHARGE-DENSITY FILES
  460.         !-------------------------------------------------------------------------------
  461.         !
  462.         ! ... also writes rho%ns if lda+U and rho%bec if PAW
  463.         !
  464.         If (lrho) Call write_rho (rho, nspin)
  465.         !-------------------------------------------------------------------------------
  466.         ! ... END RESTART SECTIONS
  467.         !-------------------------------------------------------------------------------
  468.         !
  469.         Deallocate (mill_g)
  470.         Deallocate (ngk_g)
  471.         !
  472.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  473.         !
  474.         Call errore ('pw_writefile ', 'cannot save history', ierr)
  475.         !
  476.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  477.         !
  478.         Call errore ('pw_writefile ', 'cannot save history', ierr)
  479.         !
  480.         Return
  481.         !
  482.     Contains
  483.     !
  484.     !--------------------------------------------------------------------
  485.         Subroutine write_gk (iun, ik, filename)
  486.         !--------------------------------------------------------------------
  487.         !
  488. #if defined __HDF5
  489.             Use hdf5_qe, Only: prepare_for_writing_final, write_gkhdf5, gk_hdf5_write, h5fclose_f
  490.             Use io_files, Only: tmp_dir
  491. #endif
  492. !
  493.             Implicit None
  494.             !
  495.             Integer, Intent (In) :: iun, ik
  496.             Character (Len=256), Intent (In) :: filename
  497.             !
  498.             Integer, Allocatable :: igwk (:, :)
  499.             Integer, Allocatable :: itmp (:)
  500.             Character (Len=256) :: filename_hdf5
  501.             Integer :: ierr
  502.             !
  503.             !
  504.             Allocate (igwk(npwx_g, nkstot))
  505.             !
  506.             igwk (:, ik) = 0
  507.             !
  508.             Allocate (itmp(npw_g))
  509.             !
  510.             itmp = 0
  511.             !
  512.             If (ik >= iks .And. ik <= ike) Then
  513.             !
  514.                 Do ig = 1, ngk (ik-iks+1)
  515.                 !
  516.                     itmp (igk_l2g(ig, ik-iks+1)) = igk_l2g (ig, ik-iks+1)
  517.                     !
  518.                 End Do
  519.                 !
  520.             End If
  521.             !
  522.             Call mp_sum (itmp, inter_pool_comm)
  523.             Call mp_sum (itmp, intra_pool_comm)
  524.             !
  525.             ngg = 0
  526.             !
  527.             Do ig = 1, npw_g
  528.             !
  529.                 If (itmp(ig) == ig) Then
  530.                 !
  531.                     ngg = ngg + 1
  532.                     !
  533.                     igwk (ngg, ik) = ig
  534.                     !
  535.                 End If
  536.                 !
  537.             End Do
  538.             !
  539.             Deallocate (itmp)
  540.             !
  541.             If (ionode) Then
  542.             !
  543. #if defined __HDF5
  544.                 filename_hdf5 = TRIM (tmp_dir) // "gk.hdf5"
  545.                 Call prepare_for_writing_final (gk_hdf5_write, inter_pool_comm, filename_hdf5, ik)
  546.                 Call write_gkhdf5 (gk_hdf5_write, xk(:, ik), igwk(1:ngk_g(ik), ik), mill_g(1:3, igwk(1:ngk_g(ik), ik)), ik)
  547.                 Call h5fclose_f (gk_hdf5_write%file_id, ierr)
  548. #else
  549. !
  550.                 Call iotk_open_write (iun, FILE=TRIM(filename), ROOT="GK-VECTORS", BINARY=.True.)
  551.                 !
  552.                 Call iotk_write_dat (iun, "NUMBER_OF_GK-VECTORS", ngk_g(ik))
  553.                 Call iotk_write_dat (iun, "MAX_NUMBER_OF_GK-VECTORS", npwx_g)
  554.                 Call iotk_write_dat (iun, "GAMMA_ONLY", gamma_only)
  555.                 !
  556.                 Call iotk_write_attr (attr, "UNITS", "2 pi / a", FIRST=.True.)
  557.                 Call iotk_write_dat (iun, "K-POINT_COORDS", xk(:, ik), attr=attr)
  558.                 !
  559.                 Call iotk_write_dat (iun, "INDEX", igwk(1:ngk_g(ik), ik))
  560.                 Call iotk_write_dat (iun, "GRID", mill_g(1:3, igwk(1:ngk_g(ik), ik)), COLUMNS=3)
  561.                 !
  562.                 Call iotk_close_write (iun)
  563.                 !
  564. #endif
  565.             End If
  566.             !
  567.             Deallocate (igwk)
  568.             !
  569.         End Subroutine write_gk
  570.         !
  571.         !
  572.         !--------------------------------------------------------------------
  573.         Subroutine write_this_wfc (iun, ik)
  574.         !--------------------------------------------------------------------
  575.         !
  576.             Implicit None
  577.             !
  578.             Integer, Intent (In) :: iun, ik
  579.             Character (Len=256) :: filename
  580.             Integer :: ispin, ik_eff
  581.             !
  582.             ! ... wavefunctions - do not read if already in memory (nsk==1)
  583.             ! ...                 read only if on this pool (iks <= ik <= ike )
  584.             !
  585.             If ((nks > 1) .And. (ik >= iks) .And. (ik <= ike)) Then
  586.                 Call get_buffer (evc, nwordwfc, iunwfc, (ik-iks+1))
  587.             End If
  588.             !
  589.             If (nspin == 2) Then
  590.             !
  591.             ! ... beware: with pools, isk(ik) has the correct value for
  592.             ! ... all k-points only on first pool (ionode proc is ok)
  593.             !
  594.                 ispin = isk (ik)
  595.                 !
  596.                 If (ionode) Then
  597.                 !
  598.                     filename = qexml_wfc_filename (".", 'evc', ik, ispin, DIR=lkpoint_dir)
  599.                     !
  600.                     Call iotk_link (iunpun, "WFC"// TRIM(iotk_index(ispin)), filename, CREATE=.False., BINARY=.True.)
  601.                     !
  602.                     filename = qexml_wfc_filename (dirname, 'evc', ik, ispin, DIR=lkpoint_dir)
  603.                     !
  604.                 End If
  605.                 !
  606.                 Call write_wfc (iunout, ik, nkstot, kunit, ispin, nspin, evc, npw_g, gamma_only, nbnd, igk_l2g_kdip(:, ik-iks+1), ngk(ik-iks+1), filename, 1.D0, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  607.                 !
  608.                 ik_eff = ik + num_k_points
  609.                 !
  610.                 ispin = isk (ik_eff)
  611.                 !
  612.                 ! ... LSDA: now read minority wavefunctions (if not already
  613.                 ! ... in memory and if they are on this pool)
  614.                 !
  615.                 If ((nks > 1) .And. (ik_eff >= iks) .And. (ik_eff <= ike)) Then
  616.                 !
  617.                     Call get_buffer (evc, nwordwfc, iunwfc, (ik_eff-iks+1))
  618.                     !
  619.                 End If
  620.                 !
  621.                 If (ionode) Then
  622.                 !
  623.                     filename = qexml_wfc_filename (".", 'evc', ik, ispin, DIR=lkpoint_dir)
  624.                     !
  625.                     Call iotk_link (iunpun, "WFC"// TRIM(iotk_index(ispin)), filename, CREATE=.False., BINARY=.True.)
  626.                     !
  627.                     filename = qexml_wfc_filename (dirname, 'evc', ik, ispin, DIR=lkpoint_dir)
  628.                     !
  629.                 End If
  630.                 !
  631.                 Call write_wfc (iunout, ik_eff, nkstot, kunit, ispin, nspin, evc, npw_g, gamma_only, nbnd, igk_l2g_kdip(:, ik_eff-iks+1), ngk(ik_eff-iks+1), filename, 1.D0, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  632.                 !
  633.             Else
  634.             !
  635.                 If (noncolin) Then
  636.                 !
  637.                     Do ipol = 1, npol
  638.                     !
  639.                         If (ionode) Then
  640.                         !
  641.                             filename = qexml_wfc_filename (".", 'evc', ik, ipol, DIR=lkpoint_dir)
  642.                             !
  643.                             Call iotk_link (iunpun, "WFC"// TRIM(iotk_index(ipol)), filename, CREATE= .False., BINARY=.True.)
  644.                             !
  645.                             filename = qexml_wfc_filename (dirname, 'evc', ik, ipol, DIR=lkpoint_dir)
  646.                             !
  647.                         End If
  648.                         !
  649.                         ! TEMP  spin-up and spin-down spinor components are written
  650.                         ! TEMP  to different files, like in LSDA - not a smart way
  651.                         !
  652.                         nkl = (ipol-1) * npwx + 1
  653.                         nkr = ipol * npwx
  654.                         Call write_wfc (iunout, ik, nkstot, kunit, ipol, npol, evc(nkl:nkr, :), npw_g, gamma_only, nbnd, igk_l2g_kdip(:, ik-iks+1), ngk(ik-iks+1), filename, 1.D0, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  655.                         !
  656.                     End Do
  657.                     !
  658.                 Else
  659.                 !
  660.                     ispin = 1
  661.                     !
  662.                     If (ionode) Then
  663.                     !
  664.                         filename = qexml_wfc_filename (".", 'evc', ik, DIR=lkpoint_dir)
  665.                         !
  666.                         Call iotk_link (iunpun, "WFC", filename, CREATE=.False., BINARY=.True.)
  667.                         !
  668.                         filename = qexml_wfc_filename (dirname, 'evc', ik, DIR=lkpoint_dir)
  669.                         !
  670.                     End If
  671.                     !
  672.                     Call write_wfc (iunout, ik, nkstot, kunit, ispin, nspin, evc, npw_g, gamma_only, nbnd, igk_l2g_kdip(:, ik-iks+1), ngk(ik-iks+1), filename, 1.D0, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  673.                     !
  674.                 End If
  675.                 !
  676.             End If
  677.             !
  678.         End Subroutine write_this_wfc
  679.         !
  680.     End Subroutine pw_writefile
  681.     !
  682.     !------------------------------------------------------------------------
  683.     Subroutine pw_readfile (what, ierr)
  684.     !------------------------------------------------------------------------
  685.     !
  686.         Use io_rho_xml, Only: read_rho
  687.         Use scf, Only: rho
  688.         Use lsda_mod, Only: nspin
  689.         Use mp_bands, Only: intra_bgrp_comm
  690.         Use mp, Only: mp_sum
  691.         !
  692.         Implicit None
  693.         !
  694.         Character (Len=*), Intent (In) :: what
  695.         Integer, Intent (Out) :: ierr
  696.         !
  697.         Character (Len=256) :: dirname
  698.         Character (Len=80) :: errmsg
  699.         Logical :: lcell, lpw, lions, lspin, linit_mag, lxc, locc, lbz, lbs, lwfc, lheader, lsymm, lrho, lefield, ldim, lef, lexx, lesm
  700.         !
  701.         Integer :: tmp
  702.         !
  703.         ierr = 0
  704.         !
  705.         dirname = TRIM (tmp_dir) // TRIM (prefix) // '.save'
  706.         !
  707.         ! ... look for an empty unit
  708.         !
  709.         Call iotk_free_unit (iunout, ierr)
  710.         !
  711.         Call errore ('pw_readfile', 'no free units to read wavefunctions', ierr)
  712.         !
  713.         lheader = .Not. qexml_version_init
  714.         !
  715.         ldim = .False.
  716.         lcell = .False.
  717.         lpw = .False.
  718.         lions = .False.
  719.         lspin = .False.
  720.         linit_mag = .False.
  721.         lxc = .False.
  722.         locc = .False.
  723.         lbz = .False.
  724.         lbs = .False.
  725.         lwfc = .False.
  726.         lsymm = .False.
  727.         lrho = .False.
  728.         lefield = .False.
  729.         lef = .False.
  730.         lexx = .False.
  731.         lesm = .False.
  732.         !
  733.         Select Case (what)
  734.         Case ('header')
  735.         !
  736.             lheader = .True.
  737.             !
  738.         Case ('dim')
  739.         !
  740.             ldim = .True.
  741.             lbz = .True.
  742.             !
  743.         Case ('pseudo')
  744.         !
  745.             lions = .True.
  746.             !
  747.         Case ('config')
  748.         !
  749.             lcell = .True.
  750.             lions = .True.
  751.             !
  752.         Case ('wave')
  753.         !
  754.             lpw = .True.
  755.             lwfc = .True.
  756.             !
  757.         Case ('nowavenobs')
  758.         !
  759.             lcell = .True.
  760.             lpw = .True.
  761.             lions = .True.
  762.             lspin = .True.
  763.             linit_mag = .True.
  764.             lxc = .True.
  765.             locc = .True.
  766.             lbz = .True.
  767.             lsymm = .True.
  768.             lefield = .True.
  769.             !
  770.         Case ('nowave')
  771.         !
  772.             lcell = .True.
  773.             lpw = .True.
  774.             lions = .True.
  775.             lspin = .True.
  776.             linit_mag = .True.
  777.             lxc = .True.
  778.             locc = .True.
  779.             lbz = .True.
  780.             lbs = .True.
  781.             lsymm = .True.
  782.             lefield = .True.
  783.             !
  784.         Case ('all')
  785.         !
  786.             lcell = .True.
  787.             lpw = .True.
  788.             lions = .True.
  789.             lspin = .True.
  790.             linit_mag = .True.
  791.             lxc = .True.
  792.             locc = .True.
  793.             lbz = .True.
  794.             lbs = .True.
  795.             lwfc = .True.
  796.             lsymm = .True.
  797.             lefield = .True.
  798.             lrho = .True.
  799.             !
  800.         Case ('reset')
  801.         !
  802.             lcell_read = .False.
  803.             lpw_read = .False.
  804.             lions_read = .False.
  805.             lspin_read = .False.
  806.             lstarting_mag_read = .False.
  807.             lxc_read = .False.
  808.             locc_read = .False.
  809.             lbz_read = .False.
  810.             lbs_read = .False.
  811.             lwfc_read = .False.
  812.             lsymm_read = .False.
  813.             lefield_read = .False.
  814.             !
  815.         Case ('ef')
  816.         !
  817.             lef = .True.
  818.             !
  819.         Case ('exx')
  820.         !
  821.             lexx = .True.
  822.             !
  823.         Case ('esm')
  824.         !
  825.             lesm = .True.
  826.             !
  827.         Case Default
  828.         !
  829.             Call errore ('pw_readfile', 'unknown case '//TRIM(what), 1)
  830.             !
  831.         End Select
  832.         !
  833.         If ( .Not. lheader .And. .Not. qexml_version_init) Call errore ('pw_readfile', 'qexml version not set', 71)
  834.         !
  835.         If (ionode) Then
  836.         !
  837.             Call qexml_init (iunpun)
  838.             Call qexml_openfile (TRIM(dirname)//'/'//TRIM(xmlpun), 'read', BINARY=.False., ierr=ierr)
  839.             !
  840.         End If
  841.         !
  842.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  843.         If (ierr /= 0) Then
  844.             errmsg = 'error opening xml data file'
  845.             Go To 100
  846.         End If
  847.         !
  848.         If (lheader) Then
  849.         !
  850.             Call read_header (ierr)
  851.             If (ierr > 0) Then
  852.                 errmsg = 'error reading header of xml data file'
  853.                 Go To 100
  854.             End If
  855.             !
  856.         End If
  857.         !
  858.         If (ldim) Then
  859.         !
  860.             Call read_dim (ierr)
  861.             If (ierr > 0) Then
  862.                 errmsg = 'error reading dimensions in xml data file'
  863.                 Go To 100
  864.             End If
  865.             !
  866.         End If
  867.         !
  868.         If (lcell) Then
  869.         !
  870.             Call read_cell (ierr)
  871.             If (ierr > 0) Then
  872.                 errmsg = 'error reading cell info in xml data file'
  873.                 Go To 100
  874.             End If
  875.             !
  876.         End If
  877.         If (lpw) Then
  878.         !
  879.             Call read_planewaves (ierr)
  880.             If (ierr > 0) Then
  881.                 errmsg = 'error reading plane-wave info in xml data file'
  882.                 Go To 100
  883.             End If
  884.             !
  885.         End If
  886.         If (lions) Then
  887.         !
  888.             Call read_ions (dirname, ierr)
  889.             If (ierr > 0) Then
  890.                 errmsg = 'error reading info on ions in xml data file'
  891.                 Go To 100
  892.             End If
  893.             !
  894.         End If
  895.         If (lspin) Then
  896.         !
  897.             Call read_spin (ierr)
  898.             If (ierr > 0) Then
  899.                 errmsg = 'error reading spin in xml data file'
  900.                 Go To 100
  901.             End If
  902.             !
  903.         End If
  904.         If (linit_mag) Then
  905.         !
  906.             Call read_magnetization (ierr)
  907.             If (ierr > 0) Then
  908.                 errmsg = 'error reading magnetization in xml data file'
  909.                 Go To 100
  910.             End If
  911.             !
  912.         End If
  913.         If (lxc) Then
  914.         !
  915.             Call read_xc (ierr)
  916.             If (ierr > 0) Then
  917.                 errmsg = 'error reading XC functional in xml data file'
  918.                 Go To 100
  919.             End If
  920.             !
  921.         End If
  922.         If (locc) Then
  923.         !
  924.             Call read_occupations (ierr)
  925.             If (ierr > 0) Then
  926.                 errmsg = 'error reading occupation numbers in xml data file'
  927.                 Go To 100
  928.             End If
  929.             !
  930.         End If
  931.         If (lbz) Then
  932.         !
  933.             Call read_brillouin_zone (ierr)
  934.             If (ierr > 0) Then
  935.                 errmsg = 'error reading Brillouin Zone in xml data file'
  936.                 Go To 100
  937.             End If
  938.             !
  939.         End If
  940.         If (lbs) Then
  941.         !
  942.             Call read_band_structure (dirname, ierr)
  943.             If (ierr > 0) Then
  944.                 errmsg = 'error reading band structure in xml data file'
  945.                 Go To 100
  946.             End If
  947.             !
  948.         End If
  949.         If (lwfc) Then
  950.         !
  951.             Call read_wavefunctions (dirname, ierr)
  952.             If (ierr > 0) Then
  953.                 errmsg = 'error reading wavefunctions in xml data file'
  954.                 Go To 100
  955.             End If
  956.             !
  957.         End If
  958.         If (lsymm) Then
  959.         !
  960.             Call read_symmetry (ierr)
  961.             If (ierr > 0) Then
  962.                 errmsg = 'error reading symmetry in xml data file'
  963.                 Go To 100
  964.             End If
  965.             !
  966.         End If
  967.         If (lefield) Then
  968.         !
  969.             Call read_efield (ierr)
  970.             If (ierr > 0) Then
  971.                 errmsg = 'error reading electric fields in xml data file'
  972.                 Go To 100
  973.             End If
  974.             !
  975.         End If
  976.         !
  977.         If (lrho) Then
  978.         !
  979.         ! ... to read the charge-density we use the routine from io_rho_xml
  980.         ! ... it also reads ns for ldaU and becsum for PAW
  981.         !
  982.             Call read_rho (rho, nspin)
  983.             !
  984.         End If
  985.         !
  986.         If (lef) Then
  987.         !
  988.             Call read_ef (ierr)
  989.             If (ierr > 0) Then
  990.                 errmsg = 'error reading Fermi energy and number of electrons in xml data file'
  991.                 Go To 100
  992.             End If
  993.             !
  994.         End If
  995.         If (lexx) Then
  996.         !
  997.             Call read_exx (ierr)
  998.             If (ierr > 0) Then
  999.                 errmsg = 'error reading hybrid functional in xml data file'
  1000.                 Go To 100
  1001.             End If
  1002.             !
  1003.         End If
  1004.         If (lesm) Then
  1005.         !
  1006.             Call read_esm (ierr)
  1007.             If (ierr > 0) Then
  1008.                 errmsg = 'error reading ESM restart data in xml data file'
  1009.                 Go To 100
  1010.             End If
  1011.             !
  1012.         End If
  1013.         !
  1014.         If (ionode) Then
  1015.         !
  1016.             Call qexml_closefile ('read', ierr=ierr)
  1017.             !
  1018.         End If
  1019.         !
  1020.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1021.         If (ierr > 0) Then
  1022.             errmsg = 'error closing xml data file'
  1023.             Go To 100
  1024.         End If
  1025.         !
  1026.         !
  1027.         Return
  1028.         !
  1029.         ! uncomment to continue execution after an error occurs
  1030.         ! 100 IF (ionode) THEN
  1031.         !        CALL qexml_closefile( 'read', IERR=tmp)
  1032.         !     ENDIF
  1033.         !     RETURN
  1034.         ! comment to continue execution after an error occurs
  1035.         !
  1036.         !
  1037. 100      Call errore ('pw_readfile', TRIM(errmsg), ierr)
  1038. !
  1039.     End Subroutine pw_readfile
  1040.     !
  1041.     !------------------------------------------------------------------------
  1042.     Subroutine read_header (ierr)
  1043.     !------------------------------------------------------------------------
  1044.     !
  1045.     ! ... this routine reads the format version of the current xml datafile
  1046.     !
  1047.         Implicit None
  1048.         !
  1049.         Integer, Intent (Out) :: ierr
  1050.         !
  1051.         ierr = 0
  1052.         !
  1053.         If (qexml_version_init) Return
  1054.         !
  1055.         If (ionode) Then
  1056.         !
  1057.             Call qexml_read_header (FORMAT_VERSION=qexml_version, ierr=ierr)
  1058.             !
  1059.             qexml_version_init = .True.
  1060.             !
  1061.         End If
  1062.         !
  1063.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1064.         !
  1065.         If (ierr /= 0) Return
  1066.         !
  1067.         Call mp_bcast (qexml_version, ionode_id, intra_image_comm)
  1068.         Call mp_bcast (qexml_version_init, ionode_id, intra_image_comm)
  1069.         !
  1070.         !
  1071.     End Subroutine read_header
  1072.     !
  1073.     !------------------------------------------------------------------------
  1074.     Subroutine read_dim (ierr)
  1075.     !------------------------------------------------------------------------
  1076.     !
  1077.     ! ... this routine collects array dimensions from various sections
  1078.     ! ... plus with some other variables needed for array allocation
  1079.     !
  1080.         Use ions_base, Only: nat, nsp
  1081.         Use symm_base, Only: nsym
  1082.         Use gvect, Only: ngm_g, ecutrho
  1083.         Use fft_base, Only: dfftp
  1084.         Use gvecs, Only: ngms_g, dual
  1085.         Use fft_base, Only: dffts
  1086.         Use lsda_mod, Only: lsda
  1087.         Use noncollin_module, Only: noncolin
  1088.         Use ktetra, Only: ntetra
  1089.         Use klist, Only: nkstot, nelec
  1090.         Use wvfct, Only: nbnd, npwx
  1091.         Use gvecw, Only: ecutwfc
  1092.         Use control_flags, Only: gamma_only
  1093.         Use mp_pools, Only: kunit
  1094.         Use mp_global, Only: nproc_file, nproc_pool_file, nproc_image_file, ntask_groups_file, nproc_bgrp_file, nproc_ortho_file
  1095.         !
  1096.         Implicit None
  1097.         !
  1098.         !CHARACTER(LEN=*), INTENT(IN)  :: dirname
  1099.         Integer, Intent (Out) :: ierr
  1100.         !
  1101.         Integer :: npwx_
  1102.         Logical :: found, found2
  1103.         Character (iotk_attlenx) :: attr
  1104.         !
  1105.         !
  1106.         ! ... first the entire CELL section is read
  1107.         ! ...
  1108.         ierr = 0
  1109.         !
  1110.         Call read_cell (ierr)
  1111.         If (ierr /= 0) Go To 100
  1112.         !
  1113.         If (ionode) Then
  1114.         !
  1115.             Call qexml_read_ions (nat=nat, nsp=nsp, ierr=ierr)
  1116.             If (ierr /= 0) Go To 100
  1117.             !
  1118.             Call qexml_read_symmetry (nsym=nsym, found=found, ierr=ierr)
  1119.             If (ierr /= 0) Go To 100
  1120.             !
  1121.             If ( .Not. found) Then
  1122.             !
  1123.                 nsym = 1
  1124.                 !
  1125.             End If
  1126.             !
  1127.             Call qexml_read_planewaves (ecutwfc=ecutwfc, ecutrho=ecutrho, npwx=npwx_, gamma_only=gamma_only, nr1=dfftp%nr1, nr2=dfftp%nr2, nr3=dfftp%nr3, ngm=ngm_g, NR1S=dffts%nr1, NR2S=dffts%nr2, NR3S=dffts%nr3, NGMS=ngms_g, ierr=ierr)
  1128.             If (ierr /= 0) Go To 100
  1129.             !
  1130.             ecutwfc = ecutwfc * e2
  1131.             ecutrho = ecutrho * e2
  1132.             !
  1133.             dual = ecutrho / ecutwfc
  1134.             !
  1135.             Call qexml_read_spin (lsda=lsda, noncolin=noncolin, ierr=ierr)
  1136.             If (ierr /= 0) Go To 100
  1137.             !
  1138.             Call qexml_read_occ (ntetra=ntetra, ierr=ierr)
  1139.             If (ierr /= 0) Go To 100
  1140.             !
  1141.             Call qexml_read_bz (num_k_points=nkstot, ierr=ierr)
  1142.             If (ierr /= 0) Go To 100
  1143.             !
  1144.             If (lsda) nkstot = nkstot * 2
  1145.             !
  1146.             Call qexml_read_bands_info (nbnd=nbnd, nelec=nelec, ierr=ierr)
  1147.             If (ierr /= 0) Go To 100
  1148.             !
  1149.             Call qexml_read_para (kunit=kunit, nproc=nproc_file, nproc_pool=nproc_pool_file, nproc_image=nproc_image_file, ntask_groups=ntask_groups_file, nproc_bgrp=nproc_bgrp_file, nproc_ortho=nproc_ortho_file, found=found, ierr=ierr)
  1150.             If (ierr /= 0) Go To 100
  1151.             !
  1152.             If ( .Not. found) Then
  1153.             !
  1154.                 kunit = 1
  1155.                 nproc_file = 1
  1156.                 nproc_pool_file = 1
  1157.                 nproc_image_file = 1
  1158.                 ntask_groups_file = 1
  1159.                 nproc_bgrp_file = 1
  1160.                 nproc_ortho_file = 1
  1161.                 !
  1162.             End If
  1163.             !
  1164.         End If
  1165.         !
  1166. 100      Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1167. !
  1168.         If (ierr > 0) Return
  1169.         !
  1170.         Call mp_bcast (nat, ionode_id, intra_image_comm)
  1171.         Call mp_bcast (nsp, ionode_id, intra_image_comm)
  1172.         Call mp_bcast (nsym, ionode_id, intra_image_comm)
  1173.         Call mp_bcast (ecutwfc, ionode_id, intra_image_comm)
  1174.         Call mp_bcast (ecutrho, ionode_id, intra_image_comm)
  1175.         Call mp_bcast (dual, ionode_id, intra_image_comm)
  1176.         Call mp_bcast (npwx_, ionode_id, intra_image_comm)
  1177.         Call mp_bcast (gamma_only, ionode_id, intra_image_comm)
  1178.         Call mp_bcast (dfftp%nr1, ionode_id, intra_image_comm)
  1179.         Call mp_bcast (dfftp%nr2, ionode_id, intra_image_comm)
  1180.         Call mp_bcast (dfftp%nr3, ionode_id, intra_image_comm)
  1181.         Call mp_bcast (ngm_g, ionode_id, intra_image_comm)
  1182.         Call mp_bcast (dffts%nr1, ionode_id, intra_image_comm)
  1183.         Call mp_bcast (dffts%nr2, ionode_id, intra_image_comm)
  1184.         Call mp_bcast (dffts%nr3, ionode_id, intra_image_comm)
  1185.         Call mp_bcast (ngms_g, ionode_id, intra_image_comm)
  1186.         Call mp_bcast (lsda, ionode_id, intra_image_comm)
  1187.         Call mp_bcast (noncolin, ionode_id, intra_image_comm)
  1188.         Call mp_bcast (ntetra, ionode_id, intra_image_comm)
  1189.         Call mp_bcast (nkstot, ionode_id, intra_image_comm)
  1190.         Call mp_bcast (nelec, ionode_id, intra_image_comm)
  1191.         Call mp_bcast (nbnd, ionode_id, intra_image_comm)
  1192.         Call mp_bcast (kunit, ionode_id, intra_image_comm)
  1193.         Call mp_bcast (nproc_file, ionode_id, intra_image_comm)
  1194.         Call mp_bcast (nproc_pool_file, ionode_id, intra_image_comm)
  1195.         Call mp_bcast (nproc_image_file, ionode_id, intra_image_comm)
  1196.         Call mp_bcast (ntask_groups_file, ionode_id, intra_image_comm)
  1197.         Call mp_bcast (nproc_bgrp_file, ionode_id, intra_image_comm)
  1198.         Call mp_bcast (nproc_ortho_file, ionode_id, intra_image_comm)
  1199.         !
  1200.         Return
  1201.         !
  1202.     End Subroutine read_dim
  1203.     !
  1204.     !--------------------------------------------------------------------------
  1205.     Subroutine read_cell (ierr)
  1206.     !------------------------------------------------------------------------
  1207.     !
  1208.         Use run_info, Only: title
  1209.         Use cell_base, Only: ibrav, alat, at, bg, celldm
  1210.         Use cell_base, Only: tpiba, tpiba2, omega
  1211.         Use cellmd, Only: lmovecell, cell_factor
  1212.         Use control_flags, Only: do_makov_payne
  1213.         Use martyna_tuckerman, Only: do_comp_mt
  1214.         Use esm, Only: do_comp_esm
  1215.         !
  1216.         !
  1217.         Implicit None
  1218.         !
  1219.         Integer, Intent (Out) :: ierr
  1220.         !
  1221.         Character (Len=80) :: bravais_lattice, es_corr
  1222.         !
  1223.         !
  1224.         ierr = 0
  1225.         If (lcell_read) Return
  1226.         !
  1227.         If (ionode) Then
  1228.         !
  1229.             Call qexml_read_cell (bravais_lattice=bravais_lattice, celldm=celldm, alat=alat, A1=at(:, 1), A2=at(:, 2), A3=at(:, 3), es_corr=es_corr, ierr=ierr)
  1230.             !
  1231.         End If
  1232.         !
  1233.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1234.         If (ierr > 0) Return
  1235.         !
  1236.         If (ionode) Then
  1237.         !
  1238.             Select Case (TRIM(es_corr))
  1239.             Case ("Makov-Payne")
  1240.                 do_makov_payne = .True.
  1241.                 do_comp_mt = .False.
  1242.                 do_comp_esm = .False.
  1243.             Case ("Martyna-Tuckerman")
  1244.                 do_makov_payne = .False.
  1245.                 do_comp_mt = .True.
  1246.                 do_comp_esm = .False.
  1247.             Case ("ESM")
  1248.                 do_makov_payne = .False.
  1249.                 do_comp_mt = .False.
  1250.                 do_comp_esm = .True.
  1251.             Case ("None")
  1252.                 do_makov_payne = .False.
  1253.                 do_comp_mt = .False.
  1254.                 do_comp_esm = .False.
  1255.             End Select
  1256.             !
  1257.             Select Case (TRIM(bravais_lattice))
  1258.             Case ("free")
  1259.                 ibrav = 0
  1260.             Case ("cubic P (sc)")
  1261.                 ibrav = 1
  1262.             Case ("cubic F (fcc)")
  1263.                 ibrav = 2
  1264.             Case ("cubic I (bcc)")
  1265.                 ibrav = 3
  1266.             Case ("Hexagonal and Trigonal P")
  1267.                 ibrav = 4
  1268.             Case ("Trigonal R")
  1269.                 ibrav = 5
  1270.             Case ("Tetragonal P (st)")
  1271.                 ibrav = 6
  1272.             Case ("Tetragonal I (bct)")
  1273.                 ibrav = 7
  1274.             Case ("Orthorhombic P")
  1275.                 ibrav = 8
  1276.             Case ("Orthorhombic base-centered(bco)")
  1277.                 ibrav = 9
  1278.             Case ("Orthorhombic face-centered")
  1279.                 ibrav = 10
  1280.             Case ("Orthorhombic body-centered")
  1281.                 ibrav = 11
  1282.             Case ("Monoclinic P")
  1283.                 ibrav = 12
  1284.             Case ("Monoclinic base-centered")
  1285.                 ibrav = 13
  1286.             Case ("Triclinic P")
  1287.                 ibrav = 14
  1288.             Case Default
  1289.                 ibrav = 0
  1290.             End Select
  1291.             !
  1292.             ! ... some internal variables
  1293.             !
  1294.             tpiba = 2.D0 * PI / alat
  1295.             tpiba2 = tpiba ** 2
  1296.             !
  1297.             ! ... to alat units
  1298.             !
  1299.             at (:, :) = at (:, :) / alat
  1300.             !
  1301.             Call volume (alat, at(1, 1), at(1, 2), at(1, 3), omega)
  1302.             !
  1303.             ! ... Generate the reciprocal lattice vectors
  1304.             !
  1305.             Call recips (at(1, 1), at(1, 2), at(1, 3), bg(1, 1), bg(1, 2), bg(1, 3))
  1306.             !
  1307.             Call qexml_read_moving_cell (lmovecell, cell_factor, ierr)
  1308.             !
  1309.             !
  1310.         End If
  1311.         !
  1312.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1313.         If (ierr > 0) Return
  1314.         !
  1315.         Call mp_bcast (ibrav, ionode_id, intra_image_comm)
  1316.         Call mp_bcast (alat, ionode_id, intra_image_comm)
  1317.         Call mp_bcast (celldm, ionode_id, intra_image_comm)
  1318.         Call mp_bcast (tpiba, ionode_id, intra_image_comm)
  1319.         Call mp_bcast (tpiba2, ionode_id, intra_image_comm)
  1320.         Call mp_bcast (omega, ionode_id, intra_image_comm)
  1321.         Call mp_bcast (at, ionode_id, intra_image_comm)
  1322.         Call mp_bcast (bg, ionode_id, intra_image_comm)
  1323.         Call mp_bcast (do_makov_payne, ionode_id, intra_image_comm)
  1324.         Call mp_bcast (do_comp_mt, ionode_id, intra_image_comm)
  1325.         Call mp_bcast (do_comp_esm, ionode_id, intra_image_comm)
  1326.         Call mp_bcast (lmovecell, ionode_id, intra_image_comm)
  1327.         If (lmovecell) Then
  1328.             Call mp_bcast (cell_factor, ionode_id, intra_image_comm)
  1329.         Else
  1330.             cell_factor = 1.0_DP
  1331.         End If
  1332.         !
  1333.         title = ' '
  1334.         !
  1335.         lcell_read = .True.
  1336.         !
  1337.         Return
  1338.         !
  1339.     End Subroutine read_cell
  1340.     !
  1341.     !
  1342.     !------------------------------------------------------------------------
  1343.     Subroutine read_ions (dirname, ierr)
  1344.     !------------------------------------------------------------------------
  1345.     !
  1346.         Use ions_base, Only: nat, nsp, ityp, amass, atm, tau, if_pos
  1347.         Use cell_base, Only: alat
  1348.         Use io_files, Only: psfile, pseudo_dir, pseudo_dir_cur
  1349.         !
  1350.         Implicit None
  1351.         !
  1352.         Character (Len=*), Intent (In) :: dirname
  1353.         Integer, Intent (Out) :: ierr
  1354.         !
  1355.         Integer :: i
  1356.         Logical :: exst
  1357.         !
  1358.         ierr = 0
  1359.         If (lions_read) Return
  1360.         !
  1361.         If ( .Not. lcell_read) Call errore ('read_ions', 'read cell first', 1)
  1362.         !
  1363.         ! this is where PP files should be read from
  1364.         !
  1365.         pseudo_dir_cur = trimcheck (dirname)
  1366.         !
  1367.         If (ionode) Then
  1368.         !
  1369.             Call qexml_read_ions (nsp=nsp, nat=nat, atm=atm, ityp=ityp, psfile=psfile, amass=amass, tau=tau, if_pos=if_pos, pseudo_dir=pseudo_dir, ierr=ierr)
  1370.             !
  1371.         End If
  1372.         !
  1373.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1374.         !
  1375.         If (ierr > 0) Return
  1376.         !
  1377.         If (ionode) Then
  1378.         !
  1379.             Do i = 1, nat
  1380.             !
  1381.                 tau (:, i) = tau (:, i) / alat
  1382.                 !
  1383.             End Do
  1384.             !
  1385.         End If
  1386.         !
  1387.         Call mp_bcast (nat, ionode_id, intra_image_comm)
  1388.         Call mp_bcast (nsp, ionode_id, intra_image_comm)
  1389.         Call mp_bcast (atm, ionode_id, intra_image_comm)
  1390.         Call mp_bcast (amass, ionode_id, intra_image_comm)
  1391.         Call mp_bcast (psfile, ionode_id, intra_image_comm)
  1392.         Call mp_bcast (pseudo_dir, ionode_id, intra_image_comm)
  1393.         Call mp_bcast (ityp, ionode_id, intra_image_comm)
  1394.         Call mp_bcast (tau, ionode_id, intra_image_comm)
  1395.         Call mp_bcast (if_pos, ionode_id, intra_image_comm)
  1396.         !
  1397.         lions_read = .True.
  1398.         !
  1399.         Return
  1400.         !
  1401.     End Subroutine read_ions
  1402.     !
  1403.     !------------------------------------------------------------------------
  1404.     Subroutine read_symmetry (ierr)
  1405.     !------------------------------------------------------------------------
  1406.     !
  1407.         Use symm_base, Only: nrot, nsym, invsym, s, ft, ftau, irt, t_rev, sname, sr, invs, inverse_s, s_axis_to_cart, time_reversal, no_t_rev
  1408.         Use control_flags, Only: noinv
  1409.         Use fft_base, Only: dfftp
  1410.         !
  1411.         Implicit None
  1412.         !
  1413.         Integer, Intent (Out) :: ierr
  1414.         Character (iotk_attlenx) :: attr
  1415.         !
  1416.         Integer :: i
  1417.         Logical :: found
  1418.         !
  1419.         ierr = 0
  1420.         If (lsymm_read) Return
  1421.         !
  1422.         If ( .Not. lpw_read) Call errore ('read_symmetry', 'read planewaves first', 1)
  1423.         !
  1424.         If (ionode) Then
  1425.         !
  1426.             Call qexml_read_symmetry (nsym=nsym, nrot=nrot, invsym=invsym, noinv=noinv, time_reversal=time_reversal, no_t_rev=no_t_rev, TRASL=ft, s=s, sname=sname, t_rev=t_rev, irt=irt, found=found, ierr=ierr)
  1427.             !
  1428.         End If
  1429.         !
  1430.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1431.         !
  1432.         If (ierr > 0) Return
  1433.         !
  1434.         If (ionode) Then
  1435.         !
  1436.             If ( .Not. found) Then
  1437.             !
  1438.                 nsym = 1
  1439.                 s (:, :, nsym) = 0
  1440.                 s (1, 1, nsym) = 1
  1441.                 s (2, 2, nsym) = 1
  1442.                 s (3, 3, nsym) = 1
  1443.                 sr (:, :, nsym) = DBLE (s(:, :, nsym))
  1444.                 ftau (:, nsym) = 0
  1445.                 ft (:, nsym) = 0.0_DP
  1446.                 sname (nsym) = 'identity'
  1447.                 Do i = 1, SIZE (irt, 2)
  1448.                     irt (nsym, i) = i
  1449.                 End Do
  1450.                 invsym = .False.
  1451.                 noinv = .False.
  1452.                 t_rev (nsym) = 0
  1453.                 invs (1) = 1
  1454.                 time_reversal = .True.
  1455.                 no_t_rev = .False.
  1456.                 !
  1457.             Else
  1458.             !
  1459.                 Do i = 1, nsym
  1460.                 !
  1461.                     ftau (1, i) = Nint (ft(1, i)*DBLE(dfftp%nr1))
  1462.                     ftau (2, i) = Nint (ft(2, i)*DBLE(dfftp%nr2))
  1463.                     ftau (3, i) = Nint (ft(3, i)*DBLE(dfftp%nr3))
  1464.                     !
  1465.                 End Do
  1466.                 !
  1467.                 ! indices of inverse operations and matrices in cartesian axis
  1468.                 ! are not saved to disk (maybe they should), are recalculated here
  1469.                 !
  1470.                 Call inverse_s ()
  1471.                 Call s_axis_to_cart ()
  1472.                 !
  1473.             End If
  1474.             !
  1475.             !
  1476.         End If
  1477.         !
  1478.         Call mp_bcast (nsym, ionode_id, intra_image_comm)
  1479.         Call mp_bcast (nrot, ionode_id, intra_image_comm)
  1480.         Call mp_bcast (invsym, ionode_id, intra_image_comm)
  1481.         Call mp_bcast (noinv, ionode_id, intra_image_comm)
  1482.         Call mp_bcast (time_reversal, ionode_id, intra_image_comm)
  1483.         Call mp_bcast (no_t_rev, ionode_id, intra_image_comm)
  1484.         Call mp_bcast (s, ionode_id, intra_image_comm)
  1485.         Call mp_bcast (ftau, ionode_id, intra_image_comm)
  1486.         Call mp_bcast (ft, ionode_id, intra_image_comm)
  1487.         Call mp_bcast (sname, ionode_id, intra_image_comm)
  1488.         Call mp_bcast (irt, ionode_id, intra_image_comm)
  1489.         Call mp_bcast (t_rev, ionode_id, intra_image_comm)
  1490.         Call mp_bcast (invs, ionode_id, intra_image_comm)
  1491.         Call mp_bcast (sr, ionode_id, intra_image_comm)
  1492.         !
  1493.         lsymm_read = .True.
  1494.         !
  1495.         Return
  1496.         !
  1497.     End Subroutine read_symmetry
  1498.     !
  1499.     !------------------------------------------------------------------------
  1500.     Subroutine read_efield (ierr)
  1501.     !----------------------------------------------------------------------
  1502.     !
  1503.         Use extfield, Only: tefield, dipfield, edir, emaxpos, eopreg, eamp, monopole, zmon, relaxz, block, block_1, block_2, block_height
  1504.         !
  1505.         Implicit None
  1506.         !
  1507.         Integer, Intent (Out) :: ierr
  1508.         Logical :: found
  1509.         !
  1510.         ierr = 0
  1511.         If (lefield_read) Return
  1512.         !
  1513.         !
  1514.         If (ionode) Then
  1515.         !
  1516.             Call qexml_read_efield (tefield=tefield, dipfield=dipfield, edir=edir, emaxpos=emaxpos, eopreg=eopreg, eamp=eamp, monopole=monopole, zmon=zmon, relaxz=relaxz, block=block, block_1=block_1, block_2=block_2, block_height=block_height, found=found, ierr=ierr)
  1517.         End If
  1518.         !
  1519.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1520.         !
  1521.         If (ierr > 0) Return
  1522.         !
  1523.         If ((ionode) .And. ( .Not. found)) Then
  1524.         !
  1525.             tefield = .False.
  1526.             dipfield = .False.
  1527.             monopole = .False.
  1528.             !
  1529.         End If
  1530.         !
  1531.         Call mp_bcast (tefield, ionode_id, intra_image_comm)
  1532.         Call mp_bcast (dipfield, ionode_id, intra_image_comm)
  1533.         Call mp_bcast (edir, ionode_id, intra_image_comm)
  1534.         Call mp_bcast (emaxpos, ionode_id, intra_image_comm)
  1535.         Call mp_bcast (eopreg, ionode_id, intra_image_comm)
  1536.         Call mp_bcast (eamp, ionode_id, intra_image_comm)
  1537.         Call mp_bcast (monopole, ionode_id, intra_image_comm)
  1538.         Call mp_bcast (zmon, ionode_id, intra_image_comm)
  1539.         Call mp_bcast (relaxz, ionode_id, intra_image_comm)
  1540.         Call mp_bcast (block, ionode_id, intra_image_comm)
  1541.         Call mp_bcast (block_1, ionode_id, intra_image_comm)
  1542.         Call mp_bcast (block_2, ionode_id, intra_image_comm)
  1543.         Call mp_bcast (block_height, ionode_id, intra_image_comm)
  1544.         !
  1545.         lefield_read = .True.
  1546.         !
  1547.         Return
  1548.         !
  1549.     End Subroutine read_efield
  1550.     !
  1551.     !------------------------------------------------------------------------
  1552.     Subroutine read_planewaves (ierr)
  1553.     !------------------------------------------------------------------------
  1554.     !
  1555.         Use gvect, Only: ngm_g, ecutrho
  1556.         Use gvecs, Only: ngms_g, dual
  1557.         Use gvecw, Only: ecutwfc
  1558.         Use fft_base, Only: dfftp
  1559.         Use fft_base, Only: dffts
  1560.         Use wvfct, Only: npwx
  1561.         Use control_flags, Only: gamma_only
  1562.         !
  1563.         Implicit None
  1564.         !
  1565.         Integer, Intent (Out) :: ierr
  1566.         !
  1567.         Integer :: npwx_
  1568.         !
  1569.         ierr = 0
  1570.         If (lpw_read) Return
  1571.         !
  1572.         !
  1573.         If (ionode) Call qexml_read_planewaves (ecutwfc=ecutwfc, ecutrho=ecutrho, npwx=npwx_, gamma_only=gamma_only, nr1=dfftp%nr1, nr2=dfftp%nr2, nr3=dfftp%nr3, ngm=ngm_g, NR1S=dffts%nr1, NR2S=dffts%nr2, NR3S=dffts%nr3, NGMS=ngms_g, ierr=ierr)
  1574.         !
  1575.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1576.         !
  1577.         If (ierr > 0) Return
  1578.         !
  1579.         If (ionode) Then
  1580.         !
  1581.             ecutwfc = ecutwfc * e2
  1582.             ecutrho = ecutrho * e2
  1583.             !
  1584.             dual = ecutrho / ecutwfc
  1585.             !
  1586.         End If
  1587.         !
  1588.         Call mp_bcast (ecutwfc, ionode_id, intra_image_comm)
  1589.         Call mp_bcast (ecutrho, ionode_id, intra_image_comm)
  1590.         Call mp_bcast (dual, ionode_id, intra_image_comm)
  1591.         Call mp_bcast (npwx_, ionode_id, intra_image_comm)
  1592.         Call mp_bcast (gamma_only, ionode_id, intra_image_comm)
  1593.         Call mp_bcast (dfftp%nr1, ionode_id, intra_image_comm)
  1594.         Call mp_bcast (dfftp%nr2, ionode_id, intra_image_comm)
  1595.         Call mp_bcast (dfftp%nr3, ionode_id, intra_image_comm)
  1596.         Call mp_bcast (ngm_g, ionode_id, intra_image_comm)
  1597.         Call mp_bcast (dffts%nr1, ionode_id, intra_image_comm)
  1598.         Call mp_bcast (dffts%nr2, ionode_id, intra_image_comm)
  1599.         Call mp_bcast (dffts%nr3, ionode_id, intra_image_comm)
  1600.         Call mp_bcast (ngms_g, ionode_id, intra_image_comm)
  1601.         !
  1602.         lpw_read = .True.
  1603.         !
  1604.         Return
  1605.         !
  1606.     End Subroutine read_planewaves
  1607.     !
  1608.     !------------------------------------------------------------------------
  1609.     Subroutine read_spin (ierr)
  1610.     !------------------------------------------------------------------------
  1611.     !
  1612.         Use spin_orb, Only: lspinorb, domag
  1613.         Use lsda_mod, Only: nspin, lsda
  1614.         Use noncollin_module, Only: noncolin, npol
  1615.         !
  1616.         Implicit None
  1617.         !
  1618.         Integer, Intent (Out) :: ierr
  1619.         !
  1620.         Logical :: found
  1621.         !
  1622.         ierr = 0
  1623.         If (lspin_read) Return
  1624.         !
  1625.         If (ionode) Then
  1626.         !
  1627.             Call qexml_read_spin (lsda, noncolin, npol, lspinorb, domag, ierr)
  1628.             !
  1629.             If (lsda) Then
  1630.             !
  1631.                 nspin = 2
  1632.                 !
  1633.             Else If (noncolin) Then
  1634.             !
  1635.                 nspin = 4
  1636.                 !
  1637.             Else
  1638.             !
  1639.                 nspin = 1
  1640.                 !
  1641.             End If
  1642.             !
  1643.         End If
  1644.         !
  1645.         Call mp_bcast (lsda, ionode_id, intra_image_comm)
  1646.         Call mp_bcast (nspin, ionode_id, intra_image_comm)
  1647.         Call mp_bcast (noncolin, ionode_id, intra_image_comm)
  1648.         Call mp_bcast (npol, ionode_id, intra_image_comm)
  1649.         Call mp_bcast (lspinorb, ionode_id, intra_image_comm)
  1650.         Call mp_bcast (domag, ionode_id, intra_image_comm)
  1651.         !
  1652.         lspin_read = .True.
  1653.         !
  1654.         Return
  1655.         !
  1656.     End Subroutine read_spin
  1657.     !
  1658.     !--------------------------------------------------------------------------
  1659.     Subroutine read_magnetization (ierr)
  1660.     !------------------------------------------------------------------------
  1661.     !
  1662.         Use klist, Only: two_fermi_energies, nelup, neldw
  1663.         Use ener, Only: ef_up, ef_dw
  1664.         Use lsda_mod, Only: starting_magnetization
  1665.         Use noncollin_module, Only: angle1, angle2, i_cons, mcons, bfield, lambda
  1666.         !
  1667.         Implicit None
  1668.         !
  1669.         Integer, Intent (Out) :: ierr
  1670.         !
  1671.         Logical :: found
  1672.         Integer :: i, nsp
  1673.         !
  1674.         ierr = 0
  1675.         If (lstarting_mag_read) Return
  1676.         !
  1677.         !
  1678.         If (ionode) Then
  1679.         !
  1680.             Call qexml_read_magnetization (starting_magnetization=starting_magnetization, angle1=angle1, angle2=angle2, two_fermi_energies=two_fermi_energies, i_cons=i_cons, mcons=mcons, bfield=bfield, ef_up=ef_up, ef_dw=ef_dw, nelup=nelup, neldw=neldw, lambda=lambda, found=found, ierr=ierr)
  1681.             !
  1682.             angle1 (:) = angle1 (:) * PI / 180.d0
  1683.             angle2 (:) = angle2 (:) * PI / 180.d0
  1684.             !
  1685.             If (two_fermi_energies) Then
  1686.             !
  1687.                 ef_up = ef_up * e2
  1688.                 ef_dw = ef_dw * e2
  1689.                 !
  1690.             End If
  1691.             !
  1692.         End If
  1693.         !
  1694.         Call mp_bcast (found, ionode_id, intra_image_comm)
  1695.         !
  1696.         If (found) Then
  1697.         !
  1698.             Call mp_bcast (starting_magnetization, ionode_id, intra_image_comm)
  1699.             Call mp_bcast (angle1, ionode_id, intra_image_comm)
  1700.             Call mp_bcast (angle2, ionode_id, intra_image_comm)
  1701.             Call mp_bcast (two_fermi_energies, ionode_id, intra_image_comm)
  1702.             Call mp_bcast (i_cons, ionode_id, intra_image_comm)
  1703.             Call mp_bcast (mcons, ionode_id, intra_image_comm)
  1704.             Call mp_bcast (bfield, ionode_id, intra_image_comm)
  1705.             Call mp_bcast (nelup, ionode_id, intra_image_comm)
  1706.             Call mp_bcast (neldw, ionode_id, intra_image_comm)
  1707.             Call mp_bcast (ef_up, ionode_id, intra_image_comm)
  1708.             Call mp_bcast (ef_dw, ionode_id, intra_image_comm)
  1709.             Call mp_bcast (lambda, ionode_id, intra_image_comm)
  1710.             !
  1711.         End If
  1712.         !
  1713.         lstarting_mag_read = .True.
  1714.         !
  1715.         Return
  1716.         !
  1717.     End Subroutine read_magnetization
  1718.     !
  1719.     !------------------------------------------------------------------------
  1720.     Subroutine read_xc (ierr)
  1721.     !------------------------------------------------------------------------
  1722.     !
  1723.         Use ions_base, Only: nsp
  1724.         Use funct, Only: enforce_input_dft
  1725.         Use ldaU, Only: lda_plus_u, lda_plus_u_kind, Hubbard_lmax, Hubbard_l, Hubbard_U, Hubbard_J, Hubbard_alpha, Hubbard_J0, Hubbard_beta, U_projection
  1726.         Use kernel_table, Only: vdw_table_name
  1727.         Use acfdt_ener, Only: acfdt_in_pw
  1728.         Use control_flags, Only: llondon, lxdm, ts_vdw
  1729.         Use london_module, Only: scal6, lon_rcut
  1730.         Use tsvdw_module, Only: vdw_isolated
  1731.         !
  1732.         Implicit None
  1733.         !
  1734.         Integer, Intent (Out) :: ierr
  1735.         !
  1736.         Character (Len=20) :: dft_name
  1737.         Integer :: nsp_, inlc
  1738.         Logical :: nomsg = .True.
  1739.         !
  1740.         ierr = 0
  1741.         If (lxc_read) Return
  1742.         !
  1743.         If ( .Not. lions_read) Call errore ('read_xc', 'read ions first', 1)
  1744.         !
  1745.         If (ionode) Then
  1746.         !
  1747.             Call qexml_read_xc (dft_name, lda_plus_u, lda_plus_u_kind, U_projection, Hubbard_lmax, Hubbard_l, nsp_, Hubbard_U, Hubbard_J, Hubbard_J0, Hubbard_alpha, Hubbard_beta, inlc, vdw_table_name, acfdt_in_pw, llondon, scal6, lon_rcut, lxdm, ts_vdw, vdw_isolated, ierr)
  1748.             !
  1749.         End If
  1750.         !
  1751.         Call mp_bcast (dft_name, ionode_id, intra_image_comm)
  1752.         Call mp_bcast (lda_plus_u, ionode_id, intra_image_comm)
  1753.         Call mp_bcast (inlc, ionode_id, intra_image_comm)
  1754.         Call mp_bcast (llondon, ionode_id, intra_image_comm)
  1755.         Call mp_bcast (lxdm, ionode_id, intra_image_comm)
  1756.         Call mp_bcast (ts_vdw, ionode_id, intra_image_comm)
  1757.         !
  1758.         If (lda_plus_u) Then
  1759.         !
  1760.             Call mp_bcast (lda_plus_u_kind, ionode_id, intra_image_comm)
  1761.             Call mp_bcast (Hubbard_lmax, ionode_id, intra_image_comm)
  1762.             Call mp_bcast (Hubbard_l, ionode_id, intra_image_comm)
  1763.             Call mp_bcast (U_projection, ionode_id, intra_image_comm)
  1764.             Call mp_bcast (Hubbard_U, ionode_id, intra_image_comm)
  1765.             Call mp_bcast (Hubbard_J, ionode_id, intra_image_comm)
  1766.             Call mp_bcast (Hubbard_J0, ionode_id, intra_image_comm)
  1767.             Call mp_bcast (Hubbard_alpha, ionode_id, intra_image_comm)
  1768.             Call mp_bcast (Hubbard_beta, ionode_id, intra_image_comm)
  1769.             !
  1770.         End If
  1771.         !
  1772.         If (llondon) Then
  1773.             Call mp_bcast (scal6, ionode_id, intra_image_comm)
  1774.             Call mp_bcast (lon_rcut, ionode_id, intra_image_comm)
  1775.         End If
  1776.         !
  1777.         If (ts_vdw) Then
  1778.             Call mp_bcast (vdw_isolated, ionode_id, intra_image_comm)
  1779.         End If
  1780.         !
  1781.         ! SCF EXX/RPA
  1782.         !
  1783.         Call mp_bcast (acfdt_in_pw, ionode_id, intra_image_comm)
  1784.         !
  1785.         If (acfdt_in_pw) dft_name = 'NOX-NOC'
  1786.         !
  1787.         If (inlc > 0) Then
  1788.             Call mp_bcast (vdw_table_name, ionode_id, intra_image_comm)
  1789.         End If
  1790.         !
  1791.         If (llondon) Then
  1792.             Call mp_bcast (scal6, ionode_id, intra_image_comm)
  1793.             Call mp_bcast (lon_rcut, ionode_id, intra_image_comm)
  1794.         End If
  1795.         !
  1796.         If (ts_vdw) Then
  1797.             Call mp_bcast (vdw_isolated, ionode_id, intra_image_comm)
  1798.         End If
  1799.         !
  1800.         ! SCF EXX/RPA
  1801.         !
  1802.         Call mp_bcast (acfdt_in_pw, ionode_id, intra_image_comm)
  1803.         !
  1804.         If (acfdt_in_pw) dft_name = 'NOX-NOC'
  1805.         !
  1806.         ! discard any further attempt to set a different dft
  1807.         Call enforce_input_dft (dft_name, nomsg)
  1808.         !
  1809.         lxc_read = .True.
  1810.         !
  1811.         Return
  1812.         !
  1813.     End Subroutine read_xc
  1814.     !
  1815.     !
  1816.     !------------------------------------------------------------------------
  1817.     Subroutine read_brillouin_zone (ierr)
  1818.     !------------------------------------------------------------------------
  1819.     !
  1820.         Use lsda_mod, Only: lsda
  1821.         Use klist, Only: nkstot, xk, wk, qnorm
  1822.         Use start_k, Only: nks_start, xk_start, wk_start, nk1, nk2, nk3, k1, k2, k3
  1823.         Use symm_base, Only: nrot, s, sname
  1824.         !
  1825.         Implicit None
  1826.         !
  1827.         Integer, Intent (Out) :: ierr
  1828.         Character (iotk_attlenx) :: attr
  1829.         !
  1830.         Integer :: i, ik, num_k_points
  1831.         Logical :: found
  1832.         !
  1833.         ierr = 0
  1834.         If (lbz_read) Return
  1835.         !
  1836.         !
  1837.         If (ionode) Then
  1838.         !
  1839.         ! xk_start and wk_start are ALLOCATABLE inside the function
  1840.             Call qexml_read_bz (num_k_points=num_k_points, xk=xk, wk=wk, k1=k1, k2=k2, k3=k3, nk1=nk1, nk2=nk2, nk3=nk3, nks_start=nks_start, xk_start=xk_start, wk_start=wk_start, qnorm=qnorm, ierr=ierr)
  1841.             !
  1842.             nkstot = num_k_points
  1843.             !
  1844.             If (lsda) nkstot = num_k_points * 2
  1845.             !
  1846.             Do ik = 1, num_k_points
  1847.             !
  1848.                 If (lsda) Then
  1849.                 !
  1850.                     xk (:, ik+num_k_points) = xk (:, ik)
  1851.                     !
  1852.                     wk (ik+num_k_points) = wk (ik)
  1853.                     !
  1854.                 End If
  1855.                 !
  1856.             End Do
  1857.             !
  1858.         End If
  1859.         !
  1860.         Call mp_bcast (nkstot, ionode_id, intra_image_comm)
  1861.         Call mp_bcast (xk, ionode_id, intra_image_comm)
  1862.         Call mp_bcast (wk, ionode_id, intra_image_comm)
  1863.         Call mp_bcast (nk1, ionode_id, intra_image_comm)
  1864.         Call mp_bcast (nk2, ionode_id, intra_image_comm)
  1865.         Call mp_bcast (nk3, ionode_id, intra_image_comm)
  1866.         Call mp_bcast (k1, ionode_id, intra_image_comm)
  1867.         Call mp_bcast (k2, ionode_id, intra_image_comm)
  1868.         Call mp_bcast (k3, ionode_id, intra_image_comm)
  1869.         Call mp_bcast (qnorm, ionode_id, intra_image_comm)
  1870.         !
  1871.         Call mp_bcast (nks_start, ionode_id, intra_image_comm)
  1872.         If (nks_start > 0 .And. .Not. ionode) Then
  1873.             If ( .Not. ALLOCATED(xk_start)) ALLOCATE (xk_start(3, nks_start))
  1874.             If ( .Not. ALLOCATED(wk_start)) ALLOCATE (wk_start(nks_start))
  1875.         End If
  1876.         If (nks_start > 0) Then
  1877.             Call mp_bcast (xk_start, ionode_id, intra_image_comm)
  1878.             Call mp_bcast (wk_start, ionode_id, intra_image_comm)
  1879.         End If
  1880.         Call mp_bcast (nrot, ionode_id, intra_image_comm)
  1881.         Call mp_bcast (s, ionode_id, intra_image_comm)
  1882.         Call mp_bcast (sname, ionode_id, intra_image_comm)
  1883.         !
  1884.         lbz_read = .True.
  1885.         !
  1886.         Return
  1887.         !
  1888.     End Subroutine read_brillouin_zone
  1889.     !
  1890.     !------------------------------------------------------------------------
  1891.     Subroutine read_occupations (ierr)
  1892.     !------------------------------------------------------------------------
  1893.     !
  1894.         Use lsda_mod, Only: lsda, nspin
  1895.         Use fixed_occ, Only: tfixed_occ, f_inp
  1896.         Use ktetra, Only: ntetra, tetra
  1897.         Use klist, Only: ltetra, lgauss, ngauss, degauss, smearing
  1898.         Use electrons_base, Only: nupdwn
  1899.         Use wvfct, Only: nbnd
  1900.         !
  1901.         Implicit None
  1902.         !
  1903.         Integer, Intent (Out) :: ierr
  1904.         Character (iotk_attlenx) :: attr
  1905.         !
  1906.         Integer :: i
  1907.         Logical :: found
  1908.         !
  1909.         ierr = 0
  1910.         If (locc_read) Return
  1911.         !
  1912.         If (ionode) Then
  1913.         !
  1914.         ! necessary to don't send nbnd and nspin as input in read_occ
  1915.             If ( .Not. ALLOCATED(f_inp)) Then
  1916.             !
  1917.                 If (nspin == 4) Then
  1918.                     Allocate (f_inp(nbnd, 1))
  1919.                 Else
  1920.                     Allocate (f_inp(nbnd, nspin))
  1921.                 End If
  1922.                 !
  1923.             End If
  1924.             !
  1925.             f_inp (:, :) = 0.0d0
  1926.             !
  1927.             Call qexml_read_occ (lgauss=lgauss, ngauss=ngauss, degauss=degauss, ltetra=ltetra, ntetra=ntetra, tetra=tetra, tfixed_occ=tfixed_occ, NSTATES_UP=nupdwn(1), NSTATES_DW=nupdwn(2), INPUT_OCC=f_inp, ierr=ierr)
  1928.             !
  1929.         End If
  1930.         !
  1931.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  1932.         !
  1933.         If (ierr > 0) Return
  1934.         !
  1935.         If (ionode) Then
  1936.         !
  1937.             If (lgauss) Then
  1938.             !
  1939.                 Select Case (ngauss)
  1940.                 Case (0)
  1941.                     smearing = 'gaussian'
  1942.                 Case (1)
  1943.                     smearing = 'Methfessel-Paxton'
  1944.                 Case (-1)
  1945.                     smearing = 'Marzari-Vanderbilt'
  1946.                 Case (-99)
  1947.                     smearing = 'Fermi-Dirac'
  1948.                 Case Default
  1949.                     Call errore ('read_occupations', 'wrong smearing index', Abs(1000+ngauss))
  1950.                 End Select
  1951.                 !
  1952.                 degauss = degauss * e2
  1953.                 !
  1954.             Else
  1955.             !
  1956.                 ngauss = 0
  1957.                 degauss = 0.d0
  1958.                 !
  1959.             End If
  1960.             !
  1961.             If ( .Not. ltetra) Then
  1962.             !
  1963.                 ntetra = 0
  1964.                 !
  1965.             End If
  1966.             !
  1967.             If ( .Not. tfixed_occ) Then
  1968.             !
  1969.                 Deallocate (f_inp)
  1970.                 !
  1971.             End If
  1972.             !
  1973.             !
  1974.         End If
  1975.         !
  1976.         Call mp_bcast (lgauss, ionode_id, intra_image_comm)
  1977.         !
  1978.         If (lgauss) Then
  1979.         !
  1980.             Call mp_bcast (ngauss, ionode_id, intra_image_comm)
  1981.             Call mp_bcast (degauss, ionode_id, intra_image_comm)
  1982.             Call mp_bcast (smearing, ionode_id, intra_image_comm)
  1983.             !
  1984.         End If
  1985.         !
  1986.         Call mp_bcast (ltetra, ionode_id, intra_image_comm)
  1987.         !
  1988.         If (ltetra) Then
  1989.         !
  1990.             Call mp_bcast (ntetra, ionode_id, intra_image_comm)
  1991.             Call mp_bcast (tetra, ionode_id, intra_image_comm)
  1992.             !
  1993.         End If
  1994.         !
  1995.         Call mp_bcast (tfixed_occ, ionode_id, intra_image_comm)
  1996.         !
  1997.         If (tfixed_occ) Then
  1998.         !
  1999.             Call mp_bcast (nupdwn, ionode_id, intra_image_comm)
  2000.             !
  2001.             If ( .Not. ALLOCATED(f_inp)) Then
  2002.             !
  2003.                 If (nspin == 4) Then
  2004.                     Allocate (f_inp(nbnd, 1))
  2005.                 Else
  2006.                     Allocate (f_inp(nbnd, nspin))
  2007.                 End If
  2008.                 !
  2009.             End If
  2010.             !
  2011.             Call mp_bcast (f_inp, ionode_id, intra_image_comm)
  2012.             !
  2013.         End If
  2014.         !
  2015.         locc_read = .True.
  2016.         !
  2017.         Return
  2018.         !
  2019.     End Subroutine read_occupations
  2020.     !
  2021.     !
  2022.     !
  2023.     !------------------------------------------------------------------------
  2024.     Subroutine read_band_structure (dirname, ierr)
  2025.     !------------------------------------------------------------------------
  2026.     !
  2027.         Use control_flags, Only: lkpoint_dir
  2028.         Use basis, Only: natomwfc
  2029.         Use lsda_mod, Only: lsda, isk
  2030.         Use klist, Only: nkstot, wk, nelec
  2031.         Use wvfct, Only: et, wg, nbnd
  2032.         Use ener, Only: ef, ef_up, ef_dw
  2033.         !
  2034.         Implicit None
  2035.         !
  2036.         Character (Len=*), Intent (In) :: dirname
  2037.         Integer, Intent (Out) :: ierr
  2038.         !
  2039.         Integer :: ik, ik_eff, num_k_points
  2040.         Logical :: found, two_fermi_energies_
  2041.         Character (Len=256) :: filename
  2042.         !
  2043.         ierr = 0
  2044.         If (lbs_read) Return
  2045.         !
  2046.         If ( .Not. lspin_read) Call errore ('read_band_structure', 'read spin first', 1)
  2047.         If ( .Not. lbz_read) Call errore ('read_band_structure', 'read band_structure first', 1)
  2048.         !
  2049.         !
  2050.         If (ionode) Then
  2051.         ! we don't need to read nspin, noncolin
  2052.             Call qexml_read_bands_info (nbnd=nbnd, num_k_points=num_k_points, natomwfc=natomwfc, nelec=nelec, ef=ef, two_fermi_energies=two_fermi_energies_, ef_up=ef_up, ef_dw=ef_dw, ierr=ierr)
  2053.             !
  2054.         End If
  2055.         !
  2056.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  2057.         !
  2058.         If (ierr > 0) Return
  2059.         !
  2060.         If (ionode) Then
  2061.             If ( .Not. two_fermi_energies_) Then
  2062.                 ef = ef * e2
  2063.             Else
  2064.                 ef = 0.d0
  2065.                 ef_up = ef_up * e2
  2066.                 ef_dw = ef_dw * e2
  2067.             End If
  2068.             !
  2069.         End If
  2070.         !
  2071.         num_k_points = nkstot
  2072.         !
  2073.         If (lsda) num_k_points = nkstot / 2
  2074.         !
  2075.         If (ionode) Then
  2076.         !
  2077.             If ( .Not. lkpoint_dir) filename = TRIM (dirname) // '/' // TRIM (xmlpun) // '.eig'
  2078.             !
  2079.             Call qexml_read_bands_pw (num_k_points, nbnd, nkstot, lsda, lkpoint_dir, filename, isk=isk, et=et, wg=wg, ierr=ierr)
  2080.             !
  2081.             et (:, :) = et (:, :) * e2
  2082.             !
  2083.             Forall (ik=1:nkstot) wg (:, ik) = wg (:, ik) * wk (ik)
  2084.             !
  2085.         End If
  2086.         !
  2087.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  2088.         !
  2089.         If (ierr > 0) Return
  2090.         !
  2091.         Call mp_bcast (nelec, ionode_id, intra_image_comm)
  2092.         Call mp_bcast (natomwfc, ionode_id, intra_image_comm)
  2093.         Call mp_bcast (nbnd, ionode_id, intra_image_comm)
  2094.         Call mp_bcast (isk, ionode_id, intra_image_comm)
  2095.         Call mp_bcast (et, ionode_id, intra_image_comm)
  2096.         Call mp_bcast (wg, ionode_id, intra_image_comm)
  2097.         Call mp_bcast (ef, ionode_id, intra_image_comm)
  2098.         !
  2099.         lbs_read = .True.
  2100.         !
  2101.         Return
  2102.         !
  2103.     End Subroutine read_band_structure
  2104.     !
  2105.     !------------------------------------------------------------------------
  2106.     Subroutine read_wavefunctions (dirname, ierr)
  2107.     !------------------------------------------------------------------------
  2108.     !
  2109.     ! ... This routines reads wavefunctions from the new file format and
  2110.     ! ... writes them into the old format
  2111.     !
  2112.         Use control_flags, Only: twfcollect, lkpoint_dir
  2113.         Use cell_base, Only: tpiba2
  2114.         Use lsda_mod, Only: nspin, isk
  2115.         Use klist, Only: nkstot, wk, nks, xk, ngk
  2116.         Use wvfct, Only: npw, npwx, et, wg, nbnd
  2117.         Use gvecw, Only: ecutwfc
  2118.         Use wavefunctions_module, Only: evc
  2119.         Use io_files, Only: nwordwfc, iunwfc
  2120.         Use buffers, Only: save_buffer
  2121.         Use gvect, Only: ngm, ngm_g, g, ig_l2g
  2122.         Use noncollin_module, Only: noncolin, npol
  2123.         Use mp_images, Only: nproc_image, intra_image_comm
  2124.         Use mp_pools, Only: kunit, nproc_pool, me_pool, root_pool, intra_pool_comm, inter_pool_comm
  2125.         Use mp_bands, Only: me_bgrp, nbgrp, root_bgrp, intra_bgrp_comm
  2126.         !
  2127. #if defined __HDF5
  2128.         Use hdf5_qe, Only: evc_hdf5_write, read_attributes_hdf5, prepare_for_reading_final
  2129.         Use mp_pools, Only: inter_pool_comm
  2130. #endif
  2131. !
  2132.         Implicit None
  2133.         !
  2134.         Character (Len=*), Intent (In) :: dirname
  2135.         Integer, Intent (Out) :: ierr
  2136.         !
  2137.         Character (Len=256) :: filename
  2138.         Integer :: ik, ipol, ik_eff, num_k_points
  2139.         Integer, Allocatable :: kisort (:)
  2140.         Integer :: nkl, nkr, npwx_g
  2141.         Integer :: nupdwn (2), ike, iks, npw_g, ispin
  2142.         Integer, External :: global_kpoint_index
  2143.         Integer, Allocatable :: ngk_g (:)
  2144.         Integer, Allocatable :: igk_l2g (:, :), igk_l2g_kdip (:, :)
  2145.         Logical :: opnd
  2146.         Real (DP), Allocatable :: gk (:)
  2147.         Real (DP) :: scalef
  2148.         !
  2149.         !
  2150.         ! The ierr output var is actually not given any value
  2151.         ! except this initialization
  2152.         !
  2153.         ierr = 0
  2154.         !
  2155.         iks = global_kpoint_index (nkstot, 1)
  2156.         ike = iks + nks - 1
  2157.         !
  2158.         ! ... find out the global number of G vectors: ngm_g
  2159.         !
  2160.         ngm_g = ngm
  2161.         !
  2162.         Call mp_sum (ngm_g, intra_bgrp_comm)
  2163.         !
  2164.         ! ... build the igk_l2g array, yielding the correspondence between
  2165.         ! ... the local k+G index and the global G index - see also ig_l2g
  2166.         !
  2167.         Allocate (igk_l2g(npwx, nks))
  2168.         igk_l2g = 0
  2169.         !
  2170.         Allocate (kisort(npwx), gk(npwx))
  2171.         !
  2172.         Do ik = 1, nks
  2173.         !
  2174.             kisort = 0
  2175.             npw = npwx
  2176.             !
  2177.             Call gk_sort (xk(1, ik+iks-1), ngm, g, ecutwfc/tpiba2, npw, kisort(1), gk)
  2178.             !
  2179.             Call gk_l2gmap (ngm, ig_l2g(1), npw, kisort(1), igk_l2g(1, ik))
  2180.             !
  2181.             ngk (ik) = npw
  2182.             !
  2183.         End Do
  2184.         !
  2185.         Deallocate (gk, kisort)
  2186.         !
  2187.         ! ... compute the global number of G+k vectors for each k point
  2188.         !
  2189.         Allocate (ngk_g(nkstot))
  2190.         !
  2191.         ngk_g = 0
  2192.         ngk_g (iks:ike) = ngk (1:nks)
  2193.         !
  2194.         Call mp_sum (ngk_g, inter_pool_comm)
  2195.         Call mp_sum (ngk_g, intra_pool_comm)
  2196.         ngk_g = ngk_g / nbgrp
  2197.         !
  2198.         ! ... compute the Maximum G vector index among all G+k an processors
  2199.         !
  2200.         npw_g = MAXVAL (igk_l2g(:, :))
  2201.         !
  2202.         Call mp_max (npw_g, inter_pool_comm)
  2203.         Call mp_max (npw_g, intra_pool_comm)
  2204.         !
  2205.         !
  2206.         ! ... compute the Maximum number of G vector among all k points
  2207.         !
  2208.         npwx_g = MAXVAL (ngk_g(1:nkstot))
  2209.         !
  2210.         !
  2211.         ! ... define a further l2g map to read gkvectors and wfc coherently
  2212.         !
  2213.         Allocate (igk_l2g_kdip(npwx_g, nks))
  2214.         igk_l2g_kdip = 0
  2215.         !
  2216.         Do ik = iks, ike
  2217.         !
  2218.             Call gk_l2gmap_kdip (npw_g, ngk_g(ik), ngk(ik-iks+1), igk_l2g(1, ik-iks+1), igk_l2g_kdip(1, ik-iks+1))
  2219.         End Do
  2220.         !
  2221.         !
  2222.         If (ionode) Then
  2223.         !
  2224.             Call iotk_scan_begin (iunpun, "EIGENVECTORS")
  2225.             !
  2226.         End If
  2227.         !
  2228.         num_k_points = nkstot
  2229.         !
  2230.         If (nspin == 2) num_k_points = nkstot / 2
  2231.         !
  2232.         k_points_loop: Do ik = 1, num_k_points
  2233.         !
  2234.             If (ionode) Then
  2235.             !
  2236.                 Call iotk_scan_begin (iunpun, "K-POINT"// TRIM(iotk_index(ik)))
  2237.                 !
  2238.                 If (nspin == 2 .Or. noncolin) Then
  2239.                 !
  2240.                     Call iotk_scan_begin (iunpun, "WFC.1", found=twfcollect)
  2241.                     If (twfcollect) Call iotk_scan_end (iunpun, "WFC.1")
  2242.                     !
  2243.                 Else
  2244.                 !
  2245.                     Call iotk_scan_begin (iunpun, "WFC", found=twfcollect)
  2246.                     If (twfcollect) Call iotk_scan_end (iunpun, "WFC")
  2247.                     !
  2248.                 End If
  2249.                 !
  2250.             End If
  2251.             !
  2252.             Call mp_bcast (twfcollect, ionode_id, intra_image_comm)
  2253.             !
  2254.             If ( .Not. twfcollect) Then
  2255.             !
  2256.                 If (ionode) Then
  2257.                 !
  2258.                     Call iotk_scan_end (iunpun, "K-POINT"// TRIM(iotk_index(ik)))
  2259.                     !
  2260.                 End If
  2261.                 !
  2262.                 Exit k_points_loop
  2263.                 !
  2264.             End If
  2265.             !
  2266.             If (nspin == 2) Then
  2267.             !
  2268.                 ispin = 1
  2269.                 evc = (0.0_DP, 0.0_DP)
  2270.                 !
  2271.                 ! ... no need to read isk here: they are read from band structure
  2272.                 ! ... and correctly distributed across pools in read_file
  2273.                 !!! isk(ik) = 1
  2274.                 !
  2275.                 If (ionode) Then
  2276.                 !
  2277.                     filename = TRIM (qexml_wfc_filename(dirname, 'evc', ik, ispin, DIR=lkpoint_dir))
  2278.                     !
  2279.                 End If
  2280.                 !
  2281.                 Call read_wfc (iunout, ik, nkstot, kunit, ispin, nspin, evc, npw_g, nbnd, igk_l2g_kdip(:, ik-iks+1), ngk(ik-iks+1), filename, scalef, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  2282.                 !
  2283.                 If ((ik >= iks) .And. (ik <= ike)) Then
  2284.                 !
  2285.                     Call save_buffer (evc, nwordwfc, iunwfc, (ik-iks+1))
  2286.                     !
  2287.                 End If
  2288.                 !
  2289.                 ispin = 2
  2290.                 ik_eff = ik + num_k_points
  2291.                 evc = (0.0_DP, 0.0_DP)
  2292.                 !
  2293.                 ! ... no need to read isk here (see above why)
  2294.                 !isk(ik_eff) = 2
  2295.                 !
  2296.                 If (ionode) Then
  2297.                 !
  2298.                     filename = TRIM (qexml_wfc_filename(dirname, 'evc', ik, ispin, DIR=lkpoint_dir))
  2299.                     !
  2300.                 End If
  2301.                 !
  2302.                 Call read_wfc (iunout, ik_eff, nkstot, kunit, ispin, nspin, evc, npw_g, nbnd, igk_l2g_kdip(:, ik_eff-iks+1), ngk(ik_eff-iks+1), filename, scalef, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  2303.                 !
  2304.                 If ((ik_eff >= iks) .And. (ik_eff <= ike)) Then
  2305.                 !
  2306.                     Call save_buffer (evc, nwordwfc, iunwfc, (ik_eff-iks+1))
  2307.                     !
  2308.                 End If
  2309.                 !
  2310.             Else
  2311.             !
  2312.             ! ... no need to read isk here (see above why)
  2313.             !isk(ik) = 1
  2314.             !
  2315.                 evc = (0.0_DP, 0.0_DP)
  2316.                 If (noncolin) Then
  2317.                 !
  2318.                     Do ipol = 1, npol
  2319.                     !
  2320.                         If (ionode) Then
  2321.                         !
  2322.                             filename = TRIM (qexml_wfc_filename(dirname, 'evc', ik, ipol, DIR=lkpoint_dir))
  2323.                             !
  2324.                         End If
  2325.                         !
  2326.                         !!! TEMP
  2327.                         nkl = (ipol-1) * npwx + 1
  2328.                         nkr = ipol * npwx
  2329.                         Call read_wfc (iunout, ik, nkstot, kunit, ispin, npol, evc(nkl:nkr, :), npw_g, nbnd, igk_l2g_kdip(:, ik-iks+1), ngk(ik-iks+1), filename, scalef, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  2330.                         !
  2331.                     End Do
  2332.                     !
  2333.                 Else
  2334.                 !
  2335.                     If (ionode) Then
  2336.                     !
  2337.                         filename = TRIM (qexml_wfc_filename(dirname, 'evc',  ik, DIR=lkpoint_dir))
  2338.                         !
  2339.                     End If
  2340.                     !
  2341.                     ! workaround for pot parallelization ( Viet Nguyen / SdG )
  2342.                     ! -pot parallelization uses mp_image communicators
  2343.                     ! note that ionode must be also reset in the similar way
  2344.                     ! to image parallelization
  2345.                     Call read_wfc (iunout, ik, nkstot, kunit, ispin, nspin, evc, npw_g, nbnd, igk_l2g_kdip(:, ik-iks+1), ngk(ik-iks+1), filename, scalef, ionode, root_pool, intra_pool_comm, inter_pool_comm, intra_image_comm)
  2346.                     !
  2347.                 End If
  2348.                 !
  2349.                 If ((ik >= iks) .And. (ik <= ike)) Then
  2350.                 !
  2351.                     Call save_buffer (evc, nwordwfc, iunwfc, (ik-iks+1))
  2352.                     !
  2353.                     ! the following two line can be used to debug read_wfc
  2354.                     ! WRITE(200+10*ik+me_pool,fmt="(2D18.10)") evc
  2355.                     ! CLOSE(200+10*ik+me_pool )
  2356.                     !
  2357.                 End If
  2358.                 !
  2359.             End If
  2360.             !
  2361.             If (ionode) Then
  2362.             !
  2363.                 Call iotk_scan_end (iunpun, "K-POINT"// TRIM(iotk_index(ik)))
  2364.                 !
  2365.             End If
  2366.             !
  2367.         End Do k_points_loop
  2368.         !
  2369.         Deallocate (igk_l2g)
  2370.         Deallocate (igk_l2g_kdip)
  2371.         !
  2372.         If (ionode) Then
  2373.         !
  2374.             Call iotk_scan_end (iunpun, "EIGENVECTORS")
  2375.             !
  2376.             !CALL iotk_close_read( iunpun )
  2377.             !
  2378.         End If
  2379.         !
  2380.         Return
  2381.         !
  2382.     End Subroutine read_wavefunctions
  2383.     !
  2384.     !------------------------------------------------------------------------
  2385.     Subroutine read_ef (ierr)
  2386.     !------------------------------------------------------------------------
  2387.     !
  2388.     ! ... this routine reads the Fermi energy and the number of electrons
  2389.     !
  2390.         Use ener, Only: ef, ef_up, ef_dw
  2391.         Use klist, Only: two_fermi_energies, nelec
  2392.         !
  2393.         Implicit None
  2394.         Integer, Intent (Out) :: ierr
  2395.         !
  2396.         ! ... then selected tags are read from the other sections
  2397.         !
  2398.         If (ionode) Then
  2399.         !
  2400.             Call qexml_read_bands_info (ef=ef, ef_up=ef_up, ef_dw=ef_dw, two_fermi_energies=two_fermi_energies, nelec=nelec, ierr=ierr)
  2401.             !
  2402.         End If
  2403.         !
  2404.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  2405.         If (ierr > 0) Return
  2406.         !
  2407.         If (ionode) Then
  2408.         !
  2409.             If ( .Not. two_fermi_energies) Then
  2410.                 ef = ef * e2
  2411.                 ef_up = 0.d0
  2412.                 ef_dw = 0.d0
  2413.             Else
  2414.                 ef = 0.d0
  2415.                 ef_up = ef_up * e2
  2416.                 ef_dw = ef_dw * e2
  2417.             End If
  2418.             !
  2419.         End If
  2420.         !
  2421.         Call mp_bcast (two_fermi_energies, ionode_id, intra_image_comm)
  2422.         Call mp_bcast (ef, ionode_id, intra_image_comm)
  2423.         Call mp_bcast (ef_up, ionode_id, intra_image_comm)
  2424.         Call mp_bcast (ef_dw, ionode_id, intra_image_comm)
  2425.         Call mp_bcast (nelec, ionode_id, intra_image_comm)
  2426.         !
  2427.         Return
  2428.         !
  2429.     End Subroutine read_ef
  2430.     !
  2431.     !------------------------------------------------------------------------
  2432.     Subroutine read_exx (ierr)
  2433.     !------------------------------------------------------------------------
  2434.     !
  2435.     ! ... read EXX variables
  2436.     !
  2437.         Use funct, Only: set_exx_fraction, set_screening_parameter, set_gau_parameter, enforce_input_dft, start_exx
  2438.         Use exx, Only: x_gamma_extrapolation, nq1, nq2, nq3, exxdiv_treatment, yukawa, ecutvcut, ecutfock
  2439.         Implicit None
  2440.         !
  2441.         Integer, Intent (Out) :: ierr
  2442.         Real (DP) :: exx_fraction, screening_parameter, gau_parameter
  2443.         Logical :: exx_is_active, found
  2444.         !
  2445.         If (ionode) Then
  2446.             Call qexml_read_exx (x_gamma_extrapolation=x_gamma_extrapolation, NQX1=nq1, NQX2=nq2, NQX3=nq3, exxdiv_treatment=exxdiv_treatment, yukawa=yukawa, ecutvcut=ecutvcut, exx_fraction=exx_fraction, screening_parameter=screening_parameter, gau_parameter=gau_parameter, exx_is_active=exx_is_active, ecutfock=ecutfock, found=found, ierr=ierr)
  2447.             !
  2448.         End If
  2449.         !
  2450.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  2451.         If (ierr > 0) Return
  2452.         !
  2453.         Call mp_bcast (found, ionode_id, intra_image_comm)
  2454.         !
  2455.         If ( .Not. found) Return
  2456.         !
  2457.         Call mp_bcast (x_gamma_extrapolation, ionode_id, intra_image_comm)
  2458.         Call mp_bcast (nq1, ionode_id, intra_image_comm)
  2459.         Call mp_bcast (nq2, ionode_id, intra_image_comm)
  2460.         Call mp_bcast (nq3, ionode_id, intra_image_comm)
  2461.         Call mp_bcast (exxdiv_treatment, ionode_id, intra_image_comm)
  2462.         Call mp_bcast (yukawa, ionode_id, intra_image_comm)
  2463.         Call mp_bcast (ecutvcut, ionode_id, intra_image_comm)
  2464.         Call mp_bcast (exx_fraction, ionode_id, intra_image_comm)
  2465.         Call mp_bcast (screening_parameter, ionode_id, intra_image_comm)
  2466.         Call mp_bcast (gau_parameter, ionode_id, intra_image_comm)
  2467.         Call mp_bcast (exx_is_active, ionode_id, intra_image_comm)
  2468.         Call mp_bcast (ecutfock, ionode_id, intra_image_comm)
  2469.         !
  2470.         Call set_exx_fraction (exx_fraction)
  2471.         Call set_screening_parameter (screening_parameter)
  2472.         Call set_gau_parameter (gau_parameter)
  2473.         If (exx_is_active) Call start_exx ()
  2474.         !
  2475.         Return
  2476.         !
  2477.     End Subroutine read_exx
  2478.     !
  2479.     !------------------------------------------------------------------------
  2480.     Subroutine read_esm (ierr)
  2481.     !------------------------------------------------------------------------
  2482.     !
  2483.     ! ... this routine reads only nelec and ef
  2484.     !
  2485.         Use esm, Only: esm_nfit, esm_efield, esm_w, esm_a, esm_bc
  2486.         !
  2487.         Implicit None
  2488.         Integer, Intent (Out) :: ierr
  2489.         !
  2490.         ! ... then selected tags are read from the other sections
  2491.         !
  2492.         If (ionode) Then
  2493.         !
  2494.             Call qexml_read_esm (esm_nfit=esm_nfit, esm_efield=esm_efield, esm_w=esm_w, esm_a=esm_a, esm_bc=esm_bc, ierr=ierr)
  2495.             !
  2496.         End If
  2497.         !
  2498.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  2499.         If (ierr > 0) Return
  2500.         !
  2501.         Call mp_bcast (esm_nfit, ionode_id, intra_image_comm)
  2502.         Call mp_bcast (esm_efield, ionode_id, intra_image_comm)
  2503.         Call mp_bcast (esm_w, ionode_id, intra_image_comm)
  2504.         Call mp_bcast (esm_a, ionode_id, intra_image_comm)
  2505.         Call mp_bcast (esm_bc, ionode_id, intra_image_comm)
  2506.         !
  2507.         Return
  2508.         !
  2509.     End Subroutine read_esm
  2510.     !
  2511.     !------------------------------------------------------------------------
  2512.     Subroutine read_ (dirname, ierr)
  2513.     !------------------------------------------------------------------------
  2514.     !
  2515.     ! ... this is a template for a "read section" subroutine
  2516.     !
  2517.         Implicit None
  2518.         !
  2519.         Character (Len=*), Intent (In) :: dirname
  2520.         Integer, Intent (Out) :: ierr
  2521.         !
  2522.         Integer :: idum
  2523.         !
  2524.         !
  2525.         If (ionode) Then
  2526.         !
  2527.             Call iotk_open_read (iunpun, FILE=TRIM(dirname)//'/'// TRIM(xmlpun), ierr=ierr)
  2528.             !
  2529.         End If
  2530.         !
  2531.         Call mp_bcast (ierr, ionode_id, intra_image_comm)
  2532.         !
  2533.         If (ierr > 0) Return
  2534.         !
  2535.         If (ionode) Then
  2536.         !
  2537.             Call iotk_scan_begin (iunpun, "")
  2538.             !
  2539.             Call iotk_scan_end (iunpun, "")
  2540.             !
  2541.             Call iotk_close_read (iunpun)
  2542.             !
  2543.         End If
  2544.         !
  2545.         Call mp_bcast (idum, ionode_id, intra_image_comm)
  2546.         !
  2547.         Return
  2548.         !
  2549.     End Subroutine read_
  2550.     !
  2551.     !----------------------------------------------------------------------------
  2552.     Subroutine gk_l2gmap (ngm, ig_l2g, ngk, igk, igk_l2g)
  2553.     !----------------------------------------------------------------------------
  2554.     !
  2555.     ! ... This subroutine maps local G+k index to the global G vector index
  2556.     ! ... the mapping is used to collect wavefunctions subsets distributed
  2557.     ! ... across processors.
  2558.     ! ... Written by Carlo Cavazzoni
  2559.     !
  2560.         Implicit None
  2561.         !
  2562.         ! ... Here the dummy variables
  2563.         !
  2564.         Integer, Intent (In) :: ngm, ngk, igk (ngk), ig_l2g (ngm)
  2565.         Integer, Intent (Out) :: igk_l2g (ngk)
  2566.         Integer :: ig
  2567.         !
  2568.         ! ... input: mapping between local and global G vector index
  2569.         !
  2570.         Do ig = 1, ngk
  2571.         !
  2572.             igk_l2g (ig) = ig_l2g (igk(ig))
  2573.             !
  2574.         End Do
  2575.         !
  2576.         Return
  2577.         !
  2578.     End Subroutine gk_l2gmap
  2579.     !
  2580.     !-----------------------------------------------------------------------
  2581.     Subroutine gk_l2gmap_kdip (npw_g, ngk_g, ngk, igk_l2g, igk_l2g_kdip, igwk)
  2582.     !-----------------------------------------------------------------------
  2583.     !
  2584.     ! ... This subroutine maps local G+k index to the global G vector index
  2585.     ! ... the mapping is used to collect wavefunctions subsets distributed
  2586.     ! ... across processors.
  2587.     ! ... This map is used to obtained the G+k grids related to each kpt
  2588.     !
  2589.         Implicit None
  2590.         !
  2591.         ! ... Here the dummy variables
  2592.         !
  2593.         Integer, Intent (In) :: npw_g, ngk_g, ngk
  2594.         Integer, Intent (In) :: igk_l2g (ngk)
  2595.         Integer, Optional, Intent (Out) :: igwk (ngk_g), igk_l2g_kdip (ngk)
  2596.         !
  2597.         Integer, Allocatable :: igwk_ (:), itmp (:), igwk_lup (:)
  2598.         Integer :: ig, ig_, ngg
  2599.         !
  2600.         !
  2601.         Allocate (itmp(npw_g))
  2602.         Allocate (igwk_(ngk_g))
  2603.         !
  2604.         itmp (:) = 0
  2605.         igwk_ (:) = 0
  2606.         !
  2607.         !
  2608.         Do ig = 1, ngk
  2609.         !
  2610.             itmp (igk_l2g(ig)) = igk_l2g (ig)
  2611.             !
  2612.         End Do
  2613.         !
  2614.         !
  2615.         Call mp_sum (itmp, intra_bgrp_comm)
  2616.         !
  2617.         ngg = 0
  2618.         Do ig = 1, npw_g
  2619.         !
  2620.             If (itmp(ig) == ig) Then
  2621.             !
  2622.                 ngg = ngg + 1
  2623.                 !
  2624.                 igwk_ (ngg) = ig
  2625.                 !
  2626.             End If
  2627.             !
  2628.         End Do
  2629.         !
  2630.         If (ngg /= ngk_g) Call errore ('gk_l2gmap_kdip', 'unexpected dimension in ngg', 1)
  2631.         !
  2632.         If (PRESENT(igwk)) Then
  2633.         !
  2634.             igwk (1:ngk_g) = igwk_ (1:ngk_g)
  2635.             !
  2636.         End If
  2637.         !
  2638.         If (PRESENT(igk_l2g_kdip)) Then
  2639.         !
  2640.             Allocate (igwk_lup(npw_g))
  2641.             !
  2642.             !$omp parallel private(ig_, ig)
  2643.             !$omp workshare
  2644.             igwk_lup = 0
  2645.             !$omp end workshare
  2646.             !$omp do
  2647.             Do ig_ = 1, ngk_g
  2648.                 igwk_lup (igwk_(ig_)) = ig_
  2649.             End Do
  2650.             !$omp end do
  2651.             !$omp do
  2652.             Do ig = 1, ngk
  2653.                 igk_l2g_kdip (ig) = igwk_lup (igk_l2g(ig))
  2654.             End Do
  2655.             !$omp end do
  2656.             !$omp end parallel
  2657.             !
  2658.             Deallocate (igwk_lup)
  2659.             !
  2660.         End If
  2661.         !
  2662.         Deallocate (itmp, igwk_)
  2663.         !
  2664.         Return
  2665.         !
  2666.     End Subroutine gk_l2gmap_kdip
  2667. #endif
  2668. !
  2669. End Module pw_restart
  2670. !
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement