Loading...
Searching...
No Matches
ObservationArchive.h
Go to the documentation of this file.
1#pragma once
2// SPDX-License-Identifier: MIT
3// Copyright (c) gpridge developers
4// See LICENSE for details.
5
22
23namespace gpr {
24namespace neb {
25class ObservationArchive;
26}
27} // namespace gpr
28
29#ifdef USE_READCON_DB
30
31#include <Eigen/Dense>
32#include <algorithm>
33#include <array>
34#include <cstddef>
35#include <cstdint>
36#include <limits>
37#include <stdexcept>
38#include <string>
39#include <vector>
40
43#include "readcon-db.h"
44
45namespace gpr {
46namespace neb {
47
48class ObservationArchive {
49public:
50 struct ArchivedRow {
51 Eigen::VectorXd positions;
52 double energy = 0.0;
53 Eigen::VectorXd gradient;
54 };
55
62 ObservationArchive(
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)
67 {
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));
73 }
74 const double cell3[3] = {box[0], box[4], box[8]};
75 int status = RKRDB_OK;
76 if (process_group_.rank == 0) {
77 status = rkrdb_archive_open(dir.c_str(), z.data(),
78 static_cast<uint32_t>(z.size()), cell3,
79 &id_);
80 open_ = status == RKRDB_OK;
81 }
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);
85#endif
86 if (status != RKRDB_OK) {
87 throw std::runtime_error(
88 "ObservationArchive: rkrdb_archive_open failed at " + dir);
89 }
90 }
91
92 ~ObservationArchive()
93 {
94 if (open_) (void)rkrdb_archive_close(id_);
95 }
96
97 ObservationArchive(const ObservationArchive&) = delete;
98 ObservationArchive& operator=(const ObservationArchive&) = delete;
99
103 [[nodiscard]] uint64_t size() const noexcept
104 {
105 uint64_t count = 0;
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);
110#endif
111 return count;
112 }
113
116 bool append(const Eigen::VectorXd& positions, double energy,
117 const Eigen::VectorXd& gradient) noexcept
118 {
119 if (process_group_.rank != 0) return true;
120 if (positions.size() != static_cast<Eigen::Index>(3 * natoms_) ||
121 gradient.size() != positions.size())
122 return false;
123 try {
124 // GP gradient -> CON forces.
125 const Eigen::VectorXd forces = -gradient;
126 return rkrdb_archive_append(id_, positions.data(), forces.data(),
127 energy) == RKRDB_OK;
128 } catch (...) {
129 return false;
130 }
131 }
132
136 [[nodiscard]] std::vector<ArchivedRow> fetchAll(
137 uint32_t limit = 0) const noexcept
138 {
139 return fetchFrom(0, limit);
140 }
141
146 [[nodiscard]] std::vector<ArchivedRow> fetchFrom(
147 uint64_t first, uint32_t limit = 0) const noexcept
148 {
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);
164 }
165 }
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);
173 }
174 if (process_group_.rank != 0) {
175 rows.reserve(count);
176 for (std::size_t i = 0; i < count; ++i) {
177 const double* row = packed.data() + i * stride;
178 rows.push_back(
179 {Eigen::Map<const Eigen::VectorXd>(row + 1, dof),
180 row[0],
181 Eigen::Map<const Eigen::VectorXd>(row + 1 + dof,
182 dof)});
183 }
184 }
185 }
186#endif
187 return rows;
188 }
189
190private:
191 [[nodiscard]] std::vector<ArchivedRow> fetchLocal(uint64_t first,
192 uint32_t limit) const
193 {
194 std::vector<ArchivedRow> rows;
195 (void)rkrdb_archive_flush(id_);
196 uint64_t count = 0;
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) {
204 double energy = 0.0;
205 if (rkrdb_archive_fetch(id_, i, pos.data(), forces.data(),
206 static_cast<uint32_t>(natoms_),
207 &energy) != RKRDB_OK) {
208 continue;
209 }
210 ArchivedRow row;
211 row.positions = Eigen::Map<Eigen::VectorXd>(
212 pos.data(), static_cast<Eigen::Index>(pos.size()));
213 // CON forces -> GP gradient.
214 row.gradient = -Eigen::Map<Eigen::VectorXd>(
215 forces.data(), static_cast<Eigen::Index>(forces.size()));
216 row.energy = energy;
217 rows.push_back(std::move(row));
218 }
219 return rows;
220 }
221
222 std::size_t natoms_ = 0;
223 linalg::MpiContext process_group_;
224 size_t id_ = static_cast<size_t>(-1);
225 bool open_ = false;
226};
227
228} /* namespace neb */
229} /* namespace gpr */
230
231#endif /* USE_READCON_DB */
@ z
Definition Enums.h:28
Tiny value type describing the MPI dispatch context for a probe loop.
int status
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.
Definition Macros.h:16