LCOV - code coverage report
Current view: top level - src - qs_dcdr_ao.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:936074a) Lines: 97.7 % 221 216
Test Date: 2025-12-04 06:27:48 Functions: 100.0 % 6 6

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2025 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Calculate the derivatives of the MO coefficients wrt nuclear coordinates
      10              : !> \author Sandra Luber, Edward Ditler
      11              : ! **************************************************************************************************
      12              : 
      13              : MODULE qs_dcdr_ao
      14              : 
      15              :    USE basis_set_types,                 ONLY: gto_basis_set_p_type,&
      16              :                                               gto_basis_set_type
      17              :    USE cp_control_types,                ONLY: dft_control_type
      18              :    USE cp_dbcsr_api,                    ONLY: dbcsr_copy,&
      19              :                                               dbcsr_get_block_p,&
      20              :                                               dbcsr_p_type,&
      21              :                                               dbcsr_set,&
      22              :                                               dbcsr_type
      23              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      24              :                                               copy_fm_to_dbcsr
      25              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      26              :                                               cp_fm_release,&
      27              :                                               cp_fm_type
      28              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      29              :                                               cp_logger_type
      30              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      31              :                                               section_vals_type
      32              :    USE kinds,                           ONLY: default_string_length,&
      33              :                                               dp
      34              :    USE orbital_pointers,                ONLY: ncoset
      35              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      36              :    USE pw_env_types,                    ONLY: pw_env_get,&
      37              :                                               pw_env_type
      38              :    USE pw_methods,                      ONLY: pw_axpy,&
      39              :                                               pw_copy,&
      40              :                                               pw_scale,&
      41              :                                               pw_transfer,&
      42              :                                               pw_zero
      43              :    USE pw_poisson_methods,              ONLY: pw_poisson_solve
      44              :    USE pw_poisson_types,                ONLY: pw_poisson_type
      45              :    USE pw_pool_types,                   ONLY: pw_pool_p_type,&
      46              :                                               pw_pool_type
      47              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      48              :                                               pw_r3d_rs_type
      49              :    USE qs_collocate_density,            ONLY: calculate_drho_core,&
      50              :                                               calculate_drho_elec_dR
      51              :    USE qs_core_matrices,                ONLY: core_matrices
      52              :    USE qs_energy_types,                 ONLY: qs_energy_type
      53              :    USE qs_environment_types,            ONLY: get_qs_env,&
      54              :                                               qs_environment_type
      55              :    USE qs_integral_utils,               ONLY: basis_set_list_setup,&
      56              :                                               get_memory_usage
      57              :    USE qs_integrate_potential,          ONLY: integrate_v_dbasis,&
      58              :                                               integrate_v_rspace
      59              :    USE qs_kind_types,                   ONLY: qs_kind_type
      60              :    USE qs_ks_types,                     ONLY: get_ks_env,&
      61              :                                               qs_ks_env_type
      62              :    USE qs_linres_types,                 ONLY: dcdr_env_type
      63              :    USE qs_neighbor_list_types,          ONLY: get_iterator_info,&
      64              :                                               get_neighbor_list_set_p,&
      65              :                                               neighbor_list_iterate,&
      66              :                                               neighbor_list_iterator_create,&
      67              :                                               neighbor_list_iterator_p_type,&
      68              :                                               neighbor_list_iterator_release,&
      69              :                                               neighbor_list_set_p_type
      70              :    USE qs_rho_methods,                  ONLY: qs_rho_rebuild,&
      71              :                                               qs_rho_update_rho
      72              :    USE qs_rho_types,                    ONLY: qs_rho_create,&
      73              :                                               qs_rho_get,&
      74              :                                               qs_rho_release,&
      75              :                                               qs_rho_type
      76              :    USE qs_vxc,                          ONLY: qs_vxc_create
      77              :    USE xc,                              ONLY: xc_calc_2nd_deriv,&
      78              :                                               xc_prep_2nd_deriv
      79              :    USE xc_derivative_set_types,         ONLY: xc_derivative_set_type,&
      80              :                                               xc_dset_release
      81              :    USE xc_rho_set_types,                ONLY: xc_rho_set_release,&
      82              :                                               xc_rho_set_type
      83              : 
      84              : !$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads
      85              : !$ USE OMP_LIB, ONLY: omp_lock_kind, &
      86              : !$                    omp_init_lock, omp_set_lock, &
      87              : !$                    omp_unset_lock, omp_destroy_lock
      88              : 
      89              : #include "./base/base_uses.f90"
      90              : 
      91              :    IMPLICIT NONE
      92              : 
      93              :    PRIVATE
      94              :    PUBLIC :: core_dR, d_vhxc_dR, d_core_charge_density_dR, apply_op_constant_term
      95              :    PUBLIC :: vhxc_R_perturbed_basis_functions
      96              :    PUBLIC :: hr_mult_by_delta_1d
      97              : 
      98              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_dcdr_ao'
      99              : 
     100              : CONTAINS
     101              : 
     102              : ! **************************************************************************************************
     103              : !> \brief Build the perturbed density matrix correction depending on the overlap derivative
     104              : !> \param qs_env ...
     105              : !> \param dcdr_env ...
     106              : !> \param overlap1 Overlap derivative in AO basis
     107              : !> \author Edward Ditler
     108              : ! **************************************************************************************************
     109          252 :    SUBROUTINE apply_op_constant_term(qs_env, dcdr_env, overlap1)
     110              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     111              :       TYPE(dcdr_env_type)                                :: dcdr_env
     112              :       TYPE(dbcsr_p_type), OPTIONAL                       :: overlap1
     113              : 
     114              :       CHARACTER(len=*), PARAMETER :: routineN = 'apply_op_constant_term'
     115              : 
     116              :       INTEGER                                            :: handle, ispin
     117              :       REAL(KIND=dp)                                      :: energy_hartree
     118              :       TYPE(cp_fm_type)                                   :: rho_ao_fm, rho_ao_s1, rho_ao_s1_rho_ao, &
     119              :                                                             s1_ao
     120          252 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho1_ao, rho_ao
     121              :       TYPE(pw_c1d_gs_type)                               :: rho1_tot_gspace, v_hartree_gspace
     122          252 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho1_g, rho1_g_pw
     123              :       TYPE(pw_env_type), POINTER                         :: pw_env
     124              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
     125              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     126              :       TYPE(pw_r3d_rs_type)                               :: v_hartree_rspace
     127          504 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho1_r, rho_r, tau1_r, v_rspace_new, &
     128          252 :                                                             v_xc, v_xc_tau
     129              :       TYPE(qs_rho_type), POINTER                         :: perturbed_density, rho
     130              :       TYPE(section_vals_type), POINTER                   :: input, xc_section
     131              :       TYPE(xc_derivative_set_type)                       :: deriv_set
     132              :       TYPE(xc_rho_set_type)                              :: rho_set
     133              : 
     134              :       ! Build the perturbed density matrix correction depending on the overlap derivative
     135              :       !   P1 = C0 C1 + C1 C0
     136              :       !        - C0_(mu j) S1_(jk) C0_(k nu)
     137              :       ! This routine is adapted from apply_op_2_dft. There, build_dm_response builds
     138              :       !  C0 * dCR + dCR * C0.
     139              :       ! build_dm_response is computing $-1 * (C^0 C^1 + C^1 C^0)$ and later on in the
     140              :       !  integration the factor 2 is applied to account for the occupancy.
     141              :       ! The sign is negative because the kernel is on the RHS of the Sternheimer equation.
     142              :       !
     143              :       ! The correction factor in this routine needs to have
     144              :       !      the opposite sign mathematically as (C0 C1 + C1 C0)
     145              :       !   so the same sign in the code     because of the $-1$ in dCR
     146              :       !   so the opposite sign in the code because we are on the LHS of the Sternheimer equation.
     147              :       !
     148              :       ! This term must not go into the kernel applied by the linear response solver, because
     149              :       !  for the (P)CG algorithm, all constant terms have to be on one side of the equations
     150              :       !  and all solution dependent terms must be on the other side.
     151              : 
     152          252 :       CALL timeset(routineN, handle)
     153              : 
     154          252 :       NULLIFY (auxbas_pw_pool, pw_env, rho1_r, rho1_g_pw, &
     155          252 :                v_xc, poisson_env, input, rho, rho1_g, v_xc_tau)
     156              : 
     157          252 :       CALL cp_fm_create(rho_ao_fm, dcdr_env%aoao_fm_struct)
     158          252 :       CALL cp_fm_create(rho_ao_s1, dcdr_env%aoao_fm_struct)
     159          252 :       CALL cp_fm_create(rho_ao_s1_rho_ao, dcdr_env%aoao_fm_struct)
     160          252 :       CALL cp_fm_create(s1_ao, dcdr_env%aoao_fm_struct)
     161              : 
     162          252 :       IF (PRESENT(overlap1)) THEN
     163            0 :          CALL copy_dbcsr_to_fm(overlap1%matrix, s1_ao)
     164              :       ELSE
     165          252 :          CALL copy_dbcsr_to_fm(dcdr_env%matrix_s1(dcdr_env%beta + 1)%matrix, s1_ao)
     166              :       END IF
     167              : 
     168          576 :       DO ispin = 1, dcdr_env%nspins
     169          324 :          CALL dbcsr_set(dcdr_env%perturbed_dm_correction(ispin)%matrix, 0._dp)
     170          324 :          CALL dbcsr_set(dcdr_env%matrix_apply_op_constant(ispin)%matrix, 0.0_dp)
     171              : 
     172              :          CALL parallel_gemm('N', 'T', dcdr_env%nao, dcdr_env%nao, dcdr_env%nmo(ispin), &
     173              :                             1.0_dp, dcdr_env%mo_coeff(ispin), dcdr_env%mo_coeff(ispin), &
     174          324 :                             0.0_dp, rho_ao_fm)
     175              : 
     176              :          CALL parallel_gemm('N', 'N', dcdr_env%nao, dcdr_env%nao, dcdr_env%nao, &
     177              :                             1.0_dp, rho_ao_fm, s1_ao, &
     178          324 :                             0.0_dp, rho_ao_s1)
     179              : 
     180              :          CALL parallel_gemm('N', 'N', dcdr_env%nao, dcdr_env%nao, dcdr_env%nao, &
     181              :                             -1._dp, rho_ao_s1, rho_ao_fm, &   ! this is the sign mentioned above.
     182          324 :                             0.0_dp, rho_ao_s1_rho_ao)
     183              : 
     184          576 :          CALL copy_fm_to_dbcsr(rho_ao_s1_rho_ao, dcdr_env%perturbed_dm_correction(ispin)%matrix)
     185              :       END DO
     186              : 
     187          252 :       CALL cp_fm_release(rho_ao_fm)
     188          252 :       CALL cp_fm_release(rho_ao_s1)
     189          252 :       CALL cp_fm_release(rho_ao_s1_rho_ao)
     190          252 :       CALL cp_fm_release(s1_ao)
     191              :       ! Done building the density matrix correction
     192              : 
     193              :       ! Build the density struct from the environment
     194          252 :       NULLIFY (perturbed_density)
     195          252 :       ALLOCATE (perturbed_density)
     196          252 :       CALL qs_rho_create(perturbed_density)
     197          252 :       CALL qs_rho_rebuild(perturbed_density, qs_env=qs_env)
     198              : 
     199              :       ! ... set the density matrix to be the perturbed density matrix
     200          252 :       CALL qs_rho_get(perturbed_density, rho_ao=rho1_ao)
     201          576 :       DO ispin = 1, dcdr_env%nspins
     202          576 :          CALL dbcsr_copy(rho1_ao(ispin)%matrix, dcdr_env%perturbed_dm_correction(ispin)%matrix)
     203              :       END DO
     204              : 
     205              :       ! ... updates rho_r and rho_g to the rho%rho_ao.
     206              :       CALL qs_rho_update_rho(rho_struct=perturbed_density, &
     207          252 :                              qs_env=qs_env)
     208              : 
     209              :       ! Also update the qs_env%rho
     210          252 :       CALL get_qs_env(qs_env, rho=rho)
     211          252 :       CALL qs_rho_update_rho(rho, qs_env=qs_env)
     212          252 :       CALL qs_rho_get(rho, rho_ao=rho_ao, rho_r=rho_r)
     213              : 
     214          252 :       energy_hartree = 0.0_dp
     215              : 
     216              :       CALL get_qs_env(qs_env=qs_env, &
     217              :                       pw_env=pw_env, &
     218          252 :                       input=input)
     219              : 
     220              :       ! Create the temporary grids
     221              :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
     222          252 :                       poisson_env=poisson_env)
     223              : 
     224              :       ! Allocate deriv_set and rho_set
     225          252 :       xc_section => section_vals_get_subs_vals(input, "DFT%XC")
     226              : 
     227              :       CALL xc_prep_2nd_deriv(deriv_set, rho_set, &
     228              :                              rho_r, auxbas_pw_pool, &
     229          252 :                              xc_section=xc_section)
     230              : 
     231              :       ! Done with deriv_set and rho_set
     232              : 
     233         1080 :       ALLOCATE (v_rspace_new(dcdr_env%nspins))
     234          252 :       CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
     235          252 :       CALL auxbas_pw_pool%create_pw(v_hartree_rspace)
     236              : 
     237              :       ! Calculate the Hartree potential on the total density
     238          252 :       CALL auxbas_pw_pool%create_pw(rho1_tot_gspace)
     239              : 
     240          252 :       CALL qs_rho_get(perturbed_density, rho_g=rho1_g, rho_r=rho1_r, tau_r=tau1_r)
     241          252 :       CALL pw_copy(rho1_g(1), rho1_tot_gspace)
     242          324 :       DO ispin = 2, dcdr_env%nspins
     243          324 :          CALL pw_axpy(rho1_g(ispin), rho1_tot_gspace)
     244              :       END DO
     245              : 
     246              :       CALL pw_poisson_solve(poisson_env, rho1_tot_gspace, &
     247              :                             energy_hartree, &
     248          252 :                             v_hartree_gspace)
     249          252 :       CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
     250              : 
     251          252 :       CALL auxbas_pw_pool%give_back_pw(rho1_tot_gspace)
     252              : 
     253              :       ! Calculate the second derivative of the exchange-correlation potential
     254              :       CALL xc_calc_2nd_deriv(v_xc, v_xc_tau, deriv_set, rho_set, &
     255          252 :                              rho1_r, rho1_g_pw, tau1_r, auxbas_pw_pool, xc_section, gapw=.FALSE.)
     256              : 
     257          576 :       DO ispin = 1, dcdr_env%nspins
     258          576 :          v_rspace_new(ispin) = v_xc(ispin)
     259              :       END DO
     260          252 :       DEALLOCATE (v_xc)
     261              : 
     262              :       ! Done calculating the potentials
     263              : 
     264              :       !-------------------------------!
     265              :       ! Add both hartree and xc terms !
     266              :       !-------------------------------!
     267          252 :       CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
     268          576 :       DO ispin = 1, dcdr_env%nspins
     269          576 :          CALL pw_scale(v_rspace_new(ispin), v_rspace_new(ispin)%pw_grid%dvol)
     270              :       END DO
     271              : 
     272          576 :       DO ispin = 1, dcdr_env%nspins
     273          324 :          CALL dbcsr_set(dcdr_env%matrix_apply_op_constant(ispin)%matrix, 0.0_dp)
     274          324 :          CALL pw_axpy(v_hartree_rspace, v_rspace_new(ispin))
     275          324 :          IF (dcdr_env%nspins == 1) THEN
     276          180 :             CALL pw_scale(v_rspace_new(1), 2.0_dp)
     277              :          END IF
     278              : 
     279              :          CALL integrate_v_rspace(v_rspace=v_rspace_new(ispin), &
     280              :                                  hmat=dcdr_env%matrix_apply_op_constant(ispin), &
     281              :                                  qs_env=qs_env, &
     282          576 :                                  calculate_forces=.FALSE.)
     283              :       END DO
     284              : 
     285          252 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
     286          252 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace)
     287          576 :       DO ispin = 1, dcdr_env%nspins
     288          576 :          CALL auxbas_pw_pool%give_back_pw(v_rspace_new(ispin))
     289              :       END DO
     290          252 :       DEALLOCATE (v_rspace_new)
     291              : 
     292          252 :       IF (ASSOCIATED(v_xc_tau)) THEN
     293            0 :          CALL pw_scale(v_xc_tau(1), 2._dp*v_xc_tau(1)%pw_grid%dvol)
     294              :          CALL integrate_v_rspace(v_rspace=v_xc_tau(1), &
     295              :                                  hmat=dcdr_env%matrix_apply_op_constant(1), &
     296              :                                  qs_env=qs_env, &
     297              :                                  compute_tau=.TRUE., &
     298            0 :                                  calculate_forces=.FALSE.)
     299              : 
     300            0 :          CALL auxbas_pw_pool%give_back_pw(v_xc_tau(1))
     301            0 :          DEALLOCATE (v_xc_tau)
     302              :       END IF
     303              : 
     304          252 :       CALL qs_rho_release(perturbed_density)
     305          252 :       DEALLOCATE (perturbed_density)
     306          252 :       CALL xc_rho_set_release(rho_set, auxbas_pw_pool)
     307          252 :       CALL xc_dset_release(deriv_set)
     308              : 
     309          252 :       CALL timestop(handle)
     310              : 
     311         6048 :    END SUBROUTINE apply_op_constant_term
     312              : 
     313              : ! **************************************************************************************************
     314              : !> \brief Calculate the derivative of the Hartree term due to the core charge density
     315              : !> \param qs_env ...
     316              : !> \param dcdr_env ...
     317              : !> \author Edward Ditler
     318              : ! **************************************************************************************************
     319           72 :    SUBROUTINE d_core_charge_density_dR(qs_env, dcdr_env)
     320              :       ! drho_core contribution
     321              :       ! sum over all directions
     322              :       ! output in ao x ao
     323              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     324              :       TYPE(dcdr_env_type)                                :: dcdr_env
     325              : 
     326              :       CHARACTER(len=*), PARAMETER :: routineN = 'd_core_charge_density_dR'
     327              : 
     328              :       INTEGER                                            :: beta, handle
     329              :       TYPE(cp_logger_type), POINTER                      :: logger
     330              :       TYPE(dft_control_type), POINTER                    :: dft_control
     331              :       TYPE(pw_c1d_gs_type)                               :: drho_g, v_hartree_gspace
     332              :       TYPE(pw_env_type), POINTER                         :: pw_env
     333              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
     334           72 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
     335              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     336              :       TYPE(pw_r3d_rs_type)                               :: v_hartree_rspace
     337              :       TYPE(qs_rho_type), POINTER                         :: rho
     338              : 
     339           72 :       CALL timeset(routineN, handle)
     340              : 
     341           72 :       logger => cp_get_default_logger()
     342              : 
     343           72 :       NULLIFY (pw_env, auxbas_pw_pool, pw_pools, poisson_env, dft_control, &
     344           72 :                rho)
     345              : 
     346              :       CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, rho=rho, &
     347           72 :                       dft_control=dft_control)
     348              : 
     349              :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, poisson_env=poisson_env, &
     350           72 :                       pw_pools=pw_pools)
     351              : 
     352              :       ! Create the Hartree potential grids in real and reciprocal space.
     353           72 :       CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
     354           72 :       CALL auxbas_pw_pool%create_pw(v_hartree_rspace)
     355              :       ! Create the grid for the derivative of the core potential
     356           72 :       CALL auxbas_pw_pool%create_pw(drho_g)
     357              : 
     358          288 :       DO beta = 1, 3
     359          216 :          CALL pw_zero(v_hartree_gspace)
     360          216 :          CALL pw_zero(v_hartree_rspace)
     361          216 :          CALL pw_zero(drho_g)
     362              : 
     363              :          ! Calculate the Hartree potential on the perturbed density and Poisson solve it
     364              :          CALL calculate_drho_core(drho_core=drho_g, qs_env=qs_env, &
     365          216 :                                   beta=beta, lambda=dcdr_env%lambda)
     366              :          CALL pw_poisson_solve(poisson_env, drho_g, &
     367          216 :                                vhartree=v_hartree_gspace)
     368          216 :          CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
     369          216 :          CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
     370              : 
     371              :          ! Calculate the integrals
     372              :          CALL integrate_v_rspace(v_rspace=v_hartree_rspace, &
     373              :                                  hmat=dcdr_env%matrix_core_charge_1(beta), &
     374              :                                  qs_env=qs_env, &
     375          288 :                                  calculate_forces=.FALSE.)
     376              :       END DO
     377              : 
     378           72 :       CALL auxbas_pw_pool%give_back_pw(drho_g)
     379           72 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace)
     380           72 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
     381              : 
     382           72 :       CALL timestop(handle)
     383           72 :    END SUBROUTINE d_core_charge_density_dR
     384              : 
     385              : ! **************************************************************************************************
     386              : !> \brief Core Hamiltonian contributions to the operator (the pseudopotentials)
     387              : !> \param qs_env ...
     388              : !> \param dcdr_env ..
     389              : !> \author Edward Ditler
     390              : ! **************************************************************************************************
     391           72 :    SUBROUTINE core_dR(qs_env, dcdr_env)
     392              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     393              :       TYPE(dcdr_env_type)                                :: dcdr_env
     394              : 
     395              :       CHARACTER(LEN=*), PARAMETER                        :: routineN = 'core_dR'
     396              : 
     397              :       CHARACTER(LEN=default_string_length)               :: my_basis_type
     398              :       INTEGER                                            :: handle, nder
     399              :       LOGICAL                                            :: calculate_forces
     400           72 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
     401           72 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_h, matrix_p_pass
     402              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     403              :       TYPE(qs_rho_type), POINTER                         :: rho
     404              : 
     405           72 :       CALL timeset(routineN, handle)
     406              : 
     407           72 :       CALL get_qs_env(qs_env=qs_env, ks_env=ks_env)
     408           72 :       CALL get_ks_env(ks_env=ks_env, rho=rho)
     409           72 :       CALL qs_rho_get(rho, rho_ao=rho_ao)
     410              : 
     411           72 :       nder = 1
     412           72 :       calculate_forces = .FALSE.
     413              : 
     414              :       my_basis_type = "ORB"
     415              : 
     416           72 :       NULLIFY (matrix_h)
     417           72 :       matrix_p_pass(1:1, 1:1) => rho_ao(1:1)
     418              :       CALL core_matrices(qs_env, matrix_h, matrix_p_pass, calculate_forces, nder, &
     419           72 :                          dcdr_env=dcdr_env)
     420              : 
     421           72 :       CALL timestop(handle)
     422              : 
     423           72 :    END SUBROUTINE core_dR
     424              : 
     425              : ! **************************************************************************************************
     426              : !> \brief The derivatives of the basis functions going into the HXC potential wrt nuclear positions
     427              : !> \param qs_env ...
     428              : !> \param dcdr_env ...
     429              : !> \author Edward Ditler
     430              : ! **************************************************************************************************
     431           72 :    SUBROUTINE d_vhxc_dR(qs_env, dcdr_env)
     432              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     433              :       TYPE(dcdr_env_type)                                :: dcdr_env
     434              : 
     435              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'd_vhxc_dR'
     436              : 
     437              :       INTEGER                                            :: handle, idir, ispin
     438           72 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: rho_ao
     439              :       TYPE(pw_c1d_gs_type)                               :: drho_g_total, v_hartree_gspace
     440           72 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: drho_g
     441              :       TYPE(pw_env_type), POINTER                         :: pw_env
     442              :       TYPE(pw_poisson_type), POINTER                     :: poisson_env
     443           72 :       TYPE(pw_pool_p_type), DIMENSION(:), POINTER        :: pw_pools
     444              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     445              :       TYPE(pw_r3d_rs_type)                               :: drho_r_total, v_hartree_rspace
     446          144 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: drho_r, dtau_r, rho_r, v_xc, v_xc_tau
     447              :       TYPE(qs_rho_type), POINTER                         :: rho
     448              :       TYPE(section_vals_type), POINTER                   :: input, xc_section
     449              :       TYPE(xc_derivative_set_type)                       :: my_deriv_set
     450              :       TYPE(xc_rho_set_type)                              :: my_rho_set
     451              : 
     452           72 :       CALL timeset(routineN, handle)
     453              : 
     454              :       CALL get_qs_env(qs_env=qs_env, &
     455              :                       pw_env=pw_env, &
     456              :                       input=input, &
     457           72 :                       rho=rho)
     458           72 :       CALL qs_rho_get(rho, rho_ao=rho_ao, rho_r=rho_r)
     459              : 
     460           72 :       xc_section => section_vals_get_subs_vals(input, "DFT%XC")
     461              : 
     462              :       ! get the tmp grids
     463          300 :       ALLOCATE (drho_r(dcdr_env%nspins))
     464          300 :       ALLOCATE (drho_g(dcdr_env%nspins))
     465              : 
     466              :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
     467           72 :                       pw_pools=pw_pools, poisson_env=poisson_env)
     468           72 :       CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
     469           72 :       CALL auxbas_pw_pool%create_pw(v_hartree_rspace)
     470              : 
     471          156 :       DO ispin = 1, dcdr_env%nspins
     472           84 :          CALL auxbas_pw_pool%create_pw(drho_r(ispin))
     473          156 :          CALL auxbas_pw_pool%create_pw(drho_g(ispin))
     474              :       END DO
     475           72 :       CALL auxbas_pw_pool%create_pw(drho_g_total)
     476           72 :       CALL auxbas_pw_pool%create_pw(drho_r_total)
     477              : 
     478          288 :       DO idir = 1, 3
     479          216 :          CALL pw_zero(v_hartree_gspace)
     480          216 :          CALL pw_zero(v_hartree_rspace)
     481          216 :          CALL pw_zero(drho_g_total)
     482          216 :          CALL pw_zero(drho_r_total)
     483              : 
     484          468 :          DO ispin = 1, dcdr_env%nspins
     485          252 :             CALL pw_zero(drho_r(ispin))
     486          252 :             CALL pw_zero(drho_g(ispin))
     487              : 
     488              :             ! Get the density
     489              :             CALL calculate_drho_elec_dR(matrix_p=rho_ao(ispin)%matrix, &
     490              :                                         drho=drho_r(ispin), &
     491              :                                         drho_gspace=drho_g(ispin), &
     492              :                                         qs_env=qs_env, &
     493          252 :                                         beta=idir, lambda=dcdr_env%lambda)
     494              : 
     495          252 :             CALL pw_axpy(drho_g(ispin), drho_g_total)
     496          468 :             CALL pw_axpy(drho_r(ispin), drho_r_total)
     497              :          END DO
     498              :          ! Get the Hartree potential corresponding to the perturbed density
     499              :          CALL pw_poisson_solve(poisson_env, drho_g_total, &
     500          216 :                                vhartree=v_hartree_gspace)
     501          216 :          CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
     502              : 
     503              :          ! Get the XC potential corresponding to the perturbed density
     504              :          CALL xc_prep_2nd_deriv(my_deriv_set, my_rho_set, &
     505              :                                 rho_r, auxbas_pw_pool, &
     506          216 :                                 xc_section=xc_section)
     507              : 
     508          216 :          NULLIFY (dtau_r)
     509              :          CALL xc_calc_2nd_deriv(v_xc, v_xc_tau, my_deriv_set, my_rho_set, &
     510          216 :                                 drho_r, drho_g, dtau_r, auxbas_pw_pool, xc_section, gapw=.FALSE.)
     511          216 :          IF (ASSOCIATED(v_xc_tau)) CPABORT("Meta functionals are not supported!")
     512              : 
     513          216 :          CALL xc_dset_release(my_deriv_set)
     514          216 :          CALL xc_rho_set_release(my_rho_set)
     515              : 
     516              :          !-------------------------------!
     517              :          ! Add both hartree and xc terms !
     518              :          !-------------------------------!
     519          468 :          DO ispin = 1, dcdr_env%nspins
     520              :             ! Can the dvol be different?
     521          252 :             CALL pw_scale(v_xc(ispin), v_xc(ispin)%pw_grid%dvol)
     522          252 :             CALL pw_axpy(v_hartree_rspace, v_xc(ispin), v_hartree_rspace%pw_grid%dvol)
     523              : 
     524              :             CALL integrate_v_rspace(v_rspace=v_xc(ispin), &
     525              :                                     hmat=dcdr_env%matrix_d_vhxc_dR(idir, ispin), &
     526              :                                     qs_env=qs_env, &
     527          252 :                                     calculate_forces=.FALSE.)
     528              : 
     529              :             ! v_xc gets allocated again in xc_calc_2nd_deriv
     530          468 :             CALL auxbas_pw_pool%give_back_pw(v_xc(ispin))
     531              :          END DO ! ispin
     532          288 :          DEALLOCATE (v_xc)
     533              :       END DO ! idir
     534              : 
     535           72 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
     536           72 :       CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace)
     537           72 :       CALL auxbas_pw_pool%give_back_pw(drho_g_total)
     538           72 :       CALL auxbas_pw_pool%give_back_pw(drho_r_total)
     539              : 
     540          156 :       DO ispin = 1, dcdr_env%nspins
     541           84 :          CALL auxbas_pw_pool%give_back_pw(drho_g(ispin))
     542          156 :          CALL auxbas_pw_pool%give_back_pw(drho_r(ispin))
     543              :       END DO
     544              : 
     545           72 :       DEALLOCATE (drho_g)
     546           72 :       DEALLOCATE (drho_r)
     547              : 
     548           72 :       CALL timestop(handle)
     549              : 
     550         1584 :    END SUBROUTINE d_vhxc_dR
     551              : 
     552              : ! **************************************************************************************************
     553              : !> \brief The derivatives of the basis functions over which the HXC potential is integrated,
     554              : !>          so < da/dR | Vhxc | b >
     555              : !> \param qs_env ...
     556              : !> \param dcdr_env ...
     557              : !> \author Edward Ditler
     558              : ! **************************************************************************************************
     559           72 :    SUBROUTINE vhxc_R_perturbed_basis_functions(qs_env, dcdr_env)
     560              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     561              :       TYPE(dcdr_env_type)                                :: dcdr_env
     562              : 
     563              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'vhxc_R_perturbed_basis_functions'
     564              : 
     565              :       INTEGER                                            :: handle, ispin
     566           72 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_vhxc_dbasis
     567           72 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_p
     568              :       TYPE(pw_env_type), POINTER                         :: pw_env
     569              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     570           72 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: v_hxc_r, v_tau_rspace
     571              :       TYPE(pw_r3d_rs_type), POINTER                      :: v_hartree_r
     572              :       TYPE(qs_energy_type), POINTER                      :: energy
     573              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     574              :       TYPE(qs_rho_type), POINTER                         :: rho_struct
     575              :       TYPE(section_vals_type), POINTER                   :: input, xc_section
     576              : 
     577           72 :       CALL timeset(routineN, handle)
     578              : 
     579           72 :       NULLIFY (rho_struct, energy, input, ks_env, pw_env, matrix_p)
     580              :       CALL get_qs_env(qs_env, &
     581              :                       rho=rho_struct, &
     582              :                       energy=energy, &
     583              :                       input=input, &
     584              :                       ks_env=ks_env, &
     585              :                       pw_env=pw_env, &
     586           72 :                       v_hartree_rspace=v_hartree_r)
     587           72 :       CALL qs_rho_get(rho_struct, rho_ao_kp=matrix_p)
     588           72 :       xc_section => section_vals_get_subs_vals(input, "DFT%XC")
     589              : 
     590           72 :       NULLIFY (auxbas_pw_pool)
     591           72 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
     592              : 
     593              :       ! *** calculate the xc potential on the pw density ***
     594              :       ! *** associates v_hxc_r if the xc potential needs to be computed.
     595              :       ! If we do wavefunction fitting, we need the vxc_potential in the auxiliary basis set
     596           72 :       NULLIFY (v_hxc_r, v_tau_rspace)
     597              :       CALL qs_vxc_create(ks_env=ks_env, rho_struct=rho_struct, xc_section=xc_section, &
     598           72 :                          vxc_rho=v_hxc_r, vxc_tau=v_tau_rspace, exc=energy%exc)
     599              : 
     600          156 :       DO ispin = 1, dcdr_env%nspins
     601           84 :          CALL pw_scale(v_hxc_r(ispin), v_hxc_r(ispin)%pw_grid%dvol)
     602              : 
     603              :          ! sum up potentials and integrate
     604           84 :          CALL pw_axpy(v_hartree_r, v_hxc_r(ispin), 1._dp)
     605              : 
     606           84 :          matrix_vhxc_dbasis => dcdr_env%matrix_vhxc_perturbed_basis(ispin, :)
     607              :          CALL integrate_v_dbasis(v_rspace=v_hxc_r(ispin), &
     608              :                                  matrix_p=matrix_p(ispin, 1)%matrix, &
     609              :                                  matrix_vhxc_dbasis=matrix_vhxc_dbasis, &
     610              :                                  qs_env=qs_env, &
     611           84 :                                  lambda=dcdr_env%lambda)
     612              : 
     613          156 :          CALL auxbas_pw_pool%give_back_pw(v_hxc_r(ispin))
     614              :       END DO
     615              : 
     616           72 :       DEALLOCATE (v_hxc_r)
     617              : 
     618           72 :       CALL timestop(handle)
     619           72 :    END SUBROUTINE vhxc_R_perturbed_basis_functions
     620              : 
     621              : ! **************************************************************************************************
     622              : !> \brief Enforce that one of the basis functions in < a | O | b > is centered on atom lambda.
     623              : !> \param matrix ...
     624              : !> \param qs_kind_set ...
     625              : !> \param basis_type ...
     626              : !> \param sab_nl ...
     627              : !> \param lambda Atom index
     628              : !> \param direction_Or True: < a | O | b==lambda >, False: < a==lambda | O | b >
     629              : ! **************************************************************************************************
     630         2610 :    SUBROUTINE hr_mult_by_delta_1d(matrix, qs_kind_set, basis_type, sab_nl, lambda, direction_Or)
     631              : 
     632              :       TYPE(dbcsr_type), POINTER                          :: matrix
     633              :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     634              :       CHARACTER(LEN=*), INTENT(IN)                       :: basis_type
     635              :       TYPE(neighbor_list_set_p_type), DIMENSION(:), &
     636              :          POINTER                                         :: sab_nl
     637              :       INTEGER, INTENT(IN)                                :: lambda
     638              :       LOGICAL, INTENT(IN)                                :: direction_Or
     639              : 
     640              :       CHARACTER(len=*), PARAMETER :: routineN = 'hr_mult_by_delta_1d'
     641              : 
     642              :       INTEGER                                            :: handle, iatom, icol, ikind, irow, jatom, &
     643              :                                                             jkind, ldsab, mepos, nkind, nseta, &
     644              :                                                             nsetb, nthread
     645              :       INTEGER, DIMENSION(3)                              :: cell
     646         2610 :       INTEGER, DIMENSION(:), POINTER                     :: la_max, la_min, lb_max, lb_min, npgfa, &
     647         2610 :                                                             npgfb, nsgfa, nsgfb
     648         2610 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgfa, first_sgfb
     649              :       LOGICAL                                            :: do_symmetric, found
     650              :       REAL(KIND=dp), DIMENSION(3)                        :: rab
     651         2610 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: set_radius_a, set_radius_b
     652         2610 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: k_block, rpgfa, rpgfb, scon_a, scon_b, &
     653         2610 :                                                             zeta, zetb
     654         2610 :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list
     655              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set_a, basis_set_b
     656              :       TYPE(neighbor_list_iterator_p_type), &
     657         2610 :          DIMENSION(:), POINTER                           :: nl_iterator
     658              : 
     659         2610 :       CALL timeset(routineN, handle)
     660              : 
     661         2610 :       nkind = SIZE(qs_kind_set)
     662              : 
     663              :       ! check for symmetry
     664         2610 :       CPASSERT(SIZE(sab_nl) > 0)
     665         2610 :       CALL get_neighbor_list_set_p(neighbor_list_sets=sab_nl, symmetric=do_symmetric)
     666              : 
     667              :       ! prepare basis set
     668        13050 :       ALLOCATE (basis_set_list(nkind))
     669         2610 :       CALL basis_set_list_setup(basis_set_list, basis_type, qs_kind_set)
     670              : 
     671              :       ! *** Allocate work storage ***
     672         2610 :       ldsab = get_memory_usage(qs_kind_set, basis_type)
     673              : 
     674              :       nthread = 1
     675         2610 : !$    nthread = omp_get_max_threads()
     676              :       ! Iterate of neighbor list
     677         2610 :       CALL neighbor_list_iterator_create(nl_iterator, sab_nl, nthread=nthread)
     678              : 
     679              : !$OMP PARALLEL DEFAULT(NONE) &
     680              : !$OMP SHARED (nthread,ldsab,nl_iterator, do_symmetric) &
     681              : !$OMP SHARED (ncoset,matrix,basis_set_list) &
     682              : !$OMP SHARED (direction_or, lambda) &
     683              : !$OMP PRIVATE (k_block,mepos,ikind,jkind,iatom,jatom,rab,cell) &
     684              : !$OMP PRIVATE (basis_set_a,basis_set_b) &
     685              : !$OMP PRIVATE (first_sgfa, la_max, la_min, npgfa, nsgfa, nseta, rpgfa, set_radius_a) &
     686              : !$OMP PRIVATE (zeta, first_sgfb, lb_max, lb_min, npgfb, nsetb, rpgfb, set_radius_b, nsgfb) &
     687         2610 : !$OMP PRIVATE (zetb, scon_a, scon_b, irow, icol, found)
     688              : 
     689              :       mepos = 0
     690              : !$    mepos = omp_get_thread_num()
     691              : 
     692              :       DO WHILE (neighbor_list_iterate(nl_iterator, mepos=mepos) == 0)
     693              :          CALL get_iterator_info(nl_iterator, mepos=mepos, ikind=ikind, jkind=jkind, &
     694              :                                 iatom=iatom, jatom=jatom, r=rab, cell=cell)
     695              :          basis_set_a => basis_set_list(ikind)%gto_basis_set
     696              :          IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
     697              :          basis_set_b => basis_set_list(jkind)%gto_basis_set
     698              :          IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
     699              :          ! basis ikind
     700              :          first_sgfa => basis_set_a%first_sgf
     701              :          la_max => basis_set_a%lmax
     702              :          la_min => basis_set_a%lmin
     703              :          npgfa => basis_set_a%npgf
     704              :          nseta = basis_set_a%nset
     705              :          nsgfa => basis_set_a%nsgf_set
     706              :          rpgfa => basis_set_a%pgf_radius
     707              :          set_radius_a => basis_set_a%set_radius
     708              :          scon_a => basis_set_a%scon
     709              :          zeta => basis_set_a%zet
     710              :          ! basis jkind
     711              :          first_sgfb => basis_set_b%first_sgf
     712              :          lb_max => basis_set_b%lmax
     713              :          lb_min => basis_set_b%lmin
     714              :          npgfb => basis_set_b%npgf
     715              :          nsetb = basis_set_b%nset
     716              :          nsgfb => basis_set_b%nsgf_set
     717              :          rpgfb => basis_set_b%pgf_radius
     718              :          set_radius_b => basis_set_b%set_radius
     719              :          scon_b => basis_set_b%scon
     720              :          zetb => basis_set_b%zet
     721              : 
     722              :          IF (do_symmetric) THEN
     723              :             IF (iatom <= jatom) THEN
     724              :                irow = iatom
     725              :                icol = jatom
     726              :             ELSE
     727              :                irow = jatom
     728              :                icol = iatom
     729              :             END IF
     730              :          ELSE
     731              :             irow = iatom
     732              :             icol = jatom
     733              :          END IF
     734              : 
     735              :          NULLIFY (k_block)
     736              :          CALL dbcsr_get_block_p(matrix, irow, icol, k_block, found)
     737              :          CPASSERT(found)
     738              : 
     739              :          IF (direction_Or) THEN
     740              :             IF (jatom /= lambda) k_block(:, :) = 0._dp
     741              :          ELSE IF (.NOT. direction_Or) THEN
     742              :             IF (iatom /= lambda) k_block(:, :) = 0._dp
     743              :          END IF
     744              :       END DO
     745              : !$OMP END PARALLEL
     746         2610 :       CALL neighbor_list_iterator_release(nl_iterator)
     747              : 
     748              :       ! Release work storage
     749         2610 :       DEALLOCATE (basis_set_list)
     750              : 
     751         2610 :       CALL timestop(handle)
     752              : 
     753         5220 :    END SUBROUTINE hr_mult_by_delta_1d
     754              : 
     755              : END MODULE qs_dcdr_ao
        

Generated by: LCOV version 2.0-1