25class ObservationArchive;
43#include "readcon-db.h"
48class ObservationArchive {
51 Eigen::VectorXd positions;
53 Eigen::VectorXd gradient;
63 const std::string& dir, std::vector<int> atomic_numbers,
64 const std::array<double, 9>& box,
65 linalg::MpiContext process_group = linalg::MpiContext::local())
66 : natoms_(atomic_numbers.size()), process_group_(process_group)
69 std::vector<uint32_t>
z;
70 z.reserve(atomic_numbers.size());
71 for (
const int zi: atomic_numbers) {
72 z.push_back(
static_cast<uint32_t
>(zi));
74 const double cell3[3] = {box[0], box[4], box[8]};
76 if (process_group_.rank == 0) {
77 status = rkrdb_archive_open(dir.c_str(),
z.data(),
78 static_cast<uint32_t
>(
z.size()), cell3,
80 open_ =
status == RKRDB_OK;
82#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
83 if (process_group_.is_distributed())
84 MPI_Bcast(&
status, 1, MPI_INT, 0, process_group_.comm);
87 throw std::runtime_error(
88 "ObservationArchive: rkrdb_archive_open failed at " + dir);
94 if (open_) (void)rkrdb_archive_close(id_);
97 ObservationArchive(
const ObservationArchive&) =
delete;
98 ObservationArchive& operator=(
const ObservationArchive&) =
delete;
103 [[nodiscard]] uint64_t size() const noexcept
106 if (open_) (void)rkrdb_archive_appended(id_, &count);
107#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
108 if (process_group_.is_distributed())
109 MPI_Bcast(&count, 1, MPI_UINT64_T, 0, process_group_.comm);
116 bool append(
const Eigen::VectorXd& positions,
double energy,
117 const Eigen::VectorXd& gradient)
noexcept
119 if (process_group_.rank != 0)
return true;
120 if (positions.size() !=
static_cast<Eigen::Index
>(3 * natoms_) ||
121 gradient.size() != positions.size())
125 const Eigen::VectorXd forces = -gradient;
126 return rkrdb_archive_append(id_, positions.data(), forces.data(),
136 [[nodiscard]] std::vector<ArchivedRow> fetchAll(
137 uint32_t limit = 0) const noexcept
139 return fetchFrom(0, limit);
146 [[nodiscard]] std::vector<ArchivedRow> fetchFrom(
147 uint64_t first, uint32_t limit = 0) const noexcept
149 std::vector<ArchivedRow> rows;
150 if (open_) rows = fetchLocal(first, limit);
151#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
152 if (process_group_.is_distributed()) {
153 uint64_t count = rows.size();
154 MPI_Bcast(&count, 1, MPI_UINT64_T, 0, process_group_.comm);
155 const std::size_t dof = 3 * natoms_;
156 const std::size_t stride = 1 + 2 * dof;
157 std::vector<double> packed(count * stride);
158 if (process_group_.rank == 0) {
159 for (std::size_t i = 0; i < rows.size(); ++i) {
160 double* row = packed.data() + i * stride;
161 row[0] = rows[i].energy;
162 std::copy_n(rows[i].positions.data(), dof, row + 1);
163 std::copy_n(rows[i].gradient.data(), dof, row + 1 + dof);
166 for (std::size_t offset = 0; offset < packed.size();) {
167 const int chunk =
static_cast<int>(std::min(
168 packed.size() - offset,
169 static_cast<std::size_t
>(std::numeric_limits<int>::max())));
170 MPI_Bcast(packed.data() + offset, chunk, MPI_DOUBLE, 0,
171 process_group_.comm);
172 offset +=
static_cast<std::size_t
>(chunk);
174 if (process_group_.rank != 0) {
176 for (std::size_t i = 0; i < count; ++i) {
177 const double* row = packed.data() + i * stride;
179 {Eigen::Map<const Eigen::VectorXd>(row + 1, dof),
181 Eigen::Map<const Eigen::VectorXd>(row + 1 + dof,
191 [[nodiscard]] std::vector<ArchivedRow> fetchLocal(uint64_t first,
192 uint32_t limit)
const
194 std::vector<ArchivedRow> rows;
195 (void)rkrdb_archive_flush(id_);
197 if (rkrdb_archive_count(id_, &count) != RKRDB_OK)
return rows;
198 if (first >= count)
return rows;
199 if (limit != 0 && count - first > limit) count = first + limit;
200 std::vector<double> pos(3 * natoms_);
201 std::vector<double> forces(3 * natoms_);
202 rows.reserve(
static_cast<std::size_t
>(count - first));
203 for (uint64_t i = first; i < count; ++i) {
205 if (rkrdb_archive_fetch(id_, i, pos.data(), forces.data(),
206 static_cast<uint32_t
>(natoms_),
207 &energy) != RKRDB_OK) {
211 row.positions = Eigen::Map<Eigen::VectorXd>(
212 pos.data(),
static_cast<Eigen::Index
>(pos.size()));
214 row.gradient = -Eigen::Map<Eigen::VectorXd>(
215 forces.data(),
static_cast<Eigen::Index
>(forces.size()));
217 rows.push_back(std::move(row));
222 std::size_t natoms_ = 0;
223 linalg::MpiContext process_group_;
224 size_t id_ =
static_cast<size_t>(-1);
Tiny value type describing the MPI dispatch context for a probe loop.
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
void requireCompatibleLibrary()
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.