Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-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/ActionRegister.h" 23 : #include "core/ActionShortcut.h" 24 : 25 : //+PLUMEDOC ANALYSIS KL_ENTROPY 26 : /* 27 : Calculate the KL entropy of a distribution 28 : 29 : \par Examples 30 : 31 : 32 : */ 33 : //+ENDPLUMEDOC 34 : 35 : namespace PLMD { 36 : namespace gridtools { 37 : 38 : class KLEntropy : public ActionShortcut { 39 : public: 40 : static void registerKeywords( Keywords& keys ); 41 : explicit KLEntropy(const ActionOptions&ao); 42 : }; 43 : 44 : PLUMED_REGISTER_ACTION(KLEntropy,"KL_ENTROPY") 45 : 46 7 : void KLEntropy::registerKeywords( Keywords& keys ) { 47 7 : ActionShortcut::registerKeywords(keys); 48 14 : keys.add("compulsory","ARG","the grid that you wish to use in the KL entropy calculation"); 49 14 : keys.add("compulsory","REFERENCE","a file containing the reference density in grid format"); 50 14 : keys.add("compulsory","VALUE","the name of the value that should be read from the grid"); 51 14 : keys.setValueDescription("scalar","the value of the KL-Entropy"); 52 21 : keys.needsAction("REFERENCE_GRID"); keys.needsAction("CUSTOM"); keys.needsAction("INTEGRATE_GRID"); 53 7 : } 54 : 55 5 : KLEntropy::KLEntropy( const ActionOptions& ao): 56 : Action(ao), 57 5 : ActionShortcut(ao) 58 : { 59 : // Reference grid object 60 15 : std::string ref_str, val_str, input_g; parse("VALUE",val_str); parse("REFERENCE",ref_str); parse("ARG",input_g); 61 10 : readInputLine( getShortcutLabel() + "_ref: REFERENCE_GRID VALUE=" + val_str + " FILE=" + ref_str ); 62 : // Compute KL divergence 63 5 : if( input_g=="") plumed_merror("could not find ARG keyword in input to KL_ENTROPY"); 64 10 : readInputLine( getShortcutLabel() + "_kl: CUSTOM ARG=" + input_g + "," + getShortcutLabel() + "_ref FUNC=y*log(y/(0.5*(x+y))) PERIODIC=NO"); 65 : // Compute integral 66 10 : readInputLine( getShortcutLabel() + ": INTEGRATE_GRID ARG=" + getShortcutLabel() + "_kl PERIODIC=NO"); 67 5 : } 68 : 69 : } 70 : }