Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2015-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 "core/ActionShortcut.h" 23 : #include "core/ActionRegister.h" 24 : 25 : //+PLUMEDOC ANALYSIS LOGSUMEXP 26 : /* 27 : This action takes the exponential of a vector of logarithms and divides each element of the vector by the sum of the exponentials. 28 : 29 : The log-exp-sum trick is used here 30 : 31 : \par Examples 32 : 33 : */ 34 : //+ENDPLUMEDOC 35 : 36 : namespace PLMD { 37 : namespace landmarks { 38 : 39 : class LogSumExp : public ActionShortcut { 40 : private: 41 : std::string fixArgumentName( const std::string& argin ); 42 : public: 43 : static void registerKeywords( Keywords& keys ); 44 : explicit LogSumExp( const ActionOptions& ao ); 45 : }; 46 : 47 : PLUMED_REGISTER_ACTION(LogSumExp,"LOGSUMEXP") 48 : 49 20 : void LogSumExp::registerKeywords( Keywords& keys ) { 50 20 : ActionShortcut::registerKeywords( keys ); 51 40 : keys.add("compulsory","ARG","the vector of logweights that you would like to normalise using the logsumexp trick"); 52 40 : keys.setValueDescription("vector","the logarithms of the input weights logweights that are computed with the log-sum weights formula"); 53 60 : keys.needsAction("HIGHEST"); keys.needsAction("CUSTOM"); keys.needsAction("SUM"); 54 20 : } 55 : 56 : 57 9 : LogSumExp::LogSumExp( const ActionOptions& ao ): 58 : Action(ao), 59 9 : ActionShortcut(ao) 60 : { 61 : // Find the argument name 62 9 : std::string argn; parse("ARG",argn); 63 : // Find the maximum weight 64 18 : readInputLine( getShortcutLabel() + "_maxlogweight: HIGHEST ARG=" + argn ); 65 18 : readInputLine( getShortcutLabel() + "_maxweight: CUSTOM ARG=" + getShortcutLabel() + "_maxlogweight FUNC=exp(x) PERIODIC=NO"); 66 : // Calculate the maximum 67 18 : readInputLine( getShortcutLabel() + "_shiftw: CUSTOM ARG=" + argn + "," + getShortcutLabel() + "_maxlogweight FUNC=exp(x-y) PERIODIC=NO"); 68 : // compute the sum of all the exponentials 69 18 : readInputLine( getShortcutLabel() + "_sumw: SUM ARG=" + getShortcutLabel() + "_shiftw PERIODIC=NO"); 70 : // and the logsum 71 18 : readInputLine( getShortcutLabel() + "_logsum: CUSTOM ARG=" + getShortcutLabel() + "_sumw," + getShortcutLabel() + "_maxlogweight FUNC=y+log(x) PERIODIC=NO"); 72 : // And the final weights 73 18 : readInputLine( getShortcutLabel() + ": CUSTOM ARG=" + argn + "," + getShortcutLabel() + "_logsum FUNC=exp(x-y) PERIODIC=NO"); 74 9 : } 75 : 76 : } 77 : }