Skip to content
Snippets Groups Projects
Commit 9e16fb37 authored by Franck Pérignon's avatar Franck Pérignon
Browse files

F2py things. Precision stuff is still broken

parent 38996304
No related branches found
No related tags found
No related merge requests found
.. _fortran2python:
Fortran to python interface
===========================
This page propose a tutorial to call some fortran subroutines in python within hysop.
Sources:
* some fortran files which contain some subroutines inside some modules
* a pyf file
TO BE DONE !!!
Kind values
-----------
According to f2py doc: "Currently, F2PY can handle only <type spec>(kind=<kindselector>) declarations where <kindselector> is a numeric integer (e.g. 1, 2, 4,...) but not a function call KIND(..) or any other expression. F2PY needs to know what would be the corresponding C type and a general solution for that would be too complicated to implement."
To overcome this, a .f2py_f2cmap is generated during the build, from hysop/f2py_f2cmap.in.
It contains all the required mappings defined as a dictionnary, like::
{'integer':{'c_int':'int'}, 'real':{'real64':'double', 'wp':'double'}}
In that example, a fortran :code:'real(kind=wp)' will be mapped into a double-precision real in python.
!! Template file - Provide an example
!! to write f2py interface between fortran and python
module template_f2py
use precision
implicit none
!> a global var
integer, parameter :: var1 = 12
contains
!> do something ...
subroutine check_f2py(input, output)
use precision
!> input array
real(kind=wp), dimension(:,:), intent(in) :: input
!> output array
real(kind=wp), dimension(:,:), intent(inout) :: output
print *, 'template f2py for tab'
output(:,:) = 2 * input(:,:)
print *, 'aha hah', output(1,1)
end subroutine check_f2py
end module template_f2py
! -*- f90 -*-
! Note: the context of this file is case sensitive.
module template_f2py ! in :template_f2py:template.f95
use precision
use ISO_FORTRAN_ENV
integer, parameter,optional :: var1=12
subroutine check_f2py(input,output) ! in :template_f2py:template.f95:template_f2py
use precision
use ISO_FORTRAN_ENV
real(kind=wp) dimension(:,:),intent(in) :: input
real(kind=wp), dimension(size(input,1), size(input,2)), intent(in,out), depend(input,input) :: output
end subroutine check_f2py
end module template_f2py
! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment