Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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"
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
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
58void 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_),
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
112 wordify(yName),
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
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
170void 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_
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{
245 {
246 os << setw(10) << x[xi] << token::SPACE << setw(10) << y[xi]<< endl;
247 }
248}
249
250
251void 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
266void Foam::graph::write(Ostream& os, const word& format) const
267{
268 writer::New(format)().write(*this, os);
269}
270
271
272void Foam::graph::write(const fileName& pName, const word& format) const
273{
274 autoPtr<writer> writer(writer::New(format));
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);
300}
301
302
303Foam::Ostream& Foam::operator<<(Ostream& os, const graph& g)
304{
305 g.writeTable(os);
306 os.check(FUNCTION_NAME);
307 return os;
308}
309
310
311// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
scalar y
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
vtk::lineWriter writer(edgeCentres, edgeList::null(), fileName(aMesh.time().globalPath()/(vtkBaseFileName+"-edgesCentres")))
const uniformDimensionedVectorField & g
bool set(const word &key, curve *ptr)
iterator begin()
iterator set to the beginning of the HashTable
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition OSstream.H:134
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
SubField is a Field obtained as a section of another Field, without its own allocation....
Definition SubField.H:58
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A single curve in a graph.
Definition curve.H:57
A class for handling file names.
Definition fileName.H:75
Abstract base class for a graph writer.
Definition graph.H:190
void writeXY(const scalarField &x, const scalarField &y, Ostream &) const
Definition graph.C:231
static autoPtr< writer > New(const word &writeFormat)
Return a reference to the selected writer.
Definition graph.C:203
Class to create, store and output qgraph files.
Definition graph.H:56
const string & yName() const
Definition graph.H:159
const string & xName() const
Definition graph.H:154
const scalarField & y() const
Definition graph.C:137
const string & title() const
Definition graph.H:149
void write(Ostream &, const word &format) const
Write graph to stream in given format.
Definition graph.C:259
void writeTable(Ostream &) const
Write out graph data as a simple table.
Definition graph.C:244
static word wordify(const string &sname)
Helper function to convert string name into appropriate word.
Definition graph.C:40
void setXRange(const scalar x0, const scalar x1)
Definition graph.C:163
graph(const string &title, const string &xName, const string &yName, const scalarField &x)
Construct from title and labels (no curves).
Definition graph.C:75
const scalarField & x() const
Definition graph.H:165
A class for handling character strings derived from std::string.
Definition string.H:76
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
@ SPACE
Space [isspace].
Definition token.H:144
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition error.H:607
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
#define FUNCTION_NAME
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition BitOps.C:35
Namespace for OpenFOAM.
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:616
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
graph::writer graphWriter
Definition graph.C:32
Omanip< int > setw(const int i)
Definition IOmanip.H:199
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
runTime write()
word format(conversionProperties.get< word >("format"))
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition stdFoam.H:214
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235