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 ); 48 131 : keys.needsAction("RDF"); 49 131 : keys.setValueDescription("the KL-entropy that is computed from the radial distribution function"); 50 131 : keys.needsAction("INTERPOLATE_GRID"); 51 131 : keys.needsAction("INTEGRATE_GRID"); 52 131 : } 53 : 54 65 : PairEntropy::PairEntropy(const ActionOptions&ao): 55 : Action(ao), 56 65 : ActionShortcut(ao) { 57 : std::string ref_str, ref_name; 58 130 : parse("REFERENCE",ref_name); 59 65 : if( ref_name.length()>0 ) { 60 128 : ref_str = "REFERENCE=" + ref_name; 61 : } else { 62 2 : ref_name = getShortcutLabel() + "_rdf"; 63 : } 64 : // Read in the atoms and get the number of atoms that we are using 65 : std::string atom_str, group_str, natoms; 66 130 : parse("GROUP",group_str); 67 65 : if( group_str.length()>0 ) { 68 1 : atom_str="GROUP=" + group_str; 69 1 : std::vector<std::string> awords=Tools::getWords(group_str,"\t\n ,"); 70 1 : Tools::interpretRanges( awords ); 71 1 : Tools::convert( awords.size(), natoms ); 72 1 : } else { 73 : std::string groupa_str, groupb_str; 74 64 : parse("GROUPA",groupa_str); 75 64 : parse("GROUPB",groupb_str); 76 128 : atom_str="GROUPA=" + groupa_str + " GROUPB=" + groupb_str; 77 64 : std::vector<std::string> awords=Tools::getWords(groupb_str,"\t\n ,"); 78 64 : Tools::interpretRanges( awords ); 79 64 : Tools::convert( awords.size()+1, natoms ); 80 64 : } 81 : // Read in all other keywords and create the RDF object 82 : std::string maxr, nbins, dens, bw, cutoff; 83 65 : parse("MAXR",maxr); 84 65 : parse("GRID_BIN",nbins); 85 65 : parse("DENSITY",dens); 86 65 : parse("BANDWIDTH",bw); 87 130 : parse("CUTOFF",cutoff); 88 : std::string dens_str; 89 65 : if( dens.length()>0 ) { 90 0 : dens_str = " DENSITY=" + dens; 91 : } 92 130 : readInputLine( getShortcutLabel() + "_rdf: RDF " + atom_str + " CUTOFF=" + cutoff + " GRID_BIN=" + nbins + " MAXR=" + maxr + dens_str + " BANDWIDTH=" + bw + " " + ref_str); 93 : // 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) 94 130 : readInputLine( getShortcutLabel() + "_conv_t1: CUSTOM ARG=" + getShortcutLabel() + "_rdf," + ref_name + "_x2 FUNC=x*y*log(x) PERIODIC=NO"); 95 130 : readInputLine( getShortcutLabel() + "_conv_t2: CUSTOM ARG=" + getShortcutLabel() + "_rdf," + ref_name + "_x2 FUNC=(1-x)*y PERIODIC=NO"); 96 130 : readInputLine( getShortcutLabel() + "_conv: CUSTOM ARG=" + getShortcutLabel() + "_conv_t1," + getShortcutLabel() + "_conv_t2 FUNC=x+y PERIODIC=NO"); 97 : // Now integrate using trapezium rule 98 130 : readInputLine( getShortcutLabel() + "_midp: INTERPOLATE_GRID ARG=" + getShortcutLabel() + "_conv INTERPOLATION_TYPE=linear MIDPOINTS"); // First interpolate onto midpoints 99 130 : readInputLine( getShortcutLabel() + "_int: INTEGRATE_GRID ARG=" + getShortcutLabel() + "_midp PERIODIC=NO"); // And then integrate 100 : // And multiply by final normalizing constant 101 : std::string norm_str; 102 65 : if( dens.length()>0 ) { 103 0 : norm_str = " FUNC=-2*pi*x*" + dens; 104 : } else { 105 130 : norm_str = "," + ref_name + "_vol FUNC=-(2*pi*x/y)*" + natoms; 106 : } 107 130 : readInputLine( getShortcutLabel() + ": CUSTOM PERIODIC=NO ARG=" + getShortcutLabel() + "_int" + norm_str ); 108 65 : } 109 : 110 : } 111 : }