Using VTK to save manage and write external array

I’m currently studying ways to integrate VTK into my electromagnetic solver. My main motivation for this is compatibility with paraview. I’ve actually managed to write valid XML with binary data myself without using VTK C++ library, but I wanted to try using it. The reason is the following: I am using SOA-type vectors, and want them to be displayed as such in paraview. I’ve found vtkSOADAtaArrayTemplate which has method SetArray and I was able to make it work with something like this:

// 2d mesh size
std::size_t nx = 10;
std::size_t ny = 10;

std::vector<double> fx(nx * ny);
std::vector<double> fy(nx * ny);

vtkNew<vtkSOADataArrayTemplate<double>> f;
f->SetName("f");
f->setNumberOfComponents(2);
f->setNumberOfTuples(nx * ny);

f->setArray(0, fx.data(), nx * ny, false, true);
f->setArray(0, fy.data(), nx * ny, false, true);

/*
Next code adds f to vtkImageData and saves with vtkXLMImageDataWriter
*/

So, questions:

  1. Is it overall a correct approach to use self-allocated memory?
  2. Am I safe in terms of memory management? Doesn’t vtk allocate additional memory before I pass my arrays to it? Doesn’t vtk try to delete my arrays itself?
  3. Is VTK as efficient in saving SOA arrays as with AOS?
  1. Yes, this is the correct approach. In order to make the VTK SOA data array use non-VTK allocated memory like from std::vector, you will need to pass true for the save argument which you currently do.
  2. VTK will not delete the memory passed in with save=true.
  3. I can’t say much about the efficiency of SOA arrays in VTK. @spyridon97
1 Like

Since writing this post about 20 minutes ago I also figured out that one can pass updateMaxIdx=true to update number of tuples. So my example can be improved a bit to avoid calling SetNumberOfTuples:

// 2d mesh size
std::size_t nx = 10;
std::size_t ny = 10;

std::vector<double> fx(nx * ny);
std::vector<double> fy(nx * ny);

vtkNew<vtkSOADataArrayTemplate<double>> f;
f->SetName("f");
f->setNumberOfComponents(2);

f->setArray(0, fx.data(), nx * ny, true, true);
f->setArray(0, fy.data(), nx * ny, false, true);

/*
Next code adds f to vtkImageData and saves with vtkXLMImageDataWriter
*/

Ah, didn’t see you had a SetNumberOfTuples in the first post. That would allocate memory so you want to avoid that when using save=true.

1 Like