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