Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor bugfixes and automation of PyBind11 bindings #3

Open
wants to merge 9 commits into
base: iso_c_binding
Choose a base branch
from

Conversation

vegardjervell
Copy link

@vegardjervell vegardjervell commented Mar 30, 2023

Edit: We are maintaining a fork at https://github.com/thermotools, and are also automating generation of pybind11 bindings for the C++ wrapper :)

Hi,

It looks like we may be adopting this for our project :) If we do, we will be maintaining a fork at https://github.com/thermotools.

While I was testing it I made some modifications:

  • Added setup.py and moved some files so that pip can be easily utilized.
  • Added sorting of modules before writing FortranISOWrappers.f90, so that modules are compiled in order of dependency. Also fixed bug where modules did not properly include each other.
  • Added name sanitizing before writing c++ interface, to prevent argument names that are reserved keywords in C++
  • Made the ConfigFile parser also read command-line arguments
  • Added code to generate a binding module for PyBind11 (still very experimental, may need manual modifications after being generated.)

Check if argument names are reserved keywords in C++. If so, prepend an underscore to the name. Specifically, this was the case when using "this" as an argument name on the fortran side.

Also added /venv to .gitignore
Moved the fortwrap.py file, and added a setup.py, such that fortwrap can be pip installed. Also moved some code from the __name__ == __main__ clause to the wrap() function, which is now called from the __name__ == __main__ clause. If fortwrap is pip installed, it can be run as

python -m fortwrap <ordinary fortwrap options>

I could have just made a symlink, but using "pip install -e" feels better, and is also nice if this is ever going out to pypi.
… based on regex match

Using the fields %<include/exclude>_files [pattern1 pattern2 ...] and %<include/exclude>_procedures [pattern1 pattern2 ...].
Also added the -d/--directory command line  option to the configuration file.

Had to change the name sanitizer (see: c8a9711) to prepend "c_", because fortran does not permit leading underscores.
Ensuring that the modules in FortranISOWrappers.f90 are ordered such that they are compiled in the order of required dependencies. Also implemented searching for parent classes, such that the modules in FortranISOWrappers.f90 correctly USE each other.

Successfully wrapped test case with multiple inheritance split among several modules.
Wrapper is compiled separately using cmake. The fortran files are included by first compiling the fortran files to an archive, and adding them to target_link_libraries in cmake.
test_py.py can be run to compute pressure, residual helmholtz energy and OPTIONALLY differentials. The pybind wrapping code in pybind11_bindings.cpp has been partially manually written. Specifically, the casting of py::none() to nullptr, the default value of the optional arguments to py::none(), and the casting from py::array_t to float**.

Solved: Don't use std::vector for arrays passed to fortran, just use float**.
… modifiable arrays and optional arguments.

The handling is done by passing modifiable values as np arrays: Pybind11 can directly access the underlying memory of the arrays in a lambda function and pass these to the internal c++ methods. Optional arguments are also handled by pybind11, rather than in the "plain" c++ wrapper classes.

Still needs to be fixed: FortWrap incorrectly generates C++ methods taking e.g. float* as arguments, when they should take float** (see FortWrap thermopack demo). This is likely a quick fix in the c_arg_list function.
… Options class

Now all parsing of options happens in one place, and the config file can be used to supply all options.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant