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:
Is it overall a correct approach to use self-allocated memory?
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?
Is VTK as efficient in saving SOA arrays as with AOS?
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.
VTK will not delete the memory passed in with save=true.
I can’t say much about the efficiency of SOA arrays in VTK. @spyridon97
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
*/
I have an electromagnetic solver and I was looking into using VTK for IO. My main goal is being able to visualize output using Paraview. In my solver I store all all field components (E_x, E_y, etc…) in separate arrays, so SOA seemed appropriate.
I’ve also tried writing VTK XML files myself, and in my simple benchmark it seems it’s faster than using XMLImageDataWriter, so I’m not sure, if there’s a point in using VTK as a dependency in my project only for IO or if I should just write those files by hand.
vtkXMLImageDataWriter might not be as fast because it does 2 things, SOA get converted to AOS while writing, and potentially it does some kind of compression (which you can deactivate).