LCOV - code coverage report
Current view: top level - src - qs_density_mixing_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:cccd2f3) Lines: 91.9 % 284 261
Test Date: 2026-05-06 07:07:47 Functions: 50.0 % 6 3

            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 module that contains the definitions of the scf types
      10              : !> \par History
      11              : !>      02.2003 created [fawzi]
      12              : !> \author fawzi
      13              : ! **************************************************************************************************
      14              : MODULE qs_density_mixing_types
      15              : #if defined(__TBLITE)
      16              :    USE tblite_scf, ONLY: mixer_type
      17              : #endif
      18              :    USE ao_util, ONLY: exp_radius
      19              :    USE input_constants, ONLY: broy_mix, &
      20              :                               direct_p_mix, &
      21              :                               gaussian, &
      22              :                               kerker_mix, &
      23              :                               multisec_mix, &
      24              :                               no_mix, &
      25              :                               pulay_mix
      26              :    USE input_keyword_types, ONLY: keyword_create, &
      27              :                                   keyword_release, &
      28              :                                   keyword_type
      29              :    USE input_section_types, ONLY: section_add_keyword, &
      30              :                                   section_create, &
      31              :                                   section_type, &
      32              :                                   section_vals_type, &
      33              :                                   section_vals_val_get
      34              :    USE input_val_types, ONLY: real_t
      35              :    USE kinds, ONLY: default_string_length, &
      36              :                     dp
      37              :    USE qs_rho_atom_types, ONLY: rho_atom_coeff
      38              :    USE string_utilities, ONLY: s2a
      39              : #include "./base/base_uses.f90"
      40              : 
      41              :    IMPLICIT NONE
      42              :    PRIVATE
      43              : 
      44              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      45              : 
      46              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_density_mixing_types'
      47              : 
      48              :    INTEGER, PARAMETER, PUBLIC :: no_mixing_nr = 0, direct_mixing_nr = 1, &
      49              :                                  gspace_mixing_nr = 2, pulay_mixing_nr = 3, &
      50              :                                  broyden_mixing_nr = 4, &
      51              :                                  multisecant_mixing_nr = 6
      52              :    PUBLIC :: cp_1d_z_p_type, mixing_storage_create, mixing_storage_type, mixing_storage_release, create_mixing_section
      53              : 
      54              :    TYPE cp_1d_z_p_type
      55              :       COMPLEX(dp), DIMENSION(:), POINTER :: cc => NULL()
      56              :    END TYPE cp_1d_z_p_type
      57              : 
      58              :    TYPE mixing_storage_type
      59              :    INTEGER                                           :: ig_max = -1, ncall = -1, ncall_p(2) = -1, nbuffer = -1, n_simple_mix = -1, &
      60              :                                                            nskip_mixing = -1, p_metric_method = -1
      61              :       INTEGER, POINTER, DIMENSION(:)                    :: ig_global_index => NULL()
      62              :       LOGICAL                                           :: gmix_p = .FALSE.
      63              :       LOGICAL, POINTER, DIMENSION(:)                    :: paw => NULL()
      64              :       CHARACTER(len=15)                                 :: iter_method = ""
      65              :       REAL(KIND=dp)                                     :: alpha = -1.0_dp, bconst = -1.0_dp, beta = -1.0_dp, broy_w0 = -1.0_dp, &
      66              :                                                            max_g2 = -1.0_dp, max_gvec_exp = -1.0_dp, pulay_alpha = -1.0_dp, &
      67              :                                                            pulay_beta = -1.0_dp, r_step = -1.0_dp, reg_par = -1.0_dp, &
      68              :                                                            sigma_max = -1.0_dp, wc = -1.0_dp, wmax = -1.0_dp
      69              :       ! Spin-channel-specific mixing parameters (for nspin==2, ispin=2 is the magnetization channel)
      70              :       REAL(KIND=dp)                                     :: alpha_mag = -1.0_dp, beta_mag = -1.0_dp
      71              :       REAL(KIND=dp), DIMENSION(:), POINTER              :: p_metric => NULL()
      72              :       REAL(KIND=dp), DIMENSION(:), POINTER              :: kerker_factor => NULL()
      73              :       REAL(KIND=dp), DIMENSION(:), POINTER              :: kerker_factor_mag => NULL()
      74              :       REAL(KIND=dp), DIMENSION(:), POINTER              :: special_metric => NULL()
      75              :       REAL(KIND=dp), DIMENSION(:, :), POINTER           :: weight => NULL()
      76              :       REAL(KIND=dp), DIMENSION(:, :), POINTER           :: norm_res_buffer => NULL()
      77              :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER        :: fmat => NULL(), gmat => NULL(), pulay_matrix => NULL(), smat => NULL()
      78              :       !
      79              :       INTEGER                                           :: nat_local = -1, max_shell = -1
      80              :       INTEGER                                           :: tb_scc_mixer_memory = 0, tb_scc_mixer_natom = 0, &
      81              :                                                            tb_scc_mixer_ns = 0, tb_scc_mixer_step = 0
      82              :       REAL(KIND=dp)                                     :: tb_scc_mixer_error = 0.0_dp
      83              :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER        :: acharge => NULL()
      84              :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER        :: dacharge => NULL()
      85              :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER        :: dfbroy => NULL()
      86              :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER        :: ubroy => NULL()
      87              :       REAL(KIND=dp), DIMENSION(:, :), POINTER           :: abroy => NULL()
      88              :       REAL(KIND=dp), DIMENSION(:), POINTER              :: wbroy => NULL()
      89              :       INTEGER, DIMENSION(:), POINTER                    :: atlist => NULL()
      90              :       !
      91              :       TYPE(cp_1d_z_p_type), DIMENSION(:), POINTER       :: last_res => NULL(), rhoin => NULL(), rhoin_old => NULL()
      92              :       TYPE(cp_1d_z_p_type), DIMENSION(:, :), POINTER    :: delta_res => NULL(), u_vec => NULL(), z_vec => NULL()
      93              :       TYPE(cp_1d_z_p_type), DIMENSION(:, :), POINTER    :: drho_buffer => NULL(), rhoin_buffer => NULL(), res_buffer => NULL()
      94              :       !
      95              :       TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER    :: cpc_h_lastres => NULL(), cpc_s_lastres => NULL()
      96              :       TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER    :: cpc_h_in => NULL(), cpc_s_in => NULL()
      97              :       TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER    :: cpc_h_old => NULL(), cpc_s_old => NULL()
      98              :       TYPE(rho_atom_coeff), DIMENSION(:, :, :), POINTER :: cpc_h_in_buffer => NULL(), cpc_s_in_buffer => NULL()
      99              :       TYPE(rho_atom_coeff), DIMENSION(:, :, :), POINTER :: cpc_h_res_buffer => NULL(), cpc_s_res_buffer => NULL()
     100              :       TYPE(rho_atom_coeff), DIMENSION(:, :, :), POINTER :: dcpc_h_in => NULL(), dcpc_s_in => NULL()
     101              : #if defined(__TBLITE)
     102              :       CLASS(mixer_type), ALLOCATABLE                    :: tb_scc_mixer
     103              : #endif
     104              :    END TYPE mixing_storage_type
     105              : 
     106              : CONTAINS
     107              : 
     108              : ! **************************************************************************************************
     109              : !> \brief creates a mixing_storage
     110              : !> \param mixing_store ...
     111              : !> \param mixing_section ...
     112              : !> \param mixing_method ...
     113              : !> \param ecut ...
     114              : !> \par History
     115              : !>      05.2009 created [MI]
     116              : !> \author [MI]
     117              : ! **************************************************************************************************
     118        28800 :    SUBROUTINE mixing_storage_create(mixing_store, mixing_section, mixing_method, ecut)
     119              :       TYPE(mixing_storage_type), INTENT(OUT)             :: mixing_store
     120              :       TYPE(section_vals_type), POINTER                   :: mixing_section
     121              :       INTEGER, INTENT(IN)                                :: mixing_method
     122              :       REAL(dp), INTENT(IN)                               :: ecut
     123              : 
     124              :       REAL(dp)                                           :: alpha, eps, gcut
     125              : 
     126         7200 :       mixing_store%nbuffer = 0
     127         7200 :       mixing_store%n_simple_mix = 0
     128         7200 :       mixing_store%ncall = 0
     129        21600 :       mixing_store%ncall_p = 0
     130         7200 :       mixing_store%alpha = 1.0_dp
     131         7200 :       mixing_store%pulay_beta = 1.0_dp
     132         7200 :       mixing_store%beta = 1.0_dp
     133         7200 :       mixing_store%alpha_mag = -1.0_dp
     134         7200 :       mixing_store%beta_mag = -1.0_dp
     135         7200 :       mixing_store%iter_method = "NoMix"
     136         7200 :       mixing_store%max_g2 = 2._dp*ecut
     137         7200 :       mixing_store%gmix_p = .FALSE.
     138         7200 :       mixing_store%tb_scc_mixer_error = 0.0_dp
     139              :       mixing_store%tb_scc_mixer_memory = 0
     140              :       mixing_store%tb_scc_mixer_natom = 0
     141              :       mixing_store%tb_scc_mixer_ns = 0
     142              :       mixing_store%tb_scc_mixer_step = 0
     143              : 
     144         7200 :       NULLIFY (mixing_store%p_metric)
     145         7200 :       NULLIFY (mixing_store%kerker_factor)
     146         7200 :       NULLIFY (mixing_store%kerker_factor_mag)
     147         7200 :       NULLIFY (mixing_store%special_metric)
     148         7200 :       NULLIFY (mixing_store%pulay_matrix)
     149         7200 :       NULLIFY (mixing_store%weight)
     150         7200 :       NULLIFY (mixing_store%fmat)
     151         7200 :       NULLIFY (mixing_store%gmat)
     152         7200 :       NULLIFY (mixing_store%smat)
     153         7200 :       NULLIFY (mixing_store%acharge)
     154         7200 :       NULLIFY (mixing_store%dacharge)
     155         7200 :       NULLIFY (mixing_store%dfbroy)
     156         7200 :       NULLIFY (mixing_store%ubroy)
     157         7200 :       NULLIFY (mixing_store%abroy)
     158         7200 :       NULLIFY (mixing_store%wbroy)
     159         7200 :       NULLIFY (mixing_store%atlist)
     160         7200 :       NULLIFY (mixing_store%last_res)
     161         7200 :       NULLIFY (mixing_store%rhoin)
     162         7200 :       NULLIFY (mixing_store%rhoin_old)
     163         7200 :       NULLIFY (mixing_store%delta_res)
     164         7200 :       NULLIFY (mixing_store%u_vec)
     165         7200 :       NULLIFY (mixing_store%z_vec)
     166         7200 :       NULLIFY (mixing_store%drho_buffer)
     167         7200 :       NULLIFY (mixing_store%rhoin_buffer)
     168         7200 :       NULLIFY (mixing_store%res_buffer)
     169         7200 :       NULLIFY (mixing_store%norm_res_buffer)
     170         7200 :       NULLIFY (mixing_store%ig_global_index)
     171         7200 :       NULLIFY (mixing_store%paw)
     172         7200 :       NULLIFY (mixing_store%cpc_h_in)
     173         7200 :       NULLIFY (mixing_store%cpc_s_in)
     174         7200 :       NULLIFY (mixing_store%cpc_h_old)
     175         7200 :       NULLIFY (mixing_store%cpc_s_old)
     176         7200 :       NULLIFY (mixing_store%dcpc_h_in)
     177         7200 :       NULLIFY (mixing_store%dcpc_s_in)
     178         7200 :       NULLIFY (mixing_store%cpc_h_lastres)
     179         7200 :       NULLIFY (mixing_store%cpc_s_lastres)
     180         7200 :       NULLIFY (mixing_store%cpc_h_in_buffer)
     181         7200 :       NULLIFY (mixing_store%cpc_s_in_buffer)
     182         7200 :       NULLIFY (mixing_store%cpc_h_res_buffer)
     183         7200 :       NULLIFY (mixing_store%cpc_s_res_buffer)
     184              : 
     185         7200 :       CALL section_vals_val_get(mixing_section, "ALPHA", r_val=mixing_store%alpha)
     186         7200 :       CALL section_vals_val_get(mixing_section, "BETA", r_val=mixing_store%beta)
     187         7200 :       CALL section_vals_val_get(mixing_section, "ALPHA_MAG", r_val=mixing_store%alpha_mag)
     188         7200 :       CALL section_vals_val_get(mixing_section, "BETA_MAG", r_val=mixing_store%beta_mag)
     189              :       ! Fall back to charge-channel values if magnetization parameters are not set
     190         7200 :       IF (mixing_store%alpha_mag < 0.0_dp) mixing_store%alpha_mag = mixing_store%alpha
     191         7200 :       IF (mixing_store%beta_mag < 0.0_dp) mixing_store%beta_mag = mixing_store%beta
     192         7200 :       CALL section_vals_val_get(mixing_section, "N_SIMPLE_MIX", i_val=mixing_store%n_simple_mix)
     193         7200 :       CALL section_vals_val_get(mixing_section, "NBUFFER", i_val=mixing_store%nbuffer)
     194         7200 :       CALL section_vals_val_get(mixing_section, "NSKIP", i_val=mixing_store%nskip_mixing)
     195         7200 :       CALL section_vals_val_get(mixing_section, "MAX_GVEC_EXP", r_val=mixing_store%max_gvec_exp)
     196         7200 :       CALL section_vals_val_get(mixing_section, "GMIX_P", l_val=mixing_store%gmix_p)
     197              : 
     198         7200 :       IF (mixing_store%max_gvec_exp > 0._dp) THEN
     199            0 :          alpha = 0.25_dp/mixing_store%max_gvec_exp
     200            0 :          eps = 1.e-4_dp
     201            0 :          gcut = exp_radius(3, alpha, eps, 1.0_dp)
     202            0 :          mixing_store%max_g2 = gcut*gcut
     203              :       END IF
     204              : 
     205         7210 :       SELECT CASE (mixing_method)
     206              :       CASE (gspace_mixing_nr)
     207           10 :          mixing_store%nbuffer = 1
     208              :       CASE (pulay_mixing_nr)
     209           36 :          CALL section_vals_val_get(mixing_section, "PULAY_ALPHA", r_val=mixing_store%pulay_alpha)
     210           36 :          CALL section_vals_val_get(mixing_section, "PULAY_BETA", r_val=mixing_store%pulay_beta)
     211              :       CASE (broyden_mixing_nr)
     212          600 :          CALL section_vals_val_get(mixing_section, "BROY_W0", r_val=mixing_store%broy_w0)
     213          600 :          mixing_store%bconst = 20.0_dp
     214              :       CASE (multisecant_mixing_nr)
     215            0 :          CALL section_vals_val_get(mixing_section, "REGULARIZATION", r_val=mixing_store%reg_par)
     216            0 :          CALL section_vals_val_get(mixing_section, "MAX_STEP", r_val=mixing_store%sigma_max)
     217         7200 :          CALL section_vals_val_get(mixing_section, "R_FACTOR", r_val=mixing_store%r_step)
     218              :       END SELECT
     219              : 
     220         7200 :    END SUBROUTINE mixing_storage_create
     221              : 
     222              : ! **************************************************************************************************
     223              : !> \brief releases a mixing_storage
     224              : !> \param mixing_store ...
     225              : !> \par History
     226              : !>      05.2009 created [MI]
     227              : !> \author [MI]
     228              : ! **************************************************************************************************
     229         7199 :    SUBROUTINE mixing_storage_release(mixing_store)
     230              :       TYPE(mixing_storage_type), INTENT(INOUT)           :: mixing_store
     231              : 
     232              :       INTEGER                                            :: i, j, k
     233              : 
     234         7199 :       IF (ASSOCIATED(mixing_store%kerker_factor)) THEN
     235          250 :          DEALLOCATE (mixing_store%kerker_factor)
     236              :       END IF
     237              : 
     238         7199 :       IF (ASSOCIATED(mixing_store%kerker_factor_mag)) THEN
     239          250 :          DEALLOCATE (mixing_store%kerker_factor_mag)
     240              :       END IF
     241              : 
     242         7199 :       IF (ASSOCIATED(mixing_store%special_metric)) THEN
     243          250 :          DEALLOCATE (mixing_store%special_metric)
     244              :       END IF
     245              : 
     246         7199 :       IF (ASSOCIATED(mixing_store%pulay_matrix)) THEN
     247           34 :          DEALLOCATE (mixing_store%pulay_matrix)
     248              :       END IF
     249              : 
     250         7199 :       IF (ASSOCIATED(mixing_store%rhoin_buffer)) THEN
     251           72 :          DO i = 1, SIZE(mixing_store%rhoin_buffer, 2)
     252          298 :             DO j = 1, SIZE(mixing_store%rhoin_buffer, 1)
     253          264 :                DEALLOCATE (mixing_store%rhoin_buffer(j, i)%cc)
     254              :             END DO
     255              :          END DO
     256           34 :          DEALLOCATE (mixing_store%rhoin_buffer)
     257              :       END IF
     258              : 
     259         7199 :       IF (ASSOCIATED(mixing_store%paw)) THEN
     260           16 :          DEALLOCATE (mixing_store%paw)
     261              :       END IF
     262         7199 :       IF (ASSOCIATED(mixing_store%cpc_h_in)) THEN
     263           38 :          DO j = 1, SIZE(mixing_store%cpc_h_in, 2)
     264          214 :          DO k = 1, SIZE(mixing_store%cpc_h_in, 1)
     265          198 :             IF (ASSOCIATED(mixing_store%cpc_h_in(k, j)%r_coef)) THEN
     266          106 :                DEALLOCATE (mixing_store%cpc_h_in(k, j)%r_coef)
     267          106 :                DEALLOCATE (mixing_store%cpc_s_in(k, j)%r_coef)
     268              :             END IF
     269              :          END DO
     270              :          END DO
     271           16 :          DEALLOCATE (mixing_store%cpc_h_in)
     272           16 :          DEALLOCATE (mixing_store%cpc_s_in)
     273              :       END IF
     274         7199 :       IF (ASSOCIATED(mixing_store%cpc_h_old)) THEN
     275           30 :          DO j = 1, SIZE(mixing_store%cpc_h_old, 2)
     276          174 :          DO k = 1, SIZE(mixing_store%cpc_h_old, 1)
     277          162 :             IF (ASSOCIATED(mixing_store%cpc_h_old(k, j)%r_coef)) THEN
     278          102 :                DEALLOCATE (mixing_store%cpc_h_old(k, j)%r_coef)
     279          102 :                DEALLOCATE (mixing_store%cpc_s_old(k, j)%r_coef)
     280              :             END IF
     281              :          END DO
     282              :          END DO
     283           12 :          DEALLOCATE (mixing_store%cpc_h_old)
     284           12 :          DEALLOCATE (mixing_store%cpc_s_old)
     285              :       END IF
     286         7199 :       IF (ASSOCIATED(mixing_store%cpc_h_in_buffer)) THEN
     287            4 :          DO i = 1, SIZE(mixing_store%cpc_h_in_buffer, 3)
     288           20 :          DO j = 1, SIZE(mixing_store%cpc_h_in_buffer, 2)
     289           98 :          DO k = 1, SIZE(mixing_store%cpc_h_in_buffer, 1)
     290           96 :             IF (ASSOCIATED(mixing_store%cpc_h_in_buffer(k, j, i)%r_coef)) THEN
     291           10 :                DEALLOCATE (mixing_store%cpc_h_in_buffer(k, j, i)%r_coef)
     292           10 :                DEALLOCATE (mixing_store%cpc_s_in_buffer(k, j, i)%r_coef)
     293              :             END IF
     294              :          END DO
     295              :          END DO
     296              :          END DO
     297            2 :          DEALLOCATE (mixing_store%cpc_h_in_buffer)
     298            2 :          DEALLOCATE (mixing_store%cpc_s_in_buffer)
     299              :       END IF
     300         7199 :       IF (ASSOCIATED(mixing_store%cpc_h_res_buffer)) THEN
     301            4 :          DO i = 1, SIZE(mixing_store%cpc_h_res_buffer, 3)
     302           20 :          DO j = 1, SIZE(mixing_store%cpc_h_res_buffer, 2)
     303           98 :          DO k = 1, SIZE(mixing_store%cpc_h_res_buffer, 1)
     304           96 :             IF (ASSOCIATED(mixing_store%cpc_h_res_buffer(k, j, i)%r_coef)) THEN
     305           10 :                DEALLOCATE (mixing_store%cpc_h_res_buffer(k, j, i)%r_coef)
     306           10 :                DEALLOCATE (mixing_store%cpc_s_res_buffer(k, j, i)%r_coef)
     307              :             END IF
     308              :          END DO
     309              :          END DO
     310              :          END DO
     311            2 :          DEALLOCATE (mixing_store%cpc_h_res_buffer)
     312            2 :          DEALLOCATE (mixing_store%cpc_s_res_buffer)
     313              :       END IF
     314              : 
     315         7199 :       IF (ASSOCIATED(mixing_store%dcpc_h_in)) THEN
     316           30 :          DO i = 1, SIZE(mixing_store%dcpc_h_in, 3)
     317          174 :          DO j = 1, SIZE(mixing_store%dcpc_h_in, 2)
     318         1266 :          DO k = 1, SIZE(mixing_store%dcpc_h_in, 1)
     319         1248 :             IF (ASSOCIATED(mixing_store%dcpc_h_in(k, j, i)%r_coef)) THEN
     320          810 :                DEALLOCATE (mixing_store%dcpc_h_in(k, j, i)%r_coef)
     321          810 :                DEALLOCATE (mixing_store%dcpc_s_in(k, j, i)%r_coef)
     322              :             END IF
     323              :          END DO
     324              :          END DO
     325              :          END DO
     326           12 :          DEALLOCATE (mixing_store%dcpc_h_in)
     327           12 :          DEALLOCATE (mixing_store%dcpc_s_in)
     328              :       END IF
     329         7199 :       IF (ASSOCIATED(mixing_store%cpc_h_lastres)) THEN
     330           30 :          DO j = 1, SIZE(mixing_store%cpc_h_lastres, 2)
     331          174 :          DO k = 1, SIZE(mixing_store%cpc_h_lastres, 1)
     332          162 :             IF (ASSOCIATED(mixing_store%cpc_h_lastres(k, j)%r_coef)) THEN
     333          102 :                DEALLOCATE (mixing_store%cpc_h_lastres(k, j)%r_coef)
     334          102 :                DEALLOCATE (mixing_store%cpc_s_lastres(k, j)%r_coef)
     335              :             END IF
     336              :          END DO
     337              :          END DO
     338           12 :          DEALLOCATE (mixing_store%cpc_h_lastres)
     339           12 :          DEALLOCATE (mixing_store%cpc_s_lastres)
     340              :       END IF
     341              : 
     342         7199 :       IF (ASSOCIATED(mixing_store%res_buffer)) THEN
     343          520 :          DO i = 1, SIZE(mixing_store%res_buffer, 2)
     344         2720 :             DO j = 1, SIZE(mixing_store%res_buffer, 1)
     345         2480 :                DEALLOCATE (mixing_store%res_buffer(j, i)%cc)
     346              :             END DO
     347              :          END DO
     348          240 :          DEALLOCATE (mixing_store%res_buffer)
     349              :       END IF
     350              : 
     351         7199 :       IF (ASSOCIATED(mixing_store%norm_res_buffer)) THEN
     352            0 :          DEALLOCATE (mixing_store%norm_res_buffer)
     353              :       END IF
     354              : 
     355         7199 :       IF (ASSOCIATED(mixing_store%ig_global_index)) THEN
     356            0 :          DEALLOCATE (mixing_store%ig_global_index)
     357              :       END IF
     358              : 
     359         7199 :       IF (ASSOCIATED(mixing_store%drho_buffer)) THEN
     360          448 :          DO i = 1, SIZE(mixing_store%drho_buffer, 2)
     361         2422 :             DO j = 1, SIZE(mixing_store%drho_buffer, 1)
     362         2216 :                DEALLOCATE (mixing_store%drho_buffer(j, i)%cc)
     363              :             END DO
     364              :          END DO
     365          206 :          DEALLOCATE (mixing_store%drho_buffer)
     366              :       END IF
     367              : 
     368         7199 :       IF (ASSOCIATED(mixing_store%last_res)) THEN
     369          448 :          DO i = 1, SIZE(mixing_store%last_res)
     370          448 :             DEALLOCATE (mixing_store%last_res(i)%cc)
     371              :          END DO
     372          206 :          DEALLOCATE (mixing_store%last_res)
     373              :       END IF
     374              : 
     375         7199 :       IF (ASSOCIATED(mixing_store%rhoin)) THEN
     376          540 :          DO i = 1, SIZE(mixing_store%rhoin)
     377          540 :             DEALLOCATE (mixing_store%rhoin(i)%cc)
     378              :          END DO
     379          250 :          DEALLOCATE (mixing_store%rhoin)
     380              :       END IF
     381              : 
     382         7199 :       IF (ASSOCIATED(mixing_store%rhoin_old)) THEN
     383          448 :          DO i = 1, SIZE(mixing_store%rhoin_old)
     384          448 :             DEALLOCATE (mixing_store%rhoin_old(i)%cc)
     385              :          END DO
     386          206 :          DEALLOCATE (mixing_store%rhoin_old)
     387              :       END IF
     388              : 
     389         7199 :       IF (ASSOCIATED(mixing_store%p_metric)) THEN
     390          206 :          DEALLOCATE (mixing_store%p_metric)
     391              :       END IF
     392              : 
     393         7199 :       IF (ASSOCIATED(mixing_store%weight)) THEN
     394            0 :          DEALLOCATE (mixing_store%weight)
     395              :       END IF
     396              : 
     397         7199 :       IF (ASSOCIATED(mixing_store%fmat)) THEN
     398            0 :          DEALLOCATE (mixing_store%fmat)
     399              :       END IF
     400              : 
     401         7199 :       IF (ASSOCIATED(mixing_store%acharge)) THEN
     402           26 :          DEALLOCATE (mixing_store%acharge)
     403              :       END IF
     404         7199 :       IF (ASSOCIATED(mixing_store%dacharge)) THEN
     405           26 :          DEALLOCATE (mixing_store%dacharge)
     406              :       END IF
     407         7199 :       IF (ASSOCIATED(mixing_store%dfbroy)) THEN
     408           26 :          DEALLOCATE (mixing_store%dfbroy)
     409              :       END IF
     410         7199 :       IF (ASSOCIATED(mixing_store%ubroy)) THEN
     411           26 :          DEALLOCATE (mixing_store%ubroy)
     412              :       END IF
     413         7199 :       IF (ASSOCIATED(mixing_store%abroy)) THEN
     414           26 :          DEALLOCATE (mixing_store%abroy)
     415              :       END IF
     416         7199 :       IF (ASSOCIATED(mixing_store%wbroy)) THEN
     417           26 :          DEALLOCATE (mixing_store%wbroy)
     418              :       END IF
     419         7199 :       IF (ASSOCIATED(mixing_store%atlist)) THEN
     420           26 :          DEALLOCATE (mixing_store%atlist)
     421              :       END IF
     422              : #if defined(__TBLITE)
     423         7199 :       IF (ALLOCATED(mixing_store%tb_scc_mixer)) THEN
     424            4 :          DEALLOCATE (mixing_store%tb_scc_mixer)
     425              :       END IF
     426              : #endif
     427              : 
     428         7199 :       IF (ASSOCIATED(mixing_store%delta_res)) THEN
     429            0 :          DO i = 1, SIZE(mixing_store%delta_res, 2)
     430            0 :             DO j = 1, SIZE(mixing_store%delta_res, 1)
     431            0 :                DEALLOCATE (mixing_store%delta_res(j, i)%cc)
     432              :             END DO
     433              :          END DO
     434            0 :          DEALLOCATE (mixing_store%delta_res)
     435              :       END IF
     436              : 
     437         7199 :       IF (ASSOCIATED(mixing_store%u_vec)) THEN
     438            0 :          DO i = 1, SIZE(mixing_store%u_vec, 2)
     439            0 :             DO j = 1, SIZE(mixing_store%u_vec, 1)
     440            0 :                DEALLOCATE (mixing_store%u_vec(j, i)%cc)
     441              :             END DO
     442              :          END DO
     443            0 :          DEALLOCATE (mixing_store%u_vec)
     444              :       END IF
     445              : 
     446         7199 :       IF (ASSOCIATED(mixing_store%z_vec)) THEN
     447            0 :          DO i = 1, SIZE(mixing_store%z_vec, 2)
     448            0 :             DO j = 1, SIZE(mixing_store%z_vec, 1)
     449            0 :                DEALLOCATE (mixing_store%z_vec(j, i)%cc)
     450              :             END DO
     451              :          END DO
     452            0 :          DEALLOCATE (mixing_store%z_vec)
     453              :       END IF
     454              : 
     455         7199 :    END SUBROUTINE mixing_storage_release
     456              : 
     457              : ! **************************************************************************************************
     458              : !> \brief      Create CP2K input section for the mixing of the density matrix to
     459              : !>             be used only with diagonalization methods, i.e. not with OT
     460              : !> \param section ...
     461              : !> \param ls_scf ...
     462              : !> \date       20.02.2009
     463              : !> \par History
     464              : !>      02.2015 moved here from input_cp2k_dft.F, modified for use in LS SCF
     465              : !>              [Patrick Seewald]
     466              : !> \author     MI
     467              : !> \version    1.0
     468              : ! **************************************************************************************************
     469        65952 :    SUBROUTINE create_mixing_section(section, ls_scf)
     470              : 
     471              :       TYPE(section_type), POINTER                        :: section
     472              :       LOGICAL, INTENT(IN), OPTIONAL                      :: ls_scf
     473              : 
     474              :       CHARACTER(LEN=default_string_length)               :: section_name
     475              :       INTEGER                                            :: default_mix
     476              :       LOGICAL                                            :: ls
     477              :       TYPE(keyword_type), POINTER                        :: keyword
     478              : 
     479        65952 :       CPASSERT(.NOT. ASSOCIATED(section))
     480              : 
     481        65952 :       IF (PRESENT(ls_scf)) THEN
     482        19662 :          IF (ls_scf) THEN
     483              :             ls = .TRUE.
     484              :          ELSE
     485              :             ls = .FALSE.
     486              :          END IF
     487              :       ELSE
     488              :          ls = .FALSE.
     489              :       END IF
     490              : 
     491              :       IF (ls) THEN
     492         9839 :          section_name = "RHO_MIXING"
     493              :       ELSE
     494        56113 :          section_name = "MIXING"
     495              :       END IF
     496              : 
     497              :       CALL section_create(section, __LOCATION__, &
     498              :                           name=section_name, &
     499              :                           description="Define type and parameters for mixing "// &
     500              :                           "procedures to be applied to the density matrix. Normally, "// &
     501              :                           "only one type of mixing method should be accepted. The mixing "// &
     502              :                           "procedures activated by this section are only active for diagonalization "// &
     503              :                           "methods and linear scaling SCF, i.e. not with minimization methods based "// &
     504              :                           "on OT.", &
     505              :                           n_keywords=16, &
     506              :                           n_subsections=0, &
     507        65952 :                           repeats=.FALSE.)
     508              : 
     509        65952 :       NULLIFY (keyword)
     510              : 
     511              :       CALL keyword_create(keyword, __LOCATION__, &
     512              :                           name="_SECTION_PARAMETERS_", &
     513              :                           description="Controls the activation of the mixing procedure", &
     514              :                           usage="&MIXING ON", &
     515              :                           default_l_val=.TRUE., &
     516        65952 :                           lone_keyword_l_val=.TRUE.)
     517        65952 :       CALL section_add_keyword(section, keyword)
     518        65952 :       CALL keyword_release(keyword)
     519              : 
     520        65952 :       IF (.NOT. ls) THEN
     521        56113 :          default_mix = direct_p_mix
     522              :       ELSE
     523         9839 :          default_mix = broy_mix
     524              :       END IF
     525              : 
     526              :       CALL keyword_create(keyword, __LOCATION__, &
     527              :                           name="METHOD", &
     528              :                           description="Mixing method to be applied", &
     529              :                           repeats=.FALSE., &
     530              :                           usage="METHOD KERKER_MIXING", &
     531              :                           default_i_val=default_mix, &
     532              :                           enum_c_vals=s2a("NONE", &
     533              :                                           "DIRECT_P_MIXING", &
     534              :                                           "KERKER_MIXING", &
     535              :                                           "PULAY_MIXING", &
     536              :                                           "BROYDEN_MIXING", &
     537              :                                           "MULTISECANT_MIXING"), &
     538              :                           enum_i_vals=[no_mix, direct_p_mix, kerker_mix, pulay_mix, broy_mix, &
     539              :                                        multisec_mix], &
     540              :                           enum_desc=s2a("No mixing is applied", &
     541              :                                         "Direct mixing of new and old density matrices", &
     542              :                                         "Mixing of the potential in reciprocal space using the Kerker damping", &
     543              :                                         "Pulay mixing", "Broyden mixing", &
     544        65952 :                                         "Multisecant scheme for mixing"))
     545              : 
     546        65952 :       CALL section_add_keyword(section, keyword)
     547        65952 :       CALL keyword_release(keyword)
     548              : 
     549              :       CALL keyword_create(keyword, __LOCATION__, &
     550              :                           name="ALPHA", &
     551              :                           description="Fraction of new density to be included", &
     552              :                           repeats=.FALSE., &
     553              :                           n_var=1, &
     554              :                           type_of_var=real_t, &
     555              :                           default_r_val=0.4_dp, &
     556        65952 :                           usage="ALPHA 0.2")
     557        65952 :       CALL section_add_keyword(section, keyword)
     558        65952 :       CALL keyword_release(keyword)
     559              : 
     560              :       CALL keyword_create(keyword, __LOCATION__, &
     561              :                           name="ALPHA_MAG", &
     562              :                           description="Fraction of new magnetization density to be included "// &
     563              :                           "(for spin-polarized calculations, ispin=2 channel after rho_total/m transform). "// &
     564              :                           "A negative value (default) means: use the same value as ALPHA. "// &
     565              :                           "For magnetic transition-metal systems, a larger value (e.g. 0.8-1.6) "// &
     566              :                           "than ALPHA often improves convergence.", &
     567              :                           repeats=.FALSE., &
     568              :                           n_var=1, &
     569              :                           type_of_var=real_t, &
     570              :                           default_r_val=-1.0_dp, &
     571        65952 :                           usage="ALPHA_MAG 0.8")
     572        65952 :       CALL section_add_keyword(section, keyword)
     573        65952 :       CALL keyword_release(keyword)
     574              : 
     575              :       CALL keyword_create(keyword, __LOCATION__, &
     576              :                           name="BETA", &
     577              :                           description="Denominator parameter in Kerker damping "// &
     578              :                           "introduced to suppress charge sloshing: "// &
     579              :                           "rho_mix(g) = rho_in(g) + alpha*g^2/(g^2 + beta^2)*(rho_out(g)-rho_in(g))", &
     580              :                           repeats=.FALSE., &
     581              :                           n_var=1, &
     582              :                           type_of_var=real_t, &
     583              :                           default_r_val=0.5_dp, &
     584              :                           unit_str="bohr^-1", &
     585        65952 :                           usage="BETA 1.5")
     586        65952 :       CALL section_add_keyword(section, keyword)
     587        65952 :       CALL keyword_release(keyword)
     588              : 
     589              :       CALL keyword_create(keyword, __LOCATION__, &
     590              :                           name="BETA_MAG", &
     591              :                           description="Kerker damping parameter for the magnetization channel "// &
     592              :                           "(for spin-polarized calculations). A negative value (default) means: "// &
     593              :                           "use the same value as BETA. Set to 0.0 to disable Kerker screening "// &
     594              :                           "on the magnetization density, which avoids suppression of long-range "// &
     595              :                           "magnetic order formation in transition-metal systems.", &
     596              :                           repeats=.FALSE., &
     597              :                           n_var=1, &
     598              :                           type_of_var=real_t, &
     599              :                           default_r_val=-1.0_dp, &
     600              :                           unit_str="bohr^-1", &
     601        65952 :                           usage="BETA_MAG 0.0")
     602        65952 :       CALL section_add_keyword(section, keyword)
     603        65952 :       CALL keyword_release(keyword)
     604              : 
     605              :       CALL keyword_create(keyword, __LOCATION__, &
     606              :                           name="PULAY_ALPHA", &
     607              :                           description="Fraction of new density to be added to the Pulay expansion", &
     608              :                           repeats=.FALSE., &
     609              :                           n_var=1, &
     610              :                           type_of_var=real_t, &
     611              :                           default_r_val=0.0_dp, &
     612        65952 :                           usage="PULAY_ALPHA 0.2")
     613        65952 :       CALL section_add_keyword(section, keyword)
     614        65952 :       CALL keyword_release(keyword)
     615              : 
     616              :       CALL keyword_create(keyword, __LOCATION__, &
     617              :                           name="PULAY_BETA", &
     618              :                           description="Fraction of residual contribution to be added to Pulay expansion", &
     619              :                           repeats=.FALSE., &
     620              :                           n_var=1, &
     621              :                           type_of_var=real_t, &
     622              :                           default_r_val=1.0_dp, &
     623        65952 :                           usage="PULAY_BETA 0.2")
     624        65952 :       CALL section_add_keyword(section, keyword)
     625        65952 :       CALL keyword_release(keyword)
     626              : 
     627              :       CALL keyword_create(keyword, __LOCATION__, name="NMIXING", &
     628              :                           description="Minimal number of density mixing (should be greater than 0), "// &
     629              :                           "before starting DIIS", &
     630        65952 :                           usage="NMIXING 1", default_i_val=2)
     631        65952 :       CALL section_add_keyword(section, keyword)
     632        65952 :       CALL keyword_release(keyword)
     633              : 
     634              :       CALL keyword_create(keyword, __LOCATION__, name="NBUFFER", &
     635              :                           variants=s2a("NPULAY", "NBROYDEN", "NMULTISECANT"), &
     636              :                           description="Number of previous steps stored for the actual mixing scheme", &
     637        65952 :                           usage="NBUFFER 2", default_i_val=4)
     638        65952 :       CALL section_add_keyword(section, keyword)
     639        65952 :       CALL keyword_release(keyword)
     640              : 
     641              :       CALL keyword_create(keyword, __LOCATION__, &
     642              :                           name="BROY_W0", &
     643              :                           description=" w0 parameter used in Broyden mixing", &
     644              :                           repeats=.FALSE., &
     645              :                           n_var=1, &
     646              :                           type_of_var=real_t, &
     647              :                           default_r_val=0.01_dp, &
     648        65952 :                           usage="BROY_W0 0.03")
     649        65952 :       CALL section_add_keyword(section, keyword)
     650        65952 :       CALL keyword_release(keyword)
     651              : 
     652              :       CALL keyword_create(keyword, __LOCATION__, &
     653              :                           name="BROY_WREF", &
     654              :                           description="", &
     655              :                           repeats=.FALSE., &
     656              :                           n_var=1, &
     657              :                           type_of_var=real_t, &
     658              :                           default_r_val=100.0_dp, &
     659        65952 :                           usage="BROY_WREF 0.2")
     660        65952 :       CALL section_add_keyword(section, keyword)
     661        65952 :       CALL keyword_release(keyword)
     662              : 
     663              :       CALL keyword_create(keyword, __LOCATION__, &
     664              :                           name="BROY_WMAX", &
     665              :                           description="", &
     666              :                           repeats=.FALSE., &
     667              :                           n_var=1, &
     668              :                           type_of_var=real_t, &
     669              :                           default_r_val=30.0_dp, &
     670        65952 :                           usage="BROY_WMAX 10.0")
     671        65952 :       CALL section_add_keyword(section, keyword)
     672        65952 :       CALL keyword_release(keyword)
     673              : 
     674              :       CALL keyword_create(keyword, __LOCATION__, &
     675              :                           name="REGULARIZATION", &
     676              :                           description="Regularization parameter to stabilize "// &
     677              :                           "the inversion of the residual matrix {Yn^t Yn} in the "// &
     678              :                           "multisecant mixing scheme (noise)", &
     679              :                           repeats=.FALSE., &
     680              :                           n_var=1, &
     681              :                           type_of_var=real_t, &
     682              :                           default_r_val=0.00001_dp, &
     683        65952 :                           usage="REGULARIZATION 0.000001")
     684        65952 :       CALL section_add_keyword(section, keyword)
     685        65952 :       CALL keyword_release(keyword)
     686              : 
     687              :       CALL keyword_create(keyword, __LOCATION__, &
     688              :                           name="MAX_STEP", &
     689              :                           description="Upper bound for the magnitude of the "// &
     690              :                           "unpredicted step size in the update by the "// &
     691              :                           "multisecant mixing scheme", &
     692              :                           repeats=.FALSE., &
     693              :                           n_var=1, &
     694              :                           type_of_var=real_t, &
     695              :                           default_r_val=0.1_dp, &
     696        65952 :                           usage="MAX_STEP .2")
     697        65952 :       CALL section_add_keyword(section, keyword)
     698        65952 :       CALL keyword_release(keyword)
     699              : 
     700              :       CALL keyword_create(keyword, __LOCATION__, &
     701              :                           name="R_FACTOR", &
     702              :                           description="Control factor for the magnitude of the "// &
     703              :                           "unpredicted step size in the update by the "// &
     704              :                           "multisecant mixing scheme", &
     705              :                           repeats=.FALSE., &
     706              :                           n_var=1, &
     707              :                           type_of_var=real_t, &
     708              :                           default_r_val=0.05_dp, &
     709        65952 :                           usage="R_FACTOR .12")
     710        65952 :       CALL section_add_keyword(section, keyword)
     711        65952 :       CALL keyword_release(keyword)
     712              : 
     713              :       CALL keyword_create(keyword, __LOCATION__, name="NSKIP", &
     714              :                           variants=["NSKIP_MIXING"], &
     715              :                           description="Number of initial iteration for which the mixing is skipped", &
     716       131904 :                           usage="NSKIP 10", default_i_val=0)
     717        65952 :       CALL section_add_keyword(section, keyword)
     718        65952 :       CALL keyword_release(keyword)
     719              : 
     720              :       CALL keyword_create(keyword, __LOCATION__, name="N_SIMPLE_MIX", &
     721              :                           variants=["NSIMPLEMIX"], &
     722              :                           description="Number of kerker damping iterations before starting other mixing procedures", &
     723       131904 :                           usage="NSIMPLEMIX", default_i_val=0)
     724        65952 :       CALL section_add_keyword(section, keyword)
     725        65952 :       CALL keyword_release(keyword)
     726              : 
     727              :       CALL keyword_create(keyword, __LOCATION__, name="MAX_GVEC_EXP", &
     728              :                           description="Restricts the G-space mixing to lower part of G-vector spectrum,"// &
     729              :                           " up to a G0, by assigning the exponent of the Gaussian that can be "// &
     730              :                           "represented by vectors smaller than G0 within a certain accuracy. ", &
     731              :                           repeats=.FALSE., &
     732              :                           n_var=1, &
     733              :                           type_of_var=real_t, &
     734              :                           default_r_val=-1._dp, &
     735        65952 :                           usage="MAX_GVEC_EXP 3.")
     736        65952 :       CALL section_add_keyword(section, keyword)
     737        65952 :       CALL keyword_release(keyword)
     738              : 
     739              :       CALL keyword_create(keyword, __LOCATION__, name="GMIX_P", &
     740              :                           description="Activate the mixing of the density matrix, using the same"// &
     741              :                           " mixing coefficient applied for the g-space mixing.", &
     742              :                           repeats=.FALSE., &
     743              :                           lone_keyword_l_val=.TRUE., &
     744              :                           default_l_val=.FALSE., &
     745        65952 :                           usage="GMIX_P")
     746        65952 :       CALL section_add_keyword(section, keyword)
     747        65952 :       CALL keyword_release(keyword)
     748              : 
     749        65952 :    END SUBROUTINE create_mixing_section
     750              : 
     751            0 : END MODULE qs_density_mixing_types
        

Generated by: LCOV version 2.0-1