2017-06-22 96 views
1

我想用f2py編譯一個fortran模塊。這是下面的代碼使用f2py編譯一個帶有接口的模塊

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中使用模塊,它可以很好地工作

回答

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 
+0

非常感謝您的回答。我嘗試了你的兩個建議,但它仍然不起作用。也許我應該擴大我的問題:「我如何編寫一個fortran子程序,將任意數組作爲輸入和輸出,並將其與f2py一起使用」 – Jannick

+0

它可以在我的計算機上運行。至少在你提供的代碼中。解釋*它不起作用*。這句話沒有說明什麼。你什麼意思? –

+0

*「我如何編寫一個將任意數組作爲輸入和輸出並將其與f2py結合使用的Fortran子例程」*您不需要。這就是我在回答中所提出的觀點。 –