Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2021 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 "TargetDistribution.h"
24 : #include "VesTools.h"
25 :
26 : #include "core/ActionRegister.h"
27 : #include "core/ActionSet.h"
28 : #include "core/PlumedMain.h"
29 : #include "tools/Grid.h"
30 :
31 : #include <limits>
32 :
33 :
34 : namespace PLMD {
35 :
36 : // class Grid;
37 : class Action;
38 :
39 : namespace ves {
40 :
41 : //+PLUMEDOC VES_TARGETDIST TD_LINEAR_COMBINATION
42 : /*
43 : Target distribution given by linear combination of distributions (static or dynamic).
44 :
45 : Employ a target distribution that is a linear combination of the other
46 : distributions, defined as
47 : \f[
48 : p(\mathbf{s}) = \sum_{i} w_{i} \, p_{i}(\mathbf{s})
49 : \f]
50 : where the weights \f$w_{i}\f$ are normalized to 1, \f$\sum_{i}w_{i}=1\f$.
51 :
52 : The labels of the distributions \f$p_{i}(\mathbf{s})\f$ to be used in the
53 : linear combination are given in the DISTRIBUTIONS keyword.
54 :
55 : The weights \f$w_{i}\f$ can be given using
56 : the WEIGHTS keyword. The distributions are weighted equally if no weights are given.
57 :
58 : It is assumed that all the distributions \f$p_{i}(\mathbf{s})\f$ are normalized.
59 : If that is not the case for some reason should you
60 : normalize each distribution separately by using the NORMALIZE
61 : keyword when defining them in the input file (i.e. before the
62 : TD_LINEAR_COMBINATION action).
63 : Note that normalizing the overall
64 : linear combination will generally lead to different results than normalizing
65 : each distribution separately.
66 :
67 : The linear combination will be a dynamic target distribution if one or more
68 : of the distributions used is a dynamic distribution, otherwise it will be a
69 : static distribution.
70 :
71 : \par Examples
72 :
73 : Here we employ a linear combination of a uniform and a Gaussian distribution.
74 : No weights are given so the two distributions will be weighted equally.
75 : \plumedfile
76 : td_uni: TD_UNIFORM
77 :
78 : td_gauss: TD_GAUSSIAN CENTER1=-2.0 SIGMA1=0.5
79 :
80 : td_comb: TD_LINEAR_COMBINATION DISTRIBUTIONS=td_uni,td_gauss
81 : \endplumedfile
82 :
83 : Here we employ a linear combination of a uniform and two Gaussian distribution.
84 : The weights are automatically normalized to 1 such that giving
85 : WEIGHTS=1.0,1.0,2.0 as we do here is equal to giving WEIGHTS=0.25,0.25,0.50.
86 : \plumedfile
87 : td_uni: TD_UNIFORM
88 :
89 : td_gauss1: TD_GAUSSIAN CENTER1=-2.0,-2.0 SIGMA1=0.5,0.3
90 :
91 : td_gauss2: TD_GAUSSIAN CENTER1=+2.0,+2.0 SIGMA1=0.3,0.5
92 :
93 : TD_LINEAR_COMBINATION ...
94 : DISTRIBUTIONS=td_uni,td_gauss1,td_gauss2
95 : WEIGHTS=1.0,1.0,2.0
96 : LABEL=td_comb
97 : ... TD_LINEAR_COMBINATION
98 : \endplumedfile
99 :
100 : In the above example the two Gaussian kernels are given using two separate
101 : DISTRIBUTION keywords. As the \ref TD_GAUSSIAN target distribution allows multiple
102 : centers is it also possible to use just one DISTRIBUTION keyword for the two
103 : Gaussian kernels. This is shown in the following example which will give the
104 : exact same result as the one above as the weights have been appropriately
105 : adjusted
106 : \plumedfile
107 : td_uni: TD_UNIFORM
108 :
109 : TD_GAUSSIAN ...
110 : CENTER1=-2.0,-2.0 SIGMA1=0.5,0.3
111 : CENTER2=+2.0,+2.0 SIGMA2=0.3,0.5
112 : WEIGHTS=1.0,2.0
113 : LABEL=td_gauss
114 : ... TD_GAUSSIAN
115 :
116 : TD_LINEAR_COMBINATION ...
117 : DISTRIBUTIONS=td_uni,td_gauss
118 : WEIGHTS=0.25,0.75
119 : LABEL=td_comb
120 : ... TD_LINEAR_COMBINATION
121 : \endplumedfile
122 :
123 : */
124 : //+ENDPLUMEDOC
125 :
126 : class VesBias;
127 :
128 : class TD_LinearCombination: public TargetDistribution {
129 : private:
130 : std::vector<TargetDistribution*> distribution_pntrs_;
131 : std::vector<Grid*> grid_pntrs_;
132 : std::vector<double> weights_;
133 : unsigned int ndist_;
134 : void setupAdditionalGrids(const std::vector<Value*>&, const std::vector<std::string>&, const std::vector<std::string>&, const std::vector<unsigned int>&) override;
135 : public:
136 : static void registerKeywords(Keywords&);
137 : explicit TD_LinearCombination(const ActionOptions& ao);
138 : void updateGrid() override;
139 : double getValue(const std::vector<double>&) const override;
140 : //
141 : void linkVesBias(VesBias*) override;
142 : void linkAction(Action*) override;
143 : //
144 : void linkBiasGrid(Grid*) override;
145 : void linkBiasWithoutCutoffGrid(Grid*) override;
146 : void linkFesGrid(Grid*) override;
147 : //
148 : };
149 :
150 :
151 : PLUMED_REGISTER_ACTION(TD_LinearCombination,"TD_LINEAR_COMBINATION")
152 :
153 :
154 14 : void TD_LinearCombination::registerKeywords(Keywords& keys) {
155 14 : TargetDistribution::registerKeywords(keys);
156 28 : keys.add("compulsory","DISTRIBUTIONS","The labels of the target distribution actions to be used in the linear combination.");
157 28 : keys.add("optional","WEIGHTS","The weights of target distributions. Have to be as many as the number of target distribution labels given in DISTRIBUTIONS. If no weights are given the distributions are weighted equally. The weights are automatically normalized to 1.");
158 14 : keys.use("WELLTEMPERED_FACTOR");
159 : //keys.use("SHIFT_TO_ZERO");
160 14 : keys.use("NORMALIZE");
161 14 : }
162 :
163 :
164 12 : TD_LinearCombination::TD_LinearCombination(const ActionOptions& ao):
165 : PLUMED_VES_TARGETDISTRIBUTION_INIT(ao),
166 24 : distribution_pntrs_(0),
167 12 : grid_pntrs_(0),
168 12 : weights_(0),
169 24 : ndist_(0)
170 : {
171 : std::vector<std::string> targetdist_labels;
172 12 : parseVector("DISTRIBUTIONS",targetdist_labels);
173 :
174 12 : std::string error_msg = "";
175 24 : distribution_pntrs_ = VesTools::getPointersFromLabels<TargetDistribution*>(targetdist_labels,plumed.getActionSet(),error_msg);
176 12 : if(error_msg.size()>0) {plumed_merror("Error in keyword DISTRIBUTIONS of "+getName()+": "+error_msg);}
177 :
178 40 : for(unsigned int i=0; i<distribution_pntrs_.size(); i++) {
179 28 : if(distribution_pntrs_[i]->isDynamic()) {setDynamic();}
180 28 : if(distribution_pntrs_[i]->fesGridNeeded()) {setFesGridNeeded();}
181 28 : if(distribution_pntrs_[i]->biasGridNeeded()) {setBiasGridNeeded();}
182 : }
183 :
184 12 : ndist_ = distribution_pntrs_.size();
185 12 : grid_pntrs_.assign(ndist_,NULL);
186 12 : if(ndist_==0) {plumed_merror(getName()+ ": no distributions are given.");}
187 12 : if(ndist_==1) {plumed_merror(getName()+ ": giving only one distribution does not make sense.");}
188 : //
189 24 : parseVector("WEIGHTS",weights_);
190 12 : if(weights_.size()==0) {weights_.assign(distribution_pntrs_.size(),1.0);}
191 12 : if(distribution_pntrs_.size()!=weights_.size()) {
192 0 : plumed_merror(getName()+ ": there has to be as many weights given in WEIGHTS as the number of target distribution labels given in DISTRIBUTIONS");
193 : }
194 : //
195 : double sum_weights=0.0;
196 40 : for(unsigned int i=0; i<weights_.size(); i++) {sum_weights+=weights_[i];}
197 40 : for(unsigned int i=0; i<weights_.size(); i++) {weights_[i]/=sum_weights;}
198 12 : checkRead();
199 12 : }
200 :
201 :
202 0 : double TD_LinearCombination::getValue(const std::vector<double>& argument) const {
203 0 : plumed_merror("getValue not implemented for TD_LinearCombination");
204 : return 0.0;
205 : }
206 :
207 :
208 12 : void TD_LinearCombination::setupAdditionalGrids(const std::vector<Value*>& arguments, const std::vector<std::string>& min, const std::vector<std::string>& max, const std::vector<unsigned int>& nbins) {
209 40 : for(unsigned int i=0; i<ndist_; i++) {
210 28 : distribution_pntrs_[i]->setupGrids(arguments,min,max,nbins);
211 28 : if(distribution_pntrs_[i]->getDimension()!=this->getDimension()) {
212 0 : plumed_merror(getName() + ": all target distribution must have the same dimension");
213 : }
214 28 : grid_pntrs_[i]=distribution_pntrs_[i]->getTargetDistGridPntr();
215 : }
216 12 : }
217 :
218 :
219 22 : void TD_LinearCombination::updateGrid() {
220 70 : for(unsigned int i=0; i<ndist_; i++) {
221 48 : distribution_pntrs_[i]->updateTargetDist();
222 : }
223 162033 : for(Grid::index_t l=0; l<targetDistGrid().getSize(); l++) {
224 : double value = 0.0;
225 506837 : for(unsigned int i=0; i<ndist_; i++) {
226 344826 : value += weights_[i]*grid_pntrs_[i]->getValue(l);
227 : }
228 162011 : if(value==0.0) value=std::numeric_limits<double>::denorm_min();
229 162011 : targetDistGrid().setValue(l,value);
230 162011 : logTargetDistGrid().setValue(l,-std::log(value));
231 : }
232 22 : logTargetDistGrid().setMinToZero();
233 22 : }
234 :
235 :
236 1 : void TD_LinearCombination::linkVesBias(VesBias* vesbias_pntr_in) {
237 1 : TargetDistribution::linkVesBias(vesbias_pntr_in);
238 3 : for(unsigned int i=0; i<ndist_; i++) {
239 2 : distribution_pntrs_[i]->linkVesBias(vesbias_pntr_in);
240 : }
241 1 : }
242 :
243 :
244 0 : void TD_LinearCombination::linkAction(Action* action_pntr_in) {
245 0 : TargetDistribution::linkAction(action_pntr_in);
246 0 : for(unsigned int i=0; i<ndist_; i++) {
247 0 : distribution_pntrs_[i]->linkAction(action_pntr_in);
248 : }
249 0 : }
250 :
251 :
252 0 : void TD_LinearCombination::linkBiasGrid(Grid* bias_grid_pntr_in) {
253 0 : TargetDistribution::linkBiasGrid(bias_grid_pntr_in);
254 0 : for(unsigned int i=0; i<ndist_; i++) {
255 0 : distribution_pntrs_[i]->linkBiasGrid(bias_grid_pntr_in);
256 : }
257 0 : }
258 :
259 :
260 0 : void TD_LinearCombination::linkBiasWithoutCutoffGrid(Grid* bias_withoutcutoff_grid_pntr_in) {
261 0 : TargetDistribution::linkBiasWithoutCutoffGrid(bias_withoutcutoff_grid_pntr_in);
262 0 : for(unsigned int i=0; i<ndist_; i++) {
263 0 : distribution_pntrs_[i]->linkBiasWithoutCutoffGrid(bias_withoutcutoff_grid_pntr_in);
264 : }
265 0 : }
266 :
267 :
268 1 : void TD_LinearCombination::linkFesGrid(Grid* fes_grid_pntr_in) {
269 1 : TargetDistribution::linkFesGrid(fes_grid_pntr_in);
270 3 : for(unsigned int i=0; i<ndist_; i++) {
271 2 : distribution_pntrs_[i]->linkFesGrid(fes_grid_pntr_in);
272 : }
273 1 : }
274 :
275 :
276 : }
277 : }
|