Loading...
Searching...
No Matches
gpr::pairlocal Namespace Reference

Classes

struct  Pair
struct  Geometry
struct  Weights
class  TileEvaluator
class  TiledLayoutSource

Functions

bool buildGeometry (const AtomsConfiguration &configuration, const Eigen::MatrixXd &positions, Geometry &geometry)
bool buildGeometry (const AtomsConfiguration &configuration, const Eigen::MatrixXd &positions, Geometry &geometry, double far_radius)
Weights weightsFromLogParameters (const Eigen::VectorXd &theta, Eigen::Index n_pt)

Function Documentation

◆ buildGeometry() [1/2]

bool gpr::pairlocal::buildGeometry ( const AtomsConfiguration & configuration,
const Eigen::MatrixXd & positions,
Geometry & geometry )
nodiscard

Builds the geometry from the moving-atom coordinates of every configuration (one row each; extra columns are ignored). Pairs are enumerated as Distance::dist_at does. A finite cutoff uses the same switched inverse distance and gradient as PairCutoff. Geometry is shared across all Cartesian channel pairs and hyperparameter evaluations.

Definition at line 27 of file PairLocalKernel.cpp.

29{
30 return buildGeometry(configuration, positions, geometry, 0.0);
31}
bool buildGeometry(const AtomsConfiguration &configuration, const Eigen::MatrixXd &positions, Geometry &geometry)

◆ buildGeometry() [2/2]

bool gpr::pairlocal::buildGeometry ( const AtomsConfiguration & configuration,
const Eigen::MatrixXd & positions,
Geometry & geometry,
double far_radius )
nodiscard

Definition at line 33 of file PairLocalKernel.cpp.

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}
uint32_t Index_t
Definition Macros.h:17
std::vector< CutoffPairFeatures > radialPairFeatures(const Coord &positions, const AtomsConfiguration &configuration)
AtomsPositionAndType atoms_froz_active
Info on active frozen atoms.
Definition Structures.h:841
AtomsPositionAndType atoms_mov
Info on moving atoms.
Definition Structures.h:844
std::vector< Pair > pairs
Eigen::Index dof
3 x moving atoms
Eigen::MatrixXd q
n_conf x F
Eigen::MatrixXd grad
n_conf x 3F

◆ weightsFromLogParameters()

Weights gpr::pairlocal::weightsFromLogParameters ( const Eigen::VectorXd & theta,
Eigen::Index n_pt )
nodiscard

Definition at line 143 of file PairLocalKernel.cpp.

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}
double exp(double x) noexcept
Eigen::VectorXd inverse_square
per pair type