Git Product home page Git Product logo

call_py_fort's Introduction

call_py_fort

status

Call python from Fortran (not the other way around). Inspired by this blog post.

Installation

This library has the following dependencies

  1. pfUnit (v3.2.9) for the unit tests
  2. python (3+) with numpy and cffi
  3. cmake (>=3.4+)

To install pfunit on a linux system, you can run a commmand like the following:

export F90=gfortran
export F90_VENDOR=GNU
export PFUNIT=/usr/local
curl -L https://github.com/Goddard-Fortran-Ecosystem/pFUnit/archive/3.2.9.tar.gz | tar xz
cd pFUnit-3.2.9 && \
    cmake . && \
    make &&\
    sudo make install INSTALL_DIR=${PFUNIT}

Installing python dependencies is out of scope of this documentation.

See the continuous integration configuration for an example of how to install all these dependencies on an ubuntu system

Once the dependencies are installed, you can compile and install this library using

mkdir build
cd build 
cmake ..
make
make install

This should install the libcallpy library to /usr/local/lib and the necessary module files to /usr/local/include. The specific way to add this library to a Fortran code base will depend on the build system of that code. Typically, you will need to add a flag -I/usr/local/include to any fortran compiler commands letting the compiler find the .mod file for this library, and a -L/usr/local/lib -lcallpy to link against the dynamic library. On some systems, you may need to set LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH at runtime to help the dynamic linker find the library.

Usage

Once installed, this library is very simple to use. For example:

program example
use callpy_mod
implicit none

real(8) :: a(10)
a = 1.0
call set_state("a", a)
call call_function("builtins", "print")
! read any changes from "a" back into a.
call get_state("a", a)

end program example

It basically operates by pushing fortran arrays into a global python dictionary, calling python functions with this dictionary as input, and then reading numpy arrays from this dictionary back into fortran. Let this dictionary by called STATE. In terms of python operations, the above lines roughly translate to

# abuse of notation signifyling that the left-hand side is a numpy array
STATE["a"] = a[:]
# same as `print` but with module name
builtins.print(STATE)
# transfer from python back to fortran memory
a[:] = STATE["a"]

You should be able to compile the above by running

gfortran -I/usr/local/include -Wl,-rpath=/usr/local/lib -L/usr/local/lib main.f90 -lcallpy

Here's what happens when you run the compiled binary:

$ ./a.out 
{'a': array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])}

By modifying, the arguments of call_function you can call any python function in the pythonpath.

Currently, get_state and set_state support 4 byte or 8 byte floating point of one, two, or three dimensions.

Examples

See the unit tests for more examples.

call_py_fort's People

Contributors

nbren12 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.