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/ActionSetup.h" 24 : #include "core/ActionRegister.h" 25 : 26 : //+PLUMEDOC MCOLVAR INVERT_MATRIX 27 : /* 28 : Calculate the inverse of the input matrix 29 : 30 : \par Examples 31 : 32 : */ 33 : //+ENDPLUMEDOC 34 : 35 : namespace PLMD { 36 : namespace matrixtools { 37 : 38 : class InvertMatrix : public MatrixOperationBase { 39 : private: 40 : bool input_is_constant; 41 : Matrix<double> mymatrix; 42 : Matrix<double> inverse; 43 : public: 44 : static void registerKeywords( Keywords& keys ); 45 : /// Constructor 46 : explicit InvertMatrix(const ActionOptions&); 47 : /// 48 6 : unsigned getNumberOfDerivatives() override { 49 6 : return 0; 50 : } 51 : /// Do the calculation 52 : void calculate() override; 53 : /// 54 : void apply() override; 55 0 : double getForceOnMatrixElement( const unsigned& jrow, const unsigned& krow ) const override { 56 0 : plumed_error(); 57 : } 58 : }; 59 : 60 : PLUMED_REGISTER_ACTION(InvertMatrix,"INVERT_MATRIX") 61 : 62 18 : void InvertMatrix::registerKeywords( Keywords& keys ) { 63 18 : MatrixOperationBase::registerKeywords( keys ); 64 18 : keys.setValueDescription("the inverse of the input matrix"); 65 18 : } 66 : 67 9 : InvertMatrix::InvertMatrix(const ActionOptions& ao): 68 : Action(ao), 69 : MatrixOperationBase(ao), 70 9 : input_is_constant(false) { 71 9 : if( getPntrToArgument(0)->getShape()[0]!=getPntrToArgument(0)->getShape()[1] ) { 72 0 : error("input matrix should be square"); 73 : } 74 : 75 9 : ActionSetup* as = dynamic_cast<ActionSetup*>( getPntrToArgument(0)->getPntrToAction() ); 76 9 : if(as) { 77 0 : input_is_constant=true; 78 : } 79 : 80 9 : std::vector<unsigned> shape(2); 81 9 : shape[0]=shape[1]=getPntrToArgument(0)->getShape()[0]; 82 9 : addValue( shape ); 83 9 : setNotPeriodic(); 84 9 : getPntrToComponent(0)->buildDataStore(); 85 9 : getPntrToComponent(0)->reshapeMatrixStore( shape[1] ); 86 9 : mymatrix.resize( shape[0], shape[1] ); 87 9 : inverse.resize( shape[0], shape[1] ); 88 9 : } 89 : 90 9 : void InvertMatrix::calculate() { 91 : // Retrieve the matrix from input 92 9 : retrieveFullMatrix( mymatrix ); 93 : // Now invert the matrix 94 9 : Invert( mymatrix, inverse ); 95 : // And set the inverse 96 : unsigned k = 0; 97 9 : Value* myval=getPntrToComponent(0); 98 30 : for(unsigned i=0; i<mymatrix.nrows(); ++i) { 99 78 : for(unsigned j=0; j<mymatrix.ncols(); ++j) { 100 57 : myval->set( k, inverse(i,j) ); 101 57 : k++; 102 : } 103 : } 104 : 105 9 : if( !doNotCalculateDerivatives() && !input_is_constant ) { 106 0 : error("derivatives of inverse matrix have not been implemented"); 107 : } 108 9 : } 109 : 110 0 : void InvertMatrix::apply() { 111 0 : if( doNotCalculateDerivatives() || input_is_constant ) { 112 0 : return; 113 : } 114 0 : error("derivatives of inverse matrix have not been implemented"); 115 : } 116 : 117 : } 118 : }