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_NATOMS 26 : /* 27 : Gives the number of atoms in the connected component 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 number of atoms that are connected together in a particular connected component. It is important to note that the quantity that is 34 : 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 and then 40 : the number of atoms in the largest cluster is found. This quantity is then output to a file called colvar 41 : 42 : \plumedfile 43 : # Calculate coordination numbers 44 : c1: COORDINATIONNUMBER SPECIES=1-512 SWITCH={EXP D_0=4.0 R_0=0.5 D_MAX=6.0} 45 : # Select coordination numbers that are more than 2.0 46 : cf: MFILTER_MORE DATA=c1 SWITCH={RATIONAL D_0=2.0 R_0=0.1} LOWMEM 47 : # Build a contact matrix 48 : mat: CONTACT_MATRIX ATOMS=cf SWITCH={EXP D_0=4.0 R_0=0.5 D_MAX=6.0} 49 : # Find largest cluster 50 : dfs: DFSCLUSTERING MATRIX=mat 51 : clust1: CLUSTER_PROPERTIES CLUSTERS=dfs CLUSTER=1 52 : nat: CLUSTER_NATOMS CLUSTERS=dfs CLUSTER=1 53 : PRINT ARG=nat FILE=COLVAR 54 : \endplumedfile 55 : 56 : */ 57 : //+ENDPLUMEDOC 58 : 59 : namespace PLMD { 60 : namespace adjmat { 61 : 62 : class ClusterSize : public ClusterAnalysisBase { 63 : private: 64 : /// The cluster we are looking for 65 : unsigned clustr; 66 : public: 67 : /// Create manual 68 : static void registerKeywords( Keywords& keys ); 69 : /// Constructor 70 : explicit ClusterSize(const ActionOptions&); 71 : /// 72 : void calculate() override; 73 : /// 74 0 : void performTask( const unsigned& task_index, const unsigned& current, MultiValue& myvals ) const override { 75 0 : plumed_error(); 76 : } 77 : /// 78 : void turnOnDerivatives() override; 79 : }; 80 : 81 13833 : PLUMED_REGISTER_ACTION(ClusterSize,"CLUSTER_NATOMS") 82 : 83 28 : void ClusterSize::registerKeywords( Keywords& keys ) { 84 28 : ClusterAnalysisBase::registerKeywords( keys ); 85 56 : 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."); 86 28 : } 87 : 88 24 : ClusterSize::ClusterSize(const ActionOptions&ao): 89 : Action(ao), 90 24 : ClusterAnalysisBase(ao) { 91 : // Find out which cluster we want 92 24 : parse("CLUSTER",clustr); 93 : 94 24 : if( clustr<1 ) { 95 0 : error("cannot look for a cluster larger than the largest cluster"); 96 : } 97 24 : if( clustr>getNumberOfNodes() ) { 98 0 : error("cluster selected is invalid - too few atoms in system"); 99 : } 100 : 101 : // Create all tasks by copying those from underlying DFS object (which is actually MultiColvar) 102 11488 : for(unsigned i=0; i<getNumberOfNodes(); ++i) { 103 11464 : addTaskToList(i); 104 : } 105 : // And now finish the setup of everything in the base 106 : std::vector<AtomNumber> fake_atoms; 107 24 : setupMultiColvarBase( fake_atoms ); 108 24 : addValue(); 109 24 : setNotPeriodic(); 110 24 : } 111 : 112 0 : void ClusterSize::turnOnDerivatives() { 113 0 : error("cannot calculate derivatives of number of atoms in cluster. This quantity is not differentiable"); 114 : } 115 : 116 27 : void ClusterSize::calculate() { 117 : // Retrieve the atoms in the largest cluster 118 : std::vector<unsigned> myatoms; 119 27 : retrieveAtomsInCluster( clustr, myatoms ); 120 27 : setValue( myatoms.size() ); 121 27 : } 122 : 123 : } 124 : }