16#include "readcon-db.h"
31 const Eigen::Index n = atoms_config.
atomicNrs.size();
32 if (n <= 0 || atoms_config.
positions.size() != 3 * n ||
34 throw std::invalid_argument(
35 "environment types require a complete atomic configuration");
37 std::vector<long long> labels;
38 std::vector<long long> bases;
39 std::map<long long, int> elements;
40 std::map<long long, long long> base_of_label;
42 while (std::getline(input, line)) {
43 line.erase(line.find(
'#') == std::string::npos ? line.size()
45 std::istringstream row(line);
47 if (row.eof())
continue;
48 long long atom, atomic_number, label;
51 if (!(row >> atom >> atomic_number >> label) ||
52 atom !=
static_cast<long long>(labels.size()) || atom >= n ||
53 label < 0 || atomic_number != atoms_config.
atomicNrs(0, atom)) {
54 throw std::invalid_argument(
55 "environment types require one ordered index, atomic number, "
56 "and nonnegative type per atom");
65 }
else if (!(row >> base) || base < 0 || (row >> extra)) {
66 throw std::invalid_argument(
67 "environment types take one optional nonnegative base "
70 if (!bases.empty() && (bases.back() < 0) != (base < 0))
71 throw std::invalid_argument(
72 "environment types either all carry a base type or none");
73 const auto [it, inserted] =
74 elements.emplace(label,
static_cast<int>(atomic_number));
75 if (!inserted && it->second != atomic_number) {
76 throw std::invalid_argument(
77 "an environment type cannot contain different elements");
80 const auto [bit, binserted] = base_of_label.emplace(label, base);
81 if (!binserted && bit->second != base)
82 throw std::invalid_argument(
83 "an environment type cannot refine two base types");
85 labels.push_back(label);
86 bases.push_back(base);
88 if (input.bad() ||
static_cast<Eigen::Index
>(labels.size()) != n) {
89 throw std::invalid_argument(
90 "environment types do not cover every atom");
93 std::map<long long, gpr::Index_t> compact;
94 std::unordered_map<std::string, gpr::Index_t> names;
95 for (
const auto& [label, element]: elements) {
96 const auto type =
static_cast<gpr::Index_t>(compact.size());
97 compact.emplace(label, type);
99 "Z" + std::to_string(element) +
":env" + std::to_string(label),
103 typed.
copy(atoms_config);
104 typed.
type.resize(1, n);
105 for (Eigen::Index atom = 0; atom < n; ++atom)
106 typed.
type(0, atom) =
107 compact.at(labels[
static_cast<std::size_t
>(atom)]);
112 if (!bases.empty() && bases.front() >= 0) {
114 std::map<std::pair<long long, long long>,
gpr::Index_t> base_pairs;
117 std::vector<char> seen(
static_cast<std::size_t
>(typed.
n_pt), 0);
118 std::vector<long long> base_of_type(compact.size(), -1);
119 for (
const auto& [label, type]: compact)
120 base_of_type[
static_cast<std::size_t
>(type)] =
121 base_of_label.at(label);
124 const auto k = typed.
pairtype(a, b);
125 if (k ==
EMPTY || k < 0 || k >= typed.
n_pt)
continue;
126 const long long ba = base_of_type[
static_cast<std::size_t
>(a)];
127 const long long bb = base_of_type[
static_cast<std::size_t
>(b)];
129 std::make_pair(std::min(ba, bb), std::max(ba, bb));
130 const auto found = base_pairs.find(key);
132 found != base_pairs.end()
138 if (seen[
static_cast<std::size_t
>(k)] &&
141 throw std::invalid_argument(
142 "a pair type cannot refine two base pair types");
144 seen[
static_cast<std::size_t
>(k)] = 1;
149 atoms_config.
copy(typed);
156 std::ifstream input(path);
158 throw std::invalid_argument(
"could not open environment types: " +
165 const std::unordered_map<std::string, gpr::Index_t>& atomtypes,
173 const bool use_legacy_cuh2_types =
174 atomtypes.count(
"H2") && atomtypes.count(
"Cu");
181 if (use_legacy_cuh2_types) {
187 for (Eigen::Index n = 0; n < atoms_config.
is_frozen.size(); ++n) {
192 if (!use_legacy_cuh2_types)
194 atoms_config.
type(0, n);
201 if (!use_legacy_cuh2_types)
203 atoms_config.
type(0, n);
211 atoms_config.
n_pt = 0;
223 std::fill(std::begin(atoms_config.
cell), std::end(atoms_config.
cell), 0.0);
228 {parameters.cell_periodic.value[0] != 0,
229 parameters.cell_periodic.value[1] != 0,
230 parameters.cell_periodic.value[2] != 0});
240 const std::array<double, 3>& cell,
const std::array<double, 3>& angles)
245 if (minimage_cell_from_con_box_exact(cell.data(), angles.data(), &box) !=
247 const char* msg = minimage_last_error();
248 throw std::invalid_argument(msg ? std::string(msg)
249 :
"minimage: con box failed");
251 return {box.ax, box.ay, box.az, box.bx, box.by,
252 box.bz, box.cx, box.cy, box.cz};
256 const std::array<double, 3>& angles,
259 const std::array<double, 9> m =
269 if (frame.num_atoms >
270 static_cast<std::size_t
>(std::numeric_limits<int>::max() / 3)) {
271 throw std::invalid_argument(
273 " atom count does not fit in the coordinate index");
275 if (frame.num_atoms > 0 && frame.atoms ==
nullptr) {
276 throw std::invalid_argument(
277 std::string(role) +
" declares atoms but the atom pointer is null");
281template <
class To,
class From>
284 if (value >
static_cast<From
>(std::numeric_limits<To>::max())) {
285 throw std::invalid_argument(std::string(
field) +
286 " is out of range for this build");
288 return static_cast<To
>(value);
295 const readcon::CFrame* product,
296 std::vector<int>* reactive_atoms)
299 const int n_atoms =
static_cast<int>(frame.num_atoms);
300 atoms_config.
clear();
302 atoms_config.
positions.resize(1, 3 * n_atoms);
303 atoms_config.
is_frozen.resize(1, n_atoms);
304 atoms_config.
id.resize(1, n_atoms);
305 atoms_config.
atomicNrs.resize(1, n_atoms);
307 atoms_config.
type.resize(1, n_atoms);
309 std::vector<std::uint64_t> atomic_nrs(
static_cast<std::size_t
>(n_atoms));
310 for (
int i = 0; i < n_atoms; ++i)
311 atomic_nrs[
static_cast<std::size_t
>(i)] = frame.atoms[i].atomic_number;
312 const auto element_types =
314 std::unordered_map<std::string, gpr::Index_t> atomtypes;
315 for (std::size_t i = 0; i < atomic_nrs.size(); ++i)
316 atomtypes[
"Z" + std::to_string(atomic_nrs[i])] = element_types[i];
318 int n_partially_fixed = 0;
319 for (
int i = 0; i < n_atoms; ++i) {
320 atoms_config.
positions(0, 3 * i) = frame.atoms[i].x;
321 atoms_config.
positions(0, 3 * i + 1) = frame.atoms[i].y;
322 atoms_config.
positions(0, 3 * i + 2) = frame.atoms[i].z;
329 if (frame.atoms[i].is_fixed &&
330 !(frame.atoms[i].fixed_x && frame.atoms[i].fixed_y &&
331 frame.atoms[i].fixed_z))
333 atoms_config.
id(0, i) =
336 frame.atoms[i].atomic_number,
"atomic_number");
338 atoms_config.
type(0, i) = element_types[
static_cast<std::size_t
>(i)];
340 if (n_partially_fixed > 0)
341 std::cerr <<
"ReadConLoader: " << n_partially_fixed
342 <<
" atom(s) carry per-axis fixed flags; gpr supports "
343 "only fully frozen atoms and treats them as frozen "
345 if (frame.has_velocities)
346 std::cerr <<
"ReadConLoader: frame declares a velocities section; "
347 "the GP drivers are static (dimer / NEB) and the "
348 "velocity data is not used\n";
352 {frame.cell[0], frame.cell[1], frame.cell[2]},
353 {frame.angles[0], frame.angles[1], frame.angles[2]}, parameters);
355 if (reactive_atoms) reactive_atoms->clear();
358 if (
static_cast<int>(product->num_atoms) != n_atoms) {
359 std::cerr <<
"ReadConLoader: product frame has "
360 << product->num_atoms <<
" atoms, reactant " << n_atoms
361 <<
"; reactive atom types skipped\n";
363 bool same_elements =
true;
364 std::vector<int> Z(
static_cast<std::size_t
>(n_atoms));
365 std::vector<gpr::Index_t> types(
static_cast<std::size_t
>(n_atoms));
366 Eigen::VectorXd x_react(3 * n_atoms), x_prod(3 * n_atoms);
367 for (
int i = 0; i < n_atoms; ++i) {
369 frame.atoms[i].atomic_number,
"atomic_number");
371 same_elements && product->atoms[i].atomic_number ==
372 frame.atoms[i].atomic_number;
373 types[
static_cast<std::size_t
>(i)] = atoms_config.
type(0, i);
374 x_react.segment<3>(3 * i) << frame.atoms[i].x, frame.atoms[i].y,
376 x_prod.segment<3>(3 * i) << product->atoms[i].x,
377 product->atoms[i].y, product->atoms[i].z;
379 if (!same_elements) {
380 std::cerr <<
"ReadConLoader: product elements differ from "
381 "the reactant; reactive atom types skipped\n";
383 std::array<double, 9> cell{};
389 x_react, x_prod, Z, types, atomtypes, cell);
390 for (
int i = 0; i < n_atoms; ++i)
391 atoms_config.
type(0, i) =
392 reactive.types[
static_cast<std::size_t
>(i)];
393 atomtypes = reactive.atomtypes;
394 if (reactive_atoms) *reactive_atoms = reactive.reactive_atoms;
403std::size_t ingestConDirectory(
const std::string& corpus_dir,
404 const std::string& tree)
406 namespace fs = std::filesystem;
407 fs::create_directories(corpus_dir);
409 std::vector<fs::path> files;
410 for (
const auto& entry: fs::recursive_directory_iterator(tree)) {
411 if (!entry.is_regular_file())
continue;
412 if (entry.path().extension() !=
".con")
continue;
413 files.push_back(entry.path());
415 std::sort(files.begin(), files.end());
416 std::size_t ingested = 0;
417 for (
const auto& file: files) {
419 const int status = rkrdb_append_trajectory(
420 corpus.id(),
static_cast<uint64_t
>(ingested + 1),
421 file.string().c_str(), &n);
422 if (
status != RKRDB_OK || n == 0)
423 throw std::runtime_error(
"ingest failed for " + file.string());
Set up the GP-dimer problem.
bool activateFrozenAtoms(const gpr::Coord &R, const double activation_distance, gpr::AtomsConfiguration &atoms_config)
Update the active frozen-atom set for one or more configurations.
void setPairtypeForMovingAtoms(const gpr::FieldMatrixIdx &atomtype_mov, gpr::Index_t &n_pt, gpr::FieldMatrixi &pairtype)
Set pairtype indices for moving+moving atom pairs and gives the updated number of active pairtypes.
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
void set(FieldMatrixd &mat, Index_t i, const Eigen::Vector3d &vec)
void zeroOpenAxes(double cell[9], const std::array< bool, 3 > &periodic) noexcept
bool copyMicLattice(double dest[9], const double src[9]) noexcept
Copy src onto dest only when src is a MIC lattice.
ReactiveAtomTypes assignReactiveAtomTypes(const Eigen::VectorXd &reactant, const Eigen::VectorXd &product, const std::vector< int > &atomic_numbers, const std::vector< Index_t > &types, const std::unordered_map< std::string, Index_t > &atomtypes, const std::array< double, 9 > &cell)
void setCellDimensionsFromLengthsAndAngles(const std::array< double, 3 > &cell, const std::array< double, 3 > &angles, gpr::InputParameters ¶meters)
void loadEnvironmentAtomTypes(std::istream &input, gpr::AtomsConfiguration &atoms_config, const gpr::InputParameters ¶meters)
void loadAtomsConfigurationFromFrame(const readcon::CFrame &frame, gpr::AtomsConfiguration &atoms_config, gpr::Coord &R_init, gpr::InputParameters ¶meters, const readcon::CFrame *product, std::vector< int > *reactive_atoms)
static To checkedNarrow(From value, const char *field)
std::array< double, 9 > cellMatrixFromLengthsAndAngles(const std::array< double, 3 > &cell, const std::array< double, 3 > &angles)
static void requireUsableFrame(const readcon::CFrame &frame, const char *role)
std::vector< Type > elementAtomTypes(const AtomicNr *atomic_nrs, std::size_t n_atoms)
void initializeAtomsConfiguration(const gpr::Coord &R_init, const gpr::InputParameters ¶meters, const std::unordered_map< std::string, gpr::Index_t > &atomtypes, gpr::AtomsConfiguration &atoms_config)
Stores configurations of all atoms in the system.
std::vector< Index_t > pair_type_parent
Index_t countMovingAtoms()
void seedNearestNeighbourFromActive()
Seed the fixed-N nearest-neighbour set from the current active (radius-activated) frozen count....
FieldMatrixi atomicNrs
Atomic Number of each atom.
AtomsPositionAndType atoms_froz_inactive
Info on inactive frozen atoms.
FieldMatrixU8 is_frozen
Indicates whether atom is frozen or not.
FieldMatrixIdx id
ID of each atom (we do not use it).
AtomsPositionAndType atoms_froz_active
Info on active frozen atoms.
Index_t n_base_pt
Number of base pair types (0: none).
AtomsPositionAndType atoms_mov
Info on moving atoms.
void copy(const AtomsConfiguration &other)
FieldMatrixIdx type
Type of each atom.
Coord positions
Positions of all atoms in the system.
Eigen::VectorXd atomic_masses
Per-atom input masses; empty selects unit masses.
void resize(const Index_t num_atoms)