Loading...
Searching...
No Matches
ReadConLoader.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef MANAGERS_IO_READCONLOADER_H_
6#define MANAGERS_IO_READCONLOADER_H_
7
8#include <algorithm>
9#include <array>
10#include <cmath>
11#include <cstddef>
12#include <cstdint>
13#include <istream>
14#include <limits>
15#include <string>
16#include <unordered_map>
17#include <vector>
18
20#include "readcon-core.h"
21
22namespace gpr::io {
23
28void loadEnvironmentAtomTypes(std::istream& input,
29 gpr::AtomsConfiguration& atoms_config,
30 const gpr::InputParameters& parameters);
31void loadEnvironmentAtomTypes(const std::string& path,
32 gpr::AtomsConfiguration& atoms_config,
33 const gpr::InputParameters& parameters);
34
36 const gpr::Coord& R_init, const gpr::InputParameters& parameters,
37 const std::unordered_map<std::string, gpr::Index_t>& atomtypes,
38 gpr::AtomsConfiguration& atoms_config);
39
42template <typename Type, typename AtomicNr>
43[[nodiscard]] std::vector<Type> elementAtomTypes(const AtomicNr* atomic_nrs,
44 std::size_t n_atoms)
45{
46 std::vector<AtomicNr> elements;
47 std::vector<Type> types(n_atoms);
48 for (std::size_t i = 0; i < n_atoms; ++i) {
49 const auto seen =
50 std::find(elements.begin(), elements.end(), atomic_nrs[i]);
51 types[i] = static_cast<Type>(seen - elements.begin());
52 if (seen == elements.end()) elements.push_back(atomic_nrs[i]);
53 }
54 return types;
55}
56
60std::array<double, 9> cellMatrixFromLengthsAndAngles(
61 const std::array<double, 3>& cell, const std::array<double, 3>& angles);
62
63void setCellDimensionsFromLengthsAndAngles(const std::array<double, 3>& cell,
64 const std::array<double, 3>& angles,
65 gpr::InputParameters& parameters);
66
76 const readcon::CFrame& frame, gpr::AtomsConfiguration& atoms_config,
77 gpr::Coord& R_init, gpr::InputParameters& parameters,
78 const readcon::CFrame* product = nullptr,
79 std::vector<int>* reactive_atoms = nullptr);
80
82[[nodiscard]] inline double maxAtomicForce(const double* forces,
83 std::size_t n_atoms) noexcept
84{
85 if (forces == nullptr) return std::numeric_limits<double>::quiet_NaN();
86 double fmax = 0.0;
87 for (std::size_t atom = 0; atom < n_atoms; ++atom) {
88 const double x = forces[3 * atom];
89 const double y = forces[3 * atom + 1];
90 const double z = forces[3 * atom + 2];
91 if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(z))
92 return std::numeric_limits<double>::quiet_NaN();
93 fmax = std::max(fmax, std::sqrt(x * x + y * y + z * z));
94 }
95 return fmax;
96}
97
99inline int addAtomWithOptionalForce(readcon::RKRConFrameBuilder* builder,
100 const char* symbol, double x, double y,
101 double z, bool fixed, std::uint64_t atom_id,
102 double mass, const double* force)
103{
104 return readcon::rkr_frame_add_atom_full(builder, symbol, x, y, z, fixed,
105 fixed, fixed, atom_id, mass,
106 nullptr, force);
107}
108
112 double energy = std::numeric_limits<double>::quiet_NaN();
113 std::vector<double> forces;
114 bool complete = false;
115};
116
118 const readcon::RKRConFrame* frame, const readcon::CFrame& parsed)
119{
121 const auto n = parsed.num_atoms;
122 if (frame == nullptr || n == 0 || !parsed.has_forces) return out;
123 out.forces.assign(3 * n, 0.0);
124 for (std::size_t atom = 0; atom < n; ++atom) {
125 const auto& site = parsed.atoms[atom];
126 if (!site.has_forces) return FrameEnergyForces{};
127 out.forces[3 * atom] = site.fx;
128 out.forces[3 * atom + 1] = site.fy;
129 out.forces[3 * atom + 2] = site.fz;
130 }
131 out.energy = readcon::rkr_frame_energy(frame);
132 out.complete = std::isfinite(out.energy) &&
133 std::isfinite(maxAtomicForce(out.forces.data(), n));
134 if (!out.complete) return FrameEnergyForces{};
135 return out;
136}
137
138#ifdef USE_READCON_DB
141std::size_t ingestConDirectory(const std::string& corpus_dir,
142 const std::string& tree);
143#endif
144
145} // namespace gpr::io
146
147#endif // MANAGERS_IO_READCONLOADER_H_
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
void setCellDimensionsFromLengthsAndAngles(const std::array< double, 3 > &cell, const std::array< double, 3 > &angles, gpr::InputParameters &parameters)
void loadEnvironmentAtomTypes(std::istream &input, gpr::AtomsConfiguration &atoms_config, const gpr::InputParameters &parameters)
void loadAtomsConfigurationFromFrame(const readcon::CFrame &frame, gpr::AtomsConfiguration &atoms_config, gpr::Coord &R_init, gpr::InputParameters &parameters, const readcon::CFrame *product, std::vector< int > *reactive_atoms)
FrameEnergyForces frameEnergyForces(const readcon::RKRConFrame *frame, const readcon::CFrame &parsed)
int addAtomWithOptionalForce(readcon::RKRConFrameBuilder *builder, const char *symbol, double x, double y, double z, bool fixed, std::uint64_t atom_id, double mass, const double *force)
Add one atom. force is three doubles, or null when the atom has none.
std::array< double, 9 > cellMatrixFromLengthsAndAngles(const std::array< double, 3 > &cell, const std::array< double, 3 > &angles)
double maxAtomicForce(const double *forces, std::size_t n_atoms) noexcept
Largest atomic force norm. NaN when any component is non-finite.
std::vector< Type > elementAtomTypes(const AtomicNr *atomic_nrs, std::size_t n_atoms)
void initializeAtomsConfiguration(const gpr::Coord &R_init, const gpr::InputParameters &parameters, const std::unordered_map< std::string, gpr::Index_t > &atomtypes, gpr::AtomsConfiguration &atoms_config)
FieldMatrixd Coord
Definition Macros.h:35
Stores configurations of all atoms in the system.
Definition Structures.h:832
Structure of the input parameters. All input parameters are pairs of string names and values.
Definition Structures.h:370
std::vector< double > forces