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 [HISTOGRAM](HISTOGRAM.md) 31 : action and to convert it to a free energy surface. This transformation performed by doing: 32 : 33 : $$ 34 : F(x) = -k_B T \ln H(x) 35 : $$ 36 : 37 : The free energy calculated on a grid is output by this action and can be printed using [DUMPGRID](DUMPGRID.md) 38 : 39 : ## 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 : ```plumed 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 ARG=hA1 TEMP=300 52 : DUMPGRID ARG=ff FILE=fes.dat 53 : ``` 54 : 55 : In this example we ensure that all the reported values of the free energy are non-negative by finding the minimum in 56 : the free energy and setting reporting the free energies relative to the estimate of the value of the free energy 57 : at this minimum. 58 : 59 : ```plumed 60 : x: DISTANCE ATOMS=1,2 61 : hA1: HISTOGRAM ARG=x GRID_MIN=0.0 GRID_MAX=3.0 GRID_BIN=100 BANDWIDTH=0.1 62 : ff: CONVERT_TO_FES ARG=hA1 TEMP=300 MINTOZERO 63 : DUMPGRID ARG=ff FILE=fes.dat 64 : ``` 65 : 66 : */ 67 : //+ENDPLUMEDOC 68 : 69 : namespace PLMD { 70 : namespace gridtools { 71 : 72 : class ConvertToFES : public ActionShortcut { 73 : public: 74 : static void registerKeywords( Keywords& keys ); 75 : explicit ConvertToFES(const ActionOptions&ao); 76 : }; 77 : 78 : PLUMED_REGISTER_ACTION(ConvertToFES,"CONVERT_TO_FES") 79 : 80 22 : void ConvertToFES::registerKeywords( Keywords& keys ) { 81 22 : ActionShortcut::registerKeywords( keys ); 82 22 : keys.add("compulsory","ARG","the histogram that you would like to convert into a free energy surface"); 83 22 : keys.addDeprecatedKeyword("GRID","ARG"); 84 22 : keys.add("optional","TEMP","the temperature at which you are operating"); 85 22 : keys.addFlag("MINTOZERO",false,"use the conjugate gradient algorithm and interpolation to find the minimum in the free energy and set the value found to be the zero of energy. " 86 : "If a minimum in the free energy cannot be found use the lowest value of the free energy of the grid as the zero of energy instead"); 87 44 : keys.setValueDescription("grid","the free energy surface"); 88 22 : keys.needsAction("FIND_GRID_MINIMUM"); 89 22 : keys.needsAction("CUSTOM"); 90 22 : } 91 : 92 18 : ConvertToFES::ConvertToFES(const ActionOptions&ao): 93 : Action(ao), 94 18 : ActionShortcut(ao) { 95 18 : bool minzero=false; 96 18 : parseFlag("MINTOZERO",minzero); 97 18 : double simtemp=getkBT(); 98 18 : if( simtemp==0 ) { 99 0 : error("TEMP not set - use keyword TEMP"); 100 : } 101 : 102 18 : std::vector<std::string> argv; 103 36 : parseVector("ARG",argv); 104 18 : if( argv.size()!=1 ) { 105 0 : error("should only have one argument"); 106 : } 107 : 108 : std::string str_temp; 109 18 : Tools::convert( simtemp, str_temp ); 110 18 : std::string flab=""; 111 18 : if( minzero ) { 112 : flab="_unz"; 113 : } 114 72 : readInputLine( getShortcutLabel() + flab + ": CUSTOM ARG=" + argv[0] + " FUNC=-" + str_temp + "*log(x) PERIODIC=NO"); 115 18 : if( minzero ) { 116 4 : readInputLine( getShortcutLabel() + "_min: FIND_GRID_MINIMUM ARG=" + getShortcutLabel() + "_unz "); 117 8 : readInputLine( getShortcutLabel() + ": CUSTOM ARG=" + getShortcutLabel() + "_unz," + getShortcutLabel() + "_min.optval FUNC=x-y PERIODIC=NO"); 118 : } 119 18 : } 120 : 121 : } 122 : }