LCOV - code coverage report
Current view: top level - gridtools - PairEntropy.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 46 48 95.8 %
Date: 2025-04-08 21:11:17 Functions: 2 3 66.7 %

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

Generated by: LCOV version 1.16