Skip to content

Parallel and composite files ​

Files that list other files: parallel (partitioned) headers, multi-block assemblies, time series; files written into memory. Complete programs: the cookbook and chapters 4, 6 and 7 of the tutorial. The examples of this page are taken from ref_parallel.f90, compiled and run with the documentation; every parallel header it writes is read back and checked against its pieces.

Parallel Structured Grid (PVTS) ​

For MPI-parallel codes, each rank writes its own partition as a regular VTS file, then a single PVTS header file references all partitions. The program below writes both from one process; the cookbook has the same with real MPI ranks, Write from MPI ranks.

f90
use vtk_fortran, only : pvtk_file
use penf,        only : I4P

type(pvtk_file)         :: a_pvtk_file
integer(I4P), parameter :: nx1=0, nx2=9, ny1=0, ny2=5, nz1=0, nz2=5 ! whole extent
integer(I4P), parameter :: nx1_p(2) = [0, 4]                        ! extents of the pieces along x:
integer(I4P), parameter :: nx2_p(2) = [4, 9]                        ! adjacent pieces share the plane i=4
integer(I4P)            :: error

! --- each rank writes its own piece: a complete .vts file (a few lines of the program, see the cookbook) ---
call write_piece(filename='ref_part_1.vts', i1=nx1_p(1), i2=nx2_p(1))
call write_piece(filename='ref_part_2.vts', i1=nx1_p(2), i2=nx2_p(2))

! --- one rank writes the parallel header ---
error = a_pvtk_file%initialize(filename='ref.pvts', mesh_topology='PStructuredGrid', mesh_kind='Float64', &
                               nx1=nx1, nx2=nx2, ny1=ny1, ny2=ny2, nz1=nz1, nz2=nz2)
error = a_pvtk_file%xml_writer%write_dataarray(location='node', action='open')
error = a_pvtk_file%xml_writer%write_parallel_dataarray(data_name='pressure', data_type='Float64', number_of_components=1)
error = a_pvtk_file%xml_writer%write_dataarray(location='node', action='close')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_1.vts', &
                                                  nx1=nx1_p(1), nx2=nx2_p(1), ny1=ny1, ny2=ny2, nz1=nz1, nz2=nz2)
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_2.vts', &
                                                  nx1=nx1_p(2), nx2=nx2_p(2), ny1=ny1, ny2=ny2, nz1=nz1, nz2=nz2)
error = a_pvtk_file%finalize()

Adjacent partition extents

Adjacent pieces must share the boundary ordinate: nx2_p(1) of piece 1 must equal nx1_p(2) of piece 2. This is required for correct rendering in ParaView.

The extents of two structured pieces, and two unstructured pieces with their ghost cellsStructured pieces: extentsi=0i=4i=9piece 1: i=0..4piece 2: i=4..9whole extent i=0..9: the points of i=4belong to both pieces (nx2 of one = nx1 of the next)Unstructured pieces: ghost cellspiece 100001piece 210000vtkGhostType (UInt8): 0 own cell, 1 ghost cellheader: ghost_level=1; readers do not draw ghosts,so the cells shared by two pieces are drawn once

Parallel Rectilinear Grid (PVTR) ​

A .pvtr header is written as a .pvts one, with mesh_topology='PRectilinearGrid': each piece is a regular .vtr file holding the coordinates of its own extent, and the header declares the coordinates type through mesh_kind (written as PCoordinates). The test src/tests/vtk_fortran_write_pvtr.f90 is a complete example with point and cell data.

f90
error = a_pvtk_file%initialize(filename='ref.pvtr', mesh_topology='PRectilinearGrid', mesh_kind='Float64', &
                               nx1=0, nx2=4, ny1=0, ny2=2, nz1=0, nz2=2)
! ... PPointData / PCellData as for PVTS ...
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_1.vtr', nx1=0, nx2=2, ny1=0, ny2=2, nz1=0, nz2=2)
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_2.vtr', nx1=2, nx2=4, ny1=0, ny2=2, nz1=0, nz2=2)
error = a_pvtk_file%finalize()

Parallel Unstructured Grid (PVTU) ​

The same approach works for unstructured grids: each rank writes its partition as a complete .vtu file, with its own points and a connectivity using local point ids (0 to np-1 of that piece), then one rank writes the .pvtu file. The .pvtu file contains no data: it declares the type of the points coordinates (mesh_kind) and of every data array of the pieces, and lists the pieces. Unstructured pieces have no extents, so write_parallel_geo takes only the piece file name.

