Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-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 "core/ActionWithMatrix.h" 23 : #include "core/ActionRegister.h" 24 : 25 : //+PLUMEDOC MCOLVAR VORONOI 26 : /* 27 : Do a voronoi analysis 28 : 29 : \par Examples 30 : 31 : */ 32 : //+ENDPLUMEDOC 33 : 34 : namespace PLMD { 35 : namespace matrixtools { 36 : 37 : class Voronoi : public ActionWithMatrix { 38 : public: 39 : static void registerKeywords( Keywords& keys ); 40 : explicit Voronoi(const ActionOptions&); 41 : void prepare() override ; 42 0 : unsigned getNumberOfDerivatives() override { 43 0 : return 0; 44 : } 45 12 : unsigned getNumberOfColumns() const override { 46 12 : return getConstPntrToComponent(0)->getShape()[1]; 47 : } 48 1036 : void setupForTask( const unsigned& task_index, std::vector<unsigned>& indices, MultiValue& myvals ) const override {} 49 0 : void performTask( const std::string& controller, const unsigned& index1, const unsigned& index2, MultiValue& myvals ) const override {} 50 1036 : void runEndOfRowJobs( const unsigned& ival, const std::vector<unsigned> & indices, MultiValue& myvals ) const override {} 51 : void gatherStoredValue( const unsigned& valindex, const unsigned& code, const MultiValue& myvals, 52 : const unsigned& bufstart, std::vector<double>& buffer ) const override ; 53 : }; 54 : 55 : PLUMED_REGISTER_ACTION(Voronoi,"VORONOI") 56 : 57 11 : void Voronoi::registerKeywords( Keywords& keys ) { 58 11 : ActionWithMatrix::registerKeywords(keys); 59 22 : keys.addInputKeyword("compulsory","ARG","matrix","the distance/adjacency matrix that should be used to perform the voronoi analysis"); 60 22 : keys.setValueDescription("matrix","a matrix in which element ij is equal to one if the ij component of the input matrix is lower than all the ik elements of the matrix where k is not j and zero otherwise"); 61 11 : } 62 : 63 6 : Voronoi::Voronoi(const ActionOptions&ao): 64 : Action(ao), 65 6 : ActionWithMatrix(ao) { 66 6 : if( getNumberOfArguments()!=1 ) { 67 0 : error("should be one arguments to this action, a matrix"); 68 : } 69 6 : if( getPntrToArgument(0)->getRank()!=2 || getPntrToArgument(0)->hasDerivatives() ) { 70 0 : error("argument to this action should be a matrix"); 71 : } 72 6 : if( getPntrToArgument(0)->getShape()[1]>getPntrToArgument(0)->getShape()[0] ) { 73 0 : warning("would expect number of columns in matrix to exceed number of rows"); 74 : } 75 6 : getPntrToArgument(0)->buildDataStore(); 76 6 : std::vector<unsigned> shape( getPntrToArgument(0)->getShape() ); 77 6 : addValue( shape ); 78 6 : setNotPeriodic(); 79 6 : getPntrToComponent(0)->buildDataStore(); 80 6 : } 81 : 82 6 : void Voronoi::prepare() { 83 6 : Value* myval = getPntrToComponent(0); 84 6 : if( myval->getShape()[0]==getPntrToArgument(0)->getShape()[0] && myval->getShape()[1]==getPntrToArgument(0)->getShape()[1] ) { 85 0 : return; 86 : } 87 6 : std::vector<unsigned> shape( getPntrToArgument(0)->getShape() ); 88 6 : myval->setShape(shape); 89 : } 90 : 91 1036 : void Voronoi::gatherStoredValue( const unsigned& valindex, const unsigned& code, const MultiValue& myvals, 92 : const unsigned& bufstart, std::vector<double>& buffer ) const { 93 : Value* arg0 = getPntrToArgument(0); 94 : unsigned nv = 0; 95 1036 : std::size_t cc=code; 96 1036 : double minmax = arg0->get( cc*arg0->getShape()[1] ); 97 251175 : for(unsigned i=0; i<arg0->getShape()[1]; ++i) { 98 250139 : double value = arg0->get( code*arg0->getShape()[1] + i ); 99 250139 : if( value<minmax ) { 100 : minmax = value; 101 : nv = i; 102 : } 103 : } 104 1036 : buffer[bufstart + code*arg0->getShape()[1] + nv] = 1; 105 1036 : } 106 : 107 : } 108 : }