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"
34
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_),
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 (
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_
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
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
265
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
304{
305 g.writeTable(os);
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::internalMeshWriter writer(topoMesh, topoCells, vtk::formatType::INLINE_ASCII, runTime.path()/"blockTopology")
const uniformDimensionedVectorField & g
bool set(const word &key, curve *ptr)
Assign a new entry, overwriting existing entries.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Output to file stream, using an OSstream.
Definition: OFstream.H:57
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: OSstream.H:107
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
SubField is a Field obtained as a section of another Field, without its own allocation....
Definition: SubField.H:62
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A single curve in a graph.
Definition: curve.H:60
A class for handling file names.
Definition: fileName.H:76
virtual bool write()
Write the output fields.
Abstract base class for a graph writer.
Definition: graph.H:186
void writeXY(const scalarField &x, const scalarField &y, Ostream &) const
Definition: graph.C:238
Class to create, store and output qgraph files.
Definition: graph.H:62
const string & yName() const
Definition: graph.H:157
const scalarField & y() const
Definition: graph.C:144
void writeTable(Ostream &) const
Write out graph data as a simple table.
Definition: graph.C:251
static word wordify(const string &sname)
Helper function to convert string name into appropriate word.
Definition: graph.C:47
void setXRange(const scalar x0, const scalar x1)
Definition: graph.C:170
A class for handling character strings derived from std::string.
Definition: string.H:79
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:108
@ SPACE
Space [isspace].
Definition: token.H:125
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:457
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
#define FUNCTION_NAME
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: MSwindows.C:515
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
graph::writer graphWriter
Definition: graph.C:39
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
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:333
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:260
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278