f90
use vtk_fortran, only : pvtk_file
use penf,        only : I4P

type(pvtk_file) :: a_pvtk_file
integer(I4P)    :: error

! --- after every rank has written its own ref_part_N.vtu ---
error = a_pvtk_file%initialize(filename='ref.pvtu', mesh_topology='PUnstructuredGrid', mesh_kind='Float64')
error = a_pvtk_file%xml_writer%write_dataarray(location='node', action='open')
error = a_pvtk_file%xml_writer%write_parallel_dataarray(data_name='temperature', data_type='Float64', &
                                                        number_of_components=1)
error = a_pvtk_file%xml_writer%write_dataarray(location='node', action='close')
error = a_pvtk_file%xml_writer%write_dataarray(location='cell', action='open')
error = a_pvtk_file%xml_writer%write_parallel_dataarray(data_name='part', data_type='Int32', number_of_components=1)
error = a_pvtk_file%xml_writer%write_dataarray(location='cell', action='close')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_1.vtu')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_2.vtu')
error = a_pvtk_file%finalize()

The names, types and numbers of components declared in the .pvtu file must match the data arrays written in every piece (a string array is declared with data_type='String', written as a PArray as VTK writes it): check_pieces verifies it, see Parallel headers. See src/tests/vtk_fortran_write_pvtu.f90 for the complete program, pieces included.

Ghost cells ​

When each piece also carries layers of the cells of its neighbours (the ghost cells a solver keeps to compute its own), declare their number in the header with the ghost_level argument of initialize (default 0), written as the GhostLevel attribute, and mark the ghost cells of each piece with the unsigned array vtkGhostType (1 for a ghost cell, see Unsigned integer arrays), declared in the header too. The header of chapter 6 of the tutorial:

f90
! one process writes the header: the layout of the data and the list of the pieces
error = header%initialize(filename='heat.pvtu', mesh_topology='PUnstructuredGrid', mesh_kind='Float64', ghost_level=1)
error = header%xml_writer%write_dataarray(location='node', action='open')
error = header%xml_writer%write_parallel_dataarray(data_name='temperature', data_type='Float64', number_of_components=1)
error = header%xml_writer%write_dataarray(location='node', action='close')
error = header%xml_writer%write_dataarray(location='cell', action='open')
error = header%xml_writer%write_parallel_dataarray(data_name='piece', data_type='Int32', number_of_components=1)
error = header%xml_writer%write_parallel_dataarray(data_name='vtkGhostType', data_type='UInt8', number_of_components=1)
error = header%xml_writer%write_dataarray(location='cell', action='close')
do p=1, pieces
  error = header%xml_writer%write_parallel_geo(source=trim(sources(p)))
enddo
error = header%finalize()

Readers then skip the ghost cells when they draw the dataset, so the cells shared by two pieces are not drawn twice.

Parallel Image Data (PVTI) ​

Each rank writes its piece as a .vti file with the same origin and spacing of the whole grid and the extents of the piece (adjacent pieces share the boundary index, as for PVTS); one rank writes the .pvti file with the whole extents, the same origin and spacing, and the extents of each piece. mesh_kind is not needed: image pieces have no points coordinates.

f90
error = a_pvtk_file%initialize(filename='ref.pvti', mesh_topology='PImageData',          &
                               nx1=0, nx2=15, ny1=0, ny2=15, nz1=0, nz2=7,              &
                               origin=[0._R8P, 0._R8P, 0._R8P], spacing=[0.1_R8P, 0.1_R8P, 0.2_R8P])
error = a_pvtk_file%xml_writer%write_dataarray(location='node', action='open')
error = a_pvtk_file%xml_writer%write_parallel_dataarray(data_name='phi', data_type='Float64', number_of_components=1)
error = a_pvtk_file%xml_writer%write_dataarray(location='node', action='close')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_1.vti', nx1=0, nx2=8,  ny1=0, ny2=15, nz1=0, nz2=7)
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_2.vti', nx1=8, nx2=15, ny1=0, ny2=15, nz1=0, nz2=7)
error = a_pvtk_file%finalize()

See src/tests/vtk_fortran_write_vti.f90 for a complete program, serial and parallel.

A direction given to pvtk_file is written in the .pvti, but VTK 9.7.2 ignores it: its .pvti reader gives an identity direction, whatever the header and the pieces hold (its own writer leaves it out of the header). A rotated image is read with its direction from the .vti pieces only.

