2012-06-07 2 views
3

파이썬 스크립트에서 사용할 수 있도록 숫자 모듈을 컴파일하는 데 f2py를 사용하고 있습니다.서브 루틴 인수가 파이썬에서 포트란으로 올바르게 전달되지 않음

fd.f :

module fd 
    ! Double precision real kind 
    integer, parameter :: dp = selected_real_kind(15) 

contains 

subroutine lprsmf(th) 
    implicit none 
    real(dp) th 
    write(*,*) 'th - fd',th 
end subroutine lprsmf 

end module fd 

itimes.f :

subroutine itimes(th) 
    use fd 
    implicit none 
    real(dp) th 

    write(*,*) 'th - it',th 
    call lprsmf(th) 
end subroutine itimes 

reprun.py :

import it 

th = 200 
it.itimes(th) 

명령 나는 아래의 최소한의 예에 내 코드를 감소 컴파일 및 실행에 사용되는 내용은 다음과 같습니다 (Windows에서 cmd을 사용하고 있음을 유의하십시오) :

gfortran -c fd.f 
f2py.py -c -m it --compiler=mingw32 fd.o itimes.f 
reprun.py 

출력은 다음과 같습니다

th - it 1.50520876326836550E-163 
th - fd 1.50520876326836550E-163 

내 첫번째 추측은 th 어떻게 든 itimes 서브 루틴을 reprun.py에서 제대로 전달되지 않는 것입니다. 그러나 코드의 정식 버전에는 다른 입력이 포함되어 있으며이 코드는 모두 올바르게 전달되므로이 동작을 이해할 수 없습니다. Fortran에서 itimes를 호출 할 때 똑같은 일을 할 수 없었기 때문에, 파이썬/포트란 인터페이스와 관련이 있다고 가정하고 있습니다. 누구든지이 문제가 발생하는 이유에 대한 통찰력을 제공 할 수 있습니까?

편집 :th = 200.0와 reprun.py에 th = 200 교체는 다음과 같은 출력을 얻을 수 :

th - it 1.19472349365371216E-298 
th - fd 1.19472349365371216E-298 
+0

파이썬이나 f2py에 대해서는 아무 것도 모른다. 그러나 th = 200을 th = 200.0으로 바꾸면 어떻게 될까? –

+0

@HighPerformanceMark, 편집 참조. 여전히 정크 값이지만 다른 값입니다. – astay13

답변

1

뿐만 아니라 모듈에 itimes 서브 루틴을 감 쌉니다. 여기에 내가 무슨 짓을 :

itimes.f90 :

module itime 

contains 

subroutine itimes(th) 
    use fd 
    implicit none 
    real(dp) th 

    write(*,*) 'th - it',th 
    call lprsmf(th) 
end subroutine itimes 

end module 

컴파일 & 실행 :

gfortran -c fd.f90 
c:\python27_w32\python.exe c:\python27_w32\scripts\f2py.py -c -m it --compiler=mingw32 fd.f90 itimes.f90 

실행 reprun.py :

import it 

th = 200 
it.itime.itimes(th) 

출력 :

th - it 200.00000000000000  
th - fd 200.00000000000000  
+0

감사합니다. 매우 도움이되었습니다. – astay13

+0

좀 더 자세히 살펴보면, itimes.f에 모듈 선언이 필요 없다는 것을 알았습니다. 임계점은 fd.o 대신 fd.f90을 f2py로 전달하는 것 같습니다. 왜 그렇게 될지 아십니까? – astay13

+0

@ astay13 아, 네 말이 맞아, 방금 습관으로 바꿨어. 확실하지, 나는 단지 f2py에 실제 Fortran 소스를 전달하려고했습니다. 또한 모듈에 항상 코드를 래핑하는 것이 좋습니다. 포트란 함정을 많이 피할 수 있습니다. – bananafish