Hello,
I am interpolating from an unstructured fine grid 1 to an unstructured coarse grid 2 many times over with changing data (both grids stay the same). I am wondering if it is possible to speed up the process so that all interpolation operations after the initial one become faster. I am thinking that VTK’s ports and connections could help here, but I don’t know how…
I created the below MWE to illustrate the problem
from time import process_time
import vtk
import numpy as np
from vtk.util.numpy_support import numpy_to_vtk, vtk_to_numpy
def create_vtk_grid(array):
# create VTK array
vtk_array = numpy_to_vtk(array.ravel(), deep=True)
vtk_array.SetNumberOfComponents(3)
vtk_array.SetNumberOfTuples(xyz1.size//3)
# create VTK points
vtk_points = vtk.vtkPoints()
vtk_points.SetData(vtk_array)
# create VTK grid
grid = vtk.vtkUnstructuredGrid()
grid.SetPoints(vtk_points)
return grid
# resolution of grid 1 & 2
n1 = 2000
n2 = 1000
# grid coordinates x,y,z for both grids
xyz1 = np.random.uniform(size=(n1,3))
xyz2 = np.random.uniform(size=(n2,3))
t = process_time()
grid1 = create_vtk_grid(xyz1)
grid2 = create_vtk_grid(xyz2)
# create VTK locator
locator = vtk.vtkStaticPointLocator()
locator.SetDataSet(grid1)
locator.BuildLocator()
# create VTK kernel
kernel = vtk.vtkShepardKernel()
# Create and setup VTK point interpolator
interpolator = vtk.vtkPointInterpolator()
interpolator.SetInputData(grid2)
interpolator.SetKernel(kernel)
interpolator.SetLocator(locator)
interpolator.SetNullPointsStrategyToClosestPoint()
print(f'Interpolation preparation time: {process_time()-t:.2e}')
# Interpolate between grid n times, each time with changing values while grids remain static
for i in range(10):
# values on grid 1 step i
var_on_grid_1 = np.random.uniform(size=n1)
# convert values to VTK array
vtk_val1 = numpy_to_vtk(var_on_grid_1, deep=True)
vtk_val1.SetNumberOfComponents(1)
vtk_val1.SetName('VARIABLE')
# Add values to grid 1 (source data)
grid1.GetPointData().AddArray(vtk_val1)
interpolator.SetSourceData(grid1)
t = process_time()
interpolator.Update()
print(f'Interpolation time {process_time() - t:.2e}')
var_on_grid_2 = vtk_to_numpy(
interpolator.GetOutput().GetPointData().GetArray('VARIABLE')
)
Thanks for your help