子例程参数没有从Python正确传递到Fortran [英] Subroutine argument not passed correctly from Python to Fortran
问题描述
我使用f2py编译一个数字模块以供Python脚本使用。我已将代码缩减为下面的最小示例:
fd.f:
模块fd
!双精度真实类型
整数,参数:: dp = selected_real_kind(15)
包含
子程序lprsmf(th)
隐含无$ b $ (*,*)'th - fd',th
结束子程序lprsmf
结束模块fd
itimes.f:
子程序itimes (th)
使用fd
隐式无
真(dp)th
写(*,*)'th - it',th
call lprsmf(th)
结束子程序itimes
reprun.py:
导入它
th = 200
it.itimes(th)
$ c $ (注意,我使用的是 cmd
用于编译和运行的命令如下在Windows下):
gfortran -c fd.f
f2py.py -c -m it --compiler =输出为: >
th - it 1.50520876326836550E-163
th - fd 1.50520876326836550E-163
我的第一个猜测是 th
不知何故从 reprun.py
到子程序 itimes
不正确。但是,我不理解这种行为,因为完整版本的代码包含其他输入,所有这些都是正确传递的。从Fortran调用itime时,我无法让它做同样的事情,所以我假设它与Python / Fortran接口有关。任何人都可以提供有关此行为发生的原因吗?
编辑:替换 th = 200 $ c $使用 th = 200.0
在reprun.py中生成以下输出:
th - it 1.19472349365371216E-298
th - fd 1.19472349365371216E-298
解决方案将你的itimes子程序包装在模块中。这是我做的:
itimes.f90:
模块itime
包含
子程序itimes(th)
使用fd
隐含无
真实(dp)th
写(*,*)'th - it',th
调用lprsmf(th)
结束子程序itimes
结束模块
编译&运行:
gfortran -c fd.f90
c:\python27_w32\python.exe c:\python27_w32 \scripts\f2py.py -c -m it --compiler = mingw32 fd.f90 itimes.f90
运行reprun.py:
导入它
th = 200
it .itime.itimes(th)
输出:
th - 它200.00000000000000
th - fd 200.00000000000000
I am using f2py to compile a numerical module for use by a Python script. I have reduced my code to the minimal example below:
fd.f:
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
itimes.f:
subroutine itimes(th)
use fd
implicit none
real(dp) th
write(*,*) 'th - it',th
call lprsmf(th)
end subroutine itimes
reprun.py:
import it
th = 200
it.itimes(th)
The commands used to compile and run are as follows (Note that I am using cmd
under Windows):
gfortran -c fd.f
f2py.py -c -m it --compiler=mingw32 fd.o itimes.f
reprun.py
The output is:
th - it 1.50520876326836550E-163
th - fd 1.50520876326836550E-163
My first guess is that th
is somehow not being passed correctly from reprun.py
to subroutine itimes
. However, I do not understand this behavior, as the full version of the code includes other inputs, all of which are passed correctly. I was not able to get it to do the same thing when calling itimes from Fortran, so I'm assuming it has something to do with the Python/Fortran interface. Can anyone provide any insights into why this behavior occurs?
EDIT: Replacing th = 200
in reprun.py with th = 200.0
yields the following output:
th - it 1.19472349365371216E-298
th - fd 1.19472349365371216E-298
解决方案 Wrap your itimes subroutine in a module as well. Here is what i did:
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
compile & run:
gfortran -c fd.f90
c:\python27_w32\python.exe c:\python27_w32\scripts\f2py.py -c -m it --compiler=mingw32 fd.f90 itimes.f90
run reprun.py:
import it
th = 200
it.itime.itimes(th)
output:
th - it 200.00000000000000
th - fd 200.00000000000000
这篇关于子例程参数没有从Python正确传递到Fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!