|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Helper functions to write and read fields from VTK files (to be used with Paraview or PyVista) |
| 5 | +""" |
| 6 | +import os |
| 7 | +import vtk |
| 8 | +from vtkmodules.util import numpy_support |
| 9 | +import numpy as np |
| 10 | + |
| 11 | + |
| 12 | +def writeToVTR(fileName: str, data, coords, varNames): |
| 13 | + """ |
| 14 | + Write a data array containing variables from a 3D rectilinear grid into a VTR file. |
| 15 | +
|
| 16 | + Parameters |
| 17 | + ---------- |
| 18 | + fileName : str |
| 19 | + Name of the VTR file, can be with or without the .vtr extension. |
| 20 | + data : np.4darray |
| 21 | + Array containing all the variables with [nVar, nX, nY, nZ] shape. |
| 22 | + coords : list[np.1darray] |
| 23 | + Coordinates in each direction. |
| 24 | + varNames : list[str] |
| 25 | + Variable names. |
| 26 | +
|
| 27 | + Returns |
| 28 | + ------- |
| 29 | + fileName : str |
| 30 | + Name of the VTR file. |
| 31 | + """ |
| 32 | + data = np.asarray(data) |
| 33 | + nVar, *gridSizes = data.shape |
| 34 | + |
| 35 | + assert len(gridSizes) == 3, "function can be used only for 3D grid data" |
| 36 | + assert nVar == len(varNames), "varNames must have as many variable as data" |
| 37 | + assert [len(np.ravel(coord)) for coord in coords] == gridSizes, "coordinate size incompatible with data shape" |
| 38 | + |
| 39 | + nX, nY, nZ = gridSizes |
| 40 | + vtr = vtk.vtkRectilinearGrid() |
| 41 | + vtr.SetDimensions(nX, nY, nZ) |
| 42 | + |
| 43 | + vect = lambda x: numpy_support.numpy_to_vtk(num_array=x, deep=True, array_type=vtk.VTK_FLOAT) |
| 44 | + x, y, z = coords |
| 45 | + vtr.SetXCoordinates(vect(x)) |
| 46 | + vtr.SetYCoordinates(vect(y)) |
| 47 | + vtr.SetZCoordinates(vect(z)) |
| 48 | + |
| 49 | + field = lambda u: numpy_support.numpy_to_vtk(num_array=u.ravel(order='F'), deep=True, array_type=vtk.VTK_FLOAT) |
| 50 | + pointData = vtr.GetPointData() |
| 51 | + for name, u in zip(varNames, data): |
| 52 | + uVTK = field(u) |
| 53 | + uVTK.SetName(name) |
| 54 | + pointData.AddArray(uVTK) |
| 55 | + |
| 56 | + writer = vtk.vtkXMLRectilinearGridWriter() |
| 57 | + if not fileName.endswith(".vtr"): |
| 58 | + fileName += ".vtr" |
| 59 | + writer.SetFileName(fileName) |
| 60 | + writer.SetInputData(vtr) |
| 61 | + writer.Write() |
| 62 | + |
| 63 | + return fileName |
| 64 | + |
| 65 | + |
| 66 | +def readFromVTR(fileName: str): |
| 67 | + """ |
| 68 | + Read a VTR file into a numpy 4darray |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + fileName : str |
| 73 | + Name of the VTR file, can be with or without the .vtr extension. |
| 74 | +
|
| 75 | + Returns |
| 76 | + ------- |
| 77 | + data : np.4darray |
| 78 | + Array containing all the variables with [nVar, nX, nY, nZ] shape. |
| 79 | + coords : list[np.1darray] |
| 80 | + Coordinates in each direction. |
| 81 | + varNames : list[str] |
| 82 | + Variable names. |
| 83 | + """ |
| 84 | + if not fileName.endswith(".vtr"): |
| 85 | + fileName += ".vtr" |
| 86 | + assert os.path.isfile(fileName), f"{fileName} does not exist" |
| 87 | + |
| 88 | + reader = vtk.vtkXMLRectilinearGridReader() |
| 89 | + reader.SetFileName(fileName) |
| 90 | + reader.Update() |
| 91 | + |
| 92 | + vtr = reader.GetOutput() |
| 93 | + dims = vtr.GetDimensions() |
| 94 | + assert len(dims) == 3, "can only read 3D data" |
| 95 | + |
| 96 | + vect = lambda x: numpy_support.vtk_to_numpy(x) |
| 97 | + coords = [vect(vtr.GetXCoordinates()), vect(vtr.GetYCoordinates()), vect(vtr.GetZCoordinates())] |
| 98 | + pointData = vtr.GetPointData() |
| 99 | + varNames = [pointData.GetArrayName(i) for i in range(pointData.GetNumberOfArrays())] |
| 100 | + data = [numpy_support.vtk_to_numpy(pointData.GetArray(name)).reshape(dims, order="F") for name in varNames] |
| 101 | + data = np.array(data) |
| 102 | + |
| 103 | + return data, coords, varNames |
0 commit comments