Python bindings

92 views
Skip to first unread message

Corbin Foucart

unread,
Apr 19, 2022, 3:04:39 PM4/19/22
to deal.II User Group
Hello everyone,

I'd like to write some custom Python bindings for deal.ii, and as a first step, I've been trying to reproduce the ipython notebook for step-49.

I've built deal.ii version 9.3.0 with the -DDEAL_II_COMPONENT_PYTHON_BINDING=ON configuration, yet when I run the notebook, the Jupyter kernel is unable to locate the PyDealII module, that is, the notebook cell

> import PyDealII.Release as dealii

fails with an import error. Where is this library located, and how should I go about setting up a (conda) environment to be able to use PyDealII? Are there dependencies?

Any guidance would be appreciated!
Corbin

Bruno Turcksin

unread,
Apr 19, 2022, 4:37:11 PM4/19/22
to deal.II User Group
Corbin,

On Tuesday, April 19, 2022 at 3:04:39 PM UTC-4 corbin....@gmail.com wrote:
I've built deal.ii version 9.3.0 with the -DDEAL_II_COMPONENT_PYTHON_BINDING=ON configuration, yet when I run the notebook, the Jupyter kernel is unable to locate the PyDealII module, that is, the notebook cell

> import PyDealII.Release as dealii

fails with an import error.
I havent' use the python binding in a while but you probably need to set the PYTHONPATH to where pydealii is located. You may also need to add the path to libdealii.so to LD_LIBRARY_PATH

Where is this library located, and how should I go about setting up a (conda) environment to be able to use PyDealII? Are there dependencies?
The library should be located in the same directory where you installed deal.II. Look somewhere in /path/to/install/lib . I don't use conda so I can't help you with that. What kind of dependencies are you talking about? PyDealII only depends on deal.II and Boost.Python, nothing else

Best,

Bruno

Corbin Foucart

unread,
Apr 20, 2022, 6:06:21 AM4/20/22
to deal.II User Group
Hi Bruno,

Thanks for the advice. 

It's my understanding that compiling boost.python code outputs a shared object file (in this case, I'm guessing something like PyDealII/Debug.so) which can be loaded as a module by a python interpreter (in this case by adding the shared object location to the PYTHONPATH). Running 'find' in my build directory, I was unable to find the shared object file.

Digging deeper, I found that the actual flag is -DDEAL_II_COMPONENT_PYTHON_BINDINGS=ON, so I hadn't built the library with the correct configuration. Attempting to build the python bindings, the build of deal.ii fails, with the following error

```
-- Setting up python bindings
-- Found PythonInterp: /usr/bin/python (found version "2.7.18")
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found version "2.7.18")
CMake Error at /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
  Could NOT find Boost (missing: python27) (found suitable version "1.71.0",
  minimum required is "1.67")
Call Stack (most recent call first):
  /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.16/Modules/FindBoost.cmake:2179 (find_package_handle_standard_args)
  contrib/python-bindings/CMakeLists.txt:46 (_FIND_PACKAGE)
```

I'm confused by this--I've installed boost and the build process finds it, but I don't know why it's looking for Python 2.7, I have more recent system versions (namely, 3.8) in the same directory. I imagine this might be causing the issue with Boost, and I wouldn't be developing bindings for python 2 anyway, as it's not supported. I'm using an Ubuntu 18.04 machine and attempting to install deal.ii-9.3.3. Am I building this the wrong way? 

Thank you,
Corbin

Corbin Foucart

unread,
Apr 20, 2022, 6:56:32 AM4/20/22
to deal.II User Group
It looks like in the python-bindings CMakeLists.txt file here: https://github.com/dealii/dealii/blob/master/contrib/python-bindings/CMakeLists.txt

There's a call to 
> INCLUDE(FindPythonInterp)
on line 24. 

It seems that for cmake versions > 3.12, FindPythonInterp has been deprecated, with FindPython3 replacing it... modifying that line of the file manually, as well as manually setting the Boost version and python versions in the same CMakeLists.txt file to 1.71 and 3.8, respectively, I am able to get past the cmake errors. If the build process succeeds, I'll try writing some bindings.

Corbin

Bruno Turcksin

unread,
Apr 20, 2022, 8:15:57 AM4/20/22
to dea...@googlegroups.com
Corbin,

That's great. Let us know if you encounter any other problems. What kind of bindings do you want to write? BTW, do you want to open a PR with the FindPython3 change?

Best,

Bruno

--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "deal.II User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dealii/XlPFOictshI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/deac2b5e-0449-40f6-aeaf-ff83e536e614n%40googlegroups.com.

Corbin Foucart

unread,
Apr 20, 2022, 7:10:13 PM4/20/22
to deal.II User Group
Hi Bruno,

I'm hesitant to propose a change, as doing so may break the build process for other people using different Python binding configurations. I'm also by no means a CMake expert :-) However, if it's a safe assumption that anyone using the bindings is using Python3, sure.

As for what type of bindings, I was planing on writing some for the mesh-refinement indicators and executing mesh refinement. Most of the bindings I'm planning to write would be specific to solvers I've developed on top of deal ii, but some of the functionality might be useful to the community. The idea is to drive the adaptive refinement of a solver with a Python code that has access to machine learning libraries such as PyTorch.

Corbin

Bruno Turcksin

unread,
Apr 21, 2022, 9:04:43 AM4/21/22
to dea...@googlegroups.com
Corbin,

Le mer. 20 avr. 2022 à 19:10, Corbin Foucart <corbin....@gmail.com> a écrit :

I'm hesitant to propose a change, as doing so may break the build process for other people using different Python binding configurations. I'm also by no means a CMake expert :-) However, if it's a safe assumption that anyone using the bindings is using Python3, sure.
Python2 reached end-of-life two years ago, I think it's fair to require Python3.
 
As for what type of bindings, I was planing on writing some for the mesh-refinement indicators and executing mesh refinement. Most of the bindings I'm planning to write would be specific to solvers I've developed on top of deal ii, but some of the functionality might be useful to the community. The idea is to drive the adaptive refinement of a solver with a Python code that has access to machine learning libraries such as PyTorch.
That sounds very interesting. Note that you can already flag a cell for refinement and then execute the refinement (see here)

Best,

Bruno
 

Corbin Foucart

unread,
Apr 22, 2022, 2:08:27 AM4/22/22
to deal.II User Group
Bruno, 

Ok, I've added my fix--please see pr13631.

Best,
Corbin

Reply all
Reply to author
Forward
0 new messages