使用带接口的f2py编译模块

时间:2017-06-22 15:21:57

标签: fortran f2py

我正在尝试使用f2py编译fortran模块。它是以下代码

module my_log_mod

   implicit none

    interface my_log
        module procedure my_log_array
        module procedure my_log_vector
    end interface my_log

   private  ! hides items not listed on public statement 
   public :: my_log


contains

    subroutine my_log_array(a,res)
        double precision, dimension (:,:), intent (in) :: a
        double precision, dimension (:,:), intent (out) :: res

        where (a>1.0)
            res = log(a)
        else where
            res = 0.D0
        end where
    end subroutine  

    subroutine my_log_vector(a,res)
        double precision, dimension (:), intent (in) :: a
        double precision, dimension (:), intent (out) :: res

        where (a>1.0)
            res = log(a)
        else where
            res = 0.D0
        end where
    end subroutine  

end module my_log_mod   

我使用以下命令编译

f2py.py -c -m my_log_mod_comp my_log_mod.f90

并导致以下错误

C:\Users\weisshau\AppData\Local\Temp\tmpf0apqa7s\src.win32-3.6\my_log_mod_comp-f2pywrappers2.f90:7:28:

       use my_log_mod, only : my_log_array
                            1
Error: Symbol 'my_log_array' referenced at (1) not found in module 'my_log_mod'
C:\Users\weisshau\AppData\Local\Temp\tmpf0apqa7s\src.win32-3.6\my_log_mod_comp-f2pywrappers2.f90:18:28:

       use my_log_mod, only : my_log_vector
                            1
Error: Symbol 'my_log_vector' referenced at (1) not found in module 'my_log_mod'

我对fortran和f2py并不是很了解,所以我不知道发生了什么。如果我在纯fortran中使用该模块,它可以很好地工作

1 个答案:

答案 0 :(得分:2)

F2py似乎正在创建另一个使用模块中子程序的包装器代码。

但是它直接调用子例程my_log_vectormy_log_array。似乎f2py不支持private。我会删除private

还要做好准备,使您无法在Python中使用通用my_log。这种泛型的概念与Python不同。如果删除private不能使其可编译,则可能需要删除通用接口。你绝对应该删除public :: my_log

不幸的是,f2py并不支持现代Fortran的所有功能。

我测试的代码:

module my_log_mod

   implicit none

    interface my_log
        module procedure my_log_array
        module procedure my_log_vector
    end interface my_log

contains

    subroutine my_log_array(a,res)
        double precision, dimension (:,:), intent (in) :: a
        double precision, dimension (:,:), intent (out) :: res

        where (a>1.0)
            res = log(a)
        else where
            res = 0.D0
        end where
    end subroutine  

    subroutine my_log_vector(a,res)
        double precision, dimension (:), intent (in) :: a
        double precision, dimension (:), intent (out) :: res

        where (a>1.0)
            res = log(a)
        else where
            res = 0.D0
        end where
    end subroutine  

end module my_log_mod   

汇编:

f2py -c -m my_log_mod_comp my_log_mod.f90

...

Post-processing...
        Block: my_log_mod_comp
                        Block: my_log_mod
                                Block: my_log
                                Block: my_log_array
                                Block: my_log_vector
Post-processing (stage 2)...
        Block: my_log_mod_comp
                Block: unknown_interface
                        Block: my_log_mod
                                Block: my_log_array
                                Block: my_log_vector
Building modules...
        Building module "my_log_mod_comp"...
                Constructing F90 module support for "my_log_mod"...
                Creating wrapper for Fortran subroutine "my_log_array"
                      res = my_log_array(a)
                          res = my_log_array(a)
                Creating wrapper for Fortran subroutine "my_log_vector"("my_log_vector")...
                        Constructing wrapper function "my_log_mod.my_log_vector"...
                          res = my_log_vector(a)
        Wrote C/API module "my_log_mod_comp" to file "/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_compmodule.c"
        Fortran 90 wrappers are saved to "/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_comp-f2pywrappers2.f90"

...

gfortran:f90: /tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_comp-f2pywrappers2.f90
/usr/bin/gfortran -Wall -g -Wall -g -shared /tmp/tmp7e5v0u/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_compmodule.o /tmp/tmp7e5v0u/tmp/tmp7e5v0u/src.linux-x86_64-2.7/fortranobject.o /tmp/tmp7e5v0u/my_log_mod.o /tmp/tmp7e5v0u/tmp/tmp7e5v0u/src.linux-x86_64-2.7/my_log_mod_comp-f2pywrappers2.o -L/usr/lib64 -lpython2.7 -lgfortran -o ./my_log_mod_comp.so
Removing build directory /tmp/tmp7e5v0u
相关问题