Fortran Routine calling a Python Function

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 6K views
LucasCampos
Messages
17
Reaction score
0
I have been trying to merge some python with fortran, to build a nice interface but still be fast. I spent the last two days learning how to use f2py, buy everything would be so much easier if I could call a Python function from Fortran instead of doing the other way around.

Anyone know how to do something like that, if it's even possible?

For example
Code:
Subroutine Example (var1,var2)
Implicit None
Real, Intent(in) :: var1,var2
Real :: var3,var4
command1
command2
.
.
.
pythonfunction(var3,var4)
.
.
.
commandn
EndSubroutine
 
Physics news on Phys.org
I found several articles that discussed fortran mixed-languague programming (my search string was 'fortran "mixed language" programming'. One article that showed up showed how to call a C function from fortran (http://www.vni.com/company/whitepapers/StandardizedMixedLanguageProgrammingforCandFortran.pdf , page 10).

It might be more difficult to call a python function from fortran, unless the python code could be compiled, instead of being interpreted, which is the usual case, I believe.
 
Last edited by a moderator:
Thanks for the answer, but I've given up all hope to call python from fortran. I remade my fortran program to be used with python. Took me the whole afternoon, far less than I expected, and the result was quite nice.

Python is quite a cool language to use with fortran. I wouldn't even think of doing a GUI on fortran, and on python took me just one afternoon, learning included!