29 const double _upperboundary,
32 const double _lowerboundary2,
33 const double _upperboundary2,
35 const double _krestr2,
36 const std::string& _outputfile,
37 const int _outputfreq,
39 const std::string& _inputfile,
40 const bool _outputgrad,
42 const double _temperature) :
43 eABF(_outputfile, _outputfreq, _restart, _inputfile, _outputgrad, _gradfreq, _temperature)
49 width.push_back(_width);
50 width.push_back(_width2);
52 krestr.push_back(_krestr2);
59 bool eABF2D::initialize()
61 for (
int i = 0; i < 2; i++)
70 countall =
new int*[
bins[0] *
bins[0]];
71 for (
int i = 0; i < bins[0] * bins[0]; i++)
73 countall[i] =
new int[bins[1] * bins[1]];
74 for (
int j = 0; j < bins[1] * bins[1]; j++)
79 for (
int i = 0; i < bins[0]; i++)
81 sumx1.push_back(std::vector<double>(bins[1], 0));
82 sumx21.push_back(std::vector<double>(bins[1], 0));
83 sumx2.push_back(std::vector<double>(bins[1], 0));
84 sumx22.push_back(std::vector<double>(bins[1], 0));
85 county.push_back(std::vector<int>(bins[1], 0));
97 std::vector<std::string> data;
114 if (abf_force > 150 && eabf_force < -150)
116 else if (abf_force < -150 && eabf_force > 150)
119 if (abf_force2 > 150 && eabf_force2 < -150)
121 else if (abf_force2 < -150 && eabf_force2 > 150)
124 int binx = int(floor(abf_force /
width[0])) -
min[0];
125 int biny = int(floor(eabf_force /
width[0])) -
min[0];
126 int binx2 = int(floor(abf_force2 /
width[1])) -
min[1];
127 int biny2 = int(floor(eabf_force2 /
width[1])) -
min[1];
140 if (binx < 0 || binx >= bins[0] || biny < 0 || biny >= bins[0] || binx2 < 0 || binx2 >= bins[1] || biny2 < 0 || biny2 >= bins[1])
145 countall[binx*bins[0] + biny][binx2*bins[1] + biny2]++;
148 if (countall[binx*bins[0] + biny][binx2*bins[1] + biny2] == 1)
150 county[biny][biny2]++;
153 sumx1[biny][biny2] += abf_force;
154 sumx21[biny][biny2] += abf_force * abf_force;
155 sumx2[biny][biny2] += abf_force2;
156 sumx22[biny][biny2] += abf_force2 * abf_force2;
165 bool eABF2D::readfile()
167 std::ifstream file(
inputfile.c_str(), std::ios::in);
170 int temp_line, temp_bins, temp_bins2;
171 double temp_lowerboundary, temp_width, temp_lowerboundary2, temp_width2;
172 file >> temp_line >> temp_lowerboundary >> temp_width >> temp_bins >>
krestr[0] >> temp_lowerboundary2 >> temp_width2 >> temp_bins2 >>
krestr[1] >>
temperature;
175 temp_bins = temp_bins + 20;
176 temp_bins2 = temp_bins2 + 20;
178 int x = 0,
y = 0, m = 0, n = 0;
179 int pos0 = 0, pos1 = 0, temp_countall = 0;
180 for (
int i = 0; i < temp_line; i++)
182 file >> x >>
y >> m >> n;
185 file >> temp_countall;
187 pos0 = convertscale(temp_lowerboundary, x, 1) * bins[0] + convertscale(temp_lowerboundary,
y, 1);
188 pos1 = convertscale(temp_lowerboundary2, m, 2) * bins[1] + convertscale(temp_lowerboundary2, n, 2);
190 if (countall[pos0][pos1] == 0)
192 countall[pos0][pos1] += temp_countall;
195 double temp_sumx1, temp_sumx21, temp_sumx2, temp_sumx22;
197 for (
int i = 0; i < temp_bins; i++)
199 for (
int j = 0; j < temp_bins2; j++)
201 file >> x >>
y >> temp_sumx1 >> temp_sumx21 >> temp_sumx2 >> temp_sumx22 >> temp_county;
205 sumx1[convertscale(temp_lowerboundary, x, 1)][convertscale(temp_lowerboundary2,
y, 2)] += temp_sumx1;
206 sumx21[convertscale(temp_lowerboundary, x, 1)][convertscale(temp_lowerboundary2,
y, 2)] += temp_sumx21;
207 sumx2[convertscale(temp_lowerboundary, x, 1)][convertscale(temp_lowerboundary2,
y, 2)] += temp_sumx2;
208 sumx22[convertscale(temp_lowerboundary, x, 1)][convertscale(temp_lowerboundary2,
y, 2)] += temp_sumx22;
209 county[convertscale(temp_lowerboundary, x, 1)][convertscale(temp_lowerboundary2,
y, 2)] += temp_county;
217 bool eABF2D::writefile()
const
220 file<<std::setprecision(15);
223 file <<
lowerboundary[1] <<
" " <<
width[1] <<
" " << bins[1] - 20 <<
" " << krestr[1] <<
" " << temperature << std::endl;
225 for (
int i = 0; i < bins[0]; i++)
226 for (
int j = 0; j < bins[0]; j++)
227 for (
int m = 0; m < bins[1]; m++)
228 for (
int n = 0; n < bins[1]; n++)
229 if (countall[i*bins[0] + j][m*bins[1] + n] != 0)
231 file << i <<
" " << j <<
" " << m <<
" " << n <<
" " << countall[i*bins[0] + j][m*bins[1] + n] << std::endl;
233 for (
int i = 0; i < bins[0]; i++)
234 for (
int j = 0; j < bins[1]; j++)
235 file << i <<
" " << j <<
" " << sumx1[i][j] <<
" " << sumx21[i][j] <<
" " << sumx2[i][j] <<
" " << sumx22[i][j] <<
" " << county[i][j] <<
" " << std::endl;
261 os<<
"# 2"<<std::endl;
263 os <<
"# " <<
lowerboundary[0] <<
" " <<
width[0] <<
" " << (bins[0] - 20) <<
" " << 0 << std::endl;
264 os <<
"# " <<
lowerboundary[1] <<
" " <<
width[1] <<
" " << (bins[1] - 20) <<
" " << 0 << std::endl;
269 bool eABF2D::calpmf()
const
274 std::vector<std::vector<double> > x_av(bins[0]);
275 std::vector<std::vector<double> > x_av2(bins[0]);
276 std::vector<std::vector<double> > sigma21(bins[0]);
277 std::vector<std::vector<double> > sigma22(bins[0]);
278 for (
int i = 0; i < bins[0]; i++)
280 x_av[i] = (std::vector<double>(bins[1], 0));
281 x_av2[i] = (std::vector<double>(bins[1], 0));
282 sigma21[i] = (std::vector<double>(bins[1], 0));
283 sigma22[i] = (std::vector<double>(bins[1], 0));
285 for (
int biny = 0; biny < bins[0]; biny++)
287 for (
int biny2 = 0; biny2 < bins[1]; biny2++)
289 norm = county[biny][biny2] > 0 ? county[biny][biny2] : 1;
290 x_av[biny][biny2] = sumx1[biny][biny2] / norm;
291 x_av2[biny][biny2] = sumx2[biny][biny2] / norm;
292 sigma21[biny][biny2] = sumx21[biny][biny2] / norm - x_av[biny][biny2] * x_av[biny][biny2];
293 sigma22[biny][biny2] = sumx22[biny][biny2] / norm - x_av2[biny][biny2] * x_av2[biny][biny2];
298 static std::string gridfilename =
outputfile +
".grad";
299 static std::string histfilename =
outputfile +
".hist.grad";
300 static std::string countfilename =
outputfile +
".count";
304 ofstream_namd ofile_hist(histfilename.c_str(), std::ios::app);
308 writehead(ofile_hist);
309 writehead(ofile_count);
316 double x1, x2, y1, y2, av1, av2, diff_av1, diff_av2, grad1, grad2;
317 for (
int binx = 0; binx < bins[0]; binx++)
319 x1 = (binx +
min[0] + 0.5) *
width[0];
320 for (
int binx2 = 0; binx2 < bins[1]; binx2++)
322 x2 = (binx2 +
min[1] + 0.5) *
width[1];
329 for (
int biny = 0; biny < bins[0]; biny++)
331 y1 = (biny +
min[0] + 0.5) *
width[0];
332 for (
int biny2 = 0; biny2 < bins[1]; biny2++)
334 y2 = (biny2 +
min[1] + 0.5) *
width[1];
335 norm += countall[binx * bins[0] + biny][binx2 * bins[1] + biny2];
337 if (sigma21[biny][biny2]>0.00001 || sigma21[biny][biny2] < -0.00001)
339 av1 += countall[binx * bins[0] + biny][binx2 * bins[1] + biny2] * (x1 - x_av[biny][biny2]) / sigma21[biny][biny2];
341 else if (countall[binx * bins[0] + biny][binx2 * bins[1] + biny2] > 1)
345 diff_av1 += countall[binx * bins[0] + biny][binx2 * bins[1] + biny2] * (x1 - y1);
347 if (sigma22[biny][biny2]>0.00001 || sigma22[biny][biny2] < -0.00001)
349 av2 += countall[binx * bins[0] + biny][binx2 * bins[1] + biny2] * (x2 - x_av2[biny][biny2]) / sigma22[biny][biny2];
351 else if (countall[binx * bins[0] + biny][binx2 * bins[1] + biny2] > 1)
356 diff_av2 += countall[binx * bins[0] + biny][binx2 * bins[1] + biny2] * (x2 - y2);
360 diff_av1 /= (norm > 0 ? norm : 1);
361 diff_av2 /= (norm > 0 ? norm : 1);
366 grad1 = av1 - krestr[0] * diff_av1;
367 grad2 = av2 - krestr[1] * diff_av2;
371 ofile << x1 <<
" " << x2 <<
" " << grad1 <<
" " << grad2 << std::endl;
372 ofile_hist << x1 <<
" " << x2 <<
" " << grad1 <<
" " << grad2 << std::endl;
373 ofile_count << x1 <<
" " << x2 <<
" " << norm << std::endl;
383 ofile_count << std::endl;
std::vector< double > lowerboundary
std::vector< double > width
std::vector< double > krestr
int chartoint(const std::string &c)
double chartodouble(const std::string &c)
eABF2D(const double _lowerboundary, const double _upperboundary, const double _width, const double _krestr, const double _lowerboundary2, const double _upperboundary2, const double _width2, const double _krestr2, const std::string &_outputfile, const int _outputfreq, const bool _restart, const std::string &_inputfile, const bool _outputgrad, const int _gradfreq, const double _temperature)
void split(const std::string &s, std::vector< std::string > &ret)
std::vector< double > upperboundary
bool update(const std::string &)
int doubletoint(const double)