【问题标题】:compiling basic fortran module with f2py用 f2py 编译基本的 fortran 模块
【发布时间】:2021-09-08 19:12:12
【问题描述】:

我正在尝试使用 F2py,但在涉及“已弃用的 numpy”的编译时收到了一些错误消息或警告。我不确定如何解决这个问题。我正在使用 python 2.7.17 和 fortran90。

我通过写作编译
f2py -c f2py_F90.f90 -m fortran

注意,编译时使用: f2py -c f2py_F90.f90 -m fortran \
也不能解决问题。

以下是指示我遇到的问题的基本 fortran 和 python 代码。这个例子是最小的、完整的和可验证的。如何解决此问题并成功让 python 执行我传递给它的 fortran 模块?

我收到的信息是

警告“使用已弃用的 NumPy API”

预期的输出应该是 a = [2,1,3] 但我收到了上述警告。

!fortran code
module fmodule
   implicit none
contains
   subroutine fast_reverse(a,n)

   integer, intent(in) :: n
   real, intent(inout) :: a(:)

   a(1:n) = a(n:1:-1)

   end subroutine fast_reverse
end module fmodule

#python code
import fortran
import numpy as np

a = np.array([1,2,3],np.float32)

fortran.fmodule.fast_reverse(a,2)

a #the output should give a = [2,1,3] 

【问题讨论】:

  • 如果你是getting some error messages or warnings,你需要把它们放在这里。
  • @RandomDavis 我明确添加了从终端获得的错误消息,谢谢。我也已经在原始帖子中说明了此错误消息。请阅读。
  • 测试一些东西:使用python -m numpy.f2py -c fmodule.f90 -m fortran 构建,这样您就知道两个命令使用了相同的python 解释器。诊断:which -a pythonwhich -a f2py。另外,有什么理由使用 Python 2?
  • 您好 Jeff,这些命令旨在查看您是否安装了多个 Python 或某些软件包,可能存在冲突。您是否有可能使用 pip 安装软件包?
  • @JeffFaraci 我同意我的建议不会修复您的设置。如果您的设置在f2py 演示(我在我的机器上验证过)上不起作用,感觉就像您遇到了设置问题。如果您没有完成安装,您可以尝试在虚拟机中运行演示吗?一旦你有了一个你知道可以工作的设置,它将使你的代码调试变得更加容易。

标签: python fortran f2py


【解决方案1】:
  1. 忽略#warning "Using deprecated NumPy API, disable it with ...,见Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryViewCython docs
  2. 将最后一行 a #the output should give a = [2,1,3] 更改为 print(a) #the output should give a = [2,1,3] 以将 a 打印为 stdout

例如:

$ python2.7 -m numpy.f2py -c f2py_F90.f90 -m fortran
running build
...
      /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h :it17 :with2 :"          "#define 
  NPY_NO_DEPRECATED_APIwarning : NPY_1_7_API_VERSION" [-W#warnings]

  "Using deprecated NumPy API, disable it with "          "#define
  NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it with " \
...

$ cat test_f2py_F90.py 
#python code
import fortran
import numpy as np

a = np.array([1,2,3],np.float32)

fortran.fmodule.fast_reverse(a,2)

print(a) #the output should give a = [2,1,3]



$ python2.7 test_f2py_F90.py 
[2. 1. 3.]

【讨论】:

  • 您好,非常感谢您的回答。我很清楚,我首先在 python 代码中将a 更改为print(a)。然后我用python2.7 test_f2py_F90.py在命令行编译,对吧?我试过了,它给出了正确的输出......我只是想确定这确实是解决方案,对吗?非常感谢
  • @JeffFaraci,“我试过这个,它给出了正确的输出......” - 恭喜,我们做到了?!关键修复是print(a)(我在将 jupyter-notebook 或交互式会话转换为 python 脚本时的常见错误)。在您当前的环境中,python2.7 可能等同于 python(在我的示例中,使用它是因为在我的环境中 pythonpython3.9 相关联)。
  • 感谢您的出色回答。你让它看起来微不足道......事实上,我只尝试了python 而不是python2.7,它也同样有效!优秀的。再次感谢
  • @JeffFaraci, “...python2.7 test_f2py_F90.py 在命令行中编译,对吗?” - 不,这些是不准确的话。 python2. 7 -m numpy.f2py -c f2py_F90.f90 -m fortran - 根据您的环境,将文件 f2py_F90.f90 中的 fortran 代码编译为 fortran.sofortran.cpython-38-darwin.so 或其他内容。 python2.7 test_f2py_F90.py - 解释来自文件test_f2py_F90.py 的python 代码,并且在解释期间,import fortran 运算符加载编译后的fortran.so
  • @JeffFaraci,P.S.你也可以使用这个目录中的 jupyter-notebook,或者使用命令python2.7 启动一个交互式会话并手动调用所有必要的 python 语句。
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多