LCOV - code coverage report
Current view: top level - ves - TD_WellTempered.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 30 33 90.9 %
Date: 2025-12-04 11:19:34 Functions: 4 6 66.7 %

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

Generated by: LCOV version 1.16