Loading...
Searching...
No Matches
PairLocalKernel.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#include "PairLocalKernel.h"
5
6#include <algorithm>
7#include <cmath>
8#include <limits>
9#include <stdexcept>
10
15
16namespace gpr {
17namespace pairlocal {
18
19namespace {
20bool activePairtype(const AtomsConfiguration& configuration, Index_t pt)
21{
22 if (pt == static_cast<Index_t>(EMPTY)) return false;
23 return pt < configuration.n_pt;
24}
25} // namespace
26
27bool buildGeometry(const AtomsConfiguration& configuration,
28 const Eigen::MatrixXd& positions, Geometry& geometry)
29{
30 return buildGeometry(configuration, positions, geometry, 0.0);
31}
32
33bool buildGeometry(const AtomsConfiguration& configuration,
34 const Eigen::MatrixXd& positions, Geometry& geometry,
35 double far_radius)
36{
38 const Eigen::Index n_mov = configuration.atoms_mov.type.size();
39 const Eigen::Index n_fro = configuration.atoms_froz_active.type.size();
40 const Eigen::Index n_conf = positions.rows();
41 const Eigen::Index dof = 3 * n_mov;
42 if (n_conf <= 0 || n_mov <= 0 || positions.cols() < dof ||
43 configuration.n_pt <= 0)
44 return false;
45 if (far_radius > 0.0) {
46 const auto rows = radialPairFeatures(positions.leftCols(dof),
47 configuration, far_radius);
48 const Eigen::Index features = rows.front().size();
49 geometry.n_conf = n_conf;
50 geometry.dof = dof;
51 geometry.n_pt = configuration.n_pt;
52 geometry.q.resize(n_conf, features);
53 geometry.grad.resize(n_conf, 3 * features);
54 for (const auto& feature: rows.front())
55 geometry.pairs.push_back({feature.first,
56 feature.second < n_mov
57 ? feature.second
58 : -1 - (feature.second - n_mov),
59 feature.type});
60 for (Eigen::Index c = 0; c < n_conf; ++c) {
61 if (static_cast<Eigen::Index>(rows[c].size()) != features)
62 throw std::logic_error(
63 "far-field feature vocabulary changed across rows");
64 for (Eigen::Index f = 0; f < features; ++f) {
65 geometry.q(c, f) = rows[c][f].value;
66 geometry.grad.block(c, 3 * f, 1, 3) =
67 rows[c][f].gradient.transpose();
68 }
69 }
70 geometry.valid = true;
71 return true;
72 }
73 std::vector<Pair> pairs;
74 pairs.reserve(
75 static_cast<std::size_t>(n_mov * (n_mov - 1) / 2 + n_mov * n_fro));
76 for (Eigen::Index j = 0; j + 1 < n_mov; ++j) {
77 for (Eigen::Index i = j + 1; i < n_mov; ++i) {
78 const Index_t pt =
79 configuration.pairtype(configuration.atoms_mov.type(0, i),
80 configuration.atoms_mov.type(0, j));
81 if (!activePairtype(configuration, pt)) continue;
82 pairs.push_back(Pair{i, j, static_cast<Eigen::Index>(pt)});
83 }
84 }
85 for (Eigen::Index j = 0; j < n_mov; ++j) {
86 for (Eigen::Index i = 0; i < n_fro; ++i) {
87 const Index_t pt = configuration.pairtype(
88 configuration.atoms_froz_active.type(0, i),
89 configuration.atoms_mov.type(0, j));
90 if (!activePairtype(configuration, pt)) continue;
91 // The frozen partner is encoded below the moving range.
92 pairs.push_back(Pair{j, -1 - i, static_cast<Eigen::Index>(pt)});
93 }
94 }
95 const Eigen::Index n_pairs = static_cast<Eigen::Index>(pairs.size());
96 if (n_pairs == 0) return false;
97
98 const geometry::PairBox box(configuration.cell);
99 const PairCutoff cutoff(configuration.pair_cutoff,
100 configuration.pair_cutoff_width);
101 geometry.q.resize(n_conf, n_pairs);
102 geometry.grad.resize(n_conf, 3 * n_pairs);
103 const auto& frozen = configuration.atoms_froz_active.positions;
104 for (Eigen::Index c = 0; c < n_conf; ++c) {
105 for (Eigen::Index f = 0; f < n_pairs; ++f) {
106 const Pair& pair = pairs[static_cast<std::size_t>(f)];
107 const Eigen::Vector3d xa =
108 positions.block(c, 3 * pair.first, 1, 3).transpose();
109 Eigen::Vector3d xb;
110 if (pair.second >= 0) {
111 xb = positions.block(c, 3 * pair.second, 1, 3).transpose();
112 } else {
113 const Eigen::Index frozen_index = -1 - pair.second;
114 xb = Eigen::Vector3d(frozen.data() + 3 * frozen_index);
115 }
116 // Displacement from the partner to the first atom, so the
117 // gradient of 1 / r with respect to the first atom is
118 // -delta / r^3.
119 const Eigen::Vector3d delta = box.displacement(xb, xa);
120 const double r = delta.norm();
121 if (!(r > 0.0) || !std::isfinite(r)) return false;
122 if (configuration.pair_cutoff > 0.0) {
123 const auto radial = cutoff(r);
124 geometry.q(c, f) = radial.value;
125 geometry.grad.block(c, 3 * f, 1, 3) =
126 (radial.first * (delta / r)).transpose();
127 } else {
128 const double inv_r = 1.0 / r;
129 geometry.q(c, f) = inv_r;
130 geometry.grad.block(c, 3 * f, 1, 3) =
131 (-delta * (inv_r * inv_r * inv_r)).transpose();
132 }
133 }
134 }
135 geometry.n_conf = n_conf;
136 geometry.dof = dof;
137 geometry.n_pt = configuration.n_pt;
138 geometry.pairs = std::move(pairs);
139 geometry.valid = true;
140 return true;
141}
142
143Weights weightsFromLogParameters(const Eigen::VectorXd& theta,
144 Eigen::Index n_pt)
145{
146 Weights weights;
147 if (theta.size() < 2 || n_pt <= 0) return weights;
148 if (!std::isfinite(theta(0))) return weights;
149 weights.scalar_length = theta.size() == 2 && n_pt > 1;
150 if (!weights.scalar_length && theta.size() != 1 + n_pt) return weights;
151 weights.magnitude = numerics::exp(theta(0));
152 weights.inverse_square.resize(n_pt);
153 for (Eigen::Index t = 0; t < n_pt; ++t) {
154 const double log_length =
155 weights.scalar_length ? theta(1) : theta(1 + t);
156 if (std::isnan(log_length)) return weights;
157 const double w = numerics::exp(-2.0 * log_length);
158 if (!std::isfinite(w)) return weights;
159 weights.inverse_square(t) = w;
160 }
161 weights.valid = true;
162 return weights;
163}
164
166 : TileEvaluator(geometry, geometry, weights)
167{
168}
169
171 const Weights& weights)
172 : TileEvaluator(left, right, weights, {})
173{
174}
175
177 const std::vector<Eigen::Index>& derivative_groups)
178 : TileEvaluator(geometry, geometry, weights, derivative_groups)
179{
180}
181
183 const Weights& weights,
184 const std::vector<Eigen::Index>& derivative_groups)
185 : geometry_(left),
186 right_(right),
187 weights_(weights),
188 derivative_groups_(derivative_groups)
189{
190 Eigen::Index group_count = left.n_pt;
191 if (!derivative_groups_.empty()) {
192 if (derivative_groups_.size() != static_cast<std::size_t>(left.n_pt))
193 throw std::invalid_argument("Pair derivative group count mismatch");
194 std::vector<bool> seen(static_cast<std::size_t>(left.n_pt), false);
195 group_count = 0;
196 for (const Eigen::Index group: derivative_groups_) {
197 if (group < 0 || group >= left.n_pt)
198 throw std::invalid_argument("Invalid pair derivative group");
199 seen[static_cast<std::size_t>(group)] = true;
200 group_count = std::max(group_count, group + 1);
201 }
202 if (std::find(seen.begin(), seen.begin() + group_count, false) !=
203 seen.begin() + group_count)
204 throw std::invalid_argument(
205 "Pair derivative groups are not compact");
206 if (weights.scalar_length && group_count != 1)
207 throw std::invalid_argument("A shared length requires one group");
208 }
209 length_count_ = weights.scalar_length ? 1 : group_count;
210 s_t_.setZero(group_count);
211 coef_.setZero(static_cast<Eigen::Index>(left.pairs.size()));
212 g_.setZero(left.dof);
213 h_.setZero(left.dof);
214 gt_.setZero(group_count, left.dof);
215 ht_.setZero(group_count, left.dof);
216}
217
218void TileEvaluator::prepare(Eigen::Index i, Eigen::Index j)
219{
220 i_ = i;
221 j_ = j;
222 s_t_.setZero();
223 gt_.setZero();
224 ht_.setZero();
225 const Eigen::Index n_pairs =
226 static_cast<Eigen::Index>(geometry_.pairs.size());
227 for (Eigen::Index f = 0; f < n_pairs; ++f) {
228 const Pair& pair = geometry_.pairs[static_cast<std::size_t>(f)];
229 const double w = weights_.inverse_square(pair.type);
230 const double d = geometry_.q(i, f) - right_.q(j, f);
231 s_t_(groupIndex(pair.type)) += w * d * d;
232 const double coef = 2.0 * w * d;
233 coef_(f) = coef;
234 if (coef == 0.0) continue;
235 const auto gi = geometry_.grad.block(i, 3 * f, 1, 3);
236 const auto gj = right_.grad.block(j, 3 * f, 1, 3);
237 gt_.block(groupIndex(pair.type), 3 * pair.first, 1, 3) += coef * gi;
238 ht_.block(groupIndex(pair.type), 3 * pair.first, 1, 3) += coef * gj;
239 if (pair.second >= 0) {
240 gt_.block(groupIndex(pair.type), 3 * pair.second, 1, 3) -=
241 coef * gi;
242 ht_.block(groupIndex(pair.type), 3 * pair.second, 1, 3) -=
243 coef * gj;
244 }
245 }
246 g_ = gt_.colwise().sum().transpose();
247 h_ = ht_.colwise().sum().transpose();
248 kernel_ = weights_.magnitude * numerics::exp(-s_t_.sum());
249}
250
251void TileEvaluator::tile(Eigen::Index parameter, Eigen::MatrixXd& out) const
252{
253 const Eigen::Index dof = geometry_.dof;
254 const Eigen::Index size = dof + 1;
255 const Eigen::Index n_pairs =
256 static_cast<Eigen::Index>(geometry_.pairs.size());
257 // The four channel blocks cover every covariance entry. A covariance
258 // request writes directly into the caller's reusable tile storage.
259 Eigen::MatrixXd value_storage;
260 Eigen::MatrixXd& value = parameter == 0 ? out : value_storage;
261 value.resize(size, size);
262 if (parameter != 0) out.setZero(size, size);
263 value(0, 0) = kernel_;
264 value.block(0, 1, 1, dof) = kernel_ * h_.transpose();
265 value.block(1, 0, dof, 1) = -kernel_ * g_;
266 value.block(1, 1, dof, dof).noalias() = (-kernel_) * g_ * h_.transpose();
267 const auto add_pair_products = [&](Eigen::MatrixXd& target,
268 const double scale,
269 const Eigen::Index only_type) {
270 for (Eigen::Index f = 0; f < n_pairs; ++f) {
271 const Pair& pair = geometry_.pairs[static_cast<std::size_t>(f)];
272 if (only_type >= 0 && groupIndex(pair.type) != only_type) continue;
273 const double w = weights_.inverse_square(pair.type);
274 if (w == 0.0) continue;
275 const Eigen::Matrix3d product =
276 (scale * w) *
277 geometry_.grad.block(i_, 3 * f, 1, 3).transpose() *
278 right_.grad.block(j_, 3 * f, 1, 3);
279 const Eigen::Index a = 1 + 3 * pair.first;
280 target.block<3, 3>(a, a) += product;
281 if (pair.second >= 0) {
282 const Eigen::Index b = 1 + 3 * pair.second;
283 target.block<3, 3>(a, b) -= product;
284 target.block<3, 3>(b, a) -= product;
285 target.block<3, 3>(b, b) += product;
286 }
287 }
288 };
289 add_pair_products(value, 2.0 * kernel_, -1);
290 if (parameter == 0) return;
291 const auto add_length = [&](const Eigen::Index t) {
292 out += (2.0 * s_t_(t)) * value;
293 out.block(0, 1, 1, dof) += (-2.0 * kernel_) * ht_.row(t);
294 out.block(1, 0, dof, 1) += (2.0 * kernel_) * gt_.row(t).transpose();
295 out.block(1, 1, dof, dof).noalias() +=
296 (2.0 * kernel_) * gt_.row(t).transpose() * h_.transpose();
297 out.block(1, 1, dof, dof).noalias() +=
298 (2.0 * kernel_) * g_ * ht_.row(t);
299 add_pair_products(out, -4.0 * kernel_, t);
300 };
301 if (weights_.scalar_length) {
302 for (Eigen::Index t = 0; t < s_t_.size(); ++t)
303 add_length(t);
304 } else {
305 add_length(parameter - 1);
306 }
307}
308
309void TileEvaluator::directional(const Eigen::VectorXd& direction,
310 Eigen::MatrixXd& first,
311 Eigen::MatrixXd& second) const
312{
313 if (i_ < 0 || j_ < 0 || direction.size() != parameterCount() ||
314 !direction.allFinite())
315 throw std::invalid_argument(
316 "Prepared pair tile and finite parameter direction required");
317 const Eigen::Index dof = geometry_.dof;
318 Eigen::VectorXd g1 = Eigen::VectorXd::Zero(dof), h1 = g1;
319 Eigen::VectorXd g2 = g1, h2 = g1;
320 double a = direction(0), b = 0.0;
321 for (Eigen::Index t = 0; t < s_t_.size(); ++t) {
322 const double d = direction(1 + lengthIndex(t));
323 a += 2.0 * d * s_t_(t);
324 b -= 4.0 * d * d * s_t_(t);
325 g1.noalias() -= (2.0 * d) * gt_.row(t).transpose();
326 h1.noalias() -= (2.0 * d) * ht_.row(t).transpose();
327 g2.noalias() += (4.0 * d * d) * gt_.row(t).transpose();
328 h2.noalias() += (4.0 * d * d) * ht_.row(t).transpose();
329 }
330 const double c = a * a + b;
331 first.resize(dof + 1, dof + 1);
332 second.resize(dof + 1, dof + 1);
333 first(0, 0) = kernel_ * a;
334 second(0, 0) = kernel_ * c;
335 first.block(0, 1, 1, dof) = kernel_ * (a * h_ + h1).transpose();
336 second.block(0, 1, 1, dof) =
337 kernel_ * (c * h_ + 2.0 * a * h1 + h2).transpose();
338 first.block(1, 0, dof, 1) = -kernel_ * (a * g_ + g1);
339 second.block(1, 0, dof, 1) = -kernel_ * (c * g_ + 2.0 * a * g1 + g2);
340 first.block(1, 1, dof, dof).noalias() =
341 (-kernel_) *
342 (a * g_ * h_.transpose() + g1 * h_.transpose() + g_ * h1.transpose());
343 second.block(1, 1, dof, dof).noalias() =
344 (-kernel_) *
345 (c * g_ * h_.transpose() + (2.0 * a) * g1 * h_.transpose() +
346 (2.0 * a) * g_ * h1.transpose() + g2 * h_.transpose() +
347 2.0 * g1 * h1.transpose() + g_ * h2.transpose());
348 for (Eigen::Index f = 0;
349 f < static_cast<Eigen::Index>(geometry_.pairs.size()); ++f) {
350 const Pair& pair = geometry_.pairs[static_cast<std::size_t>(f)];
351 const double w = weights_.inverse_square(pair.type);
352 if (w == 0.0) continue;
353 const double d = direction(1 + lengthIndex(groupIndex(pair.type)));
354 const Eigen::Matrix3d product =
355 (2.0 * kernel_ * w) *
356 geometry_.grad.block(i_, 3 * f, 1, 3).transpose() *
357 right_.grad.block(j_, 3 * f, 1, 3);
358 const auto add = [&](Eigen::MatrixXd& target, double scale) {
359 const Eigen::Index left = 1 + 3 * pair.first;
360 target.block<3, 3>(left, left) += scale * product;
361 if (pair.second >= 0) {
362 const Eigen::Index right = 1 + 3 * pair.second;
363 target.block<3, 3>(left, right) -= scale * product;
364 target.block<3, 3>(right, left) -= scale * product;
365 target.block<3, 3>(right, right) += scale * product;
366 }
367 };
368 add(first, a - 2.0 * d);
369 add(second, c - 4.0 * a * d + 4.0 * d * d);
370 }
371}
372
373void TileEvaluator::contract(const double* W, Eigen::Index row_stride,
374 Eigen::Index col_stride, bool lower_only,
375 Eigen::VectorXd& out) const
376{
377 const Eigen::Index dof = geometry_.dof;
378 const Eigen::Index n_pt = s_t_.size();
379 const Eigen::Index n_pairs =
380 static_cast<Eigen::Index>(geometry_.pairs.size());
381 out.setZero(parameterCount());
382 const auto entry = [&](Eigen::Index c, Eigen::Index cc) {
383 if (lower_only && c < cc) std::swap(c, cc);
384 return W[c * row_stride + cc * col_stride];
385 };
386 // sigma_f = J_if^T W_grad J_jf, bucketed by type.
387 Eigen::VectorXd sigma_t = Eigen::VectorXd::Zero(n_pt);
388 for (Eigen::Index f = 0; f < n_pairs; ++f) {
389 const Pair& pair = geometry_.pairs[static_cast<std::size_t>(f)];
390 const double w = weights_.inverse_square(pair.type);
391 if (w == 0.0) continue;
392 const Eigen::Vector3d gi =
393 geometry_.grad.block(i_, 3 * f, 1, 3).transpose();
394 const Eigen::Vector3d gj =
395 right_.grad.block(j_, 3 * f, 1, 3).transpose();
396 const Eigen::Index a = 1 + 3 * pair.first;
397 const Eigen::Index b = pair.second >= 0 ? 1 + 3 * pair.second : -1;
398 double sigma = 0.0;
399 for (Eigen::Index d = 0; d < 3; ++d) {
400 for (Eigen::Index e = 0; e < 3; ++e) {
401 double weight = entry(a + d, a + e);
402 if (b >= 0)
403 weight += entry(b + d, b + e) - entry(a + d, b + e) -
404 entry(b + d, a + e);
405 sigma += weight * gi(d) * gj(e);
406 }
407 }
408 sigma_t(groupIndex(pair.type)) += w * sigma;
409 }
410 const double sigma_sum = sigma_t.sum();
411 if (lower_only) {
412 // The diagonal tile: d_f = 0, so g = h = 0 and every S_t = 0.
413 out(0) = kernel_ * (entry(0, 0) + 2.0 * sigma_sum);
414 for (Eigen::Index t = 0; t < n_pt; ++t)
415 out(1 + lengthIndex(t)) += kernel_ * (-4.0 * sigma_t(t));
416 return;
417 }
418 using Strided = Eigen::Map<const Eigen::MatrixXd, 0,
419 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>>;
420 const Strided grad_weight(
421 W + row_stride + col_stride, dof, dof,
422 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(col_stride, row_stride));
423 const Strided energy_row(
424 W + col_stride, 1, dof,
425 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(col_stride, row_stride));
426 const Strided energy_col(
427 W + row_stride, dof, 1,
428 Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>(col_stride, row_stride));
429 const Eigen::VectorXd p = grad_weight * h_;
430 const Eigen::VectorXd q = grad_weight.transpose() * g_;
431 const double row_h = (energy_row * h_)(0, 0);
432 const double col_g = (energy_col.transpose() * g_)(0, 0);
433 const double value =
434 kernel_ * (entry(0, 0) + row_h - col_g - g_.dot(p) + 2.0 * sigma_sum);
435 out(0) = value;
436 for (Eigen::Index t = 0; t < n_pt; ++t) {
437 const double row_ht = (energy_row * ht_.row(t).transpose())(0, 0);
438 const double col_gt =
439 (energy_col.transpose() * gt_.row(t).transpose())(0, 0);
440 const double derivative =
441 2.0 * s_t_(t) * value +
442 kernel_ * (-2.0 * row_ht + 2.0 * col_gt + 2.0 * gt_.row(t).dot(p) +
443 2.0 * q.dot(ht_.row(t)) - 4.0 * sigma_t(t));
444 out(1 + lengthIndex(t)) += derivative;
445 }
446}
447
448void TileEvaluator::quadratic(const double* alpha_i, const double* alpha_j,
449 Eigen::VectorXd& out) const
450{
451 const Eigen::Index dof = geometry_.dof;
452 const Eigen::Index n_pt = s_t_.size();
453 const Eigen::Index n_pairs =
454 static_cast<Eigen::Index>(geometry_.pairs.size());
455 out.setZero(parameterCount());
456 const Eigen::Map<const Eigen::VectorXd> ai(alpha_i + 1, dof);
457 const Eigen::Map<const Eigen::VectorXd> aj(alpha_j + 1, dof);
458 Eigen::VectorXd mixed_t = Eigen::VectorXd::Zero(n_pt);
459 for (Eigen::Index f = 0; f < n_pairs; ++f) {
460 const Pair& pair = geometry_.pairs[static_cast<std::size_t>(f)];
461 const double w = weights_.inverse_square(pair.type);
462 if (w == 0.0) continue;
463 const Eigen::Vector3d gi =
464 geometry_.grad.block(i_, 3 * f, 1, 3).transpose();
465 const Eigen::Vector3d gj =
466 right_.grad.block(j_, 3 * f, 1, 3).transpose();
467 Eigen::Vector3d ri = ai.segment<3>(3 * pair.first);
468 Eigen::Vector3d rj = aj.segment<3>(3 * pair.first);
469 if (pair.second >= 0) {
470 ri -= ai.segment<3>(3 * pair.second);
471 rj -= aj.segment<3>(3 * pair.second);
472 }
473 mixed_t(groupIndex(pair.type)) += w * gi.dot(ri) * gj.dot(rj);
474 }
475 const double left = alpha_i[0] - g_.dot(ai);
476 const double right = alpha_j[0] + h_.dot(aj);
477 const double value = kernel_ * (left * right + 2.0 * mixed_t.sum());
478 out(0) = value;
479 for (Eigen::Index t = 0; t < n_pt; ++t) {
480 const double derivative =
481 2.0 * s_t_(t) * value +
482 kernel_ * (2.0 * gt_.row(t).dot(ai) * right -
483 2.0 * left * ht_.row(t).dot(aj) - 4.0 * mixed_t(t));
484 out(1 + lengthIndex(t)) += derivative;
485 }
486}
487
488void TileEvaluator::action(const double* vector, bool transpose,
489 Eigen::MatrixXd& out) const
490{
491 const Eigen::Index dof = geometry_.dof;
492 const Eigen::Index groups = s_t_.size();
493 const Eigen::Map<const Eigen::VectorXd> gradient(vector + 1, dof);
494 Eigen::MatrixXd products = Eigen::MatrixXd::Zero(groups, dof);
495 for (Eigen::Index f = 0;
496 f < static_cast<Eigen::Index>(geometry_.pairs.size()); ++f) {
497 const Pair& pair = geometry_.pairs[static_cast<std::size_t>(f)];
498 const double weight = weights_.inverse_square(pair.type);
499 if (weight == 0.0) continue;
500 const Eigen::Vector3d left =
501 geometry_.grad.block(i_, 3 * f, 1, 3).transpose();
502 const Eigen::Vector3d right =
503 right_.grad.block(j_, 3 * f, 1, 3).transpose();
504 Eigen::Vector3d difference = gradient.segment<3>(3 * pair.first);
505 if (pair.second >= 0)
506 difference -= gradient.segment<3>(3 * pair.second);
507 const Eigen::Vector3d product =
508 transpose ? (weight * left.dot(difference)) * right
509 : (weight * right.dot(difference)) * left;
510 products.block<1, 3>(groupIndex(pair.type), 3 * pair.first) +=
511 product.transpose();
512 if (pair.second >= 0)
513 products.block<1, 3>(groupIndex(pair.type), 3 * pair.second) -=
514 product.transpose();
515 }
516 const double energy =
517 vector[0] + (transpose ? -g_.dot(gradient) : h_.dot(gradient));
518 out.setZero(parameterCount(), dof + 1);
519 out(0, 0) = kernel_ * energy;
520 out.row(0).tail(dof) = (2.0 * kernel_) * products.colwise().sum();
521 if (transpose)
522 out.row(0).tail(dof) += (kernel_ * energy) * h_.transpose();
523 else
524 out.row(0).tail(dof) -= (kernel_ * energy) * g_.transpose();
525 for (Eigen::Index t = 0; t < groups; ++t) {
526 const Eigen::Index parameter = 1 + lengthIndex(t);
527 const double projection =
528 transpose ? gt_.row(t).dot(gradient) : ht_.row(t).dot(gradient);
529 out.row(parameter) += (2.0 * s_t_(t)) * out.row(0);
530 out(parameter, 0) += (transpose ? 2.0 : -2.0) * kernel_ * projection;
531 if (transpose) {
532 out.row(parameter).tail(dof) +=
533 kernel_ *
534 (-2.0 * energy * ht_.row(t) +
535 2.0 * projection * h_.transpose() - 4.0 * products.row(t));
536 } else {
537 out.row(parameter).tail(dof) +=
538 kernel_ *
539 (2.0 * energy * gt_.row(t) + 2.0 * projection * g_.transpose() -
540 4.0 * products.row(t));
541 }
542 }
543}
544
545void TileEvaluator::diagonals(Eigen::MatrixXd& out) const
546{
547 const Eigen::Index dof = geometry_.dof;
548 const Eigen::Index n_pt = s_t_.size();
549 const Eigen::Index n_pairs =
550 static_cast<Eigen::Index>(geometry_.pairs.size());
551 out.setZero(parameterCount(), dof + 1);
552 // Per channel and type: sum_{f in t} w_f J_if,n J_jf,n.
553 Eigen::MatrixXd pair_diag = Eigen::MatrixXd::Zero(n_pt, dof);
554 for (Eigen::Index f = 0; f < n_pairs; ++f) {
555 const Pair& pair = geometry_.pairs[static_cast<std::size_t>(f)];
556 const double w = weights_.inverse_square(pair.type);
557 if (w == 0.0) continue;
558 const Eigen::Array3d product =
559 w * geometry_.grad.block(i_, 3 * f, 1, 3).transpose().array() *
560 right_.grad.block(j_, 3 * f, 1, 3).transpose().array();
561 pair_diag.block(groupIndex(pair.type), 3 * pair.first, 1, 3) +=
562 product.matrix().transpose();
563 if (pair.second >= 0)
564 pair_diag.block(groupIndex(pair.type), 3 * pair.second, 1, 3) +=
565 product.matrix().transpose();
566 }
567 const Eigen::RowVectorXd pair_diag_sum = pair_diag.colwise().sum();
568 out(0, 0) = kernel_;
569 for (Eigen::Index n = 0; n < dof; ++n)
570 out(0, 1 + n) = kernel_ * (-g_(n) * h_(n) + 2.0 * pair_diag_sum(n));
571 for (Eigen::Index t = 0; t < n_pt; ++t) {
572 const Eigen::Index p = 1 + lengthIndex(t);
573 out.row(p) += (2.0 * s_t_(t)) * out.row(0);
574 for (Eigen::Index n = 0; n < dof; ++n)
575 out(p, 1 + n) +=
576 kernel_ * (2.0 * gt_(t, n) * h_(n) + 2.0 * g_(n) * ht_(t, n) -
577 4.0 * pair_diag(t, n));
578 }
579}
580
581bool TiledLayoutSource::channelOrder(const Eigen::VectorXd& tile_ind,
582 Eigen::Index tile_size,
583 std::vector<Eigen::Index>& order)
584{
585 if (tile_ind.size() < tile_size) return false;
586 order.assign(static_cast<std::size_t>(tile_size), -1);
587 std::vector<bool> seen(static_cast<std::size_t>(tile_size), false);
588 for (Eigen::Index k = 0; k < tile_size; ++k) {
589 const auto channel = static_cast<Eigen::Index>(tile_ind(k));
590 if (channel < 0 || channel >= tile_size ||
591 seen[static_cast<std::size_t>(channel)])
592 return false;
593 seen[static_cast<std::size_t>(channel)] = true;
594 order[static_cast<std::size_t>(k)] = channel;
595 }
596 return true;
597}
598
599bool TiledLayoutSource::positionsOf(const Eigen::MatrixXd& x_tiled,
600 Eigen::Index tile_size, Eigen::Index dof,
601 Eigen::MatrixXd& positions)
602{
603 if (tile_size <= 0 || x_tiled.cols() < dof ||
604 x_tiled.rows() % tile_size != 0)
605 return false;
606 const Eigen::Index n_obs = x_tiled.rows() / tile_size;
607 positions.resize(n_obs, dof);
608 for (Eigen::Index i = 0; i < n_obs; ++i)
609 positions.row(i) = x_tiled.row(i * tile_size).head(dof);
610 return true;
611}
612
614 const Eigen::VectorXd& theta,
615 const Eigen::MatrixXd& x_tiled,
616 Eigen::Index tile_size,
617 const Eigen::VectorXd& tile_ind)
618{
619 return prepare(configuration, theta, x_tiled, tile_size, tile_ind, x_tiled,
620 tile_size, tile_ind);
621}
622
624 const AtomsConfiguration& configuration, const Eigen::VectorXd& theta,
625 const Eigen::MatrixXd& left, Eigen::Index left_tile,
626 const Eigen::VectorXd& left_ind, const Eigen::MatrixXd& right,
627 Eigen::Index right_tile, const Eigen::VectorXd& right_ind)
628{
629 return prepare(configuration, theta, left, left_tile, left_ind, right,
630 right_tile, right_ind, 0.0);
631}
632
634 const AtomsConfiguration& configuration, const Eigen::VectorXd& theta,
635 const Eigen::MatrixXd& left, Eigen::Index left_tile,
636 const Eigen::VectorXd& left_ind, const Eigen::MatrixXd& right,
637 Eigen::Index right_tile, const Eigen::VectorXd& right_ind,
638 double far_radius)
639{
640 evaluator_.reset();
641 const Eigen::Index dof = 3 * configuration.atoms_mov.type.size();
642 if (dof <= 0 || left_tile != dof + 1 || right_tile != dof + 1) return false;
643 if (!channelOrder(left_ind, left_tile, left_order_) ||
644 !channelOrder(right_ind, right_tile, right_order_))
645 return false;
646 Eigen::MatrixXd left_positions, right_positions;
647 if (!positionsOf(left, left_tile, dof, left_positions) ||
648 !positionsOf(right, right_tile, dof, right_positions))
649 return false;
650 if (!buildGeometry(configuration, left_positions, left_, far_radius))
651 return false;
652 if (&left == &right && left_tile == right_tile) {
653 right_ = left_;
654 } else if (!buildGeometry(configuration, right_positions, right_,
655 far_radius)) {
656 return false;
657 }
658 weights_ = weightsFromLogParameters(theta, left_.n_pt);
659 if (!weights_.valid) return false;
660 evaluator_.emplace(left_, right_, weights_);
661 return true;
662}
663
664void TiledLayoutSource::tile(Eigen::Index i, Eigen::Index j,
665 Eigen::MatrixXd& out)
666{
667 evaluator_->prepare(i, j);
668 evaluator_->tile(0, block_);
669 const Eigen::Index rows = static_cast<Eigen::Index>(left_order_.size());
670 const Eigen::Index cols = static_cast<Eigen::Index>(right_order_.size());
671 out.resize(rows, cols);
672 for (Eigen::Index l = 0; l < cols; ++l)
673 for (Eigen::Index k = 0; k < rows; ++k)
674 out(k, l) = block_(left_order_[static_cast<std::size_t>(k)],
675 right_order_[static_cast<std::size_t>(l)]);
676}
677
678} // namespace pairlocal
679} // namespace gpr
#define EMPTY
Definition Macros.h:39
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.
Definition MinImage.h:210
void directional(const Eigen::VectorXd &direction, Eigen::MatrixXd &first, Eigen::MatrixXd &second) const
void contract(const double *W, Eigen::Index row_stride, Eigen::Index col_stride, bool lower_only, Eigen::VectorXd &out) const
void diagonals(Eigen::MatrixXd &out) const
void prepare(Eigen::Index i, Eigen::Index j)
void action(const double *vector, bool transpose, Eigen::MatrixXd &out) const
TileEvaluator(const Geometry &geometry, const Weights &weights)
void quadratic(const double *alpha_i, const double *alpha_j, Eigen::VectorXd &out) const
void tile(Eigen::Index parameter, Eigen::MatrixXd &out) const
Eigen::Index parameterCount() const noexcept
1 + number of log lengths.
void tile(Eigen::Index i, Eigen::Index j, Eigen::MatrixXd &out)
bool prepare(const AtomsConfiguration &configuration, const Eigen::VectorXd &theta, const Eigen::MatrixXd &x_tiled, Eigen::Index tile_size, const Eigen::VectorXd &tile_ind)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double exp(double x) noexcept
bool buildGeometry(const AtomsConfiguration &configuration, const Eigen::MatrixXd &positions, Geometry &geometry)
Weights weightsFromLogParameters(const Eigen::VectorXd &theta, Eigen::Index n_pt)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
uint32_t Index_t
Definition Macros.h:17
std::vector< CutoffPairFeatures > radialPairFeatures(const Coord &positions, const AtomsConfiguration &configuration)
Stores configurations of all atoms in the system.
Definition Structures.h:832
AtomsPositionAndType atoms_froz_active
Info on active frozen atoms.
Definition Structures.h:841
AtomsPositionAndType atoms_mov
Info on moving atoms.
Definition Structures.h:844
Eigen::Index first
moving atom whose gradient is stored
Eigen::Index type
pair type (length-scale index)
Eigen::Index second
moving partner (gradient negated) or -1
Eigen::VectorXd inverse_square
per pair type