# Python-Created HyperTreeGrid File Performance on Paraview

**URL:** https://discourse.vtk.org/t/python-created-hypertreegrid-file-performance-on-paraview/15329
**Category:** Support
**Created:** [February 28, 2025, 10:09pm UTC](https://discourse.vtk.org/t/python-created-hypertreegrid-file-performance-on-paraview/15329 "2025-02-28T22:09:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Nicolas](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicolas/32/9830_2.png) [@Nicolas](https://discourse.vtk.org/u/Nicolas)
#### Post date: [February 28, 2025, 10:09pm UTC](https://discourse.vtk.org/t/python-created-hypertreegrid-file-performance-on-paraview/15329/1 "2025-02-28T22:09:50Z")

</div>

Hi,

I am converting TreeBased-AMR CFD simulation data into VTK HyperTreeGrid using a python script similar the one of this [tutorial](https://www.kitware.com/hypertreegrid-in-vtk-data-construction/) (simplified script version at the end).

The created htg file from relatively large data snapshot (\>2GB) is handled well in Paraview when the AMR is refined in one region (see the slice in fig. 1 below).

 ![fig_1](https://discourse.vtk.org/uploads/default/original/2X/9/9273c9a6d10e2651277c382361a13e3651c59480.png)

However, when the refined regions are more spread, even for small initial data snapshot (200MB, see fig. 2 below), Paraview struggles a lot to handle the file (opening/ rendering/ or any action).

 ![fig_2](https://discourse.vtk.org/uploads/default/original/2X/2/29632e658e3879d09b6a35c95c13b1e58555ebd6.jpeg)

I am just starting to use htg and VTK for visualisation. Is there a way to improve the performance in this case? I am a bit surprised that in the second case Paraview struggles with only 200MB data.

Best wishes,  
Nicolas

Here the simplified script:

```auto
import numpy as np
import vtk
from vtkmodules.vtkCommonCore import (
    vtkDoubleArray,
    vtkLookupTable,)
from vtkmodules.vtkCommonDataModel import (
    vtkHyperTreeGrid,
    vtkHyperTreeGridNonOrientedCursor,)

def do_refine_root(xf,i,j,k,level,levelmax=3,data=[]):
    if (i==j)&(i==k)&(level<levelmax):
        return True
    else:
        return False

def refine_cell(cursor,x,y,z,max_level,data=[]):
    ii=0
    cursor.SubdivideLeaf()
    ii1 = 0
    for i1 in range(2):
        for j1 in range(2):
            for k1 in range(2):       
                cursor.ToChild(ii1)
                iLevel = cursor.GetLevel()
                iBounds = cursor.GetGrid()
                print(iLevel)
                print(iBounds)    
                idx = cursor.GetGlobalNodeIndex()
                print(iLevel)
                refine = do_refine_root(0,i1,j1,k1,iLevel,levelmax=max_level)
                if refine:
                    scalarArray.InsertTuple1(idx, ii+ii1)
                    vecArray.InsertComponent(idx, 0, ii+ii1)
                    vecArray.InsertComponent(idx, 1, ii+ii1)
                    vecArray.InsertComponent(idx, 2, ii+ii1)
                    refine_cell(cursor,x,y,z,max_level, data=[])
                else:
                    scalarArray.InsertTuple1(idx, ii+ii1)
                    vecArray.InsertComponent(idx, 0, ii+ii1)
                    vecArray.InsertComponent(idx, 1, ii+ii1)
                    vecArray.InsertComponent(idx, 2, ii+ii1)
                cursor.ToParent()  
                ii1 += 1
    return ii

# =========================================== #
# === IC PARAMETERS === #
# =========================================== #
N_base = 4
Lbox = 10.
dim=3
max_level=3
# =========================================== #
x_c = np.linspace(0.5*Lbox/N_base,Lbox-0.5*Lbox/N_base,N_base)
x_f = np.linspace(0.,Lbox,N_base+1)
Ntot_root = N_base**dim

# =========================================== #
# === INITIALIZE THE GRID === #
# =========================================== #
htg = vtk.vtkHyperTreeGrid()
htg.Initialize()

scalarArray = vtkDoubleArray()
scalarArray.SetName('scalar')
scalarArray.SetNumberOfValues(0)
htg.GetCellData().AddArray(scalarArray)
htg.GetCellData().SetActiveScalars('scalar')

vecArray = vtkDoubleArray()
vecArray.SetName('vector')
vecArray.SetNumberOfValues(0)
vecArray.SetNumberOfComponents(3)
htg.GetCellData().AddArray(vecArray)
htg.GetCellData().SetActiveVectors('vector')

if (dim==2):
    htg.SetDimensions(N_base+1, N_base+1, 1)
elif(dim==3):
    htg.SetDimensions(N_base+1, N_base+1, N_base+1)
htg.SetBranchFactor(2)

# Rectilinear root grid coordinates
xValues = vtkDoubleArray()
xValues.SetNumberOfValues(N_base+1)
yValues = vtkDoubleArray()
yValues.SetNumberOfValues(N_base+1)
for i in range(N_base+1):
    xValues.SetValue(i,x_f[i])
    yValues.SetValue(i,x_f[i])
htg.SetXCoordinates(xValues)
htg.SetYCoordinates(yValues)
if dim==3:
    zValues = vtkDoubleArray()
    zValues.SetNumberOfValues(N_base+1)
    for i in range(N_base+1):
        zValues.SetValue(i,x_f[i])
    htg.SetZCoordinates(zValues)
else:
    zValues = vtkDoubleArray()
    zValues.SetNumberOfValues(1)
    zValues.SetValue(0,x_c[0])
    htg.SetZCoordinates(zValues)

# =========================================== #
# === FILL THE GRID === #
# =========================================== #
# Let's split the various trees
cursor = vtkHyperTreeGridNonOrientedCursor()
offsetIndex =0

# ROOT CELLS
ii=0
for i in range(N_base):
    for j in range(N_base):
        for k in range(N_base):
            #htg.GetIndexFromLevelZeroCoordinates(global_index_tree, i, j, k)
            htg.InitializeNonOrientedCursor(cursor, ii, True)
            cursor.SetGlobalIndexStart(offsetIndex)
            idx = cursor.GetGlobalNodeIndex()     
            iLevel = cursor.GetLevel()
            iBounds = cursor.GetGrid()
            print(iLevel)
            print(iBounds)
            refine = do_refine_root(0,i,j,k,iLevel,levelmax=max_level)
            if refine:
                refine_cell(cursor,0,0,0,max_level=max_level)
            else:
                scalarArray.InsertTuple1(idx, ii)
                vecArray.InsertComponent(idx, 0, ii)
                vecArray.InsertComponent(idx, 1, ii)
                vecArray.InsertComponent(idx, 2, ii)
            offsetIndex += cursor.GetTree().GetNumberOfVertices()
            ii += 1

# =========================================== #
# === SAVE THE GRID === #
# =========================================== #
writer = vtk.vtkXMLHyperTreeGridWriter()
writer.SetFileName('test_'+str(dim)+'D.htg')
writer.SetInputData(htg)
writer.Write()

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 9, 2025, 9:26am UTC](https://discourse.vtk.org/t/python-created-hypertreegrid-file-performance-on-paraview/15329/2 "2025-03-09T09:26:16Z")

</div>

@Louis_Gombert @Jean_Fechter
