Try   HackMD
  •  
    Python-Fortran interface minibook
    ·
    Last edited by Kjell Jorner on May 29, 2021
    Linked with GitHub
    Contributed by
  • 1
  • Edit

Outline

tags: book

Introduction

Automatic interface generation

Writing a Fortran-C interface

Importing a shared library

Building and distributing

Last changed by 

 
Python-Fortran interface minibook
Space to work on Python-Fortran interface minibook for Fortran-lang.org
0
1
459

Read more

Collected material

Python API generators f2py Python wrapper generator that is part of NumPy Main limitations: Doesn't support derived types f90wrap Fortran to Python interface generator that extends f2yp with derived type support.

Jun 25, 2021
Importing a shared library

ctypes The ctypes library is part of the Python standard libary and provides convenient access to C compatible data types and loading of shared libraries. C datatypes like c_int, c_double, c_long etc. are supported and libraries are loaded with CDLL. A simple example on the use of ctypes was given in the Introduction. Working with NumPy arrays The most convenient way to work with arrays is through numpy.ctypeslib. In particular, it includes the functions as_array, which converts a C array to a NumPy ndarray, and as_ctypes, which converts an ndarray to a C array. Here is an example passing a NumPy array to a Fortran subroutine that sums the column values: from ctypes import CDLL, byref, c_int import numpy as np

May 29, 2021
Building and distributing

Some introduction The end goal is to be able to make our code installable with package managers such as conda or pip without the end user having to worry about compiling code, setting up paths for shared libraries etc. f2py Packaging Fortran code with f2py is embarrasingly easy thanks to numpy.distutils. We can tell setuptools to build Fortran source files by using the numpy.distutils drop-in replacements for the regular Extension and setup. Let's start with our function that does addition. ! file: add.f90

May 29, 2021
Introduction

Authors: Kjell Jorner, ... In recent years, Python has taken over as the most commonly used language in applied science. Although Python itself as a scripting language is quite slow, it often serves as a glue between computationally demanding procedures written in compiled languages such as Fortran or C. Prime examples of this approch are the NumPy and SciPy packages that power applications such as machine learning via scikit-learn and quantum chemistry via PySCF. There are a number of reasons why you would want to interface Python and Fortran, such as: Making your Fortran application available within the Python eco-system Speeding up slow parts of a Python code

May 29, 2021
Read more from Python-Fortran interface minibook

Published on HackMD
1

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up