00001 #include <fstream>
00002 #include <iostream>
00003 #include <stdio.h>
00004 #include <cstring>
00005 #include <vector>
00006
00007 #include "diff.h"
00008 #include "gnuplot.h"
00009
00010 void plotLine(std::string const& name,
00011 safevector<double> const& xx,
00012 safevector<double> const& yy,
00013 std::string const& xlabel, std::string const& ylabel,
00014 std::string const& title, std::string const& with,
00015 double ymin, double ymax,
00016 std::string const& options) throw(std::string) {
00017 if(xx.size() != yy.size())
00018 throw std::string("Array sizes must match in linePlot.");
00019
00020 if(ymin == default_axis) ymin = smallest(yy);
00021 if(ymax == default_axis) ymax = greatest(yy);
00022
00023 std::string filename = name + ".gp";
00024 std::ofstream out(filename.c_str());
00025
00026 out << "set terminal postscript eps color" << std::endl;
00027 out << "set output \"" << name << ".eps\"" << std::endl;
00028 if(xlabel.size() > 0)
00029 out << "set xlabel \"" << xlabel.c_str() << "\"" << std::endl;
00030 if(ylabel.size() > 0)
00031 out << "set ylabel \"" << ylabel << "\"" << std::endl;
00032 if(title.size() > 0)
00033 out << "set title \"" << title.c_str() << '"' << std::endl;
00034 if(options.size() > 0) out << options << std::endl;
00035 out << "set yrange [" << ymin << ":" << ymax << "]" << std::endl;
00036 out << "plot \"-\" notitle with " << with.c_str() << std::endl;
00037
00038 for(unsigned i = 0; i < xx.size(); ++i) {
00039 out << xx[i] << ", " << yy[i] << std::endl;
00040 }
00041 out << 'e' << std::endl;
00042 out << "quit" << std::endl;
00043 }
00044
00045 void plotMesh(std::string const& name,
00046 safevector<double> const& xx,
00047 safevector<double> const& yy,
00048 safevector<double> const& zz,
00049 std::string const& xlabel, std::string const& ylabel,
00050 std::string const& zlabel, std::string const& title,
00051 std::string const& options) throw(std::string) {
00052 if(xx.size() != yy.size() || xx.size() != zz.size())
00053 throw std::string("Array sizes must match in linePlot.");
00054
00055 std::string filename = name + ".gnuplot";
00056 std::ofstream out(filename.c_str());
00057
00058 out << "set terminal postscript eps color" << std::endl;
00059 out << "set output \"" << name << ".eps\"" << std::endl;
00060 out << "set grid" << std::endl;
00061 out << "set hidden" << std::endl;
00062 if(xlabel.size() > 0)
00063 out << "set xlabel \"" << xlabel.c_str() << "\"" << std::endl;
00064 if(ylabel.size() > 0)
00065 out << "set ylabel \"" << ylabel.c_str() << "\"" << std::endl;
00066 if(xlabel.size() > 0)
00067 out << "set xlabel \"" << xlabel.c_str() << "\"" << std::endl;
00068 if(title.size() > 0)
00069 out << "set title \"" << title.c_str() << '"' << std::endl;
00070 if(options.size() > 0)
00071 out << options.c_str() << std::endl;
00072 out << "splot \"-\" notitle with lines" << std::endl;
00073
00074 double last_x = xx[0];
00075 for(unsigned i = 0; i < xx.size(); ++i) {
00076 if(xx[i] != last_x) {
00077 out << std::endl;
00078 last_x = xx[i];
00079 }
00080 out << xx[i] << ", " << yy[i] << ", " << zz[i] << std::endl;
00081 }
00082 out << 'e' << std::endl;
00083 out << "quit" << std::endl;
00084 }
00085
00086 void plotTimeSeries(std::string const& name, safevector<double> const& array,
00087 std::string const& xlabel, std::string const& ylabel,
00088 std::string const& title, std::string const& with,
00089 double ymin, double ymax,
00090 int start_time, std::string const& options) throw(std::string) {
00091 safevector<double> xx(array.size());
00092 for(unsigned i = 0; i < xx.size(); ++i)
00093 xx[i] = (int)i + start_time;
00094
00095 plotLine(name, xx, array, xlabel, ylabel, title, with,
00096 ymin, ymax, options);
00097 }
00098
00099
00100 safevector <double> makeinterval(double istart, double iend, unsigned steps) {
00101 double ilen = iend-istart;
00102 double dt = ilen/steps;
00103
00104 safevector <double> res;
00105 double val = istart;
00106 res.push_back(val);
00107 for (unsigned i=0; i<=steps; i++) {
00108 val += dt;
00109 res.push_back(val);
00110 }
00111 return res;
00112 }
00113
00114
00115 #ifdef TEST
00116
00117 #include <iostream>
00118
00119 int main() {
00120 try {
00121 std::cerr << "There are no gnuplot tests, thus the module is assumed working.\n";
00122 return 0;
00123 } catch(std::string const& err) {
00124 std::cerr << err << std::endl;
00125 return 1;
00126 }
00127 }
00128
00129 #endif//TEST