Fortran magic is an IPython extension that help to use fortran code in an interactive session.
It adds a %%fortran
cell magic that compile and import the Fortran code in the cell, using F2py.
The contents of the cell are written to a .f90
file in the
directory IPYTHONDIR/fortran
using a filename with the hash of the
code. This file is then compiled. The resulting module
is imported and all of its symbols are injected into the user's
namespace.
_
nqn_
This software was originally sponsored by Phasety
Feedback, report of issues and pull requests are welcome!
You can install or upgrade via pip
pip install -U fortran-magic
or directly from the repository using %install_ext
magic command.
%install_ext https://raw.github.com/mgaitan/fortran_magic/master/fortranmagic.py
Then you are ready to load the magic
%load_ext fortranmagic
To load it each time IPython starts, list it in your configuration file:
c.InteractiveShellApp.extensions = [
'fortranmagic'
]
Just mark the cell with %%fortran
in the first line. The code will be highlighted accordingly and compiled when the cell is run
%%fortran
subroutine f1(x, y, z)
real, intent(in) :: x,y
real, intent(out) :: z
z = sin(x+y)
end subroutine f1
f1(1.0, 2.1415)
print f1.__doc__
By default the magic only returns output when the compilation process fails. But you can increase the verbosity with the flag -v
%%fortran -v
module hi
integer :: five = 5
end module
%%fortran -vv
module hi
integer :: five = 5
end module
%%fortran -vvv
module hi
integer :: five = 5
end module
Almost all f2py's command line options are exposed to the %%fortran
cell magic. See the docstring for detail. For example:
%%fortran --fcompiler gnu95 --compiler unix --f90flags "-d" --noarch
C
SUBROUTINE ZADD(A,B,C,N)
C
DOUBLE COMPLEX A(*)
DOUBLE COMPLEX B(*)
DOUBLE COMPLEX C(*)
INTEGER N
DO 20 J = 1, N
C(J) = A(J)+B(J)
20 CONTINUE
END
print(zadd.__doc__)
Use --link
option. This is --link-<resource>
in f2py command line
%%fortran --link lapack -vv
subroutine solve(A, b, x, n)
! solve the matrix equation A*x=b using LAPACK
implicit none
real*8, dimension(n,n), intent(in) :: A
real*8, dimension(n), intent(in) :: b
real*8, dimension(n), intent(out) :: x
integer :: i, j, pivot(n), ok
integer, intent(in) :: n
x = b
! find the solution using the LAPACK routine SGESV
call DGESV(n, 1, A, n, pivot, x, n, ok)
end subroutine
import numpy as np
A = np.array([[1, 2.5], [-3, 4]])
b = np.array([1, 2.5])
solve?
Which is, by the way, the same than
np.linalg.solve(A, b)
F2py could have many other arguments. You could append extra arguments with --extra
. For example:
%%fortran --extra '-L/path/to/open/ -lopenblas'
%%fortran --extra '-D<define> -U<name>'
%%fortran --extra '-DPREPEND_FORTRAN -DUPPERCASE_FORTRAN'
The option --extra
could be given multiple times.
By default, %%fortran
call to f2py
without parameters (except, of course, the -m
and -c
needed to compile a new module). You can change this behaviour with %fortran_config
. This line magic can be used in three different ways:
%fortran_config
Show the current custom configuration
%fortran_config --defaults
Delete the current configuration and back to defaults
%fortran_config <other options>
Save (persitently) <other options> to use with %%fortran. The same arguments allowed for `%%fortran` are available
For example, to set the highest verbose level (-vvv
) and gnu95
as default --fcompiler
:
%fortran_config -vvv --fcompiler gnu95
Now the use of %%fortran
will include -vvv --fcompiler gnu95
implicitly
%%fortran
module hi
integer :: five = 5
end module
We can see whatever the default config has
%fortran_config
You can override that global configuration for one specific cell. For example, %%fortran -vv
will change the the verbose level but still use --fcompiler gnu95
%%fortran -vv
module hi
integer :: five = 5
end module
To clear the custom defaults and back to the defaults (no arguments) use:
%fortran_config --defaults
F2py has some flag that output help. See the docstring of %f2py_help
%f2py_help --link blas
%f2py_help --fcompiler
%f2py_help --compiler