f2py and common blocks /Carl

C

Carl

I have been experimenting with f2py and some fortran code that I want to
port to Python.

I have the following fortran file (TEST_00.f):

C FILE: TEST_00.f
SUBROUTINE FOO(WORK)
IMPLICIT REAL*8 (A-H, O-Z)
COMMON /SIZES/ NINT
DIMENSION WORK(NINT)
DIMENSION USOL(NINT)
DO 10 I=1,NINT
WORK(I)=0.0
PRINT *, "In Fortran WORK(I)=", WORK(I)
10 CONTINUE
END
DO 10 I=1,NINT
USOL(I)=0.0
PRINT *, "In Fortran USOL(I)=", USOL(I)
10 CONTINUE
END
C END OF TEST_00.f

and the following signature file (generated by f2py TEST_00.f -m TEST_00 -h
TEST_00.pyf):

python module TEST_00 ! in
interface ! in :TEST_00
subroutine foo(work) ! in :TEST_00:TEST_00.f
real*8 dimension(nint) :: work
integer optional,check(len(work)>=nint),depend(work) ::
nint=len(work)
common /sizes/ nint
end subroutine foo
end interface
end python module TEST_00

When compiling (with f2py -c TEST_00.pyf TEST_00.f) I get the following
error message:

/tmp/tmpl75SQT/src/TEST_00module.c:149: error: `nint' undeclared (first use
in this function)

Question: How can one declare arrays passed as parameters via common block
variables?

Yours/ Carl
 
D

Dennis Lee Bieber

I have been experimenting with f2py and some fortran code that I want to
port to Python.

I have the following fortran file (TEST_00.f):

C FILE: TEST_00.f
SUBROUTINE FOO(WORK)
IMPLICIT REAL*8 (A-H, O-Z)
COMMON /SIZES/ NINT
DIMENSION WORK(NINT)
DIMENSION USOL(NINT)

What version of FORTRAN?

The above won't work with F77 as DIMENSION is mainly a compile-time
directive pre-allocating a known amount of space. The exception is when
the item referenced is passed as an argument -- in which case all
DIMENSION does is set-up indexing logic, the space is allocated by the
calling routine... (Since "work" is passed in, it might set up indexing
on that item, but "usol" is local, and "nint" is likely initialized to 0
during the compile phase -- allocating 0 (or 1) to the size of "usol".
It was common practice, in my day, to just use
double precision work(1)
for items passed as arguments; that was sufficient for the compiler to
build indexing access logic -- but, of course, no automatic limit checks
can be performed).

subroutine foo(nint, work, usol)
double precision work(nint)
double precision usol(nint)

with a smart compiler could perform automatic limit checking -- as all
parameters are fixed during the invocation of the function.


DO 10 I=1,NINT
WORK(I)=0.0
PRINT *, "In Fortran WORK(I)=", WORK(I)
10 CONTINUE
END
DO 10 I=1,NINT
USOL(I)=0.0
PRINT *, "In Fortran USOL(I)=", USOL(I)
10 CONTINUE
END
C END OF TEST_00.f
Well, that definitely looks like F77 or older... I'd be concerned
about the reuse of "10 continue"; those should have separate labels or,
if the compiler supports the option, no labels at all:

do i = 1, nint
stuff
continue

and the following signature file (generated by f2py TEST_00.f -m TEST_00 -h
TEST_00.pyf):

python module TEST_00 ! in
interface ! in :TEST_00
subroutine foo(work) ! in :TEST_00:TEST_00.f
real*8 dimension(nint) :: work
integer optional,check(len(work)>=nint),depend(work) ::
nint=len(work)
common /sizes/ nint
end subroutine foo
end interface
end python module TEST_00
And that looks more like Fortran-90 said:
Question: How can one declare arrays passed as parameters via common block
variables?
Well... you are mixing things up, firstly... you are passing the
array as a calling parameter -- but you are trying to pass the dimension
via a common block...

Compound that with the generated code assuming the length of the
array is passed as part of the array descriptor (another F90 feature, I
believe)
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top