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 : #ifndef __PLUMED_ves_LinearBasisSetExpansion_h
23 : #define __PLUMED_ves_LinearBasisSetExpansion_h
24 :
25 : #include <vector>
26 : #include <string>
27 : #include <memory>
28 :
29 :
30 : namespace PLMD {
31 :
32 : class Action;
33 : class Keywords;
34 : class Value;
35 : class Communicator;
36 : class Grid;
37 : class OFile;
38 :
39 :
40 : namespace ves {
41 :
42 : class CoeffsVector;
43 : class BasisFunctions;
44 : class TargetDistribution;
45 : class VesBias;
46 :
47 :
48 : class LinearBasisSetExpansion {
49 : LinearBasisSetExpansion& operator=(const LinearBasisSetExpansion&) = delete;
50 : private:
51 : std::string label_;
52 : //
53 : Action* action_pntr_;
54 : VesBias* vesbias_pntr_;
55 : Communicator& mycomm_;
56 : bool serial_;
57 : //
58 : double beta_;
59 : double kbt_;
60 : //
61 : std::vector<Value*> args_pntrs_;
62 : unsigned int nargs_;
63 : //
64 : std::vector<BasisFunctions*> basisf_pntrs_;
65 : std::vector<unsigned int> nbasisf_;
66 : //
67 : CoeffsVector* bias_coeffs_pntr_;
68 : size_t ncoeffs_;
69 : std::unique_ptr<CoeffsVector> targetdist_averages_pntr_;
70 : //
71 : std::vector<std::string> grid_min_;
72 : std::vector<std::string> grid_max_;
73 : std::vector<unsigned int> grid_bins_;
74 : //
75 : std::string targetdist_grid_label_;
76 : //
77 : long long int step_of_last_biasgrid_update;
78 : long long int step_of_last_biaswithoutcutoffgrid_update;
79 : long long int step_of_last_fesgrid_update;
80 : //
81 : std::unique_ptr<Grid> bias_grid_pntr_;
82 : std::unique_ptr<Grid> bias_withoutcutoff_grid_pntr_;
83 : std::unique_ptr<Grid> fes_grid_pntr_;
84 : Grid* log_targetdist_grid_pntr_;
85 : Grid* targetdist_grid_pntr_;
86 : //
87 : TargetDistribution* targetdist_pntr_;
88 : //
89 : std::string gfmt_;
90 : public:
91 : static void registerKeywords( Keywords& keys );
92 : // Constructor
93 : explicit LinearBasisSetExpansion(
94 : const std::string&,
95 : const double,
96 : Communicator&,
97 : const std::vector<Value*>&,
98 : std::vector<BasisFunctions*>&,
99 : CoeffsVector* bias_coeffs_pntr_in=NULL);
100 : //
101 : private:
102 : // copy constructor is disabled (private and unimplemented)
103 : explicit LinearBasisSetExpansion(const LinearBasisSetExpansion&);
104 : public:
105 : ~LinearBasisSetExpansion();
106 : //
107 : std::vector<Value*> getPntrsToArguments() const {
108 : return args_pntrs_;
109 : }
110 : std::vector<BasisFunctions*> getPntrsToBasisFunctions() const {
111 : return basisf_pntrs_;
112 : }
113 : CoeffsVector* getPntrToBiasCoeffs() const {
114 : return bias_coeffs_pntr_;
115 : }
116 : Grid* getPntrToBiasGrid() const {
117 : return bias_grid_pntr_.get();
118 : };
119 : //
120 : unsigned int getNumberOfArguments() const {
121 : return nargs_;
122 : };
123 : std::vector<unsigned int> getNumberOfBasisFunctions() const {
124 : return nbasisf_;
125 : };
126 : size_t getNumberOfCoeffs() const {
127 : return ncoeffs_;
128 : };
129 : //
130 : CoeffsVector& BiasCoeffs() const {
131 83 : return *bias_coeffs_pntr_;
132 : };
133 : CoeffsVector& TargetDistAverages() const {
134 : return *targetdist_averages_pntr_;
135 : };
136 : void setGridFMT(const std::string& fmt) {
137 90 : gfmt_=fmt;
138 90 : };
139 : //
140 : void setSerial() {
141 : serial_=true;
142 : }
143 : void setParallel() {
144 : serial_=false;
145 : }
146 : //
147 : void linkVesBias(VesBias*);
148 : void linkAction(Action*);
149 : // calculate bias and derivatives
150 : static double getBiasAndForces(const std::vector<double>&, bool&, std::vector<double>&, std::vector<double>&, std::vector<BasisFunctions*>&, CoeffsVector*, Communicator* comm_in=NULL);
151 : double getBiasAndForces(const std::vector<double>&, bool&, std::vector<double>&, std::vector<double>&);
152 : double getBiasAndForces(const std::vector<double>&, bool&, std::vector<double>&);
153 : double getBias(const std::vector<double>&, bool&, const bool parallel=true);
154 : //
155 : static void getBasisSetValues(const std::vector<double>&, std::vector<double>&, std::vector<BasisFunctions*>&, CoeffsVector*, Communicator* comm_in=NULL);
156 : void getBasisSetValues(const std::vector<double>&, std::vector<double>&, const bool parallel=true);
157 : //
158 : static double getBasisSetValue(const std::vector<double>&, const size_t, std::vector<BasisFunctions*>&, CoeffsVector*);
159 : double getBasisSetValue(const std::vector<double>&, const size_t);
160 : double getBasisSetConstant();
161 : // Bias grid and output stuff
162 : void setupBiasGrid(const bool usederiv=false);
163 : void updateBiasGrid();
164 : void resetStepOfLastBiasGridUpdate() {
165 36 : step_of_last_biasgrid_update = -1000;
166 : }
167 : void setStepOfLastBiasGridUpdate(long long int step) {
168 475 : step_of_last_biasgrid_update = step;
169 475 : }
170 : long long int getStepOfLastBiasGridUpdate() const {
171 712 : return step_of_last_biasgrid_update;
172 : }
173 : void writeBiasGridToFile(OFile&, const bool append=false) const;
174 : //
175 : void updateBiasWithoutCutoffGrid();
176 : void resetStepOfLastBiasWithoutCutoffGridUpdate() {
177 : step_of_last_biaswithoutcutoffgrid_update = -1000;
178 : }
179 : void setStepOfLastBiasWithoutCutoffGridUpdate(long long int step) {
180 23 : step_of_last_biaswithoutcutoffgrid_update = step;
181 23 : }
182 : long long int getStepOfLastBiasWithoutCutoffGridUpdate() const {
183 27 : return step_of_last_biaswithoutcutoffgrid_update;
184 : }
185 : void writeBiasWithoutCutoffGridToFile(OFile&, const bool append=false) const;
186 : //
187 : void setBiasMinimumToZero();
188 : void setBiasMaximumToZero();
189 : //
190 : void setupFesGrid();
191 : void updateFesGrid();
192 : void resetStepOfLastFesGridUpdate() {
193 36 : step_of_last_fesgrid_update = -1000;
194 : }
195 : void setStepOfLastFesGridUpdate(long long int step) {
196 473 : step_of_last_fesgrid_update = step;
197 473 : }
198 : long long int getStepOfLastFesGridUpdate() const {
199 539 : return step_of_last_fesgrid_update;
200 : }
201 : void writeFesGridToFile(OFile&, const bool append=false) const;
202 : //
203 : void setupFesProjGrid();
204 : void writeFesProjGridToFile(const std::vector<std::string>&, OFile&, const bool append=false) const;
205 : //
206 : void writeTargetDistGridToFile(OFile&, const bool append=false) const;
207 : void writeLogTargetDistGridToFile(OFile&, const bool append=false) const;
208 : void writeTargetDistProjGridToFile(const std::vector<std::string>&, OFile&, const bool append=false) const;
209 : void writeTargetDistributionToFile(const std::string&) const;
210 : //
211 : std::vector<unsigned int> getGridBins() const {
212 : return grid_bins_;
213 : }
214 : void setGridBins(const std::vector<unsigned int>&);
215 : void setGridBins(const unsigned int);
216 : //
217 : double getBeta() const {
218 : return beta_;
219 : }
220 : double getKbT() const {
221 : return kbt_;
222 : }
223 : double beta() const {
224 : return beta_;
225 : }
226 : double kBT() const {
227 1224051 : return kbt_;
228 : }
229 : //
230 : void setupUniformTargetDistribution();
231 : void setupTargetDistribution(TargetDistribution*);
232 : void updateTargetDistribution();
233 : //
234 : void readInRestartTargetDistribution(const std::string&);
235 : void restartTargetDistribution();
236 : //
237 : bool biasCutoffActive() const;
238 : //
239 : double calculateReweightFactor() const;
240 : //
241 : private:
242 : //
243 : std::unique_ptr<Grid> setupGeneralGrid(const std::string&, const bool usederiv=false);
244 : //
245 : void calculateTargetDistAveragesFromGrid(const Grid*);
246 : //
247 : bool isStaticTargetDistFileOutputActive() const;
248 : };
249 :
250 :
251 : inline
252 : double LinearBasisSetExpansion::getBiasAndForces(const std::vector<double>& args_values, bool& all_inside, std::vector<double>& forces, std::vector<double>& coeffsderivs_values) {
253 23750 : return getBiasAndForces(args_values,all_inside,forces,coeffsderivs_values,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
254 : }
255 :
256 :
257 : inline
258 1988037 : double LinearBasisSetExpansion::getBiasAndForces(const std::vector<double>& args_values, bool& all_inside, std::vector<double>& forces) {
259 1988037 : std::vector<double> coeffsderivs_values_dummy(ncoeffs_);
260 3976074 : return getBiasAndForces(args_values,all_inside,forces,coeffsderivs_values_dummy,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
261 : }
262 :
263 :
264 : inline
265 : double LinearBasisSetExpansion::getBias(const std::vector<double>& args_values, bool& all_inside, const bool parallel) {
266 : std::vector<double> forces_dummy(nargs_);
267 : std::vector<double> coeffsderivs_values_dummy(ncoeffs_);
268 : if(parallel) {
269 : return getBiasAndForces(args_values,all_inside,forces_dummy,coeffsderivs_values_dummy,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
270 : } else {
271 : return getBiasAndForces(args_values,all_inside,forces_dummy,coeffsderivs_values_dummy,basisf_pntrs_, bias_coeffs_pntr_, NULL);
272 : }
273 : }
274 :
275 :
276 : inline
277 862315 : void LinearBasisSetExpansion::getBasisSetValues(const std::vector<double>& args_values, std::vector<double>& basisset_values, const bool parallel) {
278 862315 : if(parallel) {
279 0 : getBasisSetValues(args_values,basisset_values,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
280 : } else {
281 862315 : getBasisSetValues(args_values,basisset_values,basisf_pntrs_, bias_coeffs_pntr_, NULL);
282 : }
283 862315 : }
284 :
285 :
286 : inline
287 : double LinearBasisSetExpansion::getBasisSetValue(const std::vector<double>& args_values, const size_t basisset_index) {
288 : return getBasisSetValue(args_values,basisset_index,basisf_pntrs_, bias_coeffs_pntr_);
289 : }
290 :
291 :
292 : inline
293 408 : double LinearBasisSetExpansion::getBasisSetConstant() {
294 408 : std::vector<double> args_dummy(nargs_,0.0);
295 816 : return getBasisSetValue(args_dummy,0,basisf_pntrs_, bias_coeffs_pntr_);
296 : }
297 :
298 :
299 : }
300 :
301 : }
302 :
303 : #endif
|