[Numpy-discussion] Getting Callbacks with arrays to work
Jon Moore
jonboym2@yahoo.co...
Thu Jan 14 02:55:46 CST 2010
Hi,
Thanks all works now! The implicit none only didn't work when defining
dv as a function now its a subroutine it seems to work.
Regards
Jon
On 12/01/2010 13:44, Pearu Peterson wrote:
> Hi,
>
> The problem is that f2py does not support callbacks that
> return arrays. There is easy workaround to that: provide
> returnable arrays as arguments to callback functions.
> Using your example:
>
> SUBROUTINE CallbackTest(dv,v0,Vout,N)
> IMPLICIT NONE
>
> !F2PY intent( hide ):: N
> INTEGER:: N, ic
> EXTERNAL:: dv
>
> DOUBLE PRECISION, DIMENSION( N ), INTENT(IN):: v0
> DOUBLE PRECISION, DIMENSION( N ), INTENT(OUT):: Vout
>
> DOUBLE PRECISION, DIMENSION( N ):: Vnow
> DOUBLE PRECISION, DIMENSION( N ):: temp
>
> Vnow = v0
> !f2py intent (out) temp
> call dv(temp, Vnow, N)
>
> DO ic = 1, N
> Vout( ic ) = temp(ic)
> END DO
>
> END SUBROUTINE CallbackTest
>
> $ f2py -c test.f90 -m t --fcompiler=gnu95
>
>>>> from numpy import *
>>>> from t import *
>>>> arr = array([2.0, 4.0, 6.0, 8.0])
>>>> def dV(v):
> print 'in Python dV: V is: ',v
> ret = v.copy()
> ret[1] = 100.0
> return ret
> ...
>>>> output = callbacktest(dV, arr)
> in Python dV: V is: [ 2. 4. 6. 8.]
>>>> output
> array([ 2., 100., 6., 8.])
>
> What problems do you have with implicit none? It works
> fine here. Check the format of your source code,
> if it is free then use `.f90` extension, not `.f`.
>
> HTH,
> Pearu
>
> Jon Moore wrote:
>> Hi,
>>
>> I'm trying to build a differential equation integrator and later a
>> stochastic differential equation integrator.
>>
>> I'm having trouble getting f2py to work where the callback itself
>> receives an array from the Fortran routine does some work on it and then
>> passes an array back.
>>
>> For the stoachastic integrator I'll need 2 callbacks both dealing with
>> arrays.
>>
>> The idea is the code that never changes (ie the integrator) will be in
>> Fortran and the code that changes (ie the callbacks defining
>> differential equations) will be different for each problem.
>>
>> To test the idea I've written basic code which should pass an array back
>> and forth between Python and Fortran if it works right.
>>
>> Here is some code which doesn't work properly:-
>>
>> SUBROUTINE CallbackTest(dv,v0,Vout,N)
>> !IMPLICIT NONE
>>
>> cF2PY intent( hide ):: N
>> INTEGER:: N, ic
>>
>> EXTERNAL:: dv
>>
>> DOUBLE PRECISION, DIMENSION( N ), INTENT(IN):: v0
>> DOUBLE PRECISION, DIMENSION( N ), INTENT(OUT):: Vout
>>
>> DOUBLE PRECISION, DIMENSION( N ):: Vnow
>> DOUBLE PRECISION, DIMENSION( N ):: temp
>>
>> Vnow = v0
>>
>>
>> temp = dv(Vnow, N)
>>
>> DO ic = 1, N
>> Vout( ic ) = temp(ic)
>> END DO
>>
>> END SUBROUTINE CallbackTest
>>
>>
>>
>> When I test it with this python code I find the code just replicates the
>> first term of the array!
>>
>>
>>
>>
>> from numpy import *
>> import callback as c
>>
>> def dV(v):
>> print 'in Python dV: V is: ',v
>> return v.copy()
>>
>> arr = array([2.0, 4.0, 6.0, 8.0])
>>
>> print 'Arr is: ', arr
>>
>> output = c.CallbackTest(dV, arr)
>>
>> print 'Out is: ', output
>>
>>
>>
>>
>> Arr is: [ 2. 4. 6. 8.]
>>
>> in Python dV: V is: [ 2. 4. 6. 8.]
>>
>> Out is: [ 2. 2. 2. 2.]
>>
>>
>>
>> Any ideas how I should do this, and also how do I get the code to work
>> with implicit none not commented out?
>>
>> Thanks
>>
>> Jon
>>
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list