Code from Poisson subdomain problem tutorial not working

I am trying to run the code from this tutorial on solving a Poisson subdomain problem. I am running FEniCS through a Linux subsystem on Windows 10, and trying to use Jupyter notebook to run some code. I have a mathematical problem that I would like to solve, which involves several sub-domains, and would like to use this as a guide.

I end up with a problem which coincides with that of a commenter on the page. I get:

TypeError: list indices must be integers or slices, not str
after trying to create the “mesh_of_triangles”.

I have spent a rather long time trying to figure out why this isn’t working, and it seems to be the case on my Ubuntu installation on my laptop as well as through the subsystem on my Windows machine. Is this mesh reading issue something that a specific Python package can be used to fix?

Also, I would like to install dolfinx completely, and have tried several parts after cloning the GitHub repository (although the one I got seems to be the master one which doesn’t include the Poisson subdomain problem) and installing packages as seen in several sub-folders.

Please find below the list of Python 3 packages I’ve installed, in case anyone can spot something that’s missing:

pip3 list

asn1crypto (0.24.0)
attrs (19.3.0)
Automat (0.6.0)
backcall (0.1.0)
bleach (3.1.5)
blinker (1.4)
certifi (2018.1.18)
cftime (1.1.3)
chardet (3.0.4)
click (6.7)
cloud-init (19.4)
cmake (3.17.1)
colorama (0.4.3)
command-not-found (0.3)
configobj (5.0.6)
constantly (15.1.0)
cryptography (2.1.4)
cycler (0.10.0)
data (0.4)
decorator (4.4.2)
defusedxml (0.6.0)
distro-info (0.18ubuntu0.18.04.1)
entrypoints (0.3)
fenics-dijitso (2019.1.0)
fenics-dolfin (2019.1.0)
fenics-ffc (2019.1.0.post0)
fenics-ffcx (2019.2.0.dev0)
fenics-fiat (2019.2.0.dev0)
fenics-ufl (2019.1.0)
funcsigs (1.0.2)
future (0.18.2)
h5py (2.10.0)
html5lib (0.999999999)
httplib2 (0.9.2)
hyperlink (17.3.1)
idna (2.6)
importlib-metadata (1.6.0)
incremental (16.10.1)
ipykernel (5.2.1)
ipython (7.14.0)
ipython-genutils (0.2.0)
ipywidgets (7.5.1)
jedi (0.17.0)
Jinja2 (2.11.2)
joblib (0.14.1)
jsonpatch (1.16)
jsonpointer (1.10)
jsonschema (3.2.0)
jupyter (1.0.0)
jupyter-client (6.1.3)
jupyter-console (6.1.0)
jupyter-contrib-core (0.3.3)
jupyter-contrib-nbextensions (0.5.1)
jupyter-core (4.6.3)
jupyter-highlight-selected-word (0.2.0)
jupyter-latex-envs (1.4.6)
jupyter-nbextensions-configurator (0.4.1)
keyring (10.6.0)
keyrings.alt (3.0)
kiwisolver (1.2.0)
language-selector (0.1)
latex (0.7.0)
line-profiler (3.0.2)
llvmlite (0.32.0)
lxml (4.5.0)
MarkupSafe (1.1.1)
matplotlib (3.2.1)
meshio (4.0.13)
mistune (0.8.4)
mpi4py (3.0.3)
mpmath (1.1.0)
mshr (2019.1.0)
nbconvert (5.6.1)
nbformat (5.0.6)
netCDF4 (1.5.3)
netifaces (0.10.4)
notebook (6.0.3)
numba (0.49.0)
numpy (1.18.4)
oauthlib (2.0.6)
packaging (20.3)
PAM (0.4.2)
pandocfilters (1.4.2)
parso (0.7.0)
petsc (3.13.1)
petsc4py (3.7.0)
pexpect (4.8.0)
pickleshare (0.7.5)
pip (9.0.1)
pkgconfig (1.3.1)
ply (3.11)
prometheus-client (0.7.1)
prompt-toolkit (3.0.5)
ptyprocess (0.6.0)
pyasn1 (0.4.2)
pyasn1-modules (0.2.1)
pybind11 (2.5.0)
pycrypto (2.6.1)
Pygments (2.6.1)
pygobject (3.26.1)
PyJWT (1.5.3)
pyOpenSSL (17.5.0)
pyparsing (2.4.7)
pypi (2.1)
pyrsistent (0.16.0)
pyserial (3.4)
python-apt (1.6.5+ubuntu0.2)
python-dateutil (2.8.1)
python-debian (0.1.32)
pyxdg (0.25)
PyYAML (5.3.1)
pyzmq (19.0.0)
qtconsole (4.7.3)
QtPy (1.9.0)
requests (2.18.4)
requests-unixsocket (0.1.5)
scipy (1.4.1)
SecretStorage (2.3.1)
Send2Trash (1.5.0)
service-identity (16.0.0)
setuptools (46.1.3)
shutilwhich (1.1.0)
simplegeneric (0.8.1)
six (1.14.0)
slepc4py (3.7.0)
ssh-import-id (5.7)
sympy (1.5.1)
systemd-python (234)
tempdir (0.7.1)
terminado (0.8.3)
testpath (0.4.4)
tornado (6.0.4)
tqdm (4.45.0)
traitlets (4.3.3)
Twisted (17.9.0)
ufw (0.36)
unattended-upgrades (0.1)
urllib3 (1.22)
wcwidth (0.1.9)
webencodings (0.5.1)
wheel (0.30.0)
widgetsnbextension (3.5.1)
zipp (3.1.0)
zope.interface (4.3.2)

