Fortran Routine calling a Python Function

In summary, the individual has been trying to merge python and fortran for a faster and more user-friendly interface. They have spent the last two days learning how to use f2py and are wondering if it is possible to call a Python function from Fortran instead of the other way around. They have found articles discussing fortran mixed-language programming and one article showed how to call a C function from fortran. However, it may be more difficult to call a python function from fortran as python code is typically interpreted. The individual has given up on this idea and has instead remade their fortran program to be used with python, which they found to be a much easier and more efficient option. They also mention that Python is a cool
  • #1
LucasCampos
17
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
 
Technology news on Phys.org
  • #2
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:
  • #3
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!
 

1. What is Fortran?

Fortran is a high-level programming language commonly used for scientific and engineering applications. It was first developed in the 1950s and has since undergone several updates and versions.

2. How can I call a Python function from a Fortran routine?

Fortran has built-in support for calling external functions written in other languages, including Python. This can be done by using the Fortran-C interoperability features or by using a Fortran wrapper library, such as f2py.

3. Can I pass data between a Fortran routine and a Python function?

Yes, it is possible to pass data between Fortran and Python by using compatible data types and following the proper syntax for the chosen method of calling the function. It is important to carefully consider data compatibility and type conversions when passing data between different languages.

4. Are there any limitations when using Fortran and Python together?

While there are some limitations when using Fortran and Python together, such as data type compatibility and potential performance issues, these can be overcome with proper planning and understanding of the available tools and techniques. Additionally, there may be differences in syntax and programming paradigms between the two languages that may require some adjustments in code.

5. What are some potential benefits of using Fortran and Python together?

Combining Fortran and Python can offer the best of both worlds - the speed and efficiency of Fortran for computationally intensive tasks, and the flexibility and ease of use of Python for data analysis and visualization. This combination can also make it easier to bridge the gap between legacy Fortran code and newer, more modern applications.

Similar threads

  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
4
Views
572
  • Programming and Computer Science
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top