(git:98357aa)
Loading...
Searching...
No Matches
hfx_pw_methods.F
Go to the documentation of this file.
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 Test routines for HFX caclulations using PW
10!>
11!>
12!> \par History
13!> refactoring 03-2011 [MI]
14!> Made GAPW compatible 12.2019 (A. Bussy)
15!> Refactored from hfx_admm_utils (JGH)
16!> \author MI
17! **************************************************************************************************
20 USE cell_types, ONLY: cell_type
22 USE cp_dbcsr_api, ONLY: dbcsr_type
24 USE cp_fm_types, ONLY: cp_fm_get_info,&
37 USE kinds, ONLY: dp
38 USE mathconstants, ONLY: fourpi
40 USE pw_env_types, ONLY: pw_env_type
42 USE pw_methods, ONLY: pw_copy,&
49 USE pw_types, ONLY: pw_c1d_gs_type,&
55 USE qs_mo_types, ONLY: get_mo_set,&
57#include "./base/base_uses.f90"
58
59 IMPLICIT NONE
60
61 PRIVATE
62
63 ! *** Public subroutines ***
64 PUBLIC :: pw_hfx
65
66 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'hfx_pw_methods'
67
68CONTAINS
69
70! **************************************************************************************************
71!> \brief computes the Hartree-Fock energy brute force in a pw basis
72!> \param qs_env ...
73!> \param ehfx ...
74!> \param hfx_section ...
75!> \param poisson_env ...
76!> \param auxbas_pw_pool ...
77!> \param irep ...
78!> \par History
79!> 12.2007 created [Joost VandeVondele]
80!> \note
81!> only computes the HFX energy, no derivatives as yet
82! **************************************************************************************************
83 SUBROUTINE pw_hfx(qs_env, ehfx, hfx_section, poisson_env, auxbas_pw_pool, irep)
84 TYPE(qs_environment_type), POINTER :: qs_env
85 REAL(kind=dp), INTENT(IN) :: ehfx
86 TYPE(section_vals_type), POINTER :: hfx_section
87 TYPE(pw_poisson_type), POINTER :: poisson_env
88 TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
89 INTEGER :: irep
90
91 CHARACTER(*), PARAMETER :: routinen = 'pw_hfx'
92
93 INTEGER :: blocksize, handle, ig, iloc, iorb, &
94 iorb_block, ispin, iw, jloc, jorb, &
95 jorb_block, norb, potential_type
96 LOGICAL :: do_kpoints, do_pw_hfx, explicit
97 REAL(kind=dp) :: exchange_energy, fraction, g2, g3d, gg, &
98 omega, pair_energy, rcut, scaling
99 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
100 TYPE(cell_type), POINTER :: cell
101 TYPE(cp_fm_type), POINTER :: mo_coeff
102 TYPE(cp_logger_type), POINTER :: logger
103 TYPE(dbcsr_type), POINTER :: mo_coeff_b
104 TYPE(dft_control_type), POINTER :: dft_control
105 TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array
106 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
107 TYPE(pw_c1d_gs_type) :: greenfn, pot_g, rho_g
108 TYPE(pw_env_type), POINTER :: pw_env
109 TYPE(pw_grid_type), POINTER :: grid
110 TYPE(pw_r3d_rs_type) :: rho_r
111 TYPE(pw_r3d_rs_type), ALLOCATABLE, DIMENSION(:) :: rho_i, rho_j
112 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
113 TYPE(section_vals_type), POINTER :: ip_section
114
115 CALL timeset(routinen, handle)
116 logger => cp_get_default_logger()
117
118 CALL section_vals_val_get(hfx_section, "PW_HFX", l_val=do_pw_hfx, i_rep_section=irep)
119
120 IF (do_pw_hfx) THEN
121 CALL section_vals_val_get(hfx_section, "FRACTION", r_val=fraction)
122 CALL section_vals_val_get(hfx_section, "PW_HFX_BLOCKSIZE", i_val=blocksize)
123
124 CALL get_qs_env(qs_env, mos=mo_array, pw_env=pw_env, dft_control=dft_control, &
125 cell=cell, particle_set=particle_set, do_kpoints=do_kpoints, &
126 atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set)
127 IF (do_kpoints) cpabort("PW HFX not implemented with K-points")
128
129 ! limit the blocksize by the number of orbitals
130 CALL get_mo_set(mo_set=mo_array(1), mo_coeff=mo_coeff)
131 CALL cp_fm_get_info(mo_coeff, ncol_global=norb)
132 blocksize = max(1, min(blocksize, norb))
133
134 CALL auxbas_pw_pool%create_pw(rho_r)
135 CALL auxbas_pw_pool%create_pw(rho_g)
136 CALL auxbas_pw_pool%create_pw(pot_g)
137
138 ALLOCATE (rho_i(blocksize))
139 ALLOCATE (rho_j(blocksize))
140
141 CALL auxbas_pw_pool%create_pw(greenfn)
142 ip_section => section_vals_get_subs_vals(hfx_section, "INTERACTION_POTENTIAL")
143 CALL section_vals_get(ip_section, explicit=explicit)
144 potential_type = do_potential_coulomb
145 IF (explicit) THEN
146 CALL section_vals_val_get(ip_section, "POTENTIAL_TYPE", i_val=potential_type)
147 END IF
148 IF (potential_type == do_potential_coulomb) THEN
149 CALL pw_copy(poisson_env%green_fft%influence_fn, greenfn)
150 ELSE IF (potential_type == do_potential_truncated) THEN
151 CALL section_vals_val_get(ip_section, "CUTOFF_RADIUS", r_val=rcut)
152 grid => poisson_env%green_fft%influence_fn%pw_grid
153 DO ig = grid%first_gne0, grid%ngpts_cut_local
154 g2 = grid%gsq(ig)
155 gg = sqrt(g2)
156 g3d = fourpi/g2
157 greenfn%array(ig) = g3d*(1.0_dp - cos(rcut*gg))
158 END DO
159 IF (grid%have_g0) THEN
160 greenfn%array(1) = 0.5_dp*fourpi*rcut*rcut
161 END IF
162 ELSE IF (potential_type == do_potential_short) THEN
163 CALL section_vals_val_get(ip_section, "OMEGA", r_val=omega)
164 IF (omega > 0.0_dp) omega = 0.25_dp/(omega*omega)
165 grid => poisson_env%green_fft%influence_fn%pw_grid
166 DO ig = grid%first_gne0, grid%ngpts_cut_local
167 g2 = grid%gsq(ig)
168 gg = -omega*g2
169 g3d = fourpi/g2
170 greenfn%array(ig) = g3d*(1.0_dp - exp(gg))
171 END DO
172 IF (grid%have_g0) greenfn%array(1) = 0.0_dp
173 ELSE
174 cpwarn("PW_SCF: Potential type not supported, calculation uses Coulomb potential.")
175 END IF
176
177 DO iorb_block = 1, blocksize
178 CALL rho_i(iorb_block)%create(rho_r%pw_grid)
179 CALL rho_j(iorb_block)%create(rho_r%pw_grid)
180 END DO
181
182 exchange_energy = 0.0_dp
183
184 DO ispin = 1, SIZE(mo_array)
185 CALL get_mo_set(mo_set=mo_array(ispin), mo_coeff=mo_coeff, mo_coeff_b=mo_coeff_b)
186
187 IF (mo_array(ispin)%use_mo_coeff_b) THEN !fm->dbcsr
188 CALL copy_dbcsr_to_fm(mo_coeff_b, mo_coeff) !fm->dbcsr
189 END IF !fm->dbcsr
190
191 CALL cp_fm_get_info(mo_coeff, ncol_global=norb)
192
193 DO iorb_block = 1, norb, blocksize
194
195 DO iorb = iorb_block, min(iorb_block + blocksize - 1, norb)
196
197 iloc = iorb - iorb_block + 1
198 CALL calculate_wavefunction(mo_coeff, iorb, rho_i(iloc), rho_g, &
199 atomic_kind_set, qs_kind_set, cell, dft_control, particle_set, &
200 pw_env)
201
202 END DO
203
204 DO jorb_block = iorb_block, norb, blocksize
205
206 DO jorb = jorb_block, min(jorb_block + blocksize - 1, norb)
207
208 jloc = jorb - jorb_block + 1
209 CALL calculate_wavefunction(mo_coeff, jorb, rho_j(jloc), rho_g, &
210 atomic_kind_set, qs_kind_set, cell, dft_control, particle_set, &
211 pw_env)
212
213 END DO
214
215 DO iorb = iorb_block, min(iorb_block + blocksize - 1, norb)
216 iloc = iorb - iorb_block + 1
217 DO jorb = jorb_block, min(jorb_block + blocksize - 1, norb)
218 jloc = jorb - jorb_block + 1
219 IF (jorb < iorb) cycle
220
221 ! compute the pair density
222 CALL pw_zero(rho_r)
223 CALL pw_multiply(rho_r, rho_i(iloc), rho_j(jloc))
224
225 ! go the g-space and compute hartree energy
226 CALL pw_transfer(rho_r, rho_g)
227 CALL pw_poisson_solve(poisson_env, rho_g, pair_energy, pot_g, &
228 greenfn=greenfn)
229
230 ! sum up to the full energy
231 scaling = fraction
232 IF (SIZE(mo_array) == 1) scaling = scaling*2.0_dp
233 IF (iorb /= jorb) scaling = scaling*2.0_dp
234
235 exchange_energy = exchange_energy - scaling*pair_energy
236
237 END DO
238 END DO
239
240 END DO
241 END DO
242 END DO
243
244 DO iorb_block = 1, blocksize
245 CALL rho_i(iorb_block)%release()
246 CALL rho_j(iorb_block)%release()
247 END DO
248
249 CALL auxbas_pw_pool%give_back_pw(rho_r)
250 CALL auxbas_pw_pool%give_back_pw(rho_g)
251 CALL auxbas_pw_pool%give_back_pw(pot_g)
252 CALL auxbas_pw_pool%give_back_pw(greenfn)
253
254 iw = cp_print_key_unit_nr(logger, hfx_section, "HF_INFO", &
255 extension=".scfLog")
256
257 IF (iw > 0) THEN
258 WRITE (unit=iw, fmt="((T3,A,T61,F20.10))") &
259 "HF_PW_HFX| PW exchange energy:", exchange_energy
260 WRITE (unit=iw, fmt="((T3,A,T61,F20.10),/)") &
261 "HF_PW_HFX| Gaussian exchange energy:", ehfx
262 END IF
263
264 CALL cp_print_key_finished_output(iw, logger, hfx_section, "HF_INFO")
265
266 END IF
267
268 CALL timestop(handle)
269
270 END SUBROUTINE pw_hfx
271
272END MODULE hfx_pw_methods
Define the atomic kind types and their sub types.
Handles all functions related to the CELL.
Definition cell_types.F:15
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
DBCSR operations in CP2K.
subroutine, public copy_dbcsr_to_fm(matrix, fm)
Copy a DBCSR matrix to a BLACS matrix.
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
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
Test routines for HFX caclulations using PW.
subroutine, public pw_hfx(qs_env, ehfx, hfx_section, poisson_env, auxbas_pw_pool, irep)
computes the Hartree-Fock energy brute force in a pw basis
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public do_potential_truncated
integer, parameter, public do_potential_coulomb
integer, parameter, public do_potential_short
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Definition of mathematical constants and functions.
real(kind=dp), parameter, public fourpi
Define the data structure for the particle information.
container for various plainwaves related things
functions related to the poisson solver on regular grids
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Calculate the plane wave density by collocating the primitive Gaussian functions (pgf).
subroutine, public calculate_wavefunction(mo_vectors, ivector, rho, rho_gspace, atomic_kind_set, qs_kind_set, cell, dft_control, particle_set, pw_env, basis_type)
maps a given wavefunction on the grid
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, mimic, 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_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, 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, matrix_vhxc, 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, xcint_weights, 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, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_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, harris_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, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Define the quickstep kind type and their sub types.
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
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.
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a full matrix
type of a logger, at the moment it contains just a print level starting at which level it should be l...
contained for different pw related things
environment for the poisson solver
Manages a pool of grids (to be used for example as tmp objects), but can also be used to instantiate ...
Provides all information about a quickstep kind.