Re: [f2py] intent(c) forced

Tim McGreevy <[email protected]> Sun, 25 Sep 2011 21:44:33 -0500
Newsgroups gmane.comp.python.f2py.user
Message-ID <CAFYj8p3rNtjPW7cak2A2vsu51Th4DyLsiPnt1yWu2bvCP=CiGQ@mail.gmail.com>
Hello (again),

I believe I have discovered the issue.

*F2PY's crackfortran code forces all character arrays to be intent(c).*

*Requests:*
1.  If I am wrong, please correct me.  I found this to be the case as listed
at:
http://code.google.com/p/pythonxy/source/browse/src/python/numpy/SSE0/numpy/f2py/crackfortran.py?spec=svn.xy-27.255075e4b3f4f34839fe2f54806e800a6134ae78&repo=xy-27&r=255075e4b3f4f34839fe2f54806e800a6134ae78

2.  If I am correct, please add into documentation that this is the case.
(I trust that I did not miss this important point).  I have not found an
example for the use of character arrays, especially multi-dimensional ones
as 1-D character arrays are not an issue as C and Fortran use them properly
due to the nature of the dimension.  Perhaps the simple example I sent with
my request can be used.

*Further Questions:*
A.  I believe/understand from compiling my own Fortran code and wrapping
with F2PY that crackfortran forces intent(c) for ALL input, output, and
'input,output' variables that are character arrays.  As such, existing
Fortran code will not be useable, as all character arrays must be treated as
intent(c) (which means 'C-contiguous' ??); as such, this will require
modification to Fortran code as in my 'var2' example.  In the case where no
source code is available, there is no solution.  Please correct me if I am
wrong.  Assuming I am correct, *is there a simple solution to modifying the
crackfortran code to do the following?*  Have the interface automatically
transposes character arrays between Fortran and Python, letting Python
always receive/send/use 'intent(c)' variable shapes/formats, and Fortran to
receive/send/use what I will call 'intent(f)' for intent(Fortran).   It
seems to me that this would be very valuable for Fortran users who utilize
Fortran not only for numerical arrays, but character arrays.

Your reply and feedback are much appreciated.

Regards,
Tim



On Sun, Sep 25, 2011 at 5:41 PM, Tim McGreevy <[email protected]> wrote:

> Hello,
>
> I am attempting to use F2PY to wrap Fortran code.
> I am generally successful.  However, I have the following problem. Any help
> would be appreciated.
>
> I have attempted to solve on my own, and used online search tools for
> examples to solve.  I have been unsuccessful.  I was under the impression
> that numpy handles everything in the background, and I need not worry about
> c vs fortran contiguous.  I am doing something wrong, but what?
>
> The issue is with the f2py compiler forcing intent(c) on character
> variables.  I am using numpy 1.3.0, python 2.6.5, intel fortran compiler
> 11.0, linxu (Ubuntu 10.04), 64 bit.
>
> *Fortran Code: saved file as Test1.f90*
> Subroutine foo(var1)
> CHAR*30 var1(5,4)
> !f2py intent(out) var1
>
> code to create var1(5,4)...
> do i=1,5
>    do j=1,4
>         var1(i,j)=....
>    end do !j
> end do !i
> return
> end subroutine
>
> *Compiling:*
> f2py -c --fcompiler=intelem -m Test1 Test1.f90
> --> forces intent(c) for var1
> analyzevars: character array "character*30 x(5,4)" is considered as
> "character x(5,4,30)"; "intent(c)" is forced.
>
> *This forcing of 'intnet(c)' requires that I create var1 differently (as
> var2 in example below) as follows in Fortran code:*
> CHAR*30 var2(4,5)
> do i=1,5
>    do j=1,4
>         var2(j,i)=....
>    end do !j
> end do !i
>
> *With var2 version, I can then access content of var2 in Python via the
> following:*
> >> var2[0,#,:] --> gives me array of 30 dtype S1 characters pertaining to
> the #th C continguous data, meaning
> var2[0,0,:] (Python) <==> Fortran:: var1(1,1)==var2(1,1)
> var2[0,1,:] (Python) <==> Fortran:: var1(1,2)==var2(2,1)
> ...
> var2[0,6,:] (Python) <==> Fortran:: var1(2,1)==var2(1,2)
>
> *Questions:*
> 1.  Can I avoid the forcing intent(c), and not have to modify my Fortran
> code?  Luckly, in my existing code, I have the source and can modify.  In
> the example above var1 / var2 are output variables.  However, this requires
> modifications of every character type variable and possible coding, e.g. use
> of do loop variables to modify/use variable such as var2(j,i) vs var1(i,j).
>
> 2. I have been unsuccessful at the use of transpose; perhaps I have made a
> simple error.  Recommendations?
> 3. I interpret, but have not tested, that other variables that are 'input'
> variables will cause greater confusion and additional effort on my part.  I
> am sure that this was not the intent of developers, and I am missing
> something very basic and key.  At first I thought that I would have to use
> arr.as_row_major_storage or arr.as_column_major_storage.  But they don't
> exist anymore, a clue that the issue has been addressed with a better
> solution.  I have failed to locate the solution.
>
> Obviously, I prefer a solution where I need not modify my existing Fortran
> code at all (only the wrapper functions).
>
> Thanks very much.
> Tim
> [email protected]
>

_______________________________________________
f2py-users mailing list
f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
http://cens.ioc.ee/mailman/listinfo/f2py-users