12#ifndef COLVARGRID_DEF_H
13#define COLVARGRID_DEF_H
21#include "colvargrid.h"
22#include "colvars_memstream.h"
25template <
class T,
class IST> IST &read_restart_template_(
colvar_grid<T> &g, IST &is)
27 auto const start_pos = is.tellg();
33 auto const error_pos = is.tellg();
36 is.setstate(std::ios::failbit);
46 return read_restart_template_<T, std::istream>(*
this, is);
52 return read_restart_template_<T, cvm::memory_stream>(*
this, is);
58 os <<
"grid_parameters {\n" << get_state_params() <<
"}\n";
66 os << std::string(
"grid_parameters") << get_state_params();
72template <
class T,
class IST> IST &read_raw_template_(
colvar_grid<T> &g, IST &is)
74 auto const start_pos = is.tellg();
77 for (
size_t imult = 0; imult < g.
mult; imult++) {
79 if (is >> new_value) {
84 is.setstate(std::ios::failbit);
86 "Error: failed to read all of the grid points from file. Possible explanations: grid "
87 "parameters in the configuration (lowerBoundary, upperBoundary, width) are different "
88 "from those in the file, or the file is corrupt/incomplete.\n",
102 return read_raw_template_<T, std::istream>(*
this, is);
108 return read_raw_template_<T, cvm::memory_stream>(*
this, is);
115 auto const w = os.width();
116 auto const p = os.precision();
119 for (
auto ix = new_index(); index_ok(ix); incr(ix)) {
120 for (
size_t imult = 0; imult < mult; imult++) {
121 os <<
" " << std::setw(w) << std::setprecision(p) << value_output(ix, imult);
122 if (((++count) % buf_size) == 0)
127 if ((count % buf_size) != 0)
137 for (
auto ix = new_index(); index_ok(ix); incr(ix)) {
138 for (
size_t imult = 0; imult < mult; imult++) {
139 os << value_output(ix, imult);
148 std::ostringstream os;
150 os <<
" n_colvars " << nd <<
"\n";
152 os <<
" lower_boundaries ";
153 for (i = 0; i < nd; i++)
154 os <<
" " << lower_boundaries[i];
157 os <<
" upper_boundaries ";
158 for (i = 0; i < nd; i++)
159 os <<
" " << upper_boundaries[i];
163 for (i = 0; i < nd; i++)
164 os <<
" " << widths[i];
168 for (i = 0; i < nd; i++)
180 cvm::log(
"Reading grid configuration from string.\n");
182 std::vector<int> old_nx = nx;
183 std::vector<colvarvalue> old_lb = lower_boundaries;
184 std::vector<colvarvalue> old_ub = upper_boundaries;
185 std::vector<cvm::real> old_w = widths;
192 cvm::error(
"Error: trying to read data for a grid "
193 "that contains a different number of colvars ("+
197 return COLVARS_ERROR;
202 colvarparse::get_keyval(conf,
"lower_boundaries",
204 colvarparse::get_keyval(conf,
"upper_boundaries",
208 colvarparse::get_keyval(conf,
"lowerBoundaries",
209 lower_boundaries, lower_boundaries, parse_mode);
210 colvarparse::get_keyval(conf,
"upperBoundaries",
211 upper_boundaries, upper_boundaries, parse_mode);
213 colvarparse::get_keyval(conf,
"widths", widths, widths, parse_mode);
218 if (nd < lower_boundaries.size()) nd = lower_boundaries.size();
220 if (! use_actual_value.size()) use_actual_value.assign(nd,
false);
221 if (! periodic.size()) periodic.assign(nd,
false);
222 if (! widths.size()) widths.assign(nd, 1.0);
226 bool new_params =
false;
228 for (
size_t i = 0; i < nd; i++) {
229 if (old_nx[i] != nx[i] ||
230 cvm::sqrt(cv[i]->dist2(old_lb[i], lower_boundaries[i])) > eps ||
231 cvm::sqrt(cv[i]->dist2(old_ub[i], upper_boundaries[i])) > eps ||
242 init_from_boundaries();
244 return this->setup(nx, T(), mult);
259 size_t n, periodic_flag;
261 std::vector<T> new_value;
262 std::vector<int> nx_read;
263 std::vector<int> bin;
265 if ( cv.size() > 0 && cv.size() != nd ) {
266 cvm::error(
"Cannot read grid file: number of variables in file differs from number referenced by grid.\n");
270 if ( !(is >> hash) || (hash !=
"#") ) {
273 " in stream(read \"" + hash +
"\")\n", COLVARS_INPUT_ERROR);
279 cvm::error(
"Error reading grid: wrong number of collective variables.\n");
285 new_value.resize(mult);
287 if (this->has_parent_data && add) {
288 new_data.resize(data.size());
292 for (
size_t i = 0; i < nd; i++ ) {
293 if ( !(is >> hash) || (hash !=
"#") ) {
296 " in stream(read \"" + hash +
"\")\n");
300 is >> lower >> width >> nx_read[i] >> periodic_flag;
303 if ( (
cvm::fabs(lower - lower_boundaries[i].real_value) > 1.0e-10) ||
304 (
cvm::fabs(width - widths[i] ) > 1.0e-10) ||
305 (nx_read[i] != nx[i]) ) {
306 cvm::log(
"Warning: reading from different grid definition (colvar "
307 +
cvm::to_str(i+1) +
"); remapping data on new grid.\n");
315 bool end_of_file =
false;
317 for (
size_t i = 0; i < nd; i++ ) {
318 if ( !(is >> x) ) end_of_file =
true;
319 bin[i] = value_to_bin_scalar(x, i);
322 wrap_detect_edge(bin);
324 if (end_of_file)
break;
326 for (
size_t imult = 0; imult < mult; imult++) {
327 is >> new_value[imult];
330 if ( index_ok(bin) ) {
331 for (
size_t imult = 0; imult < mult; imult++) {
332 value_input(bin, new_value[imult], imult, add);
338 for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
339 for (
size_t i = 0; i < nd; i++ ) {
342 for (
size_t imult = 0; imult < mult; imult++) {
343 is >> new_value[imult];
344 value_input(ix, new_value[imult], imult, add);
355 std::string description,
360 return COLVARS_FILE_ERROR;
366 return COLVARS_FILE_ERROR;
374 std::ios_base::fmtflags prev_flags(os.flags());
379 os << std::setw(2) <<
"# " << nd <<
"\n";
381 os.setf(std::ios::scientific, std::ios::floatfield);
382 for (
size_t i = 0; i < nd; i++) {
386 << std::setw(10) << nx[i] <<
" "
387 << periodic[i] <<
"\n";
390 for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
392 if (ix.back() == 0) {
397 for (
size_t i = 0; i < nd; i++) {
400 << bin_to_value_scalar(ix[i], i);
403 for (
size_t imult = 0; imult < mult; imult++) {
406 << value_output(ix, imult);
412 os.flags(prev_flags);
420 std::string description)
const
422 int error_code = COLVARS_OK;
425 return COLVARS_FILE_ERROR;
438 os <<
"object 1 class gridpositions counts";
440 for (icv = 0; icv < num_variables(); icv++) {
441 os <<
" " << number_of_points(icv);
446 for (icv = 0; icv < num_variables(); icv++) {
447 os <<
" " << (lower_boundaries[icv].real_value + 0.5 * widths[icv]);
451 for (icv = 0; icv < num_variables(); icv++) {
453 for (
size_t icv2 = 0; icv2 < num_variables(); icv2++) {
454 if (icv == icv2) os <<
" " << widths[icv];
455 else os <<
" " << 0.0;
460 os <<
"object 2 class gridconnections counts";
461 for (icv = 0; icv < num_variables(); icv++) {
462 os <<
" " << number_of_points(icv);
466 os <<
"object 3 class array type double rank 0 items "
467 << number_of_points() <<
" data follows\n";
471 os <<
"object \"collective variables scalar field\" class field\n";
478 std::string description)
const
480 int error_code = COLVARS_OK;
483 return COLVARS_FILE_ERROR;
Grid of values of a function of several collective variables.
Definition: colvargrid.h:26
std::istream & read_restart(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:44
std::string get_state_params() const
Write the current grid parameters to a string.
Definition: colvargrid_def.h:146
std::ostream & write_raw(std::ostream &os, size_t const buf_size=3) const
Definition: colvargrid_def.h:113
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:811
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:829
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:790
size_t mult
Multiplicity of each datum (allow the binning of non-scalar types such as atomic gradients)
Definition: colvargrid.h:42
bool index_ok(std::vector< int > const &ix) const
Check that the index is within range in each of the dimensions.
Definition: colvargrid.h:818
bool has_data
Whether this grid has been filled with data or is still empty.
Definition: colvargrid.h:99
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:176
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid_def.h:435
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:100
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:371
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:252
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid_def.h:56
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:95
static void log(std::string const &message, int min_log_level=10)
Definition: colvarmodule.cpp:1969
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:2046
static size_t const cv_prec
Number of digits to represent a collective variables value(s)
Definition: colvarmodule.h:664
static colvarmodule * main()
Access the one instance of the Colvars module.
Definition: colvarmodule.cpp:185
static real sqrt(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:133
static size_t const cv_width
Number of characters to represent a collective variables value(s)
Definition: colvarmodule.h:666
static bool debug()
Whether debug output should be enabled (compile-time option)
Definition: colvarmodule.h:330
static real fabs(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:127
static colvarproxy * proxy
Pointer to the proxy object, used to retrieve atomic data from the hosting program; it is static in o...
Definition: colvarmodule.h:860
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2392
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
Definition: colvars_memstream.h:30
Collective variables main module.