XDMFWriter with int arrays: H5Tcopy(): not a datatype or dataset
When writing an integer array with the XDMF2 writer, e.g.,
import meshzoo
import numpy
import vtk
from vtk.util import numpy_support
points, cells = meshzoo.rectangle(
xmin=0.0, xmax=1.0,
ymin=0.0, ymax=1.0,
nx=4, ny=4
)
# Generate VTK mesh
mesh = vtk.vtkUnstructuredGrid()
# set points
vtk_points = vtk.vtkPoints()
# Not using a deep copy here results in a segfault.
vtk_array = numpy_support.numpy_to_vtk(points, deep=True)
vtk_points.SetData(vtk_array)
mesh.SetPoints(vtk_points)
# create cell_array. It's a one-dimensional vector with
# (num_points2, p0, p1, ... ,pk, numpoints1, p10, p11, ..., p1k, ...
numcells, num_local_nodes = cells.shape
# add cell types
cell_types = numpy.full(numcells, vtk.VTK_TRIANGLE, dtype=numpy.ubyte)
# add cell offsets
cell_offsets = numpy.arange(
0,
numcells * (num_local_nodes + 1),
num_local_nodes + 1,
dtype=numpy.int64
)
cell_connectivity = numpy.c_[
num_local_nodes * numpy.ones(numcells, dtype=cells.dtype),
cells
].flatten()
connectivity = vtk.util.numpy_support.numpy_to_vtkIdTypeArray(
cell_connectivity.astype(numpy.int64),
deep=1
)
# wrap the data into a vtkCellArray
cell_array = vtk.vtkCellArray()
cell_array.SetCells(len(cell_types), connectivity)
# Add cell data to the mesh
mesh.SetCells(
numpy_support.numpy_to_vtk(
cell_types,
deep=1,
array_type=vtk.vtkUnsignedCharArray().GetDataType()
),
numpy_support.numpy_to_vtk(
cell_offsets,
deep=1,
array_type=vtk.vtkIdTypeArray().GetDataType()
),
cell_array
)
# add cell data
cd = mesh.GetCellData()
cell_data = numpy.random.randint(0, 10, len(cells))
array = vtk.util.numpy_support.numpy_to_vtk(cell_data, deep=1)
array.SetName('my_array')
cd.AddArray(array)
writer = vtk.vtkXdmfWriter()
writer.SetFileName('test.xdmf')
writer.SetInputData(mesh)
writer.Write()
one gets errors of the kind
HDF5-DIAG: Error detected in HDF5 (1.10.0-patch1) thread 0:
#000: ../../../src/H5T.c line 1678 in H5Tcopy(): not a datatype or dataset
major: Invalid arguments to routine
minor: Inappropriate type
XDMF Error in /build/vtk7-mPdZCv/vtk7-7.1.1-3cf5df73/ThirdParty/xdmf2/vtkxdmf2/libsrc/XdmfDataDesc.cxx line 492 (Error Creating Data Type)
HDF5-DIAG: Error detected in HDF5 (1.10.0-patch1) thread 0:
#000: ../../../src/H5T.c line 1846 in H5Tget_class(): not a datatype
major: Invalid arguments to routine
minor: Inappropriate type