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 "MatrixOperationBase.h" 23 : #include "core/ActionRegister.h" 24 : 25 : //+PLUMEDOC MCOLVAR TRANSPOSE 26 : /* 27 : Calculate the transpose of a matrix 28 : 29 : \par Examples 30 : 31 : */ 32 : //+ENDPLUMEDOC 33 : 34 : namespace PLMD { 35 : namespace matrixtools { 36 : 37 : class TransposeMatrix : public MatrixOperationBase { 38 : public: 39 : static void registerKeywords( Keywords& keys ); 40 : /// Constructor 41 : explicit TransposeMatrix(const ActionOptions&); 42 : /// 43 256 : unsigned getNumberOfDerivatives() override { return 0; } 44 : /// 45 : void prepare() override ; 46 : /// 47 : void calculate() override ; 48 : /// 49 : void apply() override ; 50 : /// 51 : double getForceOnMatrixElement( const unsigned& jrow, const unsigned& krow ) const override; 52 : }; 53 : 54 : PLUMED_REGISTER_ACTION(TransposeMatrix,"TRANSPOSE") 55 : 56 279 : void TransposeMatrix::registerKeywords( Keywords& keys ) { 57 279 : MatrixOperationBase::registerKeywords( keys ); 58 279 : keys.setValueDescription("the transpose of the input matrix"); 59 279 : } 60 : 61 162 : TransposeMatrix::TransposeMatrix(const ActionOptions& ao): 62 : Action(ao), 63 162 : MatrixOperationBase(ao) 64 : { 65 162 : if( getPntrToArgument(0)->isSymmetric() ) error("input matrix is symmetric. Transposing will achieve nothing!"); 66 : std::vector<unsigned> shape; 67 162 : if( getPntrToArgument(0)->getRank()==0 ) error("transposing a scalar?"); 68 162 : else if( getPntrToArgument(0)->getRank()==1 ) { shape.resize(2); shape[0]=1; shape[1]=getPntrToArgument(0)->getShape()[0]; } 69 145 : else if( getPntrToArgument(0)->getShape()[0]==1 ) { shape.resize(1); shape[0] = getPntrToArgument(0)->getShape()[1]; } 70 84 : else { shape.resize(2); shape[0]=getPntrToArgument(0)->getShape()[1]; shape[1]=getPntrToArgument(0)->getShape()[0]; } 71 162 : addValue( shape ); setNotPeriodic(); getPntrToComponent(0)->buildDataStore(); 72 162 : if( shape.size()==2 ) getPntrToComponent(0)->reshapeMatrixStore( shape[1] ); 73 162 : } 74 : 75 4821 : void TransposeMatrix::prepare() { 76 4821 : Value* myval = getPntrToComponent(0); Value* myarg = getPntrToArgument(0); 77 4821 : if( myarg->getRank()==1 ) { 78 586 : if( myval->getShape()[0]!=1 || myval->getShape()[1]!=myarg->getShape()[0] ) { 79 6 : std::vector<unsigned> shape(2); shape[0] = 1; shape[1] = myarg->getShape()[0]; 80 6 : myval->setShape( shape ); myval->reshapeMatrixStore( shape[1] ); 81 : } 82 4235 : } else if( myarg->getShape()[0]==1 ) { 83 2392 : if( myval->getShape()[0]!=myarg->getShape()[1] ) { std::vector<unsigned> shape(1); shape[0] = myarg->getShape()[1]; myval->setShape( shape ); } 84 1843 : } else if( myarg->getShape()[0]!=myval->getShape()[1] || myarg->getShape()[1]!=myval->getShape()[0] ) { 85 19 : std::vector<unsigned> shape(2); shape[0] = myarg->getShape()[1]; shape[1] = myarg->getShape()[0]; 86 19 : myval->setShape( shape ); myval->reshapeMatrixStore( shape[1] ); 87 : } 88 4821 : } 89 : 90 4225 : void TransposeMatrix::calculate() { 91 : // Retrieve the non-zero pairs 92 4225 : Value* myarg=getPntrToArgument(0); Value* myval=getPntrToComponent(0); 93 4225 : if( myarg->getRank()<=1 || myval->getRank()==1 ) { 94 2389 : if( myarg->getRank()<=1 && myval->getShape()[1]!=myarg->getShape()[0] ) { 95 0 : std::vector<unsigned> shape( 2 ); shape[0] = 1; shape[1] = myarg->getShape()[0]; 96 0 : myval->setShape( shape ); myval->reshapeMatrixStore( shape[1] ); 97 2389 : } else if( myval->getRank()==1 && myval->getShape()[0]!=myarg->getShape()[1] ) { 98 0 : std::vector<unsigned> shape( 1 ); shape[0] = myarg->getShape()[1]; 99 0 : myval->setShape( shape ); 100 : } 101 2389 : unsigned nv=myarg->getNumberOfValues(); 102 49015 : for(unsigned i=0; i<nv; ++i) myval->set( i, myarg->get(i) ); 103 : } else { 104 1836 : if( myarg->getShape()[0]!=myval->getShape()[1] || myarg->getShape()[1]!=myval->getShape()[0] ) { 105 0 : std::vector<unsigned> shape( 2 ); shape[0] = myarg->getShape()[1]; shape[1] = myarg->getShape()[0]; 106 0 : myval->setShape( shape ); myval->reshapeMatrixStore( shape[1] ); 107 : } 108 : std::vector<double> vals; std::vector<std::pair<unsigned,unsigned> > pairs; 109 1836 : std::vector<unsigned> shape( myval->getShape() ); unsigned nedge=0; myarg->retrieveEdgeList( nedge, pairs, vals ); 110 2758860 : for(unsigned i=0; i<nedge; ++i) myval->set( pairs[i].second*shape[1] + pairs[i].first, vals[i] ); 111 : } 112 4225 : } 113 : 114 4144 : void TransposeMatrix::apply() { 115 4144 : if( doNotCalculateDerivatives() ) return; 116 : 117 : // Apply force on the matrix 118 1930 : if( getPntrToComponent(0)->forcesWereAdded() ) { 119 1930 : Value* myarg=getPntrToArgument(0); Value* myval=getPntrToComponent(0); 120 1930 : if( myarg->getRank()<=1 || myval->getRank()==1 ) { 121 588 : unsigned nv=myarg->getNumberOfValues(); 122 2408 : for(unsigned i=0; i<nv; ++i) myarg->addForce( i, myval->getForce(i) ); 123 1342 : } else MatrixOperationBase::apply(); 124 : } 125 : } 126 : 127 4124572 : double TransposeMatrix::getForceOnMatrixElement( const unsigned& jrow, const unsigned& kcol ) const { 128 4124572 : return getConstPntrToComponent(0)->getForce(kcol*getConstPntrToComponent(0)->getShape()[1]+jrow); 129 : } 130 : 131 : 132 : } 133 : }