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 { 48 10 : return 0; 49 : } 50 : /// Do the calculation 51 : void calculate() override; 52 : /// 53 : void apply() override; 54 : }; 55 : 56 : PLUMED_REGISTER_ACTION(Flatten,"FLATTEN") 57 : 58 20 : void Flatten::registerKeywords( Keywords& keys ) { 59 20 : Action::registerKeywords( keys ); 60 20 : ActionWithValue::registerKeywords( keys ); 61 20 : ActionWithArguments::registerKeywords( keys ); 62 20 : keys.use("ARG"); 63 20 : keys.setValueDescription("a vector containing all the elements of the input matrix"); 64 20 : } 65 : 66 9 : Flatten::Flatten(const ActionOptions& ao): 67 : Action(ao), 68 : ActionWithValue(ao), 69 9 : ActionWithArguments(ao) { 70 9 : if( getNumberOfArguments()!=1 ) { 71 0 : error("should only be one argument for this action"); 72 : } 73 9 : if( getPntrToArgument(0)->getRank()!=2 || getPntrToArgument(0)->hasDerivatives() ) { 74 0 : error("input to this action should be a matrix"); 75 : } 76 9 : getPntrToArgument(0)->buildDataStore(true); 77 9 : std::vector<unsigned> inshape( getPntrToArgument(0)->getShape() ); 78 9 : std::vector<unsigned> shape( 1 ); 79 9 : shape[0]=inshape[0]*inshape[1]; 80 9 : addValue( shape ); 81 9 : setNotPeriodic(); 82 9 : getPntrToComponent(0)->buildDataStore(); 83 9 : } 84 : 85 9 : void Flatten::calculate() { 86 9 : Value* myval = getPntrToComponent(0); 87 9 : unsigned ss=getPntrToArgument(0)->getShape()[1]; 88 : std::vector<double> vals; 89 : std::vector<std::pair<unsigned,unsigned> > pairs; 90 : bool symmetric=getPntrToArgument(0)->isSymmetric(); 91 9 : unsigned nedge=0; 92 9 : getPntrToArgument(0)->retrieveEdgeList( nedge, pairs, vals ); 93 8139 : for(unsigned l=0; l<nedge; ++l ) { 94 8130 : unsigned i=pairs[l].first, j=pairs[l].second; 95 8130 : myval->set( i*ss + j, vals[l] ); 96 8130 : if( symmetric ) { 97 0 : myval->set( j*ss + i, vals[l] ); 98 : } 99 : } 100 9 : } 101 : 102 7 : void Flatten::apply() { 103 7 : if( doNotCalculateDerivatives() || !getPntrToComponent(0)->forcesWereAdded() ) { 104 4 : return; 105 : } 106 : 107 3 : Value* myval=getPntrToComponent(0); 108 : Value* myarg=getPntrToArgument(0); 109 3 : unsigned nvals=myval->getNumberOfValues(); 110 22 : for(unsigned j=0; j<nvals; ++j) { 111 19 : myarg->addForce( j, myval->getForce(j) ); 112 : } 113 : } 114 : 115 : } 116 : }