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 Functions to print the KS and S matrix in the CSR format to file
10 : !> \par History
11 : !> Started as a copy from the relevant part of qs_scf_post_gpw
12 : !> \author Fabian Ducry (05.2020)
13 : ! **************************************************************************************************
14 : MODULE qs_scf_csr_write
15 : USE cp_dbcsr_api, ONLY: &
16 : dbcsr_copy, dbcsr_create, dbcsr_desymmetrize, dbcsr_finalize, dbcsr_get_block_p, &
17 : dbcsr_has_symmetry, dbcsr_p_type, dbcsr_put_block, dbcsr_release, dbcsr_set, dbcsr_type, &
18 : dbcsr_type_antisymmetric, dbcsr_type_no_symmetry, dbcsr_type_symmetric
19 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
20 : USE cp_dbcsr_csr, ONLY: dbcsr_convert_dbcsr_to_csr,&
21 : dbcsr_csr_create_and_convert_complex,&
22 : dbcsr_csr_create_from_dbcsr,&
23 : dbcsr_csr_dbcsr_blkrow_dist,&
24 : dbcsr_csr_destroy,&
25 : dbcsr_csr_type,&
26 : dbcsr_csr_write
27 : USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set,&
28 : dbcsr_deallocate_matrix_set
29 : USE cp_log_handling, ONLY: cp_get_default_logger,&
30 : cp_logger_get_default_io_unit,&
31 : cp_logger_type
32 : USE cp_output_handling, ONLY: cp_p_file,&
33 : cp_print_key_finished_output,&
34 : cp_print_key_should_output,&
35 : cp_print_key_unit_nr
36 : USE input_section_types, ONLY: section_vals_get_subs_vals,&
37 : section_vals_type,&
38 : section_vals_val_get
39 : USE kinds, ONLY: default_path_length,&
40 : dp
41 : USE kpoint_methods, ONLY: rskp_transform
42 : USE kpoint_types, ONLY: get_kpoint_info,&
43 : kpoint_type
44 : USE qs_environment_types, ONLY: get_qs_env,&
45 : qs_environment_type
46 : USE qs_neighbor_list_types, ONLY: get_iterator_info,&
47 : get_neighbor_list_set_p,&
48 : neighbor_list_iterate,&
49 : neighbor_list_iterator_create,&
50 : neighbor_list_iterator_p_type,&
51 : neighbor_list_iterator_release,&
52 : neighbor_list_set_p_type
53 : USE qs_rho_types, ONLY: qs_rho_get,&
54 : qs_rho_type
55 : #include "./base/base_uses.f90"
56 :
57 : IMPLICIT NONE
58 : PRIVATE
59 :
60 : ! Global parameters
61 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_scf_csr_write'
62 : PUBLIC :: write_ks_matrix_csr, &
63 : write_s_matrix_csr, &
64 : write_p_matrix_csr, &
65 : write_hcore_matrix_csr
66 :
67 : ! **************************************************************************************************
68 :
69 : CONTAINS
70 :
71 : !**************************************************************************************************
72 : !> \brief writing the KS matrix in csr format into a file
73 : !> \param qs_env qs environment
74 : !> \param input the input
75 : !> \par History
76 : !> Moved to module qs_scf_csr_write (05.2020)
77 : !> \author Mohammad Hossein Bani-Hashemian
78 : ! **************************************************************************************************
79 25564 : SUBROUTINE write_ks_matrix_csr(qs_env, input)
80 : TYPE(qs_environment_type), POINTER :: qs_env
81 : TYPE(section_vals_type), POINTER :: input
82 :
83 : CHARACTER(len=*), PARAMETER :: routineN = 'write_ks_matrix_csr'
84 :
85 : INTEGER :: handle, output_unit
86 : LOGICAL :: do_kpoints, do_ks_csr_write, real_space
87 : TYPE(cp_logger_type), POINTER :: logger
88 25564 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks
89 : TYPE(kpoint_type), POINTER :: kpoints
90 : TYPE(section_vals_type), POINTER :: dft_section
91 :
92 25564 : CALL timeset(routineN, handle)
93 :
94 : NULLIFY (dft_section)
95 :
96 25564 : logger => cp_get_default_logger()
97 25564 : output_unit = cp_logger_get_default_io_unit(logger)
98 :
99 25564 : dft_section => section_vals_get_subs_vals(input, "DFT")
100 : do_ks_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
101 25564 : "PRINT%KS_CSR_WRITE"), cp_p_file)
102 :
103 25564 : IF (do_ks_csr_write) THEN
104 56 : CALL get_qs_env(qs_env=qs_env, kpoints=kpoints, matrix_ks_kp=matrix_ks, do_kpoints=do_kpoints)
105 : CALL section_vals_val_get(dft_section, "PRINT%KS_CSR_WRITE%REAL_SPACE", &
106 56 : l_val=real_space)
107 :
108 56 : IF (do_kpoints .AND. .NOT. real_space) THEN
109 : CALL write_matrix_kp_csr(mat=matrix_ks, dft_section=dft_section, &
110 10 : kpoints=kpoints, prefix="KS")
111 : ELSE
112 : CALL write_matrix_csr(dft_section, mat=matrix_ks, kpoints=kpoints, do_kpoints=do_kpoints, &
113 46 : prefix="KS")
114 : END IF
115 : END IF
116 :
117 25564 : CALL timestop(handle)
118 :
119 25564 : END SUBROUTINE write_ks_matrix_csr
120 :
121 : !**************************************************************************************************
122 : !> \brief writing the overlap matrix in csr format into a file
123 : !> \param qs_env qs environment
124 : !> \param input the input
125 : !> \par History
126 : !> Moved to module qs_scf_csr_write
127 : !> \author Mohammad Hossein Bani-Hashemian
128 : ! **************************************************************************************************
129 25564 : SUBROUTINE write_s_matrix_csr(qs_env, input)
130 : TYPE(qs_environment_type), POINTER :: qs_env
131 : TYPE(section_vals_type), POINTER :: input
132 :
133 : CHARACTER(len=*), PARAMETER :: routineN = 'write_s_matrix_csr'
134 :
135 : INTEGER :: handle, output_unit
136 : LOGICAL :: do_kpoints, do_s_csr_write, real_space
137 : TYPE(cp_logger_type), POINTER :: logger
138 25564 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
139 : TYPE(kpoint_type), POINTER :: kpoints
140 : TYPE(section_vals_type), POINTER :: dft_section
141 :
142 25564 : CALL timeset(routineN, handle)
143 :
144 : NULLIFY (dft_section)
145 :
146 25564 : logger => cp_get_default_logger()
147 25564 : output_unit = cp_logger_get_default_io_unit(logger)
148 :
149 25564 : dft_section => section_vals_get_subs_vals(input, "DFT")
150 : do_s_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
151 25564 : "PRINT%S_CSR_WRITE"), cp_p_file)
152 :
153 25564 : IF (do_s_csr_write) THEN
154 54 : CALL get_qs_env(qs_env=qs_env, kpoints=kpoints, matrix_s_kp=matrix_s, do_kpoints=do_kpoints)
155 : CALL section_vals_val_get(dft_section, "PRINT%S_CSR_WRITE%REAL_SPACE", &
156 54 : l_val=real_space)
157 :
158 54 : IF (do_kpoints .AND. .NOT. real_space) THEN
159 : CALL write_matrix_kp_csr(mat=matrix_s, dft_section=dft_section, &
160 10 : kpoints=kpoints, prefix="S")
161 : ELSE
162 : CALL write_matrix_csr(dft_section, mat=matrix_s, kpoints=kpoints, do_kpoints=do_kpoints, &
163 44 : prefix="S")
164 : END IF
165 : END IF
166 :
167 25564 : CALL timestop(handle)
168 :
169 25564 : END SUBROUTINE write_s_matrix_csr
170 :
171 : !**************************************************************************************************
172 : !> \brief writing the density matrix in csr format into a file
173 : !> \param qs_env qs environment
174 : !> \param input the input
175 : !> \par History
176 : !> \author Mohammad Hossein Bani-Hashemian
177 : ! **************************************************************************************************
178 25564 : SUBROUTINE write_p_matrix_csr(qs_env, input)
179 : TYPE(qs_environment_type), POINTER :: qs_env
180 : TYPE(section_vals_type), POINTER :: input
181 :
182 : CHARACTER(len=*), PARAMETER :: routineN = 'write_p_matrix_csr'
183 :
184 : INTEGER :: handle, output_unit
185 : LOGICAL :: do_kpoints, do_p_csr_write, real_space
186 : TYPE(cp_logger_type), POINTER :: logger
187 25564 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_kp
188 : TYPE(kpoint_type), POINTER :: kpoints
189 : TYPE(qs_rho_type), POINTER :: rho
190 : TYPE(section_vals_type), POINTER :: dft_section
191 :
192 25564 : CALL timeset(routineN, handle)
193 :
194 : NULLIFY (dft_section)
195 :
196 25564 : logger => cp_get_default_logger()
197 25564 : output_unit = cp_logger_get_default_io_unit(logger)
198 :
199 25564 : dft_section => section_vals_get_subs_vals(input, "DFT")
200 : do_p_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
201 25564 : "PRINT%P_CSR_WRITE"), cp_p_file)
202 :
203 25564 : IF (do_p_csr_write) THEN
204 30 : CALL get_qs_env(qs_env=qs_env, kpoints=kpoints, rho=rho, do_kpoints=do_kpoints)
205 30 : CALL qs_rho_get(rho, rho_ao_kp=rho_ao_kp)
206 : CALL section_vals_val_get(dft_section, "PRINT%P_CSR_WRITE%REAL_SPACE", &
207 30 : l_val=real_space)
208 :
209 30 : IF (do_kpoints .AND. .NOT. real_space) THEN
210 : CALL write_matrix_kp_csr(mat=rho_ao_kp, dft_section=dft_section, &
211 0 : kpoints=kpoints, prefix="P")
212 : ELSE
213 : CALL write_matrix_csr(dft_section, mat=rho_ao_kp, kpoints=kpoints, do_kpoints=do_kpoints, &
214 30 : prefix="P")
215 : END IF
216 : END IF
217 :
218 25564 : CALL timestop(handle)
219 :
220 25564 : END SUBROUTINE write_p_matrix_csr
221 :
222 : !**************************************************************************************************
223 : !> \brief writing the core Hamiltonian matrix in csr format into a file
224 : !> \param qs_env qs environment
225 : !> \param input the input
226 : !> \par History
227 : !> \author Mohammad Hossein Bani-Hashemian
228 : ! **************************************************************************************************
229 25564 : SUBROUTINE write_hcore_matrix_csr(qs_env, input)
230 : TYPE(qs_environment_type), POINTER :: qs_env
231 : TYPE(section_vals_type), POINTER :: input
232 :
233 : CHARACTER(len=*), PARAMETER :: routineN = 'write_hcore_matrix_csr'
234 :
235 : INTEGER :: handle, output_unit
236 : LOGICAL :: do_h_csr_write, do_kpoints, real_space
237 : TYPE(cp_logger_type), POINTER :: logger
238 25564 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_h
239 : TYPE(kpoint_type), POINTER :: kpoints
240 : TYPE(section_vals_type), POINTER :: dft_section
241 :
242 25564 : CALL timeset(routineN, handle)
243 :
244 : NULLIFY (dft_section)
245 :
246 25564 : logger => cp_get_default_logger()
247 25564 : output_unit = cp_logger_get_default_io_unit(logger)
248 :
249 25564 : dft_section => section_vals_get_subs_vals(input, "DFT")
250 : do_h_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
251 25564 : "PRINT%HCORE_CSR_WRITE"), cp_p_file)
252 :
253 25564 : IF (do_h_csr_write) THEN
254 30 : CALL get_qs_env(qs_env=qs_env, kpoints=kpoints, matrix_h_kp=matrix_h, do_kpoints=do_kpoints)
255 : CALL section_vals_val_get(dft_section, "PRINT%HCORE_CSR_WRITE%REAL_SPACE", &
256 30 : l_val=real_space)
257 :
258 30 : IF (do_kpoints .AND. .NOT. real_space) THEN
259 : CALL write_matrix_kp_csr(mat=matrix_h, dft_section=dft_section, &
260 0 : kpoints=kpoints, prefix="HCORE")
261 : ELSE
262 : CALL write_matrix_csr(dft_section, mat=matrix_h, kpoints=kpoints, do_kpoints=do_kpoints, &
263 30 : prefix="HCORE")
264 : END IF
265 : END IF
266 :
267 25564 : CALL timestop(handle)
268 :
269 25564 : END SUBROUTINE write_hcore_matrix_csr
270 :
271 : ! **************************************************************************************************
272 : !> \brief helper function to print the real space representation of KS or S matrix to file
273 : !> \param dft_section the dft_section
274 : !> \param mat Hamiltonian or overlap matrix
275 : !> \param kpoints Kpoint environment
276 : !> \param prefix string to distinguish between KS and S matrix
277 : !> \param do_kpoints Whether it is a gamma-point or k-point calculation
278 : !> \par History
279 : !> Moved most of the code from write_ks_matrix_csr and write_s_matrix_csr
280 : !> Removed the code for printing k-point dependent matrices and added
281 : !> printing of the real space representation
282 : ! **************************************************************************************************
283 150 : SUBROUTINE write_matrix_csr(dft_section, mat, kpoints, prefix, do_kpoints)
284 : TYPE(section_vals_type), INTENT(IN), POINTER :: dft_section
285 : TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(IN), &
286 : POINTER :: mat
287 : TYPE(kpoint_type), INTENT(IN), POINTER :: kpoints
288 : CHARACTER(*), INTENT(in) :: prefix
289 : LOGICAL, INTENT(IN) :: do_kpoints
290 :
291 : CHARACTER(len=*), PARAMETER :: routineN = 'write_matrix_csr'
292 :
293 : CHARACTER(LEN=default_path_length) :: file_name, fileformat, subs_string
294 : INTEGER :: handle, ic, ispin, ncell, nspin, &
295 : output_unit, unit_nr
296 150 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: index_to_cell
297 150 : INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: cell_to_index
298 150 : INTEGER, DIMENSION(:, :), POINTER :: i2c
299 150 : INTEGER, DIMENSION(:, :, :), POINTER :: c2i
300 : LOGICAL :: bin, do_symmetric, uptr
301 : REAL(KIND=dp) :: thld
302 : TYPE(cp_logger_type), POINTER :: logger
303 : TYPE(dbcsr_csr_type) :: mat_csr
304 150 : TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: mat_nosym
305 : TYPE(dbcsr_type) :: matrix_nosym
306 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
307 150 : POINTER :: sab_nl
308 :
309 150 : CALL timeset(routineN, handle)
310 :
311 150 : logger => cp_get_default_logger()
312 150 : output_unit = cp_logger_get_default_io_unit(logger)
313 :
314 150 : subs_string = "PRINT%"//prefix//"_CSR_WRITE"
315 :
316 150 : CALL section_vals_val_get(dft_section, subs_string//"%THRESHOLD", r_val=thld)
317 150 : CALL section_vals_val_get(dft_section, subs_string//"%UPPER_TRIANGULAR", l_val=uptr)
318 150 : CALL section_vals_val_get(dft_section, subs_string//"%BINARY", l_val=bin)
319 :
320 150 : IF (bin) THEN
321 2 : fileformat = "UNFORMATTED"
322 : ELSE
323 148 : fileformat = "FORMATTED"
324 : END IF
325 :
326 150 : nspin = SIZE(mat, 1)
327 150 : ncell = SIZE(mat, 2)
328 :
329 150 : IF (do_kpoints) THEN
330 :
331 2 : i2c => kpoints%index_to_cell
332 2 : c2i => kpoints%cell_to_index
333 :
334 2 : NULLIFY (sab_nl)
335 2 : CALL get_kpoint_info(kpoints, sab_nl=sab_nl)
336 2 : CALL get_neighbor_list_set_p(neighbor_list_sets=sab_nl, symmetric=do_symmetric)
337 :
338 : ! desymmetrize the KS or S matrices if necessary
339 2 : IF (do_symmetric) THEN
340 2 : CALL desymmetrize_rs_matrix(mat, mat_nosym, cell_to_index, index_to_cell, kpoints)
341 2 : ncell = SIZE(index_to_cell, 2) ! update the number of cells
342 : ELSE
343 0 : ALLOCATE (cell_to_index(LBOUND(c2i, 1):UBOUND(c2i, 1), &
344 : LBOUND(c2i, 2):UBOUND(c2i, 2), &
345 0 : LBOUND(c2i, 3):UBOUND(c2i, 3)))
346 : cell_to_index(LBOUND(c2i, 1):UBOUND(c2i, 1), &
347 : LBOUND(c2i, 2):UBOUND(c2i, 2), &
348 0 : LBOUND(c2i, 3):UBOUND(c2i, 3)) = c2i
349 :
350 0 : ALLOCATE (index_to_cell(3, ncell))
351 0 : index_to_cell(1:3, 1:ncell) = i2c
352 :
353 0 : mat_nosym => mat
354 : END IF
355 :
356 : ! print the index to cell mapping to the output
357 2 : IF (output_unit > 0) THEN
358 1 : WRITE (output_unit, "(/,A15,T15,I4,A)") prefix//" CSR write| ", &
359 2 : ncell, " periodic images"
360 1 : WRITE (output_unit, "(T7,A,T17,A,T24,A,T31,A)") "Number", "X", "Y", "Z"
361 28 : DO ic = 1, ncell
362 28 : WRITE (output_unit, "(T8,I3,T15,I3,T22,I3,T29,I3)") ic, index_to_cell(:, ic)
363 : END DO
364 : END IF
365 : END IF
366 :
367 : ! write the csr file(s)
368 300 : DO ispin = 1, nspin
369 502 : DO ic = 1, ncell
370 202 : IF (do_kpoints) THEN
371 54 : CALL dbcsr_copy(matrix_nosym, mat_nosym(ispin, ic)%matrix)
372 54 : WRITE (file_name, '(2(A,I0))') prefix//"_SPIN_", ispin, "_R_", ic
373 : ELSE
374 148 : IF (dbcsr_has_symmetry(mat(ispin, ic)%matrix)) THEN
375 148 : CALL dbcsr_desymmetrize(mat(ispin, ic)%matrix, matrix_nosym)
376 : ELSE
377 0 : CALL dbcsr_copy(matrix_nosym, mat(ispin, ic)%matrix)
378 : END IF
379 148 : WRITE (file_name, '(A,I0)') prefix//"_SPIN_", ispin
380 : END IF
381 : ! Convert dbcsr to csr
382 : CALL dbcsr_csr_create_from_dbcsr(matrix_nosym, &
383 202 : mat_csr, dbcsr_csr_dbcsr_blkrow_dist)
384 202 : CALL dbcsr_convert_dbcsr_to_csr(matrix_nosym, mat_csr)
385 : ! Write to file
386 : unit_nr = cp_print_key_unit_nr(logger, dft_section, subs_string, &
387 : extension=".csr", middle_name=TRIM(file_name), &
388 202 : file_status="REPLACE", file_form=fileformat)
389 202 : CALL dbcsr_csr_write(mat_csr, unit_nr, upper_triangle=uptr, threshold=thld, binary=bin)
390 :
391 202 : CALL cp_print_key_finished_output(unit_nr, logger, dft_section, subs_string)
392 202 : CALL dbcsr_csr_destroy(mat_csr)
393 352 : CALL dbcsr_release(matrix_nosym)
394 : END DO
395 : END DO
396 :
397 : ! clean up
398 150 : IF (do_kpoints) THEN
399 2 : DEALLOCATE (cell_to_index, index_to_cell)
400 2 : IF (do_symmetric) THEN
401 4 : DO ispin = 1, nspin
402 58 : DO ic = 1, ncell
403 56 : CALL dbcsr_release(mat_nosym(ispin, ic)%matrix)
404 : END DO
405 : END DO
406 2 : CALL dbcsr_deallocate_matrix_set(mat_nosym)
407 : END IF
408 : END IF
409 150 : CALL timestop(handle)
410 :
411 300 : END SUBROUTINE write_matrix_csr
412 :
413 : ! **************************************************************************************************
414 : !> \brief helper function to print the k-dependent KS or S matrix to file
415 : !> \param mat Hamiltonian or overlap matrix for k-point calculations
416 : !> \param dft_section the dft_section
417 : !> \param kpoints Kpoint environment
418 : !> \param prefix string to distinguish between KS and S matrix
419 : !> \par History
420 : !> Moved the code from write_matrix_csr to write_matrix_kp_csr
421 : !> \author Fabian Ducry
422 : ! **************************************************************************************************
423 20 : SUBROUTINE write_matrix_kp_csr(mat, dft_section, kpoints, prefix)
424 : TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(IN), &
425 : POINTER :: mat
426 : TYPE(section_vals_type), INTENT(IN), POINTER :: dft_section
427 : TYPE(kpoint_type), INTENT(IN), POINTER :: kpoints
428 : CHARACTER(*), INTENT(in) :: prefix
429 :
430 : CHARACTER(len=*), PARAMETER :: routineN = 'write_matrix_kp_csr'
431 :
432 : CHARACTER(LEN=default_path_length) :: file_name, fileformat, subs_string
433 : INTEGER :: handle, igroup, ik, ikp, ispin, kpmax, &
434 : nkp_groups, output_unit, unit_nr
435 20 : INTEGER, DIMENSION(:, :), POINTER :: kp_dist
436 : LOGICAL :: bin, uptr, use_real_wfn
437 : REAL(KIND=dp) :: thld
438 20 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
439 : TYPE(cp_logger_type), POINTER :: logger
440 : TYPE(dbcsr_csr_type) :: mat_csr
441 : TYPE(dbcsr_type) :: matrix_nosym
442 : TYPE(dbcsr_type), POINTER :: imatrix, imatrix_nosym, rmatrix, &
443 : rmatrix_nosym
444 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
445 20 : POINTER :: sab_nl
446 :
447 20 : CALL timeset(routineN, handle)
448 :
449 20 : logger => cp_get_default_logger()
450 20 : output_unit = cp_logger_get_default_io_unit(logger)
451 :
452 20 : subs_string = "PRINT%"//prefix//"_CSR_WRITE"
453 :
454 20 : CALL section_vals_val_get(dft_section, subs_string//"%THRESHOLD", r_val=thld)
455 20 : CALL section_vals_val_get(dft_section, subs_string//"%UPPER_TRIANGULAR", l_val=uptr)
456 20 : CALL section_vals_val_get(dft_section, subs_string//"%BINARY", l_val=bin)
457 :
458 20 : IF (bin) THEN
459 20 : fileformat = "UNFORMATTED"
460 : ELSE
461 0 : fileformat = "FORMATTED"
462 : END IF
463 :
464 20 : NULLIFY (sab_nl)
465 :
466 : ! Calculate the Hamiltonian at the k-points
467 : CALL get_kpoint_info(kpoints, xkp=xkp, use_real_wfn=use_real_wfn, &
468 20 : nkp_groups=nkp_groups, kp_dist=kp_dist, sab_nl=sab_nl)
469 :
470 20 : ALLOCATE (rmatrix)
471 : CALL dbcsr_create(rmatrix, template=mat(1, 1)%matrix, &
472 20 : matrix_type=dbcsr_type_symmetric)
473 20 : CALL cp_dbcsr_alloc_block_from_nbl(rmatrix, sab_nl)
474 :
475 20 : IF (.NOT. use_real_wfn) THEN
476 : ! Allocate temporary variables
477 16 : ALLOCATE (rmatrix_nosym, imatrix, imatrix_nosym)
478 : CALL dbcsr_create(rmatrix_nosym, template=mat(1, 1)%matrix, &
479 16 : matrix_type=dbcsr_type_no_symmetry)
480 : CALL dbcsr_create(imatrix, template=mat(1, 1)%matrix, &
481 16 : matrix_type=dbcsr_type_antisymmetric)
482 : CALL dbcsr_create(imatrix_nosym, template=mat(1, 1)%matrix, &
483 16 : matrix_type=dbcsr_type_no_symmetry)
484 16 : CALL cp_dbcsr_alloc_block_from_nbl(rmatrix_nosym, sab_nl)
485 16 : CALL cp_dbcsr_alloc_block_from_nbl(imatrix, sab_nl)
486 16 : CALL cp_dbcsr_alloc_block_from_nbl(imatrix_nosym, sab_nl)
487 : END IF
488 :
489 56 : kpmax = MAXVAL(kp_dist(2, :) - kp_dist(1, :) + 1)
490 56 : DO ikp = 1, kpmax
491 92 : DO ispin = 1, SIZE(mat, 1)
492 140 : DO igroup = 1, nkp_groups
493 : ! number of current kpoint
494 68 : ik = kp_dist(1, igroup) + ikp - 1
495 68 : IF (ik > kp_dist(2, igroup)) CYCLE
496 68 : CALL dbcsr_set(rmatrix, 0.0_dp)
497 68 : IF (use_real_wfn) THEN
498 : ! FT of the matrix
499 : CALL rskp_transform(rmatrix=rmatrix, rsmat=mat, ispin=ispin, &
500 4 : xkp=xkp(1:3, ik), cell_to_index=kpoints%cell_to_index, sab_nl=sab_nl)
501 : ! Convert to desymmetrized csr matrix
502 4 : CALL dbcsr_desymmetrize(rmatrix, matrix_nosym)
503 4 : CALL dbcsr_csr_create_from_dbcsr(matrix_nosym, mat_csr, dbcsr_csr_dbcsr_blkrow_dist)
504 4 : CALL dbcsr_convert_dbcsr_to_csr(matrix_nosym, mat_csr)
505 4 : CALL dbcsr_release(matrix_nosym)
506 : ELSE
507 : ! FT of the matrix
508 64 : CALL dbcsr_set(imatrix, 0.0_dp)
509 : CALL rskp_transform(rmatrix=rmatrix, cmatrix=imatrix, rsmat=mat, ispin=ispin, &
510 64 : xkp=xkp(1:3, ik), cell_to_index=kpoints%cell_to_index, sab_nl=sab_nl)
511 :
512 : ! Desymmetrize and sum the real and imaginary part into
513 : ! cmatrix
514 64 : CALL dbcsr_desymmetrize(rmatrix, rmatrix_nosym)
515 64 : CALL dbcsr_desymmetrize(imatrix, imatrix_nosym)
516 : ! Convert to csr
517 : CALL dbcsr_csr_create_and_convert_complex(rmatrix=rmatrix_nosym, &
518 : imatrix=imatrix_nosym, &
519 : csr_mat=mat_csr, &
520 64 : dist_format=dbcsr_csr_dbcsr_blkrow_dist)
521 : END IF
522 : ! Write to file
523 68 : WRITE (file_name, '(2(A,I0))') prefix//"_SPIN_", ispin, "_K_", ik
524 : unit_nr = cp_print_key_unit_nr(logger, dft_section, subs_string, &
525 : extension=".csr", middle_name=TRIM(file_name), &
526 68 : file_status="REPLACE", file_form=fileformat)
527 68 : CALL dbcsr_csr_write(mat_csr, unit_nr, upper_triangle=uptr, threshold=thld, binary=bin)
528 :
529 68 : CALL cp_print_key_finished_output(unit_nr, logger, dft_section, subs_string)
530 :
531 104 : CALL dbcsr_csr_destroy(mat_csr)
532 : END DO
533 : END DO
534 : END DO
535 20 : CALL dbcsr_release(rmatrix)
536 20 : DEALLOCATE (rmatrix)
537 20 : IF (.NOT. use_real_wfn) THEN
538 16 : CALL dbcsr_release(rmatrix_nosym)
539 16 : CALL dbcsr_release(imatrix)
540 16 : CALL dbcsr_release(imatrix_nosym)
541 16 : DEALLOCATE (rmatrix_nosym, imatrix, imatrix_nosym)
542 : END IF
543 20 : CALL timestop(handle)
544 :
545 20 : END SUBROUTINE write_matrix_kp_csr
546 :
547 : ! **************************************************************************************************
548 : !> \brief Desymmetrizes the KS or S matrices which are stored in symmetric !matrices
549 : !> \param mat Hamiltonian or overlap matrices
550 : !> \param mat_nosym Desymmetrized Hamiltonian or overlap matrices
551 : !> \param cell_to_index Mapping of cell indices to linear RS indices
552 : !> \param index_to_cell Mapping of linear RS indices to cell indices
553 : !> \param kpoints Kpoint environment
554 : !> \author Fabian Ducry
555 : ! **************************************************************************************************
556 2 : SUBROUTINE desymmetrize_rs_matrix(mat, mat_nosym, cell_to_index, index_to_cell, kpoints)
557 : TYPE(dbcsr_p_type), DIMENSION(:, :), INTENT(IN), &
558 : POINTER :: mat
559 : TYPE(dbcsr_p_type), DIMENSION(:, :), &
560 : INTENT(INOUT), POINTER :: mat_nosym
561 : INTEGER, ALLOCATABLE, DIMENSION(:, :, :), &
562 : INTENT(OUT) :: cell_to_index
563 : INTEGER, ALLOCATABLE, DIMENSION(:, :), INTENT(OUT) :: index_to_cell
564 : TYPE(kpoint_type), INTENT(IN), POINTER :: kpoints
565 :
566 : CHARACTER(len=*), PARAMETER :: routineN = 'desymmetrize_rs_matrix'
567 :
568 : INTEGER :: handle, iatom, ic, icn, icol, irow, &
569 : ispin, jatom, ncell, nomirror, nspin, &
570 : nx, ny, nz
571 : INTEGER, DIMENSION(3) :: cell
572 2 : INTEGER, DIMENSION(:, :), POINTER :: i2c
573 2 : INTEGER, DIMENSION(:, :, :), POINTER :: c2i
574 : LOGICAL :: found, lwtr
575 2 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: block
576 : TYPE(neighbor_list_iterator_p_type), &
577 2 : DIMENSION(:), POINTER :: nl_iterator
578 : TYPE(neighbor_list_set_p_type), DIMENSION(:), &
579 2 : POINTER :: sab_nl
580 :
581 2 : CALL timeset(routineN, handle)
582 :
583 2 : i2c => kpoints%index_to_cell
584 2 : c2i => kpoints%cell_to_index
585 :
586 2 : ncell = SIZE(i2c, 2)
587 2 : nspin = SIZE(mat, 1)
588 :
589 2 : nx = MAX(ABS(LBOUND(c2i, 1)), ABS(UBOUND(c2i, 1)))
590 2 : ny = MAX(ABS(LBOUND(c2i, 2)), ABS(UBOUND(c2i, 3)))
591 2 : nz = MAX(ABS(LBOUND(c2i, 3)), ABS(UBOUND(c2i, 3)))
592 10 : ALLOCATE (cell_to_index(-nx:nx, -ny:ny, -nz:nz))
593 : cell_to_index(LBOUND(c2i, 1):UBOUND(c2i, 1), &
594 : LBOUND(c2i, 2):UBOUND(c2i, 2), &
595 84 : LBOUND(c2i, 3):UBOUND(c2i, 3)) = c2i
596 :
597 : ! identify cells with no mirror img
598 : nomirror = 0
599 52 : DO ic = 1, ncell
600 200 : cell = i2c(:, ic)
601 52 : IF (cell_to_index(-cell(1), -cell(2), -cell(3)) == 0) THEN
602 4 : nomirror = nomirror + 1
603 : END IF
604 : END DO
605 :
606 : ! create the mirror imgs
607 6 : ALLOCATE (index_to_cell(3, ncell + nomirror))
608 202 : index_to_cell(:, 1:ncell) = i2c
609 :
610 : nomirror = 0 ! count the imgs without mirror
611 52 : DO ic = 1, ncell
612 200 : cell = index_to_cell(:, ic)
613 52 : IF (cell_to_index(-cell(1), -cell(2), -cell(3)) == 0) THEN
614 4 : nomirror = nomirror + 1
615 16 : index_to_cell(:, ncell + nomirror) = -cell
616 4 : cell_to_index(-cell(1), -cell(2), -cell(3)) = ncell + nomirror
617 : END IF
618 : END DO
619 2 : ncell = ncell + nomirror
620 :
621 2 : CALL get_kpoint_info(kpoints, sab_nl=sab_nl)
622 : ! allocate the nonsymmetric matrices
623 2 : NULLIFY (mat_nosym)
624 2 : CALL dbcsr_allocate_matrix_set(mat_nosym, nspin, ncell)
625 4 : DO ispin = 1, nspin
626 58 : DO ic = 1, ncell
627 54 : ALLOCATE (mat_nosym(ispin, ic)%matrix)
628 : CALL dbcsr_create(matrix=mat_nosym(ispin, ic)%matrix, &
629 : template=mat(1, 1)%matrix, &
630 54 : matrix_type=dbcsr_type_no_symmetry)
631 : CALL cp_dbcsr_alloc_block_from_nbl(mat_nosym(ispin, ic)%matrix, &
632 54 : sab_nl, desymmetrize=.TRUE.)
633 56 : CALL dbcsr_set(mat_nosym(ispin, ic)%matrix, 0.0_dp)
634 : END DO
635 : END DO
636 :
637 4 : DO ispin = 1, nspin
638 : ! desymmetrize the matrix for real space printing
639 2 : CALL neighbor_list_iterator_create(nl_iterator, sab_nl)
640 506 : DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
641 504 : CALL get_iterator_info(nl_iterator, iatom=iatom, jatom=jatom, cell=cell)
642 :
643 504 : ic = cell_to_index(cell(1), cell(2), cell(3))
644 504 : icn = cell_to_index(-cell(1), -cell(2), -cell(3))
645 504 : CPASSERT(icn > 0)
646 :
647 504 : irow = iatom
648 504 : icol = jatom
649 504 : lwtr = .FALSE.
650 : ! always copy from the top
651 504 : IF (iatom > jatom) THEN
652 216 : irow = jatom
653 216 : icol = iatom
654 216 : lwtr = .TRUE.
655 : END IF
656 :
657 : CALL dbcsr_get_block_p(matrix=mat(ispin, ic)%matrix, &
658 504 : row=irow, col=icol, block=block, found=found)
659 504 : CPASSERT(found)
660 :
661 : ! copy to M(R) at (iatom,jatom)
662 : ! copy to M(-R) at (jatom,iatom)
663 506 : IF (lwtr) THEN
664 : CALL dbcsr_put_block(matrix=mat_nosym(ispin, ic)%matrix, &
665 4536 : row=iatom, col=jatom, block=TRANSPOSE(block))
666 : CALL dbcsr_put_block(matrix=mat_nosym(ispin, icn)%matrix, &
667 216 : row=jatom, col=iatom, block=block)
668 : ELSE
669 : CALL dbcsr_put_block(matrix=mat_nosym(ispin, ic)%matrix, &
670 288 : row=iatom, col=jatom, block=block)
671 : CALL dbcsr_put_block(matrix=mat_nosym(ispin, icn)%matrix, &
672 6048 : row=jatom, col=iatom, block=TRANSPOSE(block))
673 : END IF
674 : END DO
675 4 : CALL neighbor_list_iterator_release(nl_iterator)
676 : END DO
677 :
678 4 : DO ispin = 1, nspin
679 58 : DO ic = 1, ncell
680 56 : CALL dbcsr_finalize(mat_nosym(ispin, ic)%matrix)
681 : END DO
682 : END DO
683 :
684 2 : CALL timestop(handle)
685 :
686 2 : END SUBROUTINE desymmetrize_rs_matrix
687 :
688 : END MODULE qs_scf_csr_write
|