Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2017-2023 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed 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 : plumed 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 plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #ifndef __PLUMED_isdb_MetainferenceBase_h
23 : #define __PLUMED_isdb_MetainferenceBase_h
24 :
25 : #include "core/ActionWithValue.h"
26 : #include "core/ActionAtomistic.h"
27 : #include "core/ActionWithArguments.h"
28 : #include "core/PlumedMain.h"
29 : #include "tools/Random.h"
30 : #include "tools/OpenMP.h"
31 :
32 : #define PLUMED_METAINF_INIT(ao) Action(ao),MetainferenceBase(ao)
33 :
34 : namespace PLMD {
35 : namespace isdb {
36 :
37 : /**
38 : \ingroup INHERIT
39 : This is the abstract base class to use for implementing new ISDB Metainference actions, within it there is
40 : information as to how to go about implementing a new Metainference action.
41 : */
42 :
43 : class MetainferenceBase :
44 : public ActionAtomistic,
45 : public ActionWithArguments,
46 : public ActionWithValue
47 : {
48 : private:
49 : std::vector<double> forces;
50 : std::vector<double> forcesToApply;
51 :
52 : // activate metainference
53 : bool doscore_;
54 : unsigned write_stride_;
55 : // number of experimental data
56 : unsigned narg;
57 : // experimental data
58 : std::vector<double> parameters;
59 : // metainference derivatives
60 : std::vector<double> metader_;
61 : // vector of back-calculated experimental data
62 : std::vector<double> calc_data_;
63 :
64 : // noise type
65 : unsigned noise_type_;
66 : enum { GAUSS, MGAUSS, OUTLIERS, MOUTLIERS, GENERIC };
67 : unsigned gen_likelihood_;
68 : enum { LIKE_GAUSS, LIKE_LOGN };
69 : bool doscale_;
70 : unsigned scale_prior_;
71 : enum { SC_GAUSS, SC_FLAT };
72 : double scale_;
73 : double scale_mu_;
74 : double scale_min_;
75 : double scale_max_;
76 : double Dscale_;
77 : // scale is data scaling factor
78 : // noise type
79 : unsigned offset_prior_;
80 : bool dooffset_;
81 : double offset_;
82 : double offset_mu_;
83 : double offset_min_;
84 : double offset_max_;
85 : double Doffset_;
86 : // scale and offset regression
87 : bool doregres_zero_;
88 : int nregres_zero_;
89 : // sigma is data uncertainty
90 : std::vector<double> sigma_;
91 : std::vector<double> sigma_min_;
92 : std::vector<double> sigma_max_;
93 : std::vector<double> Dsigma_;
94 : // sigma_mean is uncertainty in the mean estimate
95 : std::vector<double> sigma_mean2_;
96 : // this is the estimator of the mean value per replica for generic metainference
97 : std::vector<double> ftilde_;
98 : double Dftilde_;
99 :
100 : // temperature in kbt
101 : double kbt_;
102 :
103 : // Monte Carlo stuff
104 : std::vector<Random> random;
105 : unsigned MCsteps_;
106 : long unsigned MCaccept_;
107 : long unsigned MCacceptScale_;
108 : long unsigned MCacceptFT_;
109 : long unsigned MCtrial_;
110 : unsigned MCchunksize_;
111 :
112 : // output
113 : Value* valueScore;
114 : Value* valueScale;
115 : Value* valueOffset;
116 : Value* valueAccept;
117 : Value* valueAcceptScale;
118 : Value* valueAcceptFT;
119 : std::vector<Value*> valueSigma;
120 : std::vector<Value*> valueSigmaMean;
121 : std::vector<Value*> valueFtilde;
122 :
123 : // restart
124 : std::string status_file_name_;
125 : OFile sfile_;
126 :
127 : // others
128 : bool firstTime;
129 : std::vector<bool> firstTimeW;
130 : bool master;
131 : bool do_reweight_;
132 : unsigned do_optsigmamean_;
133 : unsigned nrep_;
134 : unsigned replica_;
135 :
136 : // selector
137 : unsigned nsel_;
138 : std::string selector_;
139 : unsigned iselect;
140 :
141 : // optimize sigma mean
142 : std::vector< std::vector < std::vector <double> > > sigma_mean2_last_;
143 : unsigned optsigmamean_stride_;
144 : // optimize sigma max
145 : unsigned N_optimized_step_;
146 : unsigned optimized_step_;
147 : bool sigmamax_opt_done_;
148 : std::vector<double> sigma_max_est_;
149 :
150 : // average weights
151 : double decay_w_;
152 : std::vector< std::vector <double> > average_weights_;
153 :
154 : double getEnergyMIGEN(const std::vector<double> &mean, const std::vector<double> &ftilde, const std::vector<double> &sigma,
155 : const double scale, const double offset);
156 : double getEnergySP(const std::vector<double> &mean, const std::vector<double> &sigma,
157 : const double scale, const double offset);
158 : double getEnergySPE(const std::vector<double> &mean, const std::vector<double> &sigma,
159 : const double scale, const double offset);
160 : double getEnergyGJ(const std::vector<double> &mean, const std::vector<double> &sigma,
161 : const double scale, const double offset);
162 : double getEnergyGJE(const std::vector<double> &mean, const std::vector<double> &sigma,
163 : const double scale, const double offset);
164 : void setMetaDer(const unsigned index, const double der);
165 : void getEnergyForceSP(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
166 : void getEnergyForceSPE(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
167 : void getEnergyForceGJ(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
168 : void getEnergyForceGJE(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
169 : void getEnergyForceMIGEN(const std::vector<double> &mean, const std::vector<double> &dmean_x, const std::vector<double> &dmean_b);
170 : double getCalcData(const unsigned index);
171 : void get_weights(double &weight, double &norm, double &neff);
172 : void replica_averaging(const double weight, const double norm, std::vector<double> &mean, std::vector<double> &dmean_b);
173 : void get_sigma_mean(const double weight, const double norm, const double neff, const std::vector<double> &mean);
174 : void do_regression_zero(const std::vector<double> &mean);
175 : void moveTilde(const std::vector<double> &mean_, double &old_energy);
176 : void moveScaleOffset(const std::vector<double> &mean_, double &old_energy);
177 : void moveSigmas(const std::vector<double> &mean_, double &old_energy, const unsigned i, const std::vector<unsigned> &indices, bool &breaknow);
178 : double doMonteCarlo(const std::vector<double> &mean);
179 :
180 : public:
181 : static void registerKeywords( Keywords& keys );
182 : explicit MetainferenceBase(const ActionOptions&);
183 : ~MetainferenceBase();
184 : void Initialise(const unsigned input);
185 : void Selector();
186 : unsigned getNarg();
187 : void setNarg(const unsigned input);
188 : void setParameters(const std::vector<double>& input);
189 : void setParameter(const double input);
190 : void setCalcData(const unsigned index, const double datum);
191 : void setCalcData(const std::vector<double>& data);
192 : bool getDoScore();
193 : unsigned getWstride();
194 : double getScore();
195 : void setScore(const double score);
196 : void setDerivatives();
197 : double getMetaDer(const unsigned index);
198 : void writeStatus();
199 : void turnOnDerivatives() override;
200 : unsigned getNumberOfDerivatives() override;
201 : void lockRequests() override;
202 : void unlockRequests() override;
203 : void calculateNumericalDerivatives( ActionWithValue* a ) override;
204 : void apply() override;
205 : void setArgDerivatives(Value *v, const double &d);
206 : void setAtomsDerivatives(Value*v, const unsigned i, const Vector&d);
207 : void setBoxDerivatives(Value*v, const Tensor&d);
208 : };
209 :
210 : inline
211 : void MetainferenceBase::setNarg(const unsigned input)
212 : {
213 31 : narg = input;
214 : }
215 :
216 : inline
217 : bool MetainferenceBase::getDoScore()
218 : {
219 21926 : return doscore_;
220 : }
221 :
222 : inline
223 : unsigned MetainferenceBase::getWstride()
224 : {
225 1374 : return write_stride_;
226 : }
227 :
228 : inline
229 : unsigned MetainferenceBase::getNarg()
230 : {
231 2225 : return narg;
232 : }
233 :
234 : inline
235 : void MetainferenceBase::setMetaDer(const unsigned index, const double der)
236 : {
237 7146 : metader_[index] = der;
238 : }
239 :
240 : inline
241 : double MetainferenceBase::getMetaDer(const unsigned index)
242 : {
243 909788 : return metader_[index];
244 : }
245 :
246 : inline
247 : double MetainferenceBase::getCalcData(const unsigned index)
248 : {
249 : return calc_data_[index];
250 : }
251 :
252 : inline
253 : void MetainferenceBase::setCalcData(const unsigned index, const double datum)
254 : {
255 3868 : calc_data_[index] = datum;
256 3868 : }
257 :
258 : inline
259 : void MetainferenceBase::setCalcData(const std::vector<double>& data)
260 : {
261 : for(unsigned i=0; i<data.size(); i++) calc_data_[i] = data[i];
262 : }
263 :
264 : inline
265 27 : void MetainferenceBase::setParameters(const std::vector<double>& input) {
266 168 : for(unsigned i=0; i<input.size(); i++) parameters.push_back(input[i]);
267 27 : }
268 :
269 : inline
270 : void MetainferenceBase::setParameter(const double input) {
271 2356 : parameters.push_back(input);
272 2356 : }
273 :
274 : inline
275 : void MetainferenceBase::setScore(const double score) {
276 2225 : valueScore->set(score);
277 2225 : }
278 :
279 : inline
280 82 : void MetainferenceBase::setDerivatives() {
281 : // Get appropriate number of derivatives
282 : // Derivatives are first for arguments and then for atoms
283 : unsigned nder;
284 82 : if( getNumberOfAtoms()>0 ) {
285 82 : nder = 3*getNumberOfAtoms() + 9 + getNumberOfArguments();
286 : } else {
287 0 : nder = getNumberOfArguments();
288 : }
289 :
290 : // Resize all derivative arrays
291 82 : forces.resize( nder ); forcesToApply.resize( nder );
292 21076 : for(int i=0; i<getNumberOfComponents(); ++i) getPntrToComponent(i)->resizeDerivatives(nder);
293 82 : }
294 :
295 : inline
296 3480111 : void MetainferenceBase::turnOnDerivatives() {
297 3480111 : ActionWithValue::turnOnDerivatives();
298 3480111 : }
299 :
300 : inline
301 4099241008 : unsigned MetainferenceBase::getNumberOfDerivatives() {
302 4099241008 : if( getNumberOfAtoms()>0 ) {
303 4099241008 : return 3*getNumberOfAtoms() + 9 + getNumberOfArguments();
304 : }
305 0 : return getNumberOfArguments();
306 : }
307 :
308 : inline
309 687 : void MetainferenceBase::lockRequests() {
310 : ActionAtomistic::lockRequests();
311 : ActionWithArguments::lockRequests();
312 687 : }
313 :
314 : inline
315 687 : void MetainferenceBase::unlockRequests() {
316 : ActionAtomistic::unlockRequests();
317 : ActionWithArguments::unlockRequests();
318 687 : }
319 :
320 : inline
321 75 : void MetainferenceBase::calculateNumericalDerivatives( ActionWithValue* a=NULL ) {
322 75 : if( getNumberOfArguments()>0 ) {
323 48 : ActionWithArguments::calculateNumericalDerivatives( a );
324 : }
325 75 : if( getNumberOfAtoms()>0 ) {
326 75 : Matrix<double> save_derivatives( getNumberOfComponents(), getNumberOfArguments() );
327 1293 : for(int j=0; j<getNumberOfComponents(); ++j) {
328 2322 : for(unsigned i=0; i<getNumberOfArguments(); ++i) if(getPntrToComponent(j)->hasDerivatives()) save_derivatives(j,i)=getPntrToComponent(j)->getDerivative(i);
329 : }
330 75 : calculateAtomicNumericalDerivatives( a, getNumberOfArguments() );
331 1293 : for(int j=0; j<getNumberOfComponents(); ++j) {
332 2322 : for(unsigned i=0; i<getNumberOfArguments(); ++i) if(getPntrToComponent(j)->hasDerivatives()) getPntrToComponent(j)->addDerivative( i, save_derivatives(j,i) );
333 : }
334 : }
335 75 : }
336 :
337 : inline
338 687 : void MetainferenceBase::apply() {
339 687 : bool wasforced=false; forcesToApply.assign(forcesToApply.size(),0.0);
340 32598 : for(int i=0; i<getNumberOfComponents(); ++i) {
341 31911 : if( getPntrToComponent(i)->applyForce( forces ) ) {
342 : wasforced=true;
343 41664408 : for(unsigned i=0; i<forces.size(); ++i) forcesToApply[i]+=forces[i];
344 : }
345 : }
346 687 : if( wasforced ) {
347 350 : addForcesOnArguments( forcesToApply );
348 350 : if( getNumberOfAtoms()>0 ) setForcesOnAtoms( forcesToApply, getNumberOfArguments() );
349 : }
350 687 : }
351 :
352 : inline
353 : void MetainferenceBase::setArgDerivatives(Value *v, const double &d) {
354 160 : v->addDerivative(0,d);
355 : }
356 :
357 : inline
358 1927088 : void MetainferenceBase::setAtomsDerivatives(Value*v, const unsigned i, const Vector&d) {
359 1927088 : const unsigned noa=getNumberOfArguments();
360 1927088 : v->addDerivative(noa+3*i+0,d[0]);
361 1927088 : v->addDerivative(noa+3*i+1,d[1]);
362 1927088 : v->addDerivative(noa+3*i+2,d[2]);
363 1927088 : }
364 :
365 : inline
366 11722 : void MetainferenceBase::setBoxDerivatives(Value* v,const Tensor&d) {
367 11722 : const unsigned noa=getNumberOfArguments();
368 : const unsigned nat=getNumberOfAtoms();
369 11722 : v->addDerivative(noa+3*nat+0,d(0,0));
370 11722 : v->addDerivative(noa+3*nat+1,d(0,1));
371 11722 : v->addDerivative(noa+3*nat+2,d(0,2));
372 11722 : v->addDerivative(noa+3*nat+3,d(1,0));
373 11722 : v->addDerivative(noa+3*nat+4,d(1,1));
374 11722 : v->addDerivative(noa+3*nat+5,d(1,2));
375 11722 : v->addDerivative(noa+3*nat+6,d(2,0));
376 11722 : v->addDerivative(noa+3*nat+7,d(2,1));
377 11722 : v->addDerivative(noa+3*nat+8,d(2,2));
378 11722 : }
379 :
380 :
381 : }
382 : }
383 :
384 : #endif
385 :
|