Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2017 of Glen Hocky and Andrew White
3 :
4 : The eds 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 eds 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 "bias/ReweightBase.h"
19 : #include "core/ActionAtomistic.h"
20 : #include "core/ActionRegister.h"
21 : #include "core/PlumedMain.h"
22 : #include "tools/File.h"
23 : #include "tools/Matrix.h"
24 : #include "tools/Random.h"
25 :
26 : #include <iostream>
27 :
28 : using namespace PLMD;
29 : using namespace bias;
30 :
31 : // namespace is lowercase to match
32 : // module names being all lowercase
33 :
34 : namespace PLMD
35 : {
36 : namespace eds
37 : {
38 :
39 : //+PLUMEDOC EDSMOD_BIAS EDS
40 : /*
41 : Add a linear bias on a set of observables.
42 :
43 : This force is the same as the linear part of the bias in \ref
44 : RESTRAINT, but this bias has the ability to compute the prefactors
45 : adaptively using the scheme of White and Voth \cite white2014efficient
46 : in order to match target observable values for a set of CVs.
47 : Further updates to the algorithm are described in \cite hocky2017cgds
48 : and you can read a review on the method and its applications here: \cite Amirkulova2019Recent.
49 :
50 : You can
51 : see a tutorial on EDS specifically for biasing coordination number at
52 : <a
53 : href="http://thewhitelab.org/blog/tutorial/2017/05/10/lammps-coordination-number-tutorial/">
54 : Andrew White's webpage</a>.
55 :
56 : The addition to the potential is of the form
57 : \f[
58 : \sum_i \frac{\alpha_i}{s_i} x_i
59 : \f]
60 :
61 : where for CV \f$x_i\f$, a coupling constant \f${\alpha}_i\f$ is determined
62 : adaptively or set by the user to match a target value for
63 : \f$x_i\f$. \f$s_i\f$ is a scale parameter, which by default is set to
64 : the target value. It may also be set separately.
65 :
66 : \warning
67 : It is not possible to set the target value of the observable
68 : to zero with the default value of \f$s_i\f$ as this will cause a
69 : divide-by-zero error. Instead, set \f$s_i=1\f$ or modify the CV so the
70 : desired target value is no longer zero.
71 :
72 : Notice that a similar method is available as \ref MAXENT, although with different features and using a different optimization algorithm.
73 :
74 : \par Virial
75 :
76 : The bias forces modify the virial and this can change your simulation density if the bias is used in an NPT simulation.
77 : One way to avoid changing the virial contribution from the bias is to add the keyword VIRIAL=1.0, which changes how the bias
78 : is computed to minimize its contribution to the virial. This can also lead to smaller magnitude biases that are more robust if
79 : transferred to other systems. VIRIAL=1.0 can be a reasonable starting point and increasing the value changes the balance between matching
80 : the set-points and minimizing the virial. See \cite Amirkulova2019Recent for details on the equations. Since the coupling constants
81 : are unique with a single CV, VIRIAL is not applicable with a single CV. When used with multiple CVs, the CVs should be correlated
82 : which is almost always the case.
83 :
84 : \par Weighting
85 :
86 : EDS computes means and variances as part of its algorithm. If you are
87 : also using a biasing method like metadynamics, you may wish to remove
88 : the effect of this bias in your EDS computations so that EDS works on
89 : the canonical values (reweighted to be unbiased). For example, you may be using
90 : metadynamics to bias a dihedral angle to enhance sampling and be using
91 : EDS to set the average distance between two particular atoms. Specifically:
92 :
93 : \plumedfile
94 : # set-up metadynamics
95 : t: TORSION ATOMS=1,2,3,4
96 : md: METAD ARG=d SIGMA=0.2 HEIGHT=0.3 PACE=500 TEMP=300
97 : # compute bias weights
98 : bias: REWEIGHT_METAD TEMP=300
99 : # now do EDS on distance while removing effect of metadynamics
100 : d: DISTANCE ATOMS=4,7
101 : eds: EDS ARG=d CENTER=3.0 PERIOD=100 TEMP=300 LOGWEIGHTS=bias
102 : \endplumedfile
103 :
104 : This is an approximation though because EDS uses a finite samples while running to get means/variances.
105 : At the end of a run,
106 : you should ensure this approach worked and indeed your reweighted CV matches the target value.
107 :
108 : \par Examples
109 :
110 : The following input for a harmonic oscillator of two beads will
111 : adaptively find a linear bias to change the mean and variance to the
112 : target values. The PRINT line shows how to access the value of the
113 : coupling constants.
114 :
115 : \plumedfile
116 : dist: DISTANCE ATOMS=1,2
117 : # this is the squared of the distance
118 : dist2: COMBINE ARG=dist POWERS=2 PERIODIC=NO
119 :
120 : # bias mean and variance
121 : eds: EDS ARG=dist,dist2 CENTER=2.0,1.0 PERIOD=100 TEMP=1.0
122 : PRINT ARG=dist,dist2,eds.dist_coupling,eds.dist2_coupling,eds.bias,eds.force2 FILE=colvars.dat STRIDE=100
123 : \endplumedfile
124 :
125 : <hr>
126 :
127 : Rather than trying to find the coupling constants adaptively, one can ramp up to a constant value.
128 : \plumedfile
129 : dist: DISTANCE ATOMS=1,2
130 : dist2: COMBINE ARG=dist POWERS=2 PERIODIC=NO
131 :
132 : # ramp couplings from 0,0 to -1,1 over 50000 steps
133 : eds: EDS ARG=dist,dist2 CENTER=2.0,1.0 FIXED=-1,1 RAMP PERIOD=50000 TEMP=1.0
134 :
135 : # same as above, except starting at -0.5,0.5 rather than default of 0,0
136 : eds2: EDS ARG=dist,dist2 CENTER=2.0,1.0 FIXED=-1,1 INIT=-0.5,0.5 RAMP PERIOD=50000 TEMP=1.0
137 : \endplumedfile
138 :
139 : <hr>
140 : A restart file can be added to dump information needed to restart/continue simulation using these parameters every PERIOD.
141 : \plumedfile
142 : dist: DISTANCE ATOMS=1,2
143 : dist2: COMBINE ARG=dist POWERS=2 PERIODIC=NO
144 :
145 : # add the option to write to a restart file
146 : eds: EDS ARG=dist,dist2 CENTER=2.0,1.0 PERIOD=100 TEMP=1.0 OUT_RESTART=checkpoint.eds
147 : \endplumedfile
148 :
149 : The first few lines of the restart file that is output if we run a calculation with one CV will look something like this:
150 :
151 : \auxfile{restart.eds}
152 : #! FIELDS time d1_center d1_set d1_target d1_coupling d1_maxrange d1_maxgrad d1_accum d1_mean d1_std
153 : #! SET adaptive 1
154 : #! SET update_period 1
155 : #! SET seed 0
156 : #! SET kbt 2.4943
157 : 0.0000 1.0000 0.0000 0.0000 0.0000 7.4830 0.1497 0.0000 0.0000 0.0000
158 : 1.0000 1.0000 0.0000 0.0000 0.0000 7.4830 0.1497 0.0000 0.0000 0.0000
159 : 2.0000 1.0000 -7.4830 0.0000 0.0000 7.4830 0.1497 0.0224 0.0000 0.0000
160 : 3.0000 1.0000 -7.4830 0.0000 -7.4830 7.4830 0.1497 0.0224 0.0000 0.0000
161 : 4.0000 1.0000 -7.4830 0.0000 -7.4830 7.4830 0.1497 0.0224 0.0000 0.0000
162 : \endauxfile
163 :
164 : <hr>
165 :
166 : Read in a previous restart file. Adding RESTART flag makes output append
167 : \plumedfile
168 : d1: DISTANCE ATOMS=1,2
169 :
170 : eds: EDS ARG=d1 CENTER=2.0 PERIOD=100 TEMP=1.0 IN_RESTART=restart.eds RESTART=YES
171 : \endplumedfile
172 :
173 : <hr>
174 :
175 : Read in a previous restart file and freeze the bias at the final level from the previous simulation
176 : \plumedfile
177 : d1: DISTANCE ATOMS=1,2
178 :
179 : eds: EDS ARG=d1 CENTER=2.0 TEMP=1.0 IN_RESTART=restart.eds FREEZE
180 : \endplumedfile
181 :
182 : <hr>
183 :
184 : Read in a previous restart file and freeze the bias at the mean from the previous simulation
185 : \plumedfile
186 : d1: DISTANCE ATOMS=1,2
187 :
188 : eds: EDS ARG=d1 CENTER=2.0 TEMP=1.0 IN_RESTART=restart.eds FREEZE MEAN
189 : \endplumedfile
190 :
191 :
192 : */
193 : //+ENDPLUMEDOC
194 :
195 : class EDS : public Bias
196 : {
197 :
198 : private:
199 : /*We will get this and store it once, since on-the-fly changing number of CVs will be fatal*/
200 : const unsigned int ncvs_;
201 : std::vector<double> center_;
202 : std::vector<Value *> center_values_;
203 : ReweightBase *logweights_; // weights to use if reweighting averages
204 : std::vector<double> scale_;
205 : std::vector<double> current_coupling_; // actually current coupling
206 : std::vector<double> set_coupling_; // what our coupling is ramping up to. Equal to current_coupling when gathering stats
207 : std::vector<double> target_coupling_; // used when loaded to reach a value
208 : std::vector<double> max_coupling_range_; // used for adaptive range
209 : std::vector<double> max_coupling_grad_; // maximum allowed gradient
210 : std::vector<double> coupling_rate_;
211 : std::vector<double> coupling_accum_;
212 : std::vector<double> means_;
213 : std::vector<double> differences_;
214 : std::vector<double> alpha_vector_;
215 : std::vector<double> alpha_vector_2_;
216 : std::vector<double> ssds_;
217 : std::vector<double> step_size_;
218 : std::vector<double> pseudo_virial_;
219 : std::vector<Value *> out_coupling_;
220 : Matrix<double> covar_;
221 : Matrix<double> covar2_;
222 : Matrix<double> lm_inv_;
223 : std::string in_restart_name_;
224 : std::string out_restart_name_;
225 : std::string fmt_;
226 : OFile out_restart_;
227 : IFile in_restart_;
228 : bool b_c_values_;
229 : bool b_adaptive_;
230 : bool b_freeze_;
231 : bool b_equil_;
232 : bool b_ramp_;
233 : bool b_covar_;
234 : bool b_restart_;
235 : bool b_write_restart_;
236 : bool b_lm_;
237 : bool b_virial_;
238 : bool b_update_virial_;
239 : bool b_weights_;
240 : int seed_;
241 : int update_period_;
242 : int avg_coupling_count_;
243 : int update_calls_;
244 : double kbt_;
245 : double multi_prop_;
246 : double lm_mixing_par_;
247 : double virial_scaling_;
248 : double pseudo_virial_sum_; // net virial for all cvs in current period
249 : double max_logweight_; // maximum observed max logweight for period
250 : double wsum_; // sum of weights thus far
251 : Random rand_;
252 : Value *value_force2_;
253 : Value *value_pressure_;
254 :
255 : /*read input restart. b_mean sets if we use mean or final value for freeze*/
256 : void readInRestart(const bool b_mean);
257 : /*setup output restart*/
258 : void setupOutRestart();
259 : /*write output restart*/
260 : void writeOutRestart();
261 : void update_statistics();
262 : void update_pseudo_virial();
263 : void calc_lm_step_size();
264 : void calc_covar_step_size();
265 : void calc_ssd_step_size();
266 : void reset_statistics();
267 : void update_bias();
268 : void apply_bias();
269 :
270 : public:
271 : explicit EDS(const ActionOptions &);
272 : void calculate();
273 : void update();
274 : void turnOnDerivatives();
275 : static void registerKeywords(Keywords &keys);
276 : ~EDS();
277 : };
278 :
279 : PLUMED_REGISTER_ACTION(EDS, "EDS")
280 :
281 10 : void EDS::registerKeywords(Keywords &keys)
282 : {
283 10 : Bias::registerKeywords(keys);
284 20 : keys.add("optional", "CENTER", "The desired centers (equilibrium values) which will be sought during the adaptive linear biasing. This is for fixed centers");
285 20 : keys.addInputKeyword("optional", "CENTER_ARG", "scalar", "The desired centers (equilibrium values) which will be sought during the adaptive linear biasing. "
286 : "CENTER_ARG is for calculated centers, e.g. from a CV or analysis. ");
287 :
288 20 : keys.add("optional", "PERIOD", "Steps over which to adjust bias for adaptive or ramping");
289 20 : keys.add("compulsory", "RANGE", "25.0", "The (starting) maximum increase in coupling constant per PERIOD (in k_B T/[BIAS_SCALE unit]) for each CV biased");
290 20 : keys.add("compulsory", "SEED", "0", "Seed for random order of changing bias");
291 20 : keys.add("compulsory", "INIT", "0", "Starting value for coupling constant");
292 20 : keys.add("compulsory", "FIXED", "0", "Fixed target values for coupling constant. Non-adaptive.");
293 20 : keys.add("optional", "BIAS_SCALE", "A divisor to set the units of the bias. "
294 : "If not set, this will be the CENTER value by default (as is done in White and Voth 2014).");
295 20 : keys.add("optional", "TEMP", "The system temperature. If not provided will be taken from MD code (if available)");
296 20 : keys.add("optional", "MULTI_PROP", "What proportion of dimensions to update at each step. "
297 : "Must be in interval [1,0), where 1 indicates all and any other indicates a stochastic update. "
298 : "If not set, default is 1 / N, where N is the number of CVs. ");
299 20 : keys.add("optional", "VIRIAL", "Add an update penalty for having non-zero virial contributions. Only makes sense with multiple correlated CVs.");
300 20 : keys.addInputKeyword("optional", "LOGWEIGHTS", "scalar", "Add weights to use for computing statistics. For example, if biasing with metadynamics.");
301 20 : keys.addFlag("LM", false, "Use Levenberg-Marquadt algorithm along with simultaneous keyword. Otherwise use gradient descent.");
302 20 : keys.add("compulsory", "LM_MIXING", "1", "Initial mixing parameter when using Levenberg-Marquadt minimization.");
303 20 : keys.add("optional", "RESTART_FMT", "the format that should be used to output real numbers in EDS restarts");
304 20 : keys.add("optional", "OUT_RESTART", "Output file for all information needed to continue EDS simulation. "
305 : "If you have the RESTART directive set (global or for EDS), this file will be appended to. "
306 : "Note that the header will be printed again if appending.");
307 20 : keys.add("optional", "IN_RESTART", "Read this file to continue an EDS simulation. "
308 : "If same as OUT_RESTART and you have not set the RESTART directive, the file will be backed-up and overwritten with new output. "
309 : "If you do have the RESTART flag set and it is the same name as OUT_RESTART, this file will be appended.");
310 :
311 20 : keys.addFlag("RAMP", false, "Slowly increase bias constant to a fixed value");
312 20 : keys.addFlag("COVAR", false, "Utilize the covariance matrix when updating the bias. Default Off, but may be enabled due to other options");
313 20 : keys.addFlag("FREEZE", false, "Fix bias at current level (only used for restarting).");
314 20 : keys.addFlag("MEAN", false, "Instead of using final bias level from restart, use average. Can only be used in conjunction with FREEZE");
315 :
316 10 : keys.use("RESTART");
317 :
318 20 : keys.addOutputComponent("force2", "default", "scalar", "squared value of force from the bias");
319 20 : keys.addOutputComponent("pressure", "default", "scalar", "If using virial keyword, this is the current sum of virial terms. It is in units of pressure (energy / vol^3)");
320 20 : keys.addOutputComponent("_coupling", "default", "scalar", "For each named CV biased, there will be a corresponding output CV_coupling storing the current linear bias prefactor.");
321 10 : }
322 :
323 8 : EDS::EDS(const ActionOptions &ao) : PLUMED_BIAS_INIT(ao),
324 8 : ncvs_(getNumberOfArguments()),
325 8 : scale_(ncvs_, 0.0),
326 8 : current_coupling_(ncvs_, 0.0),
327 8 : set_coupling_(ncvs_, 0.0),
328 8 : target_coupling_(ncvs_, 0.0),
329 8 : max_coupling_range_(ncvs_, 25.0),
330 8 : max_coupling_grad_(ncvs_, 0.0),
331 8 : coupling_rate_(ncvs_, 1.0),
332 8 : coupling_accum_(ncvs_, 0.0),
333 8 : means_(ncvs_, 0.0),
334 8 : step_size_(ncvs_, 0.0),
335 8 : pseudo_virial_(ncvs_),
336 8 : out_coupling_(ncvs_, NULL),
337 8 : in_restart_name_(""),
338 8 : out_restart_name_(""),
339 8 : fmt_("%f"),
340 8 : b_adaptive_(true),
341 8 : b_freeze_(false),
342 8 : b_equil_(true),
343 8 : b_ramp_(false),
344 8 : b_covar_(false),
345 8 : b_restart_(false),
346 8 : b_write_restart_(false),
347 8 : b_lm_(false),
348 8 : b_virial_(false),
349 8 : b_weights_(false),
350 8 : seed_(0),
351 8 : update_period_(0),
352 8 : avg_coupling_count_(1),
353 8 : update_calls_(0),
354 8 : kbt_(0.0),
355 8 : multi_prop_(-1.0),
356 8 : lm_mixing_par_(0.1),
357 8 : virial_scaling_(0.),
358 8 : pseudo_virial_sum_(0.0),
359 8 : max_logweight_(0.0),
360 8 : wsum_(0.0),
361 32 : value_force2_(NULL)
362 : {
363 : double temp = -1.0;
364 8 : bool b_mean = false;
365 : std::vector<Value *> wvalues;
366 :
367 16 : addComponent("force2");
368 8 : componentIsNotPeriodic("force2");
369 8 : value_force2_ = getPntrToComponent("force2");
370 :
371 20 : for (unsigned int i = 0; i < ncvs_; ++i)
372 : {
373 12 : std::string comp = getPntrToArgument(i)->getName() + "_coupling";
374 12 : addComponent(comp);
375 12 : componentIsNotPeriodic(comp);
376 12 : out_coupling_[i] = getPntrToComponent(comp);
377 : }
378 :
379 8 : parseVector("CENTER", center_);
380 8 : parseArgumentList("CENTER_ARG", center_values_);
381 8 : parseArgumentList("LOGWEIGHTS", wvalues);
382 8 : parseVector("BIAS_SCALE", scale_);
383 8 : parseVector("RANGE", max_coupling_range_);
384 8 : parseVector("FIXED", target_coupling_);
385 8 : parseVector("INIT", set_coupling_);
386 8 : parse("PERIOD", update_period_);
387 8 : kbt_ = getkBT();
388 8 : parse("SEED", seed_);
389 8 : parse("MULTI_PROP", multi_prop_);
390 8 : parse("LM_MIXING", lm_mixing_par_);
391 8 : parse("RESTART_FMT", fmt_);
392 8 : parse("VIRIAL", virial_scaling_);
393 8 : fmt_ = " " + fmt_; // add space since parse strips them
394 8 : parse("OUT_RESTART", out_restart_name_);
395 8 : parseFlag("LM", b_lm_);
396 8 : parseFlag("RAMP", b_ramp_);
397 8 : parseFlag("FREEZE", b_freeze_);
398 8 : parseFlag("MEAN", b_mean);
399 8 : parseFlag("COVAR", b_covar_);
400 8 : parse("IN_RESTART", in_restart_name_);
401 8 : checkRead();
402 :
403 : /*
404 : * Things that are different when using changing centers:
405 : * 1. Scale
406 : * 2. The log file
407 : * 3. Reading Restarts
408 : */
409 :
410 8 : if (center_.size() == 0)
411 : {
412 1 : if (center_values_.size() == 0)
413 0 : error("Must set either CENTER or CENTER_ARG");
414 1 : else if (center_values_.size() != ncvs_)
415 0 : error("CENTER_ARG must contain the same number of variables as ARG");
416 1 : b_c_values_ = true;
417 1 : center_.resize(ncvs_);
418 1 : log.printf(" EDS will use possibly varying centers\n");
419 : }
420 : else
421 : {
422 7 : if (center_.size() != ncvs_)
423 0 : error("Must have same number of CENTER arguments as ARG arguments");
424 7 : else if (center_values_.size() != 0)
425 0 : error("You can only set CENTER or CENTER_ARG. Not both");
426 7 : b_c_values_ = false;
427 7 : log.printf(" EDS will use fixed centers\n");
428 : }
429 :
430 : // check for weights
431 8 : if (wvalues.size() > 1)
432 : {
433 0 : error("LOGWEIGHTS can only support one weight set. Please only pass one action");
434 : }
435 8 : else if (wvalues.size() == 1)
436 : {
437 1 : logweights_ = dynamic_cast<ReweightBase *>(wvalues[0]->getPntrToAction());
438 1 : b_weights_ = true;
439 : }
440 :
441 8 : log.printf(" setting scaling:");
442 8 : if (scale_.size() > 0 && scale_.size() < ncvs_)
443 : {
444 0 : error("the number of BIAS_SCALE values be the same as number of CVs");
445 : }
446 8 : else if (scale_.size() == 0 && b_c_values_)
447 : {
448 0 : log.printf(" Setting SCALE to be 1 for all CVs\n");
449 0 : scale_.resize(ncvs_);
450 0 : for (unsigned int i = 0; i < ncvs_; ++i)
451 0 : scale_[i] = 1;
452 : }
453 8 : else if (scale_.size() == 0 && !b_c_values_)
454 : {
455 2 : log.printf(" (default) ");
456 :
457 2 : scale_.resize(ncvs_);
458 6 : for (unsigned int i = 0; i < scale_.size(); ++i)
459 : {
460 4 : if (center_[i] == 0)
461 0 : error("BIAS_SCALE parameter has been set to CENTER value of 0 (as is default). This will divide by 0, so giving up. See doc for EDS bias");
462 4 : scale_[i] = center_[i];
463 : }
464 : }
465 : else
466 : {
467 14 : for (unsigned int i = 0; i < scale_.size(); ++i)
468 8 : log.printf(" %f", scale_[i]);
469 : }
470 8 : log.printf("\n");
471 :
472 8 : if (b_lm_)
473 : {
474 1 : log.printf(" EDS will perform Levenberg-Marquardt minimization with mixing parameter = %f\n", lm_mixing_par_);
475 1 : differences_.resize(ncvs_);
476 1 : alpha_vector_.resize(ncvs_);
477 1 : alpha_vector_2_.resize(ncvs_);
478 1 : covar_.resize(ncvs_, ncvs_);
479 1 : covar2_.resize(ncvs_, ncvs_);
480 1 : lm_inv_.resize(ncvs_, ncvs_);
481 1 : covar2_ *= 0;
482 1 : lm_inv_ *= 0;
483 1 : if (multi_prop_ != 1)
484 0 : log.printf(" WARNING - doing LM minimization but MULTI_PROP!=1\n");
485 : }
486 7 : else if (b_covar_)
487 : {
488 1 : log.printf(" EDS will utilize covariance matrix for update steps\n");
489 1 : covar_.resize(ncvs_, ncvs_);
490 : }
491 : else
492 : {
493 6 : log.printf(" EDS will utilize variance for update steps\n");
494 6 : ssds_.resize(ncvs_);
495 : }
496 :
497 8 : b_virial_ = virial_scaling_;
498 :
499 8 : if (b_virial_)
500 : {
501 1 : if (ncvs_ == 1)
502 0 : error("Minimizing the virial is only valid with multiply correlated collective variables.");
503 : // check that the CVs can be used to compute pseudo-virial
504 1 : log.printf(" EDS will compute virials of CVs and penalize with scale of %f. Checking CVs are valid...", virial_scaling_);
505 4 : for (unsigned int i = 0; i < ncvs_; ++i)
506 : {
507 3 : auto a = dynamic_cast<ActionAtomistic *>(getPntrToArgument(i)->getPntrToAction());
508 3 : if (!a)
509 0 : error("If using VIRIAL keyword, you must have normal CVs as arguments to EDS. Offending action: " + getPntrToArgument(i)->getPntrToAction()->getName());
510 : // cppcheck-suppress nullPointerRedundantCheck
511 3 : if (!(a->getPbc().isOrthorombic()))
512 3 : log.printf(" WARNING: EDS Virial should have a orthorombic cell\n");
513 : }
514 1 : log.printf("done.\n");
515 2 : addComponent("pressure");
516 1 : componentIsNotPeriodic("pressure");
517 1 : value_pressure_ = getPntrToComponent("pressure");
518 : }
519 :
520 8 : if (b_mean && !b_freeze_)
521 : {
522 0 : error("EDS keyword MEAN can only be used along with keyword FREEZE");
523 : }
524 :
525 8 : if (in_restart_name_ != "")
526 : {
527 2 : b_restart_ = true;
528 2 : log.printf(" reading simulation information from file: %s\n", in_restart_name_.c_str());
529 2 : readInRestart(b_mean);
530 : }
531 : else
532 : {
533 :
534 : // in driver, this results in kbt of 0
535 6 : if (kbt_ == 0)
536 : {
537 0 : error(" Unable to determine valid kBT. "
538 : "Could be because you are runnning from driver or MD didn't give temperature.\n"
539 : "Consider setting temperature manually with the TEMP keyword.");
540 : kbt_ = 1;
541 : }
542 :
543 6 : log.printf(" kBT = %f\n", kbt_);
544 6 : log.printf(" Updating every %i steps\n", update_period_);
545 :
546 6 : if (!b_c_values_)
547 : {
548 5 : log.printf(" with centers:");
549 14 : for (unsigned int i = 0; i < ncvs_; ++i)
550 : {
551 9 : log.printf(" %f ", center_[i]);
552 : }
553 : }
554 : else
555 : {
556 1 : log.printf(" with actions centers:");
557 2 : for (unsigned int i = 0; i < ncvs_; ++i)
558 : {
559 1 : log.printf(" %s ", center_values_[i]->getName().c_str());
560 : // add dependency on these actions
561 1 : addDependency(center_values_[i]->getPntrToAction());
562 : }
563 : }
564 :
565 6 : log.printf("\n with initial ranges / rates:\n");
566 16 : for (unsigned int i = 0; i < max_coupling_range_.size(); ++i)
567 : {
568 : // this is just an empirical guess. Bigger range, bigger grads. Less frequent updates, bigger changes
569 : //
570 : // using the current maxing out scheme, max_coupling_range is the biggest step that can be taken in any given interval
571 10 : max_coupling_range_[i] *= kbt_;
572 10 : max_coupling_grad_[i] = max_coupling_range_[i];
573 10 : log.printf(" %f / %f\n", max_coupling_range_[i], max_coupling_grad_[i]);
574 : }
575 :
576 6 : if (seed_ > 0)
577 : {
578 2 : log.printf(" setting random seed = %i", seed_);
579 2 : rand_.setSeed(seed_);
580 : }
581 :
582 16 : for (unsigned int i = 0; i < ncvs_; ++i)
583 10 : if (target_coupling_[i] != 0.0)
584 1 : b_adaptive_ = false;
585 :
586 6 : if (!b_adaptive_)
587 : {
588 1 : if (b_ramp_)
589 : {
590 1 : log.printf(" ramping up coupling constants over %i steps\n", update_period_);
591 : }
592 :
593 1 : log.printf(" with starting coupling constants");
594 2 : for (unsigned int i = 0; i < set_coupling_.size(); ++i)
595 1 : log.printf(" %f", set_coupling_[i]);
596 1 : log.printf("\n");
597 1 : log.printf(" and final coupling constants");
598 2 : for (unsigned int i = 0; i < target_coupling_.size(); ++i)
599 1 : log.printf(" %f", target_coupling_[i]);
600 1 : log.printf("\n");
601 : }
602 :
603 : // now do setup
604 6 : if (b_ramp_)
605 : {
606 1 : update_period_ *= -1;
607 : }
608 :
609 16 : for (unsigned int i = 0; i < set_coupling_.size(); ++i)
610 10 : current_coupling_[i] = set_coupling_[i];
611 :
612 : // if b_adaptive_, then first half will be used for equilibrating and second half for statistics
613 6 : if (update_period_ > 0)
614 : {
615 5 : update_period_ /= 2;
616 : }
617 : }
618 :
619 8 : if (b_freeze_)
620 : {
621 1 : b_adaptive_ = false;
622 1 : update_period_ = 0;
623 1 : if (b_mean)
624 : {
625 1 : log.printf(" freezing bias at the average level from the restart file\n");
626 : }
627 : else
628 : {
629 0 : log.printf(" freezing bias at current level\n");
630 : }
631 : }
632 :
633 8 : if (multi_prop_ == -1.0)
634 : {
635 5 : log.printf(" Will update each dimension stochastically with probability 1 / number of CVs\n");
636 5 : multi_prop_ = 1.0 / ncvs_;
637 : }
638 3 : else if (multi_prop_ > 0 && multi_prop_ <= 1.0)
639 : {
640 3 : log.printf(" Will update each dimension stochastically with probability %f\n", multi_prop_);
641 : }
642 : else
643 : {
644 0 : error(" MULTI_PROP must be between 0 and 1\n");
645 : }
646 :
647 8 : if (out_restart_name_.length() > 0)
648 : {
649 8 : log.printf(" writing restart information every %i steps to file %s with format %s\n", abs(update_period_), out_restart_name_.c_str(), fmt_.c_str());
650 8 : b_write_restart_ = true;
651 8 : setupOutRestart();
652 : }
653 :
654 16 : log << " Bibliography " << plumed.cite("White and Voth, J. Chem. Theory Comput. 10 (8), 3023-3030 (2014)") << "\n";
655 16 : log << " Bibliography " << plumed.cite("G. M. Hocky, T. Dannenhoffer-Lafage, G. A. Voth, J. Chem. Theory Comput. 13 (9), 4593-4603 (2017)") << "\n";
656 8 : }
657 :
658 2 : void EDS::readInRestart(const bool b_mean)
659 : {
660 2 : int adaptive_i = 0;
661 :
662 2 : in_restart_.open(in_restart_name_);
663 :
664 4 : if (in_restart_.FieldExist("kbt"))
665 : {
666 2 : in_restart_.scanField("kbt", kbt_);
667 : }
668 : else
669 : {
670 0 : error("No field 'kbt' in restart file");
671 : }
672 2 : log.printf(" with kBT = %f\n", kbt_);
673 :
674 4 : if (in_restart_.FieldExist("update_period"))
675 : {
676 2 : in_restart_.scanField("update_period", update_period_);
677 : }
678 : else
679 : {
680 0 : error("No field 'update_period' in restart file");
681 : }
682 2 : log.printf(" Updating every %i steps\n", update_period_);
683 :
684 4 : if (in_restart_.FieldExist("adaptive"))
685 : {
686 : // note, no version of scanField for boolean
687 2 : in_restart_.scanField("adaptive", adaptive_i);
688 : }
689 : else
690 : {
691 0 : error("No field 'adaptive' in restart file");
692 : }
693 2 : b_adaptive_ = bool(adaptive_i);
694 :
695 4 : if (in_restart_.FieldExist("seed"))
696 : {
697 2 : in_restart_.scanField("seed", seed_);
698 : }
699 : else
700 : {
701 0 : error("No field 'seed' in restart file");
702 : }
703 2 : if (seed_ > 0)
704 : {
705 0 : log.printf(" setting random seed = %i", seed_);
706 0 : rand_.setSeed(seed_);
707 : }
708 :
709 : double time, tmp;
710 2 : std::vector<double> avg_bias = std::vector<double>(center_.size());
711 : unsigned int N = 0;
712 : std::string cv_name;
713 :
714 24 : while (in_restart_.scanField("time", time))
715 : {
716 :
717 20 : for (unsigned int i = 0; i < ncvs_; ++i)
718 : {
719 : cv_name = getPntrToArgument(i)->getName();
720 20 : in_restart_.scanField(cv_name + "_center", set_coupling_[i]);
721 20 : in_restart_.scanField(cv_name + "_set", set_coupling_[i]);
722 20 : in_restart_.scanField(cv_name + "_target", target_coupling_[i]);
723 20 : in_restart_.scanField(cv_name + "_coupling", current_coupling_[i]);
724 20 : in_restart_.scanField(cv_name + "_maxrange", max_coupling_range_[i]);
725 20 : in_restart_.scanField(cv_name + "_maxgrad", max_coupling_grad_[i]);
726 20 : in_restart_.scanField(cv_name + "_accum", coupling_accum_[i]);
727 10 : in_restart_.scanField(cv_name + "_mean", means_[i]);
728 20 : if (in_restart_.FieldExist(cv_name + "_pseudovirial"))
729 : {
730 0 : if (b_virial_)
731 0 : in_restart_.scanField(cv_name + "_pseudovirial", pseudo_virial_[i]);
732 : else // discard the field
733 0 : in_restart_.scanField(cv_name + "_pseudovirial", tmp);
734 : }
735 : // unused due to difference between covar/nocovar
736 20 : in_restart_.scanField(cv_name + "_std", tmp);
737 :
738 10 : avg_bias[i] += current_coupling_[i];
739 : }
740 10 : N++;
741 :
742 10 : in_restart_.scanField();
743 : }
744 :
745 2 : log.printf(" with centers:");
746 4 : for (unsigned int i = 0; i < center_.size(); ++i)
747 : {
748 2 : log.printf(" %f", center_[i]);
749 : }
750 2 : log.printf("\n and scaling:");
751 4 : for (unsigned int i = 0; i < scale_.size(); ++i)
752 : {
753 2 : log.printf(" %f", scale_[i]);
754 : }
755 :
756 2 : log.printf("\n with initial ranges / rates:\n");
757 4 : for (unsigned int i = 0; i < max_coupling_range_.size(); ++i)
758 : {
759 2 : log.printf(" %f / %f\n", max_coupling_range_[i], max_coupling_grad_[i]);
760 : }
761 :
762 2 : if (!b_adaptive_ && update_period_ < 0)
763 : {
764 0 : log.printf(" ramping up coupling constants over %i steps\n", -update_period_);
765 : }
766 :
767 2 : if (b_mean)
768 : {
769 1 : log.printf("Loaded in averages for coupling constants...\n");
770 2 : for (unsigned int i = 0; i < current_coupling_.size(); ++i)
771 1 : current_coupling_[i] = avg_bias[i] / N;
772 2 : for (unsigned int i = 0; i < current_coupling_.size(); ++i)
773 1 : set_coupling_[i] = avg_bias[i] / N;
774 : }
775 :
776 2 : log.printf(" with current coupling constants:\n ");
777 4 : for (unsigned int i = 0; i < current_coupling_.size(); ++i)
778 2 : log.printf(" %f", current_coupling_[i]);
779 2 : log.printf("\n");
780 2 : log.printf(" with initial coupling constants:\n ");
781 4 : for (unsigned int i = 0; i < set_coupling_.size(); ++i)
782 2 : log.printf(" %f", set_coupling_[i]);
783 2 : log.printf("\n");
784 2 : log.printf(" and final coupling constants:\n ");
785 4 : for (unsigned int i = 0; i < target_coupling_.size(); ++i)
786 2 : log.printf(" %f", target_coupling_[i]);
787 2 : log.printf("\n");
788 :
789 2 : in_restart_.close();
790 2 : }
791 :
792 8 : void EDS::setupOutRestart()
793 : {
794 8 : out_restart_.link(*this);
795 8 : out_restart_.fmtField(fmt_);
796 8 : out_restart_.open(out_restart_name_);
797 : out_restart_.setHeavyFlush();
798 :
799 16 : out_restart_.addConstantField("adaptive").printField("adaptive", b_adaptive_);
800 16 : out_restart_.addConstantField("update_period").printField("update_period", update_period_);
801 16 : out_restart_.addConstantField("seed").printField("seed", seed_);
802 16 : out_restart_.addConstantField("kbt").printField("kbt", kbt_);
803 8 : }
804 :
805 27 : void EDS::writeOutRestart()
806 : {
807 : std::string cv_name;
808 27 : out_restart_.printField("time", getTimeStep() * getStep());
809 :
810 66 : for (unsigned int i = 0; i < ncvs_; ++i)
811 : {
812 : cv_name = getPntrToArgument(i)->getName();
813 78 : out_restart_.printField(cv_name + "_center", center_[i]);
814 78 : out_restart_.printField(cv_name + "_set", set_coupling_[i]);
815 78 : out_restart_.printField(cv_name + "_target", target_coupling_[i]);
816 78 : out_restart_.printField(cv_name + "_coupling", current_coupling_[i]);
817 78 : out_restart_.printField(cv_name + "_maxrange", max_coupling_range_[i]);
818 78 : out_restart_.printField(cv_name + "_maxgrad", max_coupling_grad_[i]);
819 78 : out_restart_.printField(cv_name + "_accum", coupling_accum_[i]);
820 39 : out_restart_.printField(cv_name + "_mean", means_[i]);
821 39 : if (b_virial_)
822 18 : out_restart_.printField(cv_name + "_pseudovirial", pseudo_virial_[i]);
823 39 : if (!b_covar_ && !b_lm_)
824 42 : out_restart_.printField(cv_name + "_std", ssds_[i] / (fmax(1, update_calls_ - 1)));
825 : else
826 36 : out_restart_.printField(cv_name + "_std", covar_(i, i) / (fmax(1, update_calls_ - 1)));
827 : }
828 27 : out_restart_.printField();
829 27 : }
830 :
831 40 : void EDS::calculate()
832 : {
833 :
834 : // get center values from action if necessary
835 40 : if (b_c_values_)
836 10 : for (unsigned int i = 0; i < ncvs_; ++i)
837 5 : center_[i] = center_values_[i]->get();
838 :
839 40 : apply_bias();
840 40 : }
841 :
842 40 : void EDS::apply_bias()
843 : {
844 : // Compute linear force as in "restraint"
845 : double ene = 0, totf2 = 0, cv, m, f;
846 :
847 100 : for (unsigned int i = 0; i < ncvs_; ++i)
848 : {
849 60 : cv = difference(i, center_[i], getArgument(i));
850 60 : m = current_coupling_[i];
851 60 : f = -m;
852 60 : ene += m * cv;
853 60 : setOutputForce(i, f);
854 60 : totf2 += f * f;
855 : }
856 :
857 40 : setBias(ene);
858 40 : value_force2_->set(totf2);
859 40 : }
860 :
861 12 : void EDS::update_statistics()
862 : {
863 : double s, N, w = 1.0;
864 12 : std::vector<double> deltas(ncvs_);
865 :
866 : // update weight max, if necessary
867 12 : if (b_weights_)
868 : {
869 2 : w = logweights_->getLogWeight();
870 2 : if (max_logweight_ < w)
871 : {
872 : // we have new max. Need to shift existing values
873 0 : wsum_ *= exp(max_logweight_ - w);
874 0 : max_logweight_ = w;
875 : }
876 : // convert to weight
877 2 : w = exp(w - max_logweight_);
878 2 : wsum_ += w;
879 : N = wsum_;
880 : }
881 : else
882 : {
883 10 : N = fmax(1, update_calls_);
884 : }
885 :
886 : // Welford, West, and Hanso online variance method
887 : // with weights (default = 1.0)
888 32 : for (unsigned int i = 0; i < ncvs_; ++i)
889 : {
890 20 : deltas[i] = difference(i, means_[i], getArgument(i)) * w;
891 20 : means_[i] += deltas[i] / N;
892 20 : if (!b_covar_ && !b_lm_)
893 8 : ssds_[i] += deltas[i] * difference(i, means_[i], getArgument(i));
894 : }
895 12 : if (b_covar_ || b_lm_)
896 : {
897 16 : for (unsigned int i = 0; i < ncvs_; ++i)
898 : {
899 36 : for (unsigned int j = i; j < ncvs_; ++j)
900 : {
901 24 : s = (N - 1) * deltas[i] * deltas[j] / N / N - covar_(i, j) / N;
902 24 : covar_(i, j) += s;
903 : // do this so we don't double count
904 24 : covar_(j, i) = covar_(i, j);
905 : }
906 : }
907 : }
908 12 : if (b_virial_)
909 2 : update_pseudo_virial();
910 12 : }
911 :
912 8 : void EDS::reset_statistics()
913 : {
914 20 : for (unsigned int i = 0; i < ncvs_; ++i)
915 : {
916 12 : means_[i] = 0;
917 12 : if (!b_covar_ && !b_lm_)
918 6 : ssds_[i] = 0;
919 : }
920 8 : if (b_covar_ || b_lm_)
921 8 : for (unsigned int i = 0; i < ncvs_; ++i)
922 24 : for (unsigned int j = 0; j < ncvs_; ++j)
923 18 : covar_(i, j) = 0;
924 8 : if (b_virial_)
925 : {
926 4 : for (unsigned int i = 0; i < ncvs_; ++i)
927 3 : pseudo_virial_[i] = 0;
928 1 : pseudo_virial_sum_ = 0;
929 : }
930 8 : if (b_weights_)
931 : {
932 2 : wsum_ = 0;
933 2 : max_logweight_ = 0;
934 : }
935 8 : }
936 :
937 1 : void EDS::calc_lm_step_size()
938 : {
939 : // calulcate step size
940 : // uses scale here, which by default is center
941 :
942 1 : mult(covar_, covar_, covar2_);
943 4 : for (unsigned int i = 0; i < ncvs_; ++i)
944 : {
945 3 : differences_[i] = difference(i, center_[i], means_[i]);
946 3 : covar2_[i][i] += lm_mixing_par_ * covar2_[i][i];
947 : }
948 :
949 : // "step_size_vec" = 2*inv(covar*covar+ lambda diag(covar*covar))*covar*(mean-center)
950 1 : mult(covar_, differences_, alpha_vector_);
951 1 : Invert(covar2_, lm_inv_);
952 1 : mult(lm_inv_, alpha_vector_, alpha_vector_2_);
953 :
954 4 : for (unsigned int i = 0; i < ncvs_; ++i)
955 : {
956 3 : step_size_[i] = 2 * alpha_vector_2_[i] / kbt_ / scale_[i];
957 : }
958 1 : }
959 :
960 1 : void EDS::calc_covar_step_size()
961 : {
962 : // calulcate step size
963 : // uses scale here, which by default is center
964 : double tmp;
965 4 : for (unsigned int i = 0; i < ncvs_; ++i)
966 : {
967 : tmp = 0;
968 12 : for (unsigned int j = 0; j < ncvs_; ++j)
969 9 : tmp += difference(i, center_[i], means_[i]) * covar_(i, j);
970 3 : step_size_[i] = 2 * tmp / kbt_ / scale_[i] * update_calls_ / fmax(1, update_calls_ - 1);
971 : }
972 1 : }
973 :
974 6 : void EDS::calc_ssd_step_size()
975 : {
976 : double tmp;
977 12 : for (unsigned int i = 0; i < ncvs_; ++i)
978 : {
979 6 : tmp = 2. * difference(i, center_[i], means_[i]) * ssds_[i] / fmax(1, update_calls_ - 1);
980 6 : step_size_[i] = tmp / kbt_ / scale_[i];
981 : }
982 6 : }
983 :
984 2 : void EDS::update_pseudo_virial()
985 : {
986 : // We want to compute the bias force on each atom times the position
987 : // of the atoms.
988 : double p, netp = 0, netpv = 0;
989 : double volume = 0;
990 8 : for (unsigned int i = 0; i < ncvs_; ++i)
991 : {
992 : // checked in setup to ensure this cast is valid.
993 6 : ActionAtomistic *cv = dynamic_cast<ActionAtomistic *>(getPntrToArgument(i)->getPntrToAction());
994 6 : Tensor v(cv->getVirial());
995 6 : Tensor box(cv->getBox());
996 : const unsigned int natoms = cv->getNumberOfAtoms();
997 6 : if (!volume)
998 2 : volume = box.determinant();
999 :
1000 : // pressure contribution is -dBias / dV
1001 : // dBias / dV = alpha / w * dCV / dV
1002 : // to get partial of CV wrt to volume
1003 : // dCV/dV = sum dCV/dvij * vij / V
1004 : // where vij is box element
1005 : // add diagonal of virial tensor to get net pressure
1006 : // TODO: replace this with adjugate (Jacobi's Formula) for non-orthorombic case(?)
1007 6 : p = v(0, 0) * box(0, 0) + v(1, 1) * box(1, 1) + v(2, 2) * box(2, 2);
1008 6 : p /= volume;
1009 :
1010 6 : netp += p;
1011 :
1012 : // now scale for correct units in EDS algorithm
1013 6 : p *= (volume) / (kbt_ * natoms);
1014 :
1015 : // compute running mean of scaled
1016 6 : if (set_coupling_[i] != 0)
1017 0 : pseudo_virial_[i] = (p - pseudo_virial_[i]) / (fmax(1, update_calls_));
1018 : else
1019 6 : pseudo_virial_[i] = 0;
1020 : // update net pressure
1021 6 : netpv += pseudo_virial_[i];
1022 : }
1023 : // update pressure
1024 2 : value_pressure_->set(netp);
1025 2 : pseudo_virial_sum_ = netpv;
1026 2 : }
1027 :
1028 8 : void EDS::update_bias()
1029 : {
1030 8 : log.flush();
1031 8 : if (b_lm_)
1032 1 : calc_lm_step_size();
1033 7 : else if (b_covar_)
1034 1 : calc_covar_step_size();
1035 : else
1036 6 : calc_ssd_step_size();
1037 :
1038 20 : for (unsigned int i = 0; i < ncvs_; ++i)
1039 : {
1040 :
1041 : // multidimesional stochastic step
1042 12 : if (ncvs_ == 1 || (rand_.RandU01() < (multi_prop_)))
1043 : {
1044 :
1045 12 : if (b_virial_)
1046 : {
1047 : // apply virial regularization
1048 : // P * dP/dcoupling
1049 : // coupling is already included in virial term due to plumed propogating from bias to forces
1050 : // thus we need to divide by it to get the derivative (since force is linear in coupling)
1051 3 : if (fabs(set_coupling_[i]) > 0.000000001) // my heuristic for if EDS has started to prevent / 0
1052 : // scale^2 here is to align units
1053 0 : step_size_[i] -= 2 * scale_[i] * scale_[i] * virial_scaling_ * pseudo_virial_sum_ * pseudo_virial_sum_ / set_coupling_[i];
1054 : }
1055 12 : if (step_size_[i] == 0)
1056 4 : continue;
1057 :
1058 : // clip gradient
1059 8 : step_size_[i] = copysign(fmin(fabs(step_size_[i]), max_coupling_grad_[i]), step_size_[i]);
1060 8 : coupling_accum_[i] += step_size_[i] * step_size_[i];
1061 :
1062 : // equation 5 in White and Voth, JCTC 2014
1063 : // no negative sign because it's in step_size
1064 8 : set_coupling_[i] += step_size_[i] * max_coupling_range_[i] / sqrt(coupling_accum_[i]);
1065 8 : coupling_rate_[i] = (set_coupling_[i] - current_coupling_[i]) / update_period_;
1066 : }
1067 : else
1068 : {
1069 : // do not change the bias
1070 0 : coupling_rate_[i] = 0;
1071 : }
1072 : }
1073 :
1074 : // reset means/vars
1075 8 : reset_statistics();
1076 8 : }
1077 :
1078 40 : void EDS::update()
1079 : {
1080 : // adjust parameters according to EDS recipe
1081 40 : update_calls_++;
1082 :
1083 : // if we aren't wating for the bias to equilibrate, set flag to collect data
1084 : // want statistics before writing restart
1085 40 : if (!b_equil_ && update_period_ > 0)
1086 12 : update_statistics();
1087 :
1088 : // write restart with correct statistics before bias update
1089 : // check if we're ramping or doing normal updates and then restart if needed. The ramping check
1090 : // is complicated because we could be frozen, finished ramping or not ramping.
1091 : // The + 2 is so we have an extra line showing that the bias isn't changing (for my sanity and yours)
1092 40 : if (b_write_restart_)
1093 : {
1094 40 : if (getStep() == 0 ||
1095 32 : ((update_period_ < 0 && !b_freeze_ && update_calls_ <= fabs(update_period_) + 2) ||
1096 24 : (update_period_ > 0 && update_calls_ % update_period_ == 0)))
1097 27 : writeOutRestart();
1098 : }
1099 :
1100 : int b_finished_equil_flag = 1;
1101 :
1102 : // assume forces already applied and saved
1103 : // are we ramping to a constant value and not done equilibrating?
1104 40 : if (update_period_ < 0)
1105 : {
1106 5 : if (update_calls_ <= fabs(update_period_) && !b_freeze_)
1107 : {
1108 4 : for (unsigned int i = 0; i < ncvs_; ++i)
1109 2 : current_coupling_[i] += (target_coupling_[i] - set_coupling_[i]) / fabs(update_period_);
1110 : }
1111 : // make sure we don't reset update calls
1112 : b_finished_equil_flag = 0;
1113 : }
1114 35 : else if (update_period_ == 0)
1115 : { // do we have a no-update case?
1116 : // not updating
1117 : // pass
1118 : }
1119 30 : else if (b_equil_)
1120 : {
1121 : // equilibrating
1122 : // check if we've reached the setpoint
1123 48 : for (unsigned int i = 0; i < ncvs_; ++i)
1124 : {
1125 30 : if (coupling_rate_[i] == 0 || pow(current_coupling_[i] - set_coupling_[i], 2) < pow(coupling_rate_[i], 2))
1126 : {
1127 14 : b_finished_equil_flag &= 1;
1128 : }
1129 : else
1130 : {
1131 16 : current_coupling_[i] += coupling_rate_[i];
1132 : b_finished_equil_flag = 0;
1133 : }
1134 : }
1135 : }
1136 :
1137 : // reduce all the flags
1138 40 : if (b_equil_ && b_finished_equil_flag)
1139 : {
1140 11 : b_equil_ = false;
1141 11 : update_calls_ = 0;
1142 : }
1143 :
1144 : // Now we update coupling constant, if necessary
1145 40 : if (!b_equil_ && update_period_ > 0 && update_calls_ == update_period_ && !b_freeze_)
1146 : {
1147 8 : update_bias();
1148 8 : update_calls_ = 0;
1149 8 : avg_coupling_count_++;
1150 8 : b_equil_ = true; // back to equilibration now
1151 : } // close update if
1152 :
1153 : // pass couplings out so they are accessible
1154 100 : for (unsigned int i = 0; i < ncvs_; ++i)
1155 : {
1156 60 : out_coupling_[i]->set(current_coupling_[i]);
1157 : }
1158 40 : }
1159 :
1160 16 : EDS::~EDS()
1161 : {
1162 8 : out_restart_.close();
1163 24 : }
1164 :
1165 0 : void EDS::turnOnDerivatives()
1166 : {
1167 : // do nothing
1168 : // this is to avoid errors triggered when a bias is used as a CV
1169 : // (This is done in ExtendedLagrangian.cpp)
1170 0 : }
1171 :
1172 : }
1173 : } // close the 2 namespaces
|