diff --git a/notebooks/2015-07-23_Calling_Fortran_from_Julia_simple_examples.ipynb b/notebooks/2015-07-23_Calling_Fortran_from_Julia_simple_examples.ipynb new file mode 100644 index 0000000..e9dcdc1 --- /dev/null +++ b/notebooks/2015-07-23_Calling_Fortran_from_Julia_simple_examples.ipynb @@ -0,0 +1,293 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Some simple examples for calling Fortran from Julia" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Author(s): Tero Frondelius" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Abstract:** Some simple examples for calling Fortran from Julia" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "f = open(\"hello.f90\",\"w\")\n", + "write(f,\"subroutine hello(x,z,y)\\n\")\n", + "write(f,\" real*8 x,y\\n\")\n", + "write(f,\"\"\" print *, \"Hello World! \", x,y, \" testing Jupyter\"\\n\"\"\")\n", + "write(f,\"end subroutine hello\\n\")\n", + "close(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "f = open(\"array.f90\",\"w\")\n", + "write(f, \"\"\"\n", + " subroutine array(arr,siz)\n", + " integer*8 siz\n", + " real*8, dimension(siz) :: arr\n", + " do i=1,siz\n", + " print *, \"Arr \", arr(i)\n", + " end do\n", + " end subroutine array\n", + " \"\"\")\n", + "close(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "f = open(\"matrix.f90\",\"w\")\n", + "write(f, \"\"\"\n", + "subroutine matrix(arr,siz)\n", + " integer*8 siz\n", + "real*8, dimension(siz,siz) :: arr\n", + " do i=1,siz\n", + " do j=1,siz\n", + " print *, \"Arr(\",i,\",\",j,\") = \", arr(i,j)\n", + " end do\n", + " end do\n", + "end subroutine matrix\n", + " \"\"\")\n", + "close(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "run(`gfortran -shared -fPIC -o libhello.so hello.f90 array.f90 matrix.f90`)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "tt = 23.; uu = 12.;" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Hello World! 23.000000000000000 12.000000000000000 testing Jupyter\n" + ] + }, + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "t = ccall( (:hello_, \"./libhello\"), Int64, (Ptr{Float64},Ptr{Void},Ptr{Float64}),&tt,{},&uu)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "test = [6. 5. 4. 3. 2. 1.]\n", + "ltest = length(test)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Arr 6.0000000000000000 \n", + " Arr 5.0000000000000000 \n", + " Arr 4.0000000000000000 \n", + " Arr 3.0000000000000000 \n", + " Arr 2.0000000000000000 \n", + " Arr 1.0000000000000000 \n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "out = ccall( (:array_, \"./libhello\"), Int64, (Ptr{Float64},Ptr{Int64}),test,<est)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2x2 Array{Float64,2}:\n", + " 1.0 2.0\n", + " 3.0 4.0" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mat_test = [1. 2.; 3. 4.]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l_mat = size(mat_test)[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Arr( 1 , 1 ) = 1.0000000000000000 \n", + " Arr( 1 , 2 ) = 2.0000000000000000 \n", + " Arr( 2 , 1 ) = 3.0000000000000000 \n", + " Arr( 2 , 2 ) = 4.0000000000000000 \n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "out = ccall( (:matrix_, \"./libhello\"), Int64, (Ptr{Float64},Ptr{Int64}),mat_test,&l_mat)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 0.3.8", + "language": "julia", + "name": "julia-0.3" + }, + "language_info": { + "name": "julia", + "version": "0.3.10" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}