From 9e16fb3785e03cfe6d7af44af45d0ace1cf3cb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franck=20P=C3=A9rignon?= <franck.perignon@imag.fr> Date: Fri, 30 Sep 2016 17:03:32 +0200 Subject: [PATCH] F2py things. Precision stuff is still broken --- docs/sphinx/devel/fortran2python.rst | 35 ++++++++++++++++++++++++++++ hysop/common_f/template.f95 | 29 +++++++++++++++++++++++ hysop/common_f/template.pyf | 16 +++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 docs/sphinx/devel/fortran2python.rst create mode 100644 hysop/common_f/template.f95 create mode 100644 hysop/common_f/template.pyf diff --git a/docs/sphinx/devel/fortran2python.rst b/docs/sphinx/devel/fortran2python.rst new file mode 100644 index 000000000..a20e7d93a --- /dev/null +++ b/docs/sphinx/devel/fortran2python.rst @@ -0,0 +1,35 @@ +.. _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. + + + diff --git a/hysop/common_f/template.f95 b/hysop/common_f/template.f95 new file mode 100644 index 000000000..1a178b268 --- /dev/null +++ b/hysop/common_f/template.f95 @@ -0,0 +1,29 @@ + !! 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 diff --git a/hysop/common_f/template.pyf b/hysop/common_f/template.pyf new file mode 100644 index 000000000..6245f4750 --- /dev/null +++ b/hysop/common_f/template.pyf @@ -0,0 +1,16 @@ +! -*- 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/ -- GitLab