子例程参数未从Python正确传递到Fortran

2024-05-19 07:58:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用f2py编译一个数值模块,供Python脚本使用。我将代码简化为下面的最小示例:

财务总监:

module fd
  ! Double precision real kind
  integer, parameter :: dp = selected_real_kind(15)

contains

subroutine lprsmf(th)
  implicit none
  real(dp) th
  write(*,*) 'th - fd',th
end subroutine lprsmf

end module fd

时间。f:

^{pr2}$

在reprun.py公司名称:

import it

th = 200
it.itimes(th)

用于编译和运行的命令如下(请注意,我在Windows下使用cmd):

gfortran -c fd.f
f2py.py -c -m it --compiler=mingw32 fd.o itimes.f
reprun.py

输出为:

th - it  1.50520876326836550E-163
th - fd  1.50520876326836550E-163

我的第一个猜测是th以某种方式没有从reprun.py正确地传递到子例程{}。但是,我不理解这种行为,因为代码的完整版本包括其他输入,所有这些输入都是正确传递的。在从Fortran调用itimes时,我不能让它做同样的事情,所以我假设它与Python/Fortran接口有关。有人能提供一些关于这种行为发生的原因的见解吗?在

编辑:替换th = 200reprun.pywith th = 200.0生成以下输出:

th - it  1.19472349365371216E-298
th - fd  1.19472349365371216E-298

Tags: 代码pyitrealenddpmodulef2py
1条回答
网友
1楼 · 发布于 2024-05-19 07:58:28

将itimes子例程也包装在一个模块中。以下是我所做的:

itimes.f90:

module itime

contains

subroutine itimes(th)
  use fd
  implicit none
  real(dp) th

  write(*,*) 'th - it',th
  call lprsmf(th)
end subroutine itimes

end module

编译和运行:

^{pr2}$

跑reprun.py公司名称:

^{3}$

输出:

 th - it   200.00000000000000     
 th - fd   200.00000000000000     

相关问题 更多 >

    热门问题