46 character(len=1024) :: header =
""
47 logical :: header_is_written = .false.
69 class(*),
target,
intent(in) :: data
70 real(kind=
rp),
intent(in),
optional :: t
80 if (.not.
allocated(data%x))
then
81 call neko_error(
"Vector is not allocated. Use &
82 &vector%init() to associate your array &
83 &with a vector_t object")
88 if (.not.
allocated(data%x))
then
89 call neko_error(
"Matrix is not allocated. Use &
90 &matrix%init() to associate your array &
91 &with a matrix_t object")
96 call neko_error(
"Invalid data. Expected vector_t or &
103 call neko_log%message(
"Writing to " // trim(this%get_fname()))
104 if (
associated(vec))
then
106 else if (
associated(mat))
then
125 real(kind=
rp),
intent(in),
optional :: t
126 integer :: file_unit, ierr, n
129 if (f%overwrite .and. .not. f%header_is_written)
then
130 open(unit = 999,
file = trim(f%get_fname()), status =
"old", &
132 if (ierr == 0)
close(999, status =
"delete")
135 open(
file = trim(f%get_fname()), position =
"append", iostat = ierr, &
137 if (ierr .ne. 0)
then
138 call neko_error(
"Error while opening " // trim(f%get_fname()))
142 if (f%header .ne.
"" .and. .not. f%header_is_written)
then
143 write (file_unit,
'(A)') trim(f%header)
144 f%header_is_written = .true.
148 if (
present(t))
write (file_unit,
'(g0,",")', advance =
"no") t
151 write (file_unit,
'(*(g0,","))', advance =
"no") data%x(1:n-1)
152 write (file_unit,
'(g0)') data%x(n)
166 real(kind=
rp),
intent(in),
optional :: t
167 integer :: file_unit, i, ierr, nc
170 if (f%overwrite .and. .not. f%header_is_written)
then
171 open(unit = 999,
file = trim(f%get_fname()), status =
"old", &
173 if (ierr == 0)
close(999, status =
"delete")
176 open(
file = trim(f%get_fname()), position =
"append", iostat = ierr, &
178 if (ierr .ne. 0)
then
179 call neko_error(
"Error while opening " // trim(f%get_fname()))
183 if (f%header .ne.
"" .and. .not. f%header_is_written)
then
184 write (file_unit,
'(A)') trim(f%header)
185 f%header_is_written = .true.
188 do i = 1, data%get_nrows()
189 if (
present(t))
write (file_unit,
'(g0,",")', advance =
"no") t
190 nc = data%get_ncols()
191 write (file_unit,
'(*(g0,","))', advance =
"no") &
193 write (file_unit,
'(g0)') data%x(i, nc)
205 class(*),
target,
intent(inout) :: data
209 call this%check_exists()
217 if (.not.
allocated(data%x))
then
218 call neko_error(
"Vector is not allocated. Use &
219 &vector%init() to associate your array &
220 &with a vector_t object")
225 if (.not.
allocated(data%x))
then
226 call neko_error(
"Matrix is not allocated. Use &
227 &matrix%init() to associate your array &
228 &with a matrix_t object")
233 call neko_error(
"Invalid data type for csv_file (expected: vector_t, &
240 call neko_log%message(
"Reading csv file " // trim(this%get_fname()))
241 if (
associated(vec))
then
243 else if (
associated(mat))
then
260 type(
vector_t),
intent(inout) :: vec
261 integer :: ierr, file_unit, n_lines
262 character(len=80) :: tmp
264 n_lines = f%count_lines()
266 open(
file = trim(f%get_fname()), status =
'old', newunit = file_unit, &
268 if (ierr .ne. 0)
then
269 call neko_error(
"Error while opening " // trim(f%get_fname()))
273 if (n_lines .gt. 1)
then
274 read (file_unit,
'(A)') tmp
278 read (file_unit,*) vec%x
279 close(unit = file_unit)
291 type(
matrix_t),
intent(inout) :: mat
292 integer :: ierr, file_unit, i, n_lines
293 character(len=80) :: tmp
295 n_lines = f%count_lines()
297 open(
file = trim(f%get_fname()), status =
'old', newunit = file_unit, &
299 if (ierr .ne. 0)
then
300 call neko_error(
"Error while opening " // trim(f%get_fname()))
305 if (n_lines .gt. mat%get_nrows())
then
306 read (file_unit,
'(A)') tmp
310 do i = 1, mat%get_nrows()
311 read (file_unit,*) mat%x(i,:)
313 close(unit = file_unit)
323 character(len=*),
intent(in) :: hd
325 this%header = trim(hd)
335 integer :: ierr, file_unit
337 call this%check_exists()
339 open(
file = trim(this%get_fname()), status =
'old', newunit = file_unit, &
341 if (ierr .ne. 0)
then
342 call neko_error(
"Error while opening " // trim(this%get_fname()))
350 read (file_unit, *, iostat = ierr)
351 if (ierr .ne. 0)
exit
355 close(unit = file_unit)
364 logical,
intent(in) :: overwrite
366 this%overwrite = overwrite
integer, public pe_rank
MPI rank.
File format for .csv files, used for any read/write operations involving floating point data.
subroutine csv_file_set_header(this, hd)
Sets the header for a csv file. For example: hd = "u,v,w,p".
subroutine csv_file_write_matrix(f, data, t)
Writes a matrix_t object to an output file. If the parameter t is present, it will be appended at the...
subroutine csv_file_write_vector(f, data, t)
Writes a vector_t object to an output file, in a row format. If the parameter t is present,...
subroutine csv_file_write(this, data, t)
Writes data to an output file.
subroutine csv_file_set_overwrite(this, overwrite)
Sets the overwrite flag for a csv file.
subroutine csv_file_read(this, data)
Reads data from an input file.
subroutine csv_file_read_matrix(f, mat)
Read a matrix from a csv file.
integer function csv_file_count_lines(this)
Count the number of lines in a file by going through it entirely until the end is reached.
subroutine csv_file_read_vector(f, vec)
Read a vector (i.e. data on a single row) from a csv file.
Module for file I/O operations.
type(log_t), public neko_log
Global log stream.
integer, parameter, public log_size
integer, parameter, public rp
Global precision used in computations.