Package: pyprecice
Python language bindings for the C++ library preCICE⚠️ The latest version of the documentation for the python bindings can be found onprecice.org. The information from thisREADMEis currently under revision and will be moved ⚠️This package provides python language bindings for the C++ librarypreCICE. Note that the first three digits of the version number of the bindings indicate the preCICE version that the bindings support. The last digit represents the version of the bindings. Example:v2.0.0.1andv2.0.0.2of the bindings represent versions1and2of the bindings that are compatible with preCICEv2.0.0.User documentationPlease refer tothe preCICE documentationfor information on how to install and use the python bindings. Information below is intended for advanced users and developers.Required dependenciespreCICE: Refer tothe preCICE documentationfor information on building and installation.C++: A working C++ compiler, e.g.,g++.MPI:mpi4pyrequires MPI to be installed on your system.Installing the packageWe recommend using pip3 (version 19.0.0 or newer required) for the sake of simplicity. You can check your pip3 version viapip3 --version. To update pip3, use the following line:$pip3install--user--upgradepipUsing pip3preCICE system installsFor system installs of preCICE, installation works out of the box. There are different ways how pip can be used to install pyprecice. pip will fetch cython and other build-time dependencies, compile the bindings and finally install the package pyprecice.(recommended) installpyprecice from PyPI$pip3install--userpypreciceprovide the link to this repository to pip (replace<branch>with the branch you want to use, preferablymasterordevelop)$pip3install--userhttps://github.com/precice/python-bindings/archive/<branch>.zipif you already cloned this repository, execute the following command from this directory:$pip3install--user.note the dot at the end of the linepreCICE at custom location (setting PATHS)If preCICE (the C++ library) was installed in a custom prefix, or only built but not installed at all, you have to extend the following environment variables:LIBRARY_PATH,LD_LIBRARY_PATHto the library location, or$prefix/libCPATHeither to thesrcdirectory or the$prefix/includeThe preCICE documentation provides more informaiton onlinking preCICE.Using SpackYou can also install the python language bindings for preCICE via Spack by installing the Spack packagepy-pyprecice. Refer toour installation guide for preCICE via Spackfor getting started with Spack.Using setup.pypreCICE system installsIn this directory, execute:$python3setup.pyinstall--userpreCICE at custom location (setting PATHS)see above. Then run$python3setup.pyinstall--userpreCICE at custom location (explicit include path, library path)Install cython and other dependencies via pip3$pip3install--usersetuptoolswheelcythonpackagingnumpyOpen terminal in this folder.Build the bindings$python3setup.pybuild_ext--include-dirs=$PRECICE_ROOT/src--library-dirs=$PRECICE_ROOT/build/lastOptions:--include-dirs=, default:''Path to the headers of preCICE, point to the sources$PRECICE_ROOT/src, or the your custom install prefix$prefix/include.NOTES:If you have built preCICE using CMake, you can pass the path to the CMake binary directory using--library-dirs.It is recommended to use preCICE as a shared library here.Install the bindings$python3setup.pyinstall--userClean-upoptional$python3setup.pyclean--allTest the installationUpdateLD_LIBRARY_PATHsuch that python can findprecice.so$exportLD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PRECICE_ROOT/build/lastRun the following to test the installation:$python3-c"import precice"Unit testsClean-upmandatory(because we must not link against the realprecice.so, but we use a mocked version)$python3setup.pyclean--allSetCPLUS_INCLUDE_PATH(we cannot usebuild_extand the--include-dirsoption here)$exportCPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:$PRECICE_ROOT/srcRun tests with$python3setup.pytestUsageYou can find the documentation of the implemented interface in the fileprecice.pyx. For an example of howpyprecicecan be used please refer to the1D elastic tube example.NoteThe python package that is installed is calledpyprecice. It provides the python moduleprecicethat can be use in your code viaimport precice, for example.Troubleshooting & miscellaneouspreCICE is not foundThe following error shows up during installation, if preCICE is not found:/tmp/pip-install-d_fjyo1h/pyprecice/precice.cpp:643:10:fatalerror:precice/Participant.hpp:Nosuchfileordirectory643|#include "precice/Participant.hpp"|^~~~~~~~~~~~~~~~~~~~~~~~~~~~~compilationterminated.error:command'x86_64-linux-gnu-gcc'failedwithexitstatus1----------------------------------------ERROR:Failedbuildingwheelforpyprecice
Failedtobuildpyprecice
ERROR:CouldnotbuildwheelsforpyprecicewhichusePEP517andcannotbeinstalleddirectlyOr, for preCICE v2:/tmp/pip-install-d_fjyo1h/pyprecice/precice.cpp:643:10:fatalerror:precice/SolverInterface.hpp:Nosuchfileordirectory643|#include "precice/SolverInterface.hpp"|^~~~~~~~~~~~~~~~~~~~~~~~~~~~~compilationterminated.error:command'x86_64-linux-gnu-gcc'failedwithexitstatus1----------------------------------------ERROR:Failedbuildingwheelforpyprecice
Failedtobuildpyprecice
ERROR:CouldnotbuildwheelsforpyprecicewhichusePEP517andcannotbeinstalleddirectlyThere are two possible reasons, why preCICE is not found:preCICE is not installed. Please download and install the C++ library preCICE. Seeabove.preCICE is installed, but cannot be found. Please make sure that preCICE can be found during the installation process. See our wiki page onlinking to preCICEandthe instructions above.Version of Cython is too oldIn case the compilation fails withshared_ptr.pxd not foundmessages, check if you use the latest version of Cython.Python.hmissing$python3-mpipinstallpyprecice
Collectingpyprecice
.../tmp/pip-build-7rj4_h93/pyprecice/precice.cpp:25:20:fatalerror:Python.h:Nosuchfileordirectorycompilationterminated.error:command'x86_64-linux-gnu-gcc'failedwithexitstatus1----------------------------------------FailedbuildingwheelforpyprecicePlease try to installpython3-dev. E.g. viaapt install python3-dev. Please make sure that you use the correct version (e.g.python3.5-devorpython3.6-dev). You can check your version viapython3 --version.libprecice.sois not found at runtime$python3-c"import precice"Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>
ImportError:libprecice.so.2:cannotopensharedobjectfile:NosuchfileordirectoryMake sure that yourLD_LIBRARY_PATHincludes the directory that containslibprecice.so. The actual path depends on how you installed preCICE. Example: If preCICE was installed usingsudo make installand you did not define aCMAKE_INSTALL_PREFIXthe library path is/usr/local/lib. This means you have toexport LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH.I'm using preCICE < 2.0.0, but there is no matching version of the bindings. What can I do?If you want to use the old experimental python bindings (released with preCICE version < 2.0.0), please refer to the corresponding preCICE version. Example: for preCICE v1.6.1 there are three different versions of the python bindings:precice_future,preciceandPySolverInterface. Installation instructions can be found in the correspondingREADMEfiles.Installing the python bindings for Python 2.7.17Note that the instructions in this section are outdated and refer to the deprecated python bindings. Until we have updated information on the installation procedure for the python bindings under this use-case, we will keep these instructions, since they might still be very useful(Originally contributed byhuangq1234553to the precice wiki inprecice/precice/wiki:8bb74b7.)show detailsThis guide provides steps to install python bindings for precice-1.6.1 for a conda environment Python 2.7.17 on the CoolMUC. Note that preCICE no longer supports Python 2 after v1.4.0. Hence, some modifications to the python setup code was necessary. Most steps are similar if not identical to the basic guide without petsc or python above. This guide assumes that the Eigen dependencies have already been installed.Load the prerequisite libraries:moduleloadgcc/7
moduleunloadmpi.intel
moduleloadmpi.intel/2018_gcc
moduleloadcmake/3.12.1At the time of this writingmodule load boost/1.68.0is no longer available. Instead
boost 1.65.1 was installed per theboost and yaml-cppguide above.In order to have the right python dependencies, a packaged conda environment was transferred to
SuperMUC. The following dependencies were installed:numpympi4pyWith the python environment active, we have to feed the right python file directories to the cmake command.
Note that -DPYTHON_LIBRARY expects a python shared library. You can likely modify the version to fit what is required.mkdirbuild&&cdbuild
cmake-DBUILD_SHARED_LIBS=ON-DPRECICE_FEATURE_PETSC_MAPPING=OFF-DPRECICE_FEATURE_PYTHON_ACTIONS=ON-DCMAKE_INSTALL_PREFIX=/path/to/precice-DCMAKE_BUILD_TYPE=Debug..-DPYTHON_INCLUDE_DIR=$(python-c"from distutils.sysconfig import get_python_inc; print(get_python_inc())")-DPYTHON_LIBRARY=$(python-c"import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR')+'/libpython2.7.so')")-DNumPy_INCLUDE_DIR=$(python-c"import numpy; print(numpy.get_include())")make-j12makeinstallAfter installing, make sure you add the preCICE installation paths to your.bashrc, so that other programs can find it:exportPRECICE_ROOT="path/to/precice_install"exportPKG_CONFIG_PATH="path/to/precice_install/lib/pkgconfig:${PKG_CONFIG_PATH}"exportCPLUS_INCLUDE_PATH="path/to/precice_install/include:${CPLUS_INCLUDE_PATH}"exportLD_LIBRARY_PATH="path/to/precice_install/lib:${LD_LIBRARY_PATH}"Then, navigate to the python_future bindings script.cd/path/to/precice/src/precice/bindings/python_futureAppend the following to the head of the file to allow Python2 to run Python3 code. Note that
importingunicode_literalsfromfuturewill cause errors insetuptoolsmethods as string literals
in code are interpreted asunicodewith this import.from__future__import(absolute_import,division,print_function)frombuiltinsimport(bytes,dict,int,list,object,range,str,ascii,chr,hex,input,next,oct,open,pow,round,super,filter,map,zip)Modifympicompiler_default = "mpic++"tompicompiler_default = "mpicxx"in line 100.
Run the setup file using the default Python 2.7.17.pythonsetup.pyinstall--userValueError while importing preCICEIf you face the error:ValueError:numpy.ndarraysizechanged,mayindicatebinaryincompatibility.Expected88fromCheader,got80fromPyObjectmake sure that you are using an up-to-date version of NumPy. You can update NumPy withpip3installnumpy--upgradeContributorsBenjamin RodenbergIshaan DesaiSaumitra Vinay Joshicontributed first working prototype in3db9c9onprecice/preciceFrédéric SimonisFlorian LindnerBenjamin UekermannGerasimos Chourdakis