There is already a comment at the end of the tutorial with this exact error message.

To install the official version of dolfinx (and not the one used in this tutorial), you can for instance use docker, as there is an image on Dockerhub.

I guess @iitrabhi can tell you more, as it is his tutorial.

I would ideally like to forgo using Docker as I have more experience with Linux and am feeling slightly under pressure due to not being able to get numerical results for my project.

EDIT: After trying to install dolfinx with no success, I quickly found online that fiat, ffcx and ufl need to be installed as prerequisites before dolfinx can be installed. In trying to install ffcx, I get the error

Could not find a version that satisfies the requirement fenics-fiat<2019.3,>=2019.2.0.dev0 (from fenics-ffcx==2019.2.0.dev0) (from versions: 2017.1.0.post1, 2017.2.0, 2018.1.0, 2019.1.0)
No matching distribution found for fenics-fiat<2019.3,>=2019.2.0.dev0 (from fenics-ffcx==2019.2.0.dev0)

EDIT 2: I installed Docker within the Windows Subsystem for Linux, as well as the client on Windows, and pulled the file at the link provided. Will see how I get on but I’m already noticing that all the prerequisites seem to be installed automatically, which is nice!

All the commands for installing dolfinx from source on ubuntu 20.04 is outlined in the Dockerfile you should be able to copy paste these commands to install it by hand.

I tried that. I installed Docker and got it to run properly (after many failed attempts) and then tried building from the Dockerfile from that repository. It turns out that this didn’t help at all - “import dolfinx” inside my code returns an error as it does not recognise the package, even after a complete re-install of Ubuntu inside the WSL environment.

I guess the issue is probably that the “dolfinx” package is not in the right place for my Python installation to pick up. When I cloned everything from the FEniCS GitHub page I put them in ~/Programs/FEniCS with subfolders named after all the repositories on that page.

My question is now: how can I bring everything together so that dolfinx is now embedded in the same place that all my other Python packages are, so that it can be used seamlessly?

When you install the python packages, did you use pip? If so the packages is placed relative to the pip python path. If you for instance do pip3 list the dolfinx,ffcx etc. packages should be visible.

I used pip3.

After following the installation instructions, I am trying to install dolfinx from the dolfinx/python file by running “python3 install .” but it fails. Looking at the very long error message that it gives me, it seems to fail because it doesn’t recognise the header file “Python.h”.

See: https://stackoverflow.com/questions/21530577/fatal-error-python-h-no-such-file-or-directory

Many thanks for that - having installed that package, I was able to successfully install dolfinx.

The only problem now is that I am still getting the error originally mentioned at the start of this thread - I thought this was down to dolfinx not being installed but after a lot of work, it turns out this wasn’t the case. Any input from @iitrabhi would be fantastic - e.g. what packages did he have installed in order for his code to work?

The problem from the tutorial was that the way of separating out the CellBlock file into line and triangular elements (looks like it’s creating a sub-dictionary?) did not work. I replaced

mesh_of_triangles = meshio.Mesh(points=points[:, :2],
                                cells={'triangle': cells['triangle']},
                                cell_data={'triangle': 
                                    {'subdomain': 
                                        cell_data['triangle']
                                        ['gmsh:physical']}},
                                field_data=field_data) 

meshio.write("poisson_subdomain_triangle.xdmf", mesh_of_triangles )

mesh_of_lines = meshio.Mesh(points=points[:, :2],
                                cells={'line': cells['line']},
                                cell_data={'line': {'subdomain':
                                    cell_data['triangle']
                                    ['gmsh:physical']}},
                                field_data=field_data) 

with (from the post here)

line_cells = []
for cell in mesh.cells:
    if cell.type == "triangle":
        triangle_cells = cell.data
    elif  cell.type == "line":
        if len(line_cells) == 0:
            line_cells = cell.data
        else:
            line_cells = np.vstack([line_cells, cell.data])

line_data = []
for key in mesh.cell_data_dict["gmsh:physical"].keys():
    if key == "line":
        if len(line_data) == 0:
            line_data = mesh.cell_data_dict["gmsh:physical"][key]
        else:
            line_data = np.vstack([line_data, mesh.cell_data_dict["gmsh:physical"][key]])
    elif key == "triangle":
        triangle_data = mesh.cell_data_dict["gmsh:physical"][key]

triangle_mesh = meshio.Mesh(points=mesh.points, cells={"triangle": triangle_cells})
line_mesh =meshio.Mesh(points=mesh.points,
                           cells=[("line", line_cells)],
                           cell_data={"name_to_read":[line_data]})

which seems to overcome the problem I originally encountered. However, now I get a different error, which I think warrants its own thread.