Skip to content

Commit 2d25269

Browse files
committed
damping works for tree code, cleanup, nuke old code
1 parent 555fac9 commit 2d25269

24 files changed

+525
-465
lines changed

cppe/core/bmatrix.cc

Lines changed: 10 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,7 @@
66

77
namespace libcppe {
88

9-
Eigen::VectorXd BMatrix::apply_direct(const Eigen::VectorXd& induced_moments) {
10-
Eigen::VectorXd ret = Eigen::VectorXd::Zero(induced_moments.size());
11-
#pragma omp parallel for
12-
for (int i = 0; i < m_n_polsites; ++i) {
13-
int l = i * 3;
14-
Potential& pot1 = m_polsites[i];
15-
double* retx = &ret[l + 0];
16-
double* rety = &ret[l + 1];
17-
double* retz = &ret[l + 2];
18-
for (int j = 0; j < m_n_polsites; ++j) {
19-
int m = j * 3;
20-
Potential& pot2 = m_polsites[j];
21-
if (pot1.excludes_site(pot2.index) || i == j) continue;
22-
Eigen::Vector3d diff = pot2.get_site_position() - pot1.get_site_position();
23-
Eigen::VectorXd T2;
24-
if (m_options.damp_induced) {
25-
Polarizability& alpha_i = pot1.get_polarizability();
26-
Polarizability& alpha_j = pot2.get_polarizability();
27-
double v = m_options.damping_factor_induced /
28-
std::pow(alpha_i.get_isotropic_value() * alpha_j.get_isotropic_value(),
29-
1.0 / 6.0);
30-
T2 = tensors::T2_damp_thole(diff, v);
31-
} else {
32-
T2 = tensors::T2(diff);
33-
}
34-
double fx = induced_moments[m + 0];
35-
double fy = induced_moments[m + 1];
36-
double fz = induced_moments[m + 2];
37-
// inline matrix-vector product, faster than unfolding and multiplying
38-
*retx -= T2[0] * fx + T2[1] * fy + T2[2] * fz;
39-
*rety -= T2[1] * fx + T2[3] * fy + T2[4] * fz;
40-
*retz -= T2[2] * fx + T2[4] * fy + T2[5] * fz;
41-
}
42-
ret.segment<3>(l) += m_alpha_inverse[i] * induced_moments.segment<3>(l);
43-
}
44-
return ret;
45-
}
46-
47-
Eigen::VectorXd BMatrix::apply_fast_summation(const Eigen::VectorXd& induced_moments,
48-
std::string scheme) {
9+
Eigen::VectorXd BMatrix::apply(const Eigen::VectorXd& induced_moments) {
4910
Eigen::VectorXd ret = Eigen::VectorXd::Zero(induced_moments.size());
5011

5112
int n_crit = m_options.tree_ncrit;
@@ -59,33 +20,27 @@ Eigen::VectorXd BMatrix::apply_fast_summation(const Eigen::VectorXd& induced_mom
5920
S[i * 3 + 1] = s(1);
6021
S[i * 3 + 2] = s(2);
6122
}
62-
std::shared_ptr<Tree<1, 3>> tree = build_shared_tree<1, 3>(
63-
m_positions.data(), S.data(), m_n_polsites, n_crit, order, theta, m_exclusions);
23+
24+
double damping = 0.0;
25+
if (m_options.damp_induced) {
26+
damping = m_options.damping_factor_induced;
27+
}
28+
std::shared_ptr<Tree<1, 3>> tree =
29+
build_shared_tree<1, 3>(m_polsites, S.data(), n_crit, order, theta, damping);
6430
std::vector<double> induced_fields_v(3 * m_n_polsites);
31+
auto scheme = m_options.summation_induced_fields;
6532
if (scheme == "fmm") {
6633
tree->compute_field_fmm(induced_fields_v.data());
6734
} else {
68-
throw std::runtime_error("No such summation scheme " + scheme);
35+
tree->compute_field_exact(induced_fields_v.data());
6936
}
7037
Eigen::VectorXd induced_fields =
7138
Eigen::Map<Eigen::VectorXd>(induced_fields_v.data(), induced_fields_v.size());
7239
return induced_fields + apply_diagonal(induced_moments);
7340
}
7441

75-
Eigen::VectorXd BMatrix::apply(const Eigen::VectorXd& induced_moments) {
76-
auto scheme = m_options.summation_induced_fields;
77-
if (scheme == "direct") {
78-
return apply_direct(induced_moments);
79-
} else if (scheme == "fmm") {
80-
return apply_fast_summation(induced_moments, scheme);
81-
} else {
82-
throw std::invalid_argument("Invalid summation scheme for induced fields provided.");
83-
}
84-
}
85-
8642
Eigen::VectorXd BMatrix::apply_diagonal_inverse(const Eigen::VectorXd& in) {
8743
Eigen::VectorXd ret = Eigen::VectorXd::Zero(3 * m_n_polsites);
88-
8944
#pragma omp parallel for
9045
for (int i = 0; i < m_n_polsites; ++i) {
9146
int l = i * 3;
@@ -106,45 +61,6 @@ Eigen::VectorXd BMatrix::apply_diagonal(const Eigen::VectorXd& in) {
10661
return ret;
10762
}
10863

109-
Eigen::VectorXd BMatrix::gauss_seidel_update(Eigen::VectorXd induced_moments,
110-
const Eigen::VectorXd& total_fields) {
111-
#pragma omp parallel for
112-
for (int i = 0; i < m_n_polsites; ++i) {
113-
Eigen::Vector3d Ftmp = Eigen::Vector3d::Zero();
114-
int l = i * 3;
115-
Potential& pot1 = m_polsites[i];
116-
for (int j = 0; j < m_n_polsites; ++j) {
117-
int m = 3 * j;
118-
Potential& pot2 = m_polsites[j];
119-
if (pot1.excludes_site(pot2.index) || i == j) {
120-
continue;
121-
}
122-
Eigen::Vector3d diff = pot2.get_site_position() - pot1.get_site_position();
123-
Eigen::VectorXd T2;
124-
if (m_options.damp_induced) {
125-
Polarizability& alpha_i = pot1.get_polarizability();
126-
Polarizability& alpha_j = pot2.get_polarizability();
127-
double v = m_options.damping_factor_induced /
128-
std::pow(alpha_i.get_isotropic_value() * alpha_j.get_isotropic_value(),
129-
1.0 / 6.0);
130-
T2 = tensors::T2_damp_thole(diff, v);
131-
} else {
132-
T2 = tensors::T2(diff);
133-
}
134-
double fx = induced_moments[m + 0];
135-
double fy = induced_moments[m + 1];
136-
double fz = induced_moments[m + 2];
137-
// inline matrix-vector product, faster than unfolding and multiplying
138-
Ftmp[0] += T2[0] * fx + T2[1] * fy + T2[2] * fz;
139-
Ftmp[1] += T2[1] * fx + T2[3] * fy + T2[4] * fz;
140-
Ftmp[2] += T2[2] * fx + T2[4] * fy + T2[5] * fz;
141-
}
142-
Ftmp += total_fields.segment<3>(l);
143-
induced_moments.segment<3>(l) = pot1.get_polarizability().get_matrix() * Ftmp;
144-
}
145-
return induced_moments;
146-
}
147-
14864
Eigen::MatrixXd BMatrix::to_dense_matrix() {
14965
Eigen::MatrixXd B = Eigen::MatrixXd::Zero(m_n_polsites * 3, m_n_polsites * 3);
15066
#pragma omp parallel for

cppe/core/bmatrix.hh

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,33 @@ class BMatrix {
1313
size_t m_n_polsites; //!< number of polarizable sites
1414
PeOptions m_options;
1515
std::vector<Eigen::Matrix3d>
16-
m_alpha_inverse; //!< List with inverse polarizability tensors
17-
std::vector<std::vector<int>>
18-
m_exclusions; //!< List for each polarizable sites with sites that are excluded
16+
m_alpha_inverse; //!< List with inverse polarizability tensors
1917
std::vector<double> m_positions; //! list of all polarizabile site positions
2018
public:
2119
BMatrix(std::vector<Potential> polsites, const PeOptions& options)
2220
: m_polsites(polsites), m_options(options) {
2321
m_n_polsites = polsites.size();
2422
m_alpha_inverse.clear();
25-
m_exclusions.clear();
2623
m_positions = std::vector<double>(3 * m_n_polsites);
2724
std::transform(m_polsites.begin(), m_polsites.end(),
2825
std::back_inserter(m_alpha_inverse),
2926
[](Potential& p) -> Eigen::Matrix3d {
3027
return p.get_polarizability().get_matrix().inverse();
3128
});
32-
33-
m_exclusions = std::vector<std::vector<int>>(m_n_polsites);
3429
#pragma omp parallel for
3530
for (int i = 0; i < m_n_polsites; ++i) {
3631
Potential& pot1 = m_polsites[i];
3732
m_positions[i * 3 + 0] = pot1.m_x;
3833
m_positions[i * 3 + 1] = pot1.m_y;
3934
m_positions[i * 3 + 2] = pot1.m_z;
40-
std::vector<int> pot_excludes;
41-
for (int j = 0; j < m_n_polsites; ++j) {
42-
Potential& pot2 = m_polsites[j];
43-
if (pot1.excludes_site(pot2.index)) {
44-
pot_excludes.push_back(j);
45-
}
46-
}
47-
m_exclusions[i] = pot_excludes;
4835
}
4936
}
5037

5138
Eigen::VectorXd apply(const Eigen::VectorXd& induced_moments);
52-
Eigen::VectorXd apply_direct(const Eigen::VectorXd& induced_moments);
53-
Eigen::VectorXd apply_fast_summation(const Eigen::VectorXd& induced_moments,
54-
std::string scheme);
5539
Eigen::VectorXd apply_diagonal_inverse(const Eigen::VectorXd& in);
5640
Eigen::VectorXd apply_diagonal(const Eigen::VectorXd& in);
57-
Eigen::VectorXd gauss_seidel_update(Eigen::VectorXd induced_moments,
58-
const Eigen::VectorXd& total_fields);
5941
Eigen::MatrixXd to_dense_matrix();
6042
Eigen::MatrixXd direct_inverse();
61-
std::vector<std::vector<int>> get_exclusions() { return m_exclusions; }
6243
};
6344

6445
} // namespace libcppe

cppe/core/cppe_state.cc

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ void CppeState::update_induced_moments(Eigen::VectorXd elec_fields, bool elec_on
8989
}
9090
InducedMoments ind(m_potentials, m_options);
9191
ind.set_print_callback(m_printer);
92-
if (m_options.summation_induced_fields == "direct") {
93-
ind.compute(tmp_total_fields, m_induced_moments, m_make_guess);
94-
} else {
95-
m_induced_moments = ind.compute_cg(tmp_total_fields, m_induced_moments, m_make_guess);
96-
}
92+
m_induced_moments = ind.compute(tmp_total_fields, m_induced_moments, m_make_guess);
9793
if (m_make_guess) {
9894
m_make_guess = false;
9995
}
@@ -126,12 +122,7 @@ Eigen::MatrixXd CppeState::induced_moments_eef() {
126122
}
127123
for (int a = 0; a < 3; ++a) {
128124
Eigen::VectorXd ind_mom = Eigen::VectorXd::Zero(m_polarizable_sites * 3);
129-
if (m_options.summation_induced_fields == "direct") {
130-
ind.compute(Fdn.col(a), ind_mom, true);
131-
ret.col(a) = ind_mom;
132-
} else {
133-
ret.col(a) = ind.compute_cg(Fdn.col(a), ind_mom, true);
134-
}
125+
ret.col(a) = ind.compute(Fdn.col(a), ind_mom, true);
135126
}
136127
return ret;
137128
}

cppe/core/cppe_state.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class CppeState {
8383
Eigen::VectorXd get_multipole_fields() { return m_multipole_fields; }
8484

8585
std::string get_energy_summary_string();
86-
86+
8787
Eigen::MatrixXd induced_moments_eef();
8888

8989
Eigen::MatrixXd nuclear_interaction_energy_gradient();

0 commit comments

Comments
 (0)