Compatibility issue when using DOLFINx, PyVista, and PyTorch together

Dear community, I encounter a compatibility issue when using DOLFINx, PyVista, and PyTorch together. Here is the code to reproduce the compatibility issue and I wonder how to solve it.

"""
The compatibility issue of three packages: DOLFINx, PyVista, PyTorch.
At most two lines out of commands 1–3 can coexist.
Otherwise, the code will crash with following info:

Verify if there is a pass dependency cycle.
Required Passes:
Segmentation fault
"""

from dolfinx.generation import RectangleMesh  # Command 1
import torch  # Command 2
import numpy as np
import pyvista


cells = np.array([4, 0, 1, 3, 2])
cell_types = np.array([9])
coord = np.array([
    [0, 0, 0],
    [0, 1, 0],
    [1, 0, 0],
    [1, 1, 0],
])

pyvista.start_xvfb(wait=0.05)
plotter = pyvista.Plotter()
grid = pyvista.UnstructuredGrid(cells, cell_types, coord)
grid.cell_data["data"] = np.array([0.5])
plotter.add_mesh(grid)
plotter.screenshot("test.jpg")  # Command 3
plotter.close()

The problem is most likely in the installation of pytorch (you have not supplied how you installed it).
I can reproduce your issue using the docker container dolfinx/dolfinx:v0.5.0, after calling the following:

     apt-get update && apt install libgl1-mesa-glx xvfb
     pip3 install torch pyvista
     PYVISTA_OFFSCREEN=True python3 mwe.py 

However, installing torch from source:

     git clone https://github.com/pytorch/pytorch.git
     cd pytorch/
     python3 setup.py install

I get no error message

2 Likes

Thanks for the reply, Dokken! I used the first method you mentioned. I found that the problem disappears if we import PyTorch first and then import DOLFINx. I will also try the second installation method you suggested, which seems to have fewer restrictions on the code itself.