LCOV - code coverage report
Current view: top level - src - qs_update_s_mstruct.F (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:4dc10b3) Lines: 83 88 94.3 %
Date: 2024-11-21 06:45:46 Functions: 3 3 100.0 %

          Line data    Source code
       1             : !--------------------------------------------------------------------------------------------------!
       2             : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3             : !   Copyright 2000-2024 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 pw_methods,                      ONLY: pw_transfer
      24             :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      25             :                                               pw_r3d_rs_type
      26             :    USE qs_collocate_density,            ONLY: calculate_ppl_grid,&
      27             :                                               calculate_rho_core,&
      28             :                                               calculate_rho_nlcc
      29             :    USE qs_environment_types,            ONLY: get_qs_env,&
      30             :                                               qs_environment_type
      31             :    USE qs_ks_types,                     ONLY: get_ks_env,&
      32             :                                               qs_ks_did_change,&
      33             :                                               qs_ks_env_type,&
      34             :                                               set_ks_env
      35             :    USE qs_rho_methods,                  ONLY: qs_rho_rebuild
      36             :    USE qs_rho_types,                    ONLY: qs_rho_type
      37             :    USE qs_scf_types,                    ONLY: scf_env_did_change
      38             :    USE task_list_methods,               ONLY: generate_qs_task_list
      39             :    USE task_list_types,                 ONLY: allocate_task_list,&
      40             :                                               deallocate_task_list,&
      41             :                                               task_list_type
      42             : #include "./base/base_uses.f90"
      43             : 
      44             :    IMPLICIT NONE
      45             :    PRIVATE
      46             : 
      47             :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
      48             :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_update_s_mstruct'
      49             : 
      50             :    PUBLIC :: qs_env_update_s_mstruct
      51             : !***
      52             : CONTAINS
      53             : 
      54             : ! *****************************************************************************
      55             : !> \brief updates the s_mstruct to reflect the new overlap structure,
      56             : !>      and also updates rho_core distribution.
      57             : !>      Should be called after the atoms have moved and the new overlap
      58             : !>      has been calculated.
      59             : !> \param qs_env the environment to update
      60             : !> \par History
      61             : !>      07.2002 created [fawzi]
      62             : !> \author Fawzi Mohamed
      63             : ! **************************************************************************************************
      64       20372 :    SUBROUTINE qs_env_update_s_mstruct(qs_env)
      65             :       TYPE(qs_environment_type), POINTER                 :: qs_env
      66             : 
      67             :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_update_s_mstruct'
      68             : 
      69             :       INTEGER                                            :: handle
      70             :       LOGICAL                                            :: do_ppl
      71             :       TYPE(dft_control_type), POINTER                    :: dft_control
      72             :       TYPE(pw_c1d_gs_type), POINTER                      :: rho_core, rho_nlcc_g
      73             :       TYPE(pw_r3d_rs_type), POINTER                      :: rho_nlcc, vppl
      74             : 
      75       20372 :       CALL timeset(routineN, handle)
      76             : 
      77       20372 :       CPASSERT(ASSOCIATED(qs_env))
      78             : 
      79       20372 :       NULLIFY (dft_control)
      80             :       CALL get_qs_env(qs_env, &
      81       20372 :                       dft_control=dft_control)
      82             : 
      83             :       ! *** updates rho core ***
      84       20372 :       NULLIFY (rho_core)
      85       20372 :       CALL get_qs_env(qs_env, rho_core=rho_core)
      86       20372 :       IF (dft_control%qs_control%gapw) THEN
      87        1358 :          qs_env%qs_charges%total_rho_core_rspace = qs_env%local_rho_set%rhoz_tot
      88        1358 :          IF (dft_control%qs_control%gapw_control%nopaw_as_gpw) THEN
      89         160 :             CPASSERT(ASSOCIATED(rho_core))
      90             :             CALL calculate_rho_core(rho_core, &
      91         160 :                                     qs_env%qs_charges%total_rho_core_rspace, qs_env, only_nopaw=.TRUE.)
      92             :          ELSE
      93        1198 :             IF (ASSOCIATED(rho_core)) THEN
      94           0 :                CALL rho_core%release()
      95           0 :                DEALLOCATE (rho_core)
      96             :             END IF
      97             :          END IF
      98             :          ! force analytic ppl calculation
      99        1358 :          dft_control%qs_control%do_ppl_method = do_ppl_analytic
     100       19014 :       ELSE IF (dft_control%qs_control%semi_empirical) THEN
     101             :          !??
     102       14678 :       ELSE IF (dft_control%qs_control%dftb) THEN
     103             :          !??
     104       12580 :       ELSE IF (dft_control%qs_control%xtb) THEN
     105             :          !??
     106             :       ELSE
     107        9040 :          CPASSERT(ASSOCIATED(rho_core))
     108             :          CALL calculate_rho_core(rho_core, &
     109        9040 :                                  qs_env%qs_charges%total_rho_core_rspace, qs_env)
     110             :       END IF
     111             : 
     112             :       ! calculate local pseudopotential on grid
     113       20372 :       do_ppl = dft_control%qs_control%do_ppl_method == do_ppl_grid
     114       20372 :       IF (do_ppl) THEN
     115          12 :          NULLIFY (vppl)
     116          12 :          CALL get_qs_env(qs_env, vppl=vppl)
     117          12 :          CPASSERT(ASSOCIATED(vppl))
     118          12 :          CALL calculate_ppl_grid(vppl, qs_env)
     119             :       END IF
     120             : 
     121             :       ! compute the rho_nlcc
     122       20372 :       NULLIFY (rho_nlcc, rho_nlcc_g)
     123       20372 :       CALL get_qs_env(qs_env, rho_nlcc=rho_nlcc, rho_nlcc_g=rho_nlcc_g)
     124       20372 :       IF (ASSOCIATED(rho_nlcc)) THEN
     125          36 :          CALL calculate_rho_nlcc(rho_nlcc, qs_env)
     126          36 :          CALL pw_transfer(rho_nlcc, rho_nlcc_g)
     127             :       END IF
     128             : 
     129             :       ! allocates and creates the task_list
     130       20372 :       CALL qs_create_task_list(qs_env)
     131             : 
     132             :       ! *** environment for ddapc ***
     133       20372 :       IF (ASSOCIATED(qs_env%cp_ddapc_env)) THEN
     134         128 :          CALL cp_ddapc_release(qs_env%cp_ddapc_env)
     135         128 :          DEALLOCATE (qs_env%cp_ddapc_env)
     136             :       END IF
     137       20372 :       CALL cp_ddapc_init(qs_env)
     138             : 
     139             :       ! *** tell ks_env ***
     140       20372 :       CALL qs_ks_did_change(qs_env%ks_env, s_mstruct_changed=.TRUE.)
     141             : 
     142             :       !   *** Updates rho structure ***
     143       20372 :       CALL qs_env_rebuild_rho(qs_env=qs_env)
     144             : 
     145             :       ! *** tell scf_env ***
     146       20372 :       IF (ASSOCIATED(qs_env%scf_env)) THEN
     147       13322 :          CALL scf_env_did_change(qs_env%scf_env)
     148             :       END IF
     149             : 
     150       20372 :       CALL timestop(handle)
     151             : 
     152       20372 :    END SUBROUTINE qs_env_update_s_mstruct
     153             : 
     154             : ! *****************************************************************************
     155             : !> \brief ...
     156             : !> \param qs_env ...
     157             : ! **************************************************************************************************
     158       20372 :    SUBROUTINE qs_create_task_list(qs_env)
     159             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     160             : 
     161             :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_create_task_list'
     162             : 
     163             :       INTEGER                                            :: handle, isub
     164             :       LOGICAL                                            :: skip_load_balance_distributed, soft_valid
     165             :       TYPE(dft_control_type), POINTER                    :: dft_control
     166             :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     167             :       TYPE(task_list_type), POINTER                      :: task_list
     168             : 
     169       20372 :       CALL timeset(routineN, handle)
     170       20372 :       NULLIFY (ks_env, dft_control)
     171       20372 :       CALL get_qs_env(qs_env, ks_env=ks_env, dft_control=dft_control)
     172             : 
     173       20372 :       skip_load_balance_distributed = dft_control%qs_control%skip_load_balance_distributed
     174       20372 :       IF (.NOT. (dft_control%qs_control%semi_empirical &
     175             :                  .OR. dft_control%qs_control%xtb &
     176             :                  .OR. dft_control%qs_control%dftb)) THEN
     177             :          ! generate task lists (non-soft)
     178       10398 :          IF (.NOT. dft_control%qs_control%gapw) THEN
     179        9040 :             CALL get_ks_env(ks_env, task_list=task_list)
     180        9040 :             IF (.NOT. ASSOCIATED(task_list)) THEN
     181        4322 :                CALL allocate_task_list(task_list)
     182        4322 :                CALL set_ks_env(ks_env, task_list=task_list)
     183             :             END IF
     184             :             CALL generate_qs_task_list(ks_env, task_list, &
     185             :                                        reorder_rs_grid_ranks=.TRUE., soft_valid=.FALSE., &
     186        9040 :                                        skip_load_balance_distributed=skip_load_balance_distributed)
     187             :          END IF
     188             :          ! generate the soft task list
     189       10398 :          IF (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc) THEN
     190        1596 :             CALL get_ks_env(ks_env, task_list_soft=task_list)
     191        1596 :             IF (.NOT. ASSOCIATED(task_list)) THEN
     192         920 :                CALL allocate_task_list(task_list)
     193         920 :                CALL set_ks_env(ks_env, task_list_soft=task_list)
     194             :             END IF
     195             :             CALL generate_qs_task_list(ks_env, task_list, &
     196             :                                        reorder_rs_grid_ranks=.TRUE., soft_valid=.TRUE., &
     197        1596 :                                        skip_load_balance_distributed=skip_load_balance_distributed)
     198             :          END IF
     199             :       END IF
     200             : 
     201       20372 :       IF (dft_control%qs_control%do_kg) THEN
     202         112 :          soft_valid = (dft_control%qs_control%gapw .OR. dft_control%qs_control%gapw_xc)
     203             : 
     204         112 :          IF (qs_env%kg_env%tnadd_method == kg_tnadd_embed .OR. &
     205             :              qs_env%kg_env%tnadd_method == kg_tnadd_embed_ri) THEN
     206             : 
     207          82 :             IF (ASSOCIATED(qs_env%kg_env%subset)) THEN
     208         252 :                DO isub = 1, qs_env%kg_env%nsubsets
     209         170 :                   IF (ASSOCIATED(qs_env%kg_env%subset(isub)%task_list)) &
     210         146 :                      CALL deallocate_task_list(qs_env%kg_env%subset(isub)%task_list)
     211             :                END DO
     212             :             ELSE
     213           0 :                ALLOCATE (qs_env%kg_env%subset(qs_env%kg_env%nsubsets))
     214             :             END IF
     215             : 
     216         252 :             DO isub = 1, qs_env%kg_env%nsubsets
     217         170 :                CALL allocate_task_list(qs_env%kg_env%subset(isub)%task_list)
     218             :                ! generate the subset task list from the neighborlist
     219             :                CALL generate_qs_task_list(ks_env, qs_env%kg_env%subset(isub)%task_list, &
     220             :                                           reorder_rs_grid_ranks=.FALSE., soft_valid=soft_valid, &
     221             :                                           skip_load_balance_distributed=skip_load_balance_distributed, &
     222         252 :                                           sab_orb_external=qs_env%kg_env%subset(isub)%sab_orb)
     223             :             END DO
     224             : 
     225             :          END IF
     226             : 
     227             :       END IF
     228             : 
     229       20372 :       CALL timestop(handle)
     230             : 
     231       20372 :    END SUBROUTINE qs_create_task_list
     232             : 
     233             : ! *****************************************************************************
     234             : !> \brief rebuilds the rho structure, making sure that everything is allocated
     235             : !>      and has the right size
     236             : !> \param qs_env the environment in which rho should be rebuilt
     237             : !> \param rebuild_ao if it is necessary to rebuild rho_ao. Defaults to true.
     238             : !> \param rebuild_grids if it in necessary to rebuild rho_r and rho_g.
     239             : !>        Defaults to false.
     240             : !> \par History
     241             : !>      10.2002 created [fawzi]
     242             : !> \author Fawzi Mohamed
     243             : !> \note
     244             : !>      needs updated  pw pools, s_mstruct and h.
     245             : !>      The use of p to keep the structure of h (needed for the forces)
     246             : !>      is ugly and should be removed.
     247             : !>      If necessary rho is created from scratch.
     248             : ! **************************************************************************************************
     249       20372 :    SUBROUTINE qs_env_rebuild_rho(qs_env, rebuild_ao, rebuild_grids)
     250             :       TYPE(qs_environment_type), POINTER                 :: qs_env
     251             :       LOGICAL, INTENT(in), OPTIONAL                      :: rebuild_ao, rebuild_grids
     252             : 
     253             :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_env_rebuild_rho'
     254             : 
     255             :       INTEGER                                            :: handle
     256             :       LOGICAL                                            :: do_admm, gapw_xc
     257             :       TYPE(dft_control_type), POINTER                    :: dft_control
     258             :       TYPE(qs_rho_type), POINTER                         :: rho, rho_external, rho_xc
     259             : 
     260       20372 :       NULLIFY (rho)
     261       20372 :       CALL timeset(routineN, handle)
     262             : 
     263             :       CALL get_qs_env(qs_env, &
     264             :                       dft_control=dft_control, &
     265             :                       rho=rho, &
     266             :                       rho_xc=rho_xc, &
     267       20372 :                       rho_external=rho_external)
     268             : 
     269       20372 :       gapw_xc = dft_control%qs_control%gapw_xc
     270       20372 :       do_admm = dft_control%do_admm
     271             :       CALL qs_rho_rebuild(rho, qs_env=qs_env, &
     272       20372 :                           rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
     273             : 
     274       20372 :       IF (gapw_xc) THEN
     275             :          CALL qs_rho_rebuild(rho_xc, qs_env=qs_env, &
     276         238 :                              rebuild_ao=rebuild_ao, rebuild_grids=rebuild_grids)
     277             :       END IF
     278             : 
     279             : ! ZMP rebuilding external density
     280       20372 :       IF (dft_control%apply_external_density) THEN
     281             :          CALL qs_rho_rebuild(rho_external, qs_env=qs_env, &
     282           0 :                              rebuild_grids=rebuild_grids)
     283           0 :          dft_control%read_external_density = .TRUE.
     284             :       END IF
     285             : 
     286       20372 :       CALL timestop(handle)
     287             : 
     288       20372 :    END SUBROUTINE qs_env_rebuild_rho
     289             : 
     290             : END MODULE qs_update_s_mstruct

Generated by: LCOV version 1.15