(git:9111030)
Loading...
Searching...
No Matches
qs_active_space_types.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 The types needed for the calculation of active space Hamiltonians
10!> \par History
11!> 04.2016 created [JGH]
12!> \author JGH
13! **************************************************************************************************
15 USE cp_dbcsr_api, ONLY: dbcsr_csr_destroy,&
16 dbcsr_csr_p_type,&
19 USE cp_fm_types, ONLY: cp_fm_release,&
22 USE kinds, ONLY: default_path_length,&
23 dp
24 USE message_passing, ONLY: mp_comm_null,&
33#include "./base/base_uses.f90"
34
35 IMPLICIT NONE
36 PRIVATE
37
38 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_active_space_types'
39
43
44! **************************************************************************************************
45!> \brief Quantities needed for AS determination
46!> \author JGH
47! **************************************************************************************************
48 TYPE eri_gpw_type
49 LOGICAL :: redo_poisson = .false.
50 LOGICAL :: store_wfn = .false.
51 REAL(KIND=dp) :: cutoff = 0.0_dp
52 REAL(KIND=dp) :: rel_cutoff = 0.0_dp
53 REAL(KIND=dp) :: eps_grid = 0.0_dp
54 REAL(KIND=dp) :: eps_filter = 0.0_dp
55 INTEGER :: print_level = 0
56 INTEGER :: group_size = 0
57 END TYPE eri_gpw_type
58
60 INTEGER :: method = 0
61 INTEGER :: operator = 0
62 LOGICAL :: enlarge_cell = .false.
63 REAL(kind=dp) :: omega = 0.0_dp
64 INTEGER, DIMENSION(3) :: periodicity = 0
65 REAL(kind=dp), DIMENSION(3) :: eri_cell = 0
66 REAL(kind=dp), DIMENSION(3) :: eri_cell_angles = 0
67 REAL(kind=dp) :: cutoff_radius = 0.0_dp
68 REAL(kind=dp) :: eps_integral = 0.0_dp
69 TYPE(eri_gpw_type) :: eri_gpw = eri_gpw_type()
70 TYPE(dbcsr_csr_p_type), &
71 DIMENSION(:), POINTER :: eri => null()
72 INTEGER :: norb = 0
73 TYPE(mp_para_env_type), POINTER :: para_env_sub => null()
74 TYPE(mp_comm_type) :: comm_exchange = mp_comm_null
75 CONTAINS
76 PROCEDURE :: eri_foreach => eri_type_eri_foreach
77 END TYPE eri_type
78
79! **************************************************************************************************
80!> \brief Abstract function object for the `eri_type_eri_foreach` method
81! **************************************************************************************************
82 TYPE, ABSTRACT :: eri_type_eri_element_func
83 CONTAINS
84 PROCEDURE(eri_type_eri_element_func_interface), DEFERRED :: func
86
88 INTEGER :: nelec_active = 0
89 INTEGER :: nelec_inactive = 0
90 INTEGER :: nelec_total = 0
91 INTEGER, POINTER, DIMENSION(:, :) :: active_orbitals => null()
92 INTEGER, POINTER, DIMENSION(:, :) :: inactive_orbitals => null()
93 INTEGER :: nmo_active = 0
94 INTEGER :: nmo_inactive = 0
95 INTEGER :: multiplicity = 0
96 INTEGER :: nspins = 0
97 LOGICAL :: restricted_orbitals = .false.
98 LOGICAL :: molecule = .false.
99 INTEGER :: model = 0
100 REAL(kind=dp) :: energy_total = 0.0_dp
101 REAL(kind=dp) :: energy_ref = 0.0_dp
102 REAL(kind=dp) :: energy_inactive = 0.0_dp
103 REAL(kind=dp) :: energy_active = 0.0_dp
104 REAL(kind=dp) :: alpha = 0.0_dp
105 INTEGER :: as_mixing_method = direct_mixing_nr
106 INTEGER :: as_mixing_iter = 0
107 INTEGER :: as_mixing_dim = 0
108 LOGICAL :: do_scf_embedding = .false.
109 LOGICAL :: qcschema = .false.
110 LOGICAL :: fcidump = .false.
111 CHARACTER(LEN=default_path_length) :: qcschema_filename = ''
112 TYPE(eri_type) :: eri = eri_type()
113 REAL(kind=dp), DIMENSION(:), POINTER :: as_mix_r_old => null()
114 REAL(kind=dp), DIMENSION(:), POINTER :: as_mix_weight => null()
115 REAL(kind=dp), DIMENSION(:), POINTER :: as_mix_x_old => null()
116 REAL(kind=dp), DIMENSION(:, :), POINTER :: as_mix_r_buffer => null()
117 REAL(kind=dp), DIMENSION(:, :), POINTER :: as_mix_x_buffer => null()
118 TYPE(mixing_storage_type), POINTER :: as_mixing_store => null()
119 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos_active => null()
120 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos_inactive => null()
121 TYPE(cp_fm_type), DIMENSION(:), POINTER :: p_active => null()
122 TYPE(cp_fm_type), DIMENSION(:), POINTER :: ks_sub => null()
123 TYPE(cp_fm_type), DIMENSION(:), POINTER :: vxc_sub => null()
124 TYPE(cp_fm_type), DIMENSION(:), POINTER :: h_sub => null()
125 TYPE(cp_fm_type), DIMENSION(:), POINTER :: fock_sub => null()
126 TYPE(cp_fm_type), DIMENSION(:), POINTER :: sab_sub => null()
127 TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: pmat_inactive => null()
128 TYPE(section_vals_type), POINTER :: xc_section => null()
129 END TYPE active_space_type
130
131 abstract INTERFACE
132! **************************************************************************************************
133!> \brief The function signature to be implemented by a child of `eri_type_eri_element_func`
134!> \param this object reference
135!> \param i i-index
136!> \param j j-index
137!> \param k k-index
138!> \param l l-index
139!> \param val value of the integral at (i,j,k.l)
140!> \return True if the ERI foreach loop should continue, false, if not
141! **************************************************************************************************
142 LOGICAL FUNCTION eri_type_eri_element_func_interface(this, i, j, k, l, val)
144 CLASS(eri_type_eri_element_func), INTENT(inout) :: this
145 INTEGER, INTENT(in) :: i, j, k, l
146 REAL(kind=dp), INTENT(in) :: val
148 END INTERFACE
149
150! **************************************************************************************************
151
152CONTAINS
153
154! **************************************************************************************************
155!> \brief Creates an active space environment type, nullifying all quantities.
156!> \param active_space_env the active space environment to be initialized
157! **************************************************************************************************
158 SUBROUTINE create_active_space_type(active_space_env)
159 TYPE(active_space_type), POINTER :: active_space_env
160
161 IF (ASSOCIATED(active_space_env)) THEN
162 CALL release_active_space_type(active_space_env)
163 END IF
164
165 ALLOCATE (active_space_env)
166 NULLIFY (active_space_env%active_orbitals, active_space_env%inactive_orbitals)
167 NULLIFY (active_space_env%mos_active, active_space_env%mos_inactive)
168 NULLIFY (active_space_env%ks_sub, active_space_env%p_active)
169 NULLIFY (active_space_env%vxc_sub, active_space_env%h_sub)
170 NULLIFY (active_space_env%fock_sub, active_space_env%pmat_inactive)
171 NULLIFY (active_space_env%as_mixing_store)
172 NULLIFY (active_space_env%as_mix_r_old, active_space_env%as_mix_weight)
173 NULLIFY (active_space_env%as_mix_x_old)
174 NULLIFY (active_space_env%as_mix_r_buffer, active_space_env%as_mix_x_buffer)
175
176 END SUBROUTINE create_active_space_type
177
178! **************************************************************************************************
179!> \brief Releases all quantities in the active space environment.
180!> \param active_space_env the active space environment to be released
181! **************************************************************************************************
182 SUBROUTINE release_active_space_type(active_space_env)
183 TYPE(active_space_type), POINTER :: active_space_env
184
185 INTEGER :: imo
186
187 IF (ASSOCIATED(active_space_env)) THEN
188
189 IF (ASSOCIATED(active_space_env%active_orbitals)) THEN
190 DEALLOCATE (active_space_env%active_orbitals)
191 END IF
192
193 IF (ASSOCIATED(active_space_env%inactive_orbitals)) THEN
194 DEALLOCATE (active_space_env%inactive_orbitals)
195 END IF
196
197 IF (ASSOCIATED(active_space_env%mos_active)) THEN
198 DO imo = 1, SIZE(active_space_env%mos_active)
199 CALL deallocate_mo_set(active_space_env%mos_active(imo))
200 END DO
201 DEALLOCATE (active_space_env%mos_active)
202 END IF
203
204 IF (ASSOCIATED(active_space_env%mos_inactive)) THEN
205 DO imo = 1, SIZE(active_space_env%mos_inactive)
206 CALL deallocate_mo_set(active_space_env%mos_inactive(imo))
207 END DO
208 DEALLOCATE (active_space_env%mos_inactive)
209 END IF
210
211 CALL release_eri_type(active_space_env%eri)
212
213 CALL cp_fm_release(active_space_env%p_active)
214 CALL cp_fm_release(active_space_env%ks_sub)
215 CALL cp_fm_release(active_space_env%vxc_sub)
216 CALL cp_fm_release(active_space_env%h_sub)
217 CALL cp_fm_release(active_space_env%fock_sub)
218 CALL cp_fm_release(active_space_env%sab_sub)
219
220 IF (ASSOCIATED(active_space_env%as_mixing_store)) THEN
221 CALL mixing_storage_release(active_space_env%as_mixing_store)
222 DEALLOCATE (active_space_env%as_mixing_store)
223 END IF
224 IF (ASSOCIATED(active_space_env%as_mix_r_old)) THEN
225 DEALLOCATE (active_space_env%as_mix_r_old)
226 END IF
227 IF (ASSOCIATED(active_space_env%as_mix_weight)) THEN
228 DEALLOCATE (active_space_env%as_mix_weight)
229 END IF
230 IF (ASSOCIATED(active_space_env%as_mix_x_old)) THEN
231 DEALLOCATE (active_space_env%as_mix_x_old)
232 END IF
233 IF (ASSOCIATED(active_space_env%as_mix_r_buffer)) THEN
234 DEALLOCATE (active_space_env%as_mix_r_buffer)
235 END IF
236 IF (ASSOCIATED(active_space_env%as_mix_x_buffer)) THEN
237 DEALLOCATE (active_space_env%as_mix_x_buffer)
238 END IF
239
240 IF (ASSOCIATED(active_space_env%pmat_inactive)) THEN
241 CALL dbcsr_deallocate_matrix_set(active_space_env%pmat_inactive)
242 END IF
243
244 DEALLOCATE (active_space_env)
245 END IF
246
247 END SUBROUTINE release_active_space_type
248
249! **************************************************************************************************
250!> \brief Releases the ERI environment type.
251!> \param eri_env the ERI environment to be released
252! **************************************************************************************************
253 SUBROUTINE release_eri_type(eri_env)
254 TYPE(eri_type) :: eri_env
255
256 INTEGER :: i
257
258 IF (ASSOCIATED(eri_env%eri)) THEN
259
260 DO i = 1, SIZE(eri_env%eri)
261 CALL dbcsr_csr_destroy(eri_env%eri(i)%csr_mat)
262 DEALLOCATE (eri_env%eri(i)%csr_mat)
263 END DO
264 CALL mp_para_env_release(eri_env%para_env_sub)
265 CALL eri_env%comm_exchange%free()
266 DEALLOCATE (eri_env%eri)
267
268 END IF
269
270 END SUBROUTINE release_eri_type
271
272! **************************************************************************************************
273!> \brief calculates combined index (ij)
274!> \param i Index j
275!> \param j Index i
276!> \param n Dimension in i or j direction
277!> \returns The combined index
278!> \par History
279!> 04.2016 created [JGH]
280! **************************************************************************************************
281 INTEGER FUNCTION csr_idx_to_combined(i, j, n) RESULT(ij)
282 INTEGER, INTENT(IN) :: i, j, n
283
284 cpassert(i <= j)
285 cpassert(i <= n)
286 cpassert(j <= n)
287
288 ij = (i - 1)*n - ((i - 1)*(i - 2))/2 + (j - i + 1)
289
290 cpassert(ij <= (n*(n + 1))/2 .AND. 0 <= ij)
291
292 END FUNCTION csr_idx_to_combined
293
294! **************************************************************************************************
295!> \brief extracts indices i and j from combined index ij
296!> \param ij The combined index
297!> \param n Dimension in i or j direction
298!> \param i Resulting i index
299!> \param j Resulting j index
300!> \par History
301!> 04.2016 created [JGH]
302! **************************************************************************************************
303 SUBROUTINE csr_idx_from_combined(ij, n, i, j)
304 INTEGER, INTENT(IN) :: ij, n
305 INTEGER, INTENT(OUT) :: i, j
306
307 INTEGER :: m, m0
308
309 m = max(ij/n, 1)
310 DO i = m, n
311 m0 = (i - 1)*n - ((i - 1)*(i - 2))/2
312 j = ij - m0 + i - 1
313 IF (j <= n) EXIT
314 END DO
315
316 cpassert(i > 0 .AND. i <= n)
317 cpassert(j > 0 .AND. j <= n)
318 cpassert(i <= j)
319
320 END SUBROUTINE csr_idx_from_combined
321
322! **************************************************************************************************
323!> \brief Calls the provided function for each element in the ERI
324!> \param this object reference
325!> \param nspin The spin number
326!> \param active_orbitals the active orbital indices
327!> \param fobj The function object from which to call `func(i, j, k, l, val)`
328!> \param spin1 the first spin value
329!> \param spin2 the second spin value
330!> \par History
331!> 04.2016 created [JHU]
332!> 06.2016 factored out from qs_a_s_methods:fcidump [TMU]
333!> \note Calls MPI, must be executed on all ranks.
334! **************************************************************************************************
335 SUBROUTINE eri_type_eri_foreach(this, nspin, active_orbitals, fobj, spin1, spin2)
336 CLASS(eri_type), INTENT(in) :: this
337 CLASS(eri_type_eri_element_func) :: fobj
338 INTEGER, DIMENSION(:, :), INTENT(IN) :: active_orbitals
339 INTEGER, OPTIONAL :: spin1, spin2
340
341 CHARACTER(LEN=*), PARAMETER :: routinen = "eri_type_eri_foreach"
342
343 INTEGER :: i1, i12, i12l, i2, i3, i34, i34l, i4, m1, m2, m3, m4, &
344 irptr, nspin, nindex, nmo, proc, nonzero_elements_local, handle, dummy_int(1)
345 INTEGER, ALLOCATABLE, DIMENSION(:) :: colind, offsets, nonzero_elements_global
346 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: erival
347 REAL(kind=dp) :: erint, dummy_real(1)
348 TYPE(mp_comm_type) :: mp_group
349
350 CALL timeset(routinen, handle)
351
352 IF (.NOT. PRESENT(spin1)) THEN
353 spin1 = nspin
354 END IF
355 IF (.NOT. PRESENT(spin2)) THEN
356 spin2 = nspin
357 END IF
358
359 dummy_int = 0
360 dummy_real = 0.0_dp
361
362 associate(eri => this%eri(nspin)%csr_mat, norb => this%norb)
363 nindex = (norb*(norb + 1))/2
364 CALL mp_group%set_handle(eri%mp_group%get_handle())
365 nmo = SIZE(active_orbitals, 1)
366 ! Irrelevant in case of half-transformed integrals
367 ALLOCATE (erival(nindex), colind(nindex))
368 ALLOCATE (offsets(0:mp_group%num_pe - 1), &
369 nonzero_elements_global(0:mp_group%num_pe - 1))
370
371 DO m1 = 1, nmo
372 i1 = active_orbitals(m1, spin1)
373 DO m2 = m1, nmo
374 i2 = active_orbitals(m2, spin1)
375 i12 = csr_idx_to_combined(i1, i2, norb)
376 i12l = (i12 - 1)/this%comm_exchange%num_pe + 1
377
378 ! In case of half-transformed integrals, every process might carry integrals of a row
379 ! The number of integrals varies between processes and rows (related to the randomized
380 ! distribution of matrix blocks)
381
382 ! 1) Collect the amount of local data from each process
383 nonzero_elements_local = 0
384 IF (mod(i12 - 1, this%comm_exchange%num_pe) == this%comm_exchange%mepos) THEN
385 nonzero_elements_local = eri%nzerow_local(i12l)
386 END IF
387 CALL mp_group%allgather(nonzero_elements_local, nonzero_elements_global)
388
389 ! 2) Prepare arrays for communication (calculate the offsets and the total number of elements)
390 offsets(0) = 0
391 DO proc = 1, mp_group%num_pe - 1
392 offsets(proc) = offsets(proc - 1) + nonzero_elements_global(proc - 1)
393 END DO
394 nindex = offsets(mp_group%num_pe - 1) + nonzero_elements_global(mp_group%num_pe - 1)
395 irptr = 1
396 IF (mod(i12 - 1, this%comm_exchange%num_pe) == this%comm_exchange%mepos) THEN
397 irptr = eri%rowptr_local(i12l)
398
399 ! Exchange actual data
400 CALL mp_group%allgatherv(eri%colind_local(irptr:irptr + nonzero_elements_local - 1), &
401 colind(1:nindex), nonzero_elements_global, offsets)
402 CALL mp_group%allgatherv(eri%nzval_local%r_dp(irptr:irptr + nonzero_elements_local - 1), &
403 erival(1:nindex), nonzero_elements_global, offsets)
404 ELSE
405 CALL mp_group%allgatherv(dummy_int(1:0), colind(1:nindex), nonzero_elements_global, offsets)
406 CALL mp_group%allgatherv(dummy_real(1:0), erival(1:nindex), nonzero_elements_global, offsets)
407 END IF
408
409 DO i34l = 1, nindex
410 i34 = colind(i34l)
411 erint = erival(i34l)
412 CALL csr_idx_from_combined(i34, norb, i3, i4)
413
414 DO m3 = 1, nmo
415 IF (active_orbitals(m3, spin2) == i3) THEN
416 EXIT
417 END IF
418 END DO
419
420 DO m4 = 1, nmo
421 IF (active_orbitals(m4, spin2) == i4) THEN
422 EXIT
423 END IF
424 END DO
425
426 ! terminate the loop prematurely if the function returns false
427 IF (.NOT. fobj%func(m1, m2, m3, m4, erint)) RETURN
428 END DO
429
430 END DO
431 END DO
432 END associate
433
434 CALL timestop(handle)
435 END SUBROUTINE eri_type_eri_foreach
436
437END MODULE qs_active_space_types
The function signature to be implemented by a child of eri_type_eri_element_func
DBCSR operations in CP2K.
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
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
integer, parameter, public default_path_length
Definition kinds.F:58
Interface to the message passing library MPI.
type(mp_comm_type), parameter, public mp_comm_null
subroutine, public mp_para_env_release(para_env)
releases the para object (to be called when you don't want anymore the shared copy of this object)
The module to read/write QCSchema HDF5 files for interfacing CP2K with other programs.
Definition qcschema.F:14
The types needed for the calculation of active space Hamiltonians.
subroutine, public release_active_space_type(active_space_env)
Releases all quantities in the active space environment.
subroutine, public csr_idx_from_combined(ij, n, i, j)
extracts indices i and j from combined index ij
integer function, public csr_idx_to_combined(i, j, n)
calculates combined index (ij)
subroutine, public create_active_space_type(active_space_env)
Creates an active space environment type, nullifying all quantities.
module that contains the definitions of the scf types
subroutine, public mixing_storage_release(mixing_store)
releases a mixing_storage
integer, parameter, public direct_mixing_nr
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public deallocate_mo_set(mo_set)
Deallocate a wavefunction data structure.
represent a full matrix
stores all the informations relevant to an mpi environment
Abstract function object for the eri_type_eri_foreach method.