7 character(len=NEKO_FNAME_LEN) :: in_fname, out_fname, inputchar
8 character(len=80) :: extension
9 real(kind=dp) :: start_time
12 argc = command_argument_count()
14 if ((argc .lt. 3) .or. (argc .gt. 3))
then
21 call get_command_argument(1, inputchar)
22 read(inputchar, *) in_fname
23 call get_command_argument(2, inputchar)
24 read(inputchar, *) start_time
25 call get_command_argument(3, inputchar)
26 read(inputchar, *) out_fname
28 call filename_suffix(in_fname, extension)
29 select case (trim(extension))
36 character(len=LOG_SIZE) :: log_buf
37 write (log_buf, *) trim(extension),
" files not supported!"
38 call neko_warning(log_buf)
49 if (pe_rank .eq. 0)
then
51 write(*,*)
'average_fields_in_time: Computes a weighted average of a '
52 write(*,*)
'set of statistics samples in time.'
55 write(*,*)
'==========================================================='
56 write(*,*)
' ./average_fields_in_time input.fld start_time output.fld'
57 write(*,*)
' ./average_fields_in_time input.csv start_time output.csv'
59 write(*,*)
' start_time is the time at which the first file starts to '
60 write(*,*)
' collect stats.'
62 write(*,*)
'Examples:'
63 write(*,*)
'==========================================================='
64 write(*,*)
' ./average_fields_in_time fluid_stats00.fld 103.2 ' // &
67 write(*,*)
' Computes the average field over the fld files ' // &
69 write(*,*)
' in fluid_stats00.nek5000. The files need to be arranged'
70 write(*,*)
' in chronological order. The average field is then '
71 write(*,*)
' stored in a fld series, i.e. mean0.nek5000 and'
72 write(*,*)
' mean0.f00000'
75 write(*,*)
' ./average_fields_in_time ' // &
76 'stats0.csv 103.2 mean_field_avg.csv'
78 write(*,*)
' Computes the average field over the csv file'
79 write(*,*)
' stats0.csv and writes it in mean_field_avg.csv'
90 character(len=NEKO_FNAME_LEN),
intent(in) :: fld_fname, output_fname
91 real(kind=dp),
intent(in) :: start_time
93 type(file_t) ::
fld_file, output_file
94 type(fld_file_data_t) :: fld_data, fld_data_avg
95 character(len=LOG_SIZE) :: log_buf
102 call fld_data_avg%init()
106 write (log_buf,
'(A, g0)')
"dt: ", fld_data_avg%time - start_time
107 call neko_log%message(log_buf)
109 dt =
real(fld_data_avg%time - start_time, kind=rp)
110 call fld_data_avg%scale(dt)
112 do i = 1, fld_data_avg%meta_nsamples-1
115 dt =
real(fld_data%time - fld_data_avg%time, kind=rp)
116 call fld_data%scale(dt)
117 call fld_data_avg%add(fld_data)
119 write (log_buf,
'(A, g0)')
"dt: ", dt
120 call neko_log%message(log_buf)
122 fld_data_avg%time = fld_data%time
126 dt =
real(1.0_dp/(fld_data_avg%time-start_time), kind=rp)
127 call fld_data_avg%scale(dt)
129 call output_file%init(trim(output_fname))
131 call neko_log%message(
'Writing file: ' // trim(output_fname))
132 call output_file%write(fld_data_avg, fld_data_avg%time)
133 call neko_log%message(
'Done')
136 call fld_data_avg%free()
157 character(len=NEKO_FNAME_LEN),
intent(in) :: in_fname, out_fname
158 real(kind=dp),
intent(in) :: start_time
160 type(file_t) :: out_file
161 type(matrix_t) :: data, avg_data
162 character(len=LOG_SIZE) :: log_buf
163 integer :: sample_size, n_samples
165 if (pe_rank .eq. 0)
then
173 if (mod(data%get_nrows(), sample_size) .ne. 0)
then
174 write(log_buf,*)
"# of rows in csv file : ", data%get_nrows()
175 call neko_log%message(log_buf)
176 write(log_buf,*)
"Size of each sample : ", sample_size
177 call neko_log%message(log_buf)
179 call neko_error(
"The # of rows in the file must be a multiple" // &
180 " of the size of each sample.")
183 n_samples = data%get_nrows() / sample_size
184 write (log_buf,
'(A,I0)')
"Size of each sample: ", sample_size
185 call neko_log%message(log_buf)
186 write (log_buf,
'(A,I0)')
"Number of samples : ", n_samples
187 call neko_log%message(log_buf)
190 call avg_data%init(sample_size, data%get_ncols())
194 real(kind=dp) :: ta, tb
199 dt =
real(tb - ta, kind=rp)
201 write (log_buf,
'(A,I0,A,I0,A,g0)')
"Length of sample ", 1,
"/", &
203 call neko_log%message(log_buf)
206 avg_data%x(:,1:) = dt*data%x(1:sample_size, :)
208 do i = 1, n_samples-1
210 ta = data%x(sample_size*(i-1) + 1, 1)
211 tb = data%x(sample_size* i + 1, 1)
212 dt =
real(tb - ta, kind=rp)
214 write (log_buf,
'(A,I0,A,I0,A,g0)')
"Length of sample ", i+1,
"/", &
216 call neko_log%message(log_buf)
218 avg_data%x = avg_data%x + &
219 dt * data%x(sample_size*i + 1:sample_size * (i+1), :)
223 avg_data%x = avg_data%x / (data%x(data%get_nrows(), 1) - start_time)
226 avg_data%x(:,1) = data%x(data%get_nrows(), 1)
229 avg_data%x(:, 2) = data%x(1:sample_size, 2)
234 call out_file%init(trim(out_fname))
235 call out_file%set_overwrite(.true.)
236 call out_file%write(avg_data)
237 call file_free(out_file)
248 type(matrix_t),
intent(in) :: m
252 if (m%get_nrows() .le. 1)
then
259 do while ( abscmp(m%x(n,1), m%x(n+1,1)) )
261 if (n+1 > m%get_nrows()) &
262 call neko_error(
"Out of bounds while searching for sampling size!")
270 character(len=*),
intent(in) :: file_name
271 type(matrix_t),
intent(inout) :: m
274 integer :: unit, i, ierr, num_columns, num_lines, ios
275 character(len=1000) :: line
276 character(len=1) :: delimiter
277 character(len=LOG_SIZE) :: log_buf
283 open(newunit = unit,
file = trim(file_name), status =
'old', &
284 action =
'read', iostat = ios)
286 call neko_error(
"Error opening file " // trim(file_name))
294 read(unit,
'(A)', iostat = ios) line
298 if (num_lines .eq. 0)
then
301 do i = 1, len_trim(line)
302 if (line(i:i) == delimiter)
then
303 num_columns = num_columns + 1
309 num_lines = num_lines + 1
315 write (log_buf,
'(A,A,A,I0,A,I0,A)')
"Size of ", trim(file_name), &
316 " : ", num_lines,
" rows, ", num_columns,
" columns"
317 call neko_log%message(log_buf)
322 call m%init(num_lines, num_columns)
325 call f%init(trim(file_name))
subroutine avg_flds_in_time_fld(fld_fname, output_fname, start_time)
Average fields from a series of fld files.
subroutine populate_matrix(file_name, m)
Read a csv file, determine the # of rows and columns, allocate the matrix accordingly and populate th...
subroutine avg_flds_in_time_csv(in_fname, out_fname, start_time)
Average fields from a series of csv files.
integer function determine_size_of_csv_sample(m)
Finds the size of a statistics sample based on when the time stamp changes.
program average_fields_in_time
Program to sum up averaged fields computed for statistics and mean field Martin Karp 27/01-23.
Module for file I/O operations.