LCOV - code coverage report
Current view: top level - src - qs_update_s_mstruct.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:24d69ee) Lines: 97.3 % 113 110
Test Date: 2026-09-03 07:32:15 Functions: 100.0 % 3 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 qs_environment methods that use many other modules
      10              : !> \par History
      11              : !>      09.2002 created [fawzi]
      12              : !>      - local atom distribution (25.06.2003,MK)
      13              : !> \author Fawzi Mohamed
      14              : ! *****************************************************************************
      15              : MODULE qs_update_s_mstruct
      16              :    USE cp_control_types,                ONLY: dft_control_type
      17              :    USE cp_ddapc_types,                  ONLY: cp_ddapc_release
      18              :    USE cp_ddapc_util,                   ONLY: cp_ddapc_init
      19              :    USE input_constants,                 ONLY: do_ppl_analytic,&
      20              :                                               do_ppl_grid,&
      21              :                                               kg_tnadd_embed,&
      22              :                                               kg_tnadd_embed_ri
      23              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      24              :                                               section_vals_type,&
      25              :                                               section_vals_val_get
      26              :    USE kinds,                           ONLY: default_string_length,&
      27              :                                               dp
      28              :    USE pw_methods,                      ONLY: pw_transfer
      29              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      30              :                                               pw_r3d_rs_type
      31              :    USE qs_collocate_density,            ONLY: calculate_ppl_grid,&
      32              :                                               calculate_rho_core,&
      33              :                                               calculate_rho_nlcc
      34              :    USE qs_environment_types,            ONLY: get_qs_env,&
      35              :                                               qs_environment_type,&
      36              :                                               set_qs_env
      37              :    USE qs_ks_types,                     ONLY: get_ks_env,&
      38              :                                               qs_ks_did_change,&
      39              :                                               qs_ks_env_type,&
      40              :                                               set_ks_env
      41              :    USE qs_rho_methods,                  ONLY: qs_rho_rebuild
      42              :    USE qs_rho_types,                    ONLY: qs_rho_create,&
      43              :                                               qs_rho_type
      44              :    USE qs_scf_types,                    ONLY: scf_env_did_change
      45              :    USE skala_gpw_functional,            ONLY: get_gauxc_section,&
      46              :                                               native_skala_gapw_composite_direct_ao,&
      47              :                                               native_skala_gapw_composite_reference,&
      48              :                                               skala_gapw_representation
      49              :    USE task_list_methods,               ONLY: generate_qs_task_list
      50              :    USE task_list_types,                 ONLY: allocate_task_list,&
      51              :                                               deallocate_task_list,&
      52              :                                               task_list_type
      53              :    USE xc_input_constants,              ONLY: skala_gapw_paw_one_center
      54              : #include "./base/base_uses.f90"
      55              : 
      56              :    IMPLICIT NONE
      57              :    PRIVATE
      58              : 
      59              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      60              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_update_s_mstruct'
      61              : 
      62              :    PUBLIC :: qs_env_update_s_mstruct
      63              : !***
      64              : CONTAINS
      65              : 
      66              : ! *****************************************************************************
      67              : !> \brief updates the s_mstruct to reflect the new overlap structure,
      68              : !>      and also updates rho_core distribution.
      69              : !>      Should be called after the atoms have moved and the new overlap
      70              : !>      has been calculated.
      71              : !> \param qs_env the environment to update
      72              : !> \par History
      73              : !>      07.2002 created [fawzi]
      74              : !> \author Fawzi Mohamed
      75              : ! **************************************************************************************************
      76        28870 :    SUBROUTINE qs_env_update_s_mstruct(qs_env)
      77              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      78              : 
      79              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_update_s_mstruct'
      80              : 
      81              :       INTEGER                                            :: handle, nk
      82              :       LOGICAL                                            :: do_ppl
      83              :       REAL(KIND=dp)                                      :: wtot
      84        28870 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: cw
      85              :       TYPE(dft_control_type), POINTER                    :: dft_control
      86              :       TYPE(pw_c1d_gs_type), POINTER                      :: rho_core, rho_nlcc_g
      87              :       TYPE(pw_r3d_rs_type), POINTER                      :: rho_nlcc, vppl, xcint_weights
      88              : 
      89        28870 :       CALL timeset(routineN, handle)
      90              : 
      91        28870 :       CPASSERT(ASSOCIATED(qs_env))
      92              : 
      93        28870 :       NULLIFY (dft_control)
      94              :       CALL get_qs_env(qs_env, &
      95        28870 :                       dft_control=dft_control)
      96              : 
      97              :       ! *** updates rho core ***
      98        28870 :       NULLIFY (rho_core)
      99        28870 :       CALL get_qs_env(qs_env, rho_core=rho_core)
     100        28870 :       IF (dft_control%qs_control%gapw) THEN
     101         2260 :          qs_env%qs_charges%total_rho_core_rspace = qs_env%local_rho_set%rhoz_tot
     102              :          ! Initial CNEO quantum nuclear charge density is a simple Zeff sum.
     103              :          ! Later it will be calculated from numerical integration during SCF.
     104         2260 :          qs_env%qs_charges%total_rho1_hard_nuc = qs_env%local_rho_set%rhoz_cneo_tot
     105         2260 :          IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
     106          294 :             CPASSERT(ASSOCIATED(rho_core))
     107              :             CALL calculate_rho_core(rho_core, &
     108          294 :                                     qs_env%qs_charges%total_rho_core_rspace, qs_env, only_nopaw=.TRUE.)
     109              :          ELSE
     110         1966 :             IF (ASSOCIATED(rho_core)) THEN
     111            0 :                CALL rho_core%release()
     112            0 :                DEALLOCATE (rho_core)
     113              :             END IF
     114              :          END IF
     115              :          ! force analytic ppl calculation
     116         2260 :          dft_control%qs_control%do_ppl_method = do_ppl_analytic
     117        26610 :       ELSE IF (dft_control%qs_control%semi_empirical) THEN
     118              :          !??
     119        22252 :       ELSE IF (dft_control%qs_control%dftb) THEN
     120              :          !??
     121        18832 :       ELSE IF (dft_control%qs_control%xtb) THEN
     122              :          !??
     123              :       ELSE
     124        10576 :          CPASSERT(ASSOCIATED(rho_core))
     125              :          CALL calculate_rho_core(rho_core, &
     126        10576 :                                  qs_env%qs_charges%total_rho_core_rspace, qs_env)
     127              :       END IF
     128              : 
     129              :       ! calculate local pseudopotential on grid
     130        28870 :       do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
     131        28870 :       IF (do_ppl) THEN
     132           12 :          NULLIFY (vppl)
     133           12 :          CALL get_qs_env(qs_env, vppl=vppl)
     134           12 :          CPASSERT(ASSOCIATED(vppl))
     135           12 :          CALL calculate_ppl_grid(vppl, qs_env)
     136              :       END IF
     137              : 
     138              :       ! compute the rho_nlcc
     139        28870 :       NULLIFY (rho_nlcc, rho_nlcc_g)
     140        28870 :       CALL get_qs_env(qs_env, rho_nlcc=rho_nlcc, rho_nlcc_g=rho_nlcc_g)
     141        28870 :       IF (ASSOCIATED(rho_nlcc)) THEN
     142          136 :          CALL calculate_rho_nlcc(rho_nlcc, qs_env)
     143          136 :          CALL pw_transfer(rho_nlcc, rho_nlcc_g)
     144              :       END IF
     145              : 
     146              :       ! compute the xcint_weights
     147        28870 :       IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
     148         2650 :          IF (dft_control%qs_control%gapw_control%accurate_xcint) THEN
     149          670 :             CALL get_qs_env(qs_env, xcint_weights=xcint_weights, nkind=nk)
     150         2010 :             ALLOCATE (cw(nk))
     151         1978 :             cw = 1.0_dp
     152              :             CALL calculate_rho_core(xcint_weights, wtot, qs_env, &
     153          670 :                                     dft_control%qs_control%gapw_control%aw, cw)
     154     33514741 :             xcint_weights%array = 1.0_dp + xcint_weights%array
     155         1340 :             DEALLOCATE (cw)
     156              :          END IF
     157              :       END IF
     158              : 
     159              :       ! allocates and creates the task_list
     160        28870 :       CALL qs_create_task_list(qs_env)
     161              : 
     162              :       ! *** environment for ddapc ***
     163        28870 :       IF (ASSOCIATED(qs_env%cp_ddapc_env)) THEN
     164          138 :          CALL cp_ddapc_release(qs_env%cp_ddapc_env)
     165          138 :          DEALLOCATE (qs_env%cp_ddapc_env)
     166              :       END IF
     167        28870 :       CALL cp_ddapc_init(qs_env)
     168              : 
     169              :       ! *** tell ks_env ***
     170        28870 :       CALL qs_ks_did_change(qs_env%ks_env, s_mstruct_changed=.TRUE.)
     171              : 
     172              :       !   *** Updates rho structure ***
     173        28870 :       CALL qs_env_rebuild_rho(qs_env=qs_env)
     174              : 
     175              :       ! *** tell scf_env ***
     176        28870 :       IF (ASSOCIATED(qs_env%scf_env)) THEN
     177        19422 :          CALL scf_env_did_change(qs_env%scf_env)
     178              :       END IF
     179              : 
     180        28870 :       CALL timestop(handle)
     181              : 
     182        28870 :    END SUBROUTINE qs_env_update_s_mstruct
     183              : 
     184              : ! *****************************************************************************
     185              : !> \brief ...
     186              : !> \param qs_env ...
     187              : ! **************************************************************************************************
     188        28870 :    SUBROUTINE qs_create_task_list(qs_env)
     189              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     190              : 
     191              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_create_task_list'
     192              : 
     193              :       CHARACTER(LEN=default_string_length)               :: basis_type
     194              :       INTEGER                                            :: handle, isub
     195              :       LOGICAL :: composite_direct_ao, composite_reference, native_grid_diagnostics, &
     196              :          paw_one_center, skip_load_balance_distributed, soft_valid
     197              :       TYPE(dft_control_type), POINTER                    :: dft_control
     198              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     199              :       TYPE(section_vals_type), POINTER                   :: gauxc_section, input, xc_section
     200              :       TYPE(task_list_type), POINTER                      :: task_list
     201              : 
     202        28870 :       CALL timeset(routineN, handle)
     203        28870 :       NULLIFY (ks_env, dft_control, gauxc_section, input, xc_section)
     204        28870 :       CALL get_qs_env(qs_env, ks_env=ks_env, dft_control=dft_control, input=input)
     205              : 
     206        28870 :       soft_valid = (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc)
     207        28870 :       xc_section => section_vals_get_subs_vals(input, "DFT%XC")
     208        28870 :       composite_reference = native_skala_gapw_composite_reference(xc_section)
     209              :       composite_direct_ao = composite_reference .AND. &
     210        28870 :                             native_skala_gapw_composite_direct_ao(xc_section)
     211        28870 :       paw_one_center = skala_gapw_representation(xc_section) == skala_gapw_paw_one_center
     212        28870 :       native_grid_diagnostics = .FALSE.
     213        28870 :       gauxc_section => get_gauxc_section(xc_section)
     214        28870 :       IF (ASSOCIATED(gauxc_section)) THEN
     215              :          CALL section_vals_val_get(gauxc_section, "NATIVE_GRID_DIAGNOSTICS", &
     216          194 :                                    l_val=native_grid_diagnostics)
     217              :       END IF
     218        28870 :       skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
     219        28870 :       IF (.NOT. (dft_control%qs_control%semi_empirical &
     220              :                  .OR. dft_control%qs_control%xtb &
     221              :                  .OR. dft_control%qs_control%dftb)) THEN
     222              :          ! generate task lists (non-soft)
     223        12836 :          IF (.NOT. dft_control%qs_control%gapw .OR. composite_direct_ao .OR. &
     224              :              ((composite_reference .OR. paw_one_center) .AND. native_grid_diagnostics)) THEN
     225        10586 :             CALL get_ks_env(ks_env, task_list=task_list)
     226        10586 :             IF (.NOT. ASSOCIATED(task_list)) THEN
     227         5190 :                CALL allocate_task_list(task_list)
     228         5190 :                CALL set_ks_env(ks_env, task_list=task_list)
     229              :             END IF
     230              :             CALL generate_qs_task_list(ks_env, task_list, basis_type="ORB", &
     231              :                                        reorder_rs_grid_ranks=.TRUE., &
     232        10586 :                                        skip_load_balance_distributed=skip_load_balance_distributed)
     233              :          END IF
     234              :          ! generate the soft task list
     235        12836 :          IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
     236         2650 :             CALL get_ks_env(ks_env, task_list_soft=task_list)
     237         2650 :             IF (.NOT. ASSOCIATED(task_list)) THEN
     238         1458 :                CALL allocate_task_list(task_list)
     239         1458 :                CALL set_ks_env(ks_env, task_list_soft=task_list)
     240              :             END IF
     241              :             CALL generate_qs_task_list(ks_env, task_list, basis_type="ORB_SOFT", &
     242              :                                        reorder_rs_grid_ranks=.TRUE., &
     243         2650 :                                        skip_load_balance_distributed=skip_load_balance_distributed)
     244              :          END IF
     245              :       END IF
     246              : 
     247        28870 :       IF (dft_control%qs_control%do_kg) THEN
     248              : 
     249          128 :          IF (qs_env%kg_env%tnadd_method == kg_tnadd_embed .OR. &
     250              :              qs_env%kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
     251              : 
     252           94 :             IF (ASSOCIATED(qs_env%kg_env%subset)) THEN
     253          296 :                DO isub = 1, qs_env%kg_env%nsubsets
     254          296 :                   IF (ASSOCIATED(qs_env%kg_env%subset(isub)%task_list)) THEN
     255           64 :                      CALL deallocate_task_list(qs_env%kg_env%subset(isub)%task_list)
     256              :                   END IF
     257              :                END DO
     258              :             ELSE
     259            0 :                ALLOCATE (qs_env%kg_env%subset(qs_env%kg_env%nsubsets))
     260              :             END IF
     261              : 
     262           94 :             IF (soft_valid) THEN
     263           12 :                basis_type = "ORB_SOFT"
     264              :             ELSE
     265           82 :                basis_type = "ORB"
     266              :             END IF
     267              : 
     268          296 :             DO isub = 1, qs_env%kg_env%nsubsets
     269          202 :                CALL allocate_task_list(qs_env%kg_env%subset(isub)%task_list)
     270              :                ! generate the subset task list from the neighborlist
     271              :                CALL generate_qs_task_list(ks_env, qs_env%kg_env%subset(isub)%task_list, &
     272              :                                           basis_type=basis_type, &
     273              :                                           reorder_rs_grid_ranks=.FALSE., &
     274              :                                           skip_load_balance_distributed=skip_load_balance_distributed, &
     275          296 :                                           sab_orb_external=qs_env%kg_env%subset(isub)%sab_orb)
     276              :             END DO
     277              : 
     278              :          END IF
     279              : 
     280              :       END IF
     281              : 
     282        28870 :       CALL timestop(handle)
     283              : 
     284        28870 :    END SUBROUTINE qs_create_task_list
     285              : 
     286              : ! *****************************************************************************
     287              : !> \brief rebuilds the rho structure, making sure that everything is allocated
     288              : !>      and has the right size
     289              : !> \param qs_env the environment in which rho should be rebuilt
     290              : !> \param rebuild_ao if it is necessary to rebuild rho_ao. Defaults to true.
     291              : !> \param rebuild_grids if it in necessary to rebuild rho_r and rho_g.
     292              : !>        Defaults to false.
     293              : !> \par History
     294              : !>      10.2002 created [fawzi]
     295              : !> \author Fawzi Mohamed
     296              : !> \note
     297              : !>      needs updated  pw pools, s_mstruct and h.
     298              : !>      The use of p to keep the structure of h (needed for the forces)
     299              : !>      is ugly and should be removed.
     300              : !>      If necessary rho is created from scratch.
     301              : ! **************************************************************************************************
     302        28870 :    SUBROUTINE qs_env_rebuild_rho(qs_env, rebuild_ao, rebuild_grids)
     303              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     304              :       LOGICAL, INTENT(in), OPTIONAL                      :: rebuild_ao, rebuild_grids
     305              : 
     306              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_rebuild_rho'
     307              : 
     308              :       INTEGER                                            :: handle
     309              :       LOGICAL                                            :: do_admm, gapw_xc
     310              :       TYPE(dft_control_type), POINTER                    :: dft_control
     311              :       TYPE(qs_rho_type), POINTER                         :: rho, rho_external, rho_xc
     312              : 
     313        28870 :       NULLIFY (rho)
     314        28870 :       CALL timeset(routineN, handle)
     315              : 
     316              :       CALL get_qs_env(qs_env, &
     317              :                       dft_control=dft_control, &
     318              :                       rho=rho, &
     319              :                       rho_xc=rho_xc, &
     320        28870 :                       rho_external=rho_external)
     321              : 
     322        28870 :       gapw_xc = dft_control%qs_control%gapw_xc
     323        28870 :       do_admm = dft_control%do_admm
     324              :       CALL qs_rho_rebuild(rho, qs_env=qs_env, &
     325        28870 :                           rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
     326              : 
     327        28870 :       IF (gapw_xc) THEN
     328              :          CALL qs_rho_rebuild(rho_xc, qs_env=qs_env, &
     329          390 :                              rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
     330              :       END IF
     331              : 
     332              : ! ZMP rebuilding external density
     333        28870 :       IF (dft_control%apply_external_density) THEN
     334            4 :          IF (.NOT. ASSOCIATED(rho_external)) THEN
     335            4 :             ALLOCATE (rho_external)
     336            4 :             CALL qs_rho_create(rho_external)
     337            4 :             CALL set_qs_env(qs_env, rho_external=rho_external)
     338              :          END IF
     339              :          CALL qs_rho_rebuild(rho_external, qs_env=qs_env, &
     340            4 :                              rebuild_grids=rebuild_grids)
     341            4 :          dft_control%read_external_density = .TRUE.
     342              :       END IF
     343              : 
     344        28870 :       CALL timestop(handle)
     345              : 
     346        28870 :    END SUBROUTINE qs_env_rebuild_rho
     347              : 
     348              : END MODULE qs_update_s_mstruct
        

Generated by: LCOV version 2.0-1