ROOT
ROOT project
Loading...
Searching...
No Matches
writer.hpp
Go to the documentation of this file.
1#ifndef ROOT_WRITER_HPP
2#define ROOT_WRITER_HPP
3
4#include <Eigen/Dense>
5#include <fstream>
6#include <iostream>
7#include <memory>
8#include <ranges>
9#include <string>
10
11#include "writer_def.hpp"
12
13template <>
15
16template <>
17Writer<Eigen::MatrixX2d>::Writer(const Eigen::MatrixX2d& vals_to_write, WritingMethod write_method,
18 std::string filename, char separator, bool overwrite) {
19 this->values = vals_to_write;
20 this->method = write_method;
21 this->separator = separator;
22 this->overwrite = overwrite;
23 this->filename = filename;
24}
25
26template <>
28 std::cout << "The found root is " << this->values.row(this->values.rows() - 1)(0) << std::endl;
29
30 std::unique_ptr<PrinterBase<Eigen::Vector2d>> printer;
31 this->build_printer(printer);
32
33 for (int i = 0; i < this->values.rows(); i++) {
34 Eigen::Vector2d row = this->values.row(i);
35 printer->write_values(row);
36 }
37
38 // LLM
39 if (auto gp = dynamic_cast<PrinterGNUPlot<Eigen::Vector2d>*>(printer.get())) {
40 gp->generate_gnuplot_script();
41 std::cout << "Gnuplot script generated: " << this->filename << ".plt\n";
42 }
43}
44
45template <typename T>
46template <typename V>
47void Writer<T>::build_printer(std::unique_ptr<PrinterBase<V>>& printer) {
48 switch (this->method) {
50 printer = std::make_unique<PrinterCLI<V>>();
51 break;
53 printer = std::make_unique<PrinterCSV<V>>(this->filename, this->separator, this->overwrite);
54 break;
56 printer = std::make_unique<PrinterDAT<V>>(this->filename, this->overwrite);
57 break;
59 printer = std::make_unique<PrinterGNUPlot<V>>(this->filename, this->overwrite);
60 break;
61 default:
62 std::cerr << "\033[31mError: Unknown writing method.\033[0m\n";
63 std::exit(EXIT_FAILURE);
64 }
65}
66
67template <typename V>
69 std::cout << "Here are the iterations of the method: " << std::endl;
70}
71
72template <>
73void PrinterCLI<Eigen::Vector2d>::write_values(const Eigen::Vector2d& value) {
74 std::cout << "x = " << value(0) << " --- f(x) = " << value(1) << std::endl;
75}
76
77template <typename V>
78PrinterFile<V>::PrinterFile(const std::string& fname, bool ow_mode) {
79 this->filename = fname;
80 this->append = ow_mode;
81 if (this->append) {
82 file.open(this->filename, std::ios::trunc);
83 } else {
84 file.open(this->filename, std::ios::app);
85 file << std::endl;
86 }
87
88 if (!file.is_open()) {
89 std::cerr << "\033[31mError: could not open file " << this->filename << " for writing.\033[0m\n";
90 std::exit(EXIT_FAILURE);
91 }
92}
93
94template <typename V>
95PrinterCSV<V>::PrinterCSV(const std::string& fname, char sep, bool ow_mode) : PrinterFile<V>(fname + ".csv", ow_mode) {
96 this->separator = sep;
97}
98
99template <>
100void PrinterCSV<Eigen::Vector2d>::write_values(const Eigen::Vector2d& value) {
101 file << value(0) << this->separator << value(1) << std::endl;
102}
103
104template <typename V>
105PrinterDAT<V>::PrinterDAT(const std::string& fname, bool ow_mode) : PrinterFile<V>(fname + ".dat", ow_mode) {}
106
107template <>
108void PrinterDAT<Eigen::Vector2d>::write_values(const Eigen::Vector2d& value) {
109 file << value(0) << " " << value(1) << std::endl;
110}
111
112template <typename V>
113PrinterGNUPlot<V>::PrinterGNUPlot(const std::string& fname, bool ow_mode) : PrinterDAT<V>(fname, ow_mode) {}
114
115template <>
117 std::string base_name = this->filename;
118 size_t last_dot = base_name.find_last_of(".");
119 if (last_dot != std::string::npos) {
120 base_name = base_name.substr(0, last_dot);
121 }
122
123 std::string png_file = base_name + ".png"; // PNG output
124 std::string plt_file = base_name + ".plt"; // Gnuplot script
125
126 std::ofstream script(plt_file);
127 if (!script.is_open()) {
128 std::cerr << "\033[31mError: could not open gnuplot script file.\033[0m\n";
129 std::exit(EXIT_FAILURE);
130 }
131 // LLM
132 script << "# Auto-generated gnuplot script\n"
133 << "set terminal pngcairo size 1000,800 enhanced font 'Arial,12'\n"
134 << "set output '" << png_file << "'\n"
135 << "set title 'Root-Finding Iterations'\n"
136 << "set xlabel 'x'\n"
137 << "set ylabel 'f(x)'\n"
138 << "set grid\n"
139 << "plot '" << this->filename
140 << "' using 1:2 with linespoints lt rgb 'blue' pt 7 lw 2 title 'Iteration Path'\n";
141
142 script.close();
143
144 // Check if gnuplot exists before calling it
145 if (std::system("which gnuplot > /dev/null 2>&1") == 0) {
146 std::system(("gnuplot " + plt_file).c_str());
147 std::cout << "Gnuplot image generated: " << png_file << std::endl;
148 } else {
149 std::cerr << "\033[33mWarning: gnuplot not found. Script generated but PNG not created.\033[0m\n";
150 std::exit(EXIT_FAILURE);
151 }
152}
153
154#endif // ROOT_WRITER_IMPL_HPP
Abstract Printer class.
Definition writer_def.hpp:69
void write_values(const V &value) override
Method to actually print a given value in the output.
PrinterCLI()
Constructor for the PrinterCLI class - just prints out a string.
Definition writer.hpp:68
PrinterCSV(const std::string &fname, char sep, bool ow_mode)
Constructor of the PrinterCSV class.
Definition writer.hpp:95
void write_values(const V &value) override
Writes a given result into the .csv file with the sotred separator.
char separator
Separator for the .csv output file, given at construction time.
Definition writer_def.hpp:134
Class to write on .dat the result - daughter of FilePrinter and Mother of GnuPlotPrinter.
Definition writer_def.hpp:115
void write_values(const V &value) override
Writes a given result into the .dat file.
PrinterDAT(const std::string &fname, bool ow_mode)
Constructor for the PrinterDAT class.
Definition writer.hpp:105
Mother class for all the Printers which print in a file.
Definition writer_def.hpp:97
PrinterFile(const std::string &fname, bool ow_mode)
The constructor for PrinterFile class.
Definition writer.hpp:78
Class Daughter of PrinterDAT to write on .dat (inherited) and produce a gnu plot for the results.
Definition writer_def.hpp:155
PrinterGNUPlot(const std::string &fname, bool ow_mode)
Constructor for the PrinterGNUPlot.
Definition writer.hpp:113
void generate_gnuplot_script() const
Calls the generation of the .png output plot.
Writer(const T &vals_to_write, WritingMethod write_method, std::string filename="output", char separator=',', bool overwrite=true)
Constructor for a Writer object.
void build_printer(std::unique_ptr< PrinterBase< V > > &printer)
Method to convert the generic Printer into a typed one for a specific output destination.
Definition writer.hpp:47
void write()
Method to run the printing loop and correctly initialize the Printer.
Contains definitions for classes Writer and Printer to Write on different output destinations.
WritingMethod
Definition writer_def.hpp:24
@ CONSOLE
Definition writer_def.hpp:24
@ CSV
Definition writer_def.hpp:24
@ DAT
Definition writer_def.hpp:24
@ GNUPLOT
Definition writer_def.hpp:24