Sometimes, we need to use some fortran code in our project. A good practice is that we can compile the .f90
codes into dynamic link libraries (.so
in Linux or .dll
in windows). Then, we can use the ctypes
in python to load the libs and get the handlers.
# as dynamic link lib
mpifort -fPIC -shared -o libtest.so test.f90
python test.py
-
we use
subroutine
in fortran to provide the interface. However, there is no return value. There is a smart way that we can make the following argreement.- the
subroutine
we need to use in fortran only accept array as arguments. - we use
numpy.array
to pass arguments to the fortran handler.
- the
-
Pay attention to the difference in the memory modle between C and fortran. Defenitely, we use the
C
interface in python.
- MPI Use.
- Optimize fortran
subroutine
in python byscipy.optimize
.