LCOV - code coverage report
Current view: top level - src - preconditioner_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:92574dc) Lines: 95.5 % 132 126
Test Date: 2026-09-24 01:27:39 Functions: 44.4 % 9 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 types of  preconditioners
      10              : !> \par History
      11              : !>      Separate types from construction and application
      12              : !> \author Joost VandeVondele (09.2002)
      13              : ! **************************************************************************************************
      14              : MODULE preconditioner_types
      15              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_release,&
      16              :                                               cp_blacs_env_type
      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_release_p,&
      22              :                                               dbcsr_type
      23              :    USE cp_fm_types,                     ONLY: cp_fm_release,&
      24              :                                               cp_fm_type
      25              :    USE input_constants,                 ONLY: cholesky_reduce,&
      26              :                                               ot_lattice_fft_off,&
      27              :                                               ot_low_rank_base_overlap,&
      28              :                                               ot_precond_solver_default
      29              :    USE kinds,                           ONLY: dp
      30              :    USE message_passing,                 ONLY: mp_para_env_release,&
      31              :                                               mp_para_env_type
      32              : #include "./base/base_uses.f90"
      33              : 
      34              :    IMPLICIT NONE
      35              : 
      36              :    PRIVATE
      37              : 
      38              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'preconditioner_types'
      39              : 
      40              :    PUBLIC  :: preconditioner_type, preconditioner_p_type
      41              :    PUBLIC  :: lattice_rhs_distribution_type
      42              :    PUBLIC  :: init_preconditioner
      43              :    PUBLIC  :: preconditioner_in_use
      44              :    PUBLIC  :: destroy_preconditioner
      45              :    PUBLIC  :: release_lattice_rhs_distribution
      46              : 
      47              : ! **************************************************************************************************
      48              :    TYPE lattice_rhs_distribution_type
      49              :       INTEGER :: nrow_global = 0, ncol_global = 0
      50              :       INTEGER :: nrow_local = 0, ncol_local = 0, num_pe = 0
      51              :       INTEGER, ALLOCATABLE, DIMENSION(:) :: global_columns
      52              :       INTEGER, ALLOCATABLE, DIMENSION(:) :: local_rows, local_columns
      53              :       INTEGER, ALLOCATABLE, DIMENSION(:) :: send_counts, send_displacements
      54              :       INTEGER, ALLOCATABLE, DIMENSION(:) :: recv_counts, recv_displacements
      55              :       INTEGER, ALLOCATABLE, DIMENSION(:) :: send_local_rows, send_local_columns
      56              :       INTEGER, ALLOCATABLE, DIMENSION(:) :: recv_global_rows, recv_global_columns
      57              :    END TYPE lattice_rhs_distribution_type
      58              : 
      59              : ! **************************************************************************************************
      60              :    TYPE preconditioner_type
      61              : !    PRIVATE
      62              :       TYPE(dbcsr_type), POINTER :: sparse_matrix => NULL()
      63              :       TYPE(cp_fm_type), POINTER :: fm => NULL()
      64              :       TYPE(cp_cfm_type), POINTER :: complex_fm => NULL()
      65              :       TYPE(cp_cfm_type), POINTER :: occ_rotation_complex => NULL()
      66              :       LOGICAL :: owns_complex_fm = .TRUE.
      67              :       TYPE(cp_fm_type), POINTER :: base_fm => NULL()
      68              :       TYPE(dbcsr_type), POINTER           :: dbcsr_matrix => NULL()
      69              :       TYPE(dbcsr_type), POINTER           :: base_dbcsr_matrix => NULL()
      70              :       TYPE(dbcsr_type), POINTER           :: occ_rotation_dbcsr => NULL()
      71              :       TYPE(dbcsr_type), POINTER           :: max_ev_vector => NULL()
      72              :       TYPE(dbcsr_type), POINTER           :: min_ev_vector => NULL()
      73              :       TYPE(dbcsr_p_type), POINTER, DIMENSION(:) :: inverse_history => NULL()
      74              :       TYPE(mp_para_env_type), POINTER   :: para_env => NULL()
      75              :       TYPE(cp_blacs_env_type), POINTER   :: ctxt => NULL()
      76              :       TYPE(cp_fm_type), POINTER          :: occ_rotation => NULL()
      77              :       INTEGER :: in_use = -1, solver = -1, ihistory = -1, cholesky_use = -1
      78              :       REAL(KIND=dp), DIMENSION(:), POINTER :: occ_evals => NULL(), full_evals => NULL()
      79              :       REAL(KIND=dp) :: energy_gap = -1.0_dp
      80              :       REAL(KIND=dp) :: condition_num = -1.0_dp
      81              :       INTEGER :: polynomial_degree = 8
      82              :       REAL(KIND=dp) :: polynomial_min = -1.0_dp, polynomial_max = -1.0_dp
      83              :       INTEGER :: spectral_rank = 0
      84              :       INTEGER :: spectral_max_rank = 48
      85              :       REAL(KIND=dp) :: spectral_reference = 0.0_dp, spectral_window = 1.0_dp, &
      86              :                        spectral_base_scale = 1.0_dp
      87              :       INTEGER :: low_rank_base = ot_low_rank_base_overlap
      88              :       INTEGER :: lattice_fft_mode = ot_lattice_fft_off
      89              :       LOGICAL :: lattice_fft_active = .FALSE.
      90              :       INTEGER, DIMENSION(3) :: lattice_dims = [1, 1, 1]
      91              :       INTEGER :: lattice_block_size = 0
      92              :       INTEGER :: lattice_local_cells = 0
      93              :       REAL(KIND=dp) :: lattice_projection_error = 0.0_dp, &
      94              :                        lattice_corrected_projection_error = 0.0_dp, &
      95              :                        lattice_cost_ratio = 0.0_dp, &
      96              :                        lattice_storage_ratio = 0.0_dp
      97              :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: lattice_inverse_k
      98              :       COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :, :) :: lattice_state_inverse_k
      99              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: lattice_occ_vectors, lattice_occ_dual
     100              :       INTEGER, ALLOCATABLE, DIMENSION(:) :: lattice_local_indices
     101              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: lattice_local_operator, &
     102              :                                                      lattice_local_coarse_inverse
     103              :       INTEGER, ALLOCATABLE, DIMENSION(:, :) :: lattice_state_local_indices
     104              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: lattice_state_local_operator, &
     105              :                                                         lattice_state_local_coarse_inverse
     106              :       TYPE(lattice_rhs_distribution_type) :: lattice_rhs_distribution
     107              :    END TYPE preconditioner_type
     108              : 
     109              : ! **************************************************************************************************
     110              :    TYPE preconditioner_p_type
     111              :       TYPE(preconditioner_type), POINTER :: preconditioner => NULL()
     112              :    END TYPE preconditioner_p_type
     113              : 
     114              : CONTAINS
     115              : 
     116              : ! **************************************************************************************************
     117              : 
     118              : ! **************************************************************************************************
     119              : !> \brief ...
     120              : !> \param preconditioner ...
     121              : !> \return ...
     122              : ! **************************************************************************************************
     123          444 :    FUNCTION preconditioner_in_use(preconditioner)
     124              :       TYPE(preconditioner_type)                          :: preconditioner
     125              :       LOGICAL                                            :: preconditioner_in_use
     126              : 
     127          444 :       preconditioner_in_use = .NOT. (preconditioner%in_use == 0)
     128          444 :    END FUNCTION preconditioner_in_use
     129              : 
     130              : ! **************************************************************************************************
     131              : !> \brief ...
     132              : !> \param preconditioner_env ...
     133              : !> \param para_env ...
     134              : !> \param blacs_env ...
     135              : ! **************************************************************************************************
     136         7709 :    SUBROUTINE init_preconditioner(preconditioner_env, para_env, blacs_env)
     137              : 
     138              :       TYPE(preconditioner_type)                          :: preconditioner_env
     139              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     140              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     141              : 
     142         7709 :       NULLIFY (preconditioner_env%sparse_matrix)
     143         7709 :       NULLIFY (preconditioner_env%fm)
     144         7709 :       NULLIFY (preconditioner_env%complex_fm)
     145         7709 :       NULLIFY (preconditioner_env%occ_rotation_complex)
     146         7709 :       preconditioner_env%owns_complex_fm = .TRUE.
     147         7709 :       NULLIFY (preconditioner_env%base_fm)
     148         7709 :       NULLIFY (preconditioner_env%dbcsr_matrix)
     149         7709 :       NULLIFY (preconditioner_env%base_dbcsr_matrix)
     150         7709 :       NULLIFY (preconditioner_env%occ_rotation_dbcsr)
     151         7709 :       NULLIFY (preconditioner_env%occ_rotation)
     152         7709 :       NULLIFY (preconditioner_env%occ_evals)
     153         7709 :       NULLIFY (preconditioner_env%full_evals)
     154         7709 :       NULLIFY (preconditioner_env%inverse_history)
     155         7709 :       NULLIFY (preconditioner_env%max_ev_vector)
     156         7709 :       NULLIFY (preconditioner_env%min_ev_vector)
     157         7709 :       preconditioner_env%solver = ot_precond_solver_default
     158         7709 :       preconditioner_env%para_env => para_env
     159         7709 :       preconditioner_env%ctxt => blacs_env
     160              :       !inverse is used for filtering in update set it to something huge to
     161              :       ! avoid filtering if the information is not available
     162         7709 :       preconditioner_env%condition_num = -1.0_dp
     163         7709 :       preconditioner_env%polynomial_degree = 8
     164         7709 :       preconditioner_env%polynomial_min = -1.0_dp
     165         7709 :       preconditioner_env%polynomial_max = -1.0_dp
     166         7709 :       preconditioner_env%spectral_rank = 0
     167         7709 :       preconditioner_env%spectral_max_rank = 48
     168         7709 :       preconditioner_env%spectral_reference = 0.0_dp
     169         7709 :       preconditioner_env%spectral_window = 1.0_dp
     170         7709 :       preconditioner_env%spectral_base_scale = 1.0_dp
     171         7709 :       preconditioner_env%low_rank_base = ot_low_rank_base_overlap
     172         7709 :       preconditioner_env%lattice_fft_mode = ot_lattice_fft_off
     173         7709 :       preconditioner_env%lattice_fft_active = .FALSE.
     174        30836 :       preconditioner_env%lattice_dims = [1, 1, 1]
     175         7709 :       preconditioner_env%lattice_block_size = 0
     176         7709 :       preconditioner_env%lattice_local_cells = 0
     177         7709 :       preconditioner_env%lattice_projection_error = 0.0_dp
     178         7709 :       preconditioner_env%lattice_corrected_projection_error = 0.0_dp
     179         7709 :       preconditioner_env%lattice_cost_ratio = 0.0_dp
     180         7709 :       preconditioner_env%lattice_storage_ratio = 0.0_dp
     181         7709 :       preconditioner_env%ihistory = 0
     182              : 
     183         7709 :       CALL preconditioner_env%para_env%retain()
     184         7709 :       CALL preconditioner_env%ctxt%retain()
     185              : 
     186         7709 :    END SUBROUTINE init_preconditioner
     187              : 
     188              : ! **************************************************************************************************
     189              : !> \brief ...
     190              : !> \param preconditioner_env ...
     191              : ! **************************************************************************************************
     192         7709 :    SUBROUTINE destroy_preconditioner(preconditioner_env)
     193              : 
     194              :       TYPE(preconditioner_type)                          :: preconditioner_env
     195              : 
     196              :       CHARACTER(len=*), PARAMETER :: routineN = 'destroy_preconditioner'
     197              : 
     198              :       INTEGER                                            :: handle, i
     199              : 
     200         7709 :       CALL timeset(routineN, handle)
     201              : 
     202         7709 :       IF (ASSOCIATED(preconditioner_env%sparse_matrix)) THEN
     203           12 :          CALL dbcsr_deallocate_matrix(preconditioner_env%sparse_matrix)
     204           12 :          NULLIFY (preconditioner_env%sparse_matrix)
     205              :       END IF
     206              : 
     207         7709 :       IF (ASSOCIATED(preconditioner_env%fm)) THEN
     208         1736 :          CALL cp_fm_release(preconditioner_env%fm)
     209         1736 :          DEALLOCATE (preconditioner_env%fm)
     210              :          NULLIFY (preconditioner_env%fm)
     211              :       END IF
     212         7709 :       IF (preconditioner_env%owns_complex_fm) THEN
     213         7699 :          IF (ASSOCIATED(preconditioner_env%complex_fm)) THEN
     214          565 :             CALL cp_cfm_release(preconditioner_env%complex_fm)
     215          565 :             DEALLOCATE (preconditioner_env%complex_fm)
     216              :          END IF
     217              :       END IF
     218         7709 :       NULLIFY (preconditioner_env%complex_fm)
     219         7709 :       preconditioner_env%owns_complex_fm = .TRUE.
     220         7709 :       IF (ASSOCIATED(preconditioner_env%occ_rotation_complex)) THEN
     221           60 :          CALL cp_cfm_release(preconditioner_env%occ_rotation_complex)
     222           60 :          DEALLOCATE (preconditioner_env%occ_rotation_complex)
     223              :          NULLIFY (preconditioner_env%occ_rotation_complex)
     224              :       END IF
     225         7709 :       IF (ASSOCIATED(preconditioner_env%base_fm)) THEN
     226            0 :          CALL cp_fm_release(preconditioner_env%base_fm)
     227            0 :          DEALLOCATE (preconditioner_env%base_fm)
     228              :          NULLIFY (preconditioner_env%base_fm)
     229              :       END IF
     230         7709 :       IF (ASSOCIATED(preconditioner_env%dbcsr_matrix)) THEN
     231         4820 :          CALL dbcsr_release_p(preconditioner_env%dbcsr_matrix)
     232              :       END IF
     233         7709 :       IF (ASSOCIATED(preconditioner_env%base_dbcsr_matrix)) THEN
     234            4 :          CALL dbcsr_release_p(preconditioner_env%base_dbcsr_matrix)
     235              :       END IF
     236         7709 :       IF (ASSOCIATED(preconditioner_env%occ_rotation_dbcsr)) THEN
     237            6 :          CALL dbcsr_release_p(preconditioner_env%occ_rotation_dbcsr)
     238              :       END IF
     239         7709 :       IF (ASSOCIATED(preconditioner_env%occ_rotation)) THEN
     240           10 :          CALL cp_fm_release(preconditioner_env%occ_rotation)
     241           10 :          DEALLOCATE (preconditioner_env%occ_rotation)
     242              :          NULLIFY (preconditioner_env%occ_rotation)
     243              :       END IF
     244         7709 :       IF (ASSOCIATED(preconditioner_env%max_ev_vector)) THEN
     245         2075 :          CALL dbcsr_release_p(preconditioner_env%max_ev_vector)
     246              :       END IF
     247         7709 :       IF (ASSOCIATED(preconditioner_env%min_ev_vector)) THEN
     248         2075 :          CALL dbcsr_release_p(preconditioner_env%min_ev_vector)
     249              :       END IF
     250         7709 :       IF (ASSOCIATED(preconditioner_env%occ_evals)) THEN
     251         4053 :          DEALLOCATE (preconditioner_env%occ_evals)
     252              :       END IF
     253         7709 :       IF (ASSOCIATED(preconditioner_env%full_evals)) THEN
     254         4053 :          DEALLOCATE (preconditioner_env%full_evals)
     255              :       END IF
     256         7709 :       IF (ALLOCATED(preconditioner_env%lattice_inverse_k)) THEN
     257           12 :          DEALLOCATE (preconditioner_env%lattice_inverse_k)
     258              :       END IF
     259         7709 :       IF (ALLOCATED(preconditioner_env%lattice_state_inverse_k)) THEN
     260            2 :          DEALLOCATE (preconditioner_env%lattice_state_inverse_k)
     261              :       END IF
     262         7709 :       IF (ALLOCATED(preconditioner_env%lattice_occ_vectors)) THEN
     263            2 :          DEALLOCATE (preconditioner_env%lattice_occ_vectors)
     264              :       END IF
     265         7709 :       IF (ALLOCATED(preconditioner_env%lattice_occ_dual)) THEN
     266            2 :          DEALLOCATE (preconditioner_env%lattice_occ_dual)
     267              :       END IF
     268         7709 :       IF (ALLOCATED(preconditioner_env%lattice_local_indices)) THEN
     269            2 :          DEALLOCATE (preconditioner_env%lattice_local_indices)
     270              :       END IF
     271         7709 :       IF (ALLOCATED(preconditioner_env%lattice_local_operator)) THEN
     272            2 :          DEALLOCATE (preconditioner_env%lattice_local_operator)
     273              :       END IF
     274         7709 :       IF (ALLOCATED(preconditioner_env%lattice_local_coarse_inverse)) THEN
     275            2 :          DEALLOCATE (preconditioner_env%lattice_local_coarse_inverse)
     276              :       END IF
     277         7709 :       IF (ALLOCATED(preconditioner_env%lattice_state_local_indices)) THEN
     278            2 :          DEALLOCATE (preconditioner_env%lattice_state_local_indices)
     279              :       END IF
     280         7709 :       IF (ALLOCATED(preconditioner_env%lattice_state_local_operator)) THEN
     281            2 :          DEALLOCATE (preconditioner_env%lattice_state_local_operator)
     282              :       END IF
     283         7709 :       IF (ALLOCATED(preconditioner_env%lattice_state_local_coarse_inverse)) THEN
     284            2 :          DEALLOCATE (preconditioner_env%lattice_state_local_coarse_inverse)
     285              :       END IF
     286         7709 :       CALL release_lattice_rhs_distribution(preconditioner_env%lattice_rhs_distribution)
     287         7709 :       IF (ASSOCIATED(preconditioner_env%inverse_history)) THEN
     288            0 :          DO i = 1, SIZE(preconditioner_env%inverse_history)
     289            0 :             CALL dbcsr_release_p(preconditioner_env%inverse_history(i)%matrix)
     290              :          END DO
     291            0 :          DEALLOCATE (preconditioner_env%inverse_history)
     292              :       END IF
     293         7709 :       CALL mp_para_env_release(preconditioner_env%para_env)
     294         7709 :       CALL cp_blacs_env_release(preconditioner_env%ctxt)
     295              : 
     296         7709 :       preconditioner_env%in_use = 0
     297         7709 :       preconditioner_env%cholesky_use = cholesky_reduce
     298              : 
     299         7709 :       CALL timestop(handle)
     300              : 
     301         7709 :    END SUBROUTINE destroy_preconditioner
     302              : 
     303              : ! **************************************************************************************************
     304              : !> \brief Release a cached lattice right-hand-side communication map.
     305              : !> \param distribution ...
     306              : ! **************************************************************************************************
     307         7723 :    SUBROUTINE release_lattice_rhs_distribution(distribution)
     308              : 
     309              :       TYPE(lattice_rhs_distribution_type), INTENT(INOUT) :: distribution
     310              : 
     311         7723 :       IF (ALLOCATED(distribution%global_columns)) DEALLOCATE (distribution%global_columns)
     312         7723 :       IF (ALLOCATED(distribution%local_rows)) DEALLOCATE (distribution%local_rows)
     313         7723 :       IF (ALLOCATED(distribution%local_columns)) DEALLOCATE (distribution%local_columns)
     314         7723 :       IF (ALLOCATED(distribution%send_counts)) DEALLOCATE (distribution%send_counts)
     315         7723 :       IF (ALLOCATED(distribution%send_displacements)) DEALLOCATE (distribution%send_displacements)
     316         7723 :       IF (ALLOCATED(distribution%recv_counts)) DEALLOCATE (distribution%recv_counts)
     317         7723 :       IF (ALLOCATED(distribution%recv_displacements)) DEALLOCATE (distribution%recv_displacements)
     318         7723 :       IF (ALLOCATED(distribution%send_local_rows)) DEALLOCATE (distribution%send_local_rows)
     319         7723 :       IF (ALLOCATED(distribution%send_local_columns)) DEALLOCATE (distribution%send_local_columns)
     320         7723 :       IF (ALLOCATED(distribution%recv_global_rows)) DEALLOCATE (distribution%recv_global_rows)
     321         7723 :       IF (ALLOCATED(distribution%recv_global_columns)) DEALLOCATE (distribution%recv_global_columns)
     322         7723 :       distribution%nrow_global = 0
     323         7723 :       distribution%ncol_global = 0
     324         7723 :       distribution%nrow_local = 0
     325         7723 :       distribution%ncol_local = 0
     326         7723 :       distribution%num_pe = 0
     327              : 
     328         7723 :    END SUBROUTINE release_lattice_rhs_distribution
     329              : 
     330            0 : END MODULE preconditioner_types
        

Generated by: LCOV version 2.0-1