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