LCOV - code coverage report
Current view: top level - src - qs_wf_history_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:3db43b4) Lines: 95.9 % 49 47
Test Date: 2026-04-03 06:55:30 Functions: 50.0 % 8 4

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief interpolate the wavefunctions to speed up the convergence when
      10              : !>      doing MD
      11              : !> \par History
      12              : !>      12.2002 created [fawzi]
      13              : !>      02.2005 wf_mol added [MI]
      14              : !> \author fawzi
      15              : ! **************************************************************************************************
      16              : MODULE qs_wf_history_types
      17              :    USE cp_cfm_types,                    ONLY: cp_cfm_release,&
      18              :                                               cp_cfm_type
      19              :    USE cp_dbcsr_api,                    ONLY: dbcsr_deallocate_matrix,&
      20              :                                               dbcsr_p_type,&
      21              :                                               dbcsr_type
      22              :    USE cp_dbcsr_operations,             ONLY: dbcsr_deallocate_matrix_set
      23              :    USE cp_fm_types,                     ONLY: cp_fm_release,&
      24              :                                               cp_fm_type
      25              :    USE kinds,                           ONLY: dp
      26              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      27              :                                               pw_r3d_rs_type
      28              :    USE qs_rho_types,                    ONLY: qs_rho_release,&
      29              :                                               qs_rho_type
      30              : #include "./base/base_uses.f90"
      31              : 
      32              :    IMPLICIT NONE
      33              :    PRIVATE
      34              : 
      35              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      36              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_wf_history_types'
      37              : 
      38              :    PUBLIC :: qs_wf_snapshot_type, &
      39              :              qs_wf_history_type, qs_wf_history_p_type
      40              :    PUBLIC :: wfi_retain, wfi_release, wfi_get_snapshot
      41              : 
      42              : ! **************************************************************************************************
      43              : !> \brief represent a past snapshot of the wavefunction.
      44              : !>      some elements might not be associated (to spare memory)
      45              : !>      depending on how the snapshot was taken
      46              : !> \param wf the wavefunctions
      47              : !> \param rho_r the density in r space
      48              : !> \param rho_g the density in g space
      49              : !> \param rho_ao the density in ao space
      50              : !> \param overlap the overlap matrix
      51              : !> \param rho_frozen the frozen density structure
      52              : !> \param dt the time of the snapshot (wrf to te previous snapshot!)
      53              : !> \note
      54              : !>      keep track also of occupation numbers and energies?
      55              : !> \par History
      56              : !>      02.2003 created [fawzi]
      57              : !>      02.2005 wf_mol added [MI]
      58              : !> \author fawzi
      59              : ! **************************************************************************************************
      60              :    TYPE qs_wf_snapshot_type
      61              :       TYPE(cp_fm_type), DIMENSION(:), POINTER :: wf => NULL()
      62              :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r => NULL()
      63              :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g => NULL()
      64              :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao => NULL()
      65              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp => NULL()
      66              :       TYPE(dbcsr_type), POINTER :: overlap => NULL()
      67              :       TYPE(cp_fm_type), DIMENSION(:, :, :), POINTER :: wf_kp => NULL()
      68              :       TYPE(cp_cfm_type), DIMENSION(:), POINTER :: overlap_cfm_kp => NULL()
      69              :       TYPE(qs_rho_type), POINTER :: rho_frozen => NULL()
      70              :       REAL(KIND=dp) :: dt = 0.0_dp
      71              :    END TYPE qs_wf_snapshot_type
      72              : 
      73              : ! **************************************************************************************************
      74              : !> \brief pointer to a snapshot
      75              : !> \param snapshot the pointer to the snapshot
      76              : !> \par History
      77              : !>      02.2003 created [fawzi]
      78              : !> \author fawzi
      79              : ! **************************************************************************************************
      80              :    TYPE qs_wf_snapshot_p_type
      81              :       TYPE(qs_wf_snapshot_type), POINTER :: snapshot => NULL()
      82              :    END TYPE qs_wf_snapshot_p_type
      83              : 
      84              : ! **************************************************************************************************
      85              : !> \brief keeps track of the previous wavefunctions and can extrapolate them
      86              : !>      for the next step of md
      87              : !> \param ref_cont reference count (see doc/ReferenceCounting.html)
      88              : !> \param memory_depth how many snapshots should be stored
      89              : !> \param last_state_index index of the latest snapshot
      90              : !> \param past_states array with the past states (index starts at
      91              : !>        last_state_index)
      92              : !> \param interpolation_method_nr the tag of the method used to
      93              : !>        extrapolate the new start state for qs
      94              : !> \param snapshot_count number of snapshot taken so far (cumulative,
      95              : !>        can be bigger than the history depth)
      96              : !> \note
      97              : !>      use a linked list for the past states ?
      98              : !> \par History
      99              : !>      02.2003 created [fawzi]
     100              : !> \author fawzi
     101              : ! **************************************************************************************************
     102              :    TYPE qs_wf_history_type
     103              :       INTEGER :: ref_count = -1, memory_depth = -1, last_state_index = -1, &
     104              :                  interpolation_method_nr = -1, snapshot_count = -1
     105              :       LOGICAL :: store_wf = .FALSE., store_rho_r = .FALSE., store_rho_g = .FALSE., &
     106              :                  store_rho_ao = .FALSE., store_rho_ao_kp = .FALSE., &
     107              :                  store_overlap = .FALSE., store_wf_kp = .FALSE., store_overlap_kp = .FALSE., &
     108              :                  store_frozen_density = .FALSE.
     109              :       TYPE(qs_wf_snapshot_p_type), DIMENSION(:), POINTER :: past_states => NULL()
     110              :    END TYPE qs_wf_history_type
     111              : 
     112              : ! **************************************************************************************************
     113              : !> \brief to create arrays of pointers to qs_wf_history_type
     114              : !> \param wf_hist the pointer to the wf history
     115              : !> \author fawzi
     116              : ! **************************************************************************************************
     117              :    TYPE qs_wf_history_p_type
     118              :       TYPE(qs_wf_history_type), POINTER :: wf_history => NULL()
     119              :    END TYPE qs_wf_history_p_type
     120              : 
     121              : CONTAINS
     122              : 
     123              : ! **************************************************************************************************
     124              : !> \brief releases a snapshot of a wavefunction (see doc/ReferenceCounting.html)
     125              : !> \param snapshot the snapshot to release
     126              : !> \par History
     127              : !>      02.2003 created [fawzi]
     128              : !>      02.2005 wf_mol added [MI]
     129              : !> \author fawzi
     130              : ! **************************************************************************************************
     131        11358 :    SUBROUTINE wfs_release(snapshot)
     132              :       TYPE(qs_wf_snapshot_type), INTENT(INOUT)           :: snapshot
     133              : 
     134              :       INTEGER                                            :: i, ic, ikp, ispin
     135              : 
     136        11358 :       CALL cp_fm_release(snapshot%wf)
     137              :       ! snapshot%rho_r & snapshot%rho_g is deallocated in wfs_update
     138              :       ! of qs_wf_history_methods, in case you wonder about it.
     139        11358 :       IF (ASSOCIATED(snapshot%rho_ao)) THEN
     140           68 :          CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao)
     141              :       END IF
     142        11358 :       IF (ASSOCIATED(snapshot%rho_ao_kp)) THEN
     143           12 :          CALL dbcsr_deallocate_matrix_set(snapshot%rho_ao_kp)
     144              :       END IF
     145        11358 :       IF (ASSOCIATED(snapshot%overlap)) THEN
     146        10044 :          CALL dbcsr_deallocate_matrix(snapshot%overlap)
     147              :       END IF
     148        11358 :       IF (ASSOCIATED(snapshot%wf_kp)) THEN
     149         1228 :          DO ikp = 1, SIZE(snapshot%wf_kp, 1)
     150         3292 :             DO ic = 1, SIZE(snapshot%wf_kp, 2)
     151         5840 :                DO ispin = 1, SIZE(snapshot%wf_kp, 3)
     152         4808 :                   CALL cp_fm_release(snapshot%wf_kp(ikp, ic, ispin))
     153              :                END DO
     154              :             END DO
     155              :          END DO
     156          196 :          DEALLOCATE (snapshot%wf_kp)
     157              :       END IF
     158        11358 :       IF (ASSOCIATED(snapshot%overlap_cfm_kp)) THEN
     159         1228 :          DO i = 1, SIZE(snapshot%overlap_cfm_kp)
     160         1228 :             CALL cp_cfm_release(snapshot%overlap_cfm_kp(i))
     161              :          END DO
     162          196 :          DEALLOCATE (snapshot%overlap_cfm_kp)
     163              :       END IF
     164        11358 :       IF (ASSOCIATED(snapshot%rho_frozen)) THEN
     165            4 :          CALL qs_rho_release(snapshot%rho_frozen)
     166            4 :          DEALLOCATE (snapshot%rho_frozen)
     167              :       END IF
     168              : 
     169        11358 :    END SUBROUTINE wfs_release
     170              : 
     171              : ! **************************************************************************************************
     172              : !> \brief retains a wf history (see doc/ReferenceCounting.html)
     173              : !> \param wf_history the wf_history to retain
     174              : !> \par History
     175              : !>      02.2003 created [fawzi]
     176              : !> \author fawzi
     177              : ! **************************************************************************************************
     178         8124 :    SUBROUTINE wfi_retain(wf_history)
     179              :       TYPE(qs_wf_history_type), POINTER                  :: wf_history
     180              : 
     181         8124 :       CPASSERT(ASSOCIATED(wf_history))
     182         8124 :       wf_history%ref_count = wf_history%ref_count + 1
     183              : 
     184         8124 :    END SUBROUTINE wfi_retain
     185              : 
     186              : ! **************************************************************************************************
     187              : !> \brief releases a wf_history of a wavefunction
     188              : !>      (see doc/ReferenceCounting.html)
     189              : !> \param wf_history the wf_history to release
     190              : !> \par History
     191              : !>      02.2003 created [fawzi]
     192              : !> \author fawzi
     193              : ! **************************************************************************************************
     194        23583 :    SUBROUTINE wfi_release(wf_history)
     195              :       TYPE(qs_wf_history_type), POINTER                  :: wf_history
     196              : 
     197              :       INTEGER                                            :: i
     198              : 
     199        23583 :       IF (ASSOCIATED(wf_history)) THEN
     200        15852 :          CPASSERT(wf_history%ref_count > 0)
     201        15852 :          wf_history%ref_count = wf_history%ref_count - 1
     202        15852 :          IF (wf_history%ref_count == 0) THEN
     203         7728 :             IF (ASSOCIATED(wf_history%past_states)) THEN
     204        44679 :                DO i = 1, SIZE(wf_history%past_states)
     205        44679 :                   IF (ASSOCIATED(wf_history%past_states(i)%snapshot)) THEN
     206        11358 :                      CALL wfs_release(wf_history%past_states(i)%snapshot)
     207        11358 :                      DEALLOCATE (wf_history%past_states(i)%snapshot)
     208              :                   END IF
     209              :                END DO
     210         7728 :                DEALLOCATE (wf_history%past_states)
     211              :             END IF
     212         7728 :             DEALLOCATE (wf_history)
     213              :          END IF
     214              :       END IF
     215        23583 :       NULLIFY (wf_history)
     216        23583 :    END SUBROUTINE wfi_release
     217              : 
     218              : ! **************************************************************************************************
     219              : !> \brief returns a snapshot, the first being the latest snapshot
     220              : !> \param wf_history the plage where to get the snapshot
     221              : !> \param wf_index the index of the snapshot you want
     222              : !> \return ...
     223              : !> \par History
     224              : !>      12.2002 created [fawzi]
     225              : !> \author fawzi
     226              : ! **************************************************************************************************
     227        54189 :    FUNCTION wfi_get_snapshot(wf_history, wf_index) RESULT(res)
     228              :       TYPE(qs_wf_history_type), POINTER                  :: wf_history
     229              :       INTEGER, INTENT(in)                                :: wf_index
     230              :       TYPE(qs_wf_snapshot_type), POINTER                 :: res
     231              : 
     232        54189 :       NULLIFY (res)
     233              : 
     234        54189 :       CPASSERT(ASSOCIATED(wf_history))
     235        54189 :       CPASSERT(ASSOCIATED(wf_history%past_states))
     236        54189 :       IF (wf_index > wf_history%memory_depth .OR. wf_index > wf_history%snapshot_count) THEN
     237            0 :          CPABORT("")
     238              :       END IF
     239              :       res => wf_history%past_states( &
     240              :              MODULO(wf_history%snapshot_count + 1 - wf_index, &
     241        54189 :                     wf_history%memory_depth) + 1)%snapshot
     242        54189 :    END FUNCTION wfi_get_snapshot
     243              : 
     244            0 : END MODULE qs_wf_history_types
        

Generated by: LCOV version 2.0-1