I use Python to assemble an vtkOverlappingAMR datastructure. The data I fill into it comes from a CFD code. This code use an immersed boundary method, so some gridcells are “blocked” - because no fluid is present there. For non-AMR grids I set the HIDDENCELL bit in the vtkGhostType array to hide these cells.
The assembled vtkOverlappingAMR is just written to disk with the vtkXMLUniformGridAMRWriter, with the objective of opening this in Paraview.
The AMR data structure is really the correct thing to use for my cases, however, I cannot find any way of propagating the HIDDENCELL information onto Paraview.
A non-functiona code to show the principled is:
amr = vtk.vtkOverlappingAMR()
amr.Initialize(nlevels, grids_per_level)
amr.SetOrigin(globalOrigin)
for i in range(nlevels):
amr.SetSpacing(i, spacing/2**i)
amr.SetRefinementRatio(i, 2)
# Produce grid structure
for i in range(ngrids):
# Some code to determine origin and so on
# Dimensions there are the node dimensions, that is the number of cells + 1
box = vtk.vtkAMRBox(origin, ncells + 1, spacing, globalOrigin)
ug = vtk.vtkUniformGrid()
ug.SetOrigin(origin)
ug.SetSpacing(spacing)
ug.SetDimensions(ncells + 1)
# Read in a pressure in the Pg array
P = nps.numpy_to_vtk(Pg.ravel(), deep=True, array_type=vtk.VTK_TYPE_FLOAT32)
P.SetName("P")
ug.GetCellData().AddArray(P)
# Reading BP - blocking of P field, 1.0 if cell is fluid, 0.0 if cell is non-fluid
iblank = np.zeros(BP.shape, dtype=np.uint8)
iblank[BP < 0.5] = vtk.vtkDataSetAttributes.HIDDENCELL
iblank = nps.numpy_to_vtk(iblank.ravel(), deep=True, array_type=vtk.VTK_TYPE_UINT8)
iblank.SetName(vtk.vtkDataSetAttributes.GhostArrayName())
# iblank.SetName("banana")
ug.GetCellData().AddArray(iblank)
# Associate AMR box with vtkOverlappingAMR object
amr.SetAMRBox(ilevel, igridoflvl, box)
# Add the P data field
amr.SetDataSet(ilevel, igridoflvl, ug)
# Write out AMR dataset
writer = vtk.vtkXMLUniformGridAMRWriter()
writer.SetFileName("test.vth")
writer.SetInputData(amr)
writer.Write()
This give no hidden cells when I open the resulting vth in Paraview…
However, the following workaround works:
- Saving the array with the hidden cell information as something else than “vtkGhostType”, i.e. “banana” in the output dataset
- Open the “test.vth” in Paraview, adding a Calculator object
- Set the output type “UChar” and result name to “vtkGhostType”
- Use the formula “vtkGhostType + banana”
Since the vtkGhostType initially does not set the HIDDENCELL bit, and I know that “banana” only set the HIDDENCELL bit, the “vtkGhostType + banana” is a crude way of doing a binary-OR operation with a regular calculator object…
This shows the original data (upper), the calculator object used to “fix” the rendering and the correct outcome (lower):
Does anyone have an idea for a more elegant way of doing this, preferably at write-out time? The best would be to have a way to manipulate the vtkUniformGrid or vtkOverlappingAMR object before writing out.