These are chat archives for Fortran-FOSS-Programmers/General-Discussion

6th
Apr 2017
Stefano Zaghi
@szaghi
Apr 06 2017 04:42

Dear @/all

Can anyone suggest good reference for using from python modern Fortran libraries?

For example, assume you have a modern Fortran library exploiting OOP (a lot of modules, abstract types, polymorphims...), but, at the end, the library can provide a simple API to the end-user: a single concrete derived type exposing a procedure with plain real arrays dummy arguments. Assume you want to use this derived type from python. Is it possible?

F2py seems to be not useful in this scenario, but I maybe wrong, I have no experience with f2py. In the case it is not possible, with a lot of work, I can extract the derived type procedure as a standalone procedure, but I cannot totally refactor the library to trim-out all internal OOP.

Thank you in advance for any suggestions.

Cheers

Jacob Williams
@jacobwilliams
Apr 06 2017 09:12
Take a look at Ctypes. That might be what you need. The documentation doesn't mention Fortran but if you use F/C interoperability it works the same.
Stefano Zaghi
@szaghi
Apr 06 2017 09:29
Hi @jacobwilliams Thank you for your help. I found the great resource of @certik here. Indeed, Ctypes looks very promising for my needs! Thank you both!
Tom Dunn
@tomedunn
Apr 06 2017 09:48
@szaghi, I haven't used it much myself but there is https://github.com/jameskermode/f90wrap which looks to extend F2py to work with derived types.
Stefano Zaghi
@szaghi
Apr 06 2017 09:56
@tomedunn Tom, thank you for the highlight. I considered it, but it looked somehow cumbersome to use and I was not sure if the underlining OOP of the library (that is WenOOF) will be handled correctly. If my test with ctypes will fail, I'll consider it again.
Tom Dunn
@tomedunn
Apr 06 2017 10:17
@szaghi, I've also done a bit of what you're looking to do with F2py (I would have tried f90wrap but I didn't have a lot of control over the system I was doing the work on at the time). I wasn't ever able to pass derived types between Python and Fortran but I got around this in a limited sense by creating a Fortran module that contained the different top level derived types I wanted to use as module variables and then exporting functions to Python for adding/initializing these derived types. I'd love to replace it with something else someday but it works for the limited scope I needed it to.
Stefano Zaghi
@szaghi
Apr 06 2017 11:27
@tomedunn Tom, indeed, your pattern seems to be similar to what I am going to do... if you are not too much bothered by my spam I 'll probably ask for your help :smile:
Tom Dunn
@tomedunn
Apr 06 2017 11:35
That works for me, I'll do my best to answer any questions I can. From what I remember the hardest part for me was getting the compiler flags and linking working for F2py to do it's thing. So if you can get a simple example to compile then you've done well.