Skip to content

Instantly share code, notes, and snippets.

@cpelley
Created November 13, 2015 11:53
Show Gist options
  • Save cpelley/b3cf071c86fddd2c1000 to your computer and use it in GitHub Desktop.
Save cpelley/b3cf071c86fddd2c1000 to your computer and use it in GitHub Desktop.
f2py example usage
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import subprocess"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# We could generate the python interface in a single call as the example below is well defined F90 code!\n",
"#f2py -c fort.f90 -m ftest"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"subprocess.call('rm *.pyf *.f90 *.o', shell=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fort_cont = \"\"\"\n",
"SUBROUTINE ftest(c, d, a, b)\n",
"\n",
" INTEGER, INTENT(IN) :: a, b\n",
" INTEGER, DIMENSION(a), INTENT(INOUT) :: c\n",
" INTEGER, DIMENSION(b), INTENT(INOUT) :: d\n",
" \n",
" INTEGER :: i\n",
" \n",
" DO i=1, 2\n",
" c(i) = c(i) + d(i)\n",
" ENDDO\n",
" \n",
"END SUBROUTINE\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"with open('fort.f90', 'w') as fh:\n",
" fh.write(fort_cont)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create signature file\n",
"subprocess.call('f2py fort.f90 -m ftest -h fort.pyf', shell=True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"! -*- f90 -*-\n",
"! Note: the context of this file is case sensitive.\n",
"\n",
"python module ftest ! in \n",
" interface ! in :ftest\n",
" subroutine ftest(c,d,a,b) ! in :ftest:fort.f90\n",
" integer dimension(a),intent(inout) :: c\n",
" integer dimension(b),intent(inout) :: d\n",
" integer, optional,intent(in),check(len(c)>=a),depend(c) :: a=len(c)\n",
" integer, optional,intent(in),check(len(d)>=b),depend(d) :: b=len(d)\n",
" end subroutine ftest\n",
" end interface \n",
"end python module ftest\n",
"\n",
"! This file was auto-generated with f2py (version:2).\n",
"! See http://cens.ioc.ee/projects/f2py2e/\n",
"\n"
]
}
],
"source": [
"with open('fort.pyf', 'r') as fh:\n",
" signature = fh.read()\n",
"print signature"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"subprocess.call('f2py -c fort.pyf fort.f90', shell=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import ftest"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"c = np.array([1, 2], dtype='int32')\n",
"d = np.array([1, 2, 3], dtype='int32')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print ftest.ftest(c, d)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on module ftest:\n",
"\n",
"NAME\n",
" ftest\n",
"\n",
"FILE\n",
" /home/carwyn/svn/ants/doc/reports/notebooks/vegetation/ftest.so\n",
"\n",
"DESCRIPTION\n",
" This module 'ftest' is auto-generated with f2py (version:2).\n",
" Functions:\n",
" ftest(c,d,a=len(c),b=len(d))\n",
" .\n",
"\n",
"DATA\n",
" __version__ = '$Revision: $'\n",
" ftest = <fortran object>\n",
"\n",
"VERSION\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"help(ftest)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 4]\n",
"[1 2 3]\n"
]
}
],
"source": [
"print c\n",
"print d"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment