Cantera  2.1.2
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 void writePlotFile(const std::string& fname, const std::string& fmt,
14  const std::string& plotTitle,
15  const std::vector<std::string> &names,
16  const Array2D& data)
17 {
18  ofstream f(fname.c_str());
19  if (!f) {
20  throw CanteraError("writePlotFile","could not open file "+fname+
21  " for writing.");
22  }
23  if (fmt == "TEC") {
24  outputTEC(f, plotTitle, names, data);
25  f.close();
26  } else if (fmt == "XL" || fmt == "CSV") {
27  outputExcel(f, plotTitle, names, data);
28  f.close();
29  } else {
30  throw CanteraError("writePlotFile",
31  "unsupported plot type:" + fmt);
32  }
33 }
34 
35 void outputTEC(std::ostream& s, const std::string& title,
36  const std::vector<std::string>& names,
37  const Array2D& data)
38 {
39  int i,j;
40  int npts = static_cast<int>(data.nColumns());
41  int nv = static_cast<int>(data.nRows());
42  s << "TITLE = \"" + title + "\"" << endl;
43  s << "VARIABLES = " << endl;
44  for (i = 0; i < nv; i++) {
45  s << "\"" << names[i] << "\"" << endl;
46  }
47  s << "ZONE T=\"zone1\"" << endl;
48  s << " I=" << npts << ",J=1,K=1,F=POINT" << endl;
49  s << "DT=( ";
50  for (i = 0; i < nv; i++) {
51  s << " SINGLE";
52  }
53  s << " )" << endl;
54  for (i = 0; i < npts; i++) {
55  for (j = 0; j < nv; j++) {
56  s << data(j,i) << " ";
57  }
58  s << endl;
59  }
60 }
61 
62 void outputExcel(std::ostream& s, const std::string& title,
63  const std::vector<std::string>& names,
64  const Array2D& data)
65 {
66  int i,j;
67  int npts = static_cast<int>(data.nColumns());
68  int nv = static_cast<int>(data.nRows());
69  s << title + "," << endl;
70  for (i = 0; i < nv; i++) {
71  s << names[i];
72  if (i != nv-1) {
73  s << ",";
74  }
75  }
76  s << endl;
77  for (i = 0; i < npts; i++) {
78  for (j = 0; j < nv; j++) {
79  s << data(j,i);
80  if (j != nv-1) {
81  s << ",";
82  }
83  }
84  s << endl;
85  }
86 }
87 
88 }
size_t nRows() const
Number of rows.
Definition: Array.h:317
Contains declarations for utility functions for outputing to plotting programs.
void writePlotFile(const std::string &fname, const std::string &fmt, const std::string &plotTitle, const std::vector< std::string > &names, const Array2D &data)
Write a Plotting file.
Definition: plots.cpp:13
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:33
void outputExcel(std::ostream &s, const std::string &title, const std::vector< std::string > &names, const Array2D &data)
Write an Excel spreadsheet in 'csv' form.
Definition: plots.cpp:62
size_t nColumns() const
Number of columns.
Definition: Array.h:322
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
void outputTEC(std::ostream &s, const std::string &title, const std::vector< std::string > &names, const Array2D &data)
Write a Tecplot data file.
Definition: plots.cpp:35