LCOV - code coverage report
Current view: top level - src - pao_io.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 81.9 % 320 262
Test Date: 2026-11-24 01:22:08 Functions: 71.4 % 14 10

            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 Routines for reading and writing restart files.
      10              : !> \author Ole Schuett
      11              : ! **************************************************************************************************
      12              : MODULE pao_io
      13              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      14              :                                               get_atomic_kind
      15              :    USE basis_set_types,                 ONLY: gto_basis_set_type
      16              :    USE cell_types,                      ONLY: cell_type
      17              :    USE cp_dbcsr_api,                    ONLY: dbcsr_copy,&
      18              :                                               dbcsr_desymmetrize,&
      19              :                                               dbcsr_get_block_p,&
      20              :                                               dbcsr_get_info,&
      21              :                                               dbcsr_has_symmetry,&
      22              :                                               dbcsr_release,&
      23              :                                               dbcsr_type
      24              :    USE cp_dbcsr_csr,                    ONLY: dbcsr_convert_dbcsr_to_csr,&
      25              :                                               dbcsr_csr_create_from_dbcsr,&
      26              :                                               dbcsr_csr_dbcsr_blkrow_dist,&
      27              :                                               dbcsr_csr_destroy,&
      28              :                                               dbcsr_csr_type,&
      29              :                                               dbcsr_csr_write
      30              :    USE cp_files,                        ONLY: close_file,&
      31              :                                               open_file
      32              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      33              :                                               cp_logger_get_default_io_unit,&
      34              :                                               cp_logger_type
      35              :    USE cp_output_handling,              ONLY: cp_p_file,&
      36              :                                               cp_print_key_finished_output,&
      37              :                                               cp_print_key_should_output,&
      38              :                                               cp_print_key_unit_nr
      39              :    USE dm_ls_scf_types,                 ONLY: ls_scf_env_type
      40              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      41              :                                               section_vals_type,&
      42              :                                               section_vals_val_get
      43              :    USE kinds,                           ONLY: default_path_length,&
      44              :                                               default_string_length,&
      45              :                                               dp
      46              :    USE message_passing,                 ONLY: mp_para_env_type
      47              :    USE pao_input,                       ONLY: id2str
      48              :    USE pao_param,                       ONLY: pao_param_count
      49              :    USE pao_types,                       ONLY: pao_env_type
      50              :    USE particle_types,                  ONLY: particle_type
      51              :    USE physcon,                         ONLY: angstrom
      52              :    USE qs_environment_types,            ONLY: get_qs_env,&
      53              :                                               qs_environment_type
      54              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      55              :                                               pao_potential_type,&
      56              :                                               qs_kind_type
      57              : #include "./base/base_uses.f90"
      58              : 
      59              :    IMPLICIT NONE
      60              : 
      61              :    PRIVATE
      62              : 
      63              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'pao_io'
      64              : 
      65              :    PUBLIC :: pao_read_restart, pao_write_restart
      66              :    PUBLIC :: pao_read_raw, pao_kinds_ensure_equal
      67              :    PUBLIC :: pao_ioblock_type, pao_iokind_type
      68              :    PUBLIC :: pao_write_ks_matrix_csr, pao_write_s_matrix_csr
      69              :    PUBLIC :: pao_write_hcore_matrix_csr, pao_write_p_matrix_csr
      70              : 
      71              :    ! data types used by pao_read_raw()
      72              :    TYPE pao_ioblock_type
      73              :       REAL(dp), DIMENSION(:, :), ALLOCATABLE    :: p
      74              :    END TYPE pao_ioblock_type
      75              : 
      76              :    TYPE pao_iokind_type
      77              :       CHARACTER(LEN=default_string_length)     :: name = ""
      78              :       INTEGER                                  :: z = -1
      79              :       CHARACTER(LEN=default_string_length)     :: prim_basis_name = ""
      80              :       INTEGER                                  :: prim_basis_size = -1
      81              :       INTEGER                                  :: pao_basis_size = -1
      82              :       INTEGER                                  :: nparams = -1
      83              :       TYPE(pao_potential_type), ALLOCATABLE, DIMENSION(:) :: pao_potentials
      84              :    END TYPE pao_iokind_type
      85              : 
      86              :    INTEGER, PARAMETER, PRIVATE :: file_format_version = 4
      87              : 
      88              : CONTAINS
      89              : 
      90              : ! **************************************************************************************************
      91              : !> \brief Reads restart file
      92              : !> \param pao ...
      93              : !> \param qs_env ...
      94              : ! **************************************************************************************************
      95            2 :    SUBROUTINE pao_read_restart(pao, qs_env)
      96              :       TYPE(pao_env_type), POINTER                        :: pao
      97              :       TYPE(qs_environment_type), POINTER                 :: qs_env
      98              : 
      99              :       REAL(KIND=dp), PARAMETER                           :: eps_cell = 1.0E-10_dp, &
     100              :                                                             eps_pos = 1.0E-10_dp
     101              : 
     102              :       CHARACTER(LEN=default_string_length)               :: param
     103              :       INTEGER                                            :: iatom, ikind, natoms
     104            2 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom2kind
     105            2 :       INTEGER, DIMENSION(:), POINTER                     :: col_blk_sizes, row_blk_sizes
     106              :       LOGICAL                                            :: found
     107              :       REAL(dp)                                           :: diff
     108            2 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: hmat, positions
     109            2 :       REAL(dp), DIMENSION(:, :), POINTER                 :: block_X, buffer
     110              :       TYPE(cell_type), POINTER                           :: cell
     111              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     112            2 :       TYPE(pao_ioblock_type), ALLOCATABLE, DIMENSION(:)  :: xblocks
     113            2 :       TYPE(pao_iokind_type), ALLOCATABLE, DIMENSION(:)   :: kinds
     114            2 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     115              : 
     116            0 :       CPASSERT(LEN_TRIM(pao%restart_file) > 0)
     117            2 :       IF (pao%iw > 0) WRITE (pao%iw, '(A,A)') " PAO| Reading matrix_X from restart file: ", TRIM(pao%restart_file)
     118              : 
     119              :       CALL get_qs_env(qs_env, &
     120              :                       para_env=para_env, &
     121              :                       natom=natoms, &
     122              :                       cell=cell, &
     123            2 :                       particle_set=particle_set)
     124              : 
     125              :       ! read and check restart file on first rank only
     126            2 :       IF (para_env%is_source()) THEN
     127            1 :          CALL pao_read_raw(pao%restart_file, param, hmat, kinds, atom2kind, positions, xblocks)
     128              : 
     129              :          ! check cell
     130           13 :          IF (MAXVAL(ABS(hmat - cell%hmat)) > eps_cell) THEN
     131            0 :             CPWARN("Restarting from different cell")
     132              :          END IF
     133              : 
     134              :          ! check parametrization
     135            1 :          IF (TRIM(param) /= TRIM(ADJUSTL(id2str(pao%parameterization)))) THEN
     136            1 :             CPABORT("Restart PAO parametrization does not match")
     137              :          END IF
     138              : 
     139              :          ! check kinds
     140            2 :          DO ikind = 1, SIZE(kinds)
     141            2 :             CALL pao_kinds_ensure_equal(pao, qs_env, ikind, kinds(ikind))
     142              :          END DO
     143              : 
     144              :          ! check number of atoms
     145            1 :          IF (SIZE(positions, 1) /= natoms) THEN
     146            0 :             CPABORT("Number of atoms do not match")
     147              :          END IF
     148              : 
     149              :          ! check atom2kind
     150            3 :          DO iatom = 1, natoms
     151            3 :             IF (atom2kind(iatom) /= particle_set(iatom)%atomic_kind%kind_number) THEN
     152            0 :                CPABORT("Restart atomic kinds do not match.")
     153              :             END IF
     154              :          END DO
     155              : 
     156              :          ! check positions, warning only
     157            1 :          diff = 0.0_dp
     158            3 :          DO iatom = 1, natoms
     159            9 :             diff = MAX(diff, MAXVAL(ABS(positions(iatom, :) - particle_set(iatom)%r)))
     160              :          END DO
     161            1 :          CPWARN_IF(diff > eps_pos, "Restarting from different atom positions")
     162              : 
     163              :       END IF
     164              : 
     165              :       ! scatter xblocks across ranks to fill pao%matrix_X
     166              :       ! this could probably be done more efficiently
     167            2 :       CALL dbcsr_get_info(pao%matrix_X, row_blk_size=row_blk_sizes, col_blk_size=col_blk_sizes)
     168            6 :       DO iatom = 1, natoms
     169           16 :          ALLOCATE (buffer(row_blk_sizes(iatom), col_blk_sizes(iatom)))
     170            4 :          IF (para_env%is_source()) THEN
     171            2 :             CPASSERT(row_blk_sizes(iatom) == SIZE(xblocks(iatom)%p, 1))
     172            2 :             CPASSERT(col_blk_sizes(iatom) == SIZE(xblocks(iatom)%p, 2))
     173           18 :             buffer = xblocks(iatom)%p
     174              :          END IF
     175           68 :          CALL para_env%bcast(buffer)
     176            4 :          CALL dbcsr_get_block_p(matrix=pao%matrix_X, row=iatom, col=iatom, block=block_X, found=found)
     177            4 :          IF (ASSOCIATED(block_X)) THEN
     178           34 :             block_X = buffer
     179              :          END IF
     180           10 :          DEALLOCATE (buffer)
     181              :       END DO
     182              : 
     183              :       ! ALLOCATABLEs deallocate themselves
     184              : 
     185            7 :    END SUBROUTINE pao_read_restart
     186              : 
     187              : ! **************************************************************************************************
     188              : !> \brief Reads a restart file into temporary datastructures
     189              : !> \param filename ...
     190              : !> \param param ...
     191              : !> \param hmat ...
     192              : !> \param kinds ...
     193              : !> \param atom2kind ...
     194              : !> \param positions ...
     195              : !> \param xblocks ...
     196              : !> \param ml_range ...
     197              : ! **************************************************************************************************
     198           36 :    SUBROUTINE pao_read_raw(filename, param, hmat, kinds, atom2kind, positions, xblocks, ml_range)
     199              :       CHARACTER(LEN=default_path_length), INTENT(IN)     :: filename
     200              :       CHARACTER(LEN=default_string_length), INTENT(OUT)  :: param
     201              :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: hmat
     202              :       TYPE(pao_iokind_type), ALLOCATABLE, DIMENSION(:)   :: kinds
     203              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom2kind
     204              :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: positions
     205              :       TYPE(pao_ioblock_type), ALLOCATABLE, DIMENSION(:)  :: xblocks
     206              :       INTEGER, DIMENSION(2), INTENT(OUT), OPTIONAL       :: ml_range
     207              : 
     208              :       CHARACTER(LEN=default_string_length)               :: label, str_in
     209              :       INTEGER                                            :: i1, i2, iatom, ikind, ipot, natoms, &
     210              :                                                             nkinds, nparams, unit_nr, xblocks_read
     211              :       REAL(dp)                                           :: r1, r2
     212              :       REAL(dp), DIMENSION(3)                             :: pos_in
     213              :       REAL(dp), DIMENSION(3, 3)                          :: hmat_angstrom
     214              : 
     215           18 :       CPASSERT(.NOT. ALLOCATED(hmat))
     216           18 :       CPASSERT(.NOT. ALLOCATED(kinds))
     217           18 :       CPASSERT(.NOT. ALLOCATED(atom2kind))
     218           18 :       CPASSERT(.NOT. ALLOCATED(positions))
     219           18 :       CPASSERT(.NOT. ALLOCATED(xblocks))
     220              : 
     221           18 :       natoms = -1
     222           18 :       nkinds = -1
     223           18 :       xblocks_read = 0
     224              : 
     225              :       CALL open_file(file_name=filename, file_status="OLD", file_form="FORMATTED", &
     226           18 :                      file_action="READ", unit_number=unit_nr)
     227              : 
     228              :       ! check if file starts with proper header !TODO: introduce a more unique header
     229           18 :       READ (unit_nr, fmt=*) label, i1
     230           18 :       IF (TRIM(label) /= "Version") THEN
     231            0 :          CPABORT("PAO restart file appears to be corrupted.")
     232              :       END IF
     233           18 :       IF (i1 /= file_format_version) CPABORT("Restart PAO file format version is wrong")
     234              : 
     235              :       DO WHILE (.TRUE.)
     236          306 :          READ (unit_nr, fmt=*) label
     237          306 :          BACKSPACE (unit_nr)
     238              : 
     239          324 :          IF (TRIM(label) == "Parametrization") THEN
     240           18 :             READ (unit_nr, fmt=*) label, str_in
     241           18 :             param = str_in
     242              : 
     243          288 :          ELSE IF (TRIM(label) == "Cell") THEN
     244           18 :             READ (unit_nr, fmt=*) label, hmat_angstrom
     245           18 :             ALLOCATE (hmat(3, 3))
     246          234 :             hmat(:, :) = hmat_angstrom(:, :)/angstrom
     247              : 
     248          270 :          ELSE IF (TRIM(label) == "Nkinds") THEN
     249           18 :             READ (unit_nr, fmt=*) label, nkinds
     250           72 :             ALLOCATE (kinds(nkinds))
     251              : 
     252          252 :          ELSE IF (TRIM(label) == "Kind") THEN
     253           18 :             READ (unit_nr, fmt=*) label, ikind, str_in, i1
     254           18 :             CPASSERT(ALLOCATED(kinds))
     255           18 :             kinds(ikind)%name = str_in
     256           18 :             kinds(ikind)%z = i1
     257              : 
     258          234 :          ELSE IF (TRIM(label) == "PrimBasis") THEN
     259           18 :             READ (unit_nr, fmt=*) label, ikind, i1, str_in
     260           18 :             CPASSERT(ALLOCATED(kinds))
     261           18 :             kinds(ikind)%prim_basis_size = i1
     262           18 :             kinds(ikind)%prim_basis_name = str_in
     263              : 
     264          216 :          ELSE IF (TRIM(label) == "PaoBasis") THEN
     265           18 :             READ (unit_nr, fmt=*) label, ikind, i1
     266           18 :             CPASSERT(ALLOCATED(kinds))
     267           18 :             kinds(ikind)%pao_basis_size = i1
     268              : 
     269          198 :          ELSE IF (TRIM(label) == "NPaoPotentials") THEN
     270           18 :             READ (unit_nr, fmt=*) label, ikind, i1
     271           18 :             CPASSERT(ALLOCATED(kinds))
     272           72 :             ALLOCATE (kinds(ikind)%pao_potentials(i1))
     273              : 
     274          180 :          ELSE IF (TRIM(label) == "PaoPotential") THEN
     275           18 :             READ (unit_nr, fmt=*) label, ikind, ipot, i1, i2, r1, r2
     276           18 :             CPASSERT(ALLOCATED(kinds(ikind)%pao_potentials))
     277           18 :             kinds(ikind)%pao_potentials(ipot)%maxl = i1
     278           18 :             kinds(ikind)%pao_potentials(ipot)%max_projector = i2
     279           18 :             kinds(ikind)%pao_potentials(ipot)%beta = r1
     280           18 :             kinds(ikind)%pao_potentials(ipot)%weight = r2
     281              : 
     282          162 :          ELSE IF (TRIM(label) == "NParams") THEN
     283           18 :             READ (unit_nr, fmt=*) label, ikind, i1
     284           18 :             CPASSERT(ALLOCATED(kinds))
     285           18 :             kinds(ikind)%nparams = i1
     286              : 
     287          144 :          ELSE IF (TRIM(label) == "Natoms") THEN
     288           18 :             READ (unit_nr, fmt=*) label, natoms
     289          162 :             ALLOCATE (positions(natoms, 3), atom2kind(natoms), xblocks(natoms))
     290           54 :             positions = 0.0_dp; atom2kind = -1
     291           52 :             IF (PRESENT(ml_range)) ml_range = [1, natoms]
     292              : 
     293          126 :          ELSE IF (TRIM(label) == "MLRange") THEN
     294              :             ! Natoms entry has to come first
     295            0 :             CPASSERT(natoms > 0)
     296              :             ! range of atoms whose xblocks are used for machine learning
     297            0 :             READ (unit_nr, fmt=*) label, i1, i2
     298            0 :             IF (PRESENT(ml_range)) ml_range = [i1, i2]
     299              : 
     300          126 :          ELSE IF (TRIM(label) == "Atom") THEN
     301           36 :             READ (unit_nr, fmt=*) label, iatom, str_in, pos_in
     302           36 :             CPASSERT(ALLOCATED(kinds))
     303           36 :             DO ikind = 1, nkinds
     304           36 :                IF (TRIM(kinds(ikind)%name) == TRIM(str_in)) EXIT
     305              :             END DO
     306           36 :             CPASSERT(ALLOCATED(atom2kind) .AND. ALLOCATED(positions))
     307           36 :             atom2kind(iatom) = ikind
     308          144 :             positions(iatom, :) = pos_in/angstrom
     309              : 
     310           90 :          ELSE IF (TRIM(label) == "Xblock") THEN
     311           36 :             READ (unit_nr, fmt=*) label, iatom
     312           36 :             CPASSERT(ALLOCATED(kinds) .AND. ALLOCATED(atom2kind))
     313           36 :             ikind = atom2kind(iatom)
     314           36 :             nparams = kinds(ikind)%nparams
     315           36 :             CPASSERT(nparams >= 0)
     316          108 :             ALLOCATE (xblocks(iatom)%p(nparams, 1))
     317           36 :             BACKSPACE (unit_nr)
     318          324 :             READ (unit_nr, fmt=*) label, iatom, xblocks(iatom)%p
     319           36 :             xblocks_read = xblocks_read + 1
     320           36 :             CPASSERT(iatom == xblocks_read) ! ensure blocks are read in order
     321              : 
     322           54 :          ELSE IF (TRIM(label) == "THE_END") THEN
     323              :             EXIT
     324              :          ELSE
     325              :             !CPWARN("Skipping restart header with label: "//TRIM(label))
     326           36 :             READ (unit_nr, fmt=*) label ! just read again and ignore
     327              :          END IF
     328              :       END DO
     329           18 :       CALL close_file(unit_number=unit_nr)
     330              : 
     331           18 :       CPASSERT(xblocks_read == natoms) ! ensure we read all blocks
     332              : 
     333           18 :    END SUBROUTINE pao_read_raw
     334              : 
     335              : ! **************************************************************************************************
     336              : !> \brief Ensure that the kind read from the restart is equal to the kind curretly in use.
     337              : !> \param pao ...
     338              : !> \param qs_env ...
     339              : !> \param ikind ...
     340              : !> \param pao_kind ...
     341              : ! **************************************************************************************************
     342           72 :    SUBROUTINE pao_kinds_ensure_equal(pao, qs_env, ikind, pao_kind)
     343              :       TYPE(pao_env_type), POINTER                        :: pao
     344              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     345              :       INTEGER, INTENT(IN)                                :: ikind
     346              :       TYPE(pao_iokind_type), INTENT(IN)                  :: pao_kind
     347              : 
     348              :       CHARACTER(LEN=default_string_length)               :: name
     349              :       INTEGER                                            :: ipot, nparams, pao_basis_size, z
     350           18 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     351              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set
     352           18 :       TYPE(pao_potential_type), DIMENSION(:), POINTER    :: pao_potentials
     353           18 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     354              : 
     355              :       CALL get_qs_env(qs_env, &
     356              :                       atomic_kind_set=atomic_kind_set, &
     357           18 :                       qs_kind_set=qs_kind_set)
     358              : 
     359           18 :       IF (ikind > SIZE(atomic_kind_set) .OR. ikind > SIZE(qs_kind_set)) THEN
     360            0 :          CPABORT("Some kinds are missing.")
     361              :       END IF
     362              : 
     363           18 :       CALL get_atomic_kind(atomic_kind_set(ikind), z=z, name=name)
     364              :       CALL get_qs_kind(qs_kind_set(ikind), &
     365              :                        basis_set=basis_set, &
     366              :                        pao_basis_size=pao_basis_size, &
     367           18 :                        pao_potentials=pao_potentials)
     368           18 :       CALL pao_param_count(pao, qs_env, ikind=ikind, nparams=nparams)
     369              : 
     370           18 :       IF (pao_kind%nparams /= nparams) THEN
     371            0 :          CPABORT("Number of parameters do not match")
     372              :       END IF
     373           18 :       IF (TRIM(pao_kind%name) /= TRIM(name)) THEN
     374            0 :          CPABORT("Kind names do not match")
     375              :       END IF
     376           18 :       IF (pao_kind%z /= z) THEN
     377            0 :          CPABORT("Atomic numbers do not match")
     378              :       END IF
     379           18 :       IF (TRIM(pao_kind%prim_basis_name) /= TRIM(basis_set%name)) THEN
     380            0 :          CPABORT("Primary Basis-set name does not match")
     381              :       END IF
     382           18 :       IF (pao_kind%prim_basis_size /= basis_set%nsgf) THEN
     383            0 :          CPABORT("Primary Basis-set size does not match")
     384              :       END IF
     385           18 :       IF (pao_kind%pao_basis_size /= pao_basis_size) THEN
     386            0 :          CPABORT("PAO basis size does not match")
     387              :       END IF
     388           18 :       IF (SIZE(pao_kind%pao_potentials) /= SIZE(pao_potentials)) THEN
     389            0 :          CPABORT("Number of PAO_POTENTIALS does not match")
     390              :       END IF
     391              : 
     392           36 :       DO ipot = 1, SIZE(pao_potentials)
     393           18 :          IF (pao_kind%pao_potentials(ipot)%maxl /= pao_potentials(ipot)%maxl) THEN
     394            0 :             CPABORT("PAO_POT_MAXL does not match")
     395              :          END IF
     396           18 :          IF (pao_kind%pao_potentials(ipot)%max_projector /= pao_potentials(ipot)%max_projector) THEN
     397            0 :             CPABORT("PAO_POT_MAX_PROJECTOR does not match")
     398              :          END IF
     399           18 :          IF (pao_kind%pao_potentials(ipot)%beta /= pao_potentials(ipot)%beta) THEN
     400            0 :             CPWARN("PAO_POT_BETA does not match")
     401              :          END IF
     402           36 :          IF (pao_kind%pao_potentials(ipot)%weight /= pao_potentials(ipot)%weight) THEN
     403            0 :             CPWARN("PAO_POT_WEIGHT does not match")
     404              :          END IF
     405              :       END DO
     406              : 
     407           18 :    END SUBROUTINE pao_kinds_ensure_equal
     408              : 
     409              : ! **************************************************************************************************
     410              : !> \brief Writes restart file
     411              : !> \param pao ...
     412              : !> \param qs_env ...
     413              : !> \param energy ...
     414              : ! **************************************************************************************************
     415          158 :    SUBROUTINE pao_write_restart(pao, qs_env, energy)
     416              :       TYPE(pao_env_type), POINTER                        :: pao
     417              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     418              :       REAL(dp)                                           :: energy
     419              : 
     420              :       CHARACTER(len=*), PARAMETER :: printkey_section = 'DFT%LS_SCF%PAO%PRINT%RESTART', &
     421              :          routineN = 'pao_write_restart'
     422              : 
     423              :       INTEGER                                            :: handle, unit_max, unit_nr
     424              :       TYPE(cp_logger_type), POINTER                      :: logger
     425              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     426              :       TYPE(section_vals_type), POINTER                   :: input
     427              : 
     428          158 :       CALL timeset(routineN, handle)
     429          158 :       logger => cp_get_default_logger()
     430              : 
     431          158 :       CALL get_qs_env(qs_env, input=input, para_env=para_env)
     432              : 
     433              :       ! open file
     434              :       unit_nr = cp_print_key_unit_nr(logger, &
     435              :                                      input, &
     436              :                                      printkey_section, &
     437              :                                      extension=".pao", &
     438              :                                      file_action="WRITE", &
     439              :                                      file_position="REWIND", &
     440              :                                      file_status="UNKNOWN", &
     441          158 :                                      do_backup=.TRUE.)
     442              : 
     443              :       ! although just rank-0 writes the trajectory it requires collective MPI calls
     444          158 :       unit_max = unit_nr
     445          158 :       CALL para_env%max(unit_max)
     446          158 :       IF (unit_max > 0) THEN
     447           64 :          IF (pao%iw > 0) WRITE (pao%iw, '(A,A)') " PAO| Writing restart file."
     448           64 :          IF (unit_nr > 0) THEN
     449           32 :             CALL write_restart_header(pao, qs_env, energy, unit_nr)
     450              :          END IF
     451              : 
     452           64 :          CALL pao_write_diagonal_blocks(para_env, pao%matrix_X, "Xblock", unit_nr)
     453              : 
     454              :       END IF
     455              : 
     456              :       ! close file
     457          158 :       IF (unit_nr > 0) WRITE (unit_nr, '(A)') "THE_END"
     458          158 :       CALL cp_print_key_finished_output(unit_nr, logger, input, printkey_section)
     459              : 
     460          158 :       CALL timestop(handle)
     461          158 :    END SUBROUTINE pao_write_restart
     462              : 
     463              : ! **************************************************************************************************
     464              : !> \brief Write the digonal blocks of given DBCSR matrix into the provided unit_nr
     465              : !> \param para_env ...
     466              : !> \param matrix ...
     467              : !> \param label ...
     468              : !> \param unit_nr ...
     469              : ! **************************************************************************************************
     470           64 :    SUBROUTINE pao_write_diagonal_blocks(para_env, matrix, label, unit_nr)
     471              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     472              :       TYPE(dbcsr_type)                                   :: matrix
     473              :       CHARACTER(LEN=*), INTENT(IN)                       :: label
     474              :       INTEGER, INTENT(IN)                                :: unit_nr
     475              : 
     476              :       INTEGER                                            :: iatom, natoms
     477           64 :       INTEGER, DIMENSION(:), POINTER                     :: col_blk_sizes, row_blk_sizes
     478              :       LOGICAL                                            :: found
     479           64 :       REAL(dp), DIMENSION(:, :), POINTER                 :: local_block, mpi_buffer
     480              : 
     481              :       !TODO: this is a serial algorithm
     482           64 :       CALL dbcsr_get_info(matrix, row_blk_size=row_blk_sizes, col_blk_size=col_blk_sizes)
     483           64 :       CPASSERT(SIZE(row_blk_sizes) == SIZE(col_blk_sizes))
     484           64 :       natoms = SIZE(row_blk_sizes)
     485              : 
     486          192 :       DO iatom = 1, natoms
     487          512 :          ALLOCATE (mpi_buffer(row_blk_sizes(iatom), col_blk_sizes(iatom)))
     488          128 :          NULLIFY (local_block)
     489          128 :          CALL dbcsr_get_block_p(matrix=matrix, row=iatom, col=iatom, block=local_block, found=found)
     490          128 :          IF (ASSOCIATED(local_block)) THEN
     491          192 :             IF (SIZE(local_block) > 0) THEN
     492              :                ! catch corner-case
     493         1188 :                mpi_buffer(:, :) = local_block(:, :)
     494              :             END IF
     495              :          ELSE
     496          594 :             mpi_buffer(:, :) = 0.0_dp
     497              :          END IF
     498              : 
     499         2248 :          CALL para_env%sum(mpi_buffer)
     500          128 :          IF (unit_nr > 0) THEN
     501           64 :             WRITE (unit_nr, fmt="(A,1X,I10,1X)", advance='no') label, iatom
     502          594 :             WRITE (unit_nr, *) mpi_buffer
     503              :          END IF
     504          320 :          DEALLOCATE (mpi_buffer)
     505              :       END DO
     506              : 
     507              :       ! flush
     508           64 :       IF (unit_nr > 0) FLUSH (unit_nr)
     509              : 
     510           64 :    END SUBROUTINE pao_write_diagonal_blocks
     511              : 
     512              : ! **************************************************************************************************
     513              : !> \brief Writes header of restart file
     514              : !> \param pao ...
     515              : !> \param qs_env ...
     516              : !> \param energy ...
     517              : !> \param unit_nr ...
     518              : ! **************************************************************************************************
     519           32 :    SUBROUTINE write_restart_header(pao, qs_env, energy, unit_nr)
     520              :       TYPE(pao_env_type), POINTER                        :: pao
     521              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     522              :       REAL(dp)                                           :: energy
     523              :       INTEGER, INTENT(IN)                                :: unit_nr
     524              : 
     525              :       CHARACTER(LEN=default_string_length)               :: kindname
     526              :       INTEGER                                            :: iatom, ikind, ipot, nparams, &
     527              :                                                             pao_basis_size, z
     528           32 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     529              :       TYPE(cell_type), POINTER                           :: cell
     530              :       TYPE(gto_basis_set_type), POINTER                  :: basis_set
     531           32 :       TYPE(pao_potential_type), DIMENSION(:), POINTER    :: pao_potentials
     532           32 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     533           32 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     534              : 
     535              :       CALL get_qs_env(qs_env, &
     536              :                       cell=cell, &
     537              :                       particle_set=particle_set, &
     538              :                       atomic_kind_set=atomic_kind_set, &
     539           32 :                       qs_kind_set=qs_kind_set)
     540              : 
     541           32 :       WRITE (unit_nr, "(A,5X,I0)") "Version", file_format_version
     542           32 :       WRITE (unit_nr, "(A,5X,F20.10)") "Energy", energy
     543           32 :       WRITE (unit_nr, "(A,5X,I0)") "Step", pao%istep
     544           32 :       WRITE (unit_nr, "(A,5X,A)") "Parametrization", id2str(pao%parameterization)
     545              : 
     546              :       ! write kinds
     547           32 :       WRITE (unit_nr, "(A,5X,I0)") "Nkinds", SIZE(atomic_kind_set)
     548           64 :       DO ikind = 1, SIZE(atomic_kind_set)
     549           32 :          CALL get_atomic_kind(atomic_kind_set(ikind), name=kindname, z=z)
     550              :          CALL get_qs_kind(qs_kind_set(ikind), &
     551              :                           pao_basis_size=pao_basis_size, &
     552              :                           pao_potentials=pao_potentials, &
     553           32 :                           basis_set=basis_set)
     554           32 :          CALL pao_param_count(pao, qs_env, ikind, nparams)
     555           32 :          WRITE (unit_nr, "(A,5X,I10,1X,A,1X,I3)") "Kind", ikind, TRIM(kindname), z
     556           32 :          WRITE (unit_nr, "(A,5X,I10,1X,I3)") "NParams", ikind, nparams
     557           32 :          WRITE (unit_nr, "(A,5X,I10,1X,I10,1X,A)") "PrimBasis", ikind, basis_set%nsgf, TRIM(basis_set%name)
     558           32 :          WRITE (unit_nr, "(A,5X,I10,1X,I3)") "PaoBasis", ikind, pao_basis_size
     559           32 :          WRITE (unit_nr, "(A,5X,I10,1X,I3)") "NPaoPotentials", ikind, SIZE(pao_potentials)
     560          129 :          DO ipot = 1, SIZE(pao_potentials)
     561           33 :             WRITE (unit_nr, "(A,5X,I10,1X,I3)", advance='no') "PaoPotential", ikind, ipot
     562           33 :             WRITE (unit_nr, "(1X,I3)", advance='no') pao_potentials(ipot)%maxl
     563           33 :             WRITE (unit_nr, "(1X,I3)", advance='no') pao_potentials(ipot)%max_projector
     564           33 :             WRITE (unit_nr, "(1X,F20.16)", advance='no') pao_potentials(ipot)%beta
     565           65 :             WRITE (unit_nr, "(1X,F20.16)") pao_potentials(ipot)%weight
     566              :          END DO
     567              :       END DO
     568              : 
     569              :       ! write cell
     570           32 :       WRITE (unit_nr, fmt="(A,5X)", advance='no') "Cell"
     571          416 :       WRITE (unit_nr, *) cell%hmat*angstrom
     572              : 
     573              :       ! write atoms
     574           32 :       WRITE (unit_nr, "(A,5X,I0)") "Natoms", SIZE(particle_set)
     575           96 :       DO iatom = 1, SIZE(particle_set)
     576           64 :          kindname = particle_set(iatom)%atomic_kind%name
     577           64 :          WRITE (unit_nr, fmt="(A,5X,I10,5X,A,1X)", advance='no') "Atom ", iatom, TRIM(kindname)
     578          288 :          WRITE (unit_nr, *) particle_set(iatom)%r*angstrom
     579              :       END DO
     580              : 
     581           32 :    END SUBROUTINE write_restart_header
     582              : 
     583              : !**************************************************************************************************
     584              : !> \brief writing the KS matrix (in terms of the PAO basis) in csr format into a file
     585              : !> \param qs_env qs environment
     586              : !> \param ls_scf_env ls environment
     587              : !> \author Mohammad Hossein Bani-Hashemian
     588              : ! **************************************************************************************************
     589          254 :    SUBROUTINE pao_write_ks_matrix_csr(qs_env, ls_scf_env)
     590              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     591              :       TYPE(ls_scf_env_type), TARGET                      :: ls_scf_env
     592              : 
     593              :       CHARACTER(len=*), PARAMETER :: routineN = 'pao_write_ks_matrix_csr'
     594              : 
     595              :       CHARACTER(LEN=default_path_length)                 :: file_name, fileformat
     596              :       INTEGER                                            :: handle, ispin, output_unit, unit_nr
     597              :       LOGICAL                                            :: bin, do_kpoints, do_ks_csr_write, uptr
     598              :       REAL(KIND=dp)                                      :: thld
     599              :       TYPE(cp_logger_type), POINTER                      :: logger
     600              :       TYPE(dbcsr_csr_type)                               :: ks_mat_csr
     601              :       TYPE(dbcsr_type)                                   :: matrix_ks_nosym
     602              :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     603              : 
     604          254 :       CALL timeset(routineN, handle)
     605              : 
     606          254 :       NULLIFY (dft_section)
     607              : 
     608          254 :       logger => cp_get_default_logger()
     609          254 :       output_unit = cp_logger_get_default_io_unit(logger)
     610              : 
     611          254 :       CALL get_qs_env(qs_env, input=input)
     612          254 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     613              :       do_ks_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
     614          254 :                                                          "PRINT%KS_CSR_WRITE"), cp_p_file)
     615              : 
     616              :       ! NOTE: k-points has to be treated differently later. k-points has KS matrix as double pointer.
     617          254 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
     618              : 
     619          254 :       IF (do_ks_csr_write .AND. (.NOT. do_kpoints)) THEN
     620            0 :          CALL section_vals_val_get(dft_section, "PRINT%KS_CSR_WRITE%THRESHOLD", r_val=thld)
     621            0 :          CALL section_vals_val_get(dft_section, "PRINT%KS_CSR_WRITE%UPPER_TRIANGULAR", l_val=uptr)
     622            0 :          CALL section_vals_val_get(dft_section, "PRINT%KS_CSR_WRITE%BINARY", l_val=bin)
     623              : 
     624            0 :          IF (bin) THEN
     625            0 :             fileformat = "UNFORMATTED"
     626              :          ELSE
     627            0 :             fileformat = "FORMATTED"
     628              :          END IF
     629              : 
     630            0 :          DO ispin = 1, SIZE(ls_scf_env%matrix_ks)
     631              : 
     632            0 :             IF (dbcsr_has_symmetry(ls_scf_env%matrix_ks(ispin))) THEN
     633            0 :                CALL dbcsr_desymmetrize(ls_scf_env%matrix_ks(ispin), matrix_ks_nosym)
     634              :             ELSE
     635            0 :                CALL dbcsr_copy(matrix_ks_nosym, ls_scf_env%matrix_ks(ispin))
     636              :             END IF
     637              : 
     638            0 :             CALL dbcsr_csr_create_from_dbcsr(matrix_ks_nosym, ks_mat_csr, dbcsr_csr_dbcsr_blkrow_dist)
     639            0 :             CALL dbcsr_convert_dbcsr_to_csr(matrix_ks_nosym, ks_mat_csr)
     640              : 
     641            0 :             WRITE (file_name, '(A,I0)') "PAO_KS_SPIN_", ispin
     642              :             unit_nr = cp_print_key_unit_nr(logger, dft_section, "PRINT%KS_CSR_WRITE", &
     643              :                                            extension=".csr", middle_name=TRIM(file_name), &
     644            0 :                                            file_status="REPLACE", file_form=fileformat)
     645            0 :             CALL dbcsr_csr_write(ks_mat_csr, unit_nr, upper_triangle=uptr, threshold=thld, binary=bin)
     646              : 
     647            0 :             CALL cp_print_key_finished_output(unit_nr, logger, dft_section, "PRINT%KS_CSR_WRITE")
     648              : 
     649            0 :             CALL dbcsr_csr_destroy(ks_mat_csr)
     650            0 :             CALL dbcsr_release(matrix_ks_nosym)
     651              :          END DO
     652              :       END IF
     653              : 
     654          254 :       CALL timestop(handle)
     655              : 
     656          254 :    END SUBROUTINE pao_write_ks_matrix_csr
     657              : 
     658              : !**************************************************************************************************
     659              : !> \brief writing the overlap matrix (in terms of the PAO basis) in csr format into a file
     660              : !> \param qs_env qs environment
     661              : !> \param ls_scf_env ls environment
     662              : !> \author Mohammad Hossein Bani-Hashemian
     663              : ! **************************************************************************************************
     664          254 :    SUBROUTINE pao_write_s_matrix_csr(qs_env, ls_scf_env)
     665              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     666              :       TYPE(ls_scf_env_type), TARGET                      :: ls_scf_env
     667              : 
     668              :       CHARACTER(len=*), PARAMETER :: routineN = 'pao_write_s_matrix_csr'
     669              : 
     670              :       CHARACTER(LEN=default_path_length)                 :: file_name, fileformat
     671              :       INTEGER                                            :: handle, output_unit, unit_nr
     672              :       LOGICAL                                            :: bin, do_kpoints, do_s_csr_write, uptr
     673              :       REAL(KIND=dp)                                      :: thld
     674              :       TYPE(cp_logger_type), POINTER                      :: logger
     675              :       TYPE(dbcsr_csr_type)                               :: s_mat_csr
     676              :       TYPE(dbcsr_type)                                   :: matrix_s_nosym
     677              :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     678              : 
     679          254 :       CALL timeset(routineN, handle)
     680              : 
     681          254 :       NULLIFY (dft_section)
     682              : 
     683          254 :       logger => cp_get_default_logger()
     684          254 :       output_unit = cp_logger_get_default_io_unit(logger)
     685              : 
     686          254 :       CALL get_qs_env(qs_env, input=input)
     687          254 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     688              :       do_s_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
     689          254 :                                                         "PRINT%S_CSR_WRITE"), cp_p_file)
     690              : 
     691              :       ! NOTE: k-points has to be treated differently later. k-points has overlap matrix as double pointer.
     692          254 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
     693              : 
     694          254 :       IF (do_s_csr_write .AND. (.NOT. do_kpoints)) THEN
     695            0 :          CALL section_vals_val_get(dft_section, "PRINT%S_CSR_WRITE%THRESHOLD", r_val=thld)
     696            0 :          CALL section_vals_val_get(dft_section, "PRINT%S_CSR_WRITE%UPPER_TRIANGULAR", l_val=uptr)
     697            0 :          CALL section_vals_val_get(dft_section, "PRINT%S_CSR_WRITE%BINARY", l_val=bin)
     698              : 
     699            0 :          IF (bin) THEN
     700            0 :             fileformat = "UNFORMATTED"
     701              :          ELSE
     702            0 :             fileformat = "FORMATTED"
     703              :          END IF
     704              : 
     705            0 :          IF (dbcsr_has_symmetry(ls_scf_env%matrix_s)) THEN
     706            0 :             CALL dbcsr_desymmetrize(ls_scf_env%matrix_s, matrix_s_nosym)
     707              :          ELSE
     708            0 :             CALL dbcsr_copy(matrix_s_nosym, ls_scf_env%matrix_s)
     709              :          END IF
     710              : 
     711            0 :          CALL dbcsr_csr_create_from_dbcsr(matrix_s_nosym, s_mat_csr, dbcsr_csr_dbcsr_blkrow_dist)
     712            0 :          CALL dbcsr_convert_dbcsr_to_csr(matrix_s_nosym, s_mat_csr)
     713              : 
     714            0 :          WRITE (file_name, '(A,I0)') "PAO_S"
     715              :          unit_nr = cp_print_key_unit_nr(logger, dft_section, "PRINT%S_CSR_WRITE", &
     716              :                                         extension=".csr", middle_name=TRIM(file_name), &
     717            0 :                                         file_status="REPLACE", file_form=fileformat)
     718            0 :          CALL dbcsr_csr_write(s_mat_csr, unit_nr, upper_triangle=uptr, threshold=thld, binary=bin)
     719              : 
     720            0 :          CALL cp_print_key_finished_output(unit_nr, logger, dft_section, "PRINT%S_CSR_WRITE")
     721              : 
     722            0 :          CALL dbcsr_csr_destroy(s_mat_csr)
     723            0 :          CALL dbcsr_release(matrix_s_nosym)
     724              :       END IF
     725              : 
     726          254 :       CALL timestop(handle)
     727              : 
     728          254 :    END SUBROUTINE pao_write_s_matrix_csr
     729              : 
     730              : !**************************************************************************************************
     731              : !> \brief writing the core Hamiltonian matrix (NYA)
     732              : !> \param qs_env qs environment
     733              : !> \param ls_scf_env ls environment
     734              : !> \author Mohammad Hossein Bani-Hashemian
     735              : ! **************************************************************************************************
     736          254 :    SUBROUTINE pao_write_hcore_matrix_csr(qs_env, ls_scf_env)
     737              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     738              :       TYPE(ls_scf_env_type), TARGET                      :: ls_scf_env
     739              : 
     740              :       CHARACTER(len=*), PARAMETER :: routineN = 'pao_write_hcore_matrix_csr'
     741              : 
     742              :       INTEGER                                            :: handle, output_unit
     743              :       LOGICAL                                            :: do_h_csr_write, do_kpoints
     744              :       TYPE(cp_logger_type), POINTER                      :: logger
     745              :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     746              : 
     747              :       MARK_USED(ls_scf_env)
     748              : 
     749          254 :       CALL timeset(routineN, handle)
     750              : 
     751          254 :       NULLIFY (dft_section)
     752              : 
     753          254 :       logger => cp_get_default_logger()
     754          254 :       output_unit = cp_logger_get_default_io_unit(logger)
     755              : 
     756          254 :       CALL get_qs_env(qs_env, input=input)
     757          254 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     758              :       do_h_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
     759          254 :                                                         "PRINT%HCORE_CSR_WRITE"), cp_p_file)
     760              : 
     761              :       ! NOTE: k-points has to be treated differently later. k-points has KS matrix as double pointer.
     762          254 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
     763              : 
     764          254 :       IF (do_h_csr_write .AND. (.NOT. do_kpoints)) THEN
     765            0 :          CALL cp_warn(__LOCATION__, "Writing the PAO Core Hamiltonian matrix in CSR format NYA")
     766              :       END IF
     767              : 
     768          254 :       CALL timestop(handle)
     769              : 
     770          254 :    END SUBROUTINE pao_write_hcore_matrix_csr
     771              : 
     772              : !**************************************************************************************************
     773              : !> \brief writing the density matrix (NYA)
     774              : !> \param qs_env qs environment
     775              : !> \param ls_scf_env ls environment
     776              : !> \author Mohammad Hossein Bani-Hashemian
     777              : ! **************************************************************************************************
     778          254 :    SUBROUTINE pao_write_p_matrix_csr(qs_env, ls_scf_env)
     779              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     780              :       TYPE(ls_scf_env_type), TARGET                      :: ls_scf_env
     781              : 
     782              :       CHARACTER(len=*), PARAMETER :: routineN = 'pao_write_p_matrix_csr'
     783              : 
     784              :       INTEGER                                            :: handle, output_unit
     785              :       LOGICAL                                            :: do_kpoints, do_p_csr_write
     786              :       TYPE(cp_logger_type), POINTER                      :: logger
     787              :       TYPE(section_vals_type), POINTER                   :: dft_section, input
     788              : 
     789              :       MARK_USED(ls_scf_env)
     790              : 
     791          254 :       CALL timeset(routineN, handle)
     792              : 
     793          254 :       NULLIFY (dft_section)
     794              : 
     795          254 :       logger => cp_get_default_logger()
     796          254 :       output_unit = cp_logger_get_default_io_unit(logger)
     797              : 
     798          254 :       CALL get_qs_env(qs_env, input=input)
     799          254 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     800              :       do_p_csr_write = BTEST(cp_print_key_should_output(logger%iter_info, dft_section, &
     801          254 :                                                         "PRINT%P_CSR_WRITE"), cp_p_file)
     802              : 
     803              :       ! NOTE: k-points has to be treated differently later. k-points has KS matrix as double pointer.
     804          254 :       CALL get_qs_env(qs_env=qs_env, do_kpoints=do_kpoints)
     805              : 
     806          254 :       IF (do_p_csr_write .AND. (.NOT. do_kpoints)) THEN
     807            0 :          CALL cp_warn(__LOCATION__, "Writing the PAO density matrix in CSR format NYA")
     808              :       END IF
     809              : 
     810          254 :       CALL timestop(handle)
     811              : 
     812          254 :    END SUBROUTINE pao_write_p_matrix_csr
     813              : 
     814            0 : END MODULE pao_io
        

Generated by: LCOV version 2.0-1