LCOV - code coverage report
Current view: top level - src/71_wannier - m_wann_netcdf.F90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 66.7 % 81 54
Test Date: 2026-09-21 22:40:37 Functions: 57.1 % 7 4

            Line data    Source code
       1              : !!****m*ABINIT/m_wann_netcdf
       2              : !! NAME
       3              : !!  m_wann_math
       4              : !!
       5              : !! FUNCTION
       6              : !! Writting Wannier function information to netcdf file.
       7              : !!
       8              : !! COPYRIGHT
       9              : !!  Copyright (C) 2005-2026 ABINIT group (hexu)
      10              : !!  This file is distributed under the terms of the
      11              : !!  GNU General Public License, see ~abinit/COPYING
      12              : !!  or http://www.gnu.org/copyleft/gpl.txt .
      13              : !!
      14              : !! SOURCE
      15              : 
      16              : #if defined HAVE_CONFIG_H
      17              : #include "config.h"
      18              : #endif
      19              : 
      20              : #include "abi_common.h"
      21              : 
      22              : module m_wann_netcdf
      23              :   use defs_basis
      24              :   use m_abicore
      25              :   use m_errors
      26              :   use m_nctk
      27              :   use netcdf
      28              : 
      29              :   implicit none
      30              :   private
      31              : 
      32              : !---------------------------------------------------------------------
      33              : ! wrapper for netcdf file for wannier function
      34              : ! This is different for the wannier90 netcdf file in abinit.
      35              : ! It is used for the lattice Wannier function.
      36              : ! TODO: make it more similar to the electron Wannier nc file?
      37              : !---------------------------------------------------------------------
      38              :   type, public:: IOWannNC
      39              :      ! id of netcdf file
      40              :      integer:: ncid
      41              :      ! d_label is the dimension id in netcdf file
      42              :      integer:: d_nR, d_ndim, d_nwann, d_nbasis
      43              :      integer:: d_nkpt, d_nband
      44              :      integer:: d_three, d_natom
      45              :      ! i_label is the variable id in netcdf file
      46              :      integer:: i_Rlist, i_wannR_real, i_wannR_imag, i_HwannR_real, i_HwannR_imag
      47              :      integer:: i_cell, i_numbers, i_masses, i_xred, i_xcart
      48              :      integer:: i_kpts, i_eigvals, i_Amnk_real, i_Amnk_imag
      49              :    contains
      50              :      procedure:: initialize
      51              :      procedure:: write_header
      52              :      procedure:: write_wann
      53              :      procedure:: write_atoms
      54              :      procedure:: close_file
      55              :      procedure:: write_Amnk
      56              :   end type IOWannNC
      57              : 
      58              :   contains
      59              : 
      60              : !---------------------------------------------------------------------
      61              : ! initialize wannier netcdf file
      62              : !---------------------------------------------------------------------
      63            3 :   subroutine initialize(self, filename)
      64              :     class(IOwannNC), intent(inout):: self
      65              :     character(len=*), intent(in):: filename
      66              :     integer:: ncerr
      67            3 :     ncerr = nf90_create(path = trim(filename), cmode = NF90_NETCDF4, ncid = self%ncid)
      68            3 :     NCF_CHECK_MSG(ncerr, "Error when creating wannier netcdf  file")
      69            3 :   end subroutine initialize
      70              : 
      71              : !---------------------------------------------------------------------
      72              : ! write wannier function information to netcdf file
      73              : !---------------------------------------------------------------------
      74            0 :   subroutine write_header(self)
      75              :     class(IOWannNC), intent(inout):: self
      76              :     !TODO what information should be written in the header?
      77            0 :     ABI_UNUSED_A(self)
      78            0 :   end subroutine write_header
      79              : 
      80              : !---------------------------------------------------------------------
      81              : !> write wannier function information to netcdf file
      82              : !@param self instance of IOWannNC
      83              : !@param nR number of R vectors
      84              : !@param ndim dimension of R vectors
      85              : !@param nwann number of Wannier functions
      86              : !@param nbasis number of basis functions
      87              : !@param Rlist list of R vectors
      88              : !@param WannR Wannier function in real space
      89              : !@param HwannR Wannier Hamiltonian in real space
      90              : !@param wannR_unit unit of Wannier function
      91              : !@param HwannR_unit unit of Wannier Hamiltonian
      92              : !---------------------------------------------------------------------
      93            3 :   subroutine write_wann( self,  nR, ndim, nwann, nbasis, Rlist, WannR, HwannR, wannR_unit,  HwannR_unit)
      94              :     class(IOwannNC), intent(inout):: self
      95              :     integer, intent(in):: nR, ndim, nwann, nbasis, Rlist(:,:)
      96              :     complex(dp), intent(in):: WannR(:,:,:), HwannR(:,:,:)
      97              :     character(*), intent(in):: wannR_unit, HwannR_unit
      98              :     ! id of variables
      99              :     integer:: ncerr
     100              : 
     101              :     ! define dimensions
     102            3 :     ncerr = nf90_def_dim(self%ncid, "ndim", ndim, self%d_ndim)
     103            3 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension ndim in wannier nc file.")
     104              : 
     105            3 :     ncerr = nf90_def_dim(self%ncid, "nwann", nwann,  self%d_nwann)
     106            3 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension nwann in wannier nc file.")
     107              : 
     108            3 :     ncerr = nf90_def_dim(self%ncid, "nbasis", nbasis,  self%d_nbasis)
     109            3 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension nbasis in wannier nc file.")
     110              : 
     111            3 :     ncerr = nf90_def_dim(self%ncid, "nR", nR,  self%d_nR)
     112            3 :     NCF_CHECK_MSG(ncerr, "Error when defining dimension nR in wannier nc file.")
     113              : 
     114              :     ! define variables
     115              :     call ab_define_var(self%ncid, [self%d_ndim, self%d_nR], self%i_Rlist, &
     116            9 :          & NF90_INT, "Rlist", "List of R vectors", "dimensionless")
     117              : 
     118              :     call ab_define_var(self%ncid, [self%d_nbasis, self%d_nwann, self%d_nR], &
     119           12 :          & self%i_wannR_real, NF90_DOUBLE, "wannier_function_real", "The real part of the Wannier function", wannR_unit)
     120              : 
     121              :     call ab_define_var(self%ncid, [self%d_nbasis, self%d_nwann, self%d_nR], &
     122           12 :          & self%i_wannR_imag, NF90_DOUBLE, "wannier_function_imag", "The imaginary part of the Wannier function", wannR_unit)
     123              : 
     124              :     call ab_define_var(self%ncid, [self%d_nwann, self%d_nwann, self%d_nR], &
     125           12 :          & self%i_HwannR_real, NF90_DOUBLE, "HamR_real", "The real part of the Wannier Hamiltonian", HwannR_unit)
     126              : 
     127              :     call ab_define_var(self%ncid, [self%d_nwann, self%d_nwann, self%d_nR], &
     128           12 :          & self%i_HwannR_imag, NF90_DOUBLE, "HamR_imag", "The imaginary part of the Wannier Hamiltonian", HwannR_unit)
     129            3 :     ncerr = nf90_enddef(self%ncid)
     130            3 :     NCF_CHECK_MSG(ncerr, "Error when ending def mode in wannier netcdf file")
     131              : 
     132              :     ! set variables
     133              : 
     134            9 :     ncerr = nf90_put_var(self%ncid, self%i_Rlist, Rlist, start=[1, 1], count=[3, nR])
     135            3 :     NCF_CHECK_MSG(ncerr, "Error when writting Rlist in wannier netcdf file.")
     136              : 
     137              :     ncerr = nf90_put_var(self%ncid, self%i_wannR_real, real(real(wannR)), &
     138         9420 :          & start=[1, 1], count=[nbasis, nwann, nR])
     139            3 :     NCF_CHECK_MSG(ncerr, "Error when writting WannR_real in wannier netcdf file.")
     140              : 
     141              :     ncerr = nf90_put_var(self%ncid, self%i_wannR_imag, real(aimag(wannR)), &
     142         9420 :          & start=[1, 1], count=[nbasis, nwann, nR])
     143            3 :     NCF_CHECK_MSG(ncerr, "Error when writting WannR_imag in wannier netcdf file.")
     144              : 
     145              : 
     146              : 
     147              :     ncerr = nf90_put_var(self%ncid, self%i_HwannR_real, real(real(HwannR)), &
     148         2508 :          & start=[1, 1], count=[nwann, nwann, nR])
     149            3 :     NCF_CHECK_MSG(ncerr, "Error when writting HwannR_real in wannier netcdf file.")
     150              : 
     151              :     ncerr = nf90_put_var(self%ncid, self%i_HwannR_imag, real(aimag(HwannR)), &
     152         2508 :          & start=[1, 1], count=[nwann, nwann, nR])
     153            3 :     NCF_CHECK_MSG(ncerr, "Error when writting HwannR_imag in wannier netcdf file.")
     154              : 
     155            3 :   end subroutine write_wann
     156              : 
     157              : !---------------------------------------------------------------------
     158              : !@brief close the wannier netcdf file
     159              : !---------------------------------------------------------------------
     160            3 :   subroutine close_file(self)
     161              :     class(IOwannNC), intent(inout):: self
     162              :     integer:: ncerr
     163            3 :     ncerr = nf90_close(self%ncid)
     164            3 :     NCF_CHECK_MSG(ncerr, "Error close wannier netcdf file.")
     165            3 :   end subroutine close_file
     166              : 
     167              : !---------------------------------------------------------------------
     168              : !@brief write the Amnk matrix in the wannier netcdf file
     169              : !---------------------------------------------------------------------
     170            3 :   subroutine write_Amnk(self, nkpt, nband, nwann, kpoints, Amnk)
     171              :     class(IOwannNC), intent(inout):: self
     172              :     real(dp), intent(in):: kpoints(:, :)
     173              :     integer, intent(in):: nkpt, nband, nwann
     174              :     !real(dp),  intent(in):: eigvals(:,:)
     175              :     complex(dp),  intent(in):: Amnk(:,:, :)
     176              :     integer:: ncerr
     177              : 
     178            3 :     ncerr = nf90_redef(self%ncid)
     179            3 :     NCF_CHECK_MSG(ncerr, "Error starting redef in wannier netcdf file")
     180              : 
     181            3 :     ncerr = nf90_def_dim(self%ncid, "nkpt", nkpt, self%d_nkpt)
     182            3 :     NCF_CHECK_MSG(ncerr, "Error defining nkpt in wannier netcdf file")
     183              : 
     184            3 :     ncerr = nf90_def_dim(self%ncid, "nband", nband, self%d_nband)
     185            3 :     NCF_CHECK_MSG(ncerr, "Error defining nband in wannier netcdf file")
     186              : 
     187              :     call ab_define_var(self%ncid, [self%d_ndim, self%d_nkpt], &
     188              :          & self%i_kpts, NF90_DOUBLE, "kpoints", &
     189            9 :          &"KPOINTS" , "dimensionless")
     190              : 
     191              :     !call ab_define_var(self%ncid, [self%d_nband, self%d_nkpt], &
     192              :     !     & self%i_eigvals, NF90_DOUBLE, "eigvals", &
     193              :     !     &"EIGen VALueS" , "eV")
     194              : 
     195              :     call ab_define_var(self%ncid, [self%d_nband, self%d_nwann,  self%d_nkpt], &
     196              :          & self%i_Amnk_real, NF90_DOUBLE, "Amnk_real", &
     197           12 :          &"The Amnk matrix: the REAL part" , "unitless")
     198              : 
     199              :     call ab_define_var(self%ncid, [self%d_nband, self%d_nwann,  self%d_nkpt], &
     200              :          & self%i_Amnk_imag, NF90_DOUBLE, "Amnk_imag", &
     201           12 :          &"The Amnk matrix: the imaginary part" , "unitless")
     202              : 
     203            3 :     ncerr = nf90_enddef(self%ncid)
     204            3 :     NCF_CHECK_MSG(ncerr, "Error ending redef in wannier netcdf file")
     205              : 
     206            9 :     ncerr = nf90_put_var(self%ncid, self%i_kpts, kpoints, start=[1, 1], count=[3, nkpt])
     207            3 :     NCF_CHECK_MSG(ncerr, "Error when writting kpoints in wannier netcdf file.")
     208              : 
     209              :     !ncerr = nf90_put_var(self%ncid, self%i_eigvals, eigvals, start=[1, 1], count=[nband, nkpt])
     210              :     !NCF_CHECK_MSG(ncerr, "Error when writting eigvals in wannier netcdf file.")
     211              : 
     212              :     ncerr = nf90_put_var(self%ncid, self%i_Amnk_real, real(real(Amnk)), &
     213         9420 :          & start=[1, 1], count=[nband, nwann, nkpt])
     214            3 :     NCF_CHECK_MSG(ncerr, "Error when writting Amnk_real in wannier netcdf file.")
     215              : 
     216              :     ncerr = nf90_put_var(self%ncid, self%i_Amnk_imag, real(aimag(Amnk)), &
     217         9420 :          & start=[1, 1], count=[nband, nwann, nkpt])
     218            3 :     NCF_CHECK_MSG(ncerr, "Error when writting Amnk_imag in wannier netcdf file.")
     219              : 
     220            3 :   end subroutine write_Amnk
     221              : 
     222              : !---------------------------------------------------------------------
     223              : !@brief write the structure information in the wannier netcdf file
     224              : !@param natom number of atoms
     225              : !@param cell the cell vectors
     226              : !@param numbers the atomic numbers
     227              : !@param masses the atomic masses
     228              : !@param xred the reduced coordinates of the atoms
     229              : !---------------------------------------------------------------------
     230            0 :   subroutine write_atoms(self, natom, cell, numbers, masses, xred)
     231              :     class(IOwannNC), intent(inout):: self
     232              :     integer, intent(in):: natom
     233              :     integer, intent(in):: numbers(:)
     234              :     real(dp), intent(in):: cell(:,:), masses(:), xred(:, :)
     235              : 
     236              :     integer:: ncerr
     237            0 :     ncerr = nf90_redef(self%ncid)
     238            0 :     NCF_CHECK_MSG(ncerr, "Error starting redef in wannier netcdf file")
     239            0 :     ncerr = nf90_def_dim(self%ncid, "three", 3, self%d_three)
     240            0 :     NCF_CHECK_MSG(ncerr, "Error defining three in wannier netcdf file")
     241              : 
     242            0 :     ncerr = nf90_def_dim(self%ncid, "natom", natom, self%d_natom)
     243            0 :     NCF_CHECK_MSG(ncerr, "Error defining natom in wannier netcdf file")
     244              : 
     245              :     !integer:: i_cell, i_numbers, i_masses, i_xred, i_xcart
     246              : 
     247              :     call ab_define_var(self%ncid, [self%d_three, self%d_three], &
     248              :          & self%i_cell, NF90_DOUBLE, "atomic_cell", &
     249            0 :          &"ATOMic CELL" , "Angstrom")
     250              : 
     251              :     call ab_define_var(self%ncid, [ self%d_natom], &
     252              :          & self%i_numbers, NF90_DOUBLE, "atomic_numbers", &
     253            0 :          &"ATOMic NUMBERS" , "atomic unit")
     254              : 
     255              :     call ab_define_var(self%ncid, [ self%d_natom], &
     256              :          & self%i_masses, NF90_DOUBLE, "atomic_masses", &
     257            0 :          &"ATOMic MASSES" , "atomic unit")
     258              : 
     259              :     call ab_define_var(self%ncid, [self%d_three, self%d_natom], &
     260              :          & self%i_xred, NF90_DOUBLE, "atomic_xred", &
     261            0 :          &"ATOMic structure: Reduced positions" , "dimensionless")
     262              : 
     263              :     call ab_define_var(self%ncid, [self%d_three, self%d_natom], &
     264              :          & self%i_xcart, NF90_DOUBLE, "atomic_xcart", &
     265            0 :          &"ATOMic structure: CARTesian positions" , "Angstrom")
     266              : 
     267              : 
     268            0 :     ncerr = nf90_enddef(self%ncid)
     269            0 :     NCF_CHECK_MSG(ncerr, "Error ending redef in wannier netcdf file")
     270              : 
     271            0 :     ncerr = nf90_put_var(self%ncid, self%i_cell, cell, start=[1, 1], count=[3, 3])
     272            0 :     NCF_CHECK_MSG(ncerr, "Error when writting atomic_cell in wannier netcdf file.")
     273              : 
     274            0 :     ncerr = nf90_put_var(self%ncid, self%i_numbers, numbers, start=[1], count=[natom])
     275            0 :     NCF_CHECK_MSG(ncerr, "Error when writting atomic_numbers in wannier netcdf file.")
     276              : 
     277            0 :     ncerr = nf90_put_var(self%ncid, self%i_masses, masses, start=[1], count=[natom])
     278            0 :     NCF_CHECK_MSG(ncerr, "Error when writting atomic_masses in wannier netcdf file.")
     279              : 
     280            0 :     ncerr = nf90_put_var(self%ncid, self%i_xred, xred, start=[1, 1], count=[3, natom])
     281            0 :     NCF_CHECK_MSG(ncerr, "Error when writting atomic_xred in wannier netcdf file.")
     282              : 
     283              :     !ncerr = nf90_put_var(self%ncid, self%i_xcart, xcart, start=[1, 1], count=[3, natom])
     284              :     !NCF_CHECK_MSG(ncerr, "Error when writting atomic_xcart in wannier netcdf file.")
     285            0 :   end subroutine write_atoms
     286              : 
     287            0 : end module m_wann_netcdf
     288              : 
     289              : !!***
        

Generated by: LCOV version 2.3-1