LCOV - code coverage report
Current view: top level - src/70_gw - m_sigma.F90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.7 % 640 574
Test Date: 2026-11-09 14:22:37 Functions: 86.7 % 15 13
Branches: 56.0 % 2461 1377

             Branch data     Line data    Source code
       1                 :             : !!****m* ABINIT/m_sigma
       2                 :             : !! NAME
       3                 :             : !!  m_sigma
       4                 :             : !!
       5                 :             : !! FUNCTION
       6                 :             : !!  This module provides the definition of the sigma_t data type
       7                 :             : !!  used to store results of the GW calculation.
       8                 :             : !!
       9                 :             : !! COPYRIGHT
      10                 :             : !! Copyright (C) 2008-2026 ABINIT group (MG, FB, GMR, VO, LR, RWG)
      11                 :             : !! This file is distributed under the terms of the
      12                 :             : !! GNU General Public License, see ~abinit/COPYING
      13                 :             : !! or http://www.gnu.org/copyleft/gpl.txt .
      14                 :             : !!
      15                 :             : !! SOURCE
      16                 :             : 
      17                 :             : #if defined HAVE_CONFIG_H
      18                 :             : #include "config.h"
      19                 :             : #endif
      20                 :             : 
      21                 :             : #include "abi_common.h"
      22                 :             : 
      23                 :             : module m_sigma
      24                 :             : 
      25                 :             :  use, intrinsic :: iso_c_binding
      26                 :             :  use defs_basis
      27                 :             :  use m_gwdefs
      28                 :             :  use m_xmpi
      29                 :             :  use m_abicore
      30                 :             :  use m_errors
      31                 :             :  use m_nctk
      32                 :             :  use m_yaml
      33                 :             :  use m_melemts
      34                 :             :  use netcdf
      35                 :             :  use m_wfd
      36                 :             : 
      37                 :             :  use defs_abitypes,    only : MPI_type
      38                 :             :  !use m_gwdefs,         only : unt_gw, unt_sig, unt_sgr, unt_sgm, unt_gwdiag, sigparams_t, unt_sigc
      39                 :             :  use m_fstrings,       only : itoa, sjoin
      40                 :             :  use m_numeric_tools,  only : c2r
      41                 :             :  use m_crystal,        only : crystal_t
      42                 :             :  use m_ebands,         only : ebands_t
      43                 :             :  use m_bz_mesh,        only : kmesh_t, littlegroup_t, findqg0
      44                 :             :  use m_screening,      only : epsm1_t
      45                 :             :  use m_dtset,          only : dataset_type
      46                 :             : 
      47                 :             :  implicit none
      48                 :             : 
      49                 :             :  private
      50                 :             : !!***
      51                 :             : 
      52                 :             : !----------------------------------------------------------------------
      53                 :             : 
      54                 :             : !!****t* m_sigma/sigma_t
      55                 :             : !! NAME
      56                 :             : !! sigma_t
      57                 :             : !!
      58                 :             : !! FUNCTION
      59                 :             : !! The sigma_t structured datatype gathers the results of a GW calculation.
      60                 :             : !!
      61                 :             : !! TODO
      62                 :             : !!   ragged arrays (nk,nsppol) --> values ?
      63                 :             : !!
      64                 :             : !! SOURCE
      65                 :             : 
      66                 :             :  type,public :: sigma_t
      67                 :             : 
      68                 :             :   integer :: b1gw, b2gw     ! min and Max gw band indices over spin and k-points (used to dimension arrays)
      69                 :             :   integer :: gwcalctyp      ! Flag defining the calculation type.
      70                 :             :   integer :: nkptgw         ! No. of points calculated
      71                 :             :   integer :: nkibz          ! No. of irreducible k-points.
      72                 :             :   integer :: nbnds          ! Total number of bands
      73                 :             :   integer :: nomega_r       ! No. of real frequencies for the spectral function.
      74                 :             :   integer :: nomega_i       ! No. of frequencies along the imaginary axis.
      75                 :             :   integer :: nomega4sd      ! No. of real frequencies to evaluate the derivative of $\Sigma(E)$.
      76                 :             :   integer :: nsig_ab        ! 1 if nspinor=1,4 for noncollinear case.
      77                 :             :   integer :: nsppol         ! No. of spin polarizations.
      78                 :             :   integer :: usepawu        ! 1 if we are using DFT+U as starting point (only for PAW)
      79                 :             : 
      80                 :             :   real(dp) :: deltae       ! Frequency step for the calculation of d\Sigma/dE
      81                 :             :   real(dp) :: maxomega4sd  ! Max frequency around E_ks for d\Sigma/dE.
      82                 :             :   real(dp) :: maxomega_r   ! Max frequency for spectral function.
      83                 :             :   real(dp) :: scissor_ene  ! Scissor energy value. zero for None.
      84                 :             : 
      85                 :             :   integer,allocatable :: maxbnd(:,:)
      86                 :             :   ! (nkptgw, nsppol)
      87                 :             :   ! Max band index considered in GW for this k-point.
      88                 :             : 
      89                 :             :   integer,allocatable :: minbnd(:,:)
      90                 :             :   ! (nkptgw, nsppol)
      91                 :             :   ! Min band index considered in GW for this k-point.
      92                 :             : 
      93                 :             :   real(dp),allocatable :: degwgap(:,:)
      94                 :             :   ! (nkibz, nsppol)
      95                 :             :   ! Difference btw the QP and the KS direct gap.
      96                 :             : 
      97                 :             :   real(dp),allocatable :: egwgap(:,:)
      98                 :             :   ! (nkibz, nsppol))
      99                 :             :   ! QP direct gap at each k-point and spin.
     100                 :             : 
     101                 :             :   real(dp),allocatable :: en_qp_diago(:,:,:)
     102                 :             :   ! (nbnds, nkibz, nsppol))
     103                 :             :   ! QP energies obtained from the diagonalization of the Hermitian approximation to Sigma (QPSCGW)
     104                 :             : 
     105                 :             :   real(dp),allocatable :: e0(:,:,:)
     106                 :             :   ! (nbnds, nkibz, nsppol)
     107                 :             :   ! KS eigenvalues for each band, k-point and spin. In case of self-consistent?
     108                 :             : 
     109                 :             :   real(dp),allocatable :: e0gap(:,:)
     110                 :             :   ! (nkibz, nsppol),
     111                 :             :   ! KS gap at each k-point, for each spin.
     112                 :             : 
     113                 :             :   real(dp),allocatable :: omega_r(:)
     114                 :             :   ! (nomega_r)
     115                 :             :   ! real frequencies used for the self energy.
     116                 :             : 
     117                 :             :   real(dp),allocatable :: kptgw(:,:)
     118                 :             :   ! (3, nkptgw)
     119                 :             :   ! ! TODO there is a similar array in sigparams_t
     120                 :             :   ! List of calculated k-points.
     121                 :             : 
     122                 :             :   real(dp),allocatable :: sigxme(:,:,:)
     123                 :             :   ! (b1gw:b2gw, nkibz, nsppol*nsig_ab))
     124                 :             :   ! Diagonal matrix elements $\<nks|\Sigma_x|nks\>$
     125                 :             : 
     126                 :             :   real(dp),allocatable :: sigxcnofme(:,:,:)
     127                 :             :   ! (b1gw:b2gw, nkibz, nsppol*nsig_ab))
     128                 :             :   ! Diagonal matrix elements $\<nks|\Sigma_xc|nks\>$ taking sqrt(occs) in \Sigma_x, occs in [0,1]
     129                 :             : 
     130                 :             :   complex(dp),allocatable :: x_mat(:,:,:,:)
     131                 :             :   ! (b1gw:b2gw, b1gw:b2gw, nkibz, nsppol*nsig_ab)
     132                 :             :   ! Matrix elements of $\<nks|\Sigma_x|mks\>$
     133                 :             : 
     134                 :             :   real(dp),allocatable :: vxcme(:,:,:)
     135                 :             :   ! (b1gw:b2gw, nkibz, nsppol*nsig_ab))
     136                 :             :   ! $\<nks|v_{xc}[n_val]|nks\>$ matrix elements of vxc
     137                 :             :   ! NB: valence-only contribution i.e. computed without model core charge
     138                 :             : 
     139                 :             :   real(dp),allocatable :: vUme(:,:,:)
     140                 :             :   ! (b1gw:b2gw, nkibz, nsppol*nsig_ab))
     141                 :             :   ! $\<nks|v_{U}|nks\>$ for DFT+U.
     142                 :             : 
     143                 :             :   complex(dp),allocatable :: degw(:,:,:)
     144                 :             :   ! (b1gw:b2gw, nkibz, nsppol))
     145                 :             :   ! Difference between the QP and the KS energies.
     146                 :             : 
     147                 :             :   complex(dp),allocatable :: dsigmee0(:,:,:)
     148                 :             :   ! (b1gw:b2gw, nkibz, nsppol*nsig_ab))
     149                 :             :   ! Derivative of $\Sigma_c(E)$ calculated at the KS eigenvalue.
     150                 :             : 
     151                 :             :   complex(dp),allocatable :: egw(:,:,:)
     152                 :             :   ! (nbnds, nkibz, nsppol))
     153                 :             :   ! QP energies, $\epsilon_{nks}^{QP}$.
     154                 :             : 
     155                 :             :   logical :: needs_eigvec_qp = .True.
     156                 :             : 
     157                 :             :  ! FIXME: These arrays are huge and should be allocated only if self-consistent
     158                 :             :   complex(dp),allocatable :: eigvec_qp(:,:,:,:)
     159                 :             :   ! (nbnds, nbnds, nkibz, nsppol))
     160                 :             :   ! Expansion of the QP amplitudes in the QP basis set of the previous iteration.
     161                 :             : 
     162                 :             :   complex(dp),allocatable :: m_ks_to_qp(:,:,:,:)
     163                 :             :   ! (nbnds, nbnds, nkibz, nsppol))
     164                 :             :   ! m_ks_to_qp(ib,jb,k,s) := <\psi_{ib,k,s}^{KS}|\psi_{jb,k,s}^{QP}>
     165                 :             : 
     166                 :             :   complex(dp),allocatable :: hhartree(:,:,:,:)
     167                 :             :   ! (b1gw:b2gw, b1gw:b2gw, nkibz, nsppol*nsig_ab)
     168                 :             :   ! $\<nks|T+v_H+v_{loc}+v_{nl}|mks\>$
     169                 :             :   ! Note that v_{loc} does not include the contribution to vxc(r) given by the model core charge.
     170                 :             : 
     171                 :             :   complex(dp),allocatable :: sigcme(:,:,:,:)
     172                 :             :   ! (b1gw:b2gw, nkibz, nomega_r, nsppol*nsig_ab))
     173                 :             :   ! $\<nks|\Sigma_{c}(E)|nks\>$ at each nomega_r frequency
     174                 :             : 
     175                 :             :   complex(dp),allocatable :: sigmee(:,:,:)
     176                 :             :   ! (b1gw:b2gw, nkibz, nsppol*nsig_ab))
     177                 :             :   ! $\Sigma_{xc}E_{KS} + (E_{QP}- E_{KS})*dSigma/dE_KS
     178                 :             : 
     179                 :             :   complex(dp),allocatable :: sigcmee0(:,:,:)
     180                 :             :   ! (b1gw:b2gw, nkibz, nsppol*nsig_ab))
     181                 :             :   ! Diagonal matrix elements of $\Sigma_c(E)$ calculated at the KS energy $E_{KS}$
     182                 :             : 
     183                 :             :   complex(dp),allocatable :: sigcmesi(:,:,:,:)
     184                 :             :   ! (b1gw:b2gw, nkibz, nomega_i, nsppol*nsig_ab))
     185                 :             :   ! Matrix elements of $\Sigma_c$ along the imaginary axis.
     186                 :             :   ! Only used in case of analytical continuation.
     187                 :             : 
     188                 :             :   complex(dp),allocatable :: sigcme4sd(:,:,:,:)
     189                 :             :   ! (b1gw:b2gw, nkibz, nomega4sd, nsppol*nsig_ab))
     190                 :             :   ! Diagonal matrix elements of \Sigma_c around the zeroth order eigenvalue (usually KS).
     191                 :             : 
     192                 :             :   complex(dp),allocatable :: sigxcme(:,:,:,:)
     193                 :             :   ! (b1gw:b2gw, nkibz, nomega_r, nsppol*nsig_ab))
     194                 :             :   ! $\<nks|\Sigma_{xc}(E)|nks\>$ at each real frequency frequency.
     195                 :             : 
     196                 :             :   complex(dp),allocatable :: sigxcmesi(:,:,:,:)
     197                 :             :   ! (b1gw:b2gw, nkibz, nomega_i, nsppol*nsig_ab))
     198                 :             :   ! Matrix elements of $\Sigma_{xc}$ along the imaginary axis.
     199                 :             :   ! Only used in case of analytical continuation.
     200                 :             : 
     201                 :             :   complex(dp),allocatable :: sigxcme4sd(:,:,:,:)
     202                 :             :   ! (b1gw:b2gw, nkibz, nomega4sd, nsppol*nsig_ab))
     203                 :             :   ! Diagonal matrix elements of \Sigma_xc for frequencies around the zeroth order eigenvalues.
     204                 :             : 
     205                 :             :   complex(dp),allocatable :: ze0(:,:,:)
     206                 :             :   ! (b1gw:b2gw, nkibz, nsppol))
     207                 :             :   ! renormalization factor. $(1-\dfrac{\partial\Sigma_c} {\partial E_{KS}})^{-1}$
     208                 :             : 
     209                 :             :   complex(dp),allocatable :: omega_i(:)
     210                 :             :   ! (nomega_i)
     211                 :             :   ! Frequencies along the imaginary axis used for the analytical continuation.
     212                 :             : 
     213                 :             :   complex(dp),allocatable :: omega4sd(:,:,:,:)
     214                 :             :   ! (b1gw:b2gw, nkibz, nomega4sd, nsppol).
     215                 :             :   ! Frequencies used to evaluate the Derivative of Sigma.
     216                 :             : 
     217                 :             :  contains
     218                 :             :    procedure :: init => sigma_init
     219                 :             :     ! Initialize the object.
     220                 :             : 
     221                 :             :    procedure :: free => sigma_free
     222                 :             :     ! Deallocate memory.
     223                 :             : 
     224                 :             :    procedure :: get_exene => sigma_get_exene
     225                 :             :     ! Compute exchange energy.
     226                 :             : 
     227                 :             :     procedure :: get_excene => sigma_get_excene
     228                 :             :     ! Compute exchange-correlation MBB (Nat. Orb. Funct. Approx.) energy.
     229                 :             : 
     230                 :             :     procedure :: get_haene => sigma_get_haene
     231                 :             :      ! Compute hartree energy.
     232                 :             : 
     233                 :             :     procedure :: get_kiene => sigma_get_kiene
     234                 :             :      ! Compute kinetic energy.
     235                 :             : 
     236                 :             :     procedure :: ncwrite => sigma_ncwrite
     237                 :             :      ! Write data in netcdf format.
     238                 :             : 
     239                 :             :     procedure :: write_results => sigma_write_results
     240                 :             :     procedure :: print_perturbative => sigma_print_pertubative
     241                 :             :     procedure :: print_qpsc => sigma_print_qpsc
     242                 :             :  end type sigma_t
     243                 :             : 
     244                 :             :  public  :: sigma_distribute_bks
     245                 :             :  public ::  write_sigma_header
     246                 :             : !!***
     247                 :             : 
     248                 :             : contains  !========================================================================================
     249                 :             : !!***
     250                 :             : 
     251                 :             : !!****f* m_sigma/write_sigma_header
     252                 :             : !! NAME
     253                 :             : !! write_sigma_header
     254                 :             : !!
     255                 :             : !! FUNCTION
     256                 :             : !!  Write basic info and dimensions used during the calculation
     257                 :             : !!  of the QP correctoions (optdriver==4).
     258                 :             : !!
     259                 :             : !! INPUTS
     260                 :             : !!  Sigp=sigparams_t
     261                 :             : !!  Cryst<crystal_t>= Info on the Crystal structure
     262                 :             : !!  Kmesh<kmesh_t>= Description of the BZ sampling.
     263                 :             : !!
     264                 :             : !! OUTPUT
     265                 :             : !!  (for writing routines, no output) otherwise, should be described
     266                 :             : !!
     267                 :             : !! NOTES
     268                 :             : !!
     269                 :             : !! SOURCE
     270                 :             : 
     271                 :         178 : subroutine write_sigma_header(Sigp, epsm1, Cryst, Kmesh, Qmesh)
     272                 :             : 
     273                 :             : !Arguments ------------------------------------
     274                 :             : !scalars
     275                 :             :  class(sigparams_t),intent(in) :: Sigp
     276                 :             :  type(kmesh_t),intent(in) :: Kmesh,Qmesh
     277                 :             :  type(crystal_t),intent(in) :: Cryst
     278                 :             :  type(epsm1_t),intent(in) :: epsm1
     279                 :             : 
     280                 :             : !Local variables-------------------------------
     281                 :             : !scalars
     282                 :             :  integer :: gwcalctyp, mod10
     283                 :             :  character(len=500) :: msg
     284                 :             :  integer :: units(2)
     285                 :             : ! *************************************************************************
     286                 :             : 
     287         [ +  + ]:         534 :  units = [std_out, ab_out]
     288                 :         178 :  call wrtout(units, ' SIGMA fundamental parameters:')
     289                 :             : 
     290                 :         178 :  gwcalctyp = Sigp%gwcalctyp
     291                 :         178 :  mod10 = MOD(Sigp%gwcalctyp,10)
     292                 :             : 
     293                 :          92 :  SELECT CASE (mod10)
     294                 :             :  CASE (SIG_GW_PPM)
     295                 :          92 :    write(msg,'(a,i2)')' PLASMON POLE MODEL ',Sigp%ppmodel
     296                 :             :  CASE (SIG_GW_AC)
     297                 :           9 :    write(msg,'(a)')' ANALYTIC CONTINUATION'
     298                 :             :  CASE (SIG_GW_CD)
     299                 :          16 :    write(msg,'(a)')' CONTOUR DEFORMATION'
     300                 :             :  CASE (SIG_HF)
     301                 :          35 :    write(msg,'(a)')' Hartree-Fock'
     302                 :             :  CASE (SIG_SEX)
     303                 :           1 :    write(msg,'(a)')' Screened Exchange'
     304                 :             :  CASE (SIG_COHSEX)
     305                 :           2 :    write(msg,'(a)')' COHSEX'
     306                 :             :  CASE (SIG_QPGW_PPM)
     307                 :          19 :    write(msg,'(a,i2)')' MODEL GW with PLASMON POLE MODEL ',Sigp%ppmodel
     308                 :             :  CASE (SIG_QPGW_CD)
     309                 :           4 :    write(msg,'(a)')' MODEL GW without PLASMON POLE MODEL'
     310                 :             :  CASE DEFAULT
     311                 :           0 :    write(msg,'(a,i0)')' Wrong value for Sigp%gwcalctyp = ',Sigp%gwcalctyp
     312   [ +  +  +  +  :         178 :    ABI_BUG(msg)
             +  +  +  +  
                      - ]
     313                 :             :  END SELECT
     314                 :         178 :  call wrtout(units, msg)
     315                 :             : 
     316                 :         178 :  write(msg,'(a,i12)')' number of plane-waves for SigmaX         ',Sigp%npwx
     317                 :         178 :  call wrtout(units, msg)
     318                 :         178 :  write(msg,'(a,i12)')' number of plane-waves for SigmaC and W   ',Sigp%npwc
     319                 :         178 :  call wrtout(units, msg)
     320                 :         178 :  write(msg,'(a,i12)')' number of plane-waves for wavefunctions  ',Sigp%npwwfn
     321                 :         178 :  call wrtout(units, msg)
     322                 :         178 :  write(msg,'(a,i12)')' number of bands                          ',Sigp%nbnds
     323                 :         178 :  call wrtout(units, msg)
     324                 :         178 :  write(msg,'(a,i12)')' number of independent spin polarizations ',Sigp%nsppol
     325                 :         178 :  call wrtout(units, msg)
     326                 :         178 :  write(msg,'(a,i12)')' number of spinorial components           ',Sigp%nspinor
     327                 :         178 :  call wrtout(units, msg)
     328                 :         178 :  write(msg,'(a,i12)')' number of k-points in IBZ                ',Kmesh%nibz
     329                 :         178 :  call wrtout(units, msg)
     330                 :         178 :  write(msg,'(a,i12)')' number of q-points in IBZ                ',Qmesh%nibz
     331                 :         178 :  call wrtout(units, msg)
     332                 :         178 :  write(msg,'(a,i12)')' number of symmetry operations            ',Cryst%nsym
     333                 :         178 :  call wrtout(units, msg)
     334                 :         178 :  write(msg,'(a,i12)')' number of k-points in BZ                 ',Kmesh%nbz
     335                 :         178 :  call wrtout(units, msg)
     336                 :         178 :  write(msg,'(a,i12)')' number of q-points in BZ                 ',Qmesh%nbz
     337                 :         178 :  call wrtout(units, msg)
     338                 :         178 :  write(msg,'(a,i12)')' number of frequencies for dSigma/dE      ',Sigp%nomegasrd
     339                 :         178 :  call wrtout(units, msg)
     340                 :         178 :  write(msg,'(a,f12.2)')' frequency step for dSigma/dE [eV]        ',Sigp%deltae*Ha_eV
     341                 :         178 :  call wrtout(units, msg)
     342                 :         178 :  write(msg,'(a,i12)')' number of omega for Sigma on real axis   ',Sigp%nomegasr
     343                 :         178 :  call wrtout(units, msg)
     344                 :         178 :  write(msg,'(a,f12.2)')' max omega for Sigma on real axis  [eV]   ',Sigp%maxomega_r*Ha_eV
     345                 :         178 :  call wrtout(units, msg)
     346                 :         178 :  write(msg,'(a,f12.2)')' zcut for avoiding poles [eV]             ',Sigp%zcut*Ha_eV
     347                 :         178 :  call wrtout(units, msg)
     348                 :             : 
     349         [ +  + ]:         178 :  if (Sigp%mbpt_sciss>0.1d-4) then
     350                 :           2 :    write(msg,'(a,f12.2)')' scissor energy [eV]                      ',Sigp%mbpt_sciss*Ha_eV
     351                 :           2 :    call wrtout(units, msg)
     352                 :             :  end if
     353                 :             : 
     354         [ +  + ]:         178 :  if (mod10 == SIG_GW_AC) then
     355                 :           9 :    write(msg,'(a,i12)')' number of imaginary frequencies for Sigma',Sigp%nomegasi
     356                 :           9 :    call wrtout(units, msg)
     357                 :             :    ! MRM not needed for GW 1RDM
     358         [ +  + ]:           9 :    if (gwcalctyp/=21) then
     359                 :           4 :      write(msg,'(a,f12.2)')' max omega for Sigma on imag axis  [eV]   ',Sigp%omegasimax*Ha_eV
     360                 :           4 :      call wrtout(units, msg)
     361                 :             :    endif
     362                 :             :  end if
     363                 :             : 
     364         [ +  + ]:         178 :  if (Sigp%needs_w()) then
     365                 :         143 :    write(msg,'(2a)')ch10,' EPSILON^-1 parameters (SCR file):'
     366                 :         143 :    call wrtout(units, msg)
     367                 :         143 :    write(msg,'(a,i12)')' dimension of the eps^-1 matrix on file   ',epsm1%Hscr%npwe
     368                 :         143 :    call wrtout(units, msg)
     369                 :         143 :    write(msg,'(a,i12)')' dimension of the eps^-1 matrix used      ',epsm1%npwe
     370                 :         143 :    call wrtout(units, msg)
     371                 :         143 :    write(msg,'(a,i12)')' number of plane-waves for wavefunctions  ',epsm1%Hscr%npwwfn_used
     372                 :         143 :    call wrtout(units, msg)
     373                 :         143 :    write(msg,'(a,i12)')' number of bands                          ',epsm1%Hscr%nbnds_used
     374                 :         143 :    call wrtout(units, msg)
     375                 :         143 :    write(msg,'(a,i12)')' number of q-points in IBZ                ',Qmesh%nibz
     376                 :         143 :    call wrtout(units, msg)
     377                 :         143 :    write(msg,'(a,i12)')' number of frequencies                    ',epsm1%nomega
     378                 :         143 :    call wrtout(units, msg)
     379                 :         143 :    write(msg,'(a,i12)')' number of real frequencies               ',epsm1%nomega_r
     380                 :         143 :    call wrtout(units, msg)
     381                 :         143 :    write(msg,'(a,i12)')' number of imag frequencies               ',epsm1%nomega_i
     382                 :         143 :    call wrtout(units, msg)
     383                 :             :  end if
     384                 :             : 
     385                 :             :   ! MRM not needed for GW 1RDM
     386         [ +  + ]:         178 :   if (gwcalctyp /= 21) then
     387                 :         173 :     write(msg,'(3a)')ch10,' matrix elements of self-energy operator (all in [eV])',ch10
     388                 :         173 :     call wrtout(units, msg)
     389                 :             :     !call wrtout(units, "(a)")" Notations:"
     390                 :             :     !call wrtout(units, "(a)")"E0: KS eigenvalue.")
     391                 :             :     !call wrtout(units, "(a)")"VxcDFT: KS exchange-correlation potential expectation value.")
     392                 :             :     !call wrtout(units, "(a)")"SigX: exchange part of the self-energy.")
     393                 :             :     !call wrtout(units, "(a)")"SigC(E0) correlation part of the self-energy, evaluated at the KS eigenenergy.")
     394                 :             :     !call wrtout(units, "(a)")"Z: renormalization factor.")
     395                 :             :     !call wrtout(units, "(a)")"dSigC/dE: energy derivative of SigC with respect to the energy.")
     396                 :             :     !call wrtout(units, "(a)")"SigC(E): correlation part of the self-energy, evaluated at the QP energy.")
     397                 :             :     !call wrtout(units, "(a)")"E-E0: difference between QP energy and KS eigenenergy.")
     398                 :             :     !call wrtout(units, "(a)")"E: quasiparticle energy.")
     399                 :             :     !if (mod10 == SIG_GW_AC) then
     400                 :             :     !  call wrtout(units, "For AC calculations, the KS Fermi level has been set to zero.")
     401                 :             :     !  call wrtout(units, "KS and QP energies are shifted accordingly.")
     402                 :             :     !  call wrtout(units, "IMPORTANT: In AC calculations, the QP energies are obtained by solving the non-linear QP equation along the real-axis")
     403                 :             :     !end if
     404                 :             :   end if
     405                 :             : 
     406         [ +  + ]:         173 :  if (gwcalctyp < 10) then
     407                 :         118 :    write(msg,'(a)')' Perturbative Calculation'
     408         [ +  + ]:          60 :  else if (gwcalctyp < 20) then
     409                 :           7 :    write(msg,'(a)')' Self-Consistent on Energies only'
     410                 :             :  else
     411                 :          53 :    write(msg,'(a)')' Self-Consistent on Energies and Wavefunctions'
     412                 :             :  end if
     413                 :         178 :  call wrtout(units, msg)
     414                 :             : 
     415                 :         178 : end subroutine write_sigma_header
     416                 :             : !!***
     417                 :             : 
     418                 :             : !----------------------------------------------------------------------
     419                 :             : 
     420                 :             : !!****f* m_sigma/sigma_write_results
     421                 :             : !! NAME
     422                 :             : !! sigma_write_results
     423                 :             : !!
     424                 :             : !! FUNCTION
     425                 :             : !!  Write the final results of the GW calculation.
     426                 :             : !!
     427                 :             : !! INPUTS
     428                 :             : !!  Sigp=sigparams_t datatype
     429                 :             : !!  ik_ibz= index of the k-point in the array kibz, where GW corrections are calculated
     430                 :             : !!  ikcalc= index of the k-point in the array Sigp%kptgw2bz
     431                 :             : !!  ks_ebands<ebands_t>=Info on the KS band structure energies.
     432                 :             : !!
     433                 :             : !! SOURCE
     434                 :             : 
     435                 :         502 : subroutine sigma_write_results(sigma, ikcalc, ik_ibz, Sigp, ks_ebands)
     436                 :             : 
     437                 :             : !Arguments ------------------------------------
     438                 :             : !scalars
     439                 :             :  class(sigma_t),intent(in) :: sigma
     440                 :             :  integer,intent(in) :: ikcalc,ik_ibz
     441                 :             :  type(ebands_t),intent(in) :: ks_ebands
     442                 :             :  type(sigparams_t),intent(in) :: Sigp
     443                 :             : 
     444                 :             : !Local variables-------------------------------
     445                 :             : !scalars
     446                 :             :  integer :: ib,io,is,gwcalctyp,mod10
     447                 :             :  character(len=500) :: msg
     448                 :             :  type(yamldoc_t) :: ydoc
     449                 :             : !arrays
     450                 :             :  character(len=12) :: tag_spin(2)
     451                 :             : ! *************************************************************************
     452                 :             : 
     453                 :         502 :  gwcalctyp = Sigp%gwcalctyp
     454                 :         502 :  mod10 = MOD(Sigp%gwcalctyp,10)
     455                 :             : 
     456                 :             :  ! unt_gw:  File with GW corrections.
     457                 :             :  ! unt_sig: Self-energy as a function of frequency.
     458                 :             :  ! unt_sgr: Derivative wrt omega of the Self-energy.
     459                 :             :  ! unt_sigc: Sigma_c(eik) MRM
     460                 :             :  ! unt_sgm: Sigma on the Matsubara axis (imag axis)
     461                 :             : 
     462   [ +  +  +  +  :        1522 :  tag_spin=(/'            ','            '/); if (sigma%nsppol==2) tag_spin=(/',  SPIN UP  ',',  SPIN DOWN'/)
                   +  + ]
     463                 :             : 
     464         [ +  + ]:        1012 :  do is=1,sigma%nsppol
     465                 :         510 :    write(msg,'(2a,3f8.3,a)')ch10,' k = ',Sigp%kptgw(:,ikcalc),tag_spin(is)
     466                 :         510 :    call wrtout(std_out,msg)
     467                 :             :    !call wrtout(ab_out,msg)
     468                 :             : 
     469                 :         510 :    msg = ' Band     E0 <VxcDFT>   SigX SigC(E0)      Z dSigC/dE  Sig(E)    E-E0       E'
     470         [ -  + ]:         510 :    if (sigma%usepawu/=0) then
     471                 :           0 :      msg = ' Band     E0 <VxcDFT>   <H_U>  SigX SigC(E0)      Z dSigC/dE  Sig(E)    E-E0       E'
     472                 :             :    end if
     473                 :             : 
     474         [ +  + ]:         510 :    if (gwcalctyp>=10) then
     475                 :             :      write(msg,'(2a)')&
     476                 :         297 :      ' Band     E_DFT   <VxcDFT>   E(N-1)  <Hhartree>   SigX  SigC[E(N-1)]',&
     477                 :         594 :      '    Z     dSigC/dE  Sig[E(N)]  DeltaE  E(N)_pert E(N)_diago'
     478                 :             :    end if
     479                 :         510 :    call wrtout(std_out,msg)
     480                 :             : 
     481                 :         510 :    ydoc = yamldoc_open('SelfEnergy_ee', width=11, real_fmt='(3f8.3)')
     482                 :         510 :    call ydoc%add_real1d('kpoint', Sigp%kptgw(:,ikcalc))
     483                 :         510 :    call ydoc%add_int('spin', is, int_fmt="(i1)")
     484                 :         510 :    call ydoc%add_real('KS_gap', sigma%e0gap(ik_ibz,is)*Ha_eV)
     485                 :         510 :    call ydoc%add_real('QP_gap', sigma%egwgap(ik_ibz,is)*Ha_eV)
     486                 :         510 :    call ydoc%add_real('Delta_QP_KS', sigma%degwgap(ik_ibz,is)*Ha_eV)
     487                 :         510 :    call ydoc%open_tabular('data', tag='SigmaeeData')
     488                 :         510 :    call ydoc%add_tabular_line(msg)
     489                 :             : 
     490                 :         510 :    write(unt_gw,'(3f10.6)')Sigp%kptgw(:,ikcalc)
     491                 :         510 :    write(unt_gw,'(i4)')Sigp%maxbnd(ikcalc,is)-Sigp%minbnd(ikcalc,is)+1
     492                 :             : 
     493                 :         510 :    write(unt_gwdiag,'(3f10.6)')Sigp%kptgw(:,ikcalc)
     494                 :         510 :    write(unt_gwdiag,'(i4)')Sigp%maxbnd(ikcalc,is)-Sigp%minbnd(ikcalc,is)+1
     495                 :             : 
     496                 :         510 :    write(unt_sig,'("# k = ",3f10.6)')Sigp%kptgw(:,ikcalc)
     497                 :         510 :    write(unt_sig,'("# b = ",2i10)')Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is)
     498                 :             : 
     499                 :         510 :    write(unt_sgr,'("# k = ",3f10.6)')Sigp%kptgw(:,ikcalc)
     500                 :         510 :    write(unt_sgr,'("# b = ",2i10)')Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is)
     501                 :             : 
     502                 :         510 :    write(unt_sigc,'("# k = ",3f10.6)')Sigp%kptgw(:,ikcalc)
     503                 :         510 :    write(unt_sigc,'("# b = ",2i10)')Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is)
     504                 :             : 
     505         [ +  + ]:        4859 :    do ib=Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is)
     506         [ +  + ]:        4349 :      if (gwcalctyp >= 10) then
     507                 :        3182 :        call sigma%print_QPSC(ik_ibz, ib, is, ks_ebands, units=[dev_null], ydoc=ydoc)
     508         [ +  + ]:        6364 :        call sigma%print_QPSC(ik_ibz, ib, is, ks_ebands, units=[std_out], prtvol=1)
     509                 :             : 
     510                 :             :        write(unt_gwdiag,'(i6,3f9.4)')                                 &
     511                 :        3182 :         ib,                                                           &
     512                 :        3182 :         sigma%en_qp_diago(ib,ik_ibz,is)*Ha_eV,                            &
     513                 :        3182 :         (sigma%en_qp_diago(ib,ik_ibz,is) - ks_ebands%eig(ib,ik_ibz,is))*Ha_eV,&
     514                 :        6364 :         zero
     515                 :             : 
     516                 :             :      else
     517                 :             :        ! If not ppmodel, write out also the imaginary part in ab_out
     518                 :         378 :        select case(mod10)
     519                 :             :        case (SIG_GW_AC, SIG_GW_CD)
     520                 :         378 :          call sigma%print_perturbative(ik_ibz, ib, is, units=[dev_null], ydoc=ydoc, prtvol=1)
     521                 :             :        case default
     522         [ +  + ]:        1167 :          call sigma%print_perturbative(ik_ibz, ib, is, units=[dev_null], ydoc=ydoc)
     523                 :             :        end select
     524         [ +  + ]:        2334 :        call sigma%print_perturbative(ik_ibz, ib, is, units=[std_out], prtvol=1)
     525                 :             :      end if
     526                 :             : 
     527                 :             :      write(unt_gw,'(i6,3f9.4)')         &
     528                 :        4349 :       ib,                               &
     529                 :        4349 :       REAL (sigma%egw (ib,ik_ibz,is))*Ha_eV,&
     530                 :        4349 :       REAL (sigma%degw(ib,ik_ibz,is))*Ha_eV,&
     531                 :        9208 :       AIMAG(sigma%egw (ib,ik_ibz,is))*Ha_eV
     532                 :             :    end do !ib
     533                 :             : 
     534         [ +  + ]:         510 :    if (sigma%e0gap(ik_ibz,is)**2+sigma%egwgap(ik_ibz,is)**2+sigma%degwgap(ik_ibz,is)**2 > tol10) then
     535                 :             :      ! Output the direct gap for each spin
     536                 :             :      ! If all the gaps are zero, this means that they could not be computed in the calling routine
     537                 :         487 :      write(msg,'(2a,f8.3)')ch10,' E^0_gap       ',sigma%e0gap(ik_ibz,is)*Ha_eV
     538                 :         487 :      call wrtout(std_out,msg)
     539                 :         487 :      write(msg,'(a,f8.3)')      ' E^GW_gap      ',sigma%egwgap(ik_ibz,is)*Ha_eV
     540                 :         487 :      call wrtout(std_out,msg)
     541                 :         487 :      write(msg,'(a,f8.3,a)')    ' DeltaE^GW_gap ',sigma%degwgap(ik_ibz,is)*Ha_eV,ch10
     542                 :         487 :      call wrtout(std_out,msg)
     543                 :             :    end if
     544                 :             : 
     545                 :         510 :    call ydoc%write_and_free(ab_out)
     546                 :             : 
     547                 :             :    ! Output of the spectral function.
     548         [ +  + ]:        1368 :    do io=1,sigma%nomega_r
     549                 :             :      write(unt_sig,'(100(e12.5,2x))')&
     550                 :         858 :       REAL(sigma%omega_r(io))*Ha_eV,&
     551                 :        3740 :       (REAL(sigma%sigxcme(ib,ik_ibz,io,is))*Ha_eV,&
     552                 :        3740 :       AIMAG(sigma%sigxcme(ib,ik_ibz,io,is))*Ha_eV,&
     553                 :        4598 :       gw_spectral_function(sigma,io,ib,ik_ibz,is),&
     554   [ +  -  +  + ]:        6824 :       ib=Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is))
     555                 :             :    end do
     556                 :             : 
     557         [ +  + ]:        4859 :    do ib=Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is)
     558                 :        4349 :      write(unt_sgr,'("# ik, ib",2i5)')ik_ibz,ib
     559         [ +  + ]:       19424 :      do io=1,sigma%nomega4sd
     560                 :             :        write(unt_sgr,'(100(e12.5,2x))')              &
     561                 :       14565 :          REAL (sigma%omega4sd  (ib,ik_ibz,io,is)) * Ha_eV,&
     562                 :       14565 :          REAL (sigma%sigxcme4sd(ib,ik_ibz,io,is)) * Ha_eV,&
     563                 :       33479 :          AIMAG(sigma%sigxcme4sd(ib,ik_ibz,io,is)) * Ha_eV
     564                 :             :      end do
     565                 :             :    end do
     566                 :             : 
     567                 :             :    !MRM
     568         [ +  + ]:        4859 :    do ib=Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is)
     569                 :        4349 :      write(unt_sigc,'("# ik, ib",2i5)')ik_ibz,ib
     570         [ +  + ]:       19424 :      do io=1,sigma%nomega4sd
     571                 :             :        write(unt_sigc,'(100(e12.5,2x))')              &
     572                 :       14565 :          REAL (sigma%omega4sd  (ib,ik_ibz,io,is)) * Ha_eV,&
     573                 :       14565 :          REAL (sigma%sigcme4sd(ib,ik_ibz,io,is))  * Ha_eV,&
     574                 :       33479 :          AIMAG(sigma%sigcme4sd(ib,ik_ibz,io,is))  * Ha_eV
     575                 :             :      end do
     576                 :             :    end do
     577                 :             : 
     578         [ +  + ]:        1012 :    if (mod10 == SIG_GW_AC) then
     579                 :             :      ! For AC, write sigma matrix elements along the imaginary axis
     580         [ +  + ]:          60 :      do ib=Sigp%minbnd(ikcalc,is),Sigp%maxbnd(ikcalc,is)
     581                 :          53 :        write(unt_sgm,'("# ik, ib",2i5)')ik_ibz,ib
     582         [ +  + ]:         670 :        do io=1,sigma%nomega_i
     583                 :             :          write(unt_sgm,'(3(e12.5,2x))')             &
     584                 :         610 :           AIMAG(sigma%omega_i(io))                * Ha_eV,&
     585                 :         610 :           REAL (sigma%sigxcmesi(ib,ik_ibz,io,is)) * Ha_eV,&
     586                 :        1273 :           AIMAG(sigma%sigxcmesi(ib,ik_ibz,io,is)) * Ha_eV
     587                 :             :        end do
     588                 :             :      end do
     589                 :             :    end if
     590                 :             : 
     591                 :             :  end do !is
     592                 :             : 
     593                 :         502 : end subroutine sigma_write_results
     594                 :             : !!***
     595                 :             : 
     596                 :             : !----------------------------------------------------------------------
     597                 :             : 
     598                 :             : !!****f* m_sigma/gw_spectral_function
     599                 :             : !! NAME
     600                 :             : !! gw_spectral_function
     601                 :             : !!
     602                 :             : !! FUNCTION
     603                 :             : !!  Compute the spectral function
     604                 :             : !!
     605                 :             : !! INPUTS
     606                 :             : !!  io,ib,ik_ibz,spin=Frequency, band, k-point, spin index
     607                 :             : !!
     608                 :             : !! SOURCE
     609                 :             : 
     610                 :        3740 : real(dp) pure function gw_spectral_function(sigma, io, ib, ik_ibz, spin) result(aw)
     611                 :             : 
     612                 :             : !Arguments ------------------------------------
     613                 :             :  class(sigma_t),intent(in) :: sigma
     614                 :             :  integer,intent(in) :: io,ib,ik_ibz,spin
     615                 :             : ! *********************************************************************
     616                 :             : 
     617                 :             :  aw = one / pi * abs(aimag(sigma%sigcme(ib,ik_ibz,io,spin))) &
     618                 :             :    /( (real(sigma%omega_r(io) - sigma%hhartree(ib,ib,ik_ibz,spin) - sigma%sigxcme(ib,ik_ibz,io,spin)))**2 &
     619                 :        3740 :      +(aimag(sigma%sigcme(ib,ik_ibz,io,spin))) ** 2) / Ha_eV
     620                 :             : 
     621                 :        3740 : end function gw_spectral_function
     622                 :             : !!***
     623                 :             : 
     624                 :             : !----------------------------------------------------------------------
     625                 :             : 
     626                 :             : !!****f* m_sigma/sigma_print_pertubative
     627                 :             : !! NAME
     628                 :             : !! sigma_print_pertubative
     629                 :             : !!
     630                 :             : !! FUNCTION
     631                 :             : !!  Write the results of the GW calculation done with the perturbative approach
     632                 :             : !!
     633                 :             : !! INPUTS
     634                 :             : !!
     635                 :             : !! OUTPUT
     636                 :             : !!
     637                 :             : !! SOURCE
     638                 :             : 
     639   [ +  -  +  -  :        2334 : subroutine sigma_print_pertubative(sigma, ik_ibz, band, spin, units, &
                   -  + ]
     640                 :             :                                    prtvol, with_header, ydoc) ! Optional
     641                 :             : 
     642                 :             : !Arguments ------------------------------------
     643                 :             : !scalars
     644                 :             :  class(sigma_t),intent(in) :: sigma
     645                 :             :  integer,intent(in) :: band,ik_ibz,spin
     646                 :             :  integer,optional,intent(in) :: prtvol,units(:)
     647                 :             :  logical,optional,intent(in) :: with_header
     648                 :             :  type(yamldoc_t),intent(inout),optional :: ydoc
     649                 :             : 
     650                 :             : !Local variables-------------------------------
     651                 :             : !scalars
     652                 :             :  integer :: verbose
     653                 :             :  character(len=500) :: msg
     654                 :             : ! *********************************************************************
     655                 :             : 
     656         [ +  + ]:        2334 :  verbose=0      ; if (PRESENT(prtvol)) verbose=prtvol
     657                 :             : 
     658         [ -  + ]:        2334 :  if (present(with_header)) then
     659         [ #  # ]:           0 :    if (with_header) then
     660                 :           0 :      call wrtout(units,' Band     E0 <VxcDFT>   SigX SigC(E0)      Z dSigC/dE  Sig(E)    E-E0       E ')
     661                 :             :    end if
     662                 :             :  end if
     663                 :             : 
     664         [ +  - ]:        2334 :  if (sigma%usepawu == 0) then
     665                 :             : 
     666         [ +  + ]:        2334 :    if (sigma%nsig_ab /= 1) then
     667                 :             :      write(msg,'(i5,9f8.3)')                       &
     668                 :         288 :            band,                                  &
     669                 :         288 :            sigma%e0          (band,ik_ibz,1)*Ha_eV,  &
     670         [ +  + ]:        1440 :            SUM(sigma%vxcme   (band,ik_ibz,:))*Ha_eV, &
     671         [ +  + ]:        1440 :            SUM(sigma%sigxme  (band,ik_ibz,:))*Ha_eV, &
     672         [ +  + ]:        1440 :       REAL(SUM(sigma%sigcmee0(band,ik_ibz,:)))*Ha_eV,&
     673                 :         288 :       REAL(sigma%ze0         (band,ik_ibz,1)),       &
     674         [ +  + ]:        1440 :       REAL(SUM(sigma%dsigmee0(band,ik_ibz,:))),      &
     675         [ +  + ]:        1440 :       REAL(SUM(sigma%sigmee  (band,ik_ibz,:)))*Ha_eV,&
     676                 :         288 :       REAL(sigma%degw        (band,ik_ibz,1))*Ha_eV, &
     677                 :         576 :       REAL(sigma%egw         (band,ik_ibz,1))*Ha_eV
     678                 :         288 :      call wrtout(units, msg)
     679         [ +  + ]:         288 :      if (present(ydoc)) call ydoc%add_tabular_line(msg)
     680         [ +  + ]:         288 :      if (verbose /= 0) then
     681                 :             :        write(msg,'(i5,9f8.3)')                        &
     682                 :         198 :               band,                                   &
     683                 :         198 :               zero,                                   &
     684                 :         198 :               zero,                                   &
     685                 :         198 :               zero,                                   &
     686         [ +  + ]:         990 :         AIMAG(SUM(sigma%sigcmee0(band,ik_ibz,:)))*Ha_eV,&
     687                 :         198 :         AIMAG(sigma%ze0         (band,ik_ibz,1)),       &
     688         [ +  + ]:         990 :         AIMAG(SUM(sigma%dsigmee0(band,ik_ibz,:))),      &
     689         [ +  + ]:         990 :         AIMAG(SUM(sigma%sigmee  (band,ik_ibz,:)))*Ha_eV,&
     690                 :         198 :         AIMAG(sigma%degw        (band,ik_ibz,1))*Ha_eV, &
     691                 :         396 :         AIMAG(sigma%egw         (band,ik_ibz,1))*Ha_eV
     692                 :         198 :        call wrtout(units, msg)
     693         [ +  + ]:         198 :        if(present(ydoc)) call ydoc%add_tabular_line(msg)
     694                 :             :      end if
     695                 :             :   else
     696                 :             :     write(msg,'(i5,9f8.3)')                    &
     697                 :        2046 :           band,                                &
     698                 :        2046 :           sigma%e0      (band,ik_ibz,spin)*Ha_eV, &
     699                 :        2046 :           sigma%vxcme   (band,ik_ibz,spin)*Ha_eV, &
     700                 :        2046 :           sigma%sigxme  (band,ik_ibz,spin)*Ha_eV, &
     701                 :        2046 :      REAL(sigma%sigcmee0(band,ik_ibz,spin))*Ha_eV,&
     702                 :        2046 :      REAL(sigma%ze0     (band,ik_ibz,spin)),      &
     703                 :        2046 :      REAL(sigma%dsigmee0(band,ik_ibz,spin)),      &
     704                 :        2046 :      REAL(sigma%sigmee  (band,ik_ibz,spin))*Ha_eV,&
     705                 :        2046 :      REAL(sigma%degw    (band,ik_ibz,spin))*Ha_eV,&
     706                 :        4092 :      REAL(sigma%egw     (band,ik_ibz,spin))*Ha_eV
     707                 :        2046 :     call wrtout(units, msg)
     708         [ +  + ]:        2046 :     if (present(ydoc)) call ydoc%add_tabular_line(msg)
     709                 :             : 
     710         [ +  + ]:        2046 :     if (verbose /= 0) then
     711                 :             :       write(msg,'(i5,9f8.3)')                      &
     712                 :        1347 :               band,                                &
     713                 :        1347 :               zero,                                &
     714                 :        1347 :               zero,                                &
     715                 :        1347 :               zero,                                &
     716                 :        1347 :         AIMAG(sigma%sigcmee0(band,ik_ibz,spin))*Ha_eV,&
     717                 :        1347 :         AIMAG(sigma%ze0     (band,ik_ibz,spin)),      &
     718                 :        1347 :         AIMAG(sigma%dsigmee0(band,ik_ibz,spin)),      &
     719                 :        1347 :         AIMAG(sigma%sigmee  (band,ik_ibz,spin))*Ha_eV,&
     720                 :        1347 :         AIMAG(sigma%degw    (band,ik_ibz,spin))*Ha_eV,&
     721                 :        2694 :         AIMAG(sigma%egw     (band,ik_ibz,spin))*Ha_eV
     722                 :        1347 :        call wrtout(units,msg)
     723         [ +  + ]:        1347 :        if (present(ydoc)) call ydoc%add_tabular_line(msg)
     724                 :             :     end if
     725                 :             :   end if
     726                 :             : 
     727                 :             :  else
     728                 :             :    ! PAW+U+GW calculation.
     729         [ #  # ]:           0 :    ABI_CHECK(sigma%nsig_ab==1, 'DFT+U with spinor not implemented')
     730                 :             :    write(msg,'(i5,10f8.3)')                   &
     731                 :           0 :          band,                                &
     732                 :           0 :          sigma%e0      (band,ik_ibz,spin)*Ha_eV, &
     733                 :           0 :          sigma%vxcme   (band,ik_ibz,spin)*Ha_eV, &
     734                 :           0 :          sigma%vUme    (band,ik_ibz,spin)*Ha_eV, &
     735                 :           0 :          sigma%sigxme  (band,ik_ibz,spin)*Ha_eV, &
     736                 :           0 :     REAL(sigma%sigcmee0(band,ik_ibz,spin))*Ha_eV,&
     737                 :           0 :     REAL(sigma%ze0     (band,ik_ibz,spin)),      &
     738                 :           0 :     REAL(sigma%dsigmee0(band,ik_ibz,spin)),      &
     739                 :           0 :     REAL(sigma%sigmee  (band,ik_ibz,spin))*Ha_eV,&
     740                 :           0 :     REAL(sigma%degw    (band,ik_ibz,spin))*Ha_eV,&
     741                 :           0 :     REAL(sigma%egw     (band,ik_ibz,spin))*Ha_eV
     742                 :           0 :    call wrtout(units,msg)
     743         [ #  # ]:           0 :    if(present(ydoc)) call ydoc%add_tabular_line(msg)
     744                 :             : 
     745         [ #  # ]:           0 :    if (verbose/=0) then
     746                 :             :      write(msg,'(i5,10f8.3)')                   &
     747                 :           0 :            band,                               &
     748                 :           0 :            zero,                                &
     749                 :           0 :            zero,                                &
     750                 :           0 :            zero,                                &
     751                 :           0 :            zero,                                &
     752                 :           0 :      AIMAG(sigma%sigcmee0(band,ik_ibz,spin))*Ha_eV,&
     753                 :           0 :      AIMAG(sigma%ze0     (band,ik_ibz,spin)),      &
     754                 :           0 :      AIMAG(sigma%dsigmee0(band,ik_ibz,spin)),      &
     755                 :           0 :      AIMAG(sigma%sigmee  (band,ik_ibz,spin))*Ha_eV,&
     756                 :           0 :      AIMAG(sigma%degw    (band,ik_ibz,spin))*Ha_eV,&
     757                 :           0 :      AIMAG(sigma%egw     (band,ik_ibz,spin))*Ha_eV
     758                 :           0 :      call wrtout(units, msg)
     759         [ #  # ]:           0 :      if(present(ydoc)) call ydoc%add_tabular_line(msg)
     760                 :             :    end if
     761                 :             :  end if
     762                 :             : 
     763                 :        2334 : end subroutine sigma_print_pertubative
     764                 :             : !!***
     765                 :             : 
     766                 :             : !----------------------------------------------------------------------
     767                 :             : 
     768                 :             : !!****f* m_sigma/sigma_print_qpsc
     769                 :             : !! NAME
     770                 :             : !!  sigma_print_qpsc
     771                 :             : !!
     772                 :             : !! FUNCTION
     773                 :             : !!  Write the results of the GW calculation in case of self-consistency
     774                 :             : !!
     775                 :             : !! SOURCE
     776                 :             : 
     777   [ +  -  +  -  :        6364 : subroutine sigma_print_qpsc(sigma, ik_ibz, band, spin, ks_ebands, units, &
                   -  + ]
     778                 :             :                             prtvol, ydoc) ! Optional
     779                 :             : 
     780                 :             : !Arguments ------------------------------------
     781                 :             : !scalars
     782                 :             :  class(sigma_t),intent(in) :: sigma
     783                 :             :  integer,intent(in) :: band,ik_ibz,spin
     784                 :             :  integer,intent(in),optional :: prtvol, units(:)
     785                 :             :  type(ebands_t),intent(in) :: ks_ebands
     786                 :             :  type(yamldoc_t),intent(inout),optional :: ydoc
     787                 :             : 
     788                 :             : !Local variables-------------------------------
     789                 :             : !scalars
     790                 :             :  integer :: verbose
     791                 :             :  character(len=500) :: msg
     792                 :             : ! *********************************************************************
     793                 :             : 
     794         [ +  + ]:        6364 :  verbose=0      ; if (PRESENT(prtvol    )) verbose=prtvol
     795                 :             : 
     796                 :             : ! write(msg,'(a)')&
     797                 :             : !&   ' Band     E_DFT   <VxcDFT>   E(N-1)  <Hhartree>   SigX  SigC[E(N-1)]',&
     798                 :             : !&   '    Z     dSigC/dE  Sig[E(N)]  DeltaE  E(N)_pert E(N)_diago'
     799                 :             : 
     800                 :             :  if (sigma%usepawu==0 .or. .TRUE.) then
     801         [ -  + ]:        6364 :    if (sigma%nsig_ab/=1) then
     802                 :             :      write(msg,'(i5,12(2x,f8.3))')                       &
     803                 :           0 :            band,                                         &
     804                 :           0 :            ks_ebands%eig     (band,ik_ibz,1)*Ha_eV,        &
     805         [ #  # ]:           0 :            SUM(sigma%vxcme   (band,ik_ibz,:))*Ha_eV,       &
     806                 :           0 :            sigma%e0          (band,ik_ibz,1)*Ha_eV,        &
     807         [ #  # ]:           0 :       REAL(SUM(sigma%hhartree(band,band,ik_ibz,:)))*Ha_eV,&
     808         [ #  # ]:           0 :            SUM(sigma%sigxme  (band,ik_ibz,:))*Ha_eV,       &
     809         [ #  # ]:           0 :       REAL(SUM(sigma%sigcmee0(band,ik_ibz,:)))*Ha_eV,      &
     810                 :           0 :       REAL(sigma%ze0         (band,ik_ibz,1)),             &
     811         [ #  # ]:           0 :       REAL(SUM(sigma%dsigmee0(band,ik_ibz,:))),            &
     812         [ #  # ]:           0 :       REAL(SUM(sigma%sigmee  (band,ik_ibz,:)))*Ha_eV,      &
     813                 :           0 :       REAL(sigma%degw        (band,ik_ibz,1))*Ha_eV,       &
     814                 :           0 :       REAL(sigma%egw         (band,ik_ibz,1))*Ha_eV,       &
     815                 :           0 :            sigma%en_qp_diago (band,ik_ibz,1)*Ha_eV
     816                 :           0 :      call wrtout(units, msg)
     817         [ #  # ]:           0 :      if (present(ydoc)) call ydoc%add_tabular_line(msg)
     818                 :             : 
     819                 :             :      write(msg,'(i5,12(2x,f8.3))')                        &
     820                 :           0 :             band,                                         &
     821                 :           0 :             zero,                                         &
     822                 :           0 :             zero,                                         &
     823                 :           0 :             zero,                                         &
     824         [ #  # ]:           0 :       AIMAG(SUM(sigma%hhartree(band,band,ik_ibz,:)))*Ha_eV,&
     825                 :           0 :             zero,                                           &
     826         [ #  # ]:           0 :       AIMAG(SUM(sigma%sigcmee0(band,ik_ibz,:)))*Ha_eV,      &
     827                 :           0 :       AIMAG(sigma%ze0         (band,ik_ibz,1)),             &
     828         [ #  # ]:           0 :       AIMAG(SUM(sigma%dsigmee0(band,ik_ibz,:))),            &
     829         [ #  # ]:           0 :       AIMAG(SUM(sigma%sigmee  (band,ik_ibz,:)))*Ha_eV,      &
     830                 :           0 :       AIMAG(sigma%degw        (band,ik_ibz,1))*Ha_eV,       &
     831                 :           0 :       AIMAG(sigma%egw         (band,ik_ibz,1))*Ha_eV,       &
     832                 :           0 :             zero
     833         [ #  # ]:           0 :      if (verbose/=0) then
     834                 :           0 :        call wrtout(units, msg)
     835         [ #  # ]:           0 :        if (present(ydoc)) call ydoc%add_tabular_line(msg)
     836                 :             :      end if
     837                 :             :    else
     838                 :             :      write(msg,'(i5,12(2x,f8.3))')                          &
     839                 :        6364 :            band,                                            &
     840                 :        6364 :            ks_ebands%eig    (band,ik_ibz,spin)*Ha_eV,       &
     841                 :        6364 :            sigma%vxcme      (band,ik_ibz,spin)*Ha_eV,       &
     842                 :        6364 :            sigma%e0         (band,ik_ibz,spin)*Ha_eV,       &
     843                 :        6364 :       REAL(sigma%hhartree   (band,band,ik_ibz,spin))*Ha_eV, &
     844                 :        6364 :            sigma%sigxme     (band,ik_ibz,spin)*Ha_eV,       &
     845                 :        6364 :       REAL(sigma%sigcmee0   (band,ik_ibz,spin))*Ha_eV,      &
     846                 :        6364 :       REAL(sigma%ze0        (band,ik_ibz,spin)),            &
     847                 :        6364 :       REAL(sigma%dsigmee0   (band,ik_ibz,spin)),            &
     848                 :        6364 :       REAL(sigma%sigmee     (band,ik_ibz,spin))*Ha_eV,      &
     849                 :        6364 :       REAL(sigma%degw       (band,ik_ibz,spin))*Ha_eV,      &
     850                 :        6364 :       REAL(sigma%egw        (band,ik_ibz,spin))*Ha_eV,      &
     851                 :       12728 :            sigma%en_qp_diago(band,ik_ibz,spin)*Ha_eV
     852                 :        6364 :      call wrtout(units, msg)
     853         [ +  + ]:        6364 :      if (present(ydoc)) call ydoc%add_tabular_line(msg)
     854                 :             : 
     855                 :             :      write(msg,'(i5,12(2x,f8.3))')                       &
     856                 :        6364 :             band,                                        &
     857                 :        6364 :             zero,                                        &
     858                 :        6364 :             zero,                                        &
     859                 :        6364 :             zero,                                        &
     860                 :        6364 :       AIMAG(sigma%hhartree  (band,band,ik_ibz,spin))*Ha_eV,&
     861                 :        6364 :             zero,                                           &
     862                 :        6364 :       AIMAG(sigma%sigcmee0   (band,ik_ibz,spin))*Ha_eV,     &
     863                 :        6364 :       AIMAG(sigma%ze0        (band,ik_ibz,spin)),           &
     864                 :        6364 :       AIMAG(sigma%dsigmee0   (band,ik_ibz,spin)),           &
     865                 :        6364 :       AIMAG(sigma%sigmee     (band,ik_ibz,spin))*Ha_eV,     &
     866                 :        6364 :       AIMAG(sigma%degw       (band,ik_ibz,spin))*Ha_eV,     &
     867                 :        6364 :       AIMAG(sigma%egw        (band,ik_ibz,spin))*Ha_eV,     &
     868                 :       12728 :             zero
     869         [ +  + ]:        6364 :      if (verbose/=0) then
     870                 :        3182 :        call wrtout(units, msg)
     871         [ -  + ]:        3182 :        if (present(ydoc)) call ydoc%add_tabular_line(msg)
     872                 :             :      end if
     873                 :             :    end if
     874                 :             : 
     875                 :             :  else
     876                 :             :    ! PAW+U+GW calculation.
     877                 :             :    ABI_ERROR("PAW+U+GW not yet implemented")
     878                 :             :  end if
     879                 :             : 
     880                 :        6364 : end subroutine sigma_print_qpsc
     881                 :             : !!***
     882                 :             : 
     883                 :             : !----------------------------------------------------------------------
     884                 :             : 
     885                 :             : !!****f* m_sigma/sigma_init
     886                 :             : !! NAME
     887                 :             : !! sigma_init
     888                 :             : !!
     889                 :             : !! FUNCTION
     890                 :             : !! Main creation method for the sigma_t data type.
     891                 :             : !!
     892                 :             : !! INPUTS
     893                 :             : !! usepawu= /=0 if we used DFT+U as starting point (only for PAW)
     894                 :             : !!
     895                 :             : !! SOURCE
     896                 :             : 
     897                 :         206 : subroutine sigma_init(sigma, Sigp, nkibz, usepawu)
     898                 :             : 
     899                 :             : !Arguments ------------------------------------
     900                 :             :  class(sigma_t),intent(inout) :: sigma
     901                 :             :  type(sigparams_t),intent(in) :: Sigp
     902                 :             :  integer,intent(in) :: nkibz, usepawu
     903                 :             : 
     904                 :             : !Local variables-------------------------------
     905                 :             :  integer :: b1gw,b2gw,mod10
     906                 :             : ! *************************************************************************
     907                 :             : 
     908                 :         206 :  mod10 = MOD(Sigp%gwcalctyp,10)
     909                 :             : 
     910                 :             :  ! Copy important dimensions
     911                 :         206 :  sigma%nkptgw     =Sigp%nkptgw
     912                 :         206 :  sigma%gwcalctyp  =Sigp%gwcalctyp
     913                 :         206 :  sigma%deltae     =Sigp%deltae
     914                 :         206 :  sigma%maxomega4sd=Sigp%maxomega4sd
     915                 :         206 :  sigma%maxomega_r =Sigp%maxomega_r
     916                 :         206 :  sigma%scissor_ene=Sigp%mbpt_sciss
     917                 :             : 
     918                 :             :  !FIXME this should be done in sigma_allocate
     919   [ -  +  +  -  :         824 :  ABI_MALLOC(sigma%minbnd, (sigma%nkptgw,Sigp%nsppol))
          -  +  +  -  +  
          -  +  -  -  +  
             -  +  -  + ]
     920   [ -  +  +  -  :         618 :  ABI_MALLOC(sigma%maxbnd, (sigma%nkptgw,Sigp%nsppol))
          -  +  +  -  -  
             +  -  +  -  
                      + ]
     921   [ +  -  +  -  :        2342 :  sigma%minbnd=Sigp%minbnd; sigma%maxbnd=Sigp%maxbnd
          -  +  -  -  -  
          -  -  -  +  +  
          +  +  +  -  +  
          -  -  +  -  -  
          -  -  -  -  +  
                +  +  + ]
     922   [ -  +  +  -  :         618 :  ABI_MALLOC(sigma%kptgw, (3,sigma%nkptgw))
          +  -  +  -  -  
             +  -  +  -  
                      + ]
     923   [ +  -  +  -  :        2988 :  sigma%kptgw=Sigp%kptgw
          -  +  -  -  -  
          -  -  -  +  +  
                   +  + ]
     924                 :             : 
     925                 :         206 :  sigma%b1gw     =Sigp%minbdgw ! min and Max GW band index over k and spin.
     926                 :         206 :  sigma%b2gw     =Sigp%maxbdgw ! Used to dimension arrays.
     927                 :         206 :  sigma%nbnds    =Sigp%nbnds
     928                 :         206 :  sigma%nkibz    =nkibz
     929                 :         206 :  sigma%nsppol   =Sigp%nsppol
     930                 :         206 :  sigma%nsig_ab  =Sigp%nsig_ab
     931                 :         206 :  sigma%nomega_r =Sigp%nomegasr  !FIXME change name
     932                 :         206 :  sigma%nomega_i =Sigp%nomegasi
     933                 :         206 :  sigma%nomega4sd=Sigp%nomegasrd
     934                 :         206 :  sigma%usepawu  =usepawu
     935                 :             : 
     936                 :             :  !================================================
     937                 :             :  ! === Allocate arrays in the sigma_t datatype ===
     938                 :             :  !================================================
     939                 :         206 :  b1gw=sigma%b1gw
     940                 :         206 :  b2gw=sigma%b2gw
     941                 :             : 
     942                 :             :  ! hhartree(b1,b2,k,s)= <b1,k,s|T+v_{loc}+v_{nl}+v_{H}|b2,k,s>
     943   [ -  +  +  -  :      106347 :  ABI_CALLOC(sigma%hhartree, (b1gw:b2gw,b1gw:b2gw,sigma%nkibz,sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
          -  +  +  +  +  
             +  +  +  +  
                      + ]
     944                 :             : 
     945                 :             :  ! QP amplitudes and energies.
     946   [ -  +  +  -  :       31831 :  ABI_CALLOC(sigma%en_qp_diago, (sigma%nbnds,sigma%nkibz,sigma%nsppol))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
          +  -  +  +  +  
             +  +  +  + ]
     947                 :             : 
     948                 :         206 :  sigma%needs_eigvec_qp = sigp%gwcalctyp >= 10
     949         [ +  + ]:         206 :  if (sigma%needs_eigvec_qp) then
     950   [ -  +  +  -  :       73727 :    ABI_CALLOC(sigma%eigvec_qp, (sigma%nbnds,sigma%nbnds,sigma%nkibz,sigma%nsppol))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
          -  +  +  +  +  
             +  +  +  +  
                      + ]
     951                 :             :  end if
     952                 :             : 
     953   [ -  +  +  -  :       12633 :  ABI_CALLOC(sigma%vxcme, (b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
          +  -  +  +  +  
             +  +  +  + ]
     954   [ -  +  +  -  :       12427 :  ABI_CALLOC(sigma%vUme, (b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          -  +  -  +  +  
             +  +  +  +  
                      + ]
     955   [ -  +  +  -  :       12427 :  ABI_CALLOC(sigma%sigxme, (b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          -  +  -  +  +  
             +  +  +  +  
                      + ]
     956   [ -  +  +  -  :       12427 :  ABI_CALLOC(sigma%sigxcnofme, (b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          -  +  -  +  +  
             +  +  +  +  
                      + ]
     957   [ -  +  +  -  :      106141 :  ABI_CALLOC(sigma%x_mat, (b1gw:b2gw, b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  -  
          +  -  +  +  +  
          +  +  +  +  +  
                      + ]
     958   [ -  +  +  -  :       61743 :  ABI_CALLOC(sigma%sigcme, (b1gw:b2gw, sigma%nkibz, sigma%nomega_r, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  +  
          +  +  -  -  +  
          +  -  +  -  +  
          +  -  +  -  +  
          -  +  +  +  +  
             +  +  +  +  
                      + ]
     959   [ -  +  +  -  :       61537 :  ABI_CALLOC(sigma%sigxcme, (b1gw:b2gw, sigma%nkibz, sigma%nomega_r, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  +  
          +  +  -  -  +  
          +  -  -  +  -  
          +  -  +  +  +  
          +  +  +  +  +  
                      + ]
     960   [ -  +  +  -  :       12633 :  ABI_CALLOC(sigma%sigcmee0, (b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
          +  -  +  +  +  
             +  +  +  + ]
     961   [ -  +  +  -  :       11313 :  ABI_CALLOC(sigma%ze0, (b1gw:b2gw, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
          +  -  +  +  +  
             +  +  +  + ]
     962   [ -  +  +  -  :       12427 :  ABI_CALLOC(sigma%dsigmee0, (b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          -  +  -  +  +  
             +  +  +  +  
                      + ]
     963   [ -  +  +  -  :       12427 :  ABI_CALLOC(sigma%sigmee, (b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          -  +  -  +  +  
             +  +  +  +  
                      + ]
     964   [ -  +  +  -  :       11107 :  ABI_CALLOC(sigma%degw, (b1gw:b2gw, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  -  +  
          -  +  -  +  +  
             +  +  +  +  
                      + ]
     965   [ -  +  +  -  :       31625 :  ABI_CALLOC(sigma%e0, (sigma%nbnds, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  -  +  
          -  +  -  +  +  
             +  +  +  +  
                      + ]
     966   [ -  +  +  -  :       31831 :  ABI_CALLOC(sigma%egw, (sigma%nbnds, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
          +  -  +  +  +  
             +  +  +  + ]
     967   [ -  +  +  -  :        2281 :  ABI_CALLOC(sigma%e0gap, (sigma%nkibz, sigma%nsppol))
          -  +  +  -  +  
          -  +  -  -  +  
          -  +  -  +  +  
                +  +  + ]
     968   [ -  +  +  -  :        2075 :  ABI_CALLOC(sigma%degwgap, (sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  -  +  -  +  
             +  +  +  + ]
     969   [ -  +  +  -  :        2075 :  ABI_CALLOC(sigma%egwgap, (sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  -  +  -  +  
             +  +  +  + ]
     970                 :             : 
     971                 :             :  ! These quantities are used to evaluate $\Sigma(E)$ around the KS\QP eigenvalue
     972   [ -  +  +  -  :       54905 :  ABI_CALLOC(sigma%omega4sd, (b1gw:b2gw, sigma%nkibz, sigma%nomega4sd, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
          -  +  +  +  +  
             +  +  +  +  
                      + ]
     973   [ -  +  +  -  :       60662 :  ABI_CALLOC(sigma%sigcme4sd, (b1gw:b2gw, sigma%nkibz, sigma%nomega4sd, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
          -  +  +  +  +  
             +  +  +  +  
                      + ]
     974   [ -  +  +  -  :       60456 :  ABI_CALLOC(sigma%sigxcme4sd, (b1gw:b2gw, sigma%nkibz, sigma%nomega4sd, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  -  
          +  -  +  +  +  
          +  +  +  +  +  
                      + ]
     975                 :             : 
     976                 :             :  ! Mesh along the real axis.
     977                 :             :  !TODO Find  better treatment
     978         [ +  + ]:         206 :  if (sigma%nomega_r > 0) then
     979   [ -  +  +  -  :          15 :    ABI_MALLOC(sigma%omega_r, (sigma%nomega_r))
          +  -  -  +  -  
                +  -  + ]
     980         [ +  + ]:         813 :    sigma%omega_r(:)=Sigp%omega_r(:)
     981                 :             :  end if
     982                 :             : 
     983                 :             :  ! Analytic Continuation
     984                 :             :  ! FIXME omegasi should not be in Sigp% here we should construct the mesh
     985         [ +  + ]:         206 :  if (mod10 == SIG_GW_AC) then
     986   [ -  +  +  -  :          27 :    ABI_MALLOC(sigma%omega_i, (sigma%nomega_i))
          +  -  +  -  -  
             +  -  +  -  
                      + ]
     987   [ +  -  -  +  :         228 :    sigma%omega_i = Sigp%omegasi
          -  -  -  -  +  
                      + ]
     988   [ -  +  +  -  :       11493 :    ABI_CALLOC(sigma%sigcmesi,  (b1gw:b2gw, sigma%nkibz, sigma%nomega_i, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
          -  +  +  +  +  
             +  +  +  +  
                      + ]
     989   [ -  +  +  -  :       11484 :    ABI_CALLOC(sigma%sigxcmesi, (b1gw:b2gw, sigma%nkibz, sigma%nomega_i, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  -  +  -  
          +  -  +  +  +  
          +  +  +  +  +  
                      + ]
     990                 :             :  end if
     991                 :             : 
     992                 :         206 : end subroutine sigma_init
     993                 :             : !!***
     994                 :             : 
     995                 :             : !----------------------------------------------------------------------
     996                 :             : 
     997                 :             : !!****f* m_sigma/sigma_free
     998                 :             : !! NAME
     999                 :             : !! sigma_free
    1000                 :             : !!
    1001                 :             : !! FUNCTION
    1002                 :             : !!  Deallocate all associated pointers defined in the sigma_t data type.
    1003                 :             : !!
    1004                 :             : !! SOURCE
    1005                 :             : 
    1006                 :         206 : subroutine sigma_free(sigma)
    1007                 :             : 
    1008                 :             : !Arguments ------------------------------------
    1009                 :             :  class(sigma_t),intent(inout) :: sigma
    1010                 :             : ! *************************************************************************
    1011                 :             : 
    1012                 :             :  ! integer
    1013         [ +  - ]:         206 :  ABI_SFREE(sigma%maxbnd)
    1014         [ +  - ]:         206 :  ABI_SFREE(sigma%minbnd)
    1015                 :             : 
    1016                 :             :  ! real
    1017         [ +  - ]:         206 :  ABI_SFREE(sigma%degwgap)
    1018         [ +  - ]:         206 :  ABI_SFREE(sigma%egwgap)
    1019         [ +  - ]:         206 :  ABI_SFREE(sigma%en_qp_diago)
    1020         [ +  - ]:         206 :  ABI_SFREE(sigma%e0)
    1021         [ +  - ]:         206 :  ABI_SFREE(sigma%e0gap)
    1022         [ +  + ]:         206 :  ABI_SFREE(sigma%omega_r)
    1023         [ +  - ]:         206 :  ABI_SFREE(sigma%kptgw)
    1024         [ +  - ]:         206 :  ABI_SFREE(sigma%sigxme)
    1025         [ +  - ]:         206 :  ABI_SFREE(sigma%sigxcnofme)
    1026         [ +  - ]:         206 :  ABI_SFREE(sigma%x_mat)
    1027         [ +  - ]:         206 :  ABI_SFREE(sigma%vxcme)
    1028         [ +  - ]:         206 :  ABI_SFREE(sigma%vUme)
    1029                 :             : 
    1030                 :             :  ! complex
    1031         [ +  - ]:         206 :  ABI_SFREE(sigma%degw)
    1032         [ +  - ]:         206 :  ABI_SFREE(sigma%dsigmee0)
    1033         [ +  - ]:         206 :  ABI_SFREE(sigma%egw)
    1034         [ +  + ]:         206 :  ABI_SFREE(sigma%eigvec_qp)
    1035         [ +  + ]:         206 :  ABI_SFREE(sigma%m_ks_to_qp)
    1036         [ +  - ]:         206 :  ABI_SFREE(sigma%hhartree)
    1037         [ +  - ]:         206 :  ABI_SFREE(sigma%sigcme)
    1038         [ +  - ]:         206 :  ABI_SFREE(sigma%sigmee)
    1039         [ +  - ]:         206 :  ABI_SFREE(sigma%sigcmee0)
    1040         [ +  + ]:         206 :  ABI_SFREE(sigma%sigcmesi)
    1041         [ +  - ]:         206 :  ABI_SFREE(sigma%sigcme4sd)
    1042         [ +  - ]:         206 :  ABI_SFREE(sigma%sigxcme)
    1043         [ +  + ]:         206 :  ABI_SFREE(sigma%sigxcmesi)
    1044         [ +  - ]:         206 :  ABI_SFREE(sigma%sigxcme4sd)
    1045         [ +  - ]:         206 :  ABI_SFREE(sigma%ze0)
    1046         [ +  + ]:         206 :  ABI_SFREE(sigma%omega_i)
    1047         [ +  - ]:         206 :  ABI_SFREE(sigma%omega4sd)
    1048                 :             : 
    1049                 :         206 : end subroutine sigma_free
    1050                 :             : !!***
    1051                 :             : 
    1052                 :             : !----------------------------------------------------------------------
    1053                 :             : 
    1054                 :             : !!****f* m_sigma/sigma_get_exene
    1055                 :             : !! NAME
    1056                 :             : !!  sigma_get_exene
    1057                 :             : !!
    1058                 :             : !! FUNCTION
    1059                 :             : !!  Compute exchange energy.
    1060                 :             : !!
    1061                 :             : !! INPUTS
    1062                 :             : !!  sigma<sigma_t>=Sigma results
    1063                 :             : !!  kmesh<kmesh_t>=BZ sampling.
    1064                 :             : !!  bands<band_t>=Bands with occupation factors
    1065                 :             : !!
    1066                 :             : !! SOURCE
    1067                 :             : 
    1068                 :          84 : real(dp) pure function sigma_get_exene(sigma, kmesh, bands) result(ex_energy)
    1069                 :             : 
    1070                 :             : !Arguments ------------------------------------
    1071                 :             :  class(sigma_t),intent(in) :: sigma
    1072                 :             :  type(kmesh_t),intent(in) :: kmesh
    1073                 :             :  type(ebands_t),intent(in) :: bands
    1074                 :             : 
    1075                 :             : !Local variables-------------------------------
    1076                 :             : !scalars
    1077                 :             :  integer :: ik,ib,spin
    1078                 :             :  real(dp) :: wtk,occ_bks
    1079                 :             : ! *************************************************************************
    1080                 :             : 
    1081                 :          84 :  ex_energy = zero
    1082                 :             : 
    1083         [ +  + ]:         170 :  do spin=1,sigma%nsppol
    1084         [ +  + ]:         638 :    do ik=1,sigma%nkibz
    1085                 :         468 :      wtk = kmesh%wt(ik)
    1086         [ +  + ]:        5224 :      do ib=sigma%b1gw,sigma%b2gw
    1087                 :        4670 :        occ_bks = bands%occ(ib,ik,spin)
    1088         [ +  + ]:        5138 :        if (sigma%nsig_ab == 1) then
    1089                 :        4628 :          ex_energy = ex_energy + half * occ_bks * wtk * sigma%sigxme(ib,ik,spin)
    1090                 :             :        else
    1091         [ +  + ]:         210 :          ex_energy = ex_energy + half * occ_bks * wtk * SUM(sigma%sigxme(ib,ik,:))
    1092                 :             :        end if
    1093                 :             :      end do
    1094                 :             :    end do
    1095                 :             :  end do
    1096                 :             : 
    1097                 :          84 : end function sigma_get_exene
    1098                 :             : !!***
    1099                 :             : 
    1100                 :             : !----------------------------------------------------------------------
    1101                 :             : 
    1102                 :             : !!****f* m_sigma/sigma_get_excene
    1103                 :             : !! NAME
    1104                 :             : !!  sigma_get_excene
    1105                 :             : !!
    1106                 :             : !! FUNCTION
    1107                 :             : !!  Compute exchange correlation energy using MBB (nat. orb. functional approx.).
    1108                 :             : !!
    1109                 :             : !! INPUTS
    1110                 :             : !!  sigma<sigma_t>=Sigma results
    1111                 :             : !!  kmesh<kmesh_t>=BZ sampling.
    1112                 :             : !!  bands<band_t>=Bands with occupation factors
    1113                 :             : !!
    1114                 :             : !! SOURCE
    1115                 :             : 
    1116                 :           4 : real(dp) pure function sigma_get_excene(sigma, kmesh, bands) result(exc_energy)
    1117                 :             : 
    1118                 :             : !Arguments ------------------------------------
    1119                 :             :  class(sigma_t),intent(in) :: sigma
    1120                 :             :  type(kmesh_t),intent(in) :: kmesh
    1121                 :             :  type(ebands_t),intent(in) :: bands
    1122                 :             : 
    1123                 :             : !Local variables-------------------------------
    1124                 :             : !scalars
    1125                 :             :  integer :: ik, ib, spin
    1126                 :             :  real(dp) :: wtk, occ_bks
    1127                 :             : ! *************************************************************************
    1128                 :             : 
    1129                 :           4 :  exc_energy = zero
    1130                 :             : 
    1131         [ +  + ]:           8 :  do spin=1,sigma%nsppol
    1132         [ +  + ]:          32 :    do ik=1,sigma%nkibz
    1133                 :          24 :      wtk = kmesh%wt(ik)
    1134         [ +  + ]:         220 :      do ib=sigma%b1gw,sigma%b2gw
    1135                 :         192 :        occ_bks = bands%occ(ib,ik,spin)
    1136         [ +  - ]:         216 :        if (sigma%nsig_ab==1) then
    1137         [ +  - ]:         192 :          if (sigma%nsppol==1) then
    1138                 :         192 :            exc_energy = exc_energy + sqrt( abs( half * occ_bks ) ) * wtk * sigma%sigxcnofme(ib,ik,spin)   ! 2*sqrt(occ_i), occ in [0,2] -> [0,1].
    1139                 :             :          else
    1140                 :           0 :            exc_energy = exc_energy + half * sqrt( abs( occ_bks ) ) * wtk * sigma%sigxcnofme(ib,ik,spin)   ! 2*sqrt(occ_i), occ in [0,1] -> [0,1].
    1141                 :             :          end if
    1142                 :             :        else
    1143         [ #  # ]:           0 :          exc_energy = exc_energy + half * sqrt( abs( occ_bks ) ) * wtk * SUM(sigma%sigxcnofme(ib,ik,:)) ! 2*sqrt(occ_i), occ in [0,1].
    1144                 :             :        end if
    1145                 :             :      end do
    1146                 :             :    end do
    1147                 :             :  end do
    1148                 :             : 
    1149                 :           4 : end function sigma_get_excene
    1150                 :             : !!***
    1151                 :             : 
    1152                 :             : !----------------------------------------------------------------------
    1153                 :             : 
    1154                 :             : !!****f* ABINIT/sigma_get_haene
    1155                 :             : !! NAME
    1156                 :             : !! sigma_get_haene
    1157                 :             : !!
    1158                 :             : !! FUNCTION
    1159                 :             : !! Compute the Hartree energy
    1160                 :             : !!
    1161                 :             : !! INPUTS
    1162                 :             : !! Kmesh <kmesh_t>=Structure describing the k-point sampling.
    1163                 :             : !! bands=<ebands_t>=Datatype gathering info on the QP energies (KS if one shot)
    1164                 :             : !!  eig(Sigp%nbnds,Kmesh%nibz,Wfd%nsppol)=KS or QP energies for k-points, bands and spin
    1165                 :             : !!  occ(Sigp%nbnds,Kmesh%nibz,Wfd%nsppol)=occupation numbers, for each k point in IBZ, each band and spin
    1166                 :             : !! Mels
    1167                 :             : !!  %vhartr=matrix elements of $v_H$.
    1168                 :             : !!
    1169                 :             : !! OUTPUT
    1170                 :             : !! Compute the Hartree energy on eh_energy
    1171                 :             : !!
    1172                 :             : !! SOURCE
    1173                 :             : 
    1174                 :           4 : real(dp) pure function sigma_get_haene(sigma, Mels, kmesh, bands) result(eh_energy)
    1175                 :             : 
    1176                 :             : !Arguments ------------------------------------
    1177                 :             : !scalars
    1178                 :             :  class(sigma_t),intent(in) :: sigma
    1179                 :             :  type(kmesh_t),intent(in) :: kmesh
    1180                 :             :  type(ebands_t),intent(in) :: bands
    1181                 :             :  type(melements_t),intent(in) :: Mels
    1182                 :             : 
    1183                 :             : !Local variables-------------------------------
    1184                 :             :  integer :: ik,ib,spin
    1185                 :             :  real(dp) :: wtk,occ_bks
    1186                 :             : ! *************************************************************************
    1187                 :             : 
    1188                 :           4 :  eh_energy = zero
    1189                 :             : 
    1190         [ +  + ]:           8 :  do spin=1,sigma%nsppol
    1191         [ +  + ]:          32 :    do ik=1,sigma%nkibz
    1192                 :          24 :      wtk = kmesh%wt(ik)
    1193         [ +  + ]:         220 :      do ib=sigma%b1gw,sigma%b2gw
    1194                 :         192 :        occ_bks = bands%occ(ib,ik,spin)
    1195         [ +  - ]:         216 :        if (sigma%nsig_ab == 1) then ! Only closed-shell restricted is programed
    1196                 :         192 :          eh_energy = eh_energy + occ_bks * wtk * Mels%vhartree(ib,ib,ik,spin)
    1197                 :             :        end if
    1198                 :             :      end do
    1199                 :             :    end do
    1200                 :             :  end do
    1201                 :             : 
    1202                 :           4 :  eh_energy = half * eh_energy
    1203                 :             : 
    1204                 :           4 : end function sigma_get_haene
    1205                 :             : !!***
    1206                 :             : 
    1207                 :             : !----------------------------------------------------------------------
    1208                 :             : 
    1209                 :             : !!****f* ABINIT/sigma_get_kiene
    1210                 :             : !! NAME
    1211                 :             : !! sigma_get_kiene
    1212                 :             : !!
    1213                 :             : !! FUNCTION
    1214                 :             : !! Compute the kinetic energy
    1215                 :             : !!
    1216                 :             : !! INPUTS
    1217                 :             : !! Kmesh <kmesh_t>=Structure describing the k-point sampling.
    1218                 :             : !! bands=<ebands_t>=Datatype gathering info on the QP energies (KS if one shot)
    1219                 :             : !!  eig(Sigp%nbnds,Kmesh%nibz,Wfd%nsppol)=KS or QP energies for k-points, bands and spin
    1220                 :             : !!  occ(Sigp%nbnds,Kmesh%nibz,Wfd%nsppol)=occupation numbers, for each k point in IBZ, each band and spin
    1221                 :             : !! Mels
    1222                 :             : !!  %kinetic=matrix elements of $T$.
    1223                 :             : !!
    1224                 :             : !! OUTPUT
    1225                 :             : !! Compute the kinetic energy on ek_energy
    1226                 :             : !!
    1227                 :             : !! SOURCE
    1228                 :             : 
    1229                 :           4 : real(dp) pure function sigma_get_kiene(sigma, Mels, kmesh, bands) result(ek_energy)
    1230                 :             : 
    1231                 :             : !Arguments ------------------------------------
    1232                 :             :  class(sigma_t),intent(in) :: sigma
    1233                 :             :  type(kmesh_t),intent(in) :: kmesh
    1234                 :             :  type(ebands_t),intent(in) :: bands
    1235                 :             :  type(melements_t),intent(in) :: Mels
    1236                 :             : 
    1237                 :             : !Local variables-------------------------------
    1238                 :             : !scalars
    1239                 :             :  integer :: ik, ib, spin
    1240                 :             :  real(dp) :: wtk, occ_bks
    1241                 :             : ! *************************************************************************
    1242                 :             : 
    1243                 :           4 :  ek_energy = zero
    1244                 :             : 
    1245         [ +  + ]:           8 :  do spin=1,sigma%nsppol
    1246         [ +  + ]:          32 :    do ik=1,sigma%nkibz
    1247                 :          24 :      wtk = kmesh%wt(ik)
    1248         [ +  + ]:         220 :      do ib=sigma%b1gw,sigma%b2gw
    1249                 :         192 :        occ_bks = bands%occ(ib,ik,spin)
    1250         [ +  - ]:         216 :        if (sigma%nsig_ab==1) then ! Only closed-shell restricted is programed
    1251                 :         192 :          ek_energy = ek_energy + occ_bks * wtk * Mels%kinetic(ib,ib,ik,spin)
    1252                 :             :        end if
    1253                 :             :      end do
    1254                 :             :    end do
    1255                 :             :  end do
    1256                 :             : 
    1257                 :           4 : end function sigma_get_kiene
    1258                 :             : !!***
    1259                 :             : 
    1260                 :             : !----------------------------------------------------------------------
    1261                 :             : 
    1262                 :             : !!****f* m_sigma/find_wpoles_for_cd
    1263                 :             : !! NAME
    1264                 :             : !!  find_wpoles_for_cd
    1265                 :             : !!
    1266                 :             : !! FUNCTION
    1267                 :             : !!  Find the max frequency needed to account for all the poles of the GW self-energy
    1268                 :             : !!  in the case of contour deformation technique.
    1269                 :             : !!
    1270                 :             : !! INPUTS
    1271                 :             : !!  Sigp=sigparams_t
    1272                 :             : !!
    1273                 :             : !! OUTPUT
    1274                 :             : !!  omega_max
    1275                 :             : !!
    1276                 :             : !! SOURCE
    1277                 :             : 
    1278                 :             : subroutine find_wpoles_for_cd(Sigp, sigma, Kmesh, ebands, omega_max)
    1279                 :             : 
    1280                 :             : !Arguments ------------------------------------
    1281                 :             : !scalars
    1282                 :             :  class(sigparams_t),intent(in) :: Sigp
    1283                 :             :  type(sigma_t),intent(in) :: sigma
    1284                 :             :  type(ebands_t),intent(in) :: ebands
    1285                 :             :  type(kmesh_t),intent(in) :: Kmesh
    1286                 :             :  real(dp),intent(out) :: omega_max
    1287                 :             : 
    1288                 :             : !Local variables-------------------------------
    1289                 :             : !scalars
    1290                 :             :  integer :: spin,ik_ibz,band_gr,bgw_start,bgw_stop,io,ioe0j
    1291                 :             :  integer :: ikgw,ikgw_ibz,ikgw_bz,band_gw,nomega_tot
    1292                 :             :  real(dp) :: e_green,e_screen,theta_mu_minus_e0i,e_qp,fact_sp
    1293                 :             :  !character(len=500) :: msg
    1294                 :             : !arrays
    1295                 :             :  real(dp),allocatable :: omegame0i(:)
    1296                 :             : ! *************************************************************************
    1297                 :             : 
    1298                 :             :  omega_max = smallest_real
    1299                 :             :  !
    1300                 :             :  ! === Normalization of theta_mu_minus_e0i ===
    1301                 :             :  ! * If nsppol==2, qp_occ $\in [0,1]$
    1302                 :             :  fact_sp=one
    1303                 :             :  if (ebands%nsppol==1) then
    1304                 :             :    fact_sp=half; if (ebands%nspinor==2) fact_sp=one
    1305                 :             :  end if
    1306                 :             :  !
    1307                 :             :  ! Total number of frequencies for sigma (Spectral function + mesh for the derivative).
    1308                 :             :  nomega_tot=sigma%nomega_r+sigma%nomega4sd
    1309                 :             :  ABI_MALLOC(omegame0i,(nomega_tot))
    1310                 :             : 
    1311                 :             :  ioe0j=sigma%nomega4sd/2+1
    1312                 :             :  !
    1313                 :             :  ! Loop over bands used to construct the Green function.
    1314                 :             :  do spin=1,ebands%nsppol
    1315                 :             :    do ik_ibz=1,ebands%nkpt
    1316                 :             :      do band_gr=1,ebands%nband(ik_ibz+(spin-1)*ebands%nkpt)
    1317                 :             :        e_green           = ebands%eig(band_gr,ik_ibz,spin)
    1318                 :             :        theta_mu_minus_e0i= ebands%occ(band_gr,ik_ibz,spin)*fact_sp
    1319                 :             :        !
    1320                 :             :        ! Loop over GW states.
    1321                 :             :        do ikgw=1,Sigp%nkptgw
    1322                 :             :          bgw_start=Sigp%minbnd(ikgw,spin)
    1323                 :             :          bgw_stop =Sigp%minbnd(ikgw,spin)
    1324                 :             :          ikgw_bz  =Sigp%kptgw2bz(ikgw_bz)
    1325                 :             :          ikgw_ibz =Kmesh%tab(ikgw_bz)
    1326                 :             : 
    1327                 :             :          do band_gw=bgw_start,bgw_stop
    1328                 :             :            e_qp      = ebands%eig(band_gw,ikgw_ibz,spin)
    1329                 :             :            !
    1330                 :             :            ! Get frequencies $\omega$-\epsilon_in$ to evaluate  $d\Sigma/dE$, note the spin
    1331                 :             :            ! subtract e_KS since we have stored e_KS+ Delta \omega in sigma%omega4sd, not required for AC
    1332                 :             :            if (sigma%nomega_r>0) omegame0i(1:sigma%nomega_r)=DBLE(Sigp%omega_r(1:sigma%nomega_r))-e_green
    1333                 :             :            do io=sigma%nomega_r+1,nomega_tot
    1334                 :             :              !omegame0i(io)=DBLE(sigma%omega4sd(band_gw,ikgw_ibz,io-sigma%nomega_r,spin)) - e_green
    1335                 :             :              !sigma%omega4sd(jb,ik_ibz,io,spin)=sigma%egw(jb,ik_ibz,spin)+Sigp%deltae*(io-ioe0j)
    1336                 :             :              omegame0i(io) = e_qp + Sigp%deltae*(io-ioe0j) - e_green
    1337                 :             :            end do
    1338                 :             : 
    1339                 :             :            do io=1,nomega_tot
    1340                 :             :              e_screen =  ABS(omegame0i(io))
    1341                 :             :              if (omegame0i(io)>tol12) then
    1342                 :             :                !ket(spadc+ig,ios)=ket(spadc+ig,ios)+ct*(one-theta_mu_minus_e0i)
    1343                 :             :                if ( (one-theta_mu_minus_e0i) > tol12 ) omega_max = MAX(omega_max, e_screen)
    1344                 :             :              end if
    1345                 :             :              if (omegame0i(io)<-tol12) then
    1346                 :             :                !ket(spadc+ig,ios)=ket(spadc+ig,ios)-ct*theta_mu_minus_e0i
    1347                 :             :                if ( theta_mu_minus_e0i > tol12) omega_max = MAX(omega_max, e_screen)
    1348                 :             :              end if
    1349                 :             :            end do
    1350                 :             : 
    1351                 :             :          end do
    1352                 :             :        end do
    1353                 :             :        !
    1354                 :             :      end do
    1355                 :             :    end do
    1356                 :             :  end do
    1357                 :             : 
    1358                 :             :  ABI_FREE(omegame0i)
    1359                 :             : 
    1360                 :             : end subroutine find_wpoles_for_cd
    1361                 :             : !!***
    1362                 :             : 
    1363                 :             : !----------------------------------------------------------------------
    1364                 :             : 
    1365                 :             : !!****f* m_sigma/sigma_ncwrite
    1366                 :             : !! NAME
    1367                 :             : !! sigma_ncwrite
    1368                 :             : !!
    1369                 :             : !! FUNCTION
    1370                 :             : !!  Save the data stored in the sigma_t data type on a NETCDF file.
    1371                 :             : !!
    1372                 :             : !! INPUTS
    1373                 :             : !!  filename
    1374                 :             : !!
    1375                 :             : !! OUTPUT
    1376                 :             : !!
    1377                 :             : !! SOURCE
    1378                 :             : 
    1379                 :         173 : integer function sigma_ncwrite(sigma, Sigp, epsm1, dtset, ncid) result (ncerr)
    1380                 :             : 
    1381                 :             : !Arguments ------------------------------------
    1382                 :             : !scalars
    1383                 :             :  class(sigma_t),target,intent(in) :: sigma
    1384                 :             :  class(sigparams_t),target,intent(in) :: Sigp
    1385                 :             :  integer,intent(in) :: ncid
    1386                 :             :  type(epsm1_t),target,intent(in) :: epsm1
    1387                 :             :  type(dataset_type),intent(in) :: dtset
    1388                 :             : 
    1389                 :             : !Local variables ---------------------------------------
    1390                 :             : !scalars
    1391                 :             :  integer :: nbgw,ndim_sig,b1gw,b2gw,cplex
    1392                 :             :  !character(len=500) :: msg
    1393                 :             : !arrays
    1394                 :         173 :  real(dp),allocatable :: rdata2(:,:),rdata4(:,:,:,:),rdata5(:,:,:,:,:)
    1395                 :             : ! *************************************************************************
    1396                 :             : 
    1397                 :             :  !@sigma_t
    1398                 :         173 :  cplex=2; b1gw=sigma%b1gw; b2gw=sigma%b2gw; nbgw=b2gw-b1gw+1
    1399                 :         173 :  ndim_sig=sigma%nsppol*sigma%nsig_ab
    1400                 :             : 
    1401                 :             :  ncerr = nctk_def_dims(ncid, [&
    1402                 :             :    nctkdim_t("cplex", cplex), nctkdim_t("b1gw", sigma%b1gw), nctkdim_t("b2gw", sigma%b2gw),&
    1403                 :             :    nctkdim_t("nbgw", nbgw), nctkdim_t("nkptgw", sigma%nkptgw), nctkdim_t("ndim_sig", ndim_sig), &
    1404                 :             :    nctkdim_t("nomega4sd", sigma%nomega4sd), nctkdim_t("nsig_ab", sigma%nsig_ab), &
    1405                 :             :    nctkdim_t("scr_nomega", epsm1%nomega) &
    1406         [ +  + ]:        1730 :  ], defmode=.True.)
    1407         [ -  + ]:         173 :  NCF_CHECK(ncerr)
    1408                 :             : 
    1409                 :             :  ! No. of real frequencies, might be zero.
    1410         [ +  + ]:         173 :  if (sigma%nomega_r > 0) then
    1411         [ -  + ]:           5 :    NCF_CHECK(nctk_def_dims(ncid, nctkdim_t("nomega_r", sigma%nomega_r)))
    1412                 :             :  end if
    1413                 :             : 
    1414                 :             :  ! No. of imaginary frequencies, might be zero.
    1415         [ +  + ]:         173 :  if (sigma%nomega_i > 0) then
    1416         [ -  + ]:           4 :    NCF_CHECK(nctk_def_dims(ncid, nctkdim_t("nomega_i", sigma%nomega_i)))
    1417                 :             :  end if
    1418                 :             : 
    1419                 :             :  ! =======================
    1420                 :             :  ! == Define variables ===
    1421                 :             :  ! =======================
    1422                 :             :  ncerr = nctk_def_iscalars(ncid, [character(len=nctk_slen) :: &
    1423                 :             :    'sigma_nband', 'scr_nband', 'gwcalctyp', 'usepawu', "nfreqre", "nfreqim", "nfreqim_conv", &
    1424         [ +  + ]:        1730 :    "gwcomp", "ppmodel"])
    1425         [ -  + ]:         173 :  NCF_CHECK(ncerr)
    1426                 :             : 
    1427                 :             :  ncerr = nctk_def_dpscalars(ncid, [character(len=nctk_slen) :: &
    1428         [ +  + ]:        1557 :    'ecutwfn', 'ecuteps', 'ecutsigx', 'omegasrdmax', 'deltae', 'omegasrmax', 'scissor_ene', "gwencomp"])
    1429         [ -  + ]:         173 :  NCF_CHECK(ncerr)
    1430                 :             : 
    1431                 :             :  ! TODO: Decrease size of file: Remove arrays whose size scale as mband ** 2
    1432                 :             :  ! especially those that are not commonly used e.g. hhartree.
    1433                 :             :  ncerr = nctk_def_arrays(ncid, [ &
    1434                 :             :    nctkarr_t("kptgw", "dp", "number_of_reduced_dimensions, nkptgw"),&
    1435                 :             :    nctkarr_t("minbnd", "i", "nkptgw, number_of_spins"),&
    1436                 :             :    nctkarr_t("maxbnd", "i", "nkptgw, number_of_spins"), &
    1437                 :             :    nctkarr_t('degwgap', "dp", 'number_of_kpoints, number_of_spins'),&
    1438                 :             :    nctkarr_t('egwgap', "dp", 'number_of_kpoints, number_of_spins'),&
    1439                 :             :    nctkarr_t('en_qp_diago', "dp",'max_number_of_states, number_of_kpoints, number_of_spins'),&
    1440                 :             :    nctkarr_t('e0', "dp", 'max_number_of_states, number_of_kpoints, number_of_spins'),&
    1441                 :             :    nctkarr_t('e0gap', "dp", 'number_of_kpoints, number_of_spins'),&
    1442                 :             :    nctkarr_t('sigxme', "dp", 'nbgw, number_of_kpoints, ndim_sig'),&
    1443                 :             :    nctkarr_t('vxcme', "dp", 'nbgw, number_of_kpoints, ndim_sig'),&
    1444                 :             :    nctkarr_t('degw', "dp", 'cplex, nbgw, number_of_kpoints, number_of_spins'),&
    1445                 :             :    nctkarr_t('dsigmee0', "dp", 'cplex, nbgw, number_of_kpoints, ndim_sig'),&
    1446                 :             :    nctkarr_t('egw', "dp",'cplex, max_number_of_states, number_of_kpoints, number_of_spins'),&
    1447                 :             :    nctkarr_t('hhartree', "dp",'cplex, nbgw, nbgw, number_of_kpoints, ndim_sig'),&
    1448                 :             :    nctkarr_t('sigmee', "dp", 'cplex, nbgw, number_of_kpoints, ndim_sig'),&
    1449                 :             :    nctkarr_t('sigcmee0', "dp",'cplex, nbgw, number_of_kpoints, ndim_sig'),&
    1450                 :             :    nctkarr_t('sigcme4sd', "dp",'cplex, nbgw, number_of_kpoints, nomega4sd, ndim_sig'),&
    1451                 :             :    nctkarr_t('sigxcme4sd', "dp", 'cplex, nbgw, number_of_kpoints, nomega4sd, ndim_sig'),&
    1452                 :             :    nctkarr_t('ze0',"dp", 'cplex, nbgw, number_of_kpoints, number_of_spins'),&
    1453                 :             :    nctkarr_t('omega4sd', "dp", 'cplex, nbgw, number_of_kpoints, nomega4sd, number_of_spins') &
    1454         [ +  + ]:        3633 :  ])
    1455         [ -  + ]:         173 :  NCF_CHECK(ncerr)
    1456                 :             : 
    1457         [ +  + ]:         173 :  if (sigma%needs_eigvec_qp) then
    1458                 :             :    ncerr = nctk_def_arrays(ncid, [ &
    1459         [ +  + ]:         110 :      nctkarr_t('eigvec_qp', "dp",'cplex, max_number_of_states, max_number_of_states, number_of_kpoints, number_of_spins')])
    1460         [ -  + ]:          55 :    NCF_CHECK(ncerr)
    1461                 :             :  end if
    1462                 :             : 
    1463         [ +  - ]:         173 :  if (sigma%usepawu == 0) then
    1464                 :         173 :    ncerr = nctk_def_arrays(ncid, nctkarr_t("vUme", "dp", 'nbgw, number_of_kpoints, ndim_sig'))
    1465         [ -  + ]:         173 :    NCF_CHECK(ncerr)
    1466                 :             :  end if
    1467                 :             : 
    1468         [ +  + ]:         173 :  if (epsm1%nomega > 0) then
    1469                 :         138 :    ncerr = nctk_def_arrays(ncid, nctkarr_t('scr_omega', "dp", 'cplex, scr_nomega'))
    1470         [ -  + ]:         138 :    NCF_CHECK(ncerr)
    1471   [ -  +  +  -  :         414 :    ABI_MALLOC(rdata2, (2, epsm1%nomega))
          +  -  +  -  -  
                +  -  + ]
    1472   [ +  -  -  +  :         138 :    rdata2 = c2r(epsm1%omega)
                   -  - ]
    1473   [ +  +  +  +  :        2019 :    NCF_CHECK(nf90_put_var(ncid, vid('scr_omega'), rdata2 * Ha_eV))
          -  +  -  -  -  
                      - ]
    1474         [ -  + ]:         138 :    ABI_FREE(rdata2)
    1475                 :             :   end if
    1476                 :             : 
    1477         [ +  + ]:         173 :  if (sigma%nomega_r > 0) then
    1478                 :             :    ncerr = nctk_def_arrays(ncid, [&
    1479                 :             :      nctkarr_t('omega_r', "dp", "nomega_r"),&
    1480                 :             :      nctkarr_t('sigcme', "dp", 'cplex, nbgw, number_of_kpoints, nomega_r, ndim_sig'),&
    1481         [ +  + ]:          20 :      nctkarr_t('sigxcme', "dp", 'cplex, nbgw, number_of_kpoints, nomega_r, ndim_sig')])
    1482         [ -  + ]:           5 :    NCF_CHECK(ncerr)
    1483                 :             :  end if
    1484                 :             : 
    1485         [ +  + ]:         173 :  if (sigma%nomega_i > 0) then
    1486                 :             :    ncerr = nctk_def_arrays(ncid, [&
    1487                 :             :      nctkarr_t('sigxcmesi', "dp", 'cplex, nbgw, number_of_kpoints, nomega_i, ndim_sig'),&
    1488                 :             :      nctkarr_t('sigcmesi', "dp",'cplex, nbgw, number_of_kpoints, nomega_i, ndim_sig'),&
    1489         [ +  + ]:          16 :      nctkarr_t('omega_i', "dp", 'cplex, nomega_i')])
    1490         [ -  + ]:           4 :    NCF_CHECK(ncerr)
    1491                 :             :  end if
    1492                 :             : 
    1493         [ +  + ]:         173 :  if (allocated(sigma%m_ks_to_qp)) then
    1494                 :             :    ncerr = nctk_def_arrays(ncid, [nctkarr_t('m_ks_to_qp', "dp", &
    1495         [ +  + ]:         110 :        "cplex, max_number_of_states, max_number_of_states, number_of_kpoints, number_of_spins")])
    1496         [ -  + ]:          55 :    NCF_CHECK(ncerr)
    1497                 :             :  end if
    1498                 :             : 
    1499                 :             :  ! =====================
    1500                 :             :  ! === Start writing ===
    1501                 :             :  ! =====================
    1502         [ -  + ]:         173 :  NCF_CHECK(nctk_set_datamode(ncid))
    1503         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('ecutwfn'), Sigp%ecutwfn))
    1504         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('ecuteps'), Sigp%ecuteps))
    1505         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('ecutsigx'), Sigp%ecutsigx))
    1506         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('sigma_nband'), Sigp%nbnds))
    1507         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('scr_nband'), epsm1%Hscr%nbnds_used))
    1508         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('gwcalctyp'), sigma%gwcalctyp))
    1509         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('usepawu'), sigma%usepawu))
    1510         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('nfreqre'), epsm1%nomega_r))
    1511         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('nfreqim'), epsm1%nomega_i))
    1512         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('nfreqim_conv'), epsm1%nomega_i_conv))
    1513                 :             :  ! Input variables (values given in the input file, not the quantities derived from them in the code).
    1514         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('gwcomp'), dtset%gwcomp))
    1515         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('ppmodel'), dtset%ppmodel))
    1516         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('gwencomp'), dtset%gwencomp))
    1517         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('kptgw'), sigma%kptgw))
    1518         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('minbnd'), sigma%minbnd))
    1519         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('maxbnd'),sigma%maxbnd))
    1520         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('omegasrdmax'), sigma%maxomega4sd * Ha_eV))
    1521         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('deltae'), sigma%deltae * Ha_eV))
    1522         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('omegasrmax'), sigma%maxomega_r * Ha_eV))
    1523         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('scissor_ene'), sigma%scissor_ene * Ha_eV))
    1524   [ +  +  +  +  :        1363 :  NCF_CHECK(nf90_put_var(ncid, vid('degwgap'), sigma%degwgap * Ha_eV))
          -  +  -  -  -  
                      - ]
    1525   [ +  +  +  +  :        1363 :  NCF_CHECK(nf90_put_var(ncid, vid('egwgap'), sigma%egwgap * Ha_eV))
          -  +  -  -  -  
                      - ]
    1526   [ +  +  +  +  :       26099 :  NCF_CHECK(nf90_put_var(ncid, vid('en_qp_diago'), sigma%en_qp_diago * Ha_eV))
          +  +  -  +  -  
             -  -  -  -  
                      - ]
    1527   [ +  +  +  +  :       26099 :  NCF_CHECK(nf90_put_var(ncid, vid('e0'), sigma%e0 * Ha_eV))
          +  +  -  +  -  
             -  -  -  -  
                      - ]
    1528   [ +  +  +  +  :        1363 :  NCF_CHECK(nf90_put_var(ncid, vid('e0gap'), sigma%e0gap * Ha_eV))
          -  +  -  -  -  
                      - ]
    1529                 :             : 
    1530         [ +  + ]:         173 :  if (sigma%nomega_r > 0) then
    1531   [ +  +  -  +  :         813 :    NCF_CHECK(nf90_put_var(ncid, vid('omega_r'), sigma%omega_r * Ha_eV))
                   -  - ]
    1532                 :             :  end if
    1533                 :             : 
    1534   [ +  +  +  +  :        9827 :  NCF_CHECK(nf90_put_var(ncid, vid('sigxme'), sigma%sigxme * Ha_eV))
          +  +  -  +  -  
             -  -  -  -  
                      - ]
    1535   [ +  +  +  +  :        9827 :  NCF_CHECK(nf90_put_var(ncid, vid('vxcme'), sigma%vxcme * Ha_eV))
          +  +  -  +  -  
             -  -  -  -  
                      - ]
    1536   [ +  +  +  +  :        9827 :  NCF_CHECK(nf90_put_var(ncid, vid('vUme'), sigma%vUme * Ha_eV))
          +  +  -  +  -  
             -  -  -  -  
                      - ]
    1537                 :             : 
    1538                 :             :  ! Have to transfer complex arrays
    1539   [ -  +  +  -  :         865 :  ABI_MALLOC(rdata4,(cplex, b1gw:b2gw, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
                +  -  + ]
    1540   [ +  -  +  -  :         173 :  rdata4=c2r(sigma%degw)
          +  -  -  +  -  
                      - ]
    1541   [ +  +  +  +  :       22795 :  NCF_CHECK(nf90_put_var(ncid, vid('degw'), rdata4 * Ha_eV))
          +  +  +  +  -  
          +  -  -  -  -  
             -  -  -  - ]
    1542         [ -  + ]:         173 :  ABI_FREE(rdata4)
    1543                 :             : 
    1544   [ -  +  +  -  :         865 :  ABI_MALLOC(rdata4,(cplex, b1gw:b2gw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
                      + ]
    1545   [ +  -  +  -  :         173 :  rdata4 = c2r(sigma%dsigmee0)
          +  -  -  +  -  
                      - ]
    1546         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('dsigmee0'), rdata4))
    1547         [ -  + ]:         173 :  ABI_FREE(rdata4)
    1548                 :             : 
    1549   [ -  +  +  -  :         865 :  ABI_MALLOC(rdata4, (cplex, sigma%nbnds, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
                      + ]
    1550   [ +  -  +  -  :         173 :  rdata4 = c2r(sigma%egw)
          +  -  -  +  -  
                      - ]
    1551   [ +  +  +  +  :       75571 :  NCF_CHECK(nf90_put_var(ncid, vid('egw'), rdata4 *Ha_eV))
          +  +  +  +  -  
          +  -  -  -  -  
             -  -  -  - ]
    1552         [ -  + ]:         173 :  ABI_FREE(rdata4)
    1553                 :             : 
    1554         [ +  + ]:         173 :  if (sigma%needs_eigvec_qp) then
    1555   [ -  +  +  -  :         330 :    ABI_MALLOC(rdata5, (cplex, sigma%nbnds, sigma%nbnds, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
                   -  + ]
    1556   [ +  -  +  -  :          55 :    rdata5 = c2r(sigma%eigvec_qp)
          +  -  +  -  -  
                +  -  - ]
    1557         [ -  + ]:          55 :    NCF_CHECK(nf90_put_var(ncid, vid('eigvec_qp'), rdata5))
    1558         [ -  + ]:          55 :    ABI_FREE(rdata5)
    1559                 :             :  end if
    1560                 :             : 
    1561   [ -  +  +  -  :        1038 :  ABI_MALLOC(rdata5,(cplex, nbgw, nbgw, sigma%nkibz, sigma%nsppol * sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
                   -  + ]
    1562   [ +  -  +  -  :         173 :  rdata5 = c2r(sigma%hhartree)
          +  -  +  -  -  
                +  -  - ]
    1563   [ +  +  +  +  :      253229 :  NCF_CHECK(nf90_put_var(ncid, vid('hhartree'), rdata5 * Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1564         [ -  + ]:         173 :  ABI_FREE(rdata5)
    1565                 :             : 
    1566         [ +  + ]:         173 :  if (sigma%nomega_r > 0) then
    1567   [ -  +  +  -  :          30 :    ABI_MALLOC(rdata5,(cplex, nbgw, sigma%nkibz, sigma%nomega_r, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
             -  -  +  -  
                      + ]
    1568   [ +  -  +  -  :           5 :    rdata5 = c2r(sigma%sigcme)
          +  -  +  -  -  
                +  -  - ]
    1569   [ +  +  +  +  :      157566 :    NCF_CHECK(nf90_put_var(ncid, vid('sigcme'), rdata5 * Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1570         [ -  + ]:           5 :    ABI_FREE(rdata5)
    1571                 :             :  end if
    1572                 :             : 
    1573   [ -  +  +  -  :         865 :  ABI_MALLOC(rdata4, (cplex, nbgw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
                      + ]
    1574   [ +  -  +  -  :         173 :  rdata4 = c2r(sigma%sigmee)
          +  -  -  +  -  
                      - ]
    1575   [ +  +  +  +  :       26527 :  NCF_CHECK(nf90_put_var(ncid, vid('sigmee'), rdata4 * Ha_eV))
          +  +  +  +  -  
          +  -  -  -  -  
             -  -  -  - ]
    1576         [ -  + ]:         173 :  ABI_FREE(rdata4)
    1577                 :             : 
    1578   [ -  +  +  -  :         865 :  ABI_MALLOC(rdata4, (cplex, nbgw, sigma%nkibz, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
                      + ]
    1579   [ +  -  +  -  :         173 :  rdata4 = c2r(sigma%sigcmee0)
          +  -  -  +  -  
                      - ]
    1580   [ +  +  +  +  :       26527 :  NCF_CHECK(nf90_put_var(ncid, vid('sigcmee0'), rdata4 * Ha_eV))
          +  +  +  +  -  
          +  -  -  -  -  
             -  -  -  - ]
    1581         [ -  + ]:         173 :  ABI_FREE(rdata4)
    1582                 :             : 
    1583         [ +  + ]:         173 :  if (sigma%nomega_i > 0) then
    1584   [ -  +  +  -  :          24 :   ABI_MALLOC(rdata5, (cplex, nbgw, sigma%nkibz, sigma%nomega_i, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
                   -  + ]
    1585   [ +  -  +  -  :           4 :   rdata5 = c2r(sigma%sigcmesi)
          +  -  +  -  -  
                +  -  - ]
    1586   [ +  +  +  +  :        7558 :   NCF_CHECK(nf90_put_var(ncid, vid('sigcmesi'), rdata5*Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1587         [ -  + ]:           4 :   ABI_FREE(rdata5)
    1588                 :             :  end if
    1589                 :             : 
    1590   [ -  +  +  -  :        1038 :  ABI_MALLOC(rdata5, (cplex, nbgw, sigma%nkibz, sigma%nomega4sd, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
                   -  + ]
    1591   [ +  -  +  -  :         173 :  rdata5 = c2r(sigma%sigcme4sd)
          +  -  +  -  -  
                +  -  - ]
    1592   [ +  +  +  +  :      126501 :  NCF_CHECK(nf90_put_var(ncid, vid('sigcme4sd'), rdata5 * Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1593         [ -  + ]:         173 :  ABI_FREE(rdata5)
    1594                 :             : 
    1595         [ +  + ]:         173 :  if (sigma%nomega_r > 0) then
    1596   [ -  +  +  -  :          30 :    ABI_MALLOC(rdata5,(cplex, nbgw, sigma%nkibz, sigma%nomega_r, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
             -  -  +  -  
                      + ]
    1597   [ +  -  +  -  :           5 :    rdata5 = c2r(sigma%sigxcme)
          +  -  +  -  -  
                +  -  - ]
    1598   [ +  +  +  +  :      157566 :    NCF_CHECK(nf90_put_var(ncid, vid('sigxcme'), rdata5 * Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1599         [ -  + ]:           5 :    ABI_FREE(rdata5)
    1600                 :             :  end if
    1601                 :             : 
    1602         [ +  + ]:         173 :  if (sigma%nomega_i > 0) then
    1603   [ -  +  +  -  :          24 :    ABI_MALLOC(rdata5,(cplex, nbgw, sigma%nkibz, sigma%nomega_i, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
                   -  + ]
    1604   [ +  -  +  -  :           4 :    rdata5 = c2r(sigma%sigxcmesi)
          +  -  +  -  -  
                +  -  - ]
    1605   [ +  +  +  +  :        7558 :    NCF_CHECK(nf90_put_var(ncid, vid('sigxcmesi'), rdata5 * Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1606         [ -  + ]:           4 :    ABI_FREE(rdata5)
    1607                 :             :  end if
    1608                 :             : 
    1609         [ +  + ]:         173 :  if (allocated(sigma%m_ks_to_qp)) then
    1610   [ -  +  +  -  :         330 :    ABI_MALLOC(rdata5,(cplex, sigma%nbnds, sigma%nbnds, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
                   -  + ]
    1611   [ +  -  +  -  :          55 :    rdata5 = c2r(sigma%m_ks_to_qp)
          +  -  +  -  -  
                +  -  - ]
    1612         [ -  + ]:          55 :    NCF_CHECK(nf90_put_var(ncid, vid('m_ks_to_qp'), rdata5))
    1613         [ -  + ]:          55 :    ABI_FREE(rdata5)
    1614                 :             :  end if
    1615                 :             : 
    1616   [ -  +  +  -  :        1038 :  ABI_MALLOC(rdata5, (cplex, nbgw, sigma%nkibz, sigma%nomega4sd, sigma%nsppol*sigma%nsig_ab))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
          -  -  +  -  +  
                   -  + ]
    1617   [ +  -  +  -  :         173 :  rdata5 = c2r(sigma%sigxcme4sd)
          +  -  +  -  -  
                +  -  - ]
    1618   [ +  +  +  +  :      126501 :  NCF_CHECK(nf90_put_var(ncid, vid('sigxcme4sd'), rdata5 * Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1619         [ -  + ]:         173 :  ABI_FREE(rdata5)
    1620                 :             : 
    1621   [ -  +  +  -  :         865 :  ABI_MALLOC(rdata4, (cplex, nbgw, sigma%nkibz, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  +  -  
          +  -  -  +  -  
                +  -  + ]
    1622   [ +  -  +  -  :         173 :  rdata4 = c2r(sigma%ze0)
          +  -  -  +  -  
                      - ]
    1623         [ -  + ]:         173 :  NCF_CHECK(nf90_put_var(ncid, vid('ze0'), rdata4))
    1624         [ -  + ]:         173 :  ABI_FREE(rdata4)
    1625                 :             : 
    1626         [ +  + ]:         173 :  if (sigma%nomega_i > 0) then
    1627   [ -  +  +  -  :          12 :    ABI_MALLOC(rdata2, (cplex, sigma%nomega_i))
          +  -  -  +  -  
                +  -  + ]
    1628   [ +  -  -  +  :           4 :    rdata2 = c2r(sigma%omega_i)
                   -  - ]
    1629   [ +  +  +  +  :         154 :    NCF_CHECK(nf90_put_var(ncid, vid('omega_i'), rdata2 * Ha_eV))
          -  +  -  -  -  
                      - ]
    1630         [ -  + ]:           4 :    ABI_FREE(rdata2)
    1631                 :             :  end if
    1632                 :             : 
    1633   [ -  +  +  -  :        1038 :  ABI_MALLOC(rdata5, (cplex, nbgw, sigma%nkibz, sigma%nomega4sd, sigma%nsppol))
          -  +  +  -  -  
          +  +  -  -  +  
          +  -  +  -  +  
             -  -  +  -  
                      + ]
    1634   [ +  -  +  -  :         173 :  rdata5 = c2r(sigma%omega4sd)
          +  -  +  -  -  
                +  -  - ]
    1635   [ +  +  +  +  :      110268 :  NCF_CHECK(nf90_put_var(ncid, vid('omega4sd'), rdata5 * Ha_eV))
          +  +  +  +  +  
          +  -  +  -  -  
          -  -  -  -  -  
                -  -  - ]
    1636   [ -  +  -  + ]:         173 :  ABI_FREE(rdata5)
    1637                 :             : 
    1638                 :             : 
    1639                 :             : contains
    1640                 :        6849 :  integer function vid(vname)
    1641                 :             :    character(len=*),intent(in) :: vname
    1642   [ +  -  +  -  :        6849 :    vid = nctk_idname(ncid, vname)
          -  -  +  -  -  
          -  +  -  -  -  
          +  -  -  -  +  
          -  -  -  +  -  
          -  -  +  -  -  
          -  +  -  -  -  
          +  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1643                 :             :  end function vid
    1644                 :             : 
    1645                 :             : end function sigma_ncwrite
    1646                 :             : !!***
    1647                 :             : 
    1648                 :             : !----------------------------------------------------------------------
    1649                 :             : 
    1650                 :             : !!****f* m_sigma/sigma_distribute_bks
    1651                 :             : !! NAME
    1652                 :             : !!  sigma_distribute_bks
    1653                 :             : !!
    1654                 :             : !! FUNCTION
    1655                 :             : !!  Distribute the loop over (b,k,s) used to calculate the self-energy matrix elements
    1656                 :             : !!  taking into account the MPI distribution of the wavefunctions and the use of
    1657                 :             : !!  symmetries to reduce the BZ sum to an appropriate irreducible wedge.
    1658                 :             : !!
    1659                 :             : !! INPUTS
    1660                 :             : !! nsppol
    1661                 :             : !! can_symmetrize(nsppol)=.TRUE if symmetries can be used to reduce the number of k-points to be summed.
    1662                 :             : !! Kmesh<kmesh_t>
    1663                 :             : !! Qmesh<kmesh_t>
    1664                 :             : !! Ltg_kgw<littlegroup_t>
    1665                 :             : !! Wfd(wfdgw_t)
    1666                 :             : !! mg0(3)
    1667                 :             : !! kptgw(3)
    1668                 :             : !! [bks_mask(Wfd%mband,Kmesh%nbz,nsppol)]
    1669                 :             : !! [got(Wfd%nproc)]=The number of tasks already assigned to the nodes.
    1670                 :             : !! [global]=If true, an MPI global communication is performed such that each node will have the same table. Useful
    1671                 :             : !!   if for implementing algorithms in which each node needs to know the global distribution of the tasks, not only
    1672                 :             : !!   the task it has to complete. Defaults to .FALSE.
    1673                 :             : !!
    1674                 :             : !! OUTPUT
    1675                 :             : !!  my_nbks
    1676                 :             : !!  proc_distrb(Wfd%mband,Kmesh%nbz,nsppol)
    1677                 :             : !!
    1678                 :             : !! SIDE EFFECTS
    1679                 :             : !!  Wfd%bks_tab
    1680                 :             : !!
    1681                 :             : !! SOURCE
    1682                 :             : 
    1683                 :        1135 : subroutine sigma_distribute_bks(Wfd,Kmesh,Ltg_kgw,Qmesh,nsppol,can_symmetrize,kptgw,mg0,my_nbks,proc_distrb,got,bks_mask,global)
    1684                 :             : 
    1685                 :             : !Arguments ------------------------------------
    1686                 :             : !scalars
    1687                 :             :  class(wfdgw_t),intent(inout) :: Wfd
    1688                 :             :  integer,intent(in) :: nsppol
    1689                 :             :  integer,intent(out) :: my_nbks
    1690                 :             :  logical,optional,intent(in) :: global
    1691                 :             :  type(kmesh_t),intent(in) :: Kmesh,Qmesh
    1692                 :             :  type(littlegroup_t),intent(in) :: Ltg_kgw
    1693                 :             : !arrays
    1694                 :             :  integer,intent(in) :: mg0(3)
    1695                 :             :  integer,optional,intent(inout) :: got(Wfd%nproc)
    1696                 :             :  integer,intent(out) :: proc_distrb(Wfd%mband,Kmesh%nbz,nsppol)
    1697                 :             :  real(dp),intent(in) :: kptgw(3)
    1698                 :             :  logical,intent(in) :: can_symmetrize(Wfd%nsppol)
    1699                 :             :  logical,optional,intent(in) :: bks_mask(Wfd%mband,Kmesh%nbz,nsppol)
    1700                 :             : 
    1701                 :             : !Local variables-------------------------------
    1702                 :             : !scalars
    1703                 :             :  integer :: ierr,ik_bz,ik_ibz,spin,iq_bz,my_nband
    1704                 :             :  !character(len=500) :: msg
    1705                 :             : !arrays
    1706                 :             :  integer :: g0(3)
    1707                 :             :  real(dp) :: kgwmk(3)
    1708                 :        2260 :  integer :: get_more(Wfd%nproc),my_band_list(Wfd%mband)
    1709                 :        2260 :  logical :: bmask(Wfd%mband)
    1710                 :             : !************************************************************************
    1711                 :             : 
    1712                 :        1130 :  call wfd%update_bkstab()
    1713                 :             : 
    1714   [ +  +  +  +  :        4366 :  get_more=0; if (PRESENT(got)) get_more=got
                   +  + ]
    1715                 :             : 
    1716                 :             :  ! Different distribution of tasks depending whether symmetries can be used or not.
    1717   [ +  +  +  +  :     1011994 :  proc_distrb= xmpi_undefined_rank
                   +  + ]
    1718                 :             : 
    1719         [ +  + ]:        2274 :  do spin=1,Wfd%nsppol
    1720                 :             : 
    1721         [ +  + ]:        2274 :    if (can_symmetrize(spin)) then
    1722         [ +  + ]:       14970 :      do ik_bz=1,Kmesh%nbz
    1723                 :       14736 :        ik_ibz = Kmesh%tab(ik_bz)
    1724         [ +  + ]:       58944 :        kgwmk = kptgw-Kmesh%bz(:,ik_bz) ! kptgw must be inside the BZ
    1725                 :       14736 :        call findqg0(iq_bz,g0,kgwmk,Qmesh%nbz,Qmesh%bz,mG0) ! <- (mg0=mG0) Identify q_bz and G0 where q_bz+G0=k_gw-k_bz
    1726         [ +  + ]:       14970 :        if (Ltg_kgw%ibzq(iq_bz)==1) then
    1727   [ +  +  +  + ]:      174044 :          bmask=.FALSE.; bmask(1:Wfd%nband(ik_ibz,spin))=.TRUE.
    1728   [ +  +  +  + ]:       45992 :          if (PRESENT(bks_mask)) bmask = bks_mask(:,ik_bz,spin)
    1729                 :        2740 :          call wfd%distribute_bands(ik_ibz,spin,my_nband,my_band_list,got=get_more,bmask=bmask)
    1730   [ +  +  +  + ]:       47939 :          if (my_nband>0) proc_distrb(my_band_list(1:my_nband),ik_bz,spin)=Wfd%my_rank
    1731                 :             :        end if
    1732                 :             :      end do
    1733                 :             : 
    1734                 :             :    else
    1735                 :             :      ! No symmetries for this spin. Divide the full BZ among procs.
    1736         [ +  + ]:       46018 :      do ik_bz=1,Kmesh%nbz
    1737                 :       45108 :        ik_ibz = Kmesh%tab(ik_bz)
    1738   [ +  +  +  + ]:     1168748 :        bmask=.FALSE.; bmask(1:Wfd%nband(ik_ibz,spin))=.TRUE.
    1739   [ +  +  +  + ]:      371756 :        if (PRESENT(bks_mask)) bmask = bks_mask(:,ik_bz,spin)
    1740                 :       45108 :        call wfd%distribute_bands(ik_ibz,spin,my_nband,my_band_list,got=get_more,bmask=bmask)
    1741   [ +  +  +  + ]:      301164 :        if (my_nband>0) proc_distrb(my_band_list(1:my_nband),ik_bz,spin)=Wfd%my_rank
    1742                 :             :      end do
    1743                 :             :    end if
    1744                 :             :  end do ! spin
    1745                 :             : 
    1746         [ +  + ]:        1130 :  if (PRESENT(global)) then
    1747         [ +  - ]:         465 :    if (global) then ! Each node will have the same table so that it will know how the tasks are distributed.
    1748   [ +  +  +  +  :      453892 :      proc_distrb = proc_distrb + 1
                   +  + ]
    1749   [ +  +  +  +  :      453892 :      where (proc_distrb == xmpi_undefined_rank + 1)
             +  +  +  + ]
    1750                 :             :        proc_distrb = 0
    1751                 :             :      end where
    1752                 :         465 :      call xmpi_sum(proc_distrb,Wfd%comm,ierr)
    1753   [ +  +  +  +  :     1361676 :      where (proc_distrb == 0)
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
    1754                 :             :        proc_distrb = xmpi_undefined_rank
    1755                 :             :      elsewhere
    1756                 :             :        proc_distrb = proc_distrb - 1
    1757                 :             :      end where
    1758                 :             :      !where (proc_distrb /= xmpi_undefined_rank)
    1759                 :             :      !  ltest = (ANY(proc_distrb == (/(ii,ii=0,Wfd%nproc-1)/)))
    1760                 :             :      !end where
    1761                 :             :      !if (.not.ltest) then
    1762                 :             :      !  write(std_out,*)proc_distrb
    1763                 :             :      !  ABI_BUG("Bug in the generation of proc_distrb table")
    1764                 :             :      !end if
    1765                 :             :    end if
    1766                 :             :  end if
    1767                 :             : 
    1768   [ +  +  +  +  :     1011994 :  my_nbks = COUNT(proc_distrb==Wfd%my_rank)
             +  +  +  + ]
    1769   [ +  +  +  + ]:        2452 :  if (PRESENT(got)) got=get_more
    1770                 :             : 
    1771   [ +  +  +  + ]:        1595 : end subroutine sigma_distribute_bks
    1772                 :             : !!***
    1773                 :             : 
    1774                 :             : !----------------------------------------------------------------------
    1775                 :             : 
    1776   [ #  #  #  #  :           0 : end module m_sigma
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1777                 :             : !!***
        

Generated by: LCOV version 2.3-1