Add displacement data to mesh points

Suppose I have a mesh with Tet4 elements. The mesh has a total of 1695 nodes and 7726 elements. Now I can use pyvista to create the undeformed mesh like this:

points = Node_Data[:,1:4]
cells = EL_Data[:, 1:5]
cells= np.insert(cells, 0, 4, axis = 1)
celltypes = np.empty(7726, dtype=np.float32)
celltypes[:] = vtk.VTK_TETRA
grid = pv.UnstructuredGrid(cells, celltypes, points)
mesh = grid
mesh.n_cells
mesh.n_points
mesh.bounds
mesh.save("test1234.vtk")

My question is how do I add in displacement data into this mesh? So that in paraview, it will look like this:

final data

My current data looks like this:

current data

Or is there any other method to work this out?


I'm not familiar with ParaView, but it looks like you want to add vector-valued point data to your mesh. Many of PyVista's examples implicitly do that, but this is also mentioned in the Basic API Usage page of the documentation.

Assuming you have an array called displacements with shape (mesh.n_points, 3) (i.e. one 3-dimensional vector for each point in the mesh) where the vectors are in the same order as the mesh points, you just have to do

mesh.point_data['U'] = displacements
mesh.active_vectors_name = 'U'  # make these active vectors

This will define this array to be associated with your points, with a label of 'U'. For cell data you would need mesh.cell_data[...] = ..., but obviously the shape of the corresponding data array would have to be (mesh.n_cells, 3) (for vector-valued data).

Here's a simple example:

>>> import numpy as np
>>> import pyvista as pv
>>> mesh = pv.Box()  # no arrays here
>>> mesh.point_data['U'] = np.arange(mesh.n_points * 3).reshape(-1, 3)
>>> mesh.active_vectors_name = 'U'
>>> mesh.point_data
pyvista DataSetAttributes
Association     : POINT
Active Scalars  : U
Active Vectors  : U
Active Texture  : None
Active Normals  : None
Contains arrays :
    U                       int64    (8, 3)               VECTORS

You can see in the printout that the point_data contains a dataset with label 'U', int dtype and shape (8, 3), set as active vectors. Various filters either use active vectors or accept an explicit label to use as vectors (i.e. 'U' in this case).