| 1 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | // Author: Kristian Lytje |
| 3 | |
| 4 | #include <io/detail/trajectory/XYZWriter.h> |
| 5 | #include <data/Molecule.h> |
| 6 | #include <data/Body.h> |
| 7 | #include <io/File.h> |
| 8 | #include <utility/Exceptions.h> |
| 9 | #include <utility/Console.h> |
| 10 | |
| 11 | #include <iomanip> |
| 12 | |
| 13 | using namespace ausaxs::io::detail::xyz; |
| 14 | |
| 15 | XYZWriter::XYZWriter(const io::File& path) : path(path) { |
| 16 | path.directory().create(); |
| 17 | file.open(path); |
| 18 | if (!file.is_open()) { |
| 19 | throw except::io_error("io::XYZWriter: Could not open file " + path.str()); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | XYZWriter::~XYZWriter() { |
| 24 | file.close(); |
| 25 | console::print_info("Trajectory written to " + path); |
| 26 | } |
| 27 | |
| 28 | void XYZWriter::write_frame(observer_ptr<const data::Molecule> protein) { |
| 29 | static unsigned int frame = 0; |
| 30 | std::vector<data::AtomFF> atoms; |
| 31 | atoms.reserve(protein->size_atom()); |
| 32 | for (const auto& body : protein->get_bodies()) { |
| 33 | auto bsym = body.symmetry().explicit_structure(); |
| 34 | atoms.insert(atoms.end(), bsym.atoms.begin(), bsym.atoms.end()); |
| 35 | } |
| 36 | file << " " << atoms.size() << std::endl; |
| 37 | file << " Frame " << frame++ << std::endl; |
| 38 | int i = 0; |
| 39 | for (const auto& atom : atoms) { |
| 40 | file << std::setw(10) << i++ << " " |
| 41 | << std::setw(10) << std::setprecision(6) << atom.coordinates().x() << " " |
| 42 | << std::setw(10) << std::setprecision(6) << atom.coordinates().y() << " " |
| 43 | << std::setw(10) << std::setprecision(6) << atom.coordinates().z() << std::endl; |
| 44 | } |
| 45 | } |