SheafSystem  0.0.0.0
plot.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for class plot
19 
20 #include "SheafSystem/plot.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/std_iostream.h"
24 #include "SheafSystem/std_sstream.h"
25 #include "SheafSystem/std_string.h"
26 
27 using namespace std;
28 
29 // ===========================================================
30 // ANY FACET
31 // ===========================================================
32 
33 
34 
36 bool
38 invariant() const
39 {
40  bool result = true;
41 
42  // Exit
43 
44  return result;
45 }
46 
47 
48 
49 // ===========================================================
50 // PLOT FACET
51 // ===========================================================
52 
55 plot(const char* xdriver,
56  const char** xcurve_labels,
57  int xnum_curves,
58  const char* xxlabel,
59  const char* xylabel,
60  const plot_type* xtypes)
61 {
62 
63  // Preconditions:
64 
65  require(xnum_curves > 0);
66  require(unexecutable("length of xcurve_labels == xnum_curves"));
67  require(unexecutable("xtypes == 0 || size of xtypes == xnum_curves"));
68 
69  // Body:
70 
71  // Set the class variables
72 
73  _num_curves = xnum_curves;
74 
75  // Determine the plot types. If no input types are given, treat all
76  // plot types as quadratic.
77 
78  bool lhas_quadratic = false;
79  plot_type* ltypes = new plot_type[_num_curves];
80  for(int i = 0; i < _num_curves; i++)
81  {
82  if(xtypes == 0)
83  {
84  ltypes[i] = QUADRATIC;
85  lhas_quadratic = true;
86  }
87  else
88  {
89  ltypes[i] = xtypes[i];
90  if(ltypes[i] == QUADRATIC)
91  lhas_quadratic = true;
92  }
93  }
94 
95  // Write file.
96 
97  stringstream ldata_file_name_stream;
98  ldata_file_name_stream << xdriver << ".dat";
99 
100  string ldata_file_name;
101  ldata_file_name_stream >> ldata_file_name;
102 
103  _data_file = new ofstream(ldata_file_name.c_str());
104 
105  // Create the gnuplot file
106 
107  stringstream lplot_file_name_stream;
108  lplot_file_name_stream << xdriver << ".gnu";
109 
110  string lplot_file_name;
111  lplot_file_name_stream >> lplot_file_name;
112 
113  ofstream lplot_file(lplot_file_name.c_str());
114 
115  lplot_file << "set xlabel \"" << xxlabel << "\"" << endl;
116  lplot_file << "set ylabel \"" << xylabel << "\"" << endl;
117  lplot_file << "set key bottom horizontal outside box" << endl;
118  lplot_file << "set title \"" << xdriver << "\"" << endl;
119  lplot_file << "FIT_LIMIT = 1e-6" << endl << endl;
120 
121  // Create the functions
122 
123  lplot_file << "set fit logfile \"" << xdriver
124  << ".fit\" noerrorvariables" << endl;
125 
126  for(int i = 0; i < _num_curves; i++)
127  {
128  switch(ltypes[i])
129  {
130  case QUADRATIC:
131  lplot_file << "f" << i << "(x) = a" << i << "*x**2 + b"
132  << i << "*x + c" << i << endl;
133 
134  lplot_file << "fit f" << i << "(x) \"" << ldata_file_name
135  << "\" using 1:" << i+2 << " via a" << i << ", b" << i
136  << ", c" << i << endl;
137  break;
138 
139  case LINEAR:
140  lplot_file << "f" << i << "(x) = a" << i << "*x + b" << i << endl;
141 
142  lplot_file << "fit f" << i << "(x) \"" << ldata_file_name
143  << "\" using 1:" << i+2 << " via a" << i << ", b" << i << endl;
144  break;
145 
146  case CONSTANT:
147  lplot_file << "f" << i << "(x) = a" << i << endl;
148 
149  lplot_file << "fit f" << i << "(x) \"" << ldata_file_name
150  << "\" using 1:" << i+2 << " via a" << i << endl;
151  break;
152  }
153  }
154 
155  // Plot the data
156 
157  lplot_file << endl << "plot ";
158 
159  for(int i = 0; i < _num_curves; i++)
160  {
161  lplot_file << "\"" << ldata_file_name << "\" u 1:" << i+2
162  << " t \"" << xcurve_labels[i] << "\" with points, f"
163  << i << "(x)";
164 
165  if(i+1 < _num_curves)
166  lplot_file << ", ";
167  }
168 
169  lplot_file << endl << endl;
170 
171  // Save image
172 
173  lplot_file << "set terminal png size 800,600" << endl;
174  lplot_file << "set output \"" << xdriver << ".png\"" << endl;
175  lplot_file << "replot" << endl << endl;
176 
177  // Save pdf
178 
179  lplot_file << "set terminal pdf" << endl;
180  lplot_file << "set output \"" << xdriver << ".pdf\"" << endl;
181  lplot_file << "replot" << endl << endl;
182 
183  // Save report html file
184 
185  lplot_file << "set print \"" << xdriver << ".html\"" << endl;
186  lplot_file << "print \"<html><title>" << xdriver << "</title><body>\"" << endl;
187  lplot_file << "print \"<img src=\\\"" << xdriver << ".png\\\""
188  << ">\"" << endl;
189  lplot_file << "print \"<br><br>\"" << endl << endl;
190 
191  for(int i = 0; i < _num_curves; i++)
192  {
193  switch(ltypes[i])
194  {
195  case QUADRATIC:
196  lplot_file << "if(a" << i
197  << ">1e-6) print \"<font size=-1 color=red>\"; else "
198  << "print \"<font size=-1>\"" << endl;
199 
200  lplot_file << "print \"f" << i << "(x) = (\", a" << i
201  << ", \")*x^2 + (\", b" << i << ", \")*x + (\", c"
202  << i << ", \")<br></font>\"" << endl << endl;
203 
204  break;
205 
206  case LINEAR:
207  lplot_file << "print \"<font size=-1>\"" << endl;
208 
209  lplot_file << "print \"f" << i << "(x) = (\", a"
210  << i << ", \")*x + (\", b"
211  << i << ", \")<br></font>\"" << endl << endl;
212 
213  break;
214 
215  case CONSTANT:
216  lplot_file << "print \"<font size=-1>\"" << endl;
217 
218  lplot_file << "print \"f" << i << "(x) = (\", a"
219  << i << ", \")<br></font>\"" << endl << endl;
220 
221  break;
222  }
223  }
224 
225  lplot_file << "print \"</body></html>\"" << endl << endl;
226 
227  // Test for quadratic behavior
228 
229  if(lhas_quadratic)
230  {
231  lplot_file << "set print" << endl;
232  lplot_file << "if(";
233  for(int i = 0; i < _num_curves; i++)
234  {
235  if(ltypes[i] == QUADRATIC)
236  {
237  lplot_file << "a" << i << ">1e-6";
238 
239  if(i+1 < _num_curves)
240  lplot_file << " || ";
241  }
242  }
243 
244  lplot_file << ") print \"\\nWARNING: Quadratic behavior!\"" << endl;
245  }
246 
247  // Clean up.
248 
249  delete [] ltypes;
250 
251  // Postconditions:
252 
253  ensure(invariant());
254 
255  // Exit:
256 
257  return;
258 }
259 
263 {
264 
265  // Preconditions:
266 
267 
268  // Body:
269 
270  delete _data_file;
271 
272  // Postconditions:
273 
274  // Exit:
275 
276  return;
277 }
278 
280 double*
283 {
284  // Preconditions:
285 
286  // Body:
287 
288  double* result = new double[_num_curves+1];
289 
290  // Postconditions:
291 
292  ensure(invariant());
293  ensure(unexecutable("length of result == _num_curves+1"));
294 
295  // Exit:
296 
297  return result;
298 }
299 
301 void
303 add_point(double* xpoint)
304 {
305  // Preconditions:
306 
307  require(unexecutable("length of xpoint == _num_curves+1"));
308 
309  // Body:
310 
311  for(int i = 0; i < _num_curves+1; i++)
312  {
313  *_data_file << xpoint[i] << " ";
314  }
315 
316  *_data_file << endl;
317 
318  // Postconditions:
319 
320  ensure(invariant());
321 
322  // Exit:
323 
324  return;
325 }
326 
327 // ===========================================================
328 // NON-MEMBER FUNCTIONS
329 // ===========================================================
330 
plot_type
Type of curve to fit.
Definition: plot.h:60
STL namespace.
double * create_point()
An empty array for point data.
Definition: plot.cc:282
plot(const char *xdriver, const char **xcurve_labels, int xnum_curves=1, const char *xxlabel="", const char *xylabel="", const plot_type *xtypes=0)
Constructor.
Definition: plot.cc:55
void add_point(double *xpoint)
Add a data point.
Definition: plot.cc:303
virtual ~plot()
Destructor.
Definition: plot.cc:262
virtual bool invariant() const
Class invariant.
Definition: plot.cc:38