Loading...
Searching...
No Matches
PopulationPrior.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#ifndef GPR_PRIOR_POPULATION_PRIOR_H_
5#define GPR_PRIOR_POPULATION_PRIOR_H_
6
7#include <Eigen/Core>
8#include <cmath>
9#include <istream>
10#include <sstream>
11#include <string>
12#include <utility>
13#include <vector>
14
16
17namespace gpr {
18namespace priors {
19
31 int element_a = 0;
32 int element_b = 0;
33 int cells = 0;
34 double log_length_mu = 0.0;
35 double log_length_sigma = 0.0;
36};
37
39public:
40 [[nodiscard]] bool empty() const noexcept
41 {
42 return pairs_.empty();
43 }
44 [[nodiscard]] std::size_t size() const noexcept
45 {
46 return pairs_.size();
47 }
48 void clear()
49 {
50 pairs_.clear();
51 }
53 {
54 if (prior.element_a > prior.element_b)
55 std::swap(prior.element_a, prior.element_b);
56 for (auto& existing: pairs_) {
57 if (existing.element_a == prior.element_a &&
58 existing.element_b == prior.element_b) {
59 existing = prior;
60 return;
61 }
62 }
63 pairs_.push_back(prior);
64 }
65
66 [[nodiscard]] const PopulationPairPrior* find(int element_a,
67 int element_b) const noexcept
68 {
69 if (element_a > element_b) std::swap(element_a, element_b);
70 for (const auto& prior: pairs_)
71 if (prior.element_a == element_a && prior.element_b == element_b)
72 return &prior;
73 return nullptr;
74 }
75
77 bool read(std::istream& input)
78 {
79 std::string line;
80 while (std::getline(input, line)) {
81 const auto hash = line.find('#');
82 if (hash != std::string::npos) line.erase(hash);
83 std::istringstream row(line);
85 if (!(row >> prior.element_a)) continue;
86 if (!(row >> prior.element_b >> prior.cells >>
87 prior.log_length_mu >> prior.log_length_sigma) ||
88 !std::isfinite(prior.log_length_mu) ||
89 !std::isfinite(prior.log_length_sigma) ||
90 prior.log_length_sigma < 0.0)
91 return false;
92 add(prior);
93 }
94 return true;
95 }
96
99 [[nodiscard]] std::vector<const PopulationPairPrior*> perPairtype(
100 const AtomsConfiguration& conf) const
101 {
102 std::vector<const PopulationPairPrior*> entries(
103 static_cast<std::size_t>(conf.n_pt), nullptr);
104 if (pairs_.empty() || conf.n_pt <= 0) return entries;
105 const Eigen::Index n_types = conf.pairtype.rows();
106 std::vector<int> type_to_Z(static_cast<std::size_t>(n_types), -1);
107 const auto& types = conf.type.size() == conf.atomicNrs.size()
108 ? conf.type
109 : conf.atoms_mov.type;
110 const Eigen::Index count =
111 std::min(types.size(), conf.atomicNrs.size());
112 for (Eigen::Index atom = 0; atom < count; ++atom) {
113 const auto type = static_cast<Eigen::Index>(types.data()[atom]);
114 if (type < 0 || type >= n_types) continue;
115 if (type_to_Z[static_cast<std::size_t>(type)] == -1)
116 type_to_Z[static_cast<std::size_t>(type)] =
117 conf.atomicNrs.data()[atom];
118 }
119 for (Eigen::Index ti = 0; ti < n_types; ++ti) {
120 for (Eigen::Index tj = ti; tj < n_types; ++tj) {
121 if (ti >= conf.pairtype.rows() || tj >= conf.pairtype.cols())
122 continue;
123 const auto k = static_cast<Eigen::Index>(conf.pairtype(ti, tj));
124 if (k < 0 || k >= conf.n_pt) continue;
125 const int za = type_to_Z[static_cast<std::size_t>(ti)];
126 const int zb = type_to_Z[static_cast<std::size_t>(tj)];
127 if (za < 0 || zb < 0) continue;
128 if (entries[static_cast<std::size_t>(k)] == nullptr)
129 entries[static_cast<std::size_t>(k)] = find(za, zb);
130 }
131 }
132 return entries;
133 }
134
135private:
136 std::vector<PopulationPairPrior> pairs_;
137};
138
139} // namespace priors
140} // namespace gpr
141
142#endif // GPR_PRIOR_POPULATION_PRIOR_H_
std::size_t size() const noexcept
bool empty() const noexcept
void add(PopulationPairPrior prior)
std::vector< const PopulationPairPrior * > perPairtype(const AtomsConfiguration &conf) const
bool read(std::istream &input)
const PopulationPairPrior * find(int element_a, int element_b) const noexcept
The entry of an element pair, in either order; null when unseen.
Prior distributions and prior-mean functions for the GP.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Stores configurations of all atoms in the system.
Definition Structures.h:832
FieldMatrixi atomicNrs
Atomic Number of each atom.
Definition Structures.h:836
AtomsPositionAndType atoms_mov
Info on moving atoms.
Definition Structures.h:844
FieldMatrixIdx type
Type of each atom.
Definition Structures.h:839