(git:6344619)
Loading...
Searching...
No Matches
ri_rs_grid_from_file.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 Read pretabulated atom-relative RI-RS grids from data files.
10!> \par History
11!> 09.2026 created
12! **************************************************************************************************
14 USE cp_files, ONLY: close_file,&
16 USE kinds, ONLY: default_path_length,&
18 dp
22#include "./base/base_uses.f90"
23
24 IMPLICIT NONE
25 PRIVATE
26
28
29 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'ri_rs_grid_from_file'
30
31CONTAINS
32
33! **************************************************************************************************
34!> \brief Read pretabulated atom-relative RI-RS grids from the CP2K data files.
35!> \param bs_env ...
36! **************************************************************************************************
37 SUBROUTINE read_ri_rs_grid_from_file(bs_env)
38 TYPE(post_scf_bandstructure_type), POINTER :: bs_env
39
40 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_ri_rs_grid_from_file'
41
42 CHARACTER(LEN=default_path_length) :: filepath
43 CHARACTER(LEN=default_string_length) :: element_symbol
44 INTEGER :: handle, iatom, ikind, natom, nkind
45 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
46 TYPE(rirs_grid_type), ALLOCATABLE :: kind_grids(:)
47
48 CALL timeset(routinen, handle)
49
50 cpassert(ASSOCIATED(bs_env%ri_rs%atomic_kind_set))
51 nkind = SIZE(bs_env%ri_rs%atomic_kind_set)
52 particle_set => bs_env%ri_rs%particle_set
53 cpassert(ASSOCIATED(particle_set))
54 natom = bs_env%n_atom
55 ALLOCATE (kind_grids(nkind))
56
57 DO ikind = 1, nkind
58 element_symbol = trim(bs_env%ri_rs%atomic_kind_set(ikind)%element_symbol)
59 CALL get_ri_rs_grid_filepath(element_symbol, bs_env%ri_rs%grid_select, filepath)
60 CALL read_ri_rs_grid_file(trim(filepath), kind_grids(ikind)%raw_points)
61 kind_grids(ikind)%npts = SIZE(kind_grids(ikind)%raw_points, 2)
62 END DO
63
64 IF (ALLOCATED(bs_env%ri_rs%atomic_grids)) DEALLOCATE (bs_env%ri_rs%atomic_grids)
65 ALLOCATE (bs_env%ri_rs%atomic_grids(natom))
66 DO iatom = 1, natom
67 ikind = particle_set(iatom)%atomic_kind%kind_number
68 bs_env%ri_rs%atomic_grids(iatom) = kind_grids(ikind)
69 END DO
70 DEALLOCATE (kind_grids)
71
72 CALL timestop(handle)
73
74 END SUBROUTINE read_ri_rs_grid_from_file
75
76! **************************************************************************************************
77!> \brief Construct the path of a pretabulated RI-RS grid file.
78!> \param element_symbol ...
79!> \param grid_select ...
80!> \param filepath ...
81! **************************************************************************************************
82 SUBROUTINE get_ri_rs_grid_filepath(element_symbol, grid_select, filepath)
83 CHARACTER(LEN=*), INTENT(IN) :: element_symbol
84 INTEGER, INTENT(IN) :: grid_select
85 CHARACTER(LEN=*), INTENT(OUT) :: filepath
86
87 CHARACTER(LEN=default_string_length) :: suffix
88
89 SELECT CASE (grid_select)
90 CASE (1)
91 suffix = "_def2-tzvp-rs.ion"
92 CASE (2)
93 suffix = "_cc-pvtz-rs.ion"
94 CASE (3)
95 suffix = "_rirs.ion"
96 CASE DEFAULT
97 cpabort("Unknown grid_select (1=def2-TZVPP, 2=cc-pVTZ, 3=user-provided).")
98 END SELECT
99 filepath = "ri_rs_grid/"//trim(element_symbol)//trim(suffix)
100 END SUBROUTINE get_ri_rs_grid_filepath
101
102! **************************************************************************************************
103!> \brief Read Cartesian grid points from the existing CP2K RI-RS .ion format.
104!> \param filename Complete input filename; no suffix or element-name construction is performed.
105!> \param points Grid points in Bohr, indexed (alpha,l).
106! **************************************************************************************************
107 SUBROUTINE read_ri_rs_grid_file(filename, points)
108 CHARACTER(LEN=*), INTENT(IN) :: filename
109 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
110 INTENT(OUT) :: points
111
112 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_ri_rs_grid_file'
113
114 CHARACTER(LEN=default_path_length) :: line
115 INTEGER :: handle, ierr, ipoint, iunit, npoints
116 LOGICAL :: found_points
117
118 CALL timeset(routinen, handle)
119 CALL open_file(file_name=trim(filename), unit_number=iunit, &
120 file_action='READ', file_status='OLD')
121
122 CALL read_ri_rs_grid_header(iunit, filename, npoints)
123
124 rewind(iunit)
125 found_points = .false.
126 DO
127 READ (iunit, '(A)', iostat=ierr) line
128 IF (ierr /= 0) EXIT
129 IF (index(line, '<grid_points>') > 0) THEN
130 found_points = .true.
131 EXIT
132 END IF
133 END DO
134 IF (.NOT. found_points) THEN
135 cpabort('RI-RS .ion file has no <grid_points> block: '//trim(filename))
136 END IF
137
138 ALLOCATE (points(3, npoints))
139 DO ipoint = 1, npoints
140 READ (iunit, *, iostat=ierr) points(:, ipoint)
141 IF (ierr /= 0) cpabort('Invalid grid point in RI-RS .ion file: '//trim(filename))
142 END DO
143 CALL close_file(unit_number=iunit)
144 CALL timestop(handle)
145 END SUBROUTINE read_ri_rs_grid_file
146
147! **************************************************************************************************
148!> \brief Read and validate the point count in an open RI-RS grid file.
149!> \param iunit ...
150!> \param filename ...
151!> \param npoints ...
152! **************************************************************************************************
153 SUBROUTINE read_ri_rs_grid_header(iunit, filename, npoints)
154 INTEGER, INTENT(IN) :: iunit
155 CHARACTER(LEN=*), INTENT(IN) :: filename
156 INTEGER, INTENT(OUT) :: npoints
157
158 CHARACTER(LEN=default_path_length) :: line
159 INTEGER :: colon, ierr
160 LOGICAL :: found_size
161
162 found_size = .false.
163 npoints = 0
164 DO
165 READ (iunit, '(A)', iostat=ierr) line
166 IF (ierr /= 0) EXIT
167 IF (index(line, 'n points') > 0) THEN
168 colon = index(line, ':')
169 IF (colon > 0) THEN
170 READ (line(colon + 1:), *, iostat=ierr) npoints
171 found_size = ierr == 0 .AND. npoints > 0
172 END IF
173 EXIT
174 END IF
175 END DO
176 IF (.NOT. found_size) THEN
177 cpabort('RI-RS .ion file has no valid n points field: '//trim(filename))
178 END IF
179 END SUBROUTINE read_ri_rs_grid_header
180
181END MODULE ri_rs_grid_from_file
Utility routines to open and close files.
Definition cp_files.F:22
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
Definition cp_files.F:197
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
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
Define the data structure for the particle information.
Read pretabulated atom-relative RI-RS grids from data files.
subroutine, public read_ri_rs_grid_from_file(bs_env)
Read pretabulated atom-relative RI-RS grids from the CP2K data files.
Shared real-space resolution-of-identity grid and matrix data.
Definition ri_rs_types.F:11