Cantera  2.5.1
plots.cpp
Go to the documentation of this file.
1 /**
2  * @file plots.cpp
3  */
4 
5 // This file is part of Cantera. See License.txt in the top-level directory or
6 // at https://cantera.org/license.txt for license and copyright information.
7 
8 #include "cantera/base/plots.h"
9 
10 using namespace std;
11 
12 namespace Cantera
13 {
14 
15 void writePlotFile(const std::string& fname, const std::string& fmt,
16  const std::string& plotTitle,
17  const std::vector<std::string> &names,
18  const Array2D& data)
19 {
20  ofstream f(fname);
21  if (!f) {
22  throw CanteraError("writePlotFile","could not open file "+fname+
23  " for writing.");
24  }
25  if (fmt == "TEC") {
26  outputTEC(f, plotTitle, names, data);
27  } else if (fmt == "XL" || fmt == "CSV") {
28  outputExcel(f, plotTitle, names, data);
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  s << "TITLE = \"" + title + "\"" << endl;
40  s << "VARIABLES = " << endl;
41  for (size_t i = 0; i < data.nRows(); i++) {
42  s << "\"" << names[i] << "\"" << endl;
43  }
44  s << "ZONE T=\"zone1\"" << endl;
45  s << " I=" << data.nColumns() << ",J=1,K=1,F=POINT" << endl;
46  s << "DT=( ";
47  for (size_t i = 0; i < data.nRows(); i++) {
48  s << " SINGLE";
49  }
50  s << " )" << endl;
51  for (size_t i = 0; i < data.nColumns(); i++) {
52  for (size_t j = 0; j < data.nRows(); j++) {
53  s << data(j,i) << " ";
54  }
55  s << endl;
56  }
57 }
58 
59 void outputExcel(std::ostream& s, const std::string& title,
60  const std::vector<std::string>& names,
61  const Array2D& data)
62 {
63  if (!title.empty()) {
64  s << title + "," << endl;
65  }
66  for (size_t i = 0; i < data.nRows(); i++) {
67  s << names[i];
68  if (i != data.nRows()-1) {
69  s << ",";
70  }
71  }
72  s << endl;
73  for (size_t i = 0; i < data.nColumns(); i++) {
74  for (size_t j = 0; j < data.nRows(); j++) {
75  s << data(j,i);
76  if (j != data.nRows()-1) {
77  s << ",";
78  }
79  }
80  s << endl;
81  }
82 }
83 
84 }
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:32
size_t nRows() const
Number of rows.
Definition: Array.h:247
size_t nColumns() const
Number of columns.
Definition: Array.h:252
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
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
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:15
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:59
Versions 6.2.0 and 6.2.1 of fmtlib do not include this define before they include windows....
Definition: fmt.h:35
Contains declarations for utility functions for outputing to plotting programs.