18#include "colvarvalue.h"
71 std::vector<colvar *>
cv;
77 inline size_t address(std::vector<int>
const &ix)
const
80 for (
size_t i = 0; i <
nd; i++) {
81 addr += ix[i]*
static_cast<size_t>(
nxc[i]);
83 if (ix[i] >=
nx[i] || ix[i] < 0) {
85 +
") exceeds bounds in colvar_grid.\n", COLVARS_BUG_ERROR);
95 for (
int dim =
nd - 1; dim >= 0; --dim) {
141 inline std::vector<int>
const &
sizes()
const
147 inline void set_sizes(std::vector<int>
const &new_sizes)
168 int setup(std::vector<int>
const &nx_i,
170 size_t const &mult_i = 1)
173 cvmodule->
log(
"Allocating grid: multiplicity = "+
cvm::to_str(mult_i)+
188 for (
int i =
nd-1; i >= 0; i--) {
192 return COLVARS_ERROR;
211 return setup(this->
nx, T(), this->mult);
261 this->
setup(nx_i, t, mult_i);
270 bool add_extra_bin =
false,
271 std::shared_ptr<const colvar_grid_params> params =
nullptr,
272 std::string config = std::string())
276 this->init_from_colvars(colvars, mult_i, add_extra_bin, params, config);
282 colvar_grid(std::string
const &filename,
size_t mult_i = 1);
284 int init_from_colvars(std::vector<colvar *>
const &colvars,
286 bool add_extra_bin =
false,
287 std::shared_ptr<const colvar_grid_params> params =
nullptr,
288 std::string config = std::string())
291 cvmodule->
log(
"Reading grid configuration from collective variables.\n");
301 cvmodule->
log(
"Allocating a grid for "+
cvm::to_str(colvars.size())+
302 " collective variables, multiplicity = "+
cvm::to_str(mult_i)+
".\n");
305 for (i = 0; i <
nd; i++) {
308 "constructed for scalar variables. "
309 "ABF and histogram can not be used; metadynamics "
310 "can be used with useGrids disabled.\n", COLVARS_INPUT_ERROR);
311 return COLVARS_ERROR;
314 if (
cv[i]->width <= 0.0) {
316 "variable with negative or zero width.\n", COLVARS_INPUT_ERROR);
317 return COLVARS_ERROR;
330 if (i > 0 &&
cv[i-1] ==
cv[i]) {
340 if (!config.empty()) {
345 cvm::error_static(
"Error: init_from_colvars was passed both a grid config and a template grid.", COLVARS_BUG_ERROR);
346 return COLVARS_BUG_ERROR;
351 if (params->nd !=
nd) {
352 cvm::error_static(
"Trying to initialize grid from template with wrong dimension (" +
355 return COLVARS_ERROR;
364 for (i = 0; i <
nd; i++) {
383 this->init_from_boundaries();
384 return this->
setup();
387 int init_from_boundaries()
390 cvmodule->
log(
"Configuring grid dimensions from colvars boundaries.\n");
405 int nbins_round = (int)(nbins+0.5);
408 cvmodule->
log(
"Warning: grid interval("+
411 ") is not commensurate to its bin width("+
414 (nbins_round *
widths[i]);
418 cvmodule->
log(
"Number of points is "+
cvm::to_str((
int) nbins_round)+
421 nx.push_back(nbins_round);
429 inline void wrap(std::vector<int> & ix)
const
431 for (
size_t i = 0; i <
nd; i++) {
433 ix[i] = (ix[i] +
nx[i]) %
nx[i];
435 if (ix[i] < 0 || ix[i] >=
nx[i]) {
436 cvm::error_static(
"Trying to wrap illegal index vector (non-PBC) for a grid point: "
449 for (
size_t i = 0; i <
nd; i++) {
451 ix[i] = (ix[i] +
nx[i]) %
nx[i];
452 }
else if (ix[i] < 0 || ix[i] >=
nx[i]) {
461 inline bool wrap_to_edge(std::vector<int> & ix, std::vector<int> & edge_bin)
const
465 for (
size_t i = 0; i <
nd; i++) {
467 ix[i] = (ix[i] +
nx[i]) %
nx[i];
469 }
else if (ix[i] < 0) {
472 }
else if (ix[i] >=
nx[i]) {
474 edge_bin[i] =
nx[i] - 1;
492 for (
size_t i = 0; i <
nd; i++) {
509 cv[i]->actual_value().vector1d_value[iv] :
510 cv[i]->
value().vector1d_value[iv], i);
542 if (bin_index < 0) bin_index=0;
543 if (bin_index >=
int(
nx[i])) bin_index=int(
nx[i])-1;
544 return (
int) bin_index;
567 return new_offset.
real_value + new_width * (0.5 + i_bin);
573 size_t const &imult = 0)
599 if (other_grid.
multiplicity() != this->multiplicity()) {
601 "different multiplicity.\n");
605 if (other_grid.
data.size() != this->data.size()) {
607 "different size.\n");
611 for (
size_t i = 0; i <
data.size(); i++) {
623 if (other_grid.
multiplicity() != this->multiplicity()) {
625 "different multiplicity.\n");
629 if (other_grid.
data.size() != this->data.size()) {
631 "different size.\n");
636 for (
size_t i = 0; i <
data.size(); i++) {
646 for (
size_t i = 0; i <
data.size(); i++) out_data[i] =
data[i];
655 for (
size_t i = 0; i <
data.size(); i++)
data[i] = in_data[i];
669 inline T
const &
value(std::vector<int>
const &ix,
670 size_t const &imult = 0)
const
676 inline T
const &
value(
size_t i)
const
684 for (
size_t i = 0; i <
nt; i++)
692 for (
size_t i = 0; i <
nt; i++)
699 for (
size_t i = 0; i <
nt; i++)
709 for (
size_t i = 0; i <
nd; i++) {
720 for (
size_t i = 0; i <
nd; i++) {
731 for (
size_t i = 0; i <
nd; i++) {
741 bool skip_hard_boundaries =
false)
744 for (
size_t i = 0; i <
nd; i++) {
772 if (other_grid.
multiplicity() != this->multiplicity()) {
774 "different multiplicity.\n");
779 std::vector<cvm::real>
const &gw = this->
widths;
781 std::vector<cvm::real>
const &ogw = other_grid.
widths;
784 std::vector<int> oix = other_grid.
new_index();
787 cvmodule->
log(
"Remapping grid...\n");
790 for (
size_t i = 0; i <
nd; i++) {
801 for (
size_t im = 0; im <
mult; im++) {
808 cvmodule->
log(
"Remapping done.\n");
816 if (other_grid.
multiplicity() != this->multiplicity()) {
818 "different multiplicity.\n");
821 if (scale_factor != 1.0)
822 for (
size_t i = 0; i <
data.size(); i++) {
823 data[i] +=
static_cast<T
>(scale_factor * other_grid.
data[i]);
827 for (
size_t i = 0; i <
data.size(); i++) {
836 size_t const &imult = 0)
const
838 return value(ix, imult);
846 size_t const &imult = 0,
867 return std::vector<int> (
nd, 0);
872 inline bool index_ok(std::vector<int>
const &ix)
const
874 for (
size_t i = 0; i <
nd; i++) {
875 if ( (ix[i] < 0) || (ix[i] >=
int(
nx[i])) )
883 inline void incr(std::vector<int> &ix)
const
885 for (
int i = ix.size()-1; i >= 0; i--) {
889 if (ix[i] >=
nx[i]) {
919 for (
size_t i = 0; i <
nd; i++) {
925 widths[i])) > 1.0E-10) ) {
927 "inconsistent with that of its colvars.\n");
938 for (
size_t i = 0; i <
nd; i++) {
948 (
data.size() != other_grid.
data.size()) ) {
950 "two grids that are supposed to be equal, "
951 "aside from the data stored.\n");
970 std::istream &
read_raw(std::istream &is);
988 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
996 std::istream &
read_multicol(std::istream &is,
bool add =
false);
1000 std::string description =
"grid file",
1008 std::string description =
"grid file")
const;
1015 std::string description =
"grid file")
const;
1035 std::shared_ptr<const colvar_grid_params> params =
nullptr);
1038 std::string config);
1076 std::istream &
read_raw(std::istream &is);
1084 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
1092 std::istream &
read_multicol(std::istream &is,
bool add =
false);
1096 std::string description =
"grid file",
1104 std::string description =
"grid file")
const;
1111 std::string description =
"grid file")
const;
1116 size_t const &imult = 0,
1122 if (this->has_parent_data) {
1139 for (
size_t i = 0; i <
nd; i++) {
1158 for (i = -radius; i <= radius; i++) {
1168 for (i = -radius; i <= radius; i++) {
1170 for (j = -radius; j <= radius; j++) {
1181 for (i = -radius; i <= radius; i++) {
1183 for (j = -radius; j <= radius; j++) {
1185 for (k = -radius; k <= radius; k++) {
1197 cvm::error_static(
"Error: local_sample_count is not implemented for grids of dimension > 3", COLVARS_NOT_IMPLEMENTED);
1203 return count / nbins;
1213 int n = 0,
int offset = 0)
1216 std::vector<int> ix = ix0;
1221 Am1 =
value(ix) + offset;
1223 A0 =
value(ix) + offset;
1225 A1 =
value(ix) + offset;
1226 if (Am1 * A0 * A1 == 0) {
1229 return (cvmodule->
logn(A1) - cvmodule->
logn(Am1))
1232 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1234 Am1 =
value(ix) + offset;
1236 A0 =
value(ix) + offset;
1238 A1 =
value(ix) + offset;
1239 if (Am1 * A0 * A1 == 0) {
1242 return (cvmodule->
logn(A1) - cvmodule->
logn(Am1))
1247 int increment = (ix[n] == 0 ? 1 : -1);
1249 A0 =
value(ix) + offset;
1250 ix[n] += increment; A1 =
value(ix) + offset;
1251 ix[n] += increment; A2 =
value(ix) + offset;
1252 if (A0 * A1 * A2 == 0) {
1255 return (-1.5 * cvmodule->
logn(A0) + 2. * cvmodule->
logn(A1)
1256 - 0.5 * cvmodule->
logn(A2)) * increment /
widths[n];
1269 std::vector<int> ix = ix0;
1279 if (Am1 * A0 * A1 == 0) {
1282 return (A1 - Am1) / (
widths[n] * 2.);
1284 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1291 if (Am1 * A0 * A1 == 0) {
1294 return (A1 - Am1) / (
widths[n] * 2.);
1298 int increment = (ix[n] == 0 ? 1 : -1);
1301 ix[n] += increment; A1 =
value(ix);
1302 ix[n] += increment; A2 =
value(ix);
1303 return (-1.5 * A0 + 2. * A1
1304 - 0.5 * A2) * increment /
widths[n];
1330 std::shared_ptr<const colvar_grid_params> params =
nullptr,
1331 bool add_extra_bin =
false,
1332 std::string config = std::string());
1340 size_t const &imult = 0)
1370 std::istream &
read_raw(std::istream &is);
1378 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
1386 std::istream &
read_multicol(std::istream &is,
bool add =
false);
1390 std::string description =
"grid file",
1398 std::string description =
"grid file")
const;
1405 std::string description =
"grid file")
const;
1414 std::vector<int> ix;
1418 for (n = 0; n < 2; n++) {
1423 ix[1-n]++;
wrap(ix);
1427 grad[n] = 0.5 * (A1 - A0) /
widths[n];
1429 }
else if (
nd == 3) {
1434 for (i = 0; i<2; i++) {
1436 for (j = 0; j<2; j++) {
1438 for (k = 0; k<2; k++) {
1440 p[index++] =
value(ix);
1450 grad[0] = 0.25 * ((p[4] + p[5] + p[6] + p[7]) - (p[0] + p[1] + p[2] + p[3])) /
widths[0];
1452 grad[1] = 0.25 * ((p[2] + p[3] + p[6] + p[7]) - (p[0] + p[1] + p[4] + p[5])) /
widths[1];
1454 grad[2] = 0.25 * ((p[1] + p[3] + p[5] + p[7]) - (p[0] + p[2] + p[4] + p[6])) /
widths[2];
1465 int n = 0,
int offset = 0)
1468 std::vector<int> ix = ix0;
1473 A0 =
value(ix) + offset;
1476 A1 =
value(ix) + offset;
1480 return (cvmodule->
logn(A1) - cvmodule->
logn(A0))
1483 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1485 A0 =
value(ix) + offset;
1488 A1 =
value(ix) + offset;
1492 return (cvmodule->
logn(A1) - cvmodule->
logn(A0))
1497 int increment = (ix[n] == 0 ? 1 : -1);
1499 A0 =
value(ix) + offset;
1500 ix[n] += increment; A1 =
value(ix) + offset;
1501 ix[n] += increment; A2 =
value(ix) + offset;
1502 if (A0 * A1 * A2 == 0) {
1505 return (-1.5 * cvmodule->
logn(A0) + 2. * cvmodule->
logn(A1)
1506 - 0.5 * cvmodule->
logn(A2)) * increment /
widths[n];
1519 std::vector<int> ix = ix0;
1529 if (Am1 * A0 * A1 == 0) {
1532 return (A1 - Am1) / (
widths[n] * 2.);
1534 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1541 if (Am1 * A0 * A1 == 0) {
1544 return (A1 - Am1) / (
widths[n] * 2.);
1548 int increment = (ix[n] == 0 ? 1 : -1);
1551 ix[n] += increment; A1 =
value(ix);
1552 ix[n] += increment; A2 =
value(ix);
1553 return (-1.5 * A0 + 2. * A1
1554 - 0.5 * A2) * increment /
widths[n];
1562 size_t const &imult = 0)
const override
1567 "larger than 1 in a scalar data grid.\n");
1571 return ( (s =
samples->value(ix)) > 0) ?
1582 size_t const &imult = 0,
1583 bool add =
false)
override
1587 "larger than 1 in a scalar data grid.\n");
1652 colvar_grid_gradient(std::string
const &filename, std::shared_ptr<colvar_grid_count> samples_in =
nullptr);
1656 std::shared_ptr<colvar_grid_count> samples_in =
nullptr,
1657 std::shared_ptr<const colvar_grid_params> params =
nullptr,
1658 std::string config = std::string());
1684 std::istream &
read_raw(std::istream &is);
1692 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
1700 std::istream &
read_multicol(std::istream &is,
bool add =
false);
1704 std::string description =
"grid file",
1712 std::string description =
"grid file")
const;
1719 std::string description =
"grid file")
const;
1722 inline void vector_value(std::vector<int>
const &ix, std::vector<cvm::real> &v)
const
1726 int count =
samples->value(ix);
1729 for (
size_t i = 0; i <
mult; i++) {
1730 v[i] = invcount * p[i];
1733 for (
size_t i = 0; i <
mult; i++) {
1738 for (
size_t i = 0; i <
mult; i++) {
1746 inline void acc_value(std::vector<int>
const &ix, std::vector<colvarvalue>
const &values) {
1747 for (
size_t imult = 0; imult <
mult; imult++) {
1748 data[
address(ix) + imult] += values[imult].real_value;
1757 for (
size_t imult = 0; imult <
mult; imult++) {
1767 size_t const &imult = 0)
const override
1771 return ( (s =
samples->value(ix)) > 0) ?
1784 if ( weight <= min_samples ) {
1789 fact = 1.0 / weight;
1812 fact = weight > 0. ? 1. / weight : 0.;
1835 fact = weight > 0. ? 1. / weight : 0.;
1839 for (
size_t imult = 0; imult <
mult; imult++) {
1840 grad[imult] = fact * p[imult];
1849 size_t const &imult = 0,
1850 bool add =
false)
override
1870 if (
nd != 1 ||
nx[0] == 0) {
Colvar_grid derived class to hold counters in discrete n-dim colvar space.
Definition: colvargrid.h:1023
colvar_grid_count()
Default constructor.
int local_sample_count(int radius)
Return the average number of samples in a given "radius" around current bin Really a hypercube of len...
Definition: colvargrid.h:1134
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:64
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid.cpp:33
cvm::real gradient_finite_diff(const std::vector< int > &ix0, int n=0)
Return the gradient of discrete count from finite differences on the same grid for dimension n (colva...
Definition: colvargrid.h:1265
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid.cpp:54
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid.cpp:108
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid.cpp:74
int parse_params(std::string const &conf, colvarparse::Parse_Mode const parse_mode=colvarparse::parse_normal)
Read new grid parameters from a string.
Definition: colvargrid.cpp:38
virtual void value_input(std::vector< int > const &ix, size_t const &t, size_t const &imult=0, bool add=false)
Enter or add a value, but also handle parent grid.
Definition: colvargrid.h:1114
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:44
std::istream & read_multicol(std::istream &is, bool add=false)
Read a grid written by write_multicol(), incrementin if data is true.
Definition: colvargrid.cpp:85
std::ostream & write_multicol(std::ostream &os) const
Write grid in a format which is both human-readable and gnuplot-friendly.
Definition: colvargrid.cpp:97
cvm::real log_gradient_finite_diff(const std::vector< int > &ix0, int n=0, int offset=0)
Return the log-gradient from finite differences on the same grid for dimension n Order-1 centered dif...
Definition: colvargrid.h:1212
size_t const & new_value(std::vector< int > const &ix)
Get the binned count indexed by ix from the newly read data.
Definition: colvargrid.h:1051
virtual ~colvar_grid_count()
Destructor.
Definition: colvargrid.h:1030
void incr_count(std::vector< int > const &ix)
Increment the counter at given position.
Definition: colvargrid.h:1045
Class for accumulating the gradient of a scalar function on a grid.
Definition: colvargrid.h:1630
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid.cpp:361
cvm::real smooth_inverse_weight(cvm::real weight)
Definition: colvargrid.h:1781
void vector_value_smoothed(std::vector< int > const &ix, cvm::real *grad, bool smoothed=true)
Obtain the vector value of the function at ix divided by its number of samples (if the count grid is ...
Definition: colvargrid.h:1822
virtual cvm::real value_output_smoothed(std::vector< int > const &ix, bool smoothed=true)
Return the scalar value of the function at ix divided by its number of samples (if the count grid is ...
Definition: colvargrid.h:1799
void acc_value(std::vector< int > const &ix, std::vector< colvarvalue > const &values)
Accumulate the value.
Definition: colvargrid.h:1746
cvm::real average(bool smoothed=false)
Compute and return average value for a 1D gradient grid.
Definition: colvargrid.h:1868
std::shared_ptr< colvar_grid_count > samples
Provide the sample count by which each binned value should be divided.
Definition: colvargrid.h:1635
std::istream & read_multicol(std::istream &is, bool add=false)
Read a grid written by write_multicol(), incrementin if data is true.
Definition: colvargrid.cpp:413
colvar_grid_gradient()
Default constructor.
Definition: colvargrid.cpp:331
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid.cpp:382
void write_1D_integral(std::ostream &os)
If the grid is 1-dimensional, integrate it and write the integral to a file (DEPRECATED by the colvar...
Definition: colvargrid.cpp:448
int parse_params(std::string const &conf, colvarparse::Parse_Mode const parse_mode=colvarparse::parse_normal)
Read new grid parameters from a string.
Definition: colvargrid.cpp:366
virtual cvm::real value_output(std::vector< int > const &ix, size_t const &imult=0) const override
Return the value of the function at ix divided by its number of samples (if the count grid is defined...
Definition: colvargrid.h:1766
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:392
virtual ~colvar_grid_gradient()
Destructor.
Definition: colvargrid.h:1641
virtual void value_input(std::vector< int > const &ix, cvm::real const &new_value, size_t const &imult=0, bool add=false) override
Get the value from a formatted output and transform it into the internal representation (it may have ...
Definition: colvargrid.h:1847
void acc_force(std::vector< int > const &ix, cvm::real const *forces)
Accumulate the gradient based on the force (i.e. sums the opposite of the force)
Definition: colvargrid.h:1756
int full_samples
Parameters for smoothing data with low sampling.
Definition: colvargrid.h:1661
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid.cpp:402
std::ostream & write_multicol(std::ostream &os) const
Write grid in a format which is both human-readable and gnuplot-friendly.
Definition: colvargrid.cpp:425
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:372
cvm::real grid_rmsd(colvar_grid_gradient const &other_grid) const
Return the RMSD between this grid's data and another one Grids must have the same dimensions.
Definition: colvargrid.cpp:502
void vector_value(std::vector< int > const &ix, std::vector< cvm::real > &v) const
Get a vector with the binned value(s) indexed by ix, normalized if applicable.
Definition: colvargrid.h:1722
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid.cpp:436
Unified base class for grid of values of a function of several collective variables.
Definition: colvargrid.h:24
size_t nd
Number of dimensions.
Definition: colvargrid.h:28
std::vector< int > nxc
Cumulative number of points along each dimension.
Definition: colvargrid.h:34
std::vector< colvarvalue > lower_boundaries
Lower boundaries of the colvars in this grid.
Definition: colvargrid.h:37
std::vector< cvm::real > widths
Widths of the colvars in this grid.
Definition: colvargrid.h:43
std::vector< colvarvalue > upper_boundaries
Upper boundaries of the colvars in this grid.
Definition: colvargrid.h:40
std::vector< int > nx
Number of points along each dimension.
Definition: colvargrid.h:31
Class for accumulating a scalar function on a grid.
Definition: colvargrid.h:1312
void acc_value(std::vector< int > const &ix, cvm::real const &new_value, size_t const &imult=0)
Accumulate the value.
Definition: colvargrid.h:1338
cvm::real minimum_value() const
Return the lowest value.
Definition: colvargrid.cpp:245
cvm::real gradient_finite_diff(const std::vector< int > &ix0, int n=0)
Return the gradient of discrete count from finite differences on the same grid for dimension n (colva...
Definition: colvargrid.h:1515
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:159
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid.cpp:148
virtual ~colvar_grid_scalar()
Destructor.
Definition: colvargrid.cpp:144
cvm::real minimum_pos_value() const
Return the lowest positive value.
Definition: colvargrid.cpp:254
virtual cvm::real value_output(std::vector< int > const &ix, size_t const &imult=0) const override
Return the value of the function at ix divided by its number of samples (if the count grid is defined...
Definition: colvargrid.h:1561
cvm::real grid_rmsd(colvar_grid_scalar const &other_grid) const
Return the RMSD between this grid's data and another one Grids must have the same dimensions.
Definition: colvargrid.cpp:301
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid.cpp:223
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid.cpp:169
std::ostream & write_multicol(std::ostream &os) const
Write grid in a format which is both human-readable and gnuplot-friendly.
Definition: colvargrid.cpp:212
std::istream & read_multicol(std::istream &is, bool add=false)
Read a grid written by write_multicol(), incrementin if data is true.
Definition: colvargrid.cpp:200
cvm::real integral() const
Calculates the integral of the map (uses widths if they are defined)
Definition: colvargrid.cpp:270
int parse_params(std::string const &conf, colvarparse::Parse_Mode const parse_mode=colvarparse::parse_normal)
Read new grid parameters from a string.
Definition: colvargrid.cpp:153
std::shared_ptr< colvar_grid_count > samples
Provide the associated sample count by which each binned value should be divided.
Definition: colvargrid.h:1317
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid.cpp:189
virtual void value_input(std::vector< int > const &ix, cvm::real const &new_value, size_t const &imult=0, bool add=false) override
Enter or add value but also deal with count grid.
Definition: colvargrid.h:1580
cvm::real entropy() const
Assuming that the map is a normalized probability density, calculates the entropy (uses widths if the...
Definition: colvargrid.cpp:284
colvar_grid_scalar()
Default constructor.
Definition: colvargrid.cpp:121
cvm::real maximum_value() const
Return the highest value.
Definition: colvargrid.cpp:235
cvm::real log_gradient_finite_diff(const std::vector< int > &ix0, int n=0, int offset=0)
Return the log-gradient from finite differences on the same grid for dimension n (colvar_grid_scalar)
Definition: colvargrid.h:1464
void vector_gradient_finite_diff(const std::vector< int > &ix0, std::vector< cvm::real > &grad)
Return the gradient of the scalar field from finite differences Input coordinates are those of gradie...
Definition: colvargrid.h:1411
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:179
Grid of values of a function of several collective variables.
Definition: colvargrid.h:52
int setup()
Allocate data (allow initialization also after construction)
Definition: colvargrid.h:209
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:102
void set_value(size_t i, T const &t)
Set the value at the point with linear address i (for speed)
Definition: colvargrid.h:580
void check_consistency()
Check that the grid information inside (boundaries, widths, ...) is consistent with the current setti...
Definition: colvargrid.h:917
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid_def.h:203
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid_def.h:170
int current_bin_scalar_bound(int const i) const
Report the bin corresponding to the current value of variable i and assign first or last bin if out o...
Definition: colvargrid.h:500
std::vector< int > const new_index() const
Get the index corresponding to the "first" bin, to be used as the initial value for an index in loopi...
Definition: colvargrid.h:865
void add_constant(T const &t)
Add a constant to all elements (fast loop)
Definition: colvargrid.h:682
std::vector< size_t > new_data
Newly read data (used for count grids, when adding several grids read from disk)
Definition: colvargrid.h:68
cvm::real value_to_bin_scalar_fraction(colvarvalue const &value, const int i) const
Use the lower boundary and the width to report the fraction of bin beyond value_to_bin_scalar() that ...
Definition: colvargrid.h:528
void incr(std::vector< int > &ix) const
Increment the index, in a way that will make it loop over the whole nd-dimensional array.
Definition: colvargrid.h:883
size_t number_of_points(int const icv=-1) const
Definition: colvargrid.h:131
std::vector< bool > hard_upper_boundaries
Whether some colvars have hard upper boundaries.
Definition: colvargrid.h:109
virtual void value_input(std::vector< int > const &ix, T const &t, size_t const &imult=0, bool add=false)
Get the value from a formatted output and transform it into the internal representation (the two may ...
Definition: colvargrid.h:844
T const & value(size_t i) const
Get the binned value indexed by linear address i.
Definition: colvargrid.h:676
std::vector< int > const get_colvars_index_bound() const
Get the bin indices corresponding to the provided values of the colvars and assign first or last bin ...
Definition: colvargrid.h:728
int current_bin_flat_bound() const
Report the flattened bin address corresponding to the current value of all variables and assign first...
Definition: colvargrid.h:489
size_t mult
Multiplicity of each datum (allow the binning of non-scalar types such as atomic gradients)
Definition: colvargrid.h:59
size_t multiplicity() const
Return the multiplicity of the type used.
Definition: colvargrid.h:153
std::vector< int > const & sizes() const
Get the sizes in each direction.
Definition: colvargrid.h:141
void set_value(std::vector< int > const &ix, T const &t, size_t const &imult=0)
Set the value at the point with index ix.
Definition: colvargrid.h:571
colvarvalue bin_to_value_scalar(int const &i_bin, int const i) const
Use the two boundaries and the width to report the central value corresponding to a bin index.
Definition: colvargrid.h:557
std::vector< T > data
Low-level array of values.
Definition: colvargrid.h:65
void multiply_constant(cvm::real const &a)
Multiply all elements by a scalar constant (fast loop)
Definition: colvargrid.h:690
std::vector< int > const & number_of_points_vec() const
Return the numbers of points in all dimensions.
Definition: colvargrid.h:124
bool index_ok(std::vector< int > const &ix) const
Check that the index is within range in each of the dimensions.
Definition: colvargrid.h:872
cvm::real current_bin_scalar_fraction(int const i) const
Report the fraction of bin beyond current_bin_scalar()
Definition: colvargrid.h:521
bool has_data
Whether this grid has been filled with data or is still empty.
Definition: colvargrid.h:115
void add_grid(colvar_grid< T > const &other_grid, cvm::real scale_factor=1.0)
Add data from another grid of the same type, AND identical definition (boundaries,...
Definition: colvargrid.h:813
std::vector< int > const get_colvars_index(std::vector< colvarvalue > const &values) const
Get the bin indices corresponding to the provided values of the colvars.
Definition: colvargrid.h:706
void check_consistency(colvar_grid< T > const &other_grid)
Check that the grid information inside (boundaries, widths, ...) is consistent with that of another g...
Definition: colvargrid.h:936
colvar_grid(std::vector< int > const &nx_i, T const &t=T(), size_t mult_i=1)
Constructor from explicit grid sizes.
Definition: colvargrid.h:256
int parse_params(std::string const &conf, colvarparse::Parse_Mode const parse_mode=colvarparse::parse_normal)
Read new grid parameters from a string.
Definition: colvargrid_def.h:233
size_t nt
Total number of grid points.
Definition: colvargrid.h:62
T get_value(size_t i) const
Get the value at the point with linear address i (for speed)
Definition: colvargrid.h:586
void reset(T const &t=T())
Reset data (in case the grid is being reused)
Definition: colvargrid.h:215
size_t num_variables() const
Return the number of colvar objects.
Definition: colvargrid.h:118
int setup(std::vector< int > const &nx_i, T const &t=T(), size_t const &mult_i=1)
Allocate data.
Definition: colvargrid.h:168
IST & read_restart_template_(IST &is)
Helper method for read_restart.
Definition: colvargrid_def.h:84
colvar_grid(std::vector< colvar * > const &colvars, T const &t=T(), size_t mult_i=1, bool add_extra_bin=false, std::shared_ptr< const colvar_grid_params > params=nullptr, std::string config=std::string())
Constructor from a vector of colvars or an optional grid config string.
Definition: colvargrid.h:267
colvar_grid(colvar_grid< T > const &g)
"Almost copy-constructor": only copies configuration parameters from another grid,...
Definition: colvargrid.h:239
bool has_parent_data
True if this is a count grid related to another grid of data.
Definition: colvargrid.h:112
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid_def.h:498
bool wrap_to_edge(std::vector< int > &ix, std::vector< int > &edge_bin) const
Definition: colvargrid.h:461
std::vector< int > const get_colvars_index() const
Get the bin indices corresponding to the current values of the colvars.
Definition: colvargrid.h:717
int current_bin_scalar(int const i, int const iv) const
Report the bin corresponding to the current value of item iv in variable i.
Definition: colvargrid.h:506
void delta_grid(colvar_grid< T > const &other_grid)
Get the change from this to other_grid and store the result in this. this_grid := other_grid - this_g...
Definition: colvargrid.h:596
void request_actual_value(bool b=true)
Request grid to use actual values of extended coords.
Definition: colvargrid.h:159
cvm::real bin_distance_from_boundaries(std::vector< colvarvalue > const &values, bool skip_hard_boundaries=false)
Get the minimal distance (in number of bins) from the boundaries; a negative number is returned if th...
Definition: colvargrid.h:740
int value_to_bin_scalar_bound(colvarvalue const &value, const int i) const
Use the lower boundary and the width to report which bin the provided value is in and assign first or...
Definition: colvargrid.h:536
int current_bin_scalar(int const i) const
Report the bin corresponding to the current value of variable i.
Definition: colvargrid.h:482
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:157
std::vector< bool > hard_lower_boundaries
Whether some colvars have hard lower boundaries.
Definition: colvargrid.h:106
T const & value(std::vector< int > const &ix, size_t const &imult=0) const
Get the binned value indexed by ix, or the first of them if the multiplicity is larger than 1.
Definition: colvargrid.h:669
std::vector< bool > use_actual_value
Do we request actual value (for extended-system colvars)?
Definition: colvargrid.h:74
size_t raw_data_num() const
Size of the data as they are represented in memory.
Definition: colvargrid.h:664
size_t address(std::vector< int > const &ix) const
Get the low-level (unrolled) address corresponding to an index.
Definition: colvargrid.h:77
void remove_small_values(cvm::real const &a)
Assign values that are smaller than scalar constant the latter value (fast loop)
Definition: colvargrid.h:697
std::ostream & write_multicol(std::ostream &os) const
Write grid in a format which is both human-readable and gnuplot-friendly.
Definition: colvargrid_def.h:434
virtual ~colvar_grid()
Destructor.
Definition: colvargrid.h:232
int value_to_bin_scalar(colvarvalue const &value, const int i) const
Use the lower boundary and the width to report which bin the provided value is in.
Definition: colvargrid.h:515
IST & read_raw_template_(IST &is)
Helper method for read_raw.
Definition: colvargrid_def.h:129
virtual T value_output(std::vector< int > const &ix, size_t const &imult=0) const
Return the value suitable for output purposes (so that it may be rescaled or manipulated without chan...
Definition: colvargrid.h:835
void set_sizes(std::vector< int > const &new_sizes)
Set the sizes in each direction.
Definition: colvargrid.h:147
void copy_grid(colvar_grid< T > const &other_grid)
Copy data from another grid of the same type, AND identical definition (boundaries,...
Definition: colvargrid.h:621
void index(size_t address, std::vector< int > &ix)
Inverse of address(): n-dim index from linear address.
Definition: colvargrid.h:94
bool wrap_detect_edge(std::vector< int > &ix) const
Definition: colvargrid.h:446
void raw_data_out(T *out_data) const
Extract the grid data as they are represented in memory. Put the results in "out_data".
Definition: colvargrid.h:644
void map_grid(colvar_grid< T > const &other_grid)
Add data from another grid of the same type.
Definition: colvargrid.h:770
std::istream & read_multicol(std::istream &is, bool add=false)
Read a grid written by write_multicol(), incrementing if add is true.
Definition: colvargrid_def.h:311
std::vector< bool > periodic
Whether some colvars are periodic.
Definition: colvargrid.h:103
int value_to_bin_scalar(colvarvalue const &value, colvarvalue const &new_offset, cvm::real const &new_width) const
Same as the standard version, but uses another grid definition.
Definition: colvargrid.h:548
void wrap(std::vector< int > &ix) const
Definition: colvargrid.h:429
colvarvalue bin_to_value_scalar(int const &i_bin, colvarvalue const &new_offset, cvm::real const &new_width) const
Same as the standard version, but uses different parameters.
Definition: colvargrid.h:563
colvar_grid()
Default constructor.
Definition: colvargrid.h:223
std::vector< colvar * > cv
Colvars collected in this grid.
Definition: colvargrid.h:71
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid_def.h:112
void raw_data_in(const T *in_data)
Input the data as they are represented in memory.
Definition: colvargrid.h:653
@ f_cv_hard_lower_boundary
The lower boundary is not defined from user's choice.
Definition: colvardeps.h:358
@ f_cv_hard_upper_boundary
The upper boundary is not defined from user's choice.
Definition: colvardeps.h:360
Collective variables module (main class)
Definition: colvarmodule.h:72
static real logn(real const &x)
Definition: colvarmodule.h:231
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:99
static bool constexpr debug()
Whether debug output should be enabled (compile-time option)
Definition: colvarmodule.h:388
static real floor(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:136
static int error_static(std::string const &message, int code=-1)
Definition: colvarmodule.h:789
void log(std::string const &message, int min_log_level=10)
Definition: colvarmodule.cpp:2140
int error(std::string const &message, int code=-1)
Print a message to the main log and set global error code.
Definition: colvarmodule.cpp:2224
static constexpr size_t cv_prec
Number of digits to represent a collective variables value(s)
Definition: colvarmodule.h:743
static real sqrt(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:154
static real fabs(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:148
static constexpr size_t cv_width
Number of characters to represent a collective variables value(s)
Definition: colvarmodule.h:745
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2572
Base class containing parsing functions; all objects which need to parse input inherit from this.
Definition: colvarparse.h:27
int check_keywords(std::string &conf, char const *key)
Check that all the keywords within "conf" are in the list of allowed keywords; this will invoke strip...
Definition: colvarparse.cpp:598
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:63
@ parse_normal
Alias for old default behavior (should be phased out)
Definition: colvarparse.h:82
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
@ type_scalar
Scalar number, implemented as colvarmodule::real (default)
Definition: colvarvalue.h:56
cvm::real real_value
Real data member.
Definition: colvarvalue.h:77
Definition: colvars_memstream.h:30
Collective variables main module.
Parsing functions for collective variables.