MFC
Exascale flow solver
Loading...
Searching...
No Matches
m_data_input.f90
Go to the documentation of this file.
1!>
2!! @file
3!> @brief Contains module m_data_input
4
5!> @brief Reads raw simulation grid and conservative-variable data for a given time-step and fills buffer regions
7
8#ifdef MFC_MPI
9 use mpi
10#endif
11
15 use m_mpi_proxy
16 use m_mpi_common
20 use m_helper
22
23 implicit none
24
27
28 abstract interface
29
30 !> Subroutine for reading data files
31 impure subroutine s_read_abstract_data_files(t_step)
32
33 implicit none
34
35 integer, intent(in) :: t_step
36
37 end subroutine s_read_abstract_data_files
38 end interface
39
40 type(scalar_field), allocatable, dimension(:), public :: q_cons_vf !< Conservative variables
41 type(scalar_field), allocatable, dimension(:), public :: q_cons_temp
42 type(scalar_field), allocatable, dimension(:), public :: q_prim_vf !< Primitive variables
43 type(integer_field), allocatable, dimension(:,:), public :: bc_type !< Boundary condition identifiers
44 type(scalar_field), public :: q_t_sf !< Temperature field
45 type(integer_field), public :: ib_markers
46
47 !> One AMR fine-block piece owned by this rank, held for visualization overlay of the refined solution.
48 type, public :: amr_fine_block
49 integer :: lo(3), hi(3) !< global coarse-index region bounds of the parent block
50 integer :: m, n, p !< local fine extents (interior 0:m, 0:n, 0:p)
51 real(wp), allocatable, dimension(:) :: x_cb, y_cb, z_cb !< reconstructed fine cell boundaries
52 type(scalar_field), allocatable, dimension(:) :: q_cons !< fine conservative state
53 end type amr_fine_block
54
55 type(amr_fine_block), allocatable, dimension(:), public :: amr_fine !< this rank's owned block pieces
56 integer, public :: amr_num_fine !< number of block pieces this rank owns (<= file's block count)
57
58 procedure(s_read_abstract_data_files), pointer :: s_read_data_files => null()
59
60contains
61
62 !> Helper subroutine to read grid data files for a given direction
63 impure subroutine s_read_grid_data_direction(t_step_dir, direction, cb_array, d_array, cc_array, size_dim)
64
65 character(len=*), intent(in) :: t_step_dir
66 character(len=1), intent(in) :: direction
67 real(wp), dimension(-1:), intent(out) :: cb_array
68 real(wp), dimension(0:), intent(out) :: d_array
69 real(wp), dimension(0:), intent(out) :: cc_array
70 integer, intent(in) :: size_dim
71 character(LEN=len_trim(t_step_dir) + 10) :: file_loc
72 logical :: file_check
73
74 file_loc = trim(t_step_dir) // '/' // direction // '_cb.dat'
75 inquire (file=trim(file_loc), exist=file_check)
76
77 if (file_check) then
78 open (1, file=trim(file_loc), form='unformatted', status='old', action='read')
79 read (1) cb_array(-1:size_dim)
80 close (1)
81 else
82 call s_mpi_abort('File ' // direction // '_cb.dat is missing in ' // trim(t_step_dir) // '. Exiting.')
83 end if
84
85 d_array(0:size_dim) = cb_array(0:size_dim) - cb_array(-1:size_dim - 1)
86 cc_array(0:size_dim) = cb_array(-1:size_dim - 1) + d_array(0:size_dim)/2._wp
87
88 end subroutine s_read_grid_data_direction
89
90#ifdef MFC_MPI
91 !> Helper subroutine to setup MPI data I/O parameters
92 impure subroutine s_setup_mpi_io_params(data_size, m_MOK, n_MOK, p_MOK, WP_MOK, MOK, str_MOK, NVARS_MOK)
93
94 integer, intent(out) :: data_size
95 integer(KIND=MPI_OFFSET_KIND), intent(out) :: m_mok, n_mok, p_mok
96 integer(KIND=MPI_OFFSET_KIND), intent(out) :: wp_mok, mok, str_mok, nvars_mok
97
98 if (ib) then
100 else
102 end if
103
104 data_size = (m + 1)*(n + 1)*(p + 1)
105
106 m_mok = int(m_glb + 1, mpi_offset_kind)
107 n_mok = int(n_glb + 1, mpi_offset_kind)
108 p_mok = int(p_glb + 1, mpi_offset_kind)
109 wp_mok = int(storage_size(0._stp)/8, mpi_offset_kind)
110 mok = int(1._wp, mpi_offset_kind)
111 str_mok = int(name_len, mpi_offset_kind)
112 nvars_mok = int(sys_size, mpi_offset_kind)
113
114 end subroutine s_setup_mpi_io_params
115#endif
116
117 !> Does a saved restart exist for this index? Under cfl_dt a step can cross two t_save boundaries, so save indices may be
118 !! skipped legitimately and the reader must skip them. Only the shared-file layout is checked: with file_per_process the answer
119 !! would not be rank-uniform, so that path stays fail-closed.
120 impure function f_save_exists(t_step) result(present_)
121
122 integer, intent(in) :: t_step
123 logical :: present_
124 character(LEN=path_len + 2*name_len) :: floc
125 character(LEN=name_len) :: fnum
126
127 present_ = .true.
128 if (parallel_io) then
129 if (file_per_process) return
130 write (fnum, '(I0,A)') t_step, '.dat'
131 floc = trim(case_dir) // '/restart_data' // trim(mpiiofs) // trim(fnum)
132 inquire (file=trim(floc), exist=present_)
133 else
134 ! the serial layout skips the same save indices; every rank saves the same steps, so this stays rank-uniform
135 write (floc, '(A,I0,A,I0,A)') trim(case_dir) // '/p_all/p', proc_rank, '/', t_step, '/.'
136 call my_inquire(floc, present_)
137 end if
138
139 end function f_save_exists
140
141 !> Helper subroutine to read IB data files
142 impure subroutine s_read_ib_data_files(file_loc_base, t_step)
143
144 character(len=*), intent(in) :: file_loc_base
145 integer, intent(in), optional :: t_step
146 character(LEN=path_len + 2*name_len) :: file_loc
147 logical :: file_exist
148 integer :: ifile, ierr, data_size
149 character(len=10) :: t_step_string
150
151#ifdef MFC_MPI
152 integer, dimension(MPI_STATUS_SIZE) :: status
153 integer(KIND=MPI_OFFSET_KIND) :: disp
154 integer(KIND=MPI_OFFSET_KIND) :: m_mok, n_mok, p_mok, mok, wp_mok, var_mok
155 integer :: save_index
156#endif
157
158 if (.not. ib) return
159
160 if (parallel_io .and. file_per_process) then
161 call s_int_to_str(t_step, t_step_string)
162 write (file_loc, '(A,I0,A,i7.7,A)') 'ib_markers_', t_step, '_', proc_rank, '.dat'
163 file_loc = trim(case_dir) // '/restart_data/lustre_' // trim(t_step_string) // '/' // trim(file_loc)
164 else if (parallel_io) then
165 write (file_loc, '(A)') trim(file_loc_base) // 'ib.dat'
166 else
167 write (file_loc, '(A)') trim(file_loc_base) // '/ib_data.dat'
168 end if
169 inquire (file=trim(file_loc), exist=file_exist)
170
171 if (file_exist) then
172 if (parallel_io .and. file_per_process) then
173#ifdef MFC_MPI
174 call mpi_file_open(mpi_comm_self, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
175 call s_check_mpi_file_open(ierr, file_loc)
176
177 data_size = (m + 1)*(n + 1)*(p + 1)
178
179 call mpi_file_read(ifile, mpi_io_ib_data%var%sf, data_size, mpi_integer, status, ierr)
180
181 call mpi_file_close(ifile, ierr)
182#endif
183 else if (parallel_io) then
184#ifdef MFC_MPI
185 call mpi_file_open(mpi_comm_world, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
186 call s_check_mpi_file_open(ierr, file_loc)
187
188 m_mok = int(m_glb + 1, mpi_offset_kind)
189 n_mok = int(n_glb + 1, mpi_offset_kind)
190 p_mok = int(p_glb + 1, mpi_offset_kind)
191 mok = int(1._wp, mpi_offset_kind)
192 wp_mok = int(storage_size(0._stp)/8, mpi_offset_kind)
193 save_index = f_save_index(t_step, cfl_dt, t_step_save)
194
195 data_size = (m + 1)*(n + 1)*(p + 1)
196 var_mok = int(sys_size + 1, mpi_offset_kind)
197 if (t_step == 0) then
198 disp = 0
199 else
200 disp = m_mok*max(mok, n_mok)*max(mok, p_mok)*wp_mok*(var_mok - 1 + int(save_index, mpi_offset_kind))
201 end if
202
203 call mpi_file_set_view(ifile, disp, mpi_integer, mpi_io_ib_data%view, 'native', mpi_info_int, ierr)
204 call mpi_file_read(ifile, mpi_io_ib_data%var%sf, data_size, mpi_integer, status, ierr)
205
206 call mpi_file_close(ifile, ierr)
207#endif
208 else
209 open (2, file=trim(file_loc), form='unformatted', action='read', status='old')
210 read (2) ib_markers%sf(0:m,0:n,0:p)
211 close (2)
212 end if
213 else
214 call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
215 end if
216
217 end subroutine s_read_ib_data_files
218
219 !> Helper subroutine to allocate field arrays for given dimensionality
220 impure subroutine s_allocate_field_arrays(local_start_idx, end_x, end_y, end_z)
221
222 integer, intent(in) :: local_start_idx, end_x, end_y, end_z
223 integer :: i
224
225 do i = 1, sys_size
226 allocate (q_cons_vf(i)%sf(local_start_idx:end_x,local_start_idx:end_y,local_start_idx:end_z))
227 allocate (q_prim_vf(i)%sf(local_start_idx:end_x,local_start_idx:end_y,local_start_idx:end_z))
228 end do
229
230 if (ib) then
231 allocate (ib_markers%sf(local_start_idx:end_x,local_start_idx:end_y,local_start_idx:end_z))
232 end if
233
234 if (chemistry .or. heat_conduction) then
235 allocate (q_t_sf%sf(local_start_idx:end_x,local_start_idx:end_y,local_start_idx:end_z))
236 q_t_sf%sf = 0._wp ! Buffer population reads the interior before anything writes it
237 end if
238
239 end subroutine s_allocate_field_arrays
240
241 !> Read the raw data files present in the corresponding time-step directory and to populate the associated grid and conservative
242 !! variables.
243 impure subroutine s_read_serial_data_files(t_step)
244
245 integer, intent(in) :: t_step
246 character(LEN=len_trim(case_dir) + 2*name_len) :: t_step_dir
247 character(LEN=len_trim(case_dir) + 3*name_len) :: file_loc
248 character(len=int(floor(log10(real(sys_size, wp)))) + 1) :: file_num
249 logical :: dir_check
250 logical :: file_check
251 integer :: i
252
253 write (t_step_dir, '(A,I0,A,I0)') '/p_all/p', proc_rank, '/', t_step
254 t_step_dir = trim(case_dir) // trim(t_step_dir)
255
256 file_loc = trim(t_step_dir) // '/.'
257 call my_inquire(file_loc, dir_check)
258
259 if (dir_check .neqv. .true.) then
260 call s_mpi_abort('Time-step folder ' // trim(t_step_dir) // ' is missing. Exiting.')
261 end if
262
263 if (bc_io) then
265 else
267 end if
268
269 ! Pass explicit slices so the dummy `dimension(-1:)` / `dimension(0:)` arguments map to the correct interior indices of the
270 ! actual arrays. Without slicing, when offset_x%beg or buff_size > 0 (i.e. format=1 parallel 3D ranks), Fortran's
271 ! assumed-shape re-mapping shifts the read by that many slots and leaves the last interior cells uninitialized - corrupting
272 ! downstream ghost-cell extrapolation.
273 call s_read_grid_data_direction(t_step_dir, 'x', x_cb(-1:m), dx(0:m), x_cc(0:m), m)
274
275 if (n > 0) then
276 call s_read_grid_data_direction(t_step_dir, 'y', y_cb(-1:n), dy(0:n), y_cc(0:n), n)
277
278 if (p > 0) then
279 call s_read_grid_data_direction(t_step_dir, 'z', z_cb(-1:p), dz(0:p), z_cc(0:p), p)
280 end if
281 end if
282
283 do i = 1, sys_size
284 write (file_num, '(I0)') i
285 file_loc = trim(t_step_dir) // '/q_cons_vf' // trim(file_num) // '.dat'
286 inquire (file=trim(file_loc), exist=file_check)
287
288 if (file_check) then
289 open (1, file=trim(file_loc), form='unformatted', status='old', action='read')
290 read (1) q_cons_vf(i)%sf(0:m,0:n,0:p)
291 close (1)
292 else
293 call s_mpi_abort('File q_cons_vf' // trim(file_num) // '.dat is missing in ' // trim(t_step_dir) // '. Exiting.')
294 end if
295 end do
296
297 call s_read_ib_data_files(t_step_dir)
298
299 end subroutine s_read_serial_data_files
300
301 !> Parallel-read the raw data files present in the corresponding time-step directory and to populate the associated grid and
302 !! conservative variables.
303 impure subroutine s_read_parallel_data_files(t_step)
304
305 integer, intent(in) :: t_step
306
307#ifdef MFC_MPI
308 real(wp), allocatable, dimension(:) :: x_cb_glb, y_cb_glb, z_cb_glb
309 integer :: ifile, ierr, data_size, filetype, stride
310 integer, dimension(MPI_STATUS_SIZE) :: status
311 integer(KIND=MPI_OFFSET_KIND) :: disp
312 integer(KIND=MPI_OFFSET_KIND) :: m_mok, n_mok, p_mok
313 integer(KIND=MPI_OFFSET_KIND) :: wp_mok, var_mok, str_mok
314 integer(KIND=MPI_OFFSET_KIND) :: nvars_mok
315 integer(KIND=MPI_OFFSET_KIND) :: mok
316 integer(kind=MPI_OFFSET_KIND) :: offset
317 character(LEN=path_len + 2*name_len) :: file_loc
318 logical :: file_exist
319 integer(kind=8) :: file_bytes, bytes_needed
320 character(len=10) :: case_m_str, file_m_str
321 character(len=10) :: t_step_string
322 integer :: i
323
324 allocate (x_cb_glb(-1:m_glb))
325 allocate (y_cb_glb(-1:n_glb))
326 allocate (z_cb_glb(-1:p_glb))
327
328 if (down_sample) then
329 stride = 3
330 else
331 stride = 1
332 end if
333
334 file_loc = trim(case_dir) // '/restart_data' // trim(mpiiofs) // 'x_cb.dat'
335 inquire (file=trim(file_loc), exist=file_exist, size=file_bytes)
336
337 ! The grid file holds one cell boundary per value, so its size says which grid wrote the restart. Without
338 ! this check a case file whose resolution does not match the run reads past the end of every restart
339 ! file and post-processes silently, exiting 0 with NaN-filled output that is indistinguishable from
340 ! success until someone plots it. The strided read down_sample performs touches stride*(m_glb + 1) + 1
341 ! boundaries of a full-resolution file, so it needs more of the file, not less; only the un-strided read
342 ! pins the size exactly, since down-sampling three grids of different size can land on the same m_glb.
343 if (file_exist) then
344 bytes_needed = (int(stride, 8)*int(m_glb + 1, 8) + 1_8)*int(storage_size(0._wp)/8, 8)
345 if (file_bytes < bytes_needed .or. (.not. down_sample .and. file_bytes /= bytes_needed)) then
346 call s_int_to_str(m_glb, case_m_str)
347 call s_int_to_str(int(file_bytes/int(storage_size(0._wp)/8, 8)) - 2, file_m_str)
348 call s_mpi_abort('Restart grid mismatch: this case has m = ' // trim(case_m_str) // ' but ' // trim(file_loc) &
349 & // ' was written with m = ' // trim(file_m_str) &
350 & // '. Post-processing must use the same grid as the run that wrote the ' &
351 & // 'restart files, or it reads past the end of every file and writes NaN.')
352 end if
353 data_size = m_glb + 2
354 call mpi_file_open(mpi_comm_world, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
355 call s_check_mpi_file_open(ierr, file_loc)
356
357 call mpi_type_vector(data_size, 1, stride, mpi_p, filetype, ierr)
358 call mpi_type_commit(filetype, ierr)
359
360 offset = 0
361 call mpi_file_set_view(ifile, offset, mpi_p, filetype, 'native', mpi_info_int, ierr)
362
363 call mpi_file_read(ifile, x_cb_glb, data_size, mpi_p, status, ierr)
364 call mpi_file_close(ifile, ierr)
365 else
366 call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
367 end if
368
369 ! Bitwise-consistent grid distribution from the global file
370 call s_apply_grid_from_global_dim(x_cb_glb, m_glb, m, start_idx(1), bc_x%beg, bc_x%end, offset_x%beg, offset_x%end, &
372
373 if (n > 0) then
374 file_loc = trim(case_dir) // '/restart_data' // trim(mpiiofs) // 'y_cb.dat'
375 inquire (file=trim(file_loc), exist=file_exist)
376
377 if (file_exist) then
378 data_size = n_glb + 2
379 call mpi_file_open(mpi_comm_world, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
380 call s_check_mpi_file_open(ierr, file_loc)
381
382 call mpi_type_vector(data_size, 1, stride, mpi_p, filetype, ierr)
383 call mpi_type_commit(filetype, ierr)
384
385 offset = 0
386 call mpi_file_set_view(ifile, offset, mpi_p, filetype, 'native', mpi_info_int, ierr)
387
388 call mpi_file_read(ifile, y_cb_glb, data_size, mpi_p, status, ierr)
389 call mpi_file_close(ifile, ierr)
390 else
391 call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
392 end if
393
394 call s_apply_grid_from_global_dim(y_cb_glb, n_glb, n, start_idx(2), bc_y%beg, bc_y%end, offset_y%beg, offset_y%end, &
396
397 if (p > 0) then
398 file_loc = trim(case_dir) // '/restart_data' // trim(mpiiofs) // 'z_cb.dat'
399 inquire (file=trim(file_loc), exist=file_exist)
400
401 if (file_exist) then
402 data_size = p_glb + 2
403 call mpi_file_open(mpi_comm_world, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
404 call s_check_mpi_file_open(ierr, file_loc)
405
406 call mpi_type_vector(data_size, 1, stride, mpi_p, filetype, ierr)
407 call mpi_type_commit(filetype, ierr)
408
409 offset = 0
410 call mpi_file_set_view(ifile, offset, mpi_p, filetype, 'native', mpi_info_int, ierr)
411
412 call mpi_file_read(ifile, z_cb_glb, data_size, mpi_p, status, ierr)
413 call mpi_file_close(ifile, ierr)
414 else
415 call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
416 end if
417
418 call s_apply_grid_from_global_dim(z_cb_glb, p_glb, p, start_idx(3), bc_z%beg, bc_z%end, offset_z%beg, &
420 end if
421 end if
422
423 call s_read_parallel_conservative_data(t_step, m_mok, n_mok, p_mok, wp_mok, mok, str_mok, nvars_mok)
424
425 deallocate (x_cb_glb, y_cb_glb, z_cb_glb)
426
427 if (bc_io) then
429 else
431 end if
432#endif
433
434 end subroutine s_read_parallel_data_files
435
436#ifdef MFC_MPI
437 !> Helper subroutine to read parallel conservative variable data
438 impure subroutine s_read_parallel_conservative_data(t_step, m_MOK, n_MOK, p_MOK, WP_MOK, MOK, str_MOK, NVARS_MOK)
439
440 integer, intent(in) :: t_step
441 integer(KIND=MPI_OFFSET_KIND), intent(inout) :: m_mok, n_mok, p_mok
442 integer(KIND=MPI_OFFSET_KIND), intent(inout) :: wp_mok, mok, str_mok, nvars_mok
443 integer :: ifile, ierr, data_size
444 integer, dimension(MPI_STATUS_SIZE) :: status
445 integer(KIND=MPI_OFFSET_KIND) :: disp, var_mok
446 character(LEN=path_len + 2*name_len) :: file_loc
447 logical :: file_exist
448 character(len=10) :: t_step_string
449 integer :: i
450
451 if (file_per_process) then
452 call s_int_to_str(t_step, t_step_string)
453 write (file_loc, '(I0,A1,I7.7,A)') t_step, '_', proc_rank, '.dat'
454 file_loc = trim(case_dir) // '/restart_data/lustre_' // trim(t_step_string) // trim(mpiiofs) // trim(file_loc)
455 inquire (file=trim(file_loc), exist=file_exist)
456
457 if (file_exist) then
458 call mpi_file_open(mpi_comm_self, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
459 call s_check_mpi_file_open(ierr, file_loc)
460
461 if (down_sample) then
463 else
464 if (ib) then
466 else
468 end if
469 end if
470
471 if (down_sample) then
472 data_size = (m + 3)*(n + 3)*(p + 3)
473 else
474 data_size = (m + 1)*(n + 1)*(p + 1)
475 end if
476
477 m_mok = int(m_glb + 1, mpi_offset_kind)
478 n_mok = int(n_glb + 1, mpi_offset_kind)
479 p_mok = int(p_glb + 1, mpi_offset_kind)
480 wp_mok = int(storage_size(0._stp)/8, mpi_offset_kind)
481 mok = int(1._wp, mpi_offset_kind)
482 str_mok = int(name_len, mpi_offset_kind)
483 nvars_mok = int(sys_size, mpi_offset_kind)
484
485 if (bubbles_euler .or. hypoelasticity .or. mhd) then
486 do i = 1, sys_size
487 var_mok = int(i, mpi_offset_kind)
488 call mpi_file_read_all(ifile, mpi_io_data%var(i)%sf(:,:,:), data_size*mpi_io_type, mpi_io_p, status, ierr)
489 end do
490 else
491 do i = 1, sys_size
492 var_mok = int(i, mpi_offset_kind)
493 call mpi_file_read_all(ifile, mpi_io_data%var(i)%sf(:,:,:), data_size*mpi_io_type, mpi_io_p, status, ierr)
494 end do
495 end if
496
497 call s_mpi_barrier()
498 call mpi_file_close(ifile, ierr)
499
500 if (down_sample) then
501 do i = 1, sys_size
502 q_cons_vf(i)%sf(0:m,0:n,0:p) = q_cons_temp(i)%sf(0:m,0:n,0:p)
503 end do
504 end if
505
506 call s_read_ib_data_files(trim(case_dir) // '/restart_data' // trim(mpiiofs), t_step)
507 else
508 call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
509 end if
510 else
511 write (file_loc, '(I0,A)') t_step, '.dat'
512 file_loc = trim(case_dir) // '/restart_data' // trim(mpiiofs) // trim(file_loc)
513 inquire (file=trim(file_loc), exist=file_exist)
514
515 if (file_exist) then
516 call mpi_file_open(mpi_comm_world, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
517 call s_check_mpi_file_open(ierr, file_loc)
518
519 call s_setup_mpi_io_params(data_size, m_mok, n_mok, p_mok, wp_mok, mok, str_mok, nvars_mok)
520
521 do i = 1, sys_size
522 var_mok = int(i, mpi_offset_kind)
523
524 disp = m_mok*max(mok, n_mok)*max(mok, p_mok)*wp_mok*(var_mok - 1)
525
526 call mpi_file_set_view(ifile, disp, mpi_p, mpi_io_data%view(i), 'native', mpi_info_int, ierr)
527 call mpi_file_read_all(ifile, mpi_io_data%var(i)%sf(:,:,:), data_size*mpi_io_type, mpi_io_p, status, ierr)
528 end do
529
530 call s_mpi_barrier()
531 call mpi_file_close(ifile, ierr)
532
533 call s_read_ib_data_files(trim(case_dir) // '/restart_data' // trim(mpiiofs), t_step)
534 else
535 call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
536 end if
537 end if
538
540#endif
541
542 !> Computation of parameters, allocation procedures, and/or any other tasks needed to properly setup the module
544
545 integer :: i
546
547 allocate (q_cons_vf(1:sys_size))
548 allocate (q_prim_vf(1:sys_size))
549 allocate (q_cons_temp(1:sys_size))
550
551 if (n > 0) then
552 if (p > 0) then
554 if (down_sample) then
555 do i = 1, sys_size
556 allocate (q_cons_temp(i)%sf(-1:m + 1,-1:n + 1,-1:p + 1))
557 end do
558 end if
559 else
561 end if
562 else
564 end if
565
566 allocate (bc_type(1:num_dims,1:2))
567
568 allocate (bc_type(1, 1)%sf(0:0,0:n,0:p))
569 allocate (bc_type(1, 2)%sf(0:0,0:n,0:p))
570 if (n > 0) then
571 allocate (bc_type(2, 1)%sf(-buff_size:m + buff_size,0:0,0:p))
572 allocate (bc_type(2, 2)%sf(-buff_size:m + buff_size,0:0,0:p))
573 if (p > 0) then
574 allocate (bc_type(3, 1)%sf(-buff_size:m + buff_size,-buff_size:n + buff_size,0:0))
575 allocate (bc_type(3, 2)%sf(-buff_size:m + buff_size,-buff_size:n + buff_size,0:0))
576 end if
577 end if
578
579 if (parallel_io .neqv. .true.) then
581 else
583 end if
584
585 end subroutine s_initialize_data_input_module
586
587 !> Deallocation procedures for the module
589
590 integer :: i
591
592 do i = 1, sys_size
593 deallocate (q_cons_vf(i)%sf)
594 deallocate (q_prim_vf(i)%sf)
595 if (down_sample) then
596 deallocate (q_cons_temp(i)%sf)
597 end if
598 end do
599
600 deallocate (q_cons_vf)
601 deallocate (q_prim_vf)
602 deallocate (q_cons_temp)
603
604 if (ib) then
605 deallocate (ib_markers%sf)
606 end if
607
608 if (chemistry .or. heat_conduction) then
609 deallocate (q_t_sf%sf)
610 end if
611
612 deallocate (bc_type(1, 1)%sf, bc_type(1, 2)%sf)
613 if (n > 0) then
614 deallocate (bc_type(2, 1)%sf, bc_type(2, 2)%sf)
615 if (p > 0) then
616 deallocate (bc_type(3, 1)%sf, bc_type(3, 2)%sf)
617 end if
618 end if
619
620 deallocate (bc_type)
621
622 s_read_data_files => null()
623
624 call s_free_amr_data()
625
626 end subroutine s_finalize_data_input_module
627
628 !> Populate block slot `k` metadata, allocate its conservative fields, and reconstruct its fine coordinates from the coarse cell
629 !! boundaries (x_cb/y_cb/z_cb, already read for this t_step). isect_lo is the block's global coarse origin; sidx is this rank's
630 !! global coarse origin (0 for a single-rank/no-MPI run). rr is the refinement factor for this block (amr_ref_ratio**level).
631 impure subroutine s_setup_amr_block(k, reg, isect_lo, sidx, fm, fn, fp, rr)
632
633 integer, intent(in) :: k, reg(6), isect_lo(3), sidx(3), fm, fn, fp, rr
634 integer :: i
635
636 amr_fine(k)%lo = reg(1:3)
637 amr_fine(k)%hi = reg(4:6)
638 amr_fine(k)%m = fm; amr_fine(k)%n = fn; amr_fine(k)%p = fp
639
640 allocate (amr_fine(k)%q_cons(1:sys_size))
641 do i = 1, sys_size
642 allocate (amr_fine(k)%q_cons(i)%sf(0:fm,0:fn,0:fp))
643 ! zeroed: the reader fills 1..sys_size_io but the conversion reads the whole band (post-only beta_idx slot, void
644 ! fraction 0 in fine blocks)
645 amr_fine(k)%q_cons(i)%sf = 0._wp
646 end do
647
648 ! isect_lo - sidx is the local coarse index whose x_cb slice the rr-way subdivision reads
649 allocate (amr_fine(k)%x_cb(-1:fm))
650 call s_amr_subdivide_cb(x_cb, lbound(x_cb, 1), isect_lo(1) - sidx(1), fm, rr, amr_fine(k)%x_cb)
651 if (n > 0) then
652 allocate (amr_fine(k)%y_cb(-1:fn))
653 call s_amr_subdivide_cb(y_cb, lbound(y_cb, 1), isect_lo(2) - sidx(2), fn, rr, amr_fine(k)%y_cb)
654 end if
655 if (p > 0) then
656 allocate (amr_fine(k)%z_cb(-1:fp))
657 call s_amr_subdivide_cb(z_cb, lbound(z_cb, 1), isect_lo(3) - sidx(3), fp, rr, amr_fine(k)%z_cb)
658 end if
659
660 end subroutine s_setup_amr_block
661
662 !> This rank's subdomain in global coarse cells: origin sidx (0 for a single rank or no MPI) and extent ext.
663 impure subroutine s_amr_local_domain(sidx, ext)
664
665 integer, intent(out) :: sidx(3), ext(3)
666
667 sidx = 0; ext = 0
668 ext(1) = m
669 if (n > 0) ext(2) = n
670 if (p > 0) ext(3) = p
671#ifdef MFC_MPI
672 sidx(1) = start_idx(1)
673 if (n > 0) sidx(2) = start_idx(2)
674 if (p > 0) sidx(3) = start_idx(3)
675#endif
676
677 end subroutine s_amr_local_domain
678
679 !> Intersection of the block region reg with this rank's subdomain (sidx, ext); owns = non-empty in every active dimension.
680 pure subroutine s_amr_block_isect(reg, sidx, ext, isect_lo, isect_hi, owns)
681
682 integer, intent(in) :: reg(6), sidx(3), ext(3)
683 integer, intent(out) :: isect_lo(3), isect_hi(3)
684 logical, intent(out) :: owns
685 integer :: d
686
687 do d = 1, 3
688 isect_lo(d) = max(reg(d), sidx(d))
689 isect_hi(d) = min(reg(3 + d), sidx(d) + ext(d))
690 end do
691 owns = isect_lo(1) <= isect_hi(1)
692 if (n > 0) owns = owns .and. isect_lo(2) <= isect_hi(2)
693 if (p > 0) owns = owns .and. isect_lo(3) <= isect_hi(3)
694
695 end subroutine s_amr_block_isect
696
697 !> Refinement factor of a block from its level and fine x-extent fmf (cells - 1); aborts on an inconsistent header.
698 impure integer function f_amr_block_ratio(reg, lvl, fmf) result(rr)
699
700 integer, intent(in) :: reg(6), lvl, fmf
701 integer :: cw
702
703 cw = max(reg(4) - reg(1) + 1, 1)
704 rr = (fmf + 1)/cw
705 if (lvl < 1 .or. rr < 2 .or. mod(fmf + 1, &
706 & cw) /= 0) call s_mpi_abort('amr post: malformed fine-block header (level/' &
707 & // 'extent inconsistent); the AMR restart writer and reader header layouts differ')
708
709 end function f_amr_block_ratio
710
711 !> The kept sub-box of a block: fine-cell offset foff from the block origin and count fcnt of the intersection.
712 pure subroutine s_amr_block_piece(reg, isect_lo, isect_hi, rr, foff, fcnt)
713
714 integer, intent(in) :: reg(6), isect_lo(3), isect_hi(3), rr
715 integer, intent(out) :: foff(3), fcnt(3)
716
717 foff = (isect_lo - reg(1:3))*rr
718 fcnt = (isect_hi - isect_lo + 1)*rr
719 if (n == 0) then; foff(2) = 0; fcnt(2) = 1; end if
720 if (p == 0) then; foff(3) = 0; fcnt(3) = 1; end if
721
722 end subroutine s_amr_block_piece
723
724 !> Read the AMR fine-level restart file for t_step (layout: m_amr_restart_io) and keep this rank's pieces of each block for the
725 !! post-process overlay. No-op when amr is off or the file is absent.
726 impure subroutine s_read_amr_data(t_step)
727
728 integer, intent(in) :: t_step
729 character(LEN=path_len + 3*name_len) :: file_loc
730 logical :: file_exist, owns
731 integer :: k, i, nblk, ghdr(amr_restart_ghdr_ints), reg(6), lvl, rm, rn, rp
732 integer :: sidx(3), ext(3), isect_lo(3), isect_hi(3), foff(3), fcnt(3), rr
733 integer :: have_loc, have_glb, ncoarse, ncoarse_glb
734 real(stp), allocatable :: sbuf(:,:,:,:)
735
736#ifdef MFC_MPI
737 integer :: ifile, ierr, cnt, fi, fj, fk
738 integer(8) :: idx, cnt8, plane
739 type(t_amr_restart_catalog) :: cat
740 integer, dimension(MPI_STATUS_SIZE) :: status
741 real(stp), allocatable :: buf(:)
742#endif
743
744 call s_free_amr_data()
745 if (.not. amr) return
746
747 call s_amr_local_domain(sidx, ext)
748
749 file_loc = f_amr_restart_path(t_step)
750 inquire (file=trim(file_loc), exist=file_exist)
751 ! all ranks must agree: with per-rank files a partially present tree would mix fine-overlay and coarse-only ranks
752 have_loc = merge(1, 0, file_exist)
753 call s_mpi_allreduce_integer_min(have_loc, have_glb)
754 if (have_glb == 0) then
755 if (proc_rank == 0 .and. file_exist) print '(A,I0,A)', ' [amr] post: AMR fine-block file(s) at t_step ', t_step, &
756 & ' are missing on some ranks; writing the coarse mesh only'
757 if (proc_rank == 0 .and. .not. file_exist) print '(A,I0,A)', ' [amr] post: no AMR fine-block file at t_step ', &
758 & t_step, '; writing the coarse mesh only'
759 return
760 end if
761
762 if (.not. parallel_io) then
763 open (2, file=trim(file_loc), form='unformatted', action='read', status='old')
764 read (2) ghdr
765 call s_amr_restart_check_header(ghdr, sys_size_io, 'amr post', nblk)
766 allocate (amr_fine(nblk))
767 amr_num_fine = 0
768 ncoarse = 0
769 do k = 1, nblk
770 read (2) reg, lvl, rm, rn, rp ! region(6), level, fine extents (mirrors s_write_amr_restart)
771 call s_amr_block_isect(reg, sidx, ext, isect_lo, isect_hi, owns)
772 ! data presence comes from the file: the writer emits a record only for this rank's own blocks (rm < 0
773 ! otherwise), and an owner need not be the rank whose subdomain holds the block
774 if (rm < 0) then
775 if (owns .and. lvl >= 1) ncoarse = ncoarse + 1 ! refined here, but its data lives in another rank's file
776 cycle
777 end if
778 rr = f_amr_block_ratio(reg, lvl, rm)
779 allocate (sbuf(0:rm,0:rn,0:rp,1:sys_size_io))
780 do i = 1, sys_size_io
781 read (2) sbuf(0:rm,0:rn,0:rp,i)
782 end do
783 if (owns) then
784 call s_amr_block_piece(reg, isect_lo, isect_hi, rr, foff, fcnt)
786 call s_setup_amr_block(amr_num_fine, reg, isect_lo, sidx, fcnt(1) - 1, fcnt(2) - 1, fcnt(3) - 1, rr)
787 do i = 1, sys_size_io
788 amr_fine(amr_num_fine)%q_cons(i)%sf(:,:,:) = sbuf(foff(1):foff(1) + fcnt(1) - 1, &
789 & foff(2):foff(2) + fcnt(2) - 1,foff(3):foff(3) + fcnt(3) - 1,i)
790 end do
791 end if
792 deallocate (sbuf)
793 end do
794 close (2)
795 ! with per-rank files a block owned by another rank cannot be rendered here
796 call s_mpi_allreduce_integer_max(ncoarse, ncoarse_glb)
797 if (proc_rank == 0 .and. ncoarse_glb > 0) print '(A)', &
798 & ' [amr] post: WARNING: with parallel_io = F each rank ' &
799 & // 'reads only its own restart file, and some refined regions belong to a block another rank owns; those ' &
800 & // 'regions are written at coarse resolution. Use parallel_io = T for a complete AMR overlay at np > 1.'
801 else
802#ifdef MFC_MPI
803 call mpi_file_open(mpi_comm_world, file_loc, mpi_mode_rdonly, mpi_info_int, ifile, ierr)
804 call mpi_file_read_at_all(ifile, int(0, mpi_offset_kind), ghdr, amr_restart_ghdr_ints, mpi_integer, status, ierr)
805 call s_amr_restart_check_header(ghdr, sys_size_io, 'amr post', nblk)
806 allocate (amr_fine(nblk))
807 amr_num_fine = 0
808 call s_amr_restart_read_catalog(ifile, nblk, sys_size_io, cat)
809 do k = 1, nblk
810 reg = cat%reg(:,k); lvl = cat%lvl(k)
811 call s_amr_block_isect(reg, sidx, ext, isect_lo, isect_hi, owns)
812 owns = owns .and. cat%own(1, k) > 0 ! a block with no owner has no chunk
813 rr = 1
814 if (owns) rr = f_amr_block_ratio(reg, lvl, cat%own(2, k))
815 cnt8 = 0
816 if (owns) cnt8 = int(sys_size_io, 8)*(cat%own(2, k) + 1)*(cat%own(3, k) + 1)*(cat%own(4, k) + 1)
817 if (cnt8 > huge(cnt)) call s_mpi_abort('amr post: a fine block exceeds the MPI read count limit')
818 cnt = int(cnt8)
819 allocate (buf(max(cnt, 1)))
820 ! collective: every rank calls it, non-participants with count 0
821 call mpi_file_read_at_all(ifile, cat%data_disp(k), buf, cnt*mpi_io_type, mpi_io_p, status, ierr)
822 if (owns) then
823 call s_amr_block_piece(reg, isect_lo, isect_hi, rr, foff, fcnt)
825 call s_setup_amr_block(amr_num_fine, reg, isect_lo, sidx, fcnt(1) - 1, fcnt(2) - 1, fcnt(3) - 1, rr)
826 ! file order: i, fk, fj, fi (fastest) over the full block extent
827 plane = int(cat%own(3, k) + 1, 8)*(cat%own(2, k) + 1)
828 do i = 1, sys_size_io
829 do fk = 0, fcnt(3) - 1
830 do fj = 0, fcnt(2) - 1
831 do fi = 0, fcnt(1) - 1
832 idx = (i - 1)*plane*(cat%own(4, &
833 & k) + 1) + (fk + foff(3))*plane + (fj + foff(2))*int(cat%own(2, k) + 1, &
834 & 8) + (fi + foff(1)) + 1
835 amr_fine(amr_num_fine)%q_cons(i)%sf(fi, fj, fk) = buf(idx)
836 end do
837 end do
838 end do
839 end do
840 end if
841 deallocate (buf)
842 end do
843 call mpi_file_close(ifile, ierr)
844#endif
845 end if
846
847 if (proc_rank == 0) print '(A,I0,A,I0,A)', ' [amr] post: read ', nblk, ' fine block(s); rank 0 keeps ', amr_num_fine, &
848 & ' piece(s)'
849
850 end subroutine s_read_amr_data
851
852 !> Release the stored AMR fine-block pieces.
853 impure subroutine s_free_amr_data()
854
855 integer :: k, i
856
857 if (allocated(amr_fine)) then
858 do k = 1, amr_num_fine
859 if (allocated(amr_fine(k)%q_cons)) then
860 do i = 1, sys_size
861 if (associated(amr_fine(k)%q_cons(i)%sf)) deallocate (amr_fine(k)%q_cons(i)%sf)
862 end do
863 deallocate (amr_fine(k)%q_cons)
864 end if
865 if (allocated(amr_fine(k)%x_cb)) deallocate (amr_fine(k)%x_cb)
866 if (allocated(amr_fine(k)%y_cb)) deallocate (amr_fine(k)%y_cb)
867 if (allocated(amr_fine(k)%z_cb)) deallocate (amr_fine(k)%z_cb)
868 end do
869 deallocate (amr_fine)
870 end if
871 amr_num_fine = 0
872
873 end subroutine s_free_amr_data
874
875end module m_data_input
Subroutine for reading data files.
real(wp), intent(inout) rm
integer, intent(in) k
The AMR fine-level restart file layout shared by m_amr_restart (writer/reader) and post_process's ove...
pure subroutine, public s_amr_subdivide_cb(pcb, pcb_lb, lo, nfine, rr, fcb)
Fine cell boundaries fcb(-1:nfine) by rr-way subdivision of the coarse cells of pcb starting at coars...
character(len=path_len+3 *name_len) function, public f_amr_restart_path(t_step)
The fine-level restart file for time step t_step: one file per rank without parallel_io,...
integer, parameter, public amr_restart_ghdr_ints
global header length (ints)
impure subroutine, public s_amr_restart_check_header(ghdr, nvar, who, nblk, ref_ratio, max_level)
Validate the global header: the rank count (serial files need a match), nvar, the stored word size an...
impure subroutine, public s_amr_restart_read_catalog(ifile, nblk, nvar, cat)
Read every block's header and ownership record from an open parallel_io file (rank 0 reads,...
Noncharacteristic and processor boundary condition application for ghost cells and buffer regions.
Boundary condition restart I/O, capillary/IGR buffer population, and grid-variable buffers.
subroutine s_assign_default_bc_type(bc_type)
Initialize the per-cell boundary condition type arrays with the global default BC values.
subroutine s_read_serial_boundary_condition_files(step_dirpath, bc_type)
Read boundary condition type and buffer data from serial (unformatted) restart files.
subroutine s_read_parallel_boundary_condition_files(bc_type, strict)
Read boundary condition type and buffer data from per-rank parallel files using MPI I/O.
Platform-specific file and directory operations: create, delete, inquire, getcwd, and basename.
impure subroutine my_inquire(fileloc, dircheck)
Inquire on the existence of a directory or file.
Reads raw simulation grid and conservative-variable data for a given time-step and fills buffer regio...
impure subroutine s_allocate_field_arrays(local_start_idx, end_x, end_y, end_z)
Helper subroutine to allocate field arrays for given dimensionality.
pure subroutine s_amr_block_piece(reg, isect_lo, isect_hi, rr, foff, fcnt)
The kept sub-box of a block: fine-cell offset foff from the block origin and count fcnt of the inters...
type(amr_fine_block), dimension(:), allocatable, public amr_fine
this rank's owned block pieces
impure subroutine, public s_read_parallel_data_files(t_step)
Parallel-read the raw data files present in the corresponding time-step directory and to populate the...
pure subroutine s_amr_block_isect(reg, sidx, ext, isect_lo, isect_hi, owns)
Intersection of the block region reg with this rank's subdomain (sidx, ext); owns = non-empty in ever...
type(scalar_field), public q_t_sf
Temperature field.
type(scalar_field), dimension(:), allocatable, public q_cons_vf
Conservative variables.
impure subroutine s_read_parallel_conservative_data(t_step, m_mok, n_mok, p_mok, wp_mok, mok, str_mok, nvars_mok)
Helper subroutine to read parallel conservative variable data.
impure subroutine, public s_initialize_data_input_module
Computation of parameters, allocation procedures, and/or any other tasks needed to properly setup the...
type(scalar_field), dimension(:), allocatable, public q_prim_vf
Primitive variables.
impure subroutine s_setup_mpi_io_params(data_size, m_mok, n_mok, p_mok, wp_mok, mok, str_mok, nvars_mok)
Helper subroutine to setup MPI data I/O parameters.
impure logical function, public f_save_exists(t_step)
Does a saved restart exist for this index? Under cfl_dt a step can cross two t_save boundaries,...
impure subroutine, public s_finalize_data_input_module
Deallocation procedures for the module.
impure subroutine, public s_read_amr_data(t_step)
Read the AMR fine-level restart file for t_step (layout: m_amr_restart_io) and keep this rank's piece...
impure subroutine s_read_grid_data_direction(t_step_dir, direction, cb_array, d_array, cc_array, size_dim)
Helper subroutine to read grid data files for a given direction.
impure subroutine, public s_read_serial_data_files(t_step)
Read the raw data files present in the corresponding time-step directory and to populate the associat...
type(scalar_field), dimension(:), allocatable, public q_cons_temp
impure subroutine, public s_free_amr_data()
Release the stored AMR fine-block pieces.
impure subroutine s_setup_amr_block(k, reg, isect_lo, sidx, fm, fn, fp, rr)
Populate block slot k metadata, allocate its conservative fields, and reconstruct its fine coordinate...
procedure(s_read_abstract_data_files), pointer, public s_read_data_files
impure subroutine s_amr_local_domain(sidx, ext)
This rank's subdomain in global coarse cells: origin sidx (0 for a single rank or no MPI) and extent ...
type(integer_field), dimension(:,:), allocatable, public bc_type
Boundary condition identifiers.
type(integer_field), public ib_markers
impure integer function f_amr_block_ratio(reg, lvl, fmf)
Refinement factor of a block from its level and fine x-extent fmf (cells - 1); aborts on an inconsist...
impure subroutine s_read_ib_data_files(file_loc_base, t_step)
Helper subroutine to read IB data files.
integer, public amr_num_fine
number of block pieces this rank owns (<= file's block count)
Shared derived types for field data, patch geometry, bubble dynamics, and MPI I/O structures.
Global parameters for the post-process: domain geometry, equation of state, and output database setti...
type(int_bounds_info) offset_y
real(wp), dimension(:), allocatable y_cc
integer proc_rank
Rank of the local processor.
type(mpi_io_ib_var), public mpi_io_ib_data
real(wp), dimension(:), allocatable y_cb
real(wp), dimension(:), allocatable dz
integer buff_size
Number of ghost cells for boundary condition storage.
real(wp), dimension(:), allocatable z_cb
real(wp), dimension(:), allocatable x_cc
real(wp), dimension(:), allocatable x_cb
real(wp), dimension(:), allocatable dy
type(int_bounds_info) offset_x
integer sys_size_io
Variables per restart/BC record: sys_size less the post-only beta slot.
real(wp), dimension(:), allocatable z_cc
type(int_bounds_info) offset_z
type(mpi_io_var), public mpi_io_data
real(wp), dimension(:), allocatable dx
Cell-width distributions in the x-, y- and z-coordinate directions.
Basic floating-point utilities: approximate equality, default detection, and coordinate bounds.
pure integer function, public f_save_index(t_step, cfl_mode, t_step_save)
Index of the save written at t_step: t_step itself under CFL-based stepping (the caller passes the sa...
Utility routines for bubble model setup, coordinate transforms, array sampling, and special functions...
elemental subroutine, public s_int_to_str(i, res)
Convert an integer to its trimmed string representation.
MPI communication layer: domain decomposition, halo exchange, reductions, and parallel I/O setup.
impure subroutine s_mpi_abort(prnt, code)
The subroutine terminates the MPI execution environment.
impure subroutine s_mpi_barrier
Halts all processes until all have reached barrier.
impure subroutine s_mpi_allreduce_integer_min(var_loc, var_glb)
Reduce a local integer value to its global minimum across all MPI ranks.
impure subroutine s_check_mpi_file_open(ierr, file_loc)
Abort naming the file if MPI_FILE_OPEN failed. File errors default to MPI_ERRORS_RETURN,...
subroutine s_apply_grid_from_global_dim(x_cb_glb, m_dim_glb, m_dim, sidx, bc_beg, bc_end, cb_lo, cb_hi, cw_lo, cw_hi, x_cb_loc, x_cc_loc, dx_loc)
Populate the local cell-boundary, cell-center, and cell-width arrays in one direction directly from t...
impure subroutine s_initialize_mpi_data(q_cons_vf, ib_markers, ib_mpi_data, beta, qbmm_pb, qbmm_mv)
Set up MPI I/O data views and variable pointers for parallel file output.
subroutine s_initialize_mpi_data_ds(m_ds, n_ds, p_ds, q_cons_vf)
Set up MPI I/O data views for downsampled (coarsened) parallel file output.
impure subroutine s_mpi_allreduce_integer_max(var_loc, var_glb)
Reduce a local integer value to its global maximum across all MPI ranks.
MPI gather and scatter operations for distributing post-process grid and flow-variable data.
Every block's header, ownership record and data-chunk byte offset, read collectively by all ranks.
One AMR fine-block piece owned by this rank, held for visualization overlay of the refined solution.
Derived type annexing an integer scalar field (SF).
Derived type annexing a scalar field (SF).