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_BasisFunctions_h
23 : #define __PLUMED_ves_BasisFunctions_h
24 :
25 : #include "core/Action.h"
26 :
27 : #include <vector>
28 : #include <string>
29 : #include <cmath>
30 :
31 :
32 : #define PLUMED_VES_BASISFUNCTIONS_INIT(ao) BasisFunctions(ao)
33 :
34 : namespace PLMD {
35 :
36 : /**
37 : \ingroup INHERIT
38 : Abstract base class for implenting new 1D basis sets.
39 : */
40 :
41 : class Action;
42 : class Grid;
43 :
44 : namespace ves {
45 :
46 : class VesBias;
47 : class TargetDistribution;
48 :
49 812 : class BasisFunctions :
50 : public Action
51 : {
52 : private:
53 : // print extra info about the basis set
54 : bool print_debug_info_;
55 : // to check if the basis set has been defined
56 : bool has_been_set;
57 : // description of the basis set
58 : std::string description_;
59 : // the type of the basis set
60 : std::string type_;
61 : // the maximum order of the basis functions
62 : unsigned int norder_;
63 : // the total number of basis functions
64 : unsigned int nbasis_;
65 : // the keywords used to invoke the basis set
66 : std::vector<std::string> bf_keywords_;
67 : // prefix for the basis function labels
68 : std::string bf_label_prefix_;
69 : // label of each basis function
70 : std::vector<std::string> bf_labels_;
71 : // if the basis functions are periodic or not
72 : bool periodic_;
73 : // if the basis functions are orthogonal or not
74 : bool orthogonal_;
75 : // if the basis functions are defined on a bounded interval or not
76 : bool interval_bounded_;
77 : // the intrinsic interval of the basis functions
78 : std::string interval_intrinsic_min_str_;
79 : std::string interval_intrinsic_max_str_;
80 : double interval_intrinsic_min_;
81 : double interval_intrinsic_max_;
82 : double interval_intrinsic_range_;
83 : double interval_intrinsic_mean_;
84 : // the defined (translated) interval of the basis functions
85 : std::string interval_min_str_;
86 : std::string interval_max_str_;
87 : double interval_min_;
88 : double interval_max_;
89 : double interval_range_;
90 : double interval_mean_;
91 : // the derivative term in the chain rule coming from the translation of the interval
92 : double argT_derivf_;
93 : // calculate numerically the integrals of the basis functions over the intervals
94 : bool numerical_uniform_integrals_;
95 : unsigned int nbins_;
96 : // the integrals of the basis functions over the interval on which they are defined
97 : std::vector <double> uniform_integrals_;
98 : //
99 : VesBias* vesbias_pntr_;
100 : Action* action_pntr_;
101 : //
102 : void getAllValuesNumericalDerivs(const double, double&, bool&, std::vector<double>&, std::vector<double>&) const;
103 :
104 : protected:
105 : // setup various stuff
106 : void setupBF();
107 : void setupInterval();
108 : void setNumericalIntegrationBins(const unsigned int nbins) {nbins_=nbins;}
109 : void numericalUniformIntegrals();
110 : std::vector<double> numericalTargetDistributionIntegralsFromGrid(const Grid*) const ;
111 : virtual void setupLabels();
112 : virtual void setupUniformIntegrals();
113 : template<typename T>
114 : void addKeywordToList(const std::string&, const T);
115 : template<typename T>
116 : void addKeywordToList(const std::string&, const std::vector<T>&);
117 : void addKeywordToList(const std::string&, const bool);
118 : //
119 106 : void setPeriodic() {periodic_=true;}
120 36 : void setNonPeriodic() {periodic_=false;}
121 177 : void setOrthogonal() {orthogonal_=true;}
122 14 : void setNonOrthogonal() {orthogonal_=false;}
123 203 : void setIntervalBounded() {interval_bounded_=true;}
124 : void setIntervalNonBounded() {interval_bounded_=false;}
125 203 : void setType(const std::string& type_in) {type_=type_in;}
126 203 : void setDescription(const std::string& description_in) {description_=description_in;}
127 : //
128 : void setNumberOfBasisFunctions(const unsigned int);
129 12 : void setOrder(const unsigned int norder_in) {norder_=norder_in;}
130 : void setIntrinsicInterval(const double, const double);
131 : void setIntrinsicInterval(const std::string&, const std::string&);
132 : void setInterval(const double, const double);
133 : void setInterval(const std::string&, const std::string&);
134 : //
135 : double intrinsicIntervalMin() const {return interval_intrinsic_min_;}
136 : double intrinsicIntervalMax() const {return interval_intrinsic_max_;}
137 : std::string intrinsicIntervalMinStr() const {return interval_intrinsic_min_str_;}
138 : std::string intrinsicIntervalMaxStr() const {return interval_intrinsic_max_str_;}
139 : //
140 : void setUniformIntegral(const unsigned int, const double);
141 : void setUniformIntegrals(const std::vector<double>&);
142 : void setAllUniformIntegralsToZero();
143 : //
144 : void setLabelPrefix(const std::string&);
145 : void setLabel(const unsigned int, const std::string&);
146 : void setLabels(const std::vector<std::string>&);
147 :
148 : public:
149 : static void registerKeywords(Keywords&);
150 : explicit BasisFunctions(const ActionOptions&ao);
151 : bool hasBeenSet() const {return has_been_set;}
152 : std::string getType() const {return type_;}
153 : std::string getDescription() const {return description_;}
154 265910906 : unsigned int getOrder() const {return norder_;}
155 100940284 : unsigned int getNumberOfBasisFunctions() const {return nbasis_;}
156 44651168 : unsigned int numberOfBasisFunctions() const {return nbasis_;}
157 : unsigned int getSize() const {return nbasis_;}
158 536 : bool arePeriodic() const {return periodic_;}
159 : bool areOrthogonal() const {return orthogonal_;}
160 : bool intervalBounded() const {return interval_bounded_;}
161 30244761 : double intervalMin() const {return interval_min_;}
162 5874 : double intervalMax() const {return interval_max_;}
163 : double intervalRange() const {return interval_range_;}
164 : double intervalMean() const {return interval_mean_;}
165 184934766 : double intervalDerivf() const {return argT_derivf_;}
166 691 : std::string intervalMinStr() const {return interval_min_str_;}
167 691 : std::string intervalMaxStr() const {return interval_max_str_;}
168 147 : std::vector<double> getUniformIntegrals() const {return uniform_integrals_;}
169 : std::vector<double> getTargetDistributionIntegrals(const TargetDistribution*) const;
170 : //
171 : std::vector<std::string> getKeywordList() const {return bf_keywords_;}
172 : std::string getKeywordString() const;
173 : //
174 873952 : std::string getBasisFunctionLabel(const unsigned int index) const {return bf_labels_[index];}
175 : std::vector<std::string> getBasisFunctionLabels() const {return bf_labels_;}
176 : //
177 : void linkVesBias(VesBias*);
178 : void linkAction(Action*);
179 : VesBias* getPntrToVesBias() const;
180 : Action* getPntrToAction() const;
181 : //
182 : double translateArgument(const double, bool&) const;
183 : double checkIfArgumentInsideInterval(const double, bool&) const;
184 : //
185 0 : void apply() {};
186 0 : void calculate() {};
187 : // calculate the value for the n-th basis function
188 : double getValue(const double, const unsigned int, double&, bool&) const;
189 : // calcuate the values for all basis functions
190 : virtual void getAllValues(const double, double&, bool&, std::vector<double>&, std::vector<double>&) const = 0;
191 : //virtual void get2ndDerivatives(const double, std::vector<double>&)=0;
192 : void printInfo() const;
193 : //
194 : void getMultipleValue(const std::vector<double>&, std::vector<double>&, std::vector<std::vector<double> >&, std::vector<std::vector<double> >&, const bool numerical_deriv=false) const;
195 : void writeBasisFunctionsToFile(OFile&, OFile&, const std::string& min_in, const std::string& max_in, unsigned int nbins=1000, const bool ignore_periodicity=false, const std::string& output_fmt="%15.8f", const bool numerical_deriv=false) const;
196 :
197 : std::vector<std::vector<double> > getAllInnerProducts() const;
198 : std::vector<std::vector<double> > getAllInnerProducts(const Grid*) const;
199 : double getInnerProduct(const unsigned int n, const unsigned int m, const Grid*) const;
200 : void writeInnerProductsToFiles(OFile& ofile, const std::string& output_fmt="%15.8f") const;
201 :
202 0 : virtual double getInnerProductWeight(const double arg) const {return 1.0;}
203 250 : virtual std::string getInnerProductWeightStr() const {return "1";}
204 :
205 : };
206 :
207 :
208 : inline
209 203 : void BasisFunctions::setNumberOfBasisFunctions(const unsigned int nbasis_in) {
210 203 : nbasis_=nbasis_in;
211 406 : bf_labels_.assign(nbasis_,"");
212 406 : uniform_integrals_.assign(nbasis_,0.0);
213 203 : }
214 :
215 :
216 : inline
217 : VesBias* BasisFunctions::getPntrToVesBias() const {
218 : plumed_massert(vesbias_pntr_!=NULL,"the VES bias has not been linked");
219 : return vesbias_pntr_;
220 : }
221 :
222 :
223 : inline
224 : Action* BasisFunctions::getPntrToAction() const {
225 : plumed_massert(action_pntr_!=NULL,"the action has not been linked");
226 : return action_pntr_;
227 : }
228 :
229 :
230 : inline
231 : void BasisFunctions::setUniformIntegral(const unsigned index, const double value) {
232 368 : uniform_integrals_[index] = value;
233 : }
234 :
235 :
236 : inline
237 : void BasisFunctions::setUniformIntegrals(const std::vector<double>& uniform_integrals_in) {
238 : plumed_assert(uniform_integrals_in.size()==nbasis_);
239 : uniform_integrals_ = uniform_integrals_in;
240 : }
241 :
242 :
243 : inline
244 : void BasisFunctions::setAllUniformIntegralsToZero() {
245 320 : uniform_integrals_.assign(nbasis_,0.0);
246 : }
247 :
248 : inline
249 : void BasisFunctions::setLabelPrefix(const std::string& bf_label_prefix_in) {
250 71 : bf_label_prefix_ = bf_label_prefix_in;
251 : }
252 :
253 :
254 : inline
255 : void BasisFunctions::setLabel(const unsigned int index, const std::string& label) {
256 1218 : bf_labels_[index] = label;
257 : }
258 :
259 :
260 : inline
261 : void BasisFunctions::setLabels(const std::vector<std::string>& bf_labels_in) {
262 : bf_labels_ = bf_labels_in;
263 : }
264 :
265 :
266 : inline
267 : double BasisFunctions::translateArgument(const double arg, bool& inside_interval) const {
268 : // NOTE: only works for symmetric intrinsic intervals
269 : inside_interval=true;
270 13375929 : double argT = (arg-interval_mean_)*argT_derivf_;
271 13375929 : if(argT < interval_intrinsic_min_) {
272 435 : inside_interval=false;
273 435 : argT=interval_intrinsic_min_;
274 : }
275 13375494 : else if(argT > interval_intrinsic_max_) {
276 13697 : inside_interval=false;
277 13697 : argT=interval_intrinsic_max_;
278 : }
279 : return argT;
280 : }
281 :
282 :
283 : inline
284 : double BasisFunctions::checkIfArgumentInsideInterval(const double arg, bool& inside_interval) const {
285 : inside_interval=true;
286 : double argT = arg;
287 7176224 : if(arg < interval_min_) {
288 353 : inside_interval=false;
289 353 : argT=interval_min_;
290 : }
291 7175871 : else if(arg > interval_max_) {
292 338 : inside_interval=false;
293 338 : argT=interval_max_;
294 : }
295 : return argT;
296 : }
297 :
298 :
299 :
300 : template<typename T>
301 653 : void BasisFunctions::addKeywordToList(const std::string& keyword, const T value) {
302 : std::string str_value;
303 653 : Tools::convert(value,str_value);
304 2612 : bf_keywords_.push_back(keyword+"="+str_value);
305 653 : }
306 :
307 :
308 : template<typename T>
309 2 : void BasisFunctions::addKeywordToList(const std::string& keyword, const std::vector<T>& values) {
310 : std::string str_value;
311 : std::string str_keywordvalues;
312 2 : Tools::convert(values[0],str_value);
313 6 : str_keywordvalues = keyword + "=" + str_value;
314 10 : for(unsigned int i=1; i<values.size(); i++) {
315 3 : Tools::convert(values[i],str_value);
316 6 : str_keywordvalues += "," + str_value;
317 : }
318 2 : bf_keywords_.push_back(str_keywordvalues);
319 2 : }
320 :
321 :
322 : inline
323 : void BasisFunctions::addKeywordToList(const std::string& keyword, const bool value) {
324 81 : if(value) {bf_keywords_.push_back(keyword);}
325 : }
326 :
327 :
328 : }
329 : }
330 :
331 : #endif
|