#include "readcon-core.hpp"
#include <iostream>
#include <vector>
void print_frame_details(int frame_number, const readcon::ConFrame &frame) {
std::cout << "\n==================== FRAME " << frame_number
<< " ====================\n";
auto pre_headers = frame.prebox_header();
std::cout << "Pre-box Header 1: \"" << pre_headers[0] << "\"\n";
std::cout << "Pre-box Header 2: \"" << pre_headers[1] << "\"\n";
auto cell = frame.cell();
auto angles = frame.angles();
std::cout << "Cell Dimensions: " << cell[0] << ", " << cell[1] << ", "
<< cell[2] << std::endl;
std::cout << "Cell Angles: " << angles[0] << ", " << angles[1] << ", "
<< angles[2] << std::endl;
auto post_headers = frame.postbox_header();
std::cout << "Post-box Header 1:\"" << post_headers[0] << "\"\n";
std::cout << "Post-box Header 2:\"" << post_headers[1] << "\"\n";
std::cout << "Has Velocities: " << std::boolalpha << frame.has_velocities()
<< std::endl;
auto atoms = frame.atoms(); std::cout << "--- Atoms (" << atoms.size() << ") ---\n";
int atoms_to_print = 0;
for (const auto &atom : atoms) {
if (atoms_to_print >= 5) {
std::cout << "... and " << atoms.size() - 5 << " more."
<< std::endl;
break;
}
const auto fixed = atom.fixed_mask();
std::cout << " ID: " << atom.atom_id << ", Z: " << atom.atomic_number
<< ", Pos: (" << atom.x << ", " << atom.y << ", " << atom.z
<< ")"
<< ", Fixed: (" << fixed[0] << ", " << fixed[1] << ", "
<< fixed[2] << ")";
if (auto v = atom.velocity()) {
std::cout << ", Vel: (" << (*v)[0] << ", " << (*v)[1] << ", "
<< (*v)[2] << ")";
}
std::cout << std::endl;
atoms_to_print++;
}
}
static int smoke_section_buffers(const readcon::ConFrame &frame) {
using readcon::RKRStatus;
using readcon::RKR_STATUS_SUCCESS;
using readcon::RKR_STATUS_SECTION_ABSENT;
const std::size_t n = frame.atom_count();
if (n == 0) {
std::cerr << "section smoke: atom_count == 0\n";
return 1;
}
std::vector<double> pos(n * 3);
RKRStatus st = frame.copy_positions(pos.data(), pos.size());
if (st != RKR_STATUS_SUCCESS) {
std::cerr << "copy_positions failed: " << static_cast<int>(st) << "\n";
return 1;
}
std::vector<double> vel(n * 3);
st = frame.copy_velocities(vel.data(), vel.size());
if (st != RKR_STATUS_SUCCESS && st != RKR_STATUS_SECTION_ABSENT) {
std::cerr << "copy_velocities unexpected: " << static_cast<int>(st) << "\n";
return 1;
}
std::vector<double> frc(n * 3);
st = frame.copy_forces(frc.data(), frc.size());
if (st != RKR_STATUS_SUCCESS && st != RKR_STATUS_SECTION_ABSENT) {
std::cerr << "copy_forces unexpected: " << static_cast<int>(st) << "\n";
return 1;
}
std::vector<double> mass(n);
st = frame.copy_masses(mass.data(), mass.size());
if (st != RKR_STATUS_SUCCESS && st != RKR_STATUS_SECTION_ABSENT) {
std::cerr << "copy_masses unexpected: " << static_cast<int>(st) << "\n";
return 1;
}
std::cout << "section_buffers ok natoms=" << n
<< " pos0=(" << pos[0] << "," << pos[1] << "," << pos[2] << ")\n";
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 2 || argc > 3) {
std::cerr << "Usage: " << argv[0] << " <input.con> [output.con]"
<< std::endl;
return 1;
}
std::string input_filename = argv[1];
try {
if (argc == 2) {
std::cout << "Mode: Read-only. Iterating lazily through frames in: "
<< input_filename << std::endl;
readcon::ConFrameIterator frame_iterator(input_filename);
int frame_count = 0;
for (const auto &frame : frame_iterator) {
frame_count++;
print_frame_details(frame_count, frame);
if (frame_count == 1) {
if (smoke_section_buffers(frame) != 0) {
return 2;
}
}
}
std::cout
<< "\n==================================================\n";
std::cout << "Iteration complete. Total frames processed: "
<< frame_count << std::endl;
}
else { std::string output_filename = argv[2];
std::cout << "Mode: Read-Write. Reading from '" << input_filename
<< "' and writing to '" << output_filename << "'."
<< std::endl;
readcon::ConFrameIterator frame_iterator(input_filename);
std::vector<readcon::ConFrame> all_frames;
for (auto &&frame : frame_iterator) {
all_frames.push_back(std::move(frame));
}
if (all_frames.empty()) {
std::cout << "No valid frames found to write." << std::endl;
} else {
print_frame_details(all_frames.size(), all_frames.back());
std::cout << "\nWriting " << all_frames.size()
<< " frames...\n";
readcon::ConFrameWriter writer(output_filename);
writer.extend(all_frames);
std::cout << "Successfully wrote all frames." << std::endl;
}
}
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}