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 DBCSR output in CP2K
10 : !> \author VW
11 : !> \date 2009-09-09
12 : !> \version 0.1
13 : !>
14 : !> <b>Modification history:</b>
15 : !> - Created 2009-09-09
16 : ! **************************************************************************************************
17 : MODULE cp_dbcsr_output
18 : USE atomic_kind_types, ONLY: atomic_kind_type,&
19 : get_atomic_kind
20 : USE basis_set_types, ONLY: get_gto_basis_set,&
21 : gto_basis_set_type
22 : USE cp_dbcsr_api, ONLY: &
23 : dbcsr_get_data_size, dbcsr_get_info, dbcsr_get_matrix_type, dbcsr_get_num_blocks, &
24 : dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, dbcsr_iterator_start, &
25 : dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_type, dbcsr_type_antisymmetric, &
26 : dbcsr_type_no_symmetry, dbcsr_type_symmetric
27 : USE cp_fm_types, ONLY: cp_fm_get_info,&
28 : cp_fm_get_submatrix,&
29 : cp_fm_type
30 : USE cp_log_handling, ONLY: cp_get_default_logger,&
31 : cp_logger_type
32 : USE kinds, ONLY: default_string_length,&
33 : dp,&
34 : int_8
35 : USE machine, ONLY: m_flush
36 : USE mathlib, ONLY: symmetrize_matrix
37 : USE message_passing, ONLY: mp_para_env_type
38 : USE orbital_pointers, ONLY: nco,&
39 : nso
40 : USE particle_methods, ONLY: get_particle_set
41 : USE particle_types, ONLY: particle_type
42 : USE qs_environment_types, ONLY: get_qs_env,&
43 : qs_environment_type
44 : USE qs_kind_types, ONLY: get_qs_kind,&
45 : get_qs_kind_set,&
46 : qs_kind_type
47 : #include "./base/base_uses.f90"
48 :
49 : IMPLICIT NONE
50 :
51 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'cp_dbcsr_output'
52 :
53 : PUBLIC :: cp_dbcsr_write_sparse_matrix
54 : PUBLIC :: cp_dbcsr_write_matrix_dist
55 : PUBLIC :: write_fm_with_basis_info
56 :
57 : PRIVATE
58 :
59 : CONTAINS
60 :
61 : ! **************************************************************************************************
62 : !> \brief Print a spherical matrix of blacs type.
63 : !> \param blacs_matrix ...
64 : !> \param before ...
65 : !> \param after ...
66 : !> \param qs_env ...
67 : !> \param para_env ...
68 : !> \param first_row ...
69 : !> \param last_row ...
70 : !> \param first_col ...
71 : !> \param last_col ...
72 : !> \param output_unit ...
73 : !> \param omit_headers Write only the matrix data, not the row/column headers
74 : !> \author Creation (12.06.2001,MK)
75 : !> Allow for printing of a sub-matrix (01.07.2003,MK)
76 : ! **************************************************************************************************
77 8 : SUBROUTINE write_fm_with_basis_info(blacs_matrix, before, after, qs_env, para_env, &
78 : first_row, last_row, first_col, last_col, output_unit, omit_headers)
79 :
80 : TYPE(cp_fm_type), INTENT(IN) :: blacs_matrix
81 : INTEGER, INTENT(IN) :: before, after
82 : TYPE(qs_environment_type), POINTER :: qs_env
83 : TYPE(mp_para_env_type), POINTER :: para_env
84 : INTEGER, INTENT(IN), OPTIONAL :: first_row, last_row, first_col, last_col
85 : INTEGER, INTENT(IN) :: output_unit
86 : LOGICAL, INTENT(IN), OPTIONAL :: omit_headers
87 :
88 : CHARACTER(LEN=60) :: matrix_name
89 : INTEGER :: col1, col2, ncol_global, nrow_global, &
90 : nsgf, row1, row2
91 : LOGICAL :: my_omit_headers
92 8 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: matrix
93 8 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
94 :
95 0 : IF (.NOT. ASSOCIATED(blacs_matrix%matrix_struct)) RETURN
96 : CALL cp_fm_get_info(blacs_matrix, name=matrix_name, nrow_global=nrow_global, &
97 8 : ncol_global=ncol_global)
98 :
99 32 : ALLOCATE (matrix(nrow_global, ncol_global))
100 8 : CALL cp_fm_get_submatrix(blacs_matrix, matrix)
101 :
102 : ! *** Get the matrix dimension and check the optional arguments ***
103 8 : CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set)
104 8 : CALL get_qs_kind_set(qs_kind_set=qs_kind_set, nsgf=nsgf)
105 :
106 8 : IF (PRESENT(first_row)) THEN
107 0 : row1 = MAX(1, first_row)
108 : ELSE
109 8 : row1 = 1
110 : END IF
111 :
112 8 : IF (PRESENT(last_row)) THEN
113 0 : row2 = MIN(nsgf, last_row)
114 : ELSE
115 8 : row2 = nsgf
116 : END IF
117 :
118 8 : IF (PRESENT(first_col)) THEN
119 0 : col1 = MAX(1, first_col)
120 : ELSE
121 8 : col1 = 1
122 : END IF
123 :
124 8 : IF (PRESENT(last_col)) THEN
125 0 : col2 = MIN(nsgf, last_col)
126 : ELSE
127 8 : col2 = nsgf
128 : END IF
129 :
130 8 : IF (PRESENT(omit_headers)) THEN
131 4 : my_omit_headers = omit_headers
132 : ELSE
133 4 : my_omit_headers = .FALSE.
134 : END IF
135 :
136 : CALL write_matrix_sym(matrix, matrix_name, before, after, qs_env, para_env, &
137 8 : row1, row2, col1, col2, output_unit, omit_headers=my_omit_headers)
138 :
139 : ! *** Release work storage ***
140 8 : IF (ASSOCIATED(matrix)) THEN
141 8 : DEALLOCATE (matrix)
142 : END IF
143 :
144 16 : END SUBROUTINE write_fm_with_basis_info
145 :
146 : ! **************************************************************************************************
147 : !> \brief ...
148 : !> \param sparse_matrix ...
149 : !> \param before ...
150 : !> \param after ...
151 : !> \param qs_env ...
152 : !> \param para_env ...
153 : !> \param first_row ...
154 : !> \param last_row ...
155 : !> \param first_col ...
156 : !> \param last_col ...
157 : !> \param scale ...
158 : !> \param output_unit ...
159 : !> \param omit_headers Write only the matrix data, not the row/column headers
160 : !> \param cartesian_basis Use Cartesian instead of spherical basis labels
161 : ! **************************************************************************************************
162 16054 : SUBROUTINE cp_dbcsr_write_sparse_matrix(sparse_matrix, before, after, qs_env, para_env, &
163 : first_row, last_row, first_col, last_col, scale, &
164 : output_unit, omit_headers, cartesian_basis)
165 :
166 : TYPE(dbcsr_type) :: sparse_matrix
167 : INTEGER, INTENT(IN) :: before, after
168 : TYPE(qs_environment_type), POINTER :: qs_env
169 : TYPE(mp_para_env_type), POINTER :: para_env
170 : INTEGER, INTENT(IN), OPTIONAL :: first_row, last_row, first_col, last_col
171 : REAL(dp), INTENT(IN), OPTIONAL :: scale
172 : INTEGER, INTENT(IN) :: output_unit
173 : LOGICAL, INTENT(IN), OPTIONAL :: omit_headers, cartesian_basis
174 :
175 : CHARACTER(LEN=default_string_length) :: matrix_name
176 : INTEGER :: col1, col2, dim_col, dim_row, row1, row2
177 : LOGICAL :: my_cartesian_basis, my_omit_headers, &
178 : print_sym
179 16054 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: matrix
180 16054 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
181 :
182 16054 : NULLIFY (matrix)
183 :
184 16054 : CALL copy_repl_dbcsr_to_repl_fm(sparse_matrix, matrix)
185 :
186 3496270 : CALL para_env%sum(matrix)
187 :
188 16012 : SELECT CASE (dbcsr_get_matrix_type(sparse_matrix))
189 : CASE (dbcsr_type_symmetric)
190 16012 : CALL symmetrize_matrix(matrix, "upper_to_lower")
191 16012 : print_sym = .TRUE.
192 : CASE (dbcsr_type_antisymmetric)
193 42 : CALL symmetrize_matrix(matrix, "anti_upper_to_lower")
194 42 : print_sym = .TRUE.
195 : CASE (dbcsr_type_no_symmetry)
196 0 : print_sym = .FALSE.
197 : CASE DEFAULT
198 16054 : CPABORT("WRONG")
199 : END SELECT
200 :
201 : ! *** Get the matrix dimension and check the optional arguments ***
202 16054 : CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set)
203 16054 : dim_row = SIZE(matrix, 1)
204 16054 : dim_col = SIZE(matrix, 2)
205 :
206 16054 : IF (PRESENT(first_row)) THEN
207 0 : row1 = MAX(1, first_row)
208 : ELSE
209 16054 : row1 = 1
210 : END IF
211 :
212 16054 : IF (PRESENT(last_row)) THEN
213 0 : row2 = MIN(dim_row, last_row)
214 : ELSE
215 16054 : row2 = dim_row
216 : END IF
217 :
218 16054 : IF (PRESENT(first_col)) THEN
219 0 : col1 = MAX(1, first_col)
220 : ELSE
221 16054 : col1 = 1
222 : END IF
223 :
224 16054 : IF (PRESENT(last_col)) THEN
225 0 : col2 = MIN(dim_col, last_col)
226 : ELSE
227 16054 : col2 = dim_col
228 : END IF
229 :
230 16054 : IF (PRESENT(scale)) THEN
231 810626 : matrix = matrix*scale
232 : END IF
233 :
234 16054 : IF (PRESENT(omit_headers)) THEN
235 15854 : my_omit_headers = omit_headers
236 : ELSE
237 200 : my_omit_headers = .FALSE.
238 : END IF
239 16054 : my_cartesian_basis = .FALSE.
240 16054 : IF (PRESENT(cartesian_basis)) my_cartesian_basis = cartesian_basis
241 :
242 16054 : CALL dbcsr_get_info(sparse_matrix, name=matrix_name)
243 16054 : IF (print_sym) THEN
244 : CALL write_matrix_sym(matrix, matrix_name, before, after, qs_env, para_env, &
245 : row1, row2, col1, col2, output_unit, my_omit_headers, &
246 16054 : cartesian_basis=my_cartesian_basis)
247 : ELSE
248 : CALL write_matrix_gen(matrix, matrix_name, before, after, para_env, &
249 0 : row1, row2, col1, col2, output_unit, my_omit_headers)
250 : END IF
251 :
252 16054 : IF (ASSOCIATED(matrix)) THEN
253 16054 : DEALLOCATE (matrix)
254 : END IF
255 :
256 16054 : END SUBROUTINE cp_dbcsr_write_sparse_matrix
257 :
258 : ! **************************************************************************************************
259 : !> \brief ...
260 : !> \param sparse_matrix ...
261 : !> \param fm ...
262 : ! **************************************************************************************************
263 16054 : SUBROUTINE copy_repl_dbcsr_to_repl_fm(sparse_matrix, fm)
264 :
265 : TYPE(dbcsr_type) :: sparse_matrix
266 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: fm
267 :
268 : CHARACTER(len=*), PARAMETER :: routineN = 'copy_repl_dbcsr_to_repl_fm'
269 :
270 : INTEGER :: col, handle, i, j, nblkcols_total, &
271 : nblkrows_total, nc, nr, row
272 16054 : INTEGER, ALLOCATABLE, DIMENSION(:) :: c_offset, r_offset
273 16054 : INTEGER, DIMENSION(:), POINTER :: col_blk_size, row_blk_size
274 16054 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: block
275 : TYPE(dbcsr_iterator_type) :: iter
276 :
277 16054 : CALL timeset(routineN, handle)
278 :
279 16054 : IF (ASSOCIATED(fm)) DEALLOCATE (fm)
280 :
281 : CALL dbcsr_get_info(matrix=sparse_matrix, &
282 : col_blk_size=col_blk_size, &
283 : row_blk_size=row_blk_size, &
284 : nblkrows_total=nblkrows_total, &
285 16054 : nblkcols_total=nblkcols_total)
286 :
287 : !> this should be precomputed somewhere else
288 80270 : ALLOCATE (r_offset(nblkrows_total), c_offset(nblkcols_total))
289 :
290 16054 : r_offset(1) = 1
291 33816 : DO row = 2, nblkrows_total
292 33816 : r_offset(row) = r_offset(row - 1) + row_blk_size(row - 1)
293 : END DO
294 49870 : nr = SUM(row_blk_size)
295 16054 : c_offset(1) = 1
296 33816 : DO col = 2, nblkcols_total
297 33816 : c_offset(col) = c_offset(col - 1) + col_blk_size(col - 1)
298 : END DO
299 49870 : nc = SUM(col_blk_size)
300 : !<
301 :
302 64216 : ALLOCATE (fm(nr, nc))
303 :
304 1756162 : fm(:, :) = 0.0_dp
305 :
306 : !$OMP PARALLEL DEFAULT(NONE) SHARED(sparse_matrix, fm, r_offset, c_offset) &
307 16054 : !$OMP PRIVATE(iter, row, col, block, i, j)
308 : CALL dbcsr_iterator_start(iter, sparse_matrix)
309 : DO WHILE (dbcsr_iterator_blocks_left(iter))
310 : CALL dbcsr_iterator_next_block(iter, row, col, block)
311 : DO j = 1, SIZE(block, 2)
312 : DO i = 1, SIZE(block, 1)
313 : fm(r_offset(row) + i - 1, c_offset(col) + j - 1) = block(i, j)
314 : END DO
315 : END DO
316 : END DO
317 : CALL dbcsr_iterator_stop(iter)
318 : !$OMP END PARALLEL
319 :
320 16054 : DEALLOCATE (r_offset, c_offset)
321 :
322 16054 : CALL timestop(handle)
323 :
324 48162 : END SUBROUTINE copy_repl_dbcsr_to_repl_fm
325 :
326 : ! **************************************************************************************************
327 : !> \brief Write a matrix or a sub-matrix to the output unit (symmetric)
328 : !> \param matrix ...
329 : !> \param matrix_name ...
330 : !> \param before ...
331 : !> \param after ...
332 : !> \param qs_env ...
333 : !> \param para_env ...
334 : !> \param first_row ...
335 : !> \param last_row ...
336 : !> \param first_col ...
337 : !> \param last_col ...
338 : !> \param output_unit ...
339 : !> \param omit_headers Write only the matrix data, not the row/column headers
340 : !> \param cartesian_basis Use Cartesian instead of spherical basis labels
341 : !> \author Creation (01.07.2003,MK)
342 : ! **************************************************************************************************
343 16062 : SUBROUTINE write_matrix_sym(matrix, matrix_name, before, after, qs_env, para_env, &
344 : first_row, last_row, first_col, last_col, output_unit, omit_headers, cartesian_basis)
345 :
346 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: matrix
347 : CHARACTER(LEN=*), INTENT(IN) :: matrix_name
348 : INTEGER, INTENT(IN) :: before, after
349 : TYPE(qs_environment_type), POINTER :: qs_env
350 : TYPE(mp_para_env_type), POINTER :: para_env
351 : INTEGER, INTENT(IN) :: first_row, last_row, first_col, &
352 : last_col, output_unit
353 : LOGICAL, INTENT(IN) :: omit_headers
354 : LOGICAL, INTENT(IN), OPTIONAL :: cartesian_basis
355 :
356 16062 : CHARACTER(LEN=12), DIMENSION(:), POINTER :: cgf_symbol
357 : CHARACTER(LEN=2) :: element_symbol
358 : CHARACTER(LEN=25) :: fmtstr1
359 : CHARACTER(LEN=35) :: fmtstr2
360 16062 : CHARACTER(LEN=6), DIMENSION(:), POINTER :: sgf_symbol
361 : INTEGER :: from, iatom, icol, ikind, irow, iset, &
362 : isgf, ishell, iso, jcol, l, left, &
363 : natom, ncol, ndigits, nset, nsgf, &
364 : right, to, width
365 16062 : INTEGER, ALLOCATABLE, DIMENSION(:) :: first_sgf, last_sgf
366 16062 : INTEGER, DIMENSION(:), POINTER :: nshell
367 16062 : INTEGER, DIMENSION(:, :), POINTER :: lshell
368 : LOGICAL :: my_cartesian_basis
369 16062 : TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
370 : TYPE(gto_basis_set_type), POINTER :: orb_basis_set
371 16062 : TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
372 16062 : TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
373 :
374 16062 : IF (output_unit > 0) THEN
375 8031 : CALL m_flush(output_unit)
376 :
377 : CALL get_qs_env(qs_env=qs_env, &
378 : qs_kind_set=qs_kind_set, &
379 : atomic_kind_set=atomic_kind_set, &
380 8031 : particle_set=particle_set)
381 :
382 8031 : natom = SIZE(particle_set)
383 :
384 8031 : my_cartesian_basis = .FALSE.
385 8031 : IF (PRESENT(cartesian_basis)) my_cartesian_basis = cartesian_basis
386 :
387 8031 : CALL get_qs_kind_set(qs_kind_set=qs_kind_set, nsgf=nsgf)
388 :
389 24093 : ALLOCATE (first_sgf(natom))
390 16062 : ALLOCATE (last_sgf(natom))
391 : CALL get_particle_set(particle_set, qs_kind_set, &
392 : first_sgf=first_sgf, &
393 8031 : last_sgf=last_sgf)
394 :
395 : ! *** Definition of the variable formats ***
396 8031 : fmtstr1 = "(/,T2,23X, ( X,I5, X))"
397 8031 : IF (omit_headers) THEN
398 46 : fmtstr2 = "(T2, (1X,F . ))"
399 : ELSE
400 7985 : fmtstr2 = "(T2,2I5,2X,A2,1X,A8, (1X,F . ))"
401 : END IF
402 :
403 : ! *** Write headline ***
404 8031 : WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") TRIM(matrix_name)
405 :
406 : ! *** Write the variable format strings ***
407 8031 : ndigits = after
408 :
409 8031 : width = before + ndigits + 3
410 8031 : ncol = INT(56/width)
411 :
412 8031 : right = MAX((ndigits - 2), 1)
413 8031 : left = width - right - 5
414 :
415 8031 : WRITE (UNIT=fmtstr1(11:12), FMT="(I2)") ncol
416 8031 : WRITE (UNIT=fmtstr1(14:15), FMT="(I2)") left
417 8031 : WRITE (UNIT=fmtstr1(21:22), FMT="(I2)") right
418 :
419 8031 : IF (omit_headers) THEN
420 46 : WRITE (UNIT=fmtstr2(6:7), FMT="(I2)") ncol
421 46 : WRITE (UNIT=fmtstr2(13:14), FMT="(I2)") width - 1
422 46 : WRITE (UNIT=fmtstr2(16:17), FMT="(I2)") ndigits
423 : ELSE
424 7985 : WRITE (UNIT=fmtstr2(22:23), FMT="(I2)") ncol
425 7985 : WRITE (UNIT=fmtstr2(29:30), FMT="(I2)") width - 1
426 7985 : WRITE (UNIT=fmtstr2(32:33), FMT="(I2)") ndigits
427 : END IF
428 :
429 : ! *** Write the matrix in the selected format ***
430 31818 : DO icol = first_col, last_col, ncol
431 23787 : from = icol
432 23787 : to = MIN((from + ncol - 1), last_col)
433 23787 : IF (.NOT. omit_headers) THEN
434 101160 : WRITE (UNIT=output_unit, FMT=fmtstr1) (jcol, jcol=from, to)
435 : END IF
436 23787 : irow = 1
437 82097 : DO iatom = 1, natom
438 50279 : NULLIFY (orb_basis_set)
439 : CALL get_atomic_kind(particle_set(iatom)%atomic_kind, &
440 50279 : kind_number=ikind, element_symbol=element_symbol)
441 50279 : CALL get_qs_kind(qs_kind_set(ikind), basis_set=orb_basis_set)
442 124345 : IF (ASSOCIATED(orb_basis_set)) THEN
443 : CALL get_gto_basis_set(gto_basis_set=orb_basis_set, &
444 : nset=nset, nshell=nshell, l=lshell, &
445 50279 : cgf_symbol=cgf_symbol, sgf_symbol=sgf_symbol)
446 50279 : isgf = 1
447 147644 : DO iset = 1, nset
448 248713 : DO ishell = 1, nshell(iset)
449 101069 : l = lshell(ishell, iset)
450 540860 : DO iso = 1, MERGE(nco(l), nso(l), my_cartesian_basis)
451 241361 : IF ((irow >= first_row) .AND. (irow <= last_row)) THEN
452 241361 : IF (omit_headers) THEN
453 : WRITE (UNIT=output_unit, FMT=fmtstr2) &
454 12818 : (matrix(irow, jcol), jcol=from, to)
455 : ELSE
456 237757 : IF (my_cartesian_basis) THEN
457 : WRITE (UNIT=output_unit, FMT=fmtstr2) &
458 4 : irow, iatom, element_symbol, cgf_symbol(isgf), &
459 24 : (matrix(irow, jcol), jcol=from, to)
460 : ELSE
461 : WRITE (UNIT=output_unit, FMT=fmtstr2) &
462 237753 : irow, iatom, element_symbol, sgf_symbol(isgf), &
463 1261799 : (matrix(irow, jcol), jcol=from, to)
464 : END IF
465 : END IF
466 : END IF
467 241361 : isgf = isgf + 1
468 342430 : irow = irow + 1
469 : END DO
470 : END DO
471 : END DO
472 50279 : IF ((irow >= first_row) .AND. (irow <= last_row)) THEN
473 26492 : WRITE (UNIT=output_unit, FMT="(A)")
474 : END IF
475 : ELSE
476 0 : DO iso = first_sgf(iatom), last_sgf(iatom)
477 0 : IF ((irow >= first_row) .AND. (irow <= last_row)) THEN
478 0 : IF (omit_headers) THEN
479 : WRITE (UNIT=output_unit, FMT=fmtstr2) &
480 0 : (matrix(irow, jcol), jcol=from, to)
481 : ELSE
482 : WRITE (UNIT=output_unit, FMT=fmtstr2) &
483 0 : irow, iatom, element_symbol, " ", &
484 0 : (matrix(irow, jcol), jcol=from, to)
485 : END IF
486 : END IF
487 0 : irow = irow + 1
488 : END DO
489 0 : IF ((irow >= first_row) .AND. (irow <= last_row)) THEN
490 0 : WRITE (UNIT=output_unit, FMT="(A)")
491 : END IF
492 : END IF
493 : END DO
494 : END DO
495 :
496 8031 : WRITE (UNIT=output_unit, FMT="(/)")
497 8031 : DEALLOCATE (first_sgf)
498 16062 : DEALLOCATE (last_sgf)
499 : END IF
500 :
501 16062 : CALL para_env%sync()
502 16062 : IF (output_unit > 0) CALL m_flush(output_unit)
503 :
504 32124 : END SUBROUTINE write_matrix_sym
505 :
506 : ! **************************************************************************************************
507 : !> \brief Write a matrix not necessarily symmetric (no index with atomic labels)
508 : !> \param matrix ...
509 : !> \param matrix_name ...
510 : !> \param before ...
511 : !> \param after ...
512 : !> \param para_env ...
513 : !> \param first_row ...
514 : !> \param last_row ...
515 : !> \param first_col ...
516 : !> \param last_col ...
517 : !> \param output_unit ...
518 : !> \param omit_headers Write only the matrix data, not the row/column headers
519 : !> \author Teodoro Laino [tlaino] - 10.2007 - University of Zurich
520 : ! **************************************************************************************************
521 0 : SUBROUTINE write_matrix_gen(matrix, matrix_name, before, after, para_env, &
522 : first_row, last_row, first_col, last_col, output_unit, omit_headers)
523 :
524 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: matrix
525 : CHARACTER(LEN=*), INTENT(IN) :: matrix_name
526 : INTEGER, INTENT(IN) :: before, after
527 : TYPE(mp_para_env_type), POINTER :: para_env
528 : INTEGER, INTENT(IN) :: first_row, last_row, first_col, &
529 : last_col, output_unit
530 : LOGICAL, INTENT(IN) :: omit_headers
531 :
532 : CHARACTER(LEN=25) :: fmtstr1
533 : CHARACTER(LEN=35) :: fmtstr2
534 : INTEGER :: from, icol, irow, jcol, left, ncol, &
535 : ndigits, right, to, width
536 :
537 0 : IF (output_unit > 0) THEN
538 0 : CALL m_flush(output_unit)
539 :
540 : ! *** Definition of the variable formats ***
541 0 : fmtstr1 = "(/,T2,23X, ( X,I5, X))"
542 0 : IF (omit_headers) THEN
543 0 : fmtstr2 = "(T2, (1X,F . ))"
544 : ELSE
545 0 : fmtstr2 = "(T2, I5, 18X, (1X,F . ))"
546 : END IF
547 :
548 : ! *** Write headline ***
549 0 : WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") TRIM(matrix_name)
550 :
551 : ! *** Write the variable format strings ***
552 0 : ndigits = after
553 :
554 0 : width = before + ndigits + 3
555 0 : ncol = INT(56/width)
556 :
557 0 : right = MAX((ndigits - 2), 1)
558 0 : left = width - right - 5
559 :
560 0 : WRITE (UNIT=fmtstr1(11:12), FMT="(I2)") ncol
561 0 : WRITE (UNIT=fmtstr1(14:15), FMT="(I2)") left
562 0 : WRITE (UNIT=fmtstr1(21:22), FMT="(I2)") right
563 :
564 0 : IF (omit_headers) THEN
565 0 : WRITE (UNIT=fmtstr2(6:7), FMT="(I2)") ncol
566 0 : WRITE (UNIT=fmtstr2(13:14), FMT="(I2)") width - 1
567 0 : WRITE (UNIT=fmtstr2(16:17), FMT="(I2)") ndigits
568 : ELSE
569 0 : WRITE (UNIT=fmtstr2(22:23), FMT="(I2)") ncol
570 0 : WRITE (UNIT=fmtstr2(29:30), FMT="(I2)") width - 1
571 0 : WRITE (UNIT=fmtstr2(32:33), FMT="(I2)") ndigits
572 : END IF
573 :
574 : ! *** Write the matrix in the selected format ***
575 0 : DO icol = first_col, last_col, ncol
576 0 : from = icol
577 0 : to = MIN((from + ncol - 1), last_col)
578 0 : IF (.NOT. omit_headers) THEN
579 0 : WRITE (UNIT=output_unit, FMT=fmtstr1) (jcol, jcol=from, to)
580 : END IF
581 : irow = 1
582 0 : DO irow = first_row, last_row
583 0 : IF (omit_headers) THEN
584 : WRITE (UNIT=output_unit, FMT=fmtstr2) &
585 0 : (matrix(irow, jcol), jcol=from, to)
586 : ELSE
587 : WRITE (UNIT=output_unit, FMT=fmtstr2) &
588 0 : irow, (matrix(irow, jcol), jcol=from, to)
589 : END IF
590 : END DO
591 : END DO
592 :
593 0 : WRITE (UNIT=output_unit, FMT="(/)")
594 : END IF
595 :
596 0 : CALL para_env%sync()
597 0 : IF (output_unit > 0) CALL m_flush(output_unit)
598 :
599 0 : END SUBROUTINE write_matrix_gen
600 :
601 : ! **************************************************************************************************
602 : !> \brief Print the distribution of a sparse matrix.
603 : !> \param matrix ...
604 : !> \param output_unit ...
605 : !> \param para_env ...
606 : !> \par History
607 : !> Creation (25.06.2003,MK)
608 : ! **************************************************************************************************
609 92 : SUBROUTINE cp_dbcsr_write_matrix_dist(matrix, output_unit, para_env)
610 : TYPE(dbcsr_type) :: matrix
611 : INTEGER, INTENT(IN) :: output_unit
612 : TYPE(mp_para_env_type), POINTER :: para_env
613 :
614 : CHARACTER(LEN=*), PARAMETER :: routineN = 'cp_dbcsr_write_matrix_dist'
615 : LOGICAL, PARAMETER :: full_output = .FALSE.
616 :
617 : CHARACTER :: matrix_type
618 : CHARACTER(LEN=default_string_length) :: matrix_name
619 : INTEGER :: handle, ipe, mype, natom, nblock_max, &
620 : nelement_max, npe, nrow, tmp(2)
621 : INTEGER(KIND=int_8) :: nblock_sum, nblock_tot, nelement_sum
622 92 : INTEGER, ALLOCATABLE, DIMENSION(:) :: nblock, nelement
623 : LOGICAL :: ionode
624 : REAL(KIND=dp) :: occupation
625 : TYPE(cp_logger_type), POINTER :: logger
626 :
627 92 : NULLIFY (logger)
628 92 : logger => cp_get_default_logger()
629 :
630 92 : CALL timeset(routineN, handle)
631 :
632 92 : ionode = para_env%is_source()
633 92 : mype = para_env%mepos + 1
634 92 : npe = para_env%num_pe
635 :
636 : ! *** Allocate work storage ***
637 276 : ALLOCATE (nblock(npe))
638 : nblock(:) = 0
639 :
640 184 : ALLOCATE (nelement(npe))
641 : nelement(:) = 0
642 :
643 92 : nblock(mype) = dbcsr_get_num_blocks(matrix)
644 92 : nelement(mype) = dbcsr_get_data_size(matrix)
645 :
646 : CALL dbcsr_get_info(matrix=matrix, &
647 : name=matrix_name, &
648 : matrix_type=matrix_type, &
649 : nblkrows_total=natom, &
650 92 : nfullrows_total=nrow)
651 :
652 : IF (full_output) THEN
653 : ! XXXXXXXX should gather/scatter this on ionode
654 : CALL para_env%sum(nblock)
655 : CALL para_env%sum(nelement)
656 :
657 : nblock_sum = SUM(INT(nblock, KIND=int_8))
658 : nelement_sum = SUM(INT(nelement, KIND=int_8))
659 : ELSE
660 92 : nblock_sum = nblock(mype)
661 : nblock_max = nblock(mype)
662 92 : nelement_sum = nelement(mype)
663 : nelement_max = nelement(mype)
664 92 : CALL para_env%sum(nblock_sum)
665 92 : CALL para_env%sum(nelement_sum)
666 276 : tmp = [nblock_max, nelement_max]
667 92 : CALL para_env%max(tmp)
668 92 : nblock_max = tmp(1); nelement_max = tmp(2)
669 : END IF
670 :
671 92 : IF (matrix_type == dbcsr_type_symmetric .OR. &
672 : matrix_type == dbcsr_type_antisymmetric) THEN
673 92 : nblock_tot = INT(natom, KIND=int_8)*INT(natom + 1, KIND=int_8)/2
674 : ELSE
675 0 : nblock_tot = INT(natom, KIND=int_8)**2
676 : END IF
677 :
678 92 : occupation = -1.0_dp
679 92 : IF (nblock_tot /= 0) occupation = 100.0_dp*REAL(nblock_sum, dp)/REAL(nblock_tot, dp)
680 :
681 92 : IF (ionode) THEN
682 : WRITE (UNIT=output_unit, FMT="(/,/,T2,A)") &
683 46 : "DISTRIBUTION OF THE "//TRIM(matrix_name)
684 : IF (full_output) THEN
685 : WRITE (UNIT=output_unit, FMT="(/,T3,A,/,/,(I9,T27,I10,T55,I10))") &
686 : "Process Number of matrix blocks Number of matrix elements", &
687 : (ipe - 1, nblock(ipe), nelement(ipe), ipe=1, npe)
688 : WRITE (UNIT=output_unit, FMT="(/,T7,A3,T27,I10,T55,I10)") &
689 : "Sum", nblock_sum, nelement_sum
690 : WRITE (UNIT=output_unit, FMT="(/,T7,A3,T27,I10,A,F5.1,A,T55,I10,A,F5.1,A)") &
691 : " of", nblock_tot, " (", occupation, " % occupation)"
692 : ELSE
693 46 : WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Number of non-zero blocks:", nblock_sum
694 46 : WRITE (UNIT=output_unit, FMT="(T15,A,T75,F6.2)") "Percentage non-zero blocks:", occupation
695 46 : WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Average number of blocks per CPU:", &
696 92 : (nblock_sum + npe - 1)/npe
697 46 : WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Maximum number of blocks per CPU:", nblock_max
698 46 : WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Average number of matrix elements per CPU:", &
699 92 : (nelement_sum + npe - 1)/npe
700 46 : WRITE (UNIT=output_unit, FMT="(T15,A,T68,I13)") "Maximum number of matrix elements per CPU:", &
701 92 : nelement_max
702 : END IF
703 : END IF
704 :
705 : ! *** Release work storage ***
706 92 : DEALLOCATE (nblock)
707 :
708 92 : DEALLOCATE (nelement)
709 :
710 92 : CALL timestop(handle)
711 :
712 184 : END SUBROUTINE cp_dbcsr_write_matrix_dist
713 :
714 : END MODULE cp_dbcsr_output
|