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 : } 32 : 33 17 : ClusteringBase::ClusteringBase(const ActionOptions&ao): 34 : Action(ao), 35 : matrixtools::MatrixOperationBase(ao), 36 17 : number_of_cluster(-1) 37 : { 38 : // Do some checks on the input 39 17 : if( getPntrToArgument(0)->getShape()[0]!=getPntrToArgument(0)->getShape()[1] ) error("input matrix should be square"); 40 : 41 : // Now create a value - this holds the data on which cluster each guy is in 42 17 : std::vector<unsigned> shape(1); shape[0]=getPntrToArgument(0)->getShape()[0]; 43 : // Build the store here to make sure that next action has all data 44 17 : addValue( shape ); setNotPeriodic(); getPntrToValue()->buildDataStore(); 45 : // Resize local variables 46 17 : which_cluster.resize( getPntrToArgument(0)->getShape()[0] ); cluster_sizes.resize( getPntrToArgument(0)->getShape()[0] ); 47 17 : } 48 : 49 38 : void ClusteringBase::retrieveAdjacencyLists( std::vector<unsigned>& nneigh, Matrix<unsigned>& adj_list ) { 50 : // Make sure we have the edges stored 51 : std::vector<std::pair<unsigned,unsigned> > pairs; std::vector<double> vals; 52 38 : unsigned nedge; getPntrToArgument(0)->retrieveEdgeList( nedge, pairs, vals ); 53 : // Currently everything has zero neighbors 54 20912 : for(unsigned i=0; i<nneigh.size(); ++i) nneigh[i]=0; 55 : // Resize the adjacency list if it is needed 56 38 : if( adj_list.ncols()!=getPntrToArgument(0)->getNumberOfColumns() ) { 57 21 : unsigned nrows = getPntrToArgument(0)->getShape()[0]; 58 : adj_list.resize( nrows, getPntrToArgument(0)->getNumberOfColumns() ); 59 : } 60 : 61 : // And set up the adjacency list 62 66502 : for(unsigned i=0; i<nedge; ++i) { 63 : // Store if atoms are connected 64 66464 : unsigned j=pairs[i].first, k=pairs[i].second; 65 66464 : if( j==k ) continue; 66 66464 : adj_list(j,nneigh[j])=k; nneigh[j]++; 67 66464 : adj_list(k,nneigh[k])=j; nneigh[k]++; 68 : } 69 38 : } 70 : 71 38 : void ClusteringBase::calculate() { 72 : // All the clusters have zero size initially 73 20912 : for(unsigned i=0; i<cluster_sizes.size(); ++i) { cluster_sizes[i].first=0; cluster_sizes[i].second=i; } 74 : // Do the clustering bit 75 38 : performClustering(); 76 : // Order the clusters in the system by size (this returns ascending order ) 77 38 : std::sort( cluster_sizes.begin(), cluster_sizes.end() ); 78 : // Set the elements of the value to the cluster identies 79 20912 : for(unsigned i=0; i<cluster_sizes.size(); ++i) { 80 20874 : double this_size = static_cast<double>(cluster_sizes.size()-i); 81 21921410 : for(unsigned j=0; j<cluster_sizes.size(); ++j) { 82 21900536 : if( which_cluster[j]==cluster_sizes[i].second ) getPntrToValue()->set( j, this_size ); 83 : } 84 : } 85 38 : } 86 : 87 38 : void ClusteringBase::apply() { 88 38 : if( getPntrToComponent(0)->forcesWereAdded() ) error("forces on clustering actions cannot work as clustering is not differentiable"); 89 38 : } 90 : 91 : } 92 : }