I tried to use points and cell values to generate a hexahedral mesh with Python. But the final file was too large to store(>40 GB, ) and the program crashed, size of the data is 164640(56 * 60 * 49).
data.rar (882.1 KB)
import vtk
# define the dimensions of the grid
size_nx = int(57)
size_ny = int(61)
size_nz = int(50)
#define the grid
grid = vtk.vtkUnstructuredGrid()
values = vtk.vtkFloatArray()
points = vtk.vtkPoints()
#set the dimensions of the grid
# grid.SetDimensions(size_nx, size_ny, size_nz)
coor3d = open('coor3d.txt', 'r')
res = open('res.txt', 'r')
points.SetDataTypeToFloat()
for k in range(size_nx * size_ny * size_nz):
lines = coor3d.readline().split(" ")
x_coor = float(lines[0])
y_coor = float(lines[2])
z_coor = float(lines[4])
points.InsertNextPoint(x_coor, y_coor, z_coor)
grid.SetPoints(points)
n_p = points.GetNumberOfPoints()
print(f"num of points {n_p}")
#
nx = size_nx-1
ny = size_ny-1
nz = size_nz-1
for i in range(nx* ny * nz):
# poisition of the 8 vertices of the hexahedron
v1 = int(i % nx)
v2 = int((i / nx) % ny)
v3 = int(i / (nx * ny))
hex = vtk.vtkHexahedron()
hex.GetPointIds().SetId(0, v1 + v2 * size_nx + v3 * size_nx * size_ny)
hex.GetPointIds().SetId(1, v1 + v2 * size_nx + v3 * size_nx * size_ny + 1)
hex.GetPointIds().SetId(2, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx + 1)
hex.GetPointIds().SetId(3, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx)
hex.GetPointIds().SetId(4, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny)
hex.GetPointIds().SetId(5, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny + 1)
hex.GetPointIds().SetId(6, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny + size_nx + 1)
hex.GetPointIds().SetId(7, v1 + v2 * size_nx + v3 * size_nx * size_ny + size_nx * size_ny + size_nx)
if range(0,nx* ny * nz+1,34770).count(i):
print(f"{i*100/(nx* ny * nz):.2f}%")
grid.InsertNextCell(hex.GetCellType(), hex.GetPointIds())
value = res.readline().split("\n") # set value
values.InsertNextValue(float(value[0]))
grid.GetCellData().AddArray(values)
res.close()
coor3d.close()
writer = vtk.vtkXMLUnstructuredGridWriter()
writer.SetFileName("output_res.vtu") # set the file name
writer.SetInputData(grid) # set the grid as input
writer.SetDataModeToBinary()
writer.Write() # write the file