diff --git a/lib/colvars/colvar.cpp b/lib/colvars/colvar.cpp index 5e90636e1d..fcd7d57f06 100644 --- a/lib/colvars/colvar.cpp +++ b/lib/colvars/colvar.cpp @@ -846,7 +846,22 @@ void colvar::calc() if (cvm::debug()) cvm::log("Collecting data from atom groups.\n"); + // Update the enabled/disabled status of cvcs if necessary + if (cvc_flags.size()) { + bool any = false; + for (i = 0; i < cvcs.size(); i++) { + cvcs[i]->b_enabled = cvc_flags[i]; + any = any || cvc_flags[i]; + } + if (!any) { + cvm::error("ERROR: All CVCs are disabled for colvar " + this->name +"\n"); + return; + } + cvc_flags.resize(0); + } + for (i = 0; i < cvcs.size(); i++) { + if (!cvcs[i]->b_enabled) continue; for (ig = 0; ig < cvcs[i]->atom_groups.size(); ig++) { cvm::atom_group &atoms = *(cvcs[i]->atom_groups[ig]); atoms.reset_atoms_data(); @@ -1220,12 +1235,13 @@ void colvar::communicate_forces() return; } + int grad_index = 0; // index in the scripted gradients, to account for some components being disabled for (i = 0; i < cvcs.size(); i++) { if (!cvcs[i]->b_enabled) continue; cvm::increase_depth(); // cvc force is colvar force times colvar/cvc Jacobian // (vector-matrix product) - (cvcs[i])->apply_force(colvarvalue(f.as_vector() * func_grads[i], + (cvcs[i])->apply_force(colvarvalue(f.as_vector() * func_grads[grad_index++], cvcs[i]->value().type())); cvm::decrease_depth(); } @@ -1258,20 +1274,13 @@ void colvar::communicate_forces() int colvar::set_cvc_flags(std::vector const &flags) { - size_t i; if (flags.size() != cvcs.size()) { cvm::error("ERROR: Wrong number of CVC flags provided."); return COLVARS_ERROR; } - bool e = false; - for (i = 0; i < cvcs.size(); i++) { - cvcs[i]->b_enabled = flags[i]; - e = e || flags[i]; - } - if (!e) { - cvm::error("ERROR: All CVCs are disabled for this colvar."); - return COLVARS_ERROR; - } + // We cannot enable or disable cvcs in the middle of a timestep or colvar evaluation sequence + // so we store the flags that will be enforced at the next call to calc() + cvc_flags = flags; return COLVARS_OK; } diff --git a/lib/colvars/colvar.h b/lib/colvars/colvar.h index 79b00c026b..8a3e40518a 100644 --- a/lib/colvars/colvar.h +++ b/lib/colvars/colvar.h @@ -514,6 +514,9 @@ protected: /// \brief Array of \link cvc \endlink objects std::vector cvcs; + /// \brief Flags to enable or disable cvcs at next colvar evaluation + std::vector cvc_flags; + /// \brief Initialize the sorted list of atom IDs for atoms involved /// in all cvcs (called when enabling task_collect_gradients) void build_atom_list(void); diff --git a/lib/colvars/colvargrid.cpp b/lib/colvars/colvargrid.cpp index 6cc667b51c..e2e660b984 100644 --- a/lib/colvars/colvargrid.cpp +++ b/lib/colvars/colvargrid.cpp @@ -24,31 +24,6 @@ colvar_grid_count::colvar_grid_count(std::vector &colvars, : colvar_grid(colvars, def_count, 1) {} -std::istream & colvar_grid_count::read_restart(std::istream &is) -{ - size_t const start_pos = is.tellg(); - std::string key, conf; - if ((is >> key) && (key == std::string("grid_parameters"))) { - is.seekg(start_pos, std::ios::beg); - is >> colvarparse::read_block("grid_parameters", conf); - parse_params(conf); - } else { - cvm::log("Grid parameters are missing in the restart file, using those from the configuration.\n"); - is.seekg(start_pos, std::ios::beg); - } - read_raw(is); - return is; -} - -std::ostream & colvar_grid_count::write_restart(std::ostream &os) -{ - write_params(os); - write_raw(os); - return os; -} - - - colvar_grid_scalar::colvar_grid_scalar() : colvar_grid(), samples(NULL), grad(NULL) {} @@ -79,30 +54,6 @@ colvar_grid_scalar::~colvar_grid_scalar() } } -std::istream & colvar_grid_scalar::read_restart(std::istream &is) -{ - size_t const start_pos = is.tellg(); - std::string key, conf; - if ((is >> key) && (key == std::string("grid_parameters"))) { - is.seekg(start_pos, std::ios::beg); - is >> colvarparse::read_block("grid_parameters", conf); - parse_params(conf); - } else { - cvm::log("Grid parameters are missing in the restart file, using those from the configuration.\n"); - is.seekg(start_pos, std::ios::beg); - } - read_raw(is); - return is; -} - -std::ostream & colvar_grid_scalar::write_restart(std::ostream &os) -{ - write_params(os); - write_raw(os); - return os; -} - - cvm::real colvar_grid_scalar::maximum_value() const { cvm::real max = data[0]; @@ -163,29 +114,6 @@ colvar_grid_gradient::colvar_grid_gradient(std::vector &colvars) : colvar_grid(colvars, 0.0, colvars.size()), samples(NULL) {} -std::istream & colvar_grid_gradient::read_restart(std::istream &is) -{ - size_t const start_pos = is.tellg(); - std::string key, conf; - if ((is >> key) && (key == std::string("grid_parameters"))) { - is.seekg(start_pos, std::ios::beg); - is >> colvarparse::read_block("grid_parameters", conf); - parse_params(conf); - } else { - cvm::log("Grid parameters are missing in the restart file, using those from the configuration.\n"); - is.seekg(start_pos, std::ios::beg); - } - read_raw(is); - return is; -} - -std::ostream & colvar_grid_gradient::write_restart(std::ostream &os) -{ - write_params(os); - write_raw(os); - return os; -} - void colvar_grid_gradient::write_1D_integral(std::ostream &os) { cvm::real bin, min, integral; diff --git a/lib/colvars/colvargrid.h b/lib/colvars/colvargrid.h index 29d21feb35..ac192ed0ee 100644 --- a/lib/colvars/colvargrid.h +++ b/lib/colvars/colvargrid.h @@ -836,6 +836,32 @@ public: } + /// \brief Read grid entry in restart file + std::istream & read_restart(std::istream &is) + { + size_t const start_pos = is.tellg(); + std::string key, conf; + if ((is >> key) && (key == std::string("grid_parameters"))) { + is.seekg(start_pos, std::ios::beg); + is >> colvarparse::read_block("grid_parameters", conf); + parse_params(conf); + } else { + cvm::log("Grid parameters are missing in the restart file, using those from the configuration.\n"); + is.seekg(start_pos, std::ios::beg); + } + read_raw(is); + return is; + } + + /// \brief Write grid entry in restart file + std::ostream & write_restart(std::ostream &os) + { + write_params(os); + write_raw(os); + return os; + } + + /// \brief Write the grid data without labels, as they are /// represented in memory /// \param buf_size Number of values per line @@ -1106,12 +1132,6 @@ public: return new_data[address(ix) + imult]; } - /// \brief Read the grid from a restart - std::istream & read_restart(std::istream &is); - - /// \brief Write the grid to a restart - std::ostream & write_restart(std::ostream &os); - /// \brief Get the value from a formatted output and transform it /// into the internal representation (it may have been rescaled or /// manipulated) @@ -1240,12 +1260,6 @@ public: has_data = true; } - /// \brief Read the grid from a restart - std::istream & read_restart(std::istream &is); - - /// \brief Write the grid to a restart - std::ostream & write_restart(std::ostream &os); - /// \brief Return the highest value cvm::real maximum_value() const; @@ -1343,12 +1357,6 @@ public: } - /// \brief Read the grid from a restart - std::istream & read_restart(std::istream &is); - - /// \brief Write the grid to a restart - std::ostream & write_restart(std::ostream &os); - /// Compute and return average value for a 1D gradient grid inline cvm::real average() { diff --git a/lib/colvars/colvarmodule.h b/lib/colvars/colvarmodule.h index e1d25b1d97..8fe92cea15 100644 --- a/lib/colvars/colvarmodule.h +++ b/lib/colvars/colvarmodule.h @@ -4,7 +4,7 @@ #define COLVARMODULE_H #ifndef COLVARS_VERSION -#define COLVARS_VERSION "2015-07-21" +#define COLVARS_VERSION "2015-08-12" #endif #ifndef COLVARS_DEBUG