本文介绍了回调Python从Fortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我使用 f2py 从Fortran代码调用Python函数。我尝试了一个非常简单的例子,但它没有工作。

Now I am using the f2py to call Python function from Fortran code. I have tried a very easy example but it didn't work.

Fortran90代码:

Fortran90 code:

subroutine foo(fun,r)
external fun
integer ( kind = 4 ) i
real ( kind = 8 ) r
r=0.0D+00
do i= 1,5
    r=r+fun(i)
enddo
end

使用命令行:

Python代码:

import callback

def f(i):
    return i * i
print callback.foo(f)

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: `Required argument 'r' (pos 2) not found`


推荐答案

python:

# -*- coding: utf-8 -*-
import MArray

print MArray.__doc__
print MArray.s1dim_array.__doc__
print MArray.s2dim_array.__doc__

print "="*60
print help(MArray)
print "="*60
print help(MArray.s1dim_array)


print "="*60
MArray.s1dim_array([6.,7.,8.])


print "="*60
MArray.s2dim_array([[6.,7.,8.],[1,2,3]])




subroutine S1dim_Array (iN_dim, rArray)
  implicit none
  integer,  intent(in) :: iN_dim
  real,     intent(in) :: rArray(iN_dim)
  print*, iN_dim
  print*, rArray
  Return
End subroutine


subroutine S2dim_Array (iN_Row, iN_Col, rArray)
  implicit none
  integer,  intent(in) :: iN_Row, iN_Col
  real,     intent(in) :: rArray(iN_Row, iN_Col)
  integer :: i , j
  print*, iN_Row, iN_Col
  do i = 1 , iN_Row
    write(*,*) (rArray(i,j), j = 1,iN_Col)
  enddo
  Return
End subroutine

这篇关于回调Python从Fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:56