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 : If you has performed a simulation with a time-independent bias, $V(x)$, you can recover the unbiased distribution from the trajectory 30 : by constructing a histogram in which the $i$th frame of the trajectory is given weight of: 31 : 32 : $$ 33 : w_i = \frac{e^{\beta V(x_i)}}{\sum_j e^{\beta V(x_j)}} 34 : $$ 35 : 36 : where $x_i$ is used to indicate the coordinates for frame $i$ and $\beta$ is the inverse temperature in units of energy. If the bias 37 : is large then it is easy for $e^{\beta V(x_i)}$ to overflow. This action thus allows you to use the following trick is to compute the $w_i$ 38 : values in the above expression: 39 : 40 : $$ 41 : w_i = e^{\beta(V(x_i) - c} \qquad \textrm{where} \qquad c = \beta V_\textrm{max} + \log\left[ \sum_j e^{\beta V(x_j) - \beta V_\textrm{max}} \right] 42 : $$ 43 : 44 : In this expression $V_\textrm{max}$ is the maximum value the bias took during the simulation. 45 : 46 : The following example shows how you can write a PLUMED input that exploits this trick. 47 : 48 : ```plumed 49 : # Calculate some CVs for later analysis 50 : t1: TORSION ATOMS=1,2,3,4 51 : t2: TORSION ATOMS=5,6,7,8 52 : t3: TORSION ATOMS=9,10,11,12 53 : 54 : # This computes the bias potential 55 : r: RESTRAINT ARG=t1 AT=pi/2 KAPPA=10 56 : # This calculates the instantaneous reweighting weight 57 : # given the bias potential that is acting upon the system 58 : bw: REWEIGHT_BIAS TEMP=300 59 : 60 : # This collects our data for later analysis and the reweighting weights 61 : cc: COLLECT_FRAMES ARG=t1,t2,t3 LOGWEIGHTS=bw 62 : 63 : # And this determines the final vector of weights that should be used for histograms and so on. 64 : weights: LOGSUMEXP ARG=cc_logweights 65 : 66 : # This outputs the time series of reweighting weights to a file 67 : DUMPVECTOR ARG=weights FILE=weights 68 : ``` 69 : 70 : */ 71 : //+ENDPLUMEDOC 72 : 73 : namespace PLMD { 74 : namespace landmarks { 75 : 76 : class LogSumExp : public ActionShortcut { 77 : private: 78 : std::string fixArgumentName( const std::string& argin ); 79 : public: 80 : static void registerKeywords( Keywords& keys ); 81 : explicit LogSumExp( const ActionOptions& ao ); 82 : }; 83 : 84 : PLUMED_REGISTER_ACTION(LogSumExp,"LOGSUMEXP") 85 : 86 20 : void LogSumExp::registerKeywords( Keywords& keys ) { 87 20 : ActionShortcut::registerKeywords( keys ); 88 20 : keys.add("compulsory","ARG","the vector of logweights that you would like to normalise using the logsumexp trick"); 89 40 : keys.setValueDescription("vector","the logarithms of the input weights logweights that are computed with the log-sum weights formula"); 90 20 : keys.needsAction("HIGHEST"); 91 20 : keys.needsAction("CUSTOM"); 92 20 : keys.needsAction("SUM"); 93 20 : } 94 : 95 : 96 9 : LogSumExp::LogSumExp( const ActionOptions& ao ): 97 : Action(ao), 98 9 : ActionShortcut(ao) { 99 : // Find the argument name 100 : std::string argn; 101 9 : parse("ARG",argn); 102 : // Find the maximum weight 103 18 : readInputLine( getShortcutLabel() + "_maxlogweight: HIGHEST ARG=" + argn ); 104 : // Calculate the maximum 105 18 : readInputLine( getShortcutLabel() + "_shiftw: CUSTOM ARG=" + argn + "," + getShortcutLabel() + "_maxlogweight FUNC=exp(x-y) PERIODIC=NO"); 106 : // compute the sum of all the exponentials 107 18 : readInputLine( getShortcutLabel() + "_sumw: SUM ARG=" + getShortcutLabel() + "_shiftw PERIODIC=NO"); 108 : // and the logsum 109 18 : readInputLine( getShortcutLabel() + "_logsum: CUSTOM ARG=" + getShortcutLabel() + "_sumw," + getShortcutLabel() + "_maxlogweight FUNC=y+log(x) PERIODIC=NO"); 110 : // And the final weights 111 18 : readInputLine( getShortcutLabel() + ": CUSTOM ARG=" + argn + "," + getShortcutLabel() + "_logsum FUNC=exp(x-y) PERIODIC=NO"); 112 9 : } 113 : 114 : } 115 : }