jornbr edited this page Dec 3, 2018 · 29 revisions

FABM comes with a "pyfabm" package for the Python programming language. This package enables you to access FABM's biogeochemical models directly from Python. For instance, you can enumerate a model's parameters and variables, or obtain temporal derivatives and diagnostics for any given environment and model state. In combination with a Python-based time integration scheme (e.g., scipy.integrate.odeint), this also allows you to perform model simulations.

Building the Python driver

First, obtain the FABM code from GitHub. You can either download the latest official release of the source code as a zip or tar.gz file, or check out the current developer's version of the code from its git repository (recommended!)

You will need:

  • CMake, version 2.8.11 or later
  • Python 2.7 or Python 3.4 or later
  • pip (installed by default with Python 2 >= 2.7.9 or Python 3 >= 3.4)
  • wheel (install with python -m pip install --user wheel)

On Linux-like systems, pyfabm is built and installed by creating a new directory for building the code, executing cmake with the path to the FABM-Python source code, and then running "make install". For instance:

mkdir -p ~/build/python-fabm
cd ~/build/python-fabm
cmake <FABMDIR>/src/drivers/python
make install

The above make install creates the pyfabm package within the build directory, packs it in wheel format, and then calls pip to install it (default install location: the per user site-packages directory). To skip the installation step, use make all instead of make install. You can then still import pyfabm if you first add the build directory to your Python path (sys.path.insert(0, '<BUILDDIR>').

CMake will try to locate your default Python interpreter to create the pyfabm wheel and call pip. If CMake does not select the correct Python interpreter, you will need to set the PYTHON_EXECUTABLE variable to the full path of your Python interpreter. To do so when using cmake on the command line, add -DPYTHON_EXECUTABLE=<PYTHONPATH>; when using cmake-gui or ccmake, edit this variable in the user interface, then configure and generate.

After the pyfabm package has been built and installed, you can access it from python by typing import pyfabm. Please try this before continuing.

Examples

FABM comes with a number of example Python scripts that shows how to access FABM. These are located at <FABMDIR>/testcases/python. Each of these scripts can be run on the command line with -h as argument; this will list information about the script and its command line arguments.

Enumerating variables

import pyfabm
model = pyfabm.Model('fabm.yaml')
for variable in model.state_variables:
   print '  %s = %s (%s)' % (variable.name, variable.long_name, variable.units)

Run a simulation

import numpy
import scipy.integrate
import pyfabm

# Create model (loads fabm.yaml)
model = pyfabm.Model()

# Configure the environment
# Note: the set of environmental dependencies depends on the loaded biogeochemical model.
model.findDependency('surface_downwelling_photosynthetic_radiative_flux').value = 50.
model.findDependency('downwelling_photosynthetic_radiative_flux').value = 25.

# Verify the model is ready to be used
assert model.checkReady(), 'One or more model dependencies have not been fulfilled.'

# Time derivative
def dy(y, t0):
    model.state[:] = y
    return model.getRates()

# Time-integrate over 200 days (note: FABM's internal time unit is seconds!)
t = numpy.linspace(0, 200., 100)
y = scipy.integrate.odeint(dy, model.state, t*86400)

# Plot results
import pylab
pylab.plot(t, y)
pylab.legend([variable.path for variable in model.state_variables])
pylab.show()
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.