47 const Eigen::Index num_atoms = types_in.size();
52 std::vector<AtomSignature> atom_signatures(num_atoms);
55 for (Eigen::Index i = 0; i < num_atoms; ++i) {
56 atom_signatures[i].original_index = i;
57 atom_signatures[i].type = types_in(0, i);
60 for (Eigen::Index j = 0; j < num_atoms; ++j) {
74 atom_signatures[i].signature_vec.push_back(
75 r_ij > 1e-9 ? 1.0 / r_ij
76 : std::numeric_limits<double>::infinity());
80 std::sort(atom_signatures[i].signature_vec.begin(),
81 atom_signatures[i].signature_vec.end());
85 std::sort(atom_signatures.begin(), atom_signatures.end());
89 x_canonical.resize(1, num_atoms * 3);
92 for (Eigen::Index i = 0; i < num_atoms; ++i) {
93 gpr::Index_t old_index = atom_signatures[i].original_index;
95 types_canonical(0, i) = types_in(0, old_index);
98 return {x_canonical, types_canonical};
113std::vector<int> hungarian_assignment(
const Eigen::MatrixXd& cost_matrix)
115 const int n =
static_cast<int>(cost_matrix.rows());
116 if (n == 0)
return {};
117 if (cost_matrix.cols() != n || !cost_matrix.allFinite())
return {};
119 const Eigen::MatrixXd costs = -cost_matrix;
120 const double scale = std::max(1.0, cost_matrix.cwiseAbs().maxCoeff());
121 const double tight_tol = scale * 1e-9;
123 Eigen::VectorXd lx = costs.rowwise().maxCoeff();
124 Eigen::VectorXd ly = Eigen::VectorXd::Zero(n);
125 std::vector<int> xy(n, -1);
126 std::vector<int> yx(n, -1);
127 std::vector<bool> S(n), T(n);
129 auto match = [&](
int u,
auto& self) ->
bool {
131 for (
int v = 0; v < n; ++v) {
132 if (!T[v] && std::abs(lx(u) + ly(v) - costs(u, v)) < tight_tol) {
134 if (yx[v] == -1 || self(yx[v], self)) {
144 for (
int i = 0; i < n; ++i) {
149 for (
int pass = 0; pass <= n; ++pass) {
150 std::fill(S.begin(), S.end(),
false);
151 std::fill(T.begin(), T.end(),
false);
152 if (match(i, match))
break;
154 double delta = std::numeric_limits<double>::max();
155 for (
int u = 0; u < n; ++u) {
157 for (
int v = 0; v < n; ++v) {
159 delta = std::min(delta, lx(u) + ly(v) - costs(u, v));
162 if (!(delta > 0.0) || delta == std::numeric_limits<double>::max()) {
166 for (
int j = 0; j < n; ++j) {
167 if (S[j]) lx(j) -= delta;
168 if (T[j]) ly(j) += delta;
171 if (xy[i] == -1)
return {};
181 const int n =
static_cast<int>(cost_matrix.rows());
182 if (n == 0)
return 0.0;
184 const std::vector<int> xy = hungarian_assignment(cost_matrix);
185 if (xy.empty())
return std::numeric_limits<double>::infinity();
187 double min_cost = 0.0;
188 for (
int i = 0; i < n; ++i)
189 min_cost += cost_matrix(i, xy[i]);
195 return hungarian_assignment(cost_matrix);
201double solve_assignment_problem_highs(
const Eigen::MatrixXd& cost_matrix)
203 const int n = cost_matrix.rows();
204 if (n == 0)
return 0.0;
205 if (cost_matrix.cols() != n || !cost_matrix.allFinite()) {
206 return std::numeric_limits<double>::infinity();
211 highs.setOptionValue(
"output_flag",
false);
215 model.lp_.num_col_ = n * n;
216 model.lp_.num_row_ = 2 * n;
220 model.lp_.col_cost_.resize(n * n);
221 for (
int i = 0; i < n; ++i) {
222 for (
int j = 0; j < n; ++j) {
223 model.lp_.col_cost_[i * n + j] = cost_matrix(i, j);
228 model.lp_.col_lower_.assign(n * n, 0.0);
229 model.lp_.col_upper_.assign(n * n, 1.0);
233 model.lp_.row_lower_.assign(2 * n, 1.0);
234 model.lp_.row_upper_.assign(2 * n, 1.0);
239 model.lp_.a_matrix_.start_.resize(n * n + 1);
240 model.lp_.a_matrix_.index_.resize(2 * n * n);
241 model.lp_.a_matrix_.value_.resize(2 * n * n);
244 for (
int i = 0; i < n; ++i) {
245 for (
int j = 0; j < n; ++j) {
246 int var_idx = i * n + j;
247 model.lp_.a_matrix_.start_[var_idx] = nz;
250 model.lp_.a_matrix_.index_[nz] = i;
251 model.lp_.a_matrix_.value_[nz] = 1.0;
255 model.lp_.a_matrix_.index_[nz] = n + j;
256 model.lp_.a_matrix_.value_[nz] = 1.0;
260 model.lp_.a_matrix_.start_[n * n] = nz;
261 model.lp_.a_matrix_.format_ = MatrixFormat::kColwise;
264 highs.passModel(model);
265 HighsStatus
status = highs.run();
270 if (
status != HighsStatus::kOk ||
271 highs.getModelStatus() != HighsModelStatus::kOptimal) {
275 return std::numeric_limits<double>::infinity();
279 return highs.getInfo().objective_function_value;
286 if (P.cols() != Q.cols() || P.rows() < 1 || Q.rows() < 1) {
288 return std::numeric_limits<double>::max();
290 const Eigen::VectorXd p = P.row(0).transpose();
291 const Eigen::VectorXd q = Q.row(0).transpose();
301 if (!(radius >= 0.0) || !std::isfinite(radius)) {
302 throw std::invalid_argument(
303 "far-field radius must be finite and non-negative");
305 far_field_radius_ = radius;
309void Distance::inverseDistancesToPoint(
const double* data, Eigen::Index cols,
310 Eigen::Index off,
double fx,
double fy,
311 double fz, Eigen::Index n,
double*
out,
314 const double from[3] = {fx, fy, fz};
316 if (n >= kReciprocalSqrtVectorThreshold) {
317 for (Eigen::Index i = 0; i < n; ++i) {
318 const double* p = data + i * cols + off;
321 out[i] = dr[0] * dr[0] + dr[1] * dr[1] + dr[2] * dr[2];
323 gpr::hwy_kernels::recip_sqrt_inplace(
out,
static_cast<int64_t
>(n));
330 for (Eigen::Index i = 0; i < n; ++i) {
331 const double* p = data + i * cols + off;
334 out[i] = 1.0 / std::sqrt(dr[0] * dr[0] + dr[1] * dr[1] + dr[2] * dr[2]);
338void Distance::inverseDistancesBetween(
const double* data, Eigen::Index cols,
339 Eigen::Index off_a, Eigen::Index off_b,
340 Eigen::Index n,
double*
out,
341 const gpr::geometry::PairBox& box)
344 if (n >= kReciprocalSqrtVectorThreshold) {
345 for (Eigen::Index i = 0; i < n; ++i) {
346 const double* pa = data + i * cols + off_a;
347 const double* pb = data + i * cols + off_b;
350 out[i] = dr[0] * dr[0] + dr[1] * dr[1] + dr[2] * dr[2];
352 gpr::hwy_kernels::recip_sqrt_inplace(
out,
static_cast<int64_t
>(n));
356 for (Eigen::Index i = 0; i < n; ++i) {
357 const double* pa = data + i * cols + off_a;
358 const double* pb = data + i * cols + off_b;
361 out[i] = 1.0 / std::sqrt(dr[0] * dr[0] + dr[1] * dr[1] + dr[2] * dr[2]);
365void Distance::reciprocalSqrtInPlace(
double* r2, Eigen::Index count)
374 if (count >= kReciprocalSqrtVectorThreshold) {
375 gpr::hwy_kernels::recip_sqrt_inplace(r2,
static_cast<int64_t
>(count));
379 for (Eigen::Index i = 0; i < count; ++i) {
380 r2[i] = 1.0 / std::sqrt(r2[i]);
384void Distance::buildInverseSquaredLengthscales(
388 const Eigen::Index n_given = lengthscale.size();
393 const double l = *lengthscale.data();
394 const double inv_sq = 1. / (l * l);
395 s2.resize(1,
static_cast<Eigen::Index
>(n_pt));
396 s2.setConstant(inv_sq);
399 s2.resize(1, n_given);
400 for (Eigen::Index n = 0; n < n_given; ++n) {
401 const double l = *(lengthscale.data() + n);
402 *(s2.data() + n) = 1. / (l * l);
414 Eigen::Index n1 = x1.rows();
415 Eigen::Index n2 = x2.rows();
416 Eigen::Index num_cols = x1.cols();
419 if (num_cols != x2.cols()) {
431 std::vector<gpr::Coord> x2_canonical(
static_cast<std::size_t
>(n2));
432 for (Eigen::Index m = 0; m < n2; ++m) {
434 q.resize(1, num_cols);
435 for (Eigen::Index k = 0; k < num_cols; ++k) {
438 x2_canonical[
static_cast<std::size_t
>(m)] =
442 for (Eigen::Index n = 0; n < n1; ++n) {
446 p.resize(1, num_cols);
447 for (Eigen::Index k = 0; k < num_cols; ++k) {
453 for (Eigen::Index m = 0; m < n2; ++m) {
456 p_canonical_pair.first,
457 x2_canonical[
static_cast<std::size_t
>(m)]);
466 Eigen::Index n1 = x1.rows();
467 Eigen::Index n2 = x2.rows();
468 Eigen::Index N_mov = x1.cols() / 3;
477 for (Eigen::Index i = 0; i < N_mov - 1; ++i) {
478 for (Eigen::Index j = i + 1; j < N_mov; ++j) {
479 for (Eigen::Index n = 0; n < n1; ++n) {
483 for (Eigen::Index m = 0; m < n2; ++m) {
493 (r_ij_1 > 0. && r_ij_2 > 0.)
494 ? std::fabs(std::log(r_ij_2 / r_ij_1))
495 : std::numeric_limits<double>::infinity();
496 if (tmp > dist(n, m)) dist(n, m) = tmp;
505 for (Eigen::Index j = 0; j < N_mov; ++j) {
506 for (Eigen::Index i = 0; i < N_fro; ++i) {
507 Eigen::Vector3d froz_pos =
509 for (Eigen::Index n = 0; n < n1; ++n) {
513 for (Eigen::Index m = 0; m < n2; ++m) {
522 (r_ij_1 > 0. && r_ij_2 > 0.)
523 ? std::fabs(std::log(r_ij_2 / r_ij_1))
524 : std::numeric_limits<double>::infinity();
525 if (tmp > dist(n, m)) dist(n, m) = tmp;
537 const Eigen::Index dimensions = 3 * conf_info.
atoms_mov.
type.size();
538 if (positions.cols() < dimensions)
539 throw std::invalid_argument(
540 "distance range requires all moving coordinates");
541 if (positions.rows() == 0)
return 0.0;
542 if (!positions.leftCols(dimensions).allFinite())
543 return std::numeric_limits<double>::quiet_NaN();
545 std::map<std::vector<double>, Eigen::Index> seen;
546 gpr::Coord unique(positions.rows(), dimensions);
547 Eigen::Index count = 0;
548 for (Eigen::Index row = 0; row < positions.rows(); ++row) {
549 std::vector<double> key(
static_cast<std::size_t
>(dimensions));
550 for (Eigen::Index column = 0; column < dimensions; ++column)
551 key[
static_cast<std::size_t
>(column)] = positions(row, column);
552 if (seen.emplace(std::move(key), count).second)
553 unique.row(count++) = positions.row(row).head(dimensions);
555 unique.conservativeResize(count, Eigen::NoChange);
557 dist_at(unique, unique, conf_info, lengthscale, distances);
558 return distances.size() == 0 ? 0.0 : distances.maxCoeff();
563void addFarDipole(
const double* x1, Eigen::Index x1_cols, Eigen::Index n1,
564 const double* x2, Eigen::Index x2_cols, Eigen::Index n2,
565 Eigen::Index atom,
const double* frozen,
570 std::vector<gpr::FieldMatrixd>* per_type)
572 if (!(radius > 0.0) || n1 == 0 || n_frozen == 0)
return;
574 const Eigen::Index n_pt = s2.cols();
575 std::vector<std::array<double, 9>> tensor(
static_cast<std::size_t
>(n_pt));
577 for (Eigen::Index i = 0; i < n_frozen; ++i) {
578 const double* source = frozen + 3 * i;
583 static_cast<Eigen::Index
>(pt) >= n_pt)
587 const double two_s2 = 2.0 * s2(0,
static_cast<Eigen::Index
>(pt));
589 for (
int a = 0; a < 9; ++a)
590 block[a / 3][a % 3] = tensor[
static_cast<std::size_t
>(pt)][a];
592 for (
int a = 0; a < 9; ++a)
593 tensor[
static_cast<std::size_t
>(pt)][a] = block[a / 3][a % 3];
597 for (Eigen::Index n = 0; n < n1; ++n) {
598 const double* left = x1 + n * x1_cols + 3 * atom;
599 for (Eigen::Index m = 0; m < n2; ++m) {
600 const double* right = x2 + m * x2_cols + 3 * atom;
603 for (Eigen::Index pt = 0; pt < n_pt; ++pt) {
604 const auto& stored = tensor[
static_cast<std::size_t
>(pt)];
606 for (
int a = 0; a < 9; ++a)
607 block[a / 3][a % 3] = stored[a];
609 if (quad == 0.0)
continue;
610 if (total !=
nullptr) (*total)(n, m) += quad;
611 if (per_type !=
nullptr)
612 (*per_type)[
static_cast<std::size_t
>(pt)](n, m) += quad;
629 if (radial_far_field_list_) ++radial_list_entries_;
630 dist = gpr::FieldMatrixd::Zero(x1.rows(), x2.rows());
632 x1, x2, conf_info, lengthscale, far_field_radius_))
634 if (take_sqrt) dist = dist.array().sqrt().matrix();
639 Eigen::Index n1 = x1.rows();
640 Eigen::Index n2 = x2.rows();
648 buildInverseSquaredLengthscales(lengthscale, conf_info.
n_pt, s2);
663 const double*
const x1_data = x1.data();
664 const Eigen::Index x1_cols = x1.cols();
665 const double*
const x2_data = x2.data();
666 const Eigen::Index x2_cols = x2.cols();
670 scratch_invr2_.assign(n2, 0.0);
671 auto& invr2 = scratch_invr2_;
672 for (Eigen::Index j = 0; j < N_mov - 1; ++j) {
673 for (Eigen::Index i = j + 1; i < N_mov; ++i) {
677 if (!isActivePairtype(pt, s2.cols()))
continue;
678 double s2_val = s2(0, pt);
679 double two_s2 = 2. * s2_val;
680 const Eigen::Index x2_off_j = 3 * j;
681 const Eigen::Index x2_off_i = 3 * i;
688 inverseDistancesBetween(x2_data, x2_cols, x2_off_j, x2_off_i,
689 n2, invr2.data(), box);
690 const Eigen::Index x1_off_j = 3 * j;
691 const Eigen::Index x1_off_i = 3 * i;
695 scratch_invr1_.resize(
static_cast<std::size_t
>(n1));
696 inverseDistancesBetween(x1_data, x1_cols, x1_off_j, x1_off_i,
697 n1, scratch_invr1_.data(), box);
698 const double*
const invr1 = scratch_invr1_.data();
700#pragma omp parallel for schedule(static) if (n1 >= 4096 && \
701 omp_get_max_threads() > 1)
703 for (Eigen::Index n = 0; n < n1; ++n) {
704 const double invr_ij_1 = invr1[n];
707 gpr::hwy_kernels::accumulate_invr_diff_sq(
708 dist.data() + n * n2, invr2.data(), invr_ij_1,
712 for (Eigen::Index m = 0; m < n2; ++m) {
713 double invr_diff = invr_ij_1 - invr2[m];
714 dist(n, m) += two_s2 * invr_diff * invr_diff;
726 scratch_invr2_.assign(n2, 0.0);
727 auto& invr2 = scratch_invr2_;
728 const double*
const froz_data =
730 for (Eigen::Index j = 0; j < N_mov; ++j) {
731 for (Eigen::Index i = 0; i < N_fro; ++i) {
735 const double* froz_pos = froz_data + 3 * i;
736 const double fx = froz_pos[0];
737 const double fy = froz_pos[1];
738 const double fz = froz_pos[2];
742 if (!isActivePairtype(pt, s2.cols()))
continue;
743 if (far_field_radius_ > 0.0 && n1 > 0 &&
747 double s2_val = s2(0, pt);
748 double two_s2 = 2. * s2_val;
749 const Eigen::Index x2_off_j = 3 * j;
752 inverseDistancesToPoint(x2_data, x2_cols, x2_off_j, fx, fy, fz,
753 n2, invr2.data(), box);
754 const Eigen::Index x1_off_j = 3 * j;
756 scratch_invr1_.resize(
static_cast<std::size_t
>(n1));
757 inverseDistancesToPoint(x1_data, x1_cols, x1_off_j, fx, fy, fz,
758 n1, scratch_invr1_.data(), box);
759 const double*
const invr1 = scratch_invr1_.data();
761#pragma omp parallel for schedule(static) if (n1 >= 4096 && \
762 omp_get_max_threads() > 1)
764 for (Eigen::Index n = 0; n < n1; ++n) {
765 const double invr_ij_1 = invr1[n];
768 gpr::hwy_kernels::accumulate_invr_diff_sq(
769 dist.data() + n * n2, invr2.data(), invr_ij_1,
773 for (Eigen::Index m = 0; m < n2; ++m) {
774 double invr_diff = invr_ij_1 - invr2[m];
775 dist(n, m) += two_s2 * invr_diff * invr_diff;
782 if (far_field_radius_ > 0.0) {
783 addFarDipole(x1_data, x1_cols, n1, x2_data, x2_cols, n2, j,
784 froz_data, N_fro, conf_info, s2, box,
785 far_field_radius_, 1.0, &dist,
nullptr);
791 Eigen::Map<Eigen::ArrayXd>(dist.data(), dist.size()) =
792 Eigen::Map<Eigen::ArrayXd>(dist.data(), dist.size()).sqrt();
799 std::vector<gpr::FieldMatrixd>& dist)
802 if (radial_far_field_list_) ++radial_list_entries_;
805 for (
auto& part: dist)
810 Eigen::Index n1 = x1.rows();
811 Eigen::Index n2 = x2.rows();
816 buildInverseSquaredLengthscales(lengthscale, conf_info.
n_pt, s2);
821 dist.resize(
static_cast<std::size_t
>(s2.cols()));
822 for (std::size_t n = 0; n < dist.size(); ++n) {
823 dist[n].resize(n1, n2);
827 const double*
const x1_data = x1.data();
828 const Eigen::Index x1_cols = x1.cols();
829 const double*
const x2_data = x2.data();
830 const Eigen::Index x2_cols = x2.cols();
834 scratch_invr2_.assign(n2, 0.0);
835 auto& invr2 = scratch_invr2_;
836 for (Eigen::Index j = 0; j < N_mov - 1; ++j) {
837 for (Eigen::Index i = j + 1; i < N_mov; ++i) {
841 if (!isActivePairtype(pt, s2.cols()))
continue;
842 double s2_val = s2(0, pt);
843 double two_s2 = 2. * s2_val;
844 const Eigen::Index x2_off_j = 3 * j;
845 const Eigen::Index x2_off_i = 3 * i;
852 inverseDistancesBetween(x2_data, x2_cols, x2_off_j, x2_off_i,
853 n2, invr2.data(), box);
854 const Eigen::Index x1_off_j = 3 * j;
855 const Eigen::Index x1_off_i = 3 * i;
859 scratch_invr1_.resize(
static_cast<std::size_t
>(n1));
860 inverseDistancesBetween(x1_data, x1_cols, x1_off_j, x1_off_i,
861 n1, scratch_invr1_.data(), box);
862 const double*
const invr1 = scratch_invr1_.data();
864#pragma omp parallel for schedule(static) if (n1 >= 4096 && \
865 omp_get_max_threads() > 1)
867 for (Eigen::Index n = 0; n < n1; ++n) {
868 const double invr_ij_1 = invr1[n];
871 gpr::hwy_kernels::accumulate_invr_diff_sq(
872 dist[pt].data() + n * n2, invr2.data(), invr_ij_1,
876 for (Eigen::Index m = 0; m < n2; ++m) {
877 double invr_diff = invr_ij_1 - invr2[m];
878 dist[pt](n, m) -= two_s2 * invr_diff * invr_diff;
890 scratch_invr2_.assign(n2, 0.0);
891 auto& invr2 = scratch_invr2_;
892 const double*
const froz_data =
894 for (Eigen::Index j = 0; j < N_mov; ++j) {
895 for (Eigen::Index i = 0; i < N_fro; ++i) {
896 const double* froz_pos = froz_data + 3 * i;
897 const double fx = froz_pos[0];
898 const double fy = froz_pos[1];
899 const double fz = froz_pos[2];
903 if (!isActivePairtype(pt, s2.cols()))
continue;
904 if (far_field_radius_ > 0.0 && n1 > 0 &&
908 double s2_val = s2(0, pt);
909 double two_s2 = 2. * s2_val;
910 const Eigen::Index x2_off_j = 3 * j;
913 inverseDistancesToPoint(x2_data, x2_cols, x2_off_j, fx, fy, fz,
914 n2, invr2.data(), box);
915 const Eigen::Index x1_off_j = 3 * j;
917 scratch_invr1_.resize(
static_cast<std::size_t
>(n1));
918 inverseDistancesToPoint(x1_data, x1_cols, x1_off_j, fx, fy, fz,
919 n1, scratch_invr1_.data(), box);
920 const double*
const invr1 = scratch_invr1_.data();
922#pragma omp parallel for schedule(static) if (n1 >= 4096 && \
923 omp_get_max_threads() > 1)
925 for (Eigen::Index n = 0; n < n1; ++n) {
926 const double invr_ij_1 = invr1[n];
929 gpr::hwy_kernels::accumulate_invr_diff_sq(
930 dist[pt].data() + n * n2, invr2.data(), invr_ij_1,
934 for (Eigen::Index m = 0; m < n2; ++m) {
935 double invr_diff = invr_ij_1 - invr2[m];
936 dist[pt](n, m) -= two_s2 * invr_diff * invr_diff;
943 if (far_field_radius_ > 0.0) {
944 addFarDipole(x1_data, x1_cols, n1, x2_data, x2_cols, n2, j,
945 froz_data, N_fro, conf_info, s2, box,
946 far_field_radius_, -1.0,
nullptr, &dist);
963 const auto num_configs =
x.rows();
964 const auto num_moving_atoms =
x.cols() / 3;
966 const auto num_frozen_atoms = frozen_pos.cols() / 3;
969 if (num_moving_atoms == 0) {
974 dist.resize(num_configs, num_moving_atoms);
975 dist.setConstant(DBL_MAX);
980 for (Eigen::Index n = 0; n < num_configs; ++n) {
982 if (num_moving_atoms > 1) {
983 for (Eigen::Index i = 0; i < num_moving_atoms - 1; ++i) {
984 for (Eigen::Index j = i + 1; j < num_moving_atoms; ++j) {
992 dist(n, i) = std::min(dist(n, i), r_ij);
993 dist(n, j) = std::min(dist(n, j), r_ij);
999 if (num_frozen_atoms > 0) {
1000 for (Eigen::Index j = 0; j < num_moving_atoms;
1002 for (Eigen::Index i = 0; i < num_frozen_atoms;
1011 dist(n, j) = std::min(dist(n, j), r_ij);
1022 Eigen::Index n1 = x1.rows();
1023 Eigen::Index n2 = x2.rows();
1024 Eigen::Index num_atoms = x1.cols() / 3;
1026 if (num_atoms != x2.cols() / 3) {
1031 dist.resize(n1, n2);
1035 std::map<gpr::Index_t, std::vector<gpr::Index_t>> indices_by_type;
1036 for (Eigen::Index i = 0; i < types.size(); ++i) {
1037 indices_by_type[types(0, i)].push_back(i);
1040 for (Eigen::Index n = 0; n < n1; ++n) {
1041 for (Eigen::Index m = 0; m < n2; ++m) {
1042 double max_of_avg_type_emds = 0.0;
1043 double total_emd_for_type = 0.0;
1046 for (
auto const& [type, indices]: indices_by_type) {
1047 int num_type_atoms = indices.size();
1048 if (num_type_atoms == 0)
continue;
1051 Eigen::MatrixXd cost_matrix(num_type_atoms, num_type_atoms);
1052 for (
int i = 0; i < num_type_atoms; ++i) {
1053 for (
int j = 0; j < num_type_atoms; ++j) {
1064 total_emd_for_type =
1065 solve_assignment_problem_highs(cost_matrix);
1070 double avg_emd_for_type = total_emd_for_type / num_type_atoms;
1073 if (avg_emd_for_type > max_of_avg_type_emds) {
1074 max_of_avg_type_emds = avg_emd_for_type;
1080 dist(n, m) = max_of_avg_type_emds;
1090 cache_.x1_cached = x1;
1091 cache_.x2_cached = x2;
1092 for (
int i = 0; i < 9; ++i)
1093 cache_.cell_cached[i] = conf_info.
cell[i];
1094 cache_.n1 = x1.rows();
1095 cache_.n2 = x2.rows();
1100 Eigen::Index N_mov = cache_.n_mov;
1101 Eigen::Index N_fro = cache_.n_fro;
1102 Eigen::Index n1 = cache_.n1;
1103 Eigen::Index n2 = cache_.n2;
1107 for (Eigen::Index j = 0; j < N_mov - 1; ++j) {
1108 for (Eigen::Index i = j + 1; i < N_mov; ++i) {
1112 if (!isActivePairtype(
1113 pt,
static_cast<Eigen::Index
>(conf_info.
n_pt)))
1115 cache_.pair_types.push_back(pt);
1117 std::vector<double> ir1(n1), ir2(n2);
1118 for (Eigen::Index n = 0; n < n1; ++n) {
1123 for (Eigen::Index m = 0; m < n2; ++m) {
1128 cache_.invr_x1.push_back(std::move(ir1));
1129 cache_.invr_x2.push_back(std::move(ir2));
1136 for (Eigen::Index j = 0; j < N_mov; ++j) {
1137 for (Eigen::Index i = 0; i < N_fro; ++i) {
1138 Eigen::Vector3d froz_pos =
1143 if (!isActivePairtype(
1144 pt,
static_cast<Eigen::Index
>(conf_info.
n_pt)))
1146 cache_.pair_types.push_back(pt);
1148 std::vector<double> ir1(n1), ir2(n2);
1149 for (Eigen::Index n = 0; n < n1; ++n) {
1154 for (Eigen::Index m = 0; m < n2; ++m) {
1159 cache_.invr_x1.push_back(std::move(ir1));
1160 cache_.invr_x2.push_back(std::move(ir2));
1165 cache_.valid =
true;
1168void Distance::accumulateCachedSquaredDistances(
1172 const Eigen::Index n1 = cache_.
n1;
1173 const Eigen::Index n2 = cache_.
n2;
1175 dist.resize(n1, n2);
1179 buildInverseSquaredLengthscales(lengthscale, conf_info.
n_pt, s2);
1184 for (std::size_t p = 0; p < cache_.
pair_types.size(); ++p) {
1186 if (!isActivePairtype(pt, s2.cols()))
continue;
1187 const double two_s2 = 2.0 * s2(0, pt);
1188 const double* ir1 = cache_.
invr_x1[p].data();
1189 const double* ir2 = cache_.
invr_x2[p].data();
1192#pragma omp parallel for schedule(static) if (n1 >= 4096 && \
1193 omp_get_max_threads() > 1)
1195 for (Eigen::Index n = 0; n < n1; ++n) {
1196 const double invr_ij_1 = ir1[n];
1199 gpr::hwy_kernels::accumulate_invr_diff_sq(
1200 dist.data() + n * n2, ir2, invr_ij_1, two_s2, n2);
1203 for (Eigen::Index m = 0; m < n2; ++m) {
1204 const double invr_diff = invr_ij_1 - ir2[m];
1205 dist(n, m) += two_s2 * invr_diff * invr_diff;
1219 if (far_field_radius_ > 0.0 || conf_info.
pair_cutoff > 0.0 ||
1220 !cache_.matches(x1, x2, conf_info.
cell)) {
1221 dist_at(x1, x2, conf_info, lengthscale, dist);
1225 accumulateCachedSquaredDistances(conf_info, lengthscale, dist);
1226 Eigen::Map<Eigen::ArrayXd>(dist.data(), dist.size()) =
1227 Eigen::Map<Eigen::ArrayXd>(dist.data(), dist.size()).sqrt();
1235 if (far_field_radius_ > 0.0 || conf_info.
pair_cutoff > 0.0 ||
1236 !cache_.matches(x1, x2, conf_info.
cell)) {
1237 dist_at(x1, x2, conf_info, lengthscale, dist_sq,
false);
1242 accumulateCachedSquaredDistances(conf_info, lengthscale, dist_sq);
void dist_at_cached_sq(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthscale, gpr::FieldMatrixd &dist_sq)
Like dist_at_cached but returns SQUARED distances (skips final sqrt).
void dist_at_cached(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthscale, gpr::FieldMatrixd &dist)
Compute dist_at using cached inverse distances.
void dist_at_vec(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthscale, std::vector< gpr::FieldMatrixd > &dist)
Similar to dist_at but calculates vector of distances for every pair of moving and frozen atoms.
void cacheInverseDistances(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info)
Pre-compute and cache raw inverse distances for given positions.
void dist_at(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthscale, gpr::FieldMatrixd &dist, bool take_sqrt=true)
Give the distance between two atomic configurations as defined in the special GPstuff covariance func...
bool selectsRadialFarField(const gpr::AtomsConfiguration &conf) const
void dist_max1Dlog(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, gpr::FieldMatrixd &dist)
Give the maximum difference in the logarithmic inter-atomic distances between atomic configurations C...
void mindist_interatomic(const gpr::Coord &x, const gpr::AtomsConfiguration &conf_info, gpr::FieldMatrixd &dist)
Give the distance from each moving atom to its nearest neighbour atom in configuration C.
void dist_emd(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, gpr::FieldMatrixd &dist)
Calculates the Earth Mover's Distance between sets of configurations.
void setFarFieldRadius(double radius)
double range_at(const gpr::Coord &positions, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthscale)
void dist_rmsd(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, gpr::FieldMatrixd &dist)
Calculates the aligned Root Mean Square Deviation (RMSD) between sets of configurations.
void displacement(const double p[3], const double q[3], double dr[3]) const
Displacement from p to q. Cartesian q - p when the cell is zero.
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
bool farFromCentre(const gpr::geometry::PairBox &box, const double centre[3], const double frozen[3], double radius)
std::vector< int > solve_assignment_indices(const Eigen::MatrixXd &cost_matrix)
Like solve_assignment_problem, but returns the optimal assignment.
double solve_assignment_problem(const Eigen::MatrixXd &cost_matrix)
Solves the assignment problem using the Hungarian algorithm.
std::pair< gpr::Coord, gpr::FieldMatrixIdx > get_canonical_configuration(const gpr::Coord &x_in, const gpr::FieldMatrixIdx &types_in)
double farQuadratic(const double tensor[3][3], const double delta[3])
void farFieldDifference(const gpr::geometry::PairBox &box, const double centre[3], const double left[3], const double right[3], double delta[3])
double calculate_aligned_rmsd(const gpr::Coord &P, const gpr::Coord &Q)
void addFarSource(double tensor[3][3], const double r[3], double weight)
const double * farFieldCentre(const gpr::AtomsConfiguration &conf, Eigen::Index atom)
double rigidAlignedRmsd(const Eigen::VectorXd &reference, const Eigen::VectorXd &mobile)
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
void set(FieldMatrixd &mat, Index_t i, const Eigen::Vector3d &vec)
std::vector< FieldMatrixd > radialPairSquaredDistances(const Coord &left, const Coord &right, const AtomsConfiguration &configuration, const FieldMatrixd &lengths, double far_radius)
Eigen::Matrix< Index_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixIdx
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
std::vector< std::vector< double > > invr_x1
std::vector< std::vector< double > > invr_x2
std::vector< gpr::Index_t > pair_types
Stores configurations of all atoms in the system.
AtomsPositionAndType atoms_froz_active
Info on active frozen atoms.
AtomsPositionAndType atoms_mov
Info on moving atoms.