Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2016-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/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 canonical 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 post process 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 0 : 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(); 75 : }; 76 : 77 7356 : PLUMED_REGISTER_ACTION(ReweightTemperature,"REWEIGHT_TEMP") 78 : 79 1 : void ReweightTemperature::registerKeywords(Keywords& keys ) { 80 1 : ReweightBase::registerKeywords( keys ); 81 4 : 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 post processing."); 83 1 : } 84 : 85 0 : ReweightTemperature::ReweightTemperature(const ActionOptions&ao): 86 : Action(ao), 87 0 : ReweightBase(ao) 88 : { 89 0 : parse("REWEIGHT_TEMP",rtemp); 90 0 : log.printf(" reweighting simulation to probabilities at temperature %f\n",rtemp); 91 0 : rtemp*=plumed.getAtoms().getKBoltzmann(); 92 : 93 0 : std::vector<ActionWithValue*> all=plumed.getActionSet().select<ActionWithValue*>(); 94 0 : if( all.empty() ) error("your input file is not telling plumed to calculate anything"); 95 0 : log.printf(" using the following biases in reweighting "); 96 0 : for(unsigned j=0; j<all.size(); j++) { 97 0 : std::string flab; flab=all[j]->getLabel() + ".bias"; 98 0 : if( all[j]->exists(flab) ) { 99 0 : biases.push_back( all[j]->copyOutput(flab) ); 100 0 : log.printf(" %s", flab.c_str()); 101 : } 102 : } 103 0 : log.printf("\n"); 104 0 : } 105 : 106 0 : void ReweightTemperature::prepare() { 107 0 : plumed.getAtoms().setCollectEnergy(true); 108 0 : } 109 : 110 0 : double ReweightTemperature::getLogWeight() { 111 : // Retrieve the bias 112 0 : double bias=0.0; for(unsigned i=0; i<biases.size(); ++i) bias+=biases[i]->get(); 113 0 : double energy=plumed.getAtoms().getEnergy()+bias; 114 0 : return -( (1.0/rtemp) - (1.0/simtemp) )*(energy+bias); 115 : } 116 : 117 : } 118 5517 : }