LCOV - code coverage report
Current view: top level - adjmat - ClusterDiameter.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 30 34 88.2 %
Date: 2026-03-30 13:16:06 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2023 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 "ClusterAnalysisBase.h"
      23             : #include "core/ActionRegister.h"
      24             : 
      25             : //+PLUMEDOC CONCOMP CLUSTER_DIAMETER
      26             : /*
      27             : Print out the diameter of one of the connected components
      28             : 
      29             : As discussed in the section of the manual on \ref contactmatrix a useful tool for developing complex collective variables is the notion of the
      30             : 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
      31             : 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.  When analyzing these matrix
      32             : we can treat them as a graph and find connected components using some clustering algorithm.  This action is used in tandem with this form of analysis
      33             : to output the largest of the distances between the pairs of atoms that are connected together in a particular connected component.  It is important to
      34             : note that the quantity that is output by this action cannot be differentiated.  As such it cannot be used as a collective variable in a biased simulation.
      35             : 
      36             : \par Examples
      37             : 
      38             : The following input uses PLUMED to calculate a adjacency matrix that connects a pair of atoms if they both have a coordination number that is greater
      39             : than 2.0 and if they are within 6.0 nm of each other.  Depth first search clustering is used to find the connected components in this matrix.  The distance
      40             : between every pair of atoms that are within the largest of the clusters found is then calculated and the largest of these distances is output to a file named
      41             : colvar.
      42             : 
      43             : \plumedfile
      44             : # Calculate coordination numbers
      45             : c1: COORDINATIONNUMBER SPECIES=1-512 SWITCH={EXP D_0=4.0 R_0=0.5 D_MAX=6.0}
      46             : # Select coordination numbers that are more than 2.0
      47             : cf: MFILTER_MORE DATA=c1 SWITCH={RATIONAL D_0=2.0 R_0=0.1} LOWMEM
      48             : # Build a contact matrix
      49             : mat: CONTACT_MATRIX ATOMS=cf SWITCH={EXP D_0=4.0 R_0=0.5 D_MAX=6.0}
      50             : # Find largest cluster
      51             : dfs: DFSCLUSTERING MATRIX=mat
      52             : clust1: CLUSTER_PROPERTIES CLUSTERS=dfs CLUSTER=1
      53             : dia: CLUSTER_DIAMETER CLUSTERS=dfs CLUSTER=1
      54             : PRINT ARG=dia FILE=colvar
      55             : \endplumedfile
      56             : 
      57             : */
      58             : //+ENDPLUMEDOC
      59             : 
      60             : namespace PLMD {
      61             : namespace adjmat {
      62             : 
      63             : class ClusterDiameter : public ClusterAnalysisBase {
      64             : private:
      65             : /// The cluster we are looking for
      66             :   unsigned clustr;
      67             : public:
      68             : /// Create manual
      69             :   static void registerKeywords( Keywords& keys );
      70             : /// Constructor
      71             :   explicit ClusterDiameter(const ActionOptions&);
      72             : ///
      73             :   void calculate() override;
      74             : ///
      75             :   void performTask( const unsigned& task_index, const unsigned& current, MultiValue& myvals ) const override;
      76             : ///
      77             :   void turnOnDerivatives() override;
      78             : };
      79             : 
      80       13789 : PLUMED_REGISTER_ACTION(ClusterDiameter,"CLUSTER_DIAMETER")
      81             : 
      82           6 : void ClusterDiameter::registerKeywords( Keywords& keys ) {
      83           6 :   ClusterAnalysisBase::registerKeywords( keys );
      84          12 :   keys.add("compulsory","CLUSTER","1","which cluster would you like to look at 1 is the largest cluster, 2 is the second largest, 3 is the the third largest and so on.");
      85           6 : }
      86             : 
      87           2 : ClusterDiameter::ClusterDiameter(const ActionOptions&ao):
      88             :   Action(ao),
      89           2 :   ClusterAnalysisBase(ao) {
      90             :   // Find out which cluster we want
      91           2 :   parse("CLUSTER",clustr);
      92             : 
      93           2 :   if( clustr<1 ) {
      94           0 :     error("cannot look for a cluster larger than the largest cluster");
      95             :   }
      96           2 :   if( clustr>getNumberOfNodes() ) {
      97           0 :     error("cluster selected is invalid - too few atoms in system");
      98             :   }
      99             : 
     100             :   // Create the task list
     101        3994 :   for(unsigned  i=0; i<getNumberOfNodes(); ++i) {
     102     7972024 :     for(unsigned j=0; j<getNumberOfNodes(); ++j) {
     103     7968032 :       addTaskToList( i*getNumberOfNodes() + j );
     104             :     }
     105             :   }
     106             :   // Now create a highest vessel
     107           4 :   addVessel("HIGHEST", "", -1);
     108             :   std::vector<AtomNumber> fake_atoms;
     109           2 :   setupMultiColvarBase( fake_atoms );
     110           2 : }
     111             : 
     112           0 : void ClusterDiameter::turnOnDerivatives() {
     113           0 :   error("cannot calculate derivatives of cluster radius.  This quantity is not differentiable");
     114             : }
     115             : 
     116           2 : void ClusterDiameter::calculate() {
     117             :   // Retrieve the atoms in the largest cluster
     118             :   std::vector<unsigned> myatoms;
     119           2 :   retrieveAtomsInCluster( clustr, myatoms );
     120             :   // Activate the relevant tasks
     121           2 :   deactivateAllTasks();
     122         128 :   for(unsigned i=1; i<myatoms.size(); ++i) {
     123        4158 :     for(unsigned j=0; j<i; ++j) {
     124        4032 :       taskFlags[ myatoms[i]*getNumberOfNodes() + myatoms[j] ] = 1;
     125             :     }
     126             :   }
     127           2 :   lockContributors();
     128             :   // Now do the calculation
     129           2 :   runAllTasks();
     130           2 : }
     131             : 
     132        2016 : void ClusterDiameter::performTask( const unsigned& task_index, const unsigned& current, MultiValue& myvals ) const {
     133        2016 :   unsigned iatom=std::floor(current/getNumberOfNodes()), jatom = current - iatom*getNumberOfNodes();
     134        2016 :   Vector distance=getSeparation( getPosition(iatom), getPosition(jatom) );
     135        2016 :   double dd = distance.modulo();
     136             :   myvals.setValue( 0, 1.0 );
     137             :   myvals.setValue( 1, dd );
     138        2016 : }
     139             : 
     140             : }
     141             : }

Generated by: LCOV version 1.16