2016-06-18 07:07:51 +08:00
|
|
|
// -*- c++ -*-
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2016-12-28 02:17:34 +08:00
|
|
|
// This file is part of the Collective Variables module (Colvars).
|
|
|
|
// The original version of Colvars and its updates are located at:
|
|
|
|
// https://github.com/colvars/colvars
|
|
|
|
// Please update all Colvars source files before making any changes.
|
|
|
|
// If you wish to distribute your changes, please submit them to the
|
|
|
|
// Colvars repository at GitHub.
|
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
#include "colvarmodule.h"
|
|
|
|
#include "colvarvalue.h"
|
|
|
|
#include "colvar.h"
|
|
|
|
#include "colvarcomp.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
colvar::cvc::cvc()
|
2015-07-22 22:36:59 +08:00
|
|
|
: sup_coeff(1.0),
|
|
|
|
sup_np(1),
|
2014-12-02 10:09:53 +08:00
|
|
|
b_periodic(false),
|
2016-04-16 00:07:01 +08:00
|
|
|
b_try_scalable(true)
|
2016-05-12 21:52:42 +08:00
|
|
|
{
|
|
|
|
init_cvc_requires();
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
colvar::cvc::cvc(std::string const &conf)
|
2015-07-22 22:36:59 +08:00
|
|
|
: sup_coeff(1.0),
|
|
|
|
sup_np(1),
|
2014-12-02 10:09:53 +08:00
|
|
|
b_periodic(false),
|
2016-04-16 00:07:01 +08:00
|
|
|
b_try_scalable(true)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
|
|
|
if (cvm::debug())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Initializing cvc base object.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
init_cvc_requires();
|
|
|
|
|
2016-04-28 22:48:56 +08:00
|
|
|
if (get_keyval(conf, "name", this->name, std::string(""), parse_silent)) {
|
|
|
|
// Temporary description until child object is initialized
|
|
|
|
description = "cvc " + name;
|
|
|
|
} else {
|
|
|
|
description = "uninitialized cvc";
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
get_keyval(conf, "componentCoeff", sup_coeff, 1.0);
|
|
|
|
get_keyval(conf, "componentExp", sup_np, 1);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2014-12-02 10:09:53 +08:00
|
|
|
get_keyval(conf, "period", period, 0.0);
|
|
|
|
get_keyval(conf, "wrapAround", wrap_center, 0.0);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
2016-09-09 04:20:32 +08:00
|
|
|
get_keyval_feature((colvarparse *)this, conf, "debugGradients",
|
|
|
|
f_cvc_debug_gradient, false, parse_silent);
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
{
|
|
|
|
bool b_no_PBC = false;
|
|
|
|
get_keyval(conf, "forceNoPBC", b_no_PBC, false);
|
|
|
|
if (b_no_PBC) {
|
|
|
|
disable(f_cvc_pbc_minimum_image);
|
|
|
|
} else {
|
|
|
|
enable(f_cvc_pbc_minimum_image);
|
|
|
|
}
|
|
|
|
// this does not use get_keyval_feature() only for backward compatibility
|
|
|
|
}
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
// Attempt scalable calculations when in parallel? (By default yes, if available)
|
|
|
|
get_keyval(conf, "scalable", b_try_scalable, true);
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
if (cvm::debug())
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::log("Done initializing cvc base object.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-30 20:15:44 +08:00
|
|
|
int colvar::cvc::init_total_force_params(std::string const &conf)
|
|
|
|
{
|
2017-03-10 22:16:58 +08:00
|
|
|
if (cvm::get_error()) return COLVARS_ERROR;
|
|
|
|
|
2016-09-30 20:15:44 +08:00
|
|
|
if (get_keyval_feature(this, conf, "oneSiteSystemForce",
|
|
|
|
f_cvc_one_site_total_force, is_enabled(f_cvc_one_site_total_force))) {
|
|
|
|
cvm::log("Warning: keyword \"oneSiteSystemForce\" is deprecated: "
|
|
|
|
"please use \"oneSiteTotalForce\" instead.\n");
|
|
|
|
}
|
|
|
|
if (get_keyval_feature(this, conf, "oneSiteTotalForce",
|
|
|
|
f_cvc_one_site_total_force, is_enabled(f_cvc_one_site_total_force))) {
|
|
|
|
cvm::log("Computing total force on group 1 only");
|
|
|
|
}
|
2017-03-10 22:16:58 +08:00
|
|
|
|
|
|
|
if (! is_enabled(f_cvc_one_site_total_force)) {
|
|
|
|
// check whether any of the other atom groups is dummy
|
|
|
|
std::vector<cvm::atom_group *>::iterator agi = atom_groups.begin();
|
|
|
|
agi++;
|
|
|
|
for ( ; agi != atom_groups.end(); agi++) {
|
|
|
|
if ((*agi)->b_dummy) {
|
|
|
|
provide(f_cvc_inv_gradient, false);
|
|
|
|
provide(f_cvc_Jacobian, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 20:15:44 +08:00
|
|
|
return COLVARS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
cvm::atom_group *colvar::cvc::parse_group(std::string const &conf,
|
|
|
|
char const *group_key,
|
|
|
|
bool optional)
|
2012-05-24 00:20:04 +08:00
|
|
|
{
|
2016-04-16 00:07:01 +08:00
|
|
|
cvm::atom_group *group = NULL;
|
2017-07-20 02:10:43 +08:00
|
|
|
std::string group_conf;
|
2016-04-16 00:07:01 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
if (key_lookup(conf, group_key, &group_conf)) {
|
|
|
|
group = new cvm::atom_group(group_key);
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
if (b_try_scalable) {
|
2017-07-20 02:10:43 +08:00
|
|
|
if (is_available(f_cvc_scalable_com)
|
|
|
|
&& is_enabled(f_cvc_com_based)
|
|
|
|
&& !is_enabled(f_cvc_debug_gradient)) {
|
2016-04-16 00:07:01 +08:00
|
|
|
enable(f_cvc_scalable_com);
|
|
|
|
enable(f_cvc_scalable);
|
2016-10-25 05:08:36 +08:00
|
|
|
// The CVC makes the feature available;
|
|
|
|
// the atom group will enable it unless it needs to compute a rotational fit
|
|
|
|
group->provide(f_ag_scalable_com);
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check for other types of parallelism here
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
if (group_conf.size() == 0) {
|
|
|
|
cvm::error("Error: atom group \""+group->key+
|
|
|
|
"\" is set, but has no definition.\n",
|
|
|
|
INPUT_ERROR);
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
cvm::increase_depth();
|
|
|
|
if (group->parse(group_conf) == COLVARS_OK) {
|
|
|
|
register_atom_group(group);
|
|
|
|
}
|
|
|
|
group->check_keywords(group_conf, group_key);
|
|
|
|
if (cvm::get_error()) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error parsing definition for atom group \""+
|
2017-07-20 02:10:43 +08:00
|
|
|
std::string(group_key)+"\"\n.", INPUT_ERROR);
|
2014-10-29 03:53:17 +08:00
|
|
|
}
|
2017-07-20 02:10:43 +08:00
|
|
|
cvm::decrease_depth();
|
|
|
|
|
2012-05-24 00:20:04 +08:00
|
|
|
} else {
|
|
|
|
if (! optional) {
|
2014-12-02 10:09:53 +08:00
|
|
|
cvm::error("Error: definition for atom group \""+
|
2017-07-20 02:10:43 +08:00
|
|
|
std::string(group_key)+"\" not found.\n");
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-20 02:10:43 +08:00
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int colvar::cvc::setup()
|
|
|
|
{
|
|
|
|
description = "cvc " + name;
|
|
|
|
return COLVARS_OK;
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
colvar::cvc::~cvc()
|
2016-04-16 00:07:01 +08:00
|
|
|
{
|
2017-07-20 02:10:43 +08:00
|
|
|
free_children_deps();
|
2016-04-16 00:07:01 +08:00
|
|
|
remove_all_children();
|
|
|
|
for (size_t i = 0; i < atom_groups.size(); i++) {
|
|
|
|
if (atom_groups[i] != NULL) delete atom_groups[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void colvar::cvc::read_data()
|
|
|
|
{
|
|
|
|
size_t ig;
|
|
|
|
for (ig = 0; ig < atom_groups.size(); ig++) {
|
|
|
|
cvm::atom_group &atoms = *(atom_groups[ig]);
|
|
|
|
atoms.reset_atoms_data();
|
|
|
|
atoms.read_positions();
|
|
|
|
atoms.calc_required_properties();
|
2016-06-18 07:07:51 +08:00
|
|
|
// each atom group will take care of its own fitting_group, if defined
|
2016-04-16 00:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//// Don't try to get atom velocities, as no back-end currently implements it
|
|
|
|
// if (tasks[task_output_velocity] && !tasks[task_fdiff_velocity]) {
|
|
|
|
// for (i = 0; i < cvcs.size(); i++) {
|
|
|
|
// for (ig = 0; ig < cvcs[i]->atom_groups.size(); ig++) {
|
|
|
|
// cvcs[i]->atom_groups[ig]->read_velocities();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
2012-05-24 00:20:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
void colvar::cvc::calc_force_invgrads()
|
|
|
|
{
|
2016-12-28 02:17:34 +08:00
|
|
|
cvm::error("Error: calculation of inverse gradients is not implemented "
|
|
|
|
"for colvar components of type \""+function_type+"\".\n",
|
|
|
|
COLVARS_NOT_IMPLEMENTED);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void colvar::cvc::calc_Jacobian_derivative()
|
|
|
|
{
|
2016-12-28 02:17:34 +08:00
|
|
|
cvm::error("Error: calculation of inverse gradients is not implemented "
|
|
|
|
"for colvar components of type \""+function_type+"\".\n",
|
|
|
|
COLVARS_NOT_IMPLEMENTED);
|
2012-05-24 00:20:04 +08:00
|
|
|
}
|
2013-02-02 06:39:47 +08:00
|
|
|
|
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
void colvar::cvc::calc_fit_gradients()
|
|
|
|
{
|
|
|
|
for (size_t ig = 0; ig < atom_groups.size(); ig++) {
|
|
|
|
atom_groups[ig]->calc_fit_gradients();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void colvar::cvc::debug_gradients()
|
2013-02-02 06:39:47 +08:00
|
|
|
{
|
2017-07-20 02:10:43 +08:00
|
|
|
// this function should work for any scalar cvc:
|
2013-02-02 06:39:47 +08:00
|
|
|
// the only difference will be the name of the atom group (here, "group")
|
2016-04-28 22:48:56 +08:00
|
|
|
// NOTE: this assumes that groups for this cvc are non-overlapping,
|
|
|
|
// since atom coordinates are modified only within the current group
|
2013-02-02 06:39:47 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
cvm::log("Debugging gradients for " + description);
|
2013-02-02 06:39:47 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
for (size_t ig = 0; ig < atom_groups.size(); ig++) {
|
|
|
|
cvm::atom_group *group = atom_groups[ig];
|
|
|
|
if (group->b_dummy) continue;
|
2013-02-02 06:39:47 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
cvm::rotation const rot_0 = group->rot;
|
|
|
|
cvm::rotation const rot_inv = group->rot.inverse();
|
2013-02-02 06:39:47 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
cvm::real x_0 = x.real_value;
|
|
|
|
if ((x.type() == colvarvalue::type_vector) && (x.size() == 1)) x_0 = x[0];
|
2013-02-02 06:39:47 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
// cvm::log("gradients = "+cvm::to_str (gradients)+"\n");
|
2016-04-28 22:48:56 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
cvm::atom_group *group_for_fit = group->fitting_group ? group->fitting_group : group;
|
|
|
|
cvm::atom_pos fit_gradient_sum, gradient_sum;
|
2016-04-28 22:48:56 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
// print the values of the fit gradients
|
|
|
|
if (group->b_rotate || group->b_center) {
|
|
|
|
if (group->is_enabled(f_ag_fit_gradients)) {
|
|
|
|
size_t j;
|
2013-02-02 06:39:47 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
// fit_gradients are in the simulation frame: we should print them in the rotated frame
|
|
|
|
cvm::log("Fit gradients:\n");
|
|
|
|
for (j = 0; j < group_for_fit->fit_gradients.size(); j++) {
|
|
|
|
cvm::log((group->fitting_group ? std::string("refPosGroup") : group->key) +
|
|
|
|
"[" + cvm::to_str(j) + "] = " +
|
|
|
|
(group->b_rotate ?
|
|
|
|
cvm::to_str(rot_0.rotate(group_for_fit->fit_gradients[j])) :
|
|
|
|
cvm::to_str(group_for_fit->fit_gradients[j])));
|
|
|
|
}
|
|
|
|
}
|
2013-02-02 06:39:47 +08:00
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
// debug the gradients
|
|
|
|
for (size_t ia = 0; ia < group->size(); ia++) {
|
2016-04-28 22:48:56 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
// tests are best conducted in the unrotated (simulation) frame
|
|
|
|
cvm::rvector const atom_grad = (group->b_rotate ?
|
|
|
|
rot_inv.rotate((*group)[ia].grad) :
|
|
|
|
(*group)[ia].grad);
|
|
|
|
gradient_sum += atom_grad;
|
2016-04-28 22:48:56 +08:00
|
|
|
|
|
|
|
for (size_t id = 0; id < 3; id++) {
|
|
|
|
// (re)read original positions
|
|
|
|
group->read_positions();
|
|
|
|
// change one coordinate
|
2017-07-20 02:10:43 +08:00
|
|
|
(*group)[ia].pos[id] += cvm::debug_gradients_step_size;
|
2016-04-28 22:48:56 +08:00
|
|
|
group->calc_required_properties();
|
|
|
|
calc_value();
|
2017-07-20 02:10:43 +08:00
|
|
|
cvm::real x_1 = x.real_value;
|
|
|
|
if ((x.type() == colvarvalue::type_vector) && (x.size() == 1)) x_1 = x[0];
|
|
|
|
cvm::log("Atom "+cvm::to_str(ia)+", component "+cvm::to_str(id)+":\n");
|
|
|
|
cvm::log("dx(actual) = "+cvm::to_str(x_1 - x_0,
|
|
|
|
21, 14)+"\n");
|
|
|
|
cvm::real const dx_pred = (group->fit_gradients.size()) ?
|
|
|
|
(cvm::debug_gradients_step_size * (atom_grad[id] + group->fit_gradients[ia][id])) :
|
|
|
|
(cvm::debug_gradients_step_size * atom_grad[id]);
|
|
|
|
cvm::log("dx(interp) = "+cvm::to_str(dx_pred,
|
|
|
|
21, 14)+"\n");
|
|
|
|
cvm::log("|dx(actual) - dx(interp)|/|dx(actual)| = "+
|
|
|
|
cvm::to_str(std::fabs(x_1 - x_0 - dx_pred) /
|
|
|
|
std::fabs(x_1 - x_0), 12, 5)+"\n");
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 22:48:56 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
if ((group->is_enabled(f_ag_fit_gradients)) && (group->fitting_group != NULL)) {
|
|
|
|
cvm::atom_group *ref_group = group->fitting_group;
|
|
|
|
group->read_positions();
|
|
|
|
group->calc_required_properties();
|
2016-04-28 22:48:56 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
for (size_t ia = 0; ia < ref_group->size(); ia++) {
|
|
|
|
|
|
|
|
// fit gradients are in the unrotated (simulation) frame
|
|
|
|
cvm::rvector const atom_grad = ref_group->fit_gradients[ia];
|
|
|
|
fit_gradient_sum += atom_grad;
|
|
|
|
|
|
|
|
for (size_t id = 0; id < 3; id++) {
|
|
|
|
// (re)read original positions
|
|
|
|
group->read_positions();
|
|
|
|
ref_group->read_positions();
|
|
|
|
// change one coordinate
|
|
|
|
(*ref_group)[ia].pos[id] += cvm::debug_gradients_step_size;
|
|
|
|
group->calc_required_properties();
|
|
|
|
calc_value();
|
|
|
|
|
|
|
|
cvm::real const x_1 = x.real_value;
|
|
|
|
cvm::log("refPosGroup atom "+cvm::to_str(ia)+", component "+cvm::to_str (id)+":\n");
|
|
|
|
cvm::log("dx(actual) = "+cvm::to_str (x_1 - x_0,
|
|
|
|
21, 14)+"\n");
|
|
|
|
|
|
|
|
cvm::real const dx_pred = cvm::debug_gradients_step_size * atom_grad[id];
|
|
|
|
|
|
|
|
cvm::log("dx(interp) = "+cvm::to_str (dx_pred,
|
|
|
|
21, 14)+"\n");
|
|
|
|
cvm::log ("|dx(actual) - dx(interp)|/|dx(actual)| = "+
|
|
|
|
cvm::to_str(std::fabs (x_1 - x_0 - dx_pred) /
|
|
|
|
std::fabs (x_1 - x_0),
|
|
|
|
12, 5)+
|
|
|
|
".\n");
|
|
|
|
}
|
2016-04-28 22:48:56 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
|
2017-07-20 02:10:43 +08:00
|
|
|
cvm::log("Gradient sum: " + cvm::to_str(gradient_sum) +
|
|
|
|
" Fit gradient sum: " + cvm::to_str(fit_gradient_sum) +
|
|
|
|
" Total " + cvm::to_str(gradient_sum + fit_gradient_sum));
|
|
|
|
}
|
2014-10-08 04:30:25 +08:00
|
|
|
return;
|
2013-02-02 06:39:47 +08:00
|
|
|
}
|
2016-04-16 00:07:01 +08:00
|
|
|
|
|
|
|
|
2016-12-28 02:17:34 +08:00
|
|
|
cvm::real colvar::cvc::dist2(colvarvalue const &x1,
|
|
|
|
colvarvalue const &x2) const
|
|
|
|
{
|
|
|
|
return x1.dist2(x2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
colvarvalue colvar::cvc::dist2_lgrad(colvarvalue const &x1,
|
|
|
|
colvarvalue const &x2) const
|
|
|
|
{
|
|
|
|
return x1.dist2_grad(x2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
colvarvalue colvar::cvc::dist2_rgrad(colvarvalue const &x1,
|
|
|
|
colvarvalue const &x2) const
|
|
|
|
{
|
|
|
|
return x2.dist2_grad(x1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void colvar::cvc::wrap(colvarvalue &x) const
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-16 00:07:01 +08:00
|
|
|
// Static members
|
|
|
|
|
2016-09-09 04:20:32 +08:00
|
|
|
std::vector<colvardeps::feature *> colvar::cvc::cvc_features;
|