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 {return args_pntrs_;}
106 : std::vector<BasisFunctions*> getPntrsToBasisFunctions() const {return basisf_pntrs_;}
107 : CoeffsVector* getPntrToBiasCoeffs() const {return bias_coeffs_pntr_;}
108 : Grid* getPntrToBiasGrid() const {return bias_grid_pntr_.get();};
109 : //
110 : unsigned int getNumberOfArguments() const {return nargs_;};
111 : std::vector<unsigned int> getNumberOfBasisFunctions() const {return nbasisf_;};
112 : size_t getNumberOfCoeffs() const {return ncoeffs_;};
113 : //
114 83 : CoeffsVector& BiasCoeffs() const {return *bias_coeffs_pntr_;};
115 : CoeffsVector& TargetDistAverages() const {return *targetdist_averages_pntr_;};
116 : //
117 : void setSerial() {serial_=true;}
118 : void setParallel() {serial_=false;}
119 : //
120 : void linkVesBias(VesBias*);
121 : void linkAction(Action*);
122 : // calculate bias and derivatives
123 : static double getBiasAndForces(const std::vector<double>&, bool&, std::vector<double>&, std::vector<double>&, std::vector<BasisFunctions*>&, CoeffsVector*, Communicator* comm_in=NULL);
124 : double getBiasAndForces(const std::vector<double>&, bool&, std::vector<double>&, std::vector<double>&);
125 : double getBiasAndForces(const std::vector<double>&, bool&, std::vector<double>&);
126 : double getBias(const std::vector<double>&, bool&, const bool parallel=true);
127 : //
128 : static void getBasisSetValues(const std::vector<double>&, std::vector<double>&, std::vector<BasisFunctions*>&, CoeffsVector*, Communicator* comm_in=NULL);
129 : void getBasisSetValues(const std::vector<double>&, std::vector<double>&, const bool parallel=true);
130 : //
131 : static double getBasisSetValue(const std::vector<double>&, const size_t, std::vector<BasisFunctions*>&, CoeffsVector*);
132 : double getBasisSetValue(const std::vector<double>&, const size_t);
133 : double getBasisSetConstant();
134 : // Bias grid and output stuff
135 : void setupBiasGrid(const bool usederiv=false);
136 : void updateBiasGrid();
137 36 : void resetStepOfLastBiasGridUpdate() {step_of_last_biasgrid_update = -1000;}
138 475 : void setStepOfLastBiasGridUpdate(long long int step) {step_of_last_biasgrid_update = step;}
139 712 : long long int getStepOfLastBiasGridUpdate() const {return step_of_last_biasgrid_update;}
140 : void writeBiasGridToFile(OFile&, const bool append=false) const;
141 : //
142 : void updateBiasWithoutCutoffGrid();
143 : void resetStepOfLastBiasWithoutCutoffGridUpdate() {step_of_last_biaswithoutcutoffgrid_update = -1000;}
144 23 : void setStepOfLastBiasWithoutCutoffGridUpdate(long long int step) {step_of_last_biaswithoutcutoffgrid_update = step;}
145 27 : long long int getStepOfLastBiasWithoutCutoffGridUpdate() const {return step_of_last_biaswithoutcutoffgrid_update;}
146 : void writeBiasWithoutCutoffGridToFile(OFile&, const bool append=false) const;
147 : //
148 : void setBiasMinimumToZero();
149 : void setBiasMaximumToZero();
150 : //
151 : void setupFesGrid();
152 : void updateFesGrid();
153 36 : void resetStepOfLastFesGridUpdate() {step_of_last_fesgrid_update = -1000;}
154 473 : void setStepOfLastFesGridUpdate(long long int step) {step_of_last_fesgrid_update = step;}
155 539 : long long int getStepOfLastFesGridUpdate() const {return step_of_last_fesgrid_update;}
156 : void writeFesGridToFile(OFile&, const bool append=false) const;
157 : //
158 : void setupFesProjGrid();
159 : void writeFesProjGridToFile(const std::vector<std::string>&, OFile&, const bool append=false) const;
160 : //
161 : void writeTargetDistGridToFile(OFile&, const bool append=false) const;
162 : void writeLogTargetDistGridToFile(OFile&, const bool append=false) const;
163 : void writeTargetDistProjGridToFile(const std::vector<std::string>&, OFile&, const bool append=false) const;
164 : void writeTargetDistributionToFile(const std::string&) const;
165 : //
166 : std::vector<unsigned int> getGridBins() const {return grid_bins_;}
167 : void setGridBins(const std::vector<unsigned int>&);
168 : void setGridBins(const unsigned int);
169 : //
170 : double getBeta() const {return beta_;}
171 : double getKbT() const {return kbt_;}
172 : double beta() const {return beta_;}
173 1224051 : double kBT() const {return kbt_;}
174 : //
175 : void setupUniformTargetDistribution();
176 : void setupTargetDistribution(TargetDistribution*);
177 : void updateTargetDistribution();
178 : //
179 : void readInRestartTargetDistribution(const std::string&);
180 : void restartTargetDistribution();
181 : //
182 : bool biasCutoffActive() const;
183 : //
184 : double calculateReweightFactor() const;
185 : //
186 : private:
187 : //
188 : std::unique_ptr<Grid> setupGeneralGrid(const std::string&, const bool usederiv=false);
189 : //
190 : void calculateTargetDistAveragesFromGrid(const Grid*);
191 : //
192 : bool isStaticTargetDistFileOutputActive() const;
193 : };
194 :
195 :
196 : inline
197 : double LinearBasisSetExpansion::getBiasAndForces(const std::vector<double>& args_values, bool& all_inside, std::vector<double>& forces, std::vector<double>& coeffsderivs_values) {
198 23750 : return getBiasAndForces(args_values,all_inside,forces,coeffsderivs_values,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
199 : }
200 :
201 :
202 : inline
203 1988037 : double LinearBasisSetExpansion::getBiasAndForces(const std::vector<double>& args_values, bool& all_inside, std::vector<double>& forces) {
204 1988037 : std::vector<double> coeffsderivs_values_dummy(ncoeffs_);
205 3976074 : return getBiasAndForces(args_values,all_inside,forces,coeffsderivs_values_dummy,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
206 : }
207 :
208 :
209 : inline
210 : double LinearBasisSetExpansion::getBias(const std::vector<double>& args_values, bool& all_inside, const bool parallel) {
211 : std::vector<double> forces_dummy(nargs_);
212 : std::vector<double> coeffsderivs_values_dummy(ncoeffs_);
213 : if(parallel) {
214 : return getBiasAndForces(args_values,all_inside,forces_dummy,coeffsderivs_values_dummy,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
215 : }
216 : else {
217 : return getBiasAndForces(args_values,all_inside,forces_dummy,coeffsderivs_values_dummy,basisf_pntrs_, bias_coeffs_pntr_, NULL);
218 : }
219 : }
220 :
221 :
222 : inline
223 862315 : void LinearBasisSetExpansion::getBasisSetValues(const std::vector<double>& args_values, std::vector<double>& basisset_values, const bool parallel) {
224 862315 : if(parallel) {
225 0 : getBasisSetValues(args_values,basisset_values,basisf_pntrs_, bias_coeffs_pntr_, &mycomm_);
226 : }
227 : else {
228 862315 : getBasisSetValues(args_values,basisset_values,basisf_pntrs_, bias_coeffs_pntr_, NULL);
229 : }
230 862315 : }
231 :
232 :
233 : inline
234 : double LinearBasisSetExpansion::getBasisSetValue(const std::vector<double>& args_values, const size_t basisset_index) {
235 : return getBasisSetValue(args_values,basisset_index,basisf_pntrs_, bias_coeffs_pntr_);
236 : }
237 :
238 :
239 : inline
240 408 : double LinearBasisSetExpansion::getBasisSetConstant() {
241 408 : std::vector<double> args_dummy(nargs_,0.0);
242 816 : return getBasisSetValue(args_dummy,0,basisf_pntrs_, bias_coeffs_pntr_);
243 : }
244 :
245 :
246 : }
247 :
248 : }
249 :
250 : #endif
|