(git:d3d49ac)
Loading...
Searching...
No Matches
pao_io.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 Routines for reading and writing restart files.
10!> \author Ole Schuett
11! **************************************************************************************************
12MODULE pao_io
16 USE cell_types, ONLY: cell_type
17 USE cp_dbcsr_api, ONLY: &
19 dbcsr_csr_dbcsr_blkrow_dist, dbcsr_csr_destroy, dbcsr_csr_type, dbcsr_csr_write, &
22 USE cp_files, ONLY: close_file,&
27 USE cp_output_handling, ONLY: cp_p_file,&
35 USE kinds, ONLY: default_path_length,&
37 dp
39 USE pao_input, ONLY: id2str
40 USE pao_param, ONLY: pao_param_count
41 USE pao_types, ONLY: pao_env_type
43 USE physcon, ONLY: angstrom
46 USE qs_kind_types, ONLY: get_qs_kind,&
49#include "./base/base_uses.f90"
50
51 IMPLICIT NONE
52
53 PRIVATE
54
55 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'pao_io'
56
62
63 ! data types used by pao_read_raw()
65 REAL(dp), DIMENSION(:, :), ALLOCATABLE :: p
66 END TYPE pao_ioblock_type
67
69 CHARACTER(LEN=default_string_length) :: name = ""
70 INTEGER :: z = -1
71 CHARACTER(LEN=default_string_length) :: prim_basis_name = ""
72 INTEGER :: prim_basis_size = -1
73 INTEGER :: pao_basis_size = -1
74 INTEGER :: nparams = -1
75 TYPE(pao_potential_type), ALLOCATABLE, DIMENSION(:) :: pao_potentials
76 END TYPE pao_iokind_type
77
78 INTEGER, PARAMETER, PRIVATE :: file_format_version = 4
79
80CONTAINS
81
82! **************************************************************************************************
83!> \brief Reads restart file
84!> \param pao ...
85!> \param qs_env ...
86! **************************************************************************************************
87 SUBROUTINE pao_read_restart(pao, qs_env)
88 TYPE(pao_env_type), POINTER :: pao
89 TYPE(qs_environment_type), POINTER :: qs_env
90
91 CHARACTER(LEN=default_string_length) :: param
92 INTEGER :: iatom, ikind, natoms
93 INTEGER, ALLOCATABLE, DIMENSION(:) :: atom2kind
94 INTEGER, DIMENSION(:), POINTER :: col_blk_sizes, row_blk_sizes
95 LOGICAL :: found
96 REAL(dp) :: diff
97 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: hmat, positions
98 REAL(dp), DIMENSION(:, :), POINTER :: block_x, buffer
99 TYPE(cell_type), POINTER :: cell
100 TYPE(mp_para_env_type), POINTER :: para_env
101 TYPE(pao_ioblock_type), ALLOCATABLE, DIMENSION(:) :: xblocks
102 TYPE(pao_iokind_type), ALLOCATABLE, DIMENSION(:) :: kinds
103 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
104
105 cpassert(len_trim(pao%restart_file) > 0)
106 IF (pao%iw > 0) WRITE (pao%iw, '(A,A)') " PAO| Reading matrix_X from restart file: ", trim(pao%restart_file)
107
108 CALL get_qs_env(qs_env, &
109 para_env=para_env, &
110 natom=natoms, &
111 cell=cell, &
112 particle_set=particle_set)
113
114 ! read and check restart file on first rank only
115 IF (para_env%is_source()) THEN
116 CALL pao_read_raw(pao%restart_file, param, hmat, kinds, atom2kind, positions, xblocks)
117
118 ! check cell
119 IF (maxval(abs(hmat - cell%hmat)) > 1e-10) THEN
120 cpwarn("Restarting from different cell")
121 END IF
122
123 ! check parametrization
124 IF (trim(param) /= trim(adjustl(id2str(pao%parameterization)))) THEN
125 cpabort("Restart PAO parametrization does not match")
126 END IF
127
128 ! check kinds
129 DO ikind = 1, SIZE(kinds)
130 CALL pao_kinds_ensure_equal(pao, qs_env, ikind, kinds(ikind))
131 END DO
132
133 ! check number of atoms
134 IF (SIZE(positions, 1) /= natoms) THEN
135 cpabort("Number of atoms do not match")
136 END IF
137
138 ! check atom2kind
139 DO iatom = 1, natoms
140 IF (atom2kind(iatom) /= particle_set(iatom)%atomic_kind%kind_number) THEN
141 cpabort("Restart atomic kinds do not match.")
142 END IF
143 END DO
144
145 ! check positions, warning only
146 diff = 0.0_dp
147 DO iatom = 1, natoms
148 diff = max(diff, maxval(abs(positions(iatom, :) - particle_set(iatom)%r)))
149 END DO
150 cpwarn_if(diff > 1e-10, "Restarting from different atom positions")
151
152 END IF
153
154 ! scatter xblocks across ranks to fill pao%matrix_X
155 ! this could probably be done more efficiently
156 CALL dbcsr_get_info(pao%matrix_X, row_blk_size=row_blk_sizes, col_blk_size=col_blk_sizes)
157 DO iatom = 1, natoms
158 ALLOCATE (buffer(row_blk_sizes(iatom), col_blk_sizes(iatom)))
159 IF (para_env%is_source()) THEN
160 cpassert(row_blk_sizes(iatom) == SIZE(xblocks(iatom)%p, 1))
161 cpassert(col_blk_sizes(iatom) == SIZE(xblocks(iatom)%p, 2))
162 buffer = xblocks(iatom)%p
163 END IF
164 CALL para_env%bcast(buffer)
165 CALL dbcsr_get_block_p(matrix=pao%matrix_X, row=iatom, col=iatom, block=block_x, found=found)
166 IF (ASSOCIATED(block_x)) THEN
167 block_x = buffer
168 END IF
169 DEALLOCATE (buffer)
170 END DO
171
172 ! ALLOCATABLEs deallocate themselves
173
174 END SUBROUTINE pao_read_restart
175
176! **************************************************************************************************
177!> \brief Reads a restart file into temporary datastructures
178!> \param filename ...
179!> \param param ...
180!> \param hmat ...
181!> \param kinds ...
182!> \param atom2kind ...
183!> \param positions ...
184!> \param xblocks ...
185!> \param ml_range ...
186! **************************************************************************************************
187 SUBROUTINE pao_read_raw(filename, param, hmat, kinds, atom2kind, positions, xblocks, ml_range)
188 CHARACTER(LEN=default_path_length), INTENT(IN) :: filename
189 CHARACTER(LEN=default_string_length), INTENT(OUT) :: param
190 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: hmat
191 TYPE(pao_iokind_type), ALLOCATABLE, DIMENSION(:) :: kinds
192 INTEGER, ALLOCATABLE, DIMENSION(:) :: atom2kind
193 REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: positions
194 TYPE(pao_ioblock_type), ALLOCATABLE, DIMENSION(:) :: xblocks
195 INTEGER, DIMENSION(2), INTENT(OUT), OPTIONAL :: ml_range
196
197 CHARACTER(LEN=default_string_length) :: label, str_in
198 INTEGER :: i1, i2, iatom, ikind, ipot, natoms, &
199 nkinds, nparams, unit_nr, xblocks_read
200 REAL(dp) :: r1, r2
201 REAL(dp), DIMENSION(3) :: pos_in
202 REAL(dp), DIMENSION(3, 3) :: hmat_angstrom
203
204 cpassert(.NOT. ALLOCATED(hmat))
205 cpassert(.NOT. ALLOCATED(kinds))
206 cpassert(.NOT. ALLOCATED(atom2kind))
207 cpassert(.NOT. ALLOCATED(positions))
208 cpassert(.NOT. ALLOCATED(xblocks))
209
210 natoms = -1
211 nkinds = -1
212 xblocks_read = 0
213
214 CALL open_file(file_name=filename, file_status="OLD", file_form="FORMATTED", &
215 file_action="READ", unit_number=unit_nr)
216
217 ! check if file starts with proper header !TODO: introduce a more unique header
218 READ (unit_nr, fmt=*) label, i1
219 IF (trim(label) /= "Version") THEN
220 cpabort("PAO restart file appears to be corrupted.")
221 END IF
222 IF (i1 /= file_format_version) cpabort("Restart PAO file format version is wrong")
223
224 DO WHILE (.true.)
225 READ (unit_nr, fmt=*) label
226 backspace(unit_nr)
227
228 IF (trim(label) == "Parametrization") THEN
229 READ (unit_nr, fmt=*) label, str_in
230 param = str_in
231
232 ELSE IF (trim(label) == "Cell") THEN
233 READ (unit_nr, fmt=*) label, hmat_angstrom
234 ALLOCATE (hmat(3, 3))
235 hmat(:, :) = hmat_angstrom(:, :)/angstrom
236
237 ELSE IF (trim(label) == "Nkinds") THEN
238 READ (unit_nr, fmt=*) label, nkinds
239 ALLOCATE (kinds(nkinds))
240
241 ELSE IF (trim(label) == "Kind") THEN
242 READ (unit_nr, fmt=*) label, ikind, str_in, i1
243 cpassert(ALLOCATED(kinds))
244 kinds(ikind)%name = str_in
245 kinds(ikind)%z = i1
246
247 ELSE IF (trim(label) == "PrimBasis") THEN
248 READ (unit_nr, fmt=*) label, ikind, i1, str_in
249 cpassert(ALLOCATED(kinds))
250 kinds(ikind)%prim_basis_size = i1
251 kinds(ikind)%prim_basis_name = str_in
252
253 ELSE IF (trim(label) == "PaoBasis") THEN
254 READ (unit_nr, fmt=*) label, ikind, i1
255 cpassert(ALLOCATED(kinds))
256 kinds(ikind)%pao_basis_size = i1
257
258 ELSE IF (trim(label) == "NPaoPotentials") THEN
259 READ (unit_nr, fmt=*) label, ikind, i1
260 cpassert(ALLOCATED(kinds))
261 ALLOCATE (kinds(ikind)%pao_potentials(i1))
262
263 ELSE IF (trim(label) == "PaoPotential") THEN
264 READ (unit_nr, fmt=*) label, ikind, ipot, i1, i2, r1, r2
265 cpassert(ALLOCATED(kinds(ikind)%pao_potentials))
266 kinds(ikind)%pao_potentials(ipot)%maxl = i1
267 kinds(ikind)%pao_potentials(ipot)%max_projector = i2
268 kinds(ikind)%pao_potentials(ipot)%beta = r1
269 kinds(ikind)%pao_potentials(ipot)%weight = r2
270
271 ELSE IF (trim(label) == "NParams") THEN
272 READ (unit_nr, fmt=*) label, ikind, i1
273 cpassert(ALLOCATED(kinds))
274 kinds(ikind)%nparams = i1
275
276 ELSE IF (trim(label) == "Natoms") THEN
277 READ (unit_nr, fmt=*) label, natoms
278 ALLOCATE (positions(natoms, 3), atom2kind(natoms), xblocks(natoms))
279 positions = 0.0_dp; atom2kind = -1
280 IF (PRESENT(ml_range)) ml_range = [1, natoms]
281
282 ELSE IF (trim(label) == "MLRange") THEN
283 ! Natoms entry has to come first
284 cpassert(natoms > 0)
285 ! range of atoms whose xblocks are used for machine learning
286 READ (unit_nr, fmt=*) label, i1, i2
287 IF (PRESENT(ml_range)) ml_range = [i1, i2]
288
289 ELSE IF (trim(label) == "Atom") THEN
290 READ (unit_nr, fmt=*) label, iatom, str_in, pos_in
291 cpassert(ALLOCATED(kinds))
292 DO ikind = 1, nkinds
293 IF (trim(kinds(ikind)%name) == trim(str_in)) EXIT
294 END DO
295 cpassert(ALLOCATED(atom2kind) .AND. ALLOCATED(positions))
296 atom2kind(iatom) = ikind
297 positions(iatom, :) = pos_in/angstrom
298
299 ELSE IF (trim(label) == "Xblock") THEN
300 READ (unit_nr, fmt=*) label, iatom
301 cpassert(ALLOCATED(kinds) .AND. ALLOCATED(atom2kind))
302 ikind = atom2kind(iatom)
303 nparams = kinds(ikind)%nparams
304 cpassert(nparams >= 0)
305 ALLOCATE (xblocks(iatom)%p(nparams, 1))
306 backspace(unit_nr)
307 READ (unit_nr, fmt=*) label, iatom, xblocks(iatom)%p
308 xblocks_read = xblocks_read + 1
309 cpassert(iatom == xblocks_read) ! ensure blocks are read in order
310
311 ELSE IF (trim(label) == "THE_END") THEN
312 EXIT
313 ELSE
314 !CPWARN("Skipping restart header with label: "//TRIM(label))
315 READ (unit_nr, fmt=*) label ! just read again and ignore
316 END IF
317 END DO
318 CALL close_file(unit_number=unit_nr)
319
320 cpassert(xblocks_read == natoms) ! ensure we read all blocks
321
322 END SUBROUTINE pao_read_raw
323
324! **************************************************************************************************
325!> \brief Ensure that the kind read from the restart is equal to the kind curretly in use.
326!> \param pao ...
327!> \param qs_env ...
328!> \param ikind ...
329!> \param pao_kind ...
330! **************************************************************************************************
331 SUBROUTINE pao_kinds_ensure_equal(pao, qs_env, ikind, pao_kind)
332 TYPE(pao_env_type), POINTER :: pao
333 TYPE(qs_environment_type), POINTER :: qs_env
334 INTEGER, INTENT(IN) :: ikind
335 TYPE(pao_iokind_type), INTENT(IN) :: pao_kind
336
337 CHARACTER(LEN=default_string_length) :: name
338 INTEGER :: ipot, nparams, pao_basis_size, z
339 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
340 TYPE(gto_basis_set_type), POINTER :: basis_set
341 TYPE(pao_potential_type), DIMENSION(:), POINTER :: pao_potentials
342 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
343
344 CALL get_qs_env(qs_env, &
345 atomic_kind_set=atomic_kind_set, &
346 qs_kind_set=qs_kind_set)
347
348 IF (ikind > SIZE(atomic_kind_set) .OR. ikind > SIZE(qs_kind_set)) THEN
349 cpabort("Some kinds are missing.")
350 END IF
351
352 CALL get_atomic_kind(atomic_kind_set(ikind), z=z, name=name)
353 CALL get_qs_kind(qs_kind_set(ikind), &
354 basis_set=basis_set, &
355 pao_basis_size=pao_basis_size, &
357 CALL pao_param_count(pao, qs_env, ikind=ikind, nparams=nparams)
358
359 IF (pao_kind%nparams /= nparams) THEN
360 cpabort("Number of parameters do not match")
361 END IF
362 IF (trim(pao_kind%name) /= trim(name)) THEN
363 cpabort("Kind names do not match")
364 END IF
365 IF (pao_kind%z /= z) THEN
366 cpabort("Atomic numbers do not match")
367 END IF
368 IF (trim(pao_kind%prim_basis_name) /= trim(basis_set%name)) THEN
369 cpabort("Primary Basis-set name does not match")
370 END IF
371 IF (pao_kind%prim_basis_size /= basis_set%nsgf) THEN
372 cpabort("Primary Basis-set size does not match")
373 END IF
374 IF (pao_kind%pao_basis_size /= pao_basis_size) THEN
375 cpabort("PAO basis size does not match")
376 END IF
377 IF (SIZE(pao_kind%pao_potentials) /= SIZE(pao_potentials)) THEN
378 cpabort("Number of PAO_POTENTIALS does not match")
379 END IF
380
381 DO ipot = 1, SIZE(pao_potentials)
382 IF (pao_kind%pao_potentials(ipot)%maxl /= pao_potentials(ipot)%maxl) THEN
383 cpabort("PAO_POT_MAXL does not match")
384 END IF
385 IF (pao_kind%pao_potentials(ipot)%max_projector /= pao_potentials(ipot)%max_projector) THEN
386 cpabort("PAO_POT_MAX_PROJECTOR does not match")
387 END IF
388 IF (pao_kind%pao_potentials(ipot)%beta /= pao_potentials(ipot)%beta) THEN
389 cpwarn("PAO_POT_BETA does not match")
390 END IF
391 IF (pao_kind%pao_potentials(ipot)%weight /= pao_potentials(ipot)%weight) THEN
392 cpwarn("PAO_POT_WEIGHT does not match")
393 END IF
394 END DO
395
396 END SUBROUTINE pao_kinds_ensure_equal
397
398! **************************************************************************************************
399!> \brief Writes restart file
400!> \param pao ...
401!> \param qs_env ...
402!> \param energy ...
403! **************************************************************************************************
404 SUBROUTINE pao_write_restart(pao, qs_env, energy)
405 TYPE(pao_env_type), POINTER :: pao
406 TYPE(qs_environment_type), POINTER :: qs_env
407 REAL(dp) :: energy
408
409 CHARACTER(len=*), PARAMETER :: printkey_section = 'DFT%LS_SCF%PAO%PRINT%RESTART', &
410 routinen = 'pao_write_restart'
411
412 INTEGER :: handle, unit_max, unit_nr
413 TYPE(cp_logger_type), POINTER :: logger
414 TYPE(mp_para_env_type), POINTER :: para_env
415 TYPE(section_vals_type), POINTER :: input
416
417 CALL timeset(routinen, handle)
418 logger => cp_get_default_logger()
419
420 CALL get_qs_env(qs_env, input=input, para_env=para_env)
421
422 ! open file
423 unit_nr = cp_print_key_unit_nr(logger, &
424 input, &
425 printkey_section, &
426 extension=".pao", &
427 file_action="WRITE", &
428 file_position="REWIND", &
429 file_status="UNKNOWN", &
430 do_backup=.true.)
431
432 ! although just rank-0 writes the trajectory it requires collective MPI calls
433 unit_max = unit_nr
434 CALL para_env%max(unit_max)
435 IF (unit_max > 0) THEN
436 IF (pao%iw > 0) WRITE (pao%iw, '(A,A)') " PAO| Writing restart file."
437 IF (unit_nr > 0) THEN
438 CALL write_restart_header(pao, qs_env, energy, unit_nr)
439 END IF
440
441 CALL pao_write_diagonal_blocks(para_env, pao%matrix_X, "Xblock", unit_nr)
442
443 END IF
444
445 ! close file
446 IF (unit_nr > 0) WRITE (unit_nr, '(A)') "THE_END"
447 CALL cp_print_key_finished_output(unit_nr, logger, input, printkey_section)
448
449 CALL timestop(handle)
450 END SUBROUTINE pao_write_restart
451
452! **************************************************************************************************
453!> \brief Write the digonal blocks of given DBCSR matrix into the provided unit_nr
454!> \param para_env ...
455!> \param matrix ...
456!> \param label ...
457!> \param unit_nr ...
458! **************************************************************************************************
459 SUBROUTINE pao_write_diagonal_blocks(para_env, matrix, label, unit_nr)
460 TYPE(mp_para_env_type), POINTER :: para_env
461 TYPE(dbcsr_type) :: matrix
462 CHARACTER(LEN=*), INTENT(IN) :: label
463 INTEGER, INTENT(IN) :: unit_nr
464
465 INTEGER :: iatom, natoms
466 INTEGER, DIMENSION(:), POINTER :: col_blk_sizes, row_blk_sizes
467 LOGICAL :: found
468 REAL(dp), DIMENSION(:, :), POINTER :: local_block, mpi_buffer
469
470 !TODO: this is a serial algorithm
471 CALL dbcsr_get_info(matrix, row_blk_size=row_blk_sizes, col_blk_size=col_blk_sizes)
472 cpassert(SIZE(row_blk_sizes) == SIZE(col_blk_sizes))
473 natoms = SIZE(row_blk_sizes)
474
475 DO iatom = 1, natoms
476 ALLOCATE (mpi_buffer(row_blk_sizes(iatom), col_blk_sizes(iatom)))
477 NULLIFY (local_block)
478 CALL dbcsr_get_block_p(matrix=matrix, row=iatom, col=iatom, block=local_block, found=found)
479 IF (ASSOCIATED(local_block)) THEN
480 IF (SIZE(local_block) > 0) THEN
481 ! catch corner-case
482 mpi_buffer(:, :) = local_block(:, :)
483 END IF
484 ELSE
485 mpi_buffer(:, :) = 0.0_dp
486 END IF
487
488 CALL para_env%sum(mpi_buffer)
489 IF (unit_nr > 0) THEN
490 WRITE (unit_nr, fmt="(A,1X,I10,1X)", advance='no') label, iatom
491 WRITE (unit_nr, *) mpi_buffer
492 END IF
493 DEALLOCATE (mpi_buffer)
494 END DO
495
496 ! flush
497 IF (unit_nr > 0) FLUSH (unit_nr)
498
499 END SUBROUTINE pao_write_diagonal_blocks
500
501! **************************************************************************************************
502!> \brief Writes header of restart file
503!> \param pao ...
504!> \param qs_env ...
505!> \param energy ...
506!> \param unit_nr ...
507! **************************************************************************************************
508 SUBROUTINE write_restart_header(pao, qs_env, energy, unit_nr)
509 TYPE(pao_env_type), POINTER :: pao
510 TYPE(qs_environment_type), POINTER :: qs_env
511 REAL(dp) :: energy
512 INTEGER, INTENT(IN) :: unit_nr
513
514 CHARACTER(LEN=default_string_length) :: kindname
515 INTEGER :: iatom, ikind, ipot, nparams, &
516 pao_basis_size, z
517 TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
518 TYPE(cell_type), POINTER :: cell
519 TYPE(gto_basis_set_type), POINTER :: basis_set
520 TYPE(pao_potential_type), DIMENSION(:), POINTER :: pao_potentials
521 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
522 TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
523
524 CALL get_qs_env(qs_env, &
525 cell=cell, &
526 particle_set=particle_set, &
527 atomic_kind_set=atomic_kind_set, &
528 qs_kind_set=qs_kind_set)
529
530 WRITE (unit_nr, "(A,5X,I0)") "Version", file_format_version
531 WRITE (unit_nr, "(A,5X,F20.10)") "Energy", energy
532 WRITE (unit_nr, "(A,5X,I0)") "Step", pao%istep
533 WRITE (unit_nr, "(A,5X,A)") "Parametrization", id2str(pao%parameterization)
534
535 ! write kinds
536 WRITE (unit_nr, "(A,5X,I0)") "Nkinds", SIZE(atomic_kind_set)
537 DO ikind = 1, SIZE(atomic_kind_set)
538 CALL get_atomic_kind(atomic_kind_set(ikind), name=kindname, z=z)
539 CALL get_qs_kind(qs_kind_set(ikind), &
540 pao_basis_size=pao_basis_size, &
542 basis_set=basis_set)
543 CALL pao_param_count(pao, qs_env, ikind, nparams)
544 WRITE (unit_nr, "(A,5X,I10,1X,A,1X,I3)") "Kind", ikind, trim(kindname), z
545 WRITE (unit_nr, "(A,5X,I10,1X,I3)") "NParams", ikind, nparams
546 WRITE (unit_nr, "(A,5X,I10,1X,I10,1X,A)") "PrimBasis", ikind, basis_set%nsgf, trim(basis_set%name)
547 WRITE (unit_nr, "(A,5X,I10,1X,I3)") "PaoBasis", ikind, pao_basis_size
548 WRITE (unit_nr, "(A,5X,I10,1X,I3)") "NPaoPotentials", ikind, SIZE(pao_potentials)
549 DO ipot = 1, SIZE(pao_potentials)
550 WRITE (unit_nr, "(A,5X,I10,1X,I3)", advance='no') "PaoPotential", ikind, ipot
551 WRITE (unit_nr, "(1X,I3)", advance='no') pao_potentials(ipot)%maxl
552 WRITE (unit_nr, "(1X,I3)", advance='no') pao_potentials(ipot)%max_projector
553 WRITE (unit_nr, "(1X,F20.16)", advance='no') pao_potentials(ipot)%beta
554 WRITE (unit_nr, "(1X,F20.16)") pao_potentials(ipot)%weight
555 END DO
556 END DO
557
558 ! write cell
559 WRITE (unit_nr, fmt="(A,5X)", advance='no') "Cell"
560 WRITE (unit_nr, *) cell%hmat*angstrom
561
562 ! write atoms
563 WRITE (unit_nr, "(A,5X,I0)") "Natoms", SIZE(particle_set)
564 DO iatom = 1, SIZE(particle_set)
565 kindname = particle_set(iatom)%atomic_kind%name
566 WRITE (unit_nr, fmt="(A,5X,I10,5X,A,1X)", advance='no') "Atom ", iatom, trim(kindname)
567 WRITE (unit_nr, *) particle_set(iatom)%r*angstrom
568 END DO
569
570 END SUBROUTINE write_restart_header
571
572!**************************************************************************************************
573!> \brief writing the KS matrix (in terms of the PAO basis) in csr format into a file
574!> \param qs_env qs environment
575!> \param ls_scf_env ls environment
576!> \author Mohammad Hossein Bani-Hashemian
577! **************************************************************************************************
578 SUBROUTINE pao_write_ks_matrix_csr(qs_env, ls_scf_env)
579 TYPE(qs_environment_type), POINTER :: qs_env
580 TYPE(ls_scf_env_type), TARGET :: ls_scf_env
581
582 CHARACTER(len=*), PARAMETER :: routinen = 'pao_write_ks_matrix_csr'
583
584 CHARACTER(LEN=default_path_length) :: file_name, fileformat
585 INTEGER :: handle, ispin, output_unit, unit_nr
586 LOGICAL :: bin, do_kpoints, do_ks_csr_write, uptr
587 REAL(kind=dp) :: thld
588 TYPE(cp_logger_type), POINTER :: logger
589 TYPE(dbcsr_csr_type) :: ks_mat_csr
590 TYPE(dbcsr_type) :: matrix_ks_nosym
591 TYPE(section_vals_type), POINTER :: dft_section, input
592
593 CALL timeset(routinen, handle)
594
595 NULLIFY (dft_section)
596
597 logger => cp_get_default_logger()
598 output_unit = cp_logger_get_default_io_unit(logger)
599
600 CALL get_qs_env(qs_env, input=input)
601 dft_section => section_vals_get_subs_vals(input, "DFT")
602 do_ks_csr_write = btest(cp_print_key_should_output(logger%iter_info, dft_section, &
603 "PRINT%KS_CSR_WRITE"), cp_p_file)
604
605 ! NOTE: k-points has to be treated differently later. k-points has KS matrix as double pointer.
606 CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
607
608 IF (do_ks_csr_write .AND. (.NOT. do_kpoints)) THEN
609 CALL section_vals_val_get(dft_section, "PRINT%KS_CSR_WRITE%THRESHOLD", r_val=thld)
610 CALL section_vals_val_get(dft_section, "PRINT%KS_CSR_WRITE%UPPER_TRIANGULAR", l_val=uptr)
611 CALL section_vals_val_get(dft_section, "PRINT%KS_CSR_WRITE%BINARY", l_val=bin)
612
613 IF (bin) THEN
614 fileformat = "UNFORMATTED"
615 ELSE
616 fileformat = "FORMATTED"
617 END IF
618
619 DO ispin = 1, SIZE(ls_scf_env%matrix_ks)
620
621 IF (dbcsr_has_symmetry(ls_scf_env%matrix_ks(ispin))) THEN
622 CALL dbcsr_desymmetrize(ls_scf_env%matrix_ks(ispin), matrix_ks_nosym)
623 ELSE
624 CALL dbcsr_copy(matrix_ks_nosym, ls_scf_env%matrix_ks(ispin))
625 END IF
626
627 CALL dbcsr_csr_create_from_dbcsr(matrix_ks_nosym, ks_mat_csr, dbcsr_csr_dbcsr_blkrow_dist)
628 CALL dbcsr_convert_dbcsr_to_csr(matrix_ks_nosym, ks_mat_csr)
629
630 WRITE (file_name, '(A,I0)') "PAO_KS_SPIN_", ispin
631 unit_nr = cp_print_key_unit_nr(logger, dft_section, "PRINT%KS_CSR_WRITE", &
632 extension=".csr", middle_name=trim(file_name), &
633 file_status="REPLACE", file_form=fileformat)
634 CALL dbcsr_csr_write(ks_mat_csr, unit_nr, upper_triangle=uptr, threshold=thld, binary=bin)
635
636 CALL cp_print_key_finished_output(unit_nr, logger, dft_section, "PRINT%KS_CSR_WRITE")
637
638 CALL dbcsr_csr_destroy(ks_mat_csr)
639 CALL dbcsr_release(matrix_ks_nosym)
640 END DO
641 END IF
642
643 CALL timestop(handle)
644
645 END SUBROUTINE pao_write_ks_matrix_csr
646
647!**************************************************************************************************
648!> \brief writing the overlap matrix (in terms of the PAO basis) in csr format into a file
649!> \param qs_env qs environment
650!> \param ls_scf_env ls environment
651!> \author Mohammad Hossein Bani-Hashemian
652! **************************************************************************************************
653 SUBROUTINE pao_write_s_matrix_csr(qs_env, ls_scf_env)
654 TYPE(qs_environment_type), POINTER :: qs_env
655 TYPE(ls_scf_env_type), TARGET :: ls_scf_env
656
657 CHARACTER(len=*), PARAMETER :: routinen = 'pao_write_s_matrix_csr'
658
659 CHARACTER(LEN=default_path_length) :: file_name, fileformat
660 INTEGER :: handle, output_unit, unit_nr
661 LOGICAL :: bin, do_kpoints, do_s_csr_write, uptr
662 REAL(kind=dp) :: thld
663 TYPE(cp_logger_type), POINTER :: logger
664 TYPE(dbcsr_csr_type) :: s_mat_csr
665 TYPE(dbcsr_type) :: matrix_s_nosym
666 TYPE(section_vals_type), POINTER :: dft_section, input
667
668 CALL timeset(routinen, handle)
669
670 NULLIFY (dft_section)
671
672 logger => cp_get_default_logger()
673 output_unit = cp_logger_get_default_io_unit(logger)
674
675 CALL get_qs_env(qs_env, input=input)
676 dft_section => section_vals_get_subs_vals(input, "DFT")
677 do_s_csr_write = btest(cp_print_key_should_output(logger%iter_info, dft_section, &
678 "PRINT%S_CSR_WRITE"), cp_p_file)
679
680 ! NOTE: k-points has to be treated differently later. k-points has overlap matrix as double pointer.
681 CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
682
683 IF (do_s_csr_write .AND. (.NOT. do_kpoints)) THEN
684 CALL section_vals_val_get(dft_section, "PRINT%S_CSR_WRITE%THRESHOLD", r_val=thld)
685 CALL section_vals_val_get(dft_section, "PRINT%S_CSR_WRITE%UPPER_TRIANGULAR", l_val=uptr)
686 CALL section_vals_val_get(dft_section, "PRINT%S_CSR_WRITE%BINARY", l_val=bin)
687
688 IF (bin) THEN
689 fileformat = "UNFORMATTED"
690 ELSE
691 fileformat = "FORMATTED"
692 END IF
693
694 IF (dbcsr_has_symmetry(ls_scf_env%matrix_s)) THEN
695 CALL dbcsr_desymmetrize(ls_scf_env%matrix_s, matrix_s_nosym)
696 ELSE
697 CALL dbcsr_copy(matrix_s_nosym, ls_scf_env%matrix_s)
698 END IF
699
700 CALL dbcsr_csr_create_from_dbcsr(matrix_s_nosym, s_mat_csr, dbcsr_csr_dbcsr_blkrow_dist)
701 CALL dbcsr_convert_dbcsr_to_csr(matrix_s_nosym, s_mat_csr)
702
703 WRITE (file_name, '(A,I0)') "PAO_S"
704 unit_nr = cp_print_key_unit_nr(logger, dft_section, "PRINT%S_CSR_WRITE", &
705 extension=".csr", middle_name=trim(file_name), &
706 file_status="REPLACE", file_form=fileformat)
707 CALL dbcsr_csr_write(s_mat_csr, unit_nr, upper_triangle=uptr, threshold=thld, binary=bin)
708
709 CALL cp_print_key_finished_output(unit_nr, logger, dft_section, "PRINT%S_CSR_WRITE")
710
711 CALL dbcsr_csr_destroy(s_mat_csr)
712 CALL dbcsr_release(matrix_s_nosym)
713 END IF
714
715 CALL timestop(handle)
716
717 END SUBROUTINE pao_write_s_matrix_csr
718
719!**************************************************************************************************
720!> \brief writing the core Hamiltonian matrix (NYA)
721!> \param qs_env qs environment
722!> \param ls_scf_env ls environment
723!> \author Mohammad Hossein Bani-Hashemian
724! **************************************************************************************************
725 SUBROUTINE pao_write_hcore_matrix_csr(qs_env, ls_scf_env)
726 TYPE(qs_environment_type), POINTER :: qs_env
727 TYPE(ls_scf_env_type), TARGET :: ls_scf_env
728
729 CHARACTER(len=*), PARAMETER :: routinen = 'pao_write_hcore_matrix_csr'
730
731 INTEGER :: handle, output_unit
732 LOGICAL :: do_h_csr_write, do_kpoints
733 TYPE(cp_logger_type), POINTER :: logger
734 TYPE(section_vals_type), POINTER :: dft_section, input
735
736 mark_used(ls_scf_env)
737
738 CALL timeset(routinen, handle)
739
740 NULLIFY (dft_section)
741
742 logger => cp_get_default_logger()
743 output_unit = cp_logger_get_default_io_unit(logger)
744
745 CALL get_qs_env(qs_env, input=input)
746 dft_section => section_vals_get_subs_vals(input, "DFT")
747 do_h_csr_write = btest(cp_print_key_should_output(logger%iter_info, dft_section, &
748 "PRINT%HCORE_CSR_WRITE"), cp_p_file)
749
750 ! NOTE: k-points has to be treated differently later. k-points has KS matrix as double pointer.
751 CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
752
753 IF (do_h_csr_write .AND. (.NOT. do_kpoints)) THEN
754 CALL cp_warn(__location__, "Writing the PAO Core Hamiltonian matrix in CSR format NYA")
755 END IF
756
757 CALL timestop(handle)
758
759 END SUBROUTINE pao_write_hcore_matrix_csr
760
761!**************************************************************************************************
762!> \brief writing the density matrix (NYA)
763!> \param qs_env qs environment
764!> \param ls_scf_env ls environment
765!> \author Mohammad Hossein Bani-Hashemian
766! **************************************************************************************************
767 SUBROUTINE pao_write_p_matrix_csr(qs_env, ls_scf_env)
768 TYPE(qs_environment_type), POINTER :: qs_env
769 TYPE(ls_scf_env_type), TARGET :: ls_scf_env
770
771 CHARACTER(len=*), PARAMETER :: routinen = 'pao_write_p_matrix_csr'
772
773 INTEGER :: handle, output_unit
774 LOGICAL :: do_kpoints, do_p_csr_write
775 TYPE(cp_logger_type), POINTER :: logger
776 TYPE(section_vals_type), POINTER :: dft_section, input
777
778 mark_used(ls_scf_env)
779
780 CALL timeset(routinen, handle)
781
782 NULLIFY (dft_section)
783
784 logger => cp_get_default_logger()
785 output_unit = cp_logger_get_default_io_unit(logger)
786
787 CALL get_qs_env(qs_env, input=input)
788 dft_section => section_vals_get_subs_vals(input, "DFT")
789 do_p_csr_write = btest(cp_print_key_should_output(logger%iter_info, dft_section, &
790 "PRINT%P_CSR_WRITE"), cp_p_file)
791
792 ! NOTE: k-points has to be treated differently later. k-points has KS matrix as double pointer.
793 CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
794
795 IF (do_p_csr_write .AND. (.NOT. do_kpoints)) THEN
796 CALL cp_warn(__location__, "Writing the PAO density matrix in CSR format NYA")
797 END IF
798
799 CALL timestop(handle)
800
801 END SUBROUTINE pao_write_p_matrix_csr
802
803END MODULE pao_io
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
Handles all functions related to the CELL.
Definition cell_types.F:15
logical function, public dbcsr_has_symmetry(matrix)
...
subroutine, public dbcsr_convert_dbcsr_to_csr(dbcsr_mat, csr_mat)
...
subroutine, public dbcsr_desymmetrize(matrix_a, matrix_b)
...
subroutine, public dbcsr_copy(matrix_b, matrix_a, name, keep_sparsity, keep_imaginary)
...
subroutine, public dbcsr_get_block_p(matrix, row, col, block, found, row_size, col_size)
...
subroutine, public dbcsr_get_info(matrix, nblkrows_total, nblkcols_total, nfullrows_total, nfullcols_total, nblkrows_local, nblkcols_local, nfullrows_local, nfullcols_local, my_prow, my_pcol, local_rows, local_cols, proc_row_dist, proc_col_dist, row_blk_size, col_blk_size, row_blk_offset, col_blk_offset, distribution, name, matrix_type, group)
...
subroutine, public dbcsr_csr_create_from_dbcsr(dbcsr_mat, csr_mat, dist_format, csr_sparsity, numnodes)
...
subroutine, public dbcsr_release(matrix)
...
Utility routines to open and close files. Tracking of preconnections.
Definition cp_files.F:16
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.
Definition cp_files.F:311
subroutine, public close_file(unit_number, file_status, keep_preconnection)
Close an open file given by its logical unit number. Optionally, keep the file and unit preconnected.
Definition cp_files.F:122
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
integer, parameter, public cp_p_file
integer function, public cp_print_key_should_output(iteration_info, basis_section, print_key_path, used_print_key, first_time)
returns what should be done with the given property if btest(res,cp_p_store) then the property should...
Types needed for a linear scaling quickstep SCF run based on the density matrix.
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
integer, parameter, public default_path_length
Definition kinds.F:58
Interface to the message passing library MPI.
character(len=20) function, public id2str(id)
Helper routine.
Definition pao_input.F:219
Routines for reading and writing restart files.
Definition pao_io.F:12
subroutine, public pao_write_p_matrix_csr(qs_env, ls_scf_env)
writing the density matrix (NYA)
Definition pao_io.F:768
subroutine, public pao_write_hcore_matrix_csr(qs_env, ls_scf_env)
writing the core Hamiltonian matrix (NYA)
Definition pao_io.F:726
subroutine, public pao_kinds_ensure_equal(pao, qs_env, ikind, pao_kind)
Ensure that the kind read from the restart is equal to the kind curretly in use.
Definition pao_io.F:332
subroutine, public pao_write_restart(pao, qs_env, energy)
Writes restart file.
Definition pao_io.F:405
subroutine, public pao_write_ks_matrix_csr(qs_env, ls_scf_env)
writing the KS matrix (in terms of the PAO basis) in csr format into a file
Definition pao_io.F:579
subroutine, public pao_read_raw(filename, param, hmat, kinds, atom2kind, positions, xblocks, ml_range)
Reads a restart file into temporary datastructures.
Definition pao_io.F:188
subroutine, public pao_write_s_matrix_csr(qs_env, ls_scf_env)
writing the overlap matrix (in terms of the PAO basis) in csr format into a file
Definition pao_io.F:654
subroutine, public pao_read_restart(pao, qs_env)
Reads restart file.
Definition pao_io.F:88
Front-End for any PAO parametrization.
Definition pao_param.F:12
subroutine, public pao_param_count(pao, qs_env, ikind, nparams)
Returns the number of parameters for given atomic kind.
Definition pao_param.F:173
Factory routines for potentials used e.g. by pao_param_exp and pao_ml.
Types used by the PAO machinery.
Definition pao_types.F:12
Define the data structure for the particle information.
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public angstrom
Definition physcon.F:144
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
Provides all information about an atomic kind.
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
type of a logger, at the moment it contains just a print level starting at which level it should be l...
stores all the informations relevant to an mpi environment
Holds information about a PAO potential.
Provides all information about a quickstep kind.