如何访问用 Python 编写的 FORTRAN 函数?
有没有办法在 FORTRAN 中使用 python 函数?我得到了一个包含一些函数的 python 脚本,我需要从 FORTRAN 代码访问这个函数。
我见过“f2py”,它允许从 Python 访问 FORTRAN 子例程,以及 py2exe,它将 python 脚本编译为可执行文件。有“py2f”的东西吗? Python 脚本可以编译为目标文件吗?然后我可以将它与 FORTRAN 代码链接起来。
例如,将“mypython_func.py”视为包含函数的 Python 脚本,将“mainfortran.f”视为调用 Python 函数的主程序 FORTRAN 代码。我想: 从 'mypython_func.py' 编译为 'mypython_func.o',从 'mainfortran.f' 编译为 'mainfortran.o' (>> gfortran -c mainfortran.f),然后链接这些文件 (>> gfortran -c mainfortran.o mypython_func.o -o myexec.exe)。这样的事情可能吗?
感谢您的时间和帮助。
文斯
Is there any way to use a python function in FORTRAN? I was given a python script that contains some functions and I need to access this function from a FORTRAN code.
I've seen 'f2py' which allows a FORTRAN subroutine to be accessed from Python, and py2exe which will compile python script to an executable. Is there anything out there for 'py2f'? Can a Python script be compiled to an object file? Then I could link it with the FORTRAN code.
For example consider 'mypython_func.py' as a Python script containing a function and 'mainfortran.f' as the main program FORTRAN code which calls the Python function. I would like to:
from 'mypython_func.py' compile to 'mypython_func.o', from 'mainfortran.f' compile to 'mainfortran.o' (>> gfortran -c mainfortran.f), then link these files (>> gfortran -c mainfortran.o mypython_func.o -o myexec.exe). Is anything like this possible?
Thank you for your time and help.
Vince
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要浪费大量时间编译和翻译。这样做。
Fortran 第 1 部分程序编写了一个供 Python 执行的内容的文件。写入标准输出。将此称为 F1
Python 读取文件,进行 Python 计算,将响应写入 Fortran 文件。将此称为 P。
Fortran 第 2 部分程序从标准输入读取内容文件。这些是Python计算的结果。
连接它们
您无需重新编译任何内容。另请注意,所有三个同时运行,这可能会带来相当大的加速。
Python的中间部分应该是这样的。
函数的简单包装器,以 Fortran 友好的格式读取 stdin 并写入 stdout。
Don't waste a lot of time compiling and translating. Do this.
Fortran Part 1 program writes a file of stuff for Python to do. Write to stdout. Call this F1
Python reads a file, does the Python calculation, write the responses to a file for Fortran. Call this P.
Fortran Part 2 program reads a file of stuff from stdin. These are the results of the Python calculations.
Connect them
You don't recompile anything. Also note that all three run concurrently, which may be a considerable speedup.
The middle bit of Python should be something like this.
A simple wrapper around the function that reads stdin and writes stdout in a Fortran-friendly format.
@S.Lott 的解决方案是可行的方法。但要回答你的问题——是的,可以从 Fortran 调用 Python。我首先使用 Cython 的 C API 公开 Python,然后使用
iso_c_binding
模块创建这些 C 函数的 Fortran 接口,最后从 Fortran 调用它们。它非常重量级,在大多数实际问题中不值得(使用管道方法),但它是可行的(它对我有用)。@S.Lott's solution is the way to go. But to answer your question --- yes, it is possible to call Python from Fortran. I have done it by first exposing Python using Cython's C API, then creating Fortran interface to those C function using the
iso_c_binding
module, and finally calling them from Fortran. It's very heavyweight and in most practical problems not worthy (use the pipes approach), but it's doable (it worked for me).