10#ifndef COLVAR_UIESTIMATOR_H
11#define COLVAR_UIESTIMATOR_H
26namespace UIestimator {
27 const int Y_SIZE = 21;
29 const int HALF_Y_SIZE = 10;
30 const int EXTENDED_X_SIZE = HALF_Y_SIZE;
31 const double EPSILON = 0.000001;
37 n_matrix(
const std::vector<double> & lowerboundary_input,
38 const std::vector<double> & upperboundary_input,
39 const std::vector<double> & width_input,
40 const int y_size_input) {
44 this->lowerboundary = lowerboundary_input;
45 this->upperboundary = upperboundary_input;
46 this->width = width_input;
47 this->dimension = lowerboundary_input.size();
48 this->y_size = y_size_input;
49 this->y_total_size = int(
cvm::pow(
double(y_size_input),
double(dimension)) + EPSILON);
53 for (i = 0; i < dimension; i++) {
54 x_size.push_back(
int((upperboundary_input[i] - lowerboundary_input[i]) / width_input[i] + EPSILON));
55 x_total_size *= x_size[i];
59 matrix.reserve(x_total_size);
60 for (i = 0; i < x_total_size; i++) {
61 matrix.push_back(std::vector<int>(y_total_size, 0));
64 temp.resize(dimension);
67 int get_value(
const std::vector<double> & x,
const std::vector<double> & y) {
68 return matrix[convert_x(x)][convert_y(x, y)];
71 void set_value(
const std::vector<double> & x,
const std::vector<double> & y,
const int value) {
72 matrix[convert_x(x)][convert_y(x,y)] = value;
75 void increase_value(
const std::vector<double> & x,
const std::vector<double> & y,
const int value) {
76 matrix[convert_x(x)][convert_y(x,y)] += value;
80 std::vector<double> lowerboundary;
81 std::vector<double> upperboundary;
82 std::vector<double> width;
84 std::vector<int> x_size;
89 std::vector<std::vector<int> > matrix;
91 std::vector<int> temp;
93 int convert_x(
const std::vector<double> & x) {
97 for (i = 0; i < dimension; i++) {
98 temp[i] = int((x[i] - lowerboundary[i]) / width[i] + EPSILON);
102 for (i = 0; i < dimension; i++) {
103 if (i + 1 < dimension) {
105 for (j = i + 1; j < dimension; j++)
107 index += temp[i] * x_temp;
115 int convert_y(
const std::vector<double> & x,
const std::vector<double> & y) {
119 for (i = 0; i < dimension; i++) {
120 temp[i] = int(round((round(y[i] / width[i] + EPSILON) - round(x[i] / width[i] + EPSILON)) + (y_size - 1) / 2 + EPSILON));
124 for (i = 0; i < dimension; i++) {
125 if (i + 1 < dimension)
126 index += temp[i] * int(
cvm::pow(
double(y_size),
double(dimension - i - 1)) + EPSILON);
133 double round(
double r) {
134 return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
139 template <
typename T>
144 n_vector(
const std::vector<double> & lowerboundary_input,
145 const std::vector<double> & upperboundary_input,
146 const std::vector<double> & width_input,
147 const int y_size_input,
148 const T & default_value) {
150 this->width = width_input;
151 this->dimension = lowerboundary_input.size();
154 for (
int i = 0; i < dimension; i++) {
155 this->lowerboundary.push_back(lowerboundary_input[i] - (y_size_input - 1) / 2 * width_input[i] - EPSILON);
156 this->upperboundary.push_back(upperboundary_input[i] + (y_size_input - 1) / 2 * width_input[i] + EPSILON);
158 x_size.push_back(
int((this->upperboundary[i] - this->lowerboundary[i]) / this->width[i] + EPSILON));
159 x_total_size *= x_size[i];
163 vector.resize(x_total_size, default_value);
165 temp.resize(dimension);
168 T & get_value(
const std::vector<double> & x) {
169 return vector[convert_x(x)];
172 void set_value(
const std::vector<double> & x,
const T value) {
173 vector[convert_x(x)] = value;
176 void increase_value(
const std::vector<double> & x,
const T value) {
177 vector[convert_x(x)] += value;
181 std::vector<double> lowerboundary;
182 std::vector<double> upperboundary;
183 std::vector<double> width;
185 std::vector<int> x_size;
188 std::vector<T> vector;
190 std::vector<int> temp;
192 int convert_x(
const std::vector<double> & x) {
196 for (i = 0; i < dimension; i++) {
197 temp[i] = int((x[i] - lowerboundary[i]) / width[i] + EPSILON);
201 for (i = 0; i < dimension; i++) {
202 if (i + 1 < dimension) {
204 for (j = i + 1; j < dimension; j++)
206 index += temp[i] * x_temp;
221 UIestimator(
const std::vector<double> & lowerboundary_input,
222 const std::vector<double> & upperboundary_input,
223 const std::vector<double> & width_input,
224 const std::vector<double> & krestr_input,
225 const std::string & output_filename_input,
226 const int output_freq_input,
227 const bool restart_input,
228 const std::vector<std::string> & input_filename_input,
229 const double temperature_input) {
232 this->lowerboundary = lowerboundary_input;
233 this->upperboundary = upperboundary_input;
234 this->width = width_input;
235 this->krestr = krestr_input;
236 this->output_filename = output_filename_input;
237 this->output_freq = output_freq_input;
238 this->restart = restart_input;
239 this->input_filename = input_filename_input;
240 this->temperature = temperature_input;
244 dimension = lowerboundary.size();
246 for (i = 0; i < dimension; i++) {
247 sum_x.push_back(
n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
248 sum_x_square.push_back(
n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
250 x_av.push_back(
n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
251 sigma_square.push_back(
n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
254 count_y =
n_vector<int>(lowerboundary, upperboundary, width, Y_SIZE, 0);
255 distribution_x_y =
n_matrix(lowerboundary, upperboundary, width, Y_SIZE);
258 count =
n_vector<int>(lowerboundary, upperboundary, width, 1, 0);
263 if (dimension == 1) {
264 std::vector<double> upperboundary_temp = upperboundary;
265 upperboundary_temp[0] = upperboundary[0] + width[0];
266 oneD_pmf =
n_vector<double>(lowerboundary, upperboundary_temp, width, 1, 0.0);
269 if (restart ==
true) {
271 input_count =
n_vector<int>(lowerboundary, upperboundary, width, 1, 0);
275 std::vector<double> loop_flag(dimension, 0);
276 for (i = 0; i < dimension; i++) {
277 loop_flag[i] = lowerboundary[i];
282 for (j = 0; j < dimension; j++) {
283 input_grad.set_value(loop_flag, std::vector<double>(dimension,0));
285 input_count.set_value(loop_flag, 0);
290 loop_flag[i] += width[i];
291 if (loop_flag[i] > upperboundary[i] - width[i] + EPSILON) {
292 loop_flag[i] = lowerboundary[i];
299 read_inputfiles(input_filename);
307 std::vector<double> x, std::vector<double> y) {
311 for (i = 0; i < dimension; i++) {
314 if (x[i] > 150 && y[i] < -150) {
317 if (x[i] < -150 && y[i] > 150) {
321 if (x[i] < lowerboundary[i] - EXTENDED_X_SIZE * width[i] + EPSILON || x[i] > upperboundary[i] + EXTENDED_X_SIZE * width[i] - EPSILON \
322 || y[i] - x[i] < -HALF_Y_SIZE * width[i] + EPSILON || y[i] - x[i] > HALF_Y_SIZE * width[i] - EPSILON \
323 || y[i] - lowerboundary[i] < -HALF_Y_SIZE * width[i] + EPSILON || y[i] - upperboundary[i] > HALF_Y_SIZE * width[i] - EPSILON)
327 for (i = 0; i < dimension; i++) {
328 sum_x[i].increase_value(y, x[i]);
329 sum_x_square[i].increase_value(y, x[i] * x[i]);
331 count_y.increase_value(y, 1);
333 for (i = 0; i < dimension; i++) {
335 if (x[i] < lowerboundary[i] + EPSILON || x[i] > upperboundary[i] - EPSILON)
338 distribution_x_y.increase_value(x, y, 1);
344 void update_output_filename(
const std::string& filename) {
345 output_filename = filename;
349 std::vector<n_vector<double> > sum_x;
350 std::vector<n_vector<double> > sum_x_square;
356 std::vector<double> lowerboundary;
357 std::vector<double> upperboundary;
358 std::vector<double> width;
359 std::vector<double> krestr;
360 std::string output_filename;
363 std::vector<std::string> input_filename;
375 std::vector<n_vector<double> > x_av;
376 std::vector<n_vector<double> > sigma_square;
389 std::vector<double> loop_flag(dimension, 0);
390 for (i = 0; i < dimension; i++) {
391 loop_flag[i] = lowerboundary[i] - HALF_Y_SIZE * width[i];
396 norm = count_y.get_value(loop_flag) > 0 ? count_y.get_value(loop_flag) : 1;
397 for (j = 0; j < dimension; j++) {
398 x_av[j].set_value(loop_flag, sum_x[j].get_value(loop_flag) / norm);
399 sigma_square[j].set_value(loop_flag, sum_x_square[j].get_value(loop_flag) / norm - x_av[j].get_value(loop_flag) * x_av[j].get_value(loop_flag));
405 loop_flag[i] += width[i];
406 if (loop_flag[i] > upperboundary[i] + HALF_Y_SIZE * width[i] - width[i] + EPSILON) {
407 loop_flag[i] = lowerboundary[i] - HALF_Y_SIZE * width[i];
416 std::vector<double> av(dimension, 0);
417 std::vector<double> diff_av(dimension, 0);
419 std::vector<double> loop_flag_x(dimension, 0);
420 std::vector<double> loop_flag_y(dimension, 0);
421 for (i = 0; i < dimension; i++) {
422 loop_flag_x[i] = lowerboundary[i];
423 loop_flag_y[i] = loop_flag_x[i] - HALF_Y_SIZE * width[i];
429 for (k = 0; k < dimension; k++) {
432 loop_flag_y[k] = loop_flag_x[k] - HALF_Y_SIZE * width[k];
437 norm += distribution_x_y.get_value(loop_flag_x, loop_flag_y);
438 for (k = 0; k < dimension; k++) {
439 if (sigma_square[k].get_value(loop_flag_y) > EPSILON || sigma_square[k].get_value(loop_flag_y) < -EPSILON)
440 av[k] += distribution_x_y.get_value(loop_flag_x, loop_flag_y) * ( (loop_flag_x[k] + 0.5 * width[k]) - x_av[k].get_value(loop_flag_y)) / sigma_square[k].get_value(loop_flag_y);
442 diff_av[k] += distribution_x_y.get_value(loop_flag_x, loop_flag_y) * (loop_flag_x[k] - loop_flag_y[k]);
448 loop_flag_y[j] += width[j];
449 if (loop_flag_y[j] > loop_flag_x[j] + HALF_Y_SIZE * width[j] - width[j] + EPSILON) {
450 loop_flag_y[j] = loop_flag_x[j] - HALF_Y_SIZE * width[j];
458 std::vector<double> grad_temp(dimension, 0);
459 for (k = 0; k < dimension; k++) {
460 diff_av[k] /= (norm > 0 ? norm : 1);
461 av[k] = proxy->boltzmann() * temperature * av[k] / (norm > 0 ? norm : 1);
462 grad_temp[k] = av[k] - krestr[k] * diff_av[k];
464 grad.set_value(loop_flag_x, grad_temp);
465 count.set_value(loop_flag_x, norm);
470 loop_flag_x[i] += width[i];
471 if (loop_flag_x[i] > upperboundary[i] - width[i] + EPSILON) {
472 loop_flag_x[i] = lowerboundary[i];
485 std::vector<double> last_position(1, 0);
486 std::vector<double> position(1, 0);
492 oneD_pmf.set_value(lowerboundary, 0);
493 last_position = lowerboundary;
494 for (i = lowerboundary[0] + width[0]; i < upperboundary[0] + EPSILON; i += width[0]) {
495 position[0] = i + EPSILON;
496 if (restart ==
false || input_count.get_value(last_position) == 0) {
497 dG = oneD_pmf.get_value(last_position) + grad.get_value(last_position)[0] * width[0];
500 dG = oneD_pmf.get_value(last_position) + ((grad.get_value(last_position)[0] * count.get_value(last_position) + input_grad.get_value(last_position)[0] * input_count.get_value(last_position)) / (count.get_value(last_position) + input_count.get_value(last_position))) * width[0];
504 oneD_pmf.set_value(position, dG);
505 last_position[0] = i + EPSILON;
508 for (i = lowerboundary[0]; i < upperboundary[0] + EPSILON; i += width[0]) {
509 position[0] = i + EPSILON;
510 oneD_pmf.set_value(position, oneD_pmf.get_value(position) - min);
515 void write_1D_pmf() {
516 std::string pmf_filename = output_filename +
".UI.pmf";
524 std::vector<double> position(1, 0);
525 for (
double i = lowerboundary[0]; i < upperboundary[0] + EPSILON; i += width[0]) {
526 ofile_pmf << i <<
" ";
527 position[0] = i + EPSILON;
528 ofile_pmf << oneD_pmf.get_value(position) << std::endl;
536 void writehead(std::ostream& os)
const {
537 os <<
"# " << dimension << std::endl;
538 for (
int i = 0; i < dimension; i++) {
539 os <<
"# " << lowerboundary[i] <<
" " << width[i] <<
" " << int((upperboundary[i] - lowerboundary[i]) / width[i] + EPSILON) <<
" " << 0 << std::endl;
545 void write_interal_data() {
546 std::string internal_filename = output_filename +
".UI.internal";
551 std::vector<double> loop_flag(dimension, 0);
552 for (
int i = 0; i < dimension; i++) {
553 loop_flag[i] = lowerboundary[i];
558 for (
int j = 0; j < dimension; j++) {
559 ofile_internal << loop_flag[j] + 0.5 * width[j] <<
" ";
562 for (
int k = 0; k < dimension; k++) {
563 ofile_internal << grad.get_value(loop_flag)[k] <<
" ";
566 std::vector<double> ii(dimension,0);
567 for (
double i = loop_flag[0] - 10; i < loop_flag[0] + 10 + EPSILON; i+= width[0]) {
568 for (
double j = loop_flag[1] - 10; j< loop_flag[1] + 10 + EPSILON; j+=width[1]) {
571 ofile_internal << i <<
" "<<j<<
" "<< distribution_x_y.get_value(loop_flag,ii)<<
" ";
574 ofile_internal << std::endl;
579 loop_flag[n] += width[n];
580 if (loop_flag[n] > upperboundary[n] - width[n] + EPSILON) {
581 loop_flag[n] = lowerboundary[n];
593 std::string grad_filename = output_filename +
".UI.grad";
594 std::string hist_filename = output_filename +
".UI.hist.grad";
595 std::string count_filename = output_filename +
".UI.count";
607 "gradient history file");
612 writehead(ofile_hist);
613 writehead(ofile_count);
615 if (dimension == 1) {
620 std::vector<double> loop_flag(dimension, 0);
621 for (i = 0; i < dimension; i++) {
622 loop_flag[i] = lowerboundary[i];
627 for (j = 0; j < dimension; j++) {
628 ofile << loop_flag[j] + 0.5 * width[j] <<
" ";
629 ofile_hist << loop_flag[j] + 0.5 * width[j] <<
" ";
630 ofile_count << loop_flag[j] + 0.5 * width[j] <<
" ";
633 if (restart ==
false) {
634 for (j = 0; j < dimension; j++) {
635 ofile << grad.get_value(loop_flag)[j] <<
" ";
636 ofile_hist << grad.get_value(loop_flag)[j] <<
" ";
639 ofile_hist << std::endl;
640 ofile_count << count.get_value(loop_flag) <<
" " <<std::endl;
643 double final_grad = 0;
644 for (j = 0; j < dimension; j++) {
645 int total_count_temp = (count.get_value(loop_flag) + input_count.get_value(loop_flag));
646 if (input_count.get_value(loop_flag) == 0)
647 final_grad = grad.get_value(loop_flag)[j];
649 final_grad = ((grad.get_value(loop_flag)[j] * count.get_value(loop_flag) + input_grad.get_value(loop_flag)[j] * input_count.get_value(loop_flag)) / total_count_temp);
650 ofile << final_grad <<
" ";
651 ofile_hist << final_grad <<
" ";
654 ofile_hist << std::endl;
655 ofile_count << (count.get_value(loop_flag) + input_count.get_value(loop_flag)) <<
" " <<std::endl;
661 loop_flag[i] += width[i];
662 if (loop_flag[i] > upperboundary[i] - width[i] + EPSILON) {
663 loop_flag[i] = lowerboundary[i];
666 ofile_hist << std::endl;
667 ofile_count << std::endl;
681 void read_inputfiles(
const std::vector<std::string> filename)
689 std::vector<double> loop_bin_size(dimension, 0);
690 std::vector<double> position_temp(dimension, 0);
691 std::vector<double> grad_temp(dimension, 0);
693 for (i = 0; i < int(filename.size()); i++) {
694 int size = 1 , size_temp = 0;
696 std::string count_filename = filename[i] +
".UI.count";
697 std::string grad_filename = filename[i] +
".UI.grad";
699 std::istream &count_file =
700 proxy->input_stream(count_filename,
"count filename");
701 std::istream &grad_file =
702 proxy->input_stream(grad_filename,
"gradient filename");
704 if (!count_file || !grad_file) {
708 count_file >> sharp >> dimension_temp;
709 grad_file >> sharp >> dimension_temp;
711 for (j = 0; j < dimension; j++) {
712 count_file >> sharp >> nothing >> nothing >> size_temp >> nothing;
713 grad_file >> sharp >> nothing >> nothing >> nothing >> nothing;
717 for (j = 0; j < size; j++) {
719 for (k = 0; k < dimension; k++) {
720 count_file >> position_temp[k];
721 grad_file >> nothing;
724 for (l = 0; l < dimension; l++) {
725 grad_file >> grad_temp[l];
727 count_file >> count_temp;
729 while (position_temp[i] < lowerboundary[i] - EPSILON || position_temp[i] > upperboundary[i] + EPSILON);
731 if (count_temp == 0) {
735 for (m = 0; m < dimension; m++) {
736 grad_temp[m] = (grad_temp[m] * count_temp + input_grad.get_value(position_temp)[m] * input_count.get_value(position_temp)) / (count_temp + input_count.get_value(position_temp));
738 input_grad.set_value(position_temp, grad_temp);
739 input_count.increase_value(position_temp, count_temp);
742 proxy->close_input_stream(count_filename);
743 proxy->close_input_stream(grad_filename);
Definition: colvar_UIestimator.h:215
Definition: colvar_UIestimator.h:33
Definition: colvar_UIestimator.h:140
static real pow(real const &x, real const &y)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:115
static int backup_file(char const *filename)
Backup a file before writing it.
Definition: colvarmodule.cpp:1752
static colvarmodule * main()
Access the one instance of the Colvars module.
Definition: colvarmodule.cpp:185
static colvarproxy * proxy
Pointer to the proxy object, used to retrieve atomic data from the hosting program; it is static in o...
Definition: colvarmodule.h:860
long long step_number
Use a 64-bit integer to store the step number.
Definition: colvarmodule.h:92
virtual int close_output_stream(std::string const &output_name)
Closes the given output file/channel.
Definition: colvarproxy_io.cpp:475
virtual std::ostream & output_stream(std::string const &output_name, std::string const description)
Definition: colvarproxy_io.cpp:408
Definition: colvarproxy.h:542
Collective variables main module.