# Produce VTK unstuctured grid output without copying data into VTK

**URL:** https://discourse.vtk.org/t/produce-vtk-unstuctured-grid-output-without-copying-data-into-vtk/7980
**Category:** Support
**Tags:** file-formats
**Created:** [March 4, 2022, 11:19pm UTC](https://discourse.vtk.org/t/produce-vtk-unstuctured-grid-output-without-copying-data-into-vtk/7980 "2022-03-04T23:19:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Luke\_Dalessandro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luke_dalessandro/32/1279_2.png) [@Luke\_Dalessandro](https://discourse.vtk.org/u/Luke_Dalessandro)
#### Post date: [March 4, 2022, 11:19pm UTC](https://discourse.vtk.org/t/produce-vtk-unstuctured-grid-output-without-copying-data-into-vtk/7980/1 "2022-03-04T23:19:45Z")

</div>

We’ve been using VTK/ParaView for a couple of years now as an output format for our simulations for a-posteriori visualization an analysis. The current solution is to copy all of the relevant simulation data out of our `std::vector` struct-of-array data into the equivalent VTK structures.

This works fine, but roughly doubles our memory requirements and is needlessly slow.

Is there any infrastructure for producing `.vtu` outputs directly from application user code, without this intermediate copy?

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dave_demarle/32/6_2.png) [@Dave\_DeMarle](https://discourse.vtk.org/u/Dave_DeMarle)
#### Post date: [March 6, 2022, 8:12pm UTC](https://discourse.vtk.org/t/produce-vtk-unstuctured-grid-output-without-copying-data-into-vtk/7980/2 "2022-03-06T20:12:36Z")

</div>

See vtkSOADataArrayTemplate and the SetArray() method especially the “save” argument.

---

<div class="post-metadata">

### Author: ![Luke\_Dalessandro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luke_dalessandro/32/1279_2.png) [@Luke\_Dalessandro](https://discourse.vtk.org/u/Luke_Dalessandro)
#### Post date: [March 6, 2022, 8:32pm UTC](https://discourse.vtk.org/t/produce-vtk-unstuctured-grid-output-without-copying-data-into-vtk/7980/3 "2022-03-06T20:32:48Z")

</div>

Oh, cool. So where I have

```auto
auto scalars = vtkSmartPointer<vtkDoubleArray>::New();
scalars->SetName(name.c_str());
scalars->SetNumberOfComponents(1);
scalars->SetSize(size);
// copy data
vtu->GetPointData()->AddArray(scalars);

```

I can use a

```auto
auto scalars = vtkSmartPointer<vtkSOADataArrayTemplate<double>>::New();
scalars->SetName(name.c_str());
scalars->SetNumberOfComponents(1);
scalars->SetArray(0, data, size, true, true);
vtu->GetPointData()->AddArray(scalars);

```

instead?

I’ll give that a try, thank you.

---

<div class="post-metadata">

### Author: ![Luke\_Dalessandro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luke_dalessandro/32/1279_2.png) [@Luke\_Dalessandro](https://discourse.vtk.org/u/Luke_Dalessandro)
#### Post date: [March 11, 2022, 3:05pm UTC](https://discourse.vtk.org/t/produce-vtk-unstuctured-grid-output-without-copying-data-into-vtk/7980/4 "2022-03-11T15:05:52Z")

</div>

Other than VTK’s trouble with const-correctness forcing me to `const_cast` (can’t `SetArray(double const*)` or `vtkSOADataArrayTemplate<double const>`), this seems to be working. Thank you.
