PYTHONFUNCTION

Define collective variables in the Python language.

A Python module named in the IMPORT keyword is first imported. The function passed as the FUNCTION keyword is called at each time step. It is assumed to receive a numpy array of shape (N,1), N being the number of arguments passed at the ARG keyword. See also CUSTOM.

The function should return two values: a scalar (the CV value), and its gradient with respect to each coordinate (an array of the same shape as the input). Not returning the gradient will prevent biasing from working (with warnings).

Automatic differentiation and transparent compilation (including to GPU) can be performed via Google's JAX library: see PYTHONCV.

Examples

The following example mimics the one in CUSTOM.

Click on the labels of the actions for more information on what each action computes
tested on master
dAB: DISTANCE 
ATOMS
the pair of atom that we are calculating the distance between.
=10,12 dAC: DISTANCE
ATOMS
the pair of atom that we are calculating the distance between.
=10,15 diff: PYTHONFUNCTION
ARG
could not find this keyword
=dAB,dAC
IMPORT
could not find this keyword
=pythonfunction
FUNCTION
could not find this keyword
=diff
PERIODIC
could not find this keyword
=NO
METAD ARG=diff WIDTH=0.1 HEIGHT=0.5 BIASFACTOR=10 PACE=100

The file pythonfunction.py should contain something as follows.

import jax.numpy as np
def diff_f(X):
x, y = X
return y-x
# Just to demonstrate how auto grad is done.
# In this specific case it is just np.array([-1., 1.])
diff_grad = grad(diff_f)
# The function actually being called
def diff(x):
return diff_f(x), diff_grad(x)
See also

Use PYTHONCV if you are dealing with atom coordinates directly.

Please see PYTHONCV for installation and automatic differentiation.

See CUSTOM for a non-Python equivalent.

Glossary of keywords and components