plots.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifdef WIN32
00014 #pragma warning(disable:4786)
00015 #pragma warning(disable:4503)
00016 #endif
00017
00018 #include "plots.h"
00019
00020 using namespace std;
00021
00022 namespace Cantera {
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 void writePlotFile(const std::string &fname, const std::string &fmt,
00034 const std::string &plotTitle,
00035 const std::vector<std::string> &names,
00036 const Array2D& data) {
00037 ofstream f(fname.c_str());
00038 if (!f) {
00039 throw CanteraError("writePlotFile","could not open file "+fname+
00040 " for writing.");
00041 }
00042 if (fmt == "TEC") {
00043 outputTEC(f, plotTitle, names, data);
00044 f.close();
00045 }
00046 else if (fmt == "XL" || fmt == "CSV") {
00047 outputExcel(f, plotTitle, names, data);
00048 f.close();
00049 }
00050 else {
00051 throw CanteraError("writePlotFile",
00052 "unsupported plot type:" + fmt);
00053 }
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 void outputTEC(std::ostream &s, const std::string &title,
00067 const std::vector<std::string>& names,
00068 const Array2D& data) {
00069 int i,j;
00070 int npts = static_cast<int>(data.nColumns());
00071 int nv = static_cast<int>(data.nRows());
00072 s << "TITLE = \"" + title + "\"" << endl;
00073 s << "VARIABLES = " << endl;
00074 for (i = 0; i < nv; i++) {
00075 s << "\"" << names[i] << "\"" << endl;
00076 }
00077 s << "ZONE T=\"zone1\"" << endl;
00078 s << " I=" << npts << ",J=1,K=1,F=POINT" << endl;
00079 s << "DT=( ";
00080 for (i = 0; i < nv; i++) s << " SINGLE";
00081 s << " )" << endl;
00082 for (i = 0; i < npts; i++) {
00083 for (j = 0; j < nv; j++) {
00084 s << data(j,i) << " ";
00085 }
00086 s << endl;
00087 }
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 void outputExcel(std::ostream &s, const std::string &title,
00101 const std::vector<std::string>& names,
00102 const Array2D& data) {
00103 int i,j;
00104 int npts = static_cast<int>(data.nColumns());
00105 int nv = static_cast<int>(data.nRows());
00106 s << title + "," << endl;
00107 for (i = 0; i < nv; i++) {
00108 s << names[i] << ",";
00109 }
00110 s << endl;
00111 for (i = 0; i < npts; i++) {
00112 for (j = 0; j < nv; j++) {
00113 s << data(j,i) << ",";
00114 }
00115 s << endl;
00116 }
00117 }
00118
00119
00120 }