4#include "interface.hpp"
8 std::cout <<
" -> Initializing Interface ..." << std::endl;
11 if (!std::filesystem::exists(argv[1])) {
12 std::cerr <<
"[Interface] Config file not found: " << argv[1] <<
'\n';
14 m_input = toml::parse_file(argv[1]);
15 std::cout <<
"### Settings from TOML: ###\n" << m_input << std::endl;
17 catch (
const toml::parse_error& err) {
18 std::cerr <<
"[Interface] Parse error: " << err.description()
19 <<
"\n at " << err.source().begin <<
'\n';
22 throw std::runtime_error(
"Usage: <program> <config.toml>");
25 std::filesystem::path output_dir = m_input.get(
"output")->value_or(
"");
27 if (!output_dir.empty()) {
28 std::filesystem::create_directories(output_dir);
29 std::cout <<
" -> Output directory ready: " << output_dir << std::endl;
32 throw std::runtime_error(
"No output directory defined in config.toml");
35 catch (
const std::filesystem::filesystem_error& e) {
36 throw std::runtime_error(
37 "Failed to create output directory '" + output_dir.string() +
"': " + e.what()
42void IOInterface::write2csv(
const std::string& filename, Eigen::VectorXd& vector_data,
const std::string& comment)
const {
43 std::cout <<
" Writing " << filename << std::endl;
45 std::filesystem::path output_dir = m_input.get(
"output")->value_or(
"");
46 std::ofstream output_stream(output_dir / filename);
48 if (!output_stream.is_open()) {
49 throw std::runtime_error(
"Cannot open file: " + (output_dir / filename).
string());
52 output_stream <<
"# data length " << vector_data.size() << comment << std::endl;
53 output_stream << std::scientific << vector_data <<
'\n';
56IOInterface::~IOInterface() {
58 std::filesystem::path output_dir = m_input.get(
"output")->value_or(
"");
60 if (output_dir.empty()) {
61 std::cerr <<
"[Interface] No output directory, cannot write config_used.toml\n";
65 std::filesystem::path out_file = output_dir /
"config_used.toml";
66 std::ofstream os(out_file);
69 std::cerr <<
"[Interface] Failed to write config_used.toml\n";
73 os << m_input <<
"\n";
76 std::cout <<
" -> Wrote config_used.toml to " << out_file << std::endl;
78 catch (
const std::exception& e) {
79 std::cerr <<
"[Interface] Error writing config_used.toml: " << e.what() <<
"\n";
IOInterface(int argc, char *argv[])
Construct a new Interface and load the TOML configuration file.
void write2csv(const std::string &filename, const Eigen::MatrixBase< Derived > &mat_data, const std::string &comment) const
Write a matrix to a CSV file in scientific notation.