LCOV - code coverage report
Current view: top level - src/55_abiutil - m_scf_history.F90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.9 % 97 95
Test Date: 2026-09-19 15:24:51 Functions: 60.0 % 5 3

            Line data    Source code
       1              : !!****m* ABINIT/m_scf_history
       2              : !! NAME
       3              : !!  m_scf_history
       4              : !!
       5              : !! FUNCTION
       6              : !!  This module provides the definition of the scf_history_type used to store
       7              : !!  various arrays obtained from previous SCF cycles (density, positions, wavefunctions ...),
       8              : !!  as needed by the specific SCF algorithm.
       9              : !!
      10              : !! COPYRIGHT
      11              : !! Copyright (C) 2011-2026 ABINIT group (MT)
      12              : !! This file is distributed under the terms of the
      13              : !! GNU General Public License, see ~abinit/COPYING
      14              : !! or http://www.gnu.org/copyleft/gpl.txt .
      15              : !!
      16              : !! SOURCE
      17              : 
      18              : #if defined HAVE_CONFIG_H
      19              : #include "config.h"
      20              : #endif
      21              : 
      22              : #include "abi_common.h"
      23              : 
      24              : MODULE m_scf_history
      25              : 
      26              :  use defs_basis
      27              :  use m_abicore
      28              :  use m_dtset
      29              :  use m_errors
      30              : 
      31              :  use defs_abitypes, only : MPI_type
      32              :  use m_pawcprj,  only : pawcprj_type, pawcprj_free
      33              :  use m_pawrhoij, only : pawrhoij_type, pawrhoij_nullify, pawrhoij_free
      34              : 
      35              :  implicit none
      36              : 
      37              :  private
      38              : !!***
      39              : 
      40              : !!****t* m_scf_history/scf_history_type
      41              : !! NAME
      42              : !! scf_history_type
      43              : !!
      44              : !! FUNCTION
      45              : !! This structured datatype contains various arrays obtained from
      46              : !! previous SCF cycles (density, positions...)
      47              : !!
      48              : !! SOURCE
      49              : 
      50              :  type, public :: scf_history_type
      51              : 
      52              : ! WARNING : if you modify this datatype, please check whether there might be creation/destruction/copy routines,
      53              : ! declared in another part of ABINIT, that might need to take into account your modification.
      54              : 
      55              : ! Integer scalar
      56              : 
      57              :   integer :: history_size
      58              :    ! Number of previous SCF cycles stored in history
      59              :    ! If history_size<0, scf_history is not used
      60              :    ! If history_size=0, scf_history only contains
      61              :    !    current values of data (rhor, taur, pawrhoih, xred)
      62              :    ! If history_size>0, scf_history contains
      63              :    !    current values of data and also
      64              :    !    history_size previous values of these data
      65              : 
      66              :   integer :: icall
      67              :    ! Number of call for the routine extraprho or wf_mixing
      68              : 
      69              :   integer :: mcg
      70              :    ! Size of cg array
      71              : 
      72              :   integer :: mcprj
      73              :    ! Size of cprj datsatructure array
      74              : 
      75              :   integer :: meigen
      76              :    ! Size of eigen array
      77              : 
      78              :   integer :: natom
      79              :    ! Number of atoms in cell
      80              : 
      81              :   integer :: nfft
      82              :    ! Size of FFT grid (for density)
      83              : 
      84              :   integer :: nspden
      85              :    ! Number of independant spin components for density
      86              : 
      87              :   integer :: usecg
      88              :    ! usecg=0 if the extrapolation/mixing of the density/potential is active but not the one of the wavefunction
      89              :    ! usecg=1 if the extrapolation/mixing of the density/potential and wavefunctions is active
      90              :    ! usecg=2 if the extrapolation/mixing of the wavefunctions is active but not the one of the density/potential
      91              : 
      92              :   integer :: wfmixalg
      93              :    ! algorithm used to mix the wavefunctions (in case usecg=2)
      94              : 
      95              :   real(dp) :: alpha
      96              :    ! alpha mixing coefficient for the prediction of density and wavefunctions
      97              :    ! In the case of wavefunction simple mixing, contain wfmix factor
      98              : 
      99              :   real(dp) :: beta
     100              :    ! beta mixing coefficient for the prediction of density and wavefunctions
     101              : 
     102              : ! Integer arrays
     103              : 
     104              :   integer,allocatable :: hindex(:)
     105              :    ! Indexes of SCF cycles in the history
     106              :    !
     107              :    ! For the density-based schemes (with or without wavefunctions) :
     108              :    ! hindex(history_size)
     109              :    ! hindex(1) is the newest SCF cycle
     110              :    ! hindex(history_size) is the oldest SCF cycle
     111              :    !
     112              :    ! For wavefunction-based schemes (outer loop of a double loop SCF):
     113              :    ! hindex(2*history_size+1)
     114              :    ! The odd indices refer to the out wavefunction,
     115              :    ! the even indices refer to the in wavefunction (not all such wavefunctions being stored, though).
     116              :    ! hindex(1:2) is the newest SCF cycle, hindex(3:4) is the SCF cycle before the newest one ... In case of an
     117              :    ! algorithm based on a biorthogonal ensemble of wavefunctions, the reference is stored in hindex(2*history_size+1)
     118              :    ! When the index points to a location beyond history_size, the corresponding wavefunction set must be reconstructed
     119              :    ! from the existing wavefunctions sets (to be implemented)
     120              : 
     121              : ! Real (real(dp)) arrays
     122              : 
     123              :    real(dp),allocatable :: cg(:,:,:)
     124              :     ! cg(2,mcg,history_size)
     125              :     ! wavefunction coefficients needed for each SCF cycle of history
     126              :     ! Might also contain the wf residuals
     127              : 
     128              :    real(dp),allocatable :: deltarhor(:,:,:)
     129              :     ! deltarhor(nfft,nspden,history_size)
     130              :     ! Difference between electronic density (in real space)
     131              :     ! and sum of atomic densities at the end of each SCF cycle of history
     132              : 
     133              :    real(dp),allocatable :: eigen(:,:)
     134              :     ! eigen(meigen,history_size)
     135              :     ! eigenvalues for each SCF cycle of history
     136              : 
     137              :    real(dp),allocatable :: atmrho_last(:)
     138              :     ! atmrho_last(nfft)
     139              :     ! Sum of atomic densities at the end of the LAST SCF cycle
     140              : 
     141              :    real(dp),allocatable :: nextfpmd(:)
     142              :     ! extfpmd density
     143              : 
     144              :    real(dp),allocatable :: rhor_last(:,:)
     145              :     ! rhor_last(nfft,nspden)
     146              :     ! Last computed electronic density (in real space)
     147              : 
     148              :    real(dp),allocatable :: taur_last(:,:)
     149              :     ! taur_last(nfft,nspden*usekden)
     150              :     ! Last computed kinetic energy density (in real space)
     151              : 
     152              :    real(dp),allocatable :: xreddiff(:,:,:)
     153              :     ! xreddiff(3,natom,history_size)
     154              :     ! Difference of reduced coordinates of atoms between a
     155              :     ! SCF cycle and the previous
     156              : 
     157              :    real(dp),allocatable :: xred_last(:,:)
     158              :     ! xred_last(3,natom)
     159              :     ! Last computed atomic positions (reduced coordinates)
     160              : 
     161              :    real(dp),allocatable :: dotprod_sumdiag_cgcprj_ij(:,:,:)
     162              :     ! dotprod_sumdiag_cgcprj_mn(2,history_size,history_size)
     163              :     ! Container for the scalar products between aligned sets of wavefunctions or their residuals
     164              :     ! S_ij=Sum_nk <wf_nk(set i)|wf_nk(set j)> possibly with some weighting factor that might depend on nk.
     165              : 
     166              : ! Structured datatypes arrays
     167              : 
     168              :   type(pawrhoij_type), allocatable :: pawrhoij(:,:)
     169              :     ! pawrhoij(natom,history_size)
     170              :     ! PAW only: occupancies matrix at the end of each SCF cycle of history
     171              : 
     172              :   type(pawrhoij_type), allocatable :: pawrhoij_last(:)
     173              :     ! pawrhoij_last(natom)
     174              :     ! PAW only: last computed occupancies matrix
     175              : 
     176              :   type(pawcprj_type),allocatable :: cprj(:,:,:)
     177              :     !cprj(natom,nspinor*mband*mkmem*nsppol,history_size)
     178              : 
     179              :  end type scf_history_type
     180              : !!***
     181              : 
     182              : ! public procedures.
     183              :  public :: scf_history_init
     184              :  public :: scf_history_free
     185              :  public :: scf_history_nullify
     186              : 
     187              : CONTAINS !===========================================================
     188              : !!***
     189              : 
     190              : !!****f* m_scf_history/scf_history_init
     191              : !! NAME
     192              : !!  scf_history_init
     193              : !!
     194              : !! FUNCTION
     195              : !!  Init all scalars and pointers in a scf_history datastructure
     196              : !!  according to scf_history%history_size value which has to be
     197              : !!  defined before calling this routine
     198              : !!
     199              : !! INPUTS
     200              : !!  dtset <type(dataset_type)>=all input variables in this dataset
     201              : !!  mpi_enreg=MPI-parallelisation information
     202              : !!  usecg= if ==0 => no handling of wfs (and eigenvalues),
     203              : !!         if==1 => handling of density/potential AND wfs and eigen,
     204              : !!         if==2 => ONLY handling of wfs and eigen
     205              : !!
     206              : !! SIDE EFFECTS
     207              : !!  scf_history=<type(scf_history_type)>=scf_history datastructure
     208              : !!    hindex is always allocated
     209              : !!    The density/potential arrays that are possibly allocated are : atmrho_last, deltarhor,
     210              : !!      pawrhoij, pawrhoij_last, rhor_last, taur_last, xreddiff, xred_last.
     211              : !!    The wfs arrays that are possibly allocated are : cg, cprj and eigen
     212              : !!
     213              : !! SOURCE
     214              : 
     215         4235 : subroutine scf_history_init(dtset, mpi_enreg, usecg, scf_history)
     216              : 
     217              : !Arguments ------------------------------------
     218              : !scalars
     219              :  class(scf_history_type),intent(inout) :: scf_history
     220              :  integer, intent(in) :: usecg
     221              :  type(dataset_type),intent(in) :: dtset
     222              :  type(MPI_type),intent(in) :: mpi_enreg
     223              : 
     224              : !Local variables-------------------------------
     225              : !scalars
     226              :  integer :: jj,mband_cprj,my_natom,my_nspinor,nfft
     227              : !************************************************************************
     228              : 
     229              :  !@scf_history_type
     230              : 
     231         4235 :  if (scf_history%history_size<0) then
     232         3914 :    call scf_history_nullify(scf_history)
     233              :  else
     234              : 
     235          321 :    scf_history%usecg=usecg
     236          321 :    scf_history%wfmixalg=dtset%fockoptmix/100
     237              : 
     238          321 :    nfft=dtset%nfft
     239          321 :    if (dtset%usepaw==1.and.(dtset%pawecutdg>=1.0000001_dp*dtset%ecut)) nfft=dtset%nfftdg
     240          321 :    my_natom=mpi_enreg%my_natom
     241              : 
     242          321 :    if (scf_history%history_size>=0 .and. usecg<2) then
     243         1268 :      ABI_MALLOC(scf_history%rhor_last,(nfft,dtset%nspden))
     244         1268 :      ABI_MALLOC(scf_history%taur_last,(nfft,dtset%nspden*dtset%usekden))
     245          951 :      ABI_MALLOC(scf_history%xred_last,(3,dtset%natom))
     246         1177 :      ABI_MALLOC(scf_history%pawrhoij_last,(my_natom*dtset%usepaw))
     247          317 :      if (dtset%usepaw==1) then
     248          114 :        call pawrhoij_nullify(scf_history%pawrhoij_last)
     249              :      end if
     250              :    end if
     251              : 
     252          321 :    if (scf_history%history_size>0) then
     253              : 
     254          113 :      scf_history%natom=dtset%natom
     255          113 :      scf_history%nfft=nfft
     256          113 :      scf_history%nspden=dtset%nspden
     257          113 :      scf_history%beta=zero
     258          113 :      scf_history%icall=0
     259              : 
     260          113 :      scf_history%mcg=0
     261          113 :      scf_history%mcprj=0
     262          113 :      scf_history%meigen=0
     263          113 :      if (usecg>0) then
     264           16 :        my_nspinor=max(1,dtset%nspinor/mpi_enreg%nproc_spinor)
     265           16 :        scf_history%meigen=dtset%nbandhf*dtset%mkmem*dtset%nsppol
     266           16 :        scf_history%mcg=dtset%mpw*my_nspinor*scf_history%meigen ! This is for scf_history_wf
     267           16 :        if(usecg==1)then
     268           12 :          scf_history%meigen=dtset%mband*dtset%mkmem*dtset%nsppol
     269           12 :          scf_history%mcg=dtset%mpw*my_nspinor*scf_history%meigen ! This is for scf_history (when extrapwf==1)
     270              :        endif
     271           16 :        if (dtset%usepaw==1) then
     272            2 :          mband_cprj=dtset%nbandhf
     273            2 :          if(usecg==1)mband_cprj=dtset%mband
     274            2 :          if (dtset%paral_kgb/=0) mband_cprj=mband_cprj/mpi_enreg%nproc_band
     275            2 :          scf_history%mcprj=my_nspinor*mband_cprj*dtset%mkmem*dtset%nsppol
     276              :        end if
     277              :      end if
     278              : 
     279          113 :      if (usecg<2) then
     280          327 :        ABI_MALLOC(scf_history%hindex,(scf_history%history_size))
     281          109 :        scf_history%alpha=zero
     282              :      else
     283           12 :        ABI_MALLOC(scf_history%hindex,(2*scf_history%history_size+1))
     284            4 :        scf_history%alpha=dtset%wfmix
     285              :      endif
     286          372 :      scf_history%hindex(:)=0
     287              : 
     288          113 :      if (usecg<2) then
     289          545 :        ABI_MALLOC(scf_history%deltarhor,(nfft,dtset%nspden,scf_history%history_size))
     290          436 :        ABI_MALLOC(scf_history%xreddiff,(3,dtset%natom,scf_history%history_size))
     291          327 :        ABI_MALLOC(scf_history%atmrho_last,(nfft))
     292          109 :        if (dtset%usepaw==1) then
     293          550 :          ABI_MALLOC(scf_history%pawrhoij,(my_natom,scf_history%history_size))
     294          189 :          do jj=1,scf_history%history_size
     295          189 :            call pawrhoij_nullify(scf_history%pawrhoij(:,jj))
     296              :          end do
     297              :        endif
     298          109 :        if(dtset%useextfpmd>0) then
     299            0 :          ABI_MALLOC(scf_history%nextfpmd,(scf_history%history_size))
     300              :        endif
     301              :      end if
     302              : 
     303          113 :      if (scf_history%usecg>0) then
     304           64 :        ABI_MALLOC(scf_history%cg,(2,scf_history%mcg,scf_history%history_size))
     305           64 :        ABI_MALLOC(scf_history%eigen,(scf_history%meigen,scf_history%history_size))
     306              : !      Note that the allocation is made even when usepaw==0. Still, scf_history%mcprj=0 ...
     307          453 :        ABI_MALLOC(scf_history%cprj,(dtset%natom,scf_history%mcprj,scf_history%history_size))
     308              :      end if
     309              : 
     310          113 :      if (scf_history%usecg==2)then
     311              : !      This relatively small matrix is always allocated when usecg==1, even if not used
     312           16 :        ABI_MALLOC(scf_history%dotprod_sumdiag_cgcprj_ij,(2,scf_history%history_size,scf_history%history_size))
     313              :      endif
     314              : 
     315              :    end if
     316              :  end if
     317              : 
     318         4235 : end subroutine scf_history_init
     319              : !!***
     320              : 
     321              : !----------------------------------------------------------------------
     322              : 
     323              : !!****f* m_scf_history/scf_history_free
     324              : !! NAME
     325              : !!  scf_history_free
     326              : !!
     327              : !! FUNCTION
     328              : !!  Clean and destroy a scf_history datastructure
     329              : !!
     330              : !! SOURCE
     331              : 
     332         4239 : subroutine scf_history_free(scf_history)
     333              : 
     334              : !Arguments ------------------------------------
     335              :  class(scf_history_type),intent(inout) :: scf_history
     336              : 
     337              : !Local variables-------------------------------
     338              :  integer :: jj
     339              : !************************************************************************
     340              : 
     341              :  !@scf_history_type
     342              : 
     343         4239 :  if (allocated(scf_history%pawrhoij_last)) then
     344          317 :    call pawrhoij_free(scf_history%pawrhoij_last)
     345          543 :    ABI_FREE(scf_history%pawrhoij_last)
     346              :  end if
     347         4239 :  if (allocated(scf_history%pawrhoij)) then
     348          189 :    do jj=1,size(scf_history%pawrhoij,2)
     349          189 :      call pawrhoij_free(scf_history%pawrhoij(:,jj))
     350              :    end do
     351          235 :    ABI_FREE(scf_history%pawrhoij)
     352              :  end if
     353         4239 :  if (allocated(scf_history%cprj)) then
     354           53 :    do jj=1,size(scf_history%cprj,3)
     355           53 :      call pawcprj_free(scf_history%cprj(:,:,jj))
     356              :    end do
     357          240 :    ABI_FREE(scf_history%cprj)
     358              :  end if
     359              : 
     360         4239 :  ABI_SFREE(scf_history%hindex)
     361         4239 :  ABI_SFREE(scf_history%deltarhor)
     362         4239 :  ABI_SFREE(scf_history%xreddiff)
     363         4239 :  ABI_SFREE(scf_history%atmrho_last)
     364         4239 :  ABI_SFREE(scf_history%xred_last)
     365         4239 :  ABI_SFREE(scf_history%rhor_last)
     366         4239 :  ABI_SFREE(scf_history%taur_last)
     367         4239 :  ABI_SFREE(scf_history%cg)
     368         4239 :  ABI_SFREE(scf_history%eigen)
     369         4239 :  ABI_SFREE(scf_history%dotprod_sumdiag_cgcprj_ij)
     370         4239 :  ABI_SFREE(scf_history%nextfpmd)
     371              : 
     372         4239 :  scf_history%history_size=-1
     373         4239 :  scf_history%usecg=0
     374         4239 :  scf_history%icall=0
     375         4239 :  scf_history%mcprj=0
     376         4239 :  scf_history%mcg=0
     377         4239 :  scf_history%meigen=0
     378              : 
     379         4239 : end subroutine scf_history_free
     380              : !!***
     381              : 
     382              : !----------------------------------------------------------------------
     383              : 
     384              : !!****f* m_scf_history/scf_history_nullify
     385              : !! NAME
     386              : !!  scf_history_nullify
     387              : !!
     388              : !! FUNCTION
     389              : !!  Nullify (set to null) an scf_history datastructure
     390              : !!
     391              : !! SOURCE
     392              : 
     393         8149 : subroutine scf_history_nullify(scf_history)
     394              : 
     395              : !Arguments ------------------------------------
     396              :  class(scf_history_type),intent(inout) :: scf_history
     397              : !************************************************************************
     398              : 
     399              :  !@scf_history_type
     400         8149 :  scf_history%history_size=-1
     401         8149 :  scf_history%icall=0
     402         8149 :  scf_history%mcprj=0
     403         8149 :  scf_history%mcg=0
     404         8149 :  scf_history%meigen=0
     405              : 
     406         3914 : end subroutine scf_history_nullify
     407              : !!***
     408              : 
     409            0 : END MODULE m_scf_history
     410              : !!***
        

Generated by: LCOV version 2.3-1