graph.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 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  {
141  FatalErrorIn("const scalarField& graph::y() const")
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  {
154  FatalErrorIn("scalarField& graph::y()")
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  (
172  "graph::writer::New(const word&)"
173  ) << "Graph writer table is empty"
174  << exit(FatalError);
175  }
176 
177  wordConstructorTable::iterator cstrIter =
178  wordConstructorTablePtr_->find(graphFormat);
179 
180  if (cstrIter == wordConstructorTablePtr_->end())
181  {
183  (
184  "graph::writer::New(const word&)"
185  ) << "Unknown graph format " << graphFormat
186  << endl << endl
187  << "Valid graph formats are : " << endl
188  << wordConstructorTablePtr_->sortedToc()
189  << exit(FatalError);
190  }
191 
192  return autoPtr<graph::writer>(cstrIter()());
193 }
194 
195 
197 (
198  const scalarField& x,
199  const scalarField& y,
200  Ostream& os
201 ) const
202 {
203  forAll(x, xi)
204  {
205  os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
206  }
207 }
208 
209 
211 {
212  forAll(x_, xi)
213  {
214  os << setw(10) << x_[xi];
215 
216  forAllConstIter(graph, *this, iter)
217  {
218  os << token::SPACE << setw(10) << (*iter())[xi];
219  }
220  os << endl;
221  }
222 }
223 
224 
225 void Foam::graph::write(Ostream& os, const word& format) const
226 {
227  writer::New(format)().write(*this, os);
228 }
229 
230 
231 void Foam::graph::write(const fileName& pName, const word& format) const
232 {
234 
235  OFstream graphFile(pName + '.' + graphWriter().ext());
236 
237  if (graphFile.good())
238  {
239  write(graphFile, format);
240  }
241  else
242  {
243  WarningIn("graph::write(const word& format, const fileName& dir)")
244  << "Could not open graph file " << graphFile.name()
245  << endl;
246  }
247 }
248 
249 
251 (
252  const fileName& path,
253  const word& name,
254  const word& format
255 ) const
256 {
257  mkDir(path);
258  write(path/name, format);
259 }
260 
261 
263 {
264  g.writeTable(os);
265  os.check("Ostream& operator<<(Ostream&, const graph&)");
266  return os;
267 }
268 
269 
270 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
void write(Ostream &, const word &format) const
Write graph to stream in given format.
Definition: graph.C:225
static word wordify(const string &sname)
Helper function to convert string name into appropriate word.
Definition: graph.C:44
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void writeXY(const scalarField &x, const scalarField &y, Ostream &) const
Definition: graph.C:197
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A class for handling words, derived from string.
Definition: word.H:59
A class for handling character strings derived from std::string.
Definition: string.H:74
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A single curve in a graph.
Definition: curve.H:56
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:133
timeIndices insert(timeIndex, timeDirs[timeI].value())
Namespace for OpenFOAM.
Abstract base class for a graph writer.
Definition: graph.H:184
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define WarningIn(functionName)
Report a warning using Foam::Warning.
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:118
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
const scalarField & y() const
Definition: graph.C:137
void writeTable(Ostream &) const
Write out graph data as a simple table.
Definition: graph.C:210
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
Istream and Ostream manipulators taking arguments.
Class to create, store and output qgraph files.
Definition: graph.H:58
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
void write(const fileName &path, const word &name, const word &format) const
Write graph to file in given path, name and format.
Definition: graph.C:251
const dimensionedVector & g
error FatalError
scalar y
graph::writer graphWriter
Definition: graph.C:36
A class for handling file names.
Definition: fileName.H:69
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurence of sub-string oldStr with newStr.
Definition: string.C:58
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
iterator begin()
Iterator set to the beginning of the HashTable.
Definition: HashTableI.H:419
graph(const string &title, const string &xName, const string &yName, const scalarField &x)
Construct from title and labels (no curves)
Definition: graph.C:79
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:420
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
defineTypeNameAndDebug(combustionModel, 0)
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: graph.C:164