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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
// -*- c++ -*-
// This file is part of the Collective Variables module (Colvars).
// The original version of Colvars and its updates are located at:
// https://github.com/colvars/colvars
// Please update all Colvars source files before making any changes.
// If you wish to distribute your changes, please submit them to the
// Colvars repository at GitHub.
#ifndef COLVAR_UIESTIMATOR_H
#define COLVAR_UIESTIMATOR_H
#include <cmath>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <typeinfo>
// only for colvar module!
// when integrated into other code, just remove this line and "...cvm::backup_file(...)"
#include "colvarmodule.h"
namespace UIestimator {
const int Y_SIZE = 21; // defines the range of extended CV with respect to a given CV
// For example, CV=10, width=1, Y_SIZE=21, then eCV=[0-20], having a size of 21
const int HALF_Y_SIZE = 10;
const int EXTENDED_X_SIZE = HALF_Y_SIZE;
const double EPSILON = 0.000001; // for comparison of float numbers
class n_matrix { // Stores the distribution matrix of n(x,y)
public:
n_matrix() {}
n_matrix(const std::vector<double> & lowerboundary, // lowerboundary of x
const std::vector<double> & upperboundary, // upperboundary of
const std::vector<double> & width, // width of x
const int y_size) { // size of y, for example, ysize=7, then when x=1, the distribution of y in [-2,4] is considered
int i;
this->lowerboundary = lowerboundary;
this->upperboundary = upperboundary;
this->width = width;
this->dimension = lowerboundary.size();
this->y_size = y_size; // keep in mind the internal (spare) matrix is stored in diagonal form
this->y_total_size = int(std::pow(double(y_size), double(dimension)) + EPSILON);
// the range of the matrix is [lowerboundary, upperboundary]
x_total_size = 1;
for (i = 0; i < dimension; i++) {
x_size.push_back(int((upperboundary[i] - lowerboundary[i]) / width[i] + EPSILON));
x_total_size *= x_size[i];
}
// initialize the internal matrix
matrix.reserve(x_total_size);
for (i = 0; i < x_total_size; i++) {
matrix.push_back(std::vector<int>(y_total_size, 0));
}
temp.resize(dimension);
}
int inline get_value(const std::vector<double> & x, const std::vector<double> & y) {
return matrix[convert_x(x)][convert_y(x, y)];
}
void inline set_value(const std::vector<double> & x, const std::vector<double> & y, const int value) {
matrix[convert_x(x)][convert_y(x,y)] = value;
}
void inline increase_value(const std::vector<double> & x, const std::vector<double> & y, const int value) {
matrix[convert_x(x)][convert_y(x,y)] += value;
}
private:
std::vector<double> lowerboundary;
std::vector<double> upperboundary;
std::vector<double> width;
int dimension;
std::vector<int> x_size; // the size of x in each dimension
int x_total_size; // the size of x of the internal matrix
int y_size; // the size of y in each dimension
int y_total_size; // the size of y of the internal matrix
std::vector<std::vector<int> > matrix; // the internal matrix
std::vector<int> temp; // this vector is used in convert_x and convert_y to save computational resource
int i, j;
int convert_x(const std::vector<double> & x) { // convert real x value to its interal index
for (i = 0; i < dimension; i++) {
temp[i] = int((x[i] - lowerboundary[i]) / width[i] + EPSILON);
}
int index = 0;
for (i = 0; i < dimension; i++) {
if (i + 1 < dimension) {
int x_temp = 1;
for (j = i + 1; j < dimension; j++)
x_temp *= x_size[j];
index += temp[i] * x_temp;
}
else
index += temp[i];
}
return index;
}
int convert_y(const std::vector<double> & x, const std::vector<double> & y) { // convert real y value to its interal index
int i;
for (i = 0; i < dimension; i++) {
temp[i] = round((round(y[i] / width[i] + EPSILON) - round(x[i] / width[i] + EPSILON)) + (y_size - 1) / 2 + EPSILON);
}
int index = 0;
for (i = 0; i < dimension; i++) {
if (i + 1 < dimension)
index += temp[i] * int(std::pow(double(y_size), double(dimension - i - 1)) + EPSILON);
else
index += temp[i];
}
return index;
}
double round(double r) {
return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}
};
// vector, store the sum_x, sum_x_square, count_y
template <typename T>
class n_vector {
public:
n_vector() {}
n_vector(const std::vector<double> & lowerboundary, // lowerboundary of x
const std::vector<double> & upperboundary, // upperboundary of
const std::vector<double> & width, // width of x
const int y_size, // size of y, for example, ysize=7, then when x=1, the distribution of y in [-2,4] is considered
const T & default_value) { // the default value of T
this->width = width;
this->dimension = lowerboundary.size();
x_total_size = 1;
for (int i = 0; i < dimension; i++) {
this->lowerboundary.push_back(lowerboundary[i] - (y_size - 1) / 2 * width[i] - EPSILON);
this->upperboundary.push_back(upperboundary[i] + (y_size - 1) / 2 * width[i] + EPSILON);
x_size.push_back(int((this->upperboundary[i] - this->lowerboundary[i]) / this->width[i] + EPSILON));
x_total_size *= x_size[i];
}
// initialize the internal vector
vector.resize(x_total_size, default_value);
temp.resize(dimension);
}
const T inline get_value(const std::vector<double> & x) {
return vector[convert_x(x)];
}
void inline set_value(const std::vector<double> & x, const T value) {
vector[convert_x(x)] = value;
}
void inline increase_value(const std::vector<double> & x, const T value) {
vector[convert_x(x)] += value;
}
private:
std::vector<double> lowerboundary;
std::vector<double> upperboundary;
std::vector<double> width;
int dimension;
std::vector<int> x_size; // the size of x in each dimension
int x_total_size; // the size of x of the internal matrix
std::vector<T> vector; // the internal vector
std::vector<int> temp; // this vector is used in convert_x and convert_y to save computational resource
int convert_x(const std::vector<double> & x) { // convert real x value to its interal index
int i, j;
for (i = 0; i < dimension; i++) {
temp[i] = int((x[i] - lowerboundary[i]) / width[i] + EPSILON);
}
int index = 0;
for (i = 0; i < dimension; i++) {
if (i + 1 < dimension) {
int x_temp = 1;
for (j = i + 1; j < dimension; j++)
x_temp *= x_size[j];
index += temp[i] * x_temp;
}
else
index += temp[i];
}
return index;
}
};
class UIestimator { // the implemension of UI estimator
public:
UIestimator() {}
//called when (re)start an eabf simulation
UIestimator(const std::vector<double> & lowerboundary,
const std::vector<double> & upperboundary,
const std::vector<double> & width,
const std::vector<double> & krestr, // force constant in eABF
const std::string & output_filename, // the prefix of output files
const int output_freq,
const bool restart, // whether restart from a .count and a .grad file
const std::vector<std::string> & input_filename, // the prefixes of input files
const double temperature) {
// initialize variables
this->lowerboundary = lowerboundary;
this->upperboundary = upperboundary;
this->width = width;
this->krestr = krestr;
this->output_filename = output_filename;
this->output_freq = output_freq;
this->restart = restart;
this->input_filename = input_filename;
this->temperature = temperature;
int i, j;
dimension = lowerboundary.size();
for (i = 0; i < dimension; i++) {
sum_x.push_back(n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
sum_x_square.push_back(n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
x_av.push_back(n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
sigma_square.push_back(n_vector<double>(lowerboundary, upperboundary, width, Y_SIZE, 0.0));
}
count_y = n_vector<int>(lowerboundary, upperboundary, width, Y_SIZE, 0);
distribution_x_y = n_matrix(lowerboundary, upperboundary, width, Y_SIZE);
grad = n_vector<std::vector<double> >(lowerboundary, upperboundary, width, 1, std::vector<double>(dimension, 0.0));
count = n_vector<int>(lowerboundary, upperboundary, width, 1, 0);
written = false;
written_1D = false;
if (dimension == 1) {
std::vector<double> upperboundary_temp = upperboundary;
upperboundary_temp[0] = upperboundary[0] + width[0];
oneD_pmf = n_vector<double>(lowerboundary, upperboundary_temp, width, 1, 0.0);
}
if (restart == true) {
input_grad = n_vector<std::vector<double> >(lowerboundary, upperboundary, width, 1, std::vector<double>(dimension, 0.0));
input_count = n_vector<int>(lowerboundary, upperboundary, width, 1, 0);
// initialize input_Grad and input_count
// the loop_flag is a n-dimensional vector, increae from lowerboundary to upperboundary when looping
std::vector<double> loop_flag(dimension, 0);
for (i = 0; i < dimension; i++) {
loop_flag[i] = lowerboundary[i];
}
i = 0;
while (i >= 0) {
for (j = 0; j < dimension; j++) {
input_grad.set_value(loop_flag, std::vector<double>(dimension,0));
}
input_count.set_value(loop_flag, 0);
// iterate over any dimensions
i = dimension - 1;
while (i >= 0) {
loop_flag[i] += width[i];
if (loop_flag[i] > upperboundary[i] - width[i] + EPSILON) {
loop_flag[i] = lowerboundary[i];
i--;
}
else
break;
}
}
read_inputfiles(input_filename);
}
}
~UIestimator() {}
// called from MD engine every step
bool update(const int step, std::vector<double> x, std::vector<double> y) {
int i;
if (step % output_freq == 0) {
calc_pmf();
write_files();
//write_interal_data();
}
for (i = 0; i < dimension; i++) {
// for dihedral RC, it is possible that x = 179 and y = -179, should correct it
// may have problem, need to fix
if (x[i] > 150 && y[i] < -150) {
y[i] += 360;
}
if (x[i] < -150 && y[i] > 150) {
y[i] -= 360;
}
if (x[i] < lowerboundary[i] - EXTENDED_X_SIZE * width[i] + EPSILON || x[i] > upperboundary[i] + EXTENDED_X_SIZE * width[i] - EPSILON \
|| y[i] - x[i] < -HALF_Y_SIZE * width[i] + EPSILON || y[i] - x[i] > HALF_Y_SIZE * width[i] - EPSILON \
|| y[i] - lowerboundary[i] < -HALF_Y_SIZE * width[i] + EPSILON || y[i] - upperboundary[i] > HALF_Y_SIZE * width[i] - EPSILON)
return false;
}
for (i = 0; i < dimension; i++) {
sum_x[i].increase_value(y, x[i]);
sum_x_square[i].increase_value(y, x[i] * x[i]);
}
count_y.increase_value(y, 1);
for (i = 0; i < dimension; i++) {
// adapt colvars precision
if (x[i] < lowerboundary[i] + EPSILON || x[i] > upperboundary[i] - EPSILON)
return false;
}
distribution_x_y.increase_value(x, y, 1);
return true;
}
// update the output_filename
void update_output_filename(const std::string& filename) {
output_filename = filename;
}
private:
std::vector<n_vector<double> > sum_x; // the sum of x in each y bin
std::vector<n_vector<double> > sum_x_square; // the sum of x in each y bin
n_vector<int> count_y; // the distribution of y
n_matrix distribution_x_y; // the distribution of <x, y> pair
int dimension;
std::vector<double> lowerboundary;
std::vector<double> upperboundary;
std::vector<double> width;
std::vector<double> krestr;
std::string output_filename;
int output_freq;
bool restart;
std::vector<std::string> input_filename;
double temperature;
n_vector<std::vector<double> > grad;
n_vector<int> count;
n_vector<double> oneD_pmf;
n_vector<std::vector<double> > input_grad;
n_vector<int> input_count;
// used in double integration
std::vector<n_vector<double> > x_av;
std::vector<n_vector<double> > sigma_square;
bool written;
bool written_1D;
// calculate gradients from the internal variables
void calc_pmf() {
int norm;
int i, j, k;
std::vector<double> loop_flag(dimension, 0);
for (i = 0; i < dimension; i++) {
loop_flag[i] = lowerboundary[i] - HALF_Y_SIZE * width[i];
}
i = 0;
while (i >= 0) {
norm = count_y.get_value(loop_flag) > 0 ? count_y.get_value(loop_flag) : 1;
for (j = 0; j < dimension; j++) {
x_av[j].set_value(loop_flag, sum_x[j].get_value(loop_flag) / norm);
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));
}
// iterate over any dimensions
i = dimension - 1;
while (i >= 0) {
loop_flag[i] += width[i];
if (loop_flag[i] > upperboundary[i] + HALF_Y_SIZE * width[i] - width[i] + EPSILON) {
loop_flag[i] = lowerboundary[i] - HALF_Y_SIZE * width[i];
i--;
}
else
break;
}
}
// double integration
std::vector<double> av(dimension, 0);
std::vector<double> diff_av(dimension, 0);
std::vector<double> loop_flag_x(dimension, 0);
std::vector<double> loop_flag_y(dimension, 0);
for (i = 0; i < dimension; i++) {
loop_flag_x[i] = lowerboundary[i];
loop_flag_y[i] = loop_flag_x[i] - HALF_Y_SIZE * width[i];
}
i = 0;
while (i >= 0) {
norm = 0;
for (k = 0; k < dimension; k++) {
av[k] = 0;
diff_av[k] = 0;
loop_flag_y[k] = loop_flag_x[k] - HALF_Y_SIZE * width[k];
}
int j = 0;
while (j >= 0) {
norm += distribution_x_y.get_value(loop_flag_x, loop_flag_y);
for (k = 0; k < dimension; k++) {
if (sigma_square[k].get_value(loop_flag_y) > EPSILON || sigma_square[k].get_value(loop_flag_y) < -EPSILON)
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);
diff_av[k] += distribution_x_y.get_value(loop_flag_x, loop_flag_y) * (loop_flag_x[k] - loop_flag_y[k]);
}
// iterate over any dimensions
j = dimension - 1;
while (j >= 0) {
loop_flag_y[j] += width[j];
if (loop_flag_y[j] > loop_flag_x[j] + HALF_Y_SIZE * width[j] - width[j] + EPSILON) {
loop_flag_y[j] = loop_flag_x[j] - HALF_Y_SIZE * width[j];
j--;
}
else
break;
}
}
std::vector<double> grad_temp(dimension, 0);
for (k = 0; k < dimension; k++) {
diff_av[k] /= (norm > 0 ? norm : 1);
av[k] = cvm::boltzmann() * temperature * av[k] / (norm > 0 ? norm : 1);
grad_temp[k] = av[k] - krestr[k] * diff_av[k];
}
grad.set_value(loop_flag_x, grad_temp);
count.set_value(loop_flag_x, norm);
// iterate over any dimensions
i = dimension - 1;
while (i >= 0) {
loop_flag_x[i] += width[i];
if (loop_flag_x[i] > upperboundary[i] - width[i] + EPSILON) {
loop_flag_x[i] = lowerboundary[i];
i--;
}
else
break;
}
}
}
// calculate 1D pmf
void calc_1D_pmf()
{
std::vector<double> last_position(1, 0);
std::vector<double> position(1, 0);
double min = 0;
double dG = 0;
double i;
oneD_pmf.set_value(lowerboundary, 0);
last_position = lowerboundary;
for (i = lowerboundary[0] + width[0]; i < upperboundary[0] + EPSILON; i += width[0]) {
position[0] = i + EPSILON;
if (restart == false || input_count.get_value(last_position) == 0) {
dG = oneD_pmf.get_value(last_position) + grad.get_value(last_position)[0] * width[0];
}
else {
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];
}
if (dG < min)
min = dG;
oneD_pmf.set_value(position, dG);
last_position[0] = i + EPSILON;
}
for (i = lowerboundary[0]; i < upperboundary[0] + EPSILON; i += width[0]) {
position[0] = i + EPSILON;
oneD_pmf.set_value(position, oneD_pmf.get_value(position) - min);
}
}
// write 1D pmf
void write_1D_pmf() {
std::string pmf_filename = output_filename + ".UI.pmf";
// only for colvars module!
if (written_1D) cvm::backup_file(pmf_filename.c_str());
std::ostream* ofile_pmf = cvm::proxy->output_stream(pmf_filename.c_str());
std::vector<double> position(1, 0);
for (double i = lowerboundary[0]; i < upperboundary[0] + EPSILON; i += width[0]) {
*ofile_pmf << i << " ";
position[0] = i + EPSILON;
*ofile_pmf << oneD_pmf.get_value(position) << std::endl;
}
cvm::proxy->close_output_stream(pmf_filename.c_str());
written_1D = true;
}
// write heads of the output files
void writehead(std::ostream& os) const {
os << "# " << dimension << std::endl;
for (int i = 0; i < dimension; i++) {
os << "# " << lowerboundary[i] << " " << width[i] << " " << int((upperboundary[i] - lowerboundary[i]) / width[i] + EPSILON) << " " << 0 << std::endl;
}
os << std::endl;
}
// write interal data, used for testing
void write_interal_data() {
std::string internal_filename = output_filename + ".UI.internal";
std::ostream* ofile_internal = cvm::proxy->output_stream(internal_filename.c_str());
std::vector<double> loop_flag(dimension, 0);
for (int i = 0; i < dimension; i++) {
loop_flag[i] = lowerboundary[i];
}
int n = 0;
while (n >= 0) {
for (int j = 0; j < dimension; j++) {
*ofile_internal << loop_flag[j] + 0.5 * width[j] << " ";
}
for (int k = 0; k < dimension; k++) {
*ofile_internal << grad.get_value(loop_flag)[k] << " ";
}
std::vector<double> ii(dimension,0);
for (double i = loop_flag[0] - 10; i < loop_flag[0] + 10 + EPSILON; i+= width[0]) {
for (double j = loop_flag[1] - 10; j< loop_flag[1] + 10 + EPSILON; j+=width[1]) {
ii[0] = i;
ii[1] = j;
*ofile_internal << i <<" "<<j<<" "<< distribution_x_y.get_value(loop_flag,ii)<< " ";
}
}
*ofile_internal << std::endl;
// iterate over any dimensions
n = dimension - 1;
while (n >= 0) {
loop_flag[n] += width[n];
if (loop_flag[n] > upperboundary[n] - width[n] + EPSILON) {
loop_flag[n] = lowerboundary[n];
n--;
}
else
break;
}
}
cvm::proxy->close_output_stream(internal_filename.c_str());
}
// write output files
void write_files() {
std::string grad_filename = output_filename + ".UI.grad";
std::string hist_filename = output_filename + ".UI.hist.grad";
std::string count_filename = output_filename + ".UI.count";
int i, j;
//
// only for colvars module!
if (written) cvm::backup_file(grad_filename.c_str());
//if (written) cvm::backup_file(hist_filename.c_str());
if (written) cvm::backup_file(count_filename.c_str());
std::ostream* ofile = cvm::proxy->output_stream(grad_filename.c_str());
std::ostream* ofile_hist = cvm::proxy->output_stream(hist_filename.c_str(), std::ios::app);
std::ostream* ofile_count = cvm::proxy->output_stream(count_filename.c_str());
writehead(*ofile);
writehead(*ofile_hist);
writehead(*ofile_count);
if (dimension == 1) {
calc_1D_pmf();
write_1D_pmf();
}
std::vector<double> loop_flag(dimension, 0);
for (i = 0; i < dimension; i++) {
loop_flag[i] = lowerboundary[i];
}
i = 0;
while (i >= 0) {
for (j = 0; j < dimension; j++) {
*ofile << loop_flag[j] + 0.5 * width[j] << " ";
*ofile_hist << loop_flag[j] + 0.5 * width[j] << " ";
*ofile_count << loop_flag[j] + 0.5 * width[j] << " ";
}
if (restart == false) {
for (j = 0; j < dimension; j++) {
*ofile << grad.get_value(loop_flag)[j] << " ";
*ofile_hist << grad.get_value(loop_flag)[j] << " ";
}
*ofile << std::endl;
*ofile_hist << std::endl;
*ofile_count << count.get_value(loop_flag) << " " <<std::endl;
}
else {
double final_grad = 0;
for (j = 0; j < dimension; j++) {
int total_count_temp = (count.get_value(loop_flag) + input_count.get_value(loop_flag));
if (input_count.get_value(loop_flag) == 0)
final_grad = grad.get_value(loop_flag)[j];
else
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);
*ofile << final_grad << " ";
*ofile_hist << final_grad << " ";
}
*ofile << std::endl;
*ofile_hist << std::endl;
*ofile_count << (count.get_value(loop_flag) + input_count.get_value(loop_flag)) << " " <<std::endl;
}
// iterate over any dimensions
i = dimension - 1;
while (i >= 0) {
loop_flag[i] += width[i];
if (loop_flag[i] > upperboundary[i] - width[i] + EPSILON) {
loop_flag[i] = lowerboundary[i];
i--;
*ofile << std::endl;
*ofile_hist << std::endl;
*ofile_count << std::endl;
}
else
break;
}
}
cvm::proxy->close_output_stream(grad_filename.c_str());
cvm::proxy->close_output_stream(hist_filename.c_str());
cvm::proxy->close_output_stream(count_filename.c_str());
written = true;
}
// read input files
void read_inputfiles(const std::vector<std::string> input_filename)
{
char sharp;
double nothing;
int dimension_temp;
int i, j, k, l, m;
std::vector<double> loop_bin_size(dimension, 0);
std::vector<double> position_temp(dimension, 0);
std::vector<double> grad_temp(dimension, 0);
int count_temp = 0;
for (i = 0; i < int(input_filename.size()); i++) {
int size = 1 , size_temp = 0;
std::string count_filename = input_filename[i] + ".UI.count";
std::string grad_filename = input_filename[i] + ".UI.grad";
std::ifstream count_file(count_filename.c_str(), std::ios::in);
std::ifstream grad_file(grad_filename.c_str(), std::ios::in);
count_file >> sharp >> dimension_temp;
grad_file >> sharp >> dimension_temp;
for (j = 0; j < dimension; j++) {
count_file >> sharp >> nothing >> nothing >> size_temp >> nothing;
grad_file >> sharp >> nothing >> nothing >> nothing >> nothing;
size *= size_temp;
}
for (j = 0; j < size; j++) {
do {
for (k = 0; k < dimension; k++) {
count_file >> position_temp[k];
grad_file >> nothing;
}
for (l = 0; l < dimension; l++) {
grad_file >> grad_temp[l];
}
count_file >> count_temp;
}
while (position_temp[i] < lowerboundary[i] - EPSILON || position_temp[i] > upperboundary[i] + EPSILON);
if (count_temp == 0) {
continue;
}
for (m = 0; m < dimension; m++) {
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));
}
input_grad.set_value(position_temp, grad_temp);
input_count.increase_value(position_temp, count_temp);
}
count_file.close();
grad_file.close();
}
}
};
}
#endif
|