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 New version of the module for the localization of the molecular orbitals
10 : !> This should be able to use different definition of the spread functional
11 : !> It should also calculate the integrals analytically so that it can be
12 : !> used irrespective of the pw_env and the collocation of wfn on the grids
13 : !> It should also work with a selected set of states, instead than all of them,
14 : !> in this case one should check that the selected states have the same occupation number
15 : !> The spread functional can be only estimated, or also optimized by minimization
16 : !> and in principle also maximization should be available.
17 : !> This operations can be required irrespective of the printing requirements
18 : !> It would be highly desirable to do all this along a MD run every N steps,
19 : !> and have a trajectory of the centeroids of the localized wfn
20 : !> In addition these functions can be used for properties calculations
21 : !> like NMR and XAS. Therefore it is necessary that the rotated wfn are then copied
22 : !> in the mos fm matrix to be available for next use.
23 : !> \author MI (05-2005)
24 : ! **************************************************************************************************
25 : MODULE qs_loc_types
26 :
27 : USE cell_types, ONLY: cell_release,&
28 : cell_retain,&
29 : cell_type
30 : USE cp_array_utils, ONLY: cp_2d_r_p_type
31 : USE cp_dbcsr_api, ONLY: dbcsr_deallocate_matrix,&
32 : dbcsr_p_type
33 : USE cp_fm_types, ONLY: cp_fm_release,&
34 : cp_fm_type
35 : USE distribution_1d_types, ONLY: distribution_1d_release,&
36 : distribution_1d_retain,&
37 : distribution_1d_type
38 : USE kinds, ONLY: default_string_length,&
39 : dp
40 : USE message_passing, ONLY: mp_para_env_release,&
41 : mp_para_env_type
42 : USE particle_types, ONLY: particle_type
43 : #include "./base/base_uses.f90"
44 :
45 : IMPLICIT NONE
46 :
47 : PRIVATE
48 :
49 : ! *** Global parameters ***
50 :
51 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_loc_types'
52 :
53 : ! **************************************************************************************************
54 : !> \brief contains all the info needed by quickstep to calculate
55 : !> the spread of a selected set of orbitals and if required
56 : !> to minimize or maximize the spread by rotation of the orbitals
57 : !> \param para_env info for the distribution of the calculations
58 : !> \param mo_coeff full matrix containing only the selected subset of real orbitals
59 : !> \param complex_mo_coeff full matrix containing only the selected subset of complex orbitals
60 : !> \param local_molecules molecules distributed
61 : !> \param cell box that contains the system
62 : !> \param localized_wfn_control variables and parameter that define the spread
63 : !> functional and the optimization algorithm
64 : !> \param particle_set position, type, ao_indexes etc for each atom
65 : !> \param op_sm_set set of sparse matrices used to define the spread operator
66 : !> when the functional is defined by the use operator acting on the
67 : !> basis functions, e.g. the Berry phase definition
68 : !> The matrix element of the type <a|O|b> are computed in initialization
69 : !> of qs_loc_env
70 : !> \param op_fm_set set of full matrices used to define the spread operator
71 : !> when the functional has to be defined directly using the products of MOS
72 : !> as in the case of the Pipek-Mezek definition.
73 : !> \param weights for a spread defined as extension of the orbitral in the box, these
74 : !> factors renormalize with respect to the box size
75 : !> \note
76 : !> this type should replace the previous set up for the localization of the wfn
77 : !> \par History
78 : !> 04-05 created
79 : !> \author MI
80 : ! **************************************************************************************************
81 : TYPE qs_loc_env_type
82 : LOGICAL :: do_localize = .FALSE., first_time = .FALSE.
83 : LOGICAL :: molecular_states = .FALSE.
84 : LOGICAL :: wannier_states = .FALSE.
85 : CHARACTER(LEN=default_string_length) :: tag_mo = ""
86 : TYPE(mp_para_env_type), POINTER :: para_env => NULL()
87 : TYPE(cp_fm_type), DIMENSION(:), &
88 : POINTER :: moloc_coeff => NULL()
89 : TYPE(cp_fm_type), DIMENSION(:, :), &
90 : POINTER :: op_fm_set => NULL()
91 : TYPE(distribution_1d_type), POINTER :: local_molecules => NULL()
92 : TYPE(cell_type), POINTER :: cell => NULL()
93 : TYPE(localized_wfn_control_type), &
94 : POINTER :: localized_wfn_control => NULL()
95 : TYPE(particle_type), DIMENSION(:), &
96 : POINTER :: particle_set => NULL()
97 : TYPE(dbcsr_p_type), DIMENSION(:, :), &
98 : POINTER :: op_sm_set => NULL()
99 : REAL(KIND=dp) :: start_time = -1.0_dp, target_time = -1.0_dp
100 : REAL(KIND=dp) :: weights(6) = -1.0_dp
101 : INTEGER :: dim_op = -1
102 : END TYPE qs_loc_env_type
103 :
104 : ! **************************************************************************************************
105 : !> \brief A type that holds controlling information for the
106 : !> calculation of the spread of wfn and the optimization of
107 : !> the spread functional
108 : !> \param ref_count ...
109 : !> \param localization_method which algorithm is used for the optimization
110 : !> \param operator_type how the spread is defined
111 : !> \param nloc_states number of states on which the spread is computed
112 : !> \param set_of_states how to choose the states
113 : !> \param lu_bound_states lower and upper bounds of the set of states
114 : !> print_cubes:
115 : !> print_centers:
116 : !> print_spreads:
117 : !> \param loc_states list of states on which the spread is computed
118 : !> \param centers_set arrais containing centers and spreads of the selected wfn
119 : !> \param centers_file_name output file names
120 : !> \param spreads_file_name output file names
121 : ! **************************************************************************************************
122 : TYPE localized_wfn_control_type
123 : INTEGER :: ref_count = -1
124 : INTEGER :: min_or_max = -1
125 : INTEGER :: localization_method = -1
126 : INTEGER :: operator_type = -1
127 : INTEGER, DIMENSION(2) :: nloc_states = -1, nguess = -1
128 : INTEGER :: set_of_states = -1
129 : INTEGER, DIMENSION(2, 2) :: lu_bound_states = -1
130 : INTEGER :: max_iter = -1
131 : INTEGER :: out_each = -1
132 : INTEGER :: nextra = -1
133 : INTEGER :: coeff_po_guess = -1, coeff_po_guess_mo_space = -1
134 : REAL(KIND=dp) :: eps_localization = -1.0_dp
135 : REAL(KIND=dp) :: max_crazy_angle = -1.0_dp
136 : REAL(KIND=dp) :: crazy_scale = -1.0_dp
137 : REAL(KIND=dp) :: eps_occ = -1.0_dp
138 : REAL(KIND=dp), DIMENSION(2) :: lu_ene_bound = -1.0_dp
139 : LOGICAL :: crazy_use_diag = .FALSE.
140 : LOGICAL :: print_cubes = .FALSE., jacobi_fallback = .FALSE., jacobi_refinement = .FALSE.
141 : LOGICAL :: print_centers = .FALSE.
142 : LOGICAL :: print_spreads = .FALSE.
143 : LOGICAL :: do_homo = .FALSE.
144 : LOGICAL :: do_mixed = .FALSE., do_cg_po = .FALSE.
145 : LOGICAL :: loc_restart = .FALSE.
146 : LOGICAL :: use_history = .FALSE.
147 : INTEGER, POINTER, DIMENSION(:, :) :: loc_states => NULL()
148 : TYPE(cp_2d_r_p_type), DIMENSION(2) :: centers_set = cp_2d_r_p_type()
149 : END TYPE localized_wfn_control_type
150 :
151 : ! *** Public ***
152 : PUBLIC :: qs_loc_env_create, qs_loc_env_release, &
153 : get_qs_loc_env, set_qs_loc_env, &
154 : localized_wfn_control_create, localized_wfn_control_release, &
155 : loc_coeff_index
156 : PUBLIC :: qs_loc_env_type, localized_wfn_control_type
157 :
158 : CONTAINS
159 :
160 : ! **************************************************************************************************
161 : !> \brief ...
162 : !> \param qs_loc_env ...
163 : !> \par History
164 : !> 04-05 created
165 : !> \author MI
166 : ! **************************************************************************************************
167 3780 : SUBROUTINE qs_loc_env_create(qs_loc_env)
168 :
169 : TYPE(qs_loc_env_type), INTENT(OUT) :: qs_loc_env
170 :
171 540 : qs_loc_env%tag_mo = ""
172 : NULLIFY (qs_loc_env%para_env)
173 : NULLIFY (qs_loc_env%cell)
174 : NULLIFY (qs_loc_env%op_sm_set)
175 : NULLIFY (qs_loc_env%op_fm_set)
176 : NULLIFY (qs_loc_env%local_molecules)
177 : NULLIFY (qs_loc_env%moloc_coeff)
178 : NULLIFY (qs_loc_env%particle_set)
179 : NULLIFY (qs_loc_env%localized_wfn_control)
180 3780 : qs_loc_env%weights = 0.0_dp
181 :
182 540 : END SUBROUTINE qs_loc_env_create
183 :
184 : !****f* qs_loc_types/qs_loc_env_release [1.0] *
185 :
186 : ! **************************************************************************************************
187 : !> \brief ...
188 : !> \param qs_loc_env ...
189 : !> \par History
190 : !> 04-05 created
191 : !> \author MI
192 : ! **************************************************************************************************
193 540 : SUBROUTINE qs_loc_env_release(qs_loc_env)
194 :
195 : TYPE(qs_loc_env_type), INTENT(INOUT) :: qs_loc_env
196 :
197 : INTEGER :: i, ii, j
198 :
199 540 : IF (ASSOCIATED(qs_loc_env%cell)) CALL cell_release(qs_loc_env%cell)
200 540 : IF (ASSOCIATED(qs_loc_env%local_molecules)) THEN
201 492 : CALL distribution_1d_release(qs_loc_env%local_molecules)
202 : END IF
203 540 : IF (ASSOCIATED(qs_loc_env%localized_wfn_control)) THEN
204 540 : CALL localized_wfn_control_release(qs_loc_env%localized_wfn_control)
205 : END IF
206 540 : IF (ASSOCIATED(qs_loc_env%para_env)) CALL mp_para_env_release(qs_loc_env%para_env)
207 540 : IF (ASSOCIATED(qs_loc_env%particle_set)) NULLIFY (qs_loc_env%particle_set)
208 :
209 540 : IF (ASSOCIATED(qs_loc_env%moloc_coeff)) THEN
210 1180 : DO i = 1, SIZE(qs_loc_env%moloc_coeff, 1)
211 688 : ii = LBOUND(qs_loc_env%moloc_coeff, 1) + i - 1
212 1180 : CALL cp_fm_release(qs_loc_env%moloc_coeff(ii))
213 : END DO
214 492 : DEALLOCATE (qs_loc_env%moloc_coeff)
215 : END IF
216 :
217 540 : CALL cp_fm_release(qs_loc_env%op_fm_set)
218 :
219 540 : IF (ASSOCIATED(qs_loc_env%op_sm_set)) THEN
220 1980 : DO i = 1, SIZE(qs_loc_env%op_sm_set, 2)
221 4968 : DO j = 1, SIZE(qs_loc_env%op_sm_set, 1)
222 4482 : CALL dbcsr_deallocate_matrix(qs_loc_env%op_sm_set(j, i)%matrix)
223 : END DO
224 : END DO
225 486 : DEALLOCATE (qs_loc_env%op_sm_set)
226 : END IF
227 :
228 540 : END SUBROUTINE qs_loc_env_release
229 :
230 : ! **************************************************************************************************
231 : !> \brief create the localized_wfn_control_type
232 : !> \param localized_wfn_control ...
233 : !> \par History
234 : !> 04.2005 created [MI]
235 : ! **************************************************************************************************
236 540 : SUBROUTINE localized_wfn_control_create(localized_wfn_control)
237 : TYPE(localized_wfn_control_type), POINTER :: localized_wfn_control
238 :
239 540 : CPASSERT(.NOT. ASSOCIATED(localized_wfn_control))
240 10800 : ALLOCATE (localized_wfn_control)
241 :
242 540 : localized_wfn_control%ref_count = 1
243 1620 : localized_wfn_control%nloc_states = 0
244 540 : localized_wfn_control%nextra = 0
245 1620 : localized_wfn_control%nguess = 0
246 3780 : localized_wfn_control%lu_bound_states = 0
247 1620 : localized_wfn_control%lu_ene_bound = 0.0_dp
248 540 : localized_wfn_control%print_cubes = .FALSE.
249 540 : localized_wfn_control%print_centers = .FALSE.
250 540 : localized_wfn_control%print_spreads = .FALSE.
251 540 : localized_wfn_control%do_homo = .TRUE.
252 540 : localized_wfn_control%use_history = .FALSE.
253 540 : NULLIFY (localized_wfn_control%loc_states)
254 540 : NULLIFY (localized_wfn_control%centers_set(1)%array)
255 540 : NULLIFY (localized_wfn_control%centers_set(2)%array)
256 540 : END SUBROUTINE localized_wfn_control_create
257 :
258 : ! **************************************************************************************************
259 : !> \brief release the localized_wfn_control_type
260 : !> \param localized_wfn_control ...
261 : !> \par History
262 : !> 04.2005 created [MI]
263 : ! **************************************************************************************************
264 1912 : SUBROUTINE localized_wfn_control_release(localized_wfn_control)
265 :
266 : TYPE(localized_wfn_control_type), POINTER :: localized_wfn_control
267 :
268 1912 : IF (ASSOCIATED(localized_wfn_control)) THEN
269 1472 : CPASSERT(localized_wfn_control%ref_count > 0)
270 1472 : localized_wfn_control%ref_count = localized_wfn_control%ref_count - 1
271 1472 : IF (localized_wfn_control%ref_count == 0) THEN
272 540 : IF (ASSOCIATED(localized_wfn_control%loc_states)) THEN
273 458 : DEALLOCATE (localized_wfn_control%loc_states)
274 : END IF
275 540 : IF (ASSOCIATED(localized_wfn_control%centers_set(1)%array)) THEN
276 492 : DEALLOCATE (localized_wfn_control%centers_set(1)%array)
277 : END IF
278 540 : IF (ASSOCIATED(localized_wfn_control%centers_set(2)%array)) THEN
279 186 : DEALLOCATE (localized_wfn_control%centers_set(2)%array)
280 : END IF
281 540 : localized_wfn_control%ref_count = 0
282 540 : DEALLOCATE (localized_wfn_control)
283 : END IF
284 : END IF
285 1912 : END SUBROUTINE localized_wfn_control_release
286 :
287 : ! **************************************************************************************************
288 : !> \brief retain the localized_wfn_control_type
289 : !> \param localized_wfn_control ...
290 : !> \par History
291 : !> 04.2005 created [MI]
292 : ! **************************************************************************************************
293 932 : SUBROUTINE localized_wfn_control_retain(localized_wfn_control)
294 : TYPE(localized_wfn_control_type), POINTER :: localized_wfn_control
295 :
296 932 : CPASSERT(ASSOCIATED(localized_wfn_control))
297 :
298 932 : localized_wfn_control%ref_count = localized_wfn_control%ref_count + 1
299 932 : END SUBROUTINE localized_wfn_control_retain
300 :
301 : ! **************************************************************************************************
302 : !> \brief ...
303 : !> \param qs_loc_env ...
304 : !> \param cell ...
305 : !> \param local_molecules ...
306 : !> \param localized_wfn_control ...
307 : !> \param moloc_coeff ...
308 : !> \param op_sm_set ...
309 : !> \param op_fm_set ...
310 : !> \param para_env ...
311 : !> \param particle_set ...
312 : !> \param weights ...
313 : !> \param dim_op ...
314 : !> \par History
315 : !> 04-05 created
316 : !> \author MI
317 : ! **************************************************************************************************
318 2224 : SUBROUTINE get_qs_loc_env(qs_loc_env, cell, local_molecules, localized_wfn_control, &
319 : moloc_coeff, op_sm_set, op_fm_set, para_env, &
320 : particle_set, weights, dim_op)
321 :
322 : TYPE(qs_loc_env_type), INTENT(IN) :: qs_loc_env
323 : TYPE(cell_type), OPTIONAL, POINTER :: cell
324 : TYPE(distribution_1d_type), OPTIONAL, POINTER :: local_molecules
325 : TYPE(localized_wfn_control_type), OPTIONAL, &
326 : POINTER :: localized_wfn_control
327 : TYPE(cp_fm_type), DIMENSION(:), OPTIONAL, POINTER :: moloc_coeff
328 : TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
329 : POINTER :: op_sm_set
330 : TYPE(cp_fm_type), DIMENSION(:, :), OPTIONAL, &
331 : POINTER :: op_fm_set
332 : TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env
333 : TYPE(particle_type), DIMENSION(:), OPTIONAL, &
334 : POINTER :: particle_set
335 : REAL(dp), DIMENSION(6), OPTIONAL :: weights
336 : INTEGER, OPTIONAL :: dim_op
337 :
338 2224 : IF (PRESENT(cell)) cell => qs_loc_env%cell
339 2224 : IF (PRESENT(moloc_coeff)) moloc_coeff => qs_loc_env%moloc_coeff
340 2224 : IF (PRESENT(local_molecules)) local_molecules => qs_loc_env%local_molecules
341 2224 : IF (PRESENT(localized_wfn_control)) THEN
342 1638 : localized_wfn_control => qs_loc_env%localized_wfn_control
343 : END IF
344 2224 : IF (PRESENT(op_sm_set)) op_sm_set => qs_loc_env%op_sm_set
345 2224 : IF (PRESENT(op_fm_set)) op_fm_set => qs_loc_env%op_fm_set
346 2224 : IF (PRESENT(para_env)) para_env => qs_loc_env%para_env
347 2224 : IF (PRESENT(particle_set)) particle_set => qs_loc_env%particle_set
348 6578 : IF (PRESENT(weights)) weights(1:6) = qs_loc_env%weights(1:6)
349 2224 : IF (PRESENT(dim_op)) dim_op = qs_loc_env%dim_op
350 :
351 2224 : END SUBROUTINE get_qs_loc_env
352 :
353 : ! **************************************************************************************************
354 : !> \brief ...
355 : !> \param qs_loc_env ...
356 : !> \param cell ...
357 : !> \param local_molecules ...
358 : !> \param localized_wfn_control ...
359 : !> \param moloc_coeff ...
360 : !> \param op_sm_set ...
361 : !> \param op_fm_set ...
362 : !> \param para_env ...
363 : !> \param particle_set ...
364 : !> \param weights ...
365 : !> \param dim_op ...
366 : !> \par History
367 : !> 04-05 created
368 : !> \author MI
369 : ! **************************************************************************************************
370 1424 : SUBROUTINE set_qs_loc_env(qs_loc_env, cell, local_molecules, localized_wfn_control, &
371 : moloc_coeff, op_sm_set, op_fm_set, para_env, &
372 : particle_set, weights, dim_op)
373 :
374 : TYPE(qs_loc_env_type), INTENT(INOUT) :: qs_loc_env
375 : TYPE(cell_type), OPTIONAL, POINTER :: cell
376 : TYPE(distribution_1d_type), OPTIONAL, POINTER :: local_molecules
377 : TYPE(localized_wfn_control_type), OPTIONAL, &
378 : POINTER :: localized_wfn_control
379 : TYPE(cp_fm_type), DIMENSION(:), OPTIONAL, POINTER :: moloc_coeff
380 : TYPE(dbcsr_p_type), DIMENSION(:, :), OPTIONAL, &
381 : POINTER :: op_sm_set
382 : TYPE(cp_fm_type), DIMENSION(:, :), OPTIONAL, &
383 : POINTER :: op_fm_set
384 : TYPE(mp_para_env_type), OPTIONAL, POINTER :: para_env
385 : TYPE(particle_type), DIMENSION(:), OPTIONAL, &
386 : POINTER :: particle_set
387 : REAL(dp), DIMENSION(6), OPTIONAL :: weights
388 : INTEGER, OPTIONAL :: dim_op
389 :
390 1424 : IF (PRESENT(cell)) THEN
391 492 : CALL cell_retain(cell)
392 492 : CALL cell_release(qs_loc_env%cell)
393 492 : qs_loc_env%cell => cell
394 : END IF
395 :
396 1424 : IF (PRESENT(local_molecules)) THEN
397 492 : CALL distribution_1d_retain(local_molecules)
398 492 : IF (ASSOCIATED(qs_loc_env%local_molecules)) THEN
399 0 : CALL distribution_1d_release(qs_loc_env%local_molecules)
400 : END IF
401 492 : qs_loc_env%local_molecules => local_molecules
402 : END IF
403 :
404 1424 : IF (PRESENT(localized_wfn_control)) THEN
405 932 : CALL localized_wfn_control_retain(localized_wfn_control)
406 932 : CALL localized_wfn_control_release(qs_loc_env%localized_wfn_control)
407 932 : qs_loc_env%localized_wfn_control => localized_wfn_control
408 : END IF
409 1424 : IF (PRESENT(para_env)) THEN
410 492 : CALL para_env%retain()
411 492 : CALL mp_para_env_release(qs_loc_env%para_env)
412 492 : qs_loc_env%para_env => para_env
413 : END IF
414 1424 : IF (PRESENT(particle_set)) qs_loc_env%particle_set => particle_set
415 1424 : IF (PRESENT(moloc_coeff)) THEN
416 492 : CALL cp_fm_release(qs_loc_env%moloc_coeff)
417 492 : qs_loc_env%moloc_coeff => moloc_coeff
418 : END IF
419 1424 : IF (PRESENT(op_sm_set)) THEN
420 0 : qs_loc_env%op_sm_set => op_sm_set
421 : END IF
422 1424 : IF (PRESENT(op_fm_set)) THEN
423 0 : qs_loc_env%op_fm_set => op_fm_set
424 : END IF
425 1424 : IF (PRESENT(weights)) THEN
426 0 : qs_loc_env%weights = weights
427 : END IF
428 1424 : IF (PRESENT(dim_op)) THEN
429 492 : qs_loc_env%dim_op = dim_op
430 : END IF
431 :
432 1424 : END SUBROUTINE set_qs_loc_env
433 :
434 : ! **************************************************************************************************
435 : !> \brief Index of the (real) MO coefficients of a spin in an array of MO coefficients.
436 : !> In RTP the array holds the real and imaginary parts of each spin,
437 : !> interleaved as (re_1, im_1, re_2, im_2), otherwise one matrix per spin.
438 : !> \param ncoeff number of matrices in the array
439 : !> \param nspins number of spins
440 : !> \param ispin spin
441 : !> \return index of the real part of spin ispin
442 : ! **************************************************************************************************
443 844 : PURE FUNCTION loc_coeff_index(ncoeff, nspins, ispin) RESULT(icoeff)
444 : INTEGER, INTENT(IN) :: ncoeff, nspins, ispin
445 : INTEGER :: icoeff
446 :
447 844 : IF (ncoeff == 2*nspins) THEN
448 224 : icoeff = 2*ispin - 1
449 : ELSE
450 620 : icoeff = ispin
451 : END IF
452 :
453 844 : END FUNCTION loc_coeff_index
454 :
455 0 : END MODULE qs_loc_types
456 :
|