54 template <
typename Derived>
56 const Eigen::MatrixBase<Derived>& mat_data,
57 const std::string& comment)
const {
59 std::filesystem::path output_dir = m_input.get(
"output")->value_or(
"");
61 std::cout <<
" Writing " << filename << std::endl;
63 std::ofstream output_stream(output_dir / filename);
64 if (!output_stream.is_open()) {
65 throw std::runtime_error(
"Cannot open file: " +
66 (output_dir / filename).
string());
69 output_stream << std::scientific;
70 const int rows = mat_data.rows();
71 const int cols = mat_data.cols();
72 output_stream <<
"# Data size (" << rows <<
", " << cols <<
")" << comment << std::endl;
74 for (
int i = 0; i < rows; ++i) {
75 for (
int j = 0; j < cols; ++j) {
76 output_stream << mat_data(i, j);
77 if (j < cols - 1) output_stream <<
' ';
79 output_stream <<
'\n';
82 void write2csv(
const std::string& filename, Eigen::VectorXd& vector_data,
const std::string& comment)
const;
84 template <
typename VecT>
85 VecT load_vector(
const std::string& toml_path)
const
87 using Scalar =
typename VecT::Scalar;
90 auto arr = m_input.at_path(toml_path).as_array();
92 throw std::runtime_error(
"Missing " + toml_path +
" array in TOML file.");
95 const std::size_t toml_size = arr->size();
100 if constexpr (VecT::SizeAtCompileTime == Eigen::Dynamic) {
101 vec.resize(toml_size);
103 if (toml_size != VecT::SizeAtCompileTime) {
104 throw std::runtime_error(
105 "Size mismatch for " + toml_path +
106 ": expected " + std::to_string(VecT::SizeAtCompileTime) +
107 ", got " + std::to_string(toml_size)
113 for (std::size_t i = 0; i < toml_size; ++i) {
114 vec(i) =
static_cast<Scalar
>(arr->at(i).value_or(Scalar{}));
133 template <
typename T>
137 auto node = m_input.at_path(toml_path);
140 throw std::runtime_error(
"Missing scalar value at path: " + toml_path);
144 auto val = node.value<T>();
145 if (!val.has_value()) {
146 throw std::runtime_error(
147 "Type mismatch or invalid value at path: " + toml_path
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.