pyvista / pyvista-support

[moved] Please head over to the Discussions tab of the PyVista repository
https://github.com/pyvista/pyvista/discussions
60 stars 4 forks source link

Method to add tensor data? #202

Closed jrwrigh closed 4 years ago

jrwrigh commented 4 years ago

I know that adding a scalar or vector to a data set is simply:

grid['data_name'] = data_nparray

where data_nparray is Nx1 for a scalar and Nx3 for a vector.

But I'm not sure how to go about adding tensor data. Is that a supported feature? If so, what order should the data be in?

Note: In response the bot, I'll add that I submitted this as an issue since either A) the feature doesn't exist or B) the feature isn't' documented. That said, the scalar and vector operations I reference above don't seem to be documented anywhere either.

github-actions[bot] commented 4 years ago

Hi and welcome! Thanks for posting your first issue in the PyVista project! Someone from @pyvista/developers will chime in before too long. If your question is support related, it may be automatically transferred to https://github.com/pyvista/pyvista-support

banesullivan commented 4 years ago

You're right that tensor data aren't well supported/documented in PyVista

VTK (the underlying library to PyVista) supports Tensor data as 9 component arrays. I've been meaning for a long time to put together an example of how to use tensors with PyVista.

In the time being, this example dataset from our examples module does have tensors:

from pyvista import examples
tmesh = examples.download_tensors()
tensors = tmesh['tensors1'].reshape((-1, 3, 3))
tensors

Then you can plot them via this (just what I thought to do, not a standard thing)

nodes = np.repeat(tmesh.points, 3, axis=0)
vecs = tensors.reshape((-1, 3))

vecs /= np.linalg.norm(vecs, axis=1)[:, None]
vecs[np.isnan(vecs)] = 0.0

p = pv.Plotter()
p.add_mesh(tmesh, opacity=0.75)
p.add_arrows(nodes, vecs, mag=0.5, color=True)
p.show()

download

banesullivan commented 4 years ago

So effectively, just ravel your tensors from (3, 3) to (9,)

jrwrigh commented 4 years ago

Sweet. Yep that worked. Also, VTK supports symmetric tensors as well, so you only have to give it 6 of the components. Ordering for that is XX, YY, ZZ, XY, YZ, XZ as denoted here.

banesullivan commented 4 years ago

Thanks for the link!

I am going to move this to the support repo for archiving there

banesullivan commented 4 years ago

Unfortunately, we do not really have any features in PyVsita that leverage tensors. Let us know what you're doing with tensors don the road and maybe we can get feature requests going for filters that utilize tensors.

jrwrigh commented 4 years ago

I'm mainly just dealing with reynolds stress tensors. So most of my operations are really just getting the tensor values into the correct reference frame.

In a typical workflow, the stresses are stored referencing a global coordinate system. The stresses are more useful when they're referencing a wall-adjusted coordinate system (ie. normal to the wall and two directions parallel to a wall), where that coordinate system may continuously vary over a surface (both for the normal direction and the orientation of the wall-parallel directions).

All that said, I'm mainly using pyvista as a method to get plain text file into a vtk form so I can view this in Paraview. Though I'll definitely need to check out the python widgets y'all have. They look quite nice.