With user-source the path to (or the name of) a module containing user-provided routines is specified. The module must be importable into Python, i.e., it must either be a Python script (file extension .py), a compiled Python script (file extension .pyc) or a shared library (file extension .so). If the module is already in the systems $PYTHONPATH, the name of the module is sufficient.
The module must provide all user-defined routines required
within the calling program.
In many cases, those routines are written in Fortran or some other
languages, probably not even by the user himself, but are provided by a
third party. If the routine is written in Fortran we suggest using the f2py
Fortran to Python interface generator available at the SciPy homepage. See below.
Another possibility could be writing a wrapper routine which can be
imported into Python. A further possibility is also to start a
background process (e.g. using the Python built-in subprocess
module)
and communicating with it through named pipes.
The routines receive a double precision array with coordinates of all DOF in the same order as they are in the primitive basis section or the DVR file. They must return the potential value as double precision number. In fortran one therefore needs to use the function statement and not a subroutine.
Simple example of a user-provided source file (Python):
#!/usr/bin/env python # # my_pes.py import numpy def potential(Q): """ My PES routine: harmonic oscillator. Q is a numpy array containing a coordinate vector. """ return numpy.dot(Q,Q)/2
double precision function mypeswrapper(Q) implicit none integer, parameter :: ndof=6 ! problematic comment, see below double precision, intent(in) :: Q(ndof) double precision :: v ! call to the original routine: call OriginalPesSubroutine(Q(1), Q(2),...,Q(ndof),v) mypeswrapper = v end function
f2py -c mypes.f90 -m mypesThis will produce the file mypes.so. In the RUN-SECTION one would then use with the example above:
RUN-SECTION user-source = mypes # this is the name of the .so file potential-routine = mypeswrapper # this is the name of the wrapper function ... end-run-section
Hint: f2py sometimes fails with cryptic error messages. To resolve this start with removing all comments from the routines source code that are placed at the end of lines containing fortran statements like the ! problematic comment above. Often f2py gets mixed up between these comments and the preceding fortran statements. Lines only containing comments are usually not problematic.