34#include <system_error>
35#include <unordered_map>
41namespace fs = std::filesystem;
43constexpr uint8_t kConPrecision = 17;
44constexpr uint8_t kConvelPrecision = 6;
46std::string strip_nl(
const std::string &s) {
48 while (!str.empty() && (str.back() ==
'\n' || str.back() ==
'\r'))
53std::string canonical_generator_header(
const std::string &header) {
54 auto stripped = strip_nl(header);
55 if (!stripped.empty()) {
58 return "Generated by eOn";
61std::string ensure_extension(std::string filename, std::string_view ext) {
62 fs::path path(filename);
63 const auto name = path.filename().string();
64 const bool has_compound =
65 name.size() > ext.size() && (name.ends_with(std::string(ext) +
".gz") ||
66 name.ends_with(std::string(ext) +
".zst"));
67 if (!has_compound && path.extension() != ext) {
73std::string symbol_for_z(
long atomic_nr) {
74 return readcon::z_to_symbol(
static_cast<uint64_t
>(atomic_nr));
77std::vector<double> flat_row_major(
const AtomMatrix &m) {
78 static_assert(AtomMatrix::IsRowMajor,
79 "flat_row_major assumes AtomMatrix RowMajor Nx3 layout");
80 const auto n =
static_cast<size_t>(m.rows());
81 std::vector<double> flat(n * 3);
82 std::memcpy(flat.data(), m.data(), flat.size() *
sizeof(
double));
86void apply_frame_metadata(readcon::ConFrameBuilder &builder,
88 if (metadata ==
nullptr) {
92 builder.set_metadata_json(*metadata->
raw_json);
95 builder.set_energy(*metadata->
energy);
100 if (metadata->
time) {
101 builder.set_time(*metadata->
time);
104 builder.set_timestep(*metadata->
timestep);
107 builder.set_neb_bead(*metadata->
neb_bead);
110 builder.set_neb_band(*metadata->
neb_band);
113 builder.set_string_metadata(
"potential_type", *metadata->
potential_type);
115 for (
const auto &[key, value] : metadata->
scalars) {
116 builder.set_scalar_metadata(key, value);
118 for (
const auto &[key, value] : metadata->
strings) {
119 builder.set_string_metadata(key, value);
124 const std::vector<readcon::ConFrame> &frames,
127 const auto compression =
128 readcon::ConFrameWriter::compression_from_extension(path);
129 readcon::ConFrameWriter writer(path, compression, precision);
130 writer.extend(frames);
132 }
catch (
const std::exception &e) {
133 EONC_LOG_ERROR(
"Failed to write {}: {}", path.string(), e.what());
148std::vector<size_t> matter_order(
const std::vector<readcon::Atom> &atoms) {
149 const size_t n = atoms.size();
150 std::vector<size_t> order(n);
151 std::iota(order.begin(), order.end(),
size_t{0});
155 const bool ascending =
156 std::is_sorted(atoms.begin(), atoms.end(),
157 [](
const readcon::Atom &a,
const readcon::Atom &b) {
158 return a.atom_id < b.atom_id;
160 std::adjacent_find(atoms.begin(), atoms.end(),
161 [](
const readcon::Atom &a,
const readcon::Atom &b) {
162 return a.atom_id == b.atom_id;
167 std::vector<uint64_t> ids;
169 for (
const auto &atom : atoms) {
170 ids.push_back(atom.atom_id);
172 std::vector<uint64_t> sorted(ids);
173 std::sort(sorted.begin(), sorted.end());
174 if (std::adjacent_find(sorted.begin(), sorted.end()) != sorted.end()) {
177 std::stable_sort(order.begin(), order.end(),
178 [&ids](
size_t a,
size_t b) { return ids[a] < ids[b]; });
186 std::uintmax_t size{0};
187 fs::file_time_type mtime{};
188 friend bool operator==(
const FileStamp &,
const FileStamp &) =
default;
191std::optional<FileStamp> stamp_file(
const fs::path &path) {
193 const auto size = fs::file_size(path, ec);
197 const auto mtime = fs::last_write_time(path, ec);
201 return FileStamp{size, mtime};
207std::string append_key(
const fs::path &path) {
209 auto resolved = fs::weakly_canonical(path, ec);
210 if (ec || resolved.empty()) {
211 resolved = fs::absolute(path, ec);
213 return path.lexically_normal().string();
216 return resolved.lexically_normal().string();
221std::mutex &append_mutex() {
226std::unordered_map<std::string, FileStamp> &append_stamps() {
227 static std::unordered_map<std::string, FileStamp> stamps;
232void remember_stamp(
const std::string &key,
const fs::path &path) {
233 if (
auto stamp = stamp_file(path)) {
234 append_stamps()[key] = *stamp;
236 append_stamps().erase(key);
242bool tail_is_ours(
const std::string &key,
const fs::path &path) {
243 const auto it = append_stamps().find(key);
244 if (it == append_stamps().end()) {
247 const auto stamp = stamp_file(path);
248 return stamp.has_value() && *stamp == it->second;
253std::optional<long> last_xyz_natoms(
const fs::path &path) {
254 std::ifstream in(path);
258 std::optional<long> last;
260 while (std::getline(in, line)) {
267 }
catch (
const std::exception &) {
273 if (!std::getline(in, line)) {
276 for (
long i = 0; i < n; ++i) {
277 if (!std::getline(in, line)) {
287bool ends_with_newline(
const fs::path &path) {
288 std::ifstream in(path, std::ios::binary | std::ios::ate);
292 const auto len =
static_cast<std::streamoff
>(in.tellg());
312 const std::vector<readcon::ConFrame> &frames,
315 readcon::ConFrameWriter writer(
316 path, readcon::ConFrameWriter::OpenMode::Append, precision);
317 writer.extend(frames);
320 }
catch (
const std::exception &e) {
321 EONC_LOG_ERROR(
"Failed to append to {}: {}", path.string(), e.what());
329 const std::array<std::string, 2> &prebox,
330 const std::array<std::string, 2> &postbox,
331 const std::vector<uint64_t> &atom_ids) {
333 readcon::ConFrameBuilder builder(
334 {lengths[0], lengths[1], lengths[2]},
335 {angles_deg[0], angles_deg[1], angles_deg[2]}, prebox, postbox);
338 if (atom_ids.size() !=
static_cast<size_t>(n)) {
339 throw std::invalid_argument(
"seed_builder: atom_ids size mismatch");
341 for (
long i = 0; i < n; ++i) {
343 builder.add_atom(symbol_for_z(m.
getAtomicNr(i)), 0.0, 0.0, 0.0, mask,
344 atom_ids[
static_cast<size_t>(i)], m.
getMass(i));
349bool should_write_forces(
const Matter &m,
364void apply_geometry(readcon::ConFrameBuilder &builder,
Matter &m,
365 bool with_velocities,
374 builder.set_positions_from_flat(flat_row_major(m.
getPositions()));
381 builder.set_forces_from_flat(flat_row_major(m.
getForcesRaw()));
384 if (metadata !=
nullptr && !metadata->
displacements.empty()) {
385 if (metadata->
displacements.size() !=
static_cast<size_t>(3 * n)) {
386 throw std::invalid_argument(
"displacements size is not 3 x atoms");
388 builder.set_displacements_from_flat(metadata->
displacements);
390 if (metadata !=
nullptr && !metadata->
spreads.empty()) {
391 if (metadata->
spreads.size() !=
static_cast<size_t>(3 * n)) {
392 throw std::invalid_argument(
"spreads size is not 3 x atoms");
394 builder.set_spreads_from_flat(metadata->
spreads);
397 if (with_velocities) {
399 for (
long i = 0; i < n; ++i) {
400 builder.set_atom_velocity(
static_cast<size_t>(i),
401 {vel(i, 0), vel(i, 1), vel(i, 2)});
406void collect_ids_headers(
Matter &m, std::vector<uint64_t> &atom_ids,
407 std::array<std::string, 2> &prebox,
408 std::array<std::string, 2> &postbox) {
410 atom_ids.resize(
static_cast<size_t>(n));
411 for (
long i = 0; i < n; ++i) {
412 atom_ids[
static_cast<size_t>(i)] =
static_cast<uint64_t
>(m.
getAtomIndex(i));
415 prebox = {canonical_generator_header(hdr[0]), strip_nl(hdr[1])};
416 postbox = {strip_nl(hdr[3]), strip_nl(hdr[4])};
419readcon::ConFrame frame_from_matter(
Matter &m,
421 bool with_velocities) {
422 std::vector<uint64_t> atom_ids;
423 std::array<std::string, 2> prebox;
424 std::array<std::string, 2> postbox;
425 collect_ids_headers(m, atom_ids, prebox, postbox);
427 auto builder = seed_builder(m, prebox, postbox, atom_ids);
432 if (metadata ==
nullptr) {
434 meta_ptr = &auto_meta;
435 }
else if (!metadata->
energy) {
436 auto_meta = *metadata;
438 meta_ptr = &auto_meta;
441 if (with_velocities) {
444 builder.set_metadata_json(
445 R
"({"units":{"energy":"eV","length":"angstrom","mass":"amu","time":"(amu*angstrom^2/eV)^0.5"}})");
447 apply_frame_metadata(builder, meta_ptr);
448 apply_geometry(builder, m, with_velocities, meta_ptr);
449 return builder.build();
455std::optional<std::string> foreign_input(
const std::string &filename) {
461 fs::path alt(filename);
462 if (alt.extension() ==
".con" || alt.extension() ==
".convel") {
463 alt.replace_extension(fmt);
464 if (fs::exists(alt)) {
479std::atomic<bool> g_write_con_forces{
false};
484 return frame_from_matter(m, metadata,
false);
488 g_write_con_forces.store(enabled, std::memory_order_relaxed);
492 return g_write_con_forces.load(std::memory_order_relaxed);
497 meta.
energy = frame.energy_opt();
499 meta.
time = frame.time_opt();
500 meta.
timestep = frame.timestep_opt();
501 meta.
neb_bead = frame.neb_bead_opt();
502 meta.
neb_band = frame.neb_band_opt();
504 const auto json = frame.metadata_json();
505 if (!
json.empty() &&
json !=
"{}") {
511std::pair<std::array<double, 3>, std::array<double, 3>>
514 std::array<double, 3> lengths;
515 lengths[0] = cell.row(0).norm();
516 lengths[1] = cell.row(1).norm();
517 lengths[2] = cell.row(2).norm();
520 std::array<double, 3> angles;
522 cell.row(1).dot(cell.row(2)), lengths[1] * lengths[2])) *
525 cell.row(0).dot(cell.row(2)), lengths[0] * lengths[2])) *
528 cell.row(0).dot(cell.row(1)), lengths[0] * lengths[1])) *
530 return {lengths, angles};
534 const std::lock_guard<std::mutex> guard(append_mutex());
535 append_stamps().clear();
540 filename = ensure_extension(std::move(filename),
".con");
544 const fs::path path(filename);
545 const auto compression =
546 readcon::ConFrameWriter::compression_from_extension(path);
547 const bool streamable =
548 compression == readcon::ConFrameWriter::Compression::None;
553 const std::lock_guard<std::mutex> guard(append_mutex());
554 const auto key = append_key(path);
555 const bool exists = fs::exists(path);
556 const bool concatenate = append && exists && streamable;
562 std::vector<readcon::ConFrame> frames;
563 if (append && exists && !streamable) {
567 frames = readcon::read_all_frames(path);
568 }
catch (
const std::exception &e) {
572 }
else if (concatenate && !tail_is_ours(key, path)) {
577 [[maybe_unused]]
const auto existing = readcon::read_all_frames(path);
578 }
catch (
const std::exception &e) {
584 frames.push_back(frame_from_matter(m, metadata,
false));
585 }
catch (
const std::exception &e) {
586 EONC_LOG_ERROR(
"Failed to build frame for {}: {}", filename, e.what());
590 const auto status = concatenate ? append_frames(path, frames, kConPrecision)
591 : write_frames(path, frames, kConPrecision);
593 remember_stamp(key, path);
598 append_stamps().erase(key);
604 if (
auto foreign = foreign_input(filename)) {
607 filename = ensure_extension(std::move(filename),
".con");
609 auto frame = readcon::read_first_frame(filename);
615 }
catch (
const std::exception &e) {
624 const auto &atoms = frame.atoms();
625 const auto &lengths = frame.cell();
626 const auto &angles_deg = frame.angles();
627 const auto &prebox = frame.prebox_header();
628 const auto &postbox = frame.postbox_header();
635 double angles[3] = {angles_deg[0], angles_deg[1], angles_deg[2]};
636 if (angles[0] == 90.0 && angles[1] == 90.0 && angles[2] == 90.0) {
638 cell(0, 0) = lengths[0];
639 cell(1, 1) = lengths[1];
640 cell(2, 2) = lengths[2];
646 const double alpha = angles[0];
647 const double beta = angles[1];
648 const double gamma = angles[2];
652 cell(1, 0) = cos(gamma);
653 cell(1, 1) = sin(gamma);
654 cell(2, 0) = cos(beta);
655 cell(2, 1) = (cos(alpha) - cell(1, 0) * cell(2, 0)) / cell(1, 1);
659 cell(0, 0) *= lengths[0];
660 cell(1, 0) *= lengths[1];
661 cell(1, 1) *= lengths[1];
662 cell(2, 0) *= lengths[2];
663 cell(2, 1) *= lengths[2];
664 cell(2, 2) *= lengths[2];
668 std::format(
"{} {} {}\n", angles_deg[0], angles_deg[1], angles_deg[2]);
670 const auto n =
static_cast<Eigen::Index
>(atoms.size());
671 m.
resize(
static_cast<long>(atoms.size()));
675 const std::vector<size_t> order = matter_order(atoms);
676 std::vector<long> file_to_matter(
static_cast<size_t>(n));
677 for (Eigen::Index i = 0; i < n; ++i) {
678 file_to_matter[order[
static_cast<size_t>(i)]] =
static_cast<long>(i);
682 AtomMatrix positions = AtomMatrix::Zero(n, 3);
684 AtomMatrix velocities = AtomMatrix::Zero(n, 3);
685 VectorXd masses = VectorXd::Zero(n);
686 VectorXi atomic_nrs = VectorXi::Zero(n);
687 bool any_force =
false;
688 bool any_velocity =
false;
690 for (Eigen::Index i = 0; i < n; ++i) {
691 const auto &atom = atoms[order[
static_cast<size_t>(i)]];
692 positions(i, 0) = atom.x;
693 positions(i, 1) = atom.y;
694 positions(i, 2) = atom.z;
695 masses(i) = atom.mass;
696 atomic_nrs(i) =
static_cast<int>(atom.atomic_number);
697 const auto fixed = atom.fixed_mask();
698 m.
setFixedMask(
static_cast<long>(i), {fixed[0], fixed[1], fixed[2]});
700 static_cast<std::int64_t
>(atom.atom_id));
702 if (
auto vel = atom.velocity()) {
704 velocities(i, 0) = (*vel)[0];
705 velocities(i, 1) = (*vel)[1];
706 velocities(i, 2) = (*vel)[2];
708 if (
auto force = atom.force()) {
710 forces(i, 0) = (*force)[0];
711 forces(i, 1) = (*force)[1];
712 forces(i, 2) = (*force)[2];
720 if (any_velocity || frame.has_velocities()) {
725 if (out_metadata !=
nullptr) {
726 *out_metadata = meta;
733 const bool has_force_section = any_force || frame.has_forces();
734 if (meta.energy && has_force_section) {
736 }
else if (has_force_section) {
745 }
catch (
const std::exception &e) {
746 EONC_LOG_ERROR(
"Failed to convert frame to matter: {}", e.what());
752 filename = ensure_extension(std::move(filename),
".convel");
757 auto frame = frame_from_matter(m,
nullptr,
true);
758 std::vector<readcon::ConFrame> frames;
759 frames.push_back(std::move(frame));
760 const IoStatus status = write_frames(filename, frames, kConvelPrecision);
765 }
catch (
const std::exception &e) {
766 EONC_LOG_ERROR(
"Failed to write convel {}: {}", filename, e.what());
772 if (
auto foreign = foreign_input(filename)) {
775 filename = ensure_extension(std::move(filename),
".convel");
777 auto frame = readcon::read_first_frame(filename);
783 }
catch (
const std::exception &e) {
784 EONC_LOG_ERROR(
"Failed to read convel {}: {}", filename, e.what());
790 filename = ensure_extension(std::move(filename),
".xyz");
795 if (append && fs::exists(filename)) {
797 const auto sz = fs::file_size(filename, ec);
799 const auto prev = last_xyz_natoms(filename);
806 "matter2xyz: append atom count {} != last frame {} in {}", n, *prev,
815 append ? (std::ios::out | std::ios::app | std::ios::binary)
816 : (std::ios::out | std::ios::trunc | std::ios::binary));
824 if (append && !ends_with_newline(filename)) {
830 "{}\nLattice=\"{:.17g} {:.17g} {:.17g} {:.17g} {:.17g} {:.17g} "
831 "{:.17g} {:.17g} {:.17g}\" Properties=species:S:1:pos:R:3 Generated "
833 n, cell(0, 0), cell(0, 1), cell(0, 2), cell(1, 0), cell(1, 1), cell(1, 2),
834 cell(2, 0), cell(2, 1), cell(2, 2));
836 for (
long i = 0; i < n; ++i) {
837 out << std::format(
"{}\t{:.17g}\t{:.17g}\t{:.17g}\n",
838 symbol_for_z(m.
getAtomicNr(i)), pos(i, 0), pos(i, 1),
859 "writeTibble: pot is dirty, {} omits the force and energy columns",
863 std::ofstream out(fname);
868 out << (have_forces ?
"x y z fx fy fz energy mass symbol atmID fixed\n"
869 :
"x y z mass symbol atmID fixed\n");
873 out << std::format(
"{} {} {}", pos(idx, 0), pos(idx, 1), pos(idx, 2));
875 out << std::format(
" {} {} {} {}", fSys(idx, 0), fSys(idx, 1),
879 const int fixed_bits =
880 (mask[0] ? 1 : 0) | (mask[1] ? 2 : 0) | (mask[2] ? 4 : 0);
881 out << std::format(
" {} {} {} {}\n", m.
getMass(idx),
893std::vector<readcon::ConFrame>
895 const std::vector<ConFrameMetadata> &metadata_per_image) {
896 std::vector<readcon::ConFrame> frames;
897 if (path.empty() || path.size() != metadata_per_image.size()) {
899 "buildNebPathFrames: path/metadata size mismatch (path={}, meta={})",
900 path.size(), metadata_per_image.size());
903 for (
const auto &img : path) {
910 Matter &template_m = *path.front();
913 std::vector<uint64_t> atom_ids;
914 std::array<std::string, 2> prebox;
915 std::array<std::string, 2> postbox;
916 collect_ids_headers(template_m, atom_ids, prebox, postbox);
918 frames.reserve(path.size());
920 auto seed = seed_builder(template_m, prebox, postbox, atom_ids);
921 for (
size_t i = 0; i < path.size(); ++i) {
926 "buildNebPathFrames: image {} atom count {} != template {}", i,
930 auto builder = seed.clone();
931 apply_frame_metadata(builder, &metadata_per_image[i]);
932 apply_geometry(builder, img,
false,
933 &metadata_per_image[i]);
934 frames.push_back(builder.build());
936 }
catch (
const std::exception &e) {
944 const std::vector<readcon::ConFrame> &frames) {
945 if (frames.empty()) {
948 filename = ensure_extension(std::move(filename),
".con");
949 const fs::path path(filename);
950 const std::lock_guard<std::mutex> guard(append_mutex());
951 const auto key = append_key(path);
952 const auto status = write_frames(path, frames, kConPrecision);
954 remember_stamp(key, path);
957 append_stamps().erase(key);
963 const std::vector<std::shared_ptr<Matter>> &path,
964 const std::vector<ConFrameMetadata> &metadata_per_image) {
966 if (frames.empty()) {
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
#define EONC_LOG_ERROR(...)
#define EONC_LOG_WARNING(...)
bool getWriteConForces() const noexcept
Parameters.main_options().writeConForces for this Matter.
const AtomMatrix & getPositions() const
long int numberOfAtoms() const
AtomMatrix getVelocities() const
const AtomMatrix & getForcesRaw() const
double getPotentialEnergy() const
bool needsForceUpdate() const
Whether forces need recomputation (positions or surface epoch changed).
const std::array< std::string, 5 > & getHeaderCon() const
CON header lines (indices 0..4); public for I/O / bindings.
std::int64_t getAtomIndex(long int atom) const
.con column-5 index (pre-grouping); public for I/O / bindings.
void setFixedMask(long int atom, std::array< bool, 3 > mask)
const AtomMatrix & getPositions() const
void setPositions(const AtomMatrix &pos)
void setCell(const Matrix3d &newCell)
void applyPeriodicBoundaryIfEnabled()
Apply MIC wrap when periodic boundaries are enabled (I/O path).
long int numberOfAtoms() const
void setAtomicNrs(const VectorXi &atmnrs)
void setVelocities(const AtomMatrix &v)
void setAtomIndex(long int atom, std::int64_t index)
void resize(long int nAtoms)
void restoreFileForces(const AtomMatrix &fileForces, bool trustEnergy, double energy)
Write .con forces without the fixed-atom mask setForces applies.
double getMass(long int atom) const
void setMasses(const VectorXd &massesIn)
double getPotentialEnergy() const
bool needsForceUpdate() const
Whether forces need recomputation (positions or surface epoch changed).
std::array< std::string, 5 > headerCon
long getAtomicNr(long int atom) const
std::array< bool, 3 > getFixedMask(long int atom) const
Per-axis CON column-4 mask (bit0=x, bit1=y, bit2=z).
std::int64_t getAtomIndex(long int atom) const
.con column-5 index (pre-grouping); public for I/O / bindings.
const AtomMatrix & getForces() const
void setFileToMatter(std::vector< long > map)
IoStatus writeConFrames(std::string filename, const std::vector< readcon::ConFrame > &frames)
Write already-built ConFrames to a multi-frame .con (temp or durable).
IoStatus convel2matter(Matter &m, std::string filename)
IoStatus matter2convel(Matter &m, std::string filename)
void resetConAppendState()
Drop the per-path bookkeeping that lets append-mode writes skip re-parsing frames this process wrote.
std::pair< std::array< double, 3 >, std::array< double, 3 > > cell_to_lengths_angles(const Matter &m)
ConFrameMetadata metadata_from_frame(const readcon::ConFrame &frame)
Extract known frame-level fields from a parsed readcon frame.
IoStatus chemfiles2matter(Matter &, const std::string &filename, const std::string &)
bool write_con_forces() noexcept
IoStatus matter2con(Matter &m, std::string filename, bool append, const ConFrameMetadata *metadata)
Append a frame to a .con, or truncate and write one frame.
IoStatus con2matter(Matter &m, std::string filename)
bool is_chemfiles_path(std::string_view filename)
True for structure paths routed to chemfiles: a known extension (xyz, extxyz, pdb,...
std::vector< readcon::ConFrame > buildNebPathFrames(const std::vector< std::shared_ptr< Matter > > &path, const std::vector< ConFrameMetadata > &metadata_per_image)
Build NEB band ConFrames without writing (clone builder path of writeNebPath).
IoStatus matter2xyz(Matter &m, std::string filename, bool append)
Write one extended-XYZ frame: Lattice= cell and 17-digit coordinates.
IoStatus
Structured I/O result for the client surface (nanobind-friendly).
void mirror_con_corpus(const std::string &path)
Copy a con file into the run readcon-db corpus when libreadcon_db loads.
const std::string & structure_input_format() noexcept
void set_write_con_forces(bool enabled) noexcept
Whether written .con frames carry "Forces of Component" sections.
readcon::ConFrame matterToConFrame(Matter &m, const ConFrameMetadata *metadata)
Build a single stamped ConFrame from Matter (same builder as matter2con).
constexpr bool io_ok(IoStatus s) noexcept
IoStatus writeTibble(Matter &m, std::string fname)
Debug table (positions, optional cached forces). Not a structure format.
IoStatus writeNebPath(std::string filename, const std::vector< std::shared_ptr< Matter > > &path, const std::vector< ConFrameMetadata > &metadata_per_image)
Write a full NEB path as one multi-frame .con using ConFrameBuilder::clone().
constexpr double safe_div(double num, double denom, double fallback=0.0)
double safe_acos(double x)
double safe_sqrt(double x)