Plot functions in dolfinx

Hi all! I noticed some methods are mentioned in the tutorial of dolfinx (Implementation — FEniCS-X tutorial) and have some questions about them.

(1) For the pyvista method shown below, it seems to be an infinite loop when the terminal runs these lines. That is, nothing happens and the execution of the code will never stop (no warning appears). I am using dolfinx via the Docker container and VS code on the Windows 10 OS. I have already installed xvfb and pyvista.

pyvista.start_xvfb(wait=0.05)
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True, show_scalar_bar=True)
plotter.view_xy()
if not pyvista.OFF_SCREEN:
plotter.show()
figure = plotter.screenshot("fundamentals.png")

(2) For the dolfinx.io method shown below, how could I open or preview the VTK or XDMF file in the VS code?

import dolfinx.io
outfile = dolfinx.io.VTKFile("output.pvd")
outfile.write(uh)
xdmffile = dolfinx.io.XDMFFile(MPI.COMM_WORLD, "output.xdmf", "w")
xdmffile.write_mesh(mesh)
xdmffile.write_function(uh)
xdmffile.close()

I try to install the Paraview software in the Docker container, but I still could not preview the VTK or XDMF file in the VS code (I can preview the JPG). A warning appears when I run the command Paraview in the Docker container:

root@08637eaf5b16:/shared# paraview
qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.

Aborted

Could you be more specific about which of the following lines causes the code to run “an infinite loop”? I would also suggest calling export PYVISTA_OFF_SCREEN=True in the docker command line, as it should then render pngs.

  1. You should install paraview on your own system, that is, not inside docker. Then you can use Paraview to open the xdmf files.
1 Like

Thank you, Dokken!

For the first question, the figure is generated in the folder after adding export PYVISTA_OFF_SCREEN=True. Before that, “an infinite loop” starts from the line plotter.view_xy().

Hi Dokken, I have confirmed that the error results from the line plotter.view_xy(). For each time when I restart the terminal of Docker, I need to run the code without the last four lines as:

pyvista.start_xvfb(wait=0.05)
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True, show_scalar_bar=True)

And then write the command to the Docker terminal as:

export PYVISTA_OFF_SCREEN=True

Finally, I can run the whole code as:

pyvista.start_xvfb(wait=0.05)
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True, show_scalar_bar=True)
plotter.view_xy()
if not pyvista.OFF_SCREEN:
    plotter.show()
figure = plotter.screenshot("fundamentals.png")

I want to know is there an elegant way to use pyvista.Plotter?

I have not experienced that issue.
Here is my minimal example (using ubuntu 20.04 locally), where both interactive or non-interactive plotting works using docker:

#On your own system:
xhost + 

docker run -ti -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $(pwd):/home/shared -w /home/shared --name=name_of_container --shm-size=512m  dolfinx/dolfinx
# Inside docker container 
apt-get update
apt-get install -y --no-install-recommends libgl1-mesa-dev xvfb
pip3 install pyvista
# Change to True if you only want to save a png
echo 'export PYVISTA_OFF_SCREEN=False' >> ~/.bashrc
exec bash

# After exiting container
xhost -

# Next time you use the container
xhost + 
docker container start -i name_of_container

Minimal python code:

import dolfinx
import dolfinx.plot
import numpy as np
from mpi4py import MPI

try:
    import pyvista
except ModuleNotFoundError:
    print("pyvista is required for this demo")
    exit(0)

if pyvista.OFF_SCREEN:
    pyvista.start_xvfb(wait=0.1)


N = 2
mesh = dolfinx.UnitSquareMesh(MPI.COMM_WORLD, N, N)

# Extract mesh data from dolfin-X (only plot cells owned by the
# processor) and create a pyvista UnstructuredGrid
num_cells = mesh.topology.index_map(mesh.topology.dim).size_local
cell_entities = np.arange(num_cells, dtype=np.int32)
pyvista_cells, cell_types = dolfinx.plot.create_vtk_topology(
    mesh, mesh.topology.dim, cell_entities)
grid = pyvista.UnstructuredGrid(pyvista_cells, cell_types, mesh.geometry.x)

plotter = pyvista.Plotter()
plotter.add_mesh(grid, style="wireframe", line_width=2, color="black")
plotter.view_xy()
# Save as png if we are using a container with no rendering
if pyvista.OFF_SCREEN:
    plotter.screenshot("tmp.png")
else:
    plotter.show()

2 Likes

Is there a way to use matplotlib with FenicsX ?

For a short answer, yes. I think you can transfer quantities in FEniCS or FEniCSX into ndarrays and plot them with Matplotlib in a traditional way.
You could wait for more professional answers from the authors of the FEniCS project.

As @Yingqi_Jia said, you can always access the raw data and use it with matplotlib. We do not provide an interface to matplotlib internally in dolfinx as the support for quadrilateral/hexahedral meshes are very limited in matplotlib

Hi ,

For plooting quad mesh in dolfinx, i am trying to use pyvista. But, getting an error for running simple initial demo.

I read your above thread and installed pyvisa in root as well as local terminal. But, the error is as it is:

from pyvista import demos
demos.plot_wave()
/home/bagla0/.local/lib/python3.10/site-packages/pyvista/plotting/plotter.py:151: UserWarning: 
This system does not appear to be running an xserver.
PyVista will likely segfault when rendering.

Try starting a virtual frame buffer with xvfb, or using
  ``pyvista.start_xvfb()``

  warnings.warn(

after this, the kernel becomes dead. Kindly help me to run pyvista in my ubuntu 22.04.

  1. Can you also help me with the understanding of unstructured grid created by pyvista. For my quad mesh of 8 cells and 40 vertices,
topology, cell_types, x = dolfinx.plot.vtk_mesh(mesh, mesh.topology.dim)

the topology is of length 688 and cell types are of length 172 with all entiries 5. x is showing coordinate data with size (103,3). Can you tell me how this pyvista works and mwe refering to example to get the dolfinx mesh plotted using pyvista.

As the error message says,

you need to add pyvista.start_xvfb()

As you have not supplied a reproducible example, I cannot help you much more.