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