12#ifndef COLVARGRID_DEF_H
13#define COLVARGRID_DEF_H
21#include "colvargrid.h"
22#include "colvars_memstream.h"
40if ( !(is >> hash) || (hash !=
"#") ) {
41 cvmodule->
error(
"Error reading grid at position "+
43 " in stream(read \"" + hash +
"\")\n");
48mult = (mult_i == 0) ?
nd : mult_i;
50std::vector<cvm::real> lower_in(
nd), widths_in(
nd);
51std::vector<int> nx_in(
nd);
52std::vector<int> periodic_in(
nd);
54for (i = 0; i <
nd; i++ ) {
55 if ( !(is >> hash) || (hash !=
"#") ) {
56 cvmodule->
error(
"Error reading grid at position "+
58 " in stream(read \"" + hash +
"\")\n");
62 is >> lower_in[i] >> widths_in[i] >> nx_in[i] >> periodic_in[i];
69for (i = 0; i <
nd; i++ ) {
71 periodic.push_back(
static_cast<bool>(periodic_in[i]));
85 auto const start_pos = is.tellg();
92 auto const error_pos = is.tellg();
95 is.setstate(std::ios::failbit);
96 cvmodule->error(
"Error: in reading grid state from stream at position " +
97 cvm::to_str(error_pos) +
"\n", COLVARS_INPUT_ERROR);
103 return read_restart_template_(is);
108 return read_restart_template_(is);
114 os <<
"grid_parameters {\n" << get_state_params() <<
"}\n";
122 os << std::string(
"grid_parameters") << get_state_params();
131 auto const start_pos = is.tellg();
133 for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix)) {
134 for (
size_t imult = 0; imult < mult; imult++) {
136 if (is >> new_value) {
137 value_input(ix, new_value, imult);
141 is.setstate(std::ios::failbit);
143 "Error: failed to read all of the grid points from file. Possible explanations: grid "
144 "parameters in the configuration (lowerBoundary, upperBoundary, width) are different "
145 "from those in the file, or the file is corrupt/incomplete.\n",
146 COLVARS_INPUT_ERROR);
159 return read_raw_template_(is);
165 return read_raw_template_(is);
172 auto const w = os.width();
173 auto const p = os.precision();
176 for (
auto ix = new_index(); index_ok(ix); incr(ix)) {
177 for (
size_t imult = 0; imult < mult; imult++) {
178 os <<
" " << std::setw(w) << std::setprecision(p) << value_output(ix, imult);
179 if (((++count) % buf_size) == 0)
184 if ((count % buf_size) != 0)
194 for (
auto ix = new_index(); index_ok(ix); incr(ix)) {
195 for (
size_t imult = 0; imult < mult; imult++) {
196 os << value_output(ix, imult);
205 std::ostringstream os;
207 os <<
" n_colvars " << nd <<
"\n";
209 os <<
" lower_boundaries ";
210 for (i = 0; i < nd; i++)
211 os <<
" " << lower_boundaries[i];
214 os <<
" upper_boundaries ";
215 for (i = 0; i < nd; i++)
216 os <<
" " << upper_boundaries[i];
220 for (i = 0; i < nd; i++)
221 os <<
" " << widths[i];
225 for (i = 0; i < nd; i++)
237 cvmodule->log(
"Reading grid configuration from string.\n");
239 std::vector<int> old_nx = nx;
240 std::vector<colvarvalue> old_lb = lower_boundaries;
241 std::vector<colvarvalue> old_ub = upper_boundaries;
242 std::vector<cvm::real> old_w = widths;
249 cvmodule->error(
"Error: trying to read data for a grid "
250 "that contains a different number of colvars ("+
254 return COLVARS_ERROR;
259 colvarparse::get_keyval(conf,
"lower_boundaries",
261 colvarparse::get_keyval(conf,
"upper_boundaries",
267 colvarparse::get_keyval(conf,
"lowerBoundary",
268 lower_boundaries, lower_boundaries, parse_mode);
269 colvarparse::get_keyval(conf,
"upperBoundary",
270 upper_boundaries, upper_boundaries, parse_mode);
272 colvarparse::get_keyval(conf,
"width", widths, widths, parse_mode);
277 if (nd < lower_boundaries.size()) nd = lower_boundaries.size();
279 if (! use_actual_value.size()) use_actual_value.assign(nd,
false);
280 if (! periodic.size()) periodic.assign(nd,
false);
281 if (! widths.size()) widths.assign(nd, 1.0);
285 bool new_params =
false;
287 for (
size_t i = 0; i < nd; i++) {
288 if (old_nx[i] != nx[i] ||
289 cvm::sqrt(cv[i]->dist2(old_lb[i], lower_boundaries[i])) > eps ||
290 cvm::sqrt(cv[i]->dist2(old_ub[i], upper_boundaries[i])) > eps ||
301 init_from_boundaries();
303 return this->setup(nx, T(), mult);
318 size_t n, periodic_flag;
320 std::vector<T> new_value;
321 std::vector<int> nx_read;
322 std::vector<int> bin;
324 if ( cv.size() > 0 && cv.size() != nd ) {
325 cvmodule->error(
"Cannot read grid file: number of variables in file differs from number referenced by grid.\n");
329 if ( !(is >> hash) || (hash !=
"#") ) {
330 cvmodule->error(
"Error reading grid at position "+
332 " in stream(read \"" + hash +
"\")\n", COLVARS_INPUT_ERROR);
338 cvmodule->error(
"Error reading grid: wrong number of collective variables.\n");
344 new_value.resize(mult);
346 if (this->has_parent_data && add) {
347 new_data.resize(data.size());
351 for (
size_t i = 0; i < nd; i++ ) {
352 if ( !(is >> hash) || (hash !=
"#") ) {
353 cvmodule->error(
"Error reading grid at position "+
355 " in stream(read \"" + hash +
"\")\n");
359 is >> lower >> width >> nx_read[i] >> periodic_flag;
362 if ( (
cvm::fabs(lower - lower_boundaries[i].real_value) > 1.0e-10) ||
363 (
cvm::fabs(width - widths[i] ) > 1.0e-10) ||
364 (nx_read[i] != nx[i]) ) {
365 cvmodule->log(
"Warning: reading from different grid definition (colvar "
366 +
cvm::to_str(i+1) +
"); remapping data on new grid.\n");
374 bool end_of_file =
false;
376 for (
size_t i = 0; i < nd; i++ ) {
377 if ( !(is >> x) ) end_of_file =
true;
378 bin[i] = value_to_bin_scalar(x, i);
381 wrap_detect_edge(bin);
383 if (end_of_file)
break;
385 for (
size_t imult = 0; imult < mult; imult++) {
386 is >> new_value[imult];
389 if ( index_ok(bin) ) {
390 for (
size_t imult = 0; imult < mult; imult++) {
391 value_input(bin, new_value[imult], imult, add);
397 for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
398 for (
size_t i = 0; i < nd; i++ ) {
401 for (
size_t imult = 0; imult < mult; imult++) {
402 is >> new_value[imult];
403 value_input(ix, new_value[imult], imult, add);
414 std::string description,
419 return COLVARS_FILE_ERROR;
425 return COLVARS_FILE_ERROR;
433 std::ios_base::fmtflags prev_flags(os.flags());
438 os << std::setw(2) <<
"# " << nd <<
"\n";
440 os.setf(std::ios::scientific, std::ios::floatfield);
441 for (
size_t i = 0; i < nd; i++) {
443 << std::setw(cvmodule->cv_width) << std::setprecision(cvmodule->cv_prec) << lower_boundaries[i] <<
" "
444 << std::setw(cvmodule->cv_width) << std::setprecision(cvmodule->cv_prec) << widths[i] <<
" "
445 << std::setw(10) << nx[i] <<
" "
446 << periodic[i] <<
"\n";
449 for (std::vector<int> ix = new_index(); index_ok(ix); incr(ix) ) {
451 if (ix.back() == 0) {
456 for (
size_t i = 0; i < nd; i++) {
458 << std::setw(cvmodule->cv_width) << std::setprecision(cvmodule->cv_prec)
459 << bin_to_value_scalar(ix[i], i);
462 for (
size_t imult = 0; imult < mult; imult++) {
464 << std::setw(cvmodule->cv_width) << std::setprecision(cvmodule->cv_prec)
465 << value_output(ix, imult);
471 os.flags(prev_flags);
479 std::string description)
const
481 int error_code = COLVARS_OK;
484 return COLVARS_FILE_ERROR;
497 os <<
"object 1 class gridpositions counts";
499 for (icv = 0; icv < num_variables(); icv++) {
500 os <<
" " << number_of_points(icv);
505 for (icv = 0; icv < num_variables(); icv++) {
506 os <<
" " << (lower_boundaries[icv].real_value + 0.5 * widths[icv]);
510 for (icv = 0; icv < num_variables(); icv++) {
512 for (
size_t icv2 = 0; icv2 < num_variables(); icv2++) {
513 if (icv == icv2) os <<
" " << widths[icv];
514 else os <<
" " << 0.0;
519 os <<
"object 2 class gridconnections counts";
520 for (icv = 0; icv < num_variables(); icv++) {
521 os <<
" " << number_of_points(icv);
525 os <<
"object 3 class array type double rank 0 items "
526 << number_of_points() <<
" data follows\n";
530 os <<
"object \"collective variables scalar field\" class field\n";
537 std::string description)
const
539 int error_code = COLVARS_OK;
542 return COLVARS_FILE_ERROR;
size_t nd
Number of dimensions.
Definition: colvargrid.h:28
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
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:102
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
size_t mult
Multiplicity of each datum (allow the binning of non-scalar types such as atomic gradients)
Definition: colvargrid.h:59
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
IST & read_restart_template_(IST &is)
Helper method for read_restart.
Definition: colvargrid_def.h:84
std::ostream & write_opendx(std::ostream &os) const
Write the grid data without labels, as they are represented in memory.
Definition: colvargrid_def.h:494
std::istream & read_raw(std::istream &is)
Read all grid parameters and data from a formatted stream.
Definition: colvargrid_def.h:157
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:430
IST & read_raw_template_(IST &is)
Helper method for read_raw.
Definition: colvargrid_def.h:129
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:94
colvar_grid()
Default constructor.
Definition: colvargrid.h:214
std::ostream & write_restart(std::ostream &os)
Write all grid parameters and data to a formatted stream.
Definition: colvargrid_def.h:112
Collective variables module (main class)
Definition: colvarmodule.h:72
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:99
int error(std::string const &message, int code=-1)
Print a message to the main log and set global error code.
Definition: colvarmodule.cpp:2186
static colvarmodule * main()
Access the main instance of the Colvars module.
Definition: colvarmodule.cpp:205
static real sqrt(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:143
static bool debug()
Whether debug output should be enabled (compile-time option)
Definition: colvarmodule.h:376
static real fabs(real const &x)
Reimplemented to work around MS compiler issues.
Definition: colvarmodule.h:137
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2541
colvarproxy * proxy
Pointer to the proxy object, used to retrieve atomic data from the hosting program.
Definition: colvarmodule.h:957
Definition: colvarparse.h:286
Base class containing parsing functions; all objects which need to parse input inherit from this.
Definition: colvarparse.h:27
Parse_Mode
How a keyword is parsed in a string.
Definition: colvarparse.h:63
@ parse_restart
The call is being executed from a read_restart() function.
Definition: colvarparse.h:80
@ parse_silent
Do not print the keyword.
Definition: colvarparse.h:73
virtual int close_output_stream(std::string const &output_name)
Closes the given output file/channel.
Definition: colvarproxy_io.cpp:474
std::istream & input_stream(std::string const &input_name, std::string const description="file/channel", bool error_on_fail=true)
Definition: colvarproxy_io.cpp:264
virtual std::ostream & output_stream(std::string const &output_name, std::string const description)
Definition: colvarproxy_io.cpp:407
int close_input_stream(std::string const &input_name)
Closes the given input stream.
Definition: colvarproxy_io.cpp:338
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.