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 "Sum.h" 23 : #include "FunctionShortcut.h" 24 : #include "FunctionOfScalar.h" 25 : #include "FunctionOfVector.h" 26 : #include "FunctionOfMatrix.h" 27 : #include "core/ActionRegister.h" 28 : 29 : //+PLUMEDOC COLVAR SUM 30 : /* 31 : Calculate the sum of the arguments 32 : 33 : \par Examples 34 : 35 : */ 36 : //+ENDPLUMEDOC 37 : 38 : //+PLUMEDOC COLVAR SUM_VECTOR 39 : /* 40 : Calculate the sum of the elements in a vector 41 : 42 : \par Examples 43 : 44 : */ 45 : //+ENDPLUMEDOC 46 : 47 : //+PLUMEDOC COLVAR SUM_SCALAR 48 : /* 49 : Calculate the SUM of the set of input scalars 50 : 51 : \par Examples 52 : 53 : */ 54 : //+ENDPLUMEDOC 55 : 56 : //+PLUMEDOC COLVAR MEAN 57 : /* 58 : Calculate the arithmetic mean of the elements in a vector 59 : 60 : \par Examples 61 : 62 : */ 63 : //+ENDPLUMEDOC 64 : 65 : //+PLUMEDOC COLVAR MEAN_SCALAR 66 : /* 67 : Calculate the arithmetic mean of the set of input scalars 68 : 69 : \par Examples 70 : 71 : */ 72 : //+ENDPLUMEDOC 73 : 74 : //+PLUMEDOC COLVAR MEAN_VECTOR 75 : /* 76 : Calculate the arithmetic mean of the elements in a vector 77 : 78 : \par Examples 79 : 80 : */ 81 : //+ENDPLUMEDOC 82 : 83 : //+PLUMEDOC COLVAR SUM_MATRIX 84 : /* 85 : Sum all the elements in a matrix 86 : 87 : \par Examples 88 : 89 : */ 90 : //+ENDPLUMEDOC 91 : 92 : 93 : namespace PLMD { 94 : namespace function { 95 : 96 : typedef FunctionShortcut<Sum> SumShortcut; 97 : PLUMED_REGISTER_ACTION(SumShortcut,"SUM") 98 : PLUMED_REGISTER_ACTION(SumShortcut,"MEAN") 99 : typedef FunctionOfScalar<Sum> ScalarSum; 100 : PLUMED_REGISTER_ACTION(ScalarSum,"SUM_SCALAR") 101 : PLUMED_REGISTER_ACTION(ScalarSum,"MEAN_SCALAR") 102 : typedef FunctionOfVector<Sum> VectorSum; 103 : PLUMED_REGISTER_ACTION(VectorSum,"SUM_VECTOR") 104 : PLUMED_REGISTER_ACTION(VectorSum,"MEAN_VECTOR") 105 : typedef FunctionOfMatrix<Sum> MatrixSum; 106 : PLUMED_REGISTER_ACTION(MatrixSum,"SUM_MATRIX") 107 : 108 3294 : void Sum::registerKeywords( Keywords& keys ) { 109 9882 : keys.use("PERIODIC"); keys.setValueDescription("scalar","the sum"); 110 3294 : } 111 : 112 908 : void Sum::read( ActionWithArguments* action ) { 113 908 : if( action->getNumberOfArguments()!=1 ) action->error("should only be one argument to sum actions"); 114 908 : } 115 : 116 998 : void Sum::setPrefactor( ActionWithArguments* action, const double pref ) { 117 998 : if(action->getName().find("MEAN")!=std::string::npos) prefactor = pref / (action->getPntrToArgument(0))->getNumberOfValues(); 118 821 : else prefactor = pref; 119 998 : } 120 : 121 25622 : bool Sum::zeroRank() const { 122 25622 : return true; 123 : } 124 : 125 1901194 : void Sum::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const { 126 1901194 : vals[0]=prefactor*args[0]; derivatives(0,0)=prefactor; 127 1901194 : } 128 : 129 : } 130 : }