graph.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "graph.H"
27 #include "OFstream.H"
28 #include "IOmanip.H"
29 #include "Pair.H"
30 #include "OSspecific.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
37  defineTypeNameAndDebug(graphWriter, 0);
38  defineRunTimeSelectionTable(graphWriter, word);
39 }
40 
41 
42 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
43 
45 {
46  string wname = sname;
47  wname.replace(' ', '_');
48  wname.replace('(', '_');
49  wname.replace(')', "");
50 
51  return word(wname);
52 }
53 
54 
55 void Foam::graph::readCurves(Istream& is)
56 {
57  List<xy> xyData(is);
58 
59  x_.setSize(xyData.size());
60  scalarField y(xyData.size());
61 
62  forAll(xyData, i)
63  {
64  x_[i] = xyData[i].x_;
65  y[i] = xyData[i].y_;
66  }
67 
68  insert
69  (
70  wordify(yName_),
71  new curve(wordify(yName_), curve::curveStyle::CONTINUOUS, y)
72  );
73 }
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
79 (
80  const string& title,
81  const string& xName,
82  const string& yName,
83  const scalarField& x
84 )
85 :
86  title_(title),
87  xName_(xName),
88  yName_(yName),
89  x_(x)
90 {}
91 
92 
94 (
95  const string& title,
96  const string& xName,
97  const string& yName,
98  const scalarField& x,
99  const scalarField& y
100 )
101 :
102  title_(title),
103  xName_(xName),
104  yName_(yName),
105  x_(x)
106 {
107  insert(wordify(yName), new curve(yName, curve::curveStyle::CONTINUOUS, y));
108 }
109 
110 
112 (
113  const string& title,
114  const string& xName,
115  const string& yName,
116  Istream& is
117 )
118 :
119  title_(title),
120  xName_(xName),
121  yName_(yName)
122 {
123  readCurves(is);
124 }
125 
126 
128 :
129  title_(is),
130  xName_(is),
131  yName_(is)
132 {
133  readCurves(is);
134 }
135 
136 
138 {
139  if (size() != 1)
140  {
142  << "y field requested for graph containing " << size()
143  << "ys" << exit(FatalError);
144  }
145 
146  return *begin()();
147 }
148 
149 
151 {
152  if (size() != 1)
153  {
155  << "y field requested for graph containing " << size()
156  << "ys" << exit(FatalError);
157  }
158 
159  return *begin()();
160 }
161 
162 
164 (
165  const word& graphFormat
166 )
167 {
168  if (!wordConstructorTablePtr_)
169  {
171  << "Graph writer table is empty"
172  << exit(FatalError);
173  }
174 
175  wordConstructorTable::iterator cstrIter =
176  wordConstructorTablePtr_->find(graphFormat);
177 
178  if (cstrIter == wordConstructorTablePtr_->end())
179  {
181  << "Unknown graph format " << graphFormat
182  << endl << endl
183  << "Valid graph formats are : " << endl
184  << wordConstructorTablePtr_->sortedToc()
185  << exit(FatalError);
186  }
187 
188  return autoPtr<graph::writer>(cstrIter()());
189 }
190 
191 
193 (
194  const scalarField& x,
195  const scalarField& y,
196  Ostream& os
197 ) const
198 {
199  forAll(x, xi)
200  {
201  os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
202  }
203 }
204 
205 
207 {
208  forAll(x_, xi)
209  {
210  os << setw(10) << x_[xi];
211 
212  forAllConstIter(graph, *this, iter)
213  {
214  os << token::SPACE << setw(10) << (*iter())[xi];
215  }
216  os << endl;
217  }
218 }
219 
220 
221 void Foam::graph::write(Ostream& os, const word& format) const
222 {
223  writer::New(format)().write(*this, os);
224 }
225 
226 
227 void Foam::graph::write(const fileName& pName, const word& format) const
228 {
230 
231  OFstream graphFile(pName + '.' + graphWriter().ext());
232 
233  if (graphFile.good())
234  {
235  write(graphFile, format);
236  }
237  else
238  {
240  << "Could not open graph file " << graphFile.name()
241  << endl;
242  }
243 }
244 
245 
247 (
248  const fileName& path,
249  const word& name,
250  const word& format
251 ) const
252 {
253  mkDir(path);
254  write(path/name, format);
255 }
256 
257 
259 {
260  g.writeTable(os);
261  os.check("Ostream& operator<<(Ostream&, const graph&)");
262  return os;
263 }
264 
265 
266 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A class for handling file names.
Definition: fileName.H:79
void write(Ostream &, const word &format) const
Write graph to stream in given format.
Definition: graph.C:221
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
const scalarField & y() const
Definition: graph.C:137
Output to file stream.
Definition: OFstream.H:82
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void writeTable(Ostream &) const
Write out graph data as a simple table.
Definition: graph.C:206
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:120
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
void insert(const scalar, DynamicList< floatScalar > &)
Append scalar to given DynamicList.
Abstract base class for a graph writer.
Definition: graph.H:184
Class to create, store and output qgraph files.
Definition: graph.H:58
graph::writer graphWriter
Definition: graph.C:36
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
scalar y
A class for handling words, derived from string.
Definition: word.H:59
graph(const string &title, const string &xName, const string &yName, const scalarField &x)
Construct from title and labels (no curves)
Definition: graph.C:79
void writeXY(const scalarField &x, const scalarField &y, Ostream &) const
Definition: graph.C:193
A single curve in a graph.
Definition: curve.H:56
iterator begin()
Iterator set to the beginning of the HashTable.
Definition: HashTableI.H:411
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
static word wordify(const string &sname)
Helper function to convert string name into appropriate word.
Definition: graph.C:44
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
Istream and Ostream manipulators taking arguments.
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
defineTypeNameAndDebug(combustionModel, 0)
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurrence of sub-string oldStr with newStr.
Definition: string.C:64
#define WarningInFunction
Report a warning using Foam::Warning.
Ostream & operator<<(Ostream &, const ensightPart &)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
const dimensionedVector & g
A class for handling character strings derived from std::string.
Definition: string.H:76
Namespace for OpenFOAM.
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: graph.C:164