# Using VTK to save manage and write external array

**URL:** https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554
**Category:** Support
**Tags:** code
**Created:** [April 29, 2025, 2:09pm UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554 "2025-04-29T14:09:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lgkl](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lgkl/32/9952_2.png) [@lgkl](https://discourse.vtk.org/u/lgkl)
#### Post date: [April 29, 2025, 2:09pm UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554/1 "2025-04-29T14:09:17Z")

</div>

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:

```auto
// 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?

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [April 29, 2025, 2:25pm UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554/2 "2025-04-29T14:25:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lgkl](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lgkl/32/9952_2.png) [@lgkl](https://discourse.vtk.org/u/lgkl)
#### Post date: [April 29, 2025, 2:36pm UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554/3 "2025-04-29T14:36:24Z")

</div>

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`:

```auto
// 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
*/

```

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [April 29, 2025, 2:39pm UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554/4 "2025-04-29T14:39:30Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![spyridon97](https://discourse.vtk.org/user_avatar/discourse.vtk.org/spyridon97/32/7069_2.png) [@spyridon97](https://discourse.vtk.org/u/spyridon97)
#### Post date: [May 29, 2025, 1:52am UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554/5 "2025-05-29T01:52:47Z")

</div>

Out of curiosity, why do you use a vtkSOADataArrayTemplate array, and what do you plan to do with it?

---

<div class="post-metadata">

### Author: ![lgkl](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lgkl/32/9952_2.png) [@lgkl](https://discourse.vtk.org/u/lgkl)
#### Post date: [May 29, 2025, 6:44am UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554/7 "2025-05-29T06:44:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![spyridon97](https://discourse.vtk.org/user_avatar/discourse.vtk.org/spyridon97/32/7069_2.png) [@spyridon97](https://discourse.vtk.org/u/spyridon97)
#### Post date: [May 29, 2025, 3:21pm UTC](https://discourse.vtk.org/t/using-vtk-to-save-manage-and-write-external-array/15554/8 "2025-05-29T15:21:16Z")

</div>

Sounds good!

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).
