(git:c641773)
Loading...
Searching...
No Matches
qs_matrix_w.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 Utility subroutine for qs energy calculation
10!> \par History
11!> none
12!> \author MK (29.10.2002)
13! **************************************************************************************************
15 USE cp_cfm_types, ONLY: cp_cfm_create,&
21 USE cp_dbcsr_api, ONLY: dbcsr_p_type,&
28 USE cp_fm_types, ONLY: cp_fm_create,&
32 plus_u
34 USE kinds, ONLY: dp
38 USE kpoint_types, ONLY: kpoint_env_type,&
52 USE qs_mo_types, ONLY: get_mo_set,&
55 USE qs_rho_types, ONLY: qs_rho_get,&
58#include "./base/base_uses.f90"
59
60 IMPLICIT NONE
61
62 PRIVATE
63
64! *** Global parameters ***
65
66 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_matrix_w'
67
68 PUBLIC :: compute_matrix_w
69
70CONTAINS
71
72! **************************************************************************************************
73!> \brief Refactoring of qs_energies_scf. Moves computation of matrix_w
74!> into separate subroutine
75!> \param qs_env ...
76!> \param calc_forces ...
77!> \param include_lowdin_forces include the Lowdin overlap derivative before the W(k) transform
78!> \par History
79!> 05.2013 created [Florian Schiffmann]
80! **************************************************************************************************
81
82 SUBROUTINE compute_matrix_w(qs_env, calc_forces, include_lowdin_forces)
83 TYPE(qs_environment_type), POINTER :: qs_env
84 LOGICAL, INTENT(IN) :: calc_forces
85 LOGICAL, INTENT(IN), OPTIONAL :: include_lowdin_forces
86
87 CHARACTER(len=*), PARAMETER :: routinen = 'compute_matrix_w'
88
89 INTEGER :: handle, is, ispin, nao, nspin
90 LOGICAL :: do_kpoints, has_unit_metric
91 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s, matrix_w, &
92 mo_derivs, rho_ao
93 TYPE(dft_control_type), POINTER :: dft_control
94 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
95 TYPE(mo_set_type), POINTER :: mo_set
96 TYPE(qs_rho_type), POINTER :: rho
97 TYPE(scf_control_type), POINTER :: scf_control
98
99 CALL timeset(routinen, handle)
100
101 ! if calculate forces, time to compute the w matrix
102 CALL get_qs_env(qs_env, has_unit_metric=has_unit_metric)
103
104 IF (calc_forces .AND. .NOT. has_unit_metric) THEN
105 CALL get_qs_env(qs_env, do_kpoints=do_kpoints)
106
107 IF (do_kpoints) THEN
108 block
109 TYPE(cp_fm_type), DIMENSION(2) :: fmwork
110 TYPE(cp_fm_struct_type), POINTER :: ao_ao_fmstruct
111 TYPE(cp_fm_type), POINTER :: mo_coeff
112 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_kp, matrix_s_kp, &
113 matrix_w_kp
114 TYPE(kpoint_type), POINTER :: kpoints
115 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
116 POINTER :: sab_nl
117
118 CALL get_qs_env(qs_env, &
119 matrix_w_kp=matrix_w_kp, &
120 matrix_ks_kp=matrix_ks_kp, &
121 matrix_s_kp=matrix_s_kp, &
122 sab_orb=sab_nl, &
123 mos=mos, &
124 dft_control=dft_control, &
125 kpoints=kpoints, &
126 scf_control=scf_control)
127
128 CALL get_mo_set(mos(1), mo_coeff=mo_coeff, nao=nao)
129 CALL cp_fm_struct_create(fmstruct=ao_ao_fmstruct, nrow_global=nao, ncol_global=nao, &
130 template_fmstruct=mo_coeff%matrix_struct)
131
132 DO is = 1, SIZE(fmwork)
133 CALL cp_fm_create(fmwork(is), matrix_struct=ao_ao_fmstruct)
134 END DO
135 CALL cp_fm_struct_release(ao_ao_fmstruct)
136
137 ! energy weighted density matrices in k-space
138 IF (scf_control%use_ot) THEN
139 IF (ALLOCATED(kpoints%lowdin_v)) CALL plus_u(qs_env, matrix_h=matrix_ks_kp)
140 CALL kpoint_ot_energy_weighted_matrices(kpoints, matrix_ks_kp, fmwork(1))
141 ELSE
142 CALL kpoint_density_matrices(kpoints, energy_weighted=.true.)
143 END IF
144 ! Property callers need the ordinary energy-weighted density. Force callers
145 ! also need -dE_U/dS; transform both together, once, on the overlap support.
146 IF (PRESENT(include_lowdin_forces)) THEN
147 IF (include_lowdin_forces .AND. dft_control%dft_plus_u .AND. &
148 dft_control%plus_u_method_id == plus_u_lowdin) THEN
149 CALL plus_u(qs_env, matrix_w=matrix_w_kp)
150 END IF
151 END IF
152 ! energy weighted density matrices in real space
153 CALL kpoint_density_transform(kpoints, matrix_w_kp, .true., &
154 matrix_s_kp(1, 1)%matrix, sab_nl, fmwork)
155
156 DO is = 1, SIZE(fmwork)
157 CALL cp_fm_release(fmwork(is))
158 END DO
159
160 END block
161 ELSE
162
163 NULLIFY (dft_control, rho_ao)
164 CALL get_qs_env(qs_env, &
165 matrix_w=matrix_w, &
166 matrix_ks=matrix_ks, &
167 matrix_s=matrix_s, &
168 mo_derivs=mo_derivs, &
169 scf_control=scf_control, &
170 mos=mos, &
171 rho=rho, &
172 dft_control=dft_control)
173
174 CALL qs_rho_get(rho, rho_ao=rho_ao)
175
176 nspin = SIZE(mos)
177 DO ispin = 1, nspin
178 mo_set => mos(ispin)
179 IF (dft_control%roks) THEN
180 IF (scf_control%use_ot) THEN
181 IF (ispin > 1) THEN
182 ! not very elegant, indeed ...
183 CALL dbcsr_set(matrix_w(ispin)%matrix, 0.0_dp)
184 ELSE
185 CALL calculate_w_matrix_ot(mo_set, mo_derivs(ispin)%matrix, &
186 matrix_w(ispin)%matrix, matrix_s(1)%matrix)
187 END IF
188 ELSE
189 CALL calculate_w_matrix(mo_set=mo_set, &
190 matrix_ks=matrix_ks(ispin)%matrix, &
191 matrix_p=rho_ao(ispin)%matrix, &
192 matrix_w=matrix_w(ispin)%matrix)
193 END IF
194 ELSE
195 IF (scf_control%use_ot) THEN
196 CALL calculate_w_matrix_ot(mo_set, mo_derivs(ispin)%matrix, &
197 matrix_w(ispin)%matrix, matrix_s(1)%matrix)
198 ELSE
199 CALL calculate_w_matrix(mo_set, matrix_w(ispin)%matrix)
200 END IF
201 END IF
202 END DO
203
204 END IF
205
206 END IF
207
208 CALL timestop(handle)
209
210 END SUBROUTINE compute_matrix_w
211
212! **************************************************************************************************
213!> \brief Build OT energy-weighted densities using Hamiltonians on the k-point groups.
214!> \param kpoint K-point environment
215!> \param matrix_ks current real-space Kohn-Sham matrices
216!> \param ao_ao_fm square full-matrix template on the global parallel environment
217! **************************************************************************************************
218 SUBROUTINE kpoint_ot_energy_weighted_matrices(kpoint, matrix_ks, ao_ao_fm)
219
220 TYPE(kpoint_type), POINTER :: kpoint
221 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks
222 TYPE(cp_fm_type), INTENT(IN) :: ao_ao_fm
223
224 CHARACTER(LEN=*), PARAMETER :: routinen = 'kpoint_ot_energy_weighted_matrices'
225 COMPLEX(KIND=dp), PARAMETER :: zone = (1.0_dp, 0.0_dp), &
226 zzero = (0.0_dp, 0.0_dp)
227
228 INTEGER :: handle, ikpgr, ispin, kplocal, kpmax, &
229 nao, nmo, nspin
230 REAL(kind=dp), DIMENSION(:), POINTER :: occupation
231 TYPE(cp_cfm_type) :: h_k, hc, wmat
232 TYPE(cp_fm_pool_type), POINTER :: ao_ao_fm_pool
233 TYPE(cp_fm_struct_type), POINTER :: h_k_struct
234 TYPE(kpoint_env_type), POINTER :: kp
235 TYPE(kpoint_operator_context_type) :: op_ctx
236
237 CALL timeset(routinen, handle)
238 NULLIFY (ao_ao_fm_pool, h_k_struct, occupation)
239 cpassert(.NOT. kpoint%use_real_wfn)
240 cpassert(ASSOCIATED(matrix_ks))
241
242 nspin = SIZE(matrix_ks, 1)
243 kplocal = SIZE(kpoint%kp_env)
244 kpmax = maxval(kpoint%kp_dist(2, :) - kpoint%kp_dist(1, :) + 1)
245 CALL kpoint_operator_context_create(op_ctx, kpoint, ao_ao_fm, nspin)
246 ! Capture the current Hamiltonian after SCF, independently of OT's iteration caches.
247 CALL kpoint_operator_prepare(op_ctx, matrix_ks, kpoint_slot_ks)
248 CALL mpools_get(kpoint%mpools, ao_ao_fm_pool=ao_ao_fm_pool)
249 h_k_struct => fm_pool_get_el_struct(ao_ao_fm_pool)
250 CALL cp_cfm_create(h_k, h_k_struct)
251
252 DO ikpgr = 1, kpmax
253 DO ispin = 1, nspin
254 ! Every group calls the collective service; the AO/AO pool layout keeps H(k)
255 ! valid even on inactive rounds, when fallback assembly may communicate globally.
256 CALL kpoint_operator_get(op_ctx, ikpgr, ispin, matrix_ks, &
257 cmat=h_k, &
258 matrix_row=ispin, slot=kpoint_slot_ks)
259 IF (ikpgr <= kplocal) THEN
260 kp => kpoint%kp_env(ikpgr)%kpoint_env
261 CALL get_mo_set(kp%mos(ispin), nmo=nmo, &
262 occupation_numbers=occupation)
263 CALL cp_cfm_get_info(kp%mos(ispin)%cmo_coeff, nrow_global=nao)
264 cpassert(nmo >= 1)
265 cpassert(SIZE(occupation) >= nmo)
266
267 ! Smearing occupations are k-point resolved, whereas mo_set%homo is not.
268 CALL cp_cfm_create(hc, kp%mos(ispin)%cmo_coeff%matrix_struct, set_zero=.true.)
269 CALL parallel_gemm("N", "N", nao, nmo, nao, zone, h_k, &
270 kp%mos(ispin)%cmo_coeff, zzero, hc)
271 IF (ALLOCATED(kpoint%lowdin_v)) THEN
272 CALL lowdin_kp_apply_mo(kp, kpoint%lowdin_v(:, ispin), &
273 kp%mos(ispin)%cmo_coeff, hc)
274 END IF
275
276 CALL cp_cfm_create(wmat, kp%wmat(1, ispin)%matrix_struct)
277 CALL kpoint_ot_energy_weighted_density( &
278 kp%mos(ispin)%cmo_coeff, hc, occupation, wmat)
279 ! wmat remains split-FM storage; keep that conversion at its boundary.
280 CALL cp_cfm_to_fm(wmat, kp%wmat(1, ispin), kp%wmat(2, ispin))
281
282 CALL cp_cfm_release(wmat)
283 CALL cp_cfm_release(hc)
284 END IF
285 END DO
286 END DO
287
288 CALL cp_cfm_release(h_k)
289 CALL kpoint_operator_context_release(op_ctx)
290 CALL timestop(handle)
291
292 END SUBROUTINE kpoint_ot_energy_weighted_matrices
293
294END MODULE qs_matrix_w
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
subroutine, public cp_cfm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, matrix_struct, para_env)
Returns information about a full matrix.
subroutine, public cp_cfm_to_fm(msource, mtargetr, mtargeti)
Copy real and imaginary parts of a complex full matrix into separate real-value full matrices.
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_set(matrix, alpha)
...
pool for for elements that are retained and released
type(cp_fm_struct_type) function, pointer, public fm_pool_get_el_struct(pool)
returns the structure of the elements in this pool
represent the structure of a full matrix
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
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
Add the DFT+U contribution to the Hamiltonian matrix.
Definition dft_plus_u.F:18
subroutine, public lowdin_kp_apply_mo(kp, v, coeff, hc)
Add B diag(v) B**H C to the complex orbital gradient, without forming an AO matrix.
subroutine, public plus_u(qs_env, matrix_h, matrix_w)
Add the DFT+U contribution to the Hamiltonian matrix. Wrapper routine for all "+U" methods.
Definition dft_plus_u.F:134
collects all constants needed in input so that they can be used without circular dependencies
integer, parameter, public plus_u_lowdin
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
Routines needed for kpoint calculation.
subroutine, public kpoint_density_transform(kpoint, denmat, wtype, tempmat, sab_nl, fmwork, for_aux_fit, pmat_ext, overlap_rs, mixed_aux_orb)
generate real space density matrices in DBCSR format
subroutine, public kpoint_ot_energy_weighted_density(coeff, hc, occupation, wmat)
Build W(k) for noncanonical complex OT orbitals from H(k) C(k). The occupied-space Lagrange multiplie...
subroutine, public kpoint_density_matrices(kpoint, energy_weighted, for_aux_fit)
Calculate kpoint density matrices (rho(k), owned by kpoint groups).
Types and basic routines needed for a kpoint calculation.
basic linear algebra operations for full matrixes
collects routines that calculate density matrices
subroutine, public calculate_w_matrix_ot(mo_set, mo_deriv, w_matrix, s_matrix)
Calculate the W matrix from the MO coefs, MO derivs could overwrite the mo_derivs for increased memor...
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.
Assembly of real and complex k-point operators from real-space DBCSR matrices. Outputs use the distri...
subroutine, public kpoint_operator_context_create(ctx, kpoints, ao_ao_fm, nspin)
Create an empty operator context. The context owns no buffers after this call; they are created on fi...
subroutine, public kpoint_operator_prepare(ctx, rsmat, slot, prepared, matrix_row, target)
Prepare an immutable operator slot for group-local assembly. Refresh after the source values change,...
subroutine, public kpoint_operator_get(ctx, ikp, ispin, rsmat, cmat, fm_re, fm_im, mat_re, mat_im, matrix_row, slot, grid)
Assemble one operator for one global round and spin in one call: run the single-group transfer of the...
integer, parameter, public kpoint_slot_ks
subroutine, public kpoint_operator_context_release(ctx)
Release the context together with every buffer it created. Call once, at the end of the fixed-layout ...
wrapper for the pools of matrixes
subroutine, public mpools_get(mpools, ao_mo_fm_pools, ao_ao_fm_pool, mo_mo_fm_pools, ao_mosub_fm_pools, mosub_mosub_fm_pools, maxao_maxmo_fm_pool, maxao_maxao_fm_pool, maxmo_maxmo_fm_pool)
returns various attributes of the mpools (notably the pools contained in it)
Utility subroutine for qs energy calculation.
Definition qs_matrix_w.F:14
subroutine, public compute_matrix_w(qs_env, calc_forces, include_lowdin_forces)
Refactoring of qs_energies_scf. Moves computation of matrix_w into separate subroutine.
Definition qs_matrix_w.F:83
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, cmo_coeff)
Get the components of a MO set data structure.
Define the neighbor list data types and the corresponding functionality.
superstucture that hold various representations of the density and keeps track of which ones are vali...
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...
parameters that control an scf iteration
Represent a complex full matrix.
represent a pool of elements with the same structure
keeps the information about the structure of a full matrix
represent a full matrix
Keeps information about a specific k-point.
Contains information about kpoints.
Caller-owned assembly context for a fixed geometry and parallel layout. OT retains it for one inner S...
keeps the density in various representations, keeping track of which ones are valid.