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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2019-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "graph.H"
30 #include "OFstream.H"
31 #include "IOmanip.H"
32 #include "OSspecific.H"
33 #include "SubField.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39  typedef graph::writer graphWriter;
42 }
43 
44 
45 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
46 
48 {
49  string wname = sname;
50  wname.replace(" ", "_");
51  wname.replace("(", "_");
52  wname.replace(")", "");
53 
54  return word(wname);
55 }
56 
57 
58 void Foam::graph::readCurves(Istream& is)
59 {
60  List<xy> xyData(is);
61 
62  x_.setSize(xyData.size());
63  scalarField y(xyData.size());
64 
65  forAll(xyData, i)
66  {
67  x_[i] = xyData[i].x_;
68  y[i] = xyData[i].y_;
69  }
70 
71  set
72  (
73  wordify(yName_),
74  new curve(wordify(yName_), curve::curveStyle::CONTINUOUS, y)
75  );
76 }
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 (
83  const string& title,
84  const string& xName,
85  const string& yName,
86  const scalarField& x
87 )
88 :
89  title_(title),
90  xName_(xName),
91  yName_(yName),
92  x_(x)
93 {}
94 
95 
97 (
98  const string& title,
99  const string& xName,
100  const string& yName,
101  const scalarField& x,
102  const scalarField& y
103 )
104 :
105  title_(title),
106  xName_(xName),
107  yName_(yName),
108  x_(x)
109 {
110  set
111  (
114  );
115 }
116 
117 
119 (
120  const string& title,
121  const string& xName,
122  const string& yName,
123  Istream& is
124 )
125 :
126  title_(title),
127  xName_(xName),
128  yName_(yName)
129 {
130  readCurves(is);
131 }
132 
133 
135 :
136  title_(is),
137  xName_(is),
138  yName_(is)
139 {
140  readCurves(is);
141 }
142 
143 
144 const Foam::scalarField& Foam::graph::y() const
145 {
146  if (size() != 1)
147  {
149  << "y field requested for graph containing " << size()
150  << "ys" << exit(FatalError);
151  }
152 
153  return *begin()();
154 }
155 
156 
158 {
159  if (size() != 1)
160  {
162  << "y field requested for graph containing " << size()
163  << "ys" << exit(FatalError);
164  }
165 
166  return *begin()();
167 }
168 
169 
170 void Foam::graph::setXRange(const scalar x0, const scalar x1)
171 {
172  if (x1 < x0)
173  {
175  << "When setting limits, x1 must be greater than x0" << nl
176  << " x0: " << x0 << nl
177  << " x1: " << x1 << nl
178  << abort(FatalError);
179  }
180 
181  label i0 = 0;
182  label i1 = 0;
183 
184  forAll(x_, i)
185  {
186  if (x_[i] < x0)
187  {
188  i0 = i + 1;
189  }
190  if (x_[i] < x1)
191  {
192  i1 = i;
193  }
194  }
195 
196  label nX = i1 - i0 + 1;
197  scalarField xNew(SubField<scalar>(x_, nX, i0));
198  x_.transfer(xNew);
199 
200  forAllIters(*this, iter)
201  {
202  curve* c = iter();
203  scalarField cNew(SubField<scalar>(*c, nX, i0));
204  c->transfer(cNew);
205  }
206 }
207 
208 
210 (
211  const word& graphFormat
212 )
213 {
214  if (!wordConstructorTablePtr_)
215  {
217  << "Graph writer table is empty"
218  << exit(FatalError);
219  }
220 
221  auto* ctorPtr = wordConstructorTable(graphFormat);
222 
223  if (!ctorPtr)
224  {
226  (
227  "graph",
228  graphFormat,
229  *wordConstructorTablePtr_
230  ) << exit(FatalError);
231  }
232 
233  return autoPtr<graph::writer>(ctorPtr());
234 }
235 
236 
238 (
239  const scalarField& x,
240  const scalarField& y,
241  Ostream& os
242 ) const
243 {
244  forAll(x, xi)
245  {
246  os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
247  }
248 }
249 
250 
251 void Foam::graph::writeTable(Ostream& os) const
252 {
253  forAll(x_, xi)
254  {
255  os << setw(10) << x_[xi];
256 
257  forAllConstIters(*this, iter)
258  {
259  os << token::SPACE << setw(10) << (*iter())[xi];
260  }
261  os << endl;
262  }
263 }
264 
266 void Foam::graph::write(Ostream& os, const word& format) const
267 {
268  writer::New(format)().write(*this, os);
269 }
270 
271 
272 void Foam::graph::write(const fileName& pName, const word& format) const
273 {
275 
276  OFstream graphFile(pName + '.' + writer().ext());
277 
278  if (graphFile.good())
279  {
280  write(graphFile, format);
281  }
282  else
283  {
285  << "Could not open graph file " << graphFile.name()
286  << endl;
287  }
288 }
289 
290 
292 (
293  const fileName& path,
294  const word& name,
295  const word& format
296 ) const
297 {
298  mkDir(path);
299  write(path/name, format);
300 }
301 
302 
303 Foam::Ostream& Foam::operator<<(Ostream& os, const graph& g)
304 {
305  g.writeTable(os);
307  return os;
308 }
309 
310 
311 // ************************************************************************* //
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Replace first occurrence of sub-string s1 with s2, beginning at pos.
Definition: string.C:101
A class for handling file names.
Definition: fileName.H:72
void write(Ostream &, const word &format) const
Write graph to stream in given format.
Definition: graph.C:259
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
vtk::lineWriter writer(edgeCentres, edgeList::null(), fileName(aMesh.time().globalPath()/"finiteArea-edgesCentres"))
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
const scalarField & y() const
Definition: graph.C:137
Output to file stream, using an OSstream.
Definition: OFstream.H:49
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:605
void writeTable(Ostream &) const
Write out graph data as a simple table.
Definition: graph.C:244
Abstract base class for a graph writer.
Definition: graph.H:189
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
graph::writer graphWriter
Definition: graph.C:32
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
scalar y
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:614
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
void setXRange(const scalar x0, const scalar x1)
Definition: graph.C:163
A class for handling words, derived from Foam::string.
Definition: word.H:63
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Space [isspace].
Definition: token.H:131
graph(const string &title, const string &xName, const string &yName, const scalarField &x)
Construct from title and labels (no curves)
Definition: graph.C:75
void writeXY(const scalarField &x, const scalarField &y, Ostream &) const
Definition: graph.C:231
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:336
A single curve in a graph.
Definition: curve.H:54
errorManip< error > abort(error &err)
Definition: errorManip.H:139
static word wordify(const string &sname)
Helper function to convert string name into appropriate word.
Definition: graph.C:40
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const uniformDimensionedVectorField & g
Istream and Ostream manipulators taking arguments.
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
defineTypeNameAndDebug(combustionModel, 0)
word format(conversionProperties.get< word >("format"))
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
#define WarningInFunction
Report a warning using Foam::Warning.
const dimensionedScalar c
Speed of light in a vacuum.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168
A class for handling character strings derived from std::string.
Definition: string.H:72
const string & yName() const
Definition: graph.H:159
Namespace for OpenFOAM.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: graph.C:203