# writing PointData to UnstructuredGrid (and Xdmf time series)

**URL:** https://discourse.vtk.org/t/writing-pointdata-to-unstructuredgrid-and-xdmf-time-series/7201
**Category:** Support
**Tags:** file-formats
**Created:** [November 17, 2021, 4:39am UTC](https://discourse.vtk.org/t/writing-pointdata-to-unstructuredgrid-and-xdmf-time-series/7201 "2021-11-17T04:39:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![olafx](https://discourse.vtk.org/user_avatar/discourse.vtk.org/olafx/32/4286_2.png) [@olafx](https://discourse.vtk.org/u/olafx)
#### Post date: [November 17, 2021, 4:39am UTC](https://discourse.vtk.org/t/writing-pointdata-to-unstructuredgrid-and-xdmf-time-series/7201/1 "2021-11-17T04:39:11Z")

</div>

I’m trying to write only Points and PointData to an UnstructuredGrid type.  
I can’t find any examples on how to do this so having difficulties figuring it out, but I think I’m 90% of the way there I think.

My points work fine. My point data doesn’t write. I can create the data and give it a name. When I look in the file, the name and some other metadata are there, but the data is all zeroes even tho I assigned it.

I’d also like to utilize Xdmf3’s time series storage, which I know is supported through the VTK writer, but I also can’t figure out how to write multiple timesteps.

```auto
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkXdmf3Writer.h>
#include <vtkDoubleArray.h>

int main()
{
    vtkNew<vtkPoints> points;
    points->SetNumberOfPoints(3);
    points->SetPoint(0, 0, 0, 0);
    points->SetPoint(1, 0, 0, 1);
    points->SetPoint(2, 0, 1, 0);
    points->Modified();

    vtkNew<vtkDoubleArray> data;
    data->Allocate(3);
    data->SetName("NAME");
    data->SetValue(0, 42);
    data->SetValue(1, 42);
    data->SetValue(2, 42);
    data->Modified();

    vtkNew<vtkUnstructuredGrid> grid;
    grid->SetPoints(points);
    grid->GetPointData()->AddArray(data);

    vtkNew<vtkXMLUnstructuredGridWriter> writer1;
    writer1->SetFileName("0.vtu");
    writer1->SetInputData(grid);
    writer1->Write();

    vtkNew<vtkXdmf3Writer> writer2;
    writer2->SetFileName("0.xdmf");
    writer2->SetInputData(grid);
    writer2->Write();

```

I’m compiling with CMake.

```auto
cmake_minimum_required(VERSION 3.20)
project(A)
find_package(VTK COMPONENTS IOXML IOXdmf3)
add_executable(a a.cc)
target_link_libraries(a PRIVATE ${VTK_LIBRARIES})

```

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [November 17, 2021, 10:44am UTC](https://discourse.vtk.org/t/writing-pointdata-to-unstructuredgrid-and-xdmf-time-series/7201/2 "2021-11-17T10:44:48Z")

</div>

I believe that you have to replace “Allocate()” with “SetNumberOfTuples()” as follows. Allocate() is analogous to std::vector.reserve() - it allocates memory but does not change the size (i.e., number of tuples) of the data array.

vtkNew data; data-\>SetNumberOfTuples(3); //instead if Allocate(3) data-\>SetName(“NAME”); data-\>SetValue(0, 42); data-\>SetValue(1, 42); data-\>SetValue(2, 42);

---

<div class="post-metadata">

### Author: ![olafx](https://discourse.vtk.org/user_avatar/discourse.vtk.org/olafx/32/4286_2.png) [@olafx](https://discourse.vtk.org/u/olafx)
#### Post date: [November 17, 2021, 4:09pm UTC](https://discourse.vtk.org/t/writing-pointdata-to-unstructuredgrid-and-xdmf-time-series/7201/3 "2021-11-17T16:09:53Z")

</div>

Ah I see, I misinterpreted the Allocate function. This solution works, thanks a lot! It was confusing because I used data-\>GetSize() to check the size, and sure enough it was 3. data-\>GetTuples() however was still 0. With your solution, both are 3. So I guess GetSize() returns the reserved size.
