Loading...
Searching...
No Matches
ConFileIO.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/ConFileIO.h"
13#include "ReadconDbMirror.h"
14#include "eon/ChemfilesIO.h"
15#include "eon/Eigen.h"
16#include "eon/EonLogger.h"
17#include "eon/HelperFunctions.h"
18#include "eon/Matter.h"
19#include "eon/SafeMath.h"
20
21#include <algorithm>
22#include <atomic>
23#include <cmath>
24#include <cstdint>
25#include <cstring>
26#include <filesystem>
27#include <format>
28#include <fstream>
29#include <mutex>
30#include <numeric>
31#include <optional>
32#include <stdexcept>
33#include <string>
34#include <system_error>
35#include <unordered_map>
36#include <vector>
37
38namespace {
39
40using Matter = eonc::Matter;
41namespace fs = std::filesystem;
42
43constexpr uint8_t kConPrecision = 17;
44constexpr uint8_t kConvelPrecision = 6;
45
46std::string strip_nl(const std::string &s) {
47 std::string str(s);
48 while (!str.empty() && (str.back() == '\n' || str.back() == '\r'))
49 str.pop_back();
50 return str;
51}
52
53std::string canonical_generator_header(const std::string &header) {
54 auto stripped = strip_nl(header);
55 if (!stripped.empty()) {
56 return stripped;
57 }
58 return "Generated by eOn";
59}
60
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) {
68 path += ext;
69 }
70 return path.string();
71}
72
73std::string symbol_for_z(long atomic_nr) {
74 return readcon::z_to_symbol(static_cast<uint64_t>(atomic_nr));
75}
76
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));
83 return flat;
84}
85
86void apply_frame_metadata(readcon::ConFrameBuilder &builder,
87 const eonc::io::ConFrameMetadata *metadata) {
88 if (metadata == nullptr) {
89 return;
90 }
91 if (metadata->raw_json && !metadata->raw_json->empty()) {
92 builder.set_metadata_json(*metadata->raw_json);
93 }
94 if (metadata->energy) {
95 builder.set_energy(*metadata->energy);
96 }
97 if (metadata->frame_index) {
98 builder.set_frame_index(*metadata->frame_index);
99 }
100 if (metadata->time) {
101 builder.set_time(*metadata->time);
102 }
103 if (metadata->timestep) {
104 builder.set_timestep(*metadata->timestep);
105 }
106 if (metadata->neb_bead) {
107 builder.set_neb_bead(*metadata->neb_bead);
108 }
109 if (metadata->neb_band) {
110 builder.set_neb_band(*metadata->neb_band);
111 }
112 if (metadata->potential_type && !metadata->potential_type->empty()) {
113 builder.set_string_metadata("potential_type", *metadata->potential_type);
114 }
115 for (const auto &[key, value] : metadata->scalars) {
116 builder.set_scalar_metadata(key, value);
117 }
118 for (const auto &[key, value] : metadata->strings) {
119 builder.set_string_metadata(key, value);
120 }
121}
122
123eonc::io::IoStatus write_frames(const fs::path &path,
124 const std::vector<readcon::ConFrame> &frames,
125 uint8_t precision) {
126 try {
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());
135 }
136}
137
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});
152 if (n < 2) {
153 return order;
154 }
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;
159 }) &&
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;
163 }) == atoms.end();
164 if (ascending) {
165 return order;
166 }
167 std::vector<uint64_t> ids;
168 ids.reserve(n);
169 for (const auto &atom : atoms) {
170 ids.push_back(atom.atom_id);
171 }
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()) {
175 return order;
176 }
177 std::stable_sort(order.begin(), order.end(),
178 [&ids](size_t a, size_t b) { return ids[a] < ids[b]; });
179 return order;
180}
181
185struct FileStamp {
186 std::uintmax_t size{0};
187 fs::file_time_type mtime{};
188 friend bool operator==(const FileStamp &, const FileStamp &) = default;
189};
190
191std::optional<FileStamp> stamp_file(const fs::path &path) {
192 std::error_code ec;
193 const auto size = fs::file_size(path, ec);
194 if (ec) {
195 return std::nullopt;
196 }
197 const auto mtime = fs::last_write_time(path, ec);
198 if (ec) {
199 return std::nullopt;
200 }
201 return FileStamp{size, mtime};
202}
203
207std::string append_key(const fs::path &path) {
208 std::error_code ec;
209 auto resolved = fs::weakly_canonical(path, ec);
210 if (ec || resolved.empty()) {
211 resolved = fs::absolute(path, ec);
212 if (ec) {
213 return path.lexically_normal().string();
214 }
215 }
216 return resolved.lexically_normal().string();
217}
218
221std::mutex &append_mutex() {
222 static std::mutex m;
223 return m;
224}
225
226std::unordered_map<std::string, FileStamp> &append_stamps() {
227 static std::unordered_map<std::string, FileStamp> stamps;
228 return stamps;
229}
230
232void remember_stamp(const std::string &key, const fs::path &path) {
233 if (auto stamp = stamp_file(path)) {
234 append_stamps()[key] = *stamp;
235 } else {
236 append_stamps().erase(key);
237 }
238}
239
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()) {
245 return false;
246 }
247 const auto stamp = stamp_file(path);
248 return stamp.has_value() && *stamp == it->second;
249}
250
253std::optional<long> last_xyz_natoms(const fs::path &path) {
254 std::ifstream in(path);
255 if (!in) {
256 return std::nullopt;
257 }
258 std::optional<long> last;
259 std::string line;
260 while (std::getline(in, line)) {
261 if (line.empty()) {
262 continue;
263 }
264 long n = 0;
265 try {
266 n = std::stol(line);
267 } catch (const std::exception &) {
268 return std::nullopt;
269 }
270 if (n < 0) {
271 return std::nullopt;
272 }
273 if (!std::getline(in, line)) {
274 return std::nullopt;
275 }
276 for (long i = 0; i < n; ++i) {
277 if (!std::getline(in, line)) {
278 return std::nullopt;
279 }
280 }
281 last = n;
282 }
283 return last;
284}
285
287bool ends_with_newline(const fs::path &path) {
288 std::ifstream in(path, std::ios::binary | std::ios::ate);
289 if (!in) {
290 return true;
291 }
292 const auto len = static_cast<std::streamoff>(in.tellg());
293 if (len <= 0) {
294 return true;
295 }
296 in.seekg(len - 1);
297 char last = '\n';
298 if (!in.get(last)) {
299 return true;
300 }
301 return last == '\n';
302}
303
311eonc::io::IoStatus append_frames(const fs::path &path,
312 const std::vector<readcon::ConFrame> &frames,
313 uint8_t precision) {
314 try {
315 readcon::ConFrameWriter writer(
316 path, readcon::ConFrameWriter::OpenMode::Append, precision);
317 writer.extend(frames);
318 writer.flush();
320 } catch (const std::exception &e) {
321 EONC_LOG_ERROR("Failed to append to {}: {}", path.string(), e.what());
323 }
324}
325
328readcon::ConFrameBuilder seed_builder(eonc::Matter &m,
329 const std::array<std::string, 2> &prebox,
330 const std::array<std::string, 2> &postbox,
331 const std::vector<uint64_t> &atom_ids) {
332 auto [lengths, angles_deg] = eonc::io::cell_to_lengths_angles(m);
333 readcon::ConFrameBuilder builder(
334 {lengths[0], lengths[1], lengths[2]},
335 {angles_deg[0], angles_deg[1], angles_deg[2]}, prebox, postbox);
336
337 const long n = m.numberOfAtoms();
338 if (atom_ids.size() != static_cast<size_t>(n)) {
339 throw std::invalid_argument("seed_builder: atom_ids size mismatch");
340 }
341 for (long i = 0; i < n; ++i) {
342 const auto mask = m.getFixedMask(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));
345 }
346 return builder;
347}
348
349bool should_write_forces(const Matter &m,
350 const eonc::io::ConFrameMetadata *metadata) {
351 // Per-write metadata wins so two callers can disagree without a race on
352 // the process-wide flag. Parameters next, so pyeonclient setting the
353 // field on the bound Parameters object is enough. The atomic global is
354 // last (INI parse and the explicit setter).
355 if (metadata && metadata->write_con_forces.has_value()) {
356 return *metadata->write_con_forces;
357 }
358 if (m.getWriteConForces()) {
359 return true;
360 }
362}
363
364void apply_geometry(readcon::ConFrameBuilder &builder, Matter &m,
365 bool with_velocities,
366 const eonc::io::ConFrameMetadata *metadata) {
367 const long n = m.numberOfAtoms();
368 if (n <= 0) {
369 return;
370 }
371
372 // Prefer bulk flat setters (declared sections, no pointer-lifetime hazards
373 // under ASAN). AtomMatrix is RowMajor Nx3 matching the flat layout.
374 builder.set_positions_from_flat(flat_row_major(m.getPositions()));
375
376 // getForcesRaw() may trigger pot evaluation if recomputePotential is set;
377 // only enter when the pot is already clean so we serialize cached forces.
378 // Gated: force sections are opt-in ([Main] write_con_forces) because
379 // ASE-class readers reject frames that carry them.
380 if (should_write_forces(m, metadata) && !m.needsForceUpdate()) {
381 builder.set_forces_from_flat(flat_row_major(m.getForcesRaw()));
382 }
383
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");
387 }
388 builder.set_displacements_from_flat(metadata->displacements);
389 }
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");
393 }
394 builder.set_spreads_from_flat(metadata->spreads);
395 }
396
397 if (with_velocities) {
398 const AtomMatrix vel = m.getVelocities();
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)});
402 }
403 }
404}
405
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) {
409 const long n = m.numberOfAtoms();
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));
413 }
414 const auto &hdr = m.getHeaderCon();
415 prebox = {canonical_generator_header(hdr[0]), strip_nl(hdr[1])};
416 postbox = {strip_nl(hdr[3]), strip_nl(hdr[4])};
417}
418
419readcon::ConFrame frame_from_matter(Matter &m,
420 const eonc::io::ConFrameMetadata *metadata,
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);
426
427 auto builder = seed_builder(m, prebox, postbox, atom_ids);
428
430 const eonc::io::ConFrameMetadata *meta_ptr = metadata;
431 if (!m.needsForceUpdate()) {
432 if (metadata == nullptr) {
433 auto_meta.energy = m.getPotentialEnergy();
434 meta_ptr = &auto_meta;
435 } else if (!metadata->energy) {
436 auto_meta = *metadata;
437 auto_meta.energy = m.getPotentialEnergy();
438 meta_ptr = &auto_meta;
439 }
440 }
441 if (with_velocities) {
442 // Spec minor 1 rejects a velocities section that has no units.time.
443 // set_metadata_json replaces the map, so this runs before the other keys.
444 builder.set_metadata_json(
445 R"({"units":{"energy":"eV","length":"angstrom","mass":"amu","time":"(amu*angstrom^2/eV)^0.5"}})");
446 }
447 apply_frame_metadata(builder, meta_ptr);
448 apply_geometry(builder, m, with_velocities, meta_ptr);
449 return builder.build();
450}
451
455std::optional<std::string> foreign_input(const std::string &filename) {
456 if (eonc::io::is_chemfiles_path(filename)) {
457 return filename;
458 }
459 const auto &fmt = eonc::io::structure_input_format();
460 if (!fmt.empty()) {
461 fs::path alt(filename);
462 if (alt.extension() == ".con" || alt.extension() == ".convel") {
463 alt.replace_extension(fmt);
464 if (fs::exists(alt)) {
465 return alt.string();
466 }
467 }
468 }
469 return std::nullopt;
470}
471
472} // namespace
473
474namespace eonc::io {
475
476namespace {
477// Process-wide opt-in for force sections in written frames; classic con
478// layout (no sections) keeps ASE-class readers working on our outputs.
479std::atomic<bool> g_write_con_forces{false};
480} // namespace
481
482readcon::ConFrame matterToConFrame(Matter &m,
483 const ConFrameMetadata *metadata) {
484 return frame_from_matter(m, metadata, /*with_velocities=*/false);
485}
486
487void set_write_con_forces(bool enabled) noexcept {
488 g_write_con_forces.store(enabled, std::memory_order_relaxed);
489}
490
491bool write_con_forces() noexcept {
492 return g_write_con_forces.load(std::memory_order_relaxed);
493}
494
495ConFrameMetadata metadata_from_frame(const readcon::ConFrame &frame) {
496 ConFrameMetadata meta;
497 meta.energy = frame.energy_opt();
498 meta.frame_index = frame.frame_index_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();
503 meta.potential_type = frame.potential_type();
504 const auto json = frame.metadata_json();
505 if (!json.empty() && json != "{}") {
506 meta.raw_json = json;
507 }
508 return meta;
509}
510
511std::pair<std::array<double, 3>, std::array<double, 3>>
513 const Matrix3d cell = m.getCell();
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();
518 // CON header line 4 is alpha beta gamma:
519 // alpha = angle(b,c), beta = angle(a,c), gamma = angle(a,b).
520 std::array<double, 3> angles;
522 cell.row(1).dot(cell.row(2)), lengths[1] * lengths[2])) *
523 180.0 / eonc::helpers::pi;
525 cell.row(0).dot(cell.row(2)), lengths[0] * lengths[2])) *
526 180.0 / eonc::helpers::pi;
528 cell.row(0).dot(cell.row(1)), lengths[0] * lengths[1])) *
529 180.0 / eonc::helpers::pi;
530 return {lengths, angles};
531}
532
534 const std::lock_guard<std::mutex> guard(append_mutex());
535 append_stamps().clear();
536}
537
538IoStatus matter2con(Matter &m, std::string filename, bool append,
539 const ConFrameMetadata *metadata) {
540 filename = ensure_extension(std::move(filename), ".con");
541
543
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;
549
550 // The lock covers the stamp table and the read-then-extend window on one
551 // path. Nothing under it evaluates a potential: frame_from_matter reads
552 // cached energy and forces only while the pot is clean.
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;
557
558 // ConFrame is move-only (no default ctor), so frames carries the new frame
559 // either way. OpenMode::Append refuses gzip and zstd, and a compressed
560 // member cannot be extended in place. Those targets keep the whole-file
561 // rewrite.
562 std::vector<readcon::ConFrame> frames;
563 if (append && exists && !streamable) {
564 try {
565 // Prefer read_all_frames (single ownership hand-off) over the iterator
566 // path for append rewrite; simpler lifetime for ASAN/CI envs.
567 frames = readcon::read_all_frames(path);
568 } catch (const std::exception &e) {
569 EONC_LOG_ERROR("Failed to append to {}: {}", filename, e.what());
571 }
572 } else if (concatenate && !tail_is_ours(key, path)) {
573 // Refuse to extend a file eOn cannot parse, matching the rewrite path:
574 // the target keeps its bytes and the caller sees AppendError. Frames this
575 // process wrote and nobody touched since need no such check.
576 try {
577 [[maybe_unused]] const auto existing = readcon::read_all_frames(path);
578 } catch (const std::exception &e) {
579 EONC_LOG_ERROR("Failed to append to {}: {}", filename, e.what());
581 }
582 }
583 try {
584 frames.push_back(frame_from_matter(m, metadata, /*with_velocities=*/false));
585 } catch (const std::exception &e) {
586 EONC_LOG_ERROR("Failed to build frame for {}: {}", filename, e.what());
588 }
589
590 const auto status = concatenate ? append_frames(path, frames, kConPrecision)
591 : write_frames(path, frames, kConPrecision);
592 if (io_ok(status)) {
593 remember_stamp(key, path);
594 if (!concatenate) {
595 mirror_con_corpus(path.string());
596 }
597 } else {
598 append_stamps().erase(key);
599 }
600 return status;
601}
602
603IoStatus con2matter(Matter &m, std::string filename) {
604 if (auto foreign = foreign_input(filename)) {
605 return chemfiles2matter(m, *foreign);
606 }
607 filename = ensure_extension(std::move(filename), ".con");
608 try {
609 auto frame = readcon::read_first_frame(filename);
610 const IoStatus status = con2matter(m, frame, nullptr);
611 if (io_ok(status)) {
612 mirror_con_corpus(filename);
613 }
614 return status;
615 } catch (const std::exception &e) {
616 EONC_LOG_ERROR("Failed to read {}: {}", filename, e.what());
617 return IoStatus::ReadError;
618 }
619}
620
621IoStatus con2matter(Matter &m, const readcon::ConFrame &frame,
622 ConFrameMetadata *out_metadata) {
623 try {
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();
629
630 m.headerCon[0] = prebox[0] + "\n";
631 m.headerCon[1] = prebox[1] + "\n";
632 m.headerCon[3] = postbox[0] + "\n";
633 m.headerCon[4] = postbox[1] + "\n";
634
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) {
637 Matrix3d cell = Matrix3d::Zero();
638 cell(0, 0) = lengths[0];
639 cell(1, 1) = lengths[1];
640 cell(2, 2) = lengths[2];
641 m.setCell(cell);
642 } else {
643 angles[0] *= eonc::helpers::pi / 180.0;
644 angles[1] *= eonc::helpers::pi / 180.0;
645 angles[2] *= eonc::helpers::pi / 180.0;
646 const double alpha = angles[0];
647 const double beta = angles[1];
648 const double gamma = angles[2];
649
650 Matrix3d cell = Matrix3d::Zero();
651 cell(0, 0) = 1.0;
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);
656 cell(2, 2) = eonc::safemath::safe_sqrt(1.0 - pow(cell(2, 0), 2) -
657 pow(cell(2, 1), 2));
658
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];
665 m.setCell(cell);
666 }
667 m.headerCon[2] =
668 std::format("{} {} {}\n", angles_deg[0], angles_deg[1], angles_deg[2]);
669
670 const auto n = static_cast<Eigen::Index>(atoms.size());
671 m.resize(static_cast<long>(atoms.size()));
672
673 // Undo the species grouping the .con format imposes, so an index into
674 // Matter addresses the same atom as the matching row of mode.dat.
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);
679 }
680 m.setFileToMatter(std::move(file_to_matter));
681
682 AtomMatrix positions = AtomMatrix::Zero(n, 3);
683 AtomMatrix forces = 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;
689
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]});
699 m.setAtomIndex(static_cast<long>(i),
700 static_cast<std::int64_t>(atom.atom_id));
701
702 if (auto vel = atom.velocity()) {
703 any_velocity = true;
704 velocities(i, 0) = (*vel)[0];
705 velocities(i, 1) = (*vel)[1];
706 velocities(i, 2) = (*vel)[2];
707 }
708 if (auto force = atom.force()) {
709 any_force = true;
710 forces(i, 0) = (*force)[0];
711 forces(i, 1) = (*force)[1];
712 forces(i, 2) = (*force)[2];
713 }
714 }
715
716 m.setMasses(masses);
717 m.setAtomicNrs(atomic_nrs);
718 m.setPositions(positions);
719
720 if (any_velocity || frame.has_velocities()) {
721 m.setVelocities(velocities);
722 }
723
724 const auto meta = metadata_from_frame(frame);
725 if (out_metadata != nullptr) {
726 *out_metadata = meta;
727 }
728
729 // Trust file energy+forces only when both are present. Energy-only must not
730 // mark the pot clean with a zero force matrix (optimizer footgun). Prefer
731 // writing raw forces (friend) so fixed-atom components survive RT; then
732 // mark clean without setComputedPotential's net-force adjustment on zeros.
733 const bool has_force_section = any_force || frame.has_forces();
734 if (meta.energy && has_force_section) {
735 m.restoreFileForces(forces, true, *meta.energy);
736 } else if (has_force_section) {
737 m.restoreFileForces(forces, false, 0.0);
738 } else {
739 // Classic geometry-only files: always recompute pot (main-era behavior).
740 m.recomputePotential = true;
741 }
742
743 // setPositions already applied PBC when enabled; no second wrap here.
744 return IoStatus::Ok;
745 } catch (const std::exception &e) {
746 EONC_LOG_ERROR("Failed to convert frame to matter: {}", e.what());
747 return IoStatus::ReadError;
748 }
749}
750
751IoStatus matter2convel(Matter &m, std::string filename) {
752 filename = ensure_extension(std::move(filename), ".convel");
753
755
756 try {
757 auto frame = frame_from_matter(m, nullptr, /*with_velocities=*/true);
758 std::vector<readcon::ConFrame> frames;
759 frames.push_back(std::move(frame));
760 const IoStatus status = write_frames(filename, frames, kConvelPrecision);
761 if (io_ok(status)) {
762 mirror_con_corpus(filename);
763 }
764 return status;
765 } catch (const std::exception &e) {
766 EONC_LOG_ERROR("Failed to write convel {}: {}", filename, e.what());
768 }
769}
770
771IoStatus convel2matter(Matter &m, std::string filename) {
772 if (auto foreign = foreign_input(filename)) {
773 return chemfiles2matter(m, *foreign);
774 }
775 filename = ensure_extension(std::move(filename), ".convel");
776 try {
777 auto frame = readcon::read_first_frame(filename);
778 const IoStatus status = con2matter(m, frame, nullptr);
779 if (io_ok(status)) {
780 mirror_con_corpus(filename);
781 }
782 return status;
783 } catch (const std::exception &e) {
784 EONC_LOG_ERROR("Failed to read convel {}: {}", filename, e.what());
785 return IoStatus::ReadError;
786 }
787}
788
789IoStatus matter2xyz(Matter &m, std::string filename, bool append) {
790 filename = ensure_extension(std::move(filename), ".xyz");
791
793 const long n = m.numberOfAtoms();
794
795 if (append && fs::exists(filename)) {
796 std::error_code ec;
797 const auto sz = fs::file_size(filename, ec);
798 if (!ec && sz > 0) {
799 const auto prev = last_xyz_natoms(filename);
800 if (!prev) {
801 EONC_LOG_ERROR("matter2xyz: cannot parse existing {}", filename);
803 }
804 if (*prev != n) {
806 "matter2xyz: append atom count {} != last frame {} in {}", n, *prev,
807 filename);
809 }
810 }
811 }
812
813 std::ofstream out;
814 out.open(filename,
815 append ? (std::ios::out | std::ios::app | std::ios::binary)
816 : (std::ios::out | std::ios::trunc | std::ios::binary));
817 if (!out) {
818 EONC_LOG_ERROR("matter2xyz: cannot open {}", filename);
819 return IoStatus::OpenError;
820 }
821
822 // A frame header must start its own line. eOn's own frames end in a
823 // newline; a hand-written or foreign tail may not.
824 if (append && !ends_with_newline(filename)) {
825 out.put('\n');
826 }
827
828 const Matrix3d cell = m.getCell();
829 out << std::format(
830 "{}\nLattice=\"{:.17g} {:.17g} {:.17g} {:.17g} {:.17g} {:.17g} "
831 "{:.17g} {:.17g} {:.17g}\" Properties=species:S:1:pos:R:3 Generated "
832 "by eOn\n",
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));
835 const AtomMatrix pos = m.getPositions();
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),
839 pos(i, 2));
840 }
841 // Close before testing: the destructor's flush is where a full disk or a
842 // short write surfaces, and by then the state is gone.
843 out.close();
844 if (!out) {
845 EONC_LOG_ERROR("matter2xyz: failed to write {}", filename);
847 }
848 return IoStatus::Ok;
849}
850
851IoStatus writeTibble(Matter &m, std::string fname) {
852 // Debug table, not a structure format. getForces()/getPotentialEnergy()
853 // run computePotential() on a dirty pot, which would charge a dump to
854 // the force-call count in results.dat. Cached values only; the header
855 // drops the columns it cannot fill. atmID is the CON column-5 id.
856 const bool have_forces = !m.needsForceUpdate();
857 if (!have_forces) {
859 "writeTibble: pot is dirty, {} omits the force and energy columns",
860 fname);
861 }
862 const AtomMatrix pos = m.getPositions();
863 std::ofstream out(fname);
864 if (!out) {
865 EONC_LOG_ERROR("writeTibble: cannot open {}", fname);
866 return IoStatus::OpenError;
867 }
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");
870 const AtomMatrix fSys = have_forces ? m.getForces() : AtomMatrix();
871 const double eSys = have_forces ? m.getPotentialEnergy() : 0.0;
872 for (long idx = 0; idx < m.numberOfAtoms(); ++idx) {
873 out << std::format("{} {} {}", pos(idx, 0), pos(idx, 1), pos(idx, 2));
874 if (have_forces) {
875 out << std::format(" {} {} {} {}", fSys(idx, 0), fSys(idx, 1),
876 fSys(idx, 2), eSys);
877 }
878 const auto mask = m.getFixedMask(idx);
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),
882 symbol_for_z(m.getAtomicNr(idx)), m.getAtomIndex(idx),
883 fixed_bits);
884 }
885 out.close();
886 if (!out) {
887 EONC_LOG_ERROR("writeTibble: failed to write {}", fname);
889 }
890 return IoStatus::Ok;
891}
892
893std::vector<readcon::ConFrame>
894buildNebPathFrames(const std::vector<std::shared_ptr<Matter>> &path,
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());
901 return frames;
902 }
903 for (const auto &img : path) {
904 if (!img) {
905 EONC_LOG_ERROR("buildNebPathFrames: null Matter in path");
906 return {};
907 }
908 }
909
910 Matter &template_m = *path.front();
912
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);
917
918 frames.reserve(path.size());
919 try {
920 auto seed = seed_builder(template_m, prebox, postbox, atom_ids);
921 for (size_t i = 0; i < path.size(); ++i) {
922 Matter &img = *path[i];
924 if (img.numberOfAtoms() != template_m.numberOfAtoms()) {
926 "buildNebPathFrames: image {} atom count {} != template {}", i,
927 img.numberOfAtoms(), template_m.numberOfAtoms());
928 return {};
929 }
930 auto builder = seed.clone();
931 apply_frame_metadata(builder, &metadata_per_image[i]);
932 apply_geometry(builder, img, /*with_velocities=*/false,
933 &metadata_per_image[i]);
934 frames.push_back(builder.build());
935 }
936 } catch (const std::exception &e) {
937 EONC_LOG_ERROR("buildNebPathFrames failed: {}", e.what());
938 return {};
939 }
940 return frames;
941}
942
943IoStatus writeConFrames(std::string filename,
944 const std::vector<readcon::ConFrame> &frames) {
945 if (frames.empty()) {
947 }
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);
953 if (io_ok(status)) {
954 remember_stamp(key, path);
955 mirror_con_corpus(path.string());
956 } else {
957 append_stamps().erase(key);
958 }
959 return status;
960}
961
962IoStatus writeNebPath(std::string filename,
963 const std::vector<std::shared_ptr<Matter>> &path,
964 const std::vector<ConFrameMetadata> &metadata_per_image) {
965 auto frames = buildNebPathFrames(path, metadata_per_image);
966 if (frames.empty()) {
968 }
969 return writeConFrames(std::move(filename), frames);
970}
971
972} // namespace eonc::io
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Definition Eigen.h:35
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
#define EONC_LOG_ERROR(...)
Definition EonLogger.h:328
#define EONC_LOG_WARNING(...)
Definition EonLogger.h:322
nlohmann::json json
bool getWriteConForces() const noexcept
Parameters.main_options().writeConForces for this Matter.
Definition Matter.cpp:117
const AtomMatrix & getPositions() const
Definition Matter.cpp:378
long int numberOfAtoms() const
Definition Matter.cpp:343
AtomMatrix getVelocities() const
Definition Matter.cpp:819
const AtomMatrix & getForcesRaw() const
Definition Matter.cpp:493
double getPotentialEnergy() const
Definition Matter.cpp:624
bool needsForceUpdate() const
Whether forces need recomputation (positions or surface epoch changed).
Definition Matter.h:220
const std::array< std::string, 5 > & getHeaderCon() const
CON header lines (indices 0..4); public for I/O / bindings.
Definition Matter.h:328
std::int64_t getAtomIndex(long int atom) const
.con column-5 index (pre-grouping); public for I/O / bindings.
Definition Matter.cpp:959
void setFixedMask(long int atom, std::array< bool, 3 > mask)
Definition Matter.cpp:615
const AtomMatrix & getPositions() const
Definition Matter.cpp:378
void setPositions(const AtomMatrix &pos)
Definition Matter.cpp:420
void setCell(const Matrix3d &newCell)
Definition Matter.cpp:347
Matrix3d getCell() const
Definition Matter.cpp:345
void applyPeriodicBoundaryIfEnabled()
Apply MIC wrap when periodic boundaries are enabled (I/O path).
Definition Matter.h:351
long int numberOfAtoms() const
Definition Matter.cpp:343
void setAtomicNrs(const VectorXi &atmnrs)
Definition Matter.cpp:788
void setVelocities(const AtomMatrix &v)
Definition Matter.cpp:823
void setAtomIndex(long int atom, std::int64_t index)
Definition Matter.cpp:963
void resize(long int nAtoms)
Definition Matter.cpp:297
void restoreFileForces(const AtomMatrix &fileForces, bool trustEnergy, double energy)
Write .con forces without the fixed-atom mask setForces applies.
Definition Matter.cpp:967
double getMass(long int atom) const
Definition Matter.cpp:548
bool recomputePotential
Definition Matter.h:375
void setMasses(const VectorXd &massesIn)
Definition Matter.cpp:558
double getPotentialEnergy() const
Definition Matter.cpp:624
bool needsForceUpdate() const
Whether forces need recomputation (positions or surface epoch changed).
Definition Matter.h:220
std::array< std::string, 5 > headerCon
Definition Matter.h:381
long getAtomicNr(long int atom) const
Definition Matter.cpp:565
std::array< bool, 3 > getFixedMask(long int atom) const
Per-axis CON column-4 mask (bit0=x, bit1=y, bit2=z).
Definition Matter.cpp:590
std::int64_t getAtomIndex(long int atom) const
.con column-5 index (pre-grouping); public for I/O / bindings.
Definition Matter.cpp:959
const AtomMatrix & getForces() const
Definition Matter.cpp:482
void setFileToMatter(std::vector< long > map)
Definition Matter.h:325
constexpr double pi
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).
Definition ConFileIO.h:29
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
Definition ConFileIO.h:38
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)
Definition SafeMath.h:21
double safe_acos(double x)
Definition SafeMath.h:34
double safe_sqrt(double x)
Definition SafeMath.h:38
std::optional< uint64_t > frame_index
Definition ConFileIO.h:72
std::vector< double > spreads
Per-atom root-mean-square spread of the position distribution about the written coordinates,...
Definition ConFileIO.h:90
std::optional< uint64_t > neb_band
Definition ConFileIO.h:77
std::vector< ConMetadataValue > scalars
Definition ConFileIO.h:79
std::optional< double > energy
Definition ConFileIO.h:73
std::optional< double > timestep
Definition ConFileIO.h:75
std::vector< ConMetadataText > strings
Definition ConFileIO.h:80
std::optional< double > time
Definition ConFileIO.h:74
std::optional< std::string > raw_json
Definition ConFileIO.h:81
std::optional< std::string > potential_type
Definition ConFileIO.h:78
std::vector< double > displacements
Per-atom displacement, row-major N x 3 in Angstrom (a normal mode, a dimer direction).
Definition ConFileIO.h:85
std::optional< uint64_t > neb_bead
Definition ConFileIO.h:76
std::optional< bool > write_con_forces
When set, this write includes or omits force sections regardless of Parameters.main_options()....
Definition ConFileIO.h:93