subroutine wann_write_r2mn()
!========================================!
! !
! Write seedname.r2mn file !
! !
!========================================!
use w90_constants, only: dp
use w90_io, only: seedname, io_file_unit, io_error
use w90_parameters, only: num_kpts, num_wann, nntot, wb, &
m_matrix
implicit none
integer :: r2mnunit, nw1, nw2, nkp, nn
real(kind=dp) :: r2ave_mn, delta
! note that here I use formulas analogue to Eq. 23, and not to the
! shift-invariant Eq. 32 .
r2mnunit = io_file_unit()
open (r2mnunit, file=trim(seedname)//'.r2mn', form='formatted', err=158)
do nw1 = 1, num_wann
do nw2 = 1, num_wann
r2ave_mn = 0.0_dp
delta = 0.0_dp
if (nw1 .eq. nw2) delta = 1.0_dp
do nkp = 1, num_kpts
do nn = 1, nntot
r2ave_mn = r2ave_mn + wb(nn)* &
! [GP-begin, Apr13, 2012: corrected sign inside "real"]
(2.0_dp*delta - real(m_matrix(nw1, nw2, nn, nkp) + &
conjg(m_matrix(nw2, nw1, nn, nkp)), kind=dp))
! [GP-end]
enddo
enddo
r2ave_mn = r2ave_mn/real(num_kpts, dp)
write (r2mnunit, '(2i6,f20.12)') nw1, nw2, r2ave_mn
enddo
enddo
close (r2mnunit)
return
158 call io_error('Error opening file '//trim(seedname)//'.r2mn in wann_write_r2mn')
end subroutine wann_write_r2mn