Using Fortran in Python: fortran-magic

2021-12-27
#Fortran #Python

1. Install

$ pip install -U fortran-magic
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb79c279110>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/fortran-magic/
Collecting fortran-magic
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fb79c2d5ad0>, 'Connection to files.pythonhosted.org timed out. (connect timeout=15)')': /packages/6e/d8/e89e0c14bb91de926c47b91796eb7a035d9f7b7e4bbdb697fffc4df8115a/fortran-magic-0.7.tar.gz
  Downloading https://files.pythonhosted.org/packages/6e/d8/e89e0c14bb91de926c47b91796eb7a035d9f7b7e4bbdb697fffc4df8115a/fortran-magic-0.7.tar.gz
Requirement already satisfied, skipping upgrade: ipython in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from fortran-magic) (5.8.0)
Requirement already satisfied, skipping upgrade: numpy in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from fortran-magic) (1.16.5)
Requirement already satisfied, skipping upgrade: setuptools>=18.5 in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (42.0.2.post20191203)
Requirement already satisfied, skipping upgrade: prompt-toolkit<2.0.0,>=1.0.4 in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (1.0.18)
Requirement already satisfied, skipping upgrade: decorator in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (4.4.1)
Requirement already satisfied, skipping upgrade: backports.shutil-get-terminal-size; python_version == "2.7" in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (1.0.0)
Requirement already satisfied, skipping upgrade: appnope; sys_platform == "darwin" in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (0.1.0)
Requirement already satisfied, skipping upgrade: pexpect; sys_platform != "win32" in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (4.7.0)
Requirement already satisfied, skipping upgrade: pathlib2; python_version == "2.7" or python_version == "3.3" in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (2.3.5)
Requirement already satisfied, skipping upgrade: pygments in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (2.5.2)
Requirement already satisfied, skipping upgrade: traitlets>=4.2 in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (4.3.3)
Requirement already satisfied, skipping upgrade: simplegeneric>0.8 in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (0.8.1)
Requirement already satisfied, skipping upgrade: pickleshare in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from ipython->fortran-magic) (0.7.5)
Requirement already satisfied, skipping upgrade: wcwidth in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython->fortran-magic) (0.1.7)
Requirement already satisfied, skipping upgrade: six>=1.9.0 in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython->fortran-magic) (1.13.0)
Requirement already satisfied, skipping upgrade: ptyprocess>=0.5 in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from pexpect; sys_platform != "win32"->ipython->fortran-magic) (0.6.0)
Requirement already satisfied, skipping upgrade: scandir; python_version < "3.5" in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from pathlib2; python_version == "2.7" or python_version == "3.3"->ipython->fortran-magic) (1.10.0)
Requirement already satisfied, skipping upgrade: ipython-genutils in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from traitlets>=4.2->ipython->fortran-magic) (0.2.0)
Requirement already satisfied, skipping upgrade: enum34; python_version == "2.7" in ./anaconda3/envs/python2.7/lib/python2.7/site-packages (from traitlets>=4.2->ipython->fortran-magic) (1.1.6)
Building wheels for collected packages: fortran-magic
  Building wheel for fortran-magic (setup.py) ... done
  Created wheel for fortran-magic: filename=fortran_magic-0.7-cp27-none-any.whl size=7047 sha256=b43b7e253d506c450cae290c3c0f2f0ebcf047661313574aa2e01ead2401092d
  Stored in directory: /Users/name/Library/Caches/pip/wheels/69/d1/e6/83c19e88d0641ed69f32b506aa07dc1283f5339ad6d393f4f7
Successfully built fortran-magic
Installing collected packages: fortran-magic
Successfully installed fortran-magic-0.7

2. Usage

In jupyter notebook:

In[1]: %load_ext fortranmagic

In[2]: %%fortran
        subroutine f1(x, y, z)
        real, intent(in) :: x,y
        real, intent(out) :: z
        z = sin(x+y)
        end subroutine f1

In[3]: f1(1.0, 2.1415)
Out[3]: 9.26574066397734e-05
In[4]: %%fortran
        subroutine my_function(x, y, z)
            real, intent(in) :: x(:), y(:)
            real, intent(out) :: z(size(x))
            ! using vector operations
            z(:) = sin(x(:) + y(:))
        end subroutine

In[5]: 
import numpy
x = numpy.random.normal(size=10)
y = numpy.random.normal(size=10)
z = my_function(x, y)
z

Out[5]: 
array([-0.40036342, -0.5483717 , -0.71112984, -0.31596676, -0.9789854 ,
        0.16493222, -0.845283  , -0.94829416,  0.8096917 ,  0.888128  ],
      dtype=float32)

Note that fortran-magic uses f2py under background.

3. Reference