Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2018 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 "bias/Bias.h"
24 : #include "core/PlumedMain.h"
25 : #include "core/ActionRegister.h"
26 : #include "core/Atoms.h"
27 : #include "tools/Communicator.h"
28 : #include "tools/Grid.h"
29 : #include "tools/File.h"
30 : //#include <algorithm> //std::fill
31 :
32 : namespace PLMD {
33 : namespace ves {
34 :
35 : //+PLUMEDOC VES_BIAS VES_DELTA_F
36 : /*
37 : Implementation of VES\f$\Delta F\f$ method \cite Invernizzi2019vesdeltaf (step two only).
38 :
39 : \warning
40 : Notice that this is a stand-alone bias Action, it does not need any of the other VES module components
41 :
42 : First you should create some estimate of the local free energy basins of your system,
43 : using e.g. multiple \ref METAD short runs, and combining them with the \ref sum_hills utility.
44 : Once you have them, you can use this bias Action to perform the VES optimization part of the method.
45 :
46 : These \f$N+1\f$ local basins are used to model the global free energy.
47 : In particular, given the conditional probabilities \f$P(\mathbf{s}|i)\propto e^{-\beta F_i(\mathbf{s})}\f$
48 : and the probabilities of being in a given basin \f$P_i\f$, we can write:
49 : \f[
50 : e^{-\beta F(\mathbf{s})}\propto P(\mathbf{s})=\sum_{i=0}^N P(\mathbf{s}|i)P_i \, .
51 : \f]
52 : We use this free energy model and the chosen bias factor \f$\gamma\f$ to build the bias potential:
53 : \f$V(\mathbf{s})=-(1-1/\gamma)F(\mathbf{s})\f$.
54 : Or, more explicitly:
55 : \f[
56 : V(\mathbf{s})=(1-1/\gamma)\frac{1}{\beta}\log\left[e^{-\beta F_0(\mathbf{s})}
57 : +\sum_{i=1}^{N} e^{-\beta F_i(\mathbf{s})} e^{-\beta \alpha_i}\right] \, ,
58 : \f]
59 : where the parameters \f$\boldsymbol{\alpha}\f$ are the \f$N\f$ free energy differences (see below) from the \f$F_0\f$ basin.
60 :
61 : By default the \f$F_i(\mathbf{s})\f$ are shifted so that \f$\min[F_i(\mathbf{s})]=0\f$ for all \f$i=\{0,...,N\}\f$.
62 : In this case the optimization parameters \f$\alpha_i\f$ are the difference in height between the minima of the basins.
63 : Using the keyword `NORMALIZE`, you can also decide to normalize the local free energies so that
64 : \f$\int d\mathbf{s}\, e^{-\beta F_i(\mathbf{s})}=1\f$.
65 : In this case the parameters will represent not the difference in height (which depends on the chosen CVs),
66 : but the actual free energy difference, \f$\alpha_i=\Delta F_i\f$.
67 :
68 : However, as discussed in Ref. \cite Invernizzi2019vesdeltaf, a better estimate of \f$\Delta F_i\f$ should be obtained through the reweighting procedure.
69 :
70 : \par Examples
71 :
72 : The following performs the optimization of the free energy difference between two metastable basins:
73 :
74 : \plumedfile
75 : VES_DELTA_F ...
76 : LABEL=ves
77 : ARG=cv
78 : TEMP=300
79 : FILE_F0=../fesA.data
80 : FILE_F1=../fesB.data
81 : BIASFACTOR=10.0
82 : M_STEP=0.1
83 : AV_STRIDE=500
84 : PRINT_STRIDE=100
85 : ... VES_DELTA_F
86 :
87 : PRINT FMT=%g STRIDE=500 FILE=Colvar.data ARG=cv,ves.bias,ves.rct
88 : \endplumedfile
89 :
90 : The local FES files can be obtained as described in Sec. 4.2 of Ref. \cite Invernizzi2019vesdeltaf, i.e. for example:
91 : - run 4 indipendent MetaD runs, all starting from basin A, and kill them as soon as they make the first transition (see e.g. \ref COMMITTOR)
92 : - \verbatim cat HILLS* > all_HILLS \endverbatim
93 : - \verbatim plumed sum_hills --hills all_HILLS --oufile all_fesA.dat --mintozero --min -1 --max 1 --bin 100 \endverbatim
94 : - \verbatim awk -v n_rep=4 '{if($1!="#!" && $1!="") {for(i=1+(NF-1)/2; i<=NF; i++) $i/=n_rep;} print $0}' all_fesA.dat > fesA.data \endverbatim
95 :
96 : The header of the file should be similar to the following:
97 :
98 : \verbatim
99 : #! FIELDS cv file.free der_cv
100 : #! SET min_cv -1
101 : #! SET max_cv 1
102 : #! SET nbins_cv 100
103 : #! SET periodic_cv false
104 : \endverbatim
105 :
106 : */
107 : //+ENDPLUMEDOC
108 :
109 16 : class VesDeltaF : public bias::Bias {
110 :
111 : private:
112 : double beta_;
113 : unsigned NumParallel_;
114 : unsigned rank_;
115 : unsigned NumWalkers_;
116 : bool isFirstStep_;
117 :
118 : //local basins
119 : std::vector< std::unique_ptr<Grid> > grid_p_; //pointers because of GridBase::create
120 : std::vector<double> norm_;
121 :
122 : //optimizer-related stuff
123 : long unsigned mean_counter_;
124 : unsigned mean_weight_tau_;
125 : unsigned alpha_size_;
126 : unsigned sym_alpha_size_;
127 : std::vector<double> mean_alpha_;
128 : std::vector<double> inst_alpha_;
129 : std::vector<double> past_increment2_;
130 : double minimization_step_;
131 : bool damping_off_;
132 : //'tg' -> 'target distribution'
133 : double inv_gamma_;
134 : unsigned tg_counter_;
135 : unsigned tg_stride_;
136 : std::vector<double> tg_dV_dAlpha_;
137 : std::vector<double> tg_d2V_dAlpha2_;
138 : //'av' -> 'ensemble average'
139 : unsigned av_counter_;
140 : unsigned av_stride_;
141 : std::vector<double> av_dV_dAlpha_;
142 : std::vector<double> av_dV_dAlpha_prod_;
143 : std::vector<double> av_d2V_dAlpha2_;
144 : //printing
145 : unsigned print_stride_;
146 : OFile alphaOfile_;
147 : //other
148 : std::vector<double> exp_alpha_;
149 : std::vector<double> prev_exp_alpha_;
150 : double work_;
151 :
152 : //functions
153 : void update_alpha();
154 : void update_tg_and_rct();
155 : inline unsigned get_index(const unsigned, const unsigned) const;
156 :
157 : public:
158 : explicit VesDeltaF(const ActionOptions&);
159 : void calculate();
160 : void update();
161 : static void registerKeywords(Keywords& keys);
162 : };
163 :
164 7840 : PLUMED_REGISTER_ACTION(VesDeltaF,"VES_DELTA_F")
165 :
166 5 : void VesDeltaF::registerKeywords(Keywords& keys) {
167 5 : Bias::registerKeywords(keys);
168 10 : keys.use("ARG");
169 20 : keys.add("optional","TEMP","temperature is compulsory, but it can be sometimes fetched from the MD engine");
170 : //local free energies
171 : keys.add("numbered","FILE_F","names of files containing local free energies and derivatives. "
172 20 : "The first one, FILE_F0, is used as reference for all the free energy differences.");
173 15 : keys.reset_style("FILE_F","compulsory");
174 15 : keys.addFlag("NORMALIZE",false,"normalize all local free energies so that alpha will be (approx) \\f$\\Delta F\\f$");
175 15 : keys.addFlag("NO_MINTOZERO",false,"leave local free energies as provided, without shifting them to zero min");
176 : //target distribution
177 : keys.add("compulsory","BIASFACTOR","0","the \\f$\\gamma\\f$ bias factor used for well-tempered target \\f$p(\\mathbf{s})\\f$."
178 25 : " Set to 0 for non-tempered flat target");
179 : keys.add("optional","TG_STRIDE","( default=1 ) number of AV_STRIDEs between updates"
180 20 : " of target \\f$p(\\mathbf{s})\\f$ and reweighing factor \\f$c(t)\\f$");
181 : //optimization
182 25 : keys.add("compulsory","M_STEP","1.0","the \\f$\\mu\\f$ step used for the \\f$\\Omega\\f$ functional minimization");
183 25 : keys.add("compulsory","AV_STRIDE","500","number of simulation steps between alpha updates");
184 : keys.add("optional","TAU_MEAN","exponentially decaying average for alpha (rescaled using AV_STRIDE)."
185 20 : " Should be used only in very specific cases");
186 20 : keys.add("optional","INITIAL_ALPHA","( default=0 ) an initial guess for the bias potential parameter alpha");
187 15 : keys.addFlag("DAMPING_OFF",false,"do not use an AdaGrad-like term to rescale M_STEP");
188 : //output parameters file
189 25 : keys.add("compulsory","ALPHA_FILE","ALPHA","file name for output minimization parameters");
190 20 : keys.add("optional","PRINT_STRIDE","( default=10 ) stride for printing to ALPHA_FILE");
191 20 : keys.add("optional","FMT","specify format for ALPHA_FILE");
192 : //debug flags
193 15 : keys.addFlag("SERIAL",false,"perform the calculation in serial even if multiple tasks are available");
194 15 : keys.addFlag("MULTIPLE_WALKERS",false,"use multiple walkers connected via MPI for the optimization");
195 10 : keys.use("RESTART");
196 :
197 : //output components
198 5 : componentsAreNotOptional(keys);
199 20 : keys.addOutputComponent("rct","default","the reweighting factor \\f$c(t)\\f$");
200 20 : keys.addOutputComponent("work","default","the work done by the bias in one AV_STRIDE");
201 5 : }
202 :
203 4 : VesDeltaF::VesDeltaF(const ActionOptions&ao)
204 : : PLUMED_BIAS_INIT(ao)
205 : , isFirstStep_(true)
206 : , mean_counter_(0)
207 : , av_counter_(0)
208 20 : , work_(0)
209 : {
210 : //set beta
211 8 : const double Kb=plumed.getAtoms().getKBoltzmann();
212 4 : double temp=0;
213 8 : parse("TEMP",temp);
214 4 : double KbT=Kb*temp;
215 4 : if(KbT==0)
216 : {
217 0 : KbT=plumed.getAtoms().getKbT();
218 0 : plumed_massert(KbT>0,"your MD engine does not pass the temperature to plumed, you must specify it using TEMP");
219 : }
220 4 : beta_=1.0/KbT;
221 :
222 : //initialize probability grids using local free energies
223 : bool spline=true;
224 : bool sparsegrid=false;
225 4 : std::string funcl="file.free"; //typical name given by sum_hills
226 :
227 4 : std::vector<std::string> fes_names;
228 8 : for(unsigned n=0;; n++)//NB: here we start from FILE_F0 not from FILE_F1
229 : {
230 : std::string filename;
231 24 : if(!parseNumbered("FILE_F",n,filename))
232 : break;
233 8 : fes_names.push_back(filename);
234 16 : IFile gridfile;
235 8 : gridfile.open(filename);
236 8 : auto g=GridBase::create(funcl,getArguments(),gridfile,sparsegrid,spline,true);
237 : // we assume this cannot be sparse. in case we want it to be sparse, some of the methods
238 : // that are available only in Grid should be ported to GridBase
239 8 : auto gg=dynamic_cast<Grid*>(g.get());
240 : // if this throws, g is deleted
241 8 : plumed_assert(gg);
242 : // release ownership in order to transfer it to emplaced pointer
243 : g.release();
244 8 : grid_p_.emplace_back(gg);
245 8 : }
246 4 : plumed_massert(grid_p_.size()>1,"at least 2 basins must be defined, starting from FILE_F0");
247 4 : alpha_size_=grid_p_.size()-1;
248 4 : sym_alpha_size_=alpha_size_*(alpha_size_+1)/2; //useful for symmetric matrix [alpha_size_]x[alpha_size_]
249 : //check for consistency with first local free energy
250 16 : for(unsigned n=1; n<grid_p_.size(); n++)
251 : {
252 16 : std::string error_tag="FILE_F"+std::to_string(n)+" '"+fes_names[n]+"' not compatible with reference one, FILE_F0";
253 8 : plumed_massert(grid_p_[n]->getSize()==grid_p_[0]->getSize(),error_tag);
254 8 : plumed_massert(grid_p_[n]->getMin()==grid_p_[0]->getMin(),error_tag);
255 8 : plumed_massert(grid_p_[n]->getMax()==grid_p_[0]->getMax(),error_tag);
256 8 : plumed_massert(grid_p_[n]->getBinVolume()==grid_p_[0]->getBinVolume(),error_tag);
257 : }
258 :
259 4 : bool no_mintozero=false;
260 8 : parseFlag("NO_MINTOZERO",no_mintozero);
261 4 : if(!no_mintozero)
262 : {
263 10 : for(unsigned n=0; n<grid_p_.size(); n++)
264 4 : grid_p_[n]->setMinToZero();
265 : }
266 4 : bool normalize=false;
267 8 : parseFlag("NORMALIZE",normalize);
268 8 : norm_.resize(grid_p_.size(),0);
269 4 : std::vector<double> c_norm(grid_p_.size());
270 : //convert the FESs to probability distributions
271 : //NB: the spline interpolation will be done on the probability distributions, not on the given FESs
272 : const unsigned ncv=getNumberOfArguments(); //just for ease
273 24 : for(unsigned n=0; n<grid_p_.size(); n++)
274 : {
275 1608 : for(Grid::index_t t=0; t<grid_p_[n]->getSize(); t++)
276 : {
277 800 : std::vector<double> der(ncv);
278 1600 : const double val=std::exp(-beta_*grid_p_[n]->getValueAndDerivatives(t,der));
279 1600 : for(unsigned s=0; s<ncv; s++)
280 1600 : der[s]*=-beta_*val;
281 800 : grid_p_[n]->setValueAndDerivatives(t,val,der);
282 800 : norm_[n]+=val;
283 : }
284 16 : c_norm[n]=1./beta_*std::log(norm_[n]);
285 8 : if(normalize)
286 : {
287 8 : grid_p_[n]->scaleAllValuesAndDerivatives(1./norm_[n]);
288 4 : norm_[n]=1;
289 : }
290 : }
291 :
292 : //get target
293 4 : double biasfactor=0;
294 8 : parse("BIASFACTOR",biasfactor);
295 4 : plumed_massert(biasfactor==0 || biasfactor>1,"BIASFACTOR must be zero (for uniform target) or greater than one");
296 4 : if(biasfactor==0)
297 2 : inv_gamma_=0;
298 : else
299 2 : inv_gamma_=1./biasfactor;
300 4 : tg_counter_=0;
301 4 : tg_stride_=1;
302 8 : parse("TG_STRIDE",tg_stride_);
303 4 : tg_dV_dAlpha_.resize(alpha_size_,0);
304 4 : tg_d2V_dAlpha2_.resize(sym_alpha_size_,0);
305 :
306 : //setup optimization stuff
307 4 : minimization_step_=1;
308 8 : parse("M_STEP",minimization_step_);
309 :
310 4 : av_stride_=500;
311 8 : parse("AV_STRIDE",av_stride_);
312 4 : av_dV_dAlpha_.resize(alpha_size_,0);
313 4 : av_dV_dAlpha_prod_.resize(sym_alpha_size_,0);
314 4 : av_d2V_dAlpha2_.resize(sym_alpha_size_,0);
315 :
316 4 : mean_weight_tau_=0;
317 8 : parse("TAU_MEAN",mean_weight_tau_);
318 4 : if(mean_weight_tau_!=1) //set it to 1 for basic SGD
319 : {
320 4 : plumed_massert((mean_weight_tau_==0 || mean_weight_tau_>av_stride_),"TAU_MEAN is rescaled with AV_STRIDE, so it has to be greater");
321 4 : mean_weight_tau_/=av_stride_; //this way you can look at the number of simulation steps to choose TAU_MEAN
322 : }
323 :
324 8 : parseVector("INITIAL_ALPHA",mean_alpha_);
325 4 : if(mean_alpha_.size()>0)
326 : {
327 2 : plumed_massert(mean_alpha_.size()==alpha_size_,"provide one INITIAL_ALPHA for each basin beyond the first one");
328 : }
329 : else
330 2 : mean_alpha_.resize(alpha_size_,0);
331 4 : inst_alpha_=mean_alpha_;
332 4 : exp_alpha_.resize(alpha_size_);
333 4 : for(unsigned i=0; i<alpha_size_; i++)
334 12 : exp_alpha_[i]=std::exp(-beta_*mean_alpha_[i]);
335 4 : prev_exp_alpha_=exp_alpha_;
336 :
337 4 : damping_off_=false;
338 8 : parseFlag("DAMPING_OFF",damping_off_);
339 4 : if(damping_off_)
340 2 : past_increment2_.resize(alpha_size_,1);
341 : else
342 2 : past_increment2_.resize(alpha_size_,0);
343 :
344 : //file printing options
345 4 : std::string alphaFileName("ALPHA");
346 8 : parse("ALPHA_FILE",alphaFileName);
347 4 : print_stride_=10;
348 8 : parse("PRINT_STRIDE",print_stride_);
349 : std::string fmt;
350 8 : parse("FMT",fmt);
351 :
352 : //other flags, mainly for debugging
353 4 : NumParallel_=comm.Get_size();
354 4 : rank_=comm.Get_rank();
355 4 : bool serial=false;
356 8 : parseFlag("SERIAL",serial);
357 4 : if(serial)
358 : {
359 2 : log.printf(" -- SERIAL: running without loop parallelization\n");
360 2 : NumParallel_=1;
361 2 : rank_=0;
362 : }
363 :
364 4 : bool multiple_walkers=false;
365 8 : parseFlag("MULTIPLE_WALKERS",multiple_walkers);
366 4 : if(!multiple_walkers)
367 2 : NumWalkers_=1;
368 : else
369 : {
370 2 : if(comm.Get_rank()==0)//multi_sim_comm works well on first rank only
371 2 : NumWalkers_=multi_sim_comm.Get_size();
372 2 : if(comm.Get_size()>1) //if each walker has more than one processor update them all
373 0 : comm.Bcast(NumWalkers_,0);
374 : }
375 :
376 4 : checkRead();
377 :
378 : //restart if needed
379 4 : if(getRestart())
380 : {
381 2 : IFile ifile;
382 2 : ifile.link(*this);
383 2 : if(NumWalkers_>1)
384 4 : ifile.enforceSuffix("");
385 2 : if(ifile.FileExist(alphaFileName))
386 : {
387 2 : log.printf(" Restarting from: %s\n",alphaFileName.c_str());
388 2 : log.printf(" all options (also PRINT_STRIDE) must be consistent!\n");
389 2 : log.printf(" any INITIAL_ALPHA will be overwritten\n");
390 2 : ifile.open(alphaFileName);
391 : double time;
392 2 : std::vector<double> damping(alpha_size_);
393 22 : while(ifile.scanField("time",time)) //room for improvements: only last line is important
394 : {
395 16 : for(unsigned i=0; i<alpha_size_; i++)
396 : {
397 8 : const std::string index(std::to_string(i+1));
398 24 : prev_exp_alpha_[i]=std::exp(-beta_*mean_alpha_[i]);
399 16 : ifile.scanField("alpha_"+index,mean_alpha_[i]);
400 16 : ifile.scanField("auxiliary_"+index,inst_alpha_[i]);
401 16 : ifile.scanField("damping_"+index,damping[i]);
402 : }
403 8 : ifile.scanField();
404 8 : mean_counter_+=print_stride_;
405 : }
406 2 : for(unsigned i=0; i<alpha_size_; i++)
407 : {
408 6 : exp_alpha_[i]=std::exp(-beta_*mean_alpha_[i]);
409 2 : past_increment2_[i]=damping[i]*damping[i];
410 : }
411 : //sync all walkers and treads. Not sure is mandatory but is no harm
412 2 : comm.Barrier();
413 2 : if(comm.Get_rank()==0)
414 2 : multi_sim_comm.Barrier();
415 : }
416 : else
417 0 : log.printf(" -- WARNING: restart requested, but no '%s' file found!\n",alphaFileName.c_str());
418 : }
419 :
420 : //setup output file with Alpha values
421 4 : alphaOfile_.link(*this);
422 4 : if(NumWalkers_>1)
423 : {
424 2 : if(comm.Get_rank()==0 && multi_sim_comm.Get_rank()>0)
425 : alphaFileName="/dev/null"; //only first walker writes on file
426 4 : alphaOfile_.enforceSuffix("");
427 : }
428 4 : alphaOfile_.open(alphaFileName);
429 4 : if(fmt.length()>0)
430 8 : alphaOfile_.fmtField(" "+fmt);
431 :
432 : //add other output components
433 12 : addComponent("rct"); componentIsNotPeriodic("rct");
434 12 : addComponent("work"); componentIsNotPeriodic("work");
435 :
436 : //print some info
437 4 : log.printf(" Temperature T: %g\n",1./(Kb*beta_));
438 4 : log.printf(" Beta (1/Kb*T): %g\n",beta_);
439 4 : log.printf(" Local free energy basins files and normalization constants:\n");
440 20 : for(unsigned n=0; n<grid_p_.size(); n++)
441 16 : log.printf(" F_%d filename: %s c_%d=%g\n",n,fes_names[n].c_str(),n,c_norm[n]);
442 4 : if(no_mintozero)
443 2 : log.printf(" -- NO_MINTOZERO: local free energies are not shifted to be zero at minimum\n");
444 4 : if(normalize)
445 2 : log.printf(" -- NORMALIZE: F_n+=c_n, alpha=DeltaF\n");
446 4 : log.printf(" Using target distribution with 1/gamma = %g\n",inv_gamma_);
447 4 : log.printf(" and updated with stride %d\n",tg_stride_);
448 4 : log.printf(" Step for the minimization algorithm: %g\n",minimization_step_);
449 4 : log.printf(" Stride for the ensemble average: %d\n",av_stride_);
450 4 : if(mean_weight_tau_>1)
451 2 : log.printf(" Exponentially decaying average with weight=tau/av_stride=%d\n",mean_weight_tau_);
452 4 : if(mean_weight_tau_==1)
453 0 : log.printf(" +++ WARNING +++ setting TAU_MEAN=1 is equivalent to use simple SGD, without mean alpha nor hessian contribution\n");
454 4 : log.printf(" Initial guess for alpha:\n");
455 8 : for(unsigned i=0; i<alpha_size_; i++)
456 8 : log.printf(" alpha_%d = %g\n",i+1,mean_alpha_[i]);
457 4 : if(damping_off_)
458 2 : log.printf(" -- DAMPING_OFF: the minimization step will NOT become smaller as the simulation goes on\n");
459 8 : log.printf(" Printing on file %s with stride %d\n",alphaFileName.c_str(),print_stride_);
460 4 : if(serial)
461 2 : log.printf(" -- SERIAL: running without loop parallelization\n");
462 4 : if(NumParallel_>1)
463 2 : log.printf(" Using multiple threads per simulation: %d\n",NumParallel_);
464 4 : if(multiple_walkers)
465 : {
466 2 : log.printf(" -- MULTIPLE_WALKERS: multiple simulations will combine statistics for the optimization\n");
467 2 : if(NumWalkers_>1)
468 : {
469 2 : log.printf(" number of walkers: %d\n",NumWalkers_);
470 2 : log.printf(" walker rank: %d\n",multi_sim_comm.Get_rank()); //only comm.Get_rank()=0 prints, so this is fine
471 : }
472 : else
473 0 : log.printf(" +++ WARNING +++ only one replica found: are you sure you are running MPI-connected simulations?\n");
474 : }
475 4 : log.printf(" Bibliography ");
476 12 : log<<plumed.cite("Invernizzi and Parrinello, J. Chem. Theory Comput. 15, 2187-2194 (2019)");
477 12 : log<<plumed.cite("Valsson and Parrinello, Phys. Rev. Lett. 113, 090601 (2014)");
478 4 : if(inv_gamma_>0)
479 6 : log<<plumed.cite("Valsson and Parrinello, J. Chem. Theory Comput. 11, 1996-2002 (2015)");
480 :
481 : //set initial value for tg averages and rct
482 4 : update_tg_and_rct();
483 4 : }
484 :
485 804 : void VesDeltaF::calculate()
486 : {
487 : //get CVs
488 804 : const unsigned ncv=getNumberOfArguments(); //just for ease
489 804 : std::vector<double> cv(ncv);
490 1608 : for(unsigned s=0; s<ncv; s++)
491 1608 : cv[s]=getArgument(s);
492 : //get probabilities for each basin, and total one
493 804 : std::vector<double> prob(grid_p_.size());
494 3212 : std::vector< std::vector<double> > der_prob(grid_p_.size(),std::vector<double>(ncv));
495 4824 : for(unsigned n=0; n<grid_p_.size(); n++)
496 1608 : prob[n]=grid_p_[n]->getValueAndDerivatives(cv,der_prob[n]);
497 804 : double tot_prob=prob[0];
498 2412 : for(unsigned i=0; i<alpha_size_; i++)
499 2412 : tot_prob+=prob[i+1]*exp_alpha_[i];
500 :
501 : //update bias and forces: V=-(1-inv_gamma_)*fes
502 804 : setBias((1-inv_gamma_)/beta_*std::log(tot_prob));
503 804 : for(unsigned s=0; s<ncv; s++)
504 : {
505 1608 : double dProb_dCV_s=der_prob[0][s];
506 2412 : for(unsigned i=0; i<alpha_size_; i++)
507 2412 : dProb_dCV_s+=der_prob[i+1][s]*exp_alpha_[i];
508 804 : setOutputForce(s,-(1-inv_gamma_)/beta_/tot_prob*dProb_dCV_s);
509 : }
510 : //skip first step to sync getTime() and av_counter_, as in METAD
511 804 : if(isFirstStep_)
512 : {
513 4 : isFirstStep_=false;
514 808 : return;
515 : }
516 :
517 : //calculate derivatives for ensemble averages
518 800 : std::vector<double> dV_dAlpha(alpha_size_);
519 800 : std::vector<double> d2V_dAlpha2(sym_alpha_size_);
520 2400 : for(unsigned i=0; i<alpha_size_; i++)
521 3200 : dV_dAlpha[i]=-(1-inv_gamma_)/tot_prob*prob[i+1]*exp_alpha_[i];
522 800 : for(unsigned i=0; i<alpha_size_; i++)
523 : {
524 2400 : d2V_dAlpha2[get_index(i,i)]=-beta_*dV_dAlpha[i];
525 1600 : for(unsigned j=i; j<alpha_size_; j++)
526 3200 : d2V_dAlpha2[get_index(i,j)]-=beta_/(1-inv_gamma_)*dV_dAlpha[i]*dV_dAlpha[j];
527 : }
528 : //update ensemble averages
529 800 : av_counter_++;
530 1600 : for(unsigned i=0; i<alpha_size_; i++)
531 : {
532 2400 : av_dV_dAlpha_[i]+=(dV_dAlpha[i]-av_dV_dAlpha_[i])/av_counter_;
533 1600 : for(unsigned j=i; j<alpha_size_; j++)
534 : {
535 800 : const unsigned ij=get_index(i,j);
536 2400 : av_dV_dAlpha_prod_[ij]+=(dV_dAlpha[i]*dV_dAlpha[j]-av_dV_dAlpha_prod_[ij])/av_counter_;
537 1600 : av_d2V_dAlpha2_[ij]+=(d2V_dAlpha2[ij]-av_d2V_dAlpha2_[ij])/av_counter_;
538 : }
539 : }
540 : //update work
541 800 : double prev_tot_prob=prob[0];
542 2400 : for(unsigned i=0; i<alpha_size_; i++)
543 2400 : prev_tot_prob+=prob[i+1]*prev_exp_alpha_[i];
544 800 : work_+=(1-inv_gamma_)/beta_*std::log(tot_prob/prev_tot_prob);
545 : }
546 :
547 804 : void VesDeltaF::update()
548 : {
549 804 : if(av_counter_==av_stride_)
550 : {
551 16 : update_alpha();
552 16 : tg_counter_++;
553 16 : if(tg_counter_==tg_stride_)
554 : {
555 12 : update_tg_and_rct();
556 12 : tg_counter_=0;
557 : }
558 : //reset the ensemble averages
559 16 : av_counter_=0;
560 : std::fill(av_dV_dAlpha_.begin(),av_dV_dAlpha_.end(),0);
561 : std::fill(av_dV_dAlpha_prod_.begin(),av_dV_dAlpha_prod_.end(),0);
562 : std::fill(av_d2V_dAlpha2_.begin(),av_d2V_dAlpha2_.end(),0);
563 : }
564 804 : }
565 :
566 16 : void VesDeltaF::update_tg_and_rct()
567 : {
568 : //calculate target averages
569 16 : double Z_0=norm_[0];
570 48 : for(unsigned i=0; i<alpha_size_; i++)
571 48 : Z_0+=norm_[i+1]*exp_alpha_[i];
572 16 : double Z_tg=0;
573 : std::fill(tg_dV_dAlpha_.begin(),tg_dV_dAlpha_.end(),0);
574 : std::fill(tg_d2V_dAlpha2_.begin(),tg_d2V_dAlpha2_.end(),0);
575 2232 : for(Grid::index_t t=rank_; t<grid_p_[0]->getSize(); t+=NumParallel_)
576 : { //TODO can we recycle some code?
577 1100 : std::vector<double> prob(grid_p_.size());
578 6600 : for(unsigned n=0; n<grid_p_.size(); n++)
579 2200 : prob[n]=grid_p_[n]->getValue(t);
580 1100 : double tot_prob=prob[0];
581 3300 : for(unsigned i=0; i<alpha_size_; i++)
582 3300 : tot_prob+=prob[i+1]*exp_alpha_[i];
583 1100 : std::vector<double> dV_dAlpha(alpha_size_);
584 1100 : std::vector<double> d2V_dAlpha2(sym_alpha_size_);
585 3300 : for(unsigned i=0; i<alpha_size_; i++)
586 4400 : dV_dAlpha[i]=-(1-inv_gamma_)/tot_prob*prob[i+1]*exp_alpha_[i];
587 1100 : for(unsigned i=0; i<alpha_size_; i++)
588 : {
589 3300 : d2V_dAlpha2[get_index(i,i)]=-beta_*dV_dAlpha[i];
590 2200 : for(unsigned j=i; j<alpha_size_; j++)
591 4400 : d2V_dAlpha2[get_index(i,j)]-=beta_/(1-inv_gamma_)*dV_dAlpha[i]*dV_dAlpha[j];
592 : }
593 1100 : const double unnorm_tg_p=std::pow(tot_prob,inv_gamma_);
594 1100 : Z_tg+=unnorm_tg_p;
595 2200 : for(unsigned i=0; i<alpha_size_; i++)
596 3300 : tg_dV_dAlpha_[i]+=unnorm_tg_p*dV_dAlpha[i];
597 1100 : for(unsigned ij=0; ij<sym_alpha_size_; ij++)
598 3300 : tg_d2V_dAlpha2_[ij]+=unnorm_tg_p*d2V_dAlpha2[ij];
599 : }
600 16 : if(NumParallel_>1)
601 : {
602 10 : comm.Sum(Z_tg);
603 10 : comm.Sum(tg_dV_dAlpha_);
604 10 : comm.Sum(tg_d2V_dAlpha2_);
605 : }
606 16 : for(unsigned i=0; i<alpha_size_; i++)
607 32 : tg_dV_dAlpha_[i]/=Z_tg;
608 16 : for(unsigned ij=0; ij<sym_alpha_size_; ij++)
609 32 : tg_d2V_dAlpha2_[ij]/=Z_tg;
610 32 : getPntrToComponent("rct")->set(-1./beta_*std::log(Z_tg/Z_0)); //Z_tg is the best available estimate of Z_V
611 16 : }
612 :
613 16 : void VesDeltaF::update_alpha()
614 : {
615 : //combining the averages of multiple walkers
616 16 : if(NumWalkers_>1)
617 : {
618 8 : if(comm.Get_rank()==0) //sum only once: in the first rank of each walker
619 : {
620 8 : multi_sim_comm.Sum(av_dV_dAlpha_);
621 8 : multi_sim_comm.Sum(av_dV_dAlpha_prod_);
622 8 : multi_sim_comm.Sum(av_d2V_dAlpha2_);
623 8 : for(unsigned i=0; i<alpha_size_; i++)
624 16 : av_dV_dAlpha_[i]/=NumWalkers_;
625 8 : for(unsigned ij=0; ij<sym_alpha_size_; ij++)
626 : {
627 16 : av_dV_dAlpha_prod_[ij]/=NumWalkers_;
628 8 : av_d2V_dAlpha2_[ij]/=NumWalkers_;
629 : }
630 : }
631 8 : if(comm.Get_size()>1)//if there are more ranks for each walker, everybody has to know
632 : {
633 0 : comm.Bcast(av_dV_dAlpha_,0);
634 0 : comm.Bcast(av_dV_dAlpha_prod_,0);
635 0 : comm.Bcast(av_d2V_dAlpha2_,0);
636 : }
637 : }
638 : //set work and reset it
639 32 : getPntrToComponent("work")->set(work_);
640 16 : work_=0;
641 :
642 : //build the gradient and the Hessian of the functional
643 16 : std::vector<double> grad_omega(alpha_size_);
644 16 : std::vector<double> hess_omega(sym_alpha_size_);
645 32 : for(unsigned i=0; i<alpha_size_; i++)
646 : {
647 48 : grad_omega[i]=tg_dV_dAlpha_[i]-av_dV_dAlpha_[i];
648 32 : for(unsigned j=i; j<alpha_size_; j++)
649 : {
650 16 : const unsigned ij=get_index(i,j);
651 112 : hess_omega[ij]=beta_*(av_dV_dAlpha_prod_[ij]-av_dV_dAlpha_[i]*av_dV_dAlpha_[j])+tg_d2V_dAlpha2_[ij]-av_d2V_dAlpha2_[ij];
652 : }
653 : }
654 : //calculate the increment and update alpha
655 16 : mean_counter_++;
656 : long unsigned mean_weight=mean_counter_;
657 16 : if(mean_weight_tau_>0 && mean_weight_tau_<mean_counter_)
658 : mean_weight=mean_weight_tau_;
659 16 : std::vector<double> damping(alpha_size_);
660 32 : for(unsigned i=0; i<alpha_size_; i++)
661 : {
662 32 : double increment_i=grad_omega[i];
663 32 : for(unsigned j=0; j<alpha_size_; j++)
664 64 : increment_i+=hess_omega[get_index(i,j)]*(inst_alpha_[j]-mean_alpha_[j]);
665 16 : if(!damping_off_)
666 8 : past_increment2_[i]+=increment_i*increment_i;
667 16 : damping[i]=std::sqrt(past_increment2_[i]);
668 32 : prev_exp_alpha_[i]=std::exp(-beta_*mean_alpha_[i]);
669 48 : inst_alpha_[i]-=minimization_step_/damping[i]*increment_i;
670 32 : mean_alpha_[i]+=(inst_alpha_[i]-mean_alpha_[i])/mean_weight;
671 32 : exp_alpha_[i]=std::exp(-beta_*mean_alpha_[i]);
672 : }
673 :
674 : //update the Alpha file
675 16 : if(mean_counter_%print_stride_==0)
676 : {
677 32 : alphaOfile_.printField("time",getTime());
678 48 : for(unsigned i=0; i<alpha_size_; i++)
679 : {
680 16 : const std::string index(std::to_string(i+1));
681 48 : alphaOfile_.printField("alpha_"+index,mean_alpha_[i]);
682 32 : alphaOfile_.printField("auxiliary_"+index,inst_alpha_[i]);
683 32 : alphaOfile_.printField("damping_"+index,damping[i]);
684 : }
685 16 : alphaOfile_.printField();
686 : }
687 16 : }
688 :
689 : //mapping of a [alpha_size_]x[alpha_size_] symmetric matrix into a vector of size sym_alpha_size_, useful for the communicator
690 4632 : inline unsigned VesDeltaF::get_index(const unsigned i, const unsigned j) const
691 : {
692 4632 : if(i<=j)
693 4632 : return j+i*(alpha_size_-1)-i*(i-1)/2;
694 : else
695 0 : return get_index(j,i);
696 : }
697 :
698 : }
699 5874 : }
|