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

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2020 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             : #include "core/PlumedMain.h"
      25             : 
      26             : //+PLUMEDOC GRIDANALYSIS CONVERT_TO_FES
      27             : /*
      28             : Convert a histogram to a free energy surface.
      29             : 
      30             : This action allows you to take a free energy surface that was calculated using the \ref HISTOGRAM
      31             : action and to convert it to a free energy surface.  This transformation performed by doing:
      32             : 
      33             : \f[
      34             : F(x) = -k_B T \ln H(x)
      35             : \f]
      36             : 
      37             : The free energy calculated on a grid is output by this action and can be printed using \ref DUMPGRID
      38             : 
      39             : \par Examples
      40             : 
      41             : This is a typical example showing how CONVERT_TO_FES might be used when post processing a trajectory.
      42             : The input below calculates the free energy as a function of the distance between atom 1 and atom 2.
      43             : This is done by accumulating a histogram as a function of this distance using kernel density estimation
      44             : and the HISTOGRAM action.  All the data within this trajectory is used in the construction of this
      45             : HISTOGRAM.  Finally, once all the data has been read in, the histogram is converted to a free energy
      46             : using the formula above and the free energy is output to a file called fes.dat
      47             : 
      48             : \plumedfile
      49             : x: DISTANCE ATOMS=1,2
      50             : hA1: HISTOGRAM ARG=x GRID_MIN=0.0 GRID_MAX=3.0 GRID_BIN=100 BANDWIDTH=0.1
      51             : ff: CONVERT_TO_FES GRID=hA1 TEMP=300
      52             : DUMPGRID GRID=ff FILE=fes.dat
      53             : \endplumedfile
      54             : 
      55             : */
      56             : //+ENDPLUMEDOC
      57             : 
      58             : namespace PLMD {
      59             : namespace gridtools {
      60             : 
      61             : class ConvertToFES : public ActionShortcut {
      62             : public:
      63             :   static void registerKeywords( Keywords& keys );
      64             :   explicit ConvertToFES(const ActionOptions&ao);
      65             : };
      66             : 
      67             : PLUMED_REGISTER_ACTION(ConvertToFES,"CONVERT_TO_FES")
      68             : 
      69          17 : void ConvertToFES::registerKeywords( Keywords& keys ) {
      70          17 :   ActionShortcut::registerKeywords( keys );
      71          17 :   keys.add("optional","GRID","the histogram that you would like to convert into a free energy surface (old syntax)");
      72          17 :   keys.add("compulsory","ARG","the histogram that you would like to convert into a free energy surface");
      73          17 :   keys.add("optional","TEMP","the temperature at which you are operating");
      74          17 :   keys.addFlag("MINTOZERO",false,"set the minimum in the free energy to be equal to zero");
      75          34 :   keys.setValueDescription("grid","the free energy surface");
      76          17 :   keys.needsAction("FIND_GRID_MINIMUM");
      77          17 :   keys.needsAction("CUSTOM");
      78          17 : }
      79             : 
      80          15 : ConvertToFES::ConvertToFES(const ActionOptions&ao):
      81             :   Action(ao),
      82          15 :   ActionShortcut(ao) {
      83          15 :   bool minzero=false;
      84          15 :   parseFlag("MINTOZERO",minzero);
      85          15 :   double simtemp=getkBT();
      86          15 :   if( simtemp==0 ) {
      87           0 :     error("TEMP not set - use keyword TEMP");
      88             :   }
      89             : 
      90             :   std::vector<std::string> argv;
      91          30 :   parseVector("GRID",argv);
      92          15 :   if( argv.size()==0 ) {
      93          30 :     parseVector("ARG",argv);
      94             :   }
      95          15 :   if( argv.size()!=1 ) {
      96           0 :     error("should only have one argument");
      97             :   }
      98             : 
      99             :   std::string str_temp;
     100          15 :   Tools::convert( simtemp, str_temp );
     101          15 :   std::string flab="";
     102          15 :   if( minzero ) {
     103             :     flab="_unz";
     104             :   }
     105          30 :   readInputLine( getShortcutLabel() + flab + ": CUSTOM ARG=" + argv[0] + " FUNC=-" + str_temp + "*log(x) PERIODIC=NO");
     106          15 :   if( minzero ) {
     107           4 :     readInputLine( getShortcutLabel() + "_min: FIND_GRID_MINIMUM ARG=" + getShortcutLabel() + "_unz" );
     108           4 :     readInputLine( getShortcutLabel() + ": CUSTOM ARG=" + getShortcutLabel() + "_unz," + getShortcutLabel() + "_min.optval FUNC=x-y PERIODIC=NO");
     109             :   }
     110          15 : }
     111             : 
     112             : }
     113             : }

Generated by: LCOV version 1.16