Parallel Polygonal Data (PVTP) ​

As for PVTU: each rank writes its piece as a complete .vtp file (local point ids), one rank writes the .pvtp file with the type of the points coordinates (mesh_kind, required) and of each data array, and the list of the pieces:

f90
error = a_pvtk_file%initialize(filename='ref.pvtp', mesh_topology='PPolyData', mesh_kind='Float64')
error = a_pvtk_file%xml_writer%write_dataarray(location='cell', action='open')
error = a_pvtk_file%xml_writer%write_parallel_dataarray(data_name='id', data_type='Int32', number_of_components=1)
error = a_pvtk_file%xml_writer%write_dataarray(location='cell', action='close')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_1.vtp')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_part_2.vtp')
error = a_pvtk_file%finalize()

See src/tests/vtk_fortran_write_vtp.f90 for a complete program with the four blocks, serial and parallel.

Parallel Table (PVTT) ​

The pieces of a Table are .vtt files (mesh_topology='Table', see Table); the .pvtt header declares its columns between write_dataarray(location='row', action='open') and action='close' (PRowData; a string column with data_type='String', a PArray) and lists the pieces with write_parallel_geo(source=...):

f90
error = a_pvtk_file%initialize(filename='ref.pvtt', mesh_topology='PTable')
error = a_pvtk_file%xml_writer%write_dataarray(location='row', action='open')
error = a_pvtk_file%xml_writer%write_parallel_dataarray(data_name='a', data_type='Float64')
error = a_pvtk_file%xml_writer%write_parallel_dataarray(data_name='s', data_type='String')
error = a_pvtk_file%xml_writer%write_dataarray(location='row', action='close')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_table_1.vtt')
error = a_pvtk_file%xml_writer%write_parallel_geo(source='ref_table_2.vtt')
error = a_pvtk_file%finalize()

VTK 9.7.2's vtkXMLPTableReader reads one piece per request (UpdatePiece), not the whole table, and gives its string columns empty: the same with a .pvtt VTK writes. The test src/tests/vtk_fortran_read_composite.f90 writes one and reads it back (check_pieces), as this page's program does.

Multi-block Dataset (VTM) ​

A VTM file is a composite wrapper that references multiple individual VTK files organised into named blocks.

f90
use vtk_fortran, only : vtm_file, vtk_file
use penf,        only : I4P, R8P

type(vtk_file)  :: a_vtk_file
type(vtm_file)  :: a_vtm_file
character(16)   :: filenames(4)
real(R8P)       :: x(0:3), y(0:2), z(0:2)
integer(I4P)    :: f, error

filenames = ['ref_block_1.vtr', 'ref_block_2.vtr', 'ref_block_3.vtr', 'ref_block_4.vtr']
y = [0, 1, 2] ; z = [0, 1, 2]
! write each block as an independent file
do f=1, size(filenames, dim=1)
  x = [0, 1, 2, 3] + 4._R8P*(f - 1)
  error = a_vtk_file%initialize(format='raw', filename=trim(filenames(f)), mesh_topology='RectilinearGrid', &
                                nx1=0, nx2=3, ny1=0, ny2=2, nz1=0, nz2=2)
  error = a_vtk_file%xml_writer%write_piece(nx1=0, nx2=3, ny1=0, ny2=2, nz1=0, nz2=2)
  error = a_vtk_file%xml_writer%write_geo(x=x, y=y, z=z)
  error = a_vtk_file%xml_writer%write_piece()
  error = a_vtk_file%finalize()
enddo

! assemble the multi-block file
error = a_vtm_file%initialize(filename='ref.vtm')
error = a_vtm_file%write_block(filenames=[filenames(1), filenames(2)], names=['1','2'], name='first block')
error = a_vtm_file%write_block(filenames=[filenames(3), filenames(4)], names=['3','4'], name='second block')
error = a_vtm_file%finalize()

Nested blocks ​

Blocks can contain both datasets and other blocks, to any depth, e.g. to mirror the parts and assemblies of a CAD model. Build the hierarchy step by step with the action argument of write_block:

CallEffect
write_block(action='open', name=...)open a child block of the current block (or a top-level block)
write_block(filenames=[...], names=[...], action='write')write the files as datasets of the current block
write_block(filenames=[...], names=[...], name=...)write the files wrapped in a new child block
write_block(action='close')close the current block

