[f2py] f2py with commonblocks - problem
Pearu Peterson
pearu at cens.ioc.ee
Tue Jul 22 11:24:46 EEST 2008
On Tue, July 22, 2008 10:43 am, Jan.Sippli at dlr.de wrote:
> Hello,
> I'm a student at the german aerospace-center in germany.
> I have some problems with wrapping code from fortran to python by using
> common blocks. To show you my problem I paste you some example-fortran
> code. It isn't allowed (to me) to change the source code to solve the
> problem.
The example-fortran code seem to use poor programming style:
the dimensions of the work argument are defined in a common block.
Hmm, it can be also a clever trick for Fortran.. Could you check whether
the original Fortran code uses the same trick? If yes, read on.
Otherwise, fix the example code and run f2py as follows:
f2py -c --fcompiler=gnu95 sub.f -m test
(note that it does not make sense to wrap the main program to python,
hence main.f is not used in f2py argument list).
>
> When I try to create the python-modul I build the .pyf-file first with the
> following command:
>
> f2py main.f sub.f -m test -h test.pyf
>
> Reading fortran codes...
> Reading file 'main.f' (format:fix,strict)
> Reading file 'sub.f' (format:fix,strict)
> Post-processing...
> Block: test
> Block: test
> Block: set
> Block: compute
> Block: output
> Post-processing (stage 2)...
> Saving signatures to file "./test.pyf"
>
>
>
> Then I try to build the .so-file by doing this
> f2py -c test.pyf main.f sub.f --f90exec=/usr/bin/gfortran
Please use --fcompiler=gnu95 instead of --f90exec switch (otherwise
you'll get undefined symbol errors later).
> But then I get an ERROR when gcc tries to compile the testmodul.c
> for example "n1,n2 is not defined"
Ok, here follows a workaround:
1) Run
f2py -m test -h test.pyf sub.f
2) Edit test.pyf file as follows (replace dimension specs n1,n2
by *,*, respectively). Here's the content of resulting test.pyf file:
python module test ! in
interface ! in :test
subroutine set ! in :test:sub.f
integer :: n1
integer :: n2
common /sizes/ n1,n2
end subroutine set
subroutine compute(work) ! in :test:sub.f
real dimension(*,*) :: work
end subroutine compute
subroutine output(work) ! in :test:sub.f
real dimension(*,*) :: work
end subroutine output
end interface
end python module test
3) Run
f2py -c test.pyf sub.f --fcompiler=gnu95
4) In python:
>>> from numpy import *
>>> import test
>>> test.set()
>>> work = zeros((test.sizes.n1, test.sizes.n2),dtype='f',order='FORTRAN')
>>> test.compute(work)
>>> print work
>>> test.output(work)
Note that dtype='f' and order='FORTRAN' options are crucial to use.
HTH,
Pearu
More information about the f2py-users
mailing list