version 1.17 | version 1.18 |
---|
| |
/// -*- c++ -*- | // -*- c++ -*- |
| |
#include "colvarmodule.h" | #include "colvarmodule.h" |
#include "colvarvalue.h" | #include "colvarvalue.h" |
#include "colvarbias.h" | #include "colvarbias.h" |
| |
| |
colvarbias::colvarbias(std::string const &conf, char const *key) | colvarbias::colvarbias(char const *key) |
: colvarparse(conf), bias_energy(0.), has_data(false) | : bias_type(to_lower_cppstr(key)) |
{ | { |
cvm::log("Initializing a new \""+std::string(key)+"\" instance.\n"); | init_cvb_requires(); |
| |
size_t rank = 1; | rank = 1; |
std::string const key_str(key); | |
| |
if (to_lower_cppstr(key_str) == std::string("abf")) { | if (bias_type == std::string("abf")) { |
rank = cvm::n_abf_biases+1; | rank = cvm::n_abf_biases+1; |
} | } |
if (to_lower_cppstr(key_str) == std::string("harmonic") || | if (bias_type == std::string("harmonic") || |
to_lower_cppstr(key_str) == std::string("linear")) { | bias_type == std::string("linear")) { |
rank = cvm::n_rest_biases+1; | rank = cvm::n_rest_biases+1; |
} | } |
if (to_lower_cppstr(key_str) == std::string("histogram")) { | if (bias_type == std::string("histogram")) { |
rank = cvm::n_histo_biases+1; | rank = cvm::n_histo_biases+1; |
} | } |
if (to_lower_cppstr(key_str) == std::string("metadynamics")) { | if (bias_type == std::string("metadynamics")) { |
rank = cvm::n_meta_biases+1; | rank = cvm::n_meta_biases+1; |
} | } |
| |
get_keyval(conf, "name", name, key_str+cvm::to_str(rank)); | has_data = false; |
| b_output_energy = false; |
| reset(); |
| |
if (cvm::bias_by_name(this->name) != NULL) { | // Start in active state by default |
| enable(f_cvb_active); |
| } |
| |
| |
| int colvarbias::init(std::string const &conf) |
| { |
| colvarparse::init(conf); |
| |
| if (name.size() == 0) { |
| cvm::log("Initializing a new \""+bias_type+"\" instance.\n"); |
| get_keyval(conf, "name", name, bias_type+cvm::to_str(rank)); |
| |
| { |
| colvarbias *bias_with_name = cvm::bias_by_name(this->name); |
| if (bias_with_name != NULL) { |
| if ((bias_with_name->rank != this->rank) || |
| (bias_with_name->bias_type != this->bias_type)) { |
cvm::error("Error: this bias cannot have the same name, \""+this->name+ | cvm::error("Error: this bias cannot have the same name, \""+this->name+ |
"\", as another bias.\n", INPUT_ERROR); | "\", as another bias.\n", INPUT_ERROR); |
return; | return INPUT_ERROR; |
| } |
} | } |
| } |
| |
| description = "bias " + name; |
| |
| { |
// lookup the associated colvars | // lookup the associated colvars |
std::vector<std::string> colvars_str; | std::vector<std::string> colvar_names; |
if (get_keyval(conf, "colvars", colvars_str)) { | if (get_keyval(conf, "colvars", colvar_names)) { |
for (size_t i = 0; i < colvars_str.size(); i++) { | if (colvars.size()) { |
add_colvar(colvars_str[i]); | cvm::error("Error: cannot redefine the colvars that a bias was already defined on.\n", |
| INPUT_ERROR); |
| return INPUT_ERROR; |
| } |
| for (size_t i = 0; i < colvar_names.size(); i++) { |
| add_colvar(colvar_names[i]); |
| } |
} | } |
} | } |
| |
if (!colvars.size()) { | if (!colvars.size()) { |
cvm::error("Error: no collective variables specified.\n"); | cvm::error("Error: no collective variables specified.\n", INPUT_ERROR); |
return; | return INPUT_ERROR; |
| } |
| |
| } else { |
| cvm::log("Reinitializing bias \""+name+"\".\n"); |
| } |
| |
| get_keyval(conf, "outputEnergy", b_output_energy, b_output_energy); |
| |
| return COLVARS_OK; |
} | } |
| |
get_keyval(conf, "outputEnergy", b_output_energy, false); | |
| int colvarbias::reset() |
| { |
| bias_energy = 0.0; |
| for (size_t i = 0; i < colvars.size(); i++) { |
| colvar_forces[i].reset(); |
| } |
| return COLVARS_OK; |
} | } |
| |
| |
| |
: colvarparse(), has_data(false) | : colvarparse(), has_data(false) |
{} | {} |
| |
| |
colvarbias::~colvarbias() | colvarbias::~colvarbias() |
{ | { |
| colvarbias::clear(); |
| } |
| |
| |
| int colvarbias::clear() |
| { |
// Remove references to this bias from colvars | // Remove references to this bias from colvars |
for (std::vector<colvar *>::iterator cvi = colvars.begin(); | for (std::vector<colvar *>::iterator cvi = colvars.begin(); |
cvi != colvars.end(); | cvi != colvars.end(); |
| |
} | } |
} | } |
} | } |
| |
// ...and from the colvars module | // ...and from the colvars module |
for (std::vector<colvarbias *>::iterator bi = cvm::biases.begin(); | for (std::vector<colvarbias *>::iterator bi = cvm::biases.begin(); |
bi != cvm::biases.end(); | bi != cvm::biases.end(); |
| |
break; | break; |
} | } |
} | } |
| |
| return COLVARS_OK; |
} | } |
| |
void colvarbias::add_colvar(std::string const &cv_name) | |
| int colvarbias::add_colvar(std::string const &cv_name) |
{ | { |
if (colvar *cv = cvm::colvar_by_name(cv_name)) { | if (colvar *cv = cvm::colvar_by_name(cv_name)) { |
cv->enable(colvar::task_gradients); | // Removed this as nor all biases apply forces eg histogram |
if (cvm::debug()) | // cv->enable(colvar::task_gradients); |
| if (cvm::debug()) { |
cvm::log("Applying this bias to collective variable \""+ | cvm::log("Applying this bias to collective variable \""+ |
cv->name+"\".\n"); | cv->name+"\".\n"); |
| } |
colvars.push_back(cv); | colvars.push_back(cv); |
| |
colvar_forces.push_back(colvarvalue()); | colvar_forces.push_back(colvarvalue()); |
colvar_forces.back().type(cv->value()); // make sure each forces is initialized to zero | colvar_forces.back().type(cv->value()); // make sure each force is initialized to zero |
colvar_forces.back().reset(); | colvar_forces.back().reset(); |
| |
cv->biases.push_back(this); // add back-reference to this bias to colvar | cv->biases.push_back(this); // add back-reference to this bias to colvar |
| |
| // Add dependency link. |
| // All biases need at least the value of each colvar |
| // although possibly not at all timesteps |
| add_child(cv); |
| |
} else { | } else { |
cvm::error("Error: cannot find a colvar named \""+ | cvm::error("Error: cannot find a colvar named \""+ |
cv_name+"\".\n"); | cv_name+"\".\n", INPUT_ERROR); |
| return INPUT_ERROR; |
} | } |
| return COLVARS_OK; |
} | } |
| |
| |
cvm::real colvarbias::update() | int colvarbias::update() |
{ | { |
| // Note: if anything is added here, it should be added also in the SMP block of calc_biases() |
| // TODO move here debug msg of bias update |
has_data = true; | has_data = true; |
return 0.0; | return COLVARS_OK; |
} | } |
| |
| |
| |
} | } |
| |
| |
// So far, these are only implemented in colvarsbias_abf | // So far, these are only implemented in colvarbias_abf |
int colvarbias::bin_num() | int colvarbias::bin_num() |
{ | { |
cvm::error("Error: bin_num() not implemented.\n"); | cvm::error("Error: bin_num() not implemented.\n"); |
| |
<< bias_energy; | << bias_energy; |
return os; | return os; |
} | } |
| |
| // Static members |
| |
| std::vector<colvardeps::feature *> colvarbias::cvb_features; |