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]);
84 cvm::error(
"Error: exceeding bounds in colvar_grid.\n", COLVARS_BUG_ERROR);
132 inline std::vector<int>
const &
sizes()
const
138 inline void set_sizes(std::vector<int>
const &new_sizes)
159 int setup(std::vector<int>
const &nx_i,
161 size_t const &mult_i = 1)
179 for (
int i =
nd-1; i >= 0; i--) {
181 cvm::error(
"Error: providing an invalid number of grid points, "+
183 return COLVARS_ERROR;
202 return setup(this->
nx, T(), this->mult);
250 this->
setup(nx_i, t, mult_i);
259 bool add_extra_bin =
false,
260 std::shared_ptr<const colvar_grid_params> params =
nullptr,
261 std::string config = std::string())
265 this->init_from_colvars(colvars, mult_i, add_extra_bin, params, config);
271 colvar_grid(std::string
const &filename,
size_t mult_i = 1);
273 int init_from_colvars(std::vector<colvar *>
const &colvars,
275 bool add_extra_bin =
false,
276 std::shared_ptr<const colvar_grid_params> params =
nullptr,
277 std::string config = std::string())
280 cvm::log(
"Reading grid configuration from collective variables.\n");
291 " collective variables, multiplicity = "+
cvm::to_str(mult_i)+
".\n");
294 for (i = 0; i <
nd; i++) {
296 cvm::error(
"Colvar grids can only be automatically "
297 "constructed for scalar variables. "
298 "ABF and histogram can not be used; metadynamics "
299 "can be used with useGrids disabled.\n", COLVARS_INPUT_ERROR);
300 return COLVARS_ERROR;
303 if (
cv[i]->width <= 0.0) {
305 "variable with negative or zero width.\n", COLVARS_INPUT_ERROR);
306 return COLVARS_ERROR;
319 if (i > 0 &&
cv[i-1] ==
cv[i]) {
329 if (!config.empty()) {
334 cvm::error(
"Error: init_from_colvars was passed both a grid config and a template grid.", COLVARS_BUG_ERROR);
335 return COLVARS_BUG_ERROR;
340 if (params->nd !=
nd) {
341 cvm::error(
"Trying to initialize grid from template with wrong dimension (" +
344 return COLVARS_ERROR;
353 for (i = 0; i <
nd; i++) {
372 this->init_from_boundaries();
373 return this->
setup();
376 int init_from_boundaries()
379 cvm::log(
"Configuring grid dimensions from colvars boundaries.\n");
394 int nbins_round = (int)(nbins+0.5);
400 ") is not commensurate to its bin width("+
403 (nbins_round *
widths[i]);
410 nx.push_back(nbins_round);
418 inline void wrap(std::vector<int> & ix)
const
420 for (
size_t i = 0; i <
nd; i++) {
422 ix[i] = (ix[i] +
nx[i]) %
nx[i];
424 if (ix[i] < 0 || ix[i] >=
nx[i]) {
425 cvm::error(
"Trying to wrap illegal index vector (non-PBC) for a grid point: "
438 for (
size_t i = 0; i <
nd; i++) {
440 ix[i] = (ix[i] +
nx[i]) %
nx[i];
441 }
else if (ix[i] < 0 || ix[i] >=
nx[i]) {
450 inline bool wrap_to_edge(std::vector<int> & ix, std::vector<int> & edge_bin)
const
454 for (
size_t i = 0; i <
nd; i++) {
456 ix[i] = (ix[i] +
nx[i]) %
nx[i];
458 }
else if (ix[i] < 0) {
461 }
else if (ix[i] >=
nx[i]) {
463 edge_bin[i] =
nx[i] - 1;
481 for (
size_t i = 0; i <
nd; i++) {
498 cv[i]->actual_value().vector1d_value[iv] :
499 cv[i]->
value().vector1d_value[iv], i);
531 if (bin_index < 0) bin_index=0;
532 if (bin_index >=
int(
nx[i])) bin_index=int(
nx[i])-1;
533 return (
int) bin_index;
556 return new_offset.
real_value + new_width * (0.5 + i_bin);
562 size_t const &imult = 0)
588 if (other_grid.
multiplicity() != this->multiplicity()) {
589 cvm::error(
"Error: trying to subtract two grids with "
590 "different multiplicity.\n");
594 if (other_grid.
data.size() != this->data.size()) {
595 cvm::error(
"Error: trying to subtract two grids with "
596 "different size.\n");
600 for (
size_t i = 0; i <
data.size(); i++) {
612 if (other_grid.
multiplicity() != this->multiplicity()) {
613 cvm::error(
"Error: trying to copy two grids with "
614 "different multiplicity.\n");
618 if (other_grid.
data.size() != this->data.size()) {
619 cvm::error(
"Error: trying to copy two grids with "
620 "different size.\n");
625 for (
size_t i = 0; i <
data.size(); i++) {
635 for (
size_t i = 0; i <
data.size(); i++) out_data[i] =
data[i];
644 for (
size_t i = 0; i <
data.size(); i++)
data[i] = in_data[i];
658 inline T
const &
value(std::vector<int>
const &ix,
659 size_t const &imult = 0)
const
665 inline T
const &
value(
size_t i)
const
673 for (
size_t i = 0; i <
nt; i++)
681 for (
size_t i = 0; i <
nt; i++)
688 for (
size_t i = 0; i <
nt; i++)
698 for (
size_t i = 0; i <
nd; i++) {
709 for (
size_t i = 0; i <
nd; i++) {
720 for (
size_t i = 0; i <
nd; i++) {
730 bool skip_hard_boundaries =
false)
733 for (
size_t i = 0; i <
nd; i++) {
761 if (other_grid.
multiplicity() != this->multiplicity()) {
762 cvm::error(
"Error: trying to merge two grids with values of "
763 "different multiplicity.\n");
768 std::vector<cvm::real>
const &gw = this->
widths;
770 std::vector<cvm::real>
const &ogw = other_grid.
widths;
773 std::vector<int> oix = other_grid.
new_index();
779 for (
size_t i = 0; i <
nd; i++) {
790 for (
size_t im = 0; im <
mult; im++) {
805 if (other_grid.
multiplicity() != this->multiplicity()) {
806 cvm::error(
"Error: trying to sum togetehr two grids with values of "
807 "different multiplicity.\n");
810 if (scale_factor != 1.0)
811 for (
size_t i = 0; i <
data.size(); i++) {
812 data[i] +=
static_cast<T
>(scale_factor * other_grid.
data[i]);
816 for (
size_t i = 0; i <
data.size(); i++) {
825 size_t const &imult = 0)
const
827 return value(ix, imult);
835 size_t const &imult = 0,
856 return std::vector<int> (
nd, 0);
861 inline bool index_ok(std::vector<int>
const &ix)
const
863 for (
size_t i = 0; i <
nd; i++) {
864 if ( (ix[i] < 0) || (ix[i] >=
int(
nx[i])) )
872 inline void incr(std::vector<int> &ix)
const
874 for (
int i = ix.size()-1; i >= 0; i--) {
878 if (ix[i] >=
nx[i]) {
908 for (
size_t i = 0; i <
nd; i++) {
914 widths[i])) > 1.0E-10) ) {
915 cvm::error(
"Error: restart information for a grid is "
916 "inconsistent with that of its colvars.\n");
927 for (
size_t i = 0; i <
nd; i++) {
937 (
data.size() != other_grid.
data.size()) ) {
939 "two grids that are supposed to be equal, "
940 "aside from the data stored.\n");
959 std::istream &
read_raw(std::istream &is);
967 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
975 std::istream &
read_multicol(std::istream &is,
bool add =
false);
979 std::string description =
"grid file",
987 std::string description =
"grid file")
const;
994 std::string description =
"grid file")
const;
1014 std::shared_ptr<const colvar_grid_params> params =
nullptr);
1017 std::string config);
1051 std::istream &
read_raw(std::istream &is);
1059 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
1067 std::istream &
read_multicol(std::istream &is,
bool add =
false);
1071 std::string description =
"grid file",
1079 std::string description =
"grid file")
const;
1086 std::string description =
"grid file")
const;
1091 size_t const &imult = 0,
1097 if (this->has_parent_data) {
1114 for (
size_t i = 0; i <
nd; i++) {
1133 for (i = -radius; i <= radius; i++) {
1143 for (i = -radius; i <= radius; i++) {
1145 for (j = -radius; j <= radius; j++) {
1156 for (i = -radius; i <= radius; i++) {
1158 for (j = -radius; j <= radius; j++) {
1160 for (k = -radius; k <= radius; k++) {
1172 cvm::error(
"Error: local_sample_count is not implemented for grids of dimension > 3", COLVARS_NOT_IMPLEMENTED);
1178 return count / nbins;
1188 int n = 0,
int offset = 0)
1191 std::vector<int> ix = ix0;
1196 A0 =
value(ix) + offset;
1199 A1 =
value(ix) + offset;
1206 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1208 A0 =
value(ix) + offset;
1211 A1 =
value(ix) + offset;
1220 int increment = (ix[n] == 0 ? 1 : -1);
1222 A0 =
value(ix) + offset;
1223 ix[n] += increment; A1 =
value(ix) + offset;
1224 ix[n] += increment; A2 =
value(ix) + offset;
1225 if (A0 * A1 * A2 == 0) {
1242 std::vector<int> ix = ix0;
1254 return (A1 - A0) / (
widths[n] * 2.);
1256 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1265 return (A1 - A0) / (
widths[n] * 2.);
1269 int increment = (ix[n] == 0 ? 1 : -1);
1272 ix[n] += increment; A1 =
value(ix);
1273 ix[n] += increment; A2 =
value(ix);
1274 return (-1.5 * A0 + 2. * A1
1275 - 0.5 * A2) * increment /
widths[n];
1301 std::shared_ptr<const colvar_grid_params> params =
nullptr,
1302 bool add_extra_bin =
false,
1303 std::string config = std::string());
1311 size_t const &imult = 0)
1341 std::istream &
read_raw(std::istream &is);
1349 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
1357 std::istream &
read_multicol(std::istream &is,
bool add =
false);
1361 std::string description =
"grid file",
1369 std::string description =
"grid file")
const;
1376 std::string description =
"grid file")
const;
1385 std::vector<int> ix;
1389 for (n = 0; n < 2; n++) {
1394 ix[1-n]++;
wrap(ix);
1398 grad[n] = 0.5 * (A1 - A0) /
widths[n];
1400 }
else if (
nd == 3) {
1405 for (i = 0; i<2; i++) {
1407 for (j = 0; j<2; j++) {
1409 for (k = 0; k<2; k++) {
1411 p[index++] =
value(ix);
1421 grad[0] = 0.25 * ((p[4] + p[5] + p[6] + p[7]) - (p[0] + p[1] + p[2] + p[3])) /
widths[0];
1423 grad[1] = 0.25 * ((p[2] + p[3] + p[6] + p[7]) - (p[0] + p[1] + p[4] + p[5])) /
widths[1];
1425 grad[2] = 0.25 * ((p[1] + p[3] + p[5] + p[7]) - (p[0] + p[2] + p[4] + p[6])) /
widths[2];
1427 cvm::error(
"Finite differences available in dimension 2 and 3 only.");
1436 int n = 0,
int offset = 0)
1439 std::vector<int> ix = ix0;
1444 A0 =
value(ix) + offset;
1447 A1 =
value(ix) + offset;
1454 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1456 A0 =
value(ix) + offset;
1459 A1 =
value(ix) + offset;
1468 int increment = (ix[n] == 0 ? 1 : -1);
1470 A0 =
value(ix) + offset;
1471 ix[n] += increment; A1 =
value(ix) + offset;
1472 ix[n] += increment; A2 =
value(ix) + offset;
1473 if (A0 * A1 * A2 == 0) {
1490 std::vector<int> ix = ix0;
1502 return (A1 - A0) / (
widths[n] * 2.);
1504 }
else if (ix[n] > 0 && ix[n] <
nx[n]-1) {
1517 int increment = (ix[n] == 0 ? 1 : -1);
1520 ix[n] += increment; A1 =
value(ix);
1521 ix[n] += increment; A2 =
value(ix);
1531 size_t const &imult = 0)
const override
1535 cvm::error(
"Error: trying to access a component "
1536 "larger than 1 in a scalar data grid.\n");
1551 size_t const &imult = 0,
1552 bool add =
false)
override
1555 cvm::error(
"Error: trying to access a component "
1556 "larger than 1 in a scalar data grid.\n");
1624 std::shared_ptr<colvar_grid_count> samples_in =
nullptr,
1625 std::shared_ptr<const colvar_grid_params> params =
nullptr,
1626 std::string config = std::string());
1652 std::istream &
read_raw(std::istream &is);
1660 std::ostream &
write_raw(std::ostream &os,
size_t const buf_size = 3)
const;
1668 std::istream &
read_multicol(std::istream &is,
bool add =
false);
1672 std::string description =
"grid file",
1680 std::string description =
"grid file")
const;
1687 std::string description =
"grid file")
const;
1690 inline void vector_value(std::vector<int>
const &ix, std::vector<cvm::real> &v)
const
1694 int count =
samples->value(ix);
1697 for (
size_t i = 0; i <
mult; i++) {
1698 v[i] = invcount * p[i];
1701 for (
size_t i = 0; i <
mult; i++) {
1706 for (
size_t i = 0; i <
mult; i++) {
1714 inline void acc_value(std::vector<int>
const &ix, std::vector<colvarvalue>
const &values) {
1715 for (
size_t imult = 0; imult <
mult; imult++) {
1716 data[
address(ix) + imult] += values[imult].real_value;
1725 for (
size_t imult = 0; imult <
mult; imult++) {
1735 size_t const &imult = 0)
const override
1739 return ( (s =
samples->value(ix)) > 0) ?
1752 if ( weight <= min_samples ) {
1757 fact = 1.0 / weight;
1780 fact = weight > 0. ? 1. / weight : 0.;
1802 fact = weight > 0. ? 1. / weight : 0.;
1808 #ifdef __clang_analyzer__
1812 for (
size_t imult = 0; imult <
mult; imult++) {
1813 grad[imult] = fact * p[imult];
1822 size_t const &imult = 0,
1823 bool add =
false)
override
1843 if (
nd != 1 ||
nx[0] == 0) {
1880 std::shared_ptr<colvar_grid_gradient> gradients);
1913 std::shared_ptr<colvar_grid_gradient> gradients;
1926 void nr_linbcg_sym(
const std::vector<cvm::real> &b, std::vector<cvm::real> &x,
1933 void atimes(
const std::vector<cvm::real> &x, std::vector<cvm::real> &r);
Colvar_grid derived class to hold counters in discrete n-dim colvar space.
Definition: colvargrid.h:1002
colvar_grid_count()
Default constructor.
Definition: colvargrid.cpp:21
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:1109
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:68
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid.cpp:37
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:1238
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid.cpp:58
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid.cpp:112
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid.cpp:78
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:42
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:1089
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:48
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:89
std::ostream & write_multicol(std::ostream &os) const
Write grid in a format which is both human-readable and gnuplot-friendly.
Definition: colvargrid.cpp:101
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_count)
Definition: colvargrid.h:1187
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:1026
virtual ~colvar_grid_count()
Destructor.
Definition: colvargrid.h:1009
void incr_count(std::vector< int > const &ix)
Increment the counter at given position.
Definition: colvargrid.h:1020
Class for accumulating the gradient of a scalar function on a grid.
Definition: colvargrid.h:1598
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid.cpp:369
cvm::real smooth_inverse_weight(cvm::real weight)
Definition: colvargrid.h:1749
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:1789
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:1767
void acc_value(std::vector< int > const &ix, std::vector< colvarvalue > const &values)
Accumulate the value.
Definition: colvargrid.h:1714
cvm::real average(bool smoothed=false)
Compute and return average value for a 1D gradient grid.
Definition: colvargrid.h:1841
std::shared_ptr< colvar_grid_count > samples
Provide the sample count by which each binned value should be divided.
Definition: colvargrid.h:1603
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:421
colvar_grid_gradient()
Default constructor.
Definition: colvargrid.cpp:335
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid.cpp:390
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 integr...
Definition: colvargrid.cpp:456
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:374
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:1734
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:400
virtual ~colvar_grid_gradient()
Destructor.
Definition: colvargrid.h:1609
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:1820
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:1724
int full_samples
Parameters for smoothing data with low sampling.
Definition: colvargrid.h:1629
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid.cpp:410
std::ostream & write_multicol(std::ostream &os) const
Write grid in a format which is both human-readable and gnuplot-friendly.
Definition: colvargrid.cpp:433
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:380
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:513
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:1690
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid.cpp:444
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:1283
void acc_value(std::vector< int > const &ix, cvm::real const &new_value, size_t const &imult=0)
Accumulate the value.
Definition: colvargrid.h:1309
cvm::real minimum_value() const
Return the lowest value.
Definition: colvargrid.cpp:249
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:1486
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:163
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid.cpp:152
virtual ~colvar_grid_scalar()
Destructor.
Definition: colvargrid.cpp:148
cvm::real minimum_pos_value() const
Return the lowest positive value.
Definition: colvargrid.cpp:258
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:1530
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:305
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid.cpp:227
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid.cpp:173
std::ostream & write_multicol(std::ostream &os) const
Write grid in a format which is both human-readable and gnuplot-friendly.
Definition: colvargrid.cpp:216
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:204
cvm::real integral() const
Calculates the integral of the map (uses widths if they are defined)
Definition: colvargrid.cpp:274
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:157
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid.cpp:193
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:1549
cvm::real entropy() const
Assuming that the map is a normalized probability density, calculates the entropy (uses widths if the...
Definition: colvargrid.cpp:288
colvar_grid_scalar()
Default constructor.
Definition: colvargrid.cpp:125
cvm::real maximum_value() const
Return the highest value.
Definition: colvargrid.cpp:239
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:1435
colvar_grid_count * samples
Provide the associated sample count by which each binned value should be divided.
Definition: colvargrid.h:1288
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:1382
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid.cpp:183
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:200
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:100
void set_value(size_t i, T const &t)
Set the value at the point with linear address i (for speed)
Definition: colvargrid.h:569
void check_consistency()
Check that the grid information inside (boundaries, widths, ...) is consistent with the current setti...
Definition: colvargrid.h:906
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid_def.h:202
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid_def.h:169
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:489
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:854
void add_constant(T const &t)
Add a constant to all elements (fast loop)
Definition: colvargrid.h:671
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:517
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:872
size_t number_of_points(int const icv=-1) const
Definition: colvargrid.h:122
std::vector< bool > hard_upper_boundaries
Whether some colvars have hard upper boundaries.
Definition: colvargrid.h:100
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:833
T const & value(size_t i) const
Get the binned value indexed by linear address i.
Definition: colvargrid.h:665
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:717
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:478
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:144
std::vector< int > const & sizes() const
Get the sizes in each direction.
Definition: colvargrid.h:132
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:560
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:546
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:679
std::vector< int > const & number_of_points_vec() const
Return the numbers of points in all dimensions.
Definition: colvargrid.h:115
bool index_ok(std::vector< int > const &ix) const
Check that the index is within range in each of the dimensions.
Definition: colvargrid.h:861
cvm::real current_bin_scalar_fraction(int const i) const
Report the fraction of bin beyond current_bin_scalar()
Definition: colvargrid.h:510
bool has_data
Whether this grid has been filled with data or is still empty.
Definition: colvargrid.h:106
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:802
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:695
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:925
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:245
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:232
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:575
void reset(T const &t=T())
Reset data (in case the grid is being reused)
Definition: colvargrid.h:206
size_t num_variables() const
Return the number of colvar objects.
Definition: colvargrid.h:109
int setup(std::vector< int > const &nx_i, T const &t=T(), size_t const &mult_i=1)
Allocate data.
Definition: colvargrid.h:159
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:256
colvar_grid(colvar_grid< T > const &g)
"Almost copy-constructor": only copies configuration parameters from another grid,...
Definition: colvargrid.h:228
bool has_parent_data
True if this is a count grid related to another grid of data.
Definition: colvargrid.h:103
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid_def.h:493
bool wrap_to_edge(std::vector< int > &ix, std::vector< int > &edge_bin) const
Definition: colvargrid.h:450
std::vector< int > const get_colvars_index() const
Get the bin indices corresponding to the current values of the colvars.
Definition: colvargrid.h:706
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:495
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:585
void request_actual_value(bool b=true)
Request grid to use actual values of extended coords.
Definition: colvargrid.h:150
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:729
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:525
int current_bin_scalar(int const i) const
Report the bin corresponding to the current value of variable i.
Definition: colvargrid.h:471
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:156
std::vector< bool > hard_lower_boundaries
Whether some colvars have hard lower boundaries.
Definition: colvargrid.h:97
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:658
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:653
size_t address(std::vector< int > const &ix) const
Get the low-level index 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:686
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:429
virtual ~colvar_grid()
Destructor.
Definition: colvargrid.h:222
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:504
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:824
void set_sizes(std::vector< int > const &new_sizes)
Set the sizes in each direction.
Definition: colvargrid.h:138
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:610
bool wrap_detect_edge(std::vector< int > &ix) const
Definition: colvargrid.h:435
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:633
void map_grid(colvar_grid< T > const &other_grid)
Add data from another grid of the same type.
Definition: colvargrid.h:759
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:310
std::vector< bool > periodic
Whether some colvars are periodic.
Definition: colvargrid.h:94
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:537
void wrap(std::vector< int > &ix) const
Definition: colvargrid.h:418
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:552
colvar_grid()
Default constructor.
Definition: colvargrid.h:213
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:642
@ f_cv_hard_lower_boundary
The lower boundary is not defined from user's choice.
Definition: colvardeps.h:326
@ f_cv_hard_upper_boundary
The upper boundary is not defined from user's choice.
Definition: colvardeps.h:328
static real logn(real const &x)
Definition: colvarmodule.h:209
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:100
static real floor(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:126
static void log(std::string const &message, int min_log_level=10)
Definition: colvarmodule.cpp:1993
static int error(std::string const &message, int code=-1)
Print a message to the main log and set global error code.
Definition: colvarmodule.cpp:2070
static size_t const cv_prec
Number of digits to represent a collective variables value(s)
Definition: colvarmodule.h:699
static real sqrt(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:138
static size_t const cv_width
Number of characters to represent a collective variables value(s)
Definition: colvarmodule.h:701
static bool debug()
Whether debug output should be enabled (compile-time option)
Definition: colvarmodule.h:365
static real fabs(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:132
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2416
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:591
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:53
@ parse_normal
Alias for old default behavior (should be phased out)
Definition: colvarparse.h:72
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
Integrate (1D, 2D or 3D) gradients.
Definition: colvargrid.h:1870
void set_zero_minimum()
Add constant to potential so that its minimum value is zero Useful e.g. for output.
Definition: colvargrid.h:1902
void update_div_local(const std::vector< int > &ix)
Update matrix containing divergence and boundary conditions called by update_div_neighbors and by col...
Definition: colvargrid.cpp:699
std::vector< cvm::real > divergence
Array holding divergence + boundary terms (modified Neumann) if not periodic.
Definition: colvargrid.h:1916
void set_div()
Set matrix containing divergence and boundary conditions based on complete gradient grid.
Definition: colvargrid.cpp:636
void update_div_neighbors(const std::vector< int > &ix)
Update matrix containing divergence and boundary conditions based on new gradient point value,...
Definition: colvargrid.cpp:645
cvm::real l2norm(const std::vector< cvm::real > &x)
l2 norm of a vector
Definition: colvargrid.cpp:1220
bool b_smoothed
Flag requesting the use of a smoothed version of the gradient (default: false)
Definition: colvargrid.h:1907
int integrate(const int itmax, const cvm::real &tol, cvm::real &err, bool verbose=true)
Calculate potential from divergence (in 2D); return number of steps.
Definition: colvargrid.cpp:596
void get_grad(cvm::real *g, std::vector< int > &ix)
Definition: colvargrid.cpp:683
void nr_linbcg_sym(const std::vector< cvm::real > &b, std::vector< cvm::real > &x, const cvm::real &tol, const int itmax, int &iter, cvm::real &err)
Solve linear system based on CG, valid for symmetric matrices only.
Definition: colvargrid.cpp:1167
void atimes(const std::vector< cvm::real > &x, std::vector< cvm::real > &r)
Multiplication by sparse matrix representing Lagrangian (or its transpose)
Definition: colvargrid.cpp:751
Collective variables main module.
Parsing functions for collective variables.