An empty file name is an empty dataset (VTK's null block): <DataSet index="..." file=""/> in a .vtm, read back by VTK as an empty block; in a VTKHDF multi-block file a group Block_<n> without attributes, linked as the others (named after names, Block_<n> otherwise; outside the time series rules); refused in a VTKHDF collection, which VTK 9.7.2 then cannot read. vtm_file reading gives an empty dataset an empty file.

f90
error = a_vtm_file%initialize(filename='ref_machine.vtm')
error = a_vtm_file%write_block(action='open', name='assembly')                                  ! Block 0
error = a_vtm_file%write_block(filenames=['a.vtu', 'b.vtu'], names=['a', 'b'], action='write')   !   DataSet 0, 1
error = a_vtm_file%write_block(filenames=['b1.vtu', 'b2.vtu'], name='bolts')                     !   Block 2: DataSet 0, 1
error = a_vtm_file%write_block(action='open', name='sub-assembly')                              !   Block 3
error = a_vtm_file%write_block(filenames=['n.vtu'], name='nuts')                                 !     Block 0: DataSet 0
error = a_vtm_file%write_block(action='close')                                                  !   (end of Block 3)
error = a_vtm_file%write_block(action='close')                                                  ! (end of Block 0)
error = a_vtm_file%finalize()
  • The children of a block (blocks and datasets together) get the indexes 0, 1, 2, … in the order they are written, as VTK numbers the children of a multi-block dataset. Every action='open' needs its action='close'.
  • The string form (filenames='a.vtu b.vtu', names separated by blanks) accepts the same action values.
  • write_block(filenames=[...], name=..., action='pieces') wraps the files in a Piece: a vtkMultiPieceDataSet, the partitions of one dataset, as VTK writes them in a .vtm (an XML .vtm only; the partitioned composites hold their partitions as the datasets of a block).
  • The test src/tests/vtk_fortran_write_vtm_nested.f90 writes the hierarchy of issue #25, checked with VTK's reader.

Multi-block in one VTKHDF file ​

With format='vtkhdf' (the library built with HDF5), vtm_file writes one VTKHDF file, a MultiBlockDataSet, with the same write_block calls: each dataset, a .vtkhdf file written by vtk_file, is copied into it (the group Block_<n> of /VTKHDF), and the tree of the blocks is the group Assembly, whose groups are the blocks and whose soft links, named after the datasets (names, or the file names without directory and extension), point to the copies; the names of a block must differ. The copied files can be deleted afterwards. scratch units are refused (the blocks are written in order). A block is a single dataset: a composite file (a multi-block file or a collection) or an OverlappingAMR is refused as its source (the blocks of a composite are not composite, VTKHDF specification; VTK 9.7.2 crashes on a composite copied in, and reads the multi-block without an OverlappingAMR block). src/tests/vtk_fortran_write_vtkhdf_vtm.f90 writes the nested assembly above in VTKHDF.

A PartitionedDataSetCollection. With initialize(..., format='vtkhdf', composite='PartitionedDataSetCollection') the file is VTK's other composite (vtkPartitionedDataSetCollection, VTKHDF 2.1), written by the same write_block calls. The two compared:

MultiBlockDataSet (default)PartitionedDataSetCollection
Copy of a datasetthe group Block_<n>the group of its name, with the attribute Index (0, 1, ... as written)
A dataset in two blockscopied twice, or copied once and placed in other blocks with references (its name must be unique)copied once, then placed in other blocks with write_block(references='name ...')
Dataset namesunique in a blockunique in the file (a second one is refused: reference the first), not Assembly
VTK outputvtkMultiBlockDataSet, the blocks nestedvtkPartitionedDataSetCollection, flat, the tree in its vtkDataAssembly

references is an argument of the string form of write_block (blank separated, as filenames), alone (write_block(references='pump', name='rotating'), a new block) or with action='write' (the open block): soft links to the copies, no new copy. In a multi-block file a name given to several datasets cannot be referenced (ambiguous); an XML .vtm refuses references (it repeats the file; an XML .vtpc takes them, below). VTK 9.7.2 reads a referenced dataset in each of its blocks. The time series rules below are the same. ParaView 6.1.1 reads a collection and plays every step; VTK 9.7.2 as for a multi-block file (each step with SetStep). src/tests/vtk_fortran_write_vtkhdf_vtm.f90 writes the nested assembly and a time series as collections, each with a block referencing two datasets already copied, and a multi-block file referencing two datasets.

XML partitioned composites. Without format='vtkhdf', composite='PartitionedDataSetCollection' writes VTK's XML collection, a .vtpc, with the same calls: each dataset (one file) a <Partitions name=...> written once, the blocks the nodes of its vtkDataAssembly (stored base64 in <DataAssembly>, as VTK writes it; a block name must be an XML element name, not dataset, which vtkDataAssembly reserves), references placing a dataset in other blocks, an empty file name an empty dataset (named dataset_<n> without names). A refused dataset leaves its block open, as in a .vtm: finalize closes it, writes every dataset and returns the error. composite='PartitionedDataSet' writes a .vtpd, a flat list of partitions: write_block(filenames=[...], action='write'), no blocks (refused, as format='vtkhdf': a .vtkhdf has partitions of its own). VTK 9.7.2 and ParaView read both; vtm_file reads them back (get_entries).

A time series of several meshes. The blocks can be time series (write_step), so that one file holds several meshes moving together: see A time series of several meshes in one .vtkhdf file.

  • A file is either static or a time series: its first block decides, and a block of the other kind is refused (VTK cannot read the mix, VTK issue 19963). Make a fixed part a time series too, the same mesh at every step.
  • Every block has the same steps: their number and their times, compared exactly (the specification requires it). A block with other steps is refused, the message naming the first step that differs.
  • The time-series blocks are of any type but a Table (VTK 9.7.2 reads a Table-series block without rows): an UnstructuredGrid or PolyData of one or more parts per step, an ImageData, RectilinearGrid, StructuredGrid or HyperTreeGrid.
  • Readers: ParaView 6.1.1 plays every step of every block (it reads no RectilinearGrid nor StructuredGrid VTKHDF file, a block or not). VTK 9.7.2 reads each step with vtkHDFReader::SetStep called before the first update; its pipeline time (UpdateTimeStep), or SetStep after UpdateInformation, leaves an UnstructuredGrid or PolyData block at the first step and a grid block without its arrays, VTK's own multi-block files too (a single dataset follows it).
  • A refused file leaves the block opened for it by write_block open, as a missing file does: finalize then returns an error.

Reading it back. vtm_file%initialize(filename, action='read') lists the blocks and datasets of a VTKHDF multi-block file or collection (get_entries: the groups of its Assembly and the links in them, in the order written); the file of a dataset is its group under /VTKHDF, which vtk_file%initialize(filename, action='read', dataset=file) reads as a .vtkhdf of its own, steps included. See Reading.

Blocks filled in any order ​

When the files of the blocks are known one at a time, interleaved (e.g. a solver that writes, at each output, one file per part of its model), give each block a scratch unit: initialize with scratch_units_number, then write_block with the number of the unit (scratch) to open the block (action='open', its name) and to add each of its files (action='write', one filename per call). The files are kept in the scratch units, and finalize writes the blocks, in the order of their units:

f90
error = a_vtm_file%initialize(filename='ref_series.vtm', scratch_units_number=2)       ! 2 blocks
error = a_vtm_file%write_block(scratch=1, action='open', name='fluid domain')         ! the name of each block
error = a_vtm_file%write_block(scratch=2, action='open', name='solid')
do step=1, 3   ! the files of the blocks are known one at a time, interleaved
  error = a_vtm_file%write_block(scratch=1, action='write', filename='fluid/step_'//trim(str(step, .true.))//'.vtu')
  error = a_vtm_file%write_block(scratch=2, action='write', filename='solid/step_'//trim(str(step, .true.))//'.vtu')
enddo
error = a_vtm_file%finalize()                                                         ! writes the blocks
$ cat ref_series.vtm
<?xml version="1.0"?>
<VTKFile type="vtkMultiBlockDataSet" version="1.0" byte_order="LittleEndian" header_type="UInt32">
  <vtkMultiBlockDataSet>
    <Block index="0" name="fluid domain">
      <DataSet index="0" file="fluid/step_1.vtu" name="fluid/step_1.vtu"/>
      <DataSet index="1" file="fluid/step_2.vtu" name="fluid/step_2.vtu"/>
      <DataSet index="2" file="fluid/step_3.vtu" name="fluid/step_3.vtu"/>
    </Block>
    <Block index="1" name="solid">
      <DataSet index="0" file="solid/step_1.vtu" name="solid/step_1.vtu"/>
      <DataSet index="1" file="solid/step_2.vtu" name="solid/step_2.vtu"/>
      <DataSet index="2" file="solid/step_3.vtu" name="solid/step_3.vtu"/>
    </Block>
  </vtkMultiBlockDataSet>
</VTKFile>
  • The blocks are written at the top level, one per unit, and the name of each dataset is its file name.
  • It can be mixed with the other forms: the blocks written with them come first, the scratch blocks follow at finalize.

OverlappingAMR (VTHB) ​

An XML OverlappingAMR is a .vthb file listing the boxes of each level, each box an ImageData file (.vti), as vtkXMLUniformGridAMRWriter writes it. Write each box with vtk_file (mesh_topology='ImageData'): the origin of the AMR, the spacing of its level, and its extent in the point indices of that level, which places it. Then list the boxes level by level with vtm_file:

f90
error = amr%initialize(filename='ref.vthb', composite='OverlappingAMR')
error = amr%write_level(level=0, filenames=['ref_amr_0.vti'])
error = amr%write_level(level=1, filenames=['ref_amr_1.vti', 'ref_amr_2.vti'])
error = amr%finalize()
  • initialize(filename, composite='OverlappingAMR') (XML only: a VTKHDF OverlappingAMR is one file written by vtk_file, see Formats); write_level(level, filenames) gives the boxes of the levels 0, 1, ... in order, each level once; the .vthb is written by finalize.
  • write_level reads each box: the origin (the same for every box), the spacing (the same in a level: the level's) and the extent are taken from the files, the amr_box of each box is its extent in cells (a flat axis 0 -1), and the grid description (XYZ, XY, ...) the axes of the first box. A box that cannot be read, is not an ImageData, or disagrees is refused. A relative file is relative to the directory of the .vthb.
  • write_block is refused in an OverlappingAMR, write_level elsewhere.
  • VTK 9.7.2 reads it (vtkXMLUniformGridAMRReader): levels, boxes, spacing and data. ParaView 6.1.1 opens no OverlappingAMR (VTK's own .vthb neither: its reader groups the output with a filter that refuses the type).
  • vtm_file%initialize(filename, action='read') lists a .vthb (and a former .vth): its levels as blocks (index the level), its boxes as datasets with their files.

Time series (PVD) ​

A .pvd file is a collection: it lists the files of a simulation (any VTK XML file written by vtk_file, pvtk_file or vtm_file) with their time, and ParaView loads them as a single time series. Write each time step as usual, then add it to the collection with pvd_file:

f90
use vtk_fortran, only : pvd_file
use penf,        only : I4P, R8P, strz

type(pvd_file)          :: a_pvd_file
integer(I4P), parameter :: nsteps = 3
integer(I4P)            :: step, error
real(R8P)               :: time

error = a_pvd_file%initialize(filename='ref_simulation.pvd')
do step=0, nsteps
  time = step / 10._R8P
  ! ... advance the solution to time, write ref_simulation_NNNN.vtu with a vtk_file ...
  error = a_pvd_file%write_dataset(filename='ref_simulation_'//trim(strz(step, 4))//'.vtu', timestep=time)
enddo
error = a_pvd_file%finalize()

writes

$ cat ref_simulation.pvd
<?xml version="1.0"?>
<VTKFile type="Collection" version="1.0" byte_order="LittleEndian">
  <Collection>
    <DataSet timestep="0.0" part="0" file="ref_simulation_0000.vtu"/>
    <DataSet timestep="0.1" part="0" file="ref_simulation_0001.vtu"/>
    <DataSet timestep="0.2" part="0" file="ref_simulation_0002.vtu"/>
    <DataSet timestep="0.3" part="0" file="ref_simulation_0003.vtu"/>
  </Collection>
</VTKFile>

ParaView opens the collection as one dataset with a time axis: each time step shows the file listed for it.

  • The file is valid at any time. Each write_dataset writes its entry and the closing tags, and flushes the file: a run that stops before finalize (crash, job killed by the scheduler) still leaves a collection that ParaView opens, with all the steps written so far.
  • Restart. pvd%initialize(filename='simulation.pvd', action='append') reopens an existing collection and adds the new steps after the ones already listed (the default action='new' replaces the file). The file must exist and be a collection, otherwise initialize returns a non-zero error. Anything after the closing tags of the reopened file (e.g. a comment added by hand) is overwritten with blanks.
  • Optional attributes. write_dataset also accepts part (default 0; e.g. the rank, when each process writes its own file per step), group and name.
  • timestep is a real(R8P), written with the shortest representation that reads back exactly. filename is written as given: relative paths are relative to the directory of the .pvd file.

A ParaView file series. A file name ending in .series (e.g. simulation.vtu.series: ParaView picks the reader of the files by the extension before .series) writes ParaView's JSON file series instead, with the same calls, valid after each write_dataset and continued with action='append' as well:

json
{
  "file-series-version" : "1.0",
  "files" : [
    { "name" : "simulation_0000.vtu", "time" : 0.0000000000000000E+000 },
    { "name" : "simulation_0001.vtu", "time" : 1.0000000000000001E-001 }
  ]
}

It lists files and times only: part, group and name are refused. The times are written with 17 digits (they read back exactly). ParaView 6.1.1 reads it as the .pvd.

See src/tests/vtk_fortran_write_pvd.f90 for a complete program, restart and .series included.

One VTKHDF file instead of a collection ​

The VTKHDF format holds a whole time series in one HDF5 file: with format='vtkhdf', each output starts with xml_writer%write_step(time), followed by its field data and its pieces, written with the usual calls; the group Steps of the file tells where each step starts in the datasets that the steps share. A complete program: A time series in one .vtkhdf file.

The structure of a VTKHDF file holding a time series of an unstructured gridseries.vtkhdf: an UnstructuredGrid in 3 steps, 2 parts, then 1, then the same mesh/VTKHDF Type, VersionNumberOfPoints [parts]NumberOfCells [parts]Points [points, 3]Connectivity, Offsets, TypesPointData/T [values]CellData/... [values]FieldData/TIME [steps]Steps NSteps = 3Values[0.0, 0.5, 1.0]PartOffsets[0, 2, 2]NumberOfParts[2, 1, 1]PointOffsets[0, 9, 9]CellOffsets ...[0, 4, 4]PointDataOffsets/T[0, 9, 15]FieldDataSizes/TIME[[1,1], ...]step 3 repeats the offsets of step 2NumberOfPoints456PointsPointData/Tone entry per part: the parts of every step, in orderstep 1, part 1step 1, part 2step 2step 3PointOffsets: the first point of each step;the other offsets of Steps do the same forthe cells, the ids, the parts, the arrays;what a step does not write, it reusesby repeating the offset of the step beforea grid (ImageData, ...) has no parts: its arraysget a time axis, [t, k, j, i] in C order
.pvd collectionone .vtkhdf
Filesone per step, any XML format, plus the collectionone
Valid during the runafter every stepafter every piece
Restartpvd_file%initialize(..., action='append')vtk_file%initialize(format='vtkhdf', ..., action='append')
Stepsany time, any orderincreasing times; the arrays of the first step
What does not changewritten again in every filestored once: the offsets of the step before repeated
Reading back with VTKFortranyesyes, step by step (set_step)
  • action='append' reads the state of the series from the file (topology, extent, steps, parts, arrays and where each one was last written) and drops what an interrupted run left after its last complete piece; the next write_step needs a later time.

  • What does not change is stored once: a step repeats the offsets of the step before for what it does not write, as the diagram shows for the mesh of step 3 (A fixed mesh stored once):

    • the mesh of an UnstructuredGrid or a PolyData: a step after the first one without pieces (write_step, then its data groups opened directly with write_dataarray(location=..., action='open')) reuses all the parts of the step before; its arrays hold the points (cells) of all those parts, in their order;
    • the geometry of a RectilinearGrid or StructuredGrid: a piece without write_geo reuses the coordinates or points of the step before (an ImageData has none);
    • an array not written in a step reuses its values of the step before: in a step of a grid (with or without its piece) and in a step without pieces. A step with pieces writes every point and cell array, since its parts can differ from those the values were written for. A field data array not written reuses its values too.

    The first step writes everything, and an array first written at a later step is refused. The files are read by VTK 9.7.2 and ParaView 6.1.1 as if every step were written in full (checked step by step against the XML files of the same steps). Point and cell data offsets (PointDataOffsets, CellDataOffsets) are written for every array: tuples, time slices for a grid.

  • VTK 9.7.2 reads two kinds of series wrongly, in the files of its own writer too: a PolyData block of cells (lines, say) that first appears at a later step is read empty at that step; a series of one part at its first step is read as one dataset, of which a later step with more parts keeps only the last. Write every block from the first step (empty blocks are fine afterwards), and the largest number of parts at the first step, or use a .pvd collection.

Threads and processes ​

MPI processes write their own files at the same time with no limitation: they share no memory. Each rank writes its piece, one rank the header, see Write from MPI ranks. With a parallel HDF5 the ranks can also write one .vtkhdf file together, each rank a partition of it (or a region of a grid), no header, time series included: see Write one .vtkhdf from MPI ranks.

OpenMP threads writing (or reading) different files at the same time is a matter of the compiler that builds the library. VTKFortran and its dependencies keep the state of each file in its own vtk_file and share no variable that changes while files are written, but for the constant tables of PENF and BeFoR64, filled by the first initialize: initialize one file (or call penf_init and b64_init) before the parallel region. Some compilers add shared variables of their own:

CompilerFiles from threads
Intel ifx 2025.3✅ the library (and its dependencies) built with -recursive: the CMake and FoBiS builds add it
NVIDIA nvfortran 26.9✅ the library (and its dependencies) built with -mp, which locks the input/output statements only in code built so: the CMake build adds it, to the link of the programs that use the library too (they link the OpenMP runtime)
GNU gfortran, up to 16.2❌ the length of a function result of deferred-length character (character(len=:), allocatable) is a static variable, shared by the threads (GCC bug 113797); gfortran 14 and 15 also make static every variable of a type with user-defined input/output, as the strings of StringiFor. The files are corrupted, or the program crashes: write the files from one thread, or from MPI processes

The test src/tests/vtk_fortran_write_openmp.f90 writes 8 files from 4 threads, in every format, compares them byte by byte with the same files written serially, and reads them back from the threads, 25 times over: a race shows only now and then. CMake runs it with -DVTKFORTRAN_TEST_OPENMP=ON, e.g. cmake -S . -B build -DBUILD_TESTING=ON -DVTKFORTRAN_TEST_OPENMP=ON -DCMAKE_Fortran_COMPILER=ifx: it passes 20 runs out of 20 with ifx and nvfortran, and fails with gfortran. With other builds (fpm, your own build system) add -recursive (ifx) or -mp (nvfortran) to the flags of the library and of its dependencies, e.g. fpm build --flag -recursive.

Appended formats from threads, up to 3.0.1

Up to 3.0.1 the appended formats (raw, raw-zlib, binary-appended) closed their scratch file twice. A unit number, once closed, can be given to a file opened by another thread, which the second close then closed: about one run in 60 of the test failed, more under load, with the other thread looping on a read error (and writing it without end). The writer now closes it once, and stops on any read error. Older versions: write the appended formats from one thread, or use binary.

Volatile XML output ​

In some parallel setups only one process (the master) can access the file system. The other processes can write their files into memory instead: initialize the file with is_volatile=.true., write it as usual, then get its content as a string, send it to the master, which writes it to disk with write_xml_volatile.

f90
use vtk_fortran, only : vtk_file, write_xml_volatile
use penf,        only : I4P, R8P

type(vtk_file)                :: a_vtk_file
character(len=:), allocatable :: xml_volatile
integer(I4P)                  :: error

! on a process without access to the file system
error = a_vtk_file%initialize(format='binary', filename='ref_volatile.vtr', mesh_topology='RectilinearGrid', &
                              is_volatile=.true., nx1=0, nx2=1, ny1=0, ny2=1, nz1=0, nz2=1)
error = a_vtk_file%xml_writer%write_piece(nx1=0, nx2=1, ny1=0, ny2=1, nz1=0, nz2=1)
error = a_vtk_file%xml_writer%write_geo(x=[0._R8P, 1._R8P], y=[0._R8P, 1._R8P], z=[0._R8P, 1._R8P])
error = a_vtk_file%xml_writer%write_piece()
error = a_vtk_file%finalize()
call a_vtk_file%get_xml_volatile(xml_volatile) ! the whole file, as a string
call a_vtk_file%free                           ! free the memory of the volatile file
! ... send xml_volatile to the master ...

! on the master
error = write_xml_volatile(xml_volatile=xml_volatile, filename='ref_volatile.vtr')
  • The binary and ascii formats support volatile files. The appended formats (raw, raw-zlib, binary-appended) write their data to the file directly: with is_volatile=.true., initialize returns a non-zero error.
  • The string is the exact content of the file: written by the master, it is identical to the file written directly (the test src/tests/vtk_fortran_write_volatile.f90 checks it).