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 "Pair.H"
33 #include "OSspecific.H"
34 #include "SubField.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
43 }
44 
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
49 {
50  string wname = sname;
51  wname.replace(" ", "_");
52  wname.replace("(", "_");
53  wname.replace(")", "");
54 
55  return word(wname);
56 }
57 
58 
59 void Foam::graph::readCurves(Istream& is)
60 {
61  List<xy> xyData(is);
62 
63  x_.setSize(xyData.size());
64  scalarField y(xyData.size());
65 
66  forAll(xyData, i)
67  {
68  x_[i] = xyData[i].x_;
69  y[i] = xyData[i].y_;
70  }
71 
72  set
73  (
74  wordify(yName_),
75  new curve(wordify(yName_), curve::curveStyle::CONTINUOUS, y)
76  );
77 }
78 
79 
80 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 
83 (
84  const string& title,
85  const string& xName,
86  const string& yName,
87  const scalarField& x
88 )
89 :
90  title_(title),
91  xName_(xName),
92  yName_(yName),
93  x_(x)
94 {}
95 
96 
98 (
99  const string& title,
100  const string& xName,
101  const string& yName,
102  const scalarField& x,
103  const scalarField& y
104 )
105 :
106  title_(title),
107  xName_(xName),
108  yName_(yName),
109  x_(x)
110 {
111  set
112  (
113  wordify(yName),
115  );
116 }
117 
118 
120 (
121  const string& title,
122  const string& xName,
123  const string& yName,
124  Istream& is
125 )
126 :
127  title_(title),
128  xName_(xName),
129  yName_(yName)
130 {
131  readCurves(is);
132 }
133 
134 
136 :
137  title_(is),
138  xName_(is),
139  yName_(is)
140 {
141  readCurves(is);
142 }
143 
144 
146 {
147  if (size() != 1)
148  {
150  << "y field requested for graph containing " << size()
151  << "ys" << exit(FatalError);
152  }
153 
154  return *begin()();
155 }
156 
157 
159 {
160  if (size() != 1)
161  {
163  << "y field requested for graph containing " << size()
164  << "ys" << exit(FatalError);
165  }
166 
167  return *begin()();
168 }
169 
170 
171 void Foam::graph::setXRange(const scalar x0, const scalar x1)
172 {
173  if (x1 < x0)
174  {
176  << "When setting limits, x1 must be greater than x0" << nl
177  << " x0: " << x0 << nl
178  << " x1: " << x1 << nl
179  << abort(FatalError);
180  }
181 
182  label i0 = 0;
183  label i1 = 0;
184 
185  forAll(x_, i)
186  {
187  if (x_[i] < x0)
188  {
189  i0 = i + 1;
190  }
191  if (x_[i] < x1)
192  {
193  i1 = i;
194  }
195  }
196 
197  label nX = i1 - i0 + 1;
198  scalarField xNew(SubField<scalar>(x_, nX, i0));
199  x_.transfer(xNew);
200 
201  forAllIters(*this, iter)
202  {
203  curve* c = iter();
204  scalarField cNew(SubField<scalar>(*c, nX, i0));
205  c->transfer(cNew);
206  }
207 }
208 
209 
211 (
212  const word& graphFormat
213 )
214 {
215  if (!wordConstructorTablePtr_)
216  {
218  << "Graph writer table is empty"
219  << exit(FatalError);
220  }
221 
222  auto* ctorPtr = wordConstructorTable(graphFormat);
223 
224  if (!ctorPtr)
225  {
227  (
228  "graph",
229  graphFormat,
230  *wordConstructorTablePtr_
231  ) << exit(FatalError);
232  }
233 
234  return autoPtr<graph::writer>(ctorPtr());
235 }
236 
237 
239 (
240  const scalarField& x,
241  const scalarField& y,
242  Ostream& os
243 ) const
244 {
245  forAll(x, xi)
246  {
247  os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
248  }
249 }
250 
251 
253 {
254  forAll(x_, xi)
255  {
256  os << setw(10) << x_[xi];
257 
258  forAllConstIters(*this, iter)
259  {
260  os << token::SPACE << setw(10) << (*iter())[xi];
261  }
262  os << endl;
263  }
264 }
265 
266 
268 {
269  writer::New(format)().write(*this, os);
270 }
271 
272 
273 void Foam::graph::write(const fileName& pName, const word& format) const
274 {
276 
277  OFstream graphFile(pName + '.' + graphWriter().ext());
278 
279  if (graphFile.good())
280  {
281  write(graphFile, format);
282  }
283  else
284  {
286  << "Could not open graph file " << graphFile.name()
287  << endl;
288  }
289 }
290 
291 
293 (
294  const fileName& path,
295  const word& name,
296  const word& format
297 ) const
298 {
299  mkDir(path);
300  write(path/name, format);
301 }
302 
303 
305 {
306  g.writeTable(os);
308  return os;
309 }
310 
311 
312 // ************************************************************************* //
Foam::string::replace
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:108
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::graph
Class to create, store and output qgraph files.
Definition: graph.H:61
Foam::graph::y
const scalarField & y() const
Definition: graph.C:145
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
SubField.H
Foam::OFstream::name
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: OSstream.H:107
Foam::graph::wordify
static word wordify(const string &sname)
Helper function to convert string name into appropriate word.
Definition: graph.C:48
Foam::graph::setXRange
void setXRange(const scalar x0, const scalar x1)
Definition: graph.C:171
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Foam::graph::graph
graph(const string &title, const string &xName, const string &yName, const scalarField &x)
Construct from title and labels (no curves)
Definition: graph.C:83
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Pair.H
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::graph::writer
Abstract base class for a graph writer.
Definition: graph.H:190
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
OFstream.H
Foam::graphWriter
graph::writer graphWriter
Definition: graph.C:40
Foam::SubField
SubField is a Field obtained as a section of another Field, without its own allocation....
Definition: Field.H:64
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
format
word format(conversionProperties.get< word >("format"))
Foam::graph::writeTable
void writeTable(Ostream &) const
Write out graph data as a simple table.
Definition: graph.C:252
Foam::Field< scalar >
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
IOmanip.H
Istream and Ostream manipulators taking arguments.
forAllIters
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:223
Foam::graph::write
void write(Ostream &, const word &format) const
Write graph to stream in given format.
Definition: graph.C:267
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
Foam::FatalError
error FatalError
FatalErrorInLookup
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:457
os
OBJstream os(runTime.globalPath()/outputName)
g
const uniformDimensionedVectorField & g
Definition: createFluidFields.H:26
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::curve::curveStyle::CONTINUOUS
Definition: curve.H:74
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::graph::writer::New
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: graph.C:211
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
Foam::graph::writer::writeXY
void writeXY(const scalarField &x, const scalarField &y, Ostream &) const
Definition: graph.C:239
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
x
x
Definition: LISASMDCalcMethod2.H:52
graph.H
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::curve
A single curve in a graph.
Definition: curve.H:58
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
y
scalar y
Definition: LISASMDCalcMethod1.H:14
Foam::writer::New
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition: writer.C:38