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