12#ifndef COLVARGRID_DEF_H
13#define COLVARGRID_DEF_H
21#include "colvargrid.h"
22#include "colvars_memstream.h"
39if ( !(is >> hash) || (hash !=
"#") ) {
42 " in stream(read \"" + hash +
"\")\n");
47mult = (mult_i == 0) ? nd : mult_i;
49std::vector<cvm::real> lower_in(nd), widths_in(nd);
50std::vector<int> nx_in(nd);
51std::vector<int> periodic_in(nd);
53for (i = 0; i < nd; i++ ) {
54 if ( !(is >> hash) || (hash !=
"#") ) {
57 " in stream(read \"" + hash +
"\")\n");
61 is >> lower_in[i] >> widths_in[i] >> nx_in[i] >> periodic_in[i];
64this->setup(nx_in, 0., mult);
68for (i = 0; i < nd; i++ ) {
69 lower_boundaries.push_back(
colvarvalue(lower_in[i]));
70 periodic.push_back(
static_cast<bool>(periodic_in[i]));
81template <
class T,
class IST> IST &read_restart_template_(
colvar_grid<T> &g, IST &is)
83 auto const start_pos = is.tellg();
89 auto const error_pos = is.tellg();
92 is.setstate(std::ios::failbit);
102 return read_restart_template_<T, std::istream>(*
this, is);
108 return read_restart_template_<T, cvm::memory_stream>(*
this, is);
114 os <<
"grid_parameters {\n" << get_state_params() <<
"}\n";
122 os << std::string(
"grid_parameters") << get_state_params();
128template <
class T,
class IST> IST &read_raw_template_(
colvar_grid<T> &g, IST &is)
130 auto const start_pos = is.tellg();
133 for (
size_t imult = 0; imult < g.
mult; imult++) {
135 if (is >> new_value) {
140 is.setstate(std::ios::failbit);
142 "Error: failed to read all of the grid points from file. Possible explanations: grid "
143 "parameters in the configuration (lowerBoundary, upperBoundary, width) are different "
144 "from those in the file, or the file is corrupt/incomplete.\n",
145 COLVARS_INPUT_ERROR);
158 return read_raw_template_<T, std::istream>(*
this, is);
164 return read_raw_template_<T, cvm::memory_stream>(*
this, is);
171 auto const w = os.width();
172 auto const p = os.precision();
175 for (
auto ix = new_index(); index_ok(ix); incr(ix)) {
176 for (
size_t imult = 0; imult < mult; imult++) {
177 os <<
" " << std::setw(w) << std::setprecision(p) << value_output(ix, imult);
178 if (((++count) % buf_size) == 0)
183 if ((count % buf_size) != 0)
193 for (
auto ix = new_index(); index_ok(ix); incr(ix)) {
194 for (
size_t imult = 0; imult < mult; imult++) {
195 os << value_output(ix, imult);
204 std::ostringstream os;
206 os <<
" n_colvars " << nd <<
"\n";
208 os <<
" lower_boundaries ";
209 for (i = 0; i < nd; i++)
210 os <<
" " << lower_boundaries[i];
213 os <<
" upper_boundaries ";
214 for (i = 0; i < nd; i++)
215 os <<
" " << upper_boundaries[i];
219 for (i = 0; i < nd; i++)
220 os <<
" " << widths[i];
224 for (i = 0; i < nd; i++)
236 cvm::log(
"Reading grid configuration from string.\n");
238 std::vector<int> old_nx = nx;
239 std::vector<colvarvalue> old_lb = lower_boundaries;
240 std::vector<colvarvalue> old_ub = upper_boundaries;
241 std::vector<cvm::real> old_w = widths;
248 cvm::error(
"Error: trying to read data for a grid "
249 "that contains a different number of colvars ("+
253 return COLVARS_ERROR;
258 colvarparse::get_keyval(conf,
"lower_boundaries",
260 colvarparse::get_keyval(conf,
"upper_boundaries",
266 colvarparse::get_keyval(conf,
"lowerBoundary",
267 lower_boundaries, lower_boundaries, parse_mode);
268 colvarparse::get_keyval(conf,
"upperBoundary",
269 upper_boundaries, upper_boundaries, parse_mode);
271 colvarparse::get_keyval(conf,
"width", widths, widths, parse_mode);
276 if (nd < lower_boundaries.size()) nd = lower_boundaries.size();
278 if (! use_actual_value.size()) use_actual_value.assign(nd,
false);
279 if (! periodic.size()) periodic.assign(nd,
false);
280 if (! widths.size()) widths.assign(nd, 1.0);
284 bool new_params =
false;
286 for (
size_t i = 0; i < nd; i++) {
287 if (old_nx[i] != nx[i] ||
288 cvm::sqrt(cv[i]->dist2(old_lb[i], lower_boundaries[i])) > eps ||
289 cvm::sqrt(cv[i]->dist2(old_ub[i], upper_boundaries[i])) > eps ||
300 init_from_boundaries();
302 return this->setup(nx, T(), mult);
317 size_t n, periodic_flag;
319 std::vector<T> new_value;
320 std::vector<int> nx_read;
321 std::vector<int> bin;
323 if ( cv.size() > 0 && cv.size() != nd ) {
324 cvm::error(
"Cannot read grid file: number of variables in file differs from number referenced by grid.\n");
328 if ( !(is >> hash) || (hash !=
"#") ) {
331 " in stream(read \"" + hash +
"\")\n", COLVARS_INPUT_ERROR);
337 cvm::error(
"Error reading grid: wrong number of collective variables.\n");
343 new_value.resize(mult);
345 if (this->has_parent_data && add) {
346 new_data.resize(data.size());
350 for (
size_t i = 0; i < nd; i++ ) {
351 if ( !(is >> hash) || (hash !=
"#") ) {
354 " in stream(read \"" + hash +
"\")\n");
358 is >> lower >> width >> nx_read[i] >> periodic_flag;
361 if ( (
cvm::fabs(lower - lower_boundaries[i].real_value) > 1.0e-10) ||
362 (
cvm::fabs(width - widths[i] ) > 1.0e-10) ||
363 (nx_read[i] != nx[i]) ) {
364 cvm::log(
"Warning: reading from different grid definition (colvar "
365 +
cvm::to_str(i+1) +
"); remapping data on new grid.\n");
373 bool end_of_file =
false;
375 for (
size_t i = 0; i < nd; i++ ) {
376 if ( !(is >> x) ) end_of_file =
true;
377 bin[i] = value_to_bin_scalar(x, i);
380 wrap_detect_edge(bin);
382 if (end_of_file)
break;
384 for (
size_t imult = 0; imult < mult; imult++) {
385 is >> new_value[imult];
388 if ( index_ok(bin) ) {
389 for (
size_t imult = 0; imult < mult; imult++) {
390 value_input(bin, new_value[imult], imult, add);
396 for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
397 for (
size_t i = 0; i < nd; i++ ) {
400 for (
size_t imult = 0; imult < mult; imult++) {
401 is >> new_value[imult];
402 value_input(ix, new_value[imult], imult, add);
413 std::string description,
418 return COLVARS_FILE_ERROR;
424 return COLVARS_FILE_ERROR;
432 std::ios_base::fmtflags prev_flags(os.flags());
437 os << std::setw(2) <<
"# " << nd <<
"\n";
439 os.setf(std::ios::scientific, std::ios::floatfield);
440 for (
size_t i = 0; i < nd; i++) {
444 << std::setw(10) << nx[i] <<
" "
445 << periodic[i] <<
"\n";
448 for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
450 if (ix.back() == 0) {
455 for (
size_t i = 0; i < nd; i++) {
458 << bin_to_value_scalar(ix[i], i);
461 for (
size_t imult = 0; imult < mult; imult++) {
464 << value_output(ix, imult);
470 os.flags(prev_flags);
478 std::string description)
const
480 int error_code = COLVARS_OK;
483 return COLVARS_FILE_ERROR;
496 os <<
"object 1 class gridpositions counts";
498 for (icv = 0; icv < num_variables(); icv++) {
499 os <<
" " << number_of_points(icv);
504 for (icv = 0; icv < num_variables(); icv++) {
505 os <<
" " << (lower_boundaries[icv].real_value + 0.5 * widths[icv]);
509 for (icv = 0; icv < num_variables(); icv++) {
511 for (
size_t icv2 = 0; icv2 < num_variables(); icv2++) {
512 if (icv == icv2) os <<
" " << widths[icv];
513 else os <<
" " << 0.0;
518 os <<
"object 2 class gridconnections counts";
519 for (icv = 0; icv < num_variables(); icv++) {
520 os <<
" " << number_of_points(icv);
524 os <<
"object 3 class array type double rank 0 items "
525 << number_of_points() <<
" data follows\n";
529 os <<
"object \"collective variables scalar field\" class field\n";
536 std::string description)
const
538 int error_code = COLVARS_OK;
541 return COLVARS_FILE_ERROR;
Grid of values of a function of several collective variables.
Definition: colvargrid.h:52
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:100
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
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 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
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
size_t mult
Multiplicity of each datum (allow the binning of non-scalar types such as atomic gradients)
Definition: colvargrid.h:59
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
bool has_data
Whether this grid has been filled with data or is still empty.
Definition: colvargrid.h:106
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
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
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:156
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
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
colvar_grid()
Default constructor.
Definition: colvargrid.h:213
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid_def.h:112
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:100
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 colvarmodule * main()
Access the one instance of the Colvars module.
Definition: colvarmodule.cpp:175
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 colvarproxy * proxy
Pointer to the proxy object, used to retrieve atomic data from the hosting program; it is static in o...
Definition: colvarmodule.h:895
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2416
Definition: colvarparse.h:276
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:53
@ parse_restart
The call is being executed from a read_restart() function.
Definition: colvarparse.h:70
@ parse_silent
Do not print the keyword.
Definition: colvarparse.h:63
virtual int close_output_stream(std::string const &output_name)
Closes the given output file/channel.
Definition: colvarproxy_io.cpp:475
std::istream & input_stream(std::string const &input_name, std::string const description="file/channel", bool error_on_fail=true)
Definition: colvarproxy_io.cpp:265
virtual std::ostream & output_stream(std::string const &output_name, std::string const description)
Definition: colvarproxy_io.cpp:408
int close_input_stream(std::string const &input_name)
Closes the given input stream.
Definition: colvarproxy_io.cpp:339
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
Definition: colvars_memstream.h:30
Collective variables main module.