Appearance
Data arrays
What goes with the mesh: point and cell data of every shape and kind, global field data, the active arrays of the readers, unsigned arrays. Complete programs: the cookbook and chapter 3 of the tutorial. The examples of this page are taken from ref_data.f90, compiled and run with the documentation.
Writing arrays
The calls are the same in every format, VTKHDF included (see VTKHDF for how it stores the arrays). Point data and cell data are written between an open and a close of their location, write_dataarray(location='node', action='open') (<PointData>; location='cell' for <CellData>) and write_dataarray(location='node', action='close'), with any number of write_dataarray(data_name=..., x=...) in between.
Names: any name in the XML formats: the characters XML escapes (", &, <, >, ') are written escaped and read back as given, in the .pvd too. In VTKHDF a name (of a point, cell or field data array, and of a block or dataset of a multi-block file) is an HDF5 link name: it cannot be empty nor contain / or . (the VTKHDF specification), and such a name is refused with a message naming the character; it is never renamed, so that a program reading the file finds its arrays under the names it wrote.
Non-finite values (NaN, ±Infinity): kept by every format. In ascii they are written as VTK writes them, nan, inf, -inf, and read back so (also Infinity, and the + Infinity of VTKFortran 3.x). VTK 9.7.2 reads an ASCII -inf as +inf, in its own files too: use a binary format to give VTK a negative infinity.
An array has one tuple per point (node) or per cell (cell) of the piece, and each tuple has one or more components: 1 for a scalar, 3 for a vector, 6 or 9 for a tensor. write_dataarray takes the components from the shape of what it is given:
| Arguments | Components | Tuples |
|---|---|---|
x(n), rank 1 | 1 | n |
x(nc,n), rank 2 | nc = size(x,1) | n |
x(nc,ni,nj,nk), rank 4 | nc = size(x,1) | ni*nj*nk |
x of rank 2, 3 or 4 with one_component=.true. | 1 | size(x) |
x, y, z, three arrays of rank 1 or 3 | 3, interleaved: x(1), y(1), z(1), x(2), ... | size(x) |
u, v, w, x, y, z, six arrays of rank 1 or 3 | 6, interleaved in this order | size(x) |
The kind of the arrays selects the VTK type: R8P Float64, R4P Float32, I8P Int64, I4P Int32, I2P Int16, I1P Int8 (for the unsigned types see below). The tuples are taken in the Fortran order of the array elements: for a structured grid i varies first, then j, then k, which is the order of the points (and of the cells) of VTK. For a symmetric tensor VTK expects the components XX, YY, ZZ, XY, YZ, XZ: pass them in this order as u, v, w, x, y, z, or as the first dimension of a rank-2 array.
f90
real(R8P) :: s(8) ! rank 1: one value per point
real(R8P) :: t(2,2,2) ! rank 3, (i,j,k): a scalar field, needs one_component=.true.
real(R8P) :: vec(3,8) ! rank 2, (components, points)
real(R8P) :: vec4(3,2,2,2) ! rank 4, (components, i, j, k)
real(R8P) :: vx(8), vy(8), vz(8) ! the 3 components as separate rank-1 arrays
real(R8P) :: wx(2,2,2), wy(2,2,2), wz(2,2,2) ! the 3 components as separate rank-3 arrays
real(R8P) :: txx(8), tyy(8), tzz(8), txy(8), tyz(8), txz(8) ! a symmetric tensor as 6 separate arrays
real(R8P) :: full(9,8) ! rank 2: a full tensor, 9 componentsf90
error = a_vtk_file%xml_writer%write_dataarray(data_name='s', x=s)
error = a_vtk_file%xml_writer%write_dataarray(data_name='t', x=t, one_component=.true.)
error = a_vtk_file%xml_writer%write_dataarray(data_name='vec', x=vec)
error = a_vtk_file%xml_writer%write_dataarray(data_name='vec4', x=vec4)
error = a_vtk_file%xml_writer%write_dataarray(data_name='v', x=vx, y=vy, z=vz)
error = a_vtk_file%xml_writer%write_dataarray(data_name='w', x=wx, y=wy, z=wz)
error = a_vtk_file%xml_writer%write_dataarray(data_name='sym', u=txx, v=tyy, w=tzz, x=txy, y=tyz, z=txz)
error = a_vtk_file%xml_writer%write_dataarray(data_name='full', x=full)Read back, each array has the components and tuples of the table (a grid of 2x2x2 points):
$ ref_data
ref_shapes.vts, node data:
s Float64 1 components 8 tuples
t Float64 1 components 8 tuples
vec Float64 3 components 8 tuples
vec4 Float64 3 components 8 tuples
v Float64 3 components 8 tuples
w Float64 3 components 8 tuples
sym Float64 6 components 8 tuples
full Float64 9 components 8 tuplesA scalar field stored in an array of rank 2 to 4 needs one_component=.true.
Without it the first dimension is taken as the components: the rank-3 t(2,2,2) becomes an array of 2 components, a file that readers reject or misread.
f90
error = a_vtk_file%xml_writer%write_dataarray(data_name='t_wrong', x=t) ! WRONG: 2 components, 4 tuples$ grep NumberOfComponents ref_trap.vts
<DataArray type="Float64" NumberOfComponents="2" Name="t_wrong" format="ascii">Field data (global metadata)
FieldData holds values that belong to the whole dataset rather than to points or cells: time, cycle number, solver name and version, a history of residuals, etc. Write it right after initialize, before the first piece, between an open and a close call:
f90
error = a_vtk_file%xml_writer%write_fielddata(action='open')
error = a_vtk_file%xml_writer%write_fielddata(data_name='TIME', x=0.5_R8P) ! scalar
error = a_vtk_file%xml_writer%write_fielddata(data_name='CYCLE', x=7_I8P) ! scalar
error = a_vtk_file%xml_writer%write_fielddata(data_name='residuals', x=[1.e-3_R8P, 1.e-4_R8P]) ! rank-1 array
error = a_vtk_file%xml_writer%write_fielddata(data_name='solver', x='my solver v1.2') ! string
error = a_vtk_file%xml_writer%write_fielddata(data_name='species', x=['N2', 'O2']) ! array of strings
error = a_vtk_file%xml_writer%write_fielddata(action='close')x | Written as |
|---|---|
scalar of any PENF kind (R8P, R4P, I8P, I4P, I2P, I1P) | DataArray with 1 tuple |
| rank-1 array of any PENF kind | DataArray with size(x) tuples, one component each |
rank-2 array x(components, tuples) of any PENF kind | DataArray with size(x, 2) tuples of size(x, 1) components |
character scalar | Array type="String" with 1 tuple |
character rank-1 array | Array type="String" with size(x) tuples, one string each |
Strings are written as VTK writes them (each string followed by a NUL character), in every output format, and readers return them as string arrays. Trailing blanks of each string are trimmed, so a Fortran array of fixed-length strings can be passed as is. In ParaView, field data are listed in the Spreadsheet view (attribute Field Data) and are available to filters and Python. VTKFortran reads them back with read_dataarray(location='field', ...), see Reading files.
In the VTKHDF format, field data are the datasets of the group FieldData; strings are variable-length string datasets, as VTK's vtkHDFWriter writes them (in a time series the strings of each step, of any number; refused with comm: parallel HDF5 writes no variable-length type). VTK 9.7.2 reads them, static and step by step.
Component names
set_component_names(names) names the components of the next data array written (one name per component, trailing blanks trimmed), written as VTK writes them, ComponentName0="u" ComponentName1="v" ...: ParaView shows them in its lists of components. They apply to the next array only. Read back with get_dataarray_info(..., component_names=names). The VTKHDF format has no component names (not in the specification; VTK's vtkHDFWriter drops them): the call is accepted, nothing is written.
fortran
error = a_vtk_file%xml_writer%set_component_names(['u', 'v', 'w'])
error = a_vtk_file%xml_writer%write_dataarray(data_name='velocity', x=vx, y=vy, z=vz)String arrays
Point data, cell data and the columns of a Table can be strings too: write_dataarray(data_name=..., x=strings), a character rank-1 array, one string per point, cell or row (trailing blanks trimmed), written as VTK writes a vtkStringArray, <Array type="String">, in every XML format; read back into a character(len=:), allocatable :: x(:). In a .pvtu/.pvtr/... header declare them with write_parallel_dataarray(data_name=..., data_type='String') (a PArray). In the VTKHDF format they are refused: VTK 9.7.2 reads the strings of a VTKHDF file as field data only.
Active arrays
Readers such as ParaView color a dataset by its active scalars and use its active vectors, normals, tensors and texture coordinates by default. Without a designation they pick the first suitable array, which is often not the one wanted. Designate the active array of each role when opening the node or cell data, with the optional scalars, vectors, normals, tensors and tcoords arguments, and the other roles of VTK (below): each one is the data_name of an array written inside that tag.
f90
error = a_vtk_file%xml_writer%write_dataarray(location='node', action='open', scalars='pressure', vectors='velocity')
error = a_vtk_file%xml_writer%write_dataarray(data_name='temperature', x=t) ! not active
error = a_vtk_file%xml_writer%write_dataarray(data_name='pressure', x=p) ! active scalars
error = a_vtk_file%xml_writer%write_dataarray(data_name='velocity', x=u, y=v, z=w) ! active vectors
error = a_vtk_file%xml_writer%write_dataarray(location='node', action='close')writes <PointData Scalars="pressure" Vectors="velocity"> (<CellData ...> for location='cell').
| Argument | Attribute | Array components expected by readers |
|---|---|---|
scalars | Scalars | 1 (up to 4) |
vectors | Vectors | 3 |
normals | Normals | 3 |
tensors | Tensors | 9 (or 6, symmetric) |
tcoords | TCoords | 1 to 3 |
global_ids | GlobalIds | 1, integer: the global id of each point or cell |
pedigree_ids | PedigreeIds | 1: the id of each point or cell in the original data |
edge_flags | EdgeFlag | 1 (UInt8, write_dataarray_unsigned) |
tangents | Tangents | 3 |
rational_weights | RationalWeights | 1: the weights of rational (NURBS) cells |
higher_order_degrees | HigherOrderDegrees | 3: the degrees of higher-order (Lagrange, Bézier) cells |
process_ids | ProcessIds | 1, integer: the process of each point or cell |
Each argument is optional and independent: omit them all and the tag is written as before, without attributes.
The names are written as given, not checked: make sure an array with that name is written inside the same tag.
The arguments apply to
action='open'only, and are ignored when closing.In parallel files the same arguments designate the active arrays in
<PPointData>/<PCellData>: pass them to thepvtk_filewriter with the arrays declared bywrite_parallel_dataarray.In the VTKHDF format the role is the
Attributeattribute of the dataset of the array (e.g.Attribute = "Scalars"), written when the array is: the name must match an array written in the same location, or nothing is marked.Reading back:
xml_reader%get_active(location, role, data_name, piece)returns the name of the active array of a role (case insensitive), empty if none, in every format VTKHDF included; see Reading.
See src/tests/vtk_fortran_write_active_arrays.f90 for a complete program, parallel header and every role included.
Partitions, ghosts and global ids
A mesh split into pieces (partitions, one per rank) has the points of the boundaries in more than one piece. VTK finds them from two arrays: the global ids (global_ids=), the same id for the same point (or cell) in every piece, and the ghosts (vtkGhostType, unsigned, see below), 1 marking a duplicate: filters that merge, clean or extract the surface of the partitions then remove the seams. Here two tetrahedra sharing a face, a piece each:
f90
error = a_vtk_file%xml_writer%write_piece(np=4, nc=1)
error = a_vtk_file%xml_writer%write_geo(np=4, nc=1, x=x, y=y, z=z)
error = a_vtk_file%xml_writer%write_connectivity(nc=1, connectivity=[0,1,2,3], offset=[4], cell_type=[10_I1P])
error = a_vtk_file%xml_writer%write_dataarray(location='node', action='open', global_ids='id')
error = a_vtk_file%xml_writer%write_dataarray(data_name='id', x=ids) ! the same id in both
error = a_vtk_file%xml_writer%write_dataarray_unsigned(data_name='vtkGhostType', x=ghost) ! 1: a duplicate
error = a_vtk_file%xml_writer%write_dataarray(location='node', action='close')
error = a_vtk_file%xml_writer%write_dataarray(location='cell', action='open', global_ids='cell_id')
error = a_vtk_file%xml_writer%write_dataarray(data_name='cell_id', x=[cell_id])
error = a_vtk_file%xml_writer%write_dataarray(location='cell', action='close')
error = a_vtk_file%xml_writer%write_piece()The three points of the shared face are in both pieces, with the same global ids, ghosts in the second. The same calls in VTKHDF (each piece a partition) write the Attribute of the datasets GlobalIds.
Unsigned integer arrays
The VTK XML format has unsigned integer types (UInt8, UInt16, UInt32, UInt64), which Fortran lacks. Write a rank-1, one-component integer array as unsigned with write_dataarray_unsigned: the kind of the array selects the type of the same width, and its bits are written as they are.
| Fortran kind | VTK type | Stored value for unsigned v |
|---|---|---|
I1P | UInt8 | v if v < 128, else v - 256 |
I2P | UInt16 | v if v < 32768, else v - 65536 |
I4P | UInt32 | v if v < 2^31, else v - 2^32 |
I8P | UInt64 | v if v < 2^63, else v - 2^64 |
The typical use is the ghost array of VTK, vtkGhostType (UInt8 bit flags: 1 marks a duplicate, i.e. ghost, cell or point, 32 a hidden cell). Readers still load the ghost cells, but VTK and ParaView skip them when they extract the surfaces to render, so the cells a parallel piece shares with its neighbours are not drawn twice:
ghost is an integer(I1P) array, one value per cell: 0 for the cells owned by the piece, 1 for the ghost cells.
f90
error = a_vtk_file%xml_writer%write_dataarray(location='cell', action='open')
error = a_vtk_file%xml_writer%write_dataarray_unsigned(data_name='vtkGhostType', x=ghost)
error = a_vtk_file%xml_writer%write_dataarray(location='cell', action='close')- It works with every format, compression included. The binary formats write the bytes of the array; the ASCII format prints the unsigned values (e.g.
200for-56_I1P); VTKHDF stores them with the HDF5 unsigned type of the same width. - With the ASCII format a
UInt64value of2^63or more (a negativeI8P) cannot be printed: nothing is written and the function returns a non-zero error. The binary formats write any value. - Only rank-1 arrays with one component; in a parallel header declare it with
write_parallel_dataarray(data_name=..., data_type='UInt8', number_of_components=1).