Loading...
Searching...
No Matches
Distance.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5/*
6 * Distance.h
7 *
8 * Created on: 30 Jun 2020
9 * Author: Maxim Masterov
10 * Company: SURFSara
11 */
12
13#ifndef GPR_DISTANCE_H_
14#define GPR_DISTANCE_H_
15
16#include <cstdint>
17#include <vector>
18
22
23namespace gpr::geometry {
24class PairBox;
25}
26
27namespace aux {
28
29double calculate_aligned_rmsd(const gpr::Coord& P, const gpr::Coord& Q);
30
34 std::vector<double> signature_vec;
35
36 // Comparison operator for sorting
37 bool operator<(const AtomSignature& other) const
38 {
39 if (type != other.type) {
40 return type < other.type;
41 }
42 // Lexicographical comparison of the signature vectors
43 return signature_vec < other.signature_vec;
44 }
45};
46
47std::pair<gpr::Coord, gpr::FieldMatrixIdx> get_canonical_configuration(
48 const gpr::Coord& x_in, const gpr::FieldMatrixIdx& types_in);
49
61double solve_assignment_problem(const Eigen::MatrixXd& cost_matrix);
62
71std::vector<int> solve_assignment_indices(const Eigen::MatrixXd& cost_matrix);
72
73#ifdef USE_HIGHS
83double solve_assignment_problem_highs(const Eigen::MatrixXd& cost_matrix);
84#endif
85
105 std::vector<std::vector<double>> invr_x1; // [pair][n]
106 std::vector<std::vector<double>> invr_x2; // [pair][m]
107 std::vector<gpr::Index_t> pair_types;
108 // The coordinates the inverse distances were built from. Matching row
109 // counts alone does not establish that a cached entry describes the
110 // geometry being asked about: a call with different positions of the same
111 // shape would silently receive distances for the cached geometry. Keeping
112 // the coordinates makes the check exact, and costs O(n * 3 * N_atoms)
113 // against the O(n_pairs * n1 * n2) accumulation it guards.
116 double cell_cached[9] = {0., 0., 0., 0., 0., 0., 0., 0., 0.};
117 Eigen::Index n1 = 0, n2 = 0;
118 Eigen::Index n_mov = 0, n_fro = 0;
119 bool valid = false;
120
121 void clear()
122 {
123 invr_x1.clear();
124 invr_x2.clear();
125 pair_types.clear();
126 x1_cached.resize(0, 0);
127 x2_cached.resize(0, 0);
128 for (int i = 0; i < 9; ++i)
129 cell_cached[i] = 0.0;
130 n1 = n2 = n_mov = n_fro = 0;
131 valid = false;
132 }
133
135 [[nodiscard]] bool matches(const gpr::Coord& x1, const gpr::Coord& x2,
136 const double cell[9]) const
137 {
138 if (!valid || x1_cached.rows() != x1.rows() ||
139 x1_cached.cols() != x1.cols() || x2_cached.rows() != x2.rows() ||
140 x2_cached.cols() != x2.cols() || x1_cached != x1 ||
141 x2_cached != x2) {
142 return false;
143 }
144 for (int i = 0; i < 9; ++i) {
145 if (cell_cached[i] != cell[i]) return false;
146 }
147 return true;
148 }
149};
150
154class Distance {
155public:
156 Distance();
157 virtual ~Distance();
158
189 void dist_max1Dlog(const gpr::Coord& x1, const gpr::Coord& x2,
190 const gpr::AtomsConfiguration& conf_info,
191 gpr::FieldMatrixd& dist);
192
193 void dist_rmsd(const gpr::Coord& x1, const gpr::Coord& x2,
194 const gpr::AtomsConfiguration& conf_info,
195 gpr::FieldMatrixd& dist);
196
242 void dist_at(const gpr::Coord& x1, const gpr::Coord& x2,
243 const gpr::AtomsConfiguration& conf_info,
244 const gpr::FieldMatrixd& lengthscale, gpr::FieldMatrixd& dist,
245 bool take_sqrt = true);
246
250 double range_at(const gpr::Coord& positions,
251 const gpr::AtomsConfiguration& conf_info,
252 const gpr::FieldMatrixd& lengthscale);
253
270 void dist_at_vec(const gpr::Coord& x1, const gpr::Coord& x2,
271 const gpr::AtomsConfiguration& conf_info,
272 const gpr::FieldMatrixd& lengthscale,
273 std::vector<gpr::FieldMatrixd>& dist);
274
299 void mindist_interatomic(const gpr::Coord& x,
300 const gpr::AtomsConfiguration& conf_info,
301 gpr::FieldMatrixd& dist);
302
312 void dist_emd(const gpr::Coord& x1, const gpr::Coord& x2,
313 const gpr::AtomsConfiguration& conf_info,
314 gpr::FieldMatrixd& dist);
315
323 void cacheInverseDistances(const gpr::Coord& x1, const gpr::Coord& x2,
324 const gpr::AtomsConfiguration& conf_info);
325
333 void dist_at_cached(const gpr::Coord& x1, const gpr::Coord& x2,
334 const gpr::AtomsConfiguration& conf_info,
335 const gpr::FieldMatrixd& lengthscale,
336 gpr::FieldMatrixd& dist);
337
345 void dist_at_cached_sq(const gpr::Coord& x1, const gpr::Coord& x2,
346 const gpr::AtomsConfiguration& conf_info,
347 const gpr::FieldMatrixd& lengthscale,
348 gpr::FieldMatrixd& dist_sq);
349
351 {
352 return cache_.valid;
353 }
355 {
356 cache_.clear();
357 }
358
362 void setFarFieldRadius(double radius);
363 [[nodiscard]] double farFieldRadius() const
364 {
365 return far_field_radius_;
366 }
367
372 void setRadialFarFieldList(bool enabled)
373 {
374 radial_far_field_list_ = enabled;
375 clearCache();
376 }
377 [[nodiscard]] bool radialFarFieldList() const
378 {
379 return radial_far_field_list_;
380 }
381
384 [[nodiscard]] std::uint64_t radialListEntries() const
385 {
386 return radial_list_entries_;
387 }
388
391 [[nodiscard]] bool selectsRadialFarField(
392 const gpr::AtomsConfiguration& conf) const
393 {
394 if (conf.pair_cutoff > 0.0) return true;
395 return far_field_radius_ > 0.0 && radial_far_field_list_;
396 }
397
398private:
403 static constexpr Eigen::Index kReciprocalSqrtVectorThreshold = 16;
404
413 static void inverseDistancesToPoint(const double* data, Eigen::Index cols,
414 Eigen::Index off, double fx, double fy,
415 double fz, Eigen::Index n, double* out,
416 const gpr::geometry::PairBox& box);
417
423 static void inverseDistancesBetween(const double* data, Eigen::Index cols,
424 Eigen::Index off_a, Eigen::Index off_b,
425 Eigen::Index n, double* out,
426 const gpr::geometry::PairBox& box);
427
436 static void reciprocalSqrtInPlace(double* r2, Eigen::Index count);
437
446 static void buildInverseSquaredLengthscales(
447 const gpr::FieldMatrixd& lengthscale, gpr::Index_t n_pt,
449
458 static bool isActivePairtype(gpr::Index_t pt, Eigen::Index n_lengthscales)
459 {
460 return pt != static_cast<gpr::Index_t>(EMPTY) &&
461 static_cast<Eigen::Index>(pt) < n_lengthscales;
462 }
463
471 void accumulateCachedSquaredDistances(
472 const gpr::AtomsConfiguration& conf_info,
473 const gpr::FieldMatrixd& lengthscale, gpr::FieldMatrixd& dist);
474
475 InverseDistanceCache cache_;
476 double far_field_radius_ = 0.0;
477 bool radial_far_field_list_ = false;
478 std::uint64_t radial_list_entries_ = 0;
479 // Scratch for the x1 inverse distances, batched the same way as x2 so the
480 // reciprocal square root is evaluated SIMD-wide instead of once per
481 // configuration inside the accumulation loop.
482 std::vector<double> scratch_invr1_;
483 // Per-instance scratch buffer for x2 inverse distances. Hoisted out of
484 // dist_at / dist_at_vec / dist_at_cached_sq inner loops so the n_pair
485 // resize per call reuses capacity instead of round-tripping through
486 // malloc / free (the allocator otherwise takes 5-6% of post-alpha-K
487 // wall on dense-kernel fixtures). Each kernel call resizes the buffer
488 // to n2 and overwrites every element, so no cross-call invariant is
489 // implied.
490 std::vector<double> scratch_invr2_;
491};
492
493} /* namespace aux */
494
495#endif /* GPR_DISTANCE_H_ */
@ x
Definition Enums.h:28
#define EMPTY
Definition Macros.h:39
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).
bool hasCachedDistances() const
Definition Distance.h:350
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.
Definition Distance.cpp:796
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...
Definition Distance.cpp:620
bool selectsRadialFarField(const gpr::AtomsConfiguration &conf) const
Definition Distance.h:391
virtual ~Distance()
Definition Distance.cpp:297
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...
Definition Distance.cpp:462
void clearCache()
Definition Distance.h:354
void setRadialFarFieldList(bool enabled)
Definition Distance.h:372
double farFieldRadius() const
Definition Distance.h:363
bool radialFarFieldList() const
Definition Distance.h:377
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.
Definition Distance.cpp:958
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.
std::uint64_t radialListEntries() const
Definition Distance.h:384
void setFarFieldRadius(double radius)
Definition Distance.cpp:299
double range_at(const gpr::Coord &positions, const gpr::AtomsConfiguration &conf_info, const gpr::FieldMatrixd &lengthscale)
Definition Distance.cpp:533
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.
Definition Distance.cpp:410
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
std::vector< int > solve_assignment_indices(const Eigen::MatrixXd &cost_matrix)
Like solve_assignment_problem, but returns the optimal assignment.
Definition Distance.cpp:193
double solve_assignment_problem(const Eigen::MatrixXd &cost_matrix)
Solves the assignment problem using the Hungarian algorithm.
Definition Distance.cpp:179
std::pair< gpr::Coord, gpr::FieldMatrixIdx > get_canonical_configuration(const gpr::Coord &x_in, const gpr::FieldMatrixIdx &types_in)
Definition Distance.cpp:44
double calculate_aligned_rmsd(const gpr::Coord &P, const gpr::Coord &Q)
Definition Distance.cpp:283
Eigen::Matrix< Index_t, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixIdx
Definition Macros.h:29
FieldMatrixd Coord
Definition Macros.h:35
uint32_t Index_t
Definition Macros.h:17
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25
std::vector< double > signature_vec
Definition Distance.h:34
gpr::Index_t original_index
Definition Distance.h:32
bool operator<(const AtomSignature &other) const
Definition Distance.h:37
gpr::Index_t type
Definition Distance.h:33
Methods to calculate distances between atoms.
Definition Distance.h:104
bool matches(const gpr::Coord &x1, const gpr::Coord &x2, const double cell[9]) const
Whether the cache describes exactly this pair of coordinate sets.
Definition Distance.h:135
std::vector< std::vector< double > > invr_x1
Definition Distance.h:105
std::vector< std::vector< double > > invr_x2
Definition Distance.h:106
std::vector< gpr::Index_t > pair_types
Definition Distance.h:107
Stores configurations of all atoms in the system.
Definition Structures.h:832