38 const Eigen::Index n_mov = configuration.
atoms_mov.
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)
45 if (far_radius > 0.0) {
47 configuration, far_radius);
48 const Eigen::Index features = rows.front().size();
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
58 : -1 - (feature.second - n_mov),
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) {
66 geometry.grad.block(c, 3 * f, 1, 3) =
67 rows[c][f].gradient.transpose();
73 std::vector<Pair> pairs;
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) {
81 if (!activePairtype(configuration, pt))
continue;
82 pairs.push_back(
Pair{i, j,
static_cast<Eigen::Index
>(pt)});
85 for (Eigen::Index j = 0; j < n_mov; ++j) {
86 for (Eigen::Index i = 0; i < n_fro; ++i) {
90 if (!activePairtype(configuration, pt))
continue;
92 pairs.push_back(
Pair{j, -1 - i,
static_cast<Eigen::Index
>(pt)});
95 const Eigen::Index n_pairs =
static_cast<Eigen::Index
>(pairs.size());
96 if (n_pairs == 0)
return false;
102 geometry.grad.resize(n_conf, 3 * n_pairs);
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();
111 xb = positions.block(c, 3 * pair.
second, 1, 3).transpose();
113 const Eigen::Index frozen_index = -1 - pair.
second;
114 xb = Eigen::Vector3d(frozen.data() + 3 * frozen_index);
120 const double r = delta.norm();
121 if (!(r > 0.0) || !std::isfinite(r))
return false;
123 const auto radial = cutoff(r);
125 geometry.grad.block(c, 3 * f, 1, 3) =
126 (radial.first * (delta / r)).transpose();
128 const double inv_r = 1.0 / r;
130 geometry.grad.block(c, 3 * f, 1, 3) =
131 (-delta * (inv_r * inv_r * inv_r)).transpose();
184 const std::vector<Eigen::Index>& derivative_groups)
188 derivative_groups_(derivative_groups)
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);
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);
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");
207 throw std::invalid_argument(
"A shared length requires one group");
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);
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;
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;
240 gt_.block(groupIndex(pair.
type), 3 * pair.
second, 1, 3) -=
242 ht_.block(groupIndex(pair.
type), 3 * pair.
second, 1, 3) -=
246 g_ = gt_.colwise().sum().transpose();
247 h_ = ht_.colwise().sum().transpose();
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());
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,
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 =
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;
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;
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);
301 if (weights_.scalar_length) {
302 for (Eigen::Index t = 0; t < s_t_.size(); ++t)
305 add_length(parameter - 1);
310 Eigen::MatrixXd& first,
311 Eigen::MatrixXd& second)
const
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();
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() =
342 (a * g_ * h_.transpose() + g1 * h_.transpose() + g_ * h1.transpose());
343 second.block(1, 1, dof, dof).noalias() =
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;
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;
368 add(first, a - 2.0 * d);
369 add(second, c - 4.0 * a * d + 4.0 * d * d);
374 Eigen::Index col_stride,
bool lower_only,
375 Eigen::VectorXd&
out)
const
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());
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];
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;
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);
403 weight += entry(b + d, b + e) - entry(a + d, b + e) -
405 sigma += weight * gi(d) * gj(e);
408 sigma_t(groupIndex(pair.
type)) += w * sigma;
410 const double sigma_sum = sigma_t.sum();
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));
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);
434 kernel_ * (entry(0, 0) + row_h - col_g - g_.dot(p) + 2.0 * sigma_sum);
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;
449 Eigen::VectorXd&
out)
const
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());
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);
470 ri -= ai.segment<3>(3 * pair.
second);
471 rj -= aj.segment<3>(3 * pair.
second);
473 mixed_t(groupIndex(pair.
type)) += w * gi.dot(ri) * gj.dot(rj);
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());
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;
489 Eigen::MatrixXd&
out)
const
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);
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) +=
513 products.block<1, 3>(groupIndex(pair.
type), 3 * pair.
second) -=
516 const double energy =
517 vector[0] + (transpose ? -g_.dot(gradient) : h_.dot(gradient));
519 out(0, 0) = kernel_ * energy;
520 out.row(0).tail(dof) = (2.0 * kernel_) * products.colwise().sum();
522 out.row(0).tail(dof) += (kernel_ * energy) * h_.transpose();
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;
532 out.row(parameter).tail(dof) +=
534 (-2.0 * energy * ht_.row(t) +
535 2.0 * projection * h_.transpose() - 4.0 * products.row(t));
537 out.row(parameter).tail(dof) +=
539 (2.0 * energy * gt_.row(t) + 2.0 * projection * g_.transpose() -
540 4.0 * products.row(t));
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());
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();
564 pair_diag.block(groupIndex(pair.
type), 3 * pair.
second, 1, 3) +=
565 product.matrix().transpose();
567 const Eigen::RowVectorXd pair_diag_sum = pair_diag.colwise().sum();
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)
576 kernel_ * (2.0 * gt_(t, n) * h_(n) + 2.0 * g_(n) * ht_(t, n) -
577 4.0 * pair_diag(t, n));
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,
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_))
646 Eigen::MatrixXd left_positions, right_positions;
647 if (!positionsOf(left, left_tile, dof, left_positions) ||
648 !positionsOf(right, right_tile, dof, right_positions))
650 if (!
buildGeometry(configuration, left_positions, left_, far_radius))
652 if (&left == &right && left_tile == right_tile) {
654 }
else if (!
buildGeometry(configuration, right_positions, right_,
659 if (!weights_.valid)
return false;
660 evaluator_.emplace(left_, right_, weights_);