250 :
conf(configuration),
251 cell(configuration.cell),
259 [](const Exec& ex, const Posterior::matrix_type& view) {
261 auto work = view;
262 return detail::kokkosFactorLower(ex, work);
263 },
264 [](const Exec& ex, const Posterior::matrix_type& L,
265 const Posterior::matrix_type& B, bool transpose) {
267 auto work = B;
268 detail::solve_lower(ex, L, work, transpose);
269 },
271 {
272 if (!(far >= 0.0) || !std::isfinite(far) || !(
cutoff >= 0.0) ||
274 !std::isfinite(
width) || (far > 0.0 &&
cutoff > 0.0) ||
276 throw std::invalid_argument(
277 "invalid molecular descriptor configuration");
281 throw std::invalid_argument(
282 "cutoff shell must be smaller than its radius");
283 }
284 const int n_frozen =
conf.atoms_froz_active.type.size();
285 if (
conf.atoms_froz_active.positions.size() != 3 * n_frozen ||
286 !
conf.atoms_froz_active.positions.allFinite())
287 throw std::invalid_argument(
288 "frozen coordinates have incompatible dimensions");
290 for (int i = 0; i < n_frozen; ++i)
291 for (int d = 0; d < 3; ++d)
292 frozen_host(i, d) =
293 conf.atoms_froz_active.positions.data()[3 * i + d];
295 std::vector<Pair> vocabulary;
296 for (
int a = 0; a <
moving; ++a) {
297 std::vector<Eigen::Matrix3d> tensor(
types, Eigen::Matrix3d::Zero());
298 if (far > 0.0 && (
conf.atoms_mov.positions.size() != 3 *
moving ||
299 !
conf.atoms_mov.positions.allFinite()))
300 throw std::invalid_argument(
301 "far field requires finite reference positions");
302 for (
int b = a + 1; b <
moving + n_frozen; ++b) {
303 const int at =
conf.atoms_mov.type(a);
305 ?
conf.atoms_mov.type(b)
307 if (at < 0 || bt < 0 || at >=
conf.pairtype.rows() ||
308 bt >=
conf.pairtype.cols())
309 throw std::invalid_argument(
310 "atom type is outside the pair vocabulary");
311 const int type =
conf.pairtype(at, bt);
312 if (type < 0 || type >=
types)
continue;
313 if (far > 0.0 && b >=
moving) {
314 double dr[3];
315 for (int d = 0; d < 3; ++d)
316 dr[d] =
conf.atoms_mov.positions.data()[3 * a + d] -
317 frozen_host(b -
moving, d);
319 throw std::invalid_argument(
320 "far field periodic image overflow");
321 const Eigen::Vector3d r(dr[0], dr[1], dr[2]);
322 const double r2 = r.squaredNorm();
323 if (r2 >= far * far) {
324 tensor[type] += r * r.transpose() / (r2 * r2 * r2);
325 continue;
326 }
327 }
328 vocabulary.push_back(Pair{a, b, type});
329 }
330 for (
int t = 0; t <
types; ++t) {
331 if (tensor[t].isZero(0.0)) continue;
332 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eig(
333 tensor[t]);
334 if (eig.info() != Eigen::Success ||
335 eig.eigenvalues().minCoeff() <
336 -64 * std::numeric_limits<double>::epsilon() *
337 tensor[t].norm())
338 throw std::runtime_error(
339 "far field tensor is not positive semidefinite");
340 for (int d = 0; d < 3; ++d) {
341 Pair p{a,
moving + n_frozen + 3 * t + d, t,
true};
342 const auto slope =
343 (std::sqrt(std::max(0.0, eig.eigenvalues()(d))) *
344 eig.eigenvectors().col(d))
345 .eval();
346 for (int c = 0; c < 3; ++c) {
347 p.centre[c] =
348 conf.atoms_mov.positions.data()[3 * a + c];
349 p.slope[c] = slope(c);
350 }
351 vocabulary.push_back(p);
352 }
353 }
354 }
355 pairs =
decltype(
pairs)(
"gpr pair vocabulary", vocabulary.size());
356 auto ph = Kokkos::create_mirror_view(
pairs);
357 first = V::ivector(
"gpr pair first", vocabulary.size());
358 second = V::ivector(
"gpr pair second", vocabulary.size());
359 pair_type = V::ivector(
"gpr pair type", vocabulary.size());
360 auto f = Kokkos::create_mirror_view(
first),
361 s = Kokkos::create_mirror_view(
second),
362 t = Kokkos::create_mirror_view(
pair_type);
363 for (std::size_t p = 0; p < vocabulary.size(); ++p) {
364 ph(p) = vocabulary[p];
365 f(p) = ph(p).first;
366 s(p) = ph(p).second;
367 t(p) = ph(p).type;
368 }
369 detail::audited_deep_copy(
exec,
pairs, ph,
370 detail::CopyRole::ToExecution);
371 detail::audited_deep_copy(
exec,
first, f,
372 detail::CopyRole::ToExecution);
374 detail::CopyRole::ToExecution);
376 detail::CopyRole::ToExecution);
377
378
379
380 std::vector<std::vector<int>> of_atom(
moving);
381 std::vector<int> shared(
static_cast<std::size_t
>(
moving) *
moving, -1);
382 for (std::size_t p = 0; p < vocabulary.size(); ++p) {
383 const int a = vocabulary[p].first, b = vocabulary[p].second;
384 of_atom[a].push_back(static_cast<int>(p));
386 of_atom[b].push_back(static_cast<int>(p));
387 shared[
static_cast<std::size_t
>(a) *
moving + b] =
388 static_cast<int>(p);
389 shared[
static_cast<std::size_t
>(b) *
moving + a] =
390 static_cast<int>(p);
391 }
392 }
393
394
395 for (auto& list: of_atom)
396 std::stable_sort(list.begin(), list.end(), [&](int l, int r) {
397 return vocabulary[l].type < vocabulary[r].type;
398 });
399 std::size_t adjacency = 0;
400 for (const auto& list: of_atom)
401 adjacency += list.size();
403 atom_pairs = V::ivector(
"gpr atom pairs", adjacency);
404 shared_pair = V::ivector(
"gpr shared pair", shared.size());
408 int position = 0;
409 for (
int a = 0; a <
moving; ++a) {
410 oh(a) = position;
411 for (const int pair: of_atom[a])
412 ah(position++) = pair;
413 }
415 for (std::size_t k = 0; k < shared.size(); ++k)
416 sh(k) = shared[k];
418 detail::CopyRole::ToExecution);
420 detail::CopyRole::ToExecution);
422 detail::CopyRole::ToExecution);
423 exec.fence(
"gpr upload fixed pair vocabulary");
425 vocabulary.size() * (sizeof(Pair) + 3 * sizeof(int)) +
426 (
moving + 1 + adjacency + shared.size()) *
sizeof(int);
429 }
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
PairTypeIndex< ExecSpace > build_pair_type_index(const ExecSpace &exec, const typename Views< ExecSpace >::const_ivector &pair_type, int n_types)
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
detail::TransferCounters transfers
kokkos_joint::PairTypeIndex< Exec > pair_index
Kokkos::View< Pair *, V::memory_space > pairs
V::matrix upload(const EigenMatrix &host, const char *label)
KokkosMolecularStats stats