(git:e7e05ae)
ec_methods.F
Go to the documentation of this file.
1 !--------------------------------------------------------------------------------------------------!
2 ! CP2K: A general program to perform molecular dynamics simulations !
3 ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4 ! !
5 ! SPDX-License-Identifier: GPL-2.0-or-later !
6 !--------------------------------------------------------------------------------------------------!
7 
8 ! **************************************************************************************************
9 !> \brief Routines used for Harris functional
10 !> Kohn-Sham calculation
11 !> \par History
12 !> 10.2020 created
13 !> \author Fabian Belleflamme
14 ! **************************************************************************************************
15 MODULE ec_methods
16  USE cp_blacs_env, ONLY: cp_blacs_env_type
17  USE cp_control_types, ONLY: dft_control_type
21  cp_fm_struct_type
22  USE cp_fm_types, ONLY: cp_fm_get_info,&
23  cp_fm_type
24  USE cp_log_handling, ONLY: cp_to_string
25  USE dbcsr_api, ONLY: dbcsr_init_p,&
26  dbcsr_type,&
27  dbcsr_type_no_symmetry
28  USE input_section_types, ONLY: section_vals_type
29  USE kinds, ONLY: dp
30  USE message_passing, ONLY: mp_para_env_type
31  USE pw_env_types, ONLY: pw_env_get,&
32  pw_env_type
33  USE pw_pool_types, ONLY: pw_pool_type
34  USE pw_types, ONLY: pw_c1d_gs_type,&
35  pw_r3d_rs_type
36  USE qs_environment_types, ONLY: get_qs_env,&
37  qs_environment_type,&
39  USE qs_kind_types, ONLY: get_qs_kind_set,&
40  qs_kind_type
41  USE qs_matrix_pools, ONLY: mpools_release,&
42  qs_matrix_pools_type
43  USE qs_mo_types, ONLY: allocate_mo_set,&
44  get_mo_set,&
45  init_mo_set,&
46  mo_set_type
47  USE qs_rho_types, ONLY: qs_rho_get,&
48  qs_rho_type
49  USE xc, ONLY: xc_calc_2nd_deriv,&
51  USE xc_derivative_set_types, ONLY: xc_derivative_set_type,&
54  xc_rho_set_type
55 #include "./base/base_uses.f90"
56 
57  IMPLICIT NONE
58 
59  PRIVATE
60 
61 ! *** Global parameters ***
62 
63  CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ec_methods'
64 
65  PUBLIC :: create_kernel
66  PUBLIC :: ec_mos_init
67 
68 CONTAINS
69 
70 ! **************************************************************************************************
71 !> \brief Creation of second derivative xc-potential
72 !> \param qs_env ...
73 !> \param vxc will contain the partially integrated second derivative
74 !> taken with respect to rho, evaluated in rho and folded with rho1
75 !> vxc is allocated here and needs to be deallocated by the caller.
76 !> \param vxc_tau ...
77 !> \param rho density at which derivatives were calculated
78 !> \param rho1_r density in r-space with which to fold
79 !> \param rho1_g density in g-space with which to fold
80 !> \param tau1_r ...
81 !> \param xc_section XC parameters of this potential
82 !> \param compute_virial Enable stress-tensor calculation
83 !> \param virial_xc Will contain GGA contribution of XC-kernel to stress-tensor
84 !> \date 11.2019
85 !> \author fbelle
86 ! **************************************************************************************************
87  SUBROUTINE create_kernel(qs_env, vxc, vxc_tau, rho, rho1_r, rho1_g, tau1_r, xc_section, compute_virial, virial_xc)
88 
89  TYPE(qs_environment_type), POINTER :: qs_env
90  TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: vxc, vxc_tau
91  TYPE(qs_rho_type), INTENT(IN), POINTER :: rho
92  TYPE(pw_r3d_rs_type), DIMENSION(:), INTENT(IN), &
93  POINTER :: rho1_r
94  TYPE(pw_c1d_gs_type), DIMENSION(:), INTENT(IN), &
95  POINTER :: rho1_g
96  TYPE(pw_r3d_rs_type), DIMENSION(:), INTENT(IN), &
97  POINTER :: tau1_r
98  TYPE(section_vals_type), INTENT(IN), POINTER :: xc_section
99  LOGICAL, INTENT(IN), OPTIONAL :: compute_virial
100  REAL(kind=dp), DIMENSION(3, 3), INTENT(INOUT), &
101  OPTIONAL :: virial_xc
102 
103  CHARACTER(LEN=*), PARAMETER :: routinen = 'create_kernel'
104 
105  INTEGER :: handle
106  TYPE(pw_env_type), POINTER :: pw_env
107  TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
108  TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, tau_r
109  TYPE(xc_derivative_set_type) :: deriv_set
110  TYPE(xc_rho_set_type) :: rho_set
111 
112  CALL timeset(routinen, handle)
113 
114  NULLIFY (auxbas_pw_pool, pw_env, rho_r, vxc, vxc_tau)
115 
116  CALL get_qs_env(qs_env, pw_env=pw_env)
117  CALL pw_env_get(pw_env=pw_env, auxbas_pw_pool=auxbas_pw_pool)
118  ! Get density
119  CALL qs_rho_get(rho, rho_r=rho_r, tau_r=tau_r)
120 
121  CALL xc_prep_2nd_deriv(deriv_set=deriv_set, & ! containing potentials
122  rho_set=rho_set, & ! density at which derivs are calculated
123  rho_r=rho_r, & ! place where derivative is evaluated
124  tau_r=tau_r, &
125  pw_pool=auxbas_pw_pool, & ! pool for grids
126  xc_section=xc_section)
127 
128  ! folding of second deriv with density in rho1_set
129  CALL xc_calc_2nd_deriv(v_xc=vxc, & ! XC-potential, rho-dependent
130  v_xc_tau=vxc_tau, & ! XC-potential, tau-dependent
131  deriv_set=deriv_set, & ! deriv of xc-potential
132  rho_set=rho_set, & ! density at which deriv are calculated
133  rho1_r=rho1_r, & ! density with which to fold
134  rho1_g=rho1_g, & ! density with which to fold
135  tau1_r=tau1_r, &
136  pw_pool=auxbas_pw_pool, & ! pool for grids
137  xc_section=xc_section, &
138  gapw=.false., &
139  compute_virial=compute_virial, &
140  virial_xc=virial_xc)
141 
142  ! Release second deriv stuff
143  CALL xc_dset_release(deriv_set)
144  CALL xc_rho_set_release(rho_set=rho_set, pw_pool=auxbas_pw_pool)
145 
146  CALL timestop(handle)
147 
148  END SUBROUTINE
149 
150 ! **************************************************************************************************
151 !> \brief Allocate and initiate molecular orbitals environment
152 !>
153 !> \param qs_env ...
154 !> \param matrix_s Used as template
155 !> \param
156 !>
157 !> \par History
158 !> 2020.10 created [Fabian Belleflamme]
159 !> \author Fabian Belleflamme
160 ! **************************************************************************************************
161  SUBROUTINE ec_mos_init(qs_env, matrix_s)
162  TYPE(qs_environment_type), POINTER :: qs_env
163  TYPE(dbcsr_type) :: matrix_s
164 
165  CHARACTER(len=*), PARAMETER :: routinen = 'ec_mos_init'
166 
167  INTEGER :: handle, ispin, multiplicity, n_ao, &
168  nelectron, nmo, nspins
169  INTEGER, DIMENSION(2) :: n_mo, nelectron_spin
170  REAL(dp) :: maxocc
171  TYPE(cp_blacs_env_type), POINTER :: blacs_env
172  TYPE(cp_fm_struct_type), POINTER :: fm_struct
173  TYPE(cp_fm_type), POINTER :: mo_coeff
174  TYPE(dbcsr_type), POINTER :: mo_coeff_b
175  TYPE(dft_control_type), POINTER :: dft_control
176  TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
177  TYPE(mp_para_env_type), POINTER :: para_env
178  TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
179  TYPE(qs_matrix_pools_type), POINTER :: my_mpools
180 
181  CALL timeset(routinen, handle)
182 
183  NULLIFY (blacs_env, dft_control, mo_coeff, mo_coeff_b, mos, my_mpools, qs_kind_set)
184 
185  CALL get_qs_env(qs_env=qs_env, &
186  dft_control=dft_control, &
187  blacs_env=blacs_env, &
188  qs_kind_set=qs_kind_set, &
189  nelectron_spin=nelectron_spin, &
190  para_env=para_env)
191  nspins = dft_control%nspins
192 
193  ! Start setup
194  CALL get_qs_kind_set(qs_kind_set, nsgf=n_ao, nelectron=nelectron)
195 
196  ! the total number of electrons
197  nelectron = nelectron - dft_control%charge
198  multiplicity = dft_control%multiplicity
199 
200  ! setting maxocc and n_mo
201  IF (dft_control%nspins == 1) THEN
202  maxocc = 2.0_dp
203  nelectron_spin(1) = nelectron
204  nelectron_spin(2) = 0
205  IF (modulo(nelectron, 2) == 0) THEN
206  n_mo(1) = nelectron/2
207  ELSE
208  n_mo(1) = int(nelectron/2._dp) + 1
209  END IF
210  n_mo(2) = 0
211  ELSE
212  maxocc = 1.0_dp
213 
214  ! The simplist spin distribution is written here. Special cases will
215  ! need additional user input
216  IF (modulo(nelectron + multiplicity - 1, 2) /= 0) THEN
217  cpabort("LSD: try to use a different multiplicity")
218  END IF
219 
220  nelectron_spin(1) = (nelectron + multiplicity - 1)/2
221  nelectron_spin(2) = (nelectron - multiplicity + 1)/2
222 
223  IF (nelectron_spin(2) < 0) THEN
224  cpabort("LSD: too few electrons for this multiplicity")
225  END IF
226 
227  n_mo(1) = nelectron_spin(1)
228  n_mo(2) = nelectron_spin(2)
229 
230  END IF
231 
232  ! Allocate MO set
233  ALLOCATE (mos(nspins))
234  DO ispin = 1, nspins
235  CALL allocate_mo_set(mo_set=mos(ispin), &
236  nao=n_ao, &
237  nmo=n_mo(ispin), &
238  nelectron=nelectron_spin(ispin), &
239  n_el_f=real(nelectron_spin(ispin), dp), &
240  maxocc=maxocc, &
241  flexible_electron_count=dft_control%relax_multiplicity)
242  END DO
243 
244  CALL set_qs_env(qs_env, mos=mos)
245 
246  ! finish initialization of the MOs
247  NULLIFY (mo_coeff, mo_coeff_b)
248  DO ispin = 1, SIZE(mos)
249  CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, mo_coeff_b=mo_coeff_b, &
250  nmo=nmo, nao=n_ao)
251 
252  IF (.NOT. ASSOCIATED(mo_coeff)) THEN
253  CALL cp_fm_struct_create(fm_struct, nrow_global=n_ao, &
254  ncol_global=nmo, para_env=para_env, &
255  context=blacs_env)
256 
257  CALL init_mo_set(mos(ispin), &
258  fm_struct=fm_struct, &
259  name="qs_env%mo"//trim(adjustl(cp_to_string(ispin))))
260  CALL cp_fm_struct_release(fm_struct)
261  END IF
262 
263  IF (.NOT. ASSOCIATED(mo_coeff_b)) THEN
264  CALL cp_fm_get_info(mos(ispin)%mo_coeff, ncol_global=nmo)
265  CALL dbcsr_init_p(mos(ispin)%mo_coeff_b)
266  CALL cp_dbcsr_m_by_n_from_row_template(mos(ispin)%mo_coeff_b, &
267  template=matrix_s, &
268  n=nmo, &
269  sym=dbcsr_type_no_symmetry)
270  END IF
271  END DO
272 
273  CALL mpools_release(mpools=my_mpools)
274 
275  CALL timestop(handle)
276 
277  END SUBROUTINE ec_mos_init
278 
279 END MODULE ec_methods
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
Definition: grid_common.h:117
methods related to the blacs parallel environment
Definition: cp_blacs_env.F:15
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
DBCSR operations in CP2K.
subroutine, public cp_dbcsr_m_by_n_from_row_template(matrix, template, n, sym, data_type)
Utility function to create dbcsr matrix, m x n matrix (n arbitrary) with the same processor grid and ...
represent the structure of a full matrix
Definition: cp_fm_struct.F:14
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
Definition: cp_fm_struct.F:125
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
Definition: cp_fm_struct.F:320
represent a full matrix distributed on many processors
Definition: cp_fm_types.F:15
subroutine, public cp_fm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, nrow_locals, ncol_locals, matrix_struct, para_env)
returns all kind of information about the full matrix
Definition: cp_fm_types.F:1016
various routines to log and control the output. The idea is that decisions about where to log should ...
Routines used for Harris functional Kohn-Sham calculation.
Definition: ec_methods.F:15
subroutine, public ec_mos_init(qs_env, matrix_s)
Allocate and initiate molecular orbitals environment.
Definition: ec_methods.F:162
subroutine, public create_kernel(qs_env, vxc, vxc_tau, rho, rho1_r, rho1_g, tau1_r, xc_section, compute_virial, virial_xc)
Creation of second derivative xc-potential.
Definition: ec_methods.F:88
objects that represent the structure of input sections and the data contained in an input section
Defines the basic variable types.
Definition: kinds.F:23
integer, parameter, public dp
Definition: kinds.F:34
Interface to the message passing library MPI.
container for various plainwaves related things
Definition: pw_env_types.F:14
subroutine, public pw_env_get(pw_env, pw_pools, cube_info, gridlevel_info, auxbas_pw_pool, auxbas_grid, auxbas_rs_desc, auxbas_rs_grid, rs_descs, rs_grids, xc_pw_pool, vdw_pw_pool, poisson_env, interp_section)
returns the various attributes of the pw env
Definition: pw_env_types.F:113
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Definition: pw_pool_types.F:24
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_RI_aux_kp, matrix_s, matrix_s_RI_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, ecoul_1c, rho0_s_rs, rho0_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, WannierCentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, rhs)
Get the QUICKSTEP environment.
subroutine, public set_qs_env(qs_env, super_cell, mos, qmmm, qmmm_periodic, ewald_env, ewald_pw, mpools, rho_external, external_vxc, mask, scf_control, rel_control, qs_charges, ks_env, ks_qmmm_env, wf_history, scf_env, active_space, input, oce, rho_atom_set, rho0_atom_set, rho0_mpole, run_rtp, rtp, rhoz_set, rhoz_tot, ecoul_1c, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, efield, linres_control, xas_env, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, ls_scf_env, do_transport, transport_env, lri_env, lri_density, exstate_env, ec_env, dispersion_env, gcp_env, mp2_env, bs_env, kg_env, force, kpoints, WannierCentres, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, rhs)
Set the QUICKSTEP environment.
Define the quickstep kind type and their sub types.
Definition: qs_kind_types.F:23
subroutine, public get_qs_kind_set(qs_kind_set, all_potential_present, tnadd_potential_present, gth_potential_present, sgp_potential_present, paw_atom_present, dft_plus_u_atom_present, maxcgf, maxsgf, maxco, maxco_proj, maxgtops, maxlgto, maxlprj, maxnset, maxsgf_set, ncgf, npgf, nset, nsgf, nshell, maxpol, maxlppl, maxlppnl, maxppnl, nelectron, maxder, max_ngrid_rad, max_sph_harm, maxg_iso_not0, lmax_rho0, basis_rcut, basis_type, total_zeff_corr)
Get attributes of an atomic kind set.
wrapper for the pools of matrixes
subroutine, public mpools_release(mpools)
releases the given mpools
Definition and initialisation of the mo data type.
Definition: qs_mo_types.F:22
subroutine, public allocate_mo_set(mo_set, nao, nmo, nelectron, n_el_f, maxocc, flexible_electron_count)
Allocates a mo set and partially initializes it (nao,nmo,nelectron, and flexible_electron_count are v...
Definition: qs_mo_types.F:206
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kTS, mu, flexible_electron_count)
Get the components of a MO set data structure.
Definition: qs_mo_types.F:397
subroutine, public init_mo_set(mo_set, fm_pool, fm_ref, fm_struct, name)
initializes an allocated mo_set. eigenvalues, mo_coeff, occupation_numbers are valid only after this ...
Definition: qs_mo_types.F:245
superstucture that hold various representations of the density and keeps track of which ones are vali...
Definition: qs_rho_types.F:18
subroutine, public qs_rho_get(rho_struct, rho_ao, rho_ao_im, rho_ao_kp, rho_ao_im_kp, rho_r, drho_r, rho_g, drho_g, tau_r, tau_g, rho_r_valid, drho_r_valid, rho_g_valid, drho_g_valid, tau_r_valid, tau_g_valid, tot_rho_r, tot_rho_g, rho_r_sccs, soft_valid, complex_rho_ao)
returns info about the density described by this object. If some representation is not available an e...
Definition: qs_rho_types.F:229
represent a group ofunctional derivatives
subroutine, public xc_dset_release(derivative_set)
releases a derivative set
contains the structure
subroutine, public xc_rho_set_release(rho_set, pw_pool)
releases the given rho_set
Exchange and Correlation functional calculations.
Definition: xc.F:17
subroutine, public xc_calc_2nd_deriv(v_xc, v_xc_tau, deriv_set, rho_set, rho1_r, rho1_g, tau1_r, pw_pool, xc_section, gapw, vxg, lsd_singlets, do_excitations, do_triplet, do_tddft, compute_virial, virial_xc)
Caller routine to calculate the second order potential in the direction of rho1_r.
Definition: xc.F:1523
subroutine, public xc_prep_2nd_deriv(deriv_set, rho_set, rho_r, pw_pool, xc_section, tau_r)
Prepare objects for the calculation of the 2nd derivatives of the density functional....
Definition: xc.F:5364