LCOV - code coverage report
Current view: top level - src - qs_dcdr_ao.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:70636b1) Lines: 97.7 % 221 216
Test Date: 2026-02-11 07:00:35 Functions: 100.0 % 6 6

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

Generated by: LCOV version 2.0-1