forked from plumed/plumed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiasRepresentation.cpp
257 lines (242 loc) · 9.82 KB
/
BiasRepresentation.cpp
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
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2013 The plumed team
(see the PEOPLE file at the root of the distribution for a list of names)
See http://www.plumed-code.org for more information.
This file is part of plumed, version 2.0.
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/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "core/Value.h"
#include "Communicator.h"
#include "BiasRepresentation.h"
#include <iostream>
namespace PLMD {
//+PLUMEDOC INTERNAL BiasRepresentation
/*
An internal tool in plumed that is used to represent a bias
*/
//+ENDPLUMEDOC
/// the constructor here
BiasRepresentation::BiasRepresentation(vector<Value*> tmpvalues, Communicator &cc ):hasgrid(false),mycomm(cc){
ndim=tmpvalues.size();
for(int i=0;i<ndim;i++){
values.push_back(tmpvalues[i]);
names.push_back(values[i]->getName());
}
}
/// overload the constructor: add the sigma at constructor time
BiasRepresentation::BiasRepresentation(vector<Value*> tmpvalues, Communicator &cc, vector<double> sigma ):hasgrid(false),histosigma(sigma),mycomm(cc){
ndim=tmpvalues.size();
for(int i=0;i<ndim;i++){
values.push_back(tmpvalues[i]);
names.push_back(values[i]->getName());
}
}
/// overload the constructor: add the grid at constructor time
BiasRepresentation::BiasRepresentation(vector<Value*> tmpvalues, Communicator &cc , vector<string> gmin, vector<string> gmax, vector<unsigned> nbin ):hasgrid(false), rescaledToBias(false), mycomm(cc){
ndim=tmpvalues.size();
for(int i=0;i<ndim;i++){
values.push_back(tmpvalues[i]);
names.push_back(values[i]->getName());
}
// initialize the grid
addGrid(gmin,gmax,nbin);
}
/// overload the constructor with some external sigmas: needed for histogram
BiasRepresentation::BiasRepresentation(vector<Value*> tmpvalues, Communicator &cc , vector<string> gmin, vector<string> gmax, vector<unsigned> nbin , vector<double> sigma):hasgrid(false), rescaledToBias(false),histosigma(sigma),mycomm(cc){
ndim=tmpvalues.size();
for(int i=0;i<ndim;i++){
values.push_back(tmpvalues[i]);
names.push_back(values[i]->getName());
}
// initialize the grid
addGrid(gmin,gmax,nbin);
}
void BiasRepresentation::addGrid( vector<string> gmin, vector<string> gmax, vector<unsigned> nbin ){
plumed_massert(hills.size()==0,"you can set the grid before loading the hills");
plumed_massert(hasgrid==false,"to build the grid you should not having the grid in this bias representation");
string ss; ss="file.free";
vector<Value*> vv;for(unsigned i=0;i<values.size();i++)vv.push_back(values[i]);
//cerr<<" initializing grid "<<endl;
BiasGrid_=new Grid(ss,vv,gmin,gmax,nbin,false,true);
hasgrid=true;
}
bool BiasRepresentation::hasSigmaInInput(){
if(histosigma.size()==0){return false;}else{return true;}
}
void BiasRepresentation::setRescaledToBias(bool rescaled){
plumed_massert(hills.size()==0,"you can set the rescaling function only before loading hills");
rescaledToBias=rescaled;
}
const bool & BiasRepresentation::isRescaledToBias(){
return rescaledToBias;
}
unsigned BiasRepresentation::getNumberOfDimensions(){
return values.size();
}
vector<string> BiasRepresentation::getNames(){
return names;
}
const string & BiasRepresentation::getName(unsigned i){
return names[i];
}
const vector<Value*>& BiasRepresentation::getPtrToValues(){
return values;
}
Value* BiasRepresentation::getPtrToValue(unsigned i){
return values[i];
}
KernelFunctions* BiasRepresentation::readFromPoint(IFile *ifile){
vector<double> cc( names.size() );
for(unsigned i=0;i<names.size();++i){
ifile->scanField(names[i],cc[i]);
}
double h=1.0;
return new KernelFunctions(cc,histosigma,"gaussian",false,h,false);
}
void BiasRepresentation::pushKernel( IFile *ifile ){
KernelFunctions *kk;
// here below the reading of the kernel is completely hidden
if(histosigma.size()==0){
ifile->allowIgnoredFields();
kk=KernelFunctions::read(ifile,names) ;
}else{
// when doing histogram assume gaussian with a given diagonal sigma
// and neglect all the rest
kk=readFromPoint(ifile) ;
}
hills.push_back(kk);
// the bias factor is not something about the kernels but
// must be stored to keep the bias/free energy duality
string dummy; double dummyd;
if(ifile->FieldExist("biasf")){
ifile->scanField("biasf",dummy);
Tools::convert(dummy,dummyd);
}else{dummyd=1.0;}
biasf.push_back(dummyd);
// the domain does not pertain to the kernel but to the values here defined
string mins,maxs,minv,maxv,mini,maxi;mins="min_";maxs="max_";
for(unsigned i=0 ; i<ndim; i++){
if(values[i]->isPeriodic()){
ifile->scanField(mins+names[i],minv);
ifile->scanField(maxs+names[i],maxv);
// verify that the domain is correct
values[i]->getDomain(mini,maxi);
plumed_massert(mini==minv,"the input periodicity in hills and in value definition does not match" );
plumed_massert(maxi==maxv,"the input periodicity in hills and in value definition does not match" );
}
}
// if grid is defined then it should be added on the grid
//cerr<<"now with "<<hills.size()<<endl;
if(hasgrid){
vector<unsigned> nneighb=kk->getSupport(BiasGrid_->getDx());
vector<unsigned> neighbors=BiasGrid_->getNeighbors(kk->getCenter(),nneighb);
vector<double> der(ndim);
vector<double> xx(ndim);
if(mycomm.Get_size()==1){
for(int i=0;i<neighbors.size();++i){
unsigned ineigh=neighbors[i];
for(int j=0;j<ndim;++j){der[j]=0.0;}
BiasGrid_->getPoint(ineigh,xx);
// assign xx to a new vector of values
for(int j=0;j<ndim;++j){values[j]->set(xx[j]);}
double bias=kk->evaluate(values,der,true);
if(rescaledToBias){
double f=(biasf.back()-1.)/(biasf.back());
bias*=f;
for(int j=0;j<ndim;++j){der[j]*=f;}
}
BiasGrid_->addValueAndDerivatives(ineigh,bias,der);
}
} else {
unsigned stride=mycomm.Get_size();
unsigned rank=mycomm.Get_rank();
vector<double> allder(ndim*neighbors.size(),0.0);
vector<double> allbias(neighbors.size(),0.0);
vector<double> tmpder(ndim);
for(unsigned i=rank;i<neighbors.size();i+=stride){
unsigned ineigh=neighbors[i];
BiasGrid_->getPoint(ineigh,xx);
for(int j=0;j<ndim;++j){values[j]->set(xx[j]);}
allbias[i]=kk->evaluate(values,tmpder,true);
if(rescaledToBias){
double f=(biasf.back()-1.)/(biasf.back());
allbias[i]*=f;
for(int j=0;j<ndim;++j){tmpder[j]*=f;}
}
// this solution with the temporary vector is rather bad, probably better to take
// a pointer of double as it was in old gaussian
for(int j=0;j<ndim;++j){ allder[ndim*i+j]=tmpder[j];tmpder[j]=0.;}
}
mycomm.Sum(&allbias[0],allbias.size());
mycomm.Sum(&allder[0],allder.size());
for(unsigned i=0;i<neighbors.size();++i){
unsigned ineigh=neighbors[i];
for(unsigned j=0;j<ndim;++j){der[j]=allder[ndim*i+j];}
BiasGrid_->addValueAndDerivatives(ineigh,allbias[i],der);
}
}
}
}
int BiasRepresentation::getNumberOfKernels(){
return hills.size();
}
Grid* BiasRepresentation::getGridPtr(){
plumed_massert(hasgrid,"if you want the grid pointer then you should have defined a grid before");
return BiasGrid_;
}
void BiasRepresentation::getMinMaxBin(vector<double> &vmin, vector<double> &vmax, vector<unsigned> &vbin){
vector<double> ss,cc,binsize;
vmin.clear();vmin.resize(ndim,10.e20);
vmax.clear();vmax.resize(ndim,-10.e20);
vbin.clear();vbin.resize(ndim);
binsize.clear();binsize.resize(ndim,10.e20);
int ndiv=10; // adjustable parameter: division per support
for(unsigned i=0;i<hills.size();i++){
if(histosigma.size()!=0){
ss=histosigma;
}else{
ss=hills[i]->getContinuousSupport();
}
cc=hills[i]->getCenter();
for(unsigned j=0;j<ndim;j++){
double dmin=cc[j]-ss[j];
double dmax=cc[j]+ss[j];
double ddiv=ss[j]/double(ndiv);
if(dmin<vmin[j])vmin[j]=dmin;
if(dmax>vmax[j])vmax[j]=dmax;
if(ddiv<binsize[j])binsize[j]=ddiv;
}
}
for(unsigned j=0;j<ndim;j++){
// reset to periodicity
if(values[j]->isPeriodic()){
double minv,maxv;
values[j]->getDomain(minv,maxv);
if(minv>vmin[j])vmin[j]=minv;
if(maxv<vmax[j])vmax[j]=maxv;
}
vbin[j]=static_cast<unsigned>(ceil((vmax[j]-vmin[j])/binsize[j]) );
}
}
void BiasRepresentation::clear(){
// clear the hills
for(vector<KernelFunctions*>::const_iterator it = hills.begin(); it != hills.end(); ++it)
{
delete *it;
}
hills.clear();
// clear the grid
if(hasgrid){
BiasGrid_->clear();
}
}
}