LCOV - code coverage report
Current view: top level - adjmat - DistanceMatrix.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 25 25 100.0 %
Date: 2026-11-05 10:19:04 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2017 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "AdjacencyMatrixBase.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "tools/Matrix.h"
      25             : 
      26             : //+PLUMEDOC MATRIX DISTANCE_MATRIX
      27             : /*
      28             : Calculate a matrix of distances between atoms.
      29             : 
      30             : To calculate the matrix of distances between every distinct pair of atoms in a single group you can use the following command:
      31             : 
      32             : ```plumed
      33             : d1: DISTANCE_MATRIX GROUP=1-7
      34             : ```
      35             : 
      36             : If you would like to calculate the matrix of distances between the atoms in two different groups of atoms you can use the following command:
      37             : 
      38             : ```plumed
      39             : d2: DISTANCE_MATRIX GROUPA=1-7 GROUPB=8-20
      40             : ```
      41             : 
      42             : For both these inputs the distances between atoms are calculated in a way that takes the periodic boundary conditions into account. If you want to
      43             : ignore the periodic boundaries when calculating distances you use the NOPBC flag as shown below:
      44             : 
      45             : ```plumed
      46             : d3: DISTANCE_MATRIX GROUP=1-7 NOPBC
      47             : ```
      48             : 
      49             : Once you have calculated your distance matrix in this way you can do many of the operations that were discussed for [CONTACT_MATRIX](CONTACT_MATRIX.md) with the output.
      50             : For example, you can use the COMPONENTS flag to calculate the $x$, $y$ and $z$ components of the vectors connecting the atoms in your two groups by using
      51             : an input like that shown below:
      52             : 
      53             : ```plumed
      54             : d1: DISTANCE_MATRIX GROUP=1-7 COMPONENTS
      55             : ```
      56             : 
      57             : ## Optimisation details
      58             : 
      59             : If for some reason, you only want to calculate the distances if they are less than a certain cutoff can add the LINKCELL_CUTOFF keyword as follows:
      60             : 
      61             : ```plumed
      62             : d3: DISTANCE_MATRIX GROUP=1-7 LINKCELL_CUTOFF=1.0
      63             : ```
      64             : 
      65             : Using a LINKCELL_CUTOFF ensures that PLUMED can use the link cell technique that is described in the documentation for the [CONTACT_MATRIX](CONTACT_MATRIX.md) action to optimise the calculation.
      66             : Using this technique ensures that many of the distance calculations are avoided. However, this __does not__ mean that PLUMED will not evaluate and store the distances between pairs of
      67             : atoms that are more than the cutoff apart. __The distances for some of the values that are larger than the cutoff are calculated and stored__.
      68             : 
      69             : You can see how to work around this strange implementation detail in the following example input.  Lets suppose that we want to calculate the average distances
      70             : between atoms 1-10 and all the atoms that are within 1 nm of them.  To do this we would use an input similar to the one shown below:
      71             : 
      72             : ```plumed
      73             : d5: DISTANCE_MATRIX GROUPA=1-10 GROUPB=1-250 LINKCELL_CUTOFF=1.0
      74             : # Apply a switching function to determine the elements in the matrix d5
      75             : # where the distance is less than the cutoff
      76             : cut: CUSTOM ARG=d5 FUNC=step(1-x) PERIODIC=NO
      77             : # Taking the element-wise product in the next command gives us a matrix
      78             : # where every element that is greater than the cutoff is zero.
      79             : d5cut: CUSTOM ARG=d5,cut FUNC=x*y PERIODIC=NO
      80             : # We can now calculate the average distances by multiplying these matrices by
      81             : # a vector of ones and thus summing the rows.
      82             : ones: ONES SIZE=250
      83             : totdist: MATRIX_VECTOR_PRODUCT ARG=d5cut,ones
      84             : ndist: MATRIX_VECTOR_PRODUCT ARG=cut,ones
      85             : average: CUSTOM ARG=totdist,ndist FUNC=x/y PERIODIC=NO
      86             : DUMPATOMS ATOMS=1-10 ARG=average FILE=avdist.xyz
      87             : ```
      88             : 
      89             : __In short, if you use DISTANCE_MATRIX and LINKCELL_CUTOFF and what to ignore distances that are larger than the cutoff you need to use an additional [CUSTOM](CUSTOM.md) command later in the input.__
      90             : You should thus only use this combination of action and keyword it you are certain it is necessary. Normally, you are far better using the [CONTACT_MATRIX](CONTACT_MATRIX.md) command in place
      91             : of the DISTANCE_MATRIX command. To obtain a result similar to the one above using this command you would use the following input:
      92             : 
      93             : ```plumed
      94             : cmap: CONTACT_MATRIX GROUPA=1-10 GROUPB=1-250 SWITCH={RATIONAL R_0=0.5 D_MAX=1.0 NN=6 MM=12} COMPONENTS
      95             : # Evaluate the distances for all pairs of atoms that are within D_MAX of each other
      96             : dmat: CUSTOM ARG=cmap.x,cmap.y,cmap.z FUNC=sqrt(x*x+y*y+z*z) PERIODIC=NO
      97             : cdist: CUSTOM ARG=cmap.w,dmat FUNC=x*y PERIODIC=NO
      98             : ones: ONES SIZE=250
      99             : totdist: MATRIX_VECTOR_PRODUCT ARG=cdist,ones
     100             : ndist: MATRIX_VECTOR_PRODUCT ARG=cmap.w,ones
     101             : average: CUSTOM ARG=totdist,ndist FUNC=x/y PERIODIC=NO
     102             : DUMPATOMS ATOMS=1-10 ARG=average FILE=avdist.xyz
     103             : ``
     104             : 
     105             : The advantage when using this input is the final vector `average` that is evaluated here is a continous function. You can thus evaluate derivatives for it and use it as input in a biasing
     106             : method.
     107             : 
     108             : Also notice that you can use MASK to calculate a subset of the rows in the DISTANCE matrix as is done in the following example:
     109             : 
     110             : ```plumed
     111             : # The atoms that are of interest
     112             : ow: GROUP ATOMS=1-16500
     113             : # Fixed virtual atom which serves as the probe volume's center (pos. in nm)
     114             : center: FIXEDATOM AT=2.5,2.5,2.5
     115             : # Vector in which element i is one if atom i is in sphere of interest and zero otherwise
     116             : sphere: INSPHERE ATOMS=ow CENTER=center RADIUS={GAUSSIAN D_0=0.5 R_0=0.01 D_MAX=0.52}
     117             : # The distance matrix
     118             : dmap: DISTANCE_MATRIX COMPONENTS GROUP=ow LINKCELL_CUTOFF=1.0 MASK=sphere
     119             : # Find the four nearest neighbors
     120             : acv_neigh: NEIGHBORS ARG=dmap.w NLOWEST=4 MASK=sphere
     121             : # Compute a function for the atoms that are in the first coordination sphere
     122             : acv_g8: GSYMFUNC_THREEBODY ...
     123             :   WEIGHT=acv_neigh ARG=dmap.x,dmap.y,dmap.z
     124             :   FUNCTION1={FUNC=(cos(ajik)+1/3)^2 LABEL=g8}
     125             :   MASK=sphere
     126             : ...
     127             : # Now compute the value of the function above for those atoms that are in the
     128             : # sphere of interest
     129             : acv: CUSTOM ARG=acv_g8.g8,sphere FUNC=y*(1-(3*x/8)) PERIODIC=NO
     130             : # And now compute the final average
     131             : acv_sum: SUM ARG=acv PERIODIC=NO
     132             : acv_norm: SUM ARG=sphere PERIODIC=NO
     133             : mean: CUSTOM ARG=acv_sum,acv_norm FUNC=x/y PERIODIC=NO
     134             : PRINT ARG=mean FILE=colvar
     135             : ```
     136             : 
     137             : This input calculates the average value for measure of tetrahedral order that is introduced in the documentation for the [TETRA_ANGULAR](TETRA_ANGULAR.md) shortcut
     138             : for those atom that are within a sphere that is centered on the point $(2.5,2.5,2.5)$.
     139             : 
     140             : */
     141             : //+ENDPLUMEDOC
     142             : 
     143             : 
     144             : namespace PLMD {
     145             : namespace adjmat {
     146             : 
     147             : class DistanceMatrix {
     148             : public:
     149             :   double cutoff;
     150             :   static void registerKeywords( Keywords& keys );
     151             :   void parseInput( AdjacencyMatrixBase<DistanceMatrix>* action );
     152             :   static void calculateWeight( const DistanceMatrix& data,
     153             :                                const AdjacencyMatrixInput& input,
     154             :                                MatrixOutput output );
     155             : };
     156             : 
     157             : typedef AdjacencyMatrixBase<DistanceMatrix> dmap;
     158             : PLUMED_REGISTER_ACTION(dmap,"DISTANCE_MATRIX")
     159             : 
     160          78 : void DistanceMatrix::registerKeywords( Keywords& keys ) {
     161          78 :   keys.add("compulsory","LINKCELL_CUTOFF","-1","use a link cells algorithm with this cutoff to optimise the calculation - distances (but not derivatives) for atoms that are further apart than this cutoff and that in the same link cells will still be computed");
     162          78 :   keys.addDeprecatedKeyword("CUTOFF","LINKCELL_CUTOFF");
     163          78 : }
     164             : 
     165          46 : void DistanceMatrix::parseInput( AdjacencyMatrixBase<DistanceMatrix>* action ) {
     166             :   // And set the link cell cutoff
     167          46 :   action->log.printf("  weight is distance between atoms \n");
     168          46 :   action->parse("LINKCELL_CUTOFF",cutoff);
     169          46 :   if( cutoff<0 ) {
     170          12 :     action->setLinkCellCutoff( true, std::numeric_limits<double>::max() );
     171             :   } else {
     172          34 :     action->log.printf("  using link cells with cutoff %f to optimise calculation \n", cutoff);
     173          34 :     action->warning("some distances that are greater than the cutoff will still be evaluated. If you want to ignore them you will need to use a further CUSTOM action in your PLUMED input as detailed in the manual");
     174          34 :     action->setLinkCellCutoff( true, cutoff );
     175             :   }
     176          46 : }
     177             : 
     178     7295619 : void DistanceMatrix::calculateWeight( const DistanceMatrix& data,
     179             :                                       const AdjacencyMatrixInput& input,
     180             :                                       MatrixOutput output ) {
     181     7295619 :   output.val[0] = input.pos.modulo();
     182     7295619 :   double invd = 1.0/output.val[0];
     183     7295619 :   Vector v = (-invd)*input.pos;
     184     7295619 :   output.deriv[0] = v[0];
     185     7295619 :   output.deriv[1] = v[1];
     186     7295619 :   output.deriv[2] = v[2];
     187     7295619 :   output.deriv[3] = -v[0];
     188     7295619 :   output.deriv[4] = -v[1];
     189     7295619 :   output.deriv[5] = -v[2];
     190             : 
     191     7295619 :   output.assignOuterProduct(6,v,input.pos);
     192     7295619 : }
     193             : 
     194             : }
     195             : }
     196             : 

Generated by: LCOV version 1.16