Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2017 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 "RDF.h" 23 : #include "core/ActionRegister.h" 24 : #include "core/ActionShortcut.h" 25 : 26 : //+PLUMEDOC MCOLVAR PAIRENTROPY 27 : /* 28 : Calculate the KL Entropy from the radial distribution function 29 : 30 : \par Examples 31 : 32 : */ 33 : //+ENDPLUMEDOC 34 : 35 : namespace PLMD { 36 : namespace gridtools { 37 : 38 : class PairEntropy : public ActionShortcut { 39 : public: 40 : static void registerKeywords( Keywords& keys ); 41 : explicit PairEntropy(const ActionOptions&ao); 42 : }; 43 : 44 : PLUMED_REGISTER_ACTION(PairEntropy,"PAIRENTROPY") 45 : 46 131 : void PairEntropy::registerKeywords( Keywords& keys ) { 47 131 : RDF::registerKeywords( keys ); keys.needsAction("RDF"); 48 262 : keys.setValueDescription("scalar","the KL-entropy that is computed from the radial distribution function"); 49 262 : keys.needsAction("INTERPOLATE_GRID"); keys.needsAction("INTEGRATE_GRID"); 50 131 : } 51 : 52 65 : PairEntropy::PairEntropy(const ActionOptions&ao): 53 : Action(ao), 54 65 : ActionShortcut(ao) 55 : { 56 130 : std::string ref_str, ref_name; parse("REFERENCE",ref_name); 57 130 : if( ref_name.length()>0 ) ref_str = "REFERENCE=" + ref_name; else ref_name = getShortcutLabel() + "_rdf"; 58 : // Read in the atoms and get the number of atoms that we are using 59 130 : std::string atom_str, group_str, natoms; parse("GROUP",group_str); 60 65 : if( group_str.length()>0 ) { 61 1 : atom_str="GROUP=" + group_str; 62 1 : std::vector<std::string> awords=Tools::getWords(group_str,"\t\n ,"); 63 1 : Tools::interpretRanges( awords ); Tools::convert( awords.size(), natoms ); 64 1 : } else { 65 : std::string groupa_str, groupb_str; 66 128 : parse("GROUPA",groupa_str); parse("GROUPB",groupb_str); 67 128 : atom_str="GROUPA=" + groupa_str + " GROUPB=" + groupb_str; 68 64 : std::vector<std::string> awords=Tools::getWords(groupb_str,"\t\n ,"); 69 64 : Tools::interpretRanges( awords ); Tools::convert( awords.size()+1, natoms ); 70 64 : } 71 : // Read in all other keywords and create the RDF object 72 390 : std::string maxr, nbins, dens, bw, cutoff; parse("MAXR",maxr); parse("GRID_BIN",nbins); parse("DENSITY",dens); parse("BANDWIDTH",bw); parse("CUTOFF",cutoff); 73 65 : std::string dens_str; if( dens.length()>0 ) dens_str = " DENSITY=" + dens; 74 130 : readInputLine( getShortcutLabel() + "_rdf: RDF " + atom_str + " CUTOFF=" + cutoff + " GRID_BIN=" + nbins + " MAXR=" + maxr + dens_str + " BANDWIDTH=" + bw + " " + ref_str); 75 : // And compute the two functions we are integrating (we use two matheval objects here and sum them in order to avoid nans from taking logarithms of zero) 76 130 : readInputLine( getShortcutLabel() + "_conv_t1: CUSTOM ARG=" + getShortcutLabel() + "_rdf," + ref_name + "_x2 FUNC=x*y*log(x) PERIODIC=NO"); 77 130 : readInputLine( getShortcutLabel() + "_conv_t2: CUSTOM ARG=" + getShortcutLabel() + "_rdf," + ref_name + "_x2 FUNC=(1-x)*y PERIODIC=NO"); 78 130 : readInputLine( getShortcutLabel() + "_conv: CUSTOM ARG=" + getShortcutLabel() + "_conv_t1," + getShortcutLabel() + "_conv_t2 FUNC=x+y PERIODIC=NO"); 79 : // Now integrate using trapezium rule 80 130 : readInputLine( getShortcutLabel() + "_midp: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_conv INTERPOLATION_TYPE=linear MIDPOINTS"); // First interpolate onto midpoints 81 130 : readInputLine( getShortcutLabel() + "_int: INTEGRATE_GRID ARG=" + getShortcutLabel() + "_midp PERIODIC=NO"); // And then integrate 82 : // And multiply by final normalizing constant 83 : std::string norm_str; 84 65 : if( dens.length()>0 ) norm_str = " FUNC=-2*pi*x*" + dens; 85 130 : else norm_str = "," + ref_name + "_vol FUNC=-(2*pi*x/y)*" + natoms; 86 130 : readInputLine( getShortcutLabel() + ": CUSTOM PERIODIC=NO ARG=" + getShortcutLabel() + "_int" + norm_str ); 87 65 : } 88 : 89 : } 90 : }