Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
vtkhdf_file.F90
Go to the documentation of this file.
1! Copyright (c) 2026, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
33!> VTKHDF file format
35 use num_types, only : rp, sp, dp, qp, i8
37 use checkpoint, only : chkp_t
40 use mesh, only : mesh_t
41 use field, only : field_t
42 use field_list, only : field_list_t
44 use dofmap, only : dofmap_t
45 use logger, only : neko_log
46 use comm, only : pe_rank, pe_size, neko_comm
48 use mpi_f08, only : mpi_info_null, mpi_allreduce, mpi_allgather, &
49 mpi_in_place, mpi_integer8, mpi_sum, mpi_max, mpi_comm_size, &
50 mpi_exscan, mpi_barrier, mpi_bcast, mpi_logical
51 use vtk, only : vtk_ordering
53#ifdef HAVE_HDF5
54 use hdf5, only : &
55 hid_t, hsize_t, size_t, &
56 h5fcreate_f, h5fopen_f, h5fclose_f, h5fflush_f, h5fget_obj_count_f, &
57 h5f_obj_all_f, h5f_scope_global_f, &
58 h5gcreate_f, h5gopen_f, h5gclose_f, &
59 h5acreate_f, h5aopen_f, h5awrite_f, h5aread_f, h5aclose_f, h5aexists_f, &
60 h5aopen_by_name_f, &
61 h5dcreate_f, h5dopen_f, h5dwrite_f, h5dread_f, h5dclose_f, &
62 h5dget_create_plist_f, &
63 h5tcopy_f, h5tclose_f, h5tset_strpad_f, h5tset_size_f, &
64 h5screate_f, h5screate_simple_f, h5sclose_f, &
65 h5sselect_hyperslab_f, h5sselect_all_f, h5sget_simple_extent_dims_f, &
66 h5dget_space_f, h5dset_extent_f, &
67 h5pcreate_f, h5pclose_f, h5pset_fapl_mpio_f, h5pset_dxpl_mpio_f, &
68 h5pget_virtual_count_f, h5pget_virtual_filename_f, h5pget_layout_f, &
69 h5lexists_f, h5ldelete_f, &
70 h5p_file_access_f, h5p_dataset_xfer_f, h5p_dataset_create_f, &
71 h5f_acc_trunc_f, h5f_acc_rdwr_f, h5f_acc_rdonly_f, h5pset_chunk_f, &
72 h5t_std_u8le, h5t_native_integer, h5t_fortran_s1, h5t_str_nullterm_f, &
73 h5kind_to_type, h5_real_kind, h5_integer_kind, h5pset_virtual_f, &
74 h5s_scalar_f, h5s_select_set_f, h5fd_mpio_collective_f, h5p_default_f, &
75 h5s_unlimited_f, h5d_virtual_f
76#endif
77 implicit none
78 private
79
80 !> Interface for HDF5 files
81 type, public, extends(generic_file_t) :: vtkhdf_file_t
82 logical :: amr_enabled = .false.
83 logical :: subdivide = .false.
84 logical :: enable_vds = .false.
85 integer :: precision = -1
86 contains
87 procedure :: get_vtkhdf_fname => vtkhdf_file_get_fname
88 procedure :: get_next_output_fname => vtkhdf_file_get_next_output_fname
89 procedure :: read => vtkhdf_file_read
90 procedure :: write => vtkhdf_file_write
91 procedure :: set_overwrite => vtkhdf_file_set_overwrite
92 procedure :: enable_amr => vtkhdf_file_enable_amr
93 procedure :: set_precision => vtkhdf_file_set_precision
94 procedure :: set_subdivide => vtkhdf_file_set_subdivide
95 end type vtkhdf_file_t
96
97 integer, dimension(2), parameter :: vtkhdf_version = [2, 6]
98
99contains
100
101 ! -------------------------------------------------------------------------- !
102 ! Well defined subroutines
103
104 !> Set the overwrite flag for HDF5 files
105 subroutine vtkhdf_file_set_overwrite(this, overwrite)
106 class(vtkhdf_file_t), intent(inout) :: this
107 logical, intent(in) :: overwrite
108 this%overwrite = overwrite
109 end subroutine vtkhdf_file_set_overwrite
110
111 !> Enable support for Adaptive Mesh Refinement
112 subroutine vtkhdf_file_enable_amr(this)
113 class(vtkhdf_file_t), intent(inout) :: this
114 this%amr_enabled = .false.
115 end subroutine vtkhdf_file_enable_amr
116
117 !> Set the precision for VTKHDF output (single or double)
118 subroutine vtkhdf_file_set_precision(this, precision)
119 class(vtkhdf_file_t), intent(inout) :: this
120 integer, intent(in) :: precision
121 this%precision = precision
122 end subroutine vtkhdf_file_set_precision
123
124 !> Return the file name with the start counter.
125 function vtkhdf_file_get_fname(this) result(base_fname)
126 class(vtkhdf_file_t), intent(in) :: this
127 character(len=1024) :: base_fname
128 character(len=1024) :: fname
129 character(len=1024) :: path, name, suffix
130
131 fname = trim(this%get_base_fname())
132 call filename_split(fname, path, name, suffix)
133
134 write(base_fname, '(A,A,"_",I0,A)') &
135 trim(path), trim(name), this%get_start_counter(), trim(suffix)
136
137 end function vtkhdf_file_get_fname
138
139 !> Get the physical file name generated by the next write.
140 function vtkhdf_file_get_next_output_fname(this) result(fname)
141 class(vtkhdf_file_t), intent(in) :: this
142 character(len=1024) :: fname
143
144 fname = this%get_vtkhdf_fname()
145
147
148 !> Enable or disable subdivision of spectral elements into linear sub-cells.
149 !! When subdivision is enabled, each spectral element is written as multiple
150 !! linear VTK cells (VTK_HEXAHEDRON in 3D, VTK_QUAD in 2D) with connectivity
151 !! corresponding to the tensor-product grid of the spectral element.
152 !! @param subdivide Whether to subdivide into linear sub-cells.
153 subroutine vtkhdf_file_set_subdivide(this, subdivide)
154 class(vtkhdf_file_t), intent(inout) :: this
155 logical, intent(in) :: subdivide
156 this%subdivide = subdivide
157 end subroutine vtkhdf_file_set_subdivide
158
159#ifdef HAVE_HDF5
160 ! -------------------------------------------------------------------------- !
161 ! HDF5 Required subroutines
162
163 !> Write data in HDF5 format following official VTKHDF UnstructuredGrid
164 !! specification
165 subroutine vtkhdf_file_write(this, data, t)
166 class(vtkhdf_file_t), intent(inout) :: this
167 class(*), target, intent(in) :: data
168 real(kind=dp), intent(in), optional :: t
169 type(mesh_t), pointer :: msh
170 type(dofmap_t), pointer :: dof
171 type(field_list_t) :: fields
172 integer :: ierr, mpi_info, mpi_comm, i, n_fields
173 integer(hid_t) :: plist_id, file_id, attr_id, vtkhdf_grp
174 integer(hid_t) :: filespace, H5T_NEKO_STRING
175 integer(hsize_t), dimension(1) :: vdims
176 integer(size_t) :: type_len
177 integer :: lx, ly, lz
178 integer :: local_points, local_cells, local_conn
179 integer, allocatable :: part_points(:), part_cells(:), part_conns(:)
180 character(len=1024) :: fname
181 character(len=16) :: type_str
182 logical :: exists
183 integer :: counter
184
185 ! Determine mesh and field data
186 select type(data)
187 type is (field_t)
188 msh => data%msh
189 dof => data%dof
190 n_fields = 1
191 call fields%init(1)
192 ! Call the specific bindings; resolving the assign generic here
193 ! requires matching a TARGET actual to a POINTER, INTENT(IN) dummy
194 ! (F2008), which the Fujitsu compiler rejects
195 call fields%assign_to_field(1, data)
196 type is (field_list_t)
197 msh => data%msh(1)
198 dof => data%dof(1)
199 call fields%assign_to_list(data)
200 class default
201 call neko_error('Invalid data type for vtkhdf_file_write')
202 end select
203
204 ! Check conditions to ensure the input data is supported.
205 if (.not. associated(msh)) then
206 call neko_error('Mesh must be associated for vtkhdf_file_write')
207 end if
208 if (dof%Xh%lx .lt. 2 .or. dof%Xh%ly .lt. 2) then
209 call neko_error('VTKHDF linear output requires lx, ly >= 2')
210 end if
211 if (msh%gdim .eq. 3 .and. dof%Xh%lz .lt. 2) then
212 call neko_error('VTKHDF linear output requires lz >= 2 in 3D')
213 end if
214 if (msh%gdim .lt. 2 .or. msh%gdim .gt. 3) then
215 call neko_error('VTKHDF output only supports 2D and 3D meshes')
216 end if
217
218 ! Ensure precision is set and are valid.
219 if (this%precision .gt. rp) then
220 this%precision = rp
221 call neko_warning('Requested precision is higher than working precision')
222 else if (this%precision .eq. -1) then
223 this%precision = rp
224 end if
225
226 call this%increment_counter()
227 fname = trim(this%get_vtkhdf_fname())
228 counter = this%get_counter() - this%get_start_counter()
229
230 mpi_info = mpi_info_null%mpi_val
231 mpi_comm = neko_comm%mpi_val
232
233 call hdf5_session_init()
234 call h5pcreate_f(h5p_file_access_f, plist_id, ierr)
235 call h5pset_fapl_mpio_f(plist_id, mpi_comm, mpi_info, ierr)
236
237 if (counter .eq. 0) then
238 ! First write: always create a fresh file to avoid stale data
239 call h5fcreate_f(fname, h5f_acc_trunc_f, &
240 file_id, ierr, access_prp = plist_id)
241 else
242 call h5fopen_f(fname, h5f_acc_rdwr_f, file_id, ierr, &
243 access_prp = plist_id)
244 end if
245
246 ! Create/open VTKHDF root group with vtkhdf_version and type attributes
247 call h5lexists_f(file_id, "VTKHDF", exists, ierr)
248 if (exists) then
249 call h5gopen_f(file_id, "VTKHDF", vtkhdf_grp, ierr)
250 else
251 call h5gcreate_f(file_id, "VTKHDF", vtkhdf_grp, ierr)
252
253 ! Write Version attribute
254 vdims = 2_hsize_t
255 call h5screate_simple_f(1, vdims, filespace, ierr)
256 call h5acreate_f(vtkhdf_grp, "Version", h5t_native_integer, filespace, &
257 attr_id, ierr)
258 call h5awrite_f(attr_id, h5t_native_integer, vtkhdf_version, vdims, ierr)
259 call h5aclose_f(attr_id, ierr)
260 call h5sclose_f(filespace, ierr)
261
262 ! Write Type attribute "UnstructuredGrid" as a fixed-length string
263 type_str = "UnstructuredGrid"
264 type_len = int(len_trim(type_str), kind=size_t)
265 vdims = 1_hsize_t
266 call h5screate_f(h5s_scalar_f, filespace, ierr)
267
268 call h5tcopy_f(h5t_fortran_s1, h5t_neko_string, ierr)
269 call h5tset_size_f(h5t_neko_string, type_len, ierr)
270 call h5tset_strpad_f(h5t_neko_string, h5t_str_nullterm_f, ierr)
271
272 call h5acreate_f(vtkhdf_grp, "Type", h5t_neko_string, filespace, &
273 attr_id, ierr)
274 call h5awrite_f(attr_id, h5t_neko_string, [type_str], vdims, ierr)
275 call h5aclose_f(attr_id, ierr)
276
277 call h5tclose_f(h5t_neko_string, ierr)
278 call h5sclose_f(filespace, ierr)
279 end if
280
281 if (present(t)) then
282 call vtkhdf_write_steps(vtkhdf_grp, counter, t)
283 end if
284
285 if (associated(msh)) then
286 call vtkhdf_write_mesh(vtkhdf_grp, dof, msh, &
287 this%amr_enabled, counter, this%subdivide, t)
288 end if
289
290 ! Write field data in PointData group
291 if (fields%size() .gt. 0) then
292 call vtkhdf_write_pointdata(vtkhdf_grp, fields, this%precision, &
293 counter, fname, t)
294 end if
295
296 call h5gclose_f(vtkhdf_grp, ierr)
297 call h5pclose_f(plist_id, ierr)
298
299 ! A leaked id defers the real (weak) close indefinitely, so the
300 ! metadata cache would never reach disk; flush explicitly and warn
301 ! if anything besides the file itself is still open.
302 block
303 integer(size_t) :: obj_count
304 character(len=80) :: wrn_buf
305 call h5fget_obj_count_f(file_id, h5f_obj_all_f, obj_count, ierr)
306 if (obj_count .gt. 1_size_t .and. pe_rank .eq. 0) then
307 write(wrn_buf, '(A,I0,A)') 'VTKHDF: ', obj_count - 1, &
308 ' HDF5 id(s) still open at file close'
309 call neko_warning(trim(wrn_buf))
310 end if
311 end block
312 call h5fflush_f(file_id, h5f_scope_global_f, ierr)
313 call h5fclose_f(file_id, ierr)
315
316 call fields%free()
317
318 end subroutine vtkhdf_file_write
319
320 ! -------------------------------------------------------------------------- !
321 ! Internal helper subroutines for VTKHDF writing
322
323 !> Write mesh geometry datasets to the VTKHDF group.
324 !! Writes NumberOfPoints, NumberOfCells, NumberOfConnectivityIds,
325 !! Points, Connectivity, Offsets, and Types datasets.
326 !! @param vtkhdf_grp HDF5 group ID for VTKHDF root group
327 !! @param dof Dofmap for coordinate data
328 !! @param msh Mesh object
329 !! @param amr AMR flag to determine if mesh should be rewritten at every time
330 !! step
331 !! @param t Optional time value for time-dependent mesh output (e.g. for AMR)
332 subroutine vtkhdf_write_mesh(vtkhdf_grp, dof, msh, amr, counter, subdivide, t)
333 type(dofmap_t), intent(in) :: dof
334 type(mesh_t), intent(in) :: msh
335 integer(hid_t), intent(in) :: vtkhdf_grp
336 logical, intent(in) :: amr
337 integer, intent(in) :: counter
338 logical, intent(in) :: subdivide
339 real(kind=dp), intent(in), optional :: t
340
341 integer(kind=1) :: VTK_cell_type
342 integer :: ierr, i, ii, jj, kk, el, local_idx
343 integer :: lx, ly, lz, npts_per_cell, nodes_per_cell, cells_per_element
344 integer :: local_points, local_cells, local_conn
345 integer(kind=i8) :: total_points, total_cells, total_conn
346 integer(kind=i8) :: point_offset, max_local_points
347 integer(kind=i8) :: total_offsets, cell_offset, conn_offset, offsets_offset
348 integer(kind=i8) :: max_local_cells, max_local_conn
349 integer(hid_t) :: xf_id, dset_id, dcpl_id, grp_id, attr_id
350 integer(hid_t) :: filespace, memspace, H5T_NEKO_DOUBLE
351 integer(hsize_t), dimension(1) :: dcount, vdims, maxdims, doffset, chunkdims
352 integer(hsize_t), dimension(2) :: dcount2, vdims2, maxdims2, doffset2
353 integer(kind=i8) :: i8_value
354 logical :: exists
355 integer(kind=i8), dimension(3) :: component_sizes
356 integer(kind=i8), dimension(3) :: component_offsets
357 integer(kind=i8), dimension(3) :: component_max_sizes
358
359 lx = dof%Xh%lx
360 ly = dof%Xh%ly
361 lz = dof%Xh%lz
362
363 if (subdivide .and. msh%gdim .eq. 3) then
364 vtk_cell_type = int(12, kind=1) ! VTK_HEXAHEDRON
365 cells_per_element = (lx - 1) * (ly - 1) * (lz - 1)
366 nodes_per_cell = 8
367 else if (subdivide .and. msh%gdim .eq. 2) then
368 vtk_cell_type = int(9, kind=1) ! VTK_QUAD
369 cells_per_element = (lx - 1) * (ly - 1)
370 nodes_per_cell = 4
371 else if (msh%gdim .eq. 3) then
372 vtk_cell_type = int(72, kind=1) ! VTK_LAGRANGE_HEXAHEDRON
373 cells_per_element = 1
374 nodes_per_cell = lx * ly * lz
375 else if (msh%gdim .eq. 2) then
376 vtk_cell_type = int(70, kind=1) ! VTK_LAGRANGE_QUADRILATERAL
377 cells_per_element = 1
378 nodes_per_cell = lx * ly
379 end if
380
381 ! --- Build the number of cells and the connectivity
382 local_points = dof%size()
383 local_cells = msh%nelv * cells_per_element
384
385 ! Connectivity and Offsets hold part-local values, stored as default
386 ! integers, so unlike the global totals these must fit one on each rank.
387 if (int(local_cells, i8) * int(nodes_per_cell, i8) .gt. &
388 int(huge(local_conn), i8)) then
389 call neko_error('VTKHDF: connectivity of a rank exceeds the range ' // &
390 'of a default integer')
391 end if
392 local_conn = local_cells * nodes_per_cell
393
394 total_points = dof%global_size()
395 total_cells = int(msh%glb_nelv, i8) * int(cells_per_element, i8)
396 total_conn = total_cells * int(nodes_per_cell, i8)
397
398 component_sizes = [int(local_points, i8), int(local_cells, i8), &
399 int(local_conn, i8)]
400 component_offsets = 0_i8
401 component_max_sizes = 0_i8
402
403 call mpi_exscan(component_sizes, component_offsets, 3, mpi_integer8, &
404 mpi_sum, neko_comm, ierr)
405 call mpi_allreduce(component_sizes, component_max_sizes, 3, mpi_integer8, &
406 mpi_max, neko_comm, ierr)
407
408 point_offset = component_offsets(1)
409 cell_offset = component_offsets(2)
410 conn_offset = component_offsets(3)
411 max_local_points = component_max_sizes(1)
412 max_local_cells = component_max_sizes(2)
413 max_local_conn = component_max_sizes(3)
414
415 offsets_offset = cell_offset + pe_rank
416 total_offsets = total_cells + pe_size
417
418 ! Create collective transfer property list
419 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
420 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
421
422 ! --- NumberOfPoints, NumberOfCells, NumberOfConnectivityIds ---
423 ! These datasets must accumulate nPieces entries per timestep,
424 ! giving a total size of nSteps * nPieces. VTK's reader computes
425 ! numberOfPieces = dims[0] / nSteps, so missing entries cause
426 ! garbage reads.
427 block
428 integer(hsize_t), dimension(1) :: nof_dims, nof_maxdims
429 integer(hsize_t), dimension(1) :: nof_count, nof_offset, nof_chunk
430 integer(hid_t) :: nof_filespace, nof_memspace, nof_dcpl
431 integer(kind=i8) :: nof_values(3)
432
433 nof_count(1) = 1_hsize_t
434 nof_offset(1) = int(counter, hsize_t) * int(pe_size, hsize_t) &
435 + int(pe_rank, hsize_t)
436 nof_chunk(1) = max(1_hsize_t, int(pe_size, hsize_t))
437 nof_values = [int(local_points, kind=i8), int(local_cells, kind=i8), &
438 int(local_conn, kind=i8)]
439
440 call h5pcreate_f(h5p_dataset_create_f, nof_dcpl, ierr)
441 call h5pset_chunk_f(nof_dcpl, 1, nof_chunk, ierr)
442
443 call vtkhdf_write_numberof(vtkhdf_grp, "NumberOfPoints", &
444 nof_values(1), nof_offset, nof_count, nof_dcpl, &
445 counter, xf_id, ierr)
446 call vtkhdf_write_numberof(vtkhdf_grp, "NumberOfCells", &
447 nof_values(2), nof_offset, nof_count, nof_dcpl, &
448 counter, xf_id, ierr)
449 call vtkhdf_write_numberof(vtkhdf_grp, "NumberOfConnectivityIds", &
450 nof_values(3), nof_offset, nof_count, nof_dcpl, &
451 counter, xf_id, ierr)
452
453 call h5pclose_f(nof_dcpl, ierr)
454 end block
455
456 ! --- Points dataset (global coordinates) ---
457 call h5lexists_f(vtkhdf_grp, "Points", exists, ierr)
458 if (.not. exists) then
459
460 vdims2 = [3_hsize_t, int(total_points, hsize_t)]
461 maxdims2 = [3_hsize_t, h5s_unlimited_f]
462 chunkdims(1) = vtkhdf_chunk_extent(max_local_points, total_points, &
463 3 * storage_size(1.0_dp) / 8)
464 dcount2 = [3_hsize_t, int(local_points, hsize_t)]
465 doffset2 = [0_hsize_t, int(point_offset, hsize_t)]
466 h5t_neko_double = h5kind_to_type(dp, h5_real_kind)
467
468 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
469 call h5screate_simple_f(2, dcount2, memspace, ierr)
470 call h5screate_simple_f(2, vdims2, filespace, ierr, maxdims2)
471
472 call h5pset_chunk_f(dcpl_id, 2, [3_hsize_t, chunkdims(1)], ierr)
473 call h5dcreate_f(vtkhdf_grp, "Points", h5t_neko_double, &
474 filespace, dset_id, ierr, dcpl_id = dcpl_id)
475 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
476 doffset2, dcount2, ierr)
477
478 block
479 real(kind=dp), allocatable :: coords(:,:)
480
481 allocate(coords(3, local_points))
482 do concurrent(local_idx = 1:local_points)
483 block
484 integer :: idx(4)
485 real(kind=dp) :: x, y, z
486 idx = nonlinear_index(local_idx, lx, ly, lz)
487
488 x = real(dof%x%x(idx(1), idx(2), idx(3), idx(4)), dp)
489 y = real(dof%y%x(idx(1), idx(2), idx(3), idx(4)), dp)
490 z = real(dof%z%x(idx(1), idx(2), idx(3), idx(4)), dp)
491
492 coords(1, local_idx) = x
493 coords(2, local_idx) = y
494 coords(3, local_idx) = z
495 end block
496 end do
497 call h5dwrite_f(dset_id, h5t_neko_double, coords, dcount2, ierr, &
498 file_space_id = filespace, mem_space_id = memspace, &
499 xfer_prp = xf_id)
500 deallocate(coords)
501 end block
502
503 call h5dclose_f(dset_id, ierr)
504 call h5sclose_f(filespace, ierr)
505 call h5sclose_f(memspace, ierr)
506 call h5pclose_f(dcpl_id, ierr)
507 end if
508
509 ! --- Connectivity dataset ---
510 call h5lexists_f(vtkhdf_grp, "Connectivity", exists, ierr)
511 if (exists) call h5ldelete_f(vtkhdf_grp, "Connectivity", ierr)
512
513 vdims = int(total_conn, hsize_t)
514 maxdims = h5s_unlimited_f
515 chunkdims = vtkhdf_chunk_extent(max_local_conn, total_conn, &
516 storage_size(local_conn) / 8)
517 dcount = int(local_conn, hsize_t)
518 doffset = int(conn_offset, hsize_t)
519
520 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
521 call h5screate_simple_f(1, dcount, memspace, ierr)
522 call h5screate_simple_f(1, vdims, filespace, ierr, maxdims)
523
524 call h5pset_chunk_f(dcpl_id, 1, chunkdims, ierr)
525 call h5dcreate_f(vtkhdf_grp, "Connectivity", h5t_native_integer, &
526 filespace, dset_id, ierr, dcpl_id = dcpl_id)
527 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
528 doffset, dcount, ierr)
529
530 block
531 integer, allocatable :: connectivity(:)
532
533 allocate(connectivity(local_conn))
534 call vtkhdf_build_connectivity(connectivity, vtk_cell_type, msh, dof, &
535 subdivide)
536 call h5dwrite_f(dset_id, h5t_native_integer, connectivity, dcount, &
537 ierr, file_space_id = filespace, mem_space_id = memspace, &
538 xfer_prp = xf_id)
539 deallocate(connectivity)
540 end block
541
542 call h5dclose_f(dset_id, ierr)
543 call h5sclose_f(filespace, ierr)
544 call h5sclose_f(memspace, ierr)
545 call h5pclose_f(dcpl_id, ierr)
546
547 ! --- Offsets dataset ---
548 call h5lexists_f(vtkhdf_grp, "Offsets", exists, ierr)
549 if (exists) call h5ldelete_f(vtkhdf_grp, "Offsets", ierr)
550
551 vdims = int(total_offsets, hsize_t)
552 maxdims = h5s_unlimited_f
553 chunkdims = vtkhdf_chunk_extent(max_local_cells + 1_i8, total_offsets, &
554 storage_size(local_conn) / 8)
555 dcount = int(local_cells + 1, hsize_t)
556 doffset = int(offsets_offset, hsize_t)
557
558 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
559 call h5screate_simple_f(1, dcount, memspace, ierr)
560 call h5screate_simple_f(1, vdims, filespace, ierr, maxdims)
561
562 call h5pset_chunk_f(dcpl_id, 1, chunkdims, ierr)
563 call h5dcreate_f(vtkhdf_grp, "Offsets", h5t_native_integer, &
564 filespace, dset_id, ierr, dcpl_id = dcpl_id)
565 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
566 doffset, dcount, ierr)
567
568 block
569 integer, allocatable :: offsets(:)
570
571 allocate(offsets(local_cells + 1))
572 do concurrent(i = 1:local_cells)
573 offsets(i) = (i - 1) * nodes_per_cell
574 end do
575 offsets(local_cells + 1) = local_conn
576 call h5dwrite_f(dset_id, h5t_native_integer, offsets, dcount, ierr, &
577 file_space_id = filespace, mem_space_id = memspace, xfer_prp = xf_id)
578 deallocate(offsets)
579 end block
580
581 call h5dclose_f(dset_id, ierr)
582 call h5sclose_f(filespace, ierr)
583 call h5sclose_f(memspace, ierr)
584 call h5pclose_f(dcpl_id, ierr)
585
586 ! --- Types dataset (VTK cell types) ---
587 call h5lexists_f(vtkhdf_grp, "Types", exists, ierr)
588 if (exists) call h5ldelete_f(vtkhdf_grp, "Types", ierr)
589
590 vdims = int(total_cells, hsize_t)
591 maxdims = h5s_unlimited_f
592 chunkdims = vtkhdf_chunk_extent(max_local_cells, total_cells, &
593 storage_size(vtk_cell_type) / 8)
594 dcount = int(local_cells, hsize_t)
595 doffset = int(cell_offset, hsize_t)
596
597 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
598 call h5screate_simple_f(1, dcount, memspace, ierr)
599 call h5screate_simple_f(1, vdims, filespace, ierr, maxdims)
600
601 call h5pset_chunk_f(dcpl_id, 1, chunkdims, ierr)
602 call h5dcreate_f(vtkhdf_grp, "Types", h5t_std_u8le, &
603 filespace, dset_id, ierr, dcpl_id = dcpl_id)
604 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
605 doffset, dcount, ierr)
606
607 block
608 integer(kind=1), allocatable :: cell_types(:)
609 allocate(cell_types(local_cells), source=vtk_cell_type)
610 call h5dwrite_f(dset_id, h5t_std_u8le, cell_types, dcount, ierr, &
611 file_space_id = filespace, mem_space_id = memspace, xfer_prp = xf_id)
612 deallocate(cell_types)
613 end block
614
615 call h5dclose_f(dset_id, ierr)
616 call h5sclose_f(filespace, ierr)
617 call h5sclose_f(memspace, ierr)
618 call h5pclose_f(dcpl_id, ierr)
619
620 if (present(t)) then
621 ! Open Steps group
622 call h5gopen_f(vtkhdf_grp, "Steps", grp_id, ierr)
623
624 ! --- NumberOfParts ---
625 call vtkhdf_write_i8_at(grp_id, "NumberOfParts", int(pe_size, kind=i8), &
626 counter)
627
628 ! --- PartOffsets ---
629 i8_value = 0_i8
630 if (amr) then
631 i8_value = int(counter - 1, kind=i8) * int(pe_size, kind=i8)
632 call vtkhdf_write_i8_at(grp_id, "PartOffsets", i8_value, counter)
633
634 i8_value = int(counter - 1, kind=i8) * int(total_points, kind=i8)
635 call vtkhdf_write_i8_at(grp_id, "PointOffsets", i8_value, counter)
636
637 i8_value = int(counter - 1, kind=i8) * int(total_cells, kind=i8)
638 call vtkhdf_write_i8_at(grp_id, "CellOffsets", i8_value, counter)
639
640 i8_value = int(counter - 1, kind=i8) * int(total_conn, kind=i8)
641 call vtkhdf_write_i8_at(grp_id, "ConnectivityIdOffsets", i8_value, &
642 counter)
643
644 else
645 i8_value = 0_i8
646 call vtkhdf_write_i8_at(grp_id, "PartOffsets", i8_value, counter)
647 call vtkhdf_write_i8_at(grp_id, "PointOffsets", i8_value, counter)
648 call vtkhdf_write_i8_at(grp_id, "CellOffsets", i8_value, counter)
649 call vtkhdf_write_i8_at(grp_id, "ConnectivityIdOffsets", i8_value, &
650 counter)
651 end if
652
653 call h5gclose_f(grp_id, ierr)
654 end if
655
656 call h5pclose_f(xf_id, ierr)
657
658 end subroutine vtkhdf_write_mesh
659
660 !> Write temporal Steps group metadata to the VTKHDF group.
661 !! Writes Values, NumberOfParts, PartOffsets, PointOffsets,
662 !! CellOffsets, ConnectivityIdOffsets datasets, and NSteps attribute.
663 !! @param vtkhdf_grp HDF5 group ID for VTKHDF root group
664 !! @param t Current simulation time
665 !! @param counter Current counter for how many steps have been written.
666 subroutine vtkhdf_write_steps(vtkhdf_grp, counter, t)
667 integer(hid_t), intent(in) :: vtkhdf_grp
668 integer, intent(in) :: counter
669 real(kind=dp), intent(in) :: t
670
671 integer(hid_t) :: xf_id, H5T_NEKO_DOUBLE
672 integer :: ierr
673 integer(hid_t) :: grp_id, dset_id, dcpl_id, filespace, memspace, attr_id
674 integer(hsize_t), dimension(1) :: step_dims, step_maxdims
675 integer(hsize_t), dimension(1) :: step_count, step_offset, chunkdims, ddim
676 real(kind=dp), dimension(1) :: time_value
677 integer(kind=i8) :: i8_value
678 logical :: exists, attr_exists
679
680 ! Create collective transfer property list
681 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
682 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
683 h5t_neko_double = h5kind_to_type(dp, h5_real_kind)
684
685 ! Create or open Steps group
686 call h5lexists_f(vtkhdf_grp, "Steps", exists, ierr)
687 if (exists) then
688 call h5gopen_f(vtkhdf_grp, "Steps", grp_id, ierr)
689 else
690 call h5gcreate_f(vtkhdf_grp, "Steps", grp_id, ierr)
691 end if
692
693 ! --- Values dataset (time values, real type) ---
694 call h5lexists_f(grp_id, "Values", exists, ierr)
695 if (exists) then
696 call h5dopen_f(grp_id, "Values", dset_id, ierr)
697 call h5dget_space_f(dset_id, filespace, ierr)
698 call h5sget_simple_extent_dims_f(filespace, step_dims, step_maxdims, &
699 ierr)
700 call h5sclose_f(filespace, ierr)
701
702 ! We have not written this timestep yet, expand the array
703 if (step_dims(1) .eq. int(counter, hsize_t)) then
704 step_dims(1) = int(counter + 1, hsize_t)
705 call h5dset_extent_f(dset_id, step_dims, ierr)
706 else if (step_dims(1) .lt. int(counter, hsize_t)) then
707 call neko_error("VTKHDF: Time steps written out of order.")
708 end if
709 else
710 step_dims(1) = 1_hsize_t
711 step_maxdims(1) = h5s_unlimited_f
712 chunkdims(1) = 1_hsize_t
713
714 call h5screate_simple_f(1, step_dims, filespace, ierr, step_maxdims)
715 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
716 call h5pset_chunk_f(dcpl_id, 1, chunkdims, ierr)
717 call h5dcreate_f(grp_id, "Values", h5t_neko_double, &
718 filespace, dset_id, ierr, dcpl_id = dcpl_id)
719 call h5sclose_f(filespace, ierr)
720 call h5pclose_f(dcpl_id, ierr)
721 end if
722
723 step_count(1) = 1_hsize_t
724 step_offset(1) = int(counter, hsize_t)
725
726 call h5dget_space_f(dset_id, filespace, ierr)
727 call h5screate_simple_f(1, step_count, memspace, ierr)
728 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
729 step_offset, step_count, ierr)
730
731 time_value(1) = t
732 call h5dwrite_f(dset_id, h5t_neko_double, time_value, step_count, ierr, &
733 file_space_id = filespace, mem_space_id = memspace, xfer_prp = xf_id)
734
735 call h5sclose_f(memspace, ierr)
736 call h5sclose_f(filespace, ierr)
737 call h5dclose_f(dset_id, ierr)
738
739 ! --- NSteps attribute ---
740 ddim(1) = 1_hsize_t
741 call h5aexists_f(grp_id, "NSteps", attr_exists, ierr)
742 if (attr_exists) then
743 call h5aopen_f(grp_id, "NSteps", attr_id, ierr)
744 else
745 call h5screate_f(h5s_scalar_f, filespace, ierr)
746 call h5acreate_f(grp_id, "NSteps", h5t_native_integer, filespace, &
747 attr_id, ierr, h5p_default_f, h5p_default_f)
748 call h5sclose_f(filespace, ierr)
749 end if
750
751 call h5awrite_f(attr_id, h5t_native_integer, counter + 1, ddim, ierr)
752
753 call h5aclose_f(attr_id, ierr)
754 call h5gclose_f(grp_id, ierr)
755 call h5pclose_f(xf_id, ierr)
756
757 end subroutine vtkhdf_write_steps
758
759 !> Write field data into the VTKHDF PointData group.
760 !! Groups u, v, w fields into a 3-component Velocity vector dataset.
761 !! All other fields are written as scalar datasets.
762 !! For temporal output (when t is present), each timestep's data is stored
763 !! in a separate external HDF5 file and the main file uses HDF5 Virtual
764 !! Datasets (VDS) with printf-style source file patterns to map them.
765 !! For non-temporal output (when t is absent), fields are written directly
766 !! into the main file's PointData group as regular datasets.
767 !! @param vtkhdf_grp Root VTKHDF group
768 !! @param fields Field list containing the fields to write.
769 !! @param precision Output precision
770 !! @param counter Current timestep counter
771 !! @param fname Main VTKHDF file path (used to derive external file names)
772 !! @param t Current simulation time (optional, for temporal output)
773 subroutine vtkhdf_write_pointdata(vtkhdf_grp, fields, precision, counter, &
774 fname, t)
775 integer(hid_t), intent(in) :: vtkhdf_grp
776 type(field_list_t), intent(inout) :: fields
777 integer, intent(in) :: precision
778 integer, intent(in) :: counter
779 character(len=*), intent(in) :: fname
780 real(kind=dp), intent(in), optional :: t
781
782 integer(kind=i8) :: time_offset, point_offset, total_points
783 integer :: local_points
784 integer(hid_t) :: precision_hdf
785 integer :: ierr, i, j
786 integer :: n_fields
787 integer(hid_t) :: pointdata_grp, grp_id, step_grp_id
788 integer(hid_t) :: dset_id, dcpl_id, filespace
789 integer(hsize_t), dimension(1) :: pd_dims1, pd_maxdims1
790 integer(hsize_t), dimension(2) :: pd_dims2, pd_maxdims2
791 type(field_t), pointer :: u, v, w
792 character(len=128) :: field_name
793 logical :: exists, is_vector
794
795 ! VDS and per-timestep external file variables
796 character(len=1024) :: ext_fname, ext_path, src_pattern
797 character(len=1024) :: main_path, main_name, main_suffix
798 integer(hid_t) :: ext_file_id, ext_plist_id, vds_src_space
799 integer(hid_t) :: write_target, attr_id, H5T_NEKO_STRING
800 integer :: mpi_info, mpi_comm
801
802 ! Collected field info for VDS phase
803 integer :: fields_written
804 character(len=128), allocatable :: name_list(:)
805 logical, allocatable :: vector_list(:)
806
807 mpi_info = mpi_info_null%mpi_val
808 mpi_comm = neko_comm%mpi_val
809
810 n_fields = fields%size()
811
812 ! Compute local/global point counts and MPI offsets
813 local_points = fields%item_size(1)
814 total_points = fields%items(1)%ptr%dof%global_size()
815 point_offset = vtkhdf_rank_offset(local_points)
816
817 ! Sync all the fields
818 do i = 1, n_fields
819 if (associated(fields%items(i)%ptr)) then
820 call fields%items(i)%ptr%copy_from(device_to_host, sync = i .eq. n_fields)
821 end if
822 end do
823
824 fields_written = 0
825 allocate(name_list(n_fields))
826 allocate(vector_list(n_fields))
827
828 ! Create PointData group if missing
829 call h5lexists_f(vtkhdf_grp, "PointData", exists, ierr)
830 if (.not. exists) then
831 call h5gcreate_f(vtkhdf_grp, "PointData", pointdata_grp, ierr)
832 call h5gclose_f(pointdata_grp, ierr)
833 end if
834
835 ! ------------------------------------------------------------------------ !
836 ! Construct the target where data is written
837
838 if (present(t)) then
839
840 ! Derive base path from main filename for external files
841 call filename_split(fname, main_path, main_name, main_suffix)
842 write(ext_path, '(A,A,".data/")') trim(main_path), trim(main_name)
843 write(ext_fname, '(A,I0,".h5")') trim(ext_path), counter
844 write(src_pattern, '(A,".data/%b.h5")') trim(main_name)
845
846 if (pe_rank .eq. 0) then
847 call mkdir(trim(ext_path))
848 end if
849 call mpi_barrier(neko_comm, ierr)
850
851 call h5pcreate_f(h5p_file_access_f, ext_plist_id, ierr)
852 call h5pset_fapl_mpio_f(ext_plist_id, mpi_comm, mpi_info, ierr)
853 call h5fcreate_f(trim(ext_fname), h5f_acc_trunc_f, write_target, ierr, &
854 access_prp = ext_plist_id)
855 if (ierr .ne. 0) then
856 call neko_error('VTKHDF: Cannot create step file: ' // &
857 trim(ext_fname))
858 end if
859 call h5pclose_f(ext_plist_id, ierr)
860
861 ! Readers find the step files through the VDS printf pattern and
862 ! probe the next index themselves. Flush now so the superblock is on
863 ! disk before any field data: an interrupted write then leaves a
864 ! valid file without datasets, which readers skip, rather than a
865 ! headerless file that breaks every read through the VDS (bar the
866 ! brief window before this flush completes).
867 call h5fflush_f(write_target, h5f_scope_global_f, ierr)
868 if (ierr .ne. 0) then
869 call neko_error('VTKHDF: Cannot flush step file: ' // &
870 trim(ext_fname))
871 end if
872
873 else
874 ! Non-temporal: write directly into the main file's PointData group
875 call h5gopen_f(vtkhdf_grp, "PointData", write_target, ierr)
876 end if
877
878 ! ------------------------------------------------------------------------ !
879 ! Write field data
880
881 do i = 1, n_fields
882 field_name = fields%name(i)
883 if (field_name .eq. 'p') field_name = 'Pressure'
884
885 ! Determine if this is a velocity component to group as a vector
886 is_vector = .false.
887 if (field_name .eq. 'u' .or. field_name .eq. 'v' .or. &
888 field_name .eq. 'w') then
889 u => null()
890 v => null()
891 w => null()
892 do j = 1, n_fields
893 select case (trim(fields%name(j)))
894 case ('u')
895 u => fields%get(j)
896 case ('v')
897 v => fields%get(j)
898 case ('w')
899 w => fields%get(j)
900 end select
901 end do
902
903 if (associated(u) .and. associated(v) .and. associated(w)) then
904 is_vector = .true.
905 field_name = 'Velocity'
906 end if
907 end if
908
909 ! Skip duplicate fields (e.g. fluid_rho added by both fluid output
910 ! and field_writer when sharing the same output file)
911 exists = .false.
912 do j = 1, fields_written
913 if (trim(name_list(j)) .eq. trim(field_name)) then
914 exists = .true.
915 exit
916 end if
917 end do
918
919 ! Skip duplicate fields
920 if (exists) cycle
921
922 ! Track unique field names for VDS phase
923 fields_written = fields_written + 1
924 name_list(fields_written) = field_name
925 vector_list(fields_written) = is_vector
926
927 ! Write field data to the target
928 if (is_vector) then
929 call write_vector_field(write_target, field_name, u%x, v%x, w%x, &
930 local_points, precision, total_points, point_offset)
931 else
932 call write_scalar_field(write_target, field_name, fields%x(i), &
933 local_points, precision, total_points, point_offset)
934 end if
935 end do
936
937 ! Close write target
938 if (present(t)) then
939 call h5fclose_f(write_target, ierr)
940 else
941 call h5gclose_f(write_target, ierr)
942 end if
943
944 ! ------------------------------------------------------------------------ !
945 ! Manage temporal datasets through VDS
946
947 if (present(t)) then
948
949 ! Temporal: record where this step's PointData starts
950 call h5gopen_f(vtkhdf_grp, "Steps", step_grp_id, ierr)
951 time_offset = int(counter, kind=i8) * int(total_points, kind=i8)
952
953 ! Write PointDataOffsets under Steps for each unique field
954 call h5lexists_f(step_grp_id, "PointDataOffsets", exists, ierr)
955 if (exists) then
956 call h5gopen_f(step_grp_id, "PointDataOffsets", grp_id, ierr)
957 else
958 call h5gcreate_f(step_grp_id, "PointDataOffsets", grp_id, ierr)
959 end if
960 do i = 1, fields_written
961 call vtkhdf_write_i8_at(grp_id, trim(name_list(i)), &
962 time_offset, counter)
963 end do
964 call h5gclose_f(grp_id, ierr)
965 call h5gclose_f(step_grp_id, ierr)
966
967 ! ===== Create the VDS in the main file on the first write =====
968 ! The VDS extent is never extended on later steps: with the %b
969 ! pattern and an unlimited dimension, HDF5 derives the extent from
970 ! the step files present when a reader asks for it. Extending it
971 ! explicitly made HDF5 open every existing step file at each write.
972 if (counter .eq. 0) then
973 call h5gopen_f(vtkhdf_grp, "PointData", pointdata_grp, ierr)
974
975 do i = 1, fields_written
976 field_name = name_list(i)
977 is_vector = vector_list(i)
978
979 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
980 precision_hdf = h5kind_to_type(precision, h5_real_kind)
981
982 if (is_vector) then
983 pd_dims2 = [3_hsize_t, int(total_points, hsize_t)]
984 call h5screate_simple_f(2, pd_dims2, vds_src_space, ierr)
985 call h5sselect_all_f(vds_src_space, ierr)
986
987 pd_maxdims2 = [3_hsize_t, h5s_unlimited_f]
988 call h5screate_simple_f(2, pd_dims2, filespace, ierr, &
989 pd_maxdims2)
990
991 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
992 [0_hsize_t, 0_hsize_t], &
993 [1_hsize_t, h5s_unlimited_f], &
994 ierr, &
995 stride = [3_hsize_t, int(total_points, hsize_t)], &
996 block = [3_hsize_t, int(total_points, hsize_t)])
997
998 call h5pset_virtual_f(dcpl_id, filespace, trim(src_pattern), &
999 trim(field_name), vds_src_space, ierr)
1000 call h5sclose_f(vds_src_space, ierr)
1001
1002 call h5dcreate_f(pointdata_grp, trim(field_name), &
1003 precision_hdf, filespace, dset_id, ierr, &
1004 dcpl_id = dcpl_id)
1005 call h5sclose_f(filespace, ierr)
1006 else
1007 pd_dims1 = int(total_points, hsize_t)
1008 call h5screate_simple_f(1, pd_dims1, vds_src_space, ierr)
1009 call h5sselect_all_f(vds_src_space, ierr)
1010
1011 pd_maxdims1(1) = h5s_unlimited_f
1012 call h5screate_simple_f(1, pd_dims1, filespace, ierr, &
1013 pd_maxdims1)
1014
1015 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1016 [0_hsize_t], &
1017 [h5s_unlimited_f], &
1018 ierr, &
1019 stride = [int(total_points, hsize_t)], &
1020 block = [int(total_points, hsize_t)])
1021
1022 call h5pset_virtual_f(dcpl_id, filespace, trim(src_pattern), &
1023 trim(field_name), vds_src_space, ierr)
1024 call h5sclose_f(vds_src_space, ierr)
1025
1026 call h5dcreate_f(pointdata_grp, trim(field_name), &
1027 precision_hdf, filespace, dset_id, ierr, &
1028 dcpl_id = dcpl_id)
1029 call h5sclose_f(filespace, ierr)
1030 end if
1031
1032 call h5pclose_f(dcpl_id, ierr)
1033 call h5dclose_f(dset_id, ierr)
1034 end do
1035
1036 call h5gclose_f(pointdata_grp, ierr)
1037 end if
1038 end if
1039
1040 ! ------------------------------------------------------------------------ !
1041 ! Cleanup before returning
1042
1043 deallocate(name_list)
1044 deallocate(vector_list)
1045
1046 end subroutine vtkhdf_write_pointdata
1047
1048 ! -------------------------------------------------------------------------- !
1049 ! Helper functions and routines
1050
1051 !> Offset of this rank's entries in a dataset decomposed by rank.
1052 !! @param n_local Number of entries on this rank
1053 function vtkhdf_rank_offset(n_local) result(offset)
1054 integer, intent(in) :: n_local
1055 integer(kind=i8) :: offset, n_local_i8
1056 integer :: ierr
1057
1058 n_local_i8 = int(n_local, i8)
1059 offset = 0_i8
1060 call mpi_exscan(n_local_i8, offset, 1, mpi_integer8, mpi_sum, neko_comm, &
1061 ierr)
1062 end function vtkhdf_rank_offset
1063
1064 !> Chunk extent along the rank-decomposed dimension of a dataset.
1065 !! @details The extent is the largest local block, limited by the size of
1066 !! the dataset and by HDF5, which refuses to create a dataset with a chunk of
1067 !! 2^32 bytes or more.
1068 !! @param max_local Largest number of entries on any rank
1069 !! @param total Number of entries in the dataset
1070 !! @param entry_bytes Size of one entry in the file, in bytes
1071 pure function vtkhdf_chunk_extent(max_local, total, entry_bytes) &
1072 result(extent)
1073 integer(kind=i8), intent(in) :: max_local, total
1074 integer, intent(in) :: entry_bytes
1075 integer(hsize_t) :: extent
1076 integer(kind=i8), parameter :: max_chunk_bytes = 4294967295_i8
1077
1078 extent = int(max(1_i8, min(max_local, total, &
1079 max_chunk_bytes / int(entry_bytes, i8))), hsize_t)
1080 end function vtkhdf_chunk_extent
1081
1082 !> Build local connectivity for VTK cells from a spectral element
1083 !! tensor-product grid. For linear types (12, 9) subdivides each spectral
1084 !! element into linear sub-cells. For Lagrange types (72, 70) writes one
1085 !! high-order cell per element with VTK node ordering.
1086 !! @param conn Output connectivity array (pre-allocated)
1087 !! @param vtk_type VTK cell type: 12 = VTK_HEXAHEDRON, 9 = VTK_QUAD,
1088 !! 72 = VTK_LAGRANGE_HEXAHEDRON, 70 = VTK_LAGRANGE_QUADRILATERAL
1089 !! @param msh Mesh object containing element information
1090 !! @param dof Dofmap containing the lx, ly, lz dimensions of the spectral
1091 !! element grid
1092 !! @param subdivide Logical flag indicating whether to subdivide to linear
1093 !! elements
1094 subroutine vtkhdf_build_connectivity(conn, vtk_type, msh, dof, subdivide)
1095 integer, intent(inout) :: conn(:)
1096 integer(kind=1), intent(in) :: vtk_type
1097 type(mesh_t), intent(in) :: msh
1098 type(dofmap_t), intent(in) :: dof
1099 logical, intent(in) :: subdivide
1100 integer :: lx, ly, lz, nelv
1101 integer :: ie, ii, n_pts_per_elem, n_conn_per_elem
1102 integer, allocatable :: node_order(:)
1103
1104 nelv = msh%nelv
1105 lx = dof%Xh%lx
1106 ly = dof%Xh%ly
1107 lz = dof%Xh%lz
1108 n_pts_per_elem = lx * ly * lz
1109
1110 if (subdivide .and. vtk_type .eq. int(12, kind=1)) then
1111 node_order = subdivide_to_hex_ordering(lx, ly, lz)
1112 else if (subdivide .and. vtk_type .eq. int(9, kind=1)) then
1113 node_order = subdivide_to_quad_ordering(lx, ly)
1114 else
1115 node_order = vtk_ordering(vtk_type, lx, ly, lz)
1116 end if
1117
1118 n_conn_per_elem = size(node_order)
1119
1120 do concurrent(ie = 1:nelv, ii = 1:n_conn_per_elem)
1121 block
1122 integer :: idx, base
1123 idx = (ie - 1) * n_conn_per_elem
1124 base = (ie - 1) * n_pts_per_elem
1125 conn(idx + ii) = base + node_order(ii)
1126 end block
1127 end do
1128
1129 deallocate(node_order)
1130
1131 end subroutine vtkhdf_build_connectivity
1132
1133 !> Create-or-extend a 1D NumberOf* dataset, then write one entry.
1134 !! On step 0 the dataset is created with unlimited max extent.
1135 !! On subsequent steps it is extended to (counter+1)*nPieces.
1136 !! @param grp Parent HDF5 group (VTKHDF root)
1137 !! @param dset_name Dataset name, e.g. "NumberOfPoints"
1138 !! @param value The integer value to write for this rank
1139 !! @param offset 1-element array: counter*pe_size + pe_rank
1140 !! @param cnt 1-element array, always [1]
1141 !! @param dcpl Dataset creation property list (chunked)
1142 !! @param index Current timestep index (0-based)
1143 !! @param xf_id Collective transfer property list
1144 !! @param ierr HDF5 error code (output)
1145 subroutine vtkhdf_write_numberof(grp, dset_name, value, offset, cnt, &
1146 dcpl, index, xf_id, ierr)
1147 integer(hid_t), intent(in) :: grp, dcpl, xf_id
1148 character(len=*), intent(in) :: dset_name
1149 integer(kind=i8), intent(in) :: value
1150 integer, intent(in):: index
1151 integer(hsize_t), dimension(1), intent(in) :: offset, cnt
1152 integer, intent(out) :: ierr
1153
1154 integer(hid_t) :: dset_id, fspace, mspace
1155 integer(hsize_t), dimension(1) :: dims, maxdims
1156 integer(hid_t) :: H5T_NEKO_INTEGER
1157 logical :: exists
1158
1159 h5t_neko_integer = h5kind_to_type(i8, h5_integer_kind)
1160
1161 call h5lexists_f(grp, dset_name, exists, ierr)
1162 if (exists) then
1163 call h5dopen_f(grp, dset_name, dset_id, ierr)
1164 dims(1) = int(index + 1, hsize_t) * int(pe_size, hsize_t)
1165 call h5dset_extent_f(dset_id, dims, ierr)
1166 else
1167 dims(1) = int(pe_size, hsize_t)
1168 maxdims(1) = h5s_unlimited_f
1169 call h5screate_simple_f(1, dims, fspace, ierr, maxdims)
1170 call h5dcreate_f(grp, dset_name, h5t_neko_integer, &
1171 fspace, dset_id, ierr, dcpl_id = dcpl)
1172 call h5sclose_f(fspace, ierr)
1173 end if
1174
1175 call h5dget_space_f(dset_id, fspace, ierr)
1176 call h5screate_simple_f(1, cnt, mspace, ierr)
1177 call h5sselect_hyperslab_f(fspace, h5s_select_set_f, offset, cnt, ierr)
1178
1179 call h5dwrite_f(dset_id, h5t_neko_integer, value, cnt, ierr, &
1180 file_space_id = fspace, mem_space_id = mspace, xfer_prp = xf_id)
1181
1182 call h5sclose_f(mspace, ierr)
1183 call h5sclose_f(fspace, ierr)
1184 call h5dclose_f(dset_id, ierr)
1185 end subroutine vtkhdf_write_numberof
1186
1187 !> Append an integer scalar to a 1D chunked HDF5 dataset.
1188 !! Opens the dataset if it exists, or creates a new empty chunked dataset.
1189 !! Extends by 1 element and writes the value at the end.
1190 !! @param grp_id HDF5 group containing the dataset
1191 !! @param name Dataset name
1192 !! @param value Value to append
1193 !! @param index Position to write to
1194 subroutine vtkhdf_write_i8_at(grp_id, name, value, index)
1195 integer(hid_t), intent(in) :: grp_id
1196 character(len=*), intent(in) :: name
1197 integer(kind=i8), intent(in) :: value
1198 integer, intent(in) :: index
1199
1200 integer :: ierr
1201 integer(hid_t) :: dset_id, dcpl_id, xf_id, filespace, memspace
1202 integer(hsize_t), dimension(1) :: dims, maxdims, count, offset, chunkdims
1203 integer(hid_t) :: H5T_NEKO_INTEGER
1204 logical :: exists
1205
1206 h5t_neko_integer = h5kind_to_type(i8, h5_integer_kind)
1207
1208 ! Create collective transfer property list
1209 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
1210 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
1211
1212 call h5lexists_f(grp_id, name, exists, ierr)
1213 if (exists) then
1214 call h5dopen_f(grp_id, name, dset_id, ierr)
1215 call h5dget_space_f(dset_id, filespace, ierr)
1216 call h5sget_simple_extent_dims_f(filespace, dims, maxdims, ierr)
1217 call h5sclose_f(filespace, ierr)
1218
1219 if (int(index, hsize_t) .eq. dims(1)) then
1220 dims(1) = int(index + 1, hsize_t)
1221 call h5dset_extent_f(dset_id, dims, ierr)
1222 else if (int(index, hsize_t) .gt. dims(1)) then
1223 call neko_error("VTKHDF: Values written out of order.")
1224 end if
1225 else
1226 dims = 1_hsize_t
1227 maxdims = h5s_unlimited_f
1228 chunkdims = 1_hsize_t
1229
1230 call h5screate_simple_f(1, dims, filespace, ierr, maxdims)
1231 call h5pcreate_f(h5p_dataset_create_f, dcpl_id, ierr)
1232 call h5pset_chunk_f(dcpl_id, 1, chunkdims, ierr)
1233 call h5dcreate_f(grp_id, name, h5t_neko_integer, &
1234 filespace, dset_id, ierr, dcpl_id = dcpl_id)
1235 call h5sclose_f(filespace, ierr)
1236 call h5pclose_f(dcpl_id, ierr)
1237 end if
1238
1239 count = 1_hsize_t
1240 offset = int(index, hsize_t)
1241
1242 call h5dget_space_f(dset_id, filespace, ierr)
1243 call h5screate_simple_f(1, count, memspace, ierr)
1244 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, offset, count, ierr)
1245 call h5dwrite_f(dset_id, h5t_neko_integer, value, count, ierr, &
1246 file_space_id = filespace, mem_space_id = memspace, xfer_prp = xf_id)
1247
1248 call h5sclose_f(memspace, ierr)
1249 call h5sclose_f(filespace, ierr)
1250 call h5dclose_f(dset_id, ierr)
1251 call h5pclose_f(xf_id, ierr)
1252
1253 end subroutine vtkhdf_write_i8_at
1254
1255 !> @brief Write a scalar field as a 1D dataset.
1256 !! @details
1257 !! This function simplifies the interfacing with HDF5 for the purpose of
1258 !! writing an array.
1259 !! @param hdf_root HDF5 group or file identifier to write under
1260 !! @param name Dataset name
1261 !! @param x Local array of field data to write
1262 !! @param n_local Number of local points in x
1263 !! @param precision Desired output precision (sp, dp, or rp)
1264 !! @param n_total Total number of points across all MPI ranks (optional)
1265 !! @param offset Starting index offset for this rank (optional)
1266 subroutine write_scalar_field(hdf_root, name, x, n_local, &
1267 precision, n_total, offset)
1268 integer, intent(in) :: n_local
1269 integer(hid_t), intent(in) :: hdf_root
1270 character(len=*), intent(in) :: name
1271 real(kind=rp), dimension(n_local), intent(in) :: x
1272 integer, intent(in), optional :: precision
1273 integer(kind=i8), intent(in), optional :: n_total, offset
1274
1275 integer(hsize_t), dimension(1) :: dims, dcount, doffset
1276 integer(hid_t) :: xf_id, dset_id, filespace, memspace, precision_hdf
1277 integer :: i, ierr, precision_local
1278
1279 ! Setup data sizes, offsets and precision
1280 dcount = int(n_local, hsize_t)
1281
1282 if (present(n_total)) then
1283 dims = int(n_total, hsize_t)
1284 else
1285 dims = int(n_local, hsize_t)
1286 call mpi_allreduce(mpi_in_place, dims(1), 1, mpi_integer8, mpi_sum, &
1287 neko_comm, ierr)
1288 end if
1289
1290 if (present(offset)) then
1291 doffset = int(offset, hsize_t)
1292 else
1293 doffset = int(vtkhdf_rank_offset(n_local), hsize_t)
1294 end if
1295
1296 if (present(precision)) then
1297 precision_local = precision
1298 else
1299 precision_local = rp
1300 end if
1301 precision_hdf = h5kind_to_type(precision_local, h5_real_kind)
1302
1303 ! Prepare memory and filespaces
1304 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
1305 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
1306
1307 call h5screate_simple_f(1, dims, filespace, ierr)
1308 call h5screate_simple_f(1, dcount, memspace, ierr)
1309 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1310 doffset, dcount, ierr)
1311
1312 call h5dcreate_f(hdf_root, trim(name), precision_hdf, &
1313 filespace, dset_id, ierr)
1314
1315 if (precision_local .eq. rp) then
1316 call h5dwrite_f(dset_id, precision_hdf, x, dcount, ierr, &
1317 file_space_id = filespace, mem_space_id = memspace, &
1318 xfer_prp = xf_id)
1319
1320 else if (precision_local .eq. sp) then
1321 block
1322 real(kind=sp), allocatable :: x_sp(:)
1323
1324 allocate(x_sp(n_local))
1325 do concurrent(i = 1:n_local)
1326 x_sp(i) = real(x(i), sp)
1327 end do
1328
1329 call h5dwrite_f(dset_id, precision_hdf, x_sp, dcount, ierr, &
1330 file_space_id = filespace, mem_space_id = memspace, &
1331 xfer_prp = xf_id)
1332 deallocate(x_sp)
1333 end block
1334
1335 else if (precision_local .eq. dp) then
1336 block
1337 real(kind=dp), allocatable :: x_dp(:)
1338
1339 allocate(x_dp(n_local))
1340 do concurrent(i = 1:n_local)
1341 x_dp(i) = real(x(i), dp)
1342 end do
1343
1344 call h5dwrite_f(dset_id, precision_hdf, x_dp, dcount, ierr, &
1345 file_space_id = filespace, mem_space_id = memspace, &
1346 xfer_prp = xf_id)
1347 deallocate(x_dp)
1348 end block
1349
1350 else if (precision_local .eq. qp) then
1351 block
1352 real(kind=qp), allocatable :: x_qp(:)
1353
1354 allocate(x_qp(n_local))
1355 do concurrent(i = 1:n_local)
1356 x_qp(i) = real(x(i), qp)
1357 end do
1358
1359 call h5dwrite_f(dset_id, precision_hdf, x_qp, dcount, ierr, &
1360 file_space_id = filespace, mem_space_id = memspace, &
1361 xfer_prp = xf_id)
1362 deallocate(x_qp)
1363 end block
1364
1365 else
1366 call neko_error("Unsupported precision in HDF5 write_scalar_field")
1367 end if
1368
1369 call h5sclose_f(filespace, ierr)
1370 call h5sclose_f(memspace, ierr)
1371 call h5dclose_f(dset_id, ierr)
1372 call h5pclose_f(xf_id, ierr)
1373 end subroutine write_scalar_field
1374
1375 !> @brief Write 3 vector components as a single 2D dataset with shape (3, n_local).
1376 !! @details
1377 !! This function simplifies the interfacing with HDF5 for the purpose of
1378 !! writing an array.
1379 !! @param hdf_root HDF5 group or file identifier to write under
1380 !! @param name Dataset name
1381 !! @param u Local array of u component to write
1382 !! @param v Local array of v component to write
1383 !! @param w Local array of w component to write
1384 !! @param n_local Number of entries in each component
1385 !! @param precision Desired output precision (sp, dp, or rp)
1386 !! @param n_total Total number of points across all MPI ranks (optional)
1387 !! @param offset Starting index offset for this rank (optional)
1388 subroutine write_vector_field(hdf_root, name, u, v, w, n_local, &
1389 precision, n_total, offset)
1390 integer, intent(in) :: n_local
1391 integer(hid_t), intent(in) :: hdf_root
1392 character(len=*), intent(in) :: name
1393 real(kind=rp), dimension(n_local), intent(in) :: u, v, w
1394 integer, intent(in), optional :: precision
1395 integer(kind=i8), intent(in), optional :: n_total, offset
1396
1397 integer(hsize_t), dimension(2) :: dims, dcount, doffset
1398 integer(hid_t) :: xf_id, dset_id, filespace, memspace, precision_hdf
1399 integer :: i, ierr, precision_local
1400
1401 ! Setup data sizes, offsets and precision
1402 dcount = [3_hsize_t, int(n_local, hsize_t)]
1403
1404 if (present(n_total)) then
1405 dims = [3_hsize_t, int(n_total, hsize_t)]
1406 else
1407 dims = [3_hsize_t, int(n_local, hsize_t)]
1408 call mpi_allreduce(mpi_in_place, dims(2), 1, mpi_integer8, mpi_sum, &
1409 neko_comm, ierr)
1410 end if
1411
1412 if (present(offset)) then
1413 doffset = [0_hsize_t, int(offset, hsize_t)]
1414 else
1415 doffset = [0_hsize_t, int(vtkhdf_rank_offset(n_local), hsize_t)]
1416 end if
1417
1418 if (present(precision)) then
1419 precision_local = precision
1420 else
1421 precision_local = rp
1422 end if
1423 precision_hdf = h5kind_to_type(precision_local, h5_real_kind)
1424
1425 ! Prepare memory and filespaces
1426 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
1427 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
1428
1429 call h5screate_simple_f(2, dims, filespace, ierr)
1430 call h5screate_simple_f(2, dcount, memspace, ierr)
1431 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1432 doffset, dcount, ierr)
1433
1434 call h5dcreate_f(hdf_root, trim(name), precision_hdf, filespace, dset_id, &
1435 ierr)
1436
1437 if (precision_local .eq. rp) then
1438 ! Perform direct write for each component separately to avoid extra
1439 ! memory allocation: the n_local values of one component go into one
1440 ! row of the file hyperslab, so the memory space is 1D
1441 call h5sclose_f(memspace, ierr)
1442 call h5screate_simple_f(1, dcount(2:2), memspace, ierr)
1443
1444 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1445 [0_hsize_t, doffset(2)], [1_hsize_t, dcount(2)], ierr)
1446 call h5dwrite_f(dset_id, precision_hdf, u, dcount(2:2), ierr, &
1447 file_space_id = filespace, mem_space_id = memspace, &
1448 xfer_prp = xf_id)
1449 if (ierr .ne. 0) then
1450 call neko_error('VTKHDF: Cannot write vector field: ' // &
1451 trim(name))
1452 end if
1453
1454 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1455 [1_hsize_t, doffset(2)], [1_hsize_t, dcount(2)], ierr)
1456 call h5dwrite_f(dset_id, precision_hdf, v, dcount(2:2), ierr, &
1457 file_space_id = filespace, mem_space_id = memspace, &
1458 xfer_prp = xf_id)
1459 if (ierr .ne. 0) then
1460 call neko_error('VTKHDF: Cannot write vector field: ' // &
1461 trim(name))
1462 end if
1463
1464 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1465 [2_hsize_t, doffset(2)], [1_hsize_t, dcount(2)], ierr)
1466 call h5dwrite_f(dset_id, precision_hdf, w, dcount(2:2), ierr, &
1467 file_space_id = filespace, mem_space_id = memspace, &
1468 xfer_prp = xf_id)
1469 if (ierr .ne. 0) then
1470 call neko_error('VTKHDF: Cannot write vector field: ' // &
1471 trim(name))
1472 end if
1473 else if (precision_local .eq. sp) then
1474 block
1475 real(kind=sp), allocatable :: f(:,:)
1476
1477 allocate(f(3, n_local))
1478 do concurrent(i = 1:n_local)
1479 f(1, i) = real(u(i), sp)
1480 f(2, i) = real(v(i), sp)
1481 f(3, i) = real(w(i), sp)
1482 end do
1483
1484 call h5dwrite_f(dset_id, precision_hdf, f, dcount, ierr, &
1485 file_space_id = filespace, mem_space_id = memspace, &
1486 xfer_prp = xf_id)
1487 deallocate(f)
1488 end block
1489
1490 else if (precision_local .eq. dp) then
1491 block
1492 real(kind=dp), allocatable :: f(:,:)
1493
1494 allocate(f(3, n_local))
1495 do concurrent(i = 1:n_local)
1496 f(1, i) = real(u(i), dp)
1497 f(2, i) = real(v(i), dp)
1498 f(3, i) = real(w(i), dp)
1499 end do
1500
1501 call h5dwrite_f(dset_id, precision_hdf, f, dcount, ierr, &
1502 file_space_id = filespace, mem_space_id = memspace, &
1503 xfer_prp = xf_id)
1504 deallocate(f)
1505 end block
1506
1507 else if (precision_local .eq. qp) then
1508 block
1509 real(kind=qp), allocatable :: f(:,:)
1510
1511 allocate(f(3, n_local))
1512 do concurrent(i = 1:n_local)
1513 f(1, i) = real(u(i), qp)
1514 f(2, i) = real(v(i), qp)
1515 f(3, i) = real(w(i), qp)
1516 end do
1517
1518 call h5dwrite_f(dset_id, precision_hdf, f, dcount, ierr, &
1519 file_space_id = filespace, mem_space_id = memspace, &
1520 xfer_prp = xf_id)
1521 deallocate(f)
1522 end block
1523
1524 else
1525 call neko_error("Unsupported precision in HDF5 write_vector_field")
1526 end if
1527
1528 call h5sclose_f(filespace, ierr)
1529 call h5sclose_f(memspace, ierr)
1530 call h5dclose_f(dset_id, ierr)
1531 call h5pclose_f(xf_id, ierr)
1532 end subroutine write_vector_field
1533
1534 ! -------------------------------------------------------------------------- !
1535 ! Reader functions and routines
1536
1537 !> Read data in HDF5 format following official VTKHDF specification
1538 subroutine vtkhdf_file_read(this, data)
1539 class(vtkhdf_file_t) :: this
1540 class(*), target, intent(inout) :: data
1541 character(len=1024) :: fname
1542 integer(hid_t) :: plist_id, file_id, vtkhdf_grp
1543 integer :: mpi_info, mpi_comm, counter, ierr
1544 type(field_list_t) :: fields
1545 integer :: i
1546
1547 ! Open the file
1548 fname = trim(this%get_base_fname())
1549 counter = this%get_counter() - this%get_start_counter()
1550 if (counter .lt. 0) counter = 0
1551
1552 mpi_info = mpi_info_null%mpi_val
1553 mpi_comm = neko_comm%mpi_val
1554
1555 call hdf5_session_init()
1556 call h5pcreate_f(h5p_file_access_f, plist_id, ierr)
1557 call h5pset_fapl_mpio_f(plist_id, mpi_comm, mpi_info, ierr)
1558
1559 call h5fopen_f(fname, h5f_acc_rdonly_f, file_id, ierr, &
1560 access_prp = plist_id)
1561 if (ierr .ne. 0) then
1562 call neko_error('Error opening VTKHDF file: ' // trim(fname))
1563 end if
1564
1565 call h5gopen_f(file_id, "VTKHDF", vtkhdf_grp, ierr)
1566 if (ierr .ne. 0) then
1567 call h5fclose_f(file_id, ierr)
1568 call h5pclose_f(plist_id, ierr)
1569 call neko_error('VTKHDF group not found in file: ' // trim(fname))
1570 end if
1571
1572 select type (data)
1573 type is (field_t)
1574 call fields%init(1)
1575 ! Specific bindings instead of the assign generic (see
1576 ! vtkhdf_file_write); Fujitsu frt cannot resolve the generic
1577 call fields%assign_to_field(1, data)
1578 type is (field_list_t)
1579 call fields%assign_to_list(data)
1580 class default
1581 call neko_error("Unsupported data type in vtkhdf_file_read")
1582 end select
1583
1584 do i = 1, fields%size()
1585 call vtkhdf_read_field(vtkhdf_grp, fname, counter, fields%get(i))
1586 end do
1587 call fields%copy_from(host_to_device, .true.)
1588
1589 call h5gclose_f(vtkhdf_grp, ierr)
1590 call h5fclose_f(file_id, ierr)
1591 call h5pclose_f(plist_id, ierr)
1592 call hdf5_session_finalize()
1593
1594 call fields%free()
1595
1596 end subroutine vtkhdf_file_read
1597
1598 !> Read one field from VTKHDF/PointData.
1599 !! The PointData dataset name is expected to match @a field_name.
1600 !! For compatibility with the writer, this routine also supports:
1601 !! - p -> Pressure
1602 !! - u/v/w -> Velocity component 1/2/3
1603 !!
1604 !! If the dataset is a Virtual Dataset (VDS), HDF5 does not support parallel
1605 !! reads on VDS. We detect this from the dataset's storage layout and read
1606 !! directly from the external source file instead.
1607 !!
1608 !! A temporal file whose PointData is not virtual holds every step in one
1609 !! dataset. The rows of step @a counter then start at
1610 !! Steps/PointDataOffsets/<dataset>(@a counter). As in the VTKHDF
1611 !! specification, a dataset without such offsets starts at the geometry
1612 !! offset of the step, Steps/PointOffsets(@a counter).
1613 subroutine vtkhdf_read_field(vtkhdf_grp, fname, counter, fld)
1614 integer(hid_t), intent(in) :: vtkhdf_grp
1615 character(len=*), intent(in) :: fname
1616 integer, intent(in) :: counter
1617 type(field_t), intent(inout) :: fld
1618
1619 character(len=:), allocatable :: field_name
1620 integer :: ierr, local_points, component
1621 integer(kind=i8) :: point_offset
1622 integer(hid_t) :: pointdata_grp, dset_id, filespace, memspace, xf_id
1623 integer(hid_t) :: dcpl_id, ext_file_id, ext_plist_id, attr_id
1624 integer(hid_t) :: offsets_id, offsets_space, step_space
1625 integer(hid_t) :: H5T_NEKO_REAL
1626 integer(hsize_t), dimension(1) :: dcount1, doffset1
1627 integer(hsize_t), dimension(2) :: dcount2, doffset2
1628
1629 integer(size_t) :: vds_count
1630 integer(kind=i8) :: step_offset
1631 character(len=128) :: dset_name
1632 character(len=1024) :: vds_src_file, ext_fname, main_path, main_name, main_suffix
1633 logical :: exists, is_vds, is_temporal
1634 real(kind=rp), allocatable :: vec_component(:,:)
1635 integer :: mpi_info, mpi_comm, pct_pos, nsteps, layout
1636 character(len=256) :: error_message
1637
1638 field_name = trim(fld%name)
1639
1640 ! Validate counter against file's NSteps if temporal data exists
1641 call h5lexists_f(vtkhdf_grp, "Steps", is_temporal, ierr)
1642 if (is_temporal) then
1643 call h5aopen_by_name_f(vtkhdf_grp, "Steps", "NSteps", attr_id, ierr)
1644 call h5aread_f(attr_id, h5t_native_integer, nsteps, [1_hsize_t], ierr)
1645 call h5aclose_f(attr_id, ierr)
1646
1647 if (counter .ge. nsteps) then
1648 write(error_message, '(A,I0,A,I0)') &
1649 'VTKHDF read: counter ', counter, ' >= NSteps ', nsteps
1650 call neko_error(trim(error_message))
1651 end if
1652 end if
1653
1654 mpi_info = mpi_info_null%mpi_val
1655 mpi_comm = neko_comm%mpi_val
1656
1657 local_points = fld%dof%size()
1658 point_offset = vtkhdf_rank_offset(local_points)
1659
1660 h5t_neko_real = h5kind_to_type(rp, h5_real_kind)
1661
1662 ! Open PointData group
1663 call h5gopen_f(vtkhdf_grp, "PointData", pointdata_grp, ierr)
1664
1665 ! Determine dataset name with standard mappings
1666 dset_name = trim(field_name)
1667 if (trim(dset_name) .eq. 'p') dset_name = 'Pressure'
1668
1669 component = -1
1670 call h5lexists_f(pointdata_grp, trim(dset_name), exists, ierr)
1671
1672 if (.not. exists) then
1673 if (trim(field_name) .eq. 'u' .or. trim(field_name) .eq. 'v' .or. &
1674 trim(field_name) .eq. 'w') then
1675 call h5lexists_f(pointdata_grp, "Velocity", exists, ierr)
1676 if (exists) then
1677 dset_name = 'Velocity'
1678 select case (trim(field_name))
1679 case ('u')
1680 component = 0
1681 case ('v')
1682 component = 1
1683 case ('w')
1684 component = 2
1685 end select
1686 end if
1687 end if
1688 end if
1689
1690 if (.not. exists) then
1691 call h5gclose_f(pointdata_grp, ierr)
1692 call neko_error('VTKHDF PointData field not found: ' // trim(dset_name))
1693 end if
1694
1695 ! Open the dataset and check if it's a VDS. Only a virtual layout may be
1696 ! queried for its mappings; h5pget_virtual_count_f fails on any other.
1697 call h5dopen_f(pointdata_grp, trim(dset_name), dset_id, ierr)
1698 call h5dget_create_plist_f(dset_id, dcpl_id, ierr)
1699 call h5pget_layout_f(dcpl_id, layout, ierr)
1700 if (ierr .ne. 0) then
1701 call neko_error('VTKHDF: Cannot get the storage layout of: ' // &
1702 trim(dset_name))
1703 end if
1704 is_vds = (layout .eq. h5d_virtual_f)
1705
1706 vds_count = 0_size_t
1707 step_offset = 0_i8
1708 if (is_vds) then
1709 call h5pget_virtual_count_f(dcpl_id, vds_count, ierr)
1710 if (ierr .ne. 0) then
1711 call neko_error('VTKHDF: Cannot get the VDS mappings of: ' // &
1712 trim(dset_name))
1713 end if
1714
1715 call filename_split(fname, main_path, main_name, main_suffix)
1716
1717 ! Query mapping 0 to detect whether this is a printf-style pattern VDS
1718 ! (Neko's own format uses a single mapping with %b as a block counter
1719 ! placeholder) or a multi-mapping VDS where each timestep has its own
1720 ! literal source file.
1721 call h5pget_virtual_filename_f(dcpl_id, 0_size_t, vds_src_file, ierr)
1722 if (ierr .ne. 0) then
1723 call neko_error('VTKHDF: Cannot get the VDS source file of: ' // &
1724 trim(dset_name))
1725 end if
1726 pct_pos = index(vds_src_file, '%b')
1727
1728 if (pct_pos .gt. 0) then
1729 ! Pattern-based VDS: replace %b with the timestep counter.
1730 ! If the stored path is absolute, use it as-is; otherwise prepend the
1731 ! main file's directory so the path is resolved correctly regardless
1732 ! of the process working directory.
1733 if (vds_src_file(1:1) .eq. '/') then
1734 write(ext_fname, '(A,I0,A)') &
1735 vds_src_file(1:pct_pos-1), counter, &
1736 trim(vds_src_file(pct_pos+2:))
1737 else
1738 write(ext_fname, '(A,A,I0,A)') &
1739 trim(main_path), vds_src_file(1:pct_pos-1), counter, &
1740 trim(vds_src_file(pct_pos+2:))
1741 end if
1742 else
1743 ! Multi-mapping VDS: one mapping per timestep. Query the mapping
1744 ! that corresponds directly to the requested counter instead of
1745 ! always using mapping 0, which would silently read the wrong file.
1746 if (int(counter, size_t) .ge. vds_count) then
1747 call h5pclose_f(dcpl_id, ierr)
1748 call h5dclose_f(dset_id, ierr)
1749 call h5gclose_f(pointdata_grp, ierr)
1750 write(error_message, '(A,I0,A,I0)') &
1751 'VTKHDF: VDS counter ', counter, &
1752 ' is out of range, number of mappings: ', int(vds_count)
1753 call neko_error(trim(error_message))
1754 end if
1755 call h5pget_virtual_filename_f(dcpl_id, int(counter, size_t), &
1756 vds_src_file, ierr)
1757 if (ierr .ne. 0) then
1758 call neko_error('VTKHDF: Cannot get the VDS source file of: ' // &
1759 trim(dset_name))
1760 end if
1761 if (vds_src_file(1:1) .eq. '/') then
1762 ext_fname = trim(vds_src_file)
1763 else
1764 write(ext_fname, '(A,A)') trim(main_path), trim(vds_src_file)
1765 end if
1766 end if
1767
1768 call h5pclose_f(dcpl_id, ierr)
1769 call h5dclose_f(dset_id, ierr)
1770 call h5gclose_f(pointdata_grp, ierr)
1771
1772 call h5pcreate_f(h5p_file_access_f, ext_plist_id, ierr)
1773 call h5pset_fapl_mpio_f(ext_plist_id, mpi_comm, mpi_info, ierr)
1774 call h5fopen_f(trim(ext_fname), h5f_acc_rdonly_f, ext_file_id, ierr, &
1775 access_prp = ext_plist_id)
1776
1777 if (ierr .ne. 0) then
1778 call neko_error('VTKHDF: Cannot open VDS source file: ' // &
1779 trim(ext_fname))
1780 end if
1781
1782 ! Open the dataset from the external file
1783 call h5dopen_f(ext_file_id, trim(dset_name), dset_id, ierr)
1784 if (ierr .ne. 0) then
1785 call neko_error('VTKHDF: PointData field ' // trim(dset_name) // &
1786 ' not found in VDS source file: ' // trim(ext_fname))
1787 end if
1788
1789 call h5pclose_f(ext_plist_id, ierr)
1790 else
1791 call h5pclose_f(dcpl_id, ierr)
1792 ext_file_id = -1
1793
1794 ! Inline temporal data: find where the rows of this step start,
1795 ! falling back to the geometry offset without data offsets
1796 if (is_temporal) then
1797 call h5lexists_f(vtkhdf_grp, "Steps/PointDataOffsets", exists, ierr)
1798 if (exists) then
1799 call h5lexists_f(vtkhdf_grp, "Steps/PointDataOffsets/" // &
1800 trim(dset_name), exists, ierr)
1801 end if
1802 if (exists) then
1803 call h5dopen_f(vtkhdf_grp, "Steps/PointDataOffsets/" // &
1804 trim(dset_name), offsets_id, ierr)
1805 else
1806 call h5lexists_f(vtkhdf_grp, "Steps/PointOffsets", exists, ierr)
1807 if (.not. exists) then
1808 call neko_error('VTKHDF: Neither Steps/PointDataOffsets/' // &
1809 trim(dset_name) // ' nor Steps/PointOffsets found ' // &
1810 'in temporal file: ' // &
1811 trim(fname))
1812 end if
1813 call h5dopen_f(vtkhdf_grp, "Steps/PointOffsets", offsets_id, ierr)
1814 end if
1815 call h5dget_space_f(offsets_id, offsets_space, ierr)
1816 call h5sselect_hyperslab_f(offsets_space, h5s_select_set_f, &
1817 [int(counter, hsize_t)], [1_hsize_t], ierr)
1818 call h5screate_simple_f(1, [1_hsize_t], step_space, ierr)
1819 call h5dread_f(offsets_id, h5kind_to_type(i8, h5_integer_kind), &
1820 step_offset, [1_hsize_t], ierr, &
1821 file_space_id = offsets_space, mem_space_id = step_space)
1822 if (ierr .ne. 0) then
1823 write(error_message, '(A,A,A,I0)') &
1824 'VTKHDF: Cannot read the step offset of ', &
1825 trim(dset_name), ' at step ', counter
1826 call neko_error(trim(error_message))
1827 end if
1828 call h5sclose_f(step_space, ierr)
1829 call h5sclose_f(offsets_space, ierr)
1830 call h5dclose_f(offsets_id, ierr)
1831 end if
1832 end if
1833
1834 ! Now read from the dataset (either original or from external file)
1835 call h5pcreate_f(h5p_dataset_xfer_f, xf_id, ierr)
1836 call h5pset_dxpl_mpio_f(xf_id, h5fd_mpio_collective_f, ierr)
1837 call h5dget_space_f(dset_id, filespace, ierr)
1838
1839 if (trim(dset_name) .eq. 'Velocity' .and. component .ge. 0) then
1840 ! Velocity is stored as (3, n_points), read one component
1841 dcount2 = [1_hsize_t, int(local_points, hsize_t)]
1842 doffset2 = [int(component, hsize_t), &
1843 int(point_offset, hsize_t) + int(step_offset, hsize_t)]
1844
1845 call h5screate_simple_f(2, dcount2, memspace, ierr)
1846 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1847 doffset2, dcount2, ierr)
1848
1849 allocate(vec_component(1, local_points))
1850 call h5dread_f(dset_id, h5t_neko_real, vec_component, dcount2, ierr, &
1851 file_space_id = filespace, mem_space_id = memspace, &
1852 xfer_prp = xf_id)
1853 if (ierr .ne. 0) then
1854 call neko_error('VTKHDF: Cannot read PointData field: ' // &
1855 trim(dset_name))
1856 end if
1857 fld%x = reshape(vec_component, shape(fld%x))
1858 deallocate(vec_component)
1859 else
1860 ! Scalar field: 1D array
1861 dcount1 = int(local_points, hsize_t)
1862 doffset1 = int(point_offset, hsize_t) + int(step_offset, hsize_t)
1863
1864 call h5screate_simple_f(1, dcount1, memspace, ierr)
1865 call h5sselect_hyperslab_f(filespace, h5s_select_set_f, &
1866 doffset1, dcount1, ierr)
1867
1868 call h5dread_f(dset_id, h5t_neko_real, fld%x(1,1,1,1), dcount1, ierr, &
1869 file_space_id = filespace, mem_space_id = memspace, &
1870 xfer_prp = xf_id)
1871 if (ierr .ne. 0) then
1872 call neko_error('VTKHDF: Cannot read PointData field: ' // &
1873 trim(dset_name))
1874 end if
1875 end if
1876
1877 call h5sclose_f(memspace, ierr)
1878 call h5sclose_f(filespace, ierr)
1879 call h5dclose_f(dset_id, ierr)
1880 call h5pclose_f(xf_id, ierr)
1881
1882 if (is_vds) then
1883 call h5fclose_f(ext_file_id, ierr)
1884 else
1885 call h5gclose_f(pointdata_grp, ierr)
1886 end if
1887
1888 end subroutine vtkhdf_read_field
1889
1890#else
1891 ! -------------------------------------------------------------------------- !
1892 ! Dummy functions and subroutines
1893
1894 !> Write data in HDF5 format (no HDF5 support)
1895 subroutine vtkhdf_file_write(this, data, t)
1896 class(vtkhdf_file_t), intent(inout) :: this
1897 class(*), target, intent(in) :: data
1898 real(kind=dp), intent(in), optional :: t
1899 call neko_error('Neko needs to be built with HDF5 support')
1900 end subroutine vtkhdf_file_write
1901
1902 !> Read data in HDF5 format (no HDF5 support)
1903 subroutine vtkhdf_file_read(this, data)
1904 class(vtkhdf_file_t) :: this
1905 class(*), target, intent(inout) :: data
1906 call neko_error('Neko needs to be built with HDF5 support')
1907 end subroutine vtkhdf_file_read
1908
1909#endif
1910
1911 ! -------------------------------------------------------------------------- !
1912 ! Sub-cell node ordering functions for VTK compatibility
1913
1914 !> Build linear hexahedron sub-cell node ordering for a spectral element.
1915 !! Returns an array of 0-based tensor-product indices that subdivides
1916 !! the lx*ly*lz grid into (lx-1)*(ly-1)*(lz-1) linear hexahedra
1917 !! (VTK_HEXAHEDRON, type 12), with 8 nodes per sub-cell.
1918 !! @param lx Number of points in x-direction
1919 !! @param ly Number of points in y-direction
1920 !! @param lz Number of points in z-direction
1921 !! @return Array of size 8*(lx-1)*(ly-1)*(lz-1) with 0-based indices
1922 pure function subdivide_to_hex_ordering(lx, ly, lz) result(node_order)
1923 integer, intent(in) :: lx, ly, lz
1924 integer :: node_order(8 * (lx - 1) * (ly - 1) * (lz - 1))
1925 integer :: ii, jj, kk, idx
1926
1927 idx = 0
1928
1929 do ii = 1, lx - 1
1930 do jj = 1, ly - 1
1931 do kk = 1, lz - 1
1932 node_order(idx + 1) = (kk - 1) * lx * ly + (jj - 1) * lx + ii - 1
1933 node_order(idx + 2) = (kk - 1) * lx * ly + (jj - 1) * lx + ii
1934 node_order(idx + 3) = (kk - 1) * lx * ly + jj * lx + ii
1935 node_order(idx + 4) = (kk - 1) * lx * ly + jj * lx + ii - 1
1936 node_order(idx + 5) = kk * lx * ly + (jj - 1) * lx + ii - 1
1937 node_order(idx + 6) = kk * lx * ly + (jj - 1) * lx + ii
1938 node_order(idx + 7) = kk * lx * ly + jj * lx + ii
1939 node_order(idx + 8) = kk * lx * ly + jj * lx + ii - 1
1940 idx = idx + 8
1941 end do
1942 end do
1943 end do
1944
1945 end function subdivide_to_hex_ordering
1946
1947 !> Build linear quadrilateral sub-cell node ordering for a spectral element.
1948 !! Returns an array of 0-based tensor-product indices that subdivides
1949 !! the lx*ly grid into (lx-1)*(ly-1) linear quadrilaterals
1950 !! (VTK_QUAD, type 9), with 4 nodes per sub-cell.
1951 !! @param lx Number of points in x-direction
1952 !! @param ly Number of points in y-direction
1953 !! @return Array of size 4*(lx-1)*(ly-1) with 0-based indices
1954 pure function subdivide_to_quad_ordering(lx, ly) result(node_order)
1955 integer, intent(in) :: lx, ly
1956 integer :: node_order(4 * (lx - 1) * (ly - 1))
1957 integer :: ii, jj, idx
1958
1959 idx = 0
1960
1961 do jj = 1, ly - 1
1962 do ii = 1, lx - 1
1963 node_order(idx + 1) = (jj - 1) * lx + ii - 1
1964 node_order(idx + 2) = (jj - 1) * lx + ii
1965 node_order(idx + 3) = jj * lx + ii
1966 node_order(idx + 4) = jj * lx + ii - 1
1967 idx = idx + 4
1968 end do
1969 end do
1970
1971 end function subdivide_to_quad_ordering
1972
1973end module vtkhdf_file
__inline__ __device__ void nonlinear_index(const int idx, const int lx, int *index)
Definition bc_utils.h:44
double real
Defines format-independent checkpoint registration and restart state.
Definition comm.F90:1
integer, public pe_size
MPI size of communicator.
Definition comm.F90:62
integer, public pe_rank
MPI rank.
Definition comm.F90:59
type(mpi_comm), public neko_comm
MPI communicator.
Definition comm.F90:46
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public host_to_device
Definition device.F90:48
integer, parameter, public device_to_host
Definition device.F90:48
Defines a mapping of the degrees of freedom.
Definition dofmap.f90:35
Contains the field_serties_t type.
Defines a field.
Definition field.f90:34
HDF5 session management.
subroutine, public hdf5_session_init
Initialise the global HDF5 session HDF5 does not maintain a global reference count,...
subroutine, public hdf5_session_finalize
Finalize the global HDF5 session.
Logging routines.
Definition log.f90:34
type(log_t), public neko_log
Global log stream.
Definition log.f90:92
Defines a mesh.
Definition mesh.f90:34
integer, parameter, public i8
Definition num_types.f90:7
integer, parameter, public qp
Definition num_types.f90:12
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public sp
Definition num_types.f90:8
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Utilities.
Definition utils.f90:35
pure integer function, public linear_index(i, j, k, l, lx, ly, lz)
Compute the address of a (i,j,k,l) array with sizes (1:lx, 1:ly, 1:lz, :).
Definition utils.f90:328
subroutine, public filename_split(fname, path, name, suffix)
Extract file name components.
Definition utils.f90:170
subroutine, public neko_warning(warning_msg)
Reports a warning to standard output.
Definition utils.f90:454
recursive subroutine, public mkdir(path, mode)
Recursively create a directory and all parent directories if they do not exist. This should be safer ...
Definition utils.f90:213
pure integer function, dimension(4), public nonlinear_index(linear_index, lx, ly, lz)
Compute (i,j,k,l) array given linear index with sizes (1:lx, 1:ly, 1:lz, :).
Definition utils.f90:337
VTK Module containing utilities for VTK file handling.
Definition vtk.f90:42
integer function, dimension(:), allocatable, public vtk_ordering(cell_type, lx, ly, lz)
Get the VTK node ordering for a given cell type. For Lagrange cells, returns an array mapping VTK nod...
Definition vtk.f90:60
VTKHDF file format.
subroutine vtkhdf_file_set_overwrite(this, overwrite)
Set the overwrite flag for HDF5 files.
subroutine vtkhdf_file_enable_amr(this)
Enable support for Adaptive Mesh Refinement.
character(len=1024) function vtkhdf_file_get_next_output_fname(this)
Get the physical file name generated by the next write.
subroutine vtkhdf_file_set_precision(this, precision)
Set the precision for VTKHDF output (single or double).
subroutine vtkhdf_file_write(this, data, t)
Write data in HDF5 format (no HDF5 support).
pure integer function, dimension(8 *(lx - 1) *(ly - 1) *(lz - 1)) subdivide_to_hex_ordering(lx, ly, lz)
Build linear hexahedron sub-cell node ordering for a spectral element. Returns an array of 0-based te...
integer, dimension(2), parameter vtkhdf_version
subroutine vtkhdf_file_set_subdivide(this, subdivide)
Enable or disable subdivision of spectral elements into linear sub-cells. When subdivision is enabled...
subroutine vtkhdf_file_read(this, data)
Read data in HDF5 format (no HDF5 support).
character(len=1024) function vtkhdf_file_get_fname(this)
Return the file name with the start counter.
pure integer function, dimension(4 *(lx - 1) *(ly - 1)) subdivide_to_quad_ordering(lx, ly)
Build linear quadrilateral sub-cell node ordering for a spectral element. Returns an array of 0-based...
Collection of live simulation data registered for checkpointing.
field_list_t, To be able to group fields together
A wrapper for a pointer to a field_series_t.
Stores a series (sequence) of fields, logically connected to a base field, and arranged according to ...
A generic file handler.
Interface for HDF5 files.
#define max(a, b)
Definition tensor.cu:40