Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2023 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 : 24 : namespace PLMD { 25 : namespace matrixtools { 26 : 27 363 : void MatrixOperationBase::registerKeywords( Keywords& keys ) { 28 363 : Action::registerKeywords( keys ); 29 363 : ActionWithArguments::registerKeywords( keys ); 30 363 : ActionWithValue::registerKeywords( keys ); 31 726 : keys.addInputKeyword("compulsory","ARG","matrix","the input matrix"); 32 726 : keys.remove("NUMERICAL_DERIVATIVES"); 33 363 : keys.add("optional","MATRIX","the input matrix (can use ARG instead)"); 34 363 : } 35 : 36 211 : MatrixOperationBase::MatrixOperationBase(const ActionOptions&ao): 37 : Action(ao), 38 : ActionWithArguments(ao), 39 211 : ActionWithValue(ao) { 40 211 : if( getNumberOfArguments()==0 ) { 41 : std::vector<Value*> args; 42 0 : parseArgumentList("MATRIX",args); 43 0 : requestArguments(args); 44 : } 45 211 : if( getNumberOfArguments()!=1 ) { 46 0 : error("should only be one argument to this action"); 47 : } 48 211 : if( getPntrToArgument(0)->getRank()!=2 || getPntrToArgument(0)->hasDerivatives() ) { 49 17 : if( getName()=="TRANSPOSE" ) { 50 17 : if (getPntrToArgument(0)->getRank()!=1 || getPntrToArgument(0)->hasDerivatives() ) { 51 0 : error("input to this argument should be a matrix or vector"); 52 : } 53 : } else { 54 0 : error("input to this argument should be a matrix"); 55 : } 56 : } 57 211 : getPntrToArgument(0)->buildDataStore(); 58 211 : } 59 : 60 131 : void MatrixOperationBase::retrieveFullMatrix( Matrix<double>& mymatrix ) { 61 131 : if( mymatrix.nrows()!=getPntrToArgument(0)->getShape()[0] || mymatrix.nrows()!=getPntrToArgument(0)->getShape()[1] ) { 62 0 : mymatrix.resize( getPntrToArgument(0)->getShape()[0], getPntrToArgument(0)->getShape()[1] ); 63 : } 64 : unsigned nedge; 65 131 : getPntrToArgument(0)->retrieveEdgeList( nedge, pairs, vals ); 66 : mymatrix=0; 67 : bool symmetric = getPntrToArgument(0)->isSymmetric(); 68 373270 : for(unsigned i=0; i<nedge; ++i ) { 69 373139 : mymatrix( pairs[i].first, pairs[i].second ) = vals[i]; 70 373139 : if( symmetric ) { 71 714 : mymatrix( pairs[i].second, pairs[i].first ) = vals[i]; 72 : } 73 : } 74 131 : } 75 : 76 : 77 1448 : void MatrixOperationBase::apply() { 78 1448 : if( doNotCalculateDerivatives() ) { 79 : return; 80 : } 81 : 82 : bool forces=false; 83 1546 : for(int i=0; i<getNumberOfComponents(); ++i) { 84 1546 : if( getPntrToComponent(i)->forcesWereAdded() ) { 85 : forces=true; 86 : break; 87 : } 88 : } 89 1444 : if( !forces ) { 90 : return; 91 : } 92 : 93 : Value* mat = getPntrToArgument(0); 94 : unsigned ncols=mat->getNumberOfColumns(); 95 147286 : for(unsigned i=0; i<mat->getShape()[0]; ++i) { 96 : unsigned ncol = mat->getRowLength(i); 97 4282909 : for(unsigned j=0; j<ncol; ++j) { 98 4137067 : mat->addForce( i*ncols+j, getForceOnMatrixElement( i, mat->getRowIndex(i,j) ), false ); 99 : } 100 : } 101 : } 102 : 103 : 104 : } 105 : }