Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2020 of Glen Hocky
3 :
4 : The FISST module is free software: you can redistribute it and/or modify
5 : it under the terms of the GNU Lesser General Public License as published by
6 : the Free Software Foundation, either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : The FISST module is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU Lesser General Public License for more details.
13 :
14 : You should have received a copy of the GNU Lesser General Public License
15 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
16 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
17 : #include "bias/Bias.h"
18 : #include "core/ActionRegister.h"
19 : #include "tools/File.h"
20 : #include "tools/Matrix.h"
21 : #include "tools/Random.h"
22 : #include "legendre_rule_fast.h"
23 :
24 : #include <iostream>
25 :
26 :
27 : using namespace PLMD;
28 : using namespace bias;
29 :
30 : //namespace is lowercase to match
31 : //module names being all lowercase
32 :
33 : namespace PLMD {
34 : namespace fisst {
35 :
36 : //+PLUMEDOC FISSTMOD_BIAS FISST
37 : /*
38 : Compute and apply the optimal linear force on an observable to enhance sampling of conformational distributions over a range of applied forces.
39 :
40 : This method is described in \cite Hartmann-FISST-2019
41 :
42 : If the system's Hamiltonian is given by:
43 : \f[
44 : H(\vec{p},\vec{q}) = \sum_{j} \frac{p_j^2}{2m_j} + U(\vec{q}),
45 : \f]
46 :
47 : This bias modifies the Hamiltonian to be:
48 : \f[
49 : H'(\vec{p},\vec{q}) = H(\vec{p},\vec{q}) - \bar{F} Q
50 : \f]
51 :
52 : where for CV \f$Q\f$, a coupling constant \f${\bar{F}}\f$ is determined
53 : adaptively according to the FISST algorithm.
54 :
55 : Specifically,
56 : \f[
57 : \bar{F}(Q)=\frac{ \int_{F_{min}}^{F_{max}} e^{\beta F Q(\vec{q})} \omega(F) F dF}{\int_{F_{min}}^{F_{max}} e^{\beta F Q(\vec{q})} \omega(F) dF},
58 : \f]
59 :
60 : where \f$\vec{q}\f$ are the molecular coordinates of the system, and \f$w(F)\f$ is a weighting function that is learned on the fly for each force by the FISST algorithm (starting from an initial weight distribution, uniform by default).
61 :
62 : The target for \f$w(F)=1/Z_q(F)\f$, where
63 : \f[
64 : Z_q(F) \equiv \int d\vec{q} e^{-\beta U(\vec{q}) + \beta F Q(\vec{q})}.
65 : \f]
66 :
67 : FISST also computes and writes Observable Weights \f$W_F(\vec{q}_t)\f$ for a molecular configuration at time \f$t\f$, so that averages of other quantities \f$A(\vec{q})\f$ can be reconstructed later at different force values (over a trajectory with \f$T\f$ samples):
68 : \f[
69 : \langle A \rangle_F = \frac{1}{T} \sum_t W_F(\vec{q}_t) A(\vec{q}_t).
70 : \f]
71 :
72 :
73 : \par Examples
74 :
75 : In the following example, an adaptive restraint is learned to bias the distance between two atoms in a system, for a force range of 0-100 pN.
76 :
77 : \plumedfile
78 : UNITS LENGTH=A TIME=fs ENERGY=kcal/mol
79 :
80 : b1: GROUP ATOMS=1
81 : b2: GROUP ATOMS=12
82 :
83 : dend: DISTANCE ATOMS=b1,b2
84 :
85 : #The conversion factor is 69.4786 pN = 1 kcal/mol/Angstrom
86 :
87 : #0 pN to 100 pN
88 : f: FISST MIN_FORCE=0 MAX_FORCE=1.44 PERIOD=100 NINTERPOLATE=31 ARG=dend OUT_RESTART=pull.restart.txt OUT_OBSERVABLE=pull.observable.txt OBSERVABLE_FREQ=1000
89 :
90 : PRINT ARG=dend,f.dend_fbar,f.bias,f.force2 FILE=pull.colvar.txt STRIDE=1000
91 : \endplumedfile
92 :
93 :
94 : */
95 : //+ENDPLUMEDOC
96 :
97 :
98 : class FISST : public Bias {
99 :
100 :
101 : private:
102 : /*We will get this and store it once, since on-the-fly changing number of CVs will be fatal*/
103 : const unsigned int ncvs_;
104 : std::vector<double> center_;
105 : std::vector<double> current_avg_force_;
106 :
107 : std::vector<double> forces_;
108 : std::vector<double> force_weight_;
109 : std::vector<double> gauss_weight_;
110 : std::vector<double> partition_estimate_;
111 : std::vector<double> observable_weight_;
112 :
113 : std::string in_restart_name_;
114 : std::string out_restart_name_;
115 : std::string out_observable_name_;
116 : std::string fmt_;
117 : std::string initial_weight_dist_;
118 : OFile out_restart_;
119 : OFile out_observable_;
120 : IFile in_restart_;
121 : bool b_freeze_;
122 : bool b_adaptive_;
123 : bool b_restart_;
124 : bool b_write_restart_;
125 : bool b_write_observable_;
126 : bool b_first_restart_sample_;
127 : int period_;
128 : int reset_period_;
129 : int observable_freq_;
130 : int n_interpolation_;
131 : int n_samples_;
132 : double kbt_;
133 : double beta_;
134 : //change min_force and max_force to vectors if going to do more than one cv
135 : double max_force_;
136 : double min_force_;
137 : double initial_weight_rate_;
138 : double threshold_;
139 : Random rand_;
140 :
141 :
142 : Value* value_force2_;
143 : void readInRestart();
144 : void NormalizeForceWeights();
145 : /*setup output restart*/
146 : void setupOutRestart();
147 : void setupOutObservable();
148 : /*write output restart*/
149 : void writeOutRestart();
150 : void writeOutObservable();
151 : void update_statistics();
152 : void update_bias();
153 : void apply_bias();
154 : void compute_observable_weight();
155 :
156 : public:
157 : explicit FISST(const ActionOptions&);
158 : void calculate();
159 : void update();
160 : void turnOnDerivatives();
161 : static void registerKeywords(Keywords& keys);
162 : ~FISST();
163 : };
164 :
165 : PLUMED_REGISTER_ACTION(FISST,"FISST")
166 :
167 4 : void FISST::registerKeywords(Keywords& keys) {
168 4 : Bias::registerKeywords(keys);
169 4 : keys.use("ARG");
170 8 : keys.add("compulsory","PERIOD","Steps corresponding to the learning rate");
171 8 : keys.add("optional","RESET_PERIOD","Reset the learning statistics every time this number of steps comes around.");
172 8 : keys.add("compulsory","NINTERPOLATE","Number of grid points on which to do interpolation.");
173 8 : keys.add("compulsory","MIN_FORCE","Minimum force (per CV) to use for sampling. Units: [Energy]/[CV] (can be negative).");
174 8 : keys.add("compulsory","MAX_FORCE","Maximum force (per CV) to use for sampling.");
175 8 : keys.add("compulsory","CENTER","0","The CV value at which the applied bias energy will be zero");
176 8 : keys.add("optional","KBT","The system temperature in units of KB*T. If not provided will be taken from MD code (if available)");
177 :
178 8 : keys.add("optional","INITIAL_WEIGHT_DIST","Starting distribution for the force weights (options: UNIFORM, EXP, GAUSS).");
179 8 : keys.add("optional","INITIAL_WEIGHT_RATE","Rate of decay for exponential and gaussian distributions. W(F)~exp(-r |F|^d).");
180 :
181 8 : keys.add("optional","RESTART_FMT","the format that should be used to output real numbers in FISST restarts.");
182 8 : keys.add("optional","OUT_RESTART","Output file for all information needed to continue FISST simulation."
183 : "If you have the RESTART directive set (global or for FISST), this file will be appended to."
184 : "Note that the header will be printed again if appending.");
185 8 : keys.add("optional","IN_RESTART","Read this file to continue an FISST simulation. "
186 : "If same as OUT_RESTART and you have not set the RESTART directive, the file will be backed-up and overwritten with new output."
187 : "If you do have the RESTART flag set and it is the same name as OUT_RESTART, this file will be appended.");
188 8 : keys.add("optional","OUT_OBSERVABLE","Output file putting weights needed to compute observables at different force values."
189 : "If you have the RESTART directive set (global or for FISST), this file will be appended to. "
190 : "Note that the header will be printed again if appending.");
191 8 : keys.add("optional","OBSERVABLE_FREQ","How often to write out observable weights (default=period).");
192 8 : keys.addFlag("FREEZE",false,"Fix bias weights at current level (only used for restarting).");
193 4 : keys.use("RESTART");
194 8 : keys.addOutputComponent("force2","default","squared value of force from the bias.");
195 8 : keys.addOutputComponent("_fbar","default", "For each named CV biased, there will be a corresponding output CV_fbar storing the current linear bias prefactor.");
196 4 : }
197 :
198 2 : FISST::FISST(const ActionOptions&ao):
199 : PLUMED_BIAS_INIT(ao),
200 2 : ncvs_(getNumberOfArguments()),
201 0 : current_avg_force_(ncvs_,0.0),
202 2 : center_(ncvs_,0.0),
203 : //change min_force and max_force to vectors if going to do more than one cv
204 2 : min_force_(0.0),
205 2 : max_force_(0.0),
206 2 : in_restart_name_(""),
207 2 : out_restart_name_(""),
208 2 : out_observable_name_(""),
209 2 : fmt_("%e"),
210 2 : b_freeze_(false),
211 2 : b_restart_(false),
212 2 : b_write_restart_(false),
213 2 : b_write_observable_(false),
214 2 : b_first_restart_sample_(true),
215 2 : n_interpolation_(0),
216 2 : n_samples_(0),
217 2 : initial_weight_rate_(0),
218 2 : initial_weight_dist_("UNIFORM"),
219 2 : period_(0),
220 2 : reset_period_(0),
221 2 : observable_freq_(0),
222 2 : kbt_(0.0),
223 6 : value_force2_(NULL) {
224 2 : if(ncvs_==0) {
225 0 : error("Must specify at least one CV with ARG");
226 : }
227 :
228 : //temporary
229 2 : if(ncvs_>1) {
230 0 : error("FISST only supports using one CV right now");
231 : }
232 :
233 4 : addComponent("force2");
234 2 : componentIsNotPeriodic("force2");
235 2 : value_force2_ = getPntrToComponent("force2");
236 :
237 4 : for(unsigned int i = 0; i<ncvs_; i++) {
238 2 : std::string comp = getPntrToArgument(i)->getName() + "_fbar";
239 2 : addComponent(comp);
240 2 : componentIsNotPeriodic(comp);
241 : }
242 :
243 2 : parseVector("CENTER",center_);
244 : //change min_force and max_force to vectors if going to do more than one cv
245 2 : parse("MIN_FORCE",min_force_);
246 2 : parse("MAX_FORCE",max_force_);
247 2 : parse("PERIOD",period_);
248 2 : parse("RESET_PERIOD",reset_period_);
249 2 : parse("INITIAL_WEIGHT_DIST",initial_weight_dist_);
250 2 : parse("INITIAL_WEIGHT_RATE",initial_weight_rate_);
251 2 : parse("OBSERVABLE_FREQ",observable_freq_);
252 2 : parse("NINTERPOLATE",n_interpolation_);
253 2 : parseFlag("FREEZE",b_freeze_);
254 2 : parse("KBT",kbt_);
255 2 : parse("RESTART_FMT", fmt_);
256 2 : fmt_ = " " + fmt_;//add space since parse strips them
257 2 : parse("OUT_RESTART",out_restart_name_);
258 2 : parse("OUT_OBSERVABLE",out_observable_name_);
259 2 : parse("IN_RESTART",in_restart_name_);
260 2 : checkRead();
261 :
262 2 : if(center_.size() != ncvs_) {
263 0 : error("Must have same number of CENTER arguments as ARG arguments");
264 : }
265 :
266 2 : if(in_restart_name_ != "") {
267 0 : b_restart_ = true;
268 0 : log.printf(" reading simulation information from file: %s\n",in_restart_name_.c_str());
269 0 : readInRestart();
270 : } else {
271 :
272 2 : if(! kbt_ > 0.0) {
273 2 : kbt_=getkBT();
274 : }
275 :
276 : //in driver, this results in kbt of 0
277 2 : if(kbt_ == 0) {
278 0 : error(" Unable to determine valid kBT. "
279 : "Could be because you are runnning from driver or MD didn't give temperature.\n"
280 : "Consider setting temperature manually with the KBT keyword.");
281 : }
282 :
283 2 : log.printf(" kBT = %f\n",kbt_);
284 2 : log.printf(" Updating with a time scale of %i steps\n",period_);
285 :
286 2 : log.printf(" Using centers for CVs of:");
287 4 : for(unsigned int i = 0; i< ncvs_; i++) {
288 2 : log.printf(" %f ",center_[i]);
289 : }
290 2 : log.printf("\n");
291 2 : observable_weight_.resize(n_interpolation_);
292 64 : for(unsigned int i = 0; i<n_interpolation_; i++) {
293 62 : observable_weight_[i] = 1.0;
294 : }
295 :
296 2 : forces_.resize(n_interpolation_);
297 2 : force_weight_.resize(n_interpolation_);
298 : //using code from the MIST project
299 2 : gauss_weight_.resize(n_interpolation_);
300 2 : legendre_compute_glr(n_interpolation_, &forces_[0], &gauss_weight_[0]);
301 2 : rescale(min_force_, max_force_, n_interpolation_, &forces_[0], &gauss_weight_[0]);
302 :
303 2 : log.printf("Using weight distribution %s with rate %f\n",initial_weight_dist_.c_str(),initial_weight_rate_);
304 2 : if(initial_weight_dist_ == "UNIFORM" ) {
305 32 : for(unsigned int i = 0; i<n_interpolation_; i++) {
306 31 : force_weight_[i] = 1.0;
307 : }
308 1 : } else if (initial_weight_dist_ == "EXP" ) {
309 32 : for(unsigned int i = 0; i<n_interpolation_; i++) {
310 31 : force_weight_[i] = exp(-fabs(forces_[i])*initial_weight_rate_);
311 : }
312 0 : } else if (initial_weight_dist_ == "GAUSS" ) {
313 0 : for(unsigned int i = 0; i<n_interpolation_; i++) {
314 0 : force_weight_[i] = exp(-pow(forces_[i],2)*initial_weight_rate_);
315 : }
316 : } else {
317 0 : error(" Specified weight distribution is not from the allowed list.");
318 :
319 : }
320 :
321 2 : partition_estimate_.resize(n_interpolation_);
322 2 : NormalizeForceWeights();
323 : double sum = 0.0;
324 64 : for(unsigned int i = 0; i<n_interpolation_; i++) {
325 : //setting partition estimate as 1/w_i
326 62 : partition_estimate_[i] = 1/force_weight_[i];
327 62 : log.printf("force/gauss weight/force_weight: %i %f %f %f\n",i,forces_[i],gauss_weight_[i],force_weight_[i]);
328 62 : sum+=gauss_weight_[i]*force_weight_[i];
329 : }
330 2 : log.printf("--Sum_i w_i g_i: %f\n",sum);
331 :
332 : }
333 :
334 : //set inverse temperature
335 2 : beta_ = 1/kbt_;
336 :
337 2 : if(b_freeze_ && b_restart_) {
338 0 : log.printf(" freezing weights read in from the restart file\n");
339 : }
340 :
341 2 : if(out_restart_name_.length()>0) {
342 2 : log.printf(" writing restart information every %i steps to file %s with format %s\n",abs(period_),out_restart_name_.c_str(), fmt_.c_str());
343 2 : b_write_restart_ = true;
344 2 : setupOutRestart();
345 : }
346 2 : if(out_observable_name_.length()>0) {
347 2 : if(observable_freq_==0) {
348 2 : observable_freq_ = period_;
349 : }
350 2 : log.printf(" writing observable information every %i steps to file %s with format %s\n",observable_freq_,out_observable_name_.c_str(), fmt_.c_str());
351 2 : b_write_observable_ = true;
352 2 : setupOutObservable();
353 : }
354 :
355 : //add citation later:
356 : //log<<" Bibliography "<<plumed.cite("")<<"\n";
357 2 : }
358 :
359 12 : void FISST::NormalizeForceWeights() {
360 : double denom = 0.0;
361 :
362 384 : for(unsigned i=0; i<n_interpolation_; i++) {
363 372 : denom += gauss_weight_[i] * force_weight_[i];
364 : }
365 :
366 384 : for(unsigned i=0; i<n_interpolation_; i++) {
367 372 : force_weight_[i] /= denom;
368 : }
369 12 : }
370 :
371 0 : void FISST::readInRestart() {
372 0 : in_restart_.open(in_restart_name_);
373 :
374 0 : if(in_restart_.FieldExist("kbt")) {
375 0 : in_restart_.scanField("kbt",kbt_);
376 : } else {
377 0 : error("No field 'kbt' in restart file");
378 : }
379 0 : log.printf(" with kBT = %f\n",kbt_);
380 :
381 0 : if(in_restart_.FieldExist("period")) {
382 0 : in_restart_.scanField("period",period_);
383 : } else {
384 0 : error("No field 'period' in restart file");
385 : }
386 0 : log.printf(" Updating every %i steps\n",period_);
387 :
388 : //this one can be optional
389 0 : if(in_restart_.FieldExist("reset_period")) {
390 0 : in_restart_.scanField("reset_period",reset_period_);
391 : }
392 0 : log.printf(" Resetting statistics every %i steps\n",reset_period_);
393 :
394 0 : if(in_restart_.FieldExist("n_interpolation")) {
395 0 : in_restart_.scanField("n_interpolation",n_interpolation_);
396 : } else {
397 0 : error("No field 'n_interpolation' in restart file");
398 : }
399 :
400 0 : if(in_restart_.FieldExist("min_force")) {
401 0 : in_restart_.scanField("min_force",min_force_);
402 : } else {
403 0 : error("No field 'min_force' in restart file");
404 : }
405 0 : if(in_restart_.FieldExist("max_force")) {
406 0 : in_restart_.scanField("max_force",max_force_);
407 : } else {
408 0 : error("No field 'max_force' in restart file");
409 : }
410 0 : log.printf(" with forces from min_force=%e to max_force=%e over %i bins\n",min_force_,max_force_,n_interpolation_);
411 :
412 : unsigned int N = 0;
413 : std::string cv_name;
414 : double tmp, time;
415 :
416 0 : while(in_restart_.scanField("time",time)) {
417 0 : in_restart_.scanField("nsamples",n_samples_);
418 :
419 0 : observable_weight_.resize(n_interpolation_);
420 0 : partition_estimate_.resize(n_interpolation_);
421 0 : force_weight_.resize(n_interpolation_);
422 0 : gauss_weight_.resize(n_interpolation_);
423 0 : forces_.resize(n_interpolation_);
424 :
425 0 : for(unsigned int i = 0; i<ncvs_; ++i) {
426 : cv_name = getPntrToArgument(i)->getName();
427 0 : in_restart_.scanField(cv_name,tmp);
428 0 : for(unsigned int j =0; j<n_interpolation_; ++j) {
429 0 : in_restart_.scanField(cv_name + "_f"+std::to_string(j),forces_[j]);
430 0 : in_restart_.scanField(cv_name + "_g"+std::to_string(j),gauss_weight_[j]);
431 0 : in_restart_.scanField(cv_name + "_w"+std::to_string(j),force_weight_[j]);
432 0 : in_restart_.scanField(cv_name + "_z"+std::to_string(j),partition_estimate_[j]);
433 : }
434 : }
435 : N++;
436 :
437 0 : in_restart_.scanField();
438 : }
439 :
440 : double sum = 0.0;
441 0 : for(unsigned int j =0; j<n_interpolation_; ++j) {
442 : //clear observable weight, which will be set later
443 0 : observable_weight_[j] = 1.0;
444 :
445 : //setting partition estimate as 1/w_i
446 0 : log.printf("force/gauss weight/force_weight: %i %e %e %e\n",j,forces_[j],gauss_weight_[j],force_weight_[j]);
447 0 : sum+=gauss_weight_[j]*force_weight_[j];
448 : }
449 0 : log.printf("--Sum_i w_i g_i: %f\n",sum);
450 :
451 0 : in_restart_.close();
452 0 : }
453 :
454 2 : void FISST::setupOutObservable() {
455 2 : out_observable_.link(*this);
456 2 : out_observable_.fmtField(fmt_);
457 2 : out_observable_.open(out_observable_name_);
458 : out_observable_.setHeavyFlush();
459 :
460 4 : out_observable_.addConstantField("kbt").printField("kbt",kbt_);
461 4 : out_observable_.addConstantField("n_interpolation").printField("n_interpolation",n_interpolation_);
462 4 : out_observable_.addConstantField("period").printField("period",period_);
463 4 : out_observable_.addConstantField("min_force").printField("min_force",min_force_);
464 4 : out_observable_.addConstantField("max_force").printField("max_force",max_force_);
465 2 : }
466 :
467 2 : void FISST::setupOutRestart() {
468 2 : out_restart_.link(*this);
469 2 : out_restart_.fmtField(fmt_);
470 2 : out_restart_.open(out_restart_name_);
471 : out_restart_.setHeavyFlush();
472 :
473 4 : out_restart_.addConstantField("kbt").printField("kbt",kbt_);
474 4 : out_restart_.addConstantField("n_interpolation").printField("n_interpolation",n_interpolation_);
475 4 : out_restart_.addConstantField("period").printField("period",period_);
476 2 : if(reset_period_>0) {
477 0 : out_restart_.addConstantField("reset_period").printField("reset_period",reset_period_);
478 : }
479 4 : out_restart_.addConstantField("min_force").printField("min_force",min_force_);
480 4 : out_restart_.addConstantField("max_force").printField("max_force",max_force_);
481 2 : }
482 :
483 10 : void FISST::writeOutRestart() {
484 : std::string cv_name;
485 10 : out_restart_.printField("time",getTimeStep()*getStep());
486 10 : out_restart_.printField("nsamples",n_samples_);
487 :
488 20 : for(unsigned int i = 0; i<ncvs_; ++i) {
489 : cv_name = getPntrToArgument(i)->getName();
490 10 : double Q_i = difference(i, center_[i], getArgument(i));
491 10 : out_restart_.printField(cv_name,Q_i);
492 320 : for(int j = 0; j < n_interpolation_; j++ ) {
493 : //have to update this for multiple cvs
494 620 : out_restart_.printField(cv_name + "_f"+std::to_string(j),forces_[j]);
495 620 : out_restart_.printField(cv_name + "_g"+std::to_string(j),gauss_weight_[j]);
496 620 : out_restart_.printField(cv_name + "_w"+std::to_string(j),force_weight_[j]);
497 620 : out_restart_.printField(cv_name + "_z"+std::to_string(j),partition_estimate_[j]);
498 : }
499 : }
500 10 : out_restart_.printField();
501 10 : }
502 :
503 10 : void FISST::writeOutObservable() {
504 : std::string cv_name;
505 10 : out_observable_.printField("time",getTimeStep()*getStep());
506 10 : out_observable_.printField("nsamples",n_samples_);
507 :
508 20 : for(unsigned int i = 0; i<ncvs_; ++i) {
509 : cv_name = getPntrToArgument(i)->getName();
510 10 : double Q_i = difference(i, center_[i], getArgument(i));
511 10 : out_observable_.printField(cv_name,Q_i);
512 10 : out_observable_.printField(cv_name + "_fbar",current_avg_force_[i]);
513 320 : for(int j = 0; j < n_interpolation_; j++ ) {
514 : //have to update this for multiple cvs
515 620 : out_observable_.printField(cv_name + "_f"+std::to_string(j),forces_[j]);
516 620 : out_observable_.printField(cv_name + "_ow"+std::to_string(j),observable_weight_[j]);
517 : }
518 : }
519 10 : out_observable_.printField();
520 10 : }
521 :
522 :
523 10 : void FISST::calculate() {
524 10 : if(getStep() == 0 ) {
525 2 : if(b_write_restart_) {
526 2 : writeOutRestart();
527 : }
528 2 : if(b_write_observable_) {
529 2 : writeOutObservable();
530 : }
531 : }
532 :
533 10 : if(! b_freeze_) {
534 10 : if(b_restart_ && b_first_restart_sample_) {
535 : //dont' update statistics if restarting and first sample
536 0 : b_first_restart_sample_ = false;
537 : } else {
538 10 : update_statistics();
539 : }
540 : }
541 10 : update_bias();
542 10 : apply_bias();
543 :
544 : //check about writing restart file
545 10 : if(getStep()>0 && getStep()%period_==0) {
546 8 : if(b_write_restart_) {
547 8 : writeOutRestart();
548 : }
549 : }
550 10 : if(getStep()>0 && getStep()%observable_freq_==0) {
551 8 : if(b_write_observable_) {
552 8 : compute_observable_weight();
553 8 : writeOutObservable();
554 : }
555 : }
556 10 : }
557 :
558 :
559 10 : void FISST::apply_bias() {
560 : //Compute linear force as in "restraint"
561 : double ene = 0, totf2 = 0, cv, m, f;
562 :
563 20 : for(unsigned int i = 0; i < ncvs_; ++i) {
564 10 : cv = difference(i, center_[i], getArgument(i));
565 10 : double fbar = current_avg_force_[i];
566 10 : ene -= fbar*cv;
567 10 : setOutputForce(i,fbar);
568 10 : totf2 += fbar*fbar;
569 :
570 10 : std::string fbar_name_ = getPntrToArgument(i)->getName() + "_fbar";
571 10 : Value* fbar_ = getPntrToComponent(fbar_name_);
572 : fbar_->set(fbar);
573 : };
574 :
575 10 : setBias(ene);
576 10 : value_force2_->set(totf2);
577 : //log.flush();
578 10 : }
579 :
580 10 : void FISST::update_statistics() {
581 : //get stride is for multiple time stepping
582 10 : double dt=getTimeStep()*getStride();
583 10 : double h = dt/(period_*getTimeStep());
584 : double fbar_denum_integral = 0.0;
585 :
586 10 : int step = getStep();
587 10 : if(reset_period_>0 && step>0 && step%reset_period_==0) {
588 0 : n_samples_=1;
589 : } else {
590 10 : n_samples_++;
591 : }
592 10 : double d_n_samples = (double)n_samples_;
593 :
594 20 : for(unsigned int i = 0; i < ncvs_; ++i) {
595 10 : double Q_i = difference(i, center_[i], getArgument(i));
596 320 : for(unsigned int j=0; j<n_interpolation_; j++) {
597 : //if multiple cvs, these need to be updated to have 2 columns
598 310 : double f_j = forces_[j];
599 310 : double w_j = force_weight_[j];
600 310 : double g_j = gauss_weight_[j];
601 :
602 310 : fbar_denum_integral += g_j * w_j * exp(beta_*f_j * Q_i);
603 : }
604 :
605 320 : for(unsigned int j=0; j<n_interpolation_; j++) {
606 310 : double f_j = forces_[j];
607 310 : double sample_weight = exp(beta_*f_j * Q_i) / fbar_denum_integral;
608 :
609 310 : partition_estimate_[j] = sample_weight/d_n_samples + partition_estimate_[j]*(d_n_samples-1)/(d_n_samples);
610 :
611 310 : double w_jn = force_weight_[j];
612 310 : double z_jn = partition_estimate_[j];
613 :
614 310 : double w_jp1 = (1.0 - h) * w_jn + h / z_jn;
615 310 : force_weight_[j] = w_jp1;
616 : }
617 : }
618 :
619 : // make sure that the weights are normalised
620 10 : NormalizeForceWeights();
621 10 : }
622 :
623 :
624 10 : void FISST::update_bias() {
625 20 : for(unsigned int i = 0; i < ncvs_; ++i) {
626 10 : double Q_i = difference(i, center_[i], getArgument(i));
627 : double fbar_num_integral = 0.0;
628 : double fbar_denum_integral = 0.0;
629 :
630 320 : for(unsigned int j=0; j<n_interpolation_; j++ ) {
631 310 : double f_j = forces_[j];
632 310 : double w_j = force_weight_[j];
633 310 : double g_j = gauss_weight_[j];
634 :
635 310 : fbar_num_integral += g_j * f_j * w_j * exp(beta_*f_j*Q_i);
636 310 : fbar_denum_integral += g_j * w_j * exp(beta_*f_j*Q_i);
637 : }
638 :
639 10 : current_avg_force_[i] = fbar_num_integral/fbar_denum_integral;
640 : }
641 10 : }
642 :
643 8 : void FISST::compute_observable_weight() {
644 8 : double obs_num = (max_force_ - min_force_);
645 :
646 16 : for(unsigned int i = 0; i < ncvs_; ++i) {
647 8 : double Q_i = difference(i, center_[i], getArgument(i));
648 :
649 256 : for(unsigned int j=0; j<n_interpolation_; j++ ) {
650 248 : double z_j = partition_estimate_[j];
651 248 : double f_j = forces_[j];
652 : double denum_integral = 0.0;
653 :
654 7936 : for( unsigned int k=0; k<n_interpolation_; k++ ) {
655 7688 : double f_k = forces_[k];
656 7688 : double w_k = force_weight_[k];
657 7688 : double g_k = gauss_weight_[k];
658 :
659 7688 : denum_integral += g_k * w_k * exp(beta_*(f_k-f_j)*Q_i);
660 : }
661 248 : observable_weight_[j] = obs_num/(denum_integral*z_j);
662 : }
663 : }
664 8 : }
665 :
666 :
667 :
668 10 : void FISST::update() {
669 : //pass
670 10 : }
671 :
672 4 : FISST::~FISST() {
673 2 : out_restart_.close();
674 2 : out_observable_.close();
675 6 : }
676 :
677 0 : void FISST::turnOnDerivatives() {
678 : // do nothing
679 : // this is to avoid errors triggered when a bias is used as a CV
680 : // (This is done in ExtendedLagrangian.cpp)
681 0 : }
682 :
683 :
684 : }
685 : }//close the 2 namespaces
|