Python interface for GMIN/OPTIM

From Docswiki
Jump to navigation Jump to search

Introduction & Motivation

GMIN can be compiled as a python interface. Use the cmake option -DWITH_PYTHON. The goal of this interface is to allow for quick testing of new ideas before implementing them in Fortran. There is no need to recompile the code during testing and python offers a huge selection of 3rd party libraries.

Implementation

The strategy for the python hook is similar to that of the testing framework: call the main rountine, let it read the data file and initialize all the structures, then stop and go back to the python interpreter. The main file for the interface is automatically generated from main.F using a set of sed command. This part is then called from python by pygmin.init()

Hopefully this interface will help modularizing the GMIN/OPTIM codebase a bit more.

Example

Once compiled, set the python path to the directory which contains the newly generated pygmin.so. Then it can be used, as in the following example script

import pygmin
import numpy as np

# parse the data file and setup all gmin structures
pygmin.init()

# get the number of atoms
natoms = pygmin.getNAtoms()

# initialize array with random coordinates
x = np.random.random(3*natoms)

# calculate the energy
print pygmin.getEnergy(x)

# initialize gradient array
g = np.zeros(x.shape)

# calculate energy and gradient
print pygmin.getEnergyGradient(x, g)
print g

Known problems

  • Compilation only tested using gfortran
  • If you get an error during linking, stating something with -fPIC, add -fPIC to CMAKE_CXX_FLAGS, CMAKE_FORTRAN_FLAGS, CMAKE_C_FLAGS