Commit b471d4cd authored by frozar's avatar frozar

[DEV] Documentation and examples of binding between python and fortran using f2py.

parent 5650b219
Documentation:
http://docs.scipy.org/doc/numpy-1.10.1/user/c-info.python-as-glue.html
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def usage( prog_name ):
print " Usage:", prog_name
print " Example:", prog_name
if __name__ == "__main__":
import sys
# Add the local installation directory of the demonstration spam module
lib_dir = sys.path[0] + '/../src_1'
sys.path = [lib_dir] + sys.path
try:
import add
except ImportError:
print " FAIL: Cannot load the demonstration module spam."
print " hint: cd ../src_1 ; make ; cd -"
exit( -1 )
print add.zadd.__doc__
print add.zadd( [1,2,3], [1,2], [3,4], 1000 )
# spam.system('echo " SUCCESS: Loading spam module done."')
# if ( len( sys.argv[ 1: ] ) != 2 ):
# usage( sys.argv[ 0 ] )
# exit( 0 )
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def usage( prog_name ):
print " Usage:", prog_name
print " Example:", prog_name
if __name__ == "__main__":
import sys
# Add the local installation directory of the demonstration spam module
lib_dir = sys.path[0] + '/../src_2'
sys.path = [lib_dir] + sys.path
try:
import add
except ImportError:
print " FAIL: Cannot load the demonstration module spam."
print " hint: cd ../src_2 ; make ; cd -"
exit( -1 )
print add.zadd.__doc__
print add.zadd( [1,2], [1,2] )
# spam.system('echo " SUCCESS: Loading spam module done."')
# if ( len( sys.argv[ 1: ] ) != 2 ):
# usage( sys.argv[ 0 ] )
# exit( 0 )
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def usage( prog_name ):
print " Usage:", prog_name
print " Example:", prog_name
if __name__ == "__main__":
import sys
# Add the local installation directory of the demonstration spam module
lib_dir = sys.path[0] + '/../src_3'
sys.path = [lib_dir] + sys.path
try:
import filter
except ImportError:
print " FAIL: Cannot load the demonstration module spam."
print " hint: cd ../src_1 ; make ; cd -"
exit( -1 )
print filter.dfilter2d.__doc__
# print add.zadd( [1,2], [1,2] )
# spam.system('echo " SUCCESS: Loading spam module done."')
# if ( len( sys.argv[ 1: ] ) != 2 ):
# usage( sys.argv[ 0 ] )
# exit( 0 )
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def usage( prog_name ):
print " Usage:", prog_name
print " Example:", prog_name
if __name__ == "__main__":
from numpy import f2py
with open("../src_3/filter.f90") as sourcefile:
sourcecode = sourcefile.read()
f2py.compile( sourcecode, modulename='filter', verbose=1 )
try:
import filter
except ImportError:
print " FAIL: Cannot load the demonstration module spam."
print " hint: cd ../src_3 ; make ; cd -"
exit( -1 )
print filter.dfilter2d.__doc__
# print add.zadd( [1,2], [1,2] )
# spam.system('echo " SUCCESS: Loading spam module done."')
# if ( len( sys.argv[ 1: ] ) != 2 ):
# usage( sys.argv[ 0 ] )
# exit( 0 )
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def usage( prog_name ):
print " Usage:", prog_name
print " Example:", prog_name
if __name__ == "__main__":
import sys
# Add the local installation directory of the demonstration spam module
lib_dir = sys.path[0] + '/../src_4'
sys.path = [lib_dir] + sys.path
try:
import add_n_sub
except ImportError:
print " FAIL: Cannot load the demonstration module spam."
print " hint: cd ../src_4 ; make ; cd -"
exit( -1 )
print add_n_sub.zadd_n_sub.__doc__
print add_n_sub.zadd_n_sub( [1,2], [1,2] )
# spam.system('echo " SUCCESS: Loading spam module done."')
# if ( len( sys.argv[ 1: ] ) != 2 ):
# usage( sys.argv[ 0 ] )
# exit( 0 )
subroutine zadd( a, b, c, n )
double complex a( * )
double complex b( * )
double complex c( * )
integer n
do j = 1, n
c( j ) = a( j ) + b( j )
end do
end subroutine zadd
! f2py -c -m add add.f90
all: build_addmodule
build_addmodule: add.f90
f2py -c -m add add.f90
clean:
@rm -f *.so *.c *.mod *~
subroutine zadd( a, b, c, n )
implicit none
!F2PY INTENT(OUT) :: C
!F2PY INTENT(HIDE) :: N
!F2PY DOUBLE COMPLEX :: A(N)
!F2PY DOUBLE COMPLEX :: B(N)
!F2PY DOUBLE COMPLEX :: C(N)
double complex a( * )
double complex b( * )
double complex c( * )
integer n, j
do j = 1, n
c( j ) = a( j ) + b( j )
end do
end subroutine zadd
! f2py -c -m add add.f90
all: build_addmodule
build_addmodule: add.f90
f2py -c -m add add.f90
clean:
@rm -f *.so *.c *.mod *~
subroutine dfilter2d( a, b, m, n )
implicit none
double precision a( m, n )
double precision b( m, n )
integer m, n
!F2PY INTENT(OUT) :: b
!F2PY INTENT(HIDE) :: M
!F2PY INTENT(HIDE) :: N
integer i, j
do i = 2, m-1
do j = 2, n-1
b( i, j ) = a( i, j ) + &
( a( i-1, j ) + a( i+1, j ) + &
a( i , j-1 ) + a( i , j+1 ) ) * 0.5D0 + &
( a( i-1, j-1 ) + a( i-1, j+1 ) + &
a( i+1, j-1 ) + a( i+1, j+1 ) ) * 0.25D0
end do
end do
end subroutine dfilter2d
! f2py -c -m filter filter.f90
all: build_filtermodule
build_filtermodule: filter.f90
f2py -c -m filter filter.f90
clean:
@rm -f *.so *.c *.mod *~
subroutine zadd_n_sub( a, b, c, d, n )
implicit none
!F2PY INTENT(OUT) :: c
!F2PY INTENT(OUT) :: d
!F2PY INTENT(HIDE) :: N
!F2PY DOUBLE COMPLEX :: a(N)
!F2PY DOUBLE COMPLEX :: b(N)
!F2PY DOUBLE COMPLEX :: c(N)
!F2PY DOUBLE COMPLEX :: d(N)
double complex a( * )
double complex b( * )
double complex c( * )
double complex d( * )
integer n, j
do j = 1, n
c( j ) = a( j ) + b( j )
d( j ) = a( j ) - b( j )
end do
end subroutine zadd_n_sub
! f2py -c -m add_n_sub add_n_sub.f90
all: build_addmodule
build_addmodule: add_n_sub.f90
f2py -c -m add_n_sub add_n_sub.f90
clean:
@rm -f *.so *.c *.mod *~
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment