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 : #ifndef __PLUMED_ves_TargetDistribution_h
23 : #define __PLUMED_ves_TargetDistribution_h
24 :
25 : #include "core/Action.h"
26 :
27 : #include <vector>
28 : #include <string>
29 : #include <cmath>
30 :
31 : #define PLUMED_VES_TARGETDISTRIBUTION_INIT(ao) TargetDistribution(ao)
32 :
33 : namespace PLMD {
34 :
35 : /**
36 : \ingroup INHERIT
37 : Abstract base class for implenting new target distributions.
38 : */
39 :
40 : class Action;
41 : class Grid;
42 : class Value;
43 : class Keywords;
44 :
45 : namespace ves {
46 :
47 : class TargetDistModifer;
48 : class VesBias;
49 :
50 : class TargetDistribution :
51 : public Action
52 : {
53 : private:
54 : enum TargetDistType {
55 : static_targetdist,
56 : dynamic_targetdist
57 : } type_;
58 : //
59 : bool force_normalization_;
60 : bool check_normalization_;
61 : bool check_nonnegative_;
62 : bool check_nan_inf_;
63 : bool shift_targetdist_to_zero_;
64 : // dimension of the distribution
65 : unsigned int dimension_;
66 : // grid parameters
67 : std::vector<Value*> grid_args_;
68 : //
69 : Grid* targetdist_grid_pntr_;
70 : Grid* log_targetdist_grid_pntr_;
71 : //
72 : std::vector<TargetDistModifer*> targetdist_modifer_pntrs_;
73 : //
74 : Action* action_pntr_;
75 : VesBias* vesbias_pntr_;
76 : //
77 : bool needs_bias_grid_;
78 : bool needs_bias_withoutcutoff_grid_;
79 : bool needs_fes_grid_;
80 : //
81 : Grid* bias_grid_pntr_;
82 : Grid* bias_withoutcutoff_grid_pntr_;
83 : Grid* fes_grid_pntr_;
84 : //
85 : bool static_grid_calculated;
86 : //
87 : bool allow_bias_cutoff_;
88 : bool bias_cutoff_active_;
89 : //
90 : void calculateStaticDistributionGrid();
91 : void updateBiasCutoffForTargetDistGrid();
92 : void checkNanAndInf();
93 : protected:
94 : void setStatic() {type_=static_targetdist;}
95 36 : void setDynamic() {type_=dynamic_targetdist;}
96 : // set the that target distribution is normalized
97 68 : void setForcedNormalization() {force_normalization_=true; check_normalization_=false;}
98 : void unsetForcedNormalization() {force_normalization_=false; check_normalization_=true;};
99 : //
100 0 : void setBiasGridNeeded() {needs_bias_grid_=true;}
101 3 : void setBiasWithoutCutoffGridNeeded() {needs_bias_withoutcutoff_grid_=true;}
102 33 : void setFesGridNeeded() {needs_fes_grid_=true;}
103 : //
104 : VesBias* getPntrToVesBias() const;
105 : Action* getPntrToAction() const;
106 : //
107 234 : virtual void setupAdditionalGrids(const std::vector<Value*>&, const std::vector<std::string>&, const std::vector<std::string>&, const std::vector<unsigned int>&) {}
108 : //
109 : void normalizeTargetDistGrid();
110 : //
111 3182769 : Grid& targetDistGrid() const {return *targetdist_grid_pntr_;}
112 1494463 : Grid& logTargetDistGrid() const {return *log_targetdist_grid_pntr_;}
113 : //
114 : Grid* getBiasGridPntr() const {return bias_grid_pntr_;}
115 2624 : Grid* getBiasWithoutCutoffGridPntr() const {return bias_withoutcutoff_grid_pntr_;}
116 1130107 : Grid* getFesGridPntr() const {return fes_grid_pntr_;}
117 : //
118 : double getBeta() const;
119 : //
120 : void applyTargetDistModiferToGrid(TargetDistModifer* modifer_pntr);
121 : //
122 : void setMinimumOfTargetDistGridToZero();
123 : void updateLogTargetDistGrid();
124 : //
125 308 : virtual void updateGrid() {calculateStaticDistributionGrid();}
126 : public:
127 : static void registerKeywords(Keywords&);
128 : explicit TargetDistribution(const ActionOptions&);
129 : ~TargetDistribution();
130 : //
131 : bool isStatic() const {return type_==static_targetdist;}
132 573 : bool isDynamic() const {return type_==dynamic_targetdist;}
133 : // is the target distribution normalize or not
134 : bool forcedNormalization() const {return force_normalization_;};
135 5194 : bool isTargetDistGridShiftedToZero() const {return shift_targetdist_to_zero_;}
136 : //
137 437 : bool biasGridNeeded() const {return needs_bias_grid_;}
138 40 : bool biasWithoutCutoffGridNeeded() const {return needs_bias_withoutcutoff_grid_;}
139 437 : bool fesGridNeeded() const {return needs_fes_grid_;}
140 : //
141 : void allowBiasCutoff() {allow_bias_cutoff_=true;}
142 : void doNotAllowBiasCutoff() {allow_bias_cutoff_=false;}
143 : bool isBiasCutoffAllowed() const {return allow_bias_cutoff_;}
144 : bool biasCutoffActive() const {return bias_cutoff_active_;}
145 : //
146 : void setDimension(const unsigned int dimension);
147 499151 : unsigned getDimension() const {return dimension_;}
148 : //
149 : virtual void linkVesBias(VesBias*);
150 : virtual void linkAction(Action*);
151 : //
152 : virtual void linkBiasGrid(Grid*);
153 : virtual void linkBiasWithoutCutoffGrid(Grid*);
154 : virtual void linkFesGrid(Grid*);
155 : //
156 : void setupBiasCutoff();
157 : //
158 826 : Grid* getTargetDistGridPntr() const {return targetdist_grid_pntr_;}
159 160 : Grid* getLogTargetDistGridPntr() const {return log_targetdist_grid_pntr_;}
160 : //
161 : void clearLogTargetDistGrid();
162 : // calculate the target distribution itself
163 : virtual double getValue(const std::vector<double>&) const = 0;
164 : //
165 : void setupGrids(const std::vector<Value*>&, const std::vector<std::string>&, const std::vector<std::string>&, const std::vector<unsigned int>&);
166 : //
167 : Grid getMarginal(const std::vector<std::string>&);
168 : //
169 : void updateTargetDist();
170 : //
171 : void readInRestartTargetDistGrid(const std::string&);
172 : //
173 : static double integrateGrid(const Grid*);
174 : static double normalizeGrid(Grid*);
175 : static Grid getMarginalDistributionGrid(Grid*, const std::vector<std::string>&);
176 : // empty standard action stuff
177 0 : void update() {};
178 0 : void apply() {};
179 0 : void calculate() {};
180 : };
181 :
182 :
183 : inline
184 : VesBias* TargetDistribution::getPntrToVesBias() const {
185 : plumed_massert(vesbias_pntr_!=NULL,"the VES bias has not been linked");
186 : return vesbias_pntr_;
187 : }
188 :
189 :
190 : inline
191 : Action* TargetDistribution::getPntrToAction() const {
192 : plumed_massert(action_pntr_!=NULL,"the action has not been linked");
193 : return action_pntr_;
194 : }
195 :
196 :
197 : inline
198 78 : void TargetDistribution::normalizeTargetDistGrid() {
199 78 : double normalization = normalizeGrid(targetdist_grid_pntr_);
200 78 : if(normalization<0.0) {plumed_merror(getName()+": something went wrong trying to normalize the target distribution, integrating over it gives a negative value.");}
201 78 : }
202 :
203 :
204 :
205 :
206 : }
207 : }
208 : #endif
|