How to use module parameters in dimension()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use module parameters in dimension()

Luis JM Amoreira

Hello

I have a module that defines the dimension of an array as a parameter, and a subroutine in that module that computes and returns that array dimension:

 

1 module tests

2 implicit none

3 integer, parameter:: n=3 !Dimension of arrays

4

5 contains

6

7 subroutine calc(t,z)

8 real, intent(in):: t

9 real, dimension(n), intent(out):: z

10 z= 2*n + t

11 end subroutine calc

12 end module tests

 

This simple example works fine in fortran. But when I try to turn it into a python module with f2py, the process fails with exit status 1, stating that:

"In function ‘f2py_rout_tests_tests_calc’:

/tmp/tmpZZiuce/src.linux-x86_64-2.7/testsmodule.c:230:14: error: ‘n’ undeclared (first use in this function)"

 

I find it strange that this failure only occurs if array z is a dummy argument of the calc subroutine. Otherwise, as in the listing below, f2py doesn't complain.

 

1 module tests

2 implicit none

3 integer, parameter:: n=3

4

5 contains

6

7 subroutine calc(t,x)

8 real,intent(in):: t

9 real,intent(out):: x

10 real,dimension(n):: z

11 z=0.

12 x= 2*n + t

13 end subroutine calc

14 end module tests

 

So, my problem: is there any way to fix this? I mean, is it possible for f2py to compile a fortran module containing subroutines with parametrized dimension dummy arguments? Or am I missing some trivial tweak here?

 

Thanks,

Jose Amoreira


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: How to use module parameters in dimension()

Paul Anton Letnes
Hi,

I was just thinking that you could probably avoid the whole issue by using assumed-shape arrays. This actually gives you more checks and balances when using e.g. bounds checking with your fortran compiler (gfortran: -fbounds-check). Explicit size does not, afaik.

Paul

module tests
implicit none

contains

subroutine calc(t,z)
implicit none
real, intent(in):: t
real, dimension(:), intent(out):: z
integer :: n
    n = size(z)
    z = 2*n + t
end subroutine calc
end module tests


On 16. feb. 2012, at 18:49, Jose Amoreira wrote:

> Hello
> I have a module that defines the dimension of an array as a parameter, and a subroutine in that module that computes and returns that array dimension:
>  
>  1 module tests
>  2 implicit none
>  3 integer, parameter:: n=3      !Dimension of arrays
>  4
>  5 contains
>  6
>  7 subroutine calc(t,z)
>  8 real, intent(in):: t
>  9 real, dimension(n), intent(out):: z
> 10    z= 2*n + t
> 11 end subroutine calc
> 12 end module tests
>  
> This simple example works fine in fortran. But when I try to turn it into a python module with f2py, the process fails with exit status 1, stating that:
> "In function ‘f2py_rout_tests_tests_calc’:
> /tmp/tmpZZiuce/src.linux-x86_64-2.7/testsmodule.c:230:14: error: ‘n’ undeclared (first use in this function)"
>  
> I find it strange that this failure only occurs if array z is a dummy argument of the calc subroutine. Otherwise, as in the listing below, f2py doesn't complain.
>  
>  1 module tests
>  2 implicit none
>  3 integer, parameter:: n=3
>  4
>  5 contains
>  6
>  7 subroutine calc(t,x)
>  8 real,intent(in):: t
>  9 real,intent(out):: x
> 10    real,dimension(n):: z
> 11    z=0.
> 12    x= 2*n + t
> 13 end subroutine calc
> 14 end module tests
>  
> So, my problem: is there any way to fix this? I mean, is it possible for f2py to compile a fortran module containing subroutines with parametrized dimension dummy arguments? Or am I missing some trivial tweak here?
>  
> Thanks,
> Jose Amoreira
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: How to use module parameters in dimension()

Luis JM Amoreira

Thank you very much, Paul. You were a big help. I started rewriting my subroutines using arrays bounds explicitly specified in the argument lists. Your suggestion is a lot better.

(Still, it's troubling that array size parameters of subroutine dummy arguments can not be shared across a module, as other data can.)

Thanks again

Jose Amoreira

 

 

On Monday, February 20, 2012 10:02:10 AM Paul Anton Letnes wrote:

> Hi,

>

> I was just thinking that you could probably avoid the whole issue by using

> assumed-shape arrays. This actually gives you more checks and balances when

> using e.g. bounds checking with your fortran compiler (gfortran:

> -fbounds-check). Explicit size does not, afaik.

>

> Paul

>

> module tests

> implicit none

>

> contains

>

> subroutine calc(t,z)

> implicit none

> real, intent(in):: t

> real, dimension(:), intent(out):: z

> integer :: n

> n = size(z)

> z = 2*n + t

> end subroutine calc

> end module tests

>

> On 16. feb. 2012, at 18:49, Jose Amoreira wrote:

> > Hello

> >

> > I have a module that defines the dimension of an array as a parameter, and a subroutine in that module that computes and returns that array dimension:

> > 1 module tests

> > 2 implicit none

> > 3 integer, parameter:: n=3 !Dimension of arrays

> > 4

> > 5 contains

> > 6

> > 7 subroutine calc(t,z)

> > 8 real, intent(in):: t

> > 9 real, dimension(n), intent(out):: z

> >

> > 10 z= 2*n + t

> > 11 end subroutine calc

> > 12 end module tests

> >

> > This simple example works fine in fortran. But when I try to turn it

> > into a python module with f2py, the process fails with exit status 1,

> > stating that: "In function ‘f2py_rout_tests_tests_calc’:

> > /tmp/tmpZZiuce/src.linux-x86_64-2.7/testsmodule.c:230:14: error: ‘n’

> > undeclared (first use in this function)"

> >

> > I find it strange that this failure only occurs if array z is a dummy

> > argument of the calc subroutine. Otherwise, as in the listing below,

> > f2py doesn't complain.>

> > 1 module tests

> > 2 implicit none

> > 3 integer, parameter:: n=3

> > 4

> > 5 contains

> > 6

> > 7 subroutine calc(t,x)

> > 8 real,intent(in):: t

> > 9 real,intent(out):: x

> >

> > 10 real,dimension(n):: z

> > 11 z=0.

> > 12 x= 2*n + t

> > 13 end subroutine calc

> > 14 end module tests

> >

> > So, my problem: is there any way to fix this? I mean, is it possible for

> > f2py to compile a fortran module containing subroutines with

> > parametrized dimension dummy arguments? Or am I missing some trivial

> > tweak here?

> >

> > Thanks,

> > Jose Amoreira

> > _______________________________________________

> > SciPy-User mailing list

> > [hidden email]

> > http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user