小编Jfr*_*ixa的帖子

无法从ctypes获取fortran函数输出

我正在尝试使用ctypes从Python调用Fortran函数.我试图从子程序和函数(两者都具有相同的功能)获得结果,但我无法从函数中获得预期的输出,而子程序运行良好.问题是我有很多库使用Fortran函数而不是子例程.Fortran函数和ctypes有什么问题吗?

一段Fortran代码:

MODULE Vector
! Public types
 TYPE VectorType
    PRIVATE
    DOUBLE PRECISION, DIMENSION(3):: components = 0.0d0
 END TYPE VectorType
!---------------------------------------------------------------------    
 CONTAINS 
!---------------------------------------------------------------------
 SUBROUTINE newVect(this,vectorIn)
 TYPE (VectorType),       INTENT(OUT):: this
 DOUBLE PRECISION, DIMENSION(3), INTENT(IN)::vectorIn

   this%components = (/vectorIn(1), vectorIn(2), vectorIn(3)/)

 END SUBROUTINE newVect
!---------------------------------------------------------------------
 SUBROUTINE subVect(this,vectorOut)

 TYPE(VectorType), INTENT (OUT):: vectorOut
 TYPE(VectorType), INTENT (IN) :: this

   vectorOut%components = this%components

 END SUBROUTINE subVect
!----------------------------------------------------------------------
 TYPE(VectorType) FUNCTION getVect(this) RESULT(vectorOut)

 TYPE(VectorType), INTENT (IN) :: this

   vectorOut%components = this%components

  END FUNCTION getVect
!--------------------------------------------------------------------
END MODULE Vector
Run Code Online (Sandbox Code Playgroud)

我正在使用的Python代码是:

import …
Run Code Online (Sandbox Code Playgroud)

python fortran ctypes language-interoperability

3
推荐指数
1
解决办法
267
查看次数