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

Generated by: LCOV version 2.0-1