######################################################################################### # # Scientific Python Course # # L05 - f2py : Fortran/C Interface # ######################################################################################### # # ARCHER, 2015 # # http://www.archer.ac.uk # support@archer.ac.uk # # # Neelofer Banglawala : nbanglaw@epcc.ed.ac.uk # Kevin Stratford : kevin@epcc.ed.ac.uk # # EPCC, University of Ediburgh, EPSRC, NERC, CRAY # # # ******************************************************************** # * Reusing this material : * # * * # * This work is licensed under a Creative Commons Attribution- * # * Non-Commercial-ShareAlike 4.0 Internatiional License. * # * http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_US * # * * # * This means you are free to copy and redistributhe the material * # * and adapt and build on the material under the following terms: * # * You must give appropriate credit, provide a link to the license * # * and indicate if changes were made. If you adapt or build on the * # * material you must distribute your work under the same license as * # * the original. * # * * # ******************************************************************** # # ######################################################################################### # # S5 [f2py] General recipe # ######################################################################################### # # f2py -m -h .pyf # # f2py -c .pyf # # from import # # ######################################################################################### # # S6 [f2py] Fortran : farray_sqrt.f90 # ######################################################################################### # # ! file farray_sqrt.f90 # ! Fortran Example : calculate the sqrt of each array element # subroutine array_sqrt(n, a_in, a_out) # implicit none # integer, intent(in) :: n # real*8, dimension(n), intent(in) :: a_in # real*8, dimension(n), intent(out) :: a_out # integer :: i # do i = 1, n # a_out(i) = sqrt(a_in(i)) # end do # end subroutine array_sqrt # # ######################################################################################### # # S7 [f2py] Create a signature file # ######################################################################################### # # f2py farray_sqrt.f90 -m farray -h farray_sqrt.pyf # # # # ######################################################################################### # # S8 [f2py] Check signature file # ######################################################################################### # # # ! -*- f90 -*- # ! Note: the context of this file is case sensitive. # python module farray ! in # interface ! in :farray # subroutine array_sqrt(n,a_in,a_out) ! in :farray:farray_sqrt.f90 # integer, optional,intent(in),check(len(a_in)>=n),depend(a_in) :: n=len(a_in) # real*8 dimension(n),intent(in) :: a_in # real*8 dimension(n),intent(out), depend(n) :: a_out # end subroutine array_sqrt # end interface # end python module farray # ! This file was auto-generated with f2py (version:2). # ! See http://cens.ioc.ee/projects/f2py2e/ # # # ######################################################################################### # # S9 [f2py] Produce extension module # ######################################################################################### # # f2py -c farray_sqrt.pyf farray_sqrt.f90 # # ######################################################################################### # # S10 [f2py] Fortran : farray_sqrt.f90 # ######################################################################################### # # # # import the extension module # import numpy as np # from farray import array_sqrt # # # # view docsting of function (automatically produced) # array_sqrt? # # # # let's use the function # ain = np.array([1.0,4.0,9.0,16.0]); # aout = array_sqrt(ain) # print aout # # ######################################################################################### # # S11 [f2py] fibonacci.f90 I # ######################################################################################### # # # # ! file : fibonacci.f90 # ! Fortran Example : # ! calculate first n Fibonacci numbers (not efficient!) # ! # subroutine fibonacci(n, a_out) # implicit none # integer, intent(in) :: n # real*8, dimension(n) :: a_out # integer :: i # do i = 1, n # if (i.eq.1) then # a_out(i) = 0.0 # elseif (i.eq.2) then # a_out(i) = 1.0 # else # a_out(i) = a_out(i-1) + a_out(i-2) # endif # enddo # end subroutine fibonacci # ######################################################################################### # # S12 [f2py] fibonacci.f90 II # ######################################################################################### # # # create signature file # !f2py fibonacci.f90 -m ffib -h fibonacci.pyf; # # %%capture # produce compiled library # !f2py -c fibonacci.pyf fibonacci.f90; # # # import fibonacci from ffib # from ffib import fibonacci # fibonacci? # # # type that Fortran expects matter (effect 'd' and 'i') # f = np.zeros(10); # fibonacci(f.size, f) # need to specify n # print f # # ######################################################################################### # # S14 [f2py] Interface with C : carray_sqrt.f90 # ######################################################################################### # # // file carray_sqrt.f90 # // C Example : calculate the sqrt of each array element # // # #include "math.h" # void array_sqrt(int n, double * a_in, double * a_out) # { # for(int i = 0; i