2014-10-08 04:30:25 +08:00
|
|
|
/// -*- c++ -*-
|
|
|
|
|
2013-10-30 23:25:42 +08:00
|
|
|
#include <sstream>
|
2014-12-02 10:09:53 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
#include "colvarmodule.h"
|
|
|
|
#include "colvarparse.h"
|
|
|
|
#include "colvarproxy.h"
|
|
|
|
#include "colvar.h"
|
|
|
|
#include "colvarbias.h"
|
2015-04-30 22:09:42 +08:00
|
|
|
#include "colvarbias_abf.h"
|
2014-08-15 07:29:25 +08:00
|
|
|
#include "colvarbias_alb.h"
|
2015-04-30 22:09:42 +08:00
|
|
|
#include "colvarbias_histogram.h"
|
2012-05-24 00:20:04 +08:00
|
|
|
#include "colvarbias_meta.h"
|
2014-10-08 04:30:25 +08:00
|
|
|
#include "colvarbias_restraint.h"
|
|
|
|
#include "colvarscript.h"
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
colvarmodule::colvarmodule(colvarproxy *proxy_in)
|
2013-06-28 06:48:27 +08:00
|
|
|
{
|
2012-05-24 00:20:04 +08:00
|
|
|
// pointer to the proxy object
|
|
|
|
if (proxy == NULL) {
|
|
|
|
proxy = proxy_in;
|
|
|
|
parse = new colvarparse();
|
|
|
|
} else {
|
2014-10-08 04:30:25 +08:00
|
|
|
// TODO relax this error to handle multiple molecules in VMD
|
|
|
|
// once the module is not static anymore
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: trying to allocate the collective "
|
2015-02-20 02:20:52 +08:00
|
|
|
"variable module twice.\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
return;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::log("Initializing the collective variables module, version "+
|
2015-02-20 02:20:52 +08:00
|
|
|
cvm::to_str(COLVARS_VERSION)+".\n");
|
2016-04-14 06:25:46 +08:00
|
|
|
cvm::log("Please cite Fiorin et al, Mol Phys 2013 in any publication "
|
|
|
|
"based on this calculation.\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2016-05-12 21:52:42 +08:00
|
|
|
if (proxy->smp_enabled() == COLVARS_OK) {
|
|
|
|
cvm::log("SMP parallelism is available.\n");
|
|
|
|
}
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// set initial default values
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
// "it_restart" will be set by the input state file, if any;
|
|
|
|
// "it" should be updated by the proxy
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarmodule::it = colvarmodule::it_restart = 0;
|
|
|
|
colvarmodule::it_restart_from_state_file = true;
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarmodule::use_scripted_forces = false;
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarmodule::b_analysis = false;
|
2015-03-09 22:50:53 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarmodule::debug_gradients_step_size = 1.0e-07;
|
2015-03-09 22:50:53 +08:00
|
|
|
|
|
|
|
colvarmodule::rotation::monitor_crossings = false;
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarmodule::rotation::crossing_threshold = 1.0e-02;
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarmodule::cv_traj_freq = 100;
|
|
|
|
colvarmodule::restart_out_freq = proxy->restart_frequency();
|
2013-06-28 06:48:27 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// by default overwrite the existing trajectory file
|
|
|
|
colvarmodule::cv_traj_append = false;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2015-03-09 22:50:53 +08:00
|
|
|
int colvarmodule::read_config_file(char const *config_filename)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::log("Reading new configuration from file \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string(config_filename)+"\":\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// open the configfile
|
2014-12-02 10:09:53 +08:00
|
|
|
config_s.open(config_filename);
|
2015-02-20 02:20:52 +08:00
|
|
|
if (!config_s.is_open()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: in opening configuration file \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string(config_filename)+"\".\n",
|
|
|
|
FILE_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// read the config file into a string
|
|
|
|
std::string conf = "";
|
|
|
|
std::string line;
|
2014-12-02 10:09:53 +08:00
|
|
|
while (colvarparse::getline_nocomments(config_s, line)) {
|
|
|
|
conf.append(line+"\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
config_s.close();
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2015-03-09 22:50:53 +08:00
|
|
|
return parse_config(conf);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2015-03-09 22:50:53 +08:00
|
|
|
int colvarmodule::read_config_string(std::string const &config_str)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::log("Reading new configuration:\n");
|
|
|
|
std::istringstream config_s(config_str);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// strip the comments away
|
|
|
|
std::string conf = "";
|
|
|
|
std::string line;
|
2014-12-02 10:09:53 +08:00
|
|
|
while (colvarparse::getline_nocomments(config_s, line)) {
|
|
|
|
conf.append(line+"\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2015-03-09 22:50:53 +08:00
|
|
|
return parse_config(conf);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2015-03-09 22:50:53 +08:00
|
|
|
int colvarmodule::parse_config(std::string &conf)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
|
|
|
// parse global options
|
2016-03-02 04:40:25 +08:00
|
|
|
if (catch_input_errors(parse_global_params(conf))) {
|
|
|
|
return get_error();
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the options for collective variables
|
2016-03-02 04:40:25 +08:00
|
|
|
if (catch_input_errors(parse_colvars(conf))) {
|
|
|
|
return get_error();
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
// parse the options for biases
|
2016-03-02 04:40:25 +08:00
|
|
|
if (catch_input_errors(parse_biases(conf))) {
|
|
|
|
return get_error();
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// done parsing known keywords, check that all keywords found were valid ones
|
2016-03-02 04:40:25 +08:00
|
|
|
if (catch_input_errors(parse->check_keywords(conf, "colvarmodule"))) {
|
|
|
|
return get_error();
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::log("Collective variables module (re)initialized.\n");
|
|
|
|
cvm::log(cvm::line_marker);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2015-02-20 02:20:52 +08:00
|
|
|
if (cv_traj_os.is_open()) {
|
|
|
|
// configuration might have changed, better redo the labels
|
|
|
|
write_traj_label(cv_traj_os);
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2016-03-02 04:40:25 +08:00
|
|
|
return get_error();
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::parse_global_params(std::string const &conf)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2014-10-08 04:30:25 +08:00
|
|
|
std::string index_file_name;
|
2014-12-02 10:09:53 +08:00
|
|
|
if (parse->get_keyval(conf, "indexFile", index_file_name)) {
|
|
|
|
read_index_file(index_file_name.c_str());
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
parse->get_keyval(conf, "analysis", b_analysis, b_analysis);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
parse->get_keyval(conf, "debugGradientsStepSize", debug_gradients_step_size,
|
2015-02-20 02:20:52 +08:00
|
|
|
debug_gradients_step_size,
|
|
|
|
colvarparse::parse_silent);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2015-03-09 22:50:53 +08:00
|
|
|
parse->get_keyval(conf, "monitorEigenvalueCrossing",
|
|
|
|
colvarmodule::rotation::monitor_crossings,
|
|
|
|
colvarmodule::rotation::monitor_crossings,
|
|
|
|
colvarparse::parse_silent);
|
2014-12-02 10:09:53 +08:00
|
|
|
parse->get_keyval(conf, "eigenvalueCrossingThreshold",
|
2015-02-20 02:20:52 +08:00
|
|
|
colvarmodule::rotation::crossing_threshold,
|
|
|
|
colvarmodule::rotation::crossing_threshold,
|
|
|
|
colvarparse::parse_silent);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
parse->get_keyval(conf, "colvarsTrajFrequency", cv_traj_freq, cv_traj_freq);
|
|
|
|
parse->get_keyval(conf, "colvarsRestartFrequency",
|
2015-02-20 02:20:52 +08:00
|
|
|
restart_out_freq, restart_out_freq);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
// if this is true when initializing, it means
|
|
|
|
// we are continuing after a reset(): default to true
|
2014-12-02 10:09:53 +08:00
|
|
|
parse->get_keyval(conf, "colvarsTrajAppend", cv_traj_append, cv_traj_append);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
parse->get_keyval(conf, "scriptedColvarForces", use_scripted_forces, false);
|
|
|
|
|
|
|
|
parse->get_keyval(conf, "scriptingAfterBiases", scripting_after_biases, true);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
if (use_scripted_forces && !proxy->force_script_defined) {
|
2016-03-02 04:40:25 +08:00
|
|
|
cvm::error("User script for scripted colvar forces not found.", INPUT_ERROR);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::parse_colvars(std::string const &conf)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
|
|
|
if (cvm::debug())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Initializing the collective variables.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
std::string colvar_conf = "";
|
|
|
|
size_t pos = 0;
|
2014-12-02 10:09:53 +08:00
|
|
|
while (parse->key_lookup(conf, "colvar", colvar_conf, pos)) {
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
if (colvar_conf.size()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::increase_depth();
|
2014-12-02 10:09:53 +08:00
|
|
|
colvars.push_back(new colvar(colvar_conf));
|
2014-10-29 03:53:17 +08:00
|
|
|
if (cvm::get_error() ||
|
2014-12-02 10:09:53 +08:00
|
|
|
((colvars.back())->check_keywords(colvar_conf, "colvar") != COLVARS_OK)) {
|
2015-02-20 02:20:52 +08:00
|
|
|
cvm::log("Error while constructing colvar number " +
|
|
|
|
cvm::to_str(colvars.size()) + " : deleting.");
|
2014-10-29 03:53:17 +08:00
|
|
|
delete colvars.back(); // the colvar destructor updates the colvars array
|
2014-10-08 04:30:25 +08:00
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
} else {
|
2014-10-08 04:30:25 +08:00
|
|
|
cvm::error("Error: \"colvar\" keyword found without any configuration.\n", INPUT_ERROR);
|
|
|
|
return COLVARS_ERROR;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2014-10-29 03:53:17 +08:00
|
|
|
cvm::decrease_depth();
|
2012-05-24 00:20:04 +08:00
|
|
|
colvar_conf = "";
|
|
|
|
}
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
if (!colvars.size()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Warning: no collective variables defined.\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
if (colvars.size())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::log("Collective variables initialized, "+
|
2015-02-20 02:20:52 +08:00
|
|
|
cvm::to_str(colvars.size())+
|
|
|
|
" in total.\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2014-10-29 03:53:17 +08:00
|
|
|
bool colvarmodule::check_new_bias(std::string &conf, char const *key)
|
|
|
|
{
|
|
|
|
if (cvm::get_error() ||
|
2015-02-20 02:20:52 +08:00
|
|
|
(biases.back()->check_keywords(conf, key) != COLVARS_OK)) {
|
2014-10-29 03:53:17 +08:00
|
|
|
cvm::log("Error while constructing bias number " +
|
2015-02-20 02:20:52 +08:00
|
|
|
cvm::to_str(biases.size()) + " : deleting.\n");
|
2014-10-29 03:53:17 +08:00
|
|
|
delete biases.back(); // the bias destructor updates the biases array
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
template <class bias_type>
|
|
|
|
int colvarmodule::parse_biases_type(std::string const &conf,
|
|
|
|
char const *keyword,
|
|
|
|
size_t &bias_count)
|
|
|
|
{
|
|
|
|
std::string bias_conf = "";
|
|
|
|
size_t conf_saved_pos = 0;
|
|
|
|
while (parse->key_lookup(conf, keyword, bias_conf, conf_saved_pos)) {
|
|
|
|
if (bias_conf.size()) {
|
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::increase_depth();
|
|
|
|
biases.push_back(new bias_type(bias_conf, keyword));
|
|
|
|
if (cvm::check_new_bias(bias_conf, keyword)) {
|
2014-10-08 04:30:25 +08:00
|
|
|
return COLVARS_ERROR;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
bias_count++;
|
|
|
|
} else {
|
|
|
|
cvm::error("Error: keyword \""+std::string(keyword)+"\" found without configuration.\n",
|
|
|
|
INPUT_ERROR);
|
|
|
|
return COLVARS_ERROR;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
bias_conf = "";
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
return COLVARS_OK;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::parse_biases(std::string const &conf)
|
|
|
|
{
|
|
|
|
if (cvm::debug())
|
|
|
|
cvm::log("Initializing the collective variables biases.\n");
|
2014-08-15 07:29:25 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
/// initialize ABF instances
|
|
|
|
parse_biases_type<colvarbias_abf>(conf, "abf", n_abf_biases);
|
2014-08-15 07:29:25 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
/// initialize adaptive linear biases
|
|
|
|
parse_biases_type<colvarbias_alb>(conf, "ALB", n_rest_biases);
|
2014-08-15 07:29:25 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
/// initialize harmonic restraints
|
|
|
|
parse_biases_type<colvarbias_restraint_harmonic>(conf, "harmonic", n_rest_biases);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
/// initialize histograms
|
|
|
|
parse_biases_type<colvarbias_histogram>(conf, "histogram", n_histo_biases);
|
|
|
|
|
|
|
|
/// initialize linear restraints
|
|
|
|
parse_biases_type<colvarbias_restraint_linear>(conf, "linear", n_rest_biases);
|
|
|
|
|
|
|
|
/// initialize metadynamics instances
|
|
|
|
parse_biases_type<colvarbias_meta>(conf, "metadynamics", n_meta_biases);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
if (use_scripted_forces) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
2014-10-08 04:30:25 +08:00
|
|
|
cvm::increase_depth();
|
|
|
|
cvm::log("User forces script will be run at each bias update.");
|
|
|
|
cvm::decrease_depth();
|
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
for (size_t i = 0; i < biases.size(); i++) {
|
|
|
|
biases[i]->enable(cvm::deps::f_cvb_active);
|
|
|
|
if (cvm::debug())
|
|
|
|
biases[i]->print_state();
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
if (biases.size() || use_scripted_forces) {
|
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::log("Collective variables biases initialized, "+
|
|
|
|
cvm::to_str(biases.size())+" in total.\n");
|
|
|
|
} else {
|
|
|
|
if (!use_scripted_forces) {
|
|
|
|
cvm::log("No collective variables biases were defined.\n");
|
|
|
|
}
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-02 04:40:25 +08:00
|
|
|
int colvarmodule::catch_input_errors(int result)
|
|
|
|
{
|
|
|
|
if (result != COLVARS_OK || get_error()) {
|
2016-04-28 22:48:56 +08:00
|
|
|
set_error_bit(result);
|
|
|
|
set_error_bit(INPUT_ERROR);
|
2016-03-02 04:40:25 +08:00
|
|
|
parse->reset();
|
|
|
|
return get_error();
|
|
|
|
}
|
|
|
|
return COLVARS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarbias * colvarmodule::bias_by_name(std::string const &name) {
|
2012-05-24 00:20:04 +08:00
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
2014-10-08 04:30:25 +08:00
|
|
|
if ((*bi)->name == name) {
|
|
|
|
return (*bi);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
return NULL;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 23:25:42 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
colvar *colvarmodule::colvar_by_name(std::string const &name) {
|
2013-10-30 23:25:42 +08:00
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
2014-10-08 04:30:25 +08:00
|
|
|
if ((*cvi)->name == name) {
|
|
|
|
return (*cvi);
|
2013-10-30 23:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::change_configuration(std::string const &bias_name,
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string const &conf)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
|
|
|
// This is deprecated; supported strategy is to delete the bias
|
|
|
|
// and parse the new config
|
|
|
|
cvm::increase_depth();
|
|
|
|
colvarbias *b;
|
2014-12-02 10:09:53 +08:00
|
|
|
b = bias_by_name(bias_name);
|
|
|
|
if (b == NULL) { cvm::error("Error: bias not found: " + bias_name); }
|
|
|
|
b->change_configuration(conf);
|
2014-10-08 04:30:25 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string colvarmodule::read_colvar(std::string const &name)
|
|
|
|
{
|
|
|
|
cvm::increase_depth();
|
|
|
|
colvar *c;
|
|
|
|
std::stringstream ss;
|
2014-12-02 10:09:53 +08:00
|
|
|
c = colvar_by_name(name);
|
|
|
|
if (c == NULL) { cvm::fatal_error("Error: colvar not found: " + name); }
|
2014-10-08 04:30:25 +08:00
|
|
|
ss << c->value();
|
2013-10-30 23:25:42 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::real colvarmodule::energy_difference(std::string const &bias_name,
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string const &conf)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
|
|
|
cvm::increase_depth();
|
2014-10-08 04:30:25 +08:00
|
|
|
colvarbias *b;
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::real energy_diff = 0.;
|
2014-12-02 10:09:53 +08:00
|
|
|
b = bias_by_name(bias_name);
|
|
|
|
if (b == NULL) { cvm::fatal_error("Error: bias not found: " + bias_name); }
|
|
|
|
energy_diff = b->energy_difference(conf);
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
return energy_diff;
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::bias_current_bin(std::string const &bias_name)
|
2014-10-29 03:53:17 +08:00
|
|
|
{
|
|
|
|
cvm::increase_depth();
|
2014-12-02 10:09:53 +08:00
|
|
|
int ret;
|
|
|
|
colvarbias *b = bias_by_name(bias_name);
|
2014-10-29 03:53:17 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
if (b != NULL) {
|
|
|
|
ret = b->current_bin();
|
2014-10-29 03:53:17 +08:00
|
|
|
} else {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: bias not found.\n");
|
|
|
|
ret = COLVARS_ERROR;
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
|
|
|
|
cvm::decrease_depth();
|
2014-10-29 03:53:17 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::bias_bin_num(std::string const &bias_name)
|
2014-10-29 03:53:17 +08:00
|
|
|
{
|
|
|
|
cvm::increase_depth();
|
2014-12-02 10:09:53 +08:00
|
|
|
int ret;
|
|
|
|
colvarbias *b = bias_by_name(bias_name);
|
2014-10-29 03:53:17 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
if (b != NULL) {
|
|
|
|
ret = b->bin_num();
|
2014-10-29 03:53:17 +08:00
|
|
|
} else {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: bias not found.\n");
|
|
|
|
ret = COLVARS_ERROR;
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
|
|
|
|
cvm::decrease_depth();
|
2014-10-29 03:53:17 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::bias_bin_count(std::string const &bias_name, size_t bin_index)
|
2014-10-29 03:53:17 +08:00
|
|
|
{
|
|
|
|
cvm::increase_depth();
|
2014-12-02 10:09:53 +08:00
|
|
|
int ret;
|
|
|
|
colvarbias *b = bias_by_name(bias_name);
|
2014-10-29 03:53:17 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
if (b != NULL) {
|
|
|
|
ret = b->bin_count(bin_index);
|
2014-10-29 03:53:17 +08:00
|
|
|
} else {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: bias not found.\n");
|
|
|
|
ret = COLVARS_ERROR;
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
|
|
|
|
cvm::decrease_depth();
|
2014-10-29 03:53:17 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::bias_share(std::string const &bias_name)
|
2014-10-29 03:53:17 +08:00
|
|
|
{
|
|
|
|
cvm::increase_depth();
|
2014-12-02 10:09:53 +08:00
|
|
|
int ret;
|
|
|
|
colvarbias *b = bias_by_name(bias_name);
|
2014-10-29 03:53:17 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
if (b != NULL) {
|
|
|
|
b->replica_share();
|
|
|
|
ret = COLVARS_OK;
|
|
|
|
} else {
|
|
|
|
cvm::error("Error: bias not found.\n");
|
|
|
|
ret = COLVARS_ERROR;
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
|
2014-10-29 03:53:17 +08:00
|
|
|
cvm::decrease_depth();
|
2014-12-02 10:09:53 +08:00
|
|
|
return ret;
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
int colvarmodule::calc()
|
|
|
|
{
|
|
|
|
int error_code = COLVARS_OK;
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
if (cvm::debug()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
|
|
|
cvm::log("Collective variables module, step no. "+
|
2015-02-20 02:20:52 +08:00
|
|
|
cvm::to_str(cvm::step_absolute())+"\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, calc_colvars());
|
2016-04-16 00:07:01 +08:00
|
|
|
// set biasing forces to zero before biases are calculated and summed over
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end(); cvi++) {
|
|
|
|
(*cvi)->reset_bias_force();
|
|
|
|
}
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, calc_biases());
|
|
|
|
combine_errors(error_code, update_colvar_forces());
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
if (cvm::b_analysis) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, analyze());
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// write trajectory files, if needed
|
|
|
|
if (cv_traj_freq && cv_traj_name.size()) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, write_traj_files());
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// write restart files, if needed
|
|
|
|
if (restart_out_freq && restart_out_name.size()) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, write_restart_files());
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return error_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int colvarmodule::calc_colvars()
|
|
|
|
{
|
2013-06-28 06:48:27 +08:00
|
|
|
if (cvm::debug())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Calculating collective variables.\n");
|
2016-04-16 00:07:01 +08:00
|
|
|
// calculate collective variables and their gradients
|
|
|
|
|
|
|
|
int error_code = COLVARS_OK;
|
|
|
|
std::vector<colvar *>::iterator cvi;
|
|
|
|
|
2016-06-02 22:03:00 +08:00
|
|
|
// Determine which colvars are active at this time step
|
|
|
|
for (cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
|
|
|
|
(*cvi)->feature_states[cvm::deps::f_cv_active]->enabled = (step_absolute() % (*cvi)->get_time_step_factor() == 0);
|
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
// if SMP support is available, split up the work
|
|
|
|
if (proxy->smp_enabled() == COLVARS_OK) {
|
|
|
|
|
|
|
|
// first, calculate how much work (currently, how many active CVCs) each colvar has
|
|
|
|
|
|
|
|
colvars_smp.resize(0);
|
|
|
|
colvars_smp_items.resize(0);
|
|
|
|
|
|
|
|
colvars_smp.reserve(colvars.size());
|
|
|
|
colvars_smp_items.reserve(colvars.size());
|
|
|
|
|
|
|
|
// set up a vector containing all components
|
|
|
|
size_t num_colvar_items = 0;
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
|
|
|
|
|
2016-06-02 22:03:00 +08:00
|
|
|
if (!(*cvi)->is_enabled()) continue;
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, (*cvi)->update_cvc_flags());
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
size_t num_items = (*cvi)->num_active_cvcs();
|
|
|
|
colvars_smp.reserve(colvars_smp.size() + num_items);
|
|
|
|
colvars_smp_items.reserve(colvars_smp_items.size() + num_items);
|
|
|
|
for (size_t icvc = 0; icvc < num_items; icvc++) {
|
|
|
|
colvars_smp.push_back(*cvi);
|
|
|
|
colvars_smp_items.push_back(icvc);
|
|
|
|
}
|
|
|
|
|
|
|
|
num_colvar_items += num_items;
|
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
|
|
|
|
|
|
|
// calculate colvar components in parallel
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, proxy->smp_colvars_loop());
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
|
2016-06-02 22:03:00 +08:00
|
|
|
if (!(*cvi)->is_enabled()) continue;
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, (*cvi)->collect_cvc_data());
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// calculate colvars one at a time
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
|
2016-06-02 22:03:00 +08:00
|
|
|
if (!(*cvi)->is_enabled()) continue;
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, (*cvi)->calc());
|
2016-04-16 00:07:01 +08:00
|
|
|
if (cvm::get_error()) {
|
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
cvm::decrease_depth();
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, cvm::get_error());
|
|
|
|
return error_code;
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int colvarmodule::calc_biases()
|
|
|
|
{
|
2012-05-24 00:20:04 +08:00
|
|
|
// update the biases and communicate their forces to the collective
|
|
|
|
// variables
|
|
|
|
if (cvm::debug() && biases.size())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Updating collective variable biases.\n");
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
std::vector<colvarbias *>::iterator bi;
|
|
|
|
int error_code = COLVARS_OK;
|
|
|
|
|
|
|
|
// if SMP support is available, split up the work
|
|
|
|
if (proxy->smp_enabled() == COLVARS_OK) {
|
|
|
|
|
|
|
|
if (use_scripted_forces && !scripting_after_biases) {
|
|
|
|
// calculate biases and scripted forces in parallel
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, proxy->smp_biases_script_loop());
|
2016-04-16 00:07:01 +08:00
|
|
|
} else {
|
|
|
|
// calculate biases in parallel
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, proxy->smp_biases_loop());
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (use_scripted_forces && !scripting_after_biases) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, calc_scripted_forces());
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (bi = biases.begin(); bi != biases.end(); bi++) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, (*bi)->update());
|
2016-04-16 00:07:01 +08:00
|
|
|
if (cvm::get_error()) {
|
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
cvm::decrease_depth();
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
cvm::real total_bias_energy = 0.0;
|
|
|
|
for (bi = biases.begin(); bi != biases.end(); bi++) {
|
|
|
|
total_bias_energy += (*bi)->get_energy();
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy->add_energy(total_bias_energy);
|
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int colvarmodule::update_colvar_forces()
|
|
|
|
{
|
|
|
|
int error_code = COLVARS_OK;
|
|
|
|
|
|
|
|
std::vector<colvar *>::iterator cvi;
|
|
|
|
std::vector<colvarbias *>::iterator bi;
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
// sum the forces from all biases for each collective variable
|
|
|
|
if (cvm::debug() && biases.size())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Collecting forces from all biases.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::increase_depth();
|
2014-10-08 04:30:25 +08:00
|
|
|
for (bi = biases.begin(); bi != biases.end(); bi++) {
|
2013-06-28 06:48:27 +08:00
|
|
|
(*bi)->communicate_forces();
|
2014-10-08 04:30:25 +08:00
|
|
|
if (cvm::get_error()) {
|
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
if (use_scripted_forces && scripting_after_biases) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, calc_scripted_forces());
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
cvm::real total_colvar_energy = 0.0;
|
2014-10-08 04:30:25 +08:00
|
|
|
// sum up the forces for each colvar, including wall forces
|
|
|
|
// and integrate any internal
|
|
|
|
// equation of motion (extended system)
|
2012-05-24 00:20:04 +08:00
|
|
|
if (cvm::debug())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Updating the internal degrees of freedom "
|
2015-02-20 02:20:52 +08:00
|
|
|
"of colvars (if they have any).\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::increase_depth();
|
2014-10-08 04:30:25 +08:00
|
|
|
for (cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
|
2016-06-02 22:03:00 +08:00
|
|
|
// Here we call even inactive colvars, so they accumulate biasing forces
|
|
|
|
// as well as update their extended-system dynamics
|
2016-04-16 00:07:01 +08:00
|
|
|
total_colvar_energy += (*cvi)->update_forces_energy();
|
2014-10-08 04:30:25 +08:00
|
|
|
if (cvm::get_error()) {
|
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
2016-04-16 00:07:01 +08:00
|
|
|
proxy->add_energy(total_colvar_energy);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
// make collective variables communicate their forces to their
|
|
|
|
// coupled degrees of freedom (i.e. atoms)
|
|
|
|
if (cvm::debug())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Communicating forces from the colvars to the atoms.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::increase_depth();
|
2014-10-08 04:30:25 +08:00
|
|
|
for (cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
|
2016-04-16 00:07:01 +08:00
|
|
|
if ((*cvi)->is_enabled(cvm::deps::f_cv_gradient)) {
|
2016-06-02 22:03:00 +08:00
|
|
|
if (!(*cvi)->is_enabled()) continue;
|
2013-06-28 06:48:27 +08:00
|
|
|
(*cvi)->communicate_forces();
|
2014-10-08 04:30:25 +08:00
|
|
|
if (cvm::get_error()) {
|
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int colvarmodule::calc_scripted_forces()
|
|
|
|
{
|
|
|
|
// Run user force script, if provided,
|
|
|
|
// potentially adding scripted forces to the colvars
|
|
|
|
int res;
|
|
|
|
res = proxy->run_force_callback();
|
|
|
|
if (res == COLVARS_NOT_IMPLEMENTED) {
|
|
|
|
cvm::error("Colvar forces scripts are not implemented.");
|
|
|
|
return COLVARS_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
if (res != COLVARS_OK) {
|
|
|
|
cvm::error("Error running user colvar forces script");
|
|
|
|
return COLVARS_ERROR;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
return COLVARS_OK;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
int colvarmodule::write_restart_files()
|
|
|
|
{
|
|
|
|
if ( (cvm::step_relative() > 0) &&
|
|
|
|
((cvm::step_absolute() % restart_out_freq) == 0) ) {
|
|
|
|
cvm::log("Writing the state file \""+
|
|
|
|
restart_out_name+"\".\n");
|
|
|
|
proxy->backup_file(restart_out_name.c_str());
|
|
|
|
restart_out_os.open(restart_out_name.c_str());
|
|
|
|
if (!restart_out_os.is_open() || !write_restart(restart_out_os))
|
|
|
|
cvm::error("Error: in writing restart file.\n");
|
|
|
|
restart_out_os.close();
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
int colvarmodule::write_traj_files()
|
|
|
|
{
|
|
|
|
if (!cv_traj_os.is_open()) {
|
|
|
|
open_traj_file(cv_traj_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write labels in the traj file every 1000 lines and at first timestep
|
|
|
|
if ((cvm::step_absolute() % (cv_traj_freq * 1000)) == 0 || cvm::step_relative() == 0) {
|
|
|
|
write_traj_label(cv_traj_os);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cvm::step_absolute() % cv_traj_freq) == 0) {
|
|
|
|
write_traj(cv_traj_os);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restart_out_freq && cv_traj_os.is_open()) {
|
|
|
|
// flush the trajectory file if we are at the restart frequency
|
|
|
|
if ( (cvm::step_relative() > 0) &&
|
|
|
|
((cvm::step_absolute() % restart_out_freq) == 0) ) {
|
|
|
|
cvm::log("Synchronizing (emptying the buffer of) trajectory file \""+
|
|
|
|
cv_traj_name+"\".\n");
|
|
|
|
cv_traj_os.flush();
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
int colvarmodule::analyze()
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
|
|
|
if (cvm::debug()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("colvarmodule::analyze(), step = "+cvm::to_str(it)+".\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cvm::step_relative() == 0)
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Performing analysis.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
// perform colvar-specific analysis
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
|
|
|
cvm::increase_depth();
|
2015-04-30 22:09:42 +08:00
|
|
|
(*cvi)->analyze();
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform bias-specific analysis
|
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
|
|
|
cvm::increase_depth();
|
2015-04-30 22:09:42 +08:00
|
|
|
(*bi)->analyze();
|
2012-05-24 00:20:04 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
}
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
int colvarmodule::setup()
|
2013-06-28 06:48:27 +08:00
|
|
|
{
|
|
|
|
// loop over all components of all colvars to reset masses of all groups
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end(); cvi++) {
|
|
|
|
(*cvi)->setup();
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2013-06-28 06:48:27 +08:00
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
colvarmodule::~colvarmodule()
|
|
|
|
{
|
2016-04-16 00:07:01 +08:00
|
|
|
if ((proxy->smp_thread_id() == COLVARS_NOT_IMPLEMENTED) ||
|
|
|
|
(proxy->smp_thread_id() == 0)) {
|
|
|
|
reset();
|
|
|
|
delete parse;
|
|
|
|
proxy = NULL;
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
int colvarmodule::reset()
|
|
|
|
{
|
2016-03-02 04:40:25 +08:00
|
|
|
parse->reset();
|
|
|
|
|
2014-10-29 03:53:17 +08:00
|
|
|
cvm::log("Resetting the Collective Variables Module.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-10-29 03:53:17 +08:00
|
|
|
// Iterate backwards because we are deleting the elements as we go
|
|
|
|
for (std::vector<colvarbias *>::reverse_iterator bi = biases.rbegin();
|
|
|
|
bi != biases.rend();
|
2012-05-24 00:20:04 +08:00
|
|
|
bi++) {
|
2014-10-29 03:53:17 +08:00
|
|
|
delete *bi; // the bias destructor updates the biases array
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
biases.clear();
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
// Iterate backwards because we are deleting the elements as we go
|
|
|
|
for (std::vector<colvar *>::reverse_iterator cvi = colvars.rbegin();
|
|
|
|
cvi != colvars.rend();
|
|
|
|
cvi++) {
|
|
|
|
delete *cvi; // the colvar destructor updates the colvars array
|
|
|
|
}
|
|
|
|
colvars.clear();
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
index_groups.clear();
|
|
|
|
index_group_names.clear();
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2015-04-06 23:14:53 +08:00
|
|
|
if (cv_traj_os.is_open()) {
|
|
|
|
// Do not close file here, as we might not be done with it yet.
|
|
|
|
cv_traj_os.flush();
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2013-06-28 06:48:27 +08:00
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
int colvarmodule::setup_input()
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2014-10-08 04:30:25 +08:00
|
|
|
// name of input state file
|
|
|
|
restart_in_name = proxy->input_prefix().size() ?
|
2014-12-02 10:09:53 +08:00
|
|
|
std::string(proxy->input_prefix()+".colvars.state") :
|
|
|
|
std::string("") ;
|
2012-10-24 00:57:29 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// read the restart configuration, if available
|
|
|
|
if (restart_in_name.size()) {
|
|
|
|
// read the restart file
|
2014-12-02 10:09:53 +08:00
|
|
|
std::ifstream input_is(restart_in_name.c_str());
|
2014-10-08 04:30:25 +08:00
|
|
|
if (!input_is.good()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: in opening restart file \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string(restart_in_name)+"\".\n",
|
|
|
|
FILE_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
return COLVARS_ERROR;
|
|
|
|
} else {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Restarting from file \""+restart_in_name+"\".\n");
|
|
|
|
read_restart(input_is);
|
2014-10-08 04:30:25 +08:00
|
|
|
if (cvm::get_error() != COLVARS_OK) {
|
|
|
|
return COLVARS_ERROR;
|
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(cvm::line_marker);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2012-10-24 00:57:29 +08:00
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2015-04-30 22:09:42 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int colvarmodule::setup_output()
|
|
|
|
{
|
2015-04-30 22:09:42 +08:00
|
|
|
int error_code = 0;
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// output state file (restart)
|
|
|
|
restart_out_name = proxy->restart_output_prefix().size() ?
|
2014-12-02 10:09:53 +08:00
|
|
|
std::string(proxy->restart_output_prefix()+".colvars.state") :
|
|
|
|
std::string("");
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
if (restart_out_name.size()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("The restart output state file will be \""+restart_out_name+"\".\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
output_prefix = proxy->output_prefix();
|
|
|
|
if (output_prefix.size()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("The final output state file will be \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
(output_prefix.size() ?
|
|
|
|
std::string(output_prefix+".colvars.state") :
|
|
|
|
std::string("colvars.state"))+"\".\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
// cvm::log (cvm::line_marker);
|
|
|
|
}
|
|
|
|
|
|
|
|
cv_traj_name =
|
|
|
|
(output_prefix.size() ?
|
2014-12-02 10:09:53 +08:00
|
|
|
std::string(output_prefix+".colvars.traj") :
|
|
|
|
std::string(""));
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
if (cv_traj_freq && cv_traj_name.size()) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, open_traj_file(cv_traj_name));
|
2015-04-30 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
2016-04-28 22:48:56 +08:00
|
|
|
combine_errors(error_code, (*bi)->setup_output());
|
2015-04-30 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error_code != COLVARS_OK || cvm::get_error()) {
|
2016-04-28 22:48:56 +08:00
|
|
|
set_error_bit(FILE_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
std::istream & colvarmodule::read_restart(std::istream &is)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
|
|
|
{
|
|
|
|
// read global restart information
|
|
|
|
std::string restart_conf;
|
2014-12-02 10:09:53 +08:00
|
|
|
if (is >> colvarparse::read_block("configuration", restart_conf)) {
|
2014-10-08 04:30:25 +08:00
|
|
|
if (it_restart_from_state_file) {
|
2014-12-02 10:09:53 +08:00
|
|
|
parse->get_keyval(restart_conf, "step",
|
2015-02-20 02:20:52 +08:00
|
|
|
it_restart, (size_t) 0,
|
|
|
|
colvarparse::parse_silent);
|
2014-10-08 04:30:25 +08:00
|
|
|
it = it_restart;
|
|
|
|
}
|
2016-04-28 22:48:56 +08:00
|
|
|
std::string restart_version;
|
|
|
|
parse->get_keyval(restart_conf, "version",
|
|
|
|
restart_version, std::string(""),
|
|
|
|
colvarparse::parse_silent);
|
|
|
|
if (restart_version.size() && (restart_version != std::string(COLVARS_VERSION))) {
|
|
|
|
cvm::log("This state file was generated with version "+restart_version+"\n");
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
is.clear();
|
2015-07-22 22:36:59 +08:00
|
|
|
parse->clear_keyword_registry();
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// colvars restart
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
if ( !((*cvi)->read_restart(is)) ) {
|
|
|
|
cvm::error("Error: in reading restart configuration for collective variable \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
(*cvi)->name+"\".\n",
|
|
|
|
INPUT_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// biases restart
|
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
2015-02-20 02:20:52 +08:00
|
|
|
if (!((*bi)->read_restart(is))) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: in reading restart configuration for bias \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
(*bi)->name+"\".\n",
|
|
|
|
INPUT_ERROR);
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
|
|
|
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::backup_file(char const *filename)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
return proxy->backup_file(filename);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int colvarmodule::write_output_files()
|
|
|
|
{
|
|
|
|
// if this is a simulation run (i.e. not a postprocessing), output data
|
|
|
|
// must be written to be able to restart the simulation
|
|
|
|
std::string const out_name =
|
|
|
|
(output_prefix.size() ?
|
2014-12-02 10:09:53 +08:00
|
|
|
std::string(output_prefix+".colvars.state") :
|
|
|
|
std::string("colvars.state"));
|
|
|
|
cvm::log("Saving collective variables state to \""+out_name+"\".\n");
|
2015-02-20 02:20:52 +08:00
|
|
|
|
|
|
|
std::ostream * os = proxy->output_stream(out_name);
|
|
|
|
os->setf(std::ios::scientific, std::ios::floatfield);
|
|
|
|
this->write_restart(*os);
|
|
|
|
proxy->close_output_stream(out_name);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
|
|
|
(*cvi)->write_output_files();
|
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
2013-06-28 06:48:27 +08:00
|
|
|
|
2015-04-30 22:09:42 +08:00
|
|
|
cvm::increase_depth();
|
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
|
|
|
(*bi)->write_output_files();
|
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
|
|
|
|
2015-04-06 23:14:53 +08:00
|
|
|
if (cv_traj_os.is_open()) {
|
|
|
|
// do not close to avoid problems with multiple NAMD runs
|
|
|
|
cv_traj_os.flush();
|
|
|
|
}
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::read_traj(char const *traj_filename,
|
2015-04-30 22:09:42 +08:00
|
|
|
long traj_read_begin,
|
|
|
|
long traj_read_end)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Opening trajectory file \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string(traj_filename)+"\".\n");
|
2014-12-02 10:09:53 +08:00
|
|
|
std::ifstream traj_is(traj_filename);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
while (true) {
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
std::string line("");
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
do {
|
2014-12-02 10:09:53 +08:00
|
|
|
if (!colvarparse::getline_nocomments(traj_is, line)) {
|
|
|
|
cvm::log("End of file \""+std::string(traj_filename)+
|
2015-02-20 02:20:52 +08:00
|
|
|
"\" reached, or corrupted file.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
traj_is.close();
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
} while (line.find_first_not_of(colvarparse::white_space) == std::string::npos);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
std::istringstream is(line);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
if (!(is >> it)) return false;
|
|
|
|
|
|
|
|
if ( (it < traj_read_begin) ) {
|
|
|
|
|
|
|
|
if ((it % 1000) == 0)
|
|
|
|
std::cerr << "Skipping trajectory step " << it
|
|
|
|
<< " \r";
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2013-06-28 06:48:27 +08:00
|
|
|
} else {
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
if ((it % 1000) == 0)
|
|
|
|
std::cerr << "Reading from trajectory, step = " << it
|
|
|
|
<< " \r";
|
|
|
|
|
|
|
|
if ( (traj_read_end > traj_read_begin) &&
|
|
|
|
(it > traj_read_end) ) {
|
|
|
|
std::cerr << "\n";
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Reached the end of the trajectory, "
|
2015-02-20 02:20:52 +08:00
|
|
|
"read_end = "+cvm::to_str(traj_read_end)+"\n",
|
|
|
|
FILE_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
return COLVARS_ERROR;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
if (!(*cvi)->read_traj(is)) {
|
|
|
|
cvm::error("Error: in reading colvar \""+(*cvi)->name+
|
2015-02-20 02:20:52 +08:00
|
|
|
"\" from trajectory file \""+
|
|
|
|
std::string(traj_filename)+"\".\n",
|
|
|
|
FILE_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
return COLVARS_ERROR;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
std::ostream & colvarmodule::write_restart(std::ostream &os)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
os.setf(std::ios::scientific, std::ios::floatfield);
|
2012-05-24 00:20:04 +08:00
|
|
|
os << "configuration {\n"
|
2014-12-02 10:09:53 +08:00
|
|
|
<< " step " << std::setw(it_width)
|
2012-05-24 00:20:04 +08:00
|
|
|
<< it << "\n"
|
|
|
|
<< " dt " << dt() << "\n"
|
2016-04-28 22:48:56 +08:00
|
|
|
<< " version " << std::string(COLVARS_VERSION) << "\n"
|
2012-05-24 00:20:04 +08:00
|
|
|
<< "}\n\n";
|
|
|
|
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
(*cvi)->write_restart(os);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
(*bi)->write_restart(os);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
cvm::decrease_depth();
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int colvarmodule::open_traj_file(std::string const &file_name)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
if (cv_traj_os.is_open()) {
|
|
|
|
return COLVARS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
// (re)open trajectory file
|
|
|
|
if (cv_traj_append) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Appending to colvar trajectory file \""+file_name+
|
2015-02-20 02:20:52 +08:00
|
|
|
"\".\n");
|
2014-12-02 10:09:53 +08:00
|
|
|
cv_traj_os.open(file_name.c_str(), std::ios::app);
|
2014-10-08 04:30:25 +08:00
|
|
|
} else {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Writing to colvar trajectory file \""+file_name+
|
2015-02-20 02:20:52 +08:00
|
|
|
"\".\n");
|
2014-12-02 10:09:53 +08:00
|
|
|
proxy->backup_file(file_name.c_str());
|
2015-02-20 02:20:52 +08:00
|
|
|
cv_traj_os.open(file_name.c_str());
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
|
|
|
|
if (!cv_traj_os.is_open()) {
|
|
|
|
cvm::error("Error: cannot write to file \""+file_name+"\".\n",
|
2015-02-20 02:20:52 +08:00
|
|
|
FILE_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int colvarmodule::close_traj_file()
|
|
|
|
{
|
2016-04-16 00:07:01 +08:00
|
|
|
if (cv_traj_os.is_open()) {
|
2014-10-08 04:30:25 +08:00
|
|
|
cv_traj_os.close();
|
|
|
|
}
|
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
std::ostream & colvarmodule::write_traj_label(std::ostream &os)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
|
|
|
if (!os.good()) {
|
|
|
|
cvm::error("Cannot write to trajectory file.");
|
|
|
|
return os;
|
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
os.setf(std::ios::scientific, std::ios::floatfield);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
os << "# " << cvm::wrap_string("step", cvm::it_width-2)
|
2014-10-08 04:30:25 +08:00
|
|
|
<< " ";
|
|
|
|
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
(*cvi)->write_traj_label(os);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
(*bi)->write_traj_label(os);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
os << "\n";
|
2015-04-06 23:14:53 +08:00
|
|
|
if (cvm::debug()) {
|
2014-10-08 04:30:25 +08:00
|
|
|
os.flush();
|
2015-04-06 23:14:53 +08:00
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
std::ostream & colvarmodule::write_traj(std::ostream &os)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
os.setf(std::ios::scientific, std::ios::floatfield);
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
os << std::setw(cvm::it_width) << it
|
2014-10-08 04:30:25 +08:00
|
|
|
<< " ";
|
|
|
|
|
|
|
|
cvm::increase_depth();
|
|
|
|
for (std::vector<colvar *>::iterator cvi = colvars.begin();
|
|
|
|
cvi != colvars.end();
|
|
|
|
cvi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
(*cvi)->write_traj(os);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
for (std::vector<colvarbias *>::iterator bi = biases.begin();
|
|
|
|
bi != biases.end();
|
|
|
|
bi++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
(*bi)->write_traj(os);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
os << "\n";
|
2015-04-06 23:14:53 +08:00
|
|
|
if (cvm::debug()) {
|
2014-10-08 04:30:25 +08:00
|
|
|
os.flush();
|
2015-04-06 23:14:53 +08:00
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
return os;
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
void cvm::log(std::string const &message)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2016-04-16 00:07:01 +08:00
|
|
|
size_t const d = depth();
|
|
|
|
if (d > 0)
|
|
|
|
proxy->log((std::string(2*d, ' '))+message);
|
2012-05-24 00:20:04 +08:00
|
|
|
else
|
2014-12-02 10:09:53 +08:00
|
|
|
proxy->log(message);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
void cvm::increase_depth()
|
|
|
|
{
|
2016-04-16 00:07:01 +08:00
|
|
|
(depth())++;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
void cvm::decrease_depth()
|
|
|
|
{
|
2016-04-16 00:07:01 +08:00
|
|
|
if (depth() > 0) {
|
|
|
|
(depth())--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t & cvm::depth()
|
|
|
|
{
|
|
|
|
// NOTE: do not call log() or error() here, to avoid recursion
|
|
|
|
size_t const nt = proxy->smp_num_threads();
|
|
|
|
if (proxy->smp_enabled() == COLVARS_OK) {
|
|
|
|
if (depth_v.size() != nt) {
|
|
|
|
// update array of depths
|
|
|
|
proxy->smp_lock();
|
|
|
|
if (depth_v.size() > 0) { depth_s = depth_v[0]; }
|
|
|
|
depth_v.clear();
|
|
|
|
depth_v.assign(nt, depth_s);
|
|
|
|
proxy->smp_unlock();
|
|
|
|
}
|
|
|
|
return depth_v[proxy->smp_thread_id()];
|
|
|
|
}
|
|
|
|
return depth_s;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2016-04-28 22:48:56 +08:00
|
|
|
void colvarmodule::set_error_bit(int code)
|
2016-04-16 00:07:01 +08:00
|
|
|
{
|
2016-04-28 22:48:56 +08:00
|
|
|
if (code > 0) {
|
|
|
|
cvm::fatal_error("Error: set_error_bit() received positive error code.\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
proxy->smp_lock();
|
2016-04-28 22:48:56 +08:00
|
|
|
errorCode = -1 * ((-1 * errorCode) | (-1 * code) | (-1 * COLVARS_ERROR));
|
2016-04-16 00:07:01 +08:00
|
|
|
proxy->smp_unlock();
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:48:56 +08:00
|
|
|
bool colvarmodule::get_error_bit(int code)
|
|
|
|
{
|
|
|
|
return bool((-1 * errorCode) & (-1 * code));
|
|
|
|
}
|
|
|
|
|
|
|
|
void colvarmodule::combine_errors(int &target, int const code)
|
|
|
|
{
|
|
|
|
if (code > 0 || target > 0) {
|
|
|
|
cvm::fatal_error("Error: combine_errors() received positive error code.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
target = -1 * ((-1 * target) | (-1 * code));
|
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
void colvarmodule::clear_error()
|
|
|
|
{
|
|
|
|
proxy->smp_lock();
|
|
|
|
errorCode = COLVARS_OK;
|
|
|
|
proxy->smp_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
void cvm::error(std::string const &message, int code)
|
2014-10-08 04:30:25 +08:00
|
|
|
{
|
2016-04-28 22:48:56 +08:00
|
|
|
set_error_bit(code);
|
2014-12-02 10:09:53 +08:00
|
|
|
proxy->error(message);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
void cvm::fatal_error(std::string const &message)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2016-03-02 04:40:25 +08:00
|
|
|
// TODO once all non-fatal errors have been set to be handled by error(),
|
|
|
|
// set DELETE_COLVARS here for VMD to handle it
|
2016-04-28 22:48:56 +08:00
|
|
|
set_error_bit(FATAL_ERROR);
|
2014-12-02 10:09:53 +08:00
|
|
|
proxy->fatal_error(message);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
void cvm::exit(std::string const &message)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
proxy->exit(message);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int cvm::read_index_file(char const *filename)
|
2013-06-28 06:48:27 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
std::ifstream is(filename, std::ios::binary);
|
2015-02-20 02:20:52 +08:00
|
|
|
if (!is.good()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: in opening index file \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string(filename)+"\".\n",
|
|
|
|
FILE_ERROR);
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2013-06-28 06:48:27 +08:00
|
|
|
while (is.good()) {
|
|
|
|
char open, close;
|
|
|
|
std::string group_name;
|
|
|
|
if ( (is >> open) && (open == '[') &&
|
|
|
|
(is >> group_name) &&
|
|
|
|
(is >> close) && (close == ']') ) {
|
2014-10-08 04:30:25 +08:00
|
|
|
for (std::list<std::string>::iterator names_i = index_group_names.begin();
|
|
|
|
names_i != index_group_names.end();
|
|
|
|
names_i++) {
|
|
|
|
if (*names_i == group_name) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: the group name \""+group_name+
|
2015-02-20 02:20:52 +08:00
|
|
|
"\" appears in multiple index files.\n",
|
|
|
|
FILE_ERROR);
|
2014-10-08 04:30:25 +08:00
|
|
|
}
|
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::index_group_names.push_back(group_name);
|
|
|
|
cvm::index_groups.push_back(std::vector<int> ());
|
2013-06-28 06:48:27 +08:00
|
|
|
} else {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: in parsing index file \""+
|
|
|
|
std::string(filename)+"\".\n",
|
2014-10-08 04:30:25 +08:00
|
|
|
INPUT_ERROR);
|
2013-06-28 06:48:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int atom_number = 1;
|
|
|
|
size_t pos = is.tellg();
|
|
|
|
while ( (is >> atom_number) && (atom_number > 0) ) {
|
2014-12-02 10:09:53 +08:00
|
|
|
(cvm::index_groups.back()).push_back(atom_number);
|
2013-06-28 06:48:27 +08:00
|
|
|
pos = is.tellg();
|
|
|
|
}
|
|
|
|
is.clear();
|
2014-12-02 10:09:53 +08:00
|
|
|
is.seekg(pos, std::ios::beg);
|
2013-06-28 06:48:27 +08:00
|
|
|
std::string delim;
|
|
|
|
if ( (is >> delim) && (delim == "[") ) {
|
|
|
|
// new group
|
|
|
|
is.clear();
|
2014-12-02 10:09:53 +08:00
|
|
|
is.seekg(pos, std::ios::beg);
|
2013-06-28 06:48:27 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("The following index groups were read from the index file \""+
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string(filename)+"\":\n");
|
2014-10-08 04:30:25 +08:00
|
|
|
std::list<std::string>::iterator names_i = index_group_names.begin();
|
|
|
|
std::list<std::vector<int> >::iterator lists_i = index_groups.begin();
|
|
|
|
for ( ; names_i != index_group_names.end() ; names_i++, lists_i++) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log(" "+(*names_i)+" ("+cvm::to_str(lists_i->size())+" atoms).\n");
|
2013-06-28 06:48:27 +08:00
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2013-06-28 06:48:27 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int cvm::load_atoms(char const *file_name,
|
2016-03-02 04:40:25 +08:00
|
|
|
cvm::atom_group &atoms,
|
2015-02-20 02:20:52 +08:00
|
|
|
std::string const &pdb_field,
|
|
|
|
double const pdb_field_value)
|
2014-05-12 23:08:14 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
return proxy->load_atoms(file_name, atoms, pdb_field, pdb_field_value);
|
2014-05-12 23:08:14 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int cvm::load_coords(char const *file_name,
|
2015-02-20 02:20:52 +08:00
|
|
|
std::vector<cvm::atom_pos> &pos,
|
|
|
|
const std::vector<int> &indices,
|
|
|
|
std::string const &pdb_field,
|
|
|
|
double const pdb_field_value)
|
2014-05-12 23:08:14 +08:00
|
|
|
{
|
|
|
|
// Differentiate between PDB and XYZ files
|
|
|
|
// for XYZ files, use CVM internal parser
|
|
|
|
// otherwise call proxy function for PDB
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
std::string const ext(strlen(file_name) > 4 ? (file_name + (strlen(file_name) - 4)) : file_name);
|
|
|
|
if (colvarparse::to_lower_cppstr(ext) == std::string(".xyz")) {
|
2014-05-12 23:08:14 +08:00
|
|
|
if ( pdb_field.size() > 0 ) {
|
2014-10-08 04:30:25 +08:00
|
|
|
cvm::error("Error: PDB column may not be specified for XYZ coordinate file.\n", INPUT_ERROR);
|
|
|
|
return COLVARS_ERROR;
|
2014-05-12 23:08:14 +08:00
|
|
|
}
|
2014-12-02 10:09:53 +08:00
|
|
|
return cvm::load_coords_xyz(file_name, pos, indices);
|
2014-05-12 23:08:14 +08:00
|
|
|
} else {
|
2014-12-02 10:09:53 +08:00
|
|
|
return proxy->load_coords(file_name, pos, indices, pdb_field, pdb_field_value);
|
2014-05-12 23:08:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
int cvm::load_coords_xyz(char const *filename,
|
2015-02-20 02:20:52 +08:00
|
|
|
std::vector<atom_pos> &pos,
|
|
|
|
const std::vector<int> &indices)
|
2013-10-30 23:25:42 +08:00
|
|
|
{
|
2014-12-02 10:09:53 +08:00
|
|
|
std::ifstream xyz_is(filename);
|
2014-10-08 04:30:25 +08:00
|
|
|
unsigned int natoms;
|
2013-10-30 23:25:42 +08:00
|
|
|
char symbol[256];
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
if ( ! (xyz_is >> natoms) ) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: cannot parse XYZ file "
|
2015-02-20 02:20:52 +08:00
|
|
|
+ std::string(filename) + ".\n", INPUT_ERROR);
|
2013-10-30 23:25:42 +08:00
|
|
|
}
|
|
|
|
// skip comment line
|
2014-12-02 10:09:53 +08:00
|
|
|
std::getline(xyz_is, line);
|
|
|
|
std::getline(xyz_is, line);
|
|
|
|
xyz_is.width(255);
|
2013-10-30 23:25:42 +08:00
|
|
|
std::vector<atom_pos>::iterator pos_i = pos.begin();
|
|
|
|
|
|
|
|
if (pos.size() != natoms) { // Use specified indices
|
|
|
|
int next = 0; // indices are zero-based
|
|
|
|
std::vector<int>::const_iterator index = indices.begin();
|
|
|
|
for ( ; pos_i != pos.end() ; pos_i++, index++) {
|
|
|
|
|
|
|
|
while ( next < *index ) {
|
2014-12-02 10:09:53 +08:00
|
|
|
std::getline(xyz_is, line);
|
2013-10-30 23:25:42 +08:00
|
|
|
next++;
|
|
|
|
}
|
|
|
|
xyz_is >> symbol;
|
|
|
|
xyz_is >> (*pos_i)[0] >> (*pos_i)[1] >> (*pos_i)[2];
|
|
|
|
}
|
|
|
|
} else { // Use all positions
|
|
|
|
for ( ; pos_i != pos.end() ; pos_i++) {
|
|
|
|
xyz_is >> symbol;
|
|
|
|
xyz_is >> (*pos_i)[0] >> (*pos_i)[1] >> (*pos_i)[2];
|
|
|
|
}
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK);
|
2013-10-30 23:25:42 +08:00
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
// static pointers
|
|
|
|
std::vector<colvar *> colvarmodule::colvars;
|
|
|
|
std::vector<colvarbias *> colvarmodule::biases;
|
|
|
|
size_t colvarmodule::n_abf_biases = 0;
|
2014-08-15 07:29:25 +08:00
|
|
|
size_t colvarmodule::n_rest_biases = 0;
|
2012-05-24 00:20:04 +08:00
|
|
|
size_t colvarmodule::n_histo_biases = 0;
|
|
|
|
size_t colvarmodule::n_meta_biases = 0;
|
|
|
|
colvarproxy *colvarmodule::proxy = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
// static runtime data
|
2016-04-28 22:48:56 +08:00
|
|
|
cvm::real colvarmodule::debug_gradients_step_size = 1.0e-07;
|
2014-10-08 04:30:25 +08:00
|
|
|
int colvarmodule::errorCode = 0;
|
2015-04-30 22:09:42 +08:00
|
|
|
long colvarmodule::it = 0;
|
|
|
|
long colvarmodule::it_restart = 0;
|
2012-05-24 00:20:04 +08:00
|
|
|
size_t colvarmodule::restart_out_freq = 0;
|
|
|
|
size_t colvarmodule::cv_traj_freq = 0;
|
2016-04-16 00:07:01 +08:00
|
|
|
size_t colvarmodule::depth_s = 0;
|
|
|
|
std::vector<size_t> colvarmodule::depth_v(0);
|
2012-05-24 00:20:04 +08:00
|
|
|
bool colvarmodule::b_analysis = false;
|
2013-06-28 06:48:27 +08:00
|
|
|
std::list<std::string> colvarmodule::index_group_names;
|
|
|
|
std::list<std::vector<int> > colvarmodule::index_groups;
|
2014-12-02 10:09:53 +08:00
|
|
|
bool colvarmodule::use_scripted_forces = false;
|
2016-04-16 00:07:01 +08:00
|
|
|
bool colvarmodule::scripting_after_biases = true;
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
// file name prefixes
|
|
|
|
std::string colvarmodule::output_prefix = "";
|
|
|
|
std::string colvarmodule::restart_in_name = "";
|
|
|
|
|
|
|
|
|
|
|
|
// i/o constants
|
|
|
|
size_t const colvarmodule::it_width = 12;
|
|
|
|
size_t const colvarmodule::cv_prec = 14;
|
|
|
|
size_t const colvarmodule::cv_width = 21;
|
|
|
|
size_t const colvarmodule::en_prec = 14;
|
|
|
|
size_t const colvarmodule::en_width = 21;
|
|
|
|
std::string const colvarmodule::line_marker =
|
|
|
|
"----------------------------------------------------------------------\n";
|