Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2014-2017 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/ActionWithValue.h" 23 : #include "core/ActionWithArguments.h" 24 : #include "core/ActionRegister.h" 25 : 26 : //+PLUMEDOC FUNCTION FLATTEN 27 : /* 28 : Convert a matrix into a vector 29 : 30 : \par Examples 31 : 32 : 33 : */ 34 : //+ENDPLUMEDOC 35 : 36 : namespace PLMD { 37 : namespace valtools { 38 : 39 : class Flatten : 40 : public ActionWithValue, 41 : public ActionWithArguments { 42 : public: 43 : static void registerKeywords( Keywords& keys ); 44 : /// Constructor 45 : explicit Flatten(const ActionOptions&); 46 : /// Get the number of derivatives 47 10 : unsigned getNumberOfDerivatives() override { return 0; } 48 : /// Do the calculation 49 : void calculate() override; 50 : /// 51 : void apply() override; 52 : }; 53 : 54 : PLUMED_REGISTER_ACTION(Flatten,"FLATTEN") 55 : 56 20 : void Flatten::registerKeywords( Keywords& keys ) { 57 20 : Action::registerKeywords( keys ); ActionWithValue::registerKeywords( keys ); 58 20 : ActionWithArguments::registerKeywords( keys ); 59 40 : keys.addInputKeyword("compulsory","ARG","matrix","the label for the matrix that you would like to flatten to a vector"); 60 40 : keys.setValueDescription("vector","a vector containing all the elements of the input matrix"); 61 20 : } 62 : 63 9 : Flatten::Flatten(const ActionOptions& ao): 64 : Action(ao), 65 : ActionWithValue(ao), 66 9 : ActionWithArguments(ao) 67 : { 68 9 : if( getNumberOfArguments()!=1 ) error("should only be one argument for this action"); 69 9 : if( getPntrToArgument(0)->getRank()!=2 || getPntrToArgument(0)->hasDerivatives() ) error("input to this action should be a matrix"); 70 9 : getPntrToArgument(0)->buildDataStore(true); 71 9 : std::vector<unsigned> inshape( getPntrToArgument(0)->getShape() ); 72 9 : std::vector<unsigned> shape( 1 ); shape[0]=inshape[0]*inshape[1]; 73 9 : addValue( shape ); setNotPeriodic(); getPntrToComponent(0)->buildDataStore(); 74 9 : } 75 : 76 9 : void Flatten::calculate() { 77 9 : Value* myval = getPntrToComponent(0); unsigned ss=getPntrToArgument(0)->getShape()[1]; 78 : std::vector<double> vals; std::vector<std::pair<unsigned,unsigned> > pairs; 79 : bool symmetric=getPntrToArgument(0)->isSymmetric(); 80 9 : unsigned nedge=0; getPntrToArgument(0)->retrieveEdgeList( nedge, pairs, vals ); 81 8139 : for(unsigned l=0; l<nedge; ++l ) { 82 8130 : unsigned i=pairs[l].first, j=pairs[l].second; 83 8130 : myval->set( i*ss + j, vals[l] ); 84 8130 : if( symmetric ) myval->set( j*ss + i, vals[l] ); 85 : } 86 9 : } 87 : 88 7 : void Flatten::apply() { 89 7 : if( doNotCalculateDerivatives() || !getPntrToComponent(0)->forcesWereAdded() ) return; 90 : 91 3 : Value* myval=getPntrToComponent(0); Value* myarg=getPntrToArgument(0); 92 22 : unsigned nvals=myval->getNumberOfValues(); for(unsigned j=0; j<nvals; ++j) myarg->addForce( j, myval->getForce(j) ); 93 : } 94 : 95 : } 96 : }