LCOV - code coverage report
Current view: top level - ves - TD_WellTempered.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 29 32 90.6 %
Date: 2024-10-11 08:09:47 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-2021 The VES code team
       3             :    (see the PEOPLE-VES file at the root of this folder for a list of names)
       4             : 
       5             :    See http://www.ves-code.org for more information.
       6             : 
       7             :    This file is part of VES code module.
       8             : 
       9             :    The VES code module 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             :    The VES code module 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 the VES code module.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : 
      23             : #include "TargetDistribution.h"
      24             : #include "GridIntegrationWeights.h"
      25             : 
      26             : #include "core/ActionRegister.h"
      27             : #include "tools/Grid.h"
      28             : #include "core/PlumedMain.h"
      29             : 
      30             : 
      31             : 
      32             : namespace PLMD {
      33             : namespace ves {
      34             : 
      35             : //+PLUMEDOC VES_TARGETDIST TD_WELLTEMPERED
      36             : /*
      37             : Well-tempered target distribution (dynamic).
      38             : 
      39             : Use as a target distribution the well-tempered distribution \cite Barducci:2008
      40             : given by
      41             : \f[
      42             : p(\mathbf{s}) =
      43             : \frac{e^{-(\beta/\gamma) F(\mathbf{s})}}
      44             : {\int d\mathbf{s}\, e^{-(\beta/\gamma) F(\mathbf{s})}} =
      45             : \frac{[P_{0}(\mathbf{s})]^{1/\gamma}}
      46             : {\int d\mathbf{s}\, [P_{0}(\mathbf{s})]^{1/\gamma}}
      47             : \f]
      48             : where \f$\gamma\f$ is a so-called bias factor and \f$P_{0}(\mathbf{s})\f$ is the
      49             : unbiased canonical distribution of the CVs. This target distribution thus
      50             : corresponds to a biased ensemble where, as compared to the unbiased one,
      51             : the probability peaks have been broaden and the fluctuations of the CVs are
      52             : enhanced.
      53             : The value of the bias factor \f$\gamma\f$ determines by how much the fluctuations
      54             : are enhanced.
      55             : 
      56             : The well-tempered distribution can be view as sampling on
      57             : an effective free energy surface \f$\tilde{F}(\mathbf{s}) = (1/\gamma) F(\mathbf{s})\f$
      58             : which has largely the same metastable states as the original \f$F(\mathbf{s})\f$
      59             : but with barriers that have been reduced by a factor of \f$\gamma\f$.
      60             : Generally one should use a value of \f$\gamma\f$ that results in
      61             : effective barriers on the order of few \f$k_{\mathrm{B}}T\f$
      62             : such that thermal fluctuations can easily induce transitions
      63             : between different metastable states.
      64             : 
      65             : At convergence the relationship between the bias potential and the free
      66             : energy surface is given by
      67             : \f[
      68             : F(\mathbf{s}) = - \left(\frac{1}{1-\gamma^{-1}} \right) V(\mathbf{s})
      69             : \f]
      70             : 
      71             : This target distribution depends directly on the free energy surface
      72             : \f$F(\mathbf{s})\f$ which is quantity that we do not know a-priori and
      73             : want to obtain. Therefore, this target distribution
      74             : is iteratively updated \cite Valsson-JCTC-2015 according to
      75             : \f[
      76             : p^{(m+1)}(\mathbf{s}) =
      77             : \frac{e^{-(\beta/\gamma) F^{(m+1)}(\mathbf{s})}}
      78             : {\int d\mathbf{s}\, e^{-(\beta/\gamma) F^{(m+1)}(\mathbf{s})}}
      79             : \f]
      80             : where \f$F^{(m+1)}(\mathbf{s})\f$ is the current best estimate of the
      81             : free energy surface obtained according to
      82             : \f[
      83             : F^{(m+1)}(\mathbf{s}) =
      84             : - V^{(m+1)}(\mathbf{s}) - \frac{1}{\beta} \log p^{(m)}(\mathbf{s}) =
      85             : - V^{(m+1)}(\mathbf{s}) + \frac{1}{\gamma} F^{(m)}(\mathbf{s})
      86             : \f]
      87             : The frequency of performing this update needs to be set in the
      88             : optimizer used in the calculation. Normally it is sufficient
      89             : to do it every 100-1000 bias update iterations.
      90             : 
      91             : \par Examples
      92             : 
      93             : Employ a well-tempered target distribution with a bias factor of 10
      94             : \plumedfile
      95             : td_welltemp: TD_WELLTEMPERED BIASFACTOR=10
      96             : \endplumedfile
      97             : 
      98             : */
      99             : //+ENDPLUMEDOC
     100             : 
     101             : class TD_WellTempered: public TargetDistribution {
     102             : private:
     103             :   double bias_factor_;
     104             : public:
     105             :   static void registerKeywords(Keywords&);
     106             :   explicit TD_WellTempered(const ActionOptions& ao);
     107             :   void updateGrid() override;
     108             :   double getValue(const std::vector<double>&) const override;
     109          29 :   ~TD_WellTempered() {}
     110             : };
     111             : 
     112             : 
     113       10448 : PLUMED_REGISTER_ACTION(TD_WellTempered,"TD_WELLTEMPERED")
     114             : 
     115             : 
     116          30 : void TD_WellTempered::registerKeywords(Keywords& keys) {
     117          30 :   TargetDistribution::registerKeywords(keys);
     118          60 :   keys.add("compulsory","BIASFACTOR","The bias factor used for the well-tempered distribution.");
     119          30 : }
     120             : 
     121             : 
     122          29 : TD_WellTempered::TD_WellTempered(const ActionOptions& ao):
     123             :   PLUMED_VES_TARGETDISTRIBUTION_INIT(ao),
     124          29 :   bias_factor_(0.0)
     125             : {
     126          29 :   log.printf("  Well-tempered target distribution, see and cite ");
     127          58 :   log << plumed.cite("Valsson and Parrinello, J. Chem. Theory Comput. 11, 1996-2002 (2015)");
     128          58 :   log << plumed.cite("Barducci, Bussi, and Parrinello, Phys. Rev. Lett. 100, 020603 (2008)");
     129          29 :   log.printf("\n");
     130          29 :   parse("BIASFACTOR",bias_factor_);
     131          29 :   if(bias_factor_<=1.0) {
     132           0 :     plumed_merror("TD_WELLTEMPERED target distribution: the value of the bias factor doesn't make sense, it should be larger than 1.0");
     133             :   }
     134             :   setDynamic();
     135             :   setFesGridNeeded();
     136          29 :   checkRead();
     137          29 : }
     138             : 
     139             : 
     140           0 : double TD_WellTempered::getValue(const std::vector<double>& argument) const {
     141           0 :   plumed_merror("getValue not implemented for TD_WellTempered");
     142             :   return 0.0;
     143             : }
     144             : 
     145             : 
     146         319 : void TD_WellTempered::updateGrid() {
     147         319 :   double beta_prime = getBeta()/bias_factor_;
     148         319 :   plumed_massert(getFesGridPntr()!=NULL,"the FES grid has to be linked to use TD_WellTempered!");
     149         638 :   std::vector<double> integration_weights = GridIntegrationWeights::getIntegrationWeights(getTargetDistGridPntr());
     150             :   double norm = 0.0;
     151     1127896 :   for(Grid::index_t l=0; l<targetDistGrid().getSize(); l++) {
     152     1127577 :     double value = beta_prime * getFesGridPntr()->getValue(l);
     153     1127577 :     logTargetDistGrid().setValue(l,value);
     154     1127577 :     value = exp(-value);
     155     1127577 :     norm += integration_weights[l]*value;
     156     1127577 :     targetDistGrid().setValue(l,value);
     157             :   }
     158         319 :   targetDistGrid().scaleAllValuesAndDerivatives(1.0/norm);
     159         319 :   logTargetDistGrid().setMinToZero();
     160         319 : }
     161             : 
     162             : 
     163             : }
     164             : }

Generated by: LCOV version 1.15