38 if (!std::isfinite(far_radius) || far_radius < 0.0)
39 throw std::invalid_argument(
40 "far-field radius must be finite and nonnegative");
41 if (far_radius > 0.0 && configuration.
pair_cutoff > 0.0)
42 throw std::invalid_argument(
43 "far-field compression and pair cutoff are exclusive");
46 const Eigen::Index moving = configuration.
atoms_mov.
type.size();
48 if (positions.cols() != 3 * moving || !positions.allFinite() ||
50 throw std::invalid_argument(
51 "pair descriptor coordinates have incompatible dimensions");
52 std::array<double, 9> cell{};
53 std::copy_n(configuration.
cell, 9, cell.begin());
56 std::vector<double> points(3 * (moving + frozen));
58 points.data() + 3 * moving);
61 std::vector<std::pair<Eigen::Index, Eigen::Index>> fixed_pairs;
64 for (Eigen::Index first = 0; first < moving; ++first) {
65 const double* centre =
68 std::vector<Eigen::Matrix3d> tensors(
69 far_radius > 0.0 ? configuration.
n_pt : 0,
70 Eigen::Matrix3d::Zero());
71 for (Eigen::Index second = first + 1; second < moving + frozen;
73 if (centre && second >= moving) {
74 Eigen::Vector3d displacement;
77 const double r2 = displacement.squaredNorm();
78 if (r2 >= far_radius * far_radius) {
79 const auto type = configuration.
pairtype(
83 if (type >= 0 && type < configuration.
n_pt)
84 tensors[type] += displacement *
85 displacement.transpose() /
90 fixed_pairs.emplace_back(first, second);
92 for (Eigen::Index type = 0;
93 type < static_cast<Eigen::Index>(tensors.size()); ++type) {
94 if (tensors[type].isZero(0.0))
continue;
95 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> factor(
97 if (factor.info() != Eigen::Success)
98 throw std::runtime_error(
99 "far-field tensor factorization failed");
100 const double tolerance =
101 64 * std::numeric_limits<double>::epsilon() *
102 tensors[type].norm();
103 if (factor.eigenvalues().minCoeff() < -tolerance)
104 throw std::runtime_error(
105 "far-field tensor is not positive semidefinite");
106 for (Eigen::Index dim = 0; dim < 3; ++dim) {
109 std::sqrt(std::max(0.0, factor.eigenvalues()(dim)));
110 linear_features.push_back(
111 {first, moving + frozen + 3 * type + dim, type, 0.0,
112 scale * factor.eigenvectors().col(dim),
113 Eigen::Matrix3d::Zero()});
118 std::vector<CutoffPairFeatures> result(positions.rows());
119 for (Eigen::Index row = 0; row < positions.rows(); ++row) {
120 std::copy_n(positions.data() + row * positions.cols(), positions.cols(),
122 std::vector<std::pair<Eigen::Index, Eigen::Index>> pairs;
125 points.data(), moving + frozen, cell, periodic,
127 const auto first =
static_cast<Eigen::Index
>(
128 std::min(neighbor.first, neighbor.second));
129 const auto second =
static_cast<Eigen::Index
>(
130 std::max(neighbor.first, neighbor.second));
131 if (first == second || first >= moving)
continue;
132 pairs.emplace_back(first, second);
134 std::sort(pairs.begin(), pairs.end());
135 pairs.erase(std::unique(pairs.begin(), pairs.end()), pairs.end());
139 auto& features = result[row];
140 features.reserve(pairs.size() + linear_features.size());
141 for (
const auto& [first, second]: pairs) {
143 const auto second_type =
147 const Eigen::Index type =
148 configuration.
pairtype(first_type, second_type);
149 if (type < 0 || type >= configuration.
n_pt)
continue;
150 Eigen::Vector3d displacement;
152 points.data() + 3 * first, displacement.data());
153 const double radius = displacement.norm();
157 const auto radial = cutoff(radius);
158 const Eigen::Vector3d direction = displacement / radius;
159 const Eigen::Vector3d gradient = radial.first * direction;
160 const Eigen::Matrix3d hessian =
161 (radial.second - radial.first / radius) * direction *
162 direction.transpose() +
163 (radial.first / radius) * Eigen::Matrix3d::Identity();
165 {first, second, type, radial.value, gradient, hessian});
167 for (
auto feature: linear_features) {
168 Eigen::Vector3d offset;
170 points.data() + 3 * feature.first, offset.data());
171 feature.value = feature.gradient.dot(offset);
172 features.push_back(std::move(feature));
174 if (!linear_features.empty())
175 std::sort(features.begin(), features.end(),
176 [](
const auto& a,
const auto& b) {
177 return std::make_pair(a.first, a.second) <
178 std::make_pair(b.first, b.second);
199 const Eigen::Index coordinates = 3 * configuration.
atoms_mov.
type.size();
200 if (left.cols() < coordinates || right.cols() < coordinates)
201 throw std::invalid_argument(
202 "pair distance rows do not contain every moving coordinate");
205 configuration, far_radius);
207 configuration, far_radius);
208 const Eigen::Index types =
209 lengths.size() == 1 ? configuration.
n_pt : lengths.size();
210 std::vector<FieldMatrixd> result(
211 types, FieldMatrixd::Zero(left.rows(), right.rows()));
212 for (Eigen::Index i = 0; i < left.rows(); ++i) {
213 for (Eigen::Index j = 0; j < right.rows(); ++j) {
215 left_features[i], right_features[j],
217 const Eigen::Index type = a ? a->
type : b->
type;
218 if (type >= types)
return;
219 const double length =
220 lengths.data()[lengths.size() == 1 ? 0 : type];
223 result[type](i, j) +=
224 2.0 * delta * delta / (length * length);