Search code examples
data-visualizationmeshpython-3.8fluid-dynamics

How to visualize a vtu file in python?


I am using meshio package to extract the information out of a vtu file that represents the 2d version of flow past a cylinder (Von-Karman vortices) at a Reynolds no. of 220 for one time-step. I am able to extract the x and y coordinates in a variable called data as follows:

In[7]: data    
Out[7]: 
array([[0.02692238, 0.40000474],
       [0.02049045, 0.41      ],
       [0.03102019, 0.41      ],
       ...,
       [0.27846185, 0.15545069],
       [0.04423019, 0.05609254],
       [0.04601913, 0.06239963]])
In [9]: np.shape(data)
Out[9]: (12568, 2)

Also, I am able to extract any field variable for instance pressure as follows:

In [8]: Pressure
Out[8]: 
array([ 0.04950621,  0.05545308,  0.04723278, ..., -0.05356851,
        0.07954962,  0.08006932])
In [10]: np.shape(Pressure)
Out[10]: (12568,)

The simulation had triangle 6 elements i.e. a quadratic element with 3 regular nodes at vertices and 3 extra nodes in the middle of each edge. Here is the structure of the nodal connectivity.

In [14]: cells['triangle6']
Out[14]: 
array([[    0,     1,     2,  3528,  3527,  3531],
       [    3,     4,     5,  3525,  3239,  3238],
       [    4,     6,     5,  3408,  3237,  3239],
       ...,
       [ 1737,   484,   343,  7316,  4145,  4148],
       [ 3204,  3199,  3202, 12296, 12294, 12566],
       [ 3200,  3199,  3204, 12295, 12296, 12567]], dtype=int64)
In [15]: np.shape(cells['triangle6'])
Out[15]: (6142, 6)

How do I plot the simulation using this information?


Solution

  • I am answering my own question after 1 year. So, if the point is just to visualise the vtu file inside Python. PyVista is something you need. Here, they have an excellent tutorial series on how to visualize vtu files.

    One can also use matplotlib.pyplot.scatter() to quickly plot a scatter with the dependent variables as the colormap intensity.

    For example, If I want to plot Pressure in 2D domain.

    import matplotlib.pyplot as plt
    sc = plt.scatter(x,y, c = pressure)
    plt.colormap(sc)
    

    If you want to use plt.imshow() for nicer plots, then you need to apply interpolation to convert the data to structured grid.