version 1.1 | version 1.2 |
---|
| |
/// -*- c++ -*- | // -*- c++ -*- |
| |
#include "colvarmodule.h" | #include "colvarmodule.h" |
#include "colvar.h" | #include "colvar.h" |
| |
| |
/// Histogram "bias" constructor | /// Histogram "bias" constructor |
| |
colvarbias_histogram::colvarbias_histogram(std::string const &conf, char const *key) | colvarbias_histogram::colvarbias_histogram(char const *key) |
: colvarbias(conf, key), | : colvarbias(key), |
grid(NULL), out_name("") | grid(NULL), out_name("") |
{ | { |
| } |
| |
| |
| int colvarbias_histogram::init(std::string const &conf) |
| { |
| colvarbias::init(conf); |
| |
| provide(f_cvb_history_dependent); |
| enable(f_cvb_history_dependent); |
| |
| size_t i; |
| |
get_keyval(conf, "outputFile", out_name, std::string("")); | get_keyval(conf, "outputFile", out_name, std::string("")); |
get_keyval(conf, "outputFileDX", out_name_dx, std::string("")); | get_keyval(conf, "outputFileDX", out_name_dx, std::string("")); |
get_keyval(conf, "outputFreq", output_freq, cvm::restart_out_freq); | get_keyval(conf, "outputFreq", output_freq, cvm::restart_out_freq); |
| |
| |
colvar_array_size = 0; | colvar_array_size = 0; |
{ | { |
size_t i; | |
bool colvar_array = false; | bool colvar_array = false; |
get_keyval(conf, "gatherVectorColvars", colvar_array, colvar_array); | get_keyval(conf, "gatherVectorColvars", colvar_array, colvar_array); |
| |
| |
for (i = 0; i < colvars.size(); i++) { // should be all vector | for (i = 0; i < colvars.size(); i++) { // should be all vector |
if (colvars[i]->value().type() != colvarvalue::type_vector) { | if (colvars[i]->value().type() != colvarvalue::type_vector) { |
cvm::error("Error: used gatherVectorColvars with non-vector colvar.\n", INPUT_ERROR); | cvm::error("Error: used gatherVectorColvars with non-vector colvar.\n", INPUT_ERROR); |
return; | return INPUT_ERROR; |
} | } |
if (i == 0) { | if (i == 0) { |
colvar_array_size = colvars[i]->value().size(); | colvar_array_size = colvars[i]->value().size(); |
if (colvar_array_size < 1) { | if (colvar_array_size < 1) { |
cvm::error("Error: vector variable has dimension less than one.\n", INPUT_ERROR); | cvm::error("Error: vector variable has dimension less than one.\n", INPUT_ERROR); |
return; | return INPUT_ERROR; |
} | } |
} else { | } else { |
if (colvar_array_size != colvars[i]->value().size()) { | if (colvar_array_size != colvars[i]->value().size()) { |
cvm::error("Error: trying to combine vector colvars of different lengths.\n", INPUT_ERROR); | cvm::error("Error: trying to combine vector colvars of different lengths.\n", INPUT_ERROR); |
return; | return INPUT_ERROR; |
} | } |
} | } |
} | } |
| |
for (i = 0; i < colvars.size(); i++) { // should be all scalar | for (i = 0; i < colvars.size(); i++) { // should be all scalar |
if (colvars[i]->value().type() != colvarvalue::type_scalar) { | if (colvars[i]->value().type() != colvarvalue::type_scalar) { |
cvm::error("Error: only scalar colvars are supported when gatherVectorColvars is off.\n", INPUT_ERROR); | cvm::error("Error: only scalar colvars are supported when gatherVectorColvars is off.\n", INPUT_ERROR); |
return; | return INPUT_ERROR; |
} | } |
} | } |
} | } |
| |
| |
if (colvar_array_size > 0) { | if (colvar_array_size > 0) { |
weights.assign(colvar_array_size, 1.0); | weights.assign(colvar_array_size, 1.0); |
get_keyval(conf, "weights", weights, weights, colvarparse::parse_silent); | get_keyval(conf, "weights", weights, weights); |
| } |
| |
| for (i = 0; i < colvars.size(); i++) { |
| colvars[i]->enable(f_cv_grid); |
} | } |
| |
grid = new colvar_grid_scalar(); | grid = new colvar_grid_scalar(); |
| grid->init_from_colvars(colvars); |
| |
{ | { |
std::string grid_conf; | std::string grid_conf; |
if (key_lookup(conf, "grid", grid_conf)) { | if (key_lookup(conf, "histogramGrid", grid_conf)) { |
grid->parse_params(grid_conf); | grid->parse_params(grid_conf); |
} else { | |
grid->init_from_colvars(colvars); | |
} | } |
} | } |
| |
cvm::log("Finished histogram setup.\n"); | return COLVARS_OK; |
} | } |
| |
/// Destructor | |
colvarbias_histogram::~colvarbias_histogram() | colvarbias_histogram::~colvarbias_histogram() |
{ | { |
if (grid) { | if (grid) { |
| |
cvm::n_histo_biases -= 1; | cvm::n_histo_biases -= 1; |
} | } |
| |
/// Update the grid | |
cvm::real colvarbias_histogram::update() | int colvarbias_histogram::update() |
{ | { |
| int error_code = COLVARS_OK; |
// update base class | // update base class |
colvarbias::update(); | error_code |= colvarbias::update(); |
| |
if (cvm::debug()) { | if (cvm::debug()) { |
cvm::log("Updating histogram bias " + this->name); | cvm::log("Updating histogram bias " + this->name); |
| |
// update indices for scalar values | // update indices for scalar values |
size_t i; | size_t i; |
for (i = 0; i < colvars.size(); i++) { | for (i = 0; i < colvars.size(); i++) { |
bin[i] = grid->value_to_bin_scalar(colvars[i]->value(), i); | bin[i] = grid->current_bin_scalar(i); |
} | } |
| |
if (grid->index_ok(bin)) { | if (grid->index_ok(bin)) { |
| |
size_t iv, i; | size_t iv, i; |
for (iv = 0; iv < colvar_array_size; iv++) { | for (iv = 0; iv < colvar_array_size; iv++) { |
for (i = 0; i < colvars.size(); i++) { | for (i = 0; i < colvars.size(); i++) { |
bin[i] = grid->value_to_bin_scalar(colvars[i]->value().vector1d_value[iv], i); | bin[i] = grid->current_bin_scalar(i, iv); |
} | } |
| |
if (grid->index_ok(bin)) { | if (grid->index_ok(bin)) { |
| |
write_output_files(); | write_output_files(); |
} | } |
| |
return 0.0; // no bias energy for histogram | error_code |= cvm::get_error(); |
| return error_code; |
} | } |
| |
| |
| |
| |
std::ostream & colvarbias_histogram::write_restart(std::ostream& os) | std::ostream & colvarbias_histogram::write_restart(std::ostream& os) |
{ | { |
| std::ios::fmtflags flags(os.flags()); |
| os.setf(std::ios::fmtflags(0), std::ios::floatfield); |
| |
os << "histogram {\n" | os << "histogram {\n" |
<< " configuration {\n" | << " configuration {\n" |
<< " name " << this->name << "\n"; | << " name " << this->name << "\n"; |
| |
| |
os << "}\n\n"; | os << "}\n\n"; |
| |
| os.flags(flags); |
return os; | return os; |
} | } |