MATHEVAL

This is part of the function module |

Calculate a combination of variables using a matheval expression.

This action computes an arbitrary function of one or more precomputed collective variables. Arguments are chosen with the ARG keyword, and the function is provided with the FUNC string. Notice that this string should contain no space. Within FUNC, one can refer to the arguments as x,y,z, and t (up to four variables provided as ARG). This names can be customized using the VAR keyword (see examples below).

If you want a function that depends not only on collective variables but also on time you can use the TIME action.

- Attention
- The MATHEVAL object only works if libmatheval is installed on the system and PLUMED has been linked to it

- Compulsory keywords

ARG | the input for this action is the scalar output from one or more other actions. The particular scalars that you will use are referenced using the label of the action. If the label appears on its own then it is assumed that the Action calculates a single scalar value. The value of this scalar is thus used as the input to this new action. If * or *.* appears the scalars calculated by all the proceding actions in the input file are taken. Some actions have multi-component outputs and each component of the output has a specific label. For example a DISTANCE action labelled dist may have three componets x, y and z. To take just the x component you should use dist.x, if you wish to take all three components then use dist.*.More information on the referencing of Actions can be found in the section of the manual on the PLUMED Getting started. Scalar values can also be referenced using POSIX regular expressions as detailed in the section on Regular Expressions. To use this feature you you must compile PLUMED with the appropriate flag. |

PERIODIC | if the output of your function is periodic then you should specify the periodicity of the function. If the output is not periodic you must state this using PERIODIC=NO |

FUNC | the function you wish to evaluate |

- Options

NUMERICAL_DERIVATIVES | ( default=off ) calculate the derivatives for these quantities numerically |

VAR | the names to give each of the arguments in the function. If you have up to three arguments in your function you can use x, y and z to refer to them. Otherwise you must use this flag to give your variables names. |

- Examples

The following input tells plumed to perform a metadynamics using as a CV the difference between two distances.

dAB: DISTANCE ARG=10,12 dAC: DISTANCE ARG=10,15 diff: MATHEVAL ARG=dAB,dAC FUNC=y-x PERIODIC=NO # notice: the previous line could be replaced with the following # diff: COMBINE ARG=dAB,dAC COEFFICIENTS=-1,1 METAD ARG=diff WIDTH=0.1 HEIGHT=0.5 BIASFACTOR=10 PACE=100

(see also DISTANCE, COMBINE, and METAD). Notice that forces applied to diff will be correctly propagated to atoms 10, 12, and 15. Also notice that since MATHEVAL is used without the VAR option the two arguments should be referred to as x and y in the expression FUNC. For simple functions such as this one it is possible to use COMBINE, which does not require libmatheval to be installed on your system.

The following input tells plumed to print the angle between vectors identified by atoms 1,2 and atoms 2,3 its square (as computed from the x,y,z components) and the distance again as computed from the square root of the square.

DISTANCE LABEL=d1 ATOMS=1,2 COMPONENTS DISTANCE LABEL=d2 ATOMS=2,3 COMPONENTS MATHEVAL ... LABEL=theta ARG=d1.x,d1.y,d1.z,d2.x,d2.y,d2.z VAR=ax,ay,az,bx,by,bz FUNC=acos((ax*bx+ay*by+az*bz)/sqrt((ax*ax+ay*ay+az*az)*(bx*bx+by*by+bz*bz)) PERIODIC=NO ... MATHEVAL PRINT ARG=theta

(See also PRINT and DISTANCE).

Notice that the matheval library implements a large number of functions (trigonometric, exp, log, etc). Among the useful functions, have a look at the step function (that is the Heaviside function). `step(x)`

is defined as 1 when `x`

is positive and `0`

when x is negative. This allows for a straightforward implementation of if clauses.

For example, imagine that you want to implement a restraint that only acts when a distance is larger than 0.5. You can do it with

d: DISTANCE ATOMS=10,15 m: MATHEVAL ARG=d FUNC=0.5*step(0.5-x)+x*step(x-0.5) PERIODIC=NO # check the function you are applying: PRINT ARG=d,n FILE=checkme RESTRAINT ARG=d AT=0.5 KAPPA=10.0

(see also DISTANCE, PRINT, and RESTRAINT)

The meaning of the function `0.5*step(0.5-x)+x*step(x-0.5)`

is:

- If x<0.5 (step(0.5-x)!=0) use 0.5
- If x>0.5 (step(x-0.5)!=0) use x Notice that the same could have been obtained using an UPPER_WALLS However, with MATHEVAL you can create way more complex definitions.

- Warning
- If you apply forces on the variable (as in the previous example) you should make sure that the variable is continuous! Conversely, if you are just analyzing a trajectory you can safely use discontinuous variables.

A possible continuity check with gnuplot is

# this allow to step function to be used in gnuplot: gnuplot> step(x)=0.5*(erf(x*10000000)+1) # here you can test your function gnuplot> p 0.5*step(0.5-x)+x*step(x-0.5)

Also notice that you can easily make logical operations on the conditions that you create. The equivalent of the AND operator is the product: `step(1.0-x)*step(x-0.5)`

is only equal to 1 when x is between 0.5 and 1.0. By combining negation and AND you can obtain an OR. That is, `1-step(1.0-x)*step(x-0.5)`

is only equal to 1 when x is outside the 0.5-1.0 interval.