SciPy

Using F2PY bindings in Python

All wrappers for Fortran/C routines, common blocks, or for Fortran 90 module data generated by F2PY are exposed to Python as fortran type objects. Routine wrappers are callable fortran type objects while wrappers to Fortran data have attributes referring to data objects.

All fortran type object have attribute _cpointer that contains CObject referring to the C pointer of the corresponding Fortran/C function or variable in C level. Such CObjects can be used as an callback argument of F2PY generated functions to bypass Python C/API layer of calling Python functions from Fortran or C when the computational part of such functions is implemented in C or Fortran and wrapped with F2PY (or any other tool capable of providing CObject of a function).

Scalar arguments

In general, a scalar argument of a F2PY generated wrapper function can be ordinary Python scalar (integer, float, complex number) as well as an arbitrary sequence object (list, tuple, array, string) of scalars. In the latter case, the first element of the sequence object is passed to Fortran routine as a scalar argument.

Note that when type-casting is required and there is possible loss of information (e.g. when type-casting float to integer or complex to float), F2PY does not raise any exception. In complex to real type-casting only the real part of a complex number is used.

intent(inout) scalar arguments are assumed to be array objects in order to in situ changes to be effective. It is recommended to use arrays with proper type but also other types work.

String arguments

F2PY generated wrapper functions accept (almost) any Python object as a string argument, str is applied for non-string objects. Exceptions are Numpy arrays that must have type code 'c' or '1' when used as string arguments.

A string can have arbitrary length when using it as a string argument to F2PY generated wrapper function. If the length is greater than expected, the string is truncated. If the length is smaller that expected, additional memory is allocated and filled with \0.

Because Python strings are immutable, an intent(inout) argument expects an array version of a string in order to in situ changes to be effective.

Array arguments

In general, array arguments of F2PY generated wrapper functions accept arbitrary sequences that can be transformed to Numpy array objects. An exception is intent(inout) array arguments that always must be proper-contiguous and have proper type, otherwise an exception is raised. Another exception is intent(inplace) array arguments that attributes will be changed in-situ if the argument has different type than expected (see intent(inplace) attribute for more information).

In general, if a Numpy array is proper-contiguous and has a proper type then it is directly passed to wrapped Fortran/C function. Otherwise, an element-wise copy of an input array is made and the copy, being proper-contiguous and with proper type, is used as an array argument.

There are two types of proper-contiguous Numpy arrays:

  • Fortran-contiguous arrays when data is stored column-wise, i.e. indexing of data as stored in memory starts from the lowest dimension;
  • C-contiguous or simply contiguous arrays when data is stored row-wise, i.e. indexing of data as stored in memory starts from the highest dimension.

For one-dimensional arrays these notions coincide.

For example, an 2x2 array A is Fortran-contiguous if its elements are stored in memory in the following order:

A[0,0] A[1,0] A[0,1] A[1,1]

and C-contiguous if the order is as follows:

A[0,0] A[0,1] A[1,0] A[1,1]

To test whether an array is C-contiguous, use .iscontiguous() method of Numpy arrays. To test for Fortran contiguity, all F2PY generated extension modules provide a function has_column_major_storage(<array>). This function is equivalent to <array>.flags.f_contiguous but more efficient.

Usually there is no need to worry about how the arrays are stored in memory and whether the wrapped functions, being either Fortran or C functions, assume one or another storage order. F2PY automatically ensures that wrapped functions get arguments with proper storage order; the corresponding algorithm is designed to make copies of arrays only when absolutely necessary. However, when dealing with very large multidimensional input arrays with sizes close to the size of the physical memory in your computer, then a care must be taken to use always proper-contiguous and proper type arguments.

To transform input arrays to column major storage order before passing them to Fortran routines, use a function as_column_major_storage(<array>) that is provided by all F2PY generated extension modules.

Call-back arguments

F2PY supports calling Python functions from Fortran or C codes.

In the above example F2PY was able to guess accurately the signature of a call-back function. However, sometimes F2PY cannot establish the signature as one would wish and then the signature of a call-back function must be modified in the signature file manually. Namely, signature files may contain special modules (the names of such modules contain a substring __user__) that collect various signatures of call-back functions. Callback arguments in routine signatures have attribute external (see also intent(callback) attribute). To relate a callback argument and its signature in __user__ module block, use use statement as illustrated below. The same signature of a callback argument can be referred in different routine signatures.

Sometimes a Fortran package may require that users provide routines that the package will use. F2PY can construct an interface to such routines so that Python functions could be called from Fortran.

The function is included as an argument to the python function call to the Fortran subroutine even though it was not in the Fortran subroutine argument list. The “external” refers to the C function generated by f2py, not the python function itself. The python function must be supplied to the C function.

The callback function may also be explicitly set in the module. Then it is not necessary to pass the function in the argument list to the Fortran function. This may be desired if the Fortran function calling the python callback function is itself called by another Fortran function.

Resolving arguments to call-back functions

F2PY generated interface is very flexible with respect to call-back arguments. For each call-back argument an additional optional argument <name>_extra_args is introduced by F2PY. This argument can be used to pass extra arguments to user provided call-back arguments.

If a F2PY generated wrapper function expects the following call-back argument:

def fun(a_1,...,a_n):
   ...
   return x_1,...,x_k

but the following Python function

def gun(b_1,...,b_m):
   ...
   return y_1,...,y_l

is provided by an user, and in addition,

fun_extra_args = (e_1,...,e_p)

is used, then the following rules are applied when a Fortran or C function calls the call-back argument gun:

  • If p == 0 then gun(a_1, ..., a_q) is called, here q = min(m, n).
  • If n + p <= m then gun(a_1, ..., a_n, e_1, ..., e_p) is called.
  • If p <= m < n + p then gun(a_1, ..., a_q, e_1, ..., e_p) is called, here q=m-p.
  • If p > m then gun(e_1, ..., e_m) is called.
  • If n + p is less than the number of required arguments to gun then an exception is raised.

The function gun may return any number of objects as a tuple. Then following rules are applied:

  • If k < l, then y_{k + 1}, ..., y_l are ignored.
  • If k > l, then only x_1, ..., x_l are set.

Common blocks

F2PY generates wrappers to common blocks defined in a routine signature block. Common blocks are visible by all Fortran codes linked with the current extension module, but not to other extension modules (this restriction is due to how Python imports shared libraries). In Python, the F2PY wrappers to common blocks are fortran type objects that have (dynamic) attributes related to data members of common blocks. When accessed, these attributes return as Numpy array objects (multidimensional arrays are Fortran-contiguous) that directly link to data members in common blocks. Data members can be changed by direct assignment or by in-place changes to the corresponding array objects.

Fortran 90 module data

The F2PY interface to Fortran 90 module data is similar to Fortran 77 common blocks.

Allocatable arrays

F2PY has basic support for Fortran 90 module allocatable arrays.