(git:1155b05)
Loading...
Searching...
No Matches
negf_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 NEGF restart files.
10!> \author Dmitry Ryndyk (12.2025)
11! **************************************************************************************************
12MODULE negf_io
13
14 USE cp_files, ONLY: close_file,&
21 USE kinds, ONLY: default_path_length,&
23 dp
24#include "./base/base_uses.f90"
25
26 IMPLICIT NONE
27
28 PRIVATE
29
30 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'negf_io'
31
32 PUBLIC :: negf_restart_file_name, &
35
36CONTAINS
37
38! **************************************************************************************************
39!> \brief Checks if the restart file exists and returns the filename.
40!> \param filename ...
41!> \param exist ...
42!> \param negf_section ...
43!> \param logger ...
44!> \param icontact ...
45!> \param ispin ...
46!> \param h00 ...
47!> \param h01 ...
48!> \param s00 ...
49!> \param s01 ...
50!> \param h ...
51!> \param s ...
52!> \param hc ...
53!> \param sc ...
54!> \par History
55!> * 12.2025 created [Dmitry Ryndyk]
56! **************************************************************************************************
57 SUBROUTINE negf_restart_file_name(filename, exist, negf_section, logger, icontact, ispin, h00, h01, s00, s01, h, s, hc, sc)
58 CHARACTER(LEN=default_path_length), INTENT(OUT) :: filename
59 LOGICAL, INTENT(OUT) :: exist
60 TYPE(section_vals_type), POINTER :: negf_section
61 TYPE(cp_logger_type), POINTER :: logger
62 INTEGER, INTENT(IN), OPTIONAL :: icontact, ispin
63 LOGICAL, INTENT(IN), OPTIONAL :: h00, h01, s00, s01, h, s, hc, sc
64
65 CHARACTER(len=default_string_length) :: middle_name, string1, string2
66 LOGICAL :: my_h, my_h00, my_h01, my_hc, my_s, &
67 my_s00, my_s01, my_sc
68 TYPE(section_vals_type), POINTER :: contact_section, print_key
69
70 my_h00 = .false.
71 IF (PRESENT(h00)) my_h00 = h00
72 my_h01 = .false.
73 IF (PRESENT(h01)) my_h01 = h01
74 my_s00 = .false.
75 IF (PRESENT(s00)) my_s00 = s00
76 my_s01 = .false.
77 IF (PRESENT(s01)) my_s01 = s01
78 my_h = .false.
79 IF (PRESENT(h)) my_h = h
80 my_s = .false.
81 IF (PRESENT(s)) my_s = s
82 my_hc = .false.
83 IF (PRESENT(hc)) my_hc = hc
84 my_sc = .false.
85 IF (PRESENT(sc)) my_sc = sc
86
87 exist = .false.
88
89 WRITE (string1, *) icontact
90 WRITE (string2, *) ispin
91
92 ! try to read from the filename that is generated automatically from the printkey
93 contact_section => section_vals_get_subs_vals(negf_section, "CONTACT")
94 print_key => section_vals_get_subs_vals(contact_section, "RESTART", i_rep_section=icontact)
95
96 IF (my_h00) THEN
97 IF (ispin == 0) THEN
98 middle_name = "N"//trim(string1)//"-H00"
99 ELSE
100 middle_name = "N"//trim(string1)//"-H00-S"//trim(string2)
101 END IF
102 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
103 extension=".hs", my_local=.false.)
104 END IF
105
106 IF (my_h01) THEN
107 IF (ispin == 0) THEN
108 middle_name = "N"//trim(string1)//"-H01"
109 ELSE
110 middle_name = "N"//trim(string1)//"-H01-S"//trim(string2)
111 END IF
112 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
113 extension=".hs", my_local=.false.)
114 END IF
115
116 IF (my_s00) THEN
117 middle_name = "N"//trim(string1)//"-S00"
118 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
119 extension=".hs", my_local=.false.)
120 END IF
121
122 IF (my_s01) THEN
123 middle_name = "N"//trim(string1)//"-S01"
124 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
125 extension=".hs", my_local=.false.)
126 END IF
127
128 ! try to read from the filename that is generated automatically from the printkey
129 print_key => section_vals_get_subs_vals(negf_section, "SCATTERING_REGION%RESTART")
130
131 IF (my_h) THEN
132 IF (ispin == 0) THEN
133 middle_name = "Hs"
134 ELSE
135 middle_name = "Hs-S"//trim(string2)
136 END IF
137 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
138 extension=".hs", my_local=.false.)
139 END IF
140
141 IF (my_s) THEN
142 middle_name = "Ss"
143 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
144 extension=".hs", my_local=.false.)
145 END IF
146
147 IF (my_hc) THEN
148 IF (ispin == 0) THEN
149 middle_name = "Hsc-N"//trim(string1)
150 ELSE
151 middle_name = "Hsc-N"//trim(string1)//"-S"//trim(string2)
152 END IF
153 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
154 extension=".hs", my_local=.false.)
155 END IF
156
157 IF (my_sc) THEN
158 middle_name = "Ssc-N"//trim(string1)
159 filename = negf_elecrodes_generate_filename(logger, print_key, middle_name=middle_name, &
160 extension=".hs", my_local=.false.)
161 END IF
162
163 INQUIRE (file=filename, exist=exist)
164
165 END SUBROUTINE negf_restart_file_name
166
167! **************************************************************************************************
168!> \brief ...
169!> \param logger the logger for the parallel environment, iteration info
170!> and filename generation
171!> \param print_key ...
172!> \param middle_name name to be added to the generated filename, useful when
173!> print_key activates different distinct outputs, to be able to
174!> distinguish them
175!> \param extension extension to be applied to the filename (including the ".")
176!> \param my_local if the unit should be local to this task, or global to the
177!> program (defaults to false).
178!> \return ...
179!> \par History
180!> * 12.2025 created [Dmitry Ryndyk]
181! **************************************************************************************************
182 FUNCTION negf_elecrodes_generate_filename(logger, print_key, middle_name, extension, &
183 my_local) RESULT(filename)
184 TYPE(cp_logger_type), POINTER :: logger
185 TYPE(section_vals_type), POINTER :: print_key
186 CHARACTER(len=*), INTENT(IN), OPTIONAL :: middle_name
187 CHARACTER(len=*), INTENT(IN) :: extension
188 LOGICAL, INTENT(IN) :: my_local
189 CHARACTER(len=default_path_length) :: filename
190
191 CHARACTER(len=default_path_length) :: outname, outpath, postfix, root
192 CHARACTER(len=default_string_length) :: my_middle_name
193 INTEGER :: my_ind1, my_ind2
194 LOGICAL :: has_root
195
196 CALL section_vals_val_get(print_key, "FILENAME", c_val=outpath)
197 IF (outpath(1:1) == '=') THEN
198 cpassert(len(outpath) - 1 <= len(filename))
199 filename = outpath(2:)
200 RETURN
201 END IF
202 IF (outpath == "__STD_OUT__") outpath = ""
203 outname = outpath
204 has_root = .false.
205 my_ind1 = index(outpath, "/")
206 my_ind2 = len_trim(outpath)
207 IF (my_ind1 /= 0) THEN
208 has_root = .true.
209 DO WHILE (index(outpath(my_ind1 + 1:my_ind2), "/") /= 0)
210 my_ind1 = index(outpath(my_ind1 + 1:my_ind2), "/") + my_ind1
211 END DO
212 IF (my_ind1 == my_ind2) THEN
213 outname = ""
214 ELSE
215 outname = outpath(my_ind1 + 1:my_ind2)
216 END IF
217 END IF
218
219 IF (PRESENT(middle_name)) THEN
220 IF (outname /= "") THEN
221 my_middle_name = "-"//trim(outname)//"-"//middle_name
222 ELSE
223 my_middle_name = "-"//middle_name
224 END IF
225 ELSE
226 IF (outname /= "") THEN
227 my_middle_name = "-"//trim(outname)
228 ELSE
229 my_middle_name = ""
230 END IF
231 END IF
232
233 IF (.NOT. has_root) THEN
234 root = trim(logger%iter_info%project_name)//trim(my_middle_name)
235 ELSE IF (outname == "") THEN
236 root = outpath(1:my_ind1)//trim(logger%iter_info%project_name)//trim(my_middle_name)
237 ELSE
238 root = outpath(1:my_ind1)//my_middle_name(2:len_trim(my_middle_name))
239 END IF
240
241 postfix = extension
242
243 ! and let the logger generate the filename
244 CALL cp_logger_generate_filename(logger, res=filename, &
245 root=root, postfix=postfix, local=my_local)
246
247 END FUNCTION negf_elecrodes_generate_filename
248
249! **************************************************************************************************
250!> \brief Prints full matrix to a file.
251!> \param filename ...
252!> \param matrix ...
253!> \par History
254!> * 12.2025 created [Dmitry Ryndyk]
255! **************************************************************************************************
256 SUBROUTINE negf_print_matrix_to_file(filename, matrix)
257 CHARACTER(LEN=default_path_length), INTENT(IN) :: filename
258 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: matrix
259
260 CHARACTER(len=100) :: sfmt
261 INTEGER :: i, j, ncol, nrow, print_unit
262
263 CALL open_file(file_name=filename, file_status="REPLACE", &
264 file_form="FORMATTED", file_action="WRITE", &
265 file_position="REWIND", unit_number=print_unit)
266
267 nrow = SIZE(matrix, 1)
268 ncol = SIZE(matrix, 2)
269 WRITE (sfmt, "('(',i0,'(E15.5))')") ncol
270 WRITE (print_unit, *) nrow, ncol
271 DO i = 1, nrow
272 WRITE (print_unit, sfmt) (matrix(i, j), j=1, ncol)
273 END DO
274
275 CALL close_file(print_unit)
276
277 END SUBROUTINE negf_print_matrix_to_file
278
279! **************************************************************************************************
280!> \brief Reads full matrix from a file.
281!> \param filename ...
282!> \param matrix ...
283!> \par History
284!> * 12.2025 created [Dmitry Ryndyk]
285! **************************************************************************************************
286 SUBROUTINE negf_read_matrix_from_file(filename, matrix)
287 CHARACTER(LEN=default_path_length), INTENT(IN) :: filename
288 REAL(kind=dp), DIMENSION(:, :), INTENT(INOUT) :: matrix
289
290 INTEGER :: i, j, ncol, nrow, print_unit
291
292 CALL open_file(file_name=filename, file_status="OLD", &
293 file_form="FORMATTED", file_action="READ", &
294 file_position="REWIND", unit_number=print_unit)
295
296 READ (print_unit, *) nrow, ncol
297 DO i = 1, nrow
298 READ (print_unit, *) (matrix(i, j), j=1, ncol)
299 END DO
300
301 CALL close_file(print_unit)
302
303 END SUBROUTINE negf_read_matrix_from_file
304
305END MODULE negf_io
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 ...
subroutine, public cp_logger_generate_filename(logger, res, root, postfix, local)
generates a unique filename (ie adding eventual suffixes and process ids)
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
Routines for reading and writing NEGF restart files.
Definition negf_io.F:12
subroutine, public negf_restart_file_name(filename, exist, negf_section, logger, icontact, ispin, h00, h01, s00, s01, h, s, hc, sc)
Checks if the restart file exists and returns the filename.
Definition negf_io.F:58
subroutine, public negf_print_matrix_to_file(filename, matrix)
Prints full matrix to a file.
Definition negf_io.F:257
subroutine, public negf_read_matrix_from_file(filename, matrix)
Reads full matrix from a file.
Definition negf_io.F:287
type of a logger, at the moment it contains just a print level starting at which level it should be l...