LCOV - code coverage report
Current view: top level - adjmat - ContactMatrixShortcut.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 45 57 78.9 %
Date: 2025-11-25 13:55:50 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2013-2020 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 "core/ActionShortcut.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "core/PlumedMain.h"
      25             : #include "core/ActionSet.h"
      26             : #include "core/Group.h"
      27             : #include "AdjacencyMatrixBase.h"
      28             : 
      29             : //+PLUMEDOC MATRIX CONTACT_MATRIX
      30             : /*
      31             : Adjacency matrix in which two atoms are adjacent if they are within a certain cutoff.
      32             : 
      33             : As discussed in the section of the manual on \ref contactmatrix a useful tool for developing complex collective variables is the notion of the
      34             : so called adjacency matrix.  An adjacency matrix is an \f$N \times N\f$ matrix in which the \f$i\f$th, \f$j\f$th element tells you whether
      35             : or not the \f$i\f$th and \f$j\f$th atoms/molecules from a set of \f$N\f$ atoms/molecules are adjacent or not.  These matrices can then be further
      36             : analyzed using a number of other algorithms as is detailed in \cite tribello-clustering.
      37             : 
      38             : For this action the elements of the contact matrix are calculated using:
      39             : 
      40             : \f[
      41             :  a_{ij} = \sigma( |\mathbf{r}_{ij}| )
      42             : \f]
      43             : 
      44             : where \f$|\mathbf{r}_{ij}|\f$ is the magnitude of the vector connecting atoms \f$i\f$ and \f$j\f$ and where \f$\sigma\f$ is a \ref switchingfunction.
      45             : 
      46             : \par Examples
      47             : 
      48             : The input shown below calculates a \f$6 \times 6\f$ matrix whose elements are equal to one if atom \f$i\f$ and atom \f$j\f$ are within 0.3 nm
      49             : of each other and which is zero otherwise.  The columns in this matrix are then summed so as to give the coordination number for each atom.
      50             : The final quantity output in the colvar file is thus the average coordination number.
      51             : 
      52             : \plumedfile
      53             : mat: CONTACT_MATRIX ATOMS=1-6 SWITCH={EXP D_0=0.2 R_0=0.1 D_MAX=0.66}
      54             : COLUMNSUMS MATRIX=mat MEAN LABEL=csums
      55             : PRINT ARG=csums.* FILE=colvar
      56             : \endplumedfile
      57             : 
      58             : */
      59             : //+ENDPLUMEDOC
      60             : 
      61             : namespace PLMD {
      62             : namespace adjmat {
      63             : 
      64             : class ContactMatrixShortcut : public ActionShortcut {
      65             : public:
      66             :   static void registerKeywords(Keywords& keys);
      67             :   explicit ContactMatrixShortcut(const ActionOptions&);
      68             : };
      69             : 
      70             : PLUMED_REGISTER_ACTION(ContactMatrixShortcut,"CONTACT_MATRIX")
      71             : 
      72         334 : void ContactMatrixShortcut::registerKeywords(Keywords& keys) {
      73         334 :   AdjacencyMatrixBase::registerKeywords( keys );
      74         334 :   keys.remove("GROUP");
      75         668 :   keys.add("numbered","GROUP","specifies the list of atoms that should be assumed indistinguishable");
      76         668 :   keys.add("compulsory","NN","6","The n parameter of the switching function ");
      77         668 :   keys.add("compulsory","MM","0","The m parameter of the switching function; 0 implies 2*NN");
      78         668 :   keys.add("compulsory","D_0","0.0","The d_0 parameter of the switching function");
      79         668 :   keys.add("compulsory","R_0","The r_0 parameter of the switching function");
      80         668 :   keys.add("numbered","SWITCH","specify the switching function to use between two sets of indistinguishable atoms");
      81         334 :   keys.addActionNameSuffix("_PROPER");
      82         334 :   keys.needsAction("TRANSPOSE");
      83         334 :   keys.needsAction("CONCATENATE");
      84         334 : }
      85             : 
      86         208 : ContactMatrixShortcut::ContactMatrixShortcut(const ActionOptions& ao):
      87             :   Action(ao),
      88         208 :   ActionShortcut(ao) {
      89             :   std::vector<std::string> grp_str;
      90         208 :   std::string atomsstr="";
      91             :   std::vector<std::string> atomsvec;
      92         416 :   parseVector("ATOMS",atomsvec);
      93         208 :   if( atomsvec.size()>0 )  {
      94           0 :     for(unsigned i=0; i<atomsvec.size(); ++i) {
      95           0 :       Group* gg = plumed.getActionSet().selectWithLabel<Group*>( atomsvec[i] );
      96           0 :       if( gg ) {
      97           0 :         grp_str.push_back( atomsvec[i] );
      98             :       }
      99             :     }
     100           0 :     if( grp_str.size()!=atomsvec.size() ) {
     101           0 :       grp_str.resize(0);
     102           0 :       atomsstr = " ATOMS=" + atomsvec[0];
     103           0 :       for(unsigned i=1; i<atomsvec.size(); ++i) {
     104           0 :         atomsstr += "," + atomsvec[i];
     105             :       }
     106             :     }
     107             :   } else {
     108             :     std::string grp_inpt;
     109           2 :     for(unsigned i=1;; ++i) {
     110         420 :       if( !parseNumbered("GROUP",i,grp_inpt) ) {
     111             :         break;
     112             :       }
     113           2 :       grp_str.push_back( grp_inpt );
     114             :     }
     115             :   }
     116         208 :   if( grp_str.size()>9 ) {
     117           0 :     error("cannot handle more than 9 groups");
     118             :   }
     119         208 :   if( grp_str.size()==0 )  {
     120         414 :     readInputLine( getShortcutLabel() + ": CONTACT_MATRIX_PROPER " + atomsstr + " " + convertInputLineToString() );
     121             :     return;
     122             :   }
     123             : 
     124           3 :   for(unsigned i=0; i<grp_str.size(); ++i) {
     125             :     std::string sw_str, num;
     126           2 :     Tools::convert( i+1, num );
     127           4 :     parseNumbered("SWITCH", (i+1)*10 + 1 + i,  sw_str );
     128           2 :     if( sw_str.length()==0 ) {
     129           0 :       error("missing SWITCH" + num + num + " keyword");
     130             :     }
     131           4 :     readInputLine( getShortcutLabel() + num +  num + ": CONTACT_MATRIX_PROPER GROUP=" + grp_str[i] + " SWITCH={" + sw_str + "}" );
     132           3 :     for(unsigned j=0; j<i; ++j) {
     133             :       std::string sw_str2, jnum;
     134           1 :       Tools::convert( j+1, jnum );
     135           2 :       parseNumbered("SWITCH", (j+1)*10 + 1 + i, sw_str2);
     136           1 :       if( sw_str2.length()==0 ) {
     137           0 :         error("missing SWITCH" + jnum + num + " keyword");
     138             :       }
     139           2 :       readInputLine( getShortcutLabel() + jnum + num + ": CONTACT_MATRIX_PROPER GROUPA=" + grp_str[j] + " GROUPB=" + grp_str[i] + " SWITCH={" + sw_str2 +"}");
     140           2 :       readInputLine( getShortcutLabel() + num +  jnum + ": TRANSPOSE ARG=" + getShortcutLabel() + jnum + num );
     141             :     }
     142             :   }
     143           1 :   std::string join_matrices = getShortcutLabel() + ": CONCATENATE";
     144           3 :   for(unsigned i=0; i<grp_str.size(); ++i) {
     145             :     std::string inum;
     146           2 :     Tools::convert(i+1,inum);
     147           6 :     for(unsigned j=0; j<grp_str.size(); ++j) {
     148             :       std::string jnum;
     149           4 :       Tools::convert(j+1,jnum);
     150           4 :       if( i>j ) {
     151           2 :         join_matrices += " MATRIX" + inum + jnum + "=" + getShortcutLabel() + inum +  jnum;
     152             :       } else {
     153           6 :         join_matrices += " MATRIX" + inum + jnum + "=" + getShortcutLabel() + inum +  jnum;
     154             :       }
     155             :     }
     156             :   }
     157           1 :   readInputLine( join_matrices );
     158         416 : }
     159             : 
     160             : }
     161             : }

Generated by: LCOV version 1.16