Cantera  2.0
plots.cpp
Go to the documentation of this file.
1 /**
2  * @file plots.cpp
3  */
4 // Copyright 2002 California Institute of Technology
5 
6 #include "cantera/base/plots.h"
7 
8 using namespace std;
9 
10 namespace Cantera
11 {
12 
13 // Write a Plotting file
14 /*
15  * @param fname Output file name
16  * @param fmt Either TEC or XL or CSV
17  * @param plotTitle Title of the plot
18  * @param names vector of variable names
19  * @param data N x M data array.
20  * data(n,m) is the m^th value of the n^th variable.
21  */
22 void writePlotFile(const std::string& fname, const std::string& fmt,
23  const std::string& plotTitle,
24  const std::vector<std::string> &names,
25  const Array2D& data)
26 {
27  ofstream f(fname.c_str());
28  if (!f) {
29  throw CanteraError("writePlotFile","could not open file "+fname+
30  " for writing.");
31  }
32  if (fmt == "TEC") {
33  outputTEC(f, plotTitle, names, data);
34  f.close();
35  } else if (fmt == "XL" || fmt == "CSV") {
36  outputExcel(f, plotTitle, names, data);
37  f.close();
38  } else {
39  throw CanteraError("writePlotFile",
40  "unsupported plot type:" + fmt);
41  }
42 }
43 
44 
45 
46 // Write a Tecplot data file.
47 /*
48  * @param s output stream
49  * @param title plot title
50  * @param names vector of variable names
51  * @param data N x M data array.
52  * data(n,m) is the m^th value of the n^th variable.
53  */
54 void outputTEC(std::ostream& s, const std::string& title,
55  const std::vector<std::string>& names,
56  const Array2D& data)
57 {
58  int i,j;
59  int npts = static_cast<int>(data.nColumns());
60  int nv = static_cast<int>(data.nRows());
61  s << "TITLE = \"" + title + "\"" << endl;
62  s << "VARIABLES = " << endl;
63  for (i = 0; i < nv; i++) {
64  s << "\"" << names[i] << "\"" << endl;
65  }
66  s << "ZONE T=\"zone1\"" << endl;
67  s << " I=" << npts << ",J=1,K=1,F=POINT" << endl;
68  s << "DT=( ";
69  for (i = 0; i < nv; i++) {
70  s << " SINGLE";
71  }
72  s << " )" << endl;
73  for (i = 0; i < npts; i++) {
74  for (j = 0; j < nv; j++) {
75  s << data(j,i) << " ";
76  }
77  s << endl;
78  }
79 }
80 
81 
82 
83 // Write an Excel spreadsheet in 'csv' form.
84 /*
85  * @param s output stream
86  * @param title plot title
87  * @param names vector of variable names
88  * @param data N x M data array.
89  * data(n,m) is the m^th value of the n^th variable.
90  */
91 void outputExcel(std::ostream& s, const std::string& title,
92  const std::vector<std::string>& names,
93  const Array2D& data)
94 {
95  int i,j;
96  int npts = static_cast<int>(data.nColumns());
97  int nv = static_cast<int>(data.nRows());
98  s << title + "," << endl;
99  for (i = 0; i < nv; i++) {
100  s << names[i];
101  if (i != nv-1) {
102  s << ",";
103  }
104  }
105  s << endl;
106  for (i = 0; i < npts; i++) {
107  for (j = 0; j < nv; j++) {
108  s << data(j,i);
109  if (j != nv-1) {
110  s << ",";
111  }
112  }
113  s << endl;
114  }
115 }
116 
117 }