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

Add vector data as Point data in mesh #448

Open davidcortesortuno opened 3 years ago

davidcortesortuno commented 3 years ago

Description

Hi, I'm loading data from a tecplot file which is read successfully. This data is shown as image

Is there a way to add a new Data Array as Point data? I tried using add_field

mesh.add_field_array(np.column_stack((mesh.get_array('Mx'),
                                      mesh.get_array('My'),
                                      mesh.get_array('Mz'))),
                     name='M_vec')

but this creates a Fields and not a Points field:

image

I finally tried

mesh.vectors = np.column_stack((mesh.get_array('Mx'),
                                      mesh.get_array('My'),
                                      mesh.get_array('Mz')))

which produces what I was expecting,

image

However I cannot set a name to the data with this method. Is this the way to do it or is there a better function/method to do this?

MatthewFlamm commented 3 years ago

You can try:

mesh["my_vector_name"] = np.column_stack((mesh.get_array('Mx'),
                                          mesh.get_array('My'),
                                          mesh.get_array('Mz'))
mesh.set_active_vectors("my_vector_name")  # not strictly neccessary, but it will replicate the mesh.vectors usage.

See here:

https://github.com/pyvista/pyvista/blob/274261f684aad2d9f2ce5dcf9c864a7cf2ca2c5a/pyvista/core/dataset.py#L247-L258

davidcortesortuno commented 3 years ago

Thanks, I just realised I can also do

mesh.point_arrays.update(dict(M_vec=np.column_stack((mesh.get_array('Mx'),
                                                     mesh.get_array('My'),
                                                     mesh.get_array('Mz')))))
MatthewFlamm commented 3 years ago

Your method is basically the same as mine above, when doing mesh["M_vec"] = vector_data, but your usage will only work for point_data, whereas this usage will work for both point and cell data.

To summarize, you can add data with:

mesh.point_arrays.update(dict(name=point_data))
mesh.cell_arrays.update(dict(name=cell_data))
mesh["name"] = point_data
mesh["name"] = cell_data

mesh.vectors = point_data
mesh.vectors = cell_data

I tend to use the mesh["name"] = data method as it is the most flexible. I find the mesh.vectors usage to be confusing. There should be a limited number of ways to add data.

MatthewFlamm commented 3 years ago

I was incorrect, mesh.vectors = cell_data will not work since it adds to mesh.point_arrays. This is a bug, although I'd prefer this whole functionality to be removed.