Skip to content

A demonstration of using Fortran code with derived types from Python using ctypes

Notifications You must be signed in to change notification settings

daniel-de-vries/fortran-ctypes-python-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fortran-Python Communication using Ctypes

This repo demonstrates how it is possible to use Fortran code from Python if the Fortran code contains derived types. This is not supported directly by f2py at the time of writing, which is why ctypes was used instead.

The repo contains a single fortran source file, src/fortran/example.f90, which contains a single examle module. The shared library can be build with cmake, using the CMakeLists.txt file at the root of the repo. However, since the source consists only of a single file, it can also easily be build manually. From the root of the repo:

gfortran -shared -fPIC src/fortran/example.f90 -o lib/example.so

The repo furthermore contains a single Python script: src/python/example.py. This script can be run after the shared object has been built to test that it works as it should.

This example was inspired on the Fortran Foreign Documentation document written by Danny Hermes.

About

A demonstration of using Fortran code with derived types from Python using ctypes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published