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 ); keys.use("ARG"); 59 20 : keys.setValueDescription("a vector containing all the elements of the input matrix"); 60 20 : } 61 : 62 9 : Flatten::Flatten(const ActionOptions& ao): 63 : Action(ao), 64 : ActionWithValue(ao), 65 9 : ActionWithArguments(ao) 66 : { 67 9 : if( getNumberOfArguments()!=1 ) error("should only be one argument for this action"); 68 9 : if( getPntrToArgument(0)->getRank()!=2 || getPntrToArgument(0)->hasDerivatives() ) error("input to this action should be a matrix"); 69 9 : getPntrToArgument(0)->buildDataStore(true); 70 9 : std::vector<unsigned> inshape( getPntrToArgument(0)->getShape() ); 71 9 : std::vector<unsigned> shape( 1 ); shape[0]=inshape[0]*inshape[1]; 72 9 : addValue( shape ); setNotPeriodic(); getPntrToComponent(0)->buildDataStore(); 73 9 : } 74 : 75 9 : void Flatten::calculate() { 76 9 : Value* myval = getPntrToComponent(0); unsigned ss=getPntrToArgument(0)->getShape()[1]; 77 : std::vector<double> vals; std::vector<std::pair<unsigned,unsigned> > pairs; 78 : bool symmetric=getPntrToArgument(0)->isSymmetric(); 79 9 : unsigned nedge=0; getPntrToArgument(0)->retrieveEdgeList( nedge, pairs, vals ); 80 8139 : for(unsigned l=0; l<nedge; ++l ) { 81 8130 : unsigned i=pairs[l].first, j=pairs[l].second; 82 8130 : myval->set( i*ss + j, vals[l] ); 83 8130 : if( symmetric ) myval->set( j*ss + i, vals[l] ); 84 : } 85 9 : } 86 : 87 7 : void Flatten::apply() { 88 7 : if( doNotCalculateDerivatives() || !getPntrToComponent(0)->forcesWereAdded() ) return; 89 : 90 3 : Value* myval=getPntrToComponent(0); Value* myarg=getPntrToArgument(0); 91 22 : unsigned nvals=myval->getNumberOfValues(); for(unsigned j=0; j<nvals; ++j) myarg->addForce( j, myval->getForce(j) ); 92 : } 93 : 94 : } 95 : }