Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2026 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #ifndef __PLUMED_bias_MetaDGrid_h
23 : #define __PLUMED_bias_MetaDGrid_h
24 :
25 : #include "MetaDKernel.h"
26 : #include "tools/Grid.h"
27 : #include <memory>
28 : #include <string>
29 : #include <vector>
30 :
31 : namespace PLMD {
32 :
33 : class Action;
34 : class IFile;
35 : class OFile;
36 : class Value;
37 :
38 : namespace bias {
39 :
40 : /// The grid on which METAD and PBMETAD accumulate their hills.
41 : ///
42 : /// The grid is extended along the variables that are not periodic whenever a hill does not fit in it, so that it
43 : /// always contains all the points where the hills are not zero (plus one point on each side): the bias is zero
44 : /// outside the grid. The spacing does not change and the new points are on the same lattice.
45 : /// With INTERVAL (one dimensional only) the bias beyond the limits is constant: the grid contains the interval and
46 : /// at least one point beyond each limit, and the bias beyond the grid is the value at its ends.
47 205 : class MetaDBiasGrid {
48 : /// the action that owns the grid (errors, log)
49 : Action& action_;
50 : std::unique_ptr<GridBase> grid_;
51 : bool sparse_=false;
52 : /// maximum number of points (GRID_SIZE_LIMIT)
53 : double sizelimit_=1.0e8;
54 : /// no range was given and there are no hills yet: the grid is placed around the first one
55 : bool pristine_=false;
56 : /// a line of the log is not complete (an extension starts a new line)
57 : bool midline_=false;
58 : /// INTERVAL (one dimensional)
59 : bool interval_=false;
60 : double lower_=0.0;
61 : double upper_=0.0;
62 : /// add the hill to the points within its support (no INTERVAL)
63 : void addHillToPoints(const MetaDGaussian& hill, const MetaDKernel& kernel);
64 : public:
65 : /// The number of bins from GRID_MIN, GRID_MAX, GRID_BIN and GRID_SPACING (empty if not given): with neither GRID_BIN
66 : /// nor GRID_SPACING the spacing is 1/5 of SIGMA (of SIGMA_MIN with ADAPTIVE). With GRID_SPACING alone the grid is
67 : /// placed around the first hill: gmin, gmax and gbin are set to a first range and true is returned.
68 : static bool setupBins(Action& action, const std::vector<Value*>& args, std::vector<std::string>& gmin, std::vector<std::string>& gmax,
69 : std::vector<unsigned>& gbin, std::vector<double>& gspacing, bool adaptive, const std::vector<double>& sigma0,
70 : const std::vector<double>& sigma0min);
71 : /// warnings if the spacing of the grid is larger than half the width of the hills (SIGMA, or SIGMA_MIN with ADAPTIVE,
72 : /// which must be given)
73 : static void checkSpacing(Action& action, const std::vector<std::string>& gmin, const std::vector<std::string>& gmax,
74 : const std::vector<unsigned>& gbin, bool adaptive, const std::vector<double>& sigma0,
75 : const std::vector<double>& sigma0min);
76 : /// error if GRID_SIZE_LIMIT is not valid
77 : static void checkSizeLimit(Action& action, double limit);
78 : explicit MetaDBiasGrid(Action& action);
79 : /// a new grid; nobounds: no initial range was given (GRID_SPACING only), the grid is placed around the first hill
80 : void create(const std::string& funcl, const std::vector<Value*>& args, const std::vector<std::string>& gmin,
81 : const std::vector<std::string>& gmax, const std::vector<unsigned>& gbin, bool sparse, bool spline, bool nobounds);
82 : /// read a grid from a file: it must be on the lattice of the grid given in input (gmin, gmax, gbin), with any range
83 : void read(IFile& file, const std::string& filename, const std::string& funcl, const std::vector<Value*>& args,
84 : const std::vector<std::string>& gmin, const std::vector<std::string>& gmax, const std::vector<unsigned>& gbin,
85 : bool sparse, bool spline, bool nobounds);
86 : /// maximum number of points (an initial grid that is larger raises the limit to twice its size)
87 : void setSizeLimit(double limit);
88 : /// INTERVAL: the grid is extended to contain the interval and at least one point beyond each limit
89 : void setInterval(double lower, double upper, double time);
90 : /// extend the grid, if needed, so that it contains all the points within halfwidth[i] from x along each
91 : /// variable i that is not periodic, plus one point on each side
92 : void extend(const std::vector<double>& x, const std::vector<double>& halfwidth, double time);
93 : /// add a hill (the grid is extended if needed)
94 : void addHill(const MetaDGaussian& hill, const MetaDKernel& kernel, double time);
95 : /// the bias (zero outside the grid; with INTERVAL, beyond the limits the value at the limit)
96 : double getBias(const std::vector<double>& cv) const;
97 : /// the bias and its derivatives
98 : double getBiasAndDerivatives(const std::vector<double>& cv, std::vector<double>& der) const;
99 : /// write the grid on file, replacing what was written before (and flushing it, otherwise a single grid could stay
100 : /// in the buffer), or appending it (STORE_GRIDS: the header is repeated, the flushing is left to FLUSH);
101 : /// comment is written after the grid
102 : void write(OFile& file, bool append, const std::string& comment) const;
103 : /// a line of the log has been started and is not complete (e.g. while reading hills)
104 : void setMidline(bool midline) {
105 488 : midline_=midline;
106 488 : }
107 : GridBase& getGrid() {
108 : return *grid_;
109 : }
110 : const GridBase& getGrid() const {
111 : return *grid_;
112 : }
113 : };
114 :
115 : }
116 : }
117 :
118 : #endif
|