Python f2py stop命令的synnonym
我有一个从python调用的fortran代码,只要需要它。有时,在fortran计算中会产生错误,并使用STOP命令进行处理,这将完全停止fortran和python代码。但是,我需要python继续运行。是否有任何其他命令可以停止fortran代码而不影响python?在您的情况下,我将使用一些状态变量和Python f2py stop命令的synnonym,python,fortran,f2py,Python,Fortran,F2py,我有一个从python调用的fortran代码,只要需要它。有时,在fortran计算中会产生错误,并使用STOP命令进行处理,这将完全停止fortran和python代码。但是,我需要python继续运行。是否有任何其他命令可以停止fortran代码而不影响python?在您的情况下,我将使用一些状态变量和返回,对于类似的子例程 子例程mySqrt(数字、分辨率、状态) 隐式无 真实意图:数字 真实,意图(外)::res 整数,意图(输出)::stat 如果(数值
返回
,对于类似的子例程
子例程mySqrt(数字、分辨率、状态)
隐式无
真实意图:数字
真实,意图(外)::res
整数,意图(输出)::stat
如果(数值<0.e0),则
stat=-1!任意数
回来!出口
恩迪夫
res=sqrt(数量)
统计=0
结束子程序
对于函数来说,这有点困难,但您可以通过全局(模块)变量来解决,但这不是线程安全的(在此版本中):
模块测试
整数,私有::lastSuccess
包含
函数mySqrt(编号)
隐式无
真实意图:数字
real::mySqrt
如果(数值<0.e0),则
lastSuccess=-1!任意数
mySqrt=0!设置一些值s.t。函数返回一些值
回来!出口
恩迪夫
mySqrt=sqrt(编号)
lastSuccess=0
端函数
函数checkRes()
隐式无
整数::checkRes
checkRes=最后一次成功
端函数
端模块测试
通过这种方式,您首先评估函数,然后可以检查它是否成功。不需要停止。您甚至可以使用不同的错误代码
另一种方法(没有内部变量)是设置不可信的结果(比如这里的负数),并在Python代码中进行检查
subroutine mySqrt(number, res, stat)
implicit none
real,intent(in) :: number
real,intent(out) :: res
integer,intent(out) :: stat
if ( number < 0.e0 ) then
stat = -1 ! Some arbitrary number
return ! Exit
endif
res = sqrt(number)
stat = 0
end subroutine
module test
integer,private :: lastSuccess
contains
function mySqrt(number)
implicit none
real,intent(in) :: number
real :: mySqrt
if ( number < 0.e0 ) then
lastSuccess = -1 ! Some arbitrary number
mySqrt = 0. ! Set some values s.t. the function returns something
return ! Exit
endif
mySqrt = sqrt(number)
lastSuccess = 0
end function
function checkRes()
implicit none
integer :: checkRes
checkRes = lastSuccess
end function
end module test