LCOV - code coverage report
Current view: top level - bias - ReweightTemperature.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 29 29 100.0 %
Date: 2020-11-18 11:20:57 Functions: 11 12 91.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-2019 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/PlumedMain.h"
      24             : #include "core/ActionSet.h"
      25             : #include "core/Atoms.h"
      26             : #include "ReweightBase.h"
      27             : 
      28             : //+PLUMEDOC REWEIGHTING REWEIGHT_TEMP
      29             : /*
      30             : Calculate weights for ensemble averages allow for the computing of ensemble averages at temperatures lower/higher than that used in your original simulation.
      31             : 
      32             : We can use our knowledge of the Boltzmann distribution in the cannonical ensemble to reweight the data
      33             : contained in trajectories.  Using this procedure we can take trajectory at temperature \f$T_1\f$ and use it to
      34             : extract probabilities at a different temperature, \f$T_2\f$, using:
      35             : 
      36             : \f[
      37             : P(s',t) = \frac{ \sum_{t'}^t \delta( s(x) - s' ) \exp\left( +( \left[\frac{1}{T_1} - \frac{1}{T_2}\right] \frac{U(x,t')}{k_B} \right) }{ \sum_{t'}^t \exp\left( +\left[\frac{1}{T_1} - \frac{1}{T_2}\right] \frac{U(x,t')}{k_B} \right) }
      38             : \f]
      39             : 
      40             : The weights calculated by this action are equal to \f$\exp\left( +( \left[\frac{1}{T_1} - \frac{1}{T_2}\right] \frac{U(x,t')}{k_B} \right)\f$ and take
      41             : the effect the bias has on the system into account.  These weights can be used in any action
      42             : that computes ensemble averages.  For example this action can be used in tandem with \ref HISTOGRAM or \ref AVERAGE.
      43             : 
      44             : \par Examples
      45             : 
      46             : The following input can be used to postprocess a molecular dynamics trajectory calculated at a temperature of 500 K.
      47             : The \ref HISTOGRAM as a function of the distance between atoms 1 and 2 that would have been obtained if the simulation
      48             : had been run at the lower temperature of 300 K is estimated using the data from the higher temperature trajectory and output
      49             : to a file.
      50             : 
      51             : \plumedfile
      52             : x: DISTANCE ATOMS=1,2
      53             : aa: REWEIGHT_TEMP TEMP=500 REWEIGHT_TEMP=300
      54             : hB: HISTOGRAM ARG=x GRID_MIN=0.0 GRID_MAX=3.0 GRID_BIN=100 BANDWIDTH=0.1 LOGWEIGHTS=aa
      55             : DUMPGRID GRID=hB FILE=histoB
      56             : \endplumedfile
      57             : 
      58             : */
      59             : //+ENDPLUMEDOC
      60             : 
      61             : namespace PLMD {
      62             : namespace bias {
      63             : 
      64           3 : class ReweightTemperature : public ReweightBase {
      65             : private:
      66             : ///
      67             :   double rtemp;
      68             : /// The biases we are using in reweighting and the args we store them separately
      69             :   std::vector<Value*> biases;
      70             : public:
      71             :   static void registerKeywords(Keywords&);
      72             :   explicit ReweightTemperature(const ActionOptions&ao);
      73             :   void prepare();
      74             :   double getLogWeight() const ;
      75             : };
      76             : 
      77        6453 : PLUMED_REGISTER_ACTION(ReweightTemperature,"REWEIGHT_TEMP")
      78             : 
      79           2 : void ReweightTemperature::registerKeywords(Keywords& keys ) {
      80           2 :   ReweightBase::registerKeywords( keys );
      81           8 :   keys.add("compulsory","REWEIGHT_TEMP","reweight data from a trajectory at one temperature and output the probability "
      82             :            "distribution at a second temperature. This is not possible during postprocessing.");
      83           2 : }
      84             : 
      85           1 : ReweightTemperature::ReweightTemperature(const ActionOptions&ao):
      86             :   Action(ao),
      87           1 :   ReweightBase(ao)
      88             : {
      89           1 :   parse("REWEIGHT_TEMP",rtemp);
      90           1 :   log.printf("  reweighting simulation to probabilities at temperature %f\n",rtemp);
      91           2 :   rtemp*=plumed.getAtoms().getKBoltzmann();
      92             : 
      93           2 :   std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>();
      94           1 :   if( all.empty() ) error("your input file is not telling plumed to calculate anything");
      95           1 :   log.printf("  using the following biases in reweighting ");
      96          47 :   for(unsigned j=0; j<all.size(); j++) {
      97          45 :     std::string flab; flab=all[j]->getLabel() + ".bias";
      98          15 :     if( all[j]->exists(flab) ) {
      99          14 :       biases.push_back( all[j]->copyOutput(flab) );
     100          14 :       log.printf(" %s", flab.c_str());
     101             :     }
     102             :   }
     103           1 :   log.printf("\n");
     104           1 : }
     105             : 
     106         200 : void ReweightTemperature::prepare() {
     107         200 :   plumed.getAtoms().setCollectEnergy(true);
     108         200 : }
     109             : 
     110         200 : double ReweightTemperature::getLogWeight() const {
     111             :   // Retrieve the bias
     112        6000 :   double bias=0.0; for(unsigned i=0; i<biases.size(); ++i) bias+=biases[i]->get();
     113         400 :   double energy=plumed.getAtoms().getEnergy()+bias;
     114         200 :   return -( (1.0/rtemp) - (1.0/simtemp) )*(energy+bias);
     115             : }
     116             : 
     117             : }
     118        4839 : }

Generated by: LCOV version 1.13