Hi together,

I've played around with pybind11 on my laptop, it works really great. 

It is important to link to pybind11 as explained on 
https://dune-project.org/doc/pythonbindings/ and to include pybind11 from dune 
(#include <dune/python/pybind11/embed.h> in the *.cc *.hh files.

Using pybind11 is not too tricky and it is possible to use whole Python classes 
including the different methods. This allows to store some data in the python 
class and use this data later in the methods. For example, one can save the 
current time before the beginning of each time step and use this time 
information when calling methods. For example, I've used a simple python method 
to compute the source term at a given time. The python method was called during 
every call of the source term in my problem.hh. My application was about 15-20% 
slower due to the source term calls. As Bernd said, this can be optimized by 
calling the source only once per time step.

I've also tested MPI inside of python (mpi4py). It seams that this also works, 
but I need do some further tests for this feature.

I'm not sure if there should be an test or an example in DuMux to show how one 
can use embedded Python with pybind11. Please let me know if I should add some 
test/example.

Best regards,

Leo

>     [email protected] hat am 10.09.2021 10:23 geschrieben:
> 
> 
>     Hi together,
> 
>     the 1p test looks promising. I understand that the best solution is to 
> add wrappers for the shallow water model and to use extended Python instead 
> of embedded Python. However, understanding and writing these wrappers is not 
> straight forward, one has also to understand the python concepts behind the 
> dune core modules.
> 
>     I have to think about the way to go. I looks much easier to start with 
> embedded Python (bybind11) to get a fast solution for my simple task. However 
> writing some wrappers for the shallow water model seams to be the best 
> solution since it is more flexible on the long run. 
> 
>     Thank you for the detailed information.
> 
>     Leo 
> 
> 
>         > >         Timo Koch <[email protected]> hat am 09.09.2021 15:20 
> geschrieben:
> > 
> > 
> >         see 
> > https://git.iws.uni-stuttgart.de/dumux-repositories/dumux/-/tree/master/python
> >  on how to set up your environment (might change with updates in Dune 
> > coming soon)
> > 
> > 
> >             > > >             On 9. Sep 2021, at 15:19, Timo Koch 
> > <[email protected] mailto:[email protected] > wrote:
> > > 
> > >             Hi together,
> > > 
> > >             also just to add this in case someone it interested. There 
> > > also has been some more work lately on using Dumux from Python via 
> > > pybind11 bindings.
> > >             Some first full application (1p test) can be found here: 
> > > https://git.iws.uni-stuttgart.de/dumux-repositories/dumux/-/blob/master/test/python/test_1p.py
> > >             We haven’t announced this properly yet because it’s still in 
> > > it’s infancy, and more classes need wrappers and some issues are still 
> > > not solved.
> > > 
> > >             Timo
> > > 
> > > 
> > > 
> > >                 > > > >                 On 9. Sep 2021, at 15:12, Timo 
> > > Koch <[email protected] mailto:[email protected] > wrote:
> > > > 
> > > >                 Big advantage of pybind11 is also that is does 
> > > > automatic type conversion for you between Python and C++,
> > > >                 so you can easily convert between C++ std::vector and 
> > > > Python’s list etc...
> > > > 
> > > > 
> > > >                     > > > > >                     On 9. Sep 2021, at 
> > > > 15:03, Timo Koch <[email protected] mailto:[email protected] > 
> > > > wrote:
> > > > > 
> > > > >                     Hi Leo,
> > > > > 
> > > > >                     I would definitely look into this with pybind11 
> > > > > which reads very nice on the C++ side too. There are some 
> > > > > instructions at the end 
> > > > > herehttp://dune-project.org/doc/pythonbindings and in 
> > > > > dune-common/dune/python/test/ you also find two small examples for 
> > > > > embedded Python. We also have some testing code with a fluid system 
> > > > > using embedded Python on the branch “feature/python-fluidsystem” in 
> > > > > dumux (MR !2001). 
> > > > > 
> > > > >                     And of course some test for dumux is also 
> > > > > welcome. 
> > > > > 
> > > > >                     Timo
> > > > > 
> > > > > 
> > > > >                         > > > > > > 9. sep. 2021 kl. 13:36 skrev 
> > > > > [email protected] mailto:[email protected] :
> > > > > > 
> > > > > > 
> > > > > >                     > > > > > 
> > > > >                         > > > > > >                         Hi Bernd,
> > > > > > 
> > > > > >                         thank you for this nice example. I've used 
> > > > > > embedded Python 10 years ago. My code was much less elegant and so 
> > > > > > I thought that one has to do this nowadays with Pybind11. However, 
> > > > > > looking into your example it looks like the Python/C API does also 
> > > > > > the job. For me it helps a lot to see how the Python interpreter is 
> > > > > > initialized and finalized in problem.hh and how data is converted. 
> > > > > > 
> > > > > >                         Actually I need a simple Python function 
> > > > > > for the source term so there are just a few parameters which have 
> > > > > > to be passed between C++ and Python.
> > > > > > 
> > > > > >                         Best regards,
> > > > > > 
> > > > > >                         Leo
> > > > > >                          
> > > > > > 
> > > > > >                             > > > > > > >                           
> > > > > >   Flemisch, Bernd <[email protected] 
> > > > > > mailto:[email protected] > hat am 09.09.2021 
> > > > > > 12:27 geschrieben:
> > > > > > > 
> > > > > > > 
> > > > > > > 
> > > > > > >                             Hi Leo,
> > > > > > > 
> > > > > > > 
> > > > > > >                             we're doing this here:
> > > > > > > 
> > > > > > >                             
> > > > > > > https://git.iws.uni-stuttgart.de/dumux-appl/dumux-turing/-/tree/master/test/stochastic
> > > > > > > 
> > > > > > >                             I needed the python3-dev package and 
> > > > > > > had to run the executable with
> > > > > > > 
> > > > > > >                             PYTHONPATH=. ./test_stochastic
> > > > > > > 
> > > > > > > 
> > > > > > > 
> > > > > > >                             Kind regards
> > > > > > > 
> > > > > > >                             Bernd
> > > > > > >                              
> > > > > > > https://git.iws.uni-stuttgart.de/dumux-appl/dumux-turing/-/tree/master/test/stochastic
> > > > > > > 
> > > > > > > 
> > > > > > > 
> > > > > > >                             --
> > > > > > >                             
> > > > > > > _________________________________________________________________
> > > > > > > 
> > > > > > >                             Bernd Flemisch
> > > > > > >                             IWS, Universität Stuttgart            
> > > > > > >    phone: +49 711 685 69162
> > > > > > >                             Pfaffenwaldring 61              
> > > > > > > email: [email protected] 
> > > > > > > mailto:[email protected]
> > > > > > >                             D-70569 Stuttgart           url: 
> > > > > > > www.iws.uni-stuttgart.de/en/lh2/ 
> > > > > > > http://www.iws.uni-stuttgart.de/en/lh2/
> > > > > > >                             
> > > > > > > _________________________________________________________________
> > > > > > > 
> > > > > > >                             
> > > > > > > ---------------------------------------------
> > > > > > >                             Von: DuMux 
> > > > > > > <[email protected] 
> > > > > > > mailto:[email protected] > im Auftrag von 
> > > > > > > [email protected] mailto:[email protected] 
> > > > > > > <[email protected] mailto:[email protected] >
> > > > > > >                             Gesendet: Donnerstag, 9. September 
> > > > > > > 2021 11:58:04
> > > > > > >                             An: [email protected] 
> > > > > > > mailto:[email protected]
> > > > > > >                             Betreff: [DuMux] Using Embedded 
> > > > > > > Python in DuMux
> > > > > > > 
> > > > > > >                             Dear DuMux developers and users,
> > > > > > > 
> > > > > > >                             I'm interested to call Python from a 
> > > > > > > DuMux application (embedded Python). Has anyone tried this before 
> > > > > > > with DuMux? I'm also interested to use embedded Python in a 
> > > > > > > parallel application in combination with MPI4py.
> > > > > > > 
> > > > > > >                             Maybe it is a good idea if I open an 
> > > > > > > issue in gitlab and try to add a small test (dumux/test/python) 
> > > > > > > to check if/how embedded Python works.  
> > > > > > > 
> > > > > > >                             Best regards,
> > > > > > > 
> > > > > > >                             Leo
> > > > > > > 
> > > > > > > 
> > > > > > >                             Im Auftrag
> > > > > > > 
> > > > > > >                             Dr.-Ing. Leopold Stadler
> > > > > > > 
> > > > > > >                             --
> > > > > > >                             Referat Numerische Verfahren im 
> > > > > > > Wasserbau
> > > > > > >                             Abteilung Wasserbau im Binnenbereich
> > > > > > > 
> > > > > > >                             Bundesanstalt für Wasserbau
> > > > > > >                             Federal Waterways Engineering and 
> > > > > > > Research Institute
> > > > > > >                             Kußmaulstraße 17 | 76187 Karlsruhe
> > > > > > > 
> > > > > > >                             https://www.baw.de/
> > > > > > > 
> > > > > > > 
> > > > > > > 
> > > > > > >                             
> > > > > > > _______________________________________________
> > > > > > >                             DuMux mailing list
> > > > > > >                             [email protected] 
> > > > > > > mailto:[email protected]
> > > > > > >                             
> > > > > > > https://listserv.uni-stuttgart.de/mailman/listinfo/dumux
> > > > > > > 
> > > > > > >                         > > > > > > 
> > > > > > 
> > > > > >                         Im Auftrag
> > > > > > 
> > > > > >                         Dr.-Ing. Leopold Stadler
> > > > > > 
> > > > > >                         --
> > > > > >                         Referat Numerische Verfahren im Wasserbau
> > > > > >                         Abteilung Wasserbau im Binnenbereich
> > > > > > 
> > > > > >                         Bundesanstalt für Wasserbau
> > > > > >                         Federal Waterways Engineering and Research 
> > > > > > Institute
> > > > > >                         Kußmaulstraße 17 | 76187 Karlsruhe
> > > > > > 
> > > > > >                         https://www.baw.de/
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > >                         
> > > > > > _______________________________________________
> > > > > >                         DuMux mailing list
> > > > > >                         [email protected] 
> > > > > > mailto:[email protected]
> > > > > >                         
> > > > > > https://listserv.uni-stuttgart.de/mailman/listinfo/dumux
> > > > > > 
> > > > > >                     > > > > >                     
> > > > > > _______________________________________________
> > > > >                     DuMux mailing list
> > > > >                     [email protected] 
> > > > > mailto:[email protected]
> > > > >                     
> > > > > https://listserv.uni-stuttgart.de/mailman/listinfo/dumux
> > > > > 
> > > > >                 > > > > 
> > > >                 _______________________________________________
> > > >                 DuMux mailing list
> > > >                 [email protected] 
> > > > mailto:[email protected]
> > > >                 https://listserv.uni-stuttgart.de/mailman/listinfo/dumux
> > > > 
> > > >             > > > 
> > > 
> > >         > > 
> >         _______________________________________________
> >         DuMux mailing list
> >         [email protected]
> >         https://listserv.uni-stuttgart.de/mailman/listinfo/dumux
> > 
> >     > 
> 
>     Im Auftrag
> 
>     Dr.-Ing. Leopold Stadler
> 
>     --
>     Referat Numerische Verfahren im Wasserbau
>     Abteilung Wasserbau im Binnenbereich
> 
>     Bundesanstalt für Wasserbau
>     Federal Waterways Engineering and Research Institute
>     Kußmaulstraße 17 | 76187 Karlsruhe
> 
>     https://www.baw.de
> 
> 
>     _______________________________________________
>     DuMux mailing list
>     [email protected]
>     https://listserv.uni-stuttgart.de/mailman/listinfo/dumux
> 


Im Auftrag

Dr.-Ing. Leopold Stadler

--
Referat Numerische Verfahren im Wasserbau
Abteilung Wasserbau im Binnenbereich

Bundesanstalt für Wasserbau
Federal Waterways Engineering and Research Institute
Kußmaulstraße 17 | 76187 Karlsruhe

https://www.baw.de
_______________________________________________
DuMux mailing list
[email protected]
https://listserv.uni-stuttgart.de/mailman/listinfo/dumux

Reply via email to