[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
gnu_out.cpp
1 #include <string>
2 #include <vector>
3 #include <iostream>
4 #include <fstream>
5 
6 #include <deal.II/base/exceptions.h>
7 
8 #include "gnu_out.h"
9 
10 namespace PHiLiP {
11 
12 namespace GridRefinement {
13 
14 template <typename real>
15 GnuFig<real>::GnuFig() : GnuFig<real>::GnuFig(DEFAULT_NAME){}
16 
17 template <typename real>
19  const std::string &name_input) :
20  name(name_input),
28 
29 template <typename real>
31  const std::string &name_input)
32 {
33  name = name_input;
34 }
35 
36 template <typename real>
38  const std::string &title_input)
39 {
40  title = title_input;
41 }
42 
43 template <typename real>
45  const std::string &xlabel_input)
46 {
47  xlabel = xlabel_input;
48 }
49 
50 template <typename real>
52  const std::string &ylabel_input)
53 {
54  ylabel = ylabel_input;
55 }
56 
57 template <typename real>
59  const bool grid_bool_input)
60 {
61  grid = grid_bool_input;
62 }
63 
64 template <typename real>
66  const bool xlog_bool_input)
67 {
68  xlog = xlog_bool_input;
69 }
70 
71 template <typename real>
73  const bool ylog_bool_input)
74 {
75  ylog = ylog_bool_input;
76 }
77 
78 template <typename real>
80  const bool legend_bool_input)
81 {
82  legend = legend_bool_input;
83 }
84 
85 template <typename real>
87  const std::vector<real> &x_data,
88  const std::vector<real> &y_data)
89 {
91  x_data,
92  y_data,
93  DEFAULT_LABEL_PREFIX + std::to_string(x_data_vec.size()));
94 }
95 
96 template <typename real>
98  const std::vector<real> &x_data,
99  const std::vector<real> &y_data,
100  const std::string & label_name)
101 {
102  Assert(x_data.size() == y_data.size(), dealii::ExcInternalError());
103 
104  x_data_vec.push_back(x_data);
105  y_data_vec.push_back(y_data);
106  label_name_vec.push_back(label_name);
107 }
108 
109 template <typename real>
111 {
112  std::ofstream gnu_out(name + ".gp");
113 
114  write_gnuplot_header(gnu_out);
115  write_gnuplot_body(gnu_out);
116  write_gnuplot_footer(gnu_out);
117 
118  gnu_out << std::flush;
119 }
120 
121 template <typename real>
123  std::ostream &out)
124 {
125  out << "# *********************************** " << '\n'
126  << "# * GNUPLOT OUTPUT FILE GENERATED * " << '\n'
127  << "# * AUTOMATICALLY BY PHiLiP LIBRARY * " << '\n'
128  << "# *********************************** " << '\n' << '\n';
129 
130  out << "set term png" << '\n' << '\n';
131 
132  out << "set output '" << name << ".png" << "'" << '\n';
133  out << "set title '" << title << "'" << '\n';
134  out << "set xlabel '" << xlabel << "'" << '\n';
135  out << "set ylabel '" << ylabel << "'" << '\n' << '\n';
136 
137  if(grid){
138  out << "set grid" << '\n';
139  }else{
140  out << "unset grid" << '\n';
141  }
142 
143  if(xlog){
144  out << "set log x" << '\n';
145  // out << "set format x '%g'" << '\n';
146  out << "set format x \"10^{%L}\"" << '\n';
147  }
148 
149  if(ylog){
150  out << "set log y" << '\n';
151  // out << "set format y '%g'" << '\n';
152  out << "set format y \"10^{%L}\"" << '\n';
153  }
154 
155  if(legend){
156  out << "set key" << '\n';
157  }else{
158  out << "unset key" << '\n';
159  }
160 
161  out << '\n';
162 }
163 
164 template <typename real>
166  std::ostream &out)
167 {
168  Assert(x_data_vec.size() == y_data_vec.size(), dealii::ExcInternalError());
169  Assert(x_data_vec.size() == label_name_vec.size(), dealii::ExcInternalError());
170 
171  out << "plot ";
172 
173  for(unsigned int i = 0; i < x_data_vec.size(); ++i){
174  if(i > 0)
175  out << ", \\" << '\n' << " ";
176 
177  std::string dat_filename = name+ "_" + label_name_vec[i] + ".dat";
178  std::ofstream dat_out(dat_filename);
179 
180  write_xy_data(dat_out, i);
181 
182  out << "'" << dat_filename << "' with linespoint";
183 
184  if(legend)
185  out << " title \"" << label_name_vec[i] << "\"";
186 
187  }
188  out << '\n' << '\n';
189 }
190 
191 template <typename real>
193  std::ostream &/*out*/)
194 {
195  // out << "quit" << '\n' << '\n';
196 }
197 
198 template <typename real>
200  std::ostream & out,
201  const unsigned int data_id)
202 {
203  Assert(x_data_vec[data_id].size() == y_data_vec[data_id].size(), dealii::ExcInternalError());
204  for(unsigned int i = 0; i < x_data_vec[data_id].size(); ++i)
205  out << x_data_vec[data_id][i] << '\t' << y_data_vec[data_id][i] << '\t' << '\n';
206 }
207 
208 template <typename real>
210 {
211 #if ENABLE_GNUPLOT
212  int ret = std::system(("gnuplot \"" + name + ".gp\"").c_str());
213  (void) ret;
214 #else
215  std::cout << "Note: gnuplot not availible. Set ENABLE_GNUPLOT to automatically run \""
216  << name << ".gp\"" << std::endl;
217 #endif
218 }
219 
220 template class GnuFig <double>;
221 
222 } // namespace GridRefinement
223 
224 } // namespace PHiLiP
void set_y_scale_log(const bool ylog_bool_input)
Set flag for logarithmic y-axis.
Definition: gnu_out.cpp:72
bool ylog
Flag for enabling y-axis logarithimic scale.
Definition: gnu_out.h:122
bool grid
Flag for enabling grid line visibility.
Definition: gnu_out.h:120
GnuFig()
Constructor with no name specified.
Definition: gnu_out.cpp:15
const std::string DEFAULT_TITLE
Default figure title.
Definition: gnu_out.h:103
void set_x_label(const std::string &xlabel_input)
Sets the x-axis label.
Definition: gnu_out.cpp:44
bool legend
Flag for enabling legend visibility.
Definition: gnu_out.h:123
void set_y_label(const std::string &ylabel_input)
Sets the y-axis label.
Definition: gnu_out.cpp:51
const std::string DEFAULT_YLABEL
deafult figure y-axis label
Definition: gnu_out.h:105
std::string ylabel
Figure y-axis label.
Definition: gnu_out.h:119
void set_name(const std::string &name_input)
Sets the file output name (without extension)
Definition: gnu_out.cpp:30
std::string title
Figure title.
Definition: gnu_out.h:117
Gnuplot utility class.
Definition: gnu_out.h:16
const bool DEFAULT_XLOG
Default flag for enabling x-axis logarithimic scale.
Definition: gnu_out.h:107
void set_legend(const bool legend_bool_input)
Sets display visibility of figure legend.
Definition: gnu_out.cpp:79
Files for the baseline physics.
Definition: ADTypes.hpp:10
void write_gnuplot_header(std::ostream &out)
Write the figure formatting header based on settings.
Definition: gnu_out.cpp:122
const bool DEFAULT_YLOG
Default flag for enabling y-axis logarithimic sclae.
Definition: gnu_out.h:108
const std::string DEFAULT_XLABEL
default figure x-axis label
Definition: gnu_out.h:104
void add_xy_data(const std::vector< real > &x_data, const std::vector< real > &y_data)
Adds 2D x vs. y data to be plotted (default legend label)
Definition: gnu_out.cpp:86
std::string name
File name (no extension)
Definition: gnu_out.h:116
void write_xy_data(std::ostream &out, const unsigned int data_id)
Write the i^th data entry to .dat file.
Definition: gnu_out.cpp:199
void exec_gnuplot()
Executes the gnuplot file.
Definition: gnu_out.cpp:209
const std::string DEFAULT_LABEL_PREFIX
Default legend data name prefix (followed by data_id)
Definition: gnu_out.h:112
std::vector< std::vector< real > > y_data_vec
Data entries y-component.
Definition: gnu_out.h:127
std::vector< std::string > label_name_vec
Data entries label names.
Definition: gnu_out.h:128
std::string xlabel
Figure x-axis label.
Definition: gnu_out.h:118
void write_gnuplot()
Main write function call.
Definition: gnu_out.cpp:110
const bool DEFAULT_GRID
Default flag for enabling grid line visibility.
Definition: gnu_out.h:106
std::vector< std::vector< real > > x_data_vec
Data entries x-component.
Definition: gnu_out.h:126
bool xlog
Flag for enabling x-axis logarithimic scale.
Definition: gnu_out.h:121
void set_grid(const bool grid_bool_input)
Set flag for enabling background grid.
Definition: gnu_out.cpp:58
void write_gnuplot_footer(std::ostream &out)
Writes the figure footer.
Definition: gnu_out.cpp:192
void set_x_scale_log(const bool xlog_bool_input)
Set flag for logarithmic x-axis.
Definition: gnu_out.cpp:65
const bool DEFAULT_LEGEND
Default flag for enabling legend visibility.
Definition: gnu_out.h:109
void set_title(const std::string &title_input)
Sets the figure title.
Definition: gnu_out.cpp:37
void write_gnuplot_body(std::ostream &out)
Writes the figure body including performing data outputs.
Definition: gnu_out.cpp:165