Skip to content

Commit 5767ee3

Browse files
committed
cleanup: Inherit selfCoordNum to coordNum to reuse its init function
1 parent b3df77f commit 5767ee3

File tree

2 files changed

+37
-104
lines changed

2 files changed

+37
-104
lines changed

src/colvarcomp_coordnums.cpp

Lines changed: 32 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,44 @@ int colvar::coordnum::init(std::string const &conf)
3030
int error_code = cvc::init(conf);
3131

3232
group1 = parse_group(conf, "group1");
33-
group2 = parse_group(conf, "group2");
3433

35-
if (!group1 || !group2) {
34+
if (!group1) {
3635
return error_code | COLVARS_INPUT_ERROR;
3736
}
3837

39-
if (int atom_number = cvm::atom_group::overlap(*group1, *group2)) {
40-
error_code |= cvm::error(
41-
"Error: group1 and group2 share a common atom (number: " + cvm::to_str(atom_number) + ")\n",
42-
COLVARS_INPUT_ERROR);
38+
if (group1->b_dummy) {
39+
error_code |= cvm::error("Error: group1 may not be a dummy atom\n", COLVARS_INPUT_ERROR);
4340
}
4441

45-
if (group1->b_dummy) {
46-
error_code |=
47-
cvm::error("Error: only group2 is allowed to be a dummy atom\n", COLVARS_INPUT_ERROR);
42+
if (function_type() != "selfCoordNum") {
43+
44+
group2 = parse_group(conf, "group2");
45+
if (!group2) {
46+
return error_code | COLVARS_INPUT_ERROR;
47+
}
48+
49+
if (int atom_number = cvm::atom_group::overlap(*group1, *group2)) {
50+
error_code |= cvm::error("Error: group1 and group2 share a common atom (number: " +
51+
cvm::to_str(atom_number) + ")\n",
52+
COLVARS_INPUT_ERROR);
53+
}
54+
55+
get_keyval(conf, "group2CenterOnly", b_group2_center_only, group2->b_dummy);
56+
57+
if (b_group2_center_only) {
58+
num_pairs = group1->size();
59+
} else {
60+
num_pairs = group1->size() * group2->size();
61+
}
62+
63+
} else {
64+
65+
// selfCoordNum case
66+
num_pairs = (group1->size() * (group1->size() - 1)) / 2;
4867
}
4968

69+
init_scalar_boundaries(0.0, num_pairs);
70+
5071
// Get the default value from r0_vec to report it
5172
cvm::real r0 = r0_vec[0];
5273
bool const b_redefined_cutoff = get_keyval(conf, "cutoff", r0, r0);
@@ -85,8 +106,6 @@ int colvar::coordnum::init(std::string const &conf)
85106
cvm::log("Warning: only minimum-image distances are used by this variable.\n");
86107
}
87108

88-
get_keyval(conf, "group2CenterOnly", b_group2_center_only, group2->b_dummy);
89-
90109
get_keyval(conf, "tolerance", tolerance, tolerance);
91110
if (tolerance > 0) {
92111
cvm::main()->cite_feature("coordNum pairlist");
@@ -96,21 +115,10 @@ int colvar::coordnum::init(std::string const &conf)
96115
COLVARS_INPUT_ERROR);
97116
// return and do not allocate the pairlists below
98117
}
99-
size_t n;
100-
if (b_group2_center_only) {
101-
n = group1->size();
102-
} else {
103-
n = group1->size() * group2->size();
104-
}
105-
pairlist = new bool[n];
106-
for (size_t i = 0; i < n; i++) pairlist[i] = true;
118+
pairlist = new bool[num_pairs];
119+
for (size_t i = 0; i < num_pairs; i++) pairlist[i] = true;
107120
}
108121

109-
init_scalar_boundaries(0.0, b_group2_center_only ?
110-
static_cast<cvm::real>(group1->size()) :
111-
static_cast<cvm::real>(group1->size() *
112-
group2->size()));
113-
114122
return error_code;
115123
}
116124

@@ -376,68 +384,6 @@ void colvar::h_bond::calc_gradients()
376384
colvar::selfcoordnum::selfcoordnum()
377385
{
378386
set_function_type("selfCoordNum");
379-
x.type(colvarvalue::type_scalar);
380-
cvm::real const r0 = cvm::main()->proxy->angstrom_to_internal(4.0);
381-
r0_vec = {r0, r0, r0};
382-
}
383-
384-
385-
int colvar::selfcoordnum::init(std::string const &conf)
386-
{
387-
int error_code = cvc::init(conf);
388-
389-
group1 = parse_group(conf, "group1");
390-
391-
if (!group1 || group1->size() == 0) {
392-
return error_code | COLVARS_INPUT_ERROR;
393-
}
394-
395-
cvm::real r0 = r0_vec[0];
396-
bool const b_redefined_cutoff = get_keyval(conf, "cutoff", r0, r0);
397-
if (b_redefined_cutoff) {
398-
r0_vec = {r0, r0, r0};
399-
}
400-
get_keyval(conf, "expNumer", en, en);
401-
get_keyval(conf, "expDenom", ed, ed);
402-
403-
404-
if ((en % 2) || (ed % 2)) {
405-
error_code |= cvm::error("Error: odd exponent(s) provided, can only use even ones.\n",
406-
COLVARS_INPUT_ERROR);
407-
}
408-
409-
if ((en <= 0) || (ed <= 0)) {
410-
error_code |= cvm::error("Error: negative exponent(s) provided.\n", COLVARS_INPUT_ERROR);
411-
}
412-
413-
if (!is_enabled(f_cvc_pbc_minimum_image)) {
414-
cvm::log("Warning: only minimum-image distances are used by this variable.\n");
415-
}
416-
417-
get_keyval(conf, "tolerance", tolerance, tolerance);
418-
if (tolerance > 0) {
419-
get_keyval(conf, "pairListFrequency", pairlist_freq, pairlist_freq);
420-
if ( ! (pairlist_freq > 0) ) {
421-
error_code |= cvm::error("Error: non-positive pairlistfrequency provided.\n",
422-
COLVARS_INPUT_ERROR);
423-
}
424-
size_t n = (group1->size()-1) * (group1->size()-1);
425-
pairlist = new bool[n];
426-
for (size_t i = 0; i < n; i++) pairlist[i] = true;
427-
}
428-
429-
init_scalar_boundaries(0.0, static_cast<cvm::real>((group1->size()-1) *
430-
(group1->size()-1)));
431-
432-
return error_code;
433-
}
434-
435-
436-
colvar::selfcoordnum::~selfcoordnum()
437-
{
438-
if (pairlist) {
439-
delete [] pairlist;
440-
}
441387
}
442388

443389

src/colvarcomp_coordnums.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class colvar::coordnum : public colvar::cvc {
7070
/// Integer exponent of the function denominator
7171
int ed = 12;
7272

73+
/// The number of pairwise distances being calculated
74+
size_t num_pairs = 0;
75+
7376
/// If true, group2 will be treated as a single atom
7477
bool b_group2_center_only = false;
7578

@@ -87,32 +90,16 @@ class colvar::coordnum : public colvar::cvc {
8790

8891
/// \brief Colvar component: self-coordination number within a group
8992
/// (colvarvalue::type_scalar type, range [0:N*(N-1)/2])
90-
class colvar::selfcoordnum : public colvar::cvc {
93+
class colvar::selfcoordnum : public colvar::coordnum {
9194
public:
9295

9396
selfcoordnum();
94-
~selfcoordnum();
95-
virtual int init(std::string const &conf);
9697
virtual void calc_value();
9798
virtual void calc_gradients();
9899

100+
protected:
99101
/// Main workhorse function
100102
template <int flags> int compute_selfcoordnum();
101-
102-
protected:
103-
/// Selected atoms
104-
cvm::atom_group *group1 = nullptr;
105-
/// Cutoff distances along each dimension
106-
cvm::rvector r0_vec;
107-
/// Integer exponent of the function numerator
108-
int en = 6;
109-
/// Integer exponent of the function denominator
110-
int ed = 12;
111-
cvm::real tolerance = 0.0;
112-
int pairlist_freq = 100;
113-
114-
bool *pairlist = nullptr;
115-
116103
};
117104

118105

0 commit comments

Comments
 (0)