Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2015-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 "ClusteringBase.h" 23 : #include "core/PlumedMain.h" 24 : 25 : namespace PLMD { 26 : namespace clusters { 27 : 28 21 : void ClusteringBase::registerKeywords( Keywords& keys ) { 29 21 : matrixtools::MatrixOperationBase::registerKeywords( keys ); 30 42 : keys.setValueDescription("vector","vector with length that is equal to the number of rows in the input matrix. Elements of this vector are equal to the cluster that each node is a part of"); 31 21 : keys.addDOI("10.1021/acs.jctc.6b01073"); 32 21 : } 33 : 34 17 : ClusteringBase::ClusteringBase(const ActionOptions&ao): 35 : Action(ao), 36 : matrixtools::MatrixOperationBase(ao), 37 17 : number_of_cluster(-1) { 38 : // Do some checks on the input 39 17 : if( getPntrToArgument(0)->getShape()[0]!=getPntrToArgument(0)->getShape()[1] ) { 40 0 : error("input matrix should be square"); 41 : } 42 : 43 : // Now create a value - this holds the data on which cluster each guy is in 44 17 : std::vector<unsigned> shape(1); 45 17 : shape[0]=getPntrToArgument(0)->getShape()[0]; 46 : // Build the store here to make sure that next action has all data 47 17 : addValue( shape ); 48 17 : setNotPeriodic(); 49 17 : getPntrToValue()->buildDataStore(); 50 : // Resize local variables 51 17 : which_cluster.resize( getPntrToArgument(0)->getShape()[0] ); 52 17 : cluster_sizes.resize( getPntrToArgument(0)->getShape()[0] ); 53 34 : log<<" Bibliography "<<plumed.cite("10.1021/acs.jctc.6b01073")<<"\n"; 54 17 : } 55 : 56 38 : void ClusteringBase::retrieveAdjacencyLists( std::vector<unsigned>& nneigh, Matrix<unsigned>& adj_list ) { 57 : // Make sure we have the edges stored 58 : std::vector<std::pair<unsigned,unsigned> > pairs; 59 : std::vector<double> vals; 60 : unsigned nedge; 61 38 : getPntrToArgument(0)->retrieveEdgeList( nedge, pairs, vals ); 62 : // Currently everything has zero neighbors 63 20912 : for(unsigned i=0; i<nneigh.size(); ++i) { 64 20874 : nneigh[i]=0; 65 : } 66 : // Resize the adjacency list if it is needed 67 38 : if( adj_list.ncols()!=getPntrToArgument(0)->getNumberOfColumns() ) { 68 21 : unsigned nrows = getPntrToArgument(0)->getShape()[0]; 69 : adj_list.resize( nrows, getPntrToArgument(0)->getNumberOfColumns() ); 70 : } 71 : 72 : // And set up the adjacency list 73 66502 : for(unsigned i=0; i<nedge; ++i) { 74 : // Store if atoms are connected 75 66464 : unsigned j=pairs[i].first, k=pairs[i].second; 76 66464 : if( j==k ) { 77 : continue; 78 : } 79 66464 : adj_list(j,nneigh[j])=k; 80 66464 : nneigh[j]++; 81 66464 : adj_list(k,nneigh[k])=j; 82 66464 : nneigh[k]++; 83 : } 84 38 : } 85 : 86 38 : void ClusteringBase::calculate() { 87 : // All the clusters have zero size initially 88 20912 : for(unsigned i=0; i<cluster_sizes.size(); ++i) { 89 20874 : cluster_sizes[i].first=0; 90 20874 : cluster_sizes[i].second=i; 91 : } 92 : // Do the clustering bit 93 38 : performClustering(); 94 : // Order the clusters in the system by size (this returns ascending order ) 95 38 : std::sort( cluster_sizes.begin(), cluster_sizes.end() ); 96 : // Set the elements of the value to the cluster identies 97 20912 : for(unsigned i=0; i<cluster_sizes.size(); ++i) { 98 20874 : double this_size = static_cast<double>(cluster_sizes.size()-i); 99 21921410 : for(unsigned j=0; j<cluster_sizes.size(); ++j) { 100 21900536 : if( which_cluster[j]==cluster_sizes[i].second ) { 101 20874 : getPntrToValue()->set( j, this_size ); 102 : } 103 : } 104 : } 105 38 : } 106 : 107 38 : void ClusteringBase::apply() { 108 38 : if( getPntrToComponent(0)->forcesWereAdded() ) { 109 0 : error("forces on clustering actions cannot work as clustering is not differentiable"); 110 : } 111 38 : } 112 : 113 : } 114 : }