Biswajit Banerjee

### Writing VTK XML files in C++

Outputting simulation data

I visualize the output of my simulations using either LLNL’s Visit or Kitware’s ParaView. These tools are wonderful for dealing with large datasets and can read a huge variety of file formats. In particular, they are good for remote and visualization.

One of the formats that both these tools can understand is the VTK XML format. The advantage of this format is its simplicity and that makes it suitable for small research codes.

Note: If your code is running on large modern HPC machines, you should consider formats that are more efficient for I/O and remote visualization.

In this blog post I’ll describe how I write data from my particle-based simulations into VTK format output files. If the files are small, I use ASCII for readability but for large simulations I write out VTK XML files containing binary data.

##### The data

The data produced by the particle simulation codes typically has two components:

• A grid that describes the boundary of the computational domain and the partitioning of the domain into pieces that are sent to various processors during the computation process, and
• A set of particles that contain position, geometry, and physical state data.

Each of these datasets changes with time; so they also contain a timestamp. A set of files is produced at each timestep in the simulation where the user feels that there is a need to output results. The process of creating appropriately named files is routine and won’t be described in this post.

##### Installing VTK and setting up Cmake

In Ubuntu 16.04, installing VTK (a slightly older version, 5.10), is straightforward. I haven’t tried more recent versions, but the API hasn’t changed much. Most HPC systems also have VTK installed by default because of its wide use. Installation just needs

  sudo apt-get install libvtk5-dev


To be able to build your code with VTK support, edit the root CMakeLists.txt file and add

  find_package(VTK 5.10 REQUIRED)
include_directories(${VTK_INCLUDE_DIRS})  and for linking the executable, add  target_link_libraries(your_executable YOUR_LIB${VTK_LIBRARIES}
)

##### Writing grid data

We write the grid using an unstructured grid writer (because the grid may not be regular) but stick to hexahedral elements in this example.

We will discuss the implementation of addTimeToVTKDataSet and addElementsToVTKDataSet next. Note the SetDataModeToAscii() function near the end. We can switch to binary output by just changing this call to SetDataModeToBinary().

##### Adding time to the data set

To add a time stamp to each data set, we use the following

##### Adding elements to the data set

Finally, we will add the grid elements to the dataset using the addElementsToVTKDataSet function below.

##### The output file

The output file produced by this approach has the extension .vtu and the format below.

The data can be easily read into Visit and the resulting plot can be seen below.

#### Remarks

Writing your data to files in VTK format is straightforward and reading them in Visit or ParaView is also quite simple. I prefer these tools to TecPlot, mainly because I can write plugins quite easily.

In the next post, I will discuss how you can save your particle data in VTK binary format.