diff --git a/new-manual/parsing.md b/new-manual/parsing.md index 206d7b71a7..9cf1a252ee 100644 --- a/new-manual/parsing.md +++ b/new-manual/parsing.md @@ -27,8 +27,8 @@ t: METAD ARG=e1 SIGMA=0.15 PACE=10 HEIGHT=2 GRID_MIN=-pi GRID_MAX=pi GRID_BIN=20 Notice here that the boundaries for `GRID_MIN` and `GRID_MAX` are `-pi` and `pi`. -Any real numbers that are read in from input are interpretted using the [Lepton library](Custom.md) so you can thus employ -complicated expressions such as `1+2` or `exp(10)` as shown in the in the input as shown below: +Any real numbers that are read in from input are interpretted using the [Lepton library] that is described in the documtation for the [CUSTOM](CUSTOM.md) +action. You can thus employ complicated expressions such as `1+2` or `exp(10)` as shown in the in the input as shown below: ```plumed #SETTINGS MOLFILE=regtest/basic/rt65/AA.pdb @@ -37,7 +37,7 @@ e1: TORSION ATOMS=@epsilon-1 RESTRAINT ARG=e1 AT=1+0.5 ``` -The lepton library is able to interpret any of thse following constants: +You can also also write expressions in terms of any of the following constants: | Symbol | Description | | :----- |:------------| diff --git a/src/function/Between.cpp b/src/function/Between.cpp index ae3dfe9a43..e06c347807 100644 --- a/src/function/Between.cpp +++ b/src/function/Between.cpp @@ -34,58 +34,106 @@ namespace function { /* Use a switching function to determine how many of the input variables are within a certain range. -If we have multiple instances of a variable we can estimate the probability density function -for that variable using a process called kernel density estimation: +This action takes one argument, $s$ and evaluates the following function: -\f[ -P(s) = \sum_i K\left( \frac{s - s_i}{w} \right) -\f] +$$ +w(s) = \int_a^b K\left( \frac{x - s}{\sigma} \right) \textrm{d}x +$$ -In this equation \f$K\f$ is a symmetric function that must integrate to one that is often -called a kernel function and \f$w\f$ is a smearing parameter. From a probability density function calculated using -kernel density estimation we can calculate the number/fraction of values between an upper and lower -bound using: +In this equation $K$ is a symmetric function that must integrate to one and that is often +called a [kernel function](https://en.wikipedia.org/wiki/Kernel_(statistics)) and $\sigma$ is a smearing parameter. +The above function can be used to evaluate whether $s$ is between $a$ and $b$. The advantage of using the function above is that +the resulting quantity has continuous derivatives. It can thus be used when calculating a collective variable upon which a simulation +bias will be applied. -\f[ -w(s) = \int_a^b \sum_i K\left( \frac{s - s_i}{w} \right) -\f] +The following example, shows how we can apply the function above on the instantaneous value of the distance between atom 1 and 2. +The BETWEEN action here is used to determine whether the input distance is between 0.1 and 0.2 nm. -All the input to calculate a quantity like \f$w(s)\f$ is generally provided through a single -keyword that will have the following form: +```plumed +d: DISTANCE ATOMS=1,2 +b: BETWEEN ARG=d SWITCH={GAUSSIAN LOWER=0.1 UPPER=0.2 SMEAR=0.5} +``` -KEYWORD={TYPE UPPER=\f$a\f$ LOWER=\f$b\f$ SMEAR=\f$\frac{w}{b-a}\f$} +## The Kernel function -This will calculate the number of values between \f$a\f$ and \f$b\f$. To calculate -the fraction of values you add the word NORM to the input specification. If the -function keyword SMEAR is not present \f$w\f$ is set equal to \f$0.5(b-a)\f$. Finally, -type should specify one of the kernel types that is present in plumed. These are listed -in the table below: +The $\sigma$ values in the expressions above is calculated from the parameters $a$, $b$ and $s$ that are provided using the +`LOWER`, `UPPER` and `SMEAR` parameters respectively using the following function: -
TYPE | FUNCTION | -
GAUSSIAN | \f$\frac{1}{\sqrt{2\pi}w} \exp\left( -\frac{(s-s_i)^2}{2w^2} \right)\f$ | -
TRIANGULAR | \f$ \frac{1}{2w} \left( 1. - \left| \frac{s-s_i}{w} \right| \right) \quad \frac{s-s_i}{w}<1 \f$ | -
TYPE | FUNCTION | EXAMPLE INPUT | DEFAULT PARAMETERS | -
RATIONAL | -\f$ -s(r)=\frac{ 1 - \left(\frac{ r - d_0 }{ r_0 }\right)^{n} }{ 1 - \left(\frac{ r - d_0 }{ r_0 }\right)^{m} } -\f$ - | -{RATIONAL R_0=\f$r_0\f$ D_0=\f$d_0\f$ NN=\f$n\f$ MM=\f$m\f$} - | \f$d_0=0.0\f$, \f$n=6\f$, \f$m=2n\f$ | -
EXP | -\f$ -s(r)=\exp\left(-\frac{ r - d_0 }{ r_0 }\right) -\f$ - | -{EXP R_0=\f$r_0\f$ D_0=\f$d_0\f$} - | \f$d_0=0.0\f$ | -
GAUSSIAN | -\f$ -s(r)=\exp\left(-\frac{ (r - d_0)^2 }{ 2r_0^2 }\right) -\f$ - | -{GAUSSIAN R_0=\f$r_0\f$ D_0=\f$d_0\f$} - | \f$d_0=0.0\f$ | -
SMAP | -\f$ -s(r) = \left[ 1 + ( 2^{a/b} -1 )\left( \frac{r-d_0}{r_0} \right)^a \right]^{-b/a} -\f$ - | -{SMAP R_0=\f$r_0\f$ D_0=\f$d_0\f$ A=\f$a\f$ B=\f$b\f$} - | \f$d_0=0.0\f$ | -
Q | -\f$ -s(r) = \frac{1}{1 + \exp(\beta(r_{ij} - \lambda r_{ij}^0))} -\f$ - | -{Q REF=\f$r_{ij}^0\f$ BETA=\f$\beta\f$ LAMBDA=\f$\lambda\f$ } - | \f$\lambda=1.8\f$, \f$\beta=50 nm^-1\f$ (all-atom) \f$\lambda=1.5\f$, \f$\beta=50 nm^-1\f$ (coarse-grained) |
-
CUBIC | -\f$ -s(r) = (y-1)^2(1+2y) \qquad \textrm{where} \quad y = \frac{r - r_1}{r_0-r_1} -\f$ - | -{CUBIC D_0=\f$r_1\f$ D_MAX=\f$r_0\f$} - | - |
TANH | -\f$ -s(r) = 1 - \tanh\left( \frac{ r - d_0 }{ r_0 } \right) -\f$ - | -{TANH R_0=\f$r_0\f$ D_0=\f$d_0\f$} - | - |
COSINUS |
-\f$s(r) =\left\{\begin{array}{ll}
+This action takes one argument, $r$ and evaluates the following function:
+
+$$
+w(r) = s(r)
+$$
+
+The function $s(r)$ here is a switching function so the output value $w$ is a number between 0 and 1.
+Switching functions are typically used to determine if an input value is less than some cutoff. The value
+of $w$ the switches smoothly from one to zero as the input value $r$ crosses the threshold of interest.
+
+The following example, shows how we can apply a switching function on the instantaneous value of the
+distance between atom 1 and atom 2.
+
+```plumed
+d: DISTANCE ATOMS=1,2
+s: LESS_THAN ARG=d SWITCH={RATIONAL R_0=0.2}
+```
+
+The output here is close to one when the distance between atoms 1 and 2 is less than 0.2 nm close to zero
+for values that are greater than 0.2.
+
+## Non rank zero arguments
+
+Instead of passing a single scalar in the input to the `LESS_THAN` action you can pass a single vector as shown here:
+
+```plumed
+d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8
+b: LESS_THAN ARG=d SWITCH={RATIONAL R_0=0.2}
+```
+
+The input to the `LESS_THAN` action here is a vector with four elements. The output from the action `b` is similarly
+a vector with four elements. In calculating the elements of this vector PLUMED applies the function described in the previous
+section on each of the distances in turn. The first element of `b` thus tells you if the distance between atoms 1 and 2 is less than
+0.2 nm, the second element tells you if the distance between atoms 3 and 4 is less than 0.2 nm and so on.
+
+You can use the commands in the above example input to evaluate the number of distances that are less than a cutoff as follows:
+
+```plumed
+d: DISTANCE ATOMS1=1,2 ATOMS2=3,4 ATOMS3=5,6 ATOMS4=7,8
+b: LESS_THAN ARG=d SWITCH={RATIONAL R_0=0.2}
+s: SUM ARG=b PERIODIC=NO
+PRINT ARG=s FILE=colvar
+```
+
+The final scalar that is output here is evaluated using the following summation:
+
+$$
+s = \sum_i s(d_i)
+$$
+
+where the sum over $i$ here runs over the four distances in the above expression. This scalar tells you the number of distances that are
+less than 0.2 nm.
+
+Notice that you can do something similar with a matrix as input as shown below:
+
+```plumed
+d: DISTANCE_MATRIX GROUPA=1-10 GROUPB=11-20
+b: LESS_THAN ARG=d SWITCH={RATIONAL R_0=0.2}
+s: SUM ARG=b PERIODIC=NO
+PRINT ARG=s FILE=colvar
+```
+
+This input tells PLUMED to calculate the 100 distances between the atoms in the two input groups. The final value that is printed to the colvar file then
+tells you how many of these distances are less than 0.2 nm.
+
+## Switching functions types
+
+PLUMED allows you to use a range of different switching function types. Most of these
+require you to provide at least one input parameter $r_0$. The switching function is then defined so that for
+$r \le d_0 \quad s(r)=1.0$ while for $r > d_0$ the function decays smoothly to 0. By changing the switching
+function you are using you change the decay that occurs in the switching function for $r$ values that are greater
+that $d_0$.
+
+You can specify the value at which the the switching function goes to zero by using the D_MAX keyword.
+PLUMED will ensure that the swtiching function is definitely zero for for input values that are greater than
+or equal to D_MAX by by stretching and shifting the function using the following transformations.
+
+$
+s(r) = \frac{s'(r)-s'(d_{max})}{s'(0)-s'(d_{max})}
+$
+
+The various expressions that are used for $s'(r)$ in the above expression are described in the following sections.
+Scaling the switching function so that it is exactly zero at $d_{max}$ using the transformation described above
+has been the default behaviour within PLUMED since version 2.2.
+If you wish to use a D_MAX parameter and the $s'(r)$ functions described in the following sections directly you can use the
+keyword `NOSTRETCH`. However, the advangage of performing the stretching and scaling in this way is that the function has
+no discontinuities. If you use the `NOSTRETCH` option the switching function may have a discontinuity at $d_{max}$.
+
+### Rational switching function
+
+The rational switching function is the most commonly used of the switching functions. The implementation of this function within PLUMED has been carefully
+optimised so calculations using this switching function should be fast.
+
+For this function $s'(r)$ has the following functional form:
+
+$$
+s'(r) = \begin{cases}
+1 & \textrm{if} \quad r | -{COSINUS R_0=\f$r_0\f$ D_0=\f$d_0\f$} - | - |
CUSTOM | -\f$ -s(r) = FUNC -\f$ - | -{CUSTOM FUNC=1/(1+x^6) R_0=\f$r_0\f$ D_0=\f$d_0\f$} - | - |