# Write several unstructured grids

**URL:** <https://discourse.vtk.org/t/write-several-unstructured-grids/6623>\
**Category:** Development\
**Created:** [September 14, 2021, 8:34am UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623 "2021-09-14T08:34:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sagues](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Sagues](https://discourse.vtk.org/u/Sagues)\
**Post date:** [September 14, 2021, 8:34am UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/1 "2021-09-14T08:34:46Z")

</div>

Is it possible to write several unstructured grids in one file without use MultiblockDataSet?

---

<div class="post-metadata">

**Author:** ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)\
**Post date:** [September 16, 2021, 2:05pm UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/2 "2021-09-16T14:05:19Z")

</div>

a single unstructured grid may have any number of connected components, however it will still be a single vtkDataSet.  
We may try to use the vtkPXMLUnstructuredGridWriter to write pieces and then abuse the pipeline by requesting pieces independantly, but I definitely don’t think this would be a clean solution.

What is your usecase exactly ?

---

<div class="post-metadata">

**Author:** ![Sagues](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Sagues](https://discourse.vtk.org/u/Sagues)\
**Post date:** [September 16, 2021, 2:10pm UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/3 "2021-09-16T14:10:27Z")

</div>

Now I think to use MultiblockDataSet. On the other hand I’m asking how could I write a vtkMultiPieceDataSet

---

<div class="post-metadata">

**Author:** ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)\
**Post date:** [September 16, 2021, 2:20pm UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/4 "2021-09-16T14:20:00Z")

</div>

You may try to use a `vtkXMLPartitionedDataSetWriter`.  
Be careful these class are still under development ([https://discourse.paraview.org/t/deprecating-multiblock-datasets/4950](https://discourse.paraview.org/t/deprecating-multiblock-datasets/4950))

---

<div class="post-metadata">

**Author:** ![Sagues](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Sagues](https://discourse.vtk.org/u/Sagues)\
**Post date:** [September 16, 2021, 5:49pm UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/5 "2021-09-16T17:49:42Z")

</div>

On the other hand, I’m asking what is the meaning of pieces in unstructured grid:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/8/8082835c6ecd0d50ab9b2088b891b5de5c5c788b.png)

How can I manage it?

---

<div class="post-metadata">

**Author:** ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)\
**Post date:** [September 17, 2021, 7:51am UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/6 "2021-09-17T07:51:25Z")

</div>

most of the VTK XML Writers (vtkXML…Writer) also have a multi-piece version (vtkXMLP…Writer). For example, the `vtkXMLPolyDataWriter` can write **.vtp** files, and the `vtkXMLPPolyDataWriter` can write `.pvtp` which are basically composed of:

- one **.pvtp** xml files which contain location of **vtp** files composing the data set
- (usually) several **vtp** files, each containing a sub-part of the main PolyData. These parts are named pieces.

Of course, these pieces can be independent from each others, hence we can use this mechanism to write / read several poly data at once, but the result is still composed of several files as the main use of this mechanism is in a distributed environment.  
I can give you example of how to use such reader if you want

---

<div class="post-metadata">

**Author:** ![Sagues](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Sagues](https://discourse.vtk.org/u/Sagues)\
**Post date:** [September 17, 2021, 7:58am UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/7 "2021-09-17T07:58:28Z")

</div>

Yes, this example can be very usefull for me.

Thank you very much

---

<div class="post-metadata">

**Author:** ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)\
**Post date:** [September 17, 2021, 8:31am UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/8 "2021-09-17T08:31:05Z")

</div>

Here is a small example that open each piece and retrieve the number of cell inside, then write the data set again. It is intended to work with **pvtu** files.

```cpp
#include <iostream>

#include <vtkUnstructuredGrid.h>
#include <vtkXMLPUnstructuredGridReader.h>
#include <vtkXMLPUnstructuredGridWriter.h>

int main(int argc, char *argv[]) {

  vtkNew<vtkXMLPUnstructuredGridReader> preader;
  preader->SetFileName(argv[1]);

  // retrieve the number of pieces without reading the whole data
  preader->UpdateInformation();

  int nb_pieces = preader->GetNumberOfPieces();

  std::cout << "nb pieces : " << nb_pieces << std::endl;

  for (int cur_piece = 0; cur_piece < nb_pieces; cur_piece++) {
    preader->UpdatePiece(cur_piece, nb_pieces, 0);
    auto *loadedMesh = preader->GetOutput();
    std::cout << cur_piece << " nb cells : " << loadedMesh->GetNumberOfCells() << std::endl;
  }

  vtkNew<vtkXMLPUnstructuredGridWriter> w;
  w->SetInputConnection(preader->GetOutputPort());
  w->SetFileName("res.pvtu");
  w->SetUseSubdirectory(true);
  w->SetNumberOfPieces(2);
  w->SetStartPiece(0);
  w->SetEndPiece(nb_pieces);
  w->SetDataModeToAscii();
  w->Write();

  return 0;
}

}

```

---

<div class="post-metadata">

**Author:** ![Sagues](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@Sagues](https://discourse.vtk.org/u/Sagues)\
**Post date:** [September 17, 2021, 6:42pm UTC](https://discourse.vtk.org/t/write-several-unstructured-grids/6623/9 "2021-09-17T18:42:49Z")

</div>

Thank you very much for this example, but I still don’t understand how I can create several pieces in an unstructured